i2c-at91-master.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * i2c Support for Atmel's AT91 Two-Wire Interface (TWI)
  4. *
  5. * Copyright (C) 2011 Weinmann Medical GmbH
  6. * Author: Nikolaus Voss <[email protected]>
  7. *
  8. * Evolved from original work by:
  9. * Copyright (C) 2004 Rick Bronson
  10. * Converted to 2.6 by Andrew Victor <[email protected]>
  11. *
  12. * Borrowed heavily from original work by:
  13. * Copyright (C) 2000 Philip Edelbrock <[email protected]>
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/completion.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/dmaengine.h>
  19. #include <linux/err.h>
  20. #include <linux/gpio/consumer.h>
  21. #include <linux/i2c.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/io.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/pinctrl/consumer.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/pm_runtime.h>
  29. #include "i2c-at91.h"
  30. void at91_init_twi_bus_master(struct at91_twi_dev *dev)
  31. {
  32. struct at91_twi_pdata *pdata = dev->pdata;
  33. u32 filtr = 0;
  34. /* FIFO should be enabled immediately after the software reset */
  35. if (dev->fifo_size)
  36. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_FIFOEN);
  37. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSEN);
  38. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SVDIS);
  39. at91_twi_write(dev, AT91_TWI_CWGR, dev->twi_cwgr_reg);
  40. /* enable digital filter */
  41. if (pdata->has_dig_filtr && dev->enable_dig_filt)
  42. filtr |= AT91_TWI_FILTR_FILT;
  43. /* enable advanced digital filter */
  44. if (pdata->has_adv_dig_filtr && dev->enable_dig_filt)
  45. filtr |= AT91_TWI_FILTR_FILT |
  46. (AT91_TWI_FILTR_THRES(dev->filter_width) &
  47. AT91_TWI_FILTR_THRES_MASK);
  48. /* enable analog filter */
  49. if (pdata->has_ana_filtr && dev->enable_ana_filt)
  50. filtr |= AT91_TWI_FILTR_PADFEN;
  51. if (filtr)
  52. at91_twi_write(dev, AT91_TWI_FILTR, filtr);
  53. }
  54. /*
  55. * Calculate symmetric clock as stated in datasheet:
  56. * twi_clk = F_MAIN / (2 * (cdiv * (1 << ckdiv) + offset))
  57. */
  58. static void at91_calc_twi_clock(struct at91_twi_dev *dev)
  59. {
  60. int ckdiv, cdiv, div, hold = 0, filter_width = 0;
  61. struct at91_twi_pdata *pdata = dev->pdata;
  62. int offset = pdata->clk_offset;
  63. int max_ckdiv = pdata->clk_max_div;
  64. struct i2c_timings timings, *t = &timings;
  65. i2c_parse_fw_timings(dev->dev, t, true);
  66. div = max(0, (int)DIV_ROUND_UP(clk_get_rate(dev->clk),
  67. 2 * t->bus_freq_hz) - offset);
  68. ckdiv = fls(div >> 8);
  69. cdiv = div >> ckdiv;
  70. if (ckdiv > max_ckdiv) {
  71. dev_warn(dev->dev, "%d exceeds ckdiv max value which is %d.\n",
  72. ckdiv, max_ckdiv);
  73. ckdiv = max_ckdiv;
  74. cdiv = 255;
  75. }
  76. if (pdata->has_hold_field) {
  77. /*
  78. * hold time = HOLD + 3 x T_peripheral_clock
  79. * Use clk rate in kHz to prevent overflows when computing
  80. * hold.
  81. */
  82. hold = DIV_ROUND_UP(t->sda_hold_ns
  83. * (clk_get_rate(dev->clk) / 1000), 1000000);
  84. hold -= 3;
  85. if (hold < 0)
  86. hold = 0;
  87. if (hold > AT91_TWI_CWGR_HOLD_MAX) {
  88. dev_warn(dev->dev,
  89. "HOLD field set to its maximum value (%d instead of %d)\n",
  90. AT91_TWI_CWGR_HOLD_MAX, hold);
  91. hold = AT91_TWI_CWGR_HOLD_MAX;
  92. }
  93. }
  94. if (pdata->has_adv_dig_filtr) {
  95. /*
  96. * filter width = 0 to AT91_TWI_FILTR_THRES_MAX
  97. * peripheral clocks
  98. */
  99. filter_width = DIV_ROUND_UP(t->digital_filter_width_ns
  100. * (clk_get_rate(dev->clk) / 1000), 1000000);
  101. if (filter_width > AT91_TWI_FILTR_THRES_MAX) {
  102. dev_warn(dev->dev,
  103. "Filter threshold set to its maximum value (%d instead of %d)\n",
  104. AT91_TWI_FILTR_THRES_MAX, filter_width);
  105. filter_width = AT91_TWI_FILTR_THRES_MAX;
  106. }
  107. }
  108. dev->twi_cwgr_reg = (ckdiv << 16) | (cdiv << 8) | cdiv
  109. | AT91_TWI_CWGR_HOLD(hold);
  110. dev->filter_width = filter_width;
  111. dev_dbg(dev->dev, "cdiv %d ckdiv %d hold %d (%d ns), filter_width %d (%d ns)\n",
  112. cdiv, ckdiv, hold, t->sda_hold_ns, filter_width,
  113. t->digital_filter_width_ns);
  114. }
  115. static void at91_twi_dma_cleanup(struct at91_twi_dev *dev)
  116. {
  117. struct at91_twi_dma *dma = &dev->dma;
  118. at91_twi_irq_save(dev);
  119. if (dma->xfer_in_progress) {
  120. if (dma->direction == DMA_FROM_DEVICE)
  121. dmaengine_terminate_sync(dma->chan_rx);
  122. else
  123. dmaengine_terminate_sync(dma->chan_tx);
  124. dma->xfer_in_progress = false;
  125. }
  126. if (dma->buf_mapped) {
  127. dma_unmap_single(dev->dev, sg_dma_address(&dma->sg[0]),
  128. dev->buf_len, dma->direction);
  129. dma->buf_mapped = false;
  130. }
  131. at91_twi_irq_restore(dev);
  132. }
  133. static void at91_twi_write_next_byte(struct at91_twi_dev *dev)
  134. {
  135. if (!dev->buf_len)
  136. return;
  137. /* 8bit write works with and without FIFO */
  138. writeb_relaxed(*dev->buf, dev->base + AT91_TWI_THR);
  139. /* send stop when last byte has been written */
  140. if (--dev->buf_len == 0) {
  141. if (!dev->use_alt_cmd)
  142. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
  143. at91_twi_write(dev, AT91_TWI_IDR, AT91_TWI_TXRDY);
  144. }
  145. dev_dbg(dev->dev, "wrote 0x%x, to go %zu\n", *dev->buf, dev->buf_len);
  146. ++dev->buf;
  147. }
  148. static void at91_twi_write_data_dma_callback(void *data)
  149. {
  150. struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
  151. dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg[0]),
  152. dev->buf_len, DMA_TO_DEVICE);
  153. /*
  154. * When this callback is called, THR/TX FIFO is likely not to be empty
  155. * yet. So we have to wait for TXCOMP or NACK bits to be set into the
  156. * Status Register to be sure that the STOP bit has been sent and the
  157. * transfer is completed. The NACK interrupt has already been enabled,
  158. * we just have to enable TXCOMP one.
  159. */
  160. at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
  161. if (!dev->use_alt_cmd)
  162. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
  163. }
  164. static void at91_twi_write_data_dma(struct at91_twi_dev *dev)
  165. {
  166. dma_addr_t dma_addr;
  167. struct dma_async_tx_descriptor *txdesc;
  168. struct at91_twi_dma *dma = &dev->dma;
  169. struct dma_chan *chan_tx = dma->chan_tx;
  170. unsigned int sg_len = 1;
  171. if (!dev->buf_len)
  172. return;
  173. dma->direction = DMA_TO_DEVICE;
  174. at91_twi_irq_save(dev);
  175. dma_addr = dma_map_single(dev->dev, dev->buf, dev->buf_len,
  176. DMA_TO_DEVICE);
  177. if (dma_mapping_error(dev->dev, dma_addr)) {
  178. dev_err(dev->dev, "dma map failed\n");
  179. return;
  180. }
  181. dma->buf_mapped = true;
  182. at91_twi_irq_restore(dev);
  183. if (dev->fifo_size) {
  184. size_t part1_len, part2_len;
  185. struct scatterlist *sg;
  186. unsigned fifo_mr;
  187. sg_len = 0;
  188. part1_len = dev->buf_len & ~0x3;
  189. if (part1_len) {
  190. sg = &dma->sg[sg_len++];
  191. sg_dma_len(sg) = part1_len;
  192. sg_dma_address(sg) = dma_addr;
  193. }
  194. part2_len = dev->buf_len & 0x3;
  195. if (part2_len) {
  196. sg = &dma->sg[sg_len++];
  197. sg_dma_len(sg) = part2_len;
  198. sg_dma_address(sg) = dma_addr + part1_len;
  199. }
  200. /*
  201. * DMA controller is triggered when at least 4 data can be
  202. * written into the TX FIFO
  203. */
  204. fifo_mr = at91_twi_read(dev, AT91_TWI_FMR);
  205. fifo_mr &= ~AT91_TWI_FMR_TXRDYM_MASK;
  206. fifo_mr |= AT91_TWI_FMR_TXRDYM(AT91_TWI_FOUR_DATA);
  207. at91_twi_write(dev, AT91_TWI_FMR, fifo_mr);
  208. } else {
  209. sg_dma_len(&dma->sg[0]) = dev->buf_len;
  210. sg_dma_address(&dma->sg[0]) = dma_addr;
  211. }
  212. txdesc = dmaengine_prep_slave_sg(chan_tx, dma->sg, sg_len,
  213. DMA_MEM_TO_DEV,
  214. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  215. if (!txdesc) {
  216. dev_err(dev->dev, "dma prep slave sg failed\n");
  217. goto error;
  218. }
  219. txdesc->callback = at91_twi_write_data_dma_callback;
  220. txdesc->callback_param = dev;
  221. dma->xfer_in_progress = true;
  222. dmaengine_submit(txdesc);
  223. dma_async_issue_pending(chan_tx);
  224. return;
  225. error:
  226. at91_twi_dma_cleanup(dev);
  227. }
  228. static void at91_twi_read_next_byte(struct at91_twi_dev *dev)
  229. {
  230. /*
  231. * If we are in this case, it means there is garbage data in RHR, so
  232. * delete them.
  233. */
  234. if (!dev->buf_len) {
  235. at91_twi_read(dev, AT91_TWI_RHR);
  236. return;
  237. }
  238. /* 8bit read works with and without FIFO */
  239. *dev->buf = readb_relaxed(dev->base + AT91_TWI_RHR);
  240. --dev->buf_len;
  241. /* return if aborting, we only needed to read RHR to clear RXRDY*/
  242. if (dev->recv_len_abort)
  243. return;
  244. /* handle I2C_SMBUS_BLOCK_DATA */
  245. if (unlikely(dev->msg->flags & I2C_M_RECV_LEN)) {
  246. /* ensure length byte is a valid value */
  247. if (*dev->buf <= I2C_SMBUS_BLOCK_MAX && *dev->buf > 0) {
  248. dev->msg->flags &= ~I2C_M_RECV_LEN;
  249. dev->buf_len += *dev->buf;
  250. dev->msg->len = dev->buf_len + 1;
  251. dev_dbg(dev->dev, "received block length %zu\n",
  252. dev->buf_len);
  253. } else {
  254. /* abort and send the stop by reading one more byte */
  255. dev->recv_len_abort = true;
  256. dev->buf_len = 1;
  257. }
  258. }
  259. /* send stop if second but last byte has been read */
  260. if (!dev->use_alt_cmd && dev->buf_len == 1)
  261. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
  262. dev_dbg(dev->dev, "read 0x%x, to go %zu\n", *dev->buf, dev->buf_len);
  263. ++dev->buf;
  264. }
  265. static void at91_twi_read_data_dma_callback(void *data)
  266. {
  267. struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
  268. unsigned ier = AT91_TWI_TXCOMP;
  269. dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg[0]),
  270. dev->buf_len, DMA_FROM_DEVICE);
  271. if (!dev->use_alt_cmd) {
  272. /* The last two bytes have to be read without using dma */
  273. dev->buf += dev->buf_len - 2;
  274. dev->buf_len = 2;
  275. ier |= AT91_TWI_RXRDY;
  276. }
  277. at91_twi_write(dev, AT91_TWI_IER, ier);
  278. }
  279. static void at91_twi_read_data_dma(struct at91_twi_dev *dev)
  280. {
  281. dma_addr_t dma_addr;
  282. struct dma_async_tx_descriptor *rxdesc;
  283. struct at91_twi_dma *dma = &dev->dma;
  284. struct dma_chan *chan_rx = dma->chan_rx;
  285. size_t buf_len;
  286. buf_len = (dev->use_alt_cmd) ? dev->buf_len : dev->buf_len - 2;
  287. dma->direction = DMA_FROM_DEVICE;
  288. /* Keep in mind that we won't use dma to read the last two bytes */
  289. at91_twi_irq_save(dev);
  290. dma_addr = dma_map_single(dev->dev, dev->buf, buf_len, DMA_FROM_DEVICE);
  291. if (dma_mapping_error(dev->dev, dma_addr)) {
  292. dev_err(dev->dev, "dma map failed\n");
  293. return;
  294. }
  295. dma->buf_mapped = true;
  296. at91_twi_irq_restore(dev);
  297. if (dev->fifo_size && IS_ALIGNED(buf_len, 4)) {
  298. unsigned fifo_mr;
  299. /*
  300. * DMA controller is triggered when at least 4 data can be
  301. * read from the RX FIFO
  302. */
  303. fifo_mr = at91_twi_read(dev, AT91_TWI_FMR);
  304. fifo_mr &= ~AT91_TWI_FMR_RXRDYM_MASK;
  305. fifo_mr |= AT91_TWI_FMR_RXRDYM(AT91_TWI_FOUR_DATA);
  306. at91_twi_write(dev, AT91_TWI_FMR, fifo_mr);
  307. }
  308. sg_dma_len(&dma->sg[0]) = buf_len;
  309. sg_dma_address(&dma->sg[0]) = dma_addr;
  310. rxdesc = dmaengine_prep_slave_sg(chan_rx, dma->sg, 1, DMA_DEV_TO_MEM,
  311. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  312. if (!rxdesc) {
  313. dev_err(dev->dev, "dma prep slave sg failed\n");
  314. goto error;
  315. }
  316. rxdesc->callback = at91_twi_read_data_dma_callback;
  317. rxdesc->callback_param = dev;
  318. dma->xfer_in_progress = true;
  319. dmaengine_submit(rxdesc);
  320. dma_async_issue_pending(dma->chan_rx);
  321. return;
  322. error:
  323. at91_twi_dma_cleanup(dev);
  324. }
  325. static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id)
  326. {
  327. struct at91_twi_dev *dev = dev_id;
  328. const unsigned status = at91_twi_read(dev, AT91_TWI_SR);
  329. const unsigned irqstatus = status & at91_twi_read(dev, AT91_TWI_IMR);
  330. if (!irqstatus)
  331. return IRQ_NONE;
  332. /*
  333. * In reception, the behavior of the twi device (before sama5d2) is
  334. * weird. There is some magic about RXRDY flag! When a data has been
  335. * almost received, the reception of a new one is anticipated if there
  336. * is no stop command to send. That is the reason why ask for sending
  337. * the stop command not on the last data but on the second last one.
  338. *
  339. * Unfortunately, we could still have the RXRDY flag set even if the
  340. * transfer is done and we have read the last data. It might happen
  341. * when the i2c slave device sends too quickly data after receiving the
  342. * ack from the master. The data has been almost received before having
  343. * the order to send stop. In this case, sending the stop command could
  344. * cause a RXRDY interrupt with a TXCOMP one. It is better to manage
  345. * the RXRDY interrupt first in order to not keep garbage data in the
  346. * Receive Holding Register for the next transfer.
  347. */
  348. if (irqstatus & AT91_TWI_RXRDY) {
  349. /*
  350. * Read all available bytes at once by polling RXRDY usable w/
  351. * and w/o FIFO. With FIFO enabled we could also read RXFL and
  352. * avoid polling RXRDY.
  353. */
  354. do {
  355. at91_twi_read_next_byte(dev);
  356. } while (at91_twi_read(dev, AT91_TWI_SR) & AT91_TWI_RXRDY);
  357. }
  358. /*
  359. * When a NACK condition is detected, the I2C controller sets the NACK,
  360. * TXCOMP and TXRDY bits all together in the Status Register (SR).
  361. *
  362. * 1 - Handling NACK errors with CPU write transfer.
  363. *
  364. * In such case, we should not write the next byte into the Transmit
  365. * Holding Register (THR) otherwise the I2C controller would start a new
  366. * transfer and the I2C slave is likely to reply by another NACK.
  367. *
  368. * 2 - Handling NACK errors with DMA write transfer.
  369. *
  370. * By setting the TXRDY bit in the SR, the I2C controller also triggers
  371. * the DMA controller to write the next data into the THR. Then the
  372. * result depends on the hardware version of the I2C controller.
  373. *
  374. * 2a - Without support of the Alternative Command mode.
  375. *
  376. * This is the worst case: the DMA controller is triggered to write the
  377. * next data into the THR, hence starting a new transfer: the I2C slave
  378. * is likely to reply by another NACK.
  379. * Concurrently, this interrupt handler is likely to be called to manage
  380. * the first NACK before the I2C controller detects the second NACK and
  381. * sets once again the NACK bit into the SR.
  382. * When handling the first NACK, this interrupt handler disables the I2C
  383. * controller interruptions, especially the NACK interrupt.
  384. * Hence, the NACK bit is pending into the SR. This is why we should
  385. * read the SR to clear all pending interrupts at the beginning of
  386. * at91_do_twi_transfer() before actually starting a new transfer.
  387. *
  388. * 2b - With support of the Alternative Command mode.
  389. *
  390. * When a NACK condition is detected, the I2C controller also locks the
  391. * THR (and sets the LOCK bit in the SR): even though the DMA controller
  392. * is triggered by the TXRDY bit to write the next data into the THR,
  393. * this data actually won't go on the I2C bus hence a second NACK is not
  394. * generated.
  395. */
  396. if (irqstatus & (AT91_TWI_TXCOMP | AT91_TWI_NACK)) {
  397. at91_disable_twi_interrupts(dev);
  398. complete(&dev->cmd_complete);
  399. } else if (irqstatus & AT91_TWI_TXRDY) {
  400. at91_twi_write_next_byte(dev);
  401. }
  402. /* catch error flags */
  403. dev->transfer_status |= status;
  404. return IRQ_HANDLED;
  405. }
  406. static int at91_do_twi_transfer(struct at91_twi_dev *dev)
  407. {
  408. int ret;
  409. unsigned long time_left;
  410. bool has_unre_flag = dev->pdata->has_unre_flag;
  411. bool has_alt_cmd = dev->pdata->has_alt_cmd;
  412. /*
  413. * WARNING: the TXCOMP bit in the Status Register is NOT a clear on
  414. * read flag but shows the state of the transmission at the time the
  415. * Status Register is read. According to the programmer datasheet,
  416. * TXCOMP is set when both holding register and internal shifter are
  417. * empty and STOP condition has been sent.
  418. * Consequently, we should enable NACK interrupt rather than TXCOMP to
  419. * detect transmission failure.
  420. * Indeed let's take the case of an i2c write command using DMA.
  421. * Whenever the slave doesn't acknowledge a byte, the LOCK, NACK and
  422. * TXCOMP bits are set together into the Status Register.
  423. * LOCK is a clear on write bit, which is set to prevent the DMA
  424. * controller from sending new data on the i2c bus after a NACK
  425. * condition has happened. Once locked, this i2c peripheral stops
  426. * triggering the DMA controller for new data but it is more than
  427. * likely that a new DMA transaction is already in progress, writing
  428. * into the Transmit Holding Register. Since the peripheral is locked,
  429. * these new data won't be sent to the i2c bus but they will remain
  430. * into the Transmit Holding Register, so TXCOMP bit is cleared.
  431. * Then when the interrupt handler is called, the Status Register is
  432. * read: the TXCOMP bit is clear but NACK bit is still set. The driver
  433. * manage the error properly, without waiting for timeout.
  434. * This case can be reproduced easyly when writing into an at24 eeprom.
  435. *
  436. * Besides, the TXCOMP bit is already set before the i2c transaction
  437. * has been started. For read transactions, this bit is cleared when
  438. * writing the START bit into the Control Register. So the
  439. * corresponding interrupt can safely be enabled just after.
  440. * However for write transactions managed by the CPU, we first write
  441. * into THR, so TXCOMP is cleared. Then we can safely enable TXCOMP
  442. * interrupt. If TXCOMP interrupt were enabled before writing into THR,
  443. * the interrupt handler would be called immediately and the i2c command
  444. * would be reported as completed.
  445. * Also when a write transaction is managed by the DMA controller,
  446. * enabling the TXCOMP interrupt in this function may lead to a race
  447. * condition since we don't know whether the TXCOMP interrupt is enabled
  448. * before or after the DMA has started to write into THR. So the TXCOMP
  449. * interrupt is enabled later by at91_twi_write_data_dma_callback().
  450. * Immediately after in that DMA callback, if the alternative command
  451. * mode is not used, we still need to send the STOP condition manually
  452. * writing the corresponding bit into the Control Register.
  453. */
  454. dev_dbg(dev->dev, "transfer: %s %zu bytes.\n",
  455. (dev->msg->flags & I2C_M_RD) ? "read" : "write", dev->buf_len);
  456. reinit_completion(&dev->cmd_complete);
  457. dev->transfer_status = 0;
  458. /* Clear pending interrupts, such as NACK. */
  459. at91_twi_read(dev, AT91_TWI_SR);
  460. if (dev->fifo_size) {
  461. unsigned fifo_mr = at91_twi_read(dev, AT91_TWI_FMR);
  462. /* Reset FIFO mode register */
  463. fifo_mr &= ~(AT91_TWI_FMR_TXRDYM_MASK |
  464. AT91_TWI_FMR_RXRDYM_MASK);
  465. fifo_mr |= AT91_TWI_FMR_TXRDYM(AT91_TWI_ONE_DATA);
  466. fifo_mr |= AT91_TWI_FMR_RXRDYM(AT91_TWI_ONE_DATA);
  467. at91_twi_write(dev, AT91_TWI_FMR, fifo_mr);
  468. /* Flush FIFOs */
  469. at91_twi_write(dev, AT91_TWI_CR,
  470. AT91_TWI_THRCLR | AT91_TWI_RHRCLR);
  471. }
  472. if (!dev->buf_len) {
  473. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_QUICK);
  474. at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
  475. } else if (dev->msg->flags & I2C_M_RD) {
  476. unsigned start_flags = AT91_TWI_START;
  477. /* if only one byte is to be read, immediately stop transfer */
  478. if (!dev->use_alt_cmd && dev->buf_len <= 1 &&
  479. !(dev->msg->flags & I2C_M_RECV_LEN))
  480. start_flags |= AT91_TWI_STOP;
  481. at91_twi_write(dev, AT91_TWI_CR, start_flags);
  482. /*
  483. * When using dma without alternative command mode, the last
  484. * byte has to be read manually in order to not send the stop
  485. * command too late and then to receive extra data.
  486. * In practice, there are some issues if you use the dma to
  487. * read n-1 bytes because of latency.
  488. * Reading n-2 bytes with dma and the two last ones manually
  489. * seems to be the best solution.
  490. */
  491. if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) {
  492. at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_NACK);
  493. at91_twi_read_data_dma(dev);
  494. } else {
  495. at91_twi_write(dev, AT91_TWI_IER,
  496. AT91_TWI_TXCOMP |
  497. AT91_TWI_NACK |
  498. AT91_TWI_RXRDY);
  499. }
  500. } else {
  501. if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) {
  502. at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_NACK);
  503. at91_twi_write_data_dma(dev);
  504. } else {
  505. at91_twi_write_next_byte(dev);
  506. at91_twi_write(dev, AT91_TWI_IER,
  507. AT91_TWI_TXCOMP | AT91_TWI_NACK |
  508. (dev->buf_len ? AT91_TWI_TXRDY : 0));
  509. }
  510. }
  511. time_left = wait_for_completion_timeout(&dev->cmd_complete,
  512. dev->adapter.timeout);
  513. if (time_left == 0) {
  514. dev->transfer_status |= at91_twi_read(dev, AT91_TWI_SR);
  515. dev_err(dev->dev, "controller timed out\n");
  516. at91_init_twi_bus(dev);
  517. ret = -ETIMEDOUT;
  518. goto error;
  519. }
  520. if (dev->transfer_status & AT91_TWI_NACK) {
  521. dev_dbg(dev->dev, "received nack\n");
  522. ret = -EREMOTEIO;
  523. goto error;
  524. }
  525. if (dev->transfer_status & AT91_TWI_OVRE) {
  526. dev_err(dev->dev, "overrun while reading\n");
  527. ret = -EIO;
  528. goto error;
  529. }
  530. if (has_unre_flag && dev->transfer_status & AT91_TWI_UNRE) {
  531. dev_err(dev->dev, "underrun while writing\n");
  532. ret = -EIO;
  533. goto error;
  534. }
  535. if ((has_alt_cmd || dev->fifo_size) &&
  536. (dev->transfer_status & AT91_TWI_LOCK)) {
  537. dev_err(dev->dev, "tx locked\n");
  538. ret = -EIO;
  539. goto error;
  540. }
  541. if (dev->recv_len_abort) {
  542. dev_err(dev->dev, "invalid smbus block length recvd\n");
  543. ret = -EPROTO;
  544. goto error;
  545. }
  546. dev_dbg(dev->dev, "transfer complete\n");
  547. return 0;
  548. error:
  549. /* first stop DMA transfer if still in progress */
  550. at91_twi_dma_cleanup(dev);
  551. /* then flush THR/FIFO and unlock TX if locked */
  552. if ((has_alt_cmd || dev->fifo_size) &&
  553. (dev->transfer_status & AT91_TWI_LOCK)) {
  554. dev_dbg(dev->dev, "unlock tx\n");
  555. at91_twi_write(dev, AT91_TWI_CR,
  556. AT91_TWI_THRCLR | AT91_TWI_LOCKCLR);
  557. }
  558. /*
  559. * some faulty I2C slave devices might hold SDA down;
  560. * we can send a bus clear command, hoping that the pins will be
  561. * released
  562. */
  563. i2c_recover_bus(&dev->adapter);
  564. return ret;
  565. }
  566. static int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num)
  567. {
  568. struct at91_twi_dev *dev = i2c_get_adapdata(adap);
  569. int ret;
  570. unsigned int_addr_flag = 0;
  571. struct i2c_msg *m_start = msg;
  572. bool is_read;
  573. u8 *dma_buf = NULL;
  574. dev_dbg(&adap->dev, "at91_xfer: processing %d messages:\n", num);
  575. ret = pm_runtime_get_sync(dev->dev);
  576. if (ret < 0)
  577. goto out;
  578. if (num == 2) {
  579. int internal_address = 0;
  580. int i;
  581. /* 1st msg is put into the internal address, start with 2nd */
  582. m_start = &msg[1];
  583. for (i = 0; i < msg->len; ++i) {
  584. const unsigned addr = msg->buf[msg->len - 1 - i];
  585. internal_address |= addr << (8 * i);
  586. int_addr_flag += AT91_TWI_IADRSZ_1;
  587. }
  588. at91_twi_write(dev, AT91_TWI_IADR, internal_address);
  589. }
  590. dev->use_alt_cmd = false;
  591. is_read = (m_start->flags & I2C_M_RD);
  592. if (dev->pdata->has_alt_cmd) {
  593. if (m_start->len > 0 &&
  594. m_start->len < AT91_I2C_MAX_ALT_CMD_DATA_SIZE) {
  595. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_ACMEN);
  596. at91_twi_write(dev, AT91_TWI_ACR,
  597. AT91_TWI_ACR_DATAL(m_start->len) |
  598. ((is_read) ? AT91_TWI_ACR_DIR : 0));
  599. dev->use_alt_cmd = true;
  600. } else {
  601. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_ACMDIS);
  602. }
  603. }
  604. at91_twi_write(dev, AT91_TWI_MMR,
  605. (m_start->addr << 16) |
  606. int_addr_flag |
  607. ((!dev->use_alt_cmd && is_read) ? AT91_TWI_MREAD : 0));
  608. dev->buf_len = m_start->len;
  609. dev->buf = m_start->buf;
  610. dev->msg = m_start;
  611. dev->recv_len_abort = false;
  612. if (dev->use_dma) {
  613. dma_buf = i2c_get_dma_safe_msg_buf(m_start, 1);
  614. if (!dma_buf) {
  615. ret = -ENOMEM;
  616. goto out;
  617. }
  618. dev->buf = dma_buf;
  619. }
  620. ret = at91_do_twi_transfer(dev);
  621. i2c_put_dma_safe_msg_buf(dma_buf, m_start, !ret);
  622. ret = (ret < 0) ? ret : num;
  623. out:
  624. pm_runtime_mark_last_busy(dev->dev);
  625. pm_runtime_put_autosuspend(dev->dev);
  626. return ret;
  627. }
  628. /*
  629. * The hardware can handle at most two messages concatenated by a
  630. * repeated start via it's internal address feature.
  631. */
  632. static const struct i2c_adapter_quirks at91_twi_quirks = {
  633. .flags = I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST | I2C_AQ_COMB_SAME_ADDR,
  634. .max_comb_1st_msg_len = 3,
  635. };
  636. static u32 at91_twi_func(struct i2c_adapter *adapter)
  637. {
  638. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
  639. | I2C_FUNC_SMBUS_READ_BLOCK_DATA;
  640. }
  641. static const struct i2c_algorithm at91_twi_algorithm = {
  642. .master_xfer = at91_twi_xfer,
  643. .functionality = at91_twi_func,
  644. };
  645. static int at91_twi_configure_dma(struct at91_twi_dev *dev, u32 phy_addr)
  646. {
  647. int ret = 0;
  648. struct dma_slave_config slave_config;
  649. struct at91_twi_dma *dma = &dev->dma;
  650. enum dma_slave_buswidth addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  651. /*
  652. * The actual width of the access will be chosen in
  653. * dmaengine_prep_slave_sg():
  654. * for each buffer in the scatter-gather list, if its size is aligned
  655. * to addr_width then addr_width accesses will be performed to transfer
  656. * the buffer. On the other hand, if the buffer size is not aligned to
  657. * addr_width then the buffer is transferred using single byte accesses.
  658. * Please refer to the Atmel eXtended DMA controller driver.
  659. * When FIFOs are used, the TXRDYM threshold can always be set to
  660. * trigger the XDMAC when at least 4 data can be written into the TX
  661. * FIFO, even if single byte accesses are performed.
  662. * However the RXRDYM threshold must be set to fit the access width,
  663. * deduced from buffer length, so the XDMAC is triggered properly to
  664. * read data from the RX FIFO.
  665. */
  666. if (dev->fifo_size)
  667. addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  668. memset(&slave_config, 0, sizeof(slave_config));
  669. slave_config.src_addr = (dma_addr_t)phy_addr + AT91_TWI_RHR;
  670. slave_config.src_addr_width = addr_width;
  671. slave_config.src_maxburst = 1;
  672. slave_config.dst_addr = (dma_addr_t)phy_addr + AT91_TWI_THR;
  673. slave_config.dst_addr_width = addr_width;
  674. slave_config.dst_maxburst = 1;
  675. slave_config.device_fc = false;
  676. dma->chan_tx = dma_request_chan(dev->dev, "tx");
  677. if (IS_ERR(dma->chan_tx)) {
  678. ret = PTR_ERR(dma->chan_tx);
  679. dma->chan_tx = NULL;
  680. goto error;
  681. }
  682. dma->chan_rx = dma_request_chan(dev->dev, "rx");
  683. if (IS_ERR(dma->chan_rx)) {
  684. ret = PTR_ERR(dma->chan_rx);
  685. dma->chan_rx = NULL;
  686. goto error;
  687. }
  688. slave_config.direction = DMA_MEM_TO_DEV;
  689. if (dmaengine_slave_config(dma->chan_tx, &slave_config)) {
  690. dev_err(dev->dev, "failed to configure tx channel\n");
  691. ret = -EINVAL;
  692. goto error;
  693. }
  694. slave_config.direction = DMA_DEV_TO_MEM;
  695. if (dmaengine_slave_config(dma->chan_rx, &slave_config)) {
  696. dev_err(dev->dev, "failed to configure rx channel\n");
  697. ret = -EINVAL;
  698. goto error;
  699. }
  700. sg_init_table(dma->sg, 2);
  701. dma->buf_mapped = false;
  702. dma->xfer_in_progress = false;
  703. dev->use_dma = true;
  704. dev_info(dev->dev, "using %s (tx) and %s (rx) for DMA transfers\n",
  705. dma_chan_name(dma->chan_tx), dma_chan_name(dma->chan_rx));
  706. return ret;
  707. error:
  708. if (ret != -EPROBE_DEFER)
  709. dev_info(dev->dev, "can't get DMA channel, continue without DMA support\n");
  710. if (dma->chan_rx)
  711. dma_release_channel(dma->chan_rx);
  712. if (dma->chan_tx)
  713. dma_release_channel(dma->chan_tx);
  714. return ret;
  715. }
  716. static int at91_init_twi_recovery_gpio(struct platform_device *pdev,
  717. struct at91_twi_dev *dev)
  718. {
  719. struct i2c_bus_recovery_info *rinfo = &dev->rinfo;
  720. rinfo->pinctrl = devm_pinctrl_get(&pdev->dev);
  721. if (!rinfo->pinctrl || IS_ERR(rinfo->pinctrl)) {
  722. dev_info(dev->dev, "can't get pinctrl, bus recovery not supported\n");
  723. return PTR_ERR(rinfo->pinctrl);
  724. }
  725. dev->adapter.bus_recovery_info = rinfo;
  726. return 0;
  727. }
  728. static int at91_twi_recover_bus_cmd(struct i2c_adapter *adap)
  729. {
  730. struct at91_twi_dev *dev = i2c_get_adapdata(adap);
  731. dev->transfer_status |= at91_twi_read(dev, AT91_TWI_SR);
  732. if (!(dev->transfer_status & AT91_TWI_SDA)) {
  733. dev_dbg(dev->dev, "SDA is down; sending bus clear command\n");
  734. if (dev->use_alt_cmd) {
  735. unsigned int acr;
  736. acr = at91_twi_read(dev, AT91_TWI_ACR);
  737. acr &= ~AT91_TWI_ACR_DATAL_MASK;
  738. at91_twi_write(dev, AT91_TWI_ACR, acr);
  739. }
  740. at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_CLEAR);
  741. }
  742. return 0;
  743. }
  744. static int at91_init_twi_recovery_info(struct platform_device *pdev,
  745. struct at91_twi_dev *dev)
  746. {
  747. struct i2c_bus_recovery_info *rinfo = &dev->rinfo;
  748. bool has_clear_cmd = dev->pdata->has_clear_cmd;
  749. if (!has_clear_cmd)
  750. return at91_init_twi_recovery_gpio(pdev, dev);
  751. rinfo->recover_bus = at91_twi_recover_bus_cmd;
  752. dev->adapter.bus_recovery_info = rinfo;
  753. return 0;
  754. }
  755. int at91_twi_probe_master(struct platform_device *pdev,
  756. u32 phy_addr, struct at91_twi_dev *dev)
  757. {
  758. int rc;
  759. init_completion(&dev->cmd_complete);
  760. rc = devm_request_irq(&pdev->dev, dev->irq, atmel_twi_interrupt, 0,
  761. dev_name(dev->dev), dev);
  762. if (rc) {
  763. dev_err(dev->dev, "Cannot get irq %d: %d\n", dev->irq, rc);
  764. return rc;
  765. }
  766. if (dev->dev->of_node) {
  767. rc = at91_twi_configure_dma(dev, phy_addr);
  768. if (rc == -EPROBE_DEFER)
  769. return rc;
  770. }
  771. if (!of_property_read_u32(pdev->dev.of_node, "atmel,fifo-size",
  772. &dev->fifo_size)) {
  773. dev_info(dev->dev, "Using FIFO (%u data)\n", dev->fifo_size);
  774. }
  775. dev->enable_dig_filt = of_property_read_bool(pdev->dev.of_node,
  776. "i2c-digital-filter");
  777. dev->enable_ana_filt = of_property_read_bool(pdev->dev.of_node,
  778. "i2c-analog-filter");
  779. at91_calc_twi_clock(dev);
  780. rc = at91_init_twi_recovery_info(pdev, dev);
  781. if (rc == -EPROBE_DEFER)
  782. return rc;
  783. dev->adapter.algo = &at91_twi_algorithm;
  784. dev->adapter.quirks = &at91_twi_quirks;
  785. return 0;
  786. }