1 From fbfab1ab065879370541caf0e514987368eb41b2 Mon Sep 17 00:00:00 2001
2 From: Abhishek Sahu <absahu@codeaurora.org>
3 Date: Mon, 12 Mar 2018 18:45:01 +0530
4 Subject: [PATCH 12/13] i2c: qup: reorganization of driver code to remove
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
10 Following are the major issues in current driver code
12 1. The current driver simply assumes the transfer completion
13 whenever its gets any non-error interrupts and then simply do the
14 polling of available/free bytes in FIFO.
15 2. The block mode is not working properly since no handling in
16 being done for OUT_BLOCK_WRITE_REQ and IN_BLOCK_READ_REQ.
18 Because of above, i2c v1 transfers of size greater than 32 are failing
19 with following error message
21 i2c_qup 78b6000.i2c: timeout for fifo out full
23 To make block mode working properly and move to use the interrupts
24 instead of polling, major code reorganization is required. Following
25 are the major changes done in this patch
27 1. Remove the polling of TX FIFO free space and RX FIFO available
28 bytes and move to interrupts completely. QUP has QUP_MX_OUTPUT_DONE,
29 QUP_MX_INPUT_DONE, OUT_BLOCK_WRITE_REQ and IN_BLOCK_READ_REQ
30 interrupts to handle FIFO’s properly so check all these interrupts.
31 2. During write, For FIFO mode, TX FIFO can be directly written
32 without checking for FIFO space. For block mode, the QUP will generate
33 OUT_BLOCK_WRITE_REQ interrupt whenever it has block size of available
35 3. During read, both TX and RX FIFO will be used. TX will be used
36 for writing tags and RX will be used for receiving the data. In QUP,
37 TX and RX can operate in separate mode so configure modes accordingly.
38 4. For read FIFO mode, wait for QUP_MX_INPUT_DONE interrupt which
39 will be generated after all the bytes have been copied in RX FIFO. For
40 read Block mode, QUP will generate IN_BLOCK_READ_REQ interrupts
41 whenever it has block size of available data.
43 Signed-off-by: Abhishek Sahu <absahu@codeaurora.org>
44 Reviewed-by: Sricharan R <sricharan@codeaurora.org>
45 Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
47 drivers/i2c/busses/i2c-qup.c | 366 +++++++++++++++++++++--------------
48 1 file changed, 223 insertions(+), 143 deletions(-)
50 --- a/drivers/i2c/busses/i2c-qup.c
51 +++ b/drivers/i2c/busses/i2c-qup.c
53 #define QUP_IN_SVC_FLAG BIT(9)
54 #define QUP_MX_OUTPUT_DONE BIT(10)
55 #define QUP_MX_INPUT_DONE BIT(11)
56 +#define OUT_BLOCK_WRITE_REQ BIT(12)
57 +#define IN_BLOCK_READ_REQ BIT(13)
59 /* I2C mini core related values */
60 +#define QUP_NO_INPUT BIT(7)
61 #define QUP_CLOCK_AUTO_GATE BIT(13)
62 #define I2C_MINI_CORE (2 << 8)
65 #define DEFAULT_CLK_FREQ 100000
66 #define DEFAULT_SRC_CLK 20000000
69 + * count: no of blocks
70 + * pos: current block number
71 + * tx_tag_len: tx tag length for current block
72 + * rx_tag_len: rx tag length for current block
73 + * data_len: remaining data length for current message
74 + * total_tx_len: total tx length including tag bytes for current QUP transfer
75 + * total_rx_len: total rx length including tag bytes for current QUP transfer
76 + * tx_fifo_free: number of free bytes in current QUP block write.
77 + * fifo_available: number of available bytes in RX FIFO for current
79 + * rx_bytes_read: if all the bytes have been read from rx FIFO.
80 + * is_tx_blk_mode: whether tx uses block or FIFO mode in case of non BAM xfer.
81 + * is_rx_blk_mode: whether rx uses block or FIFO mode in case of non BAM xfer.
82 + * tags: contains tx tag bytes for current QUP transfer
84 struct qup_i2c_block {
100 + bool rx_bytes_read;
101 + bool is_tx_blk_mode;
102 + bool is_rx_blk_mode;
107 @@ -186,6 +212,7 @@ struct qup_i2c_dev {
109 /* To check if this is the last msg */
113 /* To configure when bus is in run state */
115 @@ -202,11 +229,18 @@ struct qup_i2c_dev {
116 struct qup_i2c_bam btx;
118 struct completion xfer;
119 + /* function to write data in tx fifo */
120 + void (*write_tx_fifo)(struct qup_i2c_dev *qup);
121 + /* function to read data from rx fifo */
122 + void (*read_rx_fifo)(struct qup_i2c_dev *qup);
123 + /* function to write tags in tx fifo for i2c read transfer */
124 + void (*write_rx_tags)(struct qup_i2c_dev *qup);
127 static irqreturn_t qup_i2c_interrupt(int irq, void *dev)
129 struct qup_i2c_dev *qup = dev;
130 + struct qup_i2c_block *blk = &qup->blk;
134 @@ -253,12 +287,48 @@ static irqreturn_t qup_i2c_interrupt(int
138 - if (opflags & QUP_IN_SVC_FLAG)
139 - writel(QUP_IN_SVC_FLAG, qup->base + QUP_OPERATIONAL);
140 + if (!qup->is_qup_v1)
143 - if (opflags & QUP_OUT_SVC_FLAG)
144 + if (opflags & QUP_OUT_SVC_FLAG) {
145 writel(QUP_OUT_SVC_FLAG, qup->base + QUP_OPERATIONAL);
147 + if (opflags & OUT_BLOCK_WRITE_REQ) {
148 + blk->tx_fifo_free += qup->out_blk_sz;
149 + if (qup->msg->flags & I2C_M_RD)
150 + qup->write_rx_tags(qup);
152 + qup->write_tx_fifo(qup);
156 + if (opflags & QUP_IN_SVC_FLAG) {
157 + writel(QUP_IN_SVC_FLAG, qup->base + QUP_OPERATIONAL);
159 + if (!blk->is_rx_blk_mode) {
160 + blk->fifo_available += qup->in_fifo_sz;
161 + qup->read_rx_fifo(qup);
162 + } else if (opflags & IN_BLOCK_READ_REQ) {
163 + blk->fifo_available += qup->in_blk_sz;
164 + qup->read_rx_fifo(qup);
168 + if (qup->msg->flags & I2C_M_RD) {
169 + if (!blk->rx_bytes_read)
170 + return IRQ_HANDLED;
173 + * Ideally, QUP_MAX_OUTPUT_DONE_FLAG should be checked
174 + * for FIFO mode also. But, QUP_MAX_OUTPUT_DONE_FLAG lags
175 + * behind QUP_OUTPUT_SERVICE_FLAG sometimes. The only reason
176 + * of interrupt for write message in FIFO mode is
177 + * QUP_MAX_OUTPUT_DONE_FLAG condition.
179 + if (blk->is_tx_blk_mode && !(opflags & QUP_MX_OUTPUT_DONE))
180 + return IRQ_HANDLED;
184 qup->qup_err = qup_err;
185 qup->bus_err = bus_err;
186 @@ -324,6 +394,28 @@ static int qup_i2c_change_state(struct q
190 +/* Check if I2C bus returns to IDLE state */
191 +static int qup_i2c_bus_active(struct qup_i2c_dev *qup, int len)
193 + unsigned long timeout;
197 + timeout = jiffies + len * 4;
199 + status = readl(qup->base + QUP_I2C_STATUS);
200 + if (!(status & I2C_STATUS_BUS_ACTIVE))
203 + if (time_after(jiffies, timeout))
206 + usleep_range(len, len * 2);
213 * qup_i2c_wait_ready - wait for a give number of bytes in tx/rx path
214 * @qup: The qup_i2c_dev device
215 @@ -394,23 +486,6 @@ static void qup_i2c_set_write_mode_v2(st
219 -static void qup_i2c_set_write_mode(struct qup_i2c_dev *qup, struct i2c_msg *msg)
221 - /* Number of entries to shift out, including the start */
222 - int total = msg->len + 1;
224 - if (total < qup->out_fifo_sz) {
226 - writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
227 - writel(total, qup->base + QUP_MX_WRITE_CNT);
229 - /* BLOCK mode (transfer data on chunks) */
230 - writel(QUP_OUTPUT_BLK_MODE | QUP_REPACK_EN,
231 - qup->base + QUP_IO_MODE);
232 - writel(total, qup->base + QUP_MX_OUTPUT_CNT);
236 static int check_for_fifo_space(struct qup_i2c_dev *qup)
239 @@ -443,28 +518,25 @@ out:
243 -static int qup_i2c_issue_write(struct qup_i2c_dev *qup, struct i2c_msg *msg)
244 +static void qup_i2c_write_tx_fifo_v1(struct qup_i2c_dev *qup)
246 + struct qup_i2c_block *blk = &qup->blk;
247 + struct i2c_msg *msg = qup->msg;
248 u32 addr = msg->addr << 1;
255 val = QUP_TAG_START | addr;
257 + blk->tx_fifo_free--;
263 - while (qup->pos < msg->len) {
264 - /* Check that there's space in the FIFO for our pair */
265 - ret = check_for_fifo_space(qup);
269 + while (blk->tx_fifo_free && qup->pos < msg->len) {
270 if (qup->pos == msg->len - 1)
271 qup_tag = QUP_TAG_STOP;
273 @@ -481,11 +553,8 @@ static int qup_i2c_issue_write(struct qu
277 + blk->tx_fifo_free--;
280 - ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
285 static void qup_i2c_set_blk_data(struct qup_i2c_dev *qup,
286 @@ -1006,64 +1075,6 @@ err:
290 -static int qup_i2c_write_one(struct qup_i2c_dev *qup, struct i2c_msg *msg)
297 - enable_irq(qup->irq);
299 - qup_i2c_set_write_mode(qup, msg);
301 - ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
305 - writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
308 - ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
312 - ret = qup_i2c_issue_write(qup, msg);
316 - ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
320 - ret = qup_i2c_wait_for_complete(qup, msg);
323 - } while (qup->pos < msg->len);
325 - /* Wait for the outstanding data in the fifo to drain */
326 - ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY, RESET_BIT, ONE_BYTE);
328 - disable_irq(qup->irq);
334 -static void qup_i2c_set_read_mode(struct qup_i2c_dev *qup, int len)
336 - if (len < qup->in_fifo_sz) {
338 - writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
339 - writel(len, qup->base + QUP_MX_READ_CNT);
341 - /* BLOCK mode (transfer data on chunks) */
342 - writel(QUP_INPUT_BLK_MODE | QUP_REPACK_EN,
343 - qup->base + QUP_IO_MODE);
344 - writel(len, qup->base + QUP_MX_INPUT_CNT);
348 static void qup_i2c_set_read_mode_v2(struct qup_i2c_dev *qup, int len)
350 int tx_len = qup->blk.tx_tag_len;
351 @@ -1086,44 +1097,27 @@ static void qup_i2c_set_read_mode_v2(str
355 -static void qup_i2c_issue_read(struct qup_i2c_dev *qup, struct i2c_msg *msg)
357 - u32 addr, len, val;
359 - addr = i2c_8bit_addr_from_msg(msg);
361 - /* 0 is used to specify a length 256 (QUP_READ_LIMIT) */
362 - len = (msg->len == QUP_READ_LIMIT) ? 0 : msg->len;
364 - val = ((QUP_TAG_REC | len) << QUP_MSW_SHIFT) | QUP_TAG_START | addr;
365 - writel(val, qup->base + QUP_OUT_FIFO_BASE);
369 -static int qup_i2c_read_fifo(struct qup_i2c_dev *qup, struct i2c_msg *msg)
370 +static void qup_i2c_read_rx_fifo_v1(struct qup_i2c_dev *qup)
372 + struct qup_i2c_block *blk = &qup->blk;
373 + struct i2c_msg *msg = qup->msg;
379 - for (idx = 0; qup->pos < msg->len; idx++) {
380 + while (blk->fifo_available && qup->pos < msg->len) {
381 if ((idx & 1) == 0) {
382 - /* Check that FIFO have data */
383 - ret = qup_i2c_wait_ready(qup, QUP_IN_NOT_EMPTY,
384 - SET_BIT, 4 * ONE_BYTE);
388 /* Reading 2 words at time */
389 val = readl(qup->base + QUP_IN_FIFO_BASE);
391 msg->buf[qup->pos++] = val & 0xFF;
393 msg->buf[qup->pos++] = val >> QUP_MSW_SHIFT;
396 + blk->fifo_available--;
400 + if (qup->pos == msg->len)
401 + blk->rx_bytes_read = true;
404 static int qup_i2c_read_fifo_v2(struct qup_i2c_dev *qup,
405 @@ -1224,49 +1218,130 @@ err:
409 -static int qup_i2c_read_one(struct qup_i2c_dev *qup, struct i2c_msg *msg)
410 +static void qup_i2c_write_rx_tags_v1(struct qup_i2c_dev *qup)
413 + struct i2c_msg *msg = qup->msg;
414 + u32 addr, len, val;
418 + addr = i2c_8bit_addr_from_msg(msg);
420 - enable_irq(qup->irq);
421 - qup_i2c_set_read_mode(qup, msg->len);
422 + /* 0 is used to specify a length 256 (QUP_READ_LIMIT) */
423 + len = (msg->len == QUP_READ_LIMIT) ? 0 : msg->len;
425 + val = ((QUP_TAG_REC | len) << QUP_MSW_SHIFT) | QUP_TAG_START | addr;
426 + writel(val, qup->base + QUP_OUT_FIFO_BASE);
429 +static void qup_i2c_conf_v1(struct qup_i2c_dev *qup)
431 + struct qup_i2c_block *blk = &qup->blk;
432 + u32 qup_config = I2C_MINI_CORE | I2C_N_VAL;
433 + u32 io_mode = QUP_REPACK_EN;
435 + blk->is_tx_blk_mode =
436 + blk->total_tx_len > qup->out_fifo_sz ? true : false;
437 + blk->is_rx_blk_mode =
438 + blk->total_rx_len > qup->in_fifo_sz ? true : false;
440 + if (blk->is_tx_blk_mode) {
441 + io_mode |= QUP_OUTPUT_BLK_MODE;
442 + writel(0, qup->base + QUP_MX_WRITE_CNT);
443 + writel(blk->total_tx_len, qup->base + QUP_MX_OUTPUT_CNT);
445 + writel(0, qup->base + QUP_MX_OUTPUT_CNT);
446 + writel(blk->total_tx_len, qup->base + QUP_MX_WRITE_CNT);
449 + if (blk->total_rx_len) {
450 + if (blk->is_rx_blk_mode) {
451 + io_mode |= QUP_INPUT_BLK_MODE;
452 + writel(0, qup->base + QUP_MX_READ_CNT);
453 + writel(blk->total_rx_len, qup->base + QUP_MX_INPUT_CNT);
455 + writel(0, qup->base + QUP_MX_INPUT_CNT);
456 + writel(blk->total_rx_len, qup->base + QUP_MX_READ_CNT);
459 + qup_config |= QUP_NO_INPUT;
462 + writel(qup_config, qup->base + QUP_CONFIG);
463 + writel(io_mode, qup->base + QUP_IO_MODE);
466 +static void qup_i2c_clear_blk_v1(struct qup_i2c_block *blk)
468 + blk->tx_fifo_free = 0;
469 + blk->fifo_available = 0;
470 + blk->rx_bytes_read = false;
473 +static int qup_i2c_conf_xfer_v1(struct qup_i2c_dev *qup, bool is_rx)
475 + struct qup_i2c_block *blk = &qup->blk;
478 + qup_i2c_clear_blk_v1(blk);
479 + qup_i2c_conf_v1(qup);
480 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
485 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
487 ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
492 + reinit_completion(&qup->xfer);
493 + enable_irq(qup->irq);
494 + if (!blk->is_tx_blk_mode) {
495 + blk->tx_fifo_free = qup->out_fifo_sz;
497 - qup_i2c_issue_read(qup, msg);
499 + qup_i2c_write_rx_tags_v1(qup);
501 + qup_i2c_write_tx_fifo_v1(qup);
504 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
509 - ret = qup_i2c_wait_for_complete(qup, msg);
512 + ret = qup_i2c_wait_for_complete(qup, qup->msg);
516 - ret = qup_i2c_read_fifo(qup, msg);
519 - } while (qup->pos < msg->len);
520 + ret = qup_i2c_bus_active(qup, ONE_BYTE);
523 disable_irq(qup->irq);
529 +static int qup_i2c_write_one(struct qup_i2c_dev *qup)
531 + struct i2c_msg *msg = qup->msg;
532 + struct qup_i2c_block *blk = &qup->blk;
535 + blk->total_tx_len = msg->len + 1;
536 + blk->total_rx_len = 0;
538 + return qup_i2c_conf_xfer_v1(qup, false);
541 +static int qup_i2c_read_one(struct qup_i2c_dev *qup)
543 + struct qup_i2c_block *blk = &qup->blk;
546 + blk->total_tx_len = 2;
547 + blk->total_rx_len = qup->msg->len;
549 + return qup_i2c_conf_xfer_v1(qup, true);
552 static int qup_i2c_xfer(struct i2c_adapter *adap,
553 struct i2c_msg msgs[],
555 @@ -1305,10 +1380,11 @@ static int qup_i2c_xfer(struct i2c_adapt
559 + qup->msg = &msgs[idx];
560 if (msgs[idx].flags & I2C_M_RD)
561 - ret = qup_i2c_read_one(qup, &msgs[idx]);
562 + ret = qup_i2c_read_one(qup);
564 - ret = qup_i2c_write_one(qup, &msgs[idx]);
565 + ret = qup_i2c_write_one(qup);
569 @@ -1487,6 +1563,10 @@ static int qup_i2c_probe(struct platform
570 if (of_device_is_compatible(pdev->dev.of_node, "qcom,i2c-qup-v1.1.1")) {
571 qup->adap.algo = &qup_i2c_algo;
572 qup->adap.quirks = &qup_i2c_quirks;
573 + qup->is_qup_v1 = true;
574 + qup->write_tx_fifo = qup_i2c_write_tx_fifo_v1;
575 + qup->read_rx_fifo = qup_i2c_read_rx_fifo_v1;
576 + qup->write_rx_tags = qup_i2c_write_rx_tags_v1;
578 qup->adap.algo = &qup_i2c_algo_v2;
579 ret = qup_i2c_req_dma(qup);