i2c-cdns.c 12 KB

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