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 */
32 #define SW_TWSI_EXT 0x18
34 /* Controller command patterns */
35 #define SW_TWSI_V BIT_ULL(63) /* Valid bit */
36 #define SW_TWSI_EIA BIT_ULL(61) /* Extended internal address */
37 #define SW_TWSI_R BIT_ULL(56) /* Result or read bit */
38 #define SW_TWSI_SOVR BIT_ULL(55) /* Size override */
39 #define SW_TWSI_SIZE_SHIFT 52
40 #define SW_TWSI_ADDR_SHIFT 40
41 #define SW_TWSI_IA_SHIFT 32 /* Internal address */
43 /* Controller opcode word (bits 60:57) */
44 #define SW_TWSI_OP_SHIFT 57
45 #define SW_TWSI_OP_7 (0ULL << SW_TWSI_OP_SHIFT)
46 #define SW_TWSI_OP_7_IA (1ULL << SW_TWSI_OP_SHIFT)
47 #define SW_TWSI_OP_10 (2ULL << SW_TWSI_OP_SHIFT)
48 #define SW_TWSI_OP_10_IA (3ULL << SW_TWSI_OP_SHIFT)
49 #define SW_TWSI_OP_TWSI_CLK (4ULL << SW_TWSI_OP_SHIFT)
50 #define SW_TWSI_OP_EOP (6ULL << SW_TWSI_OP_SHIFT) /* Extended opcode */
52 /* Controller extended opcode word (bits 34:32) */
53 #define SW_TWSI_EOP_SHIFT 32
54 #define SW_TWSI_EOP_TWSI_DATA (SW_TWSI_OP_EOP | 1ULL << SW_TWSI_EOP_SHIFT)
55 #define SW_TWSI_EOP_TWSI_CTL (SW_TWSI_OP_EOP | 2ULL << SW_TWSI_EOP_SHIFT)
56 #define SW_TWSI_EOP_TWSI_CLKCTL (SW_TWSI_OP_EOP | 3ULL << SW_TWSI_EOP_SHIFT)
57 #define SW_TWSI_EOP_TWSI_STAT (SW_TWSI_OP_EOP | 3ULL << SW_TWSI_EOP_SHIFT)
58 #define SW_TWSI_EOP_TWSI_RST (SW_TWSI_OP_EOP | 7ULL << SW_TWSI_EOP_SHIFT)
60 /* Controller command and status bits */
61 #define TWSI_CTL_CE 0x80 /* High level controller enable */
62 #define TWSI_CTL_ENAB 0x40 /* Bus enable */
63 #define TWSI_CTL_STA 0x20 /* Master-mode start, HW clears when done */
64 #define TWSI_CTL_STP 0x10 /* Master-mode stop, HW clears when done */
65 #define TWSI_CTL_IFLG 0x08 /* HW event, SW writes 0 to ACK */
66 #define TWSI_CTL_AAK 0x04 /* Assert ACK */
69 #define STAT_ERROR 0x00
70 #define STAT_START 0x08
71 #define STAT_REP_START 0x10
72 #define STAT_TXADDR_ACK 0x18
73 #define STAT_TXADDR_NAK 0x20
74 #define STAT_TXDATA_ACK 0x28
75 #define STAT_TXDATA_NAK 0x30
76 #define STAT_LOST_ARB_38 0x38
77 #define STAT_RXADDR_ACK 0x40
78 #define STAT_RXADDR_NAK 0x48
79 #define STAT_RXDATA_ACK 0x50
80 #define STAT_RXDATA_NAK 0x58
81 #define STAT_SLAVE_60 0x60
82 #define STAT_LOST_ARB_68 0x68
83 #define STAT_SLAVE_70 0x70
84 #define STAT_LOST_ARB_78 0x78
85 #define STAT_SLAVE_80 0x80
86 #define STAT_SLAVE_88 0x88
87 #define STAT_GENDATA_ACK 0x90
88 #define STAT_GENDATA_NAK 0x98
89 #define STAT_SLAVE_A0 0xA0
90 #define STAT_SLAVE_A8 0xA8
91 #define STAT_LOST_ARB_B0 0xB0
92 #define STAT_SLAVE_LOST 0xB8
93 #define STAT_SLAVE_NAK 0xC0
94 #define STAT_SLAVE_ACK 0xC8
95 #define STAT_AD2W_ACK 0xD0
96 #define STAT_AD2W_NAK 0xD8
97 #define STAT_IDLE 0xF8
100 #define TWSI_INT_ST_INT BIT_ULL(0)
101 #define TWSI_INT_TS_INT BIT_ULL(1)
102 #define TWSI_INT_CORE_INT BIT_ULL(2)
103 #define TWSI_INT_ST_EN BIT_ULL(4)
104 #define TWSI_INT_TS_EN BIT_ULL(5)
105 #define TWSI_INT_CORE_EN BIT_ULL(6)
106 #define TWSI_INT_SDA_OVR BIT_ULL(8)
107 #define TWSI_INT_SCL_OVR BIT_ULL(9)
108 #define TWSI_INT_SDA BIT_ULL(10)
109 #define TWSI_INT_SCL BIT_ULL(11)
112 wait_queue_head_t queue;
113 struct i2c_adapter adap;
117 void __iomem *twsi_base;
122 static void octeon_i2c_writeq_flush(u64 val, void __iomem *addr)
124 __raw_writeq(val, addr);
125 __raw_readq(addr); /* wait for write to land */
129 * octeon_i2c_reg_write - write an I2C core register
130 * @i2c: The struct octeon_i2c
131 * @eop_reg: Register selector
132 * @data: Value to be written
134 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
136 static void octeon_i2c_reg_write(struct octeon_i2c *i2c, u64 eop_reg, u8 data)
140 __raw_writeq(SW_TWSI_V | eop_reg | data, i2c->twsi_base + SW_TWSI);
142 tmp = __raw_readq(i2c->twsi_base + SW_TWSI);
143 } while ((tmp & SW_TWSI_V) != 0);
146 #define octeon_i2c_ctl_write(i2c, val) \
147 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_CTL, val)
148 #define octeon_i2c_data_write(i2c, val) \
149 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_DATA, val)
152 * octeon_i2c_reg_read - read lower bits of an I2C core register
153 * @i2c: The struct octeon_i2c
154 * @eop_reg: Register selector
158 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
160 static u8 octeon_i2c_reg_read(struct octeon_i2c *i2c, u64 eop_reg)
164 __raw_writeq(SW_TWSI_V | eop_reg | SW_TWSI_R, i2c->twsi_base + SW_TWSI);
166 tmp = __raw_readq(i2c->twsi_base + SW_TWSI);
167 } while ((tmp & SW_TWSI_V) != 0);
172 #define octeon_i2c_ctl_read(i2c) \
173 octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_CTL)
174 #define octeon_i2c_data_read(i2c) \
175 octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_DATA)
176 #define octeon_i2c_stat_read(i2c) \
177 octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_STAT)
180 * octeon_i2c_read_int - read the TWSI_INT register
181 * @i2c: The struct octeon_i2c
183 * Returns the value of the register.
185 static u64 octeon_i2c_read_int(struct octeon_i2c *i2c)
187 return __raw_readq(i2c->twsi_base + TWSI_INT);
191 * octeon_i2c_write_int - write the TWSI_INT register
192 * @i2c: The struct octeon_i2c
193 * @data: Value to be written
195 static void octeon_i2c_write_int(struct octeon_i2c *i2c, u64 data)
197 octeon_i2c_writeq_flush(data, i2c->twsi_base + TWSI_INT);
201 * octeon_i2c_int_enable - enable the CORE interrupt
202 * @i2c: The struct octeon_i2c
204 * The interrupt will be asserted when there is non-STAT_IDLE state in
205 * the SW_TWSI_EOP_TWSI_STAT register.
207 static void octeon_i2c_int_enable(struct octeon_i2c *i2c)
209 octeon_i2c_write_int(i2c, TWSI_INT_CORE_EN);
212 /* disable the CORE interrupt */
213 static void octeon_i2c_int_disable(struct octeon_i2c *i2c)
215 /* clear TS/ST/IFLG events */
216 octeon_i2c_write_int(i2c, 0);
220 * Cleanup low-level state & enable high-level controller.
222 static void octeon_i2c_hlc_enable(struct octeon_i2c *i2c)
227 if (i2c->hlc_enabled)
229 i2c->hlc_enabled = true;
232 val = octeon_i2c_ctl_read(i2c);
233 if (!(val & (TWSI_CTL_STA | TWSI_CTL_STP)))
236 /* clear IFLG event */
237 if (val & TWSI_CTL_IFLG)
238 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
241 pr_err("%s: giving up\n", __func__);
245 /* spin until any start/stop has finished */
248 octeon_i2c_ctl_write(i2c, TWSI_CTL_CE | TWSI_CTL_AAK | TWSI_CTL_ENAB);
251 static void octeon_i2c_hlc_disable(struct octeon_i2c *i2c)
253 if (!i2c->hlc_enabled)
256 i2c->hlc_enabled = false;
257 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
260 /* interrupt service routine */
261 static irqreturn_t octeon_i2c_isr(int irq, void *dev_id)
263 struct octeon_i2c *i2c = dev_id;
265 octeon_i2c_int_disable(i2c);
266 wake_up(&i2c->queue);
271 static int octeon_i2c_test_iflg(struct octeon_i2c *i2c)
273 return (octeon_i2c_ctl_read(i2c) & TWSI_CTL_IFLG);
277 * octeon_i2c_wait - wait for the IFLG to be set
278 * @i2c: The struct octeon_i2c
280 * Returns 0 on success, otherwise a negative errno.
282 static int octeon_i2c_wait(struct octeon_i2c *i2c)
286 octeon_i2c_int_enable(i2c);
287 time_left = wait_event_timeout(i2c->queue, octeon_i2c_test_iflg(i2c),
289 octeon_i2c_int_disable(i2c);
291 dev_dbg(i2c->dev, "%s: timeout\n", __func__);
298 static int octeon_i2c_check_status(struct octeon_i2c *i2c, int final_read)
300 u8 stat = octeon_i2c_stat_read(i2c);
303 /* Everything is fine */
306 case STAT_RXADDR_ACK:
307 case STAT_TXADDR_ACK:
308 case STAT_TXDATA_ACK:
311 /* ACK allowed on pre-terminal bytes only */
312 case STAT_RXDATA_ACK:
317 /* NAK allowed on terminal byte only */
318 case STAT_RXDATA_NAK:
323 /* Arbitration lost */
324 case STAT_LOST_ARB_38:
325 case STAT_LOST_ARB_68:
326 case STAT_LOST_ARB_78:
327 case STAT_LOST_ARB_B0:
330 /* Being addressed as slave, should back off & listen */
333 case STAT_GENDATA_ACK:
334 case STAT_GENDATA_NAK:
337 /* Core busy as slave */
342 case STAT_SLAVE_LOST:
347 case STAT_TXDATA_NAK:
349 case STAT_TXADDR_NAK:
350 case STAT_RXADDR_NAK:
354 dev_err(i2c->dev, "unhandled state: %d\n", stat);
359 static bool octeon_i2c_hlc_test_ready(struct octeon_i2c *i2c)
361 u64 val = __raw_readq(i2c->twsi_base + SW_TWSI);
363 return (val & SW_TWSI_V) == 0;
366 static void octeon_i2c_hlc_int_enable(struct octeon_i2c *i2c)
368 octeon_i2c_write_int(i2c, TWSI_INT_ST_EN);
371 static void octeon_i2c_hlc_int_clear(struct octeon_i2c *i2c)
373 /* clear ST/TS events, listen for neither */
374 octeon_i2c_write_int(i2c, TWSI_INT_ST_INT | TWSI_INT_TS_INT);
378 * octeon_i2c_hlc_wait - wait for an HLC operation to complete
379 * @i2c: The struct octeon_i2c
381 * Returns 0 on success, otherwise -ETIMEDOUT.
383 static int octeon_i2c_hlc_wait(struct octeon_i2c *i2c)
387 octeon_i2c_hlc_int_enable(i2c);
388 time_left = wait_event_timeout(i2c->queue,
389 octeon_i2c_hlc_test_ready(i2c),
391 octeon_i2c_int_disable(i2c);
393 octeon_i2c_hlc_int_clear(i2c);
399 /* high-level-controller pure read of up to 8 bytes */
400 static int octeon_i2c_hlc_read(struct octeon_i2c *i2c, struct i2c_msg *msgs)
405 octeon_i2c_hlc_enable(i2c);
406 octeon_i2c_hlc_int_clear(i2c);
408 cmd = SW_TWSI_V | SW_TWSI_R | SW_TWSI_SOVR;
410 cmd |= (u64)(msgs[0].len - 1) << SW_TWSI_SIZE_SHIFT;
412 cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT;
414 if (msgs[0].flags & I2C_M_TEN)
415 cmd |= SW_TWSI_OP_10;
419 octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI);
420 ret = octeon_i2c_hlc_wait(i2c);
424 cmd = __raw_readq(i2c->twsi_base + SW_TWSI);
425 if ((cmd & SW_TWSI_R) == 0)
428 for (i = 0, j = msgs[0].len - 1; i < msgs[0].len && i < 4; i++, j--)
429 msgs[0].buf[j] = (cmd >> (8 * i)) & 0xff;
431 if (msgs[0].len > 4) {
432 cmd = __raw_readq(i2c->twsi_base + SW_TWSI_EXT);
433 for (i = 0; i < msgs[0].len - 4 && i < 4; i++, j--)
434 msgs[0].buf[j] = (cmd >> (8 * i)) & 0xff;
441 /* high-level-controller pure write of up to 8 bytes */
442 static int octeon_i2c_hlc_write(struct octeon_i2c *i2c, struct i2c_msg *msgs)
447 octeon_i2c_hlc_enable(i2c);
448 octeon_i2c_hlc_int_clear(i2c);
450 cmd = SW_TWSI_V | SW_TWSI_SOVR;
452 cmd |= (u64)(msgs[0].len - 1) << SW_TWSI_SIZE_SHIFT;
454 cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT;
456 if (msgs[0].flags & I2C_M_TEN)
457 cmd |= SW_TWSI_OP_10;
461 for (i = 0, j = msgs[0].len - 1; i < msgs[0].len && i < 4; i++, j--)
462 cmd |= (u64)msgs[0].buf[j] << (8 * i);
464 if (msgs[0].len > 4) {
467 for (i = 0; i < msgs[0].len - 4 && i < 4; i++, j--)
468 ext |= (u64)msgs[0].buf[j] << (8 * i);
469 octeon_i2c_writeq_flush(ext, i2c->twsi_base + SW_TWSI_EXT);
472 octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI);
473 ret = octeon_i2c_hlc_wait(i2c);
477 cmd = __raw_readq(i2c->twsi_base + SW_TWSI);
478 if ((cmd & SW_TWSI_R) == 0)
481 ret = octeon_i2c_check_status(i2c, false);
487 /* high-level-controller composite write+read, msg0=addr, msg1=data */
488 static int octeon_i2c_hlc_comp_read(struct octeon_i2c *i2c, struct i2c_msg *msgs)
493 octeon_i2c_hlc_enable(i2c);
495 cmd = SW_TWSI_V | SW_TWSI_R | SW_TWSI_SOVR;
497 cmd |= (u64)(msgs[1].len - 1) << SW_TWSI_SIZE_SHIFT;
499 cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT;
501 if (msgs[0].flags & I2C_M_TEN)
502 cmd |= SW_TWSI_OP_10_IA;
504 cmd |= SW_TWSI_OP_7_IA;
506 if (msgs[0].len == 2) {
510 ext = (u64)msgs[0].buf[0] << SW_TWSI_IA_SHIFT;
511 cmd |= (u64)msgs[0].buf[1] << SW_TWSI_IA_SHIFT;
512 octeon_i2c_writeq_flush(ext, i2c->twsi_base + SW_TWSI_EXT);
514 cmd |= (u64)msgs[0].buf[0] << SW_TWSI_IA_SHIFT;
517 octeon_i2c_hlc_int_clear(i2c);
518 octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI);
520 ret = octeon_i2c_hlc_wait(i2c);
524 cmd = __raw_readq(i2c->twsi_base + SW_TWSI);
525 if ((cmd & SW_TWSI_R) == 0)
528 for (i = 0, j = msgs[1].len - 1; i < msgs[1].len && i < 4; i++, j--)
529 msgs[1].buf[j] = (cmd >> (8 * i)) & 0xff;
531 if (msgs[1].len > 4) {
532 cmd = __raw_readq(i2c->twsi_base + SW_TWSI_EXT);
533 for (i = 0; i < msgs[1].len - 4 && i < 4; i++, j--)
534 msgs[1].buf[j] = (cmd >> (8 * i)) & 0xff;
541 /* high-level-controller composite write+write, m[0]len<=2, m[1]len<=8 */
542 static int octeon_i2c_hlc_comp_write(struct octeon_i2c *i2c, struct i2c_msg *msgs)
544 bool set_ext = false;
548 octeon_i2c_hlc_enable(i2c);
550 cmd = SW_TWSI_V | SW_TWSI_SOVR;
552 cmd |= (u64)(msgs[1].len - 1) << SW_TWSI_SIZE_SHIFT;
554 cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT;
556 if (msgs[0].flags & I2C_M_TEN)
557 cmd |= SW_TWSI_OP_10_IA;
559 cmd |= SW_TWSI_OP_7_IA;
561 if (msgs[0].len == 2) {
563 ext |= (u64)msgs[0].buf[0] << SW_TWSI_IA_SHIFT;
565 cmd |= (u64)msgs[0].buf[1] << SW_TWSI_IA_SHIFT;
567 cmd |= (u64)msgs[0].buf[0] << SW_TWSI_IA_SHIFT;
570 for (i = 0, j = msgs[1].len - 1; i < msgs[1].len && i < 4; i++, j--)
571 cmd |= (u64)msgs[1].buf[j] << (8 * i);
573 if (msgs[1].len > 4) {
574 for (i = 0; i < msgs[1].len - 4 && i < 4; i++, j--)
575 ext |= (u64)msgs[1].buf[j] << (8 * i);
579 octeon_i2c_writeq_flush(ext, i2c->twsi_base + SW_TWSI_EXT);
581 octeon_i2c_hlc_int_clear(i2c);
582 octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI);
584 ret = octeon_i2c_hlc_wait(i2c);
588 cmd = __raw_readq(i2c->twsi_base + SW_TWSI);
589 if ((cmd & SW_TWSI_R) == 0)
592 ret = octeon_i2c_check_status(i2c, false);
598 /* calculate and set clock divisors */
599 static void octeon_i2c_set_clock(struct octeon_i2c *i2c)
601 int tclk, thp_base, inc, thp_idx, mdiv_idx, ndiv_idx, foscl, diff;
602 int thp = 0x18, mdiv = 2, ndiv = 0, delta_hz = 1000000;
604 for (ndiv_idx = 0; ndiv_idx < 8 && delta_hz != 0; ndiv_idx++) {
606 * An mdiv value of less than 2 seems to not work well
607 * with ds1337 RTCs, so we constrain it to larger values.
609 for (mdiv_idx = 15; mdiv_idx >= 2 && delta_hz != 0; mdiv_idx--) {
611 * For given ndiv and mdiv values check the
612 * two closest thp values.
614 tclk = i2c->twsi_freq * (mdiv_idx + 1) * 10;
615 tclk *= (1 << ndiv_idx);
616 thp_base = (i2c->sys_freq / (tclk * 2)) - 1;
618 for (inc = 0; inc <= 1; inc++) {
619 thp_idx = thp_base + inc;
620 if (thp_idx < 5 || thp_idx > 0xff)
623 foscl = i2c->sys_freq / (2 * (thp_idx + 1));
624 foscl = foscl / (1 << ndiv_idx);
625 foscl = foscl / (mdiv_idx + 1) / 10;
626 diff = abs(foscl - i2c->twsi_freq);
627 if (diff < delta_hz) {
636 octeon_i2c_reg_write(i2c, SW_TWSI_OP_TWSI_CLK, thp);
637 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_CLKCTL, (mdiv << 3) | ndiv);
640 static int octeon_i2c_init_lowlevel(struct octeon_i2c *i2c)
645 /* reset controller */
646 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_RST, 0);
648 for (tries = 10; tries && status != STAT_IDLE; tries--) {
650 status = octeon_i2c_stat_read(i2c);
651 if (status == STAT_IDLE)
655 if (status != STAT_IDLE) {
656 dev_err(i2c->dev, "%s: TWSI_RST failed! (0x%x)\n",
661 /* toggle twice to force both teardowns */
662 octeon_i2c_hlc_enable(i2c);
663 octeon_i2c_hlc_disable(i2c);
667 static int octeon_i2c_recovery(struct octeon_i2c *i2c)
671 ret = i2c_recover_bus(&i2c->adap);
673 /* recover failed, try hardware re-init */
674 ret = octeon_i2c_init_lowlevel(i2c);
679 * octeon_i2c_start - send START to the bus
680 * @i2c: The struct octeon_i2c
682 * Returns 0 on success, otherwise a negative errno.
684 static int octeon_i2c_start(struct octeon_i2c *i2c)
689 octeon_i2c_hlc_disable(i2c);
691 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STA);
692 ret = octeon_i2c_wait(i2c);
696 stat = octeon_i2c_stat_read(i2c);
697 if (stat == STAT_START || stat == STAT_REP_START)
698 /* START successful, bail out */
702 /* START failed, try to recover */
703 ret = octeon_i2c_recovery(i2c);
704 return (ret) ? ret : -EAGAIN;
707 /* send STOP to the bus */
708 static void octeon_i2c_stop(struct octeon_i2c *i2c)
710 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STP);
714 * octeon_i2c_write - send data to the bus via low-level controller
715 * @i2c: The struct octeon_i2c
716 * @target: Target address
717 * @data: Pointer to the data to be sent
718 * @length: Length of the data
720 * The address is sent over the bus, then the data.
722 * Returns 0 on success, otherwise a negative errno.
724 static int octeon_i2c_write(struct octeon_i2c *i2c, int target,
725 const u8 *data, int length)
729 octeon_i2c_data_write(i2c, target << 1);
730 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
732 result = octeon_i2c_wait(i2c);
736 for (i = 0; i < length; i++) {
737 result = octeon_i2c_check_status(i2c, false);
741 octeon_i2c_data_write(i2c, data[i]);
742 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
744 result = octeon_i2c_wait(i2c);
753 * octeon_i2c_read - receive data from the bus via low-level controller
754 * @i2c: The struct octeon_i2c
755 * @target: Target address
756 * @data: Pointer to the location to store the data
757 * @rlength: Length of the data
758 * @recv_len: flag for length byte
760 * The address is sent over the bus, then the data is read.
762 * Returns 0 on success, otherwise a negative errno.
764 static int octeon_i2c_read(struct octeon_i2c *i2c, int target,
765 u8 *data, u16 *rlength, bool recv_len)
767 int i, result, length = *rlength;
768 bool final_read = false;
773 octeon_i2c_data_write(i2c, (target << 1) | 1);
774 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
776 result = octeon_i2c_wait(i2c);
781 result = octeon_i2c_check_status(i2c, false);
785 for (i = 0; i < length; i++) {
786 /* for the last byte TWSI_CTL_AAK must not be set */
790 /* clear iflg to allow next event */
792 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB);
794 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_AAK);
796 result = octeon_i2c_wait(i2c);
800 data[i] = octeon_i2c_data_read(i2c);
801 if (recv_len && i == 0) {
802 if (data[i] > I2C_SMBUS_BLOCK_MAX + 1) {
804 "%s: read len > I2C_SMBUS_BLOCK_MAX %d\n",
811 result = octeon_i2c_check_status(i2c, final_read);
820 * octeon_i2c_xfer - The driver's master_xfer function
821 * @adap: Pointer to the i2c_adapter structure
822 * @msgs: Pointer to the messages to be processed
823 * @num: Length of the MSGS array
825 * Returns the number of messages processed, or a negative errno on failure.
827 static int octeon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
830 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
834 if (msgs[0].len > 0 && msgs[0].len <= 8) {
835 if (msgs[0].flags & I2C_M_RD)
836 ret = octeon_i2c_hlc_read(i2c, msgs);
838 ret = octeon_i2c_hlc_write(i2c, msgs);
841 } else if (num == 2) {
842 if ((msgs[0].flags & I2C_M_RD) == 0 &&
843 (msgs[1].flags & I2C_M_RECV_LEN) == 0 &&
844 msgs[0].len > 0 && msgs[0].len <= 2 &&
845 msgs[1].len > 0 && msgs[1].len <= 8 &&
846 msgs[0].addr == msgs[1].addr) {
847 if (msgs[1].flags & I2C_M_RD)
848 ret = octeon_i2c_hlc_comp_read(i2c, msgs);
850 ret = octeon_i2c_hlc_comp_write(i2c, msgs);
855 for (i = 0; ret == 0 && i < num; i++) {
856 struct i2c_msg *pmsg = &msgs[i];
858 ret = octeon_i2c_start(i2c);
862 if (pmsg->flags & I2C_M_RD)
863 ret = octeon_i2c_read(i2c, pmsg->addr, pmsg->buf,
864 &pmsg->len, pmsg->flags & I2C_M_RECV_LEN);
866 ret = octeon_i2c_write(i2c, pmsg->addr, pmsg->buf,
869 octeon_i2c_stop(i2c);
871 return (ret != 0) ? ret : num;
874 static int octeon_i2c_get_scl(struct i2c_adapter *adap)
876 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
879 state = octeon_i2c_read_int(i2c);
880 return state & TWSI_INT_SCL;
883 static void octeon_i2c_set_scl(struct i2c_adapter *adap, int val)
885 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
887 octeon_i2c_write_int(i2c, TWSI_INT_SCL_OVR);
890 static int octeon_i2c_get_sda(struct i2c_adapter *adap)
892 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
895 state = octeon_i2c_read_int(i2c);
896 return state & TWSI_INT_SDA;
899 static void octeon_i2c_prepare_recovery(struct i2c_adapter *adap)
901 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
904 * The stop resets the state machine, does not _transmit_ STOP unless
907 octeon_i2c_stop(i2c);
909 octeon_i2c_hlc_disable(i2c);
910 octeon_i2c_write_int(i2c, 0);
913 static void octeon_i2c_unprepare_recovery(struct i2c_adapter *adap)
915 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
917 octeon_i2c_write_int(i2c, 0);
920 static struct i2c_bus_recovery_info octeon_i2c_recovery_info = {
921 .recover_bus = i2c_generic_scl_recovery,
922 .get_scl = octeon_i2c_get_scl,
923 .set_scl = octeon_i2c_set_scl,
924 .get_sda = octeon_i2c_get_sda,
925 .prepare_recovery = octeon_i2c_prepare_recovery,
926 .unprepare_recovery = octeon_i2c_unprepare_recovery,
929 static u32 octeon_i2c_functionality(struct i2c_adapter *adap)
931 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
932 I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_SMBUS_BLOCK_PROC_CALL;
935 static const struct i2c_algorithm octeon_i2c_algo = {
936 .master_xfer = octeon_i2c_xfer,
937 .functionality = octeon_i2c_functionality,
940 static struct i2c_adapter octeon_i2c_ops = {
941 .owner = THIS_MODULE,
942 .name = "OCTEON adapter",
943 .algo = &octeon_i2c_algo,
946 static int octeon_i2c_probe(struct platform_device *pdev)
948 struct device_node *node = pdev->dev.of_node;
949 struct resource *res_mem;
950 struct octeon_i2c *i2c;
953 /* All adaptors have an irq. */
954 irq = platform_get_irq(pdev, 0);
958 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
963 i2c->dev = &pdev->dev;
965 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
966 i2c->twsi_base = devm_ioremap_resource(&pdev->dev, res_mem);
967 if (IS_ERR(i2c->twsi_base)) {
968 result = PTR_ERR(i2c->twsi_base);
973 * "clock-rate" is a legacy binding, the official binding is
974 * "clock-frequency". Try the official one first and then
975 * fall back if it doesn't exist.
977 if (of_property_read_u32(node, "clock-frequency", &i2c->twsi_freq) &&
978 of_property_read_u32(node, "clock-rate", &i2c->twsi_freq)) {
980 "no I2C 'clock-rate' or 'clock-frequency' property\n");
985 i2c->sys_freq = octeon_get_io_clock_rate();
987 init_waitqueue_head(&i2c->queue);
991 result = devm_request_irq(&pdev->dev, i2c->irq,
992 octeon_i2c_isr, 0, DRV_NAME, i2c);
994 dev_err(i2c->dev, "failed to attach interrupt\n");
998 result = octeon_i2c_init_lowlevel(i2c);
1000 dev_err(i2c->dev, "init low level failed\n");
1004 octeon_i2c_set_clock(i2c);
1006 i2c->adap = octeon_i2c_ops;
1007 i2c->adap.timeout = msecs_to_jiffies(2);
1008 i2c->adap.retries = 5;
1009 i2c->adap.bus_recovery_info = &octeon_i2c_recovery_info;
1010 i2c->adap.dev.parent = &pdev->dev;
1011 i2c->adap.dev.of_node = node;
1012 i2c_set_adapdata(&i2c->adap, i2c);
1013 platform_set_drvdata(pdev, i2c);
1015 result = i2c_add_adapter(&i2c->adap);
1017 dev_err(i2c->dev, "failed to add adapter\n");
1020 dev_info(i2c->dev, "probed\n");
1027 static int octeon_i2c_remove(struct platform_device *pdev)
1029 struct octeon_i2c *i2c = platform_get_drvdata(pdev);
1031 i2c_del_adapter(&i2c->adap);
1035 static const struct of_device_id octeon_i2c_match[] = {
1036 { .compatible = "cavium,octeon-3860-twsi", },
1039 MODULE_DEVICE_TABLE(of, octeon_i2c_match);
1041 static struct platform_driver octeon_i2c_driver = {
1042 .probe = octeon_i2c_probe,
1043 .remove = octeon_i2c_remove,
1046 .of_match_table = octeon_i2c_match,
1050 module_platform_driver(octeon_i2c_driver);
1052 MODULE_AUTHOR("Michael Lawnick <michael.lawnick.ext@nsn.com>");
1053 MODULE_DESCRIPTION("I2C-Bus adapter for Cavium OCTEON processors");
1054 MODULE_LICENSE("GPL");