16 #include <linux/kernel.h>
17 #include <linux/module.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
23 #include <linux/i2c.h>
29 #define DRV_NAME "i2c-octeon"
32 #define DRV_VERSION "2.0"
39 #define SW_TWSI_V 0x8000000000000000ull
40 #define SW_TWSI_EOP_TWSI_DATA 0x0C00000100000000ull
41 #define SW_TWSI_EOP_TWSI_CTL 0x0C00000200000000ull
42 #define SW_TWSI_EOP_TWSI_CLKCTL 0x0C00000300000000ull
43 #define SW_TWSI_EOP_TWSI_STAT 0x0C00000300000000ull
44 #define SW_TWSI_EOP_TWSI_RST 0x0C00000700000000ull
45 #define SW_TWSI_OP_TWSI_CLK 0x0800000000000000ull
46 #define SW_TWSI_R 0x0100000000000000ull
49 #define TWSI_CTL_CE 0x80
50 #define TWSI_CTL_ENAB 0x40
51 #define TWSI_CTL_STA 0x20
52 #define TWSI_CTL_STP 0x10
53 #define TWSI_CTL_IFLG 0x08
54 #define TWSI_CTL_AAK 0x04
57 #define STAT_START 0x08
58 #define STAT_RSTART 0x10
59 #define STAT_TXADDR_ACK 0x18
60 #define STAT_TXDATA_ACK 0x28
61 #define STAT_RXADDR_ACK 0x40
62 #define STAT_RXDATA_ACK 0x50
63 #define STAT_IDLE 0xF8
85 static void octeon_i2c_write_sw(
struct octeon_i2c *i2c,
123 static void octeon_i2c_write_int(
struct octeon_i2c *i2c,
u64 data)
136 static void octeon_i2c_int_enable(
struct octeon_i2c *i2c)
138 octeon_i2c_write_int(i2c, 0x40);
145 static void octeon_i2c_int_disable(
struct octeon_i2c *i2c)
147 octeon_i2c_write_int(i2c, 0);
158 static void octeon_i2c_unblock(
struct octeon_i2c *i2c)
163 for (i = 0; i < 9; i++) {
164 octeon_i2c_write_int(i2c, 0x0);
166 octeon_i2c_write_int(i2c, 0x200);
169 octeon_i2c_write_int(i2c, 0x300);
171 octeon_i2c_write_int(i2c, 0x100);
173 octeon_i2c_write_int(i2c, 0x0);
185 octeon_i2c_int_disable(i2c);
192 static int octeon_i2c_test_iflg(
struct octeon_i2c *i2c)
203 static int octeon_i2c_wait(
struct octeon_i2c *i2c)
207 octeon_i2c_int_enable(i2c);
210 octeon_i2c_test_iflg(i2c),
213 octeon_i2c_int_disable(i2c);
216 dev_dbg(i2c->
dev,
"%s: wait interrupted\n", __func__);
218 }
else if (result == 0) {
232 static int octeon_i2c_start(
struct octeon_i2c *i2c)
240 result = octeon_i2c_wait(i2c);
248 octeon_i2c_unblock(i2c);
252 result = octeon_i2c_wait(i2c);
260 dev_err(i2c->
dev,
"%s: bad status (0x%x)\n", __func__, data);
273 static int octeon_i2c_stop(
struct octeon_i2c *i2c)
283 dev_err(i2c->
dev,
"%s: bad status(0x%x)\n", __func__, data);
306 result = octeon_i2c_start(i2c);
313 result = octeon_i2c_wait(i2c);
317 for (i = 0; i <
length; i++) {
321 "%s: bad status before write (0x%x)\n",
329 result = octeon_i2c_wait(i2c);
348 static int octeon_i2c_read(
struct octeon_i2c *i2c,
int target,
349 u8 *data,
int length)
357 result = octeon_i2c_start(i2c);
364 result = octeon_i2c_wait(i2c);
368 for (i = 0; i <
length; i++) {
372 "%s: bad status before read (0x%x)\n",
384 result = octeon_i2c_wait(i2c);
409 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
411 for (i = 0; ret == 0 && i < num; i++) {
414 "Doing %s %d byte(s) to/from 0x%02x - %d of %d messages\n",
416 pmsg->
len, pmsg->
addr, i + 1, num);
418 ret = octeon_i2c_read(i2c, pmsg->
addr, pmsg->
buf,
421 ret = octeon_i2c_write(i2c, pmsg->
addr, pmsg->
buf,
424 octeon_i2c_stop(i2c);
426 return (ret != 0) ? ret : num;
435 .master_xfer = octeon_i2c_xfer,
436 .functionality = octeon_i2c_functionality,
441 .name =
"OCTEON adapter",
442 .algo = &octeon_i2c_algo,
451 int tclk, thp_base,
inc, thp_idx, mdiv_idx, ndiv_idx, foscl, diff;
452 int thp = 0x18, mdiv = 2, ndiv = 0, delta_hz = 1000000;
454 for (ndiv_idx = 0; ndiv_idx < 8 && delta_hz != 0; ndiv_idx++) {
460 for (mdiv_idx = 15; mdiv_idx >= 2 && delta_hz != 0; mdiv_idx--) {
465 tclk = i2c->
twsi_freq * (mdiv_idx + 1) * 10;
466 tclk *= (1 << ndiv_idx);
467 thp_base = (i2c->
sys_freq / (tclk * 2)) - 1;
468 for (inc = 0; inc <= 1; inc++) {
469 thp_idx = thp_base +
inc;
470 if (thp_idx < 5 || thp_idx > 0xff)
473 foscl = i2c->
sys_freq / (2 * (thp_idx + 1));
474 foscl = foscl / (1 << ndiv_idx);
475 foscl = foscl / (mdiv_idx + 1) / 10;
477 if (diff < delta_hz) {
503 for (tries = 10; tries; tries--) {
509 dev_err(i2c->
dev,
"%s: TWSI_RST failed! (0x%x)\n", __func__, status);
534 if (res_mem ==
NULL) {
535 dev_err(i2c->
dev,
"found no memory resource\n");
540 i2c->
regsize = resource_size(res_mem);
547 if (of_property_read_u32(pdev->
dev.of_node,
549 of_property_read_u32(pdev->
dev.of_node,
552 "no I2C 'clock-rate' or 'clock-frequency' property\n");
561 dev_err(i2c->
dev,
"request_mem_region failed\n");
570 result = devm_request_irq(&pdev->
dev, i2c->
irq,
573 dev_err(i2c->
dev,
"failed to attach interrupt\n");
577 result = octeon_i2c_initlowlevel(i2c);
583 result = octeon_i2c_setclock(i2c);
589 i2c->
adap = octeon_i2c_ops;
590 i2c->
adap.dev.parent = &pdev->
dev;
591 i2c->
adap.dev.of_node = pdev->
dev.of_node;
592 i2c_set_adapdata(&i2c->
adap, i2c);
593 platform_set_drvdata(pdev, i2c);
607 platform_set_drvdata(pdev,
NULL);
614 struct octeon_i2c *i2c = platform_get_drvdata(pdev);
617 platform_set_drvdata(pdev,
NULL);
623 .compatible =
"cavium,octeon-3860-twsi",
630 .probe = octeon_i2c_probe,
635 .of_match_table = octeon_i2c_match,