i2c-cdns.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright (C) 2015 Moritz Fischer <moritz.fischer@ettus.com>
  3. * IP from Cadence (ID T-CS-PE-0007-100, Version R1p10f2)
  4. *
  5. * This file is based on: drivers/i2c/zynq_i2c.c,
  6. * with added driver-model support and code cleanup.
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <linux/types.h>
  12. #include <linux/io.h>
  13. #include <asm/errno.h>
  14. #include <dm/device.h>
  15. #include <dm/root.h>
  16. #include <i2c.h>
  17. #include <fdtdec.h>
  18. #include <mapmem.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. /* i2c register set */
  21. struct cdns_i2c_regs {
  22. u32 control;
  23. u32 status;
  24. u32 address;
  25. u32 data;
  26. u32 interrupt_status;
  27. u32 transfer_size;
  28. u32 slave_mon_pause;
  29. u32 time_out;
  30. u32 interrupt_mask;
  31. u32 interrupt_enable;
  32. u32 interrupt_disable;
  33. };
  34. /* Control register fields */
  35. #define CDNS_I2C_CONTROL_RW 0x00000001
  36. #define CDNS_I2C_CONTROL_MS 0x00000002
  37. #define CDNS_I2C_CONTROL_NEA 0x00000004
  38. #define CDNS_I2C_CONTROL_ACKEN 0x00000008
  39. #define CDNS_I2C_CONTROL_HOLD 0x00000010
  40. #define CDNS_I2C_CONTROL_SLVMON 0x00000020
  41. #define CDNS_I2C_CONTROL_CLR_FIFO 0x00000040
  42. #define CDNS_I2C_CONTROL_DIV_B_SHIFT 8
  43. #define CDNS_I2C_CONTROL_DIV_B_MASK 0x00003F00
  44. #define CDNS_I2C_CONTROL_DIV_A_SHIFT 14
  45. #define CDNS_I2C_CONTROL_DIV_A_MASK 0x0000C000
  46. /* Status register values */
  47. #define CDNS_I2C_STATUS_RXDV 0x00000020
  48. #define CDNS_I2C_STATUS_TXDV 0x00000040
  49. #define CDNS_I2C_STATUS_RXOVF 0x00000080
  50. #define CDNS_I2C_STATUS_BA 0x00000100
  51. /* Interrupt register fields */
  52. #define CDNS_I2C_INTERRUPT_COMP 0x00000001
  53. #define CDNS_I2C_INTERRUPT_DATA 0x00000002
  54. #define CDNS_I2C_INTERRUPT_NACK 0x00000004
  55. #define CDNS_I2C_INTERRUPT_TO 0x00000008
  56. #define CDNS_I2C_INTERRUPT_SLVRDY 0x00000010
  57. #define CDNS_I2C_INTERRUPT_RXOVF 0x00000020
  58. #define CDNS_I2C_INTERRUPT_TXOVF 0x00000040
  59. #define CDNS_I2C_INTERRUPT_RXUNF 0x00000080
  60. #define CDNS_I2C_INTERRUPT_ARBLOST 0x00000200
  61. #define CDNS_I2C_FIFO_DEPTH 16
  62. #define CDNS_I2C_TRANSFER_SIZE_MAX 255 /* Controller transfer limit */
  63. #ifdef DEBUG
  64. static void cdns_i2c_debug_status(struct cdns_i2c_regs *cdns_i2c)
  65. {
  66. int int_status;
  67. int status;
  68. int_status = readl(&cdns_i2c->interrupt_status);
  69. status = readl(&cdns_i2c->status);
  70. if (int_status || status) {
  71. debug("Status: ");
  72. if (int_status & CDNS_I2C_INTERRUPT_COMP)
  73. debug("COMP ");
  74. if (int_status & CDNS_I2C_INTERRUPT_DATA)
  75. debug("DATA ");
  76. if (int_status & CDNS_I2C_INTERRUPT_NACK)
  77. debug("NACK ");
  78. if (int_status & CDNS_I2C_INTERRUPT_TO)
  79. debug("TO ");
  80. if (int_status & CDNS_I2C_INTERRUPT_SLVRDY)
  81. debug("SLVRDY ");
  82. if (int_status & CDNS_I2C_INTERRUPT_RXOVF)
  83. debug("RXOVF ");
  84. if (int_status & CDNS_I2C_INTERRUPT_TXOVF)
  85. debug("TXOVF ");
  86. if (int_status & CDNS_I2C_INTERRUPT_RXUNF)
  87. debug("RXUNF ");
  88. if (int_status & CDNS_I2C_INTERRUPT_ARBLOST)
  89. debug("ARBLOST ");
  90. if (status & CDNS_I2C_STATUS_RXDV)
  91. debug("RXDV ");
  92. if (status & CDNS_I2C_STATUS_TXDV)
  93. debug("TXDV ");
  94. if (status & CDNS_I2C_STATUS_RXOVF)
  95. debug("RXOVF ");
  96. if (status & CDNS_I2C_STATUS_BA)
  97. debug("BA ");
  98. debug("TS%d ", readl(&cdns_i2c->transfer_size));
  99. debug("\n");
  100. }
  101. }
  102. #endif
  103. struct i2c_cdns_bus {
  104. int id;
  105. struct cdns_i2c_regs __iomem *regs; /* register base */
  106. };
  107. /** cdns_i2c_probe() - Probe method
  108. * @dev: udevice pointer
  109. *
  110. * DM callback called when device is probed
  111. */
  112. static int cdns_i2c_probe(struct udevice *dev)
  113. {
  114. struct i2c_cdns_bus *bus = dev_get_priv(dev);
  115. /* TODO: Calculate dividers based on CPU_CLK_1X */
  116. /* 111MHz / ( (3 * 17) * 22 ) = ~100KHz */
  117. writel((16 << CDNS_I2C_CONTROL_DIV_B_SHIFT) |
  118. (2 << CDNS_I2C_CONTROL_DIV_A_SHIFT), &bus->regs->control);
  119. /* Enable master mode, ack, and 7-bit addressing */
  120. setbits_le32(&bus->regs->control, CDNS_I2C_CONTROL_MS |
  121. CDNS_I2C_CONTROL_ACKEN | CDNS_I2C_CONTROL_NEA);
  122. debug("%s bus %d at %p\n", __func__, dev->seq, bus->regs);
  123. return 0;
  124. }
  125. static int cdns_i2c_remove(struct udevice *dev)
  126. {
  127. struct i2c_cdns_bus *bus = dev_get_priv(dev);
  128. debug("%s bus %d at %p\n", __func__, dev->seq, bus->regs);
  129. unmap_sysmem(bus->regs);
  130. return 0;
  131. }
  132. /* Wait for an interrupt */
  133. static u32 cdns_i2c_wait(struct cdns_i2c_regs *cdns_i2c, u32 mask)
  134. {
  135. int timeout, int_status;
  136. for (timeout = 0; timeout < 100; timeout++) {
  137. udelay(100);
  138. int_status = readl(&cdns_i2c->interrupt_status);
  139. if (int_status & mask)
  140. break;
  141. }
  142. /* Clear interrupt status flags */
  143. writel(int_status & mask, &cdns_i2c->interrupt_status);
  144. return int_status & mask;
  145. }
  146. static int cdns_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
  147. {
  148. if (speed != 100000) {
  149. printf("%s, failed to set clock speed to %u\n", __func__,
  150. speed);
  151. return -EINVAL;
  152. }
  153. return 0;
  154. }
  155. /* Probe to see if a chip is present. */
  156. static int cdns_i2c_probe_chip(struct udevice *bus, uint chip_addr,
  157. uint chip_flags)
  158. {
  159. struct i2c_cdns_bus *i2c_bus = dev_get_priv(bus);
  160. struct cdns_i2c_regs *regs = i2c_bus->regs;
  161. /* Attempt to read a byte */
  162. setbits_le32(&regs->control, CDNS_I2C_CONTROL_CLR_FIFO |
  163. CDNS_I2C_CONTROL_RW);
  164. clrbits_le32(&regs->control, CDNS_I2C_CONTROL_HOLD);
  165. writel(0xFF, &regs->interrupt_status);
  166. writel(chip_addr, &regs->address);
  167. writel(1, &regs->transfer_size);
  168. return (cdns_i2c_wait(regs, CDNS_I2C_INTERRUPT_COMP |
  169. CDNS_I2C_INTERRUPT_NACK) &
  170. CDNS_I2C_INTERRUPT_COMP) ? 0 : -ETIMEDOUT;
  171. }
  172. static int cdns_i2c_write_data(struct i2c_cdns_bus *i2c_bus, u32 addr, u8 *data,
  173. u32 len, bool next_is_read)
  174. {
  175. u8 *cur_data = data;
  176. struct cdns_i2c_regs *regs = i2c_bus->regs;
  177. setbits_le32(&regs->control, CDNS_I2C_CONTROL_CLR_FIFO |
  178. CDNS_I2C_CONTROL_HOLD);
  179. /* if next is a read, we need to clear HOLD, doesn't work */
  180. if (next_is_read)
  181. clrbits_le32(&regs->control, CDNS_I2C_CONTROL_HOLD);
  182. clrbits_le32(&regs->control, CDNS_I2C_CONTROL_RW);
  183. writel(0xFF, &regs->interrupt_status);
  184. writel(addr, &regs->address);
  185. while (len--) {
  186. writel(*(cur_data++), &regs->data);
  187. if (readl(&regs->transfer_size) == CDNS_I2C_FIFO_DEPTH) {
  188. if (!cdns_i2c_wait(regs, CDNS_I2C_INTERRUPT_COMP)) {
  189. /* Release the bus */
  190. clrbits_le32(&regs->control,
  191. CDNS_I2C_CONTROL_HOLD);
  192. return -ETIMEDOUT;
  193. }
  194. }
  195. }
  196. /* All done... release the bus */
  197. clrbits_le32(&regs->control, CDNS_I2C_CONTROL_HOLD);
  198. /* Wait for the address and data to be sent */
  199. if (!cdns_i2c_wait(regs, CDNS_I2C_INTERRUPT_COMP))
  200. return -ETIMEDOUT;
  201. return 0;
  202. }
  203. static int cdns_i2c_read_data(struct i2c_cdns_bus *i2c_bus, u32 addr, u8 *data,
  204. u32 len)
  205. {
  206. u32 status;
  207. u32 i = 0;
  208. u8 *cur_data = data;
  209. /* TODO: Fix this */
  210. struct cdns_i2c_regs *regs = i2c_bus->regs;
  211. /* Check the hardware can handle the requested bytes */
  212. if ((len < 0) || (len > CDNS_I2C_TRANSFER_SIZE_MAX))
  213. return -EINVAL;
  214. setbits_le32(&regs->control, CDNS_I2C_CONTROL_CLR_FIFO |
  215. CDNS_I2C_CONTROL_RW);
  216. /* Start reading data */
  217. writel(addr, &regs->address);
  218. writel(len, &regs->transfer_size);
  219. /* Wait for data */
  220. do {
  221. status = cdns_i2c_wait(regs, CDNS_I2C_INTERRUPT_COMP |
  222. CDNS_I2C_INTERRUPT_DATA);
  223. if (!status) {
  224. /* Release the bus */
  225. clrbits_le32(&regs->control, CDNS_I2C_CONTROL_HOLD);
  226. return -ETIMEDOUT;
  227. }
  228. debug("Read %d bytes\n",
  229. len - readl(&regs->transfer_size));
  230. for (; i < len - readl(&regs->transfer_size); i++)
  231. *(cur_data++) = readl(&regs->data);
  232. } while (readl(&regs->transfer_size) != 0);
  233. /* All done... release the bus */
  234. clrbits_le32(&regs->control, CDNS_I2C_CONTROL_HOLD);
  235. #ifdef DEBUG
  236. cdns_i2c_debug_status(regs);
  237. #endif
  238. return 0;
  239. }
  240. static int cdns_i2c_xfer(struct udevice *dev, struct i2c_msg *msg,
  241. int nmsgs)
  242. {
  243. struct i2c_cdns_bus *i2c_bus = dev_get_priv(dev);
  244. int ret;
  245. debug("i2c_xfer: %d messages\n", nmsgs);
  246. for (; nmsgs > 0; nmsgs--, msg++) {
  247. bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD);
  248. debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
  249. if (msg->flags & I2C_M_RD) {
  250. ret = cdns_i2c_read_data(i2c_bus, msg->addr, msg->buf,
  251. msg->len);
  252. } else {
  253. ret = cdns_i2c_write_data(i2c_bus, msg->addr, msg->buf,
  254. msg->len, next_is_read);
  255. }
  256. if (ret) {
  257. debug("i2c_write: error sending\n");
  258. return -EREMOTEIO;
  259. }
  260. }
  261. return 0;
  262. }
  263. static int cdns_i2c_ofdata_to_platdata(struct udevice *dev)
  264. {
  265. struct i2c_cdns_bus *i2c_bus = dev_get_priv(dev);
  266. i2c_bus->regs = (struct cdns_i2c_regs *)dev_get_addr(dev);
  267. if (!i2c_bus->regs)
  268. return -ENOMEM;
  269. return 0;
  270. }
  271. static const struct dm_i2c_ops cdns_i2c_ops = {
  272. .xfer = cdns_i2c_xfer,
  273. .probe_chip = cdns_i2c_probe_chip,
  274. .set_bus_speed = cdns_i2c_set_bus_speed,
  275. };
  276. static const struct udevice_id cdns_i2c_of_match[] = {
  277. { .compatible = "cdns,i2c-r1p10" },
  278. { /* end of table */ }
  279. };
  280. U_BOOT_DRIVER(cdns_i2c) = {
  281. .name = "i2c-cdns",
  282. .id = UCLASS_I2C,
  283. .of_match = cdns_i2c_of_match,
  284. .probe = cdns_i2c_probe,
  285. .remove = cdns_i2c_remove,
  286. .ofdata_to_platdata = cdns_i2c_ofdata_to_platdata,
  287. .priv_auto_alloc_size = sizeof(struct i2c_cdns_bus),
  288. .ops = &cdns_i2c_ops,
  289. };