2 * This driver implements I2C master functionality using the LSI API2C
5 * NOTE: The controller has a limitation in that it can only do transfers of
6 * maximum 255 bytes at a time. If a larger transfer is attempted, error code
7 * (-EINVAL) is returned.
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
13 #include <linux/clk.h>
14 #include <linux/clkdev.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/i2c.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/platform_device.h>
25 #define SCL_WAIT_TIMEOUT_NS 25000000
26 #define I2C_XFER_TIMEOUT (msecs_to_jiffies(250))
27 #define I2C_STOP_TIMEOUT (msecs_to_jiffies(100))
31 #define GLOBAL_CONTROL 0x00
32 #define GLOBAL_MST_EN BIT(0)
33 #define GLOBAL_SLV_EN BIT(1)
34 #define GLOBAL_IBML_EN BIT(2)
35 #define INTERRUPT_STATUS 0x04
36 #define INTERRUPT_ENABLE 0x08
37 #define INT_SLV BIT(1)
38 #define INT_MST BIT(0)
39 #define WAIT_TIMER_CONTROL 0x0c
41 #define WT_VALUE(_x) ((_x) & 0x7fff)
42 #define IBML_TIMEOUT 0x10
43 #define IBML_LOW_MEXT 0x14
44 #define IBML_LOW_SEXT 0x18
45 #define TIMER_CLOCK_DIV 0x1c
46 #define I2C_BUS_MONITOR 0x20
47 #define BM_SDAC BIT(3)
48 #define BM_SCLC BIT(2)
49 #define BM_SDAS BIT(1)
50 #define BM_SCLS BIT(0)
51 #define SOFT_RESET 0x24
52 #define MST_COMMAND 0x28
53 #define CMD_BUSY (1<<3)
54 #define CMD_MANUAL (0x00 | CMD_BUSY)
55 #define CMD_AUTO (0x01 | CMD_BUSY)
56 #define CMD_SEQUENCE (0x02 | CMD_BUSY)
57 #define MST_RX_XFER 0x2c
58 #define MST_TX_XFER 0x30
59 #define MST_ADDR_1 0x34
60 #define MST_ADDR_2 0x38
62 #define MST_TX_FIFO 0x40
63 #define MST_RX_FIFO 0x44
64 #define MST_INT_ENABLE 0x48
65 #define MST_INT_STATUS 0x4c
66 #define MST_STATUS_RFL (1 << 13) /* RX FIFO serivce */
67 #define MST_STATUS_TFL (1 << 12) /* TX FIFO service */
68 #define MST_STATUS_SNS (1 << 11) /* Manual mode done */
69 #define MST_STATUS_SS (1 << 10) /* Automatic mode done */
70 #define MST_STATUS_SCC (1 << 9) /* Stop complete */
71 #define MST_STATUS_IP (1 << 8) /* Invalid parameter */
72 #define MST_STATUS_TSS (1 << 7) /* Timeout */
73 #define MST_STATUS_AL (1 << 6) /* Arbitration lost */
74 #define MST_STATUS_ND (1 << 5) /* NAK on data phase */
75 #define MST_STATUS_NA (1 << 4) /* NAK on address phase */
76 #define MST_STATUS_NAK (MST_STATUS_NA | \
78 #define MST_STATUS_ERR (MST_STATUS_NAK | \
81 #define MST_TX_BYTES_XFRD 0x50
82 #define MST_RX_BYTES_XFRD 0x54
83 #define SCL_HIGH_PERIOD 0x80
84 #define SCL_LOW_PERIOD 0x84
85 #define SPIKE_FLTR_LEN 0x88
86 #define SDA_SETUP_TIME 0x8c
87 #define SDA_HOLD_TIME 0x90
90 * axxia_i2c_dev - I2C device context
91 * @base: pointer to register struct
92 * @msg: pointer to current message
93 * @msg_r: pointer to current read message (sequence transfer)
94 * @msg_xfrd: number of bytes transferred in tx_fifo
95 * @msg_xfrd_r: number of bytes transferred in rx_fifo
96 * @msg_err: error code for completed message
97 * @msg_complete: xfer completion object
98 * @dev: device reference
99 * @adapter: core i2c abstraction
100 * @i2c_clk: clock reference for i2c input clock
101 * @bus_clk_rate: current i2c bus clock rate
103 struct axxia_i2c_dev {
106 struct i2c_msg *msg_r;
110 struct completion msg_complete;
112 struct i2c_adapter adapter;
117 static void i2c_int_disable(struct axxia_i2c_dev *idev, u32 mask)
121 int_en = readl(idev->base + MST_INT_ENABLE);
122 writel(int_en & ~mask, idev->base + MST_INT_ENABLE);
125 static void i2c_int_enable(struct axxia_i2c_dev *idev, u32 mask)
129 int_en = readl(idev->base + MST_INT_ENABLE);
130 writel(int_en | mask, idev->base + MST_INT_ENABLE);
134 * ns_to_clk - Convert time (ns) to clock cycles for the given clock frequency.
136 static u32 ns_to_clk(u64 ns, u32 clk_mhz)
138 return div_u64(ns * clk_mhz, 1000);
141 static int axxia_i2c_init(struct axxia_i2c_dev *idev)
143 u32 divisor = clk_get_rate(idev->i2c_clk) / idev->bus_clk_rate;
144 u32 clk_mhz = clk_get_rate(idev->i2c_clk) / 1000000;
149 unsigned long timeout;
151 dev_dbg(idev->dev, "rate=%uHz per_clk=%uMHz -> ratio=1:%u\n",
152 idev->bus_clk_rate, clk_mhz, divisor);
154 /* Reset controller */
155 writel(0x01, idev->base + SOFT_RESET);
156 timeout = jiffies + msecs_to_jiffies(100);
157 while (readl(idev->base + SOFT_RESET) & 1) {
158 if (time_after(jiffies, timeout)) {
159 dev_warn(idev->dev, "Soft reset failed\n");
164 /* Enable Master Mode */
165 writel(0x1, idev->base + GLOBAL_CONTROL);
167 if (idev->bus_clk_rate <= 100000) {
168 /* Standard mode SCL 50/50, tSU:DAT = 250 ns */
169 t_high = divisor * 1 / 2;
170 t_low = divisor * 1 / 2;
171 t_setup = ns_to_clk(250, clk_mhz);
173 /* Fast mode SCL 33/66, tSU:DAT = 100 ns */
174 t_high = divisor * 1 / 3;
175 t_low = divisor * 2 / 3;
176 t_setup = ns_to_clk(100, clk_mhz);
180 writel(t_high, idev->base + SCL_HIGH_PERIOD);
182 writel(t_low, idev->base + SCL_LOW_PERIOD);
184 writel(t_setup, idev->base + SDA_SETUP_TIME);
185 /* SDA Hold Time, 300ns */
186 writel(ns_to_clk(300, clk_mhz), idev->base + SDA_HOLD_TIME);
187 /* Filter <50ns spikes */
188 writel(ns_to_clk(50, clk_mhz), idev->base + SPIKE_FLTR_LEN);
190 /* Configure Time-Out Registers */
191 tmo_clk = ns_to_clk(SCL_WAIT_TIMEOUT_NS, clk_mhz);
193 /* Find prescaler value that makes tmo_clk fit in 15-bits counter. */
194 for (prescale = 0; prescale < 15; ++prescale) {
195 if (tmo_clk <= 0x7fff)
199 if (tmo_clk > 0x7fff)
202 /* Prescale divider (log2) */
203 writel(prescale, idev->base + TIMER_CLOCK_DIV);
204 /* Timeout in divided clocks */
205 writel(WT_EN | WT_VALUE(tmo_clk), idev->base + WAIT_TIMER_CONTROL);
207 /* Mask all master interrupt bits */
208 i2c_int_disable(idev, ~0);
210 /* Interrupt enable */
211 writel(0x01, idev->base + INTERRUPT_ENABLE);
216 static int i2c_m_rd(const struct i2c_msg *msg)
218 return (msg->flags & I2C_M_RD) != 0;
221 static int i2c_m_ten(const struct i2c_msg *msg)
223 return (msg->flags & I2C_M_TEN) != 0;
226 static int i2c_m_recv_len(const struct i2c_msg *msg)
228 return (msg->flags & I2C_M_RECV_LEN) != 0;
232 * axxia_i2c_empty_rx_fifo - Fetch data from RX FIFO and update SMBus block
233 * transfer length if this is the first byte of such a transfer.
235 static int axxia_i2c_empty_rx_fifo(struct axxia_i2c_dev *idev)
237 struct i2c_msg *msg = idev->msg_r;
238 size_t rx_fifo_avail = readl(idev->base + MST_RX_FIFO);
239 int bytes_to_transfer = min(rx_fifo_avail, msg->len - idev->msg_xfrd_r);
241 while (bytes_to_transfer-- > 0) {
242 int c = readl(idev->base + MST_DATA);
244 if (idev->msg_xfrd_r == 0 && i2c_m_recv_len(msg)) {
246 * Check length byte for SMBus block read
248 if (c <= 0 || c > I2C_SMBUS_BLOCK_MAX) {
249 idev->msg_err = -EPROTO;
250 i2c_int_disable(idev, ~MST_STATUS_TSS);
251 complete(&idev->msg_complete);
255 writel(msg->len, idev->base + MST_RX_XFER);
257 msg->buf[idev->msg_xfrd_r++] = c;
264 * axxia_i2c_fill_tx_fifo - Fill TX FIFO from current message buffer.
265 * @return: Number of bytes left to transfer.
267 static int axxia_i2c_fill_tx_fifo(struct axxia_i2c_dev *idev)
269 struct i2c_msg *msg = idev->msg;
270 size_t tx_fifo_avail = FIFO_SIZE - readl(idev->base + MST_TX_FIFO);
271 int bytes_to_transfer = min(tx_fifo_avail, msg->len - idev->msg_xfrd);
272 int ret = msg->len - idev->msg_xfrd - bytes_to_transfer;
274 while (bytes_to_transfer-- > 0)
275 writel(msg->buf[idev->msg_xfrd++], idev->base + MST_DATA);
280 static irqreturn_t axxia_i2c_isr(int irq, void *_dev)
282 struct axxia_i2c_dev *idev = _dev;
285 if (!(readl(idev->base + INTERRUPT_STATUS) & INT_MST))
288 /* Read interrupt status bits */
289 status = readl(idev->base + MST_INT_STATUS);
292 dev_warn(idev->dev, "unexpected interrupt\n");
296 /* RX FIFO needs service? */
297 if (i2c_m_rd(idev->msg_r) && (status & MST_STATUS_RFL))
298 axxia_i2c_empty_rx_fifo(idev);
300 /* TX FIFO needs service? */
301 if (!i2c_m_rd(idev->msg) && (status & MST_STATUS_TFL)) {
302 if (axxia_i2c_fill_tx_fifo(idev) == 0)
303 i2c_int_disable(idev, MST_STATUS_TFL);
306 if (unlikely(status & MST_STATUS_ERR)) {
308 i2c_int_disable(idev, ~0);
309 if (status & MST_STATUS_AL)
310 idev->msg_err = -EAGAIN;
311 else if (status & MST_STATUS_NAK)
312 idev->msg_err = -ENXIO;
314 idev->msg_err = -EIO;
315 dev_dbg(idev->dev, "error %#x, addr=%#x rx=%u/%u tx=%u/%u\n",
318 readl(idev->base + MST_RX_BYTES_XFRD),
319 readl(idev->base + MST_RX_XFER),
320 readl(idev->base + MST_TX_BYTES_XFRD),
321 readl(idev->base + MST_TX_XFER));
322 complete(&idev->msg_complete);
323 } else if (status & MST_STATUS_SCC) {
325 i2c_int_disable(idev, ~MST_STATUS_TSS);
326 complete(&idev->msg_complete);
327 } else if (status & MST_STATUS_SNS) {
329 i2c_int_disable(idev, ~MST_STATUS_TSS);
330 if (i2c_m_rd(idev->msg_r) && idev->msg_xfrd_r < idev->msg_r->len)
331 axxia_i2c_empty_rx_fifo(idev);
332 complete(&idev->msg_complete);
333 } else if (status & MST_STATUS_SS) {
334 /* Auto/Sequence transfer done */
335 complete(&idev->msg_complete);
336 } else if (status & MST_STATUS_TSS) {
337 /* Transfer timeout */
338 idev->msg_err = -ETIMEDOUT;
339 i2c_int_disable(idev, ~MST_STATUS_TSS);
340 complete(&idev->msg_complete);
344 /* Clear interrupt */
345 writel(INT_MST, idev->base + INTERRUPT_STATUS);
350 static void axxia_i2c_set_addr(struct axxia_i2c_dev *idev, struct i2c_msg *msg)
354 if (i2c_m_ten(msg)) {
356 * addr_1: 5'b11110 | addr[9:8] | (R/nW)
359 addr_1 = 0xF0 | ((msg->addr >> 7) & 0x06);
361 addr_1 |= 1; /* Set the R/nW bit of the address */
362 addr_2 = msg->addr & 0xFF;
365 * addr_1: addr[6:0] | (R/nW)
368 addr_1 = i2c_8bit_addr_from_msg(msg);
372 writel(addr_1, idev->base + MST_ADDR_1);
373 writel(addr_2, idev->base + MST_ADDR_2);
376 /* The NAK interrupt will be sent _before_ issuing STOP command
377 * so the controller might still be busy processing it. No
378 * interrupt will be sent at the end so we have to poll for it
380 static int axxia_i2c_handle_seq_nak(struct axxia_i2c_dev *idev)
382 unsigned long timeout = jiffies + I2C_XFER_TIMEOUT;
385 if ((readl(idev->base + MST_COMMAND) & CMD_BUSY) == 0)
387 usleep_range(1, 100);
388 } while (time_before(jiffies, timeout));
393 static int axxia_i2c_xfer_seq(struct axxia_i2c_dev *idev, struct i2c_msg msgs[])
395 u32 int_mask = MST_STATUS_ERR | MST_STATUS_SS | MST_STATUS_RFL;
396 u32 rlen = i2c_m_recv_len(&msgs[1]) ? I2C_SMBUS_BLOCK_MAX : msgs[1].len;
397 unsigned long time_left;
399 axxia_i2c_set_addr(idev, &msgs[0]);
401 writel(msgs[0].len, idev->base + MST_TX_XFER);
402 writel(rlen, idev->base + MST_RX_XFER);
404 idev->msg = &msgs[0];
405 idev->msg_r = &msgs[1];
407 idev->msg_xfrd_r = 0;
408 axxia_i2c_fill_tx_fifo(idev);
410 writel(CMD_SEQUENCE, idev->base + MST_COMMAND);
412 reinit_completion(&idev->msg_complete);
413 i2c_int_enable(idev, int_mask);
415 time_left = wait_for_completion_timeout(&idev->msg_complete,
418 i2c_int_disable(idev, int_mask);
420 axxia_i2c_empty_rx_fifo(idev);
422 if (idev->msg_err == -ENXIO) {
423 if (axxia_i2c_handle_seq_nak(idev))
424 axxia_i2c_init(idev);
425 } else if (readl(idev->base + MST_COMMAND) & CMD_BUSY) {
426 dev_warn(idev->dev, "busy after xfer\n");
429 if (time_left == 0) {
430 idev->msg_err = -ETIMEDOUT;
431 i2c_recover_bus(&idev->adapter);
432 axxia_i2c_init(idev);
435 if (unlikely(idev->msg_err) && idev->msg_err != -ENXIO)
436 axxia_i2c_init(idev);
438 return idev->msg_err;
441 static int axxia_i2c_xfer_msg(struct axxia_i2c_dev *idev, struct i2c_msg *msg)
443 u32 int_mask = MST_STATUS_ERR | MST_STATUS_SNS;
444 u32 rx_xfer, tx_xfer;
445 unsigned long time_left;
446 unsigned int wt_value;
451 idev->msg_xfrd_r = 0;
452 reinit_completion(&idev->msg_complete);
454 axxia_i2c_set_addr(idev, msg);
457 /* I2C read transfer */
458 rx_xfer = i2c_m_recv_len(msg) ? I2C_SMBUS_BLOCK_MAX : msg->len;
461 /* I2C write transfer */
466 writel(rx_xfer, idev->base + MST_RX_XFER);
467 writel(tx_xfer, idev->base + MST_TX_XFER);
470 int_mask |= MST_STATUS_RFL;
471 else if (axxia_i2c_fill_tx_fifo(idev) != 0)
472 int_mask |= MST_STATUS_TFL;
474 wt_value = WT_VALUE(readl(idev->base + WAIT_TIMER_CONTROL));
475 /* Disable wait timer temporarly */
476 writel(wt_value, idev->base + WAIT_TIMER_CONTROL);
477 /* Check if timeout error happened */
481 /* Start manual mode */
482 writel(CMD_MANUAL, idev->base + MST_COMMAND);
484 writel(WT_EN | wt_value, idev->base + WAIT_TIMER_CONTROL);
486 i2c_int_enable(idev, int_mask);
488 time_left = wait_for_completion_timeout(&idev->msg_complete,
491 i2c_int_disable(idev, int_mask);
493 if (readl(idev->base + MST_COMMAND) & CMD_BUSY)
494 dev_warn(idev->dev, "busy after xfer\n");
496 if (time_left == 0) {
497 idev->msg_err = -ETIMEDOUT;
498 i2c_recover_bus(&idev->adapter);
499 axxia_i2c_init(idev);
503 if (unlikely(idev->msg_err) && idev->msg_err != -ENXIO &&
504 idev->msg_err != -ETIMEDOUT)
505 axxia_i2c_init(idev);
507 return idev->msg_err;
510 static int axxia_i2c_stop(struct axxia_i2c_dev *idev)
512 u32 int_mask = MST_STATUS_ERR | MST_STATUS_SCC | MST_STATUS_TSS;
513 unsigned long time_left;
515 reinit_completion(&idev->msg_complete);
518 writel(0xb, idev->base + MST_COMMAND);
519 i2c_int_enable(idev, int_mask);
520 time_left = wait_for_completion_timeout(&idev->msg_complete,
522 i2c_int_disable(idev, int_mask);
526 if (readl(idev->base + MST_COMMAND) & CMD_BUSY)
527 dev_warn(idev->dev, "busy after stop\n");
532 /* This function checks if the msgs[] array contains messages compatible with
533 * Sequence mode of operation. This mode assumes there will be exactly one
534 * write of non-zero length followed by exactly one read of non-zero length,
535 * both targeted at the same client device.
537 static bool axxia_i2c_sequence_ok(struct i2c_msg msgs[], int num)
539 return num == SEQ_LEN && !i2c_m_rd(&msgs[0]) && i2c_m_rd(&msgs[1]) &&
540 msgs[0].len > 0 && msgs[0].len <= FIFO_SIZE &&
541 msgs[1].len > 0 && msgs[0].addr == msgs[1].addr;
545 axxia_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
547 struct axxia_i2c_dev *idev = i2c_get_adapdata(adap);
553 if (axxia_i2c_sequence_ok(msgs, num)) {
554 ret = axxia_i2c_xfer_seq(idev, msgs);
555 return ret ? : SEQ_LEN;
558 i2c_int_enable(idev, MST_STATUS_TSS);
560 for (i = 0; ret == 0 && i < num; ++i)
561 ret = axxia_i2c_xfer_msg(idev, &msgs[i]);
563 axxia_i2c_stop(idev);
568 static int axxia_i2c_get_scl(struct i2c_adapter *adap)
570 struct axxia_i2c_dev *idev = i2c_get_adapdata(adap);
572 return !!(readl(idev->base + I2C_BUS_MONITOR) & BM_SCLS);
575 static void axxia_i2c_set_scl(struct i2c_adapter *adap, int val)
577 struct axxia_i2c_dev *idev = i2c_get_adapdata(adap);
580 /* Preserve SDA Control */
581 tmp = readl(idev->base + I2C_BUS_MONITOR) & BM_SDAC;
584 writel(tmp, idev->base + I2C_BUS_MONITOR);
587 static int axxia_i2c_get_sda(struct i2c_adapter *adap)
589 struct axxia_i2c_dev *idev = i2c_get_adapdata(adap);
591 return !!(readl(idev->base + I2C_BUS_MONITOR) & BM_SDAS);
594 static struct i2c_bus_recovery_info axxia_i2c_recovery_info = {
595 .recover_bus = i2c_generic_scl_recovery,
596 .get_scl = axxia_i2c_get_scl,
597 .set_scl = axxia_i2c_set_scl,
598 .get_sda = axxia_i2c_get_sda,
601 static u32 axxia_i2c_func(struct i2c_adapter *adap)
603 u32 caps = (I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR |
604 I2C_FUNC_SMBUS_EMUL | I2C_FUNC_SMBUS_BLOCK_DATA);
608 static const struct i2c_algorithm axxia_i2c_algo = {
609 .master_xfer = axxia_i2c_xfer,
610 .functionality = axxia_i2c_func,
613 static const struct i2c_adapter_quirks axxia_i2c_quirks = {
615 .max_write_len = 255,
618 static int axxia_i2c_probe(struct platform_device *pdev)
620 struct device_node *np = pdev->dev.of_node;
621 struct axxia_i2c_dev *idev = NULL;
622 struct resource *res;
627 idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
631 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
632 base = devm_ioremap_resource(&pdev->dev, res);
634 return PTR_ERR(base);
636 irq = platform_get_irq(pdev, 0);
638 dev_err(&pdev->dev, "missing interrupt resource\n");
642 idev->i2c_clk = devm_clk_get(&pdev->dev, "i2c");
643 if (IS_ERR(idev->i2c_clk)) {
644 dev_err(&pdev->dev, "missing clock\n");
645 return PTR_ERR(idev->i2c_clk);
649 idev->dev = &pdev->dev;
650 init_completion(&idev->msg_complete);
652 of_property_read_u32(np, "clock-frequency", &idev->bus_clk_rate);
653 if (idev->bus_clk_rate == 0)
654 idev->bus_clk_rate = 100000; /* default clock rate */
656 ret = clk_prepare_enable(idev->i2c_clk);
658 dev_err(&pdev->dev, "failed to enable clock\n");
662 ret = axxia_i2c_init(idev);
664 dev_err(&pdev->dev, "failed to initialize\n");
665 goto error_disable_clk;
668 ret = devm_request_irq(&pdev->dev, irq, axxia_i2c_isr, 0,
671 dev_err(&pdev->dev, "failed to claim IRQ%d\n", irq);
672 goto error_disable_clk;
675 i2c_set_adapdata(&idev->adapter, idev);
676 strlcpy(idev->adapter.name, pdev->name, sizeof(idev->adapter.name));
677 idev->adapter.owner = THIS_MODULE;
678 idev->adapter.algo = &axxia_i2c_algo;
679 idev->adapter.bus_recovery_info = &axxia_i2c_recovery_info;
680 idev->adapter.quirks = &axxia_i2c_quirks;
681 idev->adapter.dev.parent = &pdev->dev;
682 idev->adapter.dev.of_node = pdev->dev.of_node;
684 platform_set_drvdata(pdev, idev);
686 ret = i2c_add_adapter(&idev->adapter);
688 goto error_disable_clk;
693 clk_disable_unprepare(idev->i2c_clk);
697 static int axxia_i2c_remove(struct platform_device *pdev)
699 struct axxia_i2c_dev *idev = platform_get_drvdata(pdev);
701 clk_disable_unprepare(idev->i2c_clk);
702 i2c_del_adapter(&idev->adapter);
707 /* Match table for of_platform binding */
708 static const struct of_device_id axxia_i2c_of_match[] = {
709 { .compatible = "lsi,api2c", },
713 MODULE_DEVICE_TABLE(of, axxia_i2c_of_match);
715 static struct platform_driver axxia_i2c_driver = {
716 .probe = axxia_i2c_probe,
717 .remove = axxia_i2c_remove,
720 .of_match_table = axxia_i2c_of_match,
724 module_platform_driver(axxia_i2c_driver);
726 MODULE_DESCRIPTION("Axxia I2C Bus driver");
728 MODULE_LICENSE("GPL v2");