i2c-at91-master.c 27 KB

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