1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (c) 2002-2007 Xilinx Inc.
5 * Copyright (c) 2009-2010 Intel Corporation
7 * This code was implemented by Mocean Laboratories AB when porting linux
8 * to the automotive development board Russellville. The copyright holder
9 * as seen in the header is Intel corporation.
10 * Mocean Laboratories forked off the GNU/Linux platform work into a
11 * separate company called Pelagicore AB, which committed the code to the
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/errno.h>
21 #include <linux/err.h>
22 #include <linux/delay.h>
23 #include <linux/platform_device.h>
24 #include <linux/i2c.h>
25 #include <linux/interrupt.h>
26 #include <linux/completion.h>
27 #include <linux/platform_data/i2c-xiic.h>
29 #include <linux/slab.h>
31 #include <linux/clk.h>
32 #include <linux/pm_runtime.h>
34 #define DRIVER_NAME "xiic-i2c"
35 #define DYNAMIC_MODE_READ_BROKEN_BIT BIT(0)
37 enum xilinx_i2c_state {
49 * struct xiic_i2c - Internal representation of the XIIC I2C bus
50 * @dev: Pointer to device structure
51 * @base: Memory base of the HW registers
52 * @completion: Completion for callers
53 * @adap: Kernel adapter representation
54 * @tx_msg: Messages from above to be sent
55 * @lock: Mutual exclusion
56 * @tx_pos: Current pos in TX message
57 * @nmsgs: Number of messages in tx_msg
58 * @rx_msg: Current RX message
59 * @rx_pos: Position within current RX message
60 * @endianness: big/little-endian byte order
61 * @clk: Pointer to AXI4-lite input clock
63 * @singlemaster: Indicates bus is single master
64 * @dynamic: Mode of controller
65 * @prev_msg_tx: Previous message is Tx
66 * @quirks: To hold platform specific bug info
71 struct completion completion;
72 struct i2c_adapter adap;
73 struct i2c_msg *tx_msg;
77 struct i2c_msg *rx_msg;
79 enum xiic_endian endianness;
81 enum xilinx_i2c_state state;
88 struct xiic_version_data {
92 #define XIIC_MSB_OFFSET 0
93 #define XIIC_REG_OFFSET (0x100 + XIIC_MSB_OFFSET)
96 * Register offsets in bytes from RegisterBase. Three is added to the
97 * base offset to access LSB (IBM style) of the word
99 #define XIIC_CR_REG_OFFSET (0x00 + XIIC_REG_OFFSET) /* Control Register */
100 #define XIIC_SR_REG_OFFSET (0x04 + XIIC_REG_OFFSET) /* Status Register */
101 #define XIIC_DTR_REG_OFFSET (0x08 + XIIC_REG_OFFSET) /* Data Tx Register */
102 #define XIIC_DRR_REG_OFFSET (0x0C + XIIC_REG_OFFSET) /* Data Rx Register */
103 #define XIIC_ADR_REG_OFFSET (0x10 + XIIC_REG_OFFSET) /* Address Register */
104 #define XIIC_TFO_REG_OFFSET (0x14 + XIIC_REG_OFFSET) /* Tx FIFO Occupancy */
105 #define XIIC_RFO_REG_OFFSET (0x18 + XIIC_REG_OFFSET) /* Rx FIFO Occupancy */
106 #define XIIC_TBA_REG_OFFSET (0x1C + XIIC_REG_OFFSET) /* 10 Bit Address reg */
107 #define XIIC_RFD_REG_OFFSET (0x20 + XIIC_REG_OFFSET) /* Rx FIFO Depth reg */
108 #define XIIC_GPO_REG_OFFSET (0x24 + XIIC_REG_OFFSET) /* Output Register */
110 /* Control Register masks */
111 #define XIIC_CR_ENABLE_DEVICE_MASK 0x01 /* Device enable = 1 */
112 #define XIIC_CR_TX_FIFO_RESET_MASK 0x02 /* Transmit FIFO reset=1 */
113 #define XIIC_CR_MSMS_MASK 0x04 /* Master starts Txing=1 */
114 #define XIIC_CR_DIR_IS_TX_MASK 0x08 /* Dir of tx. Txing=1 */
115 #define XIIC_CR_NO_ACK_MASK 0x10 /* Tx Ack. NO ack = 1 */
116 #define XIIC_CR_REPEATED_START_MASK 0x20 /* Repeated start = 1 */
117 #define XIIC_CR_GENERAL_CALL_MASK 0x40 /* Gen Call enabled = 1 */
119 /* Status Register masks */
120 #define XIIC_SR_GEN_CALL_MASK 0x01 /* 1=a mstr issued a GC */
121 #define XIIC_SR_ADDR_AS_SLAVE_MASK 0x02 /* 1=when addr as slave */
122 #define XIIC_SR_BUS_BUSY_MASK 0x04 /* 1 = bus is busy */
123 #define XIIC_SR_MSTR_RDING_SLAVE_MASK 0x08 /* 1=Dir: mstr <-- slave */
124 #define XIIC_SR_TX_FIFO_FULL_MASK 0x10 /* 1 = Tx FIFO full */
125 #define XIIC_SR_RX_FIFO_FULL_MASK 0x20 /* 1 = Rx FIFO full */
126 #define XIIC_SR_RX_FIFO_EMPTY_MASK 0x40 /* 1 = Rx FIFO empty */
127 #define XIIC_SR_TX_FIFO_EMPTY_MASK 0x80 /* 1 = Tx FIFO empty */
129 /* Interrupt Status Register masks Interrupt occurs when... */
130 #define XIIC_INTR_ARB_LOST_MASK 0x01 /* 1 = arbitration lost */
131 #define XIIC_INTR_TX_ERROR_MASK 0x02 /* 1=Tx error/msg complete */
132 #define XIIC_INTR_TX_EMPTY_MASK 0x04 /* 1 = Tx FIFO/reg empty */
133 #define XIIC_INTR_RX_FULL_MASK 0x08 /* 1=Rx FIFO/reg=OCY level */
134 #define XIIC_INTR_BNB_MASK 0x10 /* 1 = Bus not busy */
135 #define XIIC_INTR_AAS_MASK 0x20 /* 1 = when addr as slave */
136 #define XIIC_INTR_NAAS_MASK 0x40 /* 1 = not addr as slave */
137 #define XIIC_INTR_TX_HALF_MASK 0x80 /* 1 = TX FIFO half empty */
139 /* The following constants specify the depth of the FIFOs */
140 #define IIC_RX_FIFO_DEPTH 16 /* Rx fifo capacity */
141 #define IIC_TX_FIFO_DEPTH 16 /* Tx fifo capacity */
143 /* The following constants specify groups of interrupts that are typically
144 * enabled or disables at the same time
146 #define XIIC_TX_INTERRUPTS \
147 (XIIC_INTR_TX_ERROR_MASK | XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK)
149 #define XIIC_TX_RX_INTERRUPTS (XIIC_INTR_RX_FULL_MASK | XIIC_TX_INTERRUPTS)
152 * Tx Fifo upper bit masks.
154 #define XIIC_TX_DYN_START_MASK 0x0100 /* 1 = Set dynamic start */
155 #define XIIC_TX_DYN_STOP_MASK 0x0200 /* 1 = Set dynamic stop */
157 /* Dynamic mode constants */
158 #define MAX_READ_LENGTH_DYNAMIC 255 /* Max length for dynamic read */
161 * The following constants define the register offsets for the Interrupt
162 * registers. There are some holes in the memory map for reserved addresses
163 * to allow other registers to be added and still match the memory map of the
164 * interrupt controller registers
166 #define XIIC_DGIER_OFFSET 0x1C /* Device Global Interrupt Enable Register */
167 #define XIIC_IISR_OFFSET 0x20 /* Interrupt Status Register */
168 #define XIIC_IIER_OFFSET 0x28 /* Interrupt Enable Register */
169 #define XIIC_RESETR_OFFSET 0x40 /* Reset Register */
171 #define XIIC_RESET_MASK 0xAUL
173 #define XIIC_PM_TIMEOUT 1000 /* ms */
174 /* timeout waiting for the controller to respond */
175 #define XIIC_I2C_TIMEOUT (msecs_to_jiffies(1000))
176 /* timeout waiting for the controller finish transfers */
177 #define XIIC_XFER_TIMEOUT (msecs_to_jiffies(10000))
180 * The following constant is used for the device global interrupt enable
181 * register, to enable all interrupts for the device, this is the only bit
184 #define XIIC_GINTR_ENABLE_MASK 0x80000000UL
186 #define xiic_tx_space(i2c) ((i2c)->tx_msg->len - (i2c)->tx_pos)
187 #define xiic_rx_space(i2c) ((i2c)->rx_msg->len - (i2c)->rx_pos)
189 static int xiic_start_xfer(struct xiic_i2c *i2c, struct i2c_msg *msgs, int num);
190 static void __xiic_start_xfer(struct xiic_i2c *i2c);
193 * For the register read and write functions, a little-endian and big-endian
194 * version are necessary. Endianness is detected during the probe function.
195 * Only the least significant byte [doublet] of the register are ever
196 * accessed. This requires an offset of 3 [2] from the base address for
197 * big-endian systems.
200 static inline void xiic_setreg8(struct xiic_i2c *i2c, int reg, u8 value)
202 if (i2c->endianness == LITTLE)
203 iowrite8(value, i2c->base + reg);
205 iowrite8(value, i2c->base + reg + 3);
208 static inline u8 xiic_getreg8(struct xiic_i2c *i2c, int reg)
212 if (i2c->endianness == LITTLE)
213 ret = ioread8(i2c->base + reg);
215 ret = ioread8(i2c->base + reg + 3);
219 static inline void xiic_setreg16(struct xiic_i2c *i2c, int reg, u16 value)
221 if (i2c->endianness == LITTLE)
222 iowrite16(value, i2c->base + reg);
224 iowrite16be(value, i2c->base + reg + 2);
227 static inline void xiic_setreg32(struct xiic_i2c *i2c, int reg, int value)
229 if (i2c->endianness == LITTLE)
230 iowrite32(value, i2c->base + reg);
232 iowrite32be(value, i2c->base + reg);
235 static inline int xiic_getreg32(struct xiic_i2c *i2c, int reg)
239 if (i2c->endianness == LITTLE)
240 ret = ioread32(i2c->base + reg);
242 ret = ioread32be(i2c->base + reg);
246 static inline void xiic_irq_dis(struct xiic_i2c *i2c, u32 mask)
248 u32 ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);
250 xiic_setreg32(i2c, XIIC_IIER_OFFSET, ier & ~mask);
253 static inline void xiic_irq_en(struct xiic_i2c *i2c, u32 mask)
255 u32 ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);
257 xiic_setreg32(i2c, XIIC_IIER_OFFSET, ier | mask);
260 static inline void xiic_irq_clr(struct xiic_i2c *i2c, u32 mask)
262 u32 isr = xiic_getreg32(i2c, XIIC_IISR_OFFSET);
264 xiic_setreg32(i2c, XIIC_IISR_OFFSET, isr & mask);
267 static inline void xiic_irq_clr_en(struct xiic_i2c *i2c, u32 mask)
269 xiic_irq_clr(i2c, mask);
270 xiic_irq_en(i2c, mask);
273 static int xiic_clear_rx_fifo(struct xiic_i2c *i2c)
276 unsigned long timeout;
278 timeout = jiffies + XIIC_I2C_TIMEOUT;
279 for (sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET);
280 !(sr & XIIC_SR_RX_FIFO_EMPTY_MASK);
281 sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET)) {
282 xiic_getreg8(i2c, XIIC_DRR_REG_OFFSET);
283 if (time_after(jiffies, timeout)) {
284 dev_err(i2c->dev, "Failed to clear rx fifo\n");
292 static int xiic_wait_tx_empty(struct xiic_i2c *i2c)
295 unsigned long timeout;
297 timeout = jiffies + XIIC_I2C_TIMEOUT;
298 for (isr = xiic_getreg32(i2c, XIIC_IISR_OFFSET);
299 !(isr & XIIC_INTR_TX_EMPTY_MASK);
300 isr = xiic_getreg32(i2c, XIIC_IISR_OFFSET)) {
301 if (time_after(jiffies, timeout)) {
302 dev_err(i2c->dev, "Timeout waiting at Tx empty\n");
310 static int xiic_reinit(struct xiic_i2c *i2c)
314 xiic_setreg32(i2c, XIIC_RESETR_OFFSET, XIIC_RESET_MASK);
316 /* Set receive Fifo depth to maximum (zero based). */
317 xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET, IIC_RX_FIFO_DEPTH - 1);
320 xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, XIIC_CR_TX_FIFO_RESET_MASK);
322 /* Enable IIC Device, remove Tx Fifo reset & disable general call. */
323 xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, XIIC_CR_ENABLE_DEVICE_MASK);
325 /* make sure RX fifo is empty */
326 ret = xiic_clear_rx_fifo(i2c);
330 /* Enable interrupts */
331 xiic_setreg32(i2c, XIIC_DGIER_OFFSET, XIIC_GINTR_ENABLE_MASK);
333 xiic_irq_clr_en(i2c, XIIC_INTR_ARB_LOST_MASK);
338 static void xiic_deinit(struct xiic_i2c *i2c)
342 xiic_setreg32(i2c, XIIC_RESETR_OFFSET, XIIC_RESET_MASK);
344 /* Disable IIC Device. */
345 cr = xiic_getreg8(i2c, XIIC_CR_REG_OFFSET);
346 xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, cr & ~XIIC_CR_ENABLE_DEVICE_MASK);
349 static void xiic_read_rx(struct xiic_i2c *i2c)
351 u8 bytes_in_fifo, cr = 0, bytes_to_read = 0;
355 bytes_in_fifo = xiic_getreg8(i2c, XIIC_RFO_REG_OFFSET) + 1;
357 dev_dbg(i2c->adap.dev.parent,
358 "%s entry, bytes in fifo: %d, rem: %d, SR: 0x%x, CR: 0x%x\n",
359 __func__, bytes_in_fifo, xiic_rx_space(i2c),
360 xiic_getreg8(i2c, XIIC_SR_REG_OFFSET),
361 xiic_getreg8(i2c, XIIC_CR_REG_OFFSET));
363 if (bytes_in_fifo > xiic_rx_space(i2c))
364 bytes_in_fifo = xiic_rx_space(i2c);
366 bytes_to_read = bytes_in_fifo;
369 bytes_rem = xiic_rx_space(i2c) - bytes_in_fifo;
371 if (bytes_rem > IIC_RX_FIFO_DEPTH) {
372 bytes_to_read = bytes_in_fifo;
373 } else if (bytes_rem > 1) {
374 bytes_to_read = bytes_rem - 1;
375 } else if (bytes_rem == 1) {
377 /* Set NACK in CR to indicate slave transmitter */
378 cr = xiic_getreg8(i2c, XIIC_CR_REG_OFFSET);
379 xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, cr |
380 XIIC_CR_NO_ACK_MASK);
381 } else if (bytes_rem == 0) {
382 bytes_to_read = bytes_in_fifo;
384 /* Generate stop on the bus if it is last message */
385 if (i2c->nmsgs == 1) {
386 cr = xiic_getreg8(i2c, XIIC_CR_REG_OFFSET);
387 xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, cr &
391 /* Make TXACK=0, clean up for next transaction */
392 cr = xiic_getreg8(i2c, XIIC_CR_REG_OFFSET);
393 xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, cr &
394 ~XIIC_CR_NO_ACK_MASK);
399 for (i = 0; i < bytes_to_read; i++) {
400 i2c->rx_msg->buf[i2c->rx_pos++] =
401 xiic_getreg8(i2c, XIIC_DRR_REG_OFFSET);
407 /* Receive remaining bytes if less than fifo depth */
408 bytes = min_t(u8, xiic_rx_space(i2c), IIC_RX_FIFO_DEPTH);
410 xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET, bytes);
414 static int xiic_tx_fifo_space(struct xiic_i2c *i2c)
416 /* return the actual space left in the FIFO */
417 return IIC_TX_FIFO_DEPTH - xiic_getreg8(i2c, XIIC_TFO_REG_OFFSET) - 1;
420 static void xiic_fill_tx_fifo(struct xiic_i2c *i2c)
422 u8 fifo_space = xiic_tx_fifo_space(i2c);
423 int len = xiic_tx_space(i2c);
425 len = (len > fifo_space) ? fifo_space : len;
427 dev_dbg(i2c->adap.dev.parent, "%s entry, len: %d, fifo space: %d\n",
428 __func__, len, fifo_space);
431 u16 data = i2c->tx_msg->buf[i2c->tx_pos++];
433 if (!xiic_tx_space(i2c) && i2c->nmsgs == 1) {
434 /* last message in transfer -> STOP */
436 data |= XIIC_TX_DYN_STOP_MASK;
439 /* Write to CR to stop */
440 cr = xiic_getreg8(i2c, XIIC_CR_REG_OFFSET);
441 xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, cr &
444 dev_dbg(i2c->adap.dev.parent, "%s TX STOP\n", __func__);
446 xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET, data);
450 static void xiic_wakeup(struct xiic_i2c *i2c, enum xilinx_i2c_state code)
456 complete(&i2c->completion);
459 static irqreturn_t xiic_process(int irq, void *dev_id)
461 struct xiic_i2c *i2c = dev_id;
466 enum xilinx_i2c_state wakeup_code = STATE_DONE;
469 /* Get the interrupt Status from the IPIF. There is no clearing of
470 * interrupts in the IPIF. Interrupts must be cleared at the source.
471 * To find which interrupts are pending; AND interrupts pending with
474 mutex_lock(&i2c->lock);
475 isr = xiic_getreg32(i2c, XIIC_IISR_OFFSET);
476 ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);
479 dev_dbg(i2c->adap.dev.parent, "%s: IER: 0x%x, ISR: 0x%x, pend: 0x%x\n",
480 __func__, ier, isr, pend);
481 dev_dbg(i2c->adap.dev.parent, "%s: SR: 0x%x, msg: %p, nmsgs: %d\n",
482 __func__, xiic_getreg8(i2c, XIIC_SR_REG_OFFSET),
483 i2c->tx_msg, i2c->nmsgs);
484 dev_dbg(i2c->adap.dev.parent, "%s, ISR: 0x%x, CR: 0x%x\n",
485 __func__, xiic_getreg32(i2c, XIIC_IISR_OFFSET),
486 xiic_getreg8(i2c, XIIC_CR_REG_OFFSET));
488 /* Service requesting interrupt */
489 if ((pend & XIIC_INTR_ARB_LOST_MASK) ||
490 ((pend & XIIC_INTR_TX_ERROR_MASK) &&
491 !(pend & XIIC_INTR_RX_FULL_MASK))) {
492 /* bus arbritration lost, or...
493 * Transmit error _OR_ RX completed
494 * if this happens when RX_FULL is not set
495 * this is probably a TX error
498 dev_dbg(i2c->adap.dev.parent, "%s error\n", __func__);
500 /* dynamic mode seem to suffer from problems if we just flushes
501 * fifos and the next message is a TX with len 0 (only addr)
502 * reset the IP instead of just flush fifos
504 ret = xiic_reinit(i2c);
506 dev_dbg(i2c->adap.dev.parent, "reinit failed\n");
510 wakeup_code = STATE_ERROR;
514 wakeup_code = STATE_ERROR;
517 if (pend & XIIC_INTR_RX_FULL_MASK) {
518 /* Receive register/FIFO is full */
520 clr |= XIIC_INTR_RX_FULL_MASK;
522 dev_dbg(i2c->adap.dev.parent,
523 "%s unexpected RX IRQ\n", __func__);
524 xiic_clear_rx_fifo(i2c);
529 if (xiic_rx_space(i2c) == 0) {
530 /* this is the last part of the message */
533 /* also clear TX error if there (RX complete) */
534 clr |= (isr & XIIC_INTR_TX_ERROR_MASK);
536 dev_dbg(i2c->adap.dev.parent,
537 "%s end of message, nmsgs: %d\n",
538 __func__, i2c->nmsgs);
540 /* send next message if this wasn't the last,
541 * otherwise the transfer will be finialise when
542 * receiving the bus not busy interrupt
544 if (i2c->nmsgs > 1) {
547 dev_dbg(i2c->adap.dev.parent,
548 "%s will start next...\n", __func__);
553 if (pend & (XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK)) {
554 /* Transmit register/FIFO is empty or ½ empty */
557 (XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK));
560 dev_dbg(i2c->adap.dev.parent,
561 "%s unexpected TX IRQ\n", __func__);
565 xiic_fill_tx_fifo(i2c);
567 /* current message sent and there is space in the fifo */
568 if (!xiic_tx_space(i2c) && xiic_tx_fifo_space(i2c) >= 2) {
569 dev_dbg(i2c->adap.dev.parent,
570 "%s end of message sent, nmsgs: %d\n",
571 __func__, i2c->nmsgs);
572 if (i2c->nmsgs > 1) {
577 xiic_irq_dis(i2c, XIIC_INTR_TX_HALF_MASK);
579 dev_dbg(i2c->adap.dev.parent,
580 "%s Got TX IRQ but no more to do...\n",
583 } else if (!xiic_tx_space(i2c) && (i2c->nmsgs == 1))
584 /* current frame is sent and is last,
585 * make sure to disable tx half
587 xiic_irq_dis(i2c, XIIC_INTR_TX_HALF_MASK);
590 if (pend & XIIC_INTR_BNB_MASK) {
591 /* IIC bus has transitioned to not busy */
592 clr |= XIIC_INTR_BNB_MASK;
594 /* The bus is not busy, disable BusNotBusy interrupt */
595 xiic_irq_dis(i2c, XIIC_INTR_BNB_MASK);
602 if (i2c->nmsgs == 1 && !i2c->rx_msg &&
603 xiic_tx_space(i2c) == 0)
604 wakeup_code = STATE_DONE;
606 wakeup_code = STATE_ERROR;
610 dev_dbg(i2c->adap.dev.parent, "%s clr: 0x%x\n", __func__, clr);
612 xiic_setreg32(i2c, XIIC_IISR_OFFSET, clr);
614 __xiic_start_xfer(i2c);
616 xiic_wakeup(i2c, wakeup_code);
618 WARN_ON(xfer_more && wakeup_req);
620 mutex_unlock(&i2c->lock);
624 static int xiic_bus_busy(struct xiic_i2c *i2c)
626 u8 sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET);
628 return (sr & XIIC_SR_BUS_BUSY_MASK) ? -EBUSY : 0;
631 static int xiic_busy(struct xiic_i2c *i2c)
636 if (i2c->tx_msg || i2c->rx_msg)
639 /* In single master mode bus can only be busy, when in use by this
640 * driver. If the register indicates bus being busy for some reason we
641 * should ignore it, since bus will never be released and i2c will be
644 if (i2c->singlemaster) {
648 /* for instance if previous transfer was terminated due to TX error
649 * it might be that the bus is on it's way to become available
650 * give it at most 3 ms to wake
652 err = xiic_bus_busy(i2c);
653 while (err && tries--) {
655 err = xiic_bus_busy(i2c);
661 static void xiic_start_recv(struct xiic_i2c *i2c)
664 u8 cr = 0, rfd_set = 0;
665 struct i2c_msg *msg = i2c->rx_msg = i2c->tx_msg;
668 dev_dbg(i2c->adap.dev.parent, "%s entry, ISR: 0x%x, CR: 0x%x\n",
669 __func__, xiic_getreg32(i2c, XIIC_IISR_OFFSET),
670 xiic_getreg8(i2c, XIIC_CR_REG_OFFSET));
672 /* Disable Tx interrupts */
673 xiic_irq_dis(i2c, XIIC_INTR_TX_HALF_MASK | XIIC_INTR_TX_EMPTY_MASK);
679 /* Clear and enable Rx full interrupt. */
680 xiic_irq_clr_en(i2c, XIIC_INTR_RX_FULL_MASK |
681 XIIC_INTR_TX_ERROR_MASK);
684 * We want to get all but last byte, because the TX_ERROR IRQ
685 * is used to indicate error ACK on the address, and
686 * negative ack on the last received byte, so to not mix
687 * them receive all but last.
688 * In the case where there is only one byte to receive
689 * we can check if ERROR and RX full is set at the same time
691 rx_watermark = msg->len;
692 bytes = min_t(u8, rx_watermark, IIC_RX_FIFO_DEPTH);
694 if (rx_watermark > 0)
696 xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET, bytes);
698 local_irq_save(flags);
700 /* write the address */
701 xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET,
702 i2c_8bit_addr_from_msg(msg) |
703 XIIC_TX_DYN_START_MASK);
705 /* If last message, include dynamic stop bit with length */
706 val = (i2c->nmsgs == 1) ? XIIC_TX_DYN_STOP_MASK : 0;
709 xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET, val);
711 xiic_irq_clr_en(i2c, XIIC_INTR_BNB_MASK);
713 local_irq_restore(flags);
716 * If previous message is Tx, make sure that Tx FIFO is empty
717 * before starting a new transfer as the repeated start in
718 * standard mode can corrupt the transaction if there are
719 * still bytes to be transmitted in FIFO
721 if (i2c->prev_msg_tx) {
724 status = xiic_wait_tx_empty(i2c);
729 cr = xiic_getreg8(i2c, XIIC_CR_REG_OFFSET);
731 /* Set Receive fifo depth */
732 rx_watermark = msg->len;
733 if (rx_watermark > IIC_RX_FIFO_DEPTH) {
734 rfd_set = IIC_RX_FIFO_DEPTH - 1;
735 } else if (rx_watermark == 1) {
736 rfd_set = rx_watermark - 1;
737 /* Handle single byte transfer separately */
738 cr |= XIIC_CR_NO_ACK_MASK;
739 } else if (rx_watermark == 0) {
740 rfd_set = rx_watermark;
741 cr |= XIIC_CR_NO_ACK_MASK;
743 rfd_set = rx_watermark - 2;
745 /* Check if RSTA should be set */
746 if (cr & XIIC_CR_MSMS_MASK) {
747 /* Already a master, RSTA should be set */
748 xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, (cr |
749 XIIC_CR_REPEATED_START_MASK) &
750 ~(XIIC_CR_DIR_IS_TX_MASK));
753 xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET, rfd_set);
755 /* Clear and enable Rx full and transmit complete interrupts */
756 xiic_irq_clr_en(i2c, XIIC_INTR_RX_FULL_MASK |
757 XIIC_INTR_TX_ERROR_MASK);
759 /* Write the address */
760 xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET,
761 i2c_8bit_addr_from_msg(msg));
763 /* Write to Control Register,to start transaction in Rx mode */
764 if ((cr & XIIC_CR_MSMS_MASK) == 0) {
765 xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, (cr |
767 & ~(XIIC_CR_DIR_IS_TX_MASK));
769 dev_dbg(i2c->adap.dev.parent, "%s end, ISR: 0x%x, CR: 0x%x\n",
770 __func__, xiic_getreg32(i2c, XIIC_IISR_OFFSET),
771 xiic_getreg8(i2c, XIIC_CR_REG_OFFSET));
775 /* very last, enable bus not busy as well */
776 xiic_irq_clr_en(i2c, XIIC_INTR_BNB_MASK);
778 /* the message is tx:ed */
779 i2c->tx_pos = msg->len;
781 /* Enable interrupts */
782 xiic_setreg32(i2c, XIIC_DGIER_OFFSET, XIIC_GINTR_ENABLE_MASK);
784 i2c->prev_msg_tx = false;
787 static void xiic_start_send(struct xiic_i2c *i2c)
791 struct i2c_msg *msg = i2c->tx_msg;
793 dev_dbg(i2c->adap.dev.parent, "%s entry, msg: %p, len: %d",
794 __func__, msg, msg->len);
795 dev_dbg(i2c->adap.dev.parent, "%s entry, ISR: 0x%x, CR: 0x%x\n",
796 __func__, xiic_getreg32(i2c, XIIC_IISR_OFFSET),
797 xiic_getreg8(i2c, XIIC_CR_REG_OFFSET));
800 /* write the address */
801 data = i2c_8bit_addr_from_msg(msg) |
802 XIIC_TX_DYN_START_MASK;
804 if (i2c->nmsgs == 1 && msg->len == 0)
805 /* no data and last message -> add STOP */
806 data |= XIIC_TX_DYN_STOP_MASK;
808 xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET, data);
810 /* Clear any pending Tx empty, Tx Error and then enable them */
811 xiic_irq_clr_en(i2c, XIIC_INTR_TX_EMPTY_MASK |
812 XIIC_INTR_TX_ERROR_MASK |
814 ((i2c->nmsgs > 1 || xiic_tx_space(i2c)) ?
815 XIIC_INTR_TX_HALF_MASK : 0));
817 xiic_fill_tx_fifo(i2c);
820 * If previous message is Tx, make sure that Tx FIFO is empty
821 * before starting a new transfer as the repeated start in
822 * standard mode can corrupt the transaction if there are
823 * still bytes to be transmitted in FIFO
825 if (i2c->prev_msg_tx) {
828 status = xiic_wait_tx_empty(i2c);
832 /* Check if RSTA should be set */
833 cr = xiic_getreg8(i2c, XIIC_CR_REG_OFFSET);
834 if (cr & XIIC_CR_MSMS_MASK) {
835 /* Already a master, RSTA should be set */
836 xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, (cr |
837 XIIC_CR_REPEATED_START_MASK |
838 XIIC_CR_DIR_IS_TX_MASK) &
839 ~(XIIC_CR_NO_ACK_MASK));
842 /* Write address to FIFO */
843 data = i2c_8bit_addr_from_msg(msg);
844 xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET, data);
847 xiic_fill_tx_fifo(i2c);
849 if ((cr & XIIC_CR_MSMS_MASK) == 0) {
850 /* Start Tx by writing to CR */
851 cr = xiic_getreg8(i2c, XIIC_CR_REG_OFFSET);
852 xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, cr |
854 XIIC_CR_DIR_IS_TX_MASK);
857 /* Clear any pending Tx empty, Tx Error and then enable them */
858 xiic_irq_clr_en(i2c, XIIC_INTR_TX_EMPTY_MASK |
859 XIIC_INTR_TX_ERROR_MASK |
862 i2c->prev_msg_tx = true;
865 static void __xiic_start_xfer(struct xiic_i2c *i2c)
867 int fifo_space = xiic_tx_fifo_space(i2c);
869 dev_dbg(i2c->adap.dev.parent, "%s entry, msg: %p, fifos space: %d\n",
870 __func__, i2c->tx_msg, fifo_space);
877 i2c->state = STATE_START;
878 if (i2c->tx_msg->flags & I2C_M_RD) {
879 /* we dont date putting several reads in the FIFO */
880 xiic_start_recv(i2c);
882 xiic_start_send(i2c);
886 static int xiic_start_xfer(struct xiic_i2c *i2c, struct i2c_msg *msgs, int num)
888 bool broken_read, max_read_len, smbus_blk_read;
891 mutex_lock(&i2c->lock);
893 ret = xiic_busy(i2c);
900 init_completion(&i2c->completion);
902 /* Decide standard mode or Dynamic mode */
905 /* Initialize prev message type */
906 i2c->prev_msg_tx = false;
909 * Scan through nmsgs, use dynamic mode when none of the below three
910 * conditions occur. We need standard mode even if one condition holds
911 * true in the entire array of messages in a single transfer.
912 * If read transaction as dynamic mode is broken for delayed reads
913 * in xlnx,axi-iic-2.0 / xlnx,xps-iic-2.00.a IP versions.
914 * If read length is > 255 bytes.
915 * If smbus_block_read transaction.
917 for (count = 0; count < i2c->nmsgs; count++) {
918 broken_read = (i2c->quirks & DYNAMIC_MODE_READ_BROKEN_BIT) &&
919 (i2c->tx_msg[count].flags & I2C_M_RD);
920 max_read_len = (i2c->tx_msg[count].flags & I2C_M_RD) &&
921 (i2c->tx_msg[count].len > MAX_READ_LENGTH_DYNAMIC);
922 smbus_blk_read = (i2c->tx_msg[count].flags & I2C_M_RECV_LEN);
924 if (broken_read || max_read_len || smbus_blk_read) {
925 i2c->dynamic = false;
930 ret = xiic_reinit(i2c);
932 __xiic_start_xfer(i2c);
935 mutex_unlock(&i2c->lock);
940 static int xiic_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
942 struct xiic_i2c *i2c = i2c_get_adapdata(adap);
945 dev_dbg(adap->dev.parent, "%s entry SR: 0x%x\n", __func__,
946 xiic_getreg8(i2c, XIIC_SR_REG_OFFSET));
948 err = pm_runtime_resume_and_get(i2c->dev);
952 err = xiic_start_xfer(i2c, msgs, num);
954 dev_err(adap->dev.parent, "Error xiic_start_xfer\n");
958 err = wait_for_completion_timeout(&i2c->completion, XIIC_XFER_TIMEOUT);
959 mutex_lock(&i2c->lock);
960 if (err == 0) { /* Timeout */
965 } else if (err < 0) { /* Completion error */
970 err = (i2c->state == STATE_DONE) ? num : -EIO;
972 mutex_unlock(&i2c->lock);
973 pm_runtime_mark_last_busy(i2c->dev);
974 pm_runtime_put_autosuspend(i2c->dev);
978 static u32 xiic_func(struct i2c_adapter *adap)
980 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
983 static const struct i2c_algorithm xiic_algorithm = {
984 .master_xfer = xiic_xfer,
985 .functionality = xiic_func,
988 static const struct i2c_adapter xiic_adapter = {
989 .owner = THIS_MODULE,
990 .class = I2C_CLASS_DEPRECATED,
991 .algo = &xiic_algorithm,
994 static const struct xiic_version_data xiic_2_00 = {
995 .quirks = DYNAMIC_MODE_READ_BROKEN_BIT,
998 #if defined(CONFIG_OF)
999 static const struct of_device_id xiic_of_match[] = {
1000 { .compatible = "xlnx,xps-iic-2.00.a", .data = &xiic_2_00 },
1003 MODULE_DEVICE_TABLE(of, xiic_of_match);
1006 static int xiic_i2c_probe(struct platform_device *pdev)
1008 struct xiic_i2c *i2c;
1009 struct xiic_i2c_platform_data *pdata;
1010 const struct of_device_id *match;
1011 struct resource *res;
1016 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
1020 match = of_match_node(xiic_of_match, pdev->dev.of_node);
1021 if (match && match->data) {
1022 const struct xiic_version_data *data = match->data;
1024 i2c->quirks = data->quirks;
1027 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1028 i2c->base = devm_ioremap_resource(&pdev->dev, res);
1029 if (IS_ERR(i2c->base))
1030 return PTR_ERR(i2c->base);
1032 irq = platform_get_irq(pdev, 0);
1036 pdata = dev_get_platdata(&pdev->dev);
1038 /* hook up driver to tree */
1039 platform_set_drvdata(pdev, i2c);
1040 i2c->adap = xiic_adapter;
1041 i2c_set_adapdata(&i2c->adap, i2c);
1042 i2c->adap.dev.parent = &pdev->dev;
1043 i2c->adap.dev.of_node = pdev->dev.of_node;
1044 snprintf(i2c->adap.name, sizeof(i2c->adap.name),
1045 DRIVER_NAME " %s", pdev->name);
1047 mutex_init(&i2c->lock);
1049 i2c->clk = devm_clk_get(&pdev->dev, NULL);
1050 if (IS_ERR(i2c->clk))
1051 return dev_err_probe(&pdev->dev, PTR_ERR(i2c->clk),
1052 "input clock not found.\n");
1054 ret = clk_prepare_enable(i2c->clk);
1056 dev_err(&pdev->dev, "Unable to enable clock.\n");
1059 i2c->dev = &pdev->dev;
1060 pm_runtime_set_autosuspend_delay(i2c->dev, XIIC_PM_TIMEOUT);
1061 pm_runtime_use_autosuspend(i2c->dev);
1062 pm_runtime_set_active(i2c->dev);
1063 pm_runtime_enable(i2c->dev);
1064 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1065 xiic_process, IRQF_ONESHOT,
1069 dev_err(&pdev->dev, "Cannot claim IRQ\n");
1074 of_property_read_bool(pdev->dev.of_node, "single-master");
1078 * Try to reset the TX FIFO. Then check the EMPTY flag. If it is not
1079 * set, assume that the endianness was wrong and swap.
1081 i2c->endianness = LITTLE;
1082 xiic_setreg32(i2c, XIIC_CR_REG_OFFSET, XIIC_CR_TX_FIFO_RESET_MASK);
1083 /* Reset is cleared in xiic_reinit */
1084 sr = xiic_getreg32(i2c, XIIC_SR_REG_OFFSET);
1085 if (!(sr & XIIC_SR_TX_FIFO_EMPTY_MASK))
1086 i2c->endianness = BIG;
1088 ret = xiic_reinit(i2c);
1090 dev_err(&pdev->dev, "Cannot xiic_reinit\n");
1094 /* add i2c adapter to i2c tree */
1095 ret = i2c_add_adapter(&i2c->adap);
1102 /* add in known devices to the bus */
1103 for (i = 0; i < pdata->num_devices; i++)
1104 i2c_new_client_device(&i2c->adap, pdata->devices + i);
1110 pm_runtime_set_suspended(&pdev->dev);
1111 pm_runtime_disable(&pdev->dev);
1112 clk_disable_unprepare(i2c->clk);
1116 static int xiic_i2c_remove(struct platform_device *pdev)
1118 struct xiic_i2c *i2c = platform_get_drvdata(pdev);
1121 /* remove adapter & data */
1122 i2c_del_adapter(&i2c->adap);
1124 ret = pm_runtime_get_sync(i2c->dev);
1127 dev_warn(&pdev->dev, "Failed to activate device for removal (%pe)\n",
1132 pm_runtime_put_sync(i2c->dev);
1133 clk_disable_unprepare(i2c->clk);
1134 pm_runtime_disable(&pdev->dev);
1135 pm_runtime_set_suspended(&pdev->dev);
1136 pm_runtime_dont_use_autosuspend(&pdev->dev);
1141 static int __maybe_unused xiic_i2c_runtime_suspend(struct device *dev)
1143 struct xiic_i2c *i2c = dev_get_drvdata(dev);
1145 clk_disable(i2c->clk);
1150 static int __maybe_unused xiic_i2c_runtime_resume(struct device *dev)
1152 struct xiic_i2c *i2c = dev_get_drvdata(dev);
1155 ret = clk_enable(i2c->clk);
1157 dev_err(dev, "Cannot enable clock.\n");
1164 static const struct dev_pm_ops xiic_dev_pm_ops = {
1165 SET_RUNTIME_PM_OPS(xiic_i2c_runtime_suspend,
1166 xiic_i2c_runtime_resume, NULL)
1169 static struct platform_driver xiic_i2c_driver = {
1170 .probe = xiic_i2c_probe,
1171 .remove = xiic_i2c_remove,
1173 .name = DRIVER_NAME,
1174 .of_match_table = of_match_ptr(xiic_of_match),
1175 .pm = &xiic_dev_pm_ops,
1179 module_platform_driver(xiic_i2c_driver);
1181 MODULE_ALIAS("platform:" DRIVER_NAME);
1182 MODULE_AUTHOR("info@mocean-labs.com");
1183 MODULE_DESCRIPTION("Xilinx I2C bus driver");
1184 MODULE_LICENSE("GPL v2");