Skip to content

Commit 114c69f

Browse files
Suneel GarapatiAndi Shyti
authored andcommitted
i2c: thunderx: Clock divisor logic changes
Handle changes to clock divisor logic for OcteonTX2 SoC family using subsystem ID and using default reference clock source as 100MHz. Signed-off-by: Suneel Garapati <sgarapati@marvell.com> Signed-off-by: Piyush Malgujar <pmalgujar@marvell.com> Acked-by: Andi Shyti <andi.shyti@kernel.org> Signed-off-by: Andi Shyti <andi.shyti@kernel.org>
1 parent 29914da commit 114c69f

3 files changed

Lines changed: 59 additions & 4 deletions

File tree

drivers/i2c/busses/i2c-octeon-core.c

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@
1717
#include <linux/interrupt.h>
1818
#include <linux/kernel.h>
1919
#include <linux/module.h>
20+
#include <linux/pci.h>
2021

2122
#include "i2c-octeon-core.h"
2223

24+
#define INITIAL_DELTA_HZ 1000000
25+
#define TWSI_MASTER_CLK_REG_DEF_VAL 0x18
26+
#define TWSI_MASTER_CLK_REG_OTX2_VAL 0x3
27+
2328
/* interrupt service routine */
2429
irqreturn_t octeon_i2c_isr(int irq, void *dev_id)
2530
{
@@ -658,31 +663,57 @@ int octeon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
658663
void octeon_i2c_set_clock(struct octeon_i2c *i2c)
659664
{
660665
int tclk, thp_base, inc, thp_idx, mdiv_idx, ndiv_idx, foscl, diff;
661-
int thp = 0x18, mdiv = 2, ndiv = 0, delta_hz = 1000000;
666+
bool is_plat_otx2;
667+
unsigned int mdiv_min = 2;
668+
/*
669+
* Find divisors to produce target frequency, start with large delta
670+
* to cover wider range of divisors, note thp = TCLK half period.
671+
*/
672+
unsigned int thp = TWSI_MASTER_CLK_REG_DEF_VAL, mdiv = 2, ndiv = 0;
673+
unsigned int delta_hz = INITIAL_DELTA_HZ;
674+
675+
is_plat_otx2 = octeon_i2c_is_otx2(to_pci_dev(i2c->dev));
676+
677+
if (is_plat_otx2) {
678+
thp = TWSI_MASTER_CLK_REG_OTX2_VAL;
679+
mdiv_min = 0;
680+
}
662681

663682
for (ndiv_idx = 0; ndiv_idx < 8 && delta_hz != 0; ndiv_idx++) {
664683
/*
665684
* An mdiv value of less than 2 seems to not work well
666685
* with ds1337 RTCs, so we constrain it to larger values.
667686
*/
668-
for (mdiv_idx = 15; mdiv_idx >= 2 && delta_hz != 0; mdiv_idx--) {
687+
for (mdiv_idx = 15; mdiv_idx >= mdiv_min && delta_hz != 0; mdiv_idx--) {
669688
/*
670689
* For given ndiv and mdiv values check the
671690
* two closest thp values.
672691
*/
673692
tclk = i2c->twsi_freq * (mdiv_idx + 1) * 10;
674693
tclk *= (1 << ndiv_idx);
675-
thp_base = (i2c->sys_freq / (tclk * 2)) - 1;
694+
if (is_plat_otx2)
695+
thp_base = (i2c->sys_freq / tclk) - 2;
696+
else
697+
thp_base = (i2c->sys_freq / (tclk * 2)) - 1;
676698

677699
for (inc = 0; inc <= 1; inc++) {
678700
thp_idx = thp_base + inc;
679701
if (thp_idx < 5 || thp_idx > 0xff)
680702
continue;
681703

682-
foscl = i2c->sys_freq / (2 * (thp_idx + 1));
704+
if (is_plat_otx2)
705+
foscl = i2c->sys_freq / (thp_idx + 2);
706+
else
707+
foscl = i2c->sys_freq /
708+
(2 * (thp_idx + 1));
683709
foscl = foscl / (1 << ndiv_idx);
684710
foscl = foscl / (mdiv_idx + 1) / 10;
685711
diff = abs(foscl - i2c->twsi_freq);
712+
/*
713+
* Diff holds difference between calculated frequency
714+
* value vs desired frequency.
715+
* Delta_hz is updated with last minimum diff.
716+
*/
686717
if (diff < delta_hz) {
687718
delta_hz = diff;
688719
thp = thp_idx;

drivers/i2c/busses/i2c-octeon-core.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
22
#include <linux/atomic.h>
3+
#include <linux/bitfield.h>
34
#include <linux/clk.h>
45
#include <linux/delay.h>
56
#include <linux/device.h>
67
#include <linux/i2c.h>
78
#include <linux/i2c-smbus.h>
89
#include <linux/io.h>
910
#include <linux/kernel.h>
11+
#include <linux/pci.h>
1012

1113
/* Controller command patterns */
1214
#define SW_TWSI_V BIT_ULL(63) /* Valid bit */
@@ -211,6 +213,21 @@ static inline void octeon_i2c_write_int(struct octeon_i2c *i2c, u64 data)
211213
octeon_i2c_writeq_flush(data, i2c->twsi_base + TWSI_INT(i2c));
212214
}
213215

216+
#define PCI_SUBSYS_DEVID_9XXX 0xB
217+
#define PCI_SUBSYS_MASK GENMASK(15, 12)
218+
/**
219+
* octeon_i2c_is_otx2 - check for chip ID
220+
* @pdev: PCI dev structure
221+
*
222+
* Returns true if the device is an OcteonTX2, false otherwise.
223+
*/
224+
static inline bool octeon_i2c_is_otx2(struct pci_dev *pdev)
225+
{
226+
u32 chip_id = FIELD_GET(PCI_SUBSYS_MASK, pdev->subsystem_device);
227+
228+
return (chip_id == PCI_SUBSYS_DEVID_9XXX);
229+
}
230+
214231
/* Prototypes */
215232
irqreturn_t octeon_i2c_isr(int irq, void *dev_id);
216233
int octeon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num);

drivers/i2c/busses/i2c-thunderx-pcidrv.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#define PCI_DEVICE_ID_THUNDER_TWSI 0xa012
2929

3030
#define SYS_FREQ_DEFAULT 700000000
31+
#define OTX2_REF_FREQ_DEFAULT 100000000
3132

3233
#define TWSI_INT_ENA_W1C 0x1028
3334
#define TWSI_INT_ENA_W1S 0x1030
@@ -205,6 +206,12 @@ static int thunder_i2c_probe_pci(struct pci_dev *pdev,
205206
if (ret)
206207
goto error;
207208

209+
/*
210+
* For OcteonTX2 chips, set reference frequency to 100MHz
211+
* as refclk_src in TWSI_MODE register defaults to 100MHz.
212+
*/
213+
if (octeon_i2c_is_otx2(pdev))
214+
i2c->sys_freq = OTX2_REF_FREQ_DEFAULT;
208215
octeon_i2c_set_clock(i2c);
209216

210217
i2c->adap = thunderx_i2c_ops;

0 commit comments

Comments
 (0)