2 * (C) Copyright 2009-2010
3 * Nokia Siemens Networks, michael.lawnick.ext@nsn.com
5 * Portions Copyright (C) 2010 - 2016 Cavium, 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 /* Register offsets */
33 /* Controller command patterns */
34 #define SW_TWSI_V BIT_ULL(63) /* Valid bit */
35 #define SW_TWSI_R BIT_ULL(56) /* Result or read bit */
37 /* Controller opcode word (bits 60:57) */
38 #define SW_TWSI_OP_SHIFT 57
39 #define SW_TWSI_OP_TWSI_CLK (4ULL << SW_TWSI_OP_SHIFT)
40 #define SW_TWSI_OP_EOP (6ULL << SW_TWSI_OP_SHIFT) /* Extended opcode */
42 /* Controller extended opcode word (bits 34:32) */
43 #define SW_TWSI_EOP_SHIFT 32
44 #define SW_TWSI_EOP_TWSI_DATA (SW_TWSI_OP_EOP | 1ULL << SW_TWSI_EOP_SHIFT)
45 #define SW_TWSI_EOP_TWSI_CTL (SW_TWSI_OP_EOP | 2ULL << SW_TWSI_EOP_SHIFT)
46 #define SW_TWSI_EOP_TWSI_CLKCTL (SW_TWSI_OP_EOP | 3ULL << SW_TWSI_EOP_SHIFT)
47 #define SW_TWSI_EOP_TWSI_STAT (SW_TWSI_OP_EOP | 3ULL << SW_TWSI_EOP_SHIFT)
48 #define SW_TWSI_EOP_TWSI_RST (SW_TWSI_OP_EOP | 7ULL << SW_TWSI_EOP_SHIFT)
50 /* Controller command and status bits */
51 #define TWSI_CTL_CE 0x80
52 #define TWSI_CTL_ENAB 0x40 /* Bus enable */
53 #define TWSI_CTL_STA 0x20 /* Master-mode start, HW clears when done */
54 #define TWSI_CTL_STP 0x10 /* Master-mode stop, HW clears when done */
55 #define TWSI_CTL_IFLG 0x08 /* HW event, SW writes 0 to ACK */
56 #define TWSI_CTL_AAK 0x04 /* Assert ACK */
58 /* Some status values */
59 #define STAT_START 0x08
60 #define STAT_RSTART 0x10
61 #define STAT_TXADDR_ACK 0x18
62 #define STAT_TXDATA_ACK 0x28
63 #define STAT_RXADDR_ACK 0x40
64 #define STAT_RXDATA_ACK 0x50
65 #define STAT_IDLE 0xF8
68 #define TWSI_INT_CORE_EN BIT_ULL(6)
69 #define TWSI_INT_SDA_OVR BIT_ULL(8)
70 #define TWSI_INT_SCL_OVR BIT_ULL(9)
73 wait_queue_head_t queue
;
74 struct i2c_adapter adap
;
78 resource_size_t twsi_phys
;
79 void __iomem
*twsi_base
;
80 resource_size_t regsize
;
85 * octeon_i2c_write_sw - write an I2C core register
86 * @i2c: The struct octeon_i2c
87 * @eop_reg: Register selector
88 * @data: Value to be written
90 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
92 static void octeon_i2c_write_sw(struct octeon_i2c
*i2c
, u64 eop_reg
, u8 data
)
96 __raw_writeq(SW_TWSI_V
| eop_reg
| data
, i2c
->twsi_base
+ SW_TWSI
);
98 tmp
= __raw_readq(i2c
->twsi_base
+ SW_TWSI
);
99 } while ((tmp
& SW_TWSI_V
) != 0);
103 * octeon_i2c_read_sw - read lower bits of an I2C core register
104 * @i2c: The struct octeon_i2c
105 * @eop_reg: Register selector
109 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
111 static u8
octeon_i2c_read_sw(struct octeon_i2c
*i2c
, u64 eop_reg
)
115 __raw_writeq(SW_TWSI_V
| eop_reg
| SW_TWSI_R
, i2c
->twsi_base
+ SW_TWSI
);
117 tmp
= __raw_readq(i2c
->twsi_base
+ SW_TWSI
);
118 } while ((tmp
& SW_TWSI_V
) != 0);
124 * octeon_i2c_write_int - write the TWSI_INT register
125 * @i2c: The struct octeon_i2c
126 * @data: Value to be written
128 static void octeon_i2c_write_int(struct octeon_i2c
*i2c
, u64 data
)
130 __raw_writeq(data
, i2c
->twsi_base
+ TWSI_INT
);
131 __raw_readq(i2c
->twsi_base
+ TWSI_INT
);
135 * octeon_i2c_int_enable - enable the CORE interrupt
136 * @i2c: The struct octeon_i2c
138 * The interrupt will be asserted when there is non-STAT_IDLE state in
139 * the SW_TWSI_EOP_TWSI_STAT register.
141 static void octeon_i2c_int_enable(struct octeon_i2c
*i2c
)
143 octeon_i2c_write_int(i2c
, TWSI_INT_CORE_EN
);
146 /* disable the CORE interrupt */
147 static void octeon_i2c_int_disable(struct octeon_i2c
*i2c
)
149 /* clear TS/ST/IFLG events */
150 octeon_i2c_write_int(i2c
, 0);
154 * octeon_i2c_unblock - unblock the bus
155 * @i2c: The struct octeon_i2c
157 * If there was a reset while a device was driving 0 to bus, bus is blocked.
158 * We toggle it free manually by some clock cycles and send a stop.
160 static void octeon_i2c_unblock(struct octeon_i2c
*i2c
)
164 dev_dbg(i2c
->dev
, "%s\n", __func__
);
166 for (i
= 0; i
< 9; i
++) {
167 octeon_i2c_write_int(i2c
, 0);
169 octeon_i2c_write_int(i2c
, TWSI_INT_SCL_OVR
);
172 /* hand-crank a STOP */
173 octeon_i2c_write_int(i2c
, TWSI_INT_SDA_OVR
| TWSI_INT_SCL_OVR
);
175 octeon_i2c_write_int(i2c
, TWSI_INT_SDA_OVR
);
177 octeon_i2c_write_int(i2c
, 0);
180 /* interrupt service routine */
181 static irqreturn_t
octeon_i2c_isr(int irq
, void *dev_id
)
183 struct octeon_i2c
*i2c
= dev_id
;
185 octeon_i2c_int_disable(i2c
);
186 wake_up(&i2c
->queue
);
192 static int octeon_i2c_test_iflg(struct octeon_i2c
*i2c
)
194 return (octeon_i2c_read_sw(i2c
, SW_TWSI_EOP_TWSI_CTL
) & TWSI_CTL_IFLG
) != 0;
198 * octeon_i2c_wait - wait for the IFLG to be set
199 * @i2c: The struct octeon_i2c
201 * Returns 0 on success, otherwise a negative errno.
203 static int octeon_i2c_wait(struct octeon_i2c
*i2c
)
207 octeon_i2c_int_enable(i2c
);
208 time_left
= wait_event_timeout(i2c
->queue
, octeon_i2c_test_iflg(i2c
),
210 octeon_i2c_int_disable(i2c
);
212 dev_dbg(i2c
->dev
, "%s: timeout\n", __func__
);
220 * octeon_i2c_start - send START to the bus
221 * @i2c: The struct octeon_i2c
223 * Returns 0 on success, otherwise a negative errno.
225 static int octeon_i2c_start(struct octeon_i2c
*i2c
)
230 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_CTL
,
231 TWSI_CTL_ENAB
| TWSI_CTL_STA
);
233 result
= octeon_i2c_wait(i2c
);
235 if (octeon_i2c_read_sw(i2c
, SW_TWSI_EOP_TWSI_STAT
) == STAT_IDLE
) {
237 * Controller refused to send start flag May
238 * be a client is holding SDA low - let's try
241 octeon_i2c_unblock(i2c
);
242 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_CTL
,
243 TWSI_CTL_ENAB
| TWSI_CTL_STA
);
244 result
= octeon_i2c_wait(i2c
);
250 data
= octeon_i2c_read_sw(i2c
, SW_TWSI_EOP_TWSI_STAT
);
251 if ((data
!= STAT_START
) && (data
!= STAT_RSTART
)) {
252 dev_err(i2c
->dev
, "%s: bad status (0x%x)\n", __func__
, data
);
259 /* send STOP to the bus */
260 static void octeon_i2c_stop(struct octeon_i2c
*i2c
)
262 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_CTL
,
263 TWSI_CTL_ENAB
| TWSI_CTL_STP
);
267 * octeon_i2c_write - send data to the bus via low-level controller
268 * @i2c: The struct octeon_i2c
269 * @target: Target address
270 * @data: Pointer to the data to be sent
271 * @length: Length of the data
273 * The address is sent over the bus, then the data.
275 * Returns 0 on success, otherwise a negative errno.
277 static int octeon_i2c_write(struct octeon_i2c
*i2c
, int target
,
278 const u8
*data
, int length
)
283 result
= octeon_i2c_start(i2c
);
287 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_DATA
, target
<< 1);
288 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_CTL
, TWSI_CTL_ENAB
);
290 result
= octeon_i2c_wait(i2c
);
294 for (i
= 0; i
< length
; i
++) {
295 tmp
= octeon_i2c_read_sw(i2c
, SW_TWSI_EOP_TWSI_STAT
);
297 if ((tmp
!= STAT_TXADDR_ACK
) && (tmp
!= STAT_TXDATA_ACK
)) {
299 "%s: bad status before write (0x%x)\n",
304 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_DATA
, data
[i
]);
305 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_CTL
, TWSI_CTL_ENAB
);
307 result
= octeon_i2c_wait(i2c
);
316 * octeon_i2c_read - receive data from the bus via low-level controller
317 * @i2c: The struct octeon_i2c
318 * @target: Target address
319 * @data: Pointer to the location to store the data
320 * @length: Length of the data
322 * The address is sent over the bus, then the data is read.
324 * Returns 0 on success, otherwise a negative errno.
326 static int octeon_i2c_read(struct octeon_i2c
*i2c
, int target
,
327 u8
*data
, int length
)
335 result
= octeon_i2c_start(i2c
);
339 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_DATA
, (target
<< 1) | 1);
340 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_CTL
, TWSI_CTL_ENAB
);
342 result
= octeon_i2c_wait(i2c
);
346 for (i
= 0; i
< length
; i
++) {
347 tmp
= octeon_i2c_read_sw(i2c
, SW_TWSI_EOP_TWSI_STAT
);
349 if ((tmp
!= STAT_RXDATA_ACK
) && (tmp
!= STAT_RXADDR_ACK
)) {
351 "%s: bad status before read (0x%x)\n",
357 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_CTL
,
358 TWSI_CTL_ENAB
| TWSI_CTL_AAK
);
360 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_CTL
,
363 result
= octeon_i2c_wait(i2c
);
367 data
[i
] = octeon_i2c_read_sw(i2c
, SW_TWSI_EOP_TWSI_DATA
);
373 * octeon_i2c_xfer - The driver's master_xfer function
374 * @adap: Pointer to the i2c_adapter structure
375 * @msgs: Pointer to the messages to be processed
376 * @num: Length of the MSGS array
378 * Returns the number of messages processed, or a negative errno on failure.
380 static int octeon_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
,
383 struct octeon_i2c
*i2c
= i2c_get_adapdata(adap
);
386 for (i
= 0; ret
== 0 && i
< num
; i
++) {
387 struct i2c_msg
*pmsg
= &msgs
[i
];
390 "Doing %s %d byte(s) to/from 0x%02x - %d of %d messages\n",
391 pmsg
->flags
& I2C_M_RD
? "read" : "write",
392 pmsg
->len
, pmsg
->addr
, i
+ 1, num
);
393 if (pmsg
->flags
& I2C_M_RD
)
394 ret
= octeon_i2c_read(i2c
, pmsg
->addr
, pmsg
->buf
,
397 ret
= octeon_i2c_write(i2c
, pmsg
->addr
, pmsg
->buf
,
400 octeon_i2c_stop(i2c
);
402 return (ret
!= 0) ? ret
: num
;
405 static u32
octeon_i2c_functionality(struct i2c_adapter
*adap
)
407 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
410 static const struct i2c_algorithm octeon_i2c_algo
= {
411 .master_xfer
= octeon_i2c_xfer
,
412 .functionality
= octeon_i2c_functionality
,
415 static struct i2c_adapter octeon_i2c_ops
= {
416 .owner
= THIS_MODULE
,
417 .name
= "OCTEON adapter",
418 .algo
= &octeon_i2c_algo
,
422 /* calculate and set clock divisors */
423 static void octeon_i2c_set_clock(struct octeon_i2c
*i2c
)
425 int tclk
, thp_base
, inc
, thp_idx
, mdiv_idx
, ndiv_idx
, foscl
, diff
;
426 int thp
= 0x18, mdiv
= 2, ndiv
= 0, delta_hz
= 1000000;
428 for (ndiv_idx
= 0; ndiv_idx
< 8 && delta_hz
!= 0; ndiv_idx
++) {
430 * An mdiv value of less than 2 seems to not work well
431 * with ds1337 RTCs, so we constrain it to larger values.
433 for (mdiv_idx
= 15; mdiv_idx
>= 2 && delta_hz
!= 0; mdiv_idx
--) {
435 * For given ndiv and mdiv values check the
436 * two closest thp values.
438 tclk
= i2c
->twsi_freq
* (mdiv_idx
+ 1) * 10;
439 tclk
*= (1 << ndiv_idx
);
440 thp_base
= (i2c
->sys_freq
/ (tclk
* 2)) - 1;
442 for (inc
= 0; inc
<= 1; inc
++) {
443 thp_idx
= thp_base
+ inc
;
444 if (thp_idx
< 5 || thp_idx
> 0xff)
447 foscl
= i2c
->sys_freq
/ (2 * (thp_idx
+ 1));
448 foscl
= foscl
/ (1 << ndiv_idx
);
449 foscl
= foscl
/ (mdiv_idx
+ 1) / 10;
450 diff
= abs(foscl
- i2c
->twsi_freq
);
451 if (diff
< delta_hz
) {
460 octeon_i2c_write_sw(i2c
, SW_TWSI_OP_TWSI_CLK
, thp
);
461 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_CLKCTL
, (mdiv
<< 3) | ndiv
);
464 static int octeon_i2c_init_lowlevel(struct octeon_i2c
*i2c
)
469 /* disable high level controller, enable bus access */
470 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_CTL
, TWSI_CTL_ENAB
);
472 /* reset controller */
473 octeon_i2c_write_sw(i2c
, SW_TWSI_EOP_TWSI_RST
, 0);
475 for (tries
= 10; tries
; tries
--) {
477 status
= octeon_i2c_read_sw(i2c
, SW_TWSI_EOP_TWSI_STAT
);
478 if (status
== STAT_IDLE
)
481 dev_err(i2c
->dev
, "%s: TWSI_RST failed! (0x%x)\n", __func__
, status
);
485 static int octeon_i2c_probe(struct platform_device
*pdev
)
487 struct device_node
*node
= pdev
->dev
.of_node
;
488 struct resource
*res_mem
;
489 struct octeon_i2c
*i2c
;
492 /* All adaptors have an irq. */
493 irq
= platform_get_irq(pdev
, 0);
497 i2c
= devm_kzalloc(&pdev
->dev
, sizeof(*i2c
), GFP_KERNEL
);
502 i2c
->dev
= &pdev
->dev
;
504 res_mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
506 if (res_mem
== NULL
) {
507 dev_err(i2c
->dev
, "found no memory resource\n");
511 i2c
->twsi_phys
= res_mem
->start
;
512 i2c
->regsize
= resource_size(res_mem
);
515 * "clock-rate" is a legacy binding, the official binding is
516 * "clock-frequency". Try the official one first and then
517 * fall back if it doesn't exist.
519 if (of_property_read_u32(node
, "clock-frequency", &i2c
->twsi_freq
) &&
520 of_property_read_u32(node
, "clock-rate", &i2c
->twsi_freq
)) {
522 "no I2C 'clock-rate' or 'clock-frequency' property\n");
527 i2c
->sys_freq
= octeon_get_io_clock_rate();
529 if (!devm_request_mem_region(&pdev
->dev
, i2c
->twsi_phys
, i2c
->regsize
,
531 dev_err(i2c
->dev
, "request_mem_region failed\n");
534 i2c
->twsi_base
= devm_ioremap(&pdev
->dev
, i2c
->twsi_phys
, i2c
->regsize
);
536 init_waitqueue_head(&i2c
->queue
);
540 result
= devm_request_irq(&pdev
->dev
, i2c
->irq
,
541 octeon_i2c_isr
, 0, DRV_NAME
, i2c
);
543 dev_err(i2c
->dev
, "failed to attach interrupt\n");
547 result
= octeon_i2c_init_lowlevel(i2c
);
549 dev_err(i2c
->dev
, "init low level failed\n");
553 octeon_i2c_set_clock(i2c
);
555 i2c
->adap
= octeon_i2c_ops
;
556 i2c
->adap
.dev
.parent
= &pdev
->dev
;
557 i2c
->adap
.dev
.of_node
= node
;
558 i2c_set_adapdata(&i2c
->adap
, i2c
);
559 platform_set_drvdata(pdev
, i2c
);
561 result
= i2c_add_adapter(&i2c
->adap
);
563 dev_err(i2c
->dev
, "failed to add adapter\n");
566 dev_info(i2c
->dev
, "probed\n");
573 static int octeon_i2c_remove(struct platform_device
*pdev
)
575 struct octeon_i2c
*i2c
= platform_get_drvdata(pdev
);
577 i2c_del_adapter(&i2c
->adap
);
581 static const struct of_device_id octeon_i2c_match
[] = {
582 { .compatible
= "cavium,octeon-3860-twsi", },
585 MODULE_DEVICE_TABLE(of
, octeon_i2c_match
);
587 static struct platform_driver octeon_i2c_driver
= {
588 .probe
= octeon_i2c_probe
,
589 .remove
= octeon_i2c_remove
,
592 .of_match_table
= octeon_i2c_match
,
596 module_platform_driver(octeon_i2c_driver
);
598 MODULE_AUTHOR("Michael Lawnick <michael.lawnick.ext@nsn.com>");
599 MODULE_DESCRIPTION("I2C-Bus adapter for Cavium OCTEON processors");
600 MODULE_LICENSE("GPL");