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

i2c: octeon: Enable High-Level Controller

Use High-Level Controller (HLC) when possible. The HLC can read/write
up to 8 bytes and is completely optional. The most important difference
of the HLC is that it only requires one interrupt for a transfer
(up to 8 bytes) where the low-level read/write requires 2 interrupts
plus one interrupt per transferred byte. Since the interrupts are costly
using the HLC improves the performance. Also, the HLC provides improved
error handling.

Signed-off-by: David Daney <ddaney@caviumnetworks.com>
Signed-off-by: Jan Glauber <jglauber@cavium.com>
[wsa: fixed trivial checkpatch warnings]
Signed-off-by: Wolfram Sang <wsa@the-dreams.de>

authored by

David Daney and committed by
Wolfram Sang
d1fbff89 30c24b25

+337 -10
+337 -10
drivers/i2c/busses/i2c-octeon.c
··· 29 29 /* Register offsets */ 30 30 #define SW_TWSI 0x00 31 31 #define TWSI_INT 0x10 32 + #define SW_TWSI_EXT 0x18 32 33 33 34 /* Controller command patterns */ 34 35 #define SW_TWSI_V BIT_ULL(63) /* Valid bit */ 36 + #define SW_TWSI_EIA BIT_ULL(61) /* Extended internal address */ 35 37 #define SW_TWSI_R BIT_ULL(56) /* Result or read bit */ 38 + #define SW_TWSI_SOVR BIT_ULL(55) /* Size override */ 39 + #define SW_TWSI_SIZE_SHIFT 52 40 + #define SW_TWSI_ADDR_SHIFT 40 41 + #define SW_TWSI_IA_SHIFT 32 /* Internal address */ 36 42 37 43 /* Controller opcode word (bits 60:57) */ 38 44 #define SW_TWSI_OP_SHIFT 57 45 + #define SW_TWSI_OP_7 (0ULL << SW_TWSI_OP_SHIFT) 46 + #define SW_TWSI_OP_7_IA (1ULL << SW_TWSI_OP_SHIFT) 47 + #define SW_TWSI_OP_10 (2ULL << SW_TWSI_OP_SHIFT) 48 + #define SW_TWSI_OP_10_IA (3ULL << SW_TWSI_OP_SHIFT) 39 49 #define SW_TWSI_OP_TWSI_CLK (4ULL << SW_TWSI_OP_SHIFT) 40 50 #define SW_TWSI_OP_EOP (6ULL << SW_TWSI_OP_SHIFT) /* Extended opcode */ 41 51 ··· 58 48 #define SW_TWSI_EOP_TWSI_RST (SW_TWSI_OP_EOP | 7ULL << SW_TWSI_EOP_SHIFT) 59 49 60 50 /* Controller command and status bits */ 61 - #define TWSI_CTL_CE 0x80 51 + #define TWSI_CTL_CE 0x80 /* High level controller enable */ 62 52 #define TWSI_CTL_ENAB 0x40 /* Bus enable */ 63 53 #define TWSI_CTL_STA 0x20 /* Master-mode start, HW clears when done */ 64 54 #define TWSI_CTL_STP 0x10 /* Master-mode stop, HW clears when done */ ··· 97 87 #define STAT_IDLE 0xF8 98 88 99 89 /* TWSI_INT values */ 90 + #define TWSI_INT_ST_INT BIT_ULL(0) 91 + #define TWSI_INT_TS_INT BIT_ULL(1) 92 + #define TWSI_INT_CORE_INT BIT_ULL(2) 93 + #define TWSI_INT_ST_EN BIT_ULL(4) 94 + #define TWSI_INT_TS_EN BIT_ULL(5) 100 95 #define TWSI_INT_CORE_EN BIT_ULL(6) 101 96 #define TWSI_INT_SDA_OVR BIT_ULL(8) 102 97 #define TWSI_INT_SCL_OVR BIT_ULL(9) ··· 116 101 int sys_freq; 117 102 void __iomem *twsi_base; 118 103 struct device *dev; 104 + bool hlc_enabled; 119 105 }; 120 106 121 107 static void octeon_i2c_writeq_flush(u64 val, void __iomem *addr) ··· 214 198 { 215 199 /* clear TS/ST/IFLG events */ 216 200 octeon_i2c_write_int(i2c, 0); 201 + } 202 + 203 + /* 204 + * Cleanup low-level state & enable high-level controller. 205 + */ 206 + static void octeon_i2c_hlc_enable(struct octeon_i2c *i2c) 207 + { 208 + int try = 0; 209 + u64 val; 210 + 211 + if (i2c->hlc_enabled) 212 + return; 213 + i2c->hlc_enabled = true; 214 + 215 + while (1) { 216 + val = octeon_i2c_ctl_read(i2c); 217 + if (!(val & (TWSI_CTL_STA | TWSI_CTL_STP))) 218 + break; 219 + 220 + /* clear IFLG event */ 221 + if (val & TWSI_CTL_IFLG) 222 + octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB); 223 + 224 + if (try++ > 100) { 225 + pr_err("%s: giving up\n", __func__); 226 + break; 227 + } 228 + 229 + /* spin until any start/stop has finished */ 230 + udelay(10); 231 + } 232 + octeon_i2c_ctl_write(i2c, TWSI_CTL_CE | TWSI_CTL_AAK | TWSI_CTL_ENAB); 233 + } 234 + 235 + static void octeon_i2c_hlc_disable(struct octeon_i2c *i2c) 236 + { 237 + if (!i2c->hlc_enabled) 238 + return; 239 + 240 + i2c->hlc_enabled = false; 241 + octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB); 217 242 } 218 243 219 244 /* interrupt service routine */ ··· 356 299 } 357 300 } 358 301 302 + static bool octeon_i2c_hlc_test_ready(struct octeon_i2c *i2c) 303 + { 304 + u64 val = __raw_readq(i2c->twsi_base + SW_TWSI); 305 + 306 + return (val & SW_TWSI_V) == 0; 307 + } 308 + 309 + static void octeon_i2c_hlc_int_enable(struct octeon_i2c *i2c) 310 + { 311 + octeon_i2c_write_int(i2c, TWSI_INT_ST_EN); 312 + } 313 + 314 + static void octeon_i2c_hlc_int_clear(struct octeon_i2c *i2c) 315 + { 316 + /* clear ST/TS events, listen for neither */ 317 + octeon_i2c_write_int(i2c, TWSI_INT_ST_INT | TWSI_INT_TS_INT); 318 + } 319 + 320 + /** 321 + * octeon_i2c_hlc_wait - wait for an HLC operation to complete 322 + * @i2c: The struct octeon_i2c 323 + * 324 + * Returns 0 on success, otherwise -ETIMEDOUT. 325 + */ 326 + static int octeon_i2c_hlc_wait(struct octeon_i2c *i2c) 327 + { 328 + int time_left; 329 + 330 + octeon_i2c_hlc_int_enable(i2c); 331 + time_left = wait_event_timeout(i2c->queue, 332 + octeon_i2c_hlc_test_ready(i2c), 333 + i2c->adap.timeout); 334 + octeon_i2c_int_disable(i2c); 335 + if (!time_left) { 336 + octeon_i2c_hlc_int_clear(i2c); 337 + return -ETIMEDOUT; 338 + } 339 + return 0; 340 + } 341 + 342 + /* high-level-controller pure read of up to 8 bytes */ 343 + static int octeon_i2c_hlc_read(struct octeon_i2c *i2c, struct i2c_msg *msgs) 344 + { 345 + int i, j, ret = 0; 346 + u64 cmd; 347 + 348 + octeon_i2c_hlc_enable(i2c); 349 + octeon_i2c_hlc_int_clear(i2c); 350 + 351 + cmd = SW_TWSI_V | SW_TWSI_R | SW_TWSI_SOVR; 352 + /* SIZE */ 353 + cmd |= (u64)(msgs[0].len - 1) << SW_TWSI_SIZE_SHIFT; 354 + /* A */ 355 + cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT; 356 + 357 + if (msgs[0].flags & I2C_M_TEN) 358 + cmd |= SW_TWSI_OP_10; 359 + else 360 + cmd |= SW_TWSI_OP_7; 361 + 362 + octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI); 363 + ret = octeon_i2c_hlc_wait(i2c); 364 + if (ret) 365 + goto err; 366 + 367 + cmd = __raw_readq(i2c->twsi_base + SW_TWSI); 368 + if ((cmd & SW_TWSI_R) == 0) 369 + return -EAGAIN; 370 + 371 + for (i = 0, j = msgs[0].len - 1; i < msgs[0].len && i < 4; i++, j--) 372 + msgs[0].buf[j] = (cmd >> (8 * i)) & 0xff; 373 + 374 + if (msgs[0].len > 4) { 375 + cmd = __raw_readq(i2c->twsi_base + SW_TWSI_EXT); 376 + for (i = 0; i < msgs[0].len - 4 && i < 4; i++, j--) 377 + msgs[0].buf[j] = (cmd >> (8 * i)) & 0xff; 378 + } 379 + 380 + err: 381 + return ret; 382 + } 383 + 384 + /* high-level-controller pure write of up to 8 bytes */ 385 + static int octeon_i2c_hlc_write(struct octeon_i2c *i2c, struct i2c_msg *msgs) 386 + { 387 + int i, j, ret = 0; 388 + u64 cmd; 389 + 390 + octeon_i2c_hlc_enable(i2c); 391 + octeon_i2c_hlc_int_clear(i2c); 392 + 393 + cmd = SW_TWSI_V | SW_TWSI_SOVR; 394 + /* SIZE */ 395 + cmd |= (u64)(msgs[0].len - 1) << SW_TWSI_SIZE_SHIFT; 396 + /* A */ 397 + cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT; 398 + 399 + if (msgs[0].flags & I2C_M_TEN) 400 + cmd |= SW_TWSI_OP_10; 401 + else 402 + cmd |= SW_TWSI_OP_7; 403 + 404 + for (i = 0, j = msgs[0].len - 1; i < msgs[0].len && i < 4; i++, j--) 405 + cmd |= (u64)msgs[0].buf[j] << (8 * i); 406 + 407 + if (msgs[0].len > 4) { 408 + u64 ext = 0; 409 + 410 + for (i = 0; i < msgs[0].len - 4 && i < 4; i++, j--) 411 + ext |= (u64)msgs[0].buf[j] << (8 * i); 412 + octeon_i2c_writeq_flush(ext, i2c->twsi_base + SW_TWSI_EXT); 413 + } 414 + 415 + octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI); 416 + ret = octeon_i2c_hlc_wait(i2c); 417 + if (ret) 418 + goto err; 419 + 420 + cmd = __raw_readq(i2c->twsi_base + SW_TWSI); 421 + if ((cmd & SW_TWSI_R) == 0) 422 + return -EAGAIN; 423 + 424 + ret = octeon_i2c_check_status(i2c, false); 425 + 426 + err: 427 + return ret; 428 + } 429 + 430 + /* high-level-controller composite write+read, msg0=addr, msg1=data */ 431 + static int octeon_i2c_hlc_comp_read(struct octeon_i2c *i2c, struct i2c_msg *msgs) 432 + { 433 + int i, j, ret = 0; 434 + u64 cmd; 435 + 436 + octeon_i2c_hlc_enable(i2c); 437 + 438 + cmd = SW_TWSI_V | SW_TWSI_R | SW_TWSI_SOVR; 439 + /* SIZE */ 440 + cmd |= (u64)(msgs[1].len - 1) << SW_TWSI_SIZE_SHIFT; 441 + /* A */ 442 + cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT; 443 + 444 + if (msgs[0].flags & I2C_M_TEN) 445 + cmd |= SW_TWSI_OP_10_IA; 446 + else 447 + cmd |= SW_TWSI_OP_7_IA; 448 + 449 + if (msgs[0].len == 2) { 450 + u64 ext = 0; 451 + 452 + cmd |= SW_TWSI_EIA; 453 + ext = (u64)msgs[0].buf[0] << SW_TWSI_IA_SHIFT; 454 + cmd |= (u64)msgs[0].buf[1] << SW_TWSI_IA_SHIFT; 455 + octeon_i2c_writeq_flush(ext, i2c->twsi_base + SW_TWSI_EXT); 456 + } else { 457 + cmd |= (u64)msgs[0].buf[0] << SW_TWSI_IA_SHIFT; 458 + } 459 + 460 + octeon_i2c_hlc_int_clear(i2c); 461 + octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI); 462 + 463 + ret = octeon_i2c_hlc_wait(i2c); 464 + if (ret) 465 + goto err; 466 + 467 + cmd = __raw_readq(i2c->twsi_base + SW_TWSI); 468 + if ((cmd & SW_TWSI_R) == 0) 469 + return -EAGAIN; 470 + 471 + for (i = 0, j = msgs[1].len - 1; i < msgs[1].len && i < 4; i++, j--) 472 + msgs[1].buf[j] = (cmd >> (8 * i)) & 0xff; 473 + 474 + if (msgs[1].len > 4) { 475 + cmd = __raw_readq(i2c->twsi_base + SW_TWSI_EXT); 476 + for (i = 0; i < msgs[1].len - 4 && i < 4; i++, j--) 477 + msgs[1].buf[j] = (cmd >> (8 * i)) & 0xff; 478 + } 479 + 480 + err: 481 + return ret; 482 + } 483 + 484 + /* high-level-controller composite write+write, m[0]len<=2, m[1]len<=8 */ 485 + static int octeon_i2c_hlc_comp_write(struct octeon_i2c *i2c, struct i2c_msg *msgs) 486 + { 487 + bool set_ext = false; 488 + int i, j, ret = 0; 489 + u64 cmd, ext = 0; 490 + 491 + octeon_i2c_hlc_enable(i2c); 492 + 493 + cmd = SW_TWSI_V | SW_TWSI_SOVR; 494 + /* SIZE */ 495 + cmd |= (u64)(msgs[1].len - 1) << SW_TWSI_SIZE_SHIFT; 496 + /* A */ 497 + cmd |= (u64)(msgs[0].addr & 0x7full) << SW_TWSI_ADDR_SHIFT; 498 + 499 + if (msgs[0].flags & I2C_M_TEN) 500 + cmd |= SW_TWSI_OP_10_IA; 501 + else 502 + cmd |= SW_TWSI_OP_7_IA; 503 + 504 + if (msgs[0].len == 2) { 505 + cmd |= SW_TWSI_EIA; 506 + ext |= (u64)msgs[0].buf[0] << SW_TWSI_IA_SHIFT; 507 + set_ext = true; 508 + cmd |= (u64)msgs[0].buf[1] << SW_TWSI_IA_SHIFT; 509 + } else { 510 + cmd |= (u64)msgs[0].buf[0] << SW_TWSI_IA_SHIFT; 511 + } 512 + 513 + for (i = 0, j = msgs[1].len - 1; i < msgs[1].len && i < 4; i++, j--) 514 + cmd |= (u64)msgs[1].buf[j] << (8 * i); 515 + 516 + if (msgs[1].len > 4) { 517 + for (i = 0; i < msgs[1].len - 4 && i < 4; i++, j--) 518 + ext |= (u64)msgs[1].buf[j] << (8 * i); 519 + set_ext = true; 520 + } 521 + if (set_ext) 522 + octeon_i2c_writeq_flush(ext, i2c->twsi_base + SW_TWSI_EXT); 523 + 524 + octeon_i2c_hlc_int_clear(i2c); 525 + octeon_i2c_writeq_flush(cmd, i2c->twsi_base + SW_TWSI); 526 + 527 + ret = octeon_i2c_hlc_wait(i2c); 528 + if (ret) 529 + goto err; 530 + 531 + cmd = __raw_readq(i2c->twsi_base + SW_TWSI); 532 + if ((cmd & SW_TWSI_R) == 0) 533 + return -EAGAIN; 534 + 535 + ret = octeon_i2c_check_status(i2c, false); 536 + 537 + err: 538 + return ret; 539 + } 540 + 359 541 /* calculate and set clock divisors */ 360 542 static void octeon_i2c_set_clock(struct octeon_i2c *i2c) 361 543 { ··· 639 343 640 344 static int octeon_i2c_init_lowlevel(struct octeon_i2c *i2c) 641 345 { 642 - u8 status; 346 + u8 status = 0; 643 347 int tries; 644 - 645 - /* disable high level controller, enable bus access */ 646 - octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB); 647 348 648 349 /* reset controller */ 649 350 octeon_i2c_reg_write(i2c, SW_TWSI_EOP_TWSI_RST, 0); 650 351 651 - for (tries = 10; tries; tries--) { 352 + for (tries = 10; tries && status != STAT_IDLE; tries--) { 652 353 udelay(1); 653 354 status = octeon_i2c_stat_read(i2c); 654 355 if (status == STAT_IDLE) 655 - return 0; 356 + break; 656 357 } 657 - dev_err(i2c->dev, "%s: TWSI_RST failed! (0x%x)\n", __func__, status); 658 - return -EIO; 358 + 359 + if (status != STAT_IDLE) { 360 + dev_err(i2c->dev, "%s: TWSI_RST failed! (0x%x)\n", 361 + __func__, status); 362 + return -EIO; 363 + } 364 + 365 + /* toggle twice to force both teardowns */ 366 + octeon_i2c_hlc_enable(i2c); 367 + octeon_i2c_hlc_disable(i2c); 368 + return 0; 659 369 } 660 370 661 371 static int octeon_i2c_recovery(struct octeon_i2c *i2c) ··· 685 383 { 686 384 int ret; 687 385 u8 stat; 386 + 387 + octeon_i2c_hlc_disable(i2c); 688 388 689 389 octeon_i2c_ctl_write(i2c, TWSI_CTL_ENAB | TWSI_CTL_STA); 690 390 ret = octeon_i2c_wait(i2c); ··· 830 526 struct octeon_i2c *i2c = i2c_get_adapdata(adap); 831 527 int i, ret = 0; 832 528 529 + if (num == 1) { 530 + if (msgs[0].len > 0 && msgs[0].len <= 8) { 531 + if (msgs[0].flags & I2C_M_RD) 532 + ret = octeon_i2c_hlc_read(i2c, msgs); 533 + else 534 + ret = octeon_i2c_hlc_write(i2c, msgs); 535 + goto out; 536 + } 537 + } else if (num == 2) { 538 + if ((msgs[0].flags & I2C_M_RD) == 0 && 539 + (msgs[1].flags & I2C_M_RECV_LEN) == 0 && 540 + msgs[0].len > 0 && msgs[0].len <= 2 && 541 + msgs[1].len > 0 && msgs[1].len <= 8 && 542 + msgs[0].addr == msgs[1].addr) { 543 + if (msgs[1].flags & I2C_M_RD) 544 + ret = octeon_i2c_hlc_comp_read(i2c, msgs); 545 + else 546 + ret = octeon_i2c_hlc_comp_write(i2c, msgs); 547 + goto out; 548 + } 549 + } 550 + 833 551 for (i = 0; ret == 0 && i < num; i++) { 834 552 struct i2c_msg *pmsg = &msgs[i]; 835 553 ··· 867 541 pmsg->len); 868 542 } 869 543 octeon_i2c_stop(i2c); 870 - 544 + out: 871 545 return (ret != 0) ? ret : num; 872 546 } 873 547 ··· 906 580 */ 907 581 octeon_i2c_stop(i2c); 908 582 583 + octeon_i2c_hlc_disable(i2c); 909 584 octeon_i2c_write_int(i2c, 0); 910 585 } 911 586