31 #include <linux/kernel.h>
32 #include <linux/module.h>
34 #include <linux/errno.h>
37 #include <linux/i2c.h>
39 #include <linux/wait.h>
42 #include <linux/slab.h>
45 #define DRIVER_NAME "xiic-i2c"
80 #define XIIC_MSB_OFFSET 0
81 #define XIIC_REG_OFFSET (0x100+XIIC_MSB_OFFSET)
87 #define XIIC_CR_REG_OFFSET (0x00+XIIC_REG_OFFSET)
88 #define XIIC_SR_REG_OFFSET (0x04+XIIC_REG_OFFSET)
89 #define XIIC_DTR_REG_OFFSET (0x08+XIIC_REG_OFFSET)
90 #define XIIC_DRR_REG_OFFSET (0x0C+XIIC_REG_OFFSET)
91 #define XIIC_ADR_REG_OFFSET (0x10+XIIC_REG_OFFSET)
92 #define XIIC_TFO_REG_OFFSET (0x14+XIIC_REG_OFFSET)
93 #define XIIC_RFO_REG_OFFSET (0x18+XIIC_REG_OFFSET)
94 #define XIIC_TBA_REG_OFFSET (0x1C+XIIC_REG_OFFSET)
95 #define XIIC_RFD_REG_OFFSET (0x20+XIIC_REG_OFFSET)
96 #define XIIC_GPO_REG_OFFSET (0x24+XIIC_REG_OFFSET)
99 #define XIIC_CR_ENABLE_DEVICE_MASK 0x01
100 #define XIIC_CR_TX_FIFO_RESET_MASK 0x02
101 #define XIIC_CR_MSMS_MASK 0x04
102 #define XIIC_CR_DIR_IS_TX_MASK 0x08
103 #define XIIC_CR_NO_ACK_MASK 0x10
104 #define XIIC_CR_REPEATED_START_MASK 0x20
105 #define XIIC_CR_GENERAL_CALL_MASK 0x40
108 #define XIIC_SR_GEN_CALL_MASK 0x01
109 #define XIIC_SR_ADDR_AS_SLAVE_MASK 0x02
110 #define XIIC_SR_BUS_BUSY_MASK 0x04
111 #define XIIC_SR_MSTR_RDING_SLAVE_MASK 0x08
112 #define XIIC_SR_TX_FIFO_FULL_MASK 0x10
113 #define XIIC_SR_RX_FIFO_FULL_MASK 0x20
114 #define XIIC_SR_RX_FIFO_EMPTY_MASK 0x40
115 #define XIIC_SR_TX_FIFO_EMPTY_MASK 0x80
118 #define XIIC_INTR_ARB_LOST_MASK 0x01
119 #define XIIC_INTR_TX_ERROR_MASK 0x02
120 #define XIIC_INTR_TX_EMPTY_MASK 0x04
121 #define XIIC_INTR_RX_FULL_MASK 0x08
122 #define XIIC_INTR_BNB_MASK 0x10
123 #define XIIC_INTR_AAS_MASK 0x20
124 #define XIIC_INTR_NAAS_MASK 0x40
125 #define XIIC_INTR_TX_HALF_MASK 0x80
128 #define IIC_RX_FIFO_DEPTH 16
129 #define IIC_TX_FIFO_DEPTH 16
134 #define XIIC_TX_INTERRUPTS \
135 (XIIC_INTR_TX_ERROR_MASK | XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK)
137 #define XIIC_TX_RX_INTERRUPTS (XIIC_INTR_RX_FULL_MASK | XIIC_TX_INTERRUPTS)
142 #define XIIC_READ_OPERATION 1
143 #define XIIC_WRITE_OPERATION 0
148 #define XIIC_TX_DYN_START_MASK 0x0100
149 #define XIIC_TX_DYN_STOP_MASK 0x0200
157 #define XIIC_DGIER_OFFSET 0x1C
158 #define XIIC_IISR_OFFSET 0x20
159 #define XIIC_IIER_OFFSET 0x28
160 #define XIIC_RESETR_OFFSET 0x40
162 #define XIIC_RESET_MASK 0xAUL
169 #define XIIC_GINTR_ENABLE_MASK 0x80000000UL
171 #define xiic_tx_space(i2c) ((i2c)->tx_msg->len - (i2c)->tx_pos)
172 #define xiic_rx_space(i2c) ((i2c)->rx_msg->len - (i2c)->rx_pos)
174 static void xiic_start_xfer(
struct xiic_i2c *i2c);
175 static void __xiic_start_xfer(
struct xiic_i2c *i2c);
197 static inline int xiic_getreg32(
struct xiic_i2c *i2c,
int reg)
222 xiic_irq_clr(i2c, mask);
223 xiic_irq_en(i2c, mask);
226 static void xiic_clear_rx_fifo(
struct xiic_i2c *i2c)
235 static void xiic_reinit(
struct xiic_i2c *i2c)
249 xiic_clear_rx_fifo(i2c);
257 static void xiic_deinit(
struct xiic_i2c *i2c)
268 static void xiic_read_rx(
struct xiic_i2c *i2c)
275 dev_dbg(i2c->
adap.dev.parent,
"%s entry, bytes in fifo: %d, msg: %d"
276 ", SR: 0x%x, CR: 0x%x\n",
284 for (i = 0; i < bytes_in_fifo; i++)
293 static int xiic_tx_fifo_space(
struct xiic_i2c *i2c)
299 static void xiic_fill_tx_fifo(
struct xiic_i2c *i2c)
301 u8 fifo_space = xiic_tx_fifo_space(i2c);
304 len = (len > fifo_space) ? fifo_space : len;
306 dev_dbg(i2c->
adap.dev.parent,
"%s entry, len: %d, fifo space: %d\n",
307 __func__, len, fifo_space);
314 dev_dbg(i2c->
adap.dev.parent,
"%s TX STOP\n", __func__);
331 static void xiic_process(
struct xiic_i2c *i2c)
345 dev_dbg(i2c->
adap.dev.parent,
"%s entry, IER: 0x%x, ISR: 0x%x, "
346 "pend: 0x%x, SR: 0x%x, msg: %p, nmsgs: %d\n",
366 dev_dbg(i2c->
adap.dev.parent,
"%s error\n", __func__);
377 }
else if (pend & XIIC_INTR_RX_FULL_MASK) {
383 "%s unexpexted RX IRQ\n", __func__);
384 xiic_clear_rx_fifo(i2c);
397 "%s end of message, nmsgs: %d\n",
398 __func__, i2c->
nmsgs);
404 if (i2c->
nmsgs > 1) {
408 "%s will start next...\n", __func__);
410 __xiic_start_xfer(i2c);
418 xiic_irq_dis(i2c, XIIC_INTR_BNB_MASK);
437 "%s unexpexted TX IRQ\n", __func__);
441 xiic_fill_tx_fifo(i2c);
446 "%s end of message sent, nmsgs: %d\n",
447 __func__, i2c->
nmsgs);
448 if (i2c->
nmsgs > 1) {
451 __xiic_start_xfer(i2c);
456 "%s Got TX IRQ but no more to do...\n",
466 dev_err(i2c->
adap.dev.parent,
"%s Got unexpected IRQ\n",
471 dev_dbg(i2c->
adap.dev.parent,
"%s clr: 0x%x\n", __func__, clr);
476 static int xiic_bus_busy(
struct xiic_i2c *i2c)
483 static int xiic_busy(
struct xiic_i2c *i2c)
495 err = xiic_bus_busy(i2c);
496 while (err && tries--) {
498 err = xiic_bus_busy(i2c);
504 static void xiic_start_recv(
struct xiic_i2c *i2c)
510 xiic_irq_clr_en(i2c, XIIC_INTR_RX_FULL_MASK | XIIC_INTR_TX_ERROR_MASK);
518 rx_watermark = msg->
len;
529 xiic_irq_clr_en(i2c, XIIC_INTR_BNB_MASK);
535 xiic_irq_clr_en(i2c, XIIC_INTR_BNB_MASK);
541 static void xiic_start_send(
struct xiic_i2c *i2c)
545 xiic_irq_clr(i2c, XIIC_INTR_TX_ERROR_MASK);
547 dev_dbg(i2c->
adap.dev.parent,
"%s entry, msg: %p, len: %d, "
548 "ISR: 0x%x, CR: 0x%x\n",
556 if ((i2c->
nmsgs == 1) && msg->
len == 0)
563 xiic_fill_tx_fifo(i2c);
574 spin_lock(&i2c->
lock);
578 dev_dbg(i2c->
adap.dev.parent,
"%s entry\n", __func__);
583 spin_unlock(&i2c->
lock);
588 static void __xiic_start_xfer(
struct xiic_i2c *i2c)
591 int fifo_space = xiic_tx_fifo_space(i2c);
592 dev_dbg(i2c->
adap.dev.parent,
"%s entry, msg: %p, fifos space: %d\n",
593 __func__, i2c->
tx_msg, fifo_space);
601 while ((fifo_space >= 2) && (first || (i2c->
nmsgs > 1))) {
611 xiic_start_recv(i2c);
614 xiic_start_send(i2c);
621 fifo_space = xiic_tx_fifo_space(i2c);
632 static void xiic_start_xfer(
struct xiic_i2c *i2c)
640 spin_unlock_irqrestore(&i2c->
lock, flags);
642 __xiic_start_xfer(i2c);
648 struct xiic_i2c *i2c = i2c_get_adapdata(adap);
651 dev_dbg(adap->
dev.parent,
"%s entry SR: 0x%x\n", __func__,
654 err = xiic_busy(i2c);
661 xiic_start_xfer(i2c);
680 .master_xfer = xiic_xfer,
681 .functionality = xiic_func,
688 .algo = &xiic_algorithm,
702 goto resource_missing;
706 goto resource_missing;
717 goto request_mem_failed;
722 dev_err(&pdev->
dev,
"Unable to map registers\n");
728 platform_set_drvdata(pdev, i2c);
729 i2c->
adap = xiic_adapter;
730 i2c_set_adapdata(&i2c->
adap, i2c);
731 i2c->
adap.dev.parent = &pdev->
dev;
732 i2c->
adap.dev.of_node = pdev->
dev.of_node;
741 goto request_irq_failed;
747 dev_err(&pdev->
dev,
"Failed to add adapter\n");
748 goto add_adapter_failed;
773 dev_err(&pdev->
dev,
"IRQ or Memory resource is missing\n");
779 struct xiic_i2c *i2c = platform_get_drvdata(pdev);
787 platform_set_drvdata(pdev,
NULL);
802 #if defined(CONFIG_OF)
804 { .compatible =
"xlnx,xps-iic-2.00.a", },
811 .probe = xiic_i2c_probe,