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/au1xxx.h>
40 #include <asm/mach-au1x00/au1xxx_psc.h>
42 struct i2c_au1550_data
{
46 struct i2c_adapter adap
;
47 struct resource
*ioarea
;
51 wait_xfer_done(struct i2c_au1550_data
*adap
)
55 volatile psc_smb_t
*sp
;
57 sp
= (volatile psc_smb_t
*)(adap
->psc_base
);
59 /* Wait for Tx Buffer Empty
61 for (i
= 0; i
< adap
->xfer_timeout
; i
++) {
62 stat
= sp
->psc_smbstat
;
64 if ((stat
& PSC_SMBSTAT_TE
) != 0)
74 wait_ack(struct i2c_au1550_data
*adap
)
77 volatile psc_smb_t
*sp
;
79 if (wait_xfer_done(adap
))
82 sp
= (volatile psc_smb_t
*)(adap
->psc_base
);
84 stat
= sp
->psc_smbevnt
;
87 if ((stat
& (PSC_SMBEVNT_DN
| PSC_SMBEVNT_AN
| PSC_SMBEVNT_AL
)) != 0)
94 wait_master_done(struct i2c_au1550_data
*adap
)
98 volatile psc_smb_t
*sp
;
100 sp
= (volatile psc_smb_t
*)(adap
->psc_base
);
102 /* Wait for Master Done.
104 for (i
= 0; i
< adap
->xfer_timeout
; i
++) {
105 stat
= sp
->psc_smbevnt
;
107 if ((stat
& PSC_SMBEVNT_MD
) != 0)
116 do_address(struct i2c_au1550_data
*adap
, unsigned int addr
, int rd
, int q
)
118 volatile psc_smb_t
*sp
;
121 sp
= (volatile psc_smb_t
*)(adap
->psc_base
);
123 /* Reset the FIFOs, clear events.
125 stat
= sp
->psc_smbstat
;
126 sp
->psc_smbevnt
= PSC_SMBEVNT_ALLCLR
;
129 if (!(stat
& PSC_SMBSTAT_TE
) || !(stat
& PSC_SMBSTAT_RE
)) {
130 sp
->psc_smbpcr
= PSC_SMBPCR_DC
;
133 stat
= sp
->psc_smbpcr
;
135 } while ((stat
& PSC_SMBPCR_DC
) != 0);
139 /* Write out the i2c chip address and specify operation
145 /* zero-byte xfers stop immediately */
147 addr
|= PSC_SMBTXRX_STP
;
149 /* Put byte into fifo, start up master.
151 sp
->psc_smbtxrx
= addr
;
153 sp
->psc_smbpcr
= PSC_SMBPCR_MS
;
157 return (q
) ? wait_master_done(adap
) : 0;
161 wait_for_rx_byte(struct i2c_au1550_data
*adap
, u32
*ret_data
)
165 volatile psc_smb_t
*sp
;
167 if (wait_xfer_done(adap
))
170 sp
= (volatile psc_smb_t
*)(adap
->psc_base
);
172 j
= adap
->xfer_timeout
* 100;
178 stat
= sp
->psc_smbstat
;
180 if ((stat
& PSC_SMBSTAT_RE
) == 0)
185 data
= sp
->psc_smbtxrx
;
193 i2c_read(struct i2c_au1550_data
*adap
, unsigned char *buf
,
198 volatile psc_smb_t
*sp
;
203 /* A read is performed by stuffing the transmit fifo with
204 * zero bytes for timing, waiting for bytes to appear in the
205 * receive fifo, then reading the bytes.
208 sp
= (volatile psc_smb_t
*)(adap
->psc_base
);
211 while (i
< (len
-1)) {
214 if (wait_for_rx_byte(adap
, &data
))
221 /* The last byte has to indicate transfer done.
223 sp
->psc_smbtxrx
= PSC_SMBTXRX_STP
;
225 if (wait_master_done(adap
))
228 data
= sp
->psc_smbtxrx
;
235 i2c_write(struct i2c_au1550_data
*adap
, unsigned char *buf
,
240 volatile psc_smb_t
*sp
;
245 sp
= (volatile psc_smb_t
*)(adap
->psc_base
);
248 while (i
< (len
-1)) {
250 sp
->psc_smbtxrx
= data
;
257 /* The last byte has to indicate transfer done.
260 data
|= PSC_SMBTXRX_STP
;
261 sp
->psc_smbtxrx
= data
;
263 if (wait_master_done(adap
))
269 au1550_xfer(struct i2c_adapter
*i2c_adap
, struct i2c_msg
*msgs
, int num
)
271 struct i2c_au1550_data
*adap
= i2c_adap
->algo_data
;
272 volatile psc_smb_t
*sp
= (volatile psc_smb_t
*)adap
->psc_base
;
276 sp
->psc_ctrl
= PSC_CTRL_ENABLE
;
279 for (i
= 0; !err
&& i
< num
; i
++) {
281 err
= do_address(adap
, p
->addr
, p
->flags
& I2C_M_RD
,
285 if (p
->flags
& I2C_M_RD
)
286 err
= i2c_read(adap
, p
->buf
, p
->len
);
288 err
= i2c_write(adap
, p
->buf
, p
->len
);
291 /* Return the number of messages processed, or the error code.
296 sp
->psc_ctrl
= PSC_CTRL_SUSPEND
;
303 au1550_func(struct i2c_adapter
*adap
)
305 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
308 static const struct i2c_algorithm au1550_algo
= {
309 .master_xfer
= au1550_xfer
,
310 .functionality
= au1550_func
,
313 static void i2c_au1550_setup(struct i2c_au1550_data
*priv
)
315 volatile psc_smb_t
*sp
= (volatile psc_smb_t
*)priv
->psc_base
;
318 sp
->psc_ctrl
= PSC_CTRL_DISABLE
;
320 sp
->psc_sel
= PSC_SEL_PS_SMBUSMODE
;
323 sp
->psc_ctrl
= PSC_CTRL_ENABLE
;
326 stat
= sp
->psc_smbstat
;
328 } while ((stat
& PSC_SMBSTAT_SR
) == 0);
330 sp
->psc_smbcfg
= (PSC_SMBCFG_RT_FIFO8
| PSC_SMBCFG_TT_FIFO8
|
331 PSC_SMBCFG_DD_DISABLE
);
333 /* Divide by 8 to get a 6.25 MHz clock. The later protocol
334 * timings are based on this clock.
336 sp
->psc_smbcfg
|= PSC_SMBCFG_SET_DIV(PSC_SMBCFG_DIV8
);
337 sp
->psc_smbmsk
= PSC_SMBMSK_ALLMASK
;
340 /* Set the protocol timer values. See Table 71 in the
341 * Au1550 Data Book for standard timing values.
343 sp
->psc_smbtmr
= PSC_SMBTMR_SET_TH(0) | PSC_SMBTMR_SET_PS(15) | \
344 PSC_SMBTMR_SET_PU(15) | PSC_SMBTMR_SET_SH(15) | \
345 PSC_SMBTMR_SET_SU(15) | PSC_SMBTMR_SET_CL(15) | \
346 PSC_SMBTMR_SET_CH(15);
349 sp
->psc_smbcfg
|= PSC_SMBCFG_DE_ENABLE
;
351 stat
= sp
->psc_smbstat
;
353 } while ((stat
& PSC_SMBSTAT_SR
) == 0);
355 sp
->psc_ctrl
= PSC_CTRL_SUSPEND
;
359 static void i2c_au1550_disable(struct i2c_au1550_data
*priv
)
361 volatile psc_smb_t
*sp
= (volatile psc_smb_t
*)priv
->psc_base
;
364 sp
->psc_ctrl
= PSC_CTRL_DISABLE
;
369 * registering functions to load algorithms at runtime
370 * Prior to calling us, the 50MHz clock frequency and routing
371 * must have been set up for the PSC indicated by the adapter.
374 i2c_au1550_probe(struct platform_device
*pdev
)
376 struct i2c_au1550_data
*priv
;
380 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
386 priv
= kzalloc(sizeof(struct i2c_au1550_data
), GFP_KERNEL
);
392 priv
->ioarea
= request_mem_region(r
->start
, resource_size(r
),
399 priv
->psc_base
= CKSEG1ADDR(r
->start
);
400 priv
->xfer_timeout
= 200;
401 priv
->ack_timeout
= 200;
403 priv
->adap
.nr
= pdev
->id
;
404 priv
->adap
.algo
= &au1550_algo
;
405 priv
->adap
.algo_data
= priv
;
406 priv
->adap
.dev
.parent
= &pdev
->dev
;
407 strlcpy(priv
->adap
.name
, "Au1xxx PSC I2C", sizeof(priv
->adap
.name
));
409 /* Now, set up the PSC for SMBus PIO mode.
411 i2c_au1550_setup(priv
);
413 ret
= i2c_add_numbered_adapter(&priv
->adap
);
415 platform_set_drvdata(pdev
, priv
);
419 i2c_au1550_disable(priv
);
421 release_resource(priv
->ioarea
);
430 i2c_au1550_remove(struct platform_device
*pdev
)
432 struct i2c_au1550_data
*priv
= platform_get_drvdata(pdev
);
434 platform_set_drvdata(pdev
, NULL
);
435 i2c_del_adapter(&priv
->adap
);
436 i2c_au1550_disable(priv
);
437 release_resource(priv
->ioarea
);
445 i2c_au1550_suspend(struct platform_device
*pdev
, pm_message_t state
)
447 struct i2c_au1550_data
*priv
= platform_get_drvdata(pdev
);
449 i2c_au1550_disable(priv
);
455 i2c_au1550_resume(struct platform_device
*pdev
)
457 struct i2c_au1550_data
*priv
= platform_get_drvdata(pdev
);
459 i2c_au1550_setup(priv
);
464 #define i2c_au1550_suspend NULL
465 #define i2c_au1550_resume NULL
468 static struct platform_driver au1xpsc_smbus_driver
= {
470 .name
= "au1xpsc_smbus",
471 .owner
= THIS_MODULE
,
473 .probe
= i2c_au1550_probe
,
474 .remove
= __devexit_p(i2c_au1550_remove
),
475 .suspend
= i2c_au1550_suspend
,
476 .resume
= i2c_au1550_resume
,
480 i2c_au1550_init(void)
482 return platform_driver_register(&au1xpsc_smbus_driver
);
486 i2c_au1550_exit(void)
488 platform_driver_unregister(&au1xpsc_smbus_driver
);
491 MODULE_AUTHOR("Dan Malek, Embedded Edge, LLC.");
492 MODULE_DESCRIPTION("SMBus adapter Alchemy pb1550");
493 MODULE_LICENSE("GPL");
494 MODULE_ALIAS("platform:au1xpsc_smbus");
496 module_init (i2c_au1550_init
);
497 module_exit (i2c_au1550_exit
);