Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

i2c: octeon: Use i2c recovery framework

Switch to the i2c bus recovery framework using generic SCL recovery.
If this fails try to reset the hardware. The recovery is triggered
during START on timeout of the interrupt or failure to reach
the START / repeated-START condition.

The START function is moved to xfer and while at it remove the
xfer debug message (i2c core already provides a debug message
for this).

Signed-off-by: Jan Glauber <jglauber@cavium.com>
[wsa: removed one empty line]
Signed-off-by: Wolfram Sang <wsa@the-dreams.de>

authored by

Jan Glauber and committed by
Wolfram Sang
c981e34e b4c715d0

+96 -63
+96 -63
drivers/i2c/busses/i2c-octeon.c
··· 90 90 #define TWSI_INT_CORE_EN BIT_ULL(6) 91 91 #define TWSI_INT_SDA_OVR BIT_ULL(8) 92 92 #define TWSI_INT_SCL_OVR BIT_ULL(9) 93 + #define TWSI_INT_SDA BIT_ULL(10) 94 + #define TWSI_INT_SCL BIT_ULL(11) 93 95 94 96 struct octeon_i2c { 95 97 wait_queue_head_t queue; ··· 155 153 octeon_i2c_reg_read(i2c, SW_TWSI_EOP_TWSI_STAT) 156 154 157 155 /** 156 + * octeon_i2c_read_int - read the TWSI_INT register 157 + * @i2c: The struct octeon_i2c 158 + * 159 + * Returns the value of the register. 160 + */ 161 + static u64 octeon_i2c_read_int(struct octeon_i2c *i2c) 162 + { 163 + return __raw_readq(i2c->twsi_base + TWSI_INT); 164 + } 165 + 166 + /** 158 167 * octeon_i2c_write_int - write the TWSI_INT register 159 168 * @i2c: The struct octeon_i2c 160 169 * @data: Value to be written ··· 192 179 static void octeon_i2c_int_disable(struct octeon_i2c *i2c) 193 180 { 194 181 /* clear TS/ST/IFLG events */ 195 - octeon_i2c_write_int(i2c, 0); 196 - } 197 - 198 - /** 199 - * octeon_i2c_unblock - unblock the bus 200 - * @i2c: The struct octeon_i2c 201 - * 202 - * If there was a reset while a device was driving 0 to bus, bus is blocked. 203 - * We toggle it free manually by some clock cycles and send a stop. 204 - */ 205 - static void octeon_i2c_unblock(struct octeon_i2c *i2c) 206 - { 207 - int i; 208 - 209 - dev_dbg(i2c->dev, "%s\n", __func__); 210 - 211 - for (i = 0; i < 9; i++) { 212 - octeon_i2c_write_int(i2c, 0); 213 - udelay(5); 214 - octeon_i2c_write_int(i2c, TWSI_INT_SCL_OVR); 215 - udelay(5); 216 - } 217 - /* hand-crank a STOP */ 218 - octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR | TWSI_INT_SCL_OVR); 219 - udelay(5); 220 - octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR); 221 - udelay(5); 222 182 octeon_i2c_write_int(i2c, 0); 223 183 } 224 184 ··· 357 371 return -EIO; 358 372 } 359 373 374 + static int octeon_i2c_recovery(struct octeon_i2c *i2c) 375 + { 376 + int ret; 377 + 378 + ret = i2c_recover_bus(&i2c->adap); 379 + if (ret) 380 + /* recover failed, try hardware re-init */ 381 + ret = octeon_i2c_init_lowlevel(i2c); 382 + return ret; 383 + } 384 + 360 385 /** 361 386 * octeon_i2c_start - send START to the bus 362 387 * @i2c: The struct octeon_i2c ··· 376 379 */ 377 380 static int octeon_i2c_start(struct octeon_i2c *i2c) 378 381 { 379 - int result; 380 - u8 data; 382 + int ret; 383 + u8 stat; 381 384 382 385 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STA); 386 + ret = octeon_i2c_wait(i2c); 387 + if (ret) 388 + goto error; 383 389 384 - result = octeon_i2c_wait(i2c); 385 - if (result) { 386 - if (octeon_i2c_stat_read(i2c) == STAT_IDLE) { 387 - /* 388 - * Controller refused to send start flag May 389 - * be a client is holding SDA low - let's try 390 - * to free it. 391 - */ 392 - octeon_i2c_unblock(i2c); 393 - octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STA); 394 - result = octeon_i2c_wait(i2c); 395 - } 396 - if (result) 397 - return result; 398 - } 390 + stat = octeon_i2c_stat_read(i2c); 391 + if (stat == STAT_START || stat == STAT_REP_START) 392 + /* START successful, bail out */ 393 + return 0; 399 394 400 - data = octeon_i2c_stat_read(i2c); 401 - if ((data != STAT_START) && (data != STAT_REP_START)) { 402 - dev_err(i2c->dev, "%s: bad status (0x%x)\n", __func__, data); 403 - return -EIO; 404 - } 405 - 406 - return 0; 395 + error: 396 + /* START failed, try to recover */ 397 + ret = octeon_i2c_recovery(i2c); 398 + return (ret) ? ret : -EAGAIN; 407 399 } 408 400 409 401 /* send STOP to the bus */ ··· 416 430 const u8 *data, int length) 417 431 { 418 432 int i, result; 419 - 420 - result = octeon_i2c_start(i2c); 421 - if (result) 422 - return result; 423 433 424 434 octeon_i2c_data_write(i2c, target << 1); 425 435 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB); ··· 460 478 461 479 if (length < 1) 462 480 return -EINVAL; 463 - 464 - result = octeon_i2c_start(i2c); 465 - if (result) 466 - return result; 467 481 468 482 octeon_i2c_data_write(i2c, (target << 1) | 1); 469 483 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB); ··· 524 546 for (i = 0; ret == 0 && i < num; i++) { 525 547 struct i2c_msg *pmsg = &msgs[i]; 526 548 527 - dev_dbg(i2c->dev, 528 - "Doing %s %d byte(s) to/from 0x%02x - %d of %d messages\n", 529 - pmsg->flags & I2C_M_RD ? "read" : "write", 530 - pmsg->len, pmsg->addr, i + 1, num); 549 + ret = octeon_i2c_start(i2c); 550 + if (ret) 551 + return ret; 552 + 531 553 if (pmsg->flags & I2C_M_RD) 532 554 ret = octeon_i2c_read(i2c, pmsg->addr, pmsg->buf, 533 555 &pmsg->len, pmsg->flags & I2C_M_RECV_LEN); ··· 539 561 540 562 return (ret != 0) ? ret : num; 541 563 } 564 + 565 + static int octeon_i2c_get_scl(struct i2c_adapter *adap) 566 + { 567 + struct octeon_i2c *i2c = i2c_get_adapdata(adap); 568 + u64 state; 569 + 570 + state = octeon_i2c_read_int(i2c); 571 + return state & TWSI_INT_SCL; 572 + } 573 + 574 + static void octeon_i2c_set_scl(struct i2c_adapter *adap, int val) 575 + { 576 + struct octeon_i2c *i2c = i2c_get_adapdata(adap); 577 + 578 + octeon_i2c_write_int(i2c, TWSI_INT_SCL_OVR); 579 + } 580 + 581 + static int octeon_i2c_get_sda(struct i2c_adapter *adap) 582 + { 583 + struct octeon_i2c *i2c = i2c_get_adapdata(adap); 584 + u64 state; 585 + 586 + state = octeon_i2c_read_int(i2c); 587 + return state & TWSI_INT_SDA; 588 + } 589 + 590 + static void octeon_i2c_prepare_recovery(struct i2c_adapter *adap) 591 + { 592 + struct octeon_i2c *i2c = i2c_get_adapdata(adap); 593 + 594 + /* 595 + * The stop resets the state machine, does not _transmit_ STOP unless 596 + * engine was active. 597 + */ 598 + octeon_i2c_stop(i2c); 599 + 600 + octeon_i2c_write_int(i2c, 0); 601 + } 602 + 603 + static void octeon_i2c_unprepare_recovery(struct i2c_adapter *adap) 604 + { 605 + struct octeon_i2c *i2c = i2c_get_adapdata(adap); 606 + 607 + octeon_i2c_write_int(i2c, 0); 608 + } 609 + 610 + static struct i2c_bus_recovery_info octeon_i2c_recovery_info = { 611 + .recover_bus = i2c_generic_scl_recovery, 612 + .get_scl = octeon_i2c_get_scl, 613 + .set_scl = octeon_i2c_set_scl, 614 + .get_sda = octeon_i2c_get_sda, 615 + .prepare_recovery = octeon_i2c_prepare_recovery, 616 + .unprepare_recovery = octeon_i2c_unprepare_recovery, 617 + }; 542 618 543 619 static u32 octeon_i2c_functionality(struct i2c_adapter *adap) 544 620 { ··· 674 642 i2c->adap = octeon_i2c_ops; 675 643 i2c->adap.timeout = msecs_to_jiffies(2); 676 644 i2c->adap.retries = 5; 645 + i2c->adap.bus_recovery_info = &octeon_i2c_recovery_info; 677 646 i2c->adap.dev.parent = &pdev->dev; 678 647 i2c->adap.dev.of_node = node; 679 648 i2c_set_adapdata(&i2c->adap, i2c);