i2c-mxs.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Freescale MXS I2C bus driver
  4. *
  5. * Copyright (C) 2012-2013 Marek Vasut <marex@denx.de>
  6. * Copyright (C) 2011-2012 Wolfram Sang, Pengutronix e.K.
  7. *
  8. * based on a (non-working) driver which was:
  9. *
  10. * Copyright (C) 2009-2010 Freescale Semiconductor, Inc. All Rights Reserved.
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/device.h>
  14. #include <linux/module.h>
  15. #include <linux/i2c.h>
  16. #include <linux/err.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/completion.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/io.h>
  22. #include <linux/stmp_device.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/dmaengine.h>
  27. #define DRIVER_NAME "mxs-i2c"
  28. #define MXS_I2C_CTRL0 (0x00)
  29. #define MXS_I2C_CTRL0_SET (0x04)
  30. #define MXS_I2C_CTRL0_CLR (0x08)
  31. #define MXS_I2C_CTRL0_SFTRST 0x80000000
  32. #define MXS_I2C_CTRL0_RUN 0x20000000
  33. #define MXS_I2C_CTRL0_SEND_NAK_ON_LAST 0x02000000
  34. #define MXS_I2C_CTRL0_PIO_MODE 0x01000000
  35. #define MXS_I2C_CTRL0_RETAIN_CLOCK 0x00200000
  36. #define MXS_I2C_CTRL0_POST_SEND_STOP 0x00100000
  37. #define MXS_I2C_CTRL0_PRE_SEND_START 0x00080000
  38. #define MXS_I2C_CTRL0_MASTER_MODE 0x00020000
  39. #define MXS_I2C_CTRL0_DIRECTION 0x00010000
  40. #define MXS_I2C_CTRL0_XFER_COUNT(v) ((v) & 0x0000FFFF)
  41. #define MXS_I2C_TIMING0 (0x10)
  42. #define MXS_I2C_TIMING1 (0x20)
  43. #define MXS_I2C_TIMING2 (0x30)
  44. #define MXS_I2C_CTRL1 (0x40)
  45. #define MXS_I2C_CTRL1_SET (0x44)
  46. #define MXS_I2C_CTRL1_CLR (0x48)
  47. #define MXS_I2C_CTRL1_CLR_GOT_A_NAK 0x10000000
  48. #define MXS_I2C_CTRL1_BUS_FREE_IRQ 0x80
  49. #define MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ 0x40
  50. #define MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ 0x20
  51. #define MXS_I2C_CTRL1_OVERSIZE_XFER_TERM_IRQ 0x10
  52. #define MXS_I2C_CTRL1_EARLY_TERM_IRQ 0x08
  53. #define MXS_I2C_CTRL1_MASTER_LOSS_IRQ 0x04
  54. #define MXS_I2C_CTRL1_SLAVE_STOP_IRQ 0x02
  55. #define MXS_I2C_CTRL1_SLAVE_IRQ 0x01
  56. #define MXS_I2C_STAT (0x50)
  57. #define MXS_I2C_STAT_GOT_A_NAK 0x10000000
  58. #define MXS_I2C_STAT_BUS_BUSY 0x00000800
  59. #define MXS_I2C_STAT_CLK_GEN_BUSY 0x00000400
  60. #define MXS_I2C_DATA(i2c) ((i2c->dev_type == MXS_I2C_V1) ? 0x60 : 0xa0)
  61. #define MXS_I2C_DEBUG0_CLR(i2c) ((i2c->dev_type == MXS_I2C_V1) ? 0x78 : 0xb8)
  62. #define MXS_I2C_DEBUG0_DMAREQ 0x80000000
  63. #define MXS_I2C_IRQ_MASK (MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ | \
  64. MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ | \
  65. MXS_I2C_CTRL1_EARLY_TERM_IRQ | \
  66. MXS_I2C_CTRL1_MASTER_LOSS_IRQ | \
  67. MXS_I2C_CTRL1_SLAVE_STOP_IRQ | \
  68. MXS_I2C_CTRL1_SLAVE_IRQ)
  69. #define MXS_CMD_I2C_SELECT (MXS_I2C_CTRL0_RETAIN_CLOCK | \
  70. MXS_I2C_CTRL0_PRE_SEND_START | \
  71. MXS_I2C_CTRL0_MASTER_MODE | \
  72. MXS_I2C_CTRL0_DIRECTION | \
  73. MXS_I2C_CTRL0_XFER_COUNT(1))
  74. #define MXS_CMD_I2C_WRITE (MXS_I2C_CTRL0_PRE_SEND_START | \
  75. MXS_I2C_CTRL0_MASTER_MODE | \
  76. MXS_I2C_CTRL0_DIRECTION)
  77. #define MXS_CMD_I2C_READ (MXS_I2C_CTRL0_SEND_NAK_ON_LAST | \
  78. MXS_I2C_CTRL0_MASTER_MODE)
  79. enum mxs_i2c_devtype {
  80. MXS_I2C_UNKNOWN = 0,
  81. MXS_I2C_V1,
  82. MXS_I2C_V2,
  83. };
  84. /**
  85. * struct mxs_i2c_dev - per device, private MXS-I2C data
  86. *
  87. * @dev: driver model device node
  88. * @dev_type: distinguish i.MX23/i.MX28 features
  89. * @regs: IO registers pointer
  90. * @cmd_complete: completion object for transaction wait
  91. * @cmd_err: error code for last transaction
  92. * @adapter: i2c subsystem adapter node
  93. */
  94. struct mxs_i2c_dev {
  95. struct device *dev;
  96. enum mxs_i2c_devtype dev_type;
  97. void __iomem *regs;
  98. struct completion cmd_complete;
  99. int cmd_err;
  100. struct i2c_adapter adapter;
  101. uint32_t timing0;
  102. uint32_t timing1;
  103. uint32_t timing2;
  104. /* DMA support components */
  105. struct dma_chan *dmach;
  106. uint32_t pio_data[2];
  107. uint32_t addr_data;
  108. struct scatterlist sg_io[2];
  109. bool dma_read;
  110. };
  111. static int mxs_i2c_reset(struct mxs_i2c_dev *i2c)
  112. {
  113. int ret = stmp_reset_block(i2c->regs);
  114. if (ret)
  115. return ret;
  116. /*
  117. * Configure timing for the I2C block. The I2C TIMING2 register has to
  118. * be programmed with this particular magic number. The rest is derived
  119. * from the XTAL speed and requested I2C speed.
  120. *
  121. * For details, see i.MX233 [25.4.2 - 25.4.4] and i.MX28 [27.5.2 - 27.5.4].
  122. */
  123. writel(i2c->timing0, i2c->regs + MXS_I2C_TIMING0);
  124. writel(i2c->timing1, i2c->regs + MXS_I2C_TIMING1);
  125. writel(i2c->timing2, i2c->regs + MXS_I2C_TIMING2);
  126. writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET);
  127. return 0;
  128. }
  129. static void mxs_i2c_dma_finish(struct mxs_i2c_dev *i2c)
  130. {
  131. if (i2c->dma_read) {
  132. dma_unmap_sg(i2c->dev, &i2c->sg_io[0], 1, DMA_TO_DEVICE);
  133. dma_unmap_sg(i2c->dev, &i2c->sg_io[1], 1, DMA_FROM_DEVICE);
  134. } else {
  135. dma_unmap_sg(i2c->dev, i2c->sg_io, 2, DMA_TO_DEVICE);
  136. }
  137. }
  138. static void mxs_i2c_dma_irq_callback(void *param)
  139. {
  140. struct mxs_i2c_dev *i2c = param;
  141. complete(&i2c->cmd_complete);
  142. mxs_i2c_dma_finish(i2c);
  143. }
  144. static int mxs_i2c_dma_setup_xfer(struct i2c_adapter *adap,
  145. struct i2c_msg *msg, uint32_t flags)
  146. {
  147. struct dma_async_tx_descriptor *desc;
  148. struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
  149. i2c->addr_data = i2c_8bit_addr_from_msg(msg);
  150. if (msg->flags & I2C_M_RD) {
  151. i2c->dma_read = true;
  152. /*
  153. * SELECT command.
  154. */
  155. /* Queue the PIO register write transfer. */
  156. i2c->pio_data[0] = MXS_CMD_I2C_SELECT;
  157. desc = dmaengine_prep_slave_sg(i2c->dmach,
  158. (struct scatterlist *)&i2c->pio_data[0],
  159. 1, DMA_TRANS_NONE, 0);
  160. if (!desc) {
  161. dev_err(i2c->dev,
  162. "Failed to get PIO reg. write descriptor.\n");
  163. goto select_init_pio_fail;
  164. }
  165. /* Queue the DMA data transfer. */
  166. sg_init_one(&i2c->sg_io[0], &i2c->addr_data, 1);
  167. dma_map_sg(i2c->dev, &i2c->sg_io[0], 1, DMA_TO_DEVICE);
  168. desc = dmaengine_prep_slave_sg(i2c->dmach, &i2c->sg_io[0], 1,
  169. DMA_MEM_TO_DEV,
  170. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  171. if (!desc) {
  172. dev_err(i2c->dev,
  173. "Failed to get DMA data write descriptor.\n");
  174. goto select_init_dma_fail;
  175. }
  176. /*
  177. * READ command.
  178. */
  179. /* Queue the PIO register write transfer. */
  180. i2c->pio_data[1] = flags | MXS_CMD_I2C_READ |
  181. MXS_I2C_CTRL0_XFER_COUNT(msg->len);
  182. desc = dmaengine_prep_slave_sg(i2c->dmach,
  183. (struct scatterlist *)&i2c->pio_data[1],
  184. 1, DMA_TRANS_NONE, DMA_PREP_INTERRUPT);
  185. if (!desc) {
  186. dev_err(i2c->dev,
  187. "Failed to get PIO reg. write descriptor.\n");
  188. goto select_init_dma_fail;
  189. }
  190. /* Queue the DMA data transfer. */
  191. sg_init_one(&i2c->sg_io[1], msg->buf, msg->len);
  192. dma_map_sg(i2c->dev, &i2c->sg_io[1], 1, DMA_FROM_DEVICE);
  193. desc = dmaengine_prep_slave_sg(i2c->dmach, &i2c->sg_io[1], 1,
  194. DMA_DEV_TO_MEM,
  195. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  196. if (!desc) {
  197. dev_err(i2c->dev,
  198. "Failed to get DMA data write descriptor.\n");
  199. goto read_init_dma_fail;
  200. }
  201. } else {
  202. i2c->dma_read = false;
  203. /*
  204. * WRITE command.
  205. */
  206. /* Queue the PIO register write transfer. */
  207. i2c->pio_data[0] = flags | MXS_CMD_I2C_WRITE |
  208. MXS_I2C_CTRL0_XFER_COUNT(msg->len + 1);
  209. desc = dmaengine_prep_slave_sg(i2c->dmach,
  210. (struct scatterlist *)&i2c->pio_data[0],
  211. 1, DMA_TRANS_NONE, 0);
  212. if (!desc) {
  213. dev_err(i2c->dev,
  214. "Failed to get PIO reg. write descriptor.\n");
  215. goto write_init_pio_fail;
  216. }
  217. /* Queue the DMA data transfer. */
  218. sg_init_table(i2c->sg_io, 2);
  219. sg_set_buf(&i2c->sg_io[0], &i2c->addr_data, 1);
  220. sg_set_buf(&i2c->sg_io[1], msg->buf, msg->len);
  221. dma_map_sg(i2c->dev, i2c->sg_io, 2, DMA_TO_DEVICE);
  222. desc = dmaengine_prep_slave_sg(i2c->dmach, i2c->sg_io, 2,
  223. DMA_MEM_TO_DEV,
  224. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  225. if (!desc) {
  226. dev_err(i2c->dev,
  227. "Failed to get DMA data write descriptor.\n");
  228. goto write_init_dma_fail;
  229. }
  230. }
  231. /*
  232. * The last descriptor must have this callback,
  233. * to finish the DMA transaction.
  234. */
  235. desc->callback = mxs_i2c_dma_irq_callback;
  236. desc->callback_param = i2c;
  237. /* Start the transfer. */
  238. dmaengine_submit(desc);
  239. dma_async_issue_pending(i2c->dmach);
  240. return 0;
  241. /* Read failpath. */
  242. read_init_dma_fail:
  243. dma_unmap_sg(i2c->dev, &i2c->sg_io[1], 1, DMA_FROM_DEVICE);
  244. select_init_dma_fail:
  245. dma_unmap_sg(i2c->dev, &i2c->sg_io[0], 1, DMA_TO_DEVICE);
  246. select_init_pio_fail:
  247. dmaengine_terminate_all(i2c->dmach);
  248. return -EINVAL;
  249. /* Write failpath. */
  250. write_init_dma_fail:
  251. dma_unmap_sg(i2c->dev, i2c->sg_io, 2, DMA_TO_DEVICE);
  252. write_init_pio_fail:
  253. dmaengine_terminate_all(i2c->dmach);
  254. return -EINVAL;
  255. }
  256. static int mxs_i2c_pio_wait_xfer_end(struct mxs_i2c_dev *i2c)
  257. {
  258. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  259. while (readl(i2c->regs + MXS_I2C_CTRL0) & MXS_I2C_CTRL0_RUN) {
  260. if (readl(i2c->regs + MXS_I2C_CTRL1) &
  261. MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ)
  262. return -ENXIO;
  263. if (time_after(jiffies, timeout))
  264. return -ETIMEDOUT;
  265. cond_resched();
  266. }
  267. return 0;
  268. }
  269. static int mxs_i2c_pio_check_error_state(struct mxs_i2c_dev *i2c)
  270. {
  271. u32 state;
  272. state = readl(i2c->regs + MXS_I2C_CTRL1_CLR) & MXS_I2C_IRQ_MASK;
  273. if (state & MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ)
  274. i2c->cmd_err = -ENXIO;
  275. else if (state & (MXS_I2C_CTRL1_EARLY_TERM_IRQ |
  276. MXS_I2C_CTRL1_MASTER_LOSS_IRQ |
  277. MXS_I2C_CTRL1_SLAVE_STOP_IRQ |
  278. MXS_I2C_CTRL1_SLAVE_IRQ))
  279. i2c->cmd_err = -EIO;
  280. return i2c->cmd_err;
  281. }
  282. static void mxs_i2c_pio_trigger_cmd(struct mxs_i2c_dev *i2c, u32 cmd)
  283. {
  284. u32 reg;
  285. writel(cmd, i2c->regs + MXS_I2C_CTRL0);
  286. /* readback makes sure the write is latched into hardware */
  287. reg = readl(i2c->regs + MXS_I2C_CTRL0);
  288. reg |= MXS_I2C_CTRL0_RUN;
  289. writel(reg, i2c->regs + MXS_I2C_CTRL0);
  290. }
  291. /*
  292. * Start WRITE transaction on the I2C bus. By studying i.MX23 datasheet,
  293. * CTRL0::PIO_MODE bit description clarifies the order in which the registers
  294. * must be written during PIO mode operation. First, the CTRL0 register has
  295. * to be programmed with all the necessary bits but the RUN bit. Then the
  296. * payload has to be written into the DATA register. Finally, the transmission
  297. * is executed by setting the RUN bit in CTRL0.
  298. */
  299. static void mxs_i2c_pio_trigger_write_cmd(struct mxs_i2c_dev *i2c, u32 cmd,
  300. u32 data)
  301. {
  302. writel(cmd, i2c->regs + MXS_I2C_CTRL0);
  303. if (i2c->dev_type == MXS_I2C_V1)
  304. writel(MXS_I2C_CTRL0_PIO_MODE, i2c->regs + MXS_I2C_CTRL0_SET);
  305. writel(data, i2c->regs + MXS_I2C_DATA(i2c));
  306. writel(MXS_I2C_CTRL0_RUN, i2c->regs + MXS_I2C_CTRL0_SET);
  307. }
  308. static int mxs_i2c_pio_setup_xfer(struct i2c_adapter *adap,
  309. struct i2c_msg *msg, uint32_t flags)
  310. {
  311. struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
  312. uint32_t addr_data = i2c_8bit_addr_from_msg(msg);
  313. uint32_t data = 0;
  314. int i, ret, xlen = 0, xmit = 0;
  315. uint32_t start;
  316. /* Mute IRQs coming from this block. */
  317. writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_CLR);
  318. /*
  319. * MX23 idea:
  320. * - Enable CTRL0::PIO_MODE (1 << 24)
  321. * - Enable CTRL1::ACK_MODE (1 << 27)
  322. *
  323. * WARNING! The MX23 is broken in some way, even if it claims
  324. * to support PIO, when we try to transfer any amount of data
  325. * that is not aligned to 4 bytes, the DMA engine will have
  326. * bits in DEBUG1::DMA_BYTES_ENABLES still set even after the
  327. * transfer. This in turn will mess up the next transfer as
  328. * the block it emit one byte write onto the bus terminated
  329. * with a NAK+STOP. A possible workaround is to reset the IP
  330. * block after every PIO transmission, which might just work.
  331. *
  332. * NOTE: The CTRL0::PIO_MODE description is important, since
  333. * it outlines how the PIO mode is really supposed to work.
  334. */
  335. if (msg->flags & I2C_M_RD) {
  336. /*
  337. * PIO READ transfer:
  338. *
  339. * This transfer MUST be limited to 4 bytes maximum. It is not
  340. * possible to transfer more than four bytes via PIO, since we
  341. * can not in any way make sure we can read the data from the
  342. * DATA register fast enough. Besides, the RX FIFO is only four
  343. * bytes deep, thus we can only really read up to four bytes at
  344. * time. Finally, there is no bit indicating us that new data
  345. * arrived at the FIFO and can thus be fetched from the DATA
  346. * register.
  347. */
  348. BUG_ON(msg->len > 4);
  349. /* SELECT command. */
  350. mxs_i2c_pio_trigger_write_cmd(i2c, MXS_CMD_I2C_SELECT,
  351. addr_data);
  352. ret = mxs_i2c_pio_wait_xfer_end(i2c);
  353. if (ret) {
  354. dev_dbg(i2c->dev,
  355. "PIO: Failed to send SELECT command!\n");
  356. goto cleanup;
  357. }
  358. /* READ command. */
  359. mxs_i2c_pio_trigger_cmd(i2c,
  360. MXS_CMD_I2C_READ | flags |
  361. MXS_I2C_CTRL0_XFER_COUNT(msg->len));
  362. ret = mxs_i2c_pio_wait_xfer_end(i2c);
  363. if (ret) {
  364. dev_dbg(i2c->dev,
  365. "PIO: Failed to send READ command!\n");
  366. goto cleanup;
  367. }
  368. data = readl(i2c->regs + MXS_I2C_DATA(i2c));
  369. for (i = 0; i < msg->len; i++) {
  370. msg->buf[i] = data & 0xff;
  371. data >>= 8;
  372. }
  373. } else {
  374. /*
  375. * PIO WRITE transfer:
  376. *
  377. * The code below implements clock stretching to circumvent
  378. * the possibility of kernel not being able to supply data
  379. * fast enough. It is possible to transfer arbitrary amount
  380. * of data using PIO write.
  381. */
  382. /*
  383. * The LSB of data buffer is the first byte blasted across
  384. * the bus. Higher order bytes follow. Thus the following
  385. * filling schematic.
  386. */
  387. data = addr_data << 24;
  388. /* Start the transfer with START condition. */
  389. start = MXS_I2C_CTRL0_PRE_SEND_START;
  390. /* If the transfer is long, use clock stretching. */
  391. if (msg->len > 3)
  392. start |= MXS_I2C_CTRL0_RETAIN_CLOCK;
  393. for (i = 0; i < msg->len; i++) {
  394. data >>= 8;
  395. data |= (msg->buf[i] << 24);
  396. xmit = 0;
  397. /* This is the last transfer of the message. */
  398. if (i + 1 == msg->len) {
  399. /* Add optional STOP flag. */
  400. start |= flags;
  401. /* Remove RETAIN_CLOCK bit. */
  402. start &= ~MXS_I2C_CTRL0_RETAIN_CLOCK;
  403. xmit = 1;
  404. }
  405. /* Four bytes are ready in the "data" variable. */
  406. if ((i & 3) == 2)
  407. xmit = 1;
  408. /* Nothing interesting happened, continue stuffing. */
  409. if (!xmit)
  410. continue;
  411. /*
  412. * Compute the size of the transfer and shift the
  413. * data accordingly.
  414. *
  415. * i = (4k + 0) .... xlen = 2
  416. * i = (4k + 1) .... xlen = 3
  417. * i = (4k + 2) .... xlen = 4
  418. * i = (4k + 3) .... xlen = 1
  419. */
  420. if ((i % 4) == 3)
  421. xlen = 1;
  422. else
  423. xlen = (i % 4) + 2;
  424. data >>= (4 - xlen) * 8;
  425. dev_dbg(i2c->dev,
  426. "PIO: len=%i pos=%i total=%i [W%s%s%s]\n",
  427. xlen, i, msg->len,
  428. start & MXS_I2C_CTRL0_PRE_SEND_START ? "S" : "",
  429. start & MXS_I2C_CTRL0_POST_SEND_STOP ? "E" : "",
  430. start & MXS_I2C_CTRL0_RETAIN_CLOCK ? "C" : "");
  431. writel(MXS_I2C_DEBUG0_DMAREQ,
  432. i2c->regs + MXS_I2C_DEBUG0_CLR(i2c));
  433. mxs_i2c_pio_trigger_write_cmd(i2c,
  434. start | MXS_I2C_CTRL0_MASTER_MODE |
  435. MXS_I2C_CTRL0_DIRECTION |
  436. MXS_I2C_CTRL0_XFER_COUNT(xlen), data);
  437. /* The START condition is sent only once. */
  438. start &= ~MXS_I2C_CTRL0_PRE_SEND_START;
  439. /* Wait for the end of the transfer. */
  440. ret = mxs_i2c_pio_wait_xfer_end(i2c);
  441. if (ret) {
  442. dev_dbg(i2c->dev,
  443. "PIO: Failed to finish WRITE cmd!\n");
  444. break;
  445. }
  446. /* Check NAK here. */
  447. ret = readl(i2c->regs + MXS_I2C_STAT) &
  448. MXS_I2C_STAT_GOT_A_NAK;
  449. if (ret) {
  450. ret = -ENXIO;
  451. goto cleanup;
  452. }
  453. }
  454. }
  455. /* make sure we capture any occurred error into cmd_err */
  456. ret = mxs_i2c_pio_check_error_state(i2c);
  457. cleanup:
  458. /* Clear any dangling IRQs and re-enable interrupts. */
  459. writel(MXS_I2C_IRQ_MASK, i2c->regs + MXS_I2C_CTRL1_CLR);
  460. writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET);
  461. /* Clear the PIO_MODE on i.MX23 */
  462. if (i2c->dev_type == MXS_I2C_V1)
  463. writel(MXS_I2C_CTRL0_PIO_MODE, i2c->regs + MXS_I2C_CTRL0_CLR);
  464. return ret;
  465. }
  466. /*
  467. * Low level master read/write transaction.
  468. */
  469. static int mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg,
  470. int stop)
  471. {
  472. struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
  473. int ret;
  474. int flags;
  475. int use_pio = 0;
  476. unsigned long time_left;
  477. flags = stop ? MXS_I2C_CTRL0_POST_SEND_STOP : 0;
  478. dev_dbg(i2c->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
  479. msg->addr, msg->len, msg->flags, stop);
  480. /*
  481. * The MX28 I2C IP block can only do PIO READ for transfer of to up
  482. * 4 bytes of length. The write transfer is not limited as it can use
  483. * clock stretching to avoid FIFO underruns.
  484. */
  485. if ((msg->flags & I2C_M_RD) && (msg->len <= 4))
  486. use_pio = 1;
  487. if (!(msg->flags & I2C_M_RD) && (msg->len < 7))
  488. use_pio = 1;
  489. i2c->cmd_err = 0;
  490. if (use_pio) {
  491. ret = mxs_i2c_pio_setup_xfer(adap, msg, flags);
  492. /* No need to reset the block if NAK was received. */
  493. if (ret && (ret != -ENXIO))
  494. mxs_i2c_reset(i2c);
  495. } else {
  496. reinit_completion(&i2c->cmd_complete);
  497. ret = mxs_i2c_dma_setup_xfer(adap, msg, flags);
  498. if (ret)
  499. return ret;
  500. time_left = wait_for_completion_timeout(&i2c->cmd_complete,
  501. msecs_to_jiffies(1000));
  502. if (!time_left)
  503. goto timeout;
  504. ret = i2c->cmd_err;
  505. }
  506. if (ret == -ENXIO) {
  507. /*
  508. * If the transfer fails with a NAK from the slave the
  509. * controller halts until it gets told to return to idle state.
  510. */
  511. writel(MXS_I2C_CTRL1_CLR_GOT_A_NAK,
  512. i2c->regs + MXS_I2C_CTRL1_SET);
  513. }
  514. /*
  515. * WARNING!
  516. * The i.MX23 is strange. After each and every operation, it's I2C IP
  517. * block must be reset, otherwise the IP block will misbehave. This can
  518. * be observed on the bus by the block sending out one single byte onto
  519. * the bus. In case such an error happens, bit 27 will be set in the
  520. * DEBUG0 register. This bit is not documented in the i.MX23 datasheet
  521. * and is marked as "TBD" instead. To reset this bit to a correct state,
  522. * reset the whole block. Since the block reset does not take long, do
  523. * reset the block after every transfer to play safe.
  524. */
  525. if (i2c->dev_type == MXS_I2C_V1)
  526. mxs_i2c_reset(i2c);
  527. dev_dbg(i2c->dev, "Done with err=%d\n", ret);
  528. return ret;
  529. timeout:
  530. dev_dbg(i2c->dev, "Timeout!\n");
  531. mxs_i2c_dma_finish(i2c);
  532. ret = mxs_i2c_reset(i2c);
  533. if (ret)
  534. return ret;
  535. return -ETIMEDOUT;
  536. }
  537. static int mxs_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
  538. int num)
  539. {
  540. int i;
  541. int err;
  542. for (i = 0; i < num; i++) {
  543. err = mxs_i2c_xfer_msg(adap, &msgs[i], i == (num - 1));
  544. if (err)
  545. return err;
  546. }
  547. return num;
  548. }
  549. static u32 mxs_i2c_func(struct i2c_adapter *adap)
  550. {
  551. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  552. }
  553. static irqreturn_t mxs_i2c_isr(int this_irq, void *dev_id)
  554. {
  555. struct mxs_i2c_dev *i2c = dev_id;
  556. u32 stat = readl(i2c->regs + MXS_I2C_CTRL1) & MXS_I2C_IRQ_MASK;
  557. if (!stat)
  558. return IRQ_NONE;
  559. if (stat & MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ)
  560. i2c->cmd_err = -ENXIO;
  561. else if (stat & (MXS_I2C_CTRL1_EARLY_TERM_IRQ |
  562. MXS_I2C_CTRL1_MASTER_LOSS_IRQ |
  563. MXS_I2C_CTRL1_SLAVE_STOP_IRQ | MXS_I2C_CTRL1_SLAVE_IRQ))
  564. /* MXS_I2C_CTRL1_OVERSIZE_XFER_TERM_IRQ is only for slaves */
  565. i2c->cmd_err = -EIO;
  566. writel(stat, i2c->regs + MXS_I2C_CTRL1_CLR);
  567. return IRQ_HANDLED;
  568. }
  569. static const struct i2c_algorithm mxs_i2c_algo = {
  570. .master_xfer = mxs_i2c_xfer,
  571. .functionality = mxs_i2c_func,
  572. };
  573. static const struct i2c_adapter_quirks mxs_i2c_quirks = {
  574. .flags = I2C_AQ_NO_ZERO_LEN,
  575. };
  576. static void mxs_i2c_derive_timing(struct mxs_i2c_dev *i2c, uint32_t speed)
  577. {
  578. /* The I2C block clock runs at 24MHz */
  579. const uint32_t clk = 24000000;
  580. uint32_t divider;
  581. uint16_t high_count, low_count, rcv_count, xmit_count;
  582. uint32_t bus_free, leadin;
  583. struct device *dev = i2c->dev;
  584. divider = DIV_ROUND_UP(clk, speed);
  585. if (divider < 25) {
  586. /*
  587. * limit the divider, so that min(low_count, high_count)
  588. * is >= 1
  589. */
  590. divider = 25;
  591. dev_warn(dev,
  592. "Speed too high (%u.%03u kHz), using %u.%03u kHz\n",
  593. speed / 1000, speed % 1000,
  594. clk / divider / 1000, clk / divider % 1000);
  595. } else if (divider > 1897) {
  596. /*
  597. * limit the divider, so that max(low_count, high_count)
  598. * cannot exceed 1023
  599. */
  600. divider = 1897;
  601. dev_warn(dev,
  602. "Speed too low (%u.%03u kHz), using %u.%03u kHz\n",
  603. speed / 1000, speed % 1000,
  604. clk / divider / 1000, clk / divider % 1000);
  605. }
  606. /*
  607. * The I2C spec specifies the following timing data:
  608. * standard mode fast mode Bitfield name
  609. * tLOW (SCL LOW period) 4700 ns 1300 ns
  610. * tHIGH (SCL HIGH period) 4000 ns 600 ns
  611. * tSU;DAT (data setup time) 250 ns 100 ns
  612. * tHD;STA (START hold time) 4000 ns 600 ns
  613. * tBUF (bus free time) 4700 ns 1300 ns
  614. *
  615. * The hardware (of the i.MX28 at least) seems to add 2 additional
  616. * clock cycles to the low_count and 7 cycles to the high_count.
  617. * This is compensated for by subtracting the respective constants
  618. * from the values written to the timing registers.
  619. */
  620. if (speed > 100000) {
  621. /* fast mode */
  622. low_count = DIV_ROUND_CLOSEST(divider * 13, (13 + 6));
  623. high_count = DIV_ROUND_CLOSEST(divider * 6, (13 + 6));
  624. leadin = DIV_ROUND_UP(600 * (clk / 1000000), 1000);
  625. bus_free = DIV_ROUND_UP(1300 * (clk / 1000000), 1000);
  626. } else {
  627. /* normal mode */
  628. low_count = DIV_ROUND_CLOSEST(divider * 47, (47 + 40));
  629. high_count = DIV_ROUND_CLOSEST(divider * 40, (47 + 40));
  630. leadin = DIV_ROUND_UP(4700 * (clk / 1000000), 1000);
  631. bus_free = DIV_ROUND_UP(4700 * (clk / 1000000), 1000);
  632. }
  633. rcv_count = high_count * 3 / 8;
  634. xmit_count = low_count * 3 / 8;
  635. dev_dbg(dev,
  636. "speed=%u(actual %u) divider=%u low=%u high=%u xmit=%u rcv=%u leadin=%u bus_free=%u\n",
  637. speed, clk / divider, divider, low_count, high_count,
  638. xmit_count, rcv_count, leadin, bus_free);
  639. low_count -= 2;
  640. high_count -= 7;
  641. i2c->timing0 = (high_count << 16) | rcv_count;
  642. i2c->timing1 = (low_count << 16) | xmit_count;
  643. i2c->timing2 = (bus_free << 16 | leadin);
  644. }
  645. static int mxs_i2c_get_ofdata(struct mxs_i2c_dev *i2c)
  646. {
  647. uint32_t speed;
  648. struct device *dev = i2c->dev;
  649. struct device_node *node = dev->of_node;
  650. int ret;
  651. ret = of_property_read_u32(node, "clock-frequency", &speed);
  652. if (ret) {
  653. dev_warn(dev, "No I2C speed selected, using 100kHz\n");
  654. speed = 100000;
  655. }
  656. mxs_i2c_derive_timing(i2c, speed);
  657. return 0;
  658. }
  659. static const struct platform_device_id mxs_i2c_devtype[] = {
  660. {
  661. .name = "imx23-i2c",
  662. .driver_data = MXS_I2C_V1,
  663. }, {
  664. .name = "imx28-i2c",
  665. .driver_data = MXS_I2C_V2,
  666. }, { /* sentinel */ }
  667. };
  668. MODULE_DEVICE_TABLE(platform, mxs_i2c_devtype);
  669. static const struct of_device_id mxs_i2c_dt_ids[] = {
  670. { .compatible = "fsl,imx23-i2c", .data = &mxs_i2c_devtype[0], },
  671. { .compatible = "fsl,imx28-i2c", .data = &mxs_i2c_devtype[1], },
  672. { /* sentinel */ }
  673. };
  674. MODULE_DEVICE_TABLE(of, mxs_i2c_dt_ids);
  675. static int mxs_i2c_probe(struct platform_device *pdev)
  676. {
  677. const struct of_device_id *of_id =
  678. of_match_device(mxs_i2c_dt_ids, &pdev->dev);
  679. struct device *dev = &pdev->dev;
  680. struct mxs_i2c_dev *i2c;
  681. struct i2c_adapter *adap;
  682. struct resource *res;
  683. int err, irq;
  684. i2c = devm_kzalloc(dev, sizeof(*i2c), GFP_KERNEL);
  685. if (!i2c)
  686. return -ENOMEM;
  687. if (of_id) {
  688. const struct platform_device_id *device_id = of_id->data;
  689. i2c->dev_type = device_id->driver_data;
  690. }
  691. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  692. i2c->regs = devm_ioremap_resource(&pdev->dev, res);
  693. if (IS_ERR(i2c->regs))
  694. return PTR_ERR(i2c->regs);
  695. irq = platform_get_irq(pdev, 0);
  696. if (irq < 0)
  697. return irq;
  698. err = devm_request_irq(dev, irq, mxs_i2c_isr, 0, dev_name(dev), i2c);
  699. if (err)
  700. return err;
  701. i2c->dev = dev;
  702. init_completion(&i2c->cmd_complete);
  703. if (dev->of_node) {
  704. err = mxs_i2c_get_ofdata(i2c);
  705. if (err)
  706. return err;
  707. }
  708. /* Setup the DMA */
  709. i2c->dmach = dma_request_slave_channel(dev, "rx-tx");
  710. if (!i2c->dmach) {
  711. dev_err(dev, "Failed to request dma\n");
  712. return -ENODEV;
  713. }
  714. platform_set_drvdata(pdev, i2c);
  715. /* Do reset to enforce correct startup after pinmuxing */
  716. err = mxs_i2c_reset(i2c);
  717. if (err)
  718. return err;
  719. adap = &i2c->adapter;
  720. strlcpy(adap->name, "MXS I2C adapter", sizeof(adap->name));
  721. adap->owner = THIS_MODULE;
  722. adap->algo = &mxs_i2c_algo;
  723. adap->quirks = &mxs_i2c_quirks;
  724. adap->dev.parent = dev;
  725. adap->nr = pdev->id;
  726. adap->dev.of_node = pdev->dev.of_node;
  727. i2c_set_adapdata(adap, i2c);
  728. err = i2c_add_numbered_adapter(adap);
  729. if (err) {
  730. writel(MXS_I2C_CTRL0_SFTRST,
  731. i2c->regs + MXS_I2C_CTRL0_SET);
  732. return err;
  733. }
  734. return 0;
  735. }
  736. static int mxs_i2c_remove(struct platform_device *pdev)
  737. {
  738. struct mxs_i2c_dev *i2c = platform_get_drvdata(pdev);
  739. i2c_del_adapter(&i2c->adapter);
  740. if (i2c->dmach)
  741. dma_release_channel(i2c->dmach);
  742. writel(MXS_I2C_CTRL0_SFTRST, i2c->regs + MXS_I2C_CTRL0_SET);
  743. return 0;
  744. }
  745. static struct platform_driver mxs_i2c_driver = {
  746. .driver = {
  747. .name = DRIVER_NAME,
  748. .of_match_table = mxs_i2c_dt_ids,
  749. },
  750. .probe = mxs_i2c_probe,
  751. .remove = mxs_i2c_remove,
  752. };
  753. static int __init mxs_i2c_init(void)
  754. {
  755. return platform_driver_register(&mxs_i2c_driver);
  756. }
  757. subsys_initcall(mxs_i2c_init);
  758. static void __exit mxs_i2c_exit(void)
  759. {
  760. platform_driver_unregister(&mxs_i2c_driver);
  761. }
  762. module_exit(mxs_i2c_exit);
  763. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  764. MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
  765. MODULE_DESCRIPTION("MXS I2C Bus Driver");
  766. MODULE_LICENSE("GPL");
  767. MODULE_ALIAS("platform:" DRIVER_NAME);