2 * i2c-au1550.c: SMBus (i2c) adapter for Alchemy PSC interface
3 * Copyright (C) 2004 Embedded Edge, LLC <dan@embeddededge.com>
5 * 2.6 port by Matt Porter <mporter@kernel.crashing.org>
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
;
51 struct resource
*ioarea
;
54 static inline void WR(struct i2c_au1550_data
*a
, int r
, unsigned long v
)
56 __raw_writel(v
, a
->psc_base
+ r
);
60 static inline unsigned long RD(struct i2c_au1550_data
*a
, int r
)
62 return __raw_readl(a
->psc_base
+ r
);
65 static int wait_xfer_done(struct i2c_au1550_data
*adap
)
69 /* Wait for Tx Buffer Empty */
70 for (i
= 0; i
< adap
->xfer_timeout
; i
++) {
71 if (RD(adap
, PSC_SMBSTAT
) & PSC_SMBSTAT_TE
)
80 static int wait_ack(struct i2c_au1550_data
*adap
)
84 if (wait_xfer_done(adap
))
87 stat
= RD(adap
, PSC_SMBEVNT
);
88 if ((stat
& (PSC_SMBEVNT_DN
| PSC_SMBEVNT_AN
| PSC_SMBEVNT_AL
)) != 0)
94 static int wait_master_done(struct i2c_au1550_data
*adap
)
98 /* Wait for Master Done. */
99 for (i
= 0; i
< 2 * adap
->xfer_timeout
; i
++) {
100 if ((RD(adap
, PSC_SMBEVNT
) & PSC_SMBEVNT_MD
) != 0)
109 do_address(struct i2c_au1550_data
*adap
, unsigned int addr
, int rd
, int q
)
113 /* Reset the FIFOs, clear events. */
114 stat
= RD(adap
, PSC_SMBSTAT
);
115 WR(adap
, PSC_SMBEVNT
, PSC_SMBEVNT_ALLCLR
);
117 if (!(stat
& PSC_SMBSTAT_TE
) || !(stat
& PSC_SMBSTAT_RE
)) {
118 WR(adap
, PSC_SMBPCR
, PSC_SMBPCR_DC
);
119 while ((RD(adap
, PSC_SMBPCR
) & PSC_SMBPCR_DC
) != 0)
124 /* Write out the i2c chip address and specify operation */
129 /* zero-byte xfers stop immediately */
131 addr
|= PSC_SMBTXRX_STP
;
133 /* Put byte into fifo, start up master. */
134 WR(adap
, PSC_SMBTXRX
, addr
);
135 WR(adap
, PSC_SMBPCR
, PSC_SMBPCR_MS
);
138 return (q
) ? wait_master_done(adap
) : 0;
141 static int wait_for_rx_byte(struct i2c_au1550_data
*adap
, unsigned char *out
)
145 if (wait_xfer_done(adap
))
148 j
= adap
->xfer_timeout
* 100;
154 if ((RD(adap
, PSC_SMBSTAT
) & PSC_SMBSTAT_RE
) == 0)
160 *out
= RD(adap
, PSC_SMBTXRX
);
165 static int i2c_read(struct i2c_au1550_data
*adap
, unsigned char *buf
,
173 /* A read is performed by stuffing the transmit fifo with
174 * zero bytes for timing, waiting for bytes to appear in the
175 * receive fifo, then reading the bytes.
178 while (i
< (len
- 1)) {
179 WR(adap
, PSC_SMBTXRX
, 0);
180 if (wait_for_rx_byte(adap
, &buf
[i
]))
186 /* The last byte has to indicate transfer done. */
187 WR(adap
, PSC_SMBTXRX
, PSC_SMBTXRX_STP
);
188 if (wait_master_done(adap
))
191 buf
[i
] = (unsigned char)(RD(adap
, PSC_SMBTXRX
) & 0xff);
195 static int i2c_write(struct i2c_au1550_data
*adap
, unsigned char *buf
,
205 while (i
< (len
-1)) {
207 WR(adap
, PSC_SMBTXRX
, data
);
213 /* The last byte has to indicate transfer done. */
215 data
|= PSC_SMBTXRX_STP
;
216 WR(adap
, PSC_SMBTXRX
, data
);
217 if (wait_master_done(adap
))
223 au1550_xfer(struct i2c_adapter
*i2c_adap
, struct i2c_msg
*msgs
, int num
)
225 struct i2c_au1550_data
*adap
= i2c_adap
->algo_data
;
229 WR(adap
, PSC_CTRL
, PSC_CTRL_ENABLE
);
231 for (i
= 0; !err
&& i
< num
; i
++) {
233 err
= do_address(adap
, p
->addr
, p
->flags
& I2C_M_RD
,
237 if (p
->flags
& I2C_M_RD
)
238 err
= i2c_read(adap
, p
->buf
, p
->len
);
240 err
= i2c_write(adap
, p
->buf
, p
->len
);
243 /* Return the number of messages processed, or the error code.
248 WR(adap
, PSC_CTRL
, PSC_CTRL_SUSPEND
);
253 static u32
au1550_func(struct i2c_adapter
*adap
)
255 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
258 static const struct i2c_algorithm au1550_algo
= {
259 .master_xfer
= au1550_xfer
,
260 .functionality
= au1550_func
,
263 static void i2c_au1550_setup(struct i2c_au1550_data
*priv
)
267 WR(priv
, PSC_CTRL
, PSC_CTRL_DISABLE
);
268 WR(priv
, PSC_SEL
, PSC_SEL_PS_SMBUSMODE
);
269 WR(priv
, PSC_SMBCFG
, 0);
270 WR(priv
, PSC_CTRL
, PSC_CTRL_ENABLE
);
271 while ((RD(priv
, PSC_SMBSTAT
) & PSC_SMBSTAT_SR
) == 0)
274 cfg
= PSC_SMBCFG_RT_FIFO8
| PSC_SMBCFG_TT_FIFO8
| PSC_SMBCFG_DD_DISABLE
;
275 WR(priv
, PSC_SMBCFG
, cfg
);
277 /* Divide by 8 to get a 6.25 MHz clock. The later protocol
278 * timings are based on this clock.
280 cfg
|= PSC_SMBCFG_SET_DIV(PSC_SMBCFG_DIV8
);
281 WR(priv
, PSC_SMBCFG
, cfg
);
282 WR(priv
, PSC_SMBMSK
, PSC_SMBMSK_ALLMASK
);
284 /* Set the protocol timer values. See Table 71 in the
285 * Au1550 Data Book for standard timing values.
287 WR(priv
, PSC_SMBTMR
, PSC_SMBTMR_SET_TH(0) | PSC_SMBTMR_SET_PS(15) | \
288 PSC_SMBTMR_SET_PU(15) | PSC_SMBTMR_SET_SH(15) | \
289 PSC_SMBTMR_SET_SU(15) | PSC_SMBTMR_SET_CL(15) | \
290 PSC_SMBTMR_SET_CH(15));
292 cfg
|= PSC_SMBCFG_DE_ENABLE
;
293 WR(priv
, PSC_SMBCFG
, cfg
);
294 while ((RD(priv
, PSC_SMBSTAT
) & PSC_SMBSTAT_SR
) == 0)
297 WR(priv
, PSC_CTRL
, PSC_CTRL_SUSPEND
);
300 static void i2c_au1550_disable(struct i2c_au1550_data
*priv
)
302 WR(priv
, PSC_SMBCFG
, 0);
303 WR(priv
, PSC_CTRL
, PSC_CTRL_DISABLE
);
307 * registering functions to load algorithms at runtime
308 * Prior to calling us, the 50MHz clock frequency and routing
309 * must have been set up for the PSC indicated by the adapter.
312 i2c_au1550_probe(struct platform_device
*pdev
)
314 struct i2c_au1550_data
*priv
;
318 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
324 priv
= kzalloc(sizeof(struct i2c_au1550_data
), GFP_KERNEL
);
330 priv
->ioarea
= request_mem_region(r
->start
, resource_size(r
),
337 priv
->psc_base
= ioremap(r
->start
, resource_size(r
));
338 if (!priv
->psc_base
) {
342 priv
->xfer_timeout
= 200;
344 priv
->adap
.nr
= pdev
->id
;
345 priv
->adap
.algo
= &au1550_algo
;
346 priv
->adap
.algo_data
= priv
;
347 priv
->adap
.dev
.parent
= &pdev
->dev
;
348 strlcpy(priv
->adap
.name
, "Au1xxx PSC I2C", sizeof(priv
->adap
.name
));
350 /* Now, set up the PSC for SMBus PIO mode. */
351 i2c_au1550_setup(priv
);
353 ret
= i2c_add_numbered_adapter(&priv
->adap
);
355 platform_set_drvdata(pdev
, priv
);
359 i2c_au1550_disable(priv
);
360 iounmap(priv
->psc_base
);
362 release_resource(priv
->ioarea
);
370 static int i2c_au1550_remove(struct platform_device
*pdev
)
372 struct i2c_au1550_data
*priv
= platform_get_drvdata(pdev
);
374 i2c_del_adapter(&priv
->adap
);
375 i2c_au1550_disable(priv
);
376 iounmap(priv
->psc_base
);
377 release_resource(priv
->ioarea
);
384 static int i2c_au1550_suspend(struct device
*dev
)
386 struct i2c_au1550_data
*priv
= dev_get_drvdata(dev
);
388 i2c_au1550_disable(priv
);
393 static int i2c_au1550_resume(struct device
*dev
)
395 struct i2c_au1550_data
*priv
= dev_get_drvdata(dev
);
397 i2c_au1550_setup(priv
);
402 static const struct dev_pm_ops i2c_au1550_pmops
= {
403 .suspend
= i2c_au1550_suspend
,
404 .resume
= i2c_au1550_resume
,
407 #define AU1XPSC_SMBUS_PMOPS (&i2c_au1550_pmops)
410 #define AU1XPSC_SMBUS_PMOPS NULL
413 static struct platform_driver au1xpsc_smbus_driver
= {
415 .name
= "au1xpsc_smbus",
416 .pm
= AU1XPSC_SMBUS_PMOPS
,
418 .probe
= i2c_au1550_probe
,
419 .remove
= i2c_au1550_remove
,
422 module_platform_driver(au1xpsc_smbus_driver
);
424 MODULE_AUTHOR("Dan Malek, Embedded Edge, LLC.");
425 MODULE_DESCRIPTION("SMBus adapter Alchemy pb1550");
426 MODULE_LICENSE("GPL");
427 MODULE_ALIAS("platform:au1xpsc_smbus");