diff options
| -rw-r--r-- | drivers/i2c/busses/i2c-mxs.c | 176 |
1 files changed, 163 insertions, 13 deletions
diff --git a/drivers/i2c/busses/i2c-mxs.c b/drivers/i2c/busses/i2c-mxs.c index d6abaf2cf2e3..e55736077d23 100644 --- a/drivers/i2c/busses/i2c-mxs.c +++ b/drivers/i2c/busses/i2c-mxs.c | |||
| @@ -39,6 +39,7 @@ | |||
| 39 | #define MXS_I2C_CTRL0_SET (0x04) | 39 | #define MXS_I2C_CTRL0_SET (0x04) |
| 40 | 40 | ||
| 41 | #define MXS_I2C_CTRL0_SFTRST 0x80000000 | 41 | #define MXS_I2C_CTRL0_SFTRST 0x80000000 |
| 42 | #define MXS_I2C_CTRL0_RUN 0x20000000 | ||
| 42 | #define MXS_I2C_CTRL0_SEND_NAK_ON_LAST 0x02000000 | 43 | #define MXS_I2C_CTRL0_SEND_NAK_ON_LAST 0x02000000 |
| 43 | #define MXS_I2C_CTRL0_RETAIN_CLOCK 0x00200000 | 44 | #define MXS_I2C_CTRL0_RETAIN_CLOCK 0x00200000 |
| 44 | #define MXS_I2C_CTRL0_POST_SEND_STOP 0x00100000 | 45 | #define MXS_I2C_CTRL0_POST_SEND_STOP 0x00100000 |
| @@ -64,6 +65,13 @@ | |||
| 64 | #define MXS_I2C_CTRL1_SLAVE_STOP_IRQ 0x02 | 65 | #define MXS_I2C_CTRL1_SLAVE_STOP_IRQ 0x02 |
| 65 | #define MXS_I2C_CTRL1_SLAVE_IRQ 0x01 | 66 | #define MXS_I2C_CTRL1_SLAVE_IRQ 0x01 |
| 66 | 67 | ||
| 68 | #define MXS_I2C_DATA (0xa0) | ||
| 69 | |||
| 70 | #define MXS_I2C_DEBUG0 (0xb0) | ||
| 71 | #define MXS_I2C_DEBUG0_CLR (0xb8) | ||
| 72 | |||
| 73 | #define MXS_I2C_DEBUG0_DMAREQ 0x80000000 | ||
| 74 | |||
| 67 | #define MXS_I2C_IRQ_MASK (MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ | \ | 75 | #define MXS_I2C_IRQ_MASK (MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ | \ |
| 68 | MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ | \ | 76 | MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ | \ |
| 69 | MXS_I2C_CTRL1_EARLY_TERM_IRQ | \ | 77 | MXS_I2C_CTRL1_EARLY_TERM_IRQ | \ |
| @@ -298,6 +306,135 @@ write_init_pio_fail: | |||
| 298 | return -EINVAL; | 306 | return -EINVAL; |
| 299 | } | 307 | } |
| 300 | 308 | ||
| 309 | static int mxs_i2c_pio_wait_dmareq(struct mxs_i2c_dev *i2c) | ||
| 310 | { | ||
| 311 | unsigned long timeout = jiffies + msecs_to_jiffies(1000); | ||
| 312 | |||
| 313 | while (!(readl(i2c->regs + MXS_I2C_DEBUG0) & | ||
| 314 | MXS_I2C_DEBUG0_DMAREQ)) { | ||
| 315 | if (time_after(jiffies, timeout)) | ||
| 316 | return -ETIMEDOUT; | ||
| 317 | cond_resched(); | ||
| 318 | } | ||
| 319 | |||
| 320 | writel(MXS_I2C_DEBUG0_DMAREQ, i2c->regs + MXS_I2C_DEBUG0_CLR); | ||
| 321 | |||
| 322 | return 0; | ||
| 323 | } | ||
| 324 | |||
| 325 | static int mxs_i2c_pio_wait_cplt(struct mxs_i2c_dev *i2c) | ||
| 326 | { | ||
| 327 | unsigned long timeout = jiffies + msecs_to_jiffies(1000); | ||
| 328 | |||
| 329 | /* | ||
| 330 | * We do not use interrupts in the PIO mode. Due to the | ||
| 331 | * maximum transfer length being 8 bytes in PIO mode, the | ||
| 332 | * overhead of interrupt would be too large and this would | ||
| 333 | * neglect the gain from using the PIO mode. | ||
| 334 | */ | ||
| 335 | |||
| 336 | while (!(readl(i2c->regs + MXS_I2C_CTRL1) & | ||
| 337 | MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ)) { | ||
| 338 | if (time_after(jiffies, timeout)) | ||
| 339 | return -ETIMEDOUT; | ||
| 340 | cond_resched(); | ||
| 341 | } | ||
| 342 | |||
| 343 | writel(MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ, | ||
| 344 | i2c->regs + MXS_I2C_CTRL1_CLR); | ||
| 345 | |||
| 346 | return 0; | ||
| 347 | } | ||
| 348 | |||
| 349 | static int mxs_i2c_pio_setup_xfer(struct i2c_adapter *adap, | ||
| 350 | struct i2c_msg *msg, uint32_t flags) | ||
| 351 | { | ||
| 352 | struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap); | ||
| 353 | uint32_t addr_data = msg->addr << 1; | ||
| 354 | uint32_t data = 0; | ||
| 355 | int i, shifts_left, ret; | ||
| 356 | |||
| 357 | /* Mute IRQs coming from this block. */ | ||
| 358 | writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_CLR); | ||
| 359 | |||
| 360 | if (msg->flags & I2C_M_RD) { | ||
| 361 | addr_data |= I2C_SMBUS_READ; | ||
| 362 | |||
| 363 | /* SELECT command. */ | ||
| 364 | writel(MXS_I2C_CTRL0_RUN | MXS_CMD_I2C_SELECT, | ||
| 365 | i2c->regs + MXS_I2C_CTRL0); | ||
| 366 | |||
| 367 | ret = mxs_i2c_pio_wait_dmareq(i2c); | ||
| 368 | if (ret) | ||
| 369 | return ret; | ||
| 370 | |||
| 371 | writel(addr_data, i2c->regs + MXS_I2C_DATA); | ||
| 372 | |||
| 373 | ret = mxs_i2c_pio_wait_cplt(i2c); | ||
| 374 | if (ret) | ||
| 375 | return ret; | ||
| 376 | |||
| 377 | /* READ command. */ | ||
| 378 | writel(MXS_I2C_CTRL0_RUN | MXS_CMD_I2C_READ | flags | | ||
| 379 | MXS_I2C_CTRL0_XFER_COUNT(msg->len), | ||
| 380 | i2c->regs + MXS_I2C_CTRL0); | ||
| 381 | |||
| 382 | for (i = 0; i < msg->len; i++) { | ||
| 383 | if ((i & 3) == 0) { | ||
| 384 | ret = mxs_i2c_pio_wait_dmareq(i2c); | ||
| 385 | if (ret) | ||
| 386 | return ret; | ||
| 387 | data = readl(i2c->regs + MXS_I2C_DATA); | ||
| 388 | } | ||
| 389 | msg->buf[i] = data & 0xff; | ||
| 390 | data >>= 8; | ||
| 391 | } | ||
| 392 | } else { | ||
| 393 | addr_data |= I2C_SMBUS_WRITE; | ||
| 394 | |||
| 395 | /* WRITE command. */ | ||
| 396 | writel(MXS_I2C_CTRL0_RUN | MXS_CMD_I2C_WRITE | flags | | ||
| 397 | MXS_I2C_CTRL0_XFER_COUNT(msg->len + 1), | ||
| 398 | i2c->regs + MXS_I2C_CTRL0); | ||
| 399 | |||
| 400 | /* | ||
| 401 | * The LSB of data buffer is the first byte blasted across | ||
| 402 | * the bus. Higher order bytes follow. Thus the following | ||
| 403 | * filling schematic. | ||
| 404 | */ | ||
| 405 | data = addr_data << 24; | ||
| 406 | for (i = 0; i < msg->len; i++) { | ||
| 407 | data >>= 8; | ||
| 408 | data |= (msg->buf[i] << 24); | ||
| 409 | if ((i & 3) == 2) { | ||
| 410 | ret = mxs_i2c_pio_wait_dmareq(i2c); | ||
| 411 | if (ret) | ||
| 412 | return ret; | ||
| 413 | writel(data, i2c->regs + MXS_I2C_DATA); | ||
| 414 | } | ||
| 415 | } | ||
| 416 | |||
| 417 | shifts_left = 24 - (i & 3) * 8; | ||
| 418 | if (shifts_left) { | ||
| 419 | data >>= shifts_left; | ||
| 420 | ret = mxs_i2c_pio_wait_dmareq(i2c); | ||
| 421 | if (ret) | ||
| 422 | return ret; | ||
| 423 | writel(data, i2c->regs + MXS_I2C_DATA); | ||
| 424 | } | ||
| 425 | } | ||
| 426 | |||
| 427 | ret = mxs_i2c_pio_wait_cplt(i2c); | ||
| 428 | if (ret) | ||
| 429 | return ret; | ||
| 430 | |||
| 431 | /* Clear any dangling IRQs and re-enable interrupts. */ | ||
| 432 | writel(MXS_I2C_IRQ_MASK, i2c->regs + MXS_I2C_CTRL1_CLR); | ||
| 433 | writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET); | ||
| 434 | |||
| 435 | return 0; | ||
| 436 | } | ||
| 437 | |||
| 301 | /* | 438 | /* |
| 302 | * Low level master read/write transaction. | 439 | * Low level master read/write transaction. |
| 303 | */ | 440 | */ |
| @@ -316,24 +453,37 @@ static int mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, | |||
| 316 | if (msg->len == 0) | 453 | if (msg->len == 0) |
| 317 | return -EINVAL; | 454 | return -EINVAL; |
| 318 | 455 | ||
| 319 | INIT_COMPLETION(i2c->cmd_complete); | 456 | /* |
| 320 | i2c->cmd_err = 0; | 457 | * The current boundary to select between PIO/DMA transfer method |
| 321 | 458 | * is set to 8 bytes, transfers shorter than 8 bytes are transfered | |
| 322 | ret = mxs_i2c_dma_setup_xfer(adap, msg, flags); | 459 | * using PIO mode while longer transfers use DMA. The 8 byte border is |
| 323 | if (ret) | 460 | * based on this empirical measurement and a lot of previous frobbing. |
| 324 | return ret; | 461 | */ |
| 462 | if (msg->len < 8) { | ||
| 463 | ret = mxs_i2c_pio_setup_xfer(adap, msg, flags); | ||
| 464 | if (ret) | ||
| 465 | mxs_i2c_reset(i2c); | ||
| 466 | } else { | ||
| 467 | i2c->cmd_err = 0; | ||
| 468 | INIT_COMPLETION(i2c->cmd_complete); | ||
| 469 | ret = mxs_i2c_dma_setup_xfer(adap, msg, flags); | ||
| 470 | if (ret) | ||
| 471 | return ret; | ||
| 325 | 472 | ||
| 326 | ret = wait_for_completion_timeout(&i2c->cmd_complete, | 473 | ret = wait_for_completion_timeout(&i2c->cmd_complete, |
| 327 | msecs_to_jiffies(1000)); | 474 | msecs_to_jiffies(1000)); |
| 328 | if (ret == 0) | 475 | if (ret == 0) |
| 329 | goto timeout; | 476 | goto timeout; |
