i2c-cdns.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 <linux/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. unsigned int input_freq;
  106. struct cdns_i2c_regs __iomem *regs; /* register base */
  107. };
  108. /* Wait for an interrupt */
  109. static u32 cdns_i2c_wait(struct cdns_i2c_regs *cdns_i2c, u32 mask)
  110. {
  111. int timeout, int_status;
  112. for (timeout = 0; timeout < 100; timeout++) {
  113. udelay(100);
  114. int_status = readl(&cdns_i2c->interrupt_status);
  115. if (int_status & mask)
  116. break;
  117. }
  118. /* Clear interrupt status flags */
  119. writel(int_status & mask, &cdns_i2c->interrupt_status);
  120. return int_status & mask;
  121. }
  122. #define CDNS_I2C_DIVA_MAX 4
  123. #define CDNS_I2C_DIVB_MAX 64
  124. static int cdns_i2c_calc_divs(unsigned long *f, unsigned long input_clk,
  125. unsigned int *a, unsigned int *b)
  126. {
  127. unsigned long fscl = *f, best_fscl = *f, actual_fscl, temp;
  128. unsigned int div_a, div_b, calc_div_a = 0, calc_div_b = 0;
  129. unsigned int last_error, current_error;
  130. /* calculate (divisor_a+1) x (divisor_b+1) */
  131. temp = input_clk / (22 * fscl);
  132. /*
  133. * If the calculated value is negative or 0CDNS_I2C_DIVA_MAX,
  134. * the fscl input is out of range. Return error.
  135. */
  136. if (!temp || (temp > (CDNS_I2C_DIVA_MAX * CDNS_I2C_DIVB_MAX)))
  137. return -EINVAL;
  138. last_error = -1;
  139. for (div_a = 0; div_a < CDNS_I2C_DIVA_MAX; div_a++) {
  140. div_b = DIV_ROUND_UP(input_clk, 22 * fscl * (div_a + 1));
  141. if ((div_b < 1) || (div_b > CDNS_I2C_DIVB_MAX))
  142. continue;
  143. div_b--;
  144. actual_fscl = input_clk / (22 * (div_a + 1) * (div_b + 1));
  145. if (actual_fscl > fscl)
  146. continue;
  147. current_error = ((actual_fscl > fscl) ? (actual_fscl - fscl) :
  148. (fscl - actual_fscl));
  149. if (last_error > current_error) {
  150. calc_div_a = div_a;
  151. calc_div_b = div_b;
  152. best_fscl = actual_fscl;
  153. last_error = current_error;
  154. }
  155. }
  156. *a = calc_div_a;
  157. *b = calc_div_b;
  158. *f = best_fscl;
  159. return 0;
  160. }
  161. static int cdns_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
  162. {
  163. struct i2c_cdns_bus *bus = dev_get_priv(dev);
  164. u32 div_a = 0, div_b = 0;
  165. unsigned long speed_p = speed;
  166. int ret = 0;
  167. if (speed > 400000) {
  168. debug("%s, failed to set clock speed to %u\n", __func__,
  169. speed);
  170. return -EINVAL;
  171. }
  172. ret = cdns_i2c_calc_divs(&speed_p, bus->input_freq, &div_a, &div_b);
  173. if (ret)
  174. return ret;
  175. debug("%s: div_a: %d, div_b: %d, input freq: %d, speed: %d/%ld\n",
  176. __func__, div_a, div_b, bus->input_freq, speed, speed_p);
  177. writel((div_b << CDNS_I2C_CONTROL_DIV_B_SHIFT) |
  178. (div_a << CDNS_I2C_CONTROL_DIV_A_SHIFT), &bus->regs->control);
  179. /* Enable master mode, ack, and 7-bit addressing */
  180. setbits_le32(&bus->regs->control, CDNS_I2C_CONTROL_MS |
  181. CDNS_I2C_CONTROL_ACKEN | CDNS_I2C_CONTROL_NEA);
  182. return 0;
  183. }
  184. /* Probe to see if a chip is present. */
  185. static int cdns_i2c_probe_chip(struct udevice *bus, uint chip_addr,
  186. uint chip_flags)
  187. {
  188. struct i2c_cdns_bus *i2c_bus = dev_get_priv(bus);
  189. struct cdns_i2c_regs *regs = i2c_bus->regs;
  190. /* Attempt to read a byte */
  191. setbits_le32(&regs->control, CDNS_I2C_CONTROL_CLR_FIFO |
  192. CDNS_I2C_CONTROL_RW);
  193. clrbits_le32(&regs->control, CDNS_I2C_CONTROL_HOLD);
  194. writel(0xFF, &regs->interrupt_status);
  195. writel(chip_addr, &regs->address);
  196. writel(1, &regs->transfer_size);
  197. return (cdns_i2c_wait(regs, CDNS_I2C_INTERRUPT_COMP |
  198. CDNS_I2C_INTERRUPT_NACK) &
  199. CDNS_I2C_INTERRUPT_COMP) ? 0 : -ETIMEDOUT;
  200. }
  201. static int cdns_i2c_write_data(struct i2c_cdns_bus *i2c_bus, u32 addr, u8 *data,
  202. u32 len, bool next_is_read)
  203. {
  204. u8 *cur_data = data;
  205. struct cdns_i2c_regs *regs = i2c_bus->regs;
  206. setbits_le32(&regs->control, CDNS_I2C_CONTROL_CLR_FIFO |
  207. CDNS_I2C_CONTROL_HOLD);
  208. /* if next is a read, we need to clear HOLD, doesn't work */
  209. if (next_is_read)
  210. clrbits_le32(&regs->control, CDNS_I2C_CONTROL_HOLD);
  211. clrbits_le32(&regs->control, CDNS_I2C_CONTROL_RW);
  212. writel(0xFF, &regs->interrupt_status);
  213. writel(addr, &regs->address);
  214. while (len--) {
  215. writel(*(cur_data++), &regs->data);
  216. if (readl(&regs->transfer_size) == CDNS_I2C_FIFO_DEPTH) {
  217. if (!cdns_i2c_wait(regs, CDNS_I2C_INTERRUPT_COMP)) {
  218. /* Release the bus */
  219. clrbits_le32(&regs->control,
  220. CDNS_I2C_CONTROL_HOLD);
  221. return -ETIMEDOUT;
  222. }
  223. }
  224. }
  225. /* All done... release the bus */
  226. clrbits_le32(&regs->control, CDNS_I2C_CONTROL_HOLD);
  227. /* Wait for the address and data to be sent */
  228. if (!cdns_i2c_wait(regs, CDNS_I2C_INTERRUPT_COMP))
  229. return -ETIMEDOUT;
  230. return 0;
  231. }
  232. static int cdns_i2c_read_data(struct i2c_cdns_bus *i2c_bus, u32 addr, u8 *data,
  233. u32 len)
  234. {
  235. u32 status;
  236. u32 i = 0;
  237. u8 *cur_data = data;
  238. /* TODO: Fix this */
  239. struct cdns_i2c_regs *regs = i2c_bus->regs;
  240. /* Check the hardware can handle the requested bytes */
  241. if ((len < 0) || (len > CDNS_I2C_TRANSFER_SIZE_MAX))
  242. return -EINVAL;
  243. setbits_le32(&regs->control, CDNS_I2C_CONTROL_CLR_FIFO |
  244. CDNS_I2C_CONTROL_RW);
  245. /* Start reading data */
  246. writel(addr, &regs->address);
  247. writel(len, &regs->transfer_size);
  248. /* Wait for data */
  249. do {
  250. status = cdns_i2c_wait(regs, CDNS_I2C_INTERRUPT_COMP |
  251. CDNS_I2C_INTERRUPT_DATA);
  252. if (!status) {
  253. /* Release the bus */
  254. clrbits_le32(&regs->control, CDNS_I2C_CONTROL_HOLD);
  255. return -ETIMEDOUT;
  256. }
  257. debug("Read %d bytes\n",
  258. len - readl(&regs->transfer_size));
  259. for (; i < len - readl(&regs->transfer_size); i++)
  260. *(cur_data++) = readl(&regs->data);
  261. } while (readl(&regs->transfer_size) != 0);
  262. /* All done... release the bus */
  263. clrbits_le32(&regs->control, CDNS_I2C_CONTROL_HOLD);
  264. #ifdef DEBUG
  265. cdns_i2c_debug_status(regs);
  266. #endif
  267. return 0;
  268. }
  269. static int cdns_i2c_xfer(struct udevice *dev, struct i2c_msg *msg,
  270. int nmsgs)
  271. {
  272. struct i2c_cdns_bus *i2c_bus = dev_get_priv(dev);
  273. int ret;
  274. debug("i2c_xfer: %d messages\n", nmsgs);
  275. for (; nmsgs > 0; nmsgs--, msg++) {
  276. bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD);
  277. debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
  278. if (msg->flags & I2C_M_RD) {
  279. ret = cdns_i2c_read_data(i2c_bus, msg->addr, msg->buf,
  280. msg->len);
  281. } else {
  282. ret = cdns_i2c_write_data(i2c_bus, msg->addr, msg->buf,
  283. msg->len, next_is_read);
  284. }
  285. if (ret) {
  286. debug("i2c_write: error sending\n");
  287. return -EREMOTEIO;
  288. }
  289. }
  290. return 0;
  291. }
  292. static int cdns_i2c_ofdata_to_platdata(struct udevice *dev)
  293. {
  294. struct i2c_cdns_bus *i2c_bus = dev_get_priv(dev);
  295. i2c_bus->regs = (struct cdns_i2c_regs *)dev_get_addr(dev);
  296. if (!i2c_bus->regs)
  297. return -ENOMEM;
  298. i2c_bus->input_freq = 100000000; /* TODO hardcode input freq for now */
  299. return 0;
  300. }
  301. static const struct dm_i2c_ops cdns_i2c_ops = {
  302. .xfer = cdns_i2c_xfer,
  303. .probe_chip = cdns_i2c_probe_chip,
  304. .set_bus_speed = cdns_i2c_set_bus_speed,
  305. };
  306. static const struct udevice_id cdns_i2c_of_match[] = {
  307. { .compatible = "cdns,i2c-r1p10" },
  308. { /* end of table */ }
  309. };
  310. U_BOOT_DRIVER(cdns_i2c) = {
  311. .name = "i2c-cdns",
  312. .id = UCLASS_I2C,
  313. .of_match = cdns_i2c_of_match,
  314. .ofdata_to_platdata = cdns_i2c_ofdata_to_platdata,
  315. .priv_auto_alloc_size = sizeof(struct i2c_cdns_bus),
  316. .ops = &cdns_i2c_ops,
  317. };