6d3288211727043b8881360163d713634ea3662e
[openwrt/staging/zorun.git] /
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
5 polling for qup v2
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
10 Following are the major issues in current driver code
11
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.
23
24 Because of above, i2c v2 transfers of size greater than 64 are failing
25 with following error message
26
27 i2c_qup 78b6000.i2c: timeout for fifo out full
28
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
32
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
45 space.
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
56 stop events.
57 7. Port the SMBus block read support for new code changes.
58
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>
62 ---
63 drivers/i2c/busses/i2c-qup.c | 900 ++++++++++++++++++++---------------
64 1 file changed, 515 insertions(+), 385 deletions(-)
65
66 --- a/drivers/i2c/busses/i2c-qup.c
67 +++ b/drivers/i2c/busses/i2c-qup.c
68 @@ -141,17 +141,40 @@
69 #define DEFAULT_SRC_CLK 20000000
70
71 /*
72 + * Max tags length (start, stop and maximum 2 bytes address) for each QUP
73 + * data transfer
74 + */
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
80 +
81 +/*
82 * count: no of blocks
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
94 * QUP block read
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 {
110 int tx_tag_len;
111 int rx_tag_len;
112 int data_len;
113 + int cur_blk_len;
114 int total_tx_len;
115 int total_rx_len;
116 + int tx_fifo_data_pos;
117 int tx_fifo_free;
118 + int rx_fifo_data_pos;
119 int fifo_available;
120 + u32 tx_fifo_data;
121 + u32 rx_fifo_data;
122 + u8 *cur_data;
123 + u8 *cur_tx_tags;
124 + bool tx_tags_sent;
125 + bool send_last_word;
126 + bool rx_tags_fetched;
127 bool rx_bytes_read;
128 bool is_tx_blk_mode;
129 bool is_rx_blk_mode;
130 @@ -198,6 +231,7 @@ struct qup_i2c_dev {
131 int out_blk_sz;
132 int in_blk_sz;
133
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 {
139
140 /* To check if this is the last msg */
141 bool is_last;
142 - bool is_qup_v1;
143 + bool is_smbus_read;
144
145 /* To configure when bus is in run state */
146 - int config_run;
147 + u32 config_run;
148
149 /* dma parameters */
150 bool is_dma;
151 @@ -223,6 +257,8 @@ struct qup_i2c_dev {
152 bool use_dma;
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
161 goto done;
162 }
163
164 - if (!qup->is_qup_v1)
165 - goto done;
166 -
167 if (opflags & QUP_OUT_SVC_FLAG) {
168 writel(QUP_OUT_SVC_FLAG, qup->base + QUP_OPERATIONAL);
169
170 @@ -416,108 +449,6 @@ static int qup_i2c_bus_active(struct qup
171 return ret;
172 }
173
174 -/**
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
180 - */
181 -static int qup_i2c_wait_ready(struct qup_i2c_dev *qup, int op, bool val,
182 - int len)
183 -{
184 - unsigned long timeout;
185 - u32 opflags;
186 - u32 status;
187 - u32 shift = __ffs(op);
188 - int ret = 0;
189 -
190 - len *= qup->one_byte_t;
191 - /* timeout after a wait of twice the max time */
192 - timeout = jiffies + len * 4;
193 -
194 - for (;;) {
195 - opflags = readl(qup->base + QUP_OPERATIONAL);
196 - status = readl(qup->base + QUP_I2C_STATUS);
197 -
198 - if (((opflags & op) >> shift) == val) {
199 - if ((op == QUP_OUT_NOT_EMPTY) && qup->is_last) {
200 - if (!(status & I2C_STATUS_BUS_ACTIVE)) {
201 - ret = 0;
202 - goto done;
203 - }
204 - } else {
205 - ret = 0;
206 - goto done;
207 - }
208 - }
209 -
210 - if (time_after(jiffies, timeout)) {
211 - ret = -ETIMEDOUT;
212 - goto done;
213 - }
214 - usleep_range(len, len * 2);
215 - }
216 -
217 -done:
218 - if (qup->bus_err || qup->qup_err)
219 - ret = (qup->bus_err & QUP_I2C_NACK_FLAG) ? -ENXIO : -EIO;
220 -
221 - return ret;
222 -}
223 -
224 -static void qup_i2c_set_write_mode_v2(struct qup_i2c_dev *qup,
225 - struct i2c_msg *msg)
226 -{
227 - /* Number of entries to shift out, including the tags */
228 - int total = msg->len + qup->blk.tx_tag_len;
229 -
230 - total |= qup->config_run;
231 -
232 - if (total < qup->out_fifo_sz) {
233 - /* FIFO mode */
234 - writel(QUP_REPACK_EN, qup->base + QUP_IO_MODE);
235 - writel(total, qup->base + QUP_MX_WRITE_CNT);
236 - } else {
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);
241 - }
242 -}
243 -
244 -static int check_for_fifo_space(struct qup_i2c_dev *qup)
245 -{
246 - int ret;
247 -
248 - ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
249 - if (ret)
250 - goto out;
251 -
252 - ret = qup_i2c_wait_ready(qup, QUP_OUT_FULL,
253 - RESET_BIT, 4 * ONE_BYTE);
254 - if (ret) {
255 - /* Fifo is full. Drain out the fifo */
256 - ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
257 - if (ret)
258 - goto out;
259 -
260 - ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY,
261 - RESET_BIT, 256 * ONE_BYTE);
262 - if (ret) {
263 - dev_err(qup->dev, "timeout for fifo out full");
264 - goto out;
265 - }
266 -
267 - ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
268 - if (ret)
269 - goto out;
270 - }
271 -
272 -out:
273 - return ret;
274 -}
275 -
276 static void qup_i2c_write_tx_fifo_v1(struct qup_i2c_dev *qup)
277 {
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,
281 struct i2c_msg *msg)
282 {
283 - memset(&qup->blk, 0, sizeof(qup->blk));
284 -
285 + qup->blk.pos = 0;
286 qup->blk.data_len = msg->len;
287 - qup->blk.count = (msg->len + QUP_READ_LIMIT - 1) / QUP_READ_LIMIT;
288 -
289 - /* 4 bytes for first block and 2 writes for rest */
290 - qup->blk.tx_tag_len = 4 + (qup->blk.count - 1) * 2;
291 -
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;
295 -}
296 -
297 -static int qup_i2c_send_data(struct qup_i2c_dev *qup, int tlen, u8 *tbuf,
298 - int dlen, u8 *dbuf)
299 -{
300 - u32 val = 0, idx = 0, pos = 0, i = 0, t;
301 - int len = tlen + dlen;
302 - u8 *buf = tbuf;
303 - int ret = 0;
304 -
305 - while (len > 0) {
306 - ret = check_for_fifo_space(qup);
307 - if (ret)
308 - return ret;
309 -
310 - t = (len >= 4) ? 4 : len;
311 -
312 - while (idx < t) {
313 - if (!i && (pos >= tlen)) {
314 - buf = dbuf;
315 - pos = 0;
316 - i = 1;
317 - }
318 - val |= buf[pos++] << (idx++ * 8);
319 - }
320 -
321 - writel(val, qup->base + QUP_OUT_FIFO_BASE);
322 - idx = 0;
323 - val = 0;
324 - len -= 4;
325 - }
326 -
327 - ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
328 -
329 - return ret;
330 + qup->blk.count = DIV_ROUND_UP(msg->len, qup->blk_xfer_limit);
331 }
332
333 static int qup_i2c_get_data_len(struct qup_i2c_dev *qup)
334 {
335 int data_len;
336
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;
341 else
342 data_len = qup->blk.data_len;
343
344 @@ -630,9 +518,9 @@ static int qup_i2c_set_tags_smb(u16 addr
345 {
346 int len = 0;
347
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);
353 } else {
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
357 return len;
358 }
359
360 -static int qup_i2c_issue_xfer_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
361 -{
362 - int data_len = 0, tag_len, index;
363 - int ret;
364 -
365 - tag_len = qup_i2c_set_tags(qup->blk.tags, qup, msg);
366 - index = msg->len - qup->blk.data_len;
367 -
368 - /* only tags are written for read */
369 - if (!(msg->flags & I2C_M_RD))
370 - data_len = qup_i2c_get_data_len(qup);
371 -
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;
375 -
376 - return ret;
377 -}
378
379 static void qup_i2c_bam_cb(void *data)
380 {
381 @@ -778,6 +648,7 @@ static int qup_i2c_bam_make_desc(struct
382 u32 i = 0, tlen, tx_len = 0;
383 u8 *tags;
384
385 + qup->blk_xfer_limit = QUP_READ_LIMIT;
386 qup_i2c_set_blk_data(qup, msg);
387
388 blocks = qup->blk.count;
389 @@ -1026,7 +897,7 @@ static int qup_i2c_wait_for_complete(str
390 unsigned long left;
391 int ret = 0;
392
393 - left = wait_for_completion_timeout(&qup->xfer, HZ);
394 + left = wait_for_completion_timeout(&qup->xfer, qup->xfer_timeout);
395 if (!left) {
396 writel(1, qup->base + QUP_SW_RESET);
397 ret = -ETIMEDOUT;
398 @@ -1038,65 +909,6 @@ static int qup_i2c_wait_for_complete(str
399 return ret;
400 }
401
402 -static int qup_i2c_write_one_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
403 -{
404 - int ret = 0;
405 -
406 - qup->msg = msg;
407 - qup->pos = 0;
408 - enable_irq(qup->irq);
409 - qup_i2c_set_blk_data(qup, msg);
410 - qup_i2c_set_write_mode_v2(qup, msg);
411 -
412 - ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
413 - if (ret)
414 - goto err;
415 -
416 - writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
417 -
418 - do {
419 - ret = qup_i2c_issue_xfer_v2(qup, msg);
420 - if (ret)
421 - goto err;
422 -
423 - ret = qup_i2c_wait_for_complete(qup, msg);
424 - if (ret)
425 - goto err;
426 -
427 - qup->blk.pos++;
428 - } while (qup->blk.pos < qup->blk.count);
429 -
430 - ret = qup_i2c_wait_ready(qup, QUP_OUT_NOT_EMPTY, RESET_BIT, ONE_BYTE);
431 -
432 -err:
433 - disable_irq(qup->irq);
434 - qup->msg = NULL;
435 -
436 - return ret;
437 -}
438 -
439 -static void qup_i2c_set_read_mode_v2(struct qup_i2c_dev *qup, int len)
440 -{
441 - int tx_len = qup->blk.tx_tag_len;
442 -
443 - len += qup->blk.rx_tag_len;
444 - len |= qup->config_run;
445 - tx_len |= qup->config_run;
446 -
447 - if (len < qup->in_fifo_sz) {
448 - /* FIFO mode */
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);
452 - } else {
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);
458 - }
459 -}
460 -
461 static void qup_i2c_read_rx_fifo_v1(struct qup_i2c_dev *qup)
462 {
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;
466 }
467
468 -static int qup_i2c_read_fifo_v2(struct qup_i2c_dev *qup,
469 - struct i2c_msg *msg)
470 -{
471 - u32 val;
472 - int idx, pos = 0, ret = 0, total, msg_offset = 0;
473 -
474 - /*
475 - * If the message length is already read in
476 - * the first byte of the buffer, account for
477 - * that by setting the offset
478 - */
479 - if (qup_i2c_check_msg_len(msg) && (msg->len > 1))
480 - msg_offset = 1;
481 - total = qup_i2c_get_data_len(qup);
482 - total -= msg_offset;
483 -
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);
489 - if (ret) {
490 - dev_err(qup->dev, "timeout for fifo not empty");
491 - return ret;
492 - }
493 - val = readl(qup->base + QUP_IN_FIFO_BASE);
494 -
495 - for (idx = 0; idx < 4; idx++, val >>= 8, pos++) {
496 - /* first 2 bytes are tag bytes */
497 - if (pos < 2)
498 - continue;
499 -
500 - if (pos >= (total + 2))
501 - goto out;
502 - msg->buf[qup->pos + msg_offset] = val & 0xff;
503 - qup->pos++;
504 - }
505 - }
506 -
507 -out:
508 - qup->blk.data_len -= total;
509 -
510 - return ret;
511 -}
512 -
513 -static int qup_i2c_read_one_v2(struct qup_i2c_dev *qup, struct i2c_msg *msg)
514 -{
515 - int ret = 0;
516 -
517 - qup->msg = msg;
518 - qup->pos = 0;
519 - enable_irq(qup->irq);
520 - qup_i2c_set_blk_data(qup, msg);
521 - qup_i2c_set_read_mode_v2(qup, msg->len);
522 -
523 - ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
524 - if (ret)
525 - goto err;
526 -
527 - writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
528 -
529 - do {
530 - ret = qup_i2c_issue_xfer_v2(qup, msg);
531 - if (ret)
532 - goto err;
533 -
534 - ret = qup_i2c_wait_for_complete(qup, msg);
535 - if (ret)
536 - goto err;
537 -
538 - ret = qup_i2c_read_fifo_v2(qup, msg);
539 - if (ret)
540 - goto err;
541 -
542 - qup->blk.pos++;
543 -
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) {
547 - ret = -EPROTO;
548 - goto err;
549 - }
550 - msg->len += msg->buf[0];
551 - qup->pos = 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]);
556 - }
557 - } while (qup->blk.pos < qup->blk.count);
558 -
559 -err:
560 - disable_irq(qup->irq);
561 - qup->msg = NULL;
562 -
563 - return ret;
564 -}
565 -
566 static void qup_i2c_write_rx_tags_v1(struct qup_i2c_dev *qup)
567 {
568 struct i2c_msg *msg = qup->msg;
569 @@ -1404,13 +1118,434 @@ out:
570 return ret;
571 }
572
573 +/*
574 + * Configure registers related with reconfiguration during run and call it
575 + * before each i2c sub transfer.
576 + */
577 +static void qup_i2c_conf_count_v2(struct qup_i2c_dev *qup)
578 +{
579 + struct qup_i2c_block *blk = &qup->blk;
580 + u32 qup_config = I2C_MINI_CORE | I2C_N_VAL_V2;
581 +
582 + if (blk->is_tx_blk_mode)
583 + writel(qup->config_run | blk->total_tx_len,
584 + qup->base + QUP_MX_OUTPUT_CNT);
585 + else
586 + writel(qup->config_run | blk->total_tx_len,
587 + qup->base + QUP_MX_WRITE_CNT);
588 +
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);
593 + else
594 + writel(qup->config_run | blk->total_rx_len,
595 + qup->base + QUP_MX_READ_CNT);
596 + } else {
597 + qup_config |= QUP_NO_INPUT;
598 + }
599 +
600 + writel(qup_config, qup->base + QUP_CONFIG);
601 +}
602 +
603 +/*
604 + * Configure registers related with transfer mode (FIFO/Block)
605 + * before starting of i2c transfer. It will be called only once in
606 + * QUP RESET state.
607 + */
608 +static void qup_i2c_conf_mode_v2(struct qup_i2c_dev *qup)
609 +{
610 + struct qup_i2c_block *blk = &qup->blk;
611 + u32 io_mode = QUP_REPACK_EN;
612 +
613 + if (blk->is_tx_blk_mode) {
614 + io_mode |= QUP_OUTPUT_BLK_MODE;
615 + writel(0, qup->base + QUP_MX_WRITE_CNT);
616 + } else {
617 + writel(0, qup->base + QUP_MX_OUTPUT_CNT);
618 + }
619 +
620 + if (blk->is_rx_blk_mode) {
621 + io_mode |= QUP_INPUT_BLK_MODE;
622 + writel(0, qup->base + QUP_MX_READ_CNT);
623 + } else {
624 + writel(0, qup->base + QUP_MX_INPUT_CNT);
625 + }
626 +
627 + writel(io_mode, qup->base + QUP_IO_MODE);
628 +}
629 +
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)
632 +{
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;
638 +
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;
644 +}
645 +
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)
648 +{
649 + struct qup_i2c_block *blk = &qup->blk;
650 + int j;
651 +
652 + for (j = blk->rx_fifo_data_pos;
653 + blk->cur_blk_len && blk->fifo_available;
654 + blk->cur_blk_len--, blk->fifo_available--) {
655 + if (j == 0)
656 + blk->rx_fifo_data = readl(qup->base + QUP_IN_FIFO_BASE);
657 +
658 + *(blk->cur_data++) = blk->rx_fifo_data;
659 + blk->rx_fifo_data >>= 8;
660 +
661 + if (j == 3)
662 + j = 0;
663 + else
664 + j++;
665 + }
666 +
667 + blk->rx_fifo_data_pos = j;
668 +}
669 +
670 +/* Receive tags for read message in QUP v2 i2c transfer. */
671 +static void qup_i2c_recv_tags(struct qup_i2c_dev *qup)
672 +{
673 + struct qup_i2c_block *blk = &qup->blk;
674 +
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;
679 +}
680 +
681 +/*
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.
688 + */
689 +static void qup_i2c_read_rx_fifo_v2(struct qup_i2c_dev *qup)
690 +{
691 + struct qup_i2c_block *blk = &qup->blk;
692 +
693 + if (!blk->rx_tags_fetched) {
694 + qup_i2c_recv_tags(qup);
695 + blk->rx_tags_fetched = true;
696 + }
697 +
698 + qup_i2c_recv_data(qup);
699 + if (!blk->cur_blk_len)
700 + blk->rx_bytes_read = true;
701 +}
702 +
703 +/*
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.
707 + */
708 +static void
709 +qup_i2c_write_blk_data(struct qup_i2c_dev *qup, u8 **data, unsigned int *len)
710 +{
711 + struct qup_i2c_block *blk = &qup->blk;
712 + unsigned int j;
713 +
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);
717 + if (j == 3) {
718 + writel(blk->tx_fifo_data,
719 + qup->base + QUP_OUT_FIFO_BASE);
720 + blk->tx_fifo_data = 0x0;
721 + j = 0;
722 + } else {
723 + j++;
724 + }
725 + }
726 +
727 + blk->tx_fifo_data_pos = j;
728 +}
729 +
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)
732 +{
733 + struct qup_i2c_block *blk = &qup->blk;
734 +
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);
738 +}
739 +
740 +/*
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
743 + *
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
749 + * in FIFO.
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.
761 + */
762 +static void qup_i2c_write_tx_fifo_v2(struct qup_i2c_dev *qup)
763 +{
764 + struct qup_i2c_block *blk = &qup->blk;
765 +
766 + if (!blk->tx_tags_sent) {
767 + qup_i2c_write_blk_data(qup, &blk->cur_tx_tags,
768 + &blk->tx_tag_len);
769 + blk->tx_tags_sent = true;
770 + }
771 +
772 + if (blk->send_last_word)
773 + goto send_last_word;
774 +
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)
778 + return;
779 +
780 + if (blk->tx_fifo_free)
781 + goto send_last_word;
782 +
783 + blk->send_last_word = true;
784 + }
785 +
786 + return;
787 +
788 +send_last_word:
789 + writel(blk->tx_fifo_data, qup->base + QUP_OUT_FIFO_BASE);
790 +}
791 +
792 +/*
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.
796 + */
797 +static int
798 +qup_i2c_conf_xfer_v2(struct qup_i2c_dev *qup, bool is_rx, bool is_first,
799 + bool change_pause_state)
800 +{
801 + struct qup_i2c_block *blk = &qup->blk;
802 + struct i2c_msg *msg = qup->msg;
803 + int ret;
804 +
805 + /*
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.
809 + */
810 + if (qup_i2c_check_msg_len(msg)) {
811 + if (qup->is_smbus_read) {
812 + /*
813 + * If the message length is already read in
814 + * the first byte of the buffer, account for
815 + * that by setting the offset
816 + */
817 + blk->cur_data += 1;
818 + is_first = false;
819 + } else {
820 + change_pause_state = false;
821 + }
822 + }
823 +
824 + qup->config_run = is_first ? 0 : QUP_I2C_MX_CONFIG_DURING_RUN;
825 +
826 + qup_i2c_clear_blk_v2(blk);
827 + qup_i2c_conf_count_v2(qup);
828 +
829 + /* If it is first sub transfer, then configure i2c bus clocks */
830 + if (is_first) {
831 + ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
832 + if (ret)
833 + return ret;
834 +
835 + writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL);
836 +
837 + ret = qup_i2c_change_state(qup, QUP_PAUSE_STATE);
838 + if (ret)
839 + return ret;
840 + }
841 +
842 + reinit_completion(&qup->xfer);
843 + enable_irq(qup->irq);
844 + /*
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
847 + */
848 + if (!blk->is_tx_blk_mode) {
849 + blk->tx_fifo_free = qup->out_fifo_sz;
850 +
851 + if (is_rx)
852 + qup_i2c_write_rx_tags_v2(qup);
853 + else
854 + qup_i2c_write_tx_fifo_v2(qup);
855 + }
856 +
857 + ret = qup_i2c_change_state(qup, QUP_RUN_STATE);
858 + if (ret)
859 + goto err;
860 +
861 + ret = qup_i2c_wait_for_complete(qup, msg);
862 + if (ret)
863 + goto err;
864 +
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);
868 + if (ret)
869 + goto err;
870 + }
871 +
872 +err:
873 + disable_irq(qup->irq);
874 + return ret;
875 +}
876 +
877 +/*
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.
881 + */
882 +static int qup_i2c_xfer_v2_msg(struct qup_i2c_dev *qup, int msg_id, bool is_rx)
883 +{
884 + int ret = 0;
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;
889 +
890 + qup->blk_xfer_limit = is_rx ? RECV_MAX_DATA_LEN : QUP_READ_LIMIT;
891 + qup_i2c_set_blk_data(qup, msg);
892 +
893 + for (i = 0; i < blk->count; i++) {
894 + data_len = qup_i2c_get_data_len(qup);
895 + blk->pos = i;
896 + blk->cur_tx_tags = blk->tags;
897 + blk->cur_blk_len = data_len;
898 + blk->tx_tag_len =
899 + qup_i2c_set_tags(blk->cur_tx_tags, qup, qup->msg);
900 +
901 + blk->cur_data = msg_buf;
902 +
903 + if (is_rx) {
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;
907 + } else {
908 + blk->total_tx_len = blk->tx_tag_len + data_len;
909 + blk->total_rx_len = 0;
910 + }
911 +
912 + ret = qup_i2c_conf_xfer_v2(qup, is_rx, !msg_id && !i,
913 + !qup->is_last || i < blk->count - 1);
914 + if (ret)
915 + return ret;
916 +
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)
921 + return -EPROTO;
922 +
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;
927 + if (ret)
928 + return ret;
929 +
930 + msg->len += 1;
931 + }
932 +
933 + msg_buf += data_len;
934 + blk->data_len -= qup->blk_xfer_limit;
935 + }
936 +
937 + return ret;
938 +}
939 +
940 +/*
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
946 + *
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
949 + * the mode used.
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
952 + * mode.
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.
955 + */
956 +static int
957 +qup_i2c_determine_mode_v2(struct qup_i2c_dev *qup,
958 + struct i2c_msg msgs[], int num)
959 +{
960 + int idx;
961 + bool no_dma = false;
962 + unsigned int max_tx_len = 0, max_rx_len = 0, total_len = 0;
963 +
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)
967 + return -EINVAL;
968 +
969 + if (msgs[idx].flags & I2C_M_RD)
970 + max_rx_len = max_t(unsigned int, max_rx_len,
971 + msgs[idx].len);
972 + else
973 + max_tx_len = max_t(unsigned int, max_tx_len,
974 + msgs[idx].len);
975 +
976 + if (is_vmalloc_addr(msgs[idx].buf))
977 + no_dma = true;
978 +
979 + total_len += msgs[idx].len;
980 + }
981 +
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;
985 + } else {
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;
990 + }
991 +
992 + return 0;
993 +}
994 +
995 static int qup_i2c_xfer_v2(struct i2c_adapter *adap,
996 struct i2c_msg msgs[],
997 int num)
998 {
999 struct qup_i2c_dev *qup = i2c_get_adapdata(adap);
1000 int ret, idx = 0;
1001 - unsigned int total_len = 0;
1002
1003 qup->bus_err = 0;
1004 qup->qup_err = 0;
1005 @@ -1419,6 +1554,10 @@ static int qup_i2c_xfer_v2(struct i2c_ad
1006 if (ret < 0)
1007 goto out;
1008
1009 + ret = qup_i2c_determine_mode_v2(qup, msgs, num);
1010 + if (ret)
1011 + goto out;
1012 +
1013 writel(1, qup->base + QUP_SW_RESET);
1014 ret = qup_i2c_poll_state(qup, QUP_RESET_STATE);
1015 if (ret)
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);
1019
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) {
1024 - ret = -EINVAL;
1025 - goto out;
1026 - }
1027 -
1028 - if (is_vmalloc_addr(msgs[idx].buf))
1029 - break;
1030 -
1031 - total_len += msgs[idx].len;
1032 - }
1033 -
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)) {
1038 + ret = -EIO;
1039 + goto out;
1040 }
1041
1042 - idx = 0;
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;
1047 + } else {
1048 + qup_i2c_conf_mode_v2(qup);
1049
1050 - do {
1051 - if (msgs[idx].len == 0) {
1052 - ret = -EINVAL;
1053 - goto out;
1054 - }
1055 + for (idx = 0; idx < num; idx++) {
1056 + qup->msg = &msgs[idx];
1057 + qup->is_last = idx == (num - 1);
1058
1059 - if (qup_i2c_poll_state_i2c_master(qup)) {
1060 - ret = -EIO;
1061 - goto out;
1062 + ret = qup_i2c_xfer_v2_msg(qup, idx,
1063 + !!(msgs[idx].flags & I2C_M_RD));
1064 + if (ret)
1065 + break;
1066 }
1067 + qup->msg = NULL;
1068 + }
1069
1070 - qup->is_last = (idx == (num - 1));
1071 - if (idx)
1072 - qup->config_run = QUP_I2C_MX_CONFIG_DURING_RUN;
1073 - else
1074 - qup->config_run = 0;
1075 -
1076 - reinit_completion(&qup->xfer);
1077 -
1078 - if (qup->use_dma) {
1079 - ret = qup_i2c_bam_xfer(adap, &msgs[idx], num);
1080 - qup->use_dma = false;
1081 - break;
1082 - } else {
1083 - if (msgs[idx].flags & I2C_M_RD)
1084 - ret = qup_i2c_read_one_v2(qup, &msgs[idx]);
1085 - else
1086 - ret = qup_i2c_write_one_v2(qup, &msgs[idx]);
1087 - }
1088 - } while ((idx++ < (num - 1)) && !ret);
1089 + if (!ret)
1090 + ret = qup_i2c_bus_active(qup, ONE_BYTE);
1091
1092 if (!ret)
1093 - ret = qup_i2c_change_state(qup, QUP_RESET_STATE);
1094 + qup_i2c_change_state(qup, QUP_RESET_STATE);
1095
1096 if (ret == 0)
1097 ret = num;
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;
1101 int blocks;
1102 + bool is_qup_v1;
1103
1104 qup = devm_kzalloc(&pdev->dev, sizeof(*qup), GFP_KERNEL);
1105 if (!qup)
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;
1114 + is_qup_v1 = true;
1115 } else {
1116 qup->adap.algo = &qup_i2c_algo_v2;
1117 + is_qup_v1 = false;
1118 ret = qup_i2c_req_dma(qup);
1119
1120 if (ret == -EPROBE_DEFER)
1121 @@ -1694,14 +1807,31 @@ nodma:
1122 ret = -EIO;
1123 goto fail;
1124 }
1125 - qup->out_blk_sz = blk_sizes[size] / 2;
1126 + qup->out_blk_sz = blk_sizes[size];
1127
1128 size = QUP_INPUT_BLOCK_SIZE(io_mode);
1129 if (size >= ARRAY_SIZE(blk_sizes)) {
1130 ret = -EIO;
1131 goto fail;
1132 }
1133 - qup->in_blk_sz = blk_sizes[size] / 2;
1134 + qup->in_blk_sz = blk_sizes[size];
1135 +
1136 + if (is_qup_v1) {
1137 + /*
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
1141 + */
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;
1147 + } else {
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;
1151 + }
1152
1153 size = QUP_OUTPUT_FIFO_SIZE(io_mode);
1154 qup->out_fifo_sz = qup->out_blk_sz * (2 << size);