i2c-cdns.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Moritz Fischer <moritz.fischer@ettus.com>
  4. * IP from Cadence (ID T-CS-PE-0007-100, Version R1p10f2)
  5. *
  6. * This file is based on: drivers/i2c/zynq_i2c.c,
  7. * with added driver-model support and code cleanup.
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <linux/types.h>
  12. #include <linux/io.h>
  13. #include <linux/errno.h>
  14. #include <dm/root.h>
  15. #include <i2c.h>
  16. #include <fdtdec.h>
  17. #include <mapmem.h>
  18. #include <wait_bit.h>
  19. /* i2c register set */
  20. struct cdns_i2c_regs {
  21. u32 control;
  22. u32 status;
  23. u32 address;
  24. u32 data;
  25. u32 interrupt_status;
  26. u32 transfer_size;
  27. u32 slave_mon_pause;
  28. u32 time_out;
  29. u32 interrupt_mask;
  30. u32 interrupt_enable;
  31. u32 interrupt_disable;
  32. };
  33. /* Control register fields */
  34. #define CDNS_I2C_CONTROL_RW 0x00000001
  35. #define CDNS_I2C_CONTROL_MS 0x00000002
  36. #define CDNS_I2C_CONTROL_NEA 0x00000004
  37. #define CDNS_I2C_CONTROL_ACKEN 0x00000008
  38. #define CDNS_I2C_CONTROL_HOLD 0x00000010
  39. #define CDNS_I2C_CONTROL_SLVMON 0x00000020
  40. #define CDNS_I2C_CONTROL_CLR_FIFO 0x00000040
  41. #define CDNS_I2C_CONTROL_DIV_B_SHIFT 8
  42. #define CDNS_I2C_CONTROL_DIV_B_MASK 0x00003F00
  43. #define CDNS_I2C_CONTROL_DIV_A_SHIFT 14
  44. #define CDNS_I2C_CONTROL_DIV_A_MASK 0x0000C000
  45. /* Status register values */
  46. #define CDNS_I2C_STATUS_RXDV 0x00000020
  47. #define CDNS_I2C_STATUS_TXDV 0x00000040
  48. #define CDNS_I2C_STATUS_RXOVF 0x00000080
  49. #define CDNS_I2C_STATUS_BA 0x00000100
  50. /* Interrupt register fields */
  51. #define CDNS_I2C_INTERRUPT_COMP 0x00000001
  52. #define CDNS_I2C_INTERRUPT_DATA 0x00000002
  53. #define CDNS_I2C_INTERRUPT_NACK 0x00000004
  54. #define CDNS_I2C_INTERRUPT_TO 0x00000008
  55. #define CDNS_I2C_INTERRUPT_SLVRDY 0x00000010
  56. #define CDNS_I2C_INTERRUPT_RXOVF 0x00000020
  57. #define CDNS_I2C_INTERRUPT_TXOVF 0x00000040
  58. #define CDNS_I2C_INTERRUPT_RXUNF 0x00000080
  59. #define CDNS_I2C_INTERRUPT_ARBLOST 0x00000200
  60. #define CDNS_I2C_FIFO_DEPTH 16
  61. #define CDNS_I2C_TRANSFER_SIZE_MAX 255 /* Controller transfer limit */
  62. #define CDNS_I2C_TRANSFER_SIZE (CDNS_I2C_TRANSFER_SIZE_MAX - 3)
  63. #define CDNS_I2C_BROKEN_HOLD_BIT BIT(0)
  64. #ifdef DEBUG
  65. static void cdns_i2c_debug_status(struct cdns_i2c_regs *cdns_i2c)
  66. {
  67. int int_status;
  68. int status;
  69. int_status = readl(&cdns_i2c->interrupt_status);
  70. status = readl(&cdns_i2c->status);
  71. if (int_status || status) {
  72. debug("Status: ");
  73. if (int_status & CDNS_I2C_INTERRUPT_COMP)
  74. debug("COMP ");
  75. if (int_status & CDNS_I2C_INTERRUPT_DATA)
  76. debug("DATA ");
  77. if (int_status & CDNS_I2C_INTERRUPT_NACK)
  78. debug("NACK ");
  79. if (int_status & CDNS_I2C_INTERRUPT_TO)
  80. debug("TO ");
  81. if (int_status & CDNS_I2C_INTERRUPT_SLVRDY)
  82. debug("SLVRDY ");
  83. if (int_status & CDNS_I2C_INTERRUPT_RXOVF)
  84. debug("RXOVF ");
  85. if (int_status & CDNS_I2C_INTERRUPT_TXOVF)
  86. debug("TXOVF ");
  87. if (int_status & CDNS_I2C_INTERRUPT_RXUNF)
  88. debug("RXUNF ");
  89. if (int_status & CDNS_I2C_INTERRUPT_ARBLOST)
  90. debug("ARBLOST ");
  91. if (status & CDNS_I2C_STATUS_RXDV)
  92. debug("RXDV ");
  93. if (status & CDNS_I2C_STATUS_TXDV)
  94. debug("TXDV ");
  95. if (status & CDNS_I2C_STATUS_RXOVF)
  96. debug("RXOVF ");
  97. if (status & CDNS_I2C_STATUS_BA)
  98. debug("BA ");
  99. debug("TS%d ", readl(&cdns_i2c->transfer_size));
  100. debug("\n");
  101. }
  102. }
  103. #endif
  104. struct i2c_cdns_bus {
  105. int id;
  106. unsigned int input_freq;
  107. struct cdns_i2c_regs __iomem *regs; /* register base */
  108. int hold_flag;
  109. u32 quirks;
  110. };
  111. struct cdns_i2c_platform_data {
  112. u32 quirks;
  113. };
  114. /* Wait for an interrupt */
  115. static u32 cdns_i2c_wait(struct cdns_i2c_regs *cdns_i2c, u32 mask)
  116. {
  117. int timeout, int_status;
  118. for (timeout = 0; timeout < 100; timeout++) {
  119. int_status = readl(&cdns_i2c->interrupt_status);
  120. if (int_status & mask)
  121. break;
  122. udelay(100);
  123. }
  124. /* Clear interrupt status flags */
  125. writel(int_status & mask, &cdns_i2c->interrupt_status);
  126. return int_status & mask;
  127. }
  128. #define CDNS_I2C_DIVA_MAX 4
  129. #define CDNS_I2C_DIVB_MAX 64
  130. static int cdns_i2c_calc_divs(unsigned long *f, unsigned long input_clk,
  131. unsigned int *a, unsigned int *b)
  132. {
  133. unsigned long fscl = *f, best_fscl = *f, actual_fscl, temp;
  134. unsigned int div_a, div_b, calc_div_a = 0, calc_div_b = 0;
  135. unsigned int last_error, current_error;
  136. /* calculate (divisor_a+1) x (divisor_b+1) */
  137. temp = input_clk / (22 * fscl);
  138. /*
  139. * If the calculated value is negative or 0CDNS_I2C_DIVA_MAX,
  140. * the fscl input is out of range. Return error.
  141. */
  142. if (!temp || (temp > (CDNS_I2C_DIVA_MAX * CDNS_I2C_DIVB_MAX)))
  143. return -EINVAL;
  144. last_error = -1;
  145. for (div_a = 0; div_a < CDNS_I2C_DIVA_MAX; div_a++) {
  146. div_b = DIV_ROUND_UP(input_clk, 22 * fscl * (div_a + 1));
  147. if ((div_b < 1) || (div_b > CDNS_I2C_DIVB_MAX))
  148. continue;
  149. div_b--;
  150. actual_fscl = input_clk / (22 * (div_a + 1) * (div_b + 1));
  151. if (actual_fscl > fscl)
  152. continue;
  153. current_error = ((actual_fscl > fscl) ? (actual_fscl - fscl) :
  154. (fscl - actual_fscl));
  155. if (last_error > current_error) {
  156. calc_div_a = div_a;
  157. calc_div_b = div_b;
  158. best_fscl = actual_fscl;
  159. last_error = current_error;
  160. }
  161. }
  162. *a = calc_div_a;
  163. *b = calc_div_b;
  164. *f = best_fscl;
  165. return 0;
  166. }
  167. static int cdns_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
  168. {
  169. struct i2c_cdns_bus *bus = dev_get_priv(dev);
  170. u32 div_a = 0, div_b = 0;
  171. unsigned long speed_p = speed;
  172. int ret = 0;
  173. if (speed > 400000) {
  174. debug("%s, failed to set clock speed to %u\n", __func__,
  175. speed);
  176. return -EINVAL;
  177. }
  178. ret = cdns_i2c_calc_divs(&speed_p, bus->input_freq, &div_a, &div_b);
  179. if (ret)
  180. return ret;
  181. debug("%s: div_a: %d, div_b: %d, input freq: %d, speed: %d/%ld\n",
  182. __func__, div_a, div_b, bus->input_freq, speed, speed_p);
  183. writel((div_b << CDNS_I2C_CONTROL_DIV_B_SHIFT) |
  184. (div_a << CDNS_I2C_CONTROL_DIV_A_SHIFT), &bus->regs->control);
  185. /* Enable master mode, ack, and 7-bit addressing */
  186. setbits_le32(&bus->regs->control, CDNS_I2C_CONTROL_MS |
  187. CDNS_I2C_CONTROL_ACKEN | CDNS_I2C_CONTROL_NEA);
  188. return 0;
  189. }
  190. static int cdns_i2c_write_data(struct i2c_cdns_bus *i2c_bus, u32 addr, u8 *data,
  191. u32 len)
  192. {
  193. u8 *cur_data = data;
  194. struct cdns_i2c_regs *regs = i2c_bus->regs;
  195. /* Set the controller in Master transmit mode and clear FIFO */
  196. setbits_le32(&regs->control, CDNS_I2C_CONTROL_CLR_FIFO);
  197. clrbits_le32(&regs->control, CDNS_I2C_CONTROL_RW);
  198. /* Check message size against FIFO depth, and set hold bus bit
  199. * if it is greater than FIFO depth
  200. */
  201. if (len > CDNS_I2C_FIFO_DEPTH)
  202. setbits_le32(&regs->control, CDNS_I2C_CONTROL_HOLD);
  203. /* Clear the interrupts in status register */
  204. writel(0xFF, &regs->interrupt_status);
  205. writel(addr, &regs->address);
  206. while (len--) {
  207. writel(*(cur_data++), &regs->data);
  208. if (readl(&regs->transfer_size) == CDNS_I2C_FIFO_DEPTH) {
  209. if (!cdns_i2c_wait(regs, CDNS_I2C_INTERRUPT_COMP)) {
  210. /* Release the bus */
  211. clrbits_le32(&regs->control,
  212. CDNS_I2C_CONTROL_HOLD);
  213. return -ETIMEDOUT;
  214. }
  215. }
  216. }
  217. /* All done... release the bus */
  218. if (!i2c_bus->hold_flag)
  219. clrbits_le32(&regs->control, CDNS_I2C_CONTROL_HOLD);
  220. /* Wait for the address and data to be sent */
  221. if (!cdns_i2c_wait(regs, CDNS_I2C_INTERRUPT_COMP))
  222. return -ETIMEDOUT;
  223. return 0;
  224. }
  225. static inline bool cdns_is_hold_quirk(int hold_quirk, int curr_recv_count)
  226. {
  227. return hold_quirk && (curr_recv_count == CDNS_I2C_FIFO_DEPTH + 1);
  228. }
  229. static int cdns_i2c_read_data(struct i2c_cdns_bus *i2c_bus, u32 addr, u8 *data,
  230. u32 recv_count)
  231. {
  232. u8 *cur_data = data;
  233. struct cdns_i2c_regs *regs = i2c_bus->regs;
  234. int curr_recv_count;
  235. int updatetx, hold_quirk;
  236. /* Check the hardware can handle the requested bytes */
  237. if ((recv_count < 0))
  238. return -EINVAL;
  239. curr_recv_count = recv_count;
  240. /* Check for the message size against the FIFO depth */
  241. if (recv_count > CDNS_I2C_FIFO_DEPTH)
  242. setbits_le32(&regs->control, CDNS_I2C_CONTROL_HOLD);
  243. setbits_le32(&regs->control, CDNS_I2C_CONTROL_CLR_FIFO |
  244. CDNS_I2C_CONTROL_RW);
  245. if (recv_count > CDNS_I2C_TRANSFER_SIZE) {
  246. curr_recv_count = CDNS_I2C_TRANSFER_SIZE;
  247. writel(curr_recv_count, &regs->transfer_size);
  248. } else {
  249. writel(recv_count, &regs->transfer_size);
  250. }
  251. /* Start reading data */
  252. writel(addr, &regs->address);
  253. updatetx = recv_count > curr_recv_count;
  254. hold_quirk = (i2c_bus->quirks & CDNS_I2C_BROKEN_HOLD_BIT) && updatetx;
  255. while (recv_count) {
  256. while (readl(&regs->status) & CDNS_I2C_STATUS_RXDV) {
  257. if (recv_count < CDNS_I2C_FIFO_DEPTH &&
  258. !i2c_bus->hold_flag) {
  259. clrbits_le32(&regs->control,
  260. CDNS_I2C_CONTROL_HOLD);
  261. }
  262. *(cur_data)++ = readl(&regs->data);
  263. recv_count--;
  264. curr_recv_count--;
  265. if (cdns_is_hold_quirk(hold_quirk, curr_recv_count))
  266. break;
  267. }
  268. if (cdns_is_hold_quirk(hold_quirk, curr_recv_count)) {
  269. /* wait while fifo is full */
  270. while (readl(&regs->transfer_size) !=
  271. (curr_recv_count - CDNS_I2C_FIFO_DEPTH))
  272. ;
  273. /*
  274. * Check number of bytes to be received against maximum
  275. * transfer size and update register accordingly.
  276. */
  277. if ((recv_count - CDNS_I2C_FIFO_DEPTH) >
  278. CDNS_I2C_TRANSFER_SIZE) {
  279. writel(CDNS_I2C_TRANSFER_SIZE,
  280. &regs->transfer_size);
  281. curr_recv_count = CDNS_I2C_TRANSFER_SIZE +
  282. CDNS_I2C_FIFO_DEPTH;
  283. } else {
  284. writel(recv_count - CDNS_I2C_FIFO_DEPTH,
  285. &regs->transfer_size);
  286. curr_recv_count = recv_count;
  287. }
  288. } else if (recv_count && !hold_quirk && !curr_recv_count) {
  289. writel(addr, &regs->address);
  290. if (recv_count > CDNS_I2C_TRANSFER_SIZE) {
  291. writel(CDNS_I2C_TRANSFER_SIZE,
  292. &regs->transfer_size);
  293. curr_recv_count = CDNS_I2C_TRANSFER_SIZE;
  294. } else {
  295. writel(recv_count, &regs->transfer_size);
  296. curr_recv_count = recv_count;
  297. }
  298. }
  299. }
  300. /* Wait for the address and data to be sent */
  301. if (!cdns_i2c_wait(regs, CDNS_I2C_INTERRUPT_COMP))
  302. return -ETIMEDOUT;
  303. return 0;
  304. }
  305. static int cdns_i2c_xfer(struct udevice *dev, struct i2c_msg *msg,
  306. int nmsgs)
  307. {
  308. struct i2c_cdns_bus *i2c_bus = dev_get_priv(dev);
  309. int ret, count;
  310. bool hold_quirk;
  311. hold_quirk = !!(i2c_bus->quirks & CDNS_I2C_BROKEN_HOLD_BIT);
  312. if (nmsgs > 1) {
  313. /*
  314. * This controller does not give completion interrupt after a
  315. * master receive message if HOLD bit is set (repeated start),
  316. * resulting in SW timeout. Hence, if a receive message is
  317. * followed by any other message, an error is returned
  318. * indicating that this sequence is not supported.
  319. */
  320. for (count = 0; (count < nmsgs - 1) && hold_quirk; count++) {
  321. if (msg[count].flags & I2C_M_RD) {
  322. printf("Can't do repeated start after a receive message\n");
  323. return -EOPNOTSUPP;
  324. }
  325. }
  326. i2c_bus->hold_flag = 1;
  327. setbits_le32(&i2c_bus->regs->control, CDNS_I2C_CONTROL_HOLD);
  328. } else {
  329. i2c_bus->hold_flag = 0;
  330. }
  331. debug("i2c_xfer: %d messages\n", nmsgs);
  332. for (; nmsgs > 0; nmsgs--, msg++) {
  333. debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
  334. if (msg->flags & I2C_M_RD) {
  335. ret = cdns_i2c_read_data(i2c_bus, msg->addr, msg->buf,
  336. msg->len);
  337. } else {
  338. ret = cdns_i2c_write_data(i2c_bus, msg->addr, msg->buf,
  339. msg->len);
  340. }
  341. if (ret) {
  342. debug("i2c_write: error sending\n");
  343. return -EREMOTEIO;
  344. }
  345. }
  346. return 0;
  347. }
  348. static int cdns_i2c_ofdata_to_platdata(struct udevice *dev)
  349. {
  350. struct i2c_cdns_bus *i2c_bus = dev_get_priv(dev);
  351. struct cdns_i2c_platform_data *pdata =
  352. (struct cdns_i2c_platform_data *)dev_get_driver_data(dev);
  353. i2c_bus->regs = (struct cdns_i2c_regs *)devfdt_get_addr(dev);
  354. if (!i2c_bus->regs)
  355. return -ENOMEM;
  356. if (pdata)
  357. i2c_bus->quirks = pdata->quirks;
  358. i2c_bus->input_freq = 100000000; /* TODO hardcode input freq for now */
  359. return 0;
  360. }
  361. static const struct dm_i2c_ops cdns_i2c_ops = {
  362. .xfer = cdns_i2c_xfer,
  363. .set_bus_speed = cdns_i2c_set_bus_speed,
  364. };
  365. static const struct cdns_i2c_platform_data r1p10_i2c_def = {
  366. .quirks = CDNS_I2C_BROKEN_HOLD_BIT,
  367. };
  368. static const struct udevice_id cdns_i2c_of_match[] = {
  369. { .compatible = "cdns,i2c-r1p10", .data = (ulong)&r1p10_i2c_def },
  370. { .compatible = "cdns,i2c-r1p14" },
  371. { /* end of table */ }
  372. };
  373. U_BOOT_DRIVER(cdns_i2c) = {
  374. .name = "i2c-cdns",
  375. .id = UCLASS_I2C,
  376. .of_match = cdns_i2c_of_match,
  377. .ofdata_to_platdata = cdns_i2c_ofdata_to_platdata,
  378. .priv_auto_alloc_size = sizeof(struct i2c_cdns_bus),
  379. .ops = &cdns_i2c_ops,
  380. };