aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/i2c/busses/i2c-qup.c900
1 files changed, 515 insertions, 385 deletions
diff --git a/drivers/i2c/busses/i2c-qup.c b/drivers/i2c/busses/i2c-qup.c
index 3bf3c349dc70..904dfec7ab96 100644
--- a/drivers/i2c/busses/i2c-qup.c
+++ b/drivers/i2c/busses/i2c-qup.c
@@ -141,17 +141,40 @@
141#define DEFAULT_SRC_CLK 20000000 141#define DEFAULT_SRC_CLK 20000000
142 142
143/* 143/*
144 * Max tags length (start, stop and maximum 2 bytes address) for each QUP
145 * data transfer
146 */
147#define QUP_MAX_TAGS_LEN 4
148/* Max data length for each DATARD tags */
149#define RECV_MAX_DATA_LEN 254
150/* TAG length for DATA READ in RX FIFO */
151#define READ_RX_TAGS_LEN 2
152
153/*
144 * count: no of blocks 154 * count: no of blocks
145 * pos: current block number 155 * pos: current block number
146 * tx_tag_len: tx tag length for current block 156 * tx_tag_len: tx tag length for current block
147 * rx_tag_len: rx tag length for current block 157 * rx_tag_len: rx tag length for current block
148 * data_len: remaining data length for current message 158 * data_len: remaining data length for current message
159 * cur_blk_len: data length for current block
149 * total_tx_len: total tx length including tag bytes for current QUP transfer 160 * total_tx_len: total tx length including tag bytes for current QUP transfer
150 * total_rx_len: total rx length including tag bytes for current QUP transfer 161 * total_rx_len: total rx length including tag bytes for current QUP transfer
162 * tx_fifo_data_pos: current byte number in TX FIFO word
151 * tx_fifo_free: number of free bytes in current QUP block write. 163 * tx_fifo_free: number of free bytes in current QUP block write.
164 * rx_fifo_data_pos: current byte number in RX FIFO word
152 * fifo_available: number of available bytes in RX FIFO for current 165 * fifo_available: number of available bytes in RX FIFO for current
153 * QUP block read 166 * QUP block read
167 * tx_fifo_data: QUP TX FIFO write works on word basis (4 bytes). New byte write
168 * to TX FIFO will be appended in this data and will be written to
169 * TX FIFO when all the 4 bytes are available.
170 * rx_fifo_data: QUP RX FIFO read works on word basis (4 bytes). This will
171 * contains the 4 bytes of RX data.
172 * cur_data: pointer to tell cur data position for current message
173 * cur_tx_tags: pointer to tell cur position in tags
174 * tx_tags_sent: all tx tag bytes have been written in FIFO word
175 * send_last_word: for tx FIFO, last word send is pending in current block
154 * rx_bytes_read: if all the bytes have been read from rx FIFO. 176 * rx_bytes_read: if all the bytes have been read from rx FIFO.
177 * rx_tags_fetched: all the rx tag bytes have been fetched from rx fifo word
155 * is_tx_blk_mode: whether tx uses block or FIFO mode in case of non BAM xfer. 178 * is_tx_blk_mode: whether tx uses block or FIFO mode in case of non BAM xfer.
156 * is_rx_blk_mode: whether rx uses block or FIFO mode in case of non BAM xfer. 179 * is_rx_blk_mode: whether rx uses block or FIFO mode in case of non BAM xfer.
157 * tags: contains tx tag bytes for current QUP transfer 180 * tags: contains tx tag bytes for current QUP transfer
@@ -162,10 +185,20 @@ struct qup_i2c_block {
162 int tx_tag_len; 185 int tx_tag_len;
163 int rx_tag_len; 186 int rx_tag_len;
164 int data_len; 187 int data_len;
188 int cur_blk_len;
165 int total_tx_len; 189 int total_tx_len;
166 int total_rx_len; 190 int total_rx_len;
191 int tx_fifo_data_pos;
167 int tx_fifo_free; 192 int tx_fifo_free;
193 int rx_fifo_data_pos;
168 int fifo_available; 194 int fifo_available;
195 u32 tx_fifo_data;
196 u32 rx_fifo_data;
197 u8 *cur_data;
198 u8 *cur_tx_tags;
199 bool tx_tags_sent;
200 bool send_last_word;
201 bool rx_tags_fetched;
169 bool rx_bytes_read; 202 bool rx_bytes_read;
170 bool is_tx_blk_mode; 203 bool is_tx_blk_mode;
171 bool is_rx_blk_mode; 204 bool is_rx_blk_mode;
@@ -198,6 +231,7 @@ struct qup_i2c_dev {
198 int out_blk_sz; 231 int out_blk_sz;
199 int in_blk_sz; 232 int in_blk_sz;
200 233
234 int blk_xfer_limit;
201 unsigned long one_byte_t; 235 unsigned long one_byte_t;
202 unsigned long xfer_timeout; 236 unsigned long xfer_timeout;
203 struct qup_i2c_block blk; 237 struct qup_i2c_block blk;
@@ -212,10 +246,10 @@ struct qup_i2c_dev {
212 246
213 /* To check if this is the last msg */ 247 /* To check if this is the last msg */
214 bool is_last; 248 bool is_last;
215 bool is_qup_v1; 249 bool is_smbus_read;
216 250
217 /* To configure when bus is in run state */ 251 /* To configure when bus is in run state */
218 int config_run; 252 u32 config_run;
219 253
220 /* dma parameters */ 254 /* dma parameters */
221 bool is_dma; 255 bool is_dma;
@@ -223,6 +257,8 @@ struct qup_i2c_dev {
223 bool use_dma; 257 bool use_dma;
224 unsigned int max_xfer_sg_len; 258 unsigned int max_xfer_sg_len;
225 unsigned int tag_buf_pos; 259 unsigned int tag_buf_pos;
260 /* The threshold length above which block mode will be used */
261 unsigned int blk_mode_threshold;
226 struct dma_pool *dpool; 262 struct dma_pool *dpool;
227 struct qup_i2c_tag start_tag; 263 struct qup_i2c_tag start_tag;
228 struct qup_i2c_bam brx; 264 struct qup_i2c_bam brx;
@@ -287,9 +323,6 @@ static irqreturn_t qup_i2c_interrupt(int irq, void *dev)
287 goto done; 323 goto done;
288 } 324 }
289 325
290 if (!qup->is_qup_v1)
291 goto done;
292
293 if (opflags & QUP_OUT_SVC_FLAG) { 326 if (opflags & QUP_OUT_SVC_FLAG) {
294 writel(QUP_OUT_SVC_FLAG, qup->base + QUP_OPERATIONAL); 327 writel(QUP_OUT_SVC_FLAG, qup->base + QUP_OPERATIONAL);
295 328
@@ -416,108 +449,6 @@ static int qup_i2c_bus_active(struct qup_i2c_dev *qup, int len)
416 return ret; 449 return ret;
417} 450}
418 451
419/**
420 * qup_i2c_wait_ready - wait for a give number of bytes in tx/rx path
421 * @qup: The qup_i2c_dev device
422 * @op: The bit/event to wait on
423 * @val: value of the bit to wait on, 0 or 1
424 * @len: The length the bytes to be transferred
425 */
426static int qup_i2c_wait_ready(struct qup_i2c_dev *qup, int op, bool val,
427 int len)
428{
429 unsigned long timeout;
430 u32 opflags;
431 u32 status;
432 u32 shift = __ffs(op);
433 int ret = 0;
434
435 len *= qup->one_byte_t;
436 /* timeout after a wait of twice the max time */
437 timeout = jiffies + len * 4;
438
439 for (;;) {
440 opflags = readl(qup->base + QUP_OPERATIONAL);
441 status = readl(qup->base + QUP_I2C_STATUS);
442
443 if (((opflags & op) >> shift) == val) {
444 if ((op == QUP_OUT_NOT_EMPTY) && qup->is_last) {
445 if (!(status & I2C_STATUS_BUS_ACTIVE)) {
446 ret = 0;
447 goto done;
448 }
449 } else {
450 ret = 0;
451 goto done;
452 }
453 }
454
455 if (time_after(jiffies, timeout)) {
456 ret = -ETIMEDOUT;
457 goto done;
458 }
459 usleep_range(len, len * 2);
460 }
461
462done:
463 if (qup->bus_err || qup->qup_err)
464 ret = (qup->bus_err & QUP_I2C_NACK_FLAG) ? -ENXIO : -EIO;
465
466 return ret;
467}
468
469static void qup_i2c_set_write_mode_v2(struct qup_i2c_dev *qup,
470 struct i2c_msg *msg)
471{
472 /* Number of entries to shift out, including the tags */
473 int total = msg->len + qup->blk.tx_tag_len;
474
475 total |= qup->config_run;
476
477 if (total < qup->out_fifo_sz) {
478 /* FIFO mode */
479 writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
480 writel(total, qup->base + QUP_MX_WRITE_CNT);
481 } else {
482 /* BLOCK mode (transfer data on chunks) */
483 writel(QUP_OUTPUT_BLK_MODE | QUP_REPACK_EN,
484 qup->base + QUP_IO_MODE);
485 writel(total, qup->base + QUP_MX_OUTPUT_CNT);
486 }
487}
488
489static int check_for_fifo_space(struct qup_i2c_dev *qup)
490{
491 int ret;
492
493 ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
494 if (ret)
495 goto out;
496
497 ret = qup_i2c_wait_ready(qup, QUP_OUT_FULL,
498 RESET_BIT, 4 * ONE_BYTE);
499 if (ret) {
500 /* Fifo is full. Drain out the fifo */
501 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
502 if (ret)
503 goto out;
504
505 ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY,
506 RESET_BIT, 256 * ONE_BYTE);
507 if (ret) {
508 dev_err(qup->dev, "timeout for fifo out full");
509 goto out;
510 }
511
512 ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
513 if (ret)
514 goto out;
515 }
516
517out:
518 return ret;
519}
520
521static void qup_i2c_write_tx_fifo_v1(struct qup_i2c_dev *qup) 452static void qup_i2c_write_tx_fifo_v1(struct qup_i2c_dev *qup)
522{ 453{
523 struct qup_i2c_block *blk = &qup->blk; 454 struct qup_i2c_block *blk = &qup->blk;
@@ -560,60 +491,17 @@ static void qup_i2c_write_tx_fifo_v1(struct qup_i2c_dev *qup)
560static void qup_i2c_set_blk_data(struct qup_i2c_dev *qup, 491static void qup_i2c_set_blk_data(struct qup_i2c_dev *qup,
561 struct i2c_msg *msg) 492 struct i2c_msg *msg)
562{ 493{
563 memset(&qup->blk, 0, sizeof(qup->blk)); 494 qup->blk.pos = 0;
564
565 qup->blk.data_len = msg->len; 495 qup->blk.data_len = msg->len;
566 qup->blk.count = (msg->len + QUP_READ_LIMIT - 1) / QUP_READ_LIMIT; 496 qup->blk.count = DIV_ROUND_UP(msg->len, qup->blk_xfer_limit);
567
568 /* 4 bytes for first block and 2 writes for rest */
569 qup->blk.tx_tag_len = 4 + (qup->blk.count - 1) * 2;
570
571 /* There are 2 tag bytes that are read in to fifo for every block */
572 if (msg->flags & I2C_M_RD)
573 qup->blk.rx_tag_len = qup->blk.count * 2;
574}
575
576static int qup_i2c_send_data(struct qup_i2c_dev *qup, int tlen, u8 *tbuf,
577 int dlen, u8 *dbuf)
578{
579 u32 val = 0, idx = 0, pos = 0, i = 0, t;
580 int len = tlen + dlen;
581 u8 *buf = tbuf;
582 int ret = 0;
583
584 while (len > 0) {
585 ret = check_for_fifo_space(qup);
586 if (ret)
587 return ret;
588
589 t = (len >= 4) ? 4 : len;
590
591 while (idx < t) {
592 if (!i && (pos >= tlen)) {
593 buf = dbuf;
594 pos = 0;
595 i = 1;
596 }
597 val |= buf[pos++] << (idx++ * 8);
598 }
599
600 writel(val, qup->base + QUP_OUT_FIFO_BASE);
601 idx = 0;
602 val = 0;
603 len -= 4;
604 }
605
606 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
607
608 return ret;
609} 497}
610 498
611static int qup_i2c_get_data_len(struct qup_i2c_dev *qup) 499static int qup_i2c_get_data_len(struct qup_i2c_dev *qup)
612{ 500{
613 int data_len; 501 int data_len;
614 502
615 if (qup->blk.data_len > QUP_READ_LIMIT) 503 if (qup->blk.data_len > qup->blk_xfer_limit)
616 data_len = QUP_READ_LIMIT; 504 data_len = qup->blk_xfer_limit;
617 else 505 else
618 data_len = qup->blk.data_len; 506 data_len = qup->blk.data_len;
619 507
@@ -630,9 +518,9 @@ static int qup_i2c_set_tags_smb(u16 addr, u8 *tags, struct qup_i2c_dev *qup,
630{ 518{
631 int len = 0; 519 int len = 0;
632 520
633 if (msg->len > 1) { 521 if (qup->is_smbus_read) {
634 tags[len++] = QUP_TAG_V2_DATARD_STOP; 522 tags[len++] = QUP_TAG_V2_DATARD_STOP;
635 tags[len++] = qup_i2c_get_data_len(qup) - 1; 523 tags[len++] = qup_i2c_get_data_len(qup);
636 } else { 524 } else {
637 tags[len++] = QUP_TAG_V2_START; 525 tags[len++] = QUP_TAG_V2_START;
638 tags[len++] = addr & 0xff; 526 tags[len++] = addr & 0xff;
@@ -694,24 +582,6 @@ static int qup_i2c_set_tags(u8 *tags, struct qup_i2c_dev *qup,
694 return len; 582 return len;
695} 583}
696 584
697static int qup_i2c_issue_xfer_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
698{
699 int data_len = 0, tag_len, index;
700 int ret;
701
702 tag_len = qup_i2c_set_tags(qup->blk.tags, qup, msg);
703 index = msg->len - qup->blk.data_len;
704
705 /* only tags are written for read */
706 if (!(msg->flags & I2C_M_RD))
707 data_len = qup_i2c_get_data_len(qup);
708
709 ret = qup_i2c_send_data(qup, tag_len, qup->blk.tags,
710 data_len, &msg->buf[index]);
711 qup->blk.data_len -= data_len;
712
713 return ret;
714}
715 585
716static void qup_i2c_bam_cb(void *data) 586static void qup_i2c_bam_cb(void *data)
717{ 587{
@@ -778,6 +648,7 @@ static int qup_i2c_bam_make_desc(struct qup_i2c_dev *qup, struct i2c_msg *msg)
778 u32 i = 0, tlen, tx_len = 0; 648 u32 i = 0, tlen, tx_len = 0;
779 u8 *tags; 649 u8 *tags;
780 650
651 qup->blk_xfer_limit = QUP_READ_LIMIT;
781 qup_i2c_set_blk_data(qup, msg); 652 qup_i2c_set_blk_data(qup, msg);
782 653
783 blocks = qup->blk.count; 654 blocks = qup->blk.count;
@@ -1026,7 +897,7 @@ static int qup_i2c_wait_for_complete(struct qup_i2c_dev *qup,
1026 unsigned long left; 897 unsigned long left;
1027 int ret = 0; 898 int ret = 0;
1028 899
1029 left = wait_for_completion_timeout(&qup->xfer, HZ); 900 left = wait_for_completion_timeout(&qup->xfer, qup->xfer_timeout);
1030 if (!left) { 901 if (!left) {
1031 writel(1, qup->base + QUP_SW_RESET); 902 writel(1, qup->base + QUP_SW_RESET);
1032 ret = -ETIMEDOUT; 903 ret = -ETIMEDOUT;
@@ -1038,65 +909,6 @@ static int qup_i2c_wait_for_complete(struct qup_i2c_dev *qup,
1038 return ret; 909 return ret;
1039} 910}
1040 911
1041static int qup_i2c_write_one_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
1042{
1043 int ret = 0;
1044
1045 qup->msg = msg;
1046 qup->pos = 0;
1047 enable_irq(qup->irq);
1048 qup_i2c_set_blk_data(qup, msg);
1049 qup_i2c_set_write_mode_v2(qup, msg);
1050
1051 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
1052 if (ret)
1053 goto err;
1054
1055 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
1056
1057 do {
1058 ret = qup_i2c_issue_xfer_v2(qup, msg);
1059 if (ret)
1060 goto err;
1061
1062 ret = qup_i2c_wait_for_complete(qup, msg);
1063 if (ret)
1064 goto err;
1065
1066 qup->blk.pos++;
1067 } while (qup->blk.pos < qup->blk.count);
1068
1069 ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY, RESET_BIT, ONE_BYTE);
1070
1071err:
1072 disable_irq(qup->irq);
1073 qup->msg = NULL;
1074
1075 return ret;
1076}
1077
1078static void qup_i2c_set_read_mode_v2(struct qup_i2c_dev *qup, int len)
1079{
1080 int tx_len = qup->blk.tx_tag_len;
1081
1082 len += qup->blk.rx_tag_len;
1083 len |= qup->config_run;
1084 tx_len |= qup->config_run;
1085
1086 if (len < qup->in_fifo_sz) {
1087 /* FIFO mode */
1088 writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
1089 writel(tx_len, qup->base + QUP_MX_WRITE_CNT);
1090 writel(len, qup->base + QUP_MX_READ_CNT);
1091 } else {
1092 /* BLOCK mode (transfer data on chunks) */
1093 writel(QUP_INPUT_BLK_MODE | QUP_REPACK_EN,
1094 qup->base + QUP_IO_MODE);
1095 writel(tx_len, qup->base + QUP_MX_OUTPUT_CNT);
1096 writel(len, qup->base + QUP_MX_INPUT_CNT);
1097 }
1098}
1099
1100static void qup_i2c_read_rx_fifo_v1(struct qup_i2c_dev *qup) 912static void qup_i2c_read_rx_fifo_v1(struct qup_i2c_dev *qup)
1101{ 913{
1102 struct qup_i2c_block *blk = &qup->blk; 914 struct qup_i2c_block *blk = &qup->blk;
@@ -1120,104 +932,6 @@ static void qup_i2c_read_rx_fifo_v1(struct qup_i2c_dev *qup)
1120 blk->rx_bytes_read = true; 932 blk->rx_bytes_read = true;
1121} 933}
1122 934
1123static int qup_i2c_read_fifo_v2(struct qup_i2c_dev *qup,
1124 struct i2c_msg *msg)
1125{
1126 u32 val;
1127 int idx, pos = 0, ret = 0, total, msg_offset = 0;
1128
1129 /*
1130 * If the message length is already read in
1131 * the first byte of the buffer, account for
1132 * that by setting the offset
1133 */
1134 if (qup_i2c_check_msg_len(msg) && (msg->len > 1))
1135 msg_offset = 1;
1136 total = qup_i2c_get_data_len(qup);
1137 total -= msg_offset;
1138
1139 /* 2 extra bytes for read tags */
1140 while (pos < (total + 2)) {
1141 /* Check that FIFO have data */
1142 ret = qup_i2c_wait_ready(qup, QUP_IN_NOT_EMPTY,
1143 SET_BIT, 4 * ONE_BYTE);
1144 if (ret) {
1145 dev_err(qup->dev, "timeout for fifo not empty");
1146 return ret;
1147 }
1148 val = readl(qup->base + QUP_IN_FIFO_BASE);
1149
1150 for (idx = 0; idx < 4; idx++, val >>= 8, pos++) {
1151 /* first 2 bytes are tag bytes */
1152 if (pos < 2)
1153 continue;
1154
1155 if (pos >= (total + 2))
1156 goto out;
1157 msg->buf[qup->pos + msg_offset] = val & 0xff;
1158 qup->pos++;
1159 }
1160 }
1161
1162out:
1163 qup->blk.data_len -= total;
1164
1165 return ret;
1166}
1167
1168static int qup_i2c_read_one_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
1169{
1170 int ret = 0;
1171
1172 qup->msg = msg;
1173 qup->pos = 0;
1174 enable_irq(qup->irq);
1175 qup_i2c_set_blk_data(qup, msg);
1176 qup_i2c_set_read_mode_v2(qup, msg->len);
1177
1178 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
1179 if (ret)
1180 goto err;
1181
1182 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
1183
1184 do {
1185 ret = qup_i2c_issue_xfer_v2(qup, msg);
1186 if (ret)
1187 goto err;
1188
1189 ret = qup_i2c_wait_for_complete(qup, msg);
1190 if (ret)
1191 goto err;
1192
1193 ret = qup_i2c_read_fifo_v2(qup, msg);
1194 if (ret)
1195 goto err;
1196
1197 qup->blk.pos++;
1198
1199 /* Handle SMBus block read length */
1200 if (qup_i2c_check_msg_len(msg) && (msg->len == 1)) {
1201 if (msg->buf[0] > I2C_SMBUS_BLOCK_MAX) {
1202 ret = -EPROTO;
1203 goto err;
1204 }
1205 msg->len += msg->buf[0];
1206 qup->pos = 0;
1207 qup_i2c_set_blk_data(qup, msg);
1208 /* set tag length for block read */
1209 qup->blk.tx_tag_len = 2;
1210 qup_i2c_set_read_mode_v2(qup, msg->buf[0]);
1211 }
1212 } while (qup->blk.pos < qup->blk.count);
1213
1214err:
1215 disable_irq(qup->irq);
1216 qup->msg = NULL;
1217
1218 return ret;
1219}
1220
1221static void qup_i2c_write_rx_tags_v1(struct qup_i2c_dev *qup) 935static void qup_i2c_write_rx_tags_v1(struct qup_i2c_dev *qup)
1222{ 936{
1223 struct i2c_msg *msg = qup->msg; 937 struct i2c_msg *msg = qup->msg;
@@ -1404,13 +1118,434 @@ out:
1404 return ret; 1118 return ret;
1405} 1119}
1406 1120
1121/*
1122 * Configure registers related with reconfiguration during run and call it
1123 * before each i2c sub transfer.
1124 */
1125static void qup_i2c_conf_count_v2(struct qup_i2c_dev *qup)
1126{
1127 struct qup_i2c_block *blk = &qup->blk;
1128 u32 qup_config = I2C_MINI_CORE | I2C_N_VAL_V2;
1129
1130 if (blk->is_tx_blk_mode)
1131 writel(qup->config_run | blk->total_tx_len,
1132 qup->base + QUP_MX_OUTPUT_CNT);
1133 else
1134 writel(qup->config_run | blk->total_tx_len,
1135 qup->base + QUP_MX_WRITE_CNT);
1136
1137 if (blk->total_rx_len) {
1138 if (blk->is_rx_blk_mode)
1139 writel(qup->config_run | blk->total_rx_len,
1140 qup->base + QUP_MX_INPUT_CNT);
1141 else
1142 writel(qup->config_run | blk->total_rx_len,
1143 qup->base + QUP_MX_READ_CNT);
1144 } else {
1145 qup_config |= QUP_NO_INPUT;
1146 }
1147
1148 writel(qup_config, qup->base + QUP_CONFIG);
1149}
1150
1151/*
1152 * Configure registers related with transfer mode (FIFO/Block)
1153 * before starting of i2c transfer. It will be called only once in
1154 * QUP RESET state.
1155 */
1156static void qup_i2c_conf_mode_v2(struct qup_i2c_dev *qup)
1157{
1158 struct qup_i2c_block *blk = &qup->blk;
1159 u32 io_mode = QUP_REPACK_EN;
1160
1161 if (blk->is_tx_blk_mode) {
1162 io_mode |= QUP_OUTPUT_BLK_MODE;
1163 writel(0, qup->base + QUP_MX_WRITE_CNT);
1164 } else {
1165 writel(0, qup->base + QUP_MX_OUTPUT_CNT);
1166 }
1167
1168 if (blk->is_rx_blk_mode) {
1169 io_mode |= QUP_INPUT_BLK_MODE;
1170 writel(0, qup->base + QUP_MX_READ_CNT);
1171 } else {
1172 writel(0, qup->base + QUP_MX_INPUT_CNT);
1173 }
1174
1175 writel(io_mode, qup->base + QUP_IO_MODE);
1176}
1177
1178/* Clear required variables before starting of any QUP v2 sub transfer. */
1179static void qup_i2c_clear_blk_v2(struct qup_i2c_block *blk)
1180{
1181 blk->send_last_word = false;
1182 blk->tx_tags_sent = false;
1183 blk->tx_fifo_data = 0;
1184 blk->tx_fifo_data_pos = 0;
1185 blk->tx_fifo_free = 0;
1186
1187 blk->rx_tags_fetched = false;
1188 blk->rx_bytes_read = false;
1189 blk->rx_fifo_data = 0;
1190 blk->rx_fifo_data_pos = 0;
1191 blk->fifo_available = 0;
1192}
1193
1194/* Receive data from RX FIFO for read message in QUP v2 i2c transfer. */
1195static void qup_i2c_recv_data(struct qup_i2c_dev *qup)
1196{
1197 struct qup_i2c_block *blk = &qup->blk;
1198 int j;
1199
1200 for (j = blk->rx_fifo_data_pos;
1201 blk->cur_blk_len && blk->fifo_available;
1202 blk->cur_blk_len--, blk->fifo_available--) {
1203 if (j == 0)
1204 blk->rx_fifo_data = readl(qup->base + QUP_IN_FIFO_BASE);
1205
1206 *(blk->cur_data++) = blk->rx_fifo_data;
1207 blk->rx_fifo_data >>= 8;
1208
1209 if (j == 3)
1210 j = 0;
1211 else
1212 j++;
1213 }
1214
1215 blk->rx_fifo_data_pos = j;
1216}
1217
1218/* Receive tags for read message in QUP v2 i2c transfer. */
1219static void qup_i2c_recv_tags(struct qup_i2c_dev *qup)
1220{
1221 struct qup_i2c_block *blk = &qup->blk;
1222
1223 blk->rx_fifo_data = readl(qup->base + QUP_IN_FIFO_BASE);
1224 blk->rx_fifo_data >>= blk->rx_tag_len * 8;
1225 blk->rx_fifo_data_pos = blk->rx_tag_len;
1226 blk->fifo_available -= blk->rx_tag_len;
1227}
1228
1229/*
1230 * Read the data and tags from RX FIFO. Since in read case, the tags will be
1231 * preceded by received data bytes so
1232 * 1. Check if rx_tags_fetched is false i.e. the start of QUP block so receive
1233 * all tag bytes and discard that.
1234 * 2. Read the data from RX FIFO. When all the data bytes have been read then
1235 * set rx_bytes_read to true.
1236 */
1237static void qup_i2c_read_rx_fifo_v2(struct qup_i2c_dev *qup)
1238{
1239 struct qup_i2c_block *blk = &qup->blk;
1240
1241 if (!blk->rx_tags_fetched) {
1242 qup_i2c_recv_tags(qup);
1243 blk->rx_tags_fetched = true;
1244 }
1245
1246 qup_i2c_recv_data(qup);
1247 if (!blk->cur_blk_len)
1248 blk->rx_bytes_read = true;
1249}
1250
1251/*
1252 * Write bytes in TX FIFO for write message in QUP v2 i2c transfer. QUP TX FIFO
1253 * write works on word basis (4 bytes). Append new data byte write for TX FIFO
1254 * in tx_fifo_data and write to TX FIFO when all the 4 bytes are present.
1255 */
1256static void
1257qup_i2c_write_blk_data(struct qup_i2c_dev *qup, u8 **data, unsigned int *len)
1258{
1259 struct qup_i2c_block *blk = &qup->blk;
1260 unsigned int j;
1261
1262 for (j = blk->tx_fifo_data_pos; *len && blk->tx_fifo_free;
1263 (*len)--, blk->tx_fifo_free--) {
1264 blk->tx_fifo_data |= *(*data)++ << (j * 8);
1265 if (j == 3) {
1266 writel(blk->tx_fifo_data,
1267 qup->base + QUP_OUT_FIFO_BASE);
1268 blk->tx_fifo_data = 0x0;
1269 j = 0;
1270 } else {
1271 j++;
1272 }
1273 }
1274
1275 blk->tx_fifo_data_pos = j;
1276}
1277
1278/* Transfer tags for read message in QUP v2 i2c transfer. */
1279static void qup_i2c_write_rx_tags_v2(struct qup_i2c_dev *qup)
1280{
1281 struct qup_i2c_block *blk = &qup->blk;
1282
1283 qup_i2c_write_blk_data(qup, &blk->cur_tx_tags, &blk->tx_tag_len);
1284 if (blk->tx_fifo_data_pos)
1285 writel(blk->tx_fifo_data, qup->base + QUP_OUT_FIFO_BASE);
1286}
1287
1288/*
1289 * Write the data and tags in TX FIFO. Since in write case, both tags and data
1290 * need to be written and QUP write tags can have maximum 256 data length, so
1291 *
1292 * 1. Check if tx_tags_sent is false i.e. the start of QUP block so write the
1293 * tags to TX FIFO and set tx_tags_sent to true.
1294 * 2. Check if send_last_word is true. It will be set when last few data bytes
1295 * (less than 4 bytes) are reamining to be written in FIFO because of no FIFO
1296 * space. All this data bytes are available in tx_fifo_data so write this
1297 * in FIFO.
1298 * 3. Write the data to TX FIFO and check for cur_blk_len. If it is non zero
1299 * then more data is pending otherwise following 3 cases can be possible
1300 * a. if tx_fifo_data_pos is zero i.e. all the data bytes in this block
1301 * have been written in TX FIFO so nothing else is required.
1302 * b. tx_fifo_free is non zero i.e tx FIFO is free so copy the remaining data
1303 * from tx_fifo_data to tx FIFO. Since, qup_i2c_write_blk_data do write
1304 * in 4 bytes and FIFO space is in multiple of 4 bytes so tx_fifo_free
1305 * will be always greater than or equal to 4 bytes.
1306 * c. tx_fifo_free is zero. In this case, last few bytes (less than 4
1307 * bytes) are copied to tx_fifo_data but couldn't be sent because of
1308 * FIFO full so make send_last_word true.
1309 */
1310static void qup_i2c_write_tx_fifo_v2(struct qup_i2c_dev *qup)
1311{
1312 struct qup_i2c_block *blk = &qup->blk;
1313
1314 if (!blk->tx_tags_sent) {
1315 qup_i2c_write_blk_data(qup, &blk->cur_tx_tags,
1316 &blk->tx_tag_len);
1317 blk->tx_tags_sent = true;
1318 }
1319
1320 if (blk->send_last_word)
1321 goto send_last_word;
1322
1323 qup_i2c_write_blk_data(qup, &blk->cur_data, &blk->cur_blk_len);
1324 if (!blk->cur_blk_len) {
1325 if (!blk->tx_fifo_data_pos)
1326 return;
1327
1328 if (blk->tx_fifo_free)
1329 goto send_last_word;
1330
1331 blk->send_last_word = true;
1332 }
1333
1334 return;
1335
1336send_last_word:
1337 writel(blk->tx_fifo_data, qup->base + QUP_OUT_FIFO_BASE);
1338}
1339
1340/*
1341 * Main transfer function which read or write i2c data.
1342 * The QUP v2 supports reconfiguration during run in which multiple i2c sub
1343 * transfers can be scheduled.
1344 */
1345static int
1346qup_i2c_conf_xfer_v2(struct qup_i2c_dev *qup, bool is_rx, bool is_first,
1347 bool change_pause_state)
1348{
1349 struct qup_i2c_block *blk = &qup->blk;
1350 struct i2c_msg *msg = qup->msg;
1351 int ret;
1352
1353 /*
1354 * Check if its SMBus Block read for which the top level read will be
1355 * done into 2 QUP reads. One with message length 1 while other one is
1356 * with actual length.
1357 */
1358 if (qup_i2c_check_msg_len(msg)) {
1359 if (qup->is_smbus_read) {
1360 /*
1361 * If the message length is already read in
1362 * the first byte of the buffer, account for
1363 * that by setting the offset
1364 */
1365 blk->cur_data += 1;
1366 is_first = false;
1367 } else {
1368 change_pause_state = false;
1369 }
1370 }
1371
1372 qup->config_run = is_first ? 0 : QUP_I2C_MX_CONFIG_DURING_RUN;
1373
1374 qup_i2c_clear_blk_v2(blk);
1375 qup_i2c_conf_count_v2(qup);
1376
1377 /* If it is first sub transfer, then configure i2c bus clocks */
1378 if (is_first) {
1379 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
1380 if (ret)
1381 return ret;
1382
1383 writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
1384
1385 ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
1386 if (ret)
1387 return ret;
1388 }
1389
1390 reinit_completion(&qup->xfer);
1391 enable_irq(qup->irq);
1392 /*
1393 * In FIFO mode, tx FIFO can be written directly while in block mode the
1394 * it will be written after getting OUT_BLOCK_WRITE_REQ interrupt
1395 */
1396 if (!blk->is_tx_blk_mode) {
1397 blk->tx_fifo_free = qup->out_fifo_sz;
1398
1399 if (is_rx)
1400 qup_i2c_write_rx_tags_v2(qup);
1401 else
1402 qup_i2c_write_tx_fifo_v2(qup);
1403 }
1404
1405 ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
1406 if (ret)
1407 goto err;
1408
1409 ret = qup_i2c_wait_for_complete(qup, msg);
1410 if (ret)
1411 goto err;
1412
1413 /* Move to pause state for all the transfers, except last one */
1414 if (change_pause_state) {
1415 ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
1416 if (ret)
1417 goto err;
1418 }
1419
1420err:
1421 disable_irq(qup->irq);
1422 return ret;
1423}
1424
1425/*
1426 * Transfer one read/write message in i2c transfer. It splits the message into
1427 * multiple of blk_xfer_limit data length blocks and schedule each
1428 * QUP block individually.
1429 */
1430static int qup_i2c_xfer_v2_msg(struct qup_i2c_dev *qup, int msg_id, bool is_rx)
1431{
1432 int ret = 0;
1433 unsigned int data_len, i;
1434 struct i2c_msg *msg = qup->msg;
1435 struct qup_i2c_block *blk = &qup->blk;
1436 u8 *msg_buf = msg->buf;
1437
1438 qup->blk_xfer_limit = is_rx ? RECV_MAX_DATA_LEN : QUP_READ_LIMIT;
1439 qup_i2c_set_blk_data(qup, msg);
1440
1441 for (i = 0; i < blk->count; i++) {
1442 data_len = qup_i2c_get_data_len(qup);
1443 blk->pos = i;
1444 blk->cur_tx_tags = blk->tags;
1445 blk->cur_blk_len = data_len;
1446 blk->tx_tag_len =
1447 qup_i2c_set_tags(blk->cur_tx_tags, qup, qup->msg);
1448
1449 blk->cur_data = msg_buf;
1450
1451 if (is_rx) {
1452 blk->total_tx_len = blk->tx_tag_len;
1453 blk->rx_tag_len = 2;
1454 blk->total_rx_len = blk->rx_tag_len + data_len;
1455 } else {
1456 blk->total_tx_len = blk->tx_tag_len + data_len;
1457 blk->total_rx_len = 0;
1458 }
1459
1460 ret = qup_i2c_conf_xfer_v2(qup, is_rx, !msg_id && !i,
1461 !qup->is_last || i < blk->count - 1);
1462 if (ret)
1463 return ret;
1464
1465 /* Handle SMBus block read length */
1466 if (qup_i2c_check_msg_len(msg) && msg->len == 1 &&
1467 !qup->is_smbus_read) {
1468 if (msg->buf[0] > I2C_SMBUS_BLOCK_MAX)
1469 return -EPROTO;
1470
1471 msg->len = msg->buf[0];
1472 qup->is_smbus_read = true;
1473 ret = qup_i2c_xfer_v2_msg(qup, msg_id, true);
1474 qup->is_smbus_read = false;
1475 if (ret)
1476 return ret;
1477
1478 msg->len += 1;
1479 }
1480
1481 msg_buf += data_len;
1482 blk->data_len -= qup->blk_xfer_limit;
1483 }
1484
1485 return ret;
1486}
1487
1488/*
1489 * QUP v2 supports 3 modes
1490 * Programmed IO using FIFO mode : Less than FIFO size
1491 * Programmed IO using Block mode : Greater than FIFO size
1492 * DMA using BAM : Appropriate for any transaction size but the address should
1493 * be DMA applicable
1494 *
1495 * This function determines the mode which will be used for this transfer. An
1496 * i2c transfer contains multiple message. Following are the rules to determine
1497 * the mode used.
1498 * 1. Determine complete length, maximum tx and rx length for complete transfer.
1499 * 2. If complete transfer length is greater than fifo size then use the DMA
1500 * mode.
1501 * 3. In FIFO or block mode, tx and rx can operate in different mode so check
1502 * for maximum tx and rx length to determine mode.
1503 */
1504static int
1505qup_i2c_determine_mode_v2(struct qup_i2c_dev *qup,
1506 struct i2c_msg msgs[], int num)
1507{
1508 int idx;
1509 bool no_dma = false;
1510 unsigned int max_tx_len = 0, max_rx_len = 0, total_len = 0;
1511
1512 /* All i2c_msgs should be transferred using either dma or cpu */
1513 for (idx = 0; idx < num; idx++) {
1514 if (msgs[idx].len == 0)
1515 return -EINVAL;
1516
1517 if (msgs[idx].flags & I2C_M_RD)
1518 max_rx_len = max_t(unsigned int, max_rx_len,
1519 msgs[idx].len);
1520 else
1521 max_tx_len = max_t(unsigned int, max_tx_len,
1522 msgs[idx].len);
1523
1524 if (is_vmalloc_addr(msgs[idx].buf))
1525 no_dma = true;
1526
1527 total_len += msgs[idx].len;
1528 }
1529
1530 if (!no_dma && qup->is_dma &&
1531 (total_len > qup->out_fifo_sz || total_len > qup->in_fifo_sz)) {
1532 qup->use_dma = true;
1533 } else {
1534 qup->blk.is_tx_blk_mode = max_tx_len > qup->out_fifo_sz -
1535 QUP_MAX_TAGS_LEN ? true : false;
1536 qup->blk.is_rx_blk_mode = max_rx_len > qup->in_fifo_sz -
1537 READ_RX_TAGS_LEN ? true : false;
1538 }
1539
1540 return 0;
1541}
1542
1407static int qup_i2c_xfer_v2(struct i2c_adapter *adap, 1543static int qup_i2c_xfer_v2(struct i2c_adapter *adap,
1408 struct i2c_msg msgs[], 1544 struct i2c_msg msgs[],
1409 int num) 1545 int num)
1410{ 1546{
1411 struct qup_i2c_dev *qup = i2c_get_adapdata(adap); 1547 struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
1412 int ret, idx = 0; 1548 int ret, idx = 0;
1413 unsigned int total_len = 0;
1414 1549
1415 qup->bus_err = 0; 1550 qup->bus_err = 0;
1416 qup->qup_err = 0; 1551 qup->qup_err = 0;
@@ -1419,6 +1554,10 @@ static int qup_i2c_xfer_v2(struct i2c_adapter *adap,
1419 if (ret < 0) 1554 if (ret < 0)
1420 goto out; 1555 goto out;
1421 1556
1557 ret = qup_i2c_determine_mode_v2(qup, msgs, num);
1558 if (ret)
1559 goto out;
1560
1422 writel(1, qup->base + QUP_SW_RESET); 1561 writel(1, qup->base + QUP_SW_RESET);
1423 ret = qup_i2c_poll_state(qup, QUP_RESET_STATE); 1562 ret = qup_i2c_poll_state(qup, QUP_RESET_STATE);
1424 if (ret) 1563 if (ret)
@@ -1428,60 +1567,35 @@ static int qup_i2c_xfer_v2(struct i2c_adapter *adap,
1428 writel(I2C_MINI_CORE | I2C_N_VAL_V2, qup->base + QUP_CONFIG); 1567 writel(I2C_MINI_CORE | I2C_N_VAL_V2, qup->base + QUP_CONFIG);
1429 writel(QUP_V2_TAGS_EN, qup->base + QUP_I2C_MASTER_GEN); 1568 writel(QUP_V2_TAGS_EN, qup->base + QUP_I2C_MASTER_GEN);
1430 1569
1431 if ((qup->is_dma)) { 1570 if (qup_i2c_poll_state_i2c_master(qup)) {
1432 /* All i2c_msgs should be transferred using either dma or cpu */ 1571 ret = -EIO;
1433 for (idx = 0; idx < num; idx++) { 1572 goto out;
1434 if (msgs[idx].len == 0) {
1435 ret = -EINVAL;
1436 goto out;
1437 }
1438
1439 if (is_vmalloc_addr(msgs[idx].buf))
1440 break;
1441
1442 total_len += msgs[idx].len;
1443 }
1444
1445 if (idx == num && (total_len > qup->out_fifo_sz ||
1446 total_len > qup->in_fifo_sz))
1447 qup->use_dma = true;
1448 } 1573 }
1449 1574
1450 idx = 0; 1575 if (qup->use_dma) {
1576 reinit_completion(&qup->xfer);
1577 ret = qup_i2c_bam_xfer(adap, &msgs[0], num);
1578 qup->use_dma = false;
1579 } else {
1580 qup_i2c_conf_mode_v2(qup);
1451 1581
1452 do { 1582 for (idx = 0; idx < num; idx++) {
1453 if (msgs[idx].len == 0) { 1583 qup->msg = &msgs[idx];
1454 ret = -EINVAL; 1584 qup->is_last = idx == (num - 1);
1455 goto out;
1456 }
1457 1585
1458 if (qup_i2c_poll_state_i2c_master(qup)) { 1586 ret = qup_i2c_xfer_v2_msg(qup, idx,
1459 ret = -EIO; 1587 !!(msgs[idx].flags & I2C_M_RD));
1460 goto out; 1588 if (ret)
1589 break;
1461 } 1590 }
1591 qup->msg = NULL;
1592 }
1462 1593
1463 qup->is_last = (idx == (num - 1)); 1594 if (!ret)
1464 if (idx) 1595 ret = qup_i2c_bus_active(qup, ONE_BYTE);
1465 qup->config_run = QUP_I2C_MX_CONFIG_DURING_RUN;
1466 else
1467 qup->config_run = 0;
1468
1469 reinit_completion(&qup->xfer);
1470
1471 if (qup->use_dma) {
1472 ret = qup_i2c_bam_xfer(adap, &msgs[idx], num);
1473 qup->use_dma = false;
1474 break;
1475 } else {
1476 if (msgs[idx].flags & I2C_M_RD)
1477 ret = qup_i2c_read_one_v2(qup, &msgs[idx]);
1478 else
1479 ret = qup_i2c_write_one_v2(qup, &msgs[idx]);
1480 }
1481 } while ((idx++ < (num - 1)) && !ret);
1482 1596
1483 if (!ret) 1597 if (!ret)
1484 ret = qup_i2c_change_state(qup, QUP_RESET_STATE); 1598 qup_i2c_change_state(qup, QUP_RESET_STATE);
1485 1599
1486 if (ret == 0) 1600 if (ret == 0)
1487 ret = num; 1601 ret = num;
@@ -1545,6 +1659,7 @@ static int qup_i2c_probe(struct platform_device *pdev)
1545 u32 src_clk_freq = DEFAULT_SRC_CLK; 1659 u32 src_clk_freq = DEFAULT_SRC_CLK;
1546 u32 clk_freq = DEFAULT_CLK_FREQ; 1660 u32 clk_freq = DEFAULT_CLK_FREQ;
1547 int blocks; 1661 int blocks;
1662 bool is_qup_v1;
1548 1663
1549 qup = devm_kzalloc(&pdev->dev, sizeof(*qup), GFP_KERNEL); 1664 qup = devm_kzalloc(&pdev->dev, sizeof(*qup), GFP_KERNEL);
1550 if (!qup) 1665 if (!qup)
@@ -1563,12 +1678,10 @@ static int qup_i2c_probe(struct platform_device *pdev)
1563 if (of_device_is_compatible(pdev->dev.of_node, "qcom,i2c-qup-v1.1.1")) { 1678 if (of_device_is_compatible(pdev->dev.of_node, "qcom,i2c-qup-v1.1.1")) {
1564 qup->adap.algo = &qup_i2c_algo; 1679 qup->adap.algo = &qup_i2c_algo;
1565 qup->adap.quirks = &qup_i2c_quirks; 1680 qup->adap.quirks = &qup_i2c_quirks;
1566 qup->is_qup_v1 = true; 1681 is_qup_v1 = true;
1567 qup->write_tx_fifo = qup_i2c_write_tx_fifo_v1;
1568 qup->read_rx_fifo = qup_i2c_read_rx_fifo_v1;
1569 qup->write_rx_tags = qup_i2c_write_rx_tags_v1;
1570 } else { 1682 } else {
1571 qup->adap.algo = &qup_i2c_algo_v2; 1683 qup->adap.algo = &qup_i2c_algo_v2;
1684 is_qup_v1 = false;
1572 ret = qup_i2c_req_dma(qup); 1685 ret = qup_i2c_req_dma(qup);
1573 1686
1574 if (ret == -EPROBE_DEFER) 1687 if (ret == -EPROBE_DEFER)
@@ -1694,14 +1807,31 @@ nodma:
1694 ret = -EIO; 1807 ret = -EIO;
1695 goto fail; 1808 goto fail;
1696 } 1809 }
1697 qup->out_blk_sz = blk_sizes[size] / 2; 1810 qup->out_blk_sz = blk_sizes[size];
1698 1811
1699 size = QUP_INPUT_BLOCK_SIZE(io_mode); 1812 size = QUP_INPUT_BLOCK_SIZE(io_mode);
1700 if (size >= ARRAY_SIZE(blk_sizes)) { 1813 if (size >= ARRAY_SIZE(blk_sizes)) {
1701 ret = -EIO; 1814 ret = -EIO;
1702 goto fail; 1815 goto fail;
1703 } 1816 }
1704 qup->in_blk_sz = blk_sizes[size] / 2; 1817 qup->in_blk_sz = blk_sizes[size];
1818
1819 if (is_qup_v1) {
1820 /*
1821 * in QUP v1, QUP_CONFIG uses N as 15 i.e 16 bits constitutes a
1822 * single transfer but the block size is in bytes so divide the
1823 * in_blk_sz and out_blk_sz by 2
1824 */
1825 qup->in_blk_sz /= 2;
1826 qup->out_blk_sz /= 2;
1827 qup->write_tx_fifo = qup_i2c_write_tx_fifo_v1;
1828 qup->read_rx_fifo = qup_i2c_read_rx_fifo_v1;
1829 qup->write_rx_tags = qup_i2c_write_rx_tags_v1;
1830 } else {
1831 qup->write_tx_fifo = qup_i2c_write_tx_fifo_v2;
1832 qup->read_rx_fifo = qup_i2c_read_rx_fifo_v2;
1833 qup->write_rx_tags = qup_i2c_write_rx_tags_v2;
1834 }
1705 1835
1706 size = QUP_OUTPUT_FIFO_SIZE(io_mode); 1836 size = QUP_OUTPUT_FIFO_SIZE(io_mode);
1707 qup->out_fifo_sz = qup->out_blk_sz * (2 << size); 1837 qup->out_fifo_sz = qup->out_blk_sz * (2 << size);