i2c-mxs.c 24 KB

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