1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * i2c-au1550.c: SMBus (i2c) adapter for Alchemy PSC interface
4 * Copyright (C) 2004 Embedded Edge, LLC <dan@embeddededge.com>
6 * 2.6 port by Matt Porter <mporter@kernel.crashing.org>
8 * The documentation describes this as an SMBus controller, but it doesn't
9 * understand any of the SMBus protocol in hardware. It's really an I2C
10 * controller that could emulate most of the SMBus in software.
12 * This is just a skeleton adapter to use with the Au1550 PSC
13 * algorithm. It was developed for the Pb1550, but will work with
14 * any Au1550 board that has a similar PSC configuration.
17 #include <linux/delay.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/errno.h>
22 #include <linux/i2c.h>
23 #include <linux/slab.h>
25 #include <asm/mach-au1x00/au1000.h>
26 #include <asm/mach-au1x00/au1xxx_psc.h>
30 #define PSC_SMBCFG 0x08
31 #define PSC_SMBMSK 0x0C
32 #define PSC_SMBPCR 0x10
33 #define PSC_SMBSTAT 0x14
34 #define PSC_SMBEVNT 0x18
35 #define PSC_SMBTXRX 0x1C
36 #define PSC_SMBTMR 0x20
38 struct i2c_au1550_data
{
39 void __iomem
*psc_base
;
41 struct i2c_adapter adap
;
44 static inline void WR(struct i2c_au1550_data
*a
, int r
, unsigned long v
)
46 __raw_writel(v
, a
->psc_base
+ r
);
50 static inline unsigned long RD(struct i2c_au1550_data
*a
, int r
)
52 return __raw_readl(a
->psc_base
+ r
);
55 static int wait_xfer_done(struct i2c_au1550_data
*adap
)
59 /* Wait for Tx Buffer Empty */
60 for (i
= 0; i
< adap
->xfer_timeout
; i
++) {
61 if (RD(adap
, PSC_SMBSTAT
) & PSC_SMBSTAT_TE
)
70 static int wait_ack(struct i2c_au1550_data
*adap
)
74 if (wait_xfer_done(adap
))
77 stat
= RD(adap
, PSC_SMBEVNT
);
78 if ((stat
& (PSC_SMBEVNT_DN
| PSC_SMBEVNT_AN
| PSC_SMBEVNT_AL
)) != 0)
84 static int wait_master_done(struct i2c_au1550_data
*adap
)
88 /* Wait for Master Done. */
89 for (i
= 0; i
< 2 * adap
->xfer_timeout
; i
++) {
90 if ((RD(adap
, PSC_SMBEVNT
) & PSC_SMBEVNT_MD
) != 0)
99 do_address(struct i2c_au1550_data
*adap
, unsigned int addr
, int rd
, int q
)
103 /* Reset the FIFOs, clear events. */
104 stat
= RD(adap
, PSC_SMBSTAT
);
105 WR(adap
, PSC_SMBEVNT
, PSC_SMBEVNT_ALLCLR
);
107 if (!(stat
& PSC_SMBSTAT_TE
) || !(stat
& PSC_SMBSTAT_RE
)) {
108 WR(adap
, PSC_SMBPCR
, PSC_SMBPCR_DC
);
109 while ((RD(adap
, PSC_SMBPCR
) & PSC_SMBPCR_DC
) != 0)
114 /* Write out the i2c chip address and specify operation */
119 /* zero-byte xfers stop immediately */
121 addr
|= PSC_SMBTXRX_STP
;
123 /* Put byte into fifo, start up master. */
124 WR(adap
, PSC_SMBTXRX
, addr
);
125 WR(adap
, PSC_SMBPCR
, PSC_SMBPCR_MS
);
128 return (q
) ? wait_master_done(adap
) : 0;
131 static int wait_for_rx_byte(struct i2c_au1550_data
*adap
, unsigned char *out
)
135 if (wait_xfer_done(adap
))
138 j
= adap
->xfer_timeout
* 100;
144 if ((RD(adap
, PSC_SMBSTAT
) & PSC_SMBSTAT_RE
) == 0)
150 *out
= RD(adap
, PSC_SMBTXRX
);
155 static int i2c_read(struct i2c_au1550_data
*adap
, unsigned char *buf
,
163 /* A read is performed by stuffing the transmit fifo with
164 * zero bytes for timing, waiting for bytes to appear in the
165 * receive fifo, then reading the bytes.
168 while (i
< (len
- 1)) {
169 WR(adap
, PSC_SMBTXRX
, 0);
170 if (wait_for_rx_byte(adap
, &buf
[i
]))
176 /* The last byte has to indicate transfer done. */
177 WR(adap
, PSC_SMBTXRX
, PSC_SMBTXRX_STP
);
178 if (wait_master_done(adap
))
181 buf
[i
] = (unsigned char)(RD(adap
, PSC_SMBTXRX
) & 0xff);
185 static int i2c_write(struct i2c_au1550_data
*adap
, unsigned char *buf
,
195 while (i
< (len
-1)) {
197 WR(adap
, PSC_SMBTXRX
, data
);
203 /* The last byte has to indicate transfer done. */
205 data
|= PSC_SMBTXRX_STP
;
206 WR(adap
, PSC_SMBTXRX
, data
);
207 if (wait_master_done(adap
))
213 au1550_xfer(struct i2c_adapter
*i2c_adap
, struct i2c_msg
*msgs
, int num
)
215 struct i2c_au1550_data
*adap
= i2c_adap
->algo_data
;
219 WR(adap
, PSC_CTRL
, PSC_CTRL_ENABLE
);
221 for (i
= 0; !err
&& i
< num
; i
++) {
223 err
= do_address(adap
, p
->addr
, p
->flags
& I2C_M_RD
,
227 if (p
->flags
& I2C_M_RD
)
228 err
= i2c_read(adap
, p
->buf
, p
->len
);
230 err
= i2c_write(adap
, p
->buf
, p
->len
);
233 /* Return the number of messages processed, or the error code.
238 WR(adap
, PSC_CTRL
, PSC_CTRL_SUSPEND
);
243 static u32
au1550_func(struct i2c_adapter
*adap
)
245 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
248 static const struct i2c_algorithm au1550_algo
= {
249 .master_xfer
= au1550_xfer
,
250 .functionality
= au1550_func
,
253 static void i2c_au1550_setup(struct i2c_au1550_data
*priv
)
257 WR(priv
, PSC_CTRL
, PSC_CTRL_DISABLE
);
258 WR(priv
, PSC_SEL
, PSC_SEL_PS_SMBUSMODE
);
259 WR(priv
, PSC_SMBCFG
, 0);
260 WR(priv
, PSC_CTRL
, PSC_CTRL_ENABLE
);
261 while ((RD(priv
, PSC_SMBSTAT
) & PSC_SMBSTAT_SR
) == 0)
264 cfg
= PSC_SMBCFG_RT_FIFO8
| PSC_SMBCFG_TT_FIFO8
| PSC_SMBCFG_DD_DISABLE
;
265 WR(priv
, PSC_SMBCFG
, cfg
);
267 /* Divide by 8 to get a 6.25 MHz clock. The later protocol
268 * timings are based on this clock.
270 cfg
|= PSC_SMBCFG_SET_DIV(PSC_SMBCFG_DIV8
);
271 WR(priv
, PSC_SMBCFG
, cfg
);
272 WR(priv
, PSC_SMBMSK
, PSC_SMBMSK_ALLMASK
);
274 /* Set the protocol timer values. See Table 71 in the
275 * Au1550 Data Book for standard timing values.
277 WR(priv
, PSC_SMBTMR
, PSC_SMBTMR_SET_TH(0) | PSC_SMBTMR_SET_PS(20) | \
278 PSC_SMBTMR_SET_PU(20) | PSC_SMBTMR_SET_SH(20) | \
279 PSC_SMBTMR_SET_SU(20) | PSC_SMBTMR_SET_CL(20) | \
280 PSC_SMBTMR_SET_CH(20));
282 cfg
|= PSC_SMBCFG_DE_ENABLE
;
283 WR(priv
, PSC_SMBCFG
, cfg
);
284 while ((RD(priv
, PSC_SMBSTAT
) & PSC_SMBSTAT_SR
) == 0)
287 WR(priv
, PSC_CTRL
, PSC_CTRL_SUSPEND
);
290 static void i2c_au1550_disable(struct i2c_au1550_data
*priv
)
292 WR(priv
, PSC_SMBCFG
, 0);
293 WR(priv
, PSC_CTRL
, PSC_CTRL_DISABLE
);
297 * registering functions to load algorithms at runtime
298 * Prior to calling us, the 50MHz clock frequency and routing
299 * must have been set up for the PSC indicated by the adapter.
302 i2c_au1550_probe(struct platform_device
*pdev
)
304 struct i2c_au1550_data
*priv
;
308 priv
= devm_kzalloc(&pdev
->dev
, sizeof(struct i2c_au1550_data
),
313 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
314 priv
->psc_base
= devm_ioremap_resource(&pdev
->dev
, r
);
315 if (IS_ERR(priv
->psc_base
))
316 return PTR_ERR(priv
->psc_base
);
318 priv
->xfer_timeout
= 200;
320 priv
->adap
.nr
= pdev
->id
;
321 priv
->adap
.algo
= &au1550_algo
;
322 priv
->adap
.algo_data
= priv
;
323 priv
->adap
.dev
.parent
= &pdev
->dev
;
324 strlcpy(priv
->adap
.name
, "Au1xxx PSC I2C", sizeof(priv
->adap
.name
));
326 /* Now, set up the PSC for SMBus PIO mode. */
327 i2c_au1550_setup(priv
);
329 ret
= i2c_add_numbered_adapter(&priv
->adap
);
331 i2c_au1550_disable(priv
);
335 platform_set_drvdata(pdev
, priv
);
339 static int i2c_au1550_remove(struct platform_device
*pdev
)
341 struct i2c_au1550_data
*priv
= platform_get_drvdata(pdev
);
343 i2c_del_adapter(&priv
->adap
);
344 i2c_au1550_disable(priv
);
349 static int i2c_au1550_suspend(struct device
*dev
)
351 struct i2c_au1550_data
*priv
= dev_get_drvdata(dev
);
353 i2c_au1550_disable(priv
);
358 static int i2c_au1550_resume(struct device
*dev
)
360 struct i2c_au1550_data
*priv
= dev_get_drvdata(dev
);
362 i2c_au1550_setup(priv
);
367 static const struct dev_pm_ops i2c_au1550_pmops
= {
368 .suspend
= i2c_au1550_suspend
,
369 .resume
= i2c_au1550_resume
,
372 #define AU1XPSC_SMBUS_PMOPS (&i2c_au1550_pmops)
375 #define AU1XPSC_SMBUS_PMOPS NULL
378 static struct platform_driver au1xpsc_smbus_driver
= {
380 .name
= "au1xpsc_smbus",
381 .pm
= AU1XPSC_SMBUS_PMOPS
,
383 .probe
= i2c_au1550_probe
,
384 .remove
= i2c_au1550_remove
,
387 module_platform_driver(au1xpsc_smbus_driver
);
389 MODULE_AUTHOR("Dan Malek, Embedded Edge, LLC.");
390 MODULE_DESCRIPTION("SMBus adapter Alchemy pb1550");
391 MODULE_LICENSE("GPL");
392 MODULE_ALIAS("platform:au1xpsc_smbus");