blob: 17f0dd1f891e2ddacecedf674882e3af10f49029 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * I2C bus driver for the Cadence I2C controller.
4 *
5 * Copyright (C) 2009 - 2014 Xilinx, Inc.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006 */
7
8#include <linux/clk.h>
9#include <linux/delay.h>
10#include <linux/i2c.h>
11#include <linux/interrupt.h>
12#include <linux/io.h>
13#include <linux/module.h>
14#include <linux/platform_device.h>
15#include <linux/of.h>
16#include <linux/pm_runtime.h>
17
18/* Register offsets for the I2C device. */
19#define CDNS_I2C_CR_OFFSET 0x00 /* Control Register, RW */
20#define CDNS_I2C_SR_OFFSET 0x04 /* Status Register, RO */
21#define CDNS_I2C_ADDR_OFFSET 0x08 /* I2C Address Register, RW */
22#define CDNS_I2C_DATA_OFFSET 0x0C /* I2C Data Register, RW */
23#define CDNS_I2C_ISR_OFFSET 0x10 /* IRQ Status Register, RW */
24#define CDNS_I2C_XFER_SIZE_OFFSET 0x14 /* Transfer Size Register, RW */
25#define CDNS_I2C_TIME_OUT_OFFSET 0x1C /* Time Out Register, RW */
26#define CDNS_I2C_IER_OFFSET 0x24 /* IRQ Enable Register, WO */
27#define CDNS_I2C_IDR_OFFSET 0x28 /* IRQ Disable Register, WO */
28
29/* Control Register Bit mask definitions */
30#define CDNS_I2C_CR_HOLD BIT(4) /* Hold Bus bit */
31#define CDNS_I2C_CR_ACK_EN BIT(3)
32#define CDNS_I2C_CR_NEA BIT(2)
33#define CDNS_I2C_CR_MS BIT(1)
34/* Read or Write Master transfer 0 = Transmitter, 1 = Receiver */
35#define CDNS_I2C_CR_RW BIT(0)
36/* 1 = Auto init FIFO to zeroes */
37#define CDNS_I2C_CR_CLR_FIFO BIT(6)
38#define CDNS_I2C_CR_DIVA_SHIFT 14
39#define CDNS_I2C_CR_DIVA_MASK (3 << CDNS_I2C_CR_DIVA_SHIFT)
40#define CDNS_I2C_CR_DIVB_SHIFT 8
41#define CDNS_I2C_CR_DIVB_MASK (0x3f << CDNS_I2C_CR_DIVB_SHIFT)
42
43/* Status Register Bit mask definitions */
44#define CDNS_I2C_SR_BA BIT(8)
45#define CDNS_I2C_SR_RXDV BIT(5)
46
47/*
48 * I2C Address Register Bit mask definitions
49 * Normal addressing mode uses [6:0] bits. Extended addressing mode uses [9:0]
50 * bits. A write access to this register always initiates a transfer if the I2C
51 * is in master mode.
52 */
53#define CDNS_I2C_ADDR_MASK 0x000003FF /* I2C Address Mask */
54
55/*
56 * I2C Interrupt Registers Bit mask definitions
57 * All the four interrupt registers (Status/Mask/Enable/Disable) have the same
58 * bit definitions.
59 */
60#define CDNS_I2C_IXR_ARB_LOST BIT(9)
61#define CDNS_I2C_IXR_RX_UNF BIT(7)
62#define CDNS_I2C_IXR_TX_OVF BIT(6)
63#define CDNS_I2C_IXR_RX_OVF BIT(5)
64#define CDNS_I2C_IXR_SLV_RDY BIT(4)
65#define CDNS_I2C_IXR_TO BIT(3)
66#define CDNS_I2C_IXR_NACK BIT(2)
67#define CDNS_I2C_IXR_DATA BIT(1)
68#define CDNS_I2C_IXR_COMP BIT(0)
69
70#define CDNS_I2C_IXR_ALL_INTR_MASK (CDNS_I2C_IXR_ARB_LOST | \
71 CDNS_I2C_IXR_RX_UNF | \
72 CDNS_I2C_IXR_TX_OVF | \
73 CDNS_I2C_IXR_RX_OVF | \
74 CDNS_I2C_IXR_SLV_RDY | \
75 CDNS_I2C_IXR_TO | \
76 CDNS_I2C_IXR_NACK | \
77 CDNS_I2C_IXR_DATA | \
78 CDNS_I2C_IXR_COMP)
79
80#define CDNS_I2C_IXR_ERR_INTR_MASK (CDNS_I2C_IXR_ARB_LOST | \
81 CDNS_I2C_IXR_RX_UNF | \
82 CDNS_I2C_IXR_TX_OVF | \
83 CDNS_I2C_IXR_RX_OVF | \
84 CDNS_I2C_IXR_NACK)
85
86#define CDNS_I2C_ENABLED_INTR_MASK (CDNS_I2C_IXR_ARB_LOST | \
87 CDNS_I2C_IXR_RX_UNF | \
88 CDNS_I2C_IXR_TX_OVF | \
89 CDNS_I2C_IXR_RX_OVF | \
90 CDNS_I2C_IXR_NACK | \
91 CDNS_I2C_IXR_DATA | \
92 CDNS_I2C_IXR_COMP)
93
94#define CDNS_I2C_TIMEOUT msecs_to_jiffies(1000)
95/* timeout for pm runtime autosuspend */
96#define CNDS_I2C_PM_TIMEOUT 1000 /* ms */
97
98#define CDNS_I2C_FIFO_DEPTH 16
99/* FIFO depth at which the DATA interrupt occurs */
100#define CDNS_I2C_DATA_INTR_DEPTH (CDNS_I2C_FIFO_DEPTH - 2)
101#define CDNS_I2C_MAX_TRANSFER_SIZE 255
102/* Transfer size in multiples of data interrupt depth */
103#define CDNS_I2C_TRANSFER_SIZE (CDNS_I2C_MAX_TRANSFER_SIZE - 3)
104
105#define DRIVER_NAME "cdns-i2c"
106
107#define CDNS_I2C_SPEED_MAX 400000
108#define CDNS_I2C_SPEED_DEFAULT 100000
109
110#define CDNS_I2C_DIVA_MAX 4
111#define CDNS_I2C_DIVB_MAX 64
112
113#define CDNS_I2C_TIMEOUT_MAX 0xFF
114
115#define CDNS_I2C_BROKEN_HOLD_BIT BIT(0)
116
117#define cdns_i2c_readreg(offset) readl_relaxed(id->membase + offset)
118#define cdns_i2c_writereg(val, offset) writel_relaxed(val, id->membase + offset)
119
120/**
121 * struct cdns_i2c - I2C device private data structure
122 *
123 * @dev: Pointer to device structure
124 * @membase: Base address of the I2C device
125 * @adap: I2C adapter instance
126 * @p_msg: Message pointer
127 * @err_status: Error status in Interrupt Status Register
128 * @xfer_done: Transfer complete status
129 * @p_send_buf: Pointer to transmit buffer
130 * @p_recv_buf: Pointer to receive buffer
131 * @send_count: Number of bytes still expected to send
132 * @recv_count: Number of bytes still expected to receive
133 * @curr_recv_count: Number of bytes to be received in current transfer
134 * @irq: IRQ number
135 * @input_clk: Input clock to I2C controller
136 * @i2c_clk: Maximum I2C clock speed
137 * @bus_hold_flag: Flag used in repeated start for clearing HOLD bit
138 * @clk: Pointer to struct clk
139 * @clk_rate_change_nb: Notifier block for clock rate changes
140 * @quirks: flag for broken hold bit usage in r1p10
141 */
142struct cdns_i2c {
143 struct device *dev;
144 void __iomem *membase;
145 struct i2c_adapter adap;
146 struct i2c_msg *p_msg;
147 int err_status;
148 struct completion xfer_done;
149 unsigned char *p_send_buf;
150 unsigned char *p_recv_buf;
151 unsigned int send_count;
152 unsigned int recv_count;
153 unsigned int curr_recv_count;
154 int irq;
155 unsigned long input_clk;
156 unsigned int i2c_clk;
157 unsigned int bus_hold_flag;
158 struct clk *clk;
159 struct notifier_block clk_rate_change_nb;
160 u32 quirks;
161};
162
163struct cdns_platform_data {
164 u32 quirks;
165};
166
167#define to_cdns_i2c(_nb) container_of(_nb, struct cdns_i2c, \
168 clk_rate_change_nb)
169
170/**
171 * cdns_i2c_clear_bus_hold - Clear bus hold bit
172 * @id: Pointer to driver data struct
173 *
174 * Helper to clear the controller's bus hold bit.
175 */
176static void cdns_i2c_clear_bus_hold(struct cdns_i2c *id)
177{
178 u32 reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
179 if (reg & CDNS_I2C_CR_HOLD)
180 cdns_i2c_writereg(reg & ~CDNS_I2C_CR_HOLD, CDNS_I2C_CR_OFFSET);
181}
182
183static inline bool cdns_is_holdquirk(struct cdns_i2c *id, bool hold_wrkaround)
184{
185 return (hold_wrkaround &&
186 (id->curr_recv_count == CDNS_I2C_FIFO_DEPTH + 1));
187}
188
189/**
190 * cdns_i2c_isr - Interrupt handler for the I2C device
191 * @irq: irq number for the I2C device
192 * @ptr: void pointer to cdns_i2c structure
193 *
194 * This function handles the data interrupt, transfer complete interrupt and
195 * the error interrupts of the I2C device.
196 *
197 * Return: IRQ_HANDLED always
198 */
199static irqreturn_t cdns_i2c_isr(int irq, void *ptr)
200{
201 unsigned int isr_status, avail_bytes, updatetx;
202 unsigned int bytes_to_send;
203 bool hold_quirk;
204 struct cdns_i2c *id = ptr;
205 /* Signal completion only after everything is updated */
206 int done_flag = 0;
207 irqreturn_t status = IRQ_NONE;
208
209 isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
210 cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
211
212 /* Handling nack and arbitration lost interrupt */
213 if (isr_status & (CDNS_I2C_IXR_NACK | CDNS_I2C_IXR_ARB_LOST)) {
214 done_flag = 1;
215 status = IRQ_HANDLED;
216 }
217
218 /*
219 * Check if transfer size register needs to be updated again for a
220 * large data receive operation.
221 */
222 updatetx = 0;
223 if (id->recv_count > id->curr_recv_count)
224 updatetx = 1;
225
226 hold_quirk = (id->quirks & CDNS_I2C_BROKEN_HOLD_BIT) && updatetx;
227
228 /* When receiving, handle data interrupt and completion interrupt */
229 if (id->p_recv_buf &&
230 ((isr_status & CDNS_I2C_IXR_COMP) ||
231 (isr_status & CDNS_I2C_IXR_DATA))) {
232 /* Read data if receive data valid is set */
233 while (cdns_i2c_readreg(CDNS_I2C_SR_OFFSET) &
234 CDNS_I2C_SR_RXDV) {
235 /*
236 * Clear hold bit that was set for FIFO control if
237 * RX data left is less than FIFO depth, unless
238 * repeated start is selected.
239 */
240 if ((id->recv_count < CDNS_I2C_FIFO_DEPTH) &&
241 !id->bus_hold_flag)
242 cdns_i2c_clear_bus_hold(id);
243
244 *(id->p_recv_buf)++ =
245 cdns_i2c_readreg(CDNS_I2C_DATA_OFFSET);
246 id->recv_count--;
247 id->curr_recv_count--;
248
249 if (cdns_is_holdquirk(id, hold_quirk))
250 break;
251 }
252
253 /*
254 * The controller sends NACK to the slave when transfer size
255 * register reaches zero without considering the HOLD bit.
256 * This workaround is implemented for large data transfers to
257 * maintain transfer size non-zero while performing a large
258 * receive operation.
259 */
260 if (cdns_is_holdquirk(id, hold_quirk)) {
261 /* wait while fifo is full */
262 while (cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET) !=
263 (id->curr_recv_count - CDNS_I2C_FIFO_DEPTH))
264 ;
265
266 /*
267 * Check number of bytes to be received against maximum
268 * transfer size and update register accordingly.
269 */
270 if (((int)(id->recv_count) - CDNS_I2C_FIFO_DEPTH) >
271 CDNS_I2C_TRANSFER_SIZE) {
272 cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
273 CDNS_I2C_XFER_SIZE_OFFSET);
274 id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE +
275 CDNS_I2C_FIFO_DEPTH;
276 } else {
277 cdns_i2c_writereg(id->recv_count -
278 CDNS_I2C_FIFO_DEPTH,
279 CDNS_I2C_XFER_SIZE_OFFSET);
280 id->curr_recv_count = id->recv_count;
281 }
282 } else if (id->recv_count && !hold_quirk &&
283 !id->curr_recv_count) {
284
285 /* Set the slave address in address register*/
286 cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK,
287 CDNS_I2C_ADDR_OFFSET);
288
289 if (id->recv_count > CDNS_I2C_TRANSFER_SIZE) {
290 cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
291 CDNS_I2C_XFER_SIZE_OFFSET);
292 id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE;
293 } else {
294 cdns_i2c_writereg(id->recv_count,
295 CDNS_I2C_XFER_SIZE_OFFSET);
296 id->curr_recv_count = id->recv_count;
297 }
298 }
299
300 /* Clear hold (if not repeated start) and signal completion */
301 if ((isr_status & CDNS_I2C_IXR_COMP) && !id->recv_count) {
302 if (!id->bus_hold_flag)
303 cdns_i2c_clear_bus_hold(id);
304 done_flag = 1;
305 }
306
307 status = IRQ_HANDLED;
308 }
309
310 /* When sending, handle transfer complete interrupt */
311 if ((isr_status & CDNS_I2C_IXR_COMP) && !id->p_recv_buf) {
312 /*
313 * If there is more data to be sent, calculate the
314 * space available in FIFO and fill with that many bytes.
315 */
316 if (id->send_count) {
317 avail_bytes = CDNS_I2C_FIFO_DEPTH -
318 cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET);
319 if (id->send_count > avail_bytes)
320 bytes_to_send = avail_bytes;
321 else
322 bytes_to_send = id->send_count;
323
324 while (bytes_to_send--) {
325 cdns_i2c_writereg(
326 (*(id->p_send_buf)++),
327 CDNS_I2C_DATA_OFFSET);
328 id->send_count--;
329 }
330 } else {
331 /*
332 * Signal the completion of transaction and
333 * clear the hold bus bit if there are no
334 * further messages to be processed.
335 */
336 done_flag = 1;
337 }
338 if (!id->send_count && !id->bus_hold_flag)
339 cdns_i2c_clear_bus_hold(id);
340
341 status = IRQ_HANDLED;
342 }
343
344 /* Update the status for errors */
345 id->err_status = isr_status & CDNS_I2C_IXR_ERR_INTR_MASK;
346 if (id->err_status)
347 status = IRQ_HANDLED;
348
349 if (done_flag)
350 complete(&id->xfer_done);
351
352 return status;
353}
354
355/**
356 * cdns_i2c_mrecv - Prepare and start a master receive operation
357 * @id: pointer to the i2c device structure
358 */
359static void cdns_i2c_mrecv(struct cdns_i2c *id)
360{
361 unsigned int ctrl_reg;
362 unsigned int isr_status;
363
364 id->p_recv_buf = id->p_msg->buf;
365 id->recv_count = id->p_msg->len;
366
367 /* Put the controller in master receive mode and clear the FIFO */
368 ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
369 ctrl_reg |= CDNS_I2C_CR_RW | CDNS_I2C_CR_CLR_FIFO;
370
371 if (id->p_msg->flags & I2C_M_RECV_LEN)
372 id->recv_count = I2C_SMBUS_BLOCK_MAX + 1;
373
374 id->curr_recv_count = id->recv_count;
375
376 /*
377 * Check for the message size against FIFO depth and set the
378 * 'hold bus' bit if it is greater than FIFO depth.
379 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200380 if (id->recv_count > CDNS_I2C_FIFO_DEPTH)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000381 ctrl_reg |= CDNS_I2C_CR_HOLD;
382
383 cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
384
385 /* Clear the interrupts in interrupt status register */
386 isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
387 cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
388
389 /*
390 * The no. of bytes to receive is checked against the limit of
391 * max transfer size. Set transfer size register with no of bytes
392 * receive if it is less than transfer size and transfer size if
393 * it is more. Enable the interrupts.
394 */
395 if (id->recv_count > CDNS_I2C_TRANSFER_SIZE) {
396 cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
397 CDNS_I2C_XFER_SIZE_OFFSET);
398 id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE;
399 } else {
400 cdns_i2c_writereg(id->recv_count, CDNS_I2C_XFER_SIZE_OFFSET);
401 }
402
403 /* Set the slave address in address register - triggers operation */
404 cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK,
405 CDNS_I2C_ADDR_OFFSET);
406 /* Clear the bus hold flag if bytes to receive is less than FIFO size */
407 if (!id->bus_hold_flag &&
408 ((id->p_msg->flags & I2C_M_RECV_LEN) != I2C_M_RECV_LEN) &&
409 (id->recv_count <= CDNS_I2C_FIFO_DEPTH))
410 cdns_i2c_clear_bus_hold(id);
411 cdns_i2c_writereg(CDNS_I2C_ENABLED_INTR_MASK, CDNS_I2C_IER_OFFSET);
412}
413
414/**
415 * cdns_i2c_msend - Prepare and start a master send operation
416 * @id: pointer to the i2c device
417 */
418static void cdns_i2c_msend(struct cdns_i2c *id)
419{
420 unsigned int avail_bytes;
421 unsigned int bytes_to_send;
422 unsigned int ctrl_reg;
423 unsigned int isr_status;
424
425 id->p_recv_buf = NULL;
426 id->p_send_buf = id->p_msg->buf;
427 id->send_count = id->p_msg->len;
428
429 /* Set the controller in Master transmit mode and clear the FIFO. */
430 ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
431 ctrl_reg &= ~CDNS_I2C_CR_RW;
432 ctrl_reg |= CDNS_I2C_CR_CLR_FIFO;
433
434 /*
435 * Check for the message size against FIFO depth and set the
436 * 'hold bus' bit if it is greater than FIFO depth.
437 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200438 if (id->send_count > CDNS_I2C_FIFO_DEPTH)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000439 ctrl_reg |= CDNS_I2C_CR_HOLD;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000440 cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
441
442 /* Clear the interrupts in interrupt status register. */
443 isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
444 cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
445
446 /*
447 * Calculate the space available in FIFO. Check the message length
448 * against the space available, and fill the FIFO accordingly.
449 * Enable the interrupts.
450 */
451 avail_bytes = CDNS_I2C_FIFO_DEPTH -
452 cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET);
453
454 if (id->send_count > avail_bytes)
455 bytes_to_send = avail_bytes;
456 else
457 bytes_to_send = id->send_count;
458
459 while (bytes_to_send--) {
460 cdns_i2c_writereg((*(id->p_send_buf)++), CDNS_I2C_DATA_OFFSET);
461 id->send_count--;
462 }
463
464 /*
465 * Clear the bus hold flag if there is no more data
466 * and if it is the last message.
467 */
468 if (!id->bus_hold_flag && !id->send_count)
469 cdns_i2c_clear_bus_hold(id);
470 /* Set the slave address in address register - triggers operation. */
471 cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK,
472 CDNS_I2C_ADDR_OFFSET);
473
474 cdns_i2c_writereg(CDNS_I2C_ENABLED_INTR_MASK, CDNS_I2C_IER_OFFSET);
475}
476
477/**
478 * cdns_i2c_master_reset - Reset the interface
479 * @adap: pointer to the i2c adapter driver instance
480 *
481 * This function cleanup the fifos, clear the hold bit and status
482 * and disable the interrupts.
483 */
484static void cdns_i2c_master_reset(struct i2c_adapter *adap)
485{
486 struct cdns_i2c *id = adap->algo_data;
487 u32 regval;
488
489 /* Disable the interrupts */
490 cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK, CDNS_I2C_IDR_OFFSET);
491 /* Clear the hold bit and fifos */
492 regval = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
493 regval &= ~CDNS_I2C_CR_HOLD;
494 regval |= CDNS_I2C_CR_CLR_FIFO;
495 cdns_i2c_writereg(regval, CDNS_I2C_CR_OFFSET);
496 /* Update the transfercount register to zero */
497 cdns_i2c_writereg(0, CDNS_I2C_XFER_SIZE_OFFSET);
498 /* Clear the interupt status register */
499 regval = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
500 cdns_i2c_writereg(regval, CDNS_I2C_ISR_OFFSET);
501 /* Clear the status register */
502 regval = cdns_i2c_readreg(CDNS_I2C_SR_OFFSET);
503 cdns_i2c_writereg(regval, CDNS_I2C_SR_OFFSET);
504}
505
506static int cdns_i2c_process_msg(struct cdns_i2c *id, struct i2c_msg *msg,
507 struct i2c_adapter *adap)
508{
509 unsigned long time_left;
510 u32 reg;
511
512 id->p_msg = msg;
513 id->err_status = 0;
514 reinit_completion(&id->xfer_done);
515
516 /* Check for the TEN Bit mode on each msg */
517 reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
518 if (msg->flags & I2C_M_TEN) {
519 if (reg & CDNS_I2C_CR_NEA)
520 cdns_i2c_writereg(reg & ~CDNS_I2C_CR_NEA,
521 CDNS_I2C_CR_OFFSET);
522 } else {
523 if (!(reg & CDNS_I2C_CR_NEA))
524 cdns_i2c_writereg(reg | CDNS_I2C_CR_NEA,
525 CDNS_I2C_CR_OFFSET);
526 }
527
528 /* Check for the R/W flag on each msg */
529 if (msg->flags & I2C_M_RD)
530 cdns_i2c_mrecv(id);
531 else
532 cdns_i2c_msend(id);
533
534 /* Wait for the signal of completion */
535 time_left = wait_for_completion_timeout(&id->xfer_done, adap->timeout);
536 if (time_left == 0) {
537 cdns_i2c_master_reset(adap);
538 dev_err(id->adap.dev.parent,
539 "timeout waiting on completion\n");
540 return -ETIMEDOUT;
541 }
542
543 cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK,
544 CDNS_I2C_IDR_OFFSET);
545
546 /* If it is bus arbitration error, try again */
547 if (id->err_status & CDNS_I2C_IXR_ARB_LOST)
548 return -EAGAIN;
549
550 return 0;
551}
552
553/**
554 * cdns_i2c_master_xfer - The main i2c transfer function
555 * @adap: pointer to the i2c adapter driver instance
556 * @msgs: pointer to the i2c message structure
557 * @num: the number of messages to transfer
558 *
559 * Initiates the send/recv activity based on the transfer message received.
560 *
561 * Return: number of msgs processed on success, negative error otherwise
562 */
563static int cdns_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
564 int num)
565{
566 int ret, count;
567 u32 reg;
568 struct cdns_i2c *id = adap->algo_data;
569 bool hold_quirk;
570
571 ret = pm_runtime_get_sync(id->dev);
572 if (ret < 0)
573 return ret;
574 /* Check if the bus is free */
575 if (cdns_i2c_readreg(CDNS_I2C_SR_OFFSET) & CDNS_I2C_SR_BA) {
576 ret = -EAGAIN;
577 goto out;
578 }
579
580 hold_quirk = !!(id->quirks & CDNS_I2C_BROKEN_HOLD_BIT);
581 /*
582 * Set the flag to one when multiple messages are to be
583 * processed with a repeated start.
584 */
585 if (num > 1) {
586 /*
587 * This controller does not give completion interrupt after a
588 * master receive message if HOLD bit is set (repeated start),
589 * resulting in SW timeout. Hence, if a receive message is
590 * followed by any other message, an error is returned
591 * indicating that this sequence is not supported.
592 */
593 for (count = 0; (count < num - 1 && hold_quirk); count++) {
594 if (msgs[count].flags & I2C_M_RD) {
595 dev_warn(adap->dev.parent,
596 "Can't do repeated start after a receive message\n");
597 ret = -EOPNOTSUPP;
598 goto out;
599 }
600 }
601 id->bus_hold_flag = 1;
602 reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
603 reg |= CDNS_I2C_CR_HOLD;
604 cdns_i2c_writereg(reg, CDNS_I2C_CR_OFFSET);
605 } else {
606 id->bus_hold_flag = 0;
607 }
608
609 /* Process the msg one by one */
610 for (count = 0; count < num; count++, msgs++) {
611 if (count == (num - 1))
612 id->bus_hold_flag = 0;
613
614 ret = cdns_i2c_process_msg(id, msgs, adap);
615 if (ret)
616 goto out;
617
618 /* Report the other error interrupts to application */
619 if (id->err_status) {
620 cdns_i2c_master_reset(adap);
621
622 if (id->err_status & CDNS_I2C_IXR_NACK) {
623 ret = -ENXIO;
624 goto out;
625 }
626 ret = -EIO;
627 goto out;
628 }
629 }
630
631 ret = num;
632out:
633 pm_runtime_mark_last_busy(id->dev);
634 pm_runtime_put_autosuspend(id->dev);
635 return ret;
636}
637
638/**
639 * cdns_i2c_func - Returns the supported features of the I2C driver
640 * @adap: pointer to the i2c adapter structure
641 *
642 * Return: 32 bit value, each bit corresponding to a feature
643 */
644static u32 cdns_i2c_func(struct i2c_adapter *adap)
645{
646 return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR |
647 (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
648 I2C_FUNC_SMBUS_BLOCK_DATA;
649}
650
651static const struct i2c_algorithm cdns_i2c_algo = {
652 .master_xfer = cdns_i2c_master_xfer,
653 .functionality = cdns_i2c_func,
654};
655
656/**
657 * cdns_i2c_calc_divs - Calculate clock dividers
658 * @f: I2C clock frequency
659 * @input_clk: Input clock frequency
660 * @a: First divider (return value)
661 * @b: Second divider (return value)
662 *
663 * f is used as input and output variable. As input it is used as target I2C
664 * frequency. On function exit f holds the actually resulting I2C frequency.
665 *
666 * Return: 0 on success, negative errno otherwise.
667 */
668static int cdns_i2c_calc_divs(unsigned long *f, unsigned long input_clk,
669 unsigned int *a, unsigned int *b)
670{
671 unsigned long fscl = *f, best_fscl = *f, actual_fscl, temp;
672 unsigned int div_a, div_b, calc_div_a = 0, calc_div_b = 0;
673 unsigned int last_error, current_error;
674
675 /* calculate (divisor_a+1) x (divisor_b+1) */
676 temp = input_clk / (22 * fscl);
677
678 /*
679 * If the calculated value is negative or 0, the fscl input is out of
680 * range. Return error.
681 */
682 if (!temp || (temp > (CDNS_I2C_DIVA_MAX * CDNS_I2C_DIVB_MAX)))
683 return -EINVAL;
684
685 last_error = -1;
686 for (div_a = 0; div_a < CDNS_I2C_DIVA_MAX; div_a++) {
687 div_b = DIV_ROUND_UP(input_clk, 22 * fscl * (div_a + 1));
688
689 if ((div_b < 1) || (div_b > CDNS_I2C_DIVB_MAX))
690 continue;
691 div_b--;
692
693 actual_fscl = input_clk / (22 * (div_a + 1) * (div_b + 1));
694
695 if (actual_fscl > fscl)
696 continue;
697
698 current_error = ((actual_fscl > fscl) ? (actual_fscl - fscl) :
699 (fscl - actual_fscl));
700
701 if (last_error > current_error) {
702 calc_div_a = div_a;
703 calc_div_b = div_b;
704 best_fscl = actual_fscl;
705 last_error = current_error;
706 }
707 }
708
709 *a = calc_div_a;
710 *b = calc_div_b;
711 *f = best_fscl;
712
713 return 0;
714}
715
716/**
717 * cdns_i2c_setclk - This function sets the serial clock rate for the I2C device
718 * @clk_in: I2C clock input frequency in Hz
719 * @id: Pointer to the I2C device structure
720 *
721 * The device must be idle rather than busy transferring data before setting
722 * these device options.
723 * The data rate is set by values in the control register.
724 * The formula for determining the correct register values is
725 * Fscl = Fpclk/(22 x (divisor_a+1) x (divisor_b+1))
726 * See the hardware data sheet for a full explanation of setting the serial
727 * clock rate. The clock can not be faster than the input clock divide by 22.
728 * The two most common clock rates are 100KHz and 400KHz.
729 *
730 * Return: 0 on success, negative error otherwise
731 */
732static int cdns_i2c_setclk(unsigned long clk_in, struct cdns_i2c *id)
733{
734 unsigned int div_a, div_b;
735 unsigned int ctrl_reg;
736 int ret = 0;
737 unsigned long fscl = id->i2c_clk;
738
739 ret = cdns_i2c_calc_divs(&fscl, clk_in, &div_a, &div_b);
740 if (ret)
741 return ret;
742
743 ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
744 ctrl_reg &= ~(CDNS_I2C_CR_DIVA_MASK | CDNS_I2C_CR_DIVB_MASK);
745 ctrl_reg |= ((div_a << CDNS_I2C_CR_DIVA_SHIFT) |
746 (div_b << CDNS_I2C_CR_DIVB_SHIFT));
747 cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
748
749 return 0;
750}
751
752/**
753 * cdns_i2c_clk_notifier_cb - Clock rate change callback
754 * @nb: Pointer to notifier block
755 * @event: Notification reason
756 * @data: Pointer to notification data object
757 *
758 * This function is called when the cdns_i2c input clock frequency changes.
759 * The callback checks whether a valid bus frequency can be generated after the
760 * change. If so, the change is acknowledged, otherwise the change is aborted.
761 * New dividers are written to the HW in the pre- or post change notification
762 * depending on the scaling direction.
763 *
764 * Return: NOTIFY_STOP if the rate change should be aborted, NOTIFY_OK
765 * to acknowledge the change, NOTIFY_DONE if the notification is
766 * considered irrelevant.
767 */
768static int cdns_i2c_clk_notifier_cb(struct notifier_block *nb, unsigned long
769 event, void *data)
770{
771 struct clk_notifier_data *ndata = data;
772 struct cdns_i2c *id = to_cdns_i2c(nb);
773
774 if (pm_runtime_suspended(id->dev))
775 return NOTIFY_OK;
776
777 switch (event) {
778 case PRE_RATE_CHANGE:
779 {
780 unsigned long input_clk = ndata->new_rate;
781 unsigned long fscl = id->i2c_clk;
782 unsigned int div_a, div_b;
783 int ret;
784
785 ret = cdns_i2c_calc_divs(&fscl, input_clk, &div_a, &div_b);
786 if (ret) {
787 dev_warn(id->adap.dev.parent,
788 "clock rate change rejected\n");
789 return NOTIFY_STOP;
790 }
791
792 /* scale up */
793 if (ndata->new_rate > ndata->old_rate)
794 cdns_i2c_setclk(ndata->new_rate, id);
795
796 return NOTIFY_OK;
797 }
798 case POST_RATE_CHANGE:
799 id->input_clk = ndata->new_rate;
800 /* scale down */
801 if (ndata->new_rate < ndata->old_rate)
802 cdns_i2c_setclk(ndata->new_rate, id);
803 return NOTIFY_OK;
804 case ABORT_RATE_CHANGE:
805 /* scale up */
806 if (ndata->new_rate > ndata->old_rate)
807 cdns_i2c_setclk(ndata->old_rate, id);
808 return NOTIFY_OK;
809 default:
810 return NOTIFY_DONE;
811 }
812}
813
814/**
815 * cdns_i2c_runtime_suspend - Runtime suspend method for the driver
816 * @dev: Address of the platform_device structure
817 *
818 * Put the driver into low power mode.
819 *
820 * Return: 0 always
821 */
822static int __maybe_unused cdns_i2c_runtime_suspend(struct device *dev)
823{
824 struct cdns_i2c *xi2c = dev_get_drvdata(dev);
825
826 clk_disable(xi2c->clk);
827
828 return 0;
829}
830
831/**
832 * cdns_i2c_runtime_resume - Runtime resume
833 * @dev: Address of the platform_device structure
834 *
835 * Runtime resume callback.
836 *
837 * Return: 0 on success and error value on error
838 */
839static int __maybe_unused cdns_i2c_runtime_resume(struct device *dev)
840{
841 struct cdns_i2c *xi2c = dev_get_drvdata(dev);
842 int ret;
843
844 ret = clk_enable(xi2c->clk);
845 if (ret) {
846 dev_err(dev, "Cannot enable clock.\n");
847 return ret;
848 }
849
850 return 0;
851}
852
853static const struct dev_pm_ops cdns_i2c_dev_pm_ops = {
854 SET_RUNTIME_PM_OPS(cdns_i2c_runtime_suspend,
855 cdns_i2c_runtime_resume, NULL)
856};
857
858static const struct cdns_platform_data r1p10_i2c_def = {
859 .quirks = CDNS_I2C_BROKEN_HOLD_BIT,
860};
861
862static const struct of_device_id cdns_i2c_of_match[] = {
863 { .compatible = "cdns,i2c-r1p10", .data = &r1p10_i2c_def },
864 { .compatible = "cdns,i2c-r1p14",},
865 { /* end of table */ }
866};
867MODULE_DEVICE_TABLE(of, cdns_i2c_of_match);
868
869/**
870 * cdns_i2c_probe - Platform registration call
871 * @pdev: Handle to the platform device structure
872 *
873 * This function does all the memory allocation and registration for the i2c
874 * device. User can modify the address mode to 10 bit address mode using the
875 * ioctl call with option I2C_TENBIT.
876 *
877 * Return: 0 on success, negative error otherwise
878 */
879static int cdns_i2c_probe(struct platform_device *pdev)
880{
881 struct resource *r_mem;
882 struct cdns_i2c *id;
883 int ret;
884 const struct of_device_id *match;
885
886 id = devm_kzalloc(&pdev->dev, sizeof(*id), GFP_KERNEL);
887 if (!id)
888 return -ENOMEM;
889
890 id->dev = &pdev->dev;
891 platform_set_drvdata(pdev, id);
892
893 match = of_match_node(cdns_i2c_of_match, pdev->dev.of_node);
894 if (match && match->data) {
895 const struct cdns_platform_data *data = match->data;
896 id->quirks = data->quirks;
897 }
898
899 r_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
900 id->membase = devm_ioremap_resource(&pdev->dev, r_mem);
901 if (IS_ERR(id->membase))
902 return PTR_ERR(id->membase);
903
Olivier Deprez0e641232021-09-23 10:07:05 +0200904 ret = platform_get_irq(pdev, 0);
905 if (ret < 0)
906 return ret;
907 id->irq = ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000908
909 id->adap.owner = THIS_MODULE;
910 id->adap.dev.of_node = pdev->dev.of_node;
911 id->adap.algo = &cdns_i2c_algo;
912 id->adap.timeout = CDNS_I2C_TIMEOUT;
913 id->adap.retries = 3; /* Default retry value. */
914 id->adap.algo_data = id;
915 id->adap.dev.parent = &pdev->dev;
916 init_completion(&id->xfer_done);
917 snprintf(id->adap.name, sizeof(id->adap.name),
918 "Cadence I2C at %08lx", (unsigned long)r_mem->start);
919
920 id->clk = devm_clk_get(&pdev->dev, NULL);
921 if (IS_ERR(id->clk)) {
922 dev_err(&pdev->dev, "input clock not found.\n");
923 return PTR_ERR(id->clk);
924 }
925 ret = clk_prepare_enable(id->clk);
926 if (ret)
927 dev_err(&pdev->dev, "Unable to enable clock.\n");
928
929 pm_runtime_enable(id->dev);
930 pm_runtime_set_autosuspend_delay(id->dev, CNDS_I2C_PM_TIMEOUT);
931 pm_runtime_use_autosuspend(id->dev);
932 pm_runtime_set_active(id->dev);
933
934 id->clk_rate_change_nb.notifier_call = cdns_i2c_clk_notifier_cb;
935 if (clk_notifier_register(id->clk, &id->clk_rate_change_nb))
936 dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
937 id->input_clk = clk_get_rate(id->clk);
938
939 ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
940 &id->i2c_clk);
941 if (ret || (id->i2c_clk > CDNS_I2C_SPEED_MAX))
942 id->i2c_clk = CDNS_I2C_SPEED_DEFAULT;
943
944 cdns_i2c_writereg(CDNS_I2C_CR_ACK_EN | CDNS_I2C_CR_NEA | CDNS_I2C_CR_MS,
945 CDNS_I2C_CR_OFFSET);
946
947 ret = cdns_i2c_setclk(id->input_clk, id);
948 if (ret) {
949 dev_err(&pdev->dev, "invalid SCL clock: %u Hz\n", id->i2c_clk);
950 ret = -EINVAL;
951 goto err_clk_dis;
952 }
953
954 ret = devm_request_irq(&pdev->dev, id->irq, cdns_i2c_isr, 0,
955 DRIVER_NAME, id);
956 if (ret) {
957 dev_err(&pdev->dev, "cannot get irq %d\n", id->irq);
958 goto err_clk_dis;
959 }
960
961 /*
962 * Cadence I2C controller has a bug wherein it generates
963 * invalid read transaction after HW timeout in master receiver mode.
964 * HW timeout is not used by this driver and the interrupt is disabled.
965 * But the feature itself cannot be disabled. Hence maximum value
966 * is written to this register to reduce the chances of error.
967 */
968 cdns_i2c_writereg(CDNS_I2C_TIMEOUT_MAX, CDNS_I2C_TIME_OUT_OFFSET);
969
970 ret = i2c_add_adapter(&id->adap);
971 if (ret < 0)
972 goto err_clk_dis;
973
974 dev_info(&pdev->dev, "%u kHz mmio %08lx irq %d\n",
975 id->i2c_clk / 1000, (unsigned long)r_mem->start, id->irq);
976
977 return 0;
978
979err_clk_dis:
980 clk_disable_unprepare(id->clk);
981 pm_runtime_set_suspended(&pdev->dev);
982 pm_runtime_disable(&pdev->dev);
983 return ret;
984}
985
986/**
987 * cdns_i2c_remove - Unregister the device after releasing the resources
988 * @pdev: Handle to the platform device structure
989 *
990 * This function frees all the resources allocated to the device.
991 *
992 * Return: 0 always
993 */
994static int cdns_i2c_remove(struct platform_device *pdev)
995{
996 struct cdns_i2c *id = platform_get_drvdata(pdev);
997
998 i2c_del_adapter(&id->adap);
999 clk_notifier_unregister(id->clk, &id->clk_rate_change_nb);
1000 clk_disable_unprepare(id->clk);
1001 pm_runtime_disable(&pdev->dev);
1002
1003 return 0;
1004}
1005
1006static struct platform_driver cdns_i2c_drv = {
1007 .driver = {
1008 .name = DRIVER_NAME,
1009 .of_match_table = cdns_i2c_of_match,
1010 .pm = &cdns_i2c_dev_pm_ops,
1011 },
1012 .probe = cdns_i2c_probe,
1013 .remove = cdns_i2c_remove,
1014};
1015
1016module_platform_driver(cdns_i2c_drv);
1017
1018MODULE_AUTHOR("Xilinx Inc.");
1019MODULE_DESCRIPTION("Cadence I2C bus driver");
1020MODULE_LICENSE("GPL");