2 * I2C bus driver for the SH7760 I2C Interfaces.
4 * (c) 2005-2008 MSC Vertriebsges.m.b.H, Manuel Lauss <mlau@msc-ge.com>
6 * licensed under the terms outlined in the file COPYING.
10 #include <linux/completion.h>
11 #include <linux/delay.h>
12 #include <linux/err.h>
13 #include <linux/i2c.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/ioport.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
20 #include <linux/module.h>
22 #include <asm/clock.h>
23 #include <asm/i2c-sh7760.h>
25 /* register offsets */
26 #define I2CSCR 0x0 /* slave ctrl */
27 #define I2CMCR 0x4 /* master ctrl */
28 #define I2CSSR 0x8 /* slave status */
29 #define I2CMSR 0xC /* master status */
30 #define I2CSIER 0x10 /* slave irq enable */
31 #define I2CMIER 0x14 /* master irq enable */
32 #define I2CCCR 0x18 /* clock dividers */
33 #define I2CSAR 0x1c /* slave address */
34 #define I2CMAR 0x20 /* master address */
35 #define I2CRXTX 0x24 /* data port */
36 #define I2CFCR 0x28 /* fifo control */
37 #define I2CFSR 0x2C /* fifo status */
38 #define I2CFIER 0x30 /* fifo irq enable */
39 #define I2CRFDR 0x34 /* rx fifo count */
40 #define I2CTFDR 0x38 /* tx fifo count */
44 #define MCR_MDBS 0x80 /* non-fifo mode switch */
45 #define MCR_FSCL 0x40 /* override SCL pin */
46 #define MCR_FSDA 0x20 /* override SDA pin */
47 #define MCR_OBPC 0x10 /* override pins */
48 #define MCR_MIE 0x08 /* master if enable */
50 #define MCR_FSB 0x02 /* force stop bit */
51 #define MCR_ESG 0x01 /* en startbit gen. */
53 #define MSR_MNR 0x40 /* nack received */
54 #define MSR_MAL 0x20 /* arbitration lost */
55 #define MSR_MST 0x10 /* sent a stop */
59 #define MSR_MAT 0x01 /* slave addr xfer done */
61 #define MIE_MNRE 0x40 /* nack irq en */
62 #define MIE_MALE 0x20 /* arblos irq en */
63 #define MIE_MSTE 0x10 /* stop irq en */
67 #define MIE_MATE 0x01 /* address sent irq en */
69 #define FCR_RFRST 0x02 /* reset rx fifo */
70 #define FCR_TFRST 0x01 /* reset tx fifo */
72 #define FSR_TEND 0x04 /* last byte sent */
73 #define FSR_RDF 0x02 /* rx fifo trigger */
74 #define FSR_TDFE 0x01 /* tx fifo empty */
76 #define FIER_TEIE 0x04 /* tx fifo empty irq en */
77 #define FIER_RXIE 0x02 /* rx fifo trig irq en */
78 #define FIER_TXIE 0x01 /* tx fifo trig irq en */
84 struct i2c_adapter adap
;
86 /* message processing */
97 struct completion xfer_done
;
100 struct resource
*ioarea
;
103 static inline void OUT32(struct cami2c
*cam
, int reg
, unsigned long val
)
105 __raw_writel(val
, (unsigned long)cam
->iobase
+ reg
);
108 static inline unsigned long IN32(struct cami2c
*cam
, int reg
)
110 return __raw_readl((unsigned long)cam
->iobase
+ reg
);
113 static irqreturn_t
sh7760_i2c_irq(int irq
, void *ptr
)
115 struct cami2c
*id
= ptr
;
116 struct i2c_msg
*msg
= id
->msg
;
117 char *data
= msg
->buf
;
118 unsigned long msr
, fsr
, fier
, len
;
120 msr
= IN32(id
, I2CMSR
);
121 fsr
= IN32(id
, I2CFSR
);
123 /* arbitration lost */
125 OUT32(id
, I2CMCR
, 0);
126 OUT32(id
, I2CSCR
, 0);
127 OUT32(id
, I2CSAR
, 0);
128 id
->status
|= IDS_DONE
| IDS_ARBLOST
;
133 /* NACK handling is very screwed up. After receiving a
134 * NAK IRQ one has to wait a bit before writing to any
135 * registers, or the ctl will lock up. After that delay
136 * do a normal i2c stop. Then wait at least 1 ms before
137 * attempting another transfer or ctl will stop working
139 udelay(100); /* wait or risk ctl hang */
140 OUT32(id
, I2CFCR
, FCR_RFRST
| FCR_TFRST
);
141 OUT32(id
, I2CMCR
, MCR_MIE
| MCR_FSB
);
142 OUT32(id
, I2CFIER
, 0);
143 OUT32(id
, I2CMIER
, MIE_MSTE
);
144 OUT32(id
, I2CSCR
, 0);
145 OUT32(id
, I2CSAR
, 0);
146 id
->status
|= IDS_NACK
;
149 /* In some cases the MST bit is also set. */
152 /* i2c-stop was sent */
154 id
->status
|= IDS_DONE
;
158 /* i2c slave addr was sent; set to "normal" operation */
160 OUT32(id
, I2CMCR
, MCR_MIE
);
162 fier
= IN32(id
, I2CFIER
);
165 len
= IN32(id
, I2CRFDR
);
166 if (msg
->len
<= len
) {
167 if (id
->flags
& IDF_STOP
) {
168 OUT32(id
, I2CMCR
, MCR_MIE
| MCR_FSB
);
169 OUT32(id
, I2CFIER
, 0);
170 /* manual says: wait >= 0.5 SCL times */
172 /* next int should be MST */
174 id
->status
|= IDS_DONE
;
175 /* keep the RDF bit: ctrl holds SCL low
176 * until the setup for the next i2c_msg
182 while (msg
->len
&& len
) {
183 *data
++ = IN32(id
, I2CRXTX
);
189 len
= (msg
->len
>= FIFO_SIZE
) ? FIFO_SIZE
- 1
192 OUT32(id
, I2CFCR
, FCR_TFRST
| ((len
& 0xf) << 4));
195 } else if (id
->flags
& IDF_SEND
) {
196 if ((fsr
& FSR_TEND
) && (msg
->len
< 1)) {
197 if (id
->flags
& IDF_STOP
) {
198 OUT32(id
, I2CMCR
, MCR_MIE
| MCR_FSB
);
200 id
->status
|= IDS_DONE
;
201 /* keep the TEND bit: ctl holds SCL low
202 * until the setup for the next i2c_msg
208 if (fsr
& FSR_TDFE
) {
209 while (msg
->len
&& (IN32(id
, I2CTFDR
) < FIFO_SIZE
)) {
210 OUT32(id
, I2CRXTX
, *data
++);
216 OUT32(id
, I2CFIER
, fier
);
218 len
= (msg
->len
>= FIFO_SIZE
) ? 2 : 0;
220 FCR_RFRST
| ((len
& 3) << 2));
225 if (id
->status
& IDS_DONE
) {
226 OUT32(id
, I2CMIER
, 0);
227 OUT32(id
, I2CFIER
, 0);
229 complete(&id
->xfer_done
);
231 /* clear status flags and ctrl resumes work */
232 OUT32(id
, I2CMSR
, ~msr
);
233 OUT32(id
, I2CFSR
, ~fsr
);
234 OUT32(id
, I2CSSR
, 0);
240 /* prepare and start a master receive operation */
241 static void sh7760_i2c_mrecv(struct cami2c
*id
)
245 id
->flags
|= IDF_RECV
;
247 /* set the slave addr reg; otherwise rcv wont work! */
248 OUT32(id
, I2CSAR
, 0xfe);
249 OUT32(id
, I2CMAR
, (id
->msg
->addr
<< 1) | 1);
251 /* adjust rx fifo trigger */
252 if (id
->msg
->len
>= FIFO_SIZE
)
253 len
= FIFO_SIZE
- 1; /* trigger at fifo full */
255 len
= id
->msg
->len
- 1; /* trigger before all received */
257 OUT32(id
, I2CFCR
, FCR_RFRST
| FCR_TFRST
);
258 OUT32(id
, I2CFCR
, FCR_TFRST
| ((len
& 0xF) << 4));
260 OUT32(id
, I2CMSR
, 0);
261 OUT32(id
, I2CMCR
, MCR_MIE
| MCR_ESG
);
262 OUT32(id
, I2CMIER
, MIE_MNRE
| MIE_MALE
| MIE_MSTE
| MIE_MATE
);
263 OUT32(id
, I2CFIER
, FIER_RXIE
);
266 /* prepare and start a master send operation */
267 static void sh7760_i2c_msend(struct cami2c
*id
)
271 id
->flags
|= IDF_SEND
;
273 /* set the slave addr reg; otherwise xmit wont work! */
274 OUT32(id
, I2CSAR
, 0xfe);
275 OUT32(id
, I2CMAR
, (id
->msg
->addr
<< 1) | 0);
277 /* adjust tx fifo trigger */
278 if (id
->msg
->len
>= FIFO_SIZE
)
279 len
= 2; /* trig: 2 bytes left in TX fifo */
281 len
= 0; /* trig: 8 bytes left in TX fifo */
283 OUT32(id
, I2CFCR
, FCR_RFRST
| FCR_TFRST
);
284 OUT32(id
, I2CFCR
, FCR_RFRST
| ((len
& 3) << 2));
286 while (id
->msg
->len
&& IN32(id
, I2CTFDR
) < FIFO_SIZE
) {
287 OUT32(id
, I2CRXTX
, *(id
->msg
->buf
));
292 OUT32(id
, I2CMSR
, 0);
293 OUT32(id
, I2CMCR
, MCR_MIE
| MCR_ESG
);
294 OUT32(id
, I2CFSR
, 0);
295 OUT32(id
, I2CMIER
, MIE_MNRE
| MIE_MALE
| MIE_MSTE
| MIE_MATE
);
296 OUT32(id
, I2CFIER
, FIER_TEIE
| (id
->msg
->len
? FIER_TXIE
: 0));
299 static inline int sh7760_i2c_busy_check(struct cami2c
*id
)
301 return (IN32(id
, I2CMCR
) & MCR_FSDA
);
304 static int sh7760_i2c_master_xfer(struct i2c_adapter
*adap
,
305 struct i2c_msg
*msgs
,
308 struct cami2c
*id
= adap
->algo_data
;
311 if (sh7760_i2c_busy_check(id
)) {
312 dev_err(&adap
->dev
, "sh7760-i2c%d: bus busy!\n", adap
->nr
);
318 retr
= adap
->retries
;
320 id
->flags
= ((i
== (num
-1)) ? IDF_STOP
: 0);
323 init_completion(&id
->xfer_done
);
325 if (msgs
->flags
& I2C_M_RD
)
326 sh7760_i2c_mrecv(id
);
328 sh7760_i2c_msend(id
);
330 wait_for_completion(&id
->xfer_done
);
332 if (id
->status
== 0) {
337 if (id
->status
& IDS_NACK
) {
338 /* wait a bit or i2c module stops working */
344 if (id
->status
& IDS_ARBLOST
) {
361 OUT32(id
, I2CMCR
, 0);
362 OUT32(id
, I2CMSR
, 0);
363 OUT32(id
, I2CMIER
, 0);
364 OUT32(id
, I2CFIER
, 0);
366 /* reset slave module registers too: master mode enables slave
367 * module for receive ops (ack, data). Without this reset,
368 * eternal bus activity might be reported after NACK / ARBLOST.
370 OUT32(id
, I2CSCR
, 0);
371 OUT32(id
, I2CSAR
, 0);
372 OUT32(id
, I2CSSR
, 0);
377 static u32
sh7760_i2c_func(struct i2c_adapter
*adap
)
379 return I2C_FUNC_I2C
| (I2C_FUNC_SMBUS_EMUL
& ~I2C_FUNC_SMBUS_QUICK
);
382 static const struct i2c_algorithm sh7760_i2c_algo
= {
383 .master_xfer
= sh7760_i2c_master_xfer
,
384 .functionality
= sh7760_i2c_func
,
387 /* calculate CCR register setting for a desired scl clock. SCL clock is
388 * derived from I2C module clock (iclk) which in turn is derived from
389 * peripheral module clock (mclk, usually around 33MHz):
390 * iclk = mclk/(CDF + 1). iclk must be < 20MHz.
391 * scl = iclk/(SCGD*8 + 20).
393 static int calc_CCR(unsigned long scl_hz
)
396 unsigned long mck
, m1
, dff
, odff
, iclk
;
397 signed char cdf
, cdfm
;
398 int scgd
, scgdm
, scgds
;
400 mclk
= clk_get(NULL
, "peripheral_clk");
402 return PTR_ERR(mclk
);
409 scgdm
= cdfm
= m1
= 0;
410 for (cdf
= 3; cdf
>= 0; cdf
--) {
411 iclk
= mck
/ (1 + cdf
);
412 if (iclk
>= 20000000)
414 scgds
= ((iclk
/ scl_hz
) - 20) >> 3;
415 for (scgd
= scgds
; (scgd
< 63) && scgd
<= scgds
+ 1; scgd
++) {
416 m1
= iclk
/ (20 + (scgd
<< 3));
417 dff
= abs(scl_hz
- m1
);
425 /* fail if more than 25% off of requested SCL */
426 if (odff
> (scl_hz
>> 2))
429 /* create a CCR register value */
430 return ((scgdm
<< 2) | cdfm
);
433 static int sh7760_i2c_probe(struct platform_device
*pdev
)
435 struct sh7760_i2c_platdata
*pd
;
436 struct resource
*res
;
440 pd
= pdev
->dev
.platform_data
;
442 dev_err(&pdev
->dev
, "no platform_data!\n");
447 id
= kzalloc(sizeof(struct cami2c
), GFP_KERNEL
);
449 dev_err(&pdev
->dev
, "no mem for private data\n");
454 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
456 dev_err(&pdev
->dev
, "no mmio resources\n");
461 id
->ioarea
= request_mem_region(res
->start
, REGSIZE
, pdev
->name
);
463 dev_err(&pdev
->dev
, "mmio already reserved\n");
468 id
->iobase
= ioremap(res
->start
, REGSIZE
);
470 dev_err(&pdev
->dev
, "cannot ioremap\n");
475 id
->irq
= platform_get_irq(pdev
, 0);
477 id
->adap
.nr
= pdev
->id
;
478 id
->adap
.algo
= &sh7760_i2c_algo
;
479 id
->adap
.class = I2C_CLASS_HWMON
| I2C_CLASS_SPD
;
480 id
->adap
.retries
= 3;
481 id
->adap
.algo_data
= id
;
482 id
->adap
.dev
.parent
= &pdev
->dev
;
483 snprintf(id
->adap
.name
, sizeof(id
->adap
.name
),
484 "SH7760 I2C at %08lx", (unsigned long)res
->start
);
486 OUT32(id
, I2CMCR
, 0);
487 OUT32(id
, I2CMSR
, 0);
488 OUT32(id
, I2CMIER
, 0);
489 OUT32(id
, I2CMAR
, 0);
490 OUT32(id
, I2CSIER
, 0);
491 OUT32(id
, I2CSAR
, 0);
492 OUT32(id
, I2CSCR
, 0);
493 OUT32(id
, I2CSSR
, 0);
494 OUT32(id
, I2CFIER
, 0);
495 OUT32(id
, I2CFCR
, FCR_RFRST
| FCR_TFRST
);
496 OUT32(id
, I2CFSR
, 0);
498 ret
= calc_CCR(pd
->speed_khz
* 1000);
500 dev_err(&pdev
->dev
, "invalid SCL clock: %dkHz\n",
504 OUT32(id
, I2CCCR
, ret
);
506 if (request_irq(id
->irq
, sh7760_i2c_irq
, 0,
507 SH7760_I2C_DEVNAME
, id
)) {
508 dev_err(&pdev
->dev
, "cannot get irq %d\n", id
->irq
);
513 ret
= i2c_add_numbered_adapter(&id
->adap
);
515 dev_err(&pdev
->dev
, "reg adap failed: %d\n", ret
);
519 platform_set_drvdata(pdev
, id
);
521 dev_info(&pdev
->dev
, "%d kHz mmio %08x irq %d\n",
522 pd
->speed_khz
, res
->start
, id
->irq
);
527 free_irq(id
->irq
, id
);
531 release_resource(id
->ioarea
);
539 static int sh7760_i2c_remove(struct platform_device
*pdev
)
541 struct cami2c
*id
= platform_get_drvdata(pdev
);
543 i2c_del_adapter(&id
->adap
);
544 free_irq(id
->irq
, id
);
546 release_resource(id
->ioarea
);
553 static struct platform_driver sh7760_i2c_drv
= {
555 .name
= SH7760_I2C_DEVNAME
,
556 .owner
= THIS_MODULE
,
558 .probe
= sh7760_i2c_probe
,
559 .remove
= sh7760_i2c_remove
,
562 module_platform_driver(sh7760_i2c_drv
);
564 MODULE_LICENSE("GPL");
565 MODULE_DESCRIPTION("SH7760 I2C bus driver");
566 MODULE_AUTHOR("Manuel Lauss <mano@roarinelk.homelinux.net>");