2 * i2c-au1550.c: SMBus (i2c) adapter for Alchemy PSC interface
7 * The documentation describes this as an SMBus controller, but it doesn't
8 * understand any of the SMBus protocol in hardware. It's really an I2C
9 * controller that could emulate most of the SMBus in software.
11 * This is just a skeleton adapter to use with the Au1550 PSC
12 * algorithm. It was developed for the Pb1550, but will work with
13 * any Au1550 board that has a similar PSC configuration.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
26 #include <linux/delay.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/platform_device.h>
30 #include <linux/errno.h>
31 #include <linux/i2c.h>
32 #include <linux/slab.h>
34 #include <asm/mach-au1x00/au1000.h>
35 #include <asm/mach-au1x00/au1xxx_psc.h>
39 #define PSC_SMBCFG 0x08
40 #define PSC_SMBMSK 0x0C
41 #define PSC_SMBPCR 0x10
42 #define PSC_SMBSTAT 0x14
43 #define PSC_SMBEVNT 0x18
44 #define PSC_SMBTXRX 0x1C
45 #define PSC_SMBTMR 0x20
47 struct i2c_au1550_data {
48 void __iomem *psc_base;
50 struct i2c_adapter adap;
53 static inline void WR(struct i2c_au1550_data *a, int r, unsigned long v)
55 __raw_writel(v, a->psc_base + r);
59 static inline unsigned long RD(struct i2c_au1550_data *a, int r)
61 return __raw_readl(a->psc_base + r);
64 static int wait_xfer_done(struct i2c_au1550_data *adap)
68 /* Wait for Tx Buffer Empty */
69 for (i = 0; i < adap->xfer_timeout; i++) {
70 if (RD(adap, PSC_SMBSTAT) & PSC_SMBSTAT_TE)
79 static int wait_ack(struct i2c_au1550_data *adap)
83 if (wait_xfer_done(adap))
86 stat = RD(adap, PSC_SMBEVNT);
87 if ((stat & (PSC_SMBEVNT_DN | PSC_SMBEVNT_AN | PSC_SMBEVNT_AL)) != 0)
93 static int wait_master_done(struct i2c_au1550_data *adap)
97 /* Wait for Master Done. */
98 for (i = 0; i < 2 * adap->xfer_timeout; i++) {
99 if ((RD(adap, PSC_SMBEVNT) & PSC_SMBEVNT_MD) != 0)
108 do_address(struct i2c_au1550_data *adap, unsigned int addr, int rd, int q)
112 /* Reset the FIFOs, clear events. */
113 stat = RD(adap, PSC_SMBSTAT);
114 WR(adap, PSC_SMBEVNT, PSC_SMBEVNT_ALLCLR);
116 if (!(stat & PSC_SMBSTAT_TE) || !(stat & PSC_SMBSTAT_RE)) {
117 WR(adap, PSC_SMBPCR, PSC_SMBPCR_DC);
118 while ((RD(adap, PSC_SMBPCR) & PSC_SMBPCR_DC) != 0)
123 /* Write out the i2c chip address and specify operation */
128 /* zero-byte xfers stop immediately */
130 addr |= PSC_SMBTXRX_STP;
132 /* Put byte into fifo, start up master. */
133 WR(adap, PSC_SMBTXRX, addr);
134 WR(adap, PSC_SMBPCR, PSC_SMBPCR_MS);
137 return (q) ? wait_master_done(adap) : 0;
140 static int wait_for_rx_byte(struct i2c_au1550_data *adap, unsigned char *out)
144 if (wait_xfer_done(adap))
147 j = adap->xfer_timeout * 100;
153 if ((RD(adap, PSC_SMBSTAT) & PSC_SMBSTAT_RE) == 0)
159 *out = RD(adap, PSC_SMBTXRX);
164 static int i2c_read(struct i2c_au1550_data *adap, unsigned char *buf,
172 /* A read is performed by stuffing the transmit fifo with
173 * zero bytes for timing, waiting for bytes to appear in the
174 * receive fifo, then reading the bytes.
177 while (i < (len - 1)) {
178 WR(adap, PSC_SMBTXRX, 0);
179 if (wait_for_rx_byte(adap, &buf[i]))
185 /* The last byte has to indicate transfer done. */
186 WR(adap, PSC_SMBTXRX, PSC_SMBTXRX_STP);
187 if (wait_master_done(adap))
190 buf[i] = (unsigned char)(RD(adap, PSC_SMBTXRX) & 0xff);
194 static int i2c_write(struct i2c_au1550_data *adap, unsigned char *buf,
204 while (i < (len-1)) {
206 WR(adap, PSC_SMBTXRX, data);
212 /* The last byte has to indicate transfer done. */
214 data |= PSC_SMBTXRX_STP;
215 WR(adap, PSC_SMBTXRX, data);
216 if (wait_master_done(adap))
222 au1550_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
224 struct i2c_au1550_data *adap = i2c_adap->algo_data;
228 WR(adap, PSC_CTRL, PSC_CTRL_ENABLE);
230 for (i = 0; !err && i < num; i++) {
232 err = do_address(adap, p->addr, p->flags & I2C_M_RD,
236 if (p->flags & I2C_M_RD)
237 err = i2c_read(adap, p->buf, p->len);
239 err = i2c_write(adap, p->buf, p->len);
242 /* Return the number of messages processed, or the error code.
247 WR(adap, PSC_CTRL, PSC_CTRL_SUSPEND);
252 static u32 au1550_func(struct i2c_adapter *adap)
254 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
257 static const struct i2c_algorithm au1550_algo = {
258 .master_xfer = au1550_xfer,
259 .functionality = au1550_func,
262 static void i2c_au1550_setup(struct i2c_au1550_data *priv)
266 WR(priv, PSC_CTRL, PSC_CTRL_DISABLE);
267 WR(priv, PSC_SEL, PSC_SEL_PS_SMBUSMODE);
268 WR(priv, PSC_SMBCFG, 0);
269 WR(priv, PSC_CTRL, PSC_CTRL_ENABLE);
270 while ((RD(priv, PSC_SMBSTAT) & PSC_SMBSTAT_SR) == 0)
273 cfg = PSC_SMBCFG_RT_FIFO8 | PSC_SMBCFG_TT_FIFO8 | PSC_SMBCFG_DD_DISABLE;
274 WR(priv, PSC_SMBCFG, cfg);
276 /* Divide by 8 to get a 6.25 MHz clock. The later protocol
277 * timings are based on this clock.
279 cfg |= PSC_SMBCFG_SET_DIV(PSC_SMBCFG_DIV8);
280 WR(priv, PSC_SMBCFG, cfg);
281 WR(priv, PSC_SMBMSK, PSC_SMBMSK_ALLMASK);
283 /* Set the protocol timer values. See Table 71 in the
284 * Au1550 Data Book for standard timing values.
286 WR(priv, PSC_SMBTMR, PSC_SMBTMR_SET_TH(0) | PSC_SMBTMR_SET_PS(20) | \
287 PSC_SMBTMR_SET_PU(20) | PSC_SMBTMR_SET_SH(20) | \
288 PSC_SMBTMR_SET_SU(20) | PSC_SMBTMR_SET_CL(20) | \
289 PSC_SMBTMR_SET_CH(20));
291 cfg |= PSC_SMBCFG_DE_ENABLE;
292 WR(priv, PSC_SMBCFG, cfg);
293 while ((RD(priv, PSC_SMBSTAT) & PSC_SMBSTAT_SR) == 0)
296 WR(priv, PSC_CTRL, PSC_CTRL_SUSPEND);
299 static void i2c_au1550_disable(struct i2c_au1550_data *priv)
301 WR(priv, PSC_SMBCFG, 0);
302 WR(priv, PSC_CTRL, PSC_CTRL_DISABLE);
306 * registering functions to load algorithms at runtime
307 * Prior to calling us, the 50MHz clock frequency and routing
308 * must have been set up for the PSC indicated by the adapter.
311 i2c_au1550_probe(struct platform_device *pdev)
313 struct i2c_au1550_data *priv;
317 priv = devm_kzalloc(&pdev->dev, sizeof(struct i2c_au1550_data),
322 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
323 priv->psc_base = devm_ioremap_resource(&pdev->dev, r);
324 if (IS_ERR(priv->psc_base))
325 return PTR_ERR(priv->psc_base);
327 priv->xfer_timeout = 200;
329 priv->adap.nr = pdev->id;
330 priv->adap.algo = &au1550_algo;
331 priv->adap.algo_data = priv;
332 priv->adap.dev.parent = &pdev->dev;
333 strlcpy(priv->adap.name, "Au1xxx PSC I2C", sizeof(priv->adap.name));
335 /* Now, set up the PSC for SMBus PIO mode. */
336 i2c_au1550_setup(priv);
338 ret = i2c_add_numbered_adapter(&priv->adap);
340 i2c_au1550_disable(priv);
344 platform_set_drvdata(pdev, priv);
348 static int i2c_au1550_remove(struct platform_device *pdev)
350 struct i2c_au1550_data *priv = platform_get_drvdata(pdev);
352 i2c_del_adapter(&priv->adap);
353 i2c_au1550_disable(priv);
358 static int i2c_au1550_suspend(struct device *dev)
360 struct i2c_au1550_data *priv = dev_get_drvdata(dev);
362 i2c_au1550_disable(priv);
367 static int i2c_au1550_resume(struct device *dev)
369 struct i2c_au1550_data *priv = dev_get_drvdata(dev);
371 i2c_au1550_setup(priv);
376 static const struct dev_pm_ops i2c_au1550_pmops = {
377 .suspend = i2c_au1550_suspend,
378 .resume = i2c_au1550_resume,
381 #define AU1XPSC_SMBUS_PMOPS (&i2c_au1550_pmops)
384 #define AU1XPSC_SMBUS_PMOPS NULL
387 static struct platform_driver au1xpsc_smbus_driver = {
389 .name = "au1xpsc_smbus",
390 .pm = AU1XPSC_SMBUS_PMOPS,
392 .probe = i2c_au1550_probe,
393 .remove = i2c_au1550_remove,
396 module_platform_driver(au1xpsc_smbus_driver);
398 MODULE_AUTHOR("Dan Malek, Embedded Edge, LLC.");
399 MODULE_DESCRIPTION("SMBus adapter Alchemy pb1550");
400 MODULE_LICENSE("GPL");
401 MODULE_ALIAS("platform:au1xpsc_smbus");