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.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #include <linux/delay.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/platform_device.h>
34 #include <linux/init.h>
35 #include <linux/errno.h>
36 #include <linux/i2c.h>
37 #include <linux/slab.h>
39 #include <asm/mach-au1x00/au1000.h>
40 #include <asm/mach-au1x00/au1xxx_psc.h>
44 #define PSC_SMBCFG 0x08
45 #define PSC_SMBMSK 0x0C
46 #define PSC_SMBPCR 0x10
47 #define PSC_SMBSTAT 0x14
48 #define PSC_SMBEVNT 0x18
49 #define PSC_SMBTXRX 0x1C
50 #define PSC_SMBTMR 0x20
52 struct i2c_au1550_data
{
53 void __iomem
*psc_base
;
55 struct i2c_adapter adap
;
56 struct resource
*ioarea
;
59 static inline void WR(struct i2c_au1550_data
*a
, int r
, unsigned long v
)
61 __raw_writel(v
, a
->psc_base
+ r
);
65 static inline unsigned long RD(struct i2c_au1550_data
*a
, int r
)
67 return __raw_readl(a
->psc_base
+ r
);
70 static int wait_xfer_done(struct i2c_au1550_data
*adap
)
74 /* Wait for Tx Buffer Empty */
75 for (i
= 0; i
< adap
->xfer_timeout
; i
++) {
76 if (RD(adap
, PSC_SMBSTAT
) & PSC_SMBSTAT_TE
)
85 static int wait_ack(struct i2c_au1550_data
*adap
)
89 if (wait_xfer_done(adap
))
92 stat
= RD(adap
, PSC_SMBEVNT
);
93 if ((stat
& (PSC_SMBEVNT_DN
| PSC_SMBEVNT_AN
| PSC_SMBEVNT_AL
)) != 0)
99 static int wait_master_done(struct i2c_au1550_data
*adap
)
103 /* Wait for Master Done. */
104 for (i
= 0; i
< 2 * adap
->xfer_timeout
; i
++) {
105 if ((RD(adap
, PSC_SMBEVNT
) & PSC_SMBEVNT_MD
) != 0)
114 do_address(struct i2c_au1550_data
*adap
, unsigned int addr
, int rd
, int q
)
118 /* Reset the FIFOs, clear events. */
119 stat
= RD(adap
, PSC_SMBSTAT
);
120 WR(adap
, PSC_SMBEVNT
, PSC_SMBEVNT_ALLCLR
);
122 if (!(stat
& PSC_SMBSTAT_TE
) || !(stat
& PSC_SMBSTAT_RE
)) {
123 WR(adap
, PSC_SMBPCR
, PSC_SMBPCR_DC
);
124 while ((RD(adap
, PSC_SMBPCR
) & PSC_SMBPCR_DC
) != 0)
129 /* Write out the i2c chip address and specify operation */
134 /* zero-byte xfers stop immediately */
136 addr
|= PSC_SMBTXRX_STP
;
138 /* Put byte into fifo, start up master. */
139 WR(adap
, PSC_SMBTXRX
, addr
);
140 WR(adap
, PSC_SMBPCR
, PSC_SMBPCR_MS
);
143 return (q
) ? wait_master_done(adap
) : 0;
146 static int wait_for_rx_byte(struct i2c_au1550_data
*adap
, unsigned char *out
)
150 if (wait_xfer_done(adap
))
153 j
= adap
->xfer_timeout
* 100;
159 if ((RD(adap
, PSC_SMBSTAT
) & PSC_SMBSTAT_RE
) == 0)
165 *out
= RD(adap
, PSC_SMBTXRX
);
170 static int i2c_read(struct i2c_au1550_data
*adap
, unsigned char *buf
,
178 /* A read is performed by stuffing the transmit fifo with
179 * zero bytes for timing, waiting for bytes to appear in the
180 * receive fifo, then reading the bytes.
183 while (i
< (len
- 1)) {
184 WR(adap
, PSC_SMBTXRX
, 0);
185 if (wait_for_rx_byte(adap
, &buf
[i
]))
191 /* The last byte has to indicate transfer done. */
192 WR(adap
, PSC_SMBTXRX
, PSC_SMBTXRX_STP
);
193 if (wait_master_done(adap
))
196 buf
[i
] = (unsigned char)(RD(adap
, PSC_SMBTXRX
) & 0xff);
200 static int i2c_write(struct i2c_au1550_data
*adap
, unsigned char *buf
,
210 while (i
< (len
-1)) {
212 WR(adap
, PSC_SMBTXRX
, data
);
218 /* The last byte has to indicate transfer done. */
220 data
|= PSC_SMBTXRX_STP
;
221 WR(adap
, PSC_SMBTXRX
, data
);
222 if (wait_master_done(adap
))
228 au1550_xfer(struct i2c_adapter
*i2c_adap
, struct i2c_msg
*msgs
, int num
)
230 struct i2c_au1550_data
*adap
= i2c_adap
->algo_data
;
234 WR(adap
, PSC_CTRL
, PSC_CTRL_ENABLE
);
236 for (i
= 0; !err
&& i
< num
; i
++) {
238 err
= do_address(adap
, p
->addr
, p
->flags
& I2C_M_RD
,
242 if (p
->flags
& I2C_M_RD
)
243 err
= i2c_read(adap
, p
->buf
, p
->len
);
245 err
= i2c_write(adap
, p
->buf
, p
->len
);
248 /* Return the number of messages processed, or the error code.
253 WR(adap
, PSC_CTRL
, PSC_CTRL_SUSPEND
);
258 static u32
au1550_func(struct i2c_adapter
*adap
)
260 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
263 static const struct i2c_algorithm au1550_algo
= {
264 .master_xfer
= au1550_xfer
,
265 .functionality
= au1550_func
,
268 static void i2c_au1550_setup(struct i2c_au1550_data
*priv
)
272 WR(priv
, PSC_CTRL
, PSC_CTRL_DISABLE
);
273 WR(priv
, PSC_SEL
, PSC_SEL_PS_SMBUSMODE
);
274 WR(priv
, PSC_SMBCFG
, 0);
275 WR(priv
, PSC_CTRL
, PSC_CTRL_ENABLE
);
276 while ((RD(priv
, PSC_SMBSTAT
) & PSC_SMBSTAT_SR
) == 0)
279 cfg
= PSC_SMBCFG_RT_FIFO8
| PSC_SMBCFG_TT_FIFO8
| PSC_SMBCFG_DD_DISABLE
;
280 WR(priv
, PSC_SMBCFG
, cfg
);
282 /* Divide by 8 to get a 6.25 MHz clock. The later protocol
283 * timings are based on this clock.
285 cfg
|= PSC_SMBCFG_SET_DIV(PSC_SMBCFG_DIV8
);
286 WR(priv
, PSC_SMBCFG
, cfg
);
287 WR(priv
, PSC_SMBMSK
, PSC_SMBMSK_ALLMASK
);
289 /* Set the protocol timer values. See Table 71 in the
290 * Au1550 Data Book for standard timing values.
292 WR(priv
, PSC_SMBTMR
, PSC_SMBTMR_SET_TH(0) | PSC_SMBTMR_SET_PS(15) | \
293 PSC_SMBTMR_SET_PU(15) | PSC_SMBTMR_SET_SH(15) | \
294 PSC_SMBTMR_SET_SU(15) | PSC_SMBTMR_SET_CL(15) | \
295 PSC_SMBTMR_SET_CH(15));
297 cfg
|= PSC_SMBCFG_DE_ENABLE
;
298 WR(priv
, PSC_SMBCFG
, cfg
);
299 while ((RD(priv
, PSC_SMBSTAT
) & PSC_SMBSTAT_SR
) == 0)
302 WR(priv
, PSC_CTRL
, PSC_CTRL_SUSPEND
);
305 static void i2c_au1550_disable(struct i2c_au1550_data
*priv
)
307 WR(priv
, PSC_SMBCFG
, 0);
308 WR(priv
, PSC_CTRL
, PSC_CTRL_DISABLE
);
312 * registering functions to load algorithms at runtime
313 * Prior to calling us, the 50MHz clock frequency and routing
314 * must have been set up for the PSC indicated by the adapter.
317 i2c_au1550_probe(struct platform_device
*pdev
)
319 struct i2c_au1550_data
*priv
;
323 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
329 priv
= kzalloc(sizeof(struct i2c_au1550_data
), GFP_KERNEL
);
335 priv
->ioarea
= request_mem_region(r
->start
, resource_size(r
),
342 priv
->psc_base
= ioremap(r
->start
, resource_size(r
));
343 if (!priv
->psc_base
) {
347 priv
->xfer_timeout
= 200;
349 priv
->adap
.nr
= pdev
->id
;
350 priv
->adap
.algo
= &au1550_algo
;
351 priv
->adap
.algo_data
= priv
;
352 priv
->adap
.dev
.parent
= &pdev
->dev
;
353 strlcpy(priv
->adap
.name
, "Au1xxx PSC I2C", sizeof(priv
->adap
.name
));
355 /* Now, set up the PSC for SMBus PIO mode. */
356 i2c_au1550_setup(priv
);
358 ret
= i2c_add_numbered_adapter(&priv
->adap
);
360 platform_set_drvdata(pdev
, priv
);
364 i2c_au1550_disable(priv
);
365 iounmap(priv
->psc_base
);
367 release_resource(priv
->ioarea
);
375 static int __devexit
i2c_au1550_remove(struct platform_device
*pdev
)
377 struct i2c_au1550_data
*priv
= platform_get_drvdata(pdev
);
379 platform_set_drvdata(pdev
, NULL
);
380 i2c_del_adapter(&priv
->adap
);
381 i2c_au1550_disable(priv
);
382 iounmap(priv
->psc_base
);
383 release_resource(priv
->ioarea
);
390 static int i2c_au1550_suspend(struct device
*dev
)
392 struct i2c_au1550_data
*priv
= dev_get_drvdata(dev
);
394 i2c_au1550_disable(priv
);
399 static int i2c_au1550_resume(struct device
*dev
)
401 struct i2c_au1550_data
*priv
= dev_get_drvdata(dev
);
403 i2c_au1550_setup(priv
);
408 static const struct dev_pm_ops i2c_au1550_pmops
= {
409 .suspend
= i2c_au1550_suspend
,
410 .resume
= i2c_au1550_resume
,
413 #define AU1XPSC_SMBUS_PMOPS (&i2c_au1550_pmops)
416 #define AU1XPSC_SMBUS_PMOPS NULL
419 static struct platform_driver au1xpsc_smbus_driver
= {
421 .name
= "au1xpsc_smbus",
422 .owner
= THIS_MODULE
,
423 .pm
= AU1XPSC_SMBUS_PMOPS
,
425 .probe
= i2c_au1550_probe
,
426 .remove
= __devexit_p(i2c_au1550_remove
),
429 module_platform_driver(au1xpsc_smbus_driver
);
431 MODULE_AUTHOR("Dan Malek, Embedded Edge, LLC.");
432 MODULE_DESCRIPTION("SMBus adapter Alchemy pb1550");
433 MODULE_LICENSE("GPL");
434 MODULE_ALIAS("platform:au1xpsc_smbus");