1 From 7545c7dba169c4c29ba5f6ab8706267a84c0febe Mon Sep 17 00:00:00 2001
2 From: Abhishek Sahu <absahu@codeaurora.org>
3 Date: Mon, 12 Mar 2018 18:45:02 +0530
4 Subject: [PATCH 13/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_READ.
17 3. An i2c transfer can contain multiple message and QUP v2
18 supports reconfiguration during run in which the mode should be same
19 for all the sub transfer. Currently the mode is being programmed
20 before every sub transfer which is functionally wrong. If one message
21 is less than FIFO length and other message is greater than FIFO
22 length, then transfers will fail.
24 Because of above, i2c v2 transfers of size greater than 64 are failing
25 with following error message
27 i2c_qup 78b6000.i2c: timeout for fifo out full
29 To make block mode working properly and move to use the interrupts
30 instead of polling, major code reorganization is required. Following
31 are the major changes done in this patch
33 1. Remove the polling of TX FIFO free space and RX FIFO available
34 bytes and move to interrupts completely. QUP has QUP_MX_OUTPUT_DONE,
35 QUP_MX_INPUT_DONE, OUT_BLOCK_WRITE_REQ and IN_BLOCK_READ_REQ
36 interrupts to handle FIFO’s properly so check all these interrupts.
37 2. Determine the mode for transfer before starting by checking
38 all the tx/rx data length in each message. The complete message can be
39 transferred either in DMA mode or Programmed IO by FIFO/Block mode.
40 in DMA mode, both tx and rx uses same mode but in PIO mode, the TX and
41 RX can be in different mode.
42 3. During write, For FIFO mode, TX FIFO can be directly written
43 without checking for FIFO space. For block mode, the QUP will generate
44 OUT_BLOCK_WRITE_REQ interrupt whenever it has block size of available
46 4. During read, both TX and RX FIFO will be used. TX will be used
47 for writing tags and RX will be used for receiving the data. In QUP,
48 TX and RX can operate in separate mode so configure modes accordingly.
49 5. For read FIFO mode, wait for QUP_MX_INPUT_DONE interrupt which
50 will be generated after all the bytes have been copied in RX FIFO. For
51 read Block mode, QUP will generate IN_BLOCK_READ_REQ interrupts
52 whenever it has block size of available data.
53 6. Split the transfer in chunk of one QUP block size(256 bytes)
54 and schedule each block separately. QUP v2 supports reconfiguration
55 during run in which QUP can transfer multiple blocks without issuing a
57 7. Port the SMBus block read support for new code changes.
59 Signed-off-by: Abhishek Sahu <absahu@codeaurora.org>
60 Reviewed-by: Sricharan R <sricharan@codeaurora.org>
61 Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
63 drivers/i2c/busses/i2c-qup.c | 900 ++++++++++++++++++++---------------
64 1 file changed, 515 insertions(+), 385 deletions(-)
66 --- a/drivers/i2c/busses/i2c-qup.c
67 +++ b/drivers/i2c/busses/i2c-qup.c
69 #define DEFAULT_SRC_CLK 20000000
72 + * Max tags length (start, stop and maximum 2 bytes address) for each QUP
75 +#define QUP_MAX_TAGS_LEN 4
76 +/* Max data length for each DATARD tags */
77 +#define RECV_MAX_DATA_LEN 254
78 +/* TAG length for DATA READ in RX FIFO */
79 +#define READ_RX_TAGS_LEN 2
83 * pos: current block number
84 * tx_tag_len: tx tag length for current block
85 * rx_tag_len: rx tag length for current block
86 * data_len: remaining data length for current message
87 + * cur_blk_len: data length for current block
88 * total_tx_len: total tx length including tag bytes for current QUP transfer
89 * total_rx_len: total rx length including tag bytes for current QUP transfer
90 + * tx_fifo_data_pos: current byte number in TX FIFO word
91 * tx_fifo_free: number of free bytes in current QUP block write.
92 + * rx_fifo_data_pos: current byte number in RX FIFO word
93 * fifo_available: number of available bytes in RX FIFO for current
95 + * tx_fifo_data: QUP TX FIFO write works on word basis (4 bytes). New byte write
96 + * to TX FIFO will be appended in this data and will be written to
97 + * TX FIFO when all the 4 bytes are available.
98 + * rx_fifo_data: QUP RX FIFO read works on word basis (4 bytes). This will
99 + * contains the 4 bytes of RX data.
100 + * cur_data: pointer to tell cur data position for current message
101 + * cur_tx_tags: pointer to tell cur position in tags
102 + * tx_tags_sent: all tx tag bytes have been written in FIFO word
103 + * send_last_word: for tx FIFO, last word send is pending in current block
104 * rx_bytes_read: if all the bytes have been read from rx FIFO.
105 + * rx_tags_fetched: all the rx tag bytes have been fetched from rx fifo word
106 * is_tx_blk_mode: whether tx uses block or FIFO mode in case of non BAM xfer.
107 * is_rx_blk_mode: whether rx uses block or FIFO mode in case of non BAM xfer.
108 * tags: contains tx tag bytes for current QUP transfer
109 @@ -162,10 +185,20 @@ struct qup_i2c_block {
116 + int tx_fifo_data_pos;
118 + int rx_fifo_data_pos;
125 + bool send_last_word;
126 + bool rx_tags_fetched;
130 @@ -198,6 +231,7 @@ struct qup_i2c_dev {
134 + int blk_xfer_limit;
135 unsigned long one_byte_t;
136 unsigned long xfer_timeout;
137 struct qup_i2c_block blk;
138 @@ -212,10 +246,10 @@ struct qup_i2c_dev {
140 /* To check if this is the last msg */
143 + bool is_smbus_read;
145 /* To configure when bus is in run state */
151 @@ -223,6 +257,8 @@ struct qup_i2c_dev {
153 unsigned int max_xfer_sg_len;
154 unsigned int tag_buf_pos;
155 + /* The threshold length above which block mode will be used */
156 + unsigned int blk_mode_threshold;
157 struct dma_pool *dpool;
158 struct qup_i2c_tag start_tag;
159 struct qup_i2c_bam brx;
160 @@ -287,9 +323,6 @@ static irqreturn_t qup_i2c_interrupt(int
164 - if (!qup->is_qup_v1)
167 if (opflags & QUP_OUT_SVC_FLAG) {
168 writel(QUP_OUT_SVC_FLAG, qup->base + QUP_OPERATIONAL);
170 @@ -416,108 +449,6 @@ static int qup_i2c_bus_active(struct qup
175 - * qup_i2c_wait_ready - wait for a give number of bytes in tx/rx path
176 - * @qup: The qup_i2c_dev device
177 - * @op: The bit/event to wait on
178 - * @val: value of the bit to wait on, 0 or 1
179 - * @len: The length the bytes to be transferred
181 -static int qup_i2c_wait_ready(struct qup_i2c_dev *qup, int op, bool val,
184 - unsigned long timeout;
187 - u32 shift = __ffs(op);
190 - len *= qup->one_byte_t;
191 - /* timeout after a wait of twice the max time */
192 - timeout = jiffies + len * 4;
195 - opflags = readl(qup->base + QUP_OPERATIONAL);
196 - status = readl(qup->base + QUP_I2C_STATUS);
198 - if (((opflags & op) >> shift) == val) {
199 - if ((op == QUP_OUT_NOT_EMPTY) && qup->is_last) {
200 - if (!(status & I2C_STATUS_BUS_ACTIVE)) {
210 - if (time_after(jiffies, timeout)) {
214 - usleep_range(len, len * 2);
218 - if (qup->bus_err || qup->qup_err)
219 - ret = (qup->bus_err & QUP_I2C_NACK_FLAG) ? -ENXIO : -EIO;
224 -static void qup_i2c_set_write_mode_v2(struct qup_i2c_dev *qup,
225 - struct i2c_msg *msg)
227 - /* Number of entries to shift out, including the tags */
228 - int total = msg->len + qup->blk.tx_tag_len;
230 - total |= qup->config_run;
232 - if (total < qup->out_fifo_sz) {
234 - writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
235 - writel(total, qup->base + QUP_MX_WRITE_CNT);
237 - /* BLOCK mode (transfer data on chunks) */
238 - writel(QUP_OUTPUT_BLK_MODE | QUP_REPACK_EN,
239 - qup->base + QUP_IO_MODE);
240 - writel(total, qup->base + QUP_MX_OUTPUT_CNT);
244 -static int check_for_fifo_space(struct qup_i2c_dev *qup)
248 - ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
252 - ret = qup_i2c_wait_ready(qup, QUP_OUT_FULL,
253 - RESET_BIT, 4 * ONE_BYTE);
255 - /* Fifo is full. Drain out the fifo */
256 - ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
260 - ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY,
261 - RESET_BIT, 256 * ONE_BYTE);
263 - dev_err(qup->dev, "timeout for fifo out full");
267 - ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
276 static void qup_i2c_write_tx_fifo_v1(struct qup_i2c_dev *qup)
278 struct qup_i2c_block *blk = &qup->blk;
279 @@ -560,60 +491,17 @@ static void qup_i2c_write_tx_fifo_v1(str
280 static void qup_i2c_set_blk_data(struct qup_i2c_dev *qup,
283 - memset(&qup->blk, 0, sizeof(qup->blk));
286 qup->blk.data_len = msg->len;
287 - qup->blk.count = (msg->len + QUP_READ_LIMIT - 1) / QUP_READ_LIMIT;
289 - /* 4 bytes for first block and 2 writes for rest */
290 - qup->blk.tx_tag_len = 4 + (qup->blk.count - 1) * 2;
292 - /* There are 2 tag bytes that are read in to fifo for every block */
293 - if (msg->flags & I2C_M_RD)
294 - qup->blk.rx_tag_len = qup->blk.count * 2;
297 -static int qup_i2c_send_data(struct qup_i2c_dev *qup, int tlen, u8 *tbuf,
298 - int dlen, u8 *dbuf)
300 - u32 val = 0, idx = 0, pos = 0, i = 0, t;
301 - int len = tlen + dlen;
306 - ret = check_for_fifo_space(qup);
310 - t = (len >= 4) ? 4 : len;
313 - if (!i && (pos >= tlen)) {
318 - val |= buf[pos++] << (idx++ * 8);
321 - writel(val, qup->base + QUP_OUT_FIFO_BASE);
327 - ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
330 + qup->blk.count = DIV_ROUND_UP(msg->len, qup->blk_xfer_limit);
333 static int qup_i2c_get_data_len(struct qup_i2c_dev *qup)
337 - if (qup->blk.data_len > QUP_READ_LIMIT)
338 - data_len = QUP_READ_LIMIT;
339 + if (qup->blk.data_len > qup->blk_xfer_limit)
340 + data_len = qup->blk_xfer_limit;
342 data_len = qup->blk.data_len;
344 @@ -630,9 +518,9 @@ static int qup_i2c_set_tags_smb(u16 addr
348 - if (msg->len > 1) {
349 + if (qup->is_smbus_read) {
350 tags[len++] = QUP_TAG_V2_DATARD_STOP;
351 - tags[len++] = qup_i2c_get_data_len(qup) - 1;
352 + tags[len++] = qup_i2c_get_data_len(qup);
354 tags[len++] = QUP_TAG_V2_START;
355 tags[len++] = addr & 0xff;
356 @@ -694,24 +582,6 @@ static int qup_i2c_set_tags(u8 *tags, st
360 -static int qup_i2c_issue_xfer_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
362 - int data_len = 0, tag_len, index;
365 - tag_len = qup_i2c_set_tags(qup->blk.tags, qup, msg);
366 - index = msg->len - qup->blk.data_len;
368 - /* only tags are written for read */
369 - if (!(msg->flags & I2C_M_RD))
370 - data_len = qup_i2c_get_data_len(qup);
372 - ret = qup_i2c_send_data(qup, tag_len, qup->blk.tags,
373 - data_len, &msg->buf[index]);
374 - qup->blk.data_len -= data_len;
379 static void qup_i2c_bam_cb(void *data)
381 @@ -778,6 +648,7 @@ static int qup_i2c_bam_make_desc(struct
382 u32 i = 0, tlen, tx_len = 0;
385 + qup->blk_xfer_limit = QUP_READ_LIMIT;
386 qup_i2c_set_blk_data(qup, msg);
388 blocks = qup->blk.count;
389 @@ -1026,7 +897,7 @@ static int qup_i2c_wait_for_complete(str
393 - left = wait_for_completion_timeout(&qup->xfer, HZ);
394 + left = wait_for_completion_timeout(&qup->xfer, qup->xfer_timeout);
396 writel(1, qup->base + QUP_SW_RESET);
398 @@ -1038,65 +909,6 @@ static int qup_i2c_wait_for_complete(str
402 -static int qup_i2c_write_one_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
408 - enable_irq(qup->irq);
409 - qup_i2c_set_blk_data(qup, msg);
410 - qup_i2c_set_write_mode_v2(qup, msg);
412 - ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
416 - writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
419 - ret = qup_i2c_issue_xfer_v2(qup, msg);
423 - ret = qup_i2c_wait_for_complete(qup, msg);
428 - } while (qup->blk.pos < qup->blk.count);
430 - ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY, RESET_BIT, ONE_BYTE);
433 - disable_irq(qup->irq);
439 -static void qup_i2c_set_read_mode_v2(struct qup_i2c_dev *qup, int len)
441 - int tx_len = qup->blk.tx_tag_len;
443 - len += qup->blk.rx_tag_len;
444 - len |= qup->config_run;
445 - tx_len |= qup->config_run;
447 - if (len < qup->in_fifo_sz) {
449 - writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
450 - writel(tx_len, qup->base + QUP_MX_WRITE_CNT);
451 - writel(len, qup->base + QUP_MX_READ_CNT);
453 - /* BLOCK mode (transfer data on chunks) */
454 - writel(QUP_INPUT_BLK_MODE | QUP_REPACK_EN,
455 - qup->base + QUP_IO_MODE);
456 - writel(tx_len, qup->base + QUP_MX_OUTPUT_CNT);
457 - writel(len, qup->base + QUP_MX_INPUT_CNT);
461 static void qup_i2c_read_rx_fifo_v1(struct qup_i2c_dev *qup)
463 struct qup_i2c_block *blk = &qup->blk;
464 @@ -1120,104 +932,6 @@ static void qup_i2c_read_rx_fifo_v1(stru
465 blk->rx_bytes_read = true;
468 -static int qup_i2c_read_fifo_v2(struct qup_i2c_dev *qup,
469 - struct i2c_msg *msg)
472 - int idx, pos = 0, ret = 0, total, msg_offset = 0;
475 - * If the message length is already read in
476 - * the first byte of the buffer, account for
477 - * that by setting the offset
479 - if (qup_i2c_check_msg_len(msg) && (msg->len > 1))
481 - total = qup_i2c_get_data_len(qup);
482 - total -= msg_offset;
484 - /* 2 extra bytes for read tags */
485 - while (pos < (total + 2)) {
486 - /* Check that FIFO have data */
487 - ret = qup_i2c_wait_ready(qup, QUP_IN_NOT_EMPTY,
488 - SET_BIT, 4 * ONE_BYTE);
490 - dev_err(qup->dev, "timeout for fifo not empty");
493 - val = readl(qup->base + QUP_IN_FIFO_BASE);
495 - for (idx = 0; idx < 4; idx++, val >>= 8, pos++) {
496 - /* first 2 bytes are tag bytes */
500 - if (pos >= (total + 2))
502 - msg->buf[qup->pos + msg_offset] = val & 0xff;
508 - qup->blk.data_len -= total;
513 -static int qup_i2c_read_one_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
519 - enable_irq(qup->irq);
520 - qup_i2c_set_blk_data(qup, msg);
521 - qup_i2c_set_read_mode_v2(qup, msg->len);
523 - ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
527 - writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
530 - ret = qup_i2c_issue_xfer_v2(qup, msg);
534 - ret = qup_i2c_wait_for_complete(qup, msg);
538 - ret = qup_i2c_read_fifo_v2(qup, msg);
544 - /* Handle SMBus block read length */
545 - if (qup_i2c_check_msg_len(msg) && (msg->len == 1)) {
546 - if (msg->buf[0] > I2C_SMBUS_BLOCK_MAX) {
550 - msg->len += msg->buf[0];
552 - qup_i2c_set_blk_data(qup, msg);
553 - /* set tag length for block read */
554 - qup->blk.tx_tag_len = 2;
555 - qup_i2c_set_read_mode_v2(qup, msg->buf[0]);
557 - } while (qup->blk.pos < qup->blk.count);
560 - disable_irq(qup->irq);
566 static void qup_i2c_write_rx_tags_v1(struct qup_i2c_dev *qup)
568 struct i2c_msg *msg = qup->msg;
569 @@ -1404,13 +1118,434 @@ out:
574 + * Configure registers related with reconfiguration during run and call it
575 + * before each i2c sub transfer.
577 +static void qup_i2c_conf_count_v2(struct qup_i2c_dev *qup)
579 + struct qup_i2c_block *blk = &qup->blk;
580 + u32 qup_config = I2C_MINI_CORE | I2C_N_VAL_V2;
582 + if (blk->is_tx_blk_mode)
583 + writel(qup->config_run | blk->total_tx_len,
584 + qup->base + QUP_MX_OUTPUT_CNT);
586 + writel(qup->config_run | blk->total_tx_len,
587 + qup->base + QUP_MX_WRITE_CNT);
589 + if (blk->total_rx_len) {
590 + if (blk->is_rx_blk_mode)
591 + writel(qup->config_run | blk->total_rx_len,
592 + qup->base + QUP_MX_INPUT_CNT);
594 + writel(qup->config_run | blk->total_rx_len,
595 + qup->base + QUP_MX_READ_CNT);
597 + qup_config |= QUP_NO_INPUT;
600 + writel(qup_config, qup->base + QUP_CONFIG);
604 + * Configure registers related with transfer mode (FIFO/Block)
605 + * before starting of i2c transfer. It will be called only once in
608 +static void qup_i2c_conf_mode_v2(struct qup_i2c_dev *qup)
610 + struct qup_i2c_block *blk = &qup->blk;
611 + u32 io_mode = QUP_REPACK_EN;
613 + if (blk->is_tx_blk_mode) {
614 + io_mode |= QUP_OUTPUT_BLK_MODE;
615 + writel(0, qup->base + QUP_MX_WRITE_CNT);
617 + writel(0, qup->base + QUP_MX_OUTPUT_CNT);
620 + if (blk->is_rx_blk_mode) {
621 + io_mode |= QUP_INPUT_BLK_MODE;
622 + writel(0, qup->base + QUP_MX_READ_CNT);
624 + writel(0, qup->base + QUP_MX_INPUT_CNT);
627 + writel(io_mode, qup->base + QUP_IO_MODE);
630 +/* Clear required variables before starting of any QUP v2 sub transfer. */
631 +static void qup_i2c_clear_blk_v2(struct qup_i2c_block *blk)
633 + blk->send_last_word = false;
634 + blk->tx_tags_sent = false;
635 + blk->tx_fifo_data = 0;
636 + blk->tx_fifo_data_pos = 0;
637 + blk->tx_fifo_free = 0;
639 + blk->rx_tags_fetched = false;
640 + blk->rx_bytes_read = false;
641 + blk->rx_fifo_data = 0;
642 + blk->rx_fifo_data_pos = 0;
643 + blk->fifo_available = 0;
646 +/* Receive data from RX FIFO for read message in QUP v2 i2c transfer. */
647 +static void qup_i2c_recv_data(struct qup_i2c_dev *qup)
649 + struct qup_i2c_block *blk = &qup->blk;
652 + for (j = blk->rx_fifo_data_pos;
653 + blk->cur_blk_len && blk->fifo_available;
654 + blk->cur_blk_len--, blk->fifo_available--) {
656 + blk->rx_fifo_data = readl(qup->base + QUP_IN_FIFO_BASE);
658 + *(blk->cur_data++) = blk->rx_fifo_data;
659 + blk->rx_fifo_data >>= 8;
667 + blk->rx_fifo_data_pos = j;
670 +/* Receive tags for read message in QUP v2 i2c transfer. */
671 +static void qup_i2c_recv_tags(struct qup_i2c_dev *qup)
673 + struct qup_i2c_block *blk = &qup->blk;
675 + blk->rx_fifo_data = readl(qup->base + QUP_IN_FIFO_BASE);
676 + blk->rx_fifo_data >>= blk->rx_tag_len * 8;
677 + blk->rx_fifo_data_pos = blk->rx_tag_len;
678 + blk->fifo_available -= blk->rx_tag_len;
682 + * Read the data and tags from RX FIFO. Since in read case, the tags will be
683 + * preceded by received data bytes so
684 + * 1. Check if rx_tags_fetched is false i.e. the start of QUP block so receive
685 + * all tag bytes and discard that.
686 + * 2. Read the data from RX FIFO. When all the data bytes have been read then
687 + * set rx_bytes_read to true.
689 +static void qup_i2c_read_rx_fifo_v2(struct qup_i2c_dev *qup)
691 + struct qup_i2c_block *blk = &qup->blk;
693 + if (!blk->rx_tags_fetched) {
694 + qup_i2c_recv_tags(qup);
695 + blk->rx_tags_fetched = true;
698 + qup_i2c_recv_data(qup);
699 + if (!blk->cur_blk_len)
700 + blk->rx_bytes_read = true;
704 + * Write bytes in TX FIFO for write message in QUP v2 i2c transfer. QUP TX FIFO
705 + * write works on word basis (4 bytes). Append new data byte write for TX FIFO
706 + * in tx_fifo_data and write to TX FIFO when all the 4 bytes are present.
709 +qup_i2c_write_blk_data(struct qup_i2c_dev *qup, u8 **data, unsigned int *len)
711 + struct qup_i2c_block *blk = &qup->blk;
714 + for (j = blk->tx_fifo_data_pos; *len && blk->tx_fifo_free;
715 + (*len)--, blk->tx_fifo_free--) {
716 + blk->tx_fifo_data |= *(*data)++ << (j * 8);
718 + writel(blk->tx_fifo_data,
719 + qup->base + QUP_OUT_FIFO_BASE);
720 + blk->tx_fifo_data = 0x0;
727 + blk->tx_fifo_data_pos = j;
730 +/* Transfer tags for read message in QUP v2 i2c transfer. */
731 +static void qup_i2c_write_rx_tags_v2(struct qup_i2c_dev *qup)
733 + struct qup_i2c_block *blk = &qup->blk;
735 + qup_i2c_write_blk_data(qup, &blk->cur_tx_tags, &blk->tx_tag_len);
736 + if (blk->tx_fifo_data_pos)
737 + writel(blk->tx_fifo_data, qup->base + QUP_OUT_FIFO_BASE);
741 + * Write the data and tags in TX FIFO. Since in write case, both tags and data
742 + * need to be written and QUP write tags can have maximum 256 data length, so
744 + * 1. Check if tx_tags_sent is false i.e. the start of QUP block so write the
745 + * tags to TX FIFO and set tx_tags_sent to true.
746 + * 2. Check if send_last_word is true. It will be set when last few data bytes
747 + * (less than 4 bytes) are reamining to be written in FIFO because of no FIFO
748 + * space. All this data bytes are available in tx_fifo_data so write this
750 + * 3. Write the data to TX FIFO and check for cur_blk_len. If it is non zero
751 + * then more data is pending otherwise following 3 cases can be possible
752 + * a. if tx_fifo_data_pos is zero i.e. all the data bytes in this block
753 + * have been written in TX FIFO so nothing else is required.
754 + * b. tx_fifo_free is non zero i.e tx FIFO is free so copy the remaining data
755 + * from tx_fifo_data to tx FIFO. Since, qup_i2c_write_blk_data do write
756 + * in 4 bytes and FIFO space is in multiple of 4 bytes so tx_fifo_free
757 + * will be always greater than or equal to 4 bytes.
758 + * c. tx_fifo_free is zero. In this case, last few bytes (less than 4
759 + * bytes) are copied to tx_fifo_data but couldn't be sent because of
760 + * FIFO full so make send_last_word true.
762 +static void qup_i2c_write_tx_fifo_v2(struct qup_i2c_dev *qup)
764 + struct qup_i2c_block *blk = &qup->blk;
766 + if (!blk->tx_tags_sent) {
767 + qup_i2c_write_blk_data(qup, &blk->cur_tx_tags,
769 + blk->tx_tags_sent = true;
772 + if (blk->send_last_word)
773 + goto send_last_word;
775 + qup_i2c_write_blk_data(qup, &blk->cur_data, &blk->cur_blk_len);
776 + if (!blk->cur_blk_len) {
777 + if (!blk->tx_fifo_data_pos)
780 + if (blk->tx_fifo_free)
781 + goto send_last_word;
783 + blk->send_last_word = true;
789 + writel(blk->tx_fifo_data, qup->base + QUP_OUT_FIFO_BASE);
793 + * Main transfer function which read or write i2c data.
794 + * The QUP v2 supports reconfiguration during run in which multiple i2c sub
795 + * transfers can be scheduled.
798 +qup_i2c_conf_xfer_v2(struct qup_i2c_dev *qup, bool is_rx, bool is_first,
799 + bool change_pause_state)
801 + struct qup_i2c_block *blk = &qup->blk;
802 + struct i2c_msg *msg = qup->msg;
806 + * Check if its SMBus Block read for which the top level read will be
807 + * done into 2 QUP reads. One with message length 1 while other one is
808 + * with actual length.
810 + if (qup_i2c_check_msg_len(msg)) {
811 + if (qup->is_smbus_read) {
813 + * If the message length is already read in
814 + * the first byte of the buffer, account for
815 + * that by setting the offset
817 + blk->cur_data += 1;
820 + change_pause_state = false;
824 + qup->config_run = is_first ? 0 : QUP_I2C_MX_CONFIG_DURING_RUN;
826 + qup_i2c_clear_blk_v2(blk);
827 + qup_i2c_conf_count_v2(qup);
829 + /* If it is first sub transfer, then configure i2c bus clocks */
831 + ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
835 + writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
837 + ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
842 + reinit_completion(&qup->xfer);
843 + enable_irq(qup->irq);
845 + * In FIFO mode, tx FIFO can be written directly while in block mode the
846 + * it will be written after getting OUT_BLOCK_WRITE_REQ interrupt
848 + if (!blk->is_tx_blk_mode) {
849 + blk->tx_fifo_free = qup->out_fifo_sz;
852 + qup_i2c_write_rx_tags_v2(qup);
854 + qup_i2c_write_tx_fifo_v2(qup);
857 + ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
861 + ret = qup_i2c_wait_for_complete(qup, msg);
865 + /* Move to pause state for all the transfers, except last one */
866 + if (change_pause_state) {
867 + ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
873 + disable_irq(qup->irq);
878 + * Transfer one read/write message in i2c transfer. It splits the message into
879 + * multiple of blk_xfer_limit data length blocks and schedule each
880 + * QUP block individually.
882 +static int qup_i2c_xfer_v2_msg(struct qup_i2c_dev *qup, int msg_id, bool is_rx)
885 + unsigned int data_len, i;
886 + struct i2c_msg *msg = qup->msg;
887 + struct qup_i2c_block *blk = &qup->blk;
888 + u8 *msg_buf = msg->buf;
890 + qup->blk_xfer_limit = is_rx ? RECV_MAX_DATA_LEN : QUP_READ_LIMIT;
891 + qup_i2c_set_blk_data(qup, msg);
893 + for (i = 0; i < blk->count; i++) {
894 + data_len = qup_i2c_get_data_len(qup);
896 + blk->cur_tx_tags = blk->tags;
897 + blk->cur_blk_len = data_len;
899 + qup_i2c_set_tags(blk->cur_tx_tags, qup, qup->msg);
901 + blk->cur_data = msg_buf;
904 + blk->total_tx_len = blk->tx_tag_len;
905 + blk->rx_tag_len = 2;
906 + blk->total_rx_len = blk->rx_tag_len + data_len;
908 + blk->total_tx_len = blk->tx_tag_len + data_len;
909 + blk->total_rx_len = 0;
912 + ret = qup_i2c_conf_xfer_v2(qup, is_rx, !msg_id && !i,
913 + !qup->is_last || i < blk->count - 1);
917 + /* Handle SMBus block read length */
918 + if (qup_i2c_check_msg_len(msg) && msg->len == 1 &&
919 + !qup->is_smbus_read) {
920 + if (msg->buf[0] > I2C_SMBUS_BLOCK_MAX)
923 + msg->len = msg->buf[0];
924 + qup->is_smbus_read = true;
925 + ret = qup_i2c_xfer_v2_msg(qup, msg_id, true);
926 + qup->is_smbus_read = false;
933 + msg_buf += data_len;
934 + blk->data_len -= qup->blk_xfer_limit;
941 + * QUP v2 supports 3 modes
942 + * Programmed IO using FIFO mode : Less than FIFO size
943 + * Programmed IO using Block mode : Greater than FIFO size
944 + * DMA using BAM : Appropriate for any transaction size but the address should
945 + * be DMA applicable
947 + * This function determines the mode which will be used for this transfer. An
948 + * i2c transfer contains multiple message. Following are the rules to determine
950 + * 1. Determine complete length, maximum tx and rx length for complete transfer.
951 + * 2. If complete transfer length is greater than fifo size then use the DMA
953 + * 3. In FIFO or block mode, tx and rx can operate in different mode so check
954 + * for maximum tx and rx length to determine mode.
957 +qup_i2c_determine_mode_v2(struct qup_i2c_dev *qup,
958 + struct i2c_msg msgs[], int num)
961 + bool no_dma = false;
962 + unsigned int max_tx_len = 0, max_rx_len = 0, total_len = 0;
964 + /* All i2c_msgs should be transferred using either dma or cpu */
965 + for (idx = 0; idx < num; idx++) {
966 + if (msgs[idx].len == 0)
969 + if (msgs[idx].flags & I2C_M_RD)
970 + max_rx_len = max_t(unsigned int, max_rx_len,
973 + max_tx_len = max_t(unsigned int, max_tx_len,
976 + if (is_vmalloc_addr(msgs[idx].buf))
979 + total_len += msgs[idx].len;
982 + if (!no_dma && qup->is_dma &&
983 + (total_len > qup->out_fifo_sz || total_len > qup->in_fifo_sz)) {
984 + qup->use_dma = true;
986 + qup->blk.is_tx_blk_mode = max_tx_len > qup->out_fifo_sz -
987 + QUP_MAX_TAGS_LEN ? true : false;
988 + qup->blk.is_rx_blk_mode = max_rx_len > qup->in_fifo_sz -
989 + READ_RX_TAGS_LEN ? true : false;
995 static int qup_i2c_xfer_v2(struct i2c_adapter *adap,
996 struct i2c_msg msgs[],
999 struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
1001 - unsigned int total_len = 0;
1005 @@ -1419,6 +1554,10 @@ static int qup_i2c_xfer_v2(struct i2c_ad
1009 + ret = qup_i2c_determine_mode_v2(qup, msgs, num);
1013 writel(1, qup->base + QUP_SW_RESET);
1014 ret = qup_i2c_poll_state(qup, QUP_RESET_STATE);
1016 @@ -1428,60 +1567,35 @@ static int qup_i2c_xfer_v2(struct i2c_ad
1017 writel(I2C_MINI_CORE | I2C_N_VAL_V2, qup->base + QUP_CONFIG);
1018 writel(QUP_V2_TAGS_EN, qup->base + QUP_I2C_MASTER_GEN);
1020 - if ((qup->is_dma)) {
1021 - /* All i2c_msgs should be transferred using either dma or cpu */
1022 - for (idx = 0; idx < num; idx++) {
1023 - if (msgs[idx].len == 0) {
1028 - if (is_vmalloc_addr(msgs[idx].buf))
1031 - total_len += msgs[idx].len;
1034 - if (idx == num && (total_len > qup->out_fifo_sz ||
1035 - total_len > qup->in_fifo_sz))
1036 - qup->use_dma = true;
1037 + if (qup_i2c_poll_state_i2c_master(qup)) {
1043 + if (qup->use_dma) {
1044 + reinit_completion(&qup->xfer);
1045 + ret = qup_i2c_bam_xfer(adap, &msgs[0], num);
1046 + qup->use_dma = false;
1048 + qup_i2c_conf_mode_v2(qup);
1051 - if (msgs[idx].len == 0) {
1055 + for (idx = 0; idx < num; idx++) {
1056 + qup->msg = &msgs[idx];
1057 + qup->is_last = idx == (num - 1);
1059 - if (qup_i2c_poll_state_i2c_master(qup)) {
1062 + ret = qup_i2c_xfer_v2_msg(qup, idx,
1063 + !!(msgs[idx].flags & I2C_M_RD));
1070 - qup->is_last = (idx == (num - 1));
1072 - qup->config_run = QUP_I2C_MX_CONFIG_DURING_RUN;
1074 - qup->config_run = 0;
1076 - reinit_completion(&qup->xfer);
1078 - if (qup->use_dma) {
1079 - ret = qup_i2c_bam_xfer(adap, &msgs[idx], num);
1080 - qup->use_dma = false;
1083 - if (msgs[idx].flags & I2C_M_RD)
1084 - ret = qup_i2c_read_one_v2(qup, &msgs[idx]);
1086 - ret = qup_i2c_write_one_v2(qup, &msgs[idx]);
1088 - } while ((idx++ < (num - 1)) && !ret);
1090 + ret = qup_i2c_bus_active(qup, ONE_BYTE);
1093 - ret = qup_i2c_change_state(qup, QUP_RESET_STATE);
1094 + qup_i2c_change_state(qup, QUP_RESET_STATE);
1098 @@ -1545,6 +1659,7 @@ static int qup_i2c_probe(struct platform
1099 u32 src_clk_freq = DEFAULT_SRC_CLK;
1100 u32 clk_freq = DEFAULT_CLK_FREQ;
1104 qup = devm_kzalloc(&pdev->dev, sizeof(*qup), GFP_KERNEL);
1106 @@ -1563,12 +1678,10 @@ static int qup_i2c_probe(struct platform
1107 if (of_device_is_compatible(pdev->dev.of_node, "qcom,i2c-qup-v1.1.1")) {
1108 qup->adap.algo = &qup_i2c_algo;
1109 qup->adap.quirks = &qup_i2c_quirks;
1110 - qup->is_qup_v1 = true;
1111 - qup->write_tx_fifo = qup_i2c_write_tx_fifo_v1;
1112 - qup->read_rx_fifo = qup_i2c_read_rx_fifo_v1;
1113 - qup->write_rx_tags = qup_i2c_write_rx_tags_v1;
1116 qup->adap.algo = &qup_i2c_algo_v2;
1117 + is_qup_v1 = false;
1118 ret = qup_i2c_req_dma(qup);
1120 if (ret == -EPROBE_DEFER)
1121 @@ -1694,14 +1807,31 @@ nodma:
1125 - qup->out_blk_sz = blk_sizes[size] / 2;
1126 + qup->out_blk_sz = blk_sizes[size];
1128 size = QUP_INPUT_BLOCK_SIZE(io_mode);
1129 if (size >= ARRAY_SIZE(blk_sizes)) {
1133 - qup->in_blk_sz = blk_sizes[size] / 2;
1134 + qup->in_blk_sz = blk_sizes[size];
1138 + * in QUP v1, QUP_CONFIG uses N as 15 i.e 16 bits constitutes a
1139 + * single transfer but the block size is in bytes so divide the
1140 + * in_blk_sz and out_blk_sz by 2
1142 + qup->in_blk_sz /= 2;
1143 + qup->out_blk_sz /= 2;
1144 + qup->write_tx_fifo = qup_i2c_write_tx_fifo_v1;
1145 + qup->read_rx_fifo = qup_i2c_read_rx_fifo_v1;
1146 + qup->write_rx_tags = qup_i2c_write_rx_tags_v1;
1148 + qup->write_tx_fifo = qup_i2c_write_tx_fifo_v2;
1149 + qup->read_rx_fifo = qup_i2c_read_rx_fifo_v2;
1150 + qup->write_rx_tags = qup_i2c_write_rx_tags_v2;
1153 size = QUP_OUTPUT_FIFO_SIZE(io_mode);
1154 qup->out_fifo_sz = qup->out_blk_sz * (2 << size);