2 * (C) Copyright 2009-2010
3 * Nokia Siemens Networks, michael.lawnick.ext@nsn.com
5 * Portions Copyright (C) 2010 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/kernel.h>
15 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
21 #include <linux/i2c.h>
22 #include <linux/interrupt.h>
23 #include <linux/delay.h>
24 #include <linux/platform_device.h>
26 #include <asm/octeon/octeon.h>
28 #define DRV_NAME "i2c-octeon"
30 /* The previous out-of-tree version was implicitly version 1.0. */
31 #define DRV_VERSION "2.0"
33 /* register offsets */
37 /* Controller command patterns */
38 #define SW_TWSI_V 0x8000000000000000ull
39 #define SW_TWSI_EOP_TWSI_DATA 0x0C00000100000000ull
40 #define SW_TWSI_EOP_TWSI_CTL 0x0C00000200000000ull
41 #define SW_TWSI_EOP_TWSI_CLKCTL 0x0C00000300000000ull
42 #define SW_TWSI_EOP_TWSI_STAT 0x0C00000300000000ull
43 #define SW_TWSI_EOP_TWSI_RST 0x0C00000700000000ull
44 #define SW_TWSI_OP_TWSI_CLK 0x0800000000000000ull
45 #define SW_TWSI_R 0x0100000000000000ull
47 /* Controller command and status bits */
48 #define TWSI_CTL_CE 0x80
49 #define TWSI_CTL_ENAB 0x40
50 #define TWSI_CTL_STA 0x20
51 #define TWSI_CTL_STP 0x10
52 #define TWSI_CTL_IFLG 0x08
53 #define TWSI_CTL_AAK 0x04
55 /* Some status values */
56 #define STAT_START 0x08
57 #define STAT_RSTART 0x10
58 #define STAT_TXADDR_ACK 0x18
59 #define STAT_TXDATA_ACK 0x28
60 #define STAT_RXADDR_ACK 0x40
61 #define STAT_RXDATA_ACK 0x50
62 #define STAT_IDLE 0xF8
65 wait_queue_head_t queue;
66 struct i2c_adapter adap;
70 resource_size_t twsi_phys;
71 void __iomem *twsi_base;
72 resource_size_t regsize;
77 * octeon_i2c_write_sw - write an I2C core register.
78 * @i2c: The struct octeon_i2c.
79 * @eop_reg: Register selector.
80 * @data: Value to be written.
82 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
84 static void octeon_i2c_write_sw(struct octeon_i2c *i2c,
90 __raw_writeq(SW_TWSI_V | eop_reg | data, i2c->twsi_base + SW_TWSI);
92 tmp = __raw_readq(i2c->twsi_base + SW_TWSI);
93 } while ((tmp & SW_TWSI_V) != 0);
97 * octeon_i2c_read_sw - write an I2C core register.
98 * @i2c: The struct octeon_i2c.
99 * @eop_reg: Register selector.
103 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
105 static u8 octeon_i2c_read_sw(struct octeon_i2c *i2c, u64 eop_reg)
109 __raw_writeq(SW_TWSI_V | eop_reg | SW_TWSI_R, i2c->twsi_base + SW_TWSI);
111 tmp = __raw_readq(i2c->twsi_base + SW_TWSI);
112 } while ((tmp & SW_TWSI_V) != 0);
118 * octeon_i2c_write_int - write the TWSI_INT register
119 * @i2c: The struct octeon_i2c.
120 * @data: Value to be written.
122 static void octeon_i2c_write_int(struct octeon_i2c *i2c, u64 data)
126 __raw_writeq(data, i2c->twsi_base + TWSI_INT);
127 tmp = __raw_readq(i2c->twsi_base + TWSI_INT);
131 * octeon_i2c_int_enable - enable the TS interrupt.
132 * @i2c: The struct octeon_i2c.
134 * The interrupt will be asserted when there is non-STAT_IDLE state in
135 * the SW_TWSI_EOP_TWSI_STAT register.
137 static void octeon_i2c_int_enable(struct octeon_i2c *i2c)
139 octeon_i2c_write_int(i2c, 0x40);
143 * octeon_i2c_int_disable - disable the TS interrupt.
144 * @i2c: The struct octeon_i2c.
146 static void octeon_i2c_int_disable(struct octeon_i2c *i2c)
148 octeon_i2c_write_int(i2c, 0);
152 * octeon_i2c_unblock - unblock the bus.
153 * @i2c: The struct octeon_i2c.
155 * If there was a reset while a device was driving 0 to bus,
156 * bus is blocked. We toggle it free manually by some clock
157 * cycles and send a stop.
159 static void octeon_i2c_unblock(struct octeon_i2c *i2c)
163 dev_dbg(i2c->dev, "%s\n", __func__);
164 for (i = 0; i < 9; i++) {
165 octeon_i2c_write_int(i2c, 0x0);
167 octeon_i2c_write_int(i2c, 0x200);
170 octeon_i2c_write_int(i2c, 0x300);
172 octeon_i2c_write_int(i2c, 0x100);
174 octeon_i2c_write_int(i2c, 0x0);
178 * octeon_i2c_isr - the interrupt service routine.
179 * @int: The irq, unused.
180 * @dev_id: Our struct octeon_i2c.
182 static irqreturn_t octeon_i2c_isr(int irq, void *dev_id)
184 struct octeon_i2c *i2c = dev_id;
186 octeon_i2c_int_disable(i2c);
187 wake_up_interruptible(&i2c->queue);
193 static int octeon_i2c_test_iflg(struct octeon_i2c *i2c)
195 return (octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_CTL) & TWSI_CTL_IFLG) != 0;
199 * octeon_i2c_wait - wait for the IFLG to be set.
200 * @i2c: The struct octeon_i2c.
202 * Returns 0 on success, otherwise a negative errno.
204 static int octeon_i2c_wait(struct octeon_i2c *i2c)
208 octeon_i2c_int_enable(i2c);
210 result = wait_event_interruptible_timeout(i2c->queue,
211 octeon_i2c_test_iflg(i2c),
214 octeon_i2c_int_disable(i2c);
217 dev_dbg(i2c->dev, "%s: wait interrupted\n", __func__);
219 } else if (result == 0) {
220 dev_dbg(i2c->dev, "%s: timeout\n", __func__);
228 * octeon_i2c_start - send START to the bus.
229 * @i2c: The struct octeon_i2c.
231 * Returns 0 on success, otherwise a negative errno.
233 static int octeon_i2c_start(struct octeon_i2c *i2c)
238 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
239 TWSI_CTL_ENAB | TWSI_CTL_STA);
241 result = octeon_i2c_wait(i2c);
243 if (octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT) == STAT_IDLE) {
245 * Controller refused to send start flag May
246 * be a client is holding SDA low - let's try
249 octeon_i2c_unblock(i2c);
250 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
251 TWSI_CTL_ENAB | TWSI_CTL_STA);
253 result = octeon_i2c_wait(i2c);
259 data = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
260 if ((data != STAT_START) && (data != STAT_RSTART)) {
261 dev_err(i2c->dev, "%s: bad status (0x%x)\n", __func__, data);
269 * octeon_i2c_stop - send STOP to the bus.
270 * @i2c: The struct octeon_i2c.
272 * Returns 0 on success, otherwise a negative errno.
274 static int octeon_i2c_stop(struct octeon_i2c *i2c)
278 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
279 TWSI_CTL_ENAB | TWSI_CTL_STP);
281 data = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
283 if (data != STAT_IDLE) {
284 dev_err(i2c->dev, "%s: bad status(0x%x)\n", __func__, data);
291 * octeon_i2c_write - send data to the bus.
292 * @i2c: The struct octeon_i2c.
293 * @target: Target address.
294 * @data: Pointer to the data to be sent.
295 * @length: Length of the data.
297 * The address is sent over the bus, then the data.
299 * Returns 0 on success, otherwise a negative errno.
301 static int octeon_i2c_write(struct octeon_i2c *i2c, int target,
302 const u8 *data, int length)
307 result = octeon_i2c_start(i2c);
311 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_DATA, target << 1);
312 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB);
314 result = octeon_i2c_wait(i2c);
318 for (i = 0; i < length; i++) {
319 tmp = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
320 if ((tmp != STAT_TXADDR_ACK) && (tmp != STAT_TXDATA_ACK)) {
322 "%s: bad status before write (0x%x)\n",
327 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_DATA, data[i]);
328 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB);
330 result = octeon_i2c_wait(i2c);
339 * octeon_i2c_read - receive data from the bus.
340 * @i2c: The struct octeon_i2c.
341 * @target: Target address.
342 * @data: Pointer to the location to store the datae .
343 * @length: Length of the data.
345 * The address is sent over the bus, then the data is read.
347 * Returns 0 on success, otherwise a negative errno.
349 static int octeon_i2c_read(struct octeon_i2c *i2c, int target,
350 u8 *data, int length)
358 result = octeon_i2c_start(i2c);
362 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_DATA, (target<<1) | 1);
363 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB);
365 result = octeon_i2c_wait(i2c);
369 for (i = 0; i < length; i++) {
370 tmp = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
371 if ((tmp != STAT_RXDATA_ACK) && (tmp != STAT_RXADDR_ACK)) {
373 "%s: bad status before read (0x%x)\n",
379 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
380 TWSI_CTL_ENAB | TWSI_CTL_AAK);
382 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
385 result = octeon_i2c_wait(i2c);
389 data[i] = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_DATA);
395 * octeon_i2c_xfer - The driver's master_xfer function.
396 * @adap: Pointer to the i2c_adapter structure.
397 * @msgs: Pointer to the messages to be processed.
398 * @num: Length of the MSGS array.
400 * Returns the number of messages processed, or a negative errno on
403 static int octeon_i2c_xfer(struct i2c_adapter *adap,
404 struct i2c_msg *msgs,
407 struct i2c_msg *pmsg;
410 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
412 for (i = 0; ret == 0 && i < num; i++) {
415 "Doing %s %d byte(s) to/from 0x%02x - %d of %d messages\n",
416 pmsg->flags & I2C_M_RD ? "read" : "write",
417 pmsg->len, pmsg->addr, i + 1, num);
418 if (pmsg->flags & I2C_M_RD)
419 ret = octeon_i2c_read(i2c, pmsg->addr, pmsg->buf,
422 ret = octeon_i2c_write(i2c, pmsg->addr, pmsg->buf,
425 octeon_i2c_stop(i2c);
427 return (ret != 0) ? ret : num;
430 static u32 octeon_i2c_functionality(struct i2c_adapter *adap)
432 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
435 static const struct i2c_algorithm octeon_i2c_algo = {
436 .master_xfer = octeon_i2c_xfer,
437 .functionality = octeon_i2c_functionality,
440 static struct i2c_adapter octeon_i2c_ops = {
441 .owner = THIS_MODULE,
442 .name = "OCTEON adapter",
443 .algo = &octeon_i2c_algo,
448 * octeon_i2c_setclock - Calculate and set clock divisors.
450 static int __devinit octeon_i2c_setclock(struct octeon_i2c *i2c)
452 int tclk, thp_base, inc, thp_idx, mdiv_idx, ndiv_idx, foscl, diff;
453 int thp = 0x18, mdiv = 2, ndiv = 0, delta_hz = 1000000;
455 for (ndiv_idx = 0; ndiv_idx < 8 && delta_hz != 0; ndiv_idx++) {
457 * An mdiv value of less than 2 seems to not work well
458 * with ds1337 RTCs, so we constrain it to larger
461 for (mdiv_idx = 15; mdiv_idx >= 2 && delta_hz != 0; mdiv_idx--) {
463 * For given ndiv and mdiv values check the
464 * two closest thp values.
466 tclk = i2c->twsi_freq * (mdiv_idx + 1) * 10;
467 tclk *= (1 << ndiv_idx);
468 thp_base = (i2c->sys_freq / (tclk * 2)) - 1;
469 for (inc = 0; inc <= 1; inc++) {
470 thp_idx = thp_base + inc;
471 if (thp_idx < 5 || thp_idx > 0xff)
474 foscl = i2c->sys_freq / (2 * (thp_idx + 1));
475 foscl = foscl / (1 << ndiv_idx);
476 foscl = foscl / (mdiv_idx + 1) / 10;
477 diff = abs(foscl - i2c->twsi_freq);
478 if (diff < delta_hz) {
487 octeon_i2c_write_sw(i2c, SW_TWSI_OP_TWSI_CLK, thp);
488 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CLKCTL, (mdiv << 3) | ndiv);
493 static int __devinit octeon_i2c_initlowlevel(struct octeon_i2c *i2c)
498 /* disable high level controller, enable bus access */
499 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB);
501 /* reset controller */
502 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_RST, 0);
504 for (tries = 10; tries; tries--) {
506 status = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
507 if (status == STAT_IDLE)
510 dev_err(i2c->dev, "%s: TWSI_RST failed! (0x%x)\n", __func__, status);
514 static int __devinit octeon_i2c_probe(struct platform_device *pdev)
517 struct octeon_i2c *i2c;
518 struct octeon_i2c_data *i2c_data;
519 struct resource *res_mem;
521 /* All adaptors have an irq. */
522 irq = platform_get_irq(pdev, 0);
526 i2c = kzalloc(sizeof(*i2c), GFP_KERNEL);
528 dev_err(&pdev->dev, "kzalloc failed\n");
532 i2c->dev = &pdev->dev;
533 i2c_data = pdev->dev.platform_data;
535 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
537 if (res_mem == NULL) {
538 dev_err(i2c->dev, "found no memory resource\n");
543 if (i2c_data == NULL) {
544 dev_err(i2c->dev, "no I2C frequency data\n");
549 i2c->twsi_phys = res_mem->start;
550 i2c->regsize = resource_size(res_mem);
551 i2c->twsi_freq = i2c_data->i2c_freq;
552 i2c->sys_freq = i2c_data->sys_freq;
554 if (!request_mem_region(i2c->twsi_phys, i2c->regsize, res_mem->name)) {
555 dev_err(i2c->dev, "request_mem_region failed\n");
558 i2c->twsi_base = ioremap(i2c->twsi_phys, i2c->regsize);
560 init_waitqueue_head(&i2c->queue);
564 result = request_irq(i2c->irq, octeon_i2c_isr, 0, DRV_NAME, i2c);
566 dev_err(i2c->dev, "failed to attach interrupt\n");
570 result = octeon_i2c_initlowlevel(i2c);
572 dev_err(i2c->dev, "init low level failed\n");
576 result = octeon_i2c_setclock(i2c);
578 dev_err(i2c->dev, "clock init failed\n");
582 i2c->adap = octeon_i2c_ops;
583 i2c->adap.dev.parent = &pdev->dev;
584 i2c->adap.nr = pdev->id >= 0 ? pdev->id : 0;
585 i2c_set_adapdata(&i2c->adap, i2c);
586 platform_set_drvdata(pdev, i2c);
588 result = i2c_add_numbered_adapter(&i2c->adap);
590 dev_err(i2c->dev, "failed to add adapter\n");
594 dev_info(i2c->dev, "version %s\n", DRV_VERSION);
599 platform_set_drvdata(pdev, NULL);
600 free_irq(i2c->irq, i2c);
602 iounmap(i2c->twsi_base);
603 release_mem_region(i2c->twsi_phys, i2c->regsize);
610 static int __devexit octeon_i2c_remove(struct platform_device *pdev)
612 struct octeon_i2c *i2c = platform_get_drvdata(pdev);
614 i2c_del_adapter(&i2c->adap);
615 platform_set_drvdata(pdev, NULL);
616 free_irq(i2c->irq, i2c);
617 iounmap(i2c->twsi_base);
618 release_mem_region(i2c->twsi_phys, i2c->regsize);
623 static struct platform_driver octeon_i2c_driver = {
624 .probe = octeon_i2c_probe,
625 .remove = __devexit_p(octeon_i2c_remove),
627 .owner = THIS_MODULE,
632 static int __init octeon_i2c_init(void)
636 rv = platform_driver_register(&octeon_i2c_driver);
640 static void __exit octeon_i2c_exit(void)
642 platform_driver_unregister(&octeon_i2c_driver);
645 MODULE_AUTHOR("Michael Lawnick <michael.lawnick.ext@nsn.com>");
646 MODULE_DESCRIPTION("I2C-Bus adapter for Cavium OCTEON processors");
647 MODULE_LICENSE("GPL");
648 MODULE_VERSION(DRV_VERSION);
649 MODULE_ALIAS("platform:" DRV_NAME);
651 module_init(octeon_i2c_init);
652 module_exit(octeon_i2c_exit);