2 * (C) Copyright 2009-2010
3 * Nokia Siemens Networks, michael.lawnick.ext@nsn.com
5 * Portions Copyright (C) 2010, 2011 Cavium Networks, Inc.
7 * This is a driver for the i2c adapter in Cavium Networks' OCTEON processors.
9 * This file is licensed under the terms of the GNU General Public
10 * License version 2. This program is licensed "as is" without any
11 * warranty of any kind, whether express or implied.
14 #include <linux/platform_device.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/delay.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/i2c.h>
25 #include <asm/octeon/octeon.h>
27 #define DRV_NAME "i2c-octeon"
29 /* The previous out-of-tree version was implicitly version 1.0. */
30 #define DRV_VERSION "2.0"
32 /* register offsets */
36 /* Controller command patterns */
37 #define SW_TWSI_V 0x8000000000000000ull
38 #define SW_TWSI_EOP_TWSI_DATA 0x0C00000100000000ull
39 #define SW_TWSI_EOP_TWSI_CTL 0x0C00000200000000ull
40 #define SW_TWSI_EOP_TWSI_CLKCTL 0x0C00000300000000ull
41 #define SW_TWSI_EOP_TWSI_STAT 0x0C00000300000000ull
42 #define SW_TWSI_EOP_TWSI_RST 0x0C00000700000000ull
43 #define SW_TWSI_OP_TWSI_CLK 0x0800000000000000ull
44 #define SW_TWSI_R 0x0100000000000000ull
46 /* Controller command and status bits */
47 #define TWSI_CTL_CE 0x80
48 #define TWSI_CTL_ENAB 0x40
49 #define TWSI_CTL_STA 0x20
50 #define TWSI_CTL_STP 0x10
51 #define TWSI_CTL_IFLG 0x08
52 #define TWSI_CTL_AAK 0x04
54 /* Some status values */
55 #define STAT_START 0x08
56 #define STAT_RSTART 0x10
57 #define STAT_TXADDR_ACK 0x18
58 #define STAT_TXDATA_ACK 0x28
59 #define STAT_RXADDR_ACK 0x40
60 #define STAT_RXDATA_ACK 0x50
61 #define STAT_IDLE 0xF8
64 wait_queue_head_t queue
;
65 struct i2c_adapter adap
;
69 resource_size_t twsi_phys
;
70 void __iomem
*twsi_base
;
71 resource_size_t regsize
;
76 * octeon_i2c_write_sw - write an I2C core register.
77 * @i2c: The struct octeon_i2c.
78 * @eop_reg: Register selector.
79 * @data: Value to be written.
81 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
83 static void octeon_i2c_write_sw(struct octeon_i2c
*i2c
,
89 __raw_writeq(SW_TWSI_V
| eop_reg
| data
, i2c
->twsi_base
+ SW_TWSI
);
91 tmp
= __raw_readq(i2c
->twsi_base
+ SW_TWSI
);
92 } while ((tmp
& SW_TWSI_V
) != 0);
96 * octeon_i2c_read_sw - write an I2C core register.
97 * @i2c: The struct octeon_i2c.
98 * @eop_reg: Register selector.
102 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
104 static u8
octeon_i2c_read_sw(struct octeon_i2c
*i2c
, u64 eop_reg
)
108 __raw_writeq(SW_TWSI_V
| eop_reg
| SW_TWSI_R
, i2c
->twsi_base
+ SW_TWSI
);
110 tmp
= __raw_readq(i2c
->twsi_base
+ SW_TWSI
);
111 } while ((tmp
& SW_TWSI_V
) != 0);
117 * octeon_i2c_write_int - write the TWSI_INT register
118 * @i2c: The struct octeon_i2c.
119 * @data: Value to be written.
121 static void octeon_i2c_write_int(struct octeon_i2c
*i2c
, u64 data
)
123 __raw_writeq(data
, i2c
->twsi_base
+ TWSI_INT
);
124 __raw_readq(i2c
->twsi_base
+ TWSI_INT
);
128 * octeon_i2c_int_enable - enable the TS interrupt.
129 * @i2c: The struct octeon_i2c.
131 * The interrupt will be asserted when there is non-STAT_IDLE state in
132 * the SW_TWSI_EOP_TWSI_STAT register.
134 static void octeon_i2c_int_enable(struct octeon_i2c
*i2c
)
136 octeon_i2c_write_int(i2c
, 0x40);
140 * octeon_i2c_int_disable - disable the TS interrupt.
141 * @i2c: The struct octeon_i2c.
143 static void octeon_i2c_int_disable(struct octeon_i2c
*i2c
)
145 octeon_i2c_write_int(i2c
, 0);
149 * octeon_i2c_unblock - unblock the bus.
150 * @i2c: The struct octeon_i2c.
152 * If there was a reset while a device was driving 0 to bus,
153 * bus is blocked. We toggle it free manually by some clock
154 * cycles and send a stop.
156 static void octeon_i2c_unblock(struct octeon_i2c
*i2c
)
160 dev_dbg(i2c
->dev
, "%s\n", __func__
);
161 for (i
= 0; i
< 9; i
++) {
162 octeon_i2c_write_int(i2c
, 0x0);
164 octeon_i2c_write_int(i2c
, 0x200);
167 octeon_i2c_write_int(i2c
, 0x300);
169 octeon_i2c_write_int(i2c
, 0x100);
171 octeon_i2c_write_int(i2c
, 0x0);
175 * octeon_i2c_isr - the interrupt service routine.
176 * @int: The irq, unused.
177 * @dev_id: Our struct octeon_i2c.
179 static irqreturn_t
octeon_i2c_isr(int irq
, void *dev_id
)
181 struct octeon_i2c
*i2c
= dev_id
;
183 octeon_i2c_int_disable(i2c
);
184 wake_up(&i2c
->queue
);
190 static int octeon_i2c_test_iflg(struct octeon_i2c
*i2c
)
192 return (octeon_i2c_read_sw(i2c
, SW_TWSI_EOP_TWSI_CTL
) & TWSI_CTL_IFLG
) != 0;
196 * octeon_i2c_wait - wait for the IFLG to be set.
197 * @i2c: The struct octeon_i2c.
199 * Returns 0 on success, otherwise a negative errno.
201 static int octeon_i2c_wait(struct octeon_i2c
*i2c
)
205 octeon_i2c_int_enable(i2c
);
207 result
= wait_event_timeout(i2c
->queue
,
208 octeon_i2c_test_iflg(i2c
),
211 octeon_i2c_int_disable(i2c
);
214 dev_dbg(i2c
->dev
, "%s: wait interrupted\n", __func__
);
216 } else if (result
== 0) {
217 dev_dbg(i2c
->dev
, "%s: timeout\n", __func__
);
225 * octeon_i2c_start - send START to the bus.
226 * @i2c: The struct octeon_i2c.
228 * Returns 0 on success, otherwise a negative errno.
230 static int octeon_i2c_start(struct octeon_i2c
*i2c
)
235 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_CTL
,
236 TWSI_CTL_ENAB
| TWSI_CTL_STA
);
238 result
= octeon_i2c_wait(i2c
);
240 if (octeon_i2c_read_sw(i2c
, SW_TWSI_EOP_TWSI_STAT
) == STAT_IDLE
) {
242 * Controller refused to send start flag May
243 * be a client is holding SDA low - let's try
246 octeon_i2c_unblock(i2c
);
247 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_CTL
,
248 TWSI_CTL_ENAB
| TWSI_CTL_STA
);
250 result
= octeon_i2c_wait(i2c
);
256 data
= octeon_i2c_read_sw(i2c
, SW_TWSI_EOP_TWSI_STAT
);
257 if ((data
!= STAT_START
) && (data
!= STAT_RSTART
)) {
258 dev_err(i2c
->dev
, "%s: bad status (0x%x)\n", __func__
, data
);
266 * octeon_i2c_stop - send STOP to the bus.
267 * @i2c: The struct octeon_i2c.
269 * Returns 0 on success, otherwise a negative errno.
271 static int octeon_i2c_stop(struct octeon_i2c
*i2c
)
275 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_CTL
,
276 TWSI_CTL_ENAB
| TWSI_CTL_STP
);
278 data
= octeon_i2c_read_sw(i2c
, SW_TWSI_EOP_TWSI_STAT
);
280 if (data
!= STAT_IDLE
) {
281 dev_err(i2c
->dev
, "%s: bad status(0x%x)\n", __func__
, data
);
288 * octeon_i2c_write - send data to the bus.
289 * @i2c: The struct octeon_i2c.
290 * @target: Target address.
291 * @data: Pointer to the data to be sent.
292 * @length: Length of the data.
294 * The address is sent over the bus, then the data.
296 * Returns 0 on success, otherwise a negative errno.
298 static int octeon_i2c_write(struct octeon_i2c
*i2c
, int target
,
299 const u8
*data
, int length
)
304 result
= octeon_i2c_start(i2c
);
308 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_DATA
, target
<< 1);
309 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_CTL
, TWSI_CTL_ENAB
);
311 result
= octeon_i2c_wait(i2c
);
315 for (i
= 0; i
< length
; i
++) {
316 tmp
= octeon_i2c_read_sw(i2c
, SW_TWSI_EOP_TWSI_STAT
);
317 if ((tmp
!= STAT_TXADDR_ACK
) && (tmp
!= STAT_TXDATA_ACK
)) {
319 "%s: bad status before write (0x%x)\n",
324 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_DATA
, data
[i
]);
325 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_CTL
, TWSI_CTL_ENAB
);
327 result
= octeon_i2c_wait(i2c
);
336 * octeon_i2c_read - receive data from the bus.
337 * @i2c: The struct octeon_i2c.
338 * @target: Target address.
339 * @data: Pointer to the location to store the datae .
340 * @length: Length of the data.
342 * The address is sent over the bus, then the data is read.
344 * Returns 0 on success, otherwise a negative errno.
346 static int octeon_i2c_read(struct octeon_i2c
*i2c
, int target
,
347 u8
*data
, int length
)
355 result
= octeon_i2c_start(i2c
);
359 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_DATA
, (target
<<1) | 1);
360 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_CTL
, TWSI_CTL_ENAB
);
362 result
= octeon_i2c_wait(i2c
);
366 for (i
= 0; i
< length
; i
++) {
367 tmp
= octeon_i2c_read_sw(i2c
, SW_TWSI_EOP_TWSI_STAT
);
368 if ((tmp
!= STAT_RXDATA_ACK
) && (tmp
!= STAT_RXADDR_ACK
)) {
370 "%s: bad status before read (0x%x)\n",
376 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_CTL
,
377 TWSI_CTL_ENAB
| TWSI_CTL_AAK
);
379 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_CTL
,
382 result
= octeon_i2c_wait(i2c
);
386 data
[i
] = octeon_i2c_read_sw(i2c
, SW_TWSI_EOP_TWSI_DATA
);
392 * octeon_i2c_xfer - The driver's master_xfer function.
393 * @adap: Pointer to the i2c_adapter structure.
394 * @msgs: Pointer to the messages to be processed.
395 * @num: Length of the MSGS array.
397 * Returns the number of messages processed, or a negative errno on
400 static int octeon_i2c_xfer(struct i2c_adapter
*adap
,
401 struct i2c_msg
*msgs
,
404 struct i2c_msg
*pmsg
;
407 struct octeon_i2c
*i2c
= i2c_get_adapdata(adap
);
409 for (i
= 0; ret
== 0 && i
< num
; i
++) {
412 "Doing %s %d byte(s) to/from 0x%02x - %d of %d messages\n",
413 pmsg
->flags
& I2C_M_RD
? "read" : "write",
414 pmsg
->len
, pmsg
->addr
, i
+ 1, num
);
415 if (pmsg
->flags
& I2C_M_RD
)
416 ret
= octeon_i2c_read(i2c
, pmsg
->addr
, pmsg
->buf
,
419 ret
= octeon_i2c_write(i2c
, pmsg
->addr
, pmsg
->buf
,
422 octeon_i2c_stop(i2c
);
424 return (ret
!= 0) ? ret
: num
;
427 static u32
octeon_i2c_functionality(struct i2c_adapter
*adap
)
429 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
432 static const struct i2c_algorithm octeon_i2c_algo
= {
433 .master_xfer
= octeon_i2c_xfer
,
434 .functionality
= octeon_i2c_functionality
,
437 static struct i2c_adapter octeon_i2c_ops
= {
438 .owner
= THIS_MODULE
,
439 .name
= "OCTEON adapter",
440 .algo
= &octeon_i2c_algo
,
445 * octeon_i2c_setclock - Calculate and set clock divisors.
447 static int octeon_i2c_setclock(struct octeon_i2c
*i2c
)
449 int tclk
, thp_base
, inc
, thp_idx
, mdiv_idx
, ndiv_idx
, foscl
, diff
;
450 int thp
= 0x18, mdiv
= 2, ndiv
= 0, delta_hz
= 1000000;
452 for (ndiv_idx
= 0; ndiv_idx
< 8 && delta_hz
!= 0; ndiv_idx
++) {
454 * An mdiv value of less than 2 seems to not work well
455 * with ds1337 RTCs, so we constrain it to larger
458 for (mdiv_idx
= 15; mdiv_idx
>= 2 && delta_hz
!= 0; mdiv_idx
--) {
460 * For given ndiv and mdiv values check the
461 * two closest thp values.
463 tclk
= i2c
->twsi_freq
* (mdiv_idx
+ 1) * 10;
464 tclk
*= (1 << ndiv_idx
);
465 thp_base
= (i2c
->sys_freq
/ (tclk
* 2)) - 1;
466 for (inc
= 0; inc
<= 1; inc
++) {
467 thp_idx
= thp_base
+ inc
;
468 if (thp_idx
< 5 || thp_idx
> 0xff)
471 foscl
= i2c
->sys_freq
/ (2 * (thp_idx
+ 1));
472 foscl
= foscl
/ (1 << ndiv_idx
);
473 foscl
= foscl
/ (mdiv_idx
+ 1) / 10;
474 diff
= abs(foscl
- i2c
->twsi_freq
);
475 if (diff
< delta_hz
) {
484 octeon_i2c_write_sw(i2c
, SW_TWSI_OP_TWSI_CLK
, thp
);
485 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_CLKCTL
, (mdiv
<< 3) | ndiv
);
490 static int octeon_i2c_initlowlevel(struct octeon_i2c
*i2c
)
495 /* disable high level controller, enable bus access */
496 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_CTL
, TWSI_CTL_ENAB
);
498 /* reset controller */
499 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_RST
, 0);
501 for (tries
= 10; tries
; tries
--) {
503 status
= octeon_i2c_read_sw(i2c
, SW_TWSI_EOP_TWSI_STAT
);
504 if (status
== STAT_IDLE
)
507 dev_err(i2c
->dev
, "%s: TWSI_RST failed! (0x%x)\n", __func__
, status
);
511 static int octeon_i2c_probe(struct platform_device
*pdev
)
514 struct octeon_i2c
*i2c
;
515 struct resource
*res_mem
;
517 /* All adaptors have an irq. */
518 irq
= platform_get_irq(pdev
, 0);
522 i2c
= devm_kzalloc(&pdev
->dev
, sizeof(*i2c
), GFP_KERNEL
);
524 dev_err(&pdev
->dev
, "kzalloc failed\n");
528 i2c
->dev
= &pdev
->dev
;
530 res_mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
532 if (res_mem
== NULL
) {
533 dev_err(i2c
->dev
, "found no memory resource\n");
537 i2c
->twsi_phys
= res_mem
->start
;
538 i2c
->regsize
= resource_size(res_mem
);
541 * "clock-rate" is a legacy binding, the official binding is
542 * "clock-frequency". Try the official one first and then
543 * fall back if it doesn't exist.
545 if (of_property_read_u32(pdev
->dev
.of_node
,
546 "clock-frequency", &i2c
->twsi_freq
) &&
547 of_property_read_u32(pdev
->dev
.of_node
,
548 "clock-rate", &i2c
->twsi_freq
)) {
550 "no I2C 'clock-rate' or 'clock-frequency' property\n");
555 i2c
->sys_freq
= octeon_get_io_clock_rate();
557 if (!devm_request_mem_region(&pdev
->dev
, i2c
->twsi_phys
, i2c
->regsize
,
559 dev_err(i2c
->dev
, "request_mem_region failed\n");
562 i2c
->twsi_base
= devm_ioremap(&pdev
->dev
, i2c
->twsi_phys
, i2c
->regsize
);
564 init_waitqueue_head(&i2c
->queue
);
568 result
= devm_request_irq(&pdev
->dev
, i2c
->irq
,
569 octeon_i2c_isr
, 0, DRV_NAME
, i2c
);
571 dev_err(i2c
->dev
, "failed to attach interrupt\n");
575 result
= octeon_i2c_initlowlevel(i2c
);
577 dev_err(i2c
->dev
, "init low level failed\n");
581 result
= octeon_i2c_setclock(i2c
);
583 dev_err(i2c
->dev
, "clock init failed\n");
587 i2c
->adap
= octeon_i2c_ops
;
588 i2c
->adap
.dev
.parent
= &pdev
->dev
;
589 i2c
->adap
.dev
.of_node
= pdev
->dev
.of_node
;
590 i2c_set_adapdata(&i2c
->adap
, i2c
);
591 platform_set_drvdata(pdev
, i2c
);
593 result
= i2c_add_adapter(&i2c
->adap
);
595 dev_err(i2c
->dev
, "failed to add adapter\n");
598 dev_info(i2c
->dev
, "version %s\n", DRV_VERSION
);
606 static int octeon_i2c_remove(struct platform_device
*pdev
)
608 struct octeon_i2c
*i2c
= platform_get_drvdata(pdev
);
610 i2c_del_adapter(&i2c
->adap
);
614 static struct of_device_id octeon_i2c_match
[] = {
616 .compatible
= "cavium,octeon-3860-twsi",
620 MODULE_DEVICE_TABLE(of
, octeon_i2c_match
);
622 static struct platform_driver octeon_i2c_driver
= {
623 .probe
= octeon_i2c_probe
,
624 .remove
= octeon_i2c_remove
,
626 .owner
= THIS_MODULE
,
628 .of_match_table
= octeon_i2c_match
,
632 module_platform_driver(octeon_i2c_driver
);
634 MODULE_AUTHOR("Michael Lawnick <michael.lawnick.ext@nsn.com>");
635 MODULE_DESCRIPTION("I2C-Bus adapter for Cavium OCTEON processors");
636 MODULE_LICENSE("GPL");
637 MODULE_VERSION(DRV_VERSION
);