aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c
diff options
context:
space:
mode:
authorMarek Vasut <marex@denx.de>2013-10-06 08:02:13 -0400
committerWolfram Sang <wsa@the-dreams.de>2013-10-06 09:56:26 -0400
commit29faeb388a1af5c2175e79ce52173c45a262924a (patch)
treeb4dc6bc55f6961e004dbf93e6a2ce820be41b3a9 /drivers/i2c
parent616228a164d4366f82d983c4a70d8006d9fb43f2 (diff)
i2c: mxs: Rework the PIO mode operation
Analyze and rework the PIO mode operation. The PIO mode operation was unreliable on MX28, by analyzing the bus with LA, the checks for when data were available or were to be sent were wrong. The PIO WRITE has to be completely reworked as it multiple problems. The MX23 datasheet helped here, see comments in the code for details. The problems boil down to: - RUN bit in CTRL0 must be set after DATA register was written - The PIO transfer must be 4 bytes long tops, otherwise use clock stretching. Both of these fixes are implemented. The PIO READ operation can only be done for up to four bytes as we are unable to read out the data from the DATA register fast enough. This patch also tries to document the investigation within the code. Signed-off-by: Marek Vasut <marex@denx.de> Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
Diffstat (limited to 'drivers/i2c')
-rw-r--r--drivers/i2c/busses/i2c-mxs.c278
1 files changed, 176 insertions, 102 deletions
diff --git a/drivers/i2c/busses/i2c-mxs.c b/drivers/i2c/busses/i2c-mxs.c
index 2cb0317b14fb..5deb88b1d77c 100644
--- a/drivers/i2c/busses/i2c-mxs.c
+++ b/drivers/i2c/busses/i2c-mxs.c
@@ -1,6 +1,7 @@
1/* 1/*
2 * Freescale MXS I2C bus driver 2 * Freescale MXS I2C bus driver
3 * 3 *
4 * Copyright (C) 2012-2013 Marek Vasut <marex@denx.de>
4 * Copyright (C) 2011-2012 Wolfram Sang, Pengutronix e.K. 5 * Copyright (C) 2011-2012 Wolfram Sang, Pengutronix e.K.
5 * 6 *
6 * based on a (non-working) driver which was: 7 * based on a (non-working) driver which was:
@@ -64,6 +65,7 @@
64#define MXS_I2C_CTRL1_SLAVE_IRQ 0x01 65#define MXS_I2C_CTRL1_SLAVE_IRQ 0x01
65 66
66#define MXS_I2C_STAT (0x50) 67#define MXS_I2C_STAT (0x50)
68#define MXS_I2C_STAT_GOT_A_NAK 0x10000000
67#define MXS_I2C_STAT_BUS_BUSY 0x00000800 69#define MXS_I2C_STAT_BUS_BUSY 0x00000800
68#define MXS_I2C_STAT_CLK_GEN_BUSY 0x00000400 70#define MXS_I2C_STAT_CLK_GEN_BUSY 0x00000400
69 71
@@ -299,48 +301,11 @@ write_init_pio_fail:
299 return -EINVAL; 301 return -EINVAL;
300} 302}
301 303
302static int mxs_i2c_pio_wait_dmareq(struct mxs_i2c_dev *i2c) 304static int mxs_i2c_pio_wait_xfer_end(struct mxs_i2c_dev *i2c)
303{ 305{
304 unsigned long timeout = jiffies + msecs_to_jiffies(1000); 306 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
305 307
306 while (!(readl(i2c->regs + MXS_I2C_DEBUG0) & 308 while (readl(i2c->regs + MXS_I2C_CTRL0) & MXS_I2C_CTRL0_RUN) {
307 MXS_I2C_DEBUG0_DMAREQ)) {
308 if (time_after(jiffies, timeout))
309 return -ETIMEDOUT;
310 cond_resched();
311 }
312
313 return 0;
314}
315
316static int mxs_i2c_pio_wait_cplt(struct mxs_i2c_dev *i2c, int last)
317{
318 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
319
320 /*
321 * We do not use interrupts in the PIO mode. Due to the
322 * maximum transfer length being 8 bytes in PIO mode, the
323 * overhead of interrupt would be too large and this would
324 * neglect the gain from using the PIO mode.
325 */
326
327 while (!(readl(i2c->regs + MXS_I2C_CTRL1) &
328 MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ)) {
329 if (time_after(jiffies, timeout))
330 return -ETIMEDOUT;
331 cond_resched();
332 }
333
334 writel(MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ,
335 i2c->regs + MXS_I2C_CTRL1_CLR);
336
337 /*
338 * When ending a transfer with a stop, we have to wait for the bus to
339 * go idle before we report the transfer as completed. Otherwise the
340 * start of the next transfer may race with the end of the current one.
341 */
342 while (last && (readl(i2c->regs + MXS_I2C_STAT) &
343 (MXS_I2C_STAT_BUS_BUSY | MXS_I2C_STAT_CLK_GEN_BUSY))) {
344 if (time_after(jiffies, timeout)) 309 if (time_after(jiffies, timeout))
345 return -ETIMEDOUT; 310 return -ETIMEDOUT;
346 cond_resched(); 311 cond_resched();
@@ -378,106 +343,208 @@ static void mxs_i2c_pio_trigger_cmd(struct mxs_i2c_dev *i2c, u32 cmd)
378 writel(reg, i2c->regs + MXS_I2C_CTRL0); 343 writel(reg, i2c->regs + MXS_I2C_CTRL0);
379} 344}
380 345
346/*
347 * Start WRITE transaction on the I2C bus. By studying i.MX23 datasheet,
348 * CTRL0::PIO_MODE bit description clarifies the order in which the registers
349 * must be written during PIO mode operation. First, the CTRL0 register has
350 * to be programmed with all the necessary bits but the RUN bit. Then the
351 * payload has to be written into the DATA register. Finally, the transmission
352 * is executed by setting the RUN bit in CTRL0.
353 */
354static void mxs_i2c_pio_trigger_write_cmd(struct mxs_i2c_dev *i2c, u32 cmd,
355 u32 data)
356{
357 writel(cmd, i2c->regs + MXS_I2C_CTRL0);
358 writel(data, i2c->regs + MXS_I2C_DATA);
359 writel(MXS_I2C_CTRL0_RUN, i2c->regs + MXS_I2C_CTRL0_SET);
360}
361
381static int mxs_i2c_pio_setup_xfer(struct i2c_adapter *adap, 362static int mxs_i2c_pio_setup_xfer(struct i2c_adapter *adap,
382 struct i2c_msg *msg, uint32_t flags) 363 struct i2c_msg *msg, uint32_t flags)
383{ 364{
384 struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap); 365 struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
385 uint32_t addr_data = msg->addr << 1; 366 uint32_t addr_data = msg->addr << 1;
386 uint32_t data = 0; 367 uint32_t data = 0;
387 int i, shifts_left, ret; 368 int i, ret, xlen = 0, xmit = 0;
369 uint32_t start;
388 370
389 /* Mute IRQs coming from this block. */ 371 /* Mute IRQs coming from this block. */
390 writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_CLR); 372 writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_CLR);
391 373
374 /*
375 * MX23 idea:
376 * - Enable CTRL0::PIO_MODE (1 << 24)
377 * - Enable CTRL1::ACK_MODE (1 << 27)
378 *
379 * WARNING! The MX23 is broken in some way, even if it claims
380 * to support PIO, when we try to transfer any amount of data
381 * that is not aligned to 4 bytes, the DMA engine will have
382 * bits in DEBUG1::DMA_BYTES_ENABLES still set even after the
383 * transfer. This in turn will mess up the next transfer as
384 * the block it emit one byte write onto the bus terminated
385 * with a NAK+STOP. A possible workaround is to reset the IP
386 * block after every PIO transmission, which might just work.
387 *
388 * NOTE: The CTRL0::PIO_MODE description is important, since
389 * it outlines how the PIO mode is really supposed to work.
390 */
391
392 if (msg->flags & I2C_M_RD) { 392 if (msg->flags & I2C_M_RD) {
393 /*
394 * PIO READ transfer:
395 *
396 * This transfer MUST be limited to 4 bytes maximum. It is not
397 * possible to transfer more than four bytes via PIO, since we
398 * can not in any way make sure we can read the data from the
399 * DATA register fast enough. Besides, the RX FIFO is only four
400 * bytes deep, thus we can only really read up to four bytes at
401 * time. Finally, there is no bit indicating us that new data
402 * arrived at the FIFO and can thus be fetched from the DATA
403 * register.
404 */
405 BUG_ON(msg->len > 4);
406
393 addr_data |= I2C_SMBUS_READ; 407 addr_data |= I2C_SMBUS_READ;
394 408
395 /* SELECT command. */ 409 /* SELECT command. */
396 mxs_i2c_pio_trigger_cmd(i2c, MXS_CMD_I2C_SELECT); 410 mxs_i2c_pio_trigger_write_cmd(i2c, MXS_CMD_I2C_SELECT,
397 411 addr_data);
398 ret = mxs_i2c_pio_wait_dmareq(i2c);
399 if (ret)
400 return ret;
401
402 writel(addr_data, i2c->regs + MXS_I2C_DATA);
403 writel(MXS_I2C_DEBUG0_DMAREQ, i2c->regs + MXS_I2C_DEBUG0_CLR);
404
405 ret = mxs_i2c_pio_wait_cplt(i2c, 0);
406 if (ret)
407 return ret;
408 412
409 if (mxs_i2c_pio_check_error_state(i2c)) 413 ret = mxs_i2c_pio_wait_xfer_end(i2c);
414 if (ret) {
415 dev_err(i2c->dev,
416 "PIO: Failed to send SELECT command!\n");
410 goto cleanup; 417 goto cleanup;
418 }
411 419
412 /* READ command. */ 420 /* READ command. */
413 mxs_i2c_pio_trigger_cmd(i2c, 421 mxs_i2c_pio_trigger_cmd(i2c,
414 MXS_CMD_I2C_READ | flags | 422 MXS_CMD_I2C_READ | flags |
415 MXS_I2C_CTRL0_XFER_COUNT(msg->len)); 423 MXS_I2C_CTRL0_XFER_COUNT(msg->len));
416 424
425 ret = mxs_i2c_pio_wait_xfer_end(i2c);
426 if (ret) {
427 dev_err(i2c->dev,
428 "PIO: Failed to send SELECT command!\n");
429 goto cleanup;
430 }
431
432 data = readl(i2c->regs + MXS_I2C_DATA);
417 for (i = 0; i < msg->len; i++) { 433 for (i = 0; i < msg->len; i++) {
418 if ((i & 3) == 0) {
419 ret = mxs_i2c_pio_wait_dmareq(i2c);
420 if (ret)
421 return ret;
422 data = readl(i2c->regs + MXS_I2C_DATA);
423 writel(MXS_I2C_DEBUG0_DMAREQ,
424 i2c->regs + MXS_I2C_DEBUG0_CLR);
425 }
426 msg->buf[i] = data & 0xff; 434 msg->buf[i] = data & 0xff;
427 data >>= 8; 435 data >>= 8;
428 } 436 }
429 } else { 437 } else {
438 /*
439 * PIO WRITE transfer:
440 *
441 * The code below implements clock stretching to circumvent
442 * the possibility of kernel not being able to supply data
443 * fast enough. It is possible to transfer arbitrary amount
444 * of data using PIO write.
445 */
430 addr_data |= I2C_SMBUS_WRITE; 446 addr_data |= I2C_SMBUS_WRITE;
431 447
432 /* WRITE command. */
433 mxs_i2c_pio_trigger_cmd(i2c,
434 MXS_CMD_I2C_WRITE | flags |
435 MXS_I2C_CTRL0_XFER_COUNT(msg->len + 1));
436
437 /* 448 /*
438 * The LSB of data buffer is the first byte blasted across 449 * The LSB of data buffer is the first byte blasted across
439 * the bus. Higher order bytes follow. Thus the following 450 * the bus. Higher order bytes follow. Thus the following
440 * filling schematic. 451 * filling schematic.
441 */ 452 */
453
442 data = addr_data << 24; 454 data = addr_data << 24;
455
456 /* Start the transfer with START condition. */
457 start = MXS_I2C_CTRL0_PRE_SEND_START;
458
459 /* If the transfer is long, use clock stretching. */
460 if (msg->len > 3)
461 start |= MXS_I2C_CTRL0_RETAIN_CLOCK;
462
443 for (i = 0; i < msg->len; i++) { 463 for (i = 0; i < msg->len; i++) {
444 data >>= 8; 464 data >>= 8;
445 data |= (msg->buf[i] << 24); 465 data |= (msg->buf[i] << 24);
446 if ((i & 3) == 2) { 466
447 ret = mxs_i2c_pio_wait_dmareq(i2c); 467 xmit = 0;
448 if (ret) 468
449 return ret; 469 /* This is the last transfer of the message. */
450 writel(data, i2c->regs + MXS_I2C_DATA); 470 if (i + 1 == msg->len) {
451 writel(MXS_I2C_DEBUG0_DMAREQ, 471 /* Add optional STOP flag. */
452 i2c->regs + MXS_I2C_DEBUG0_CLR); 472 start |= flags;
473 /* Remove RETAIN_CLOCK bit. */
474 start &= ~MXS_I2C_CTRL0_RETAIN_CLOCK;
475 xmit = 1;
453 } 476 }
454 }
455 477
456 shifts_left = 24 - (i & 3) * 8; 478 /* Four bytes are ready in the "data" variable. */
457 if (shifts_left) { 479 if ((i & 3) == 2)
458 data >>= shifts_left; 480 xmit = 1;
459 ret = mxs_i2c_pio_wait_dmareq(i2c); 481
460 if (ret) 482 /* Nothing interesting happened, continue stuffing. */
461 return ret; 483 if (!xmit)
462 writel(data, i2c->regs + MXS_I2C_DATA); 484 continue;
485
486 /*
487 * Compute the size of the transfer and shift the
488 * data accordingly.
489 *
490 * i = (4k + 0) .... xlen = 2
491 * i = (4k + 1) .... xlen = 3
492 * i = (4k + 2) .... xlen = 4
493 * i = (4k + 3) .... xlen = 1
494 */
495
496 if ((i % 4) == 3)
497 xlen = 1;
498 else
499 xlen = (i % 4) + 2;
500
501 data >>= (4 - xlen) * 8;
502
503 dev_dbg(i2c->dev,
504 "PIO: len=%i pos=%i total=%i [W%s%s%s]\n",
505 xlen, i, msg->len,
506 start & MXS_I2C_CTRL0_PRE_SEND_START ? "S" : "",
507 start & MXS_I2C_CTRL0_POST_SEND_STOP ? "E" : "",
508 start & MXS_I2C_CTRL0_RETAIN_CLOCK ? "C" : "");
509
463 writel(MXS_I2C_DEBUG0_DMAREQ, 510 writel(MXS_I2C_DEBUG0_DMAREQ,
464 i2c->regs + MXS_I2C_DEBUG0_CLR); 511 i2c->regs + MXS_I2C_DEBUG0_CLR);
512
513 mxs_i2c_pio_trigger_write_cmd(i2c,
514 start | MXS_I2C_CTRL0_MASTER_MODE |
515 MXS_I2C_CTRL0_DIRECTION |
516 MXS_I2C_CTRL0_XFER_COUNT(xlen), data);
517
518 /* The START condition is sent only once. */
519 start &= ~MXS_I2C_CTRL0_PRE_SEND_START;
520
521 /* Wait for the end of the transfer. */
522 ret = mxs_i2c_pio_wait_xfer_end(i2c);
523 if (ret) {
524 dev_err(i2c->dev,
525 "PIO: Failed to finish WRITE cmd!\n");
526 break;
527 }
528
529 /* Check NAK here. */
530 ret = readl(i2c->regs + MXS_I2C_STAT) &
531 MXS_I2C_STAT_GOT_A_NAK;
532 if (ret) {
533 ret = -ENXIO;
534 goto cleanup;
535 }
465 } 536 }
466 } 537 }
467 538
468 ret = mxs_i2c_pio_wait_cplt(i2c, flags & MXS_I2C_CTRL0_POST_SEND_STOP);
469 if (ret)
470 return ret;
471
472 /* make sure we capture any occurred error into cmd_err */ 539 /* make sure we capture any occurred error into cmd_err */
473 mxs_i2c_pio_check_error_state(i2c); 540 ret = mxs_i2c_pio_check_error_state(i2c);
474 541
475cleanup: 542cleanup:
476 /* Clear any dangling IRQs and re-enable interrupts. */ 543 /* Clear any dangling IRQs and re-enable interrupts. */
477 writel(MXS_I2C_IRQ_MASK, i2c->regs + MXS_I2C_CTRL1_CLR); 544 writel(MXS_I2C_IRQ_MASK, i2c->regs + MXS_I2C_CTRL1_CLR);
478 writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET); 545 writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET);
479 546
480 return 0; 547 return ret;
481} 548}
482 549
483/* 550/*
@@ -487,8 +554,9 @@ static int mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg,
487 int stop) 554 int stop)
488{ 555{
489 struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap); 556 struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
490 int ret, err; 557 int ret;
491 int flags; 558 int flags;
559 int use_pio = 0;
492 560
493 flags = stop ? MXS_I2C_CTRL0_POST_SEND_STOP : 0; 561 flags = stop ? MXS_I2C_CTRL0_POST_SEND_STOP : 0;
494 562
@@ -499,20 +567,25 @@ static int mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg,
499 return -EINVAL; 567 return -EINVAL;
500 568
501 /* 569 /*
502 * The current boundary to select between PIO/DMA transfer method 570 * The MX28 I2C IP block can only do PIO READ for transfer of to up
503 * is set to 8 bytes, transfers shorter than 8 bytes are transfered 571 * 4 bytes of length. The write transfer is not limited as it can use
504 * using PIO mode while longer transfers use DMA. The 8 byte border is 572 * clock stretching to avoid FIFO underruns.
505 * based on this empirical measurement and a lot of previous frobbing.
506 * Note: this special feature only works on i.MX28 SoC
507 */ 573 */
574 if ((msg->flags & I2C_M_RD) && (msg->len <= 4))
575 use_pio = 1;
576 if (!(msg->flags & I2C_M_RD) && (msg->len < 7))
577 use_pio = 1;
578
579 /* Disable PIO on MX23. */
580 if (i2c->dev_type == MXS_I2C_V1)
581 use_pio = 0;
582
508 i2c->cmd_err = 0; 583 i2c->cmd_err = 0;
509 if (0) { /* disable PIO mode until a proper fix is made */ 584 if (use_pio) {
510 ret = mxs_i2c_pio_setup_xfer(adap, msg, flags); 585 ret = mxs_i2c_pio_setup_xfer(adap, msg, flags);
511 if (ret) { 586 /* No need to reset the block if NAK was received. */
512 err = mxs_i2c_reset(i2c); 587 if (ret && (ret != -ENXIO))
513 if (err) 588 mxs_i2c_reset(i2c);
514 return err;
515 }
516 } else { 589 } else {
517 INIT_COMPLETION(i2c->cmd_complete); 590 INIT_COMPLETION(i2c->cmd_complete);
518 ret = mxs_i2c_dma_setup_xfer(adap, msg, flags); 591 ret = mxs_i2c_dma_setup_xfer(adap, msg, flags);
@@ -523,9 +596,11 @@ static int mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg,
523 msecs_to_jiffies(1000)); 596 msecs_to_jiffies(1000));
524 if (ret == 0) 597 if (ret == 0)
525 goto timeout; 598 goto timeout;
599
600 ret = i2c->cmd_err;
526 } 601 }
527 602
528 if (i2c->cmd_err == -ENXIO) { 603 if (ret == -ENXIO) {
529 /* 604 /*
530 * If the transfer fails with a NAK from the slave the 605 * If the transfer fails with a NAK from the slave the
531 * controller halts until it gets told to return to idle state. 606 * controller halts until it gets told to return to idle state.
@@ -534,8 +609,6 @@ static int mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg,
534 i2c->regs + MXS_I2C_CTRL1_SET); 609 i2c->regs + MXS_I2C_CTRL1_SET);
535 } 610 }
536 611
537 ret = i2c->cmd_err;
538
539 dev_dbg(i2c->dev, "Done with err=%d\n", ret); 612 dev_dbg(i2c->dev, "Done with err=%d\n", ret);
540 613
541 return ret; 614 return ret;
@@ -823,6 +896,7 @@ static void __exit mxs_i2c_exit(void)
823} 896}
824module_exit(mxs_i2c_exit); 897module_exit(mxs_i2c_exit);
825 898
899MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
826MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>"); 900MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
827MODULE_DESCRIPTION("MXS I2C Bus Driver"); 901MODULE_DESCRIPTION("MXS I2C Bus Driver");
828MODULE_LICENSE("GPL"); 902MODULE_LICENSE("GPL");