zynq_i2c.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for the Zynq-7000 PS I2C controller
  4. * IP from Cadence (ID T-CS-PE-0007-100, Version R1p10f2)
  5. *
  6. * Author: Joe Hershberger <joe.hershberger@ni.com>
  7. * Copyright (c) 2012 Joe Hershberger.
  8. *
  9. * Copyright (c) 2012-2013 Xilinx, Michal Simek
  10. *
  11. * NOTE: This driver should be converted to driver model before June 2017.
  12. * Please see doc/driver-model/i2c-howto.txt for instructions.
  13. */
  14. #include <common.h>
  15. #include <asm/io.h>
  16. #include <i2c.h>
  17. #include <linux/errno.h>
  18. #include <asm/arch/hardware.h>
  19. /* i2c register set */
  20. struct zynq_i2c_registers {
  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 ZYNQ_I2C_CONTROL_RW 0x00000001
  35. #define ZYNQ_I2C_CONTROL_MS 0x00000002
  36. #define ZYNQ_I2C_CONTROL_NEA 0x00000004
  37. #define ZYNQ_I2C_CONTROL_ACKEN 0x00000008
  38. #define ZYNQ_I2C_CONTROL_HOLD 0x00000010
  39. #define ZYNQ_I2C_CONTROL_SLVMON 0x00000020
  40. #define ZYNQ_I2C_CONTROL_CLR_FIFO 0x00000040
  41. #define ZYNQ_I2C_CONTROL_DIV_B_SHIFT 8
  42. #define ZYNQ_I2C_CONTROL_DIV_B_MASK 0x00003F00
  43. #define ZYNQ_I2C_CONTROL_DIV_A_SHIFT 14
  44. #define ZYNQ_I2C_CONTROL_DIV_A_MASK 0x0000C000
  45. /* Status register values */
  46. #define ZYNQ_I2C_STATUS_RXDV 0x00000020
  47. #define ZYNQ_I2C_STATUS_TXDV 0x00000040
  48. #define ZYNQ_I2C_STATUS_RXOVF 0x00000080
  49. #define ZYNQ_I2C_STATUS_BA 0x00000100
  50. /* Interrupt register fields */
  51. #define ZYNQ_I2C_INTERRUPT_COMP 0x00000001
  52. #define ZYNQ_I2C_INTERRUPT_DATA 0x00000002
  53. #define ZYNQ_I2C_INTERRUPT_NACK 0x00000004
  54. #define ZYNQ_I2C_INTERRUPT_TO 0x00000008
  55. #define ZYNQ_I2C_INTERRUPT_SLVRDY 0x00000010
  56. #define ZYNQ_I2C_INTERRUPT_RXOVF 0x00000020
  57. #define ZYNQ_I2C_INTERRUPT_TXOVF 0x00000040
  58. #define ZYNQ_I2C_INTERRUPT_RXUNF 0x00000080
  59. #define ZYNQ_I2C_INTERRUPT_ARBLOST 0x00000200
  60. #define ZYNQ_I2C_FIFO_DEPTH 16
  61. #define ZYNQ_I2C_TRANSFERT_SIZE_MAX 255 /* Controller transfer limit */
  62. static struct zynq_i2c_registers *i2c_select(struct i2c_adapter *adap)
  63. {
  64. return adap->hwadapnr ?
  65. /* Zynq PS I2C1 */
  66. (struct zynq_i2c_registers *)ZYNQ_I2C_BASEADDR1 :
  67. /* Zynq PS I2C0 */
  68. (struct zynq_i2c_registers *)ZYNQ_I2C_BASEADDR0;
  69. }
  70. /* I2C init called by cmd_i2c when doing 'i2c reset'. */
  71. static void zynq_i2c_init(struct i2c_adapter *adap, int requested_speed,
  72. int slaveadd)
  73. {
  74. struct zynq_i2c_registers *zynq_i2c = i2c_select(adap);
  75. /* 111MHz / ( (3 * 17) * 22 ) = ~100KHz */
  76. writel((16 << ZYNQ_I2C_CONTROL_DIV_B_SHIFT) |
  77. (2 << ZYNQ_I2C_CONTROL_DIV_A_SHIFT), &zynq_i2c->control);
  78. /* Enable master mode, ack, and 7-bit addressing */
  79. setbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_MS |
  80. ZYNQ_I2C_CONTROL_ACKEN | ZYNQ_I2C_CONTROL_NEA);
  81. }
  82. #ifdef DEBUG
  83. static void zynq_i2c_debug_status(struct zynq_i2c_registers *zynq_i2c)
  84. {
  85. int int_status;
  86. int status;
  87. int_status = readl(&zynq_i2c->interrupt_status);
  88. status = readl(&zynq_i2c->status);
  89. if (int_status || status) {
  90. debug("Status: ");
  91. if (int_status & ZYNQ_I2C_INTERRUPT_COMP)
  92. debug("COMP ");
  93. if (int_status & ZYNQ_I2C_INTERRUPT_DATA)
  94. debug("DATA ");
  95. if (int_status & ZYNQ_I2C_INTERRUPT_NACK)
  96. debug("NACK ");
  97. if (int_status & ZYNQ_I2C_INTERRUPT_TO)
  98. debug("TO ");
  99. if (int_status & ZYNQ_I2C_INTERRUPT_SLVRDY)
  100. debug("SLVRDY ");
  101. if (int_status & ZYNQ_I2C_INTERRUPT_RXOVF)
  102. debug("RXOVF ");
  103. if (int_status & ZYNQ_I2C_INTERRUPT_TXOVF)
  104. debug("TXOVF ");
  105. if (int_status & ZYNQ_I2C_INTERRUPT_RXUNF)
  106. debug("RXUNF ");
  107. if (int_status & ZYNQ_I2C_INTERRUPT_ARBLOST)
  108. debug("ARBLOST ");
  109. if (status & ZYNQ_I2C_STATUS_RXDV)
  110. debug("RXDV ");
  111. if (status & ZYNQ_I2C_STATUS_TXDV)
  112. debug("TXDV ");
  113. if (status & ZYNQ_I2C_STATUS_RXOVF)
  114. debug("RXOVF ");
  115. if (status & ZYNQ_I2C_STATUS_BA)
  116. debug("BA ");
  117. debug("TS%d ", readl(&zynq_i2c->transfer_size));
  118. debug("\n");
  119. }
  120. }
  121. #endif
  122. /* Wait for an interrupt */
  123. static u32 zynq_i2c_wait(struct zynq_i2c_registers *zynq_i2c, u32 mask)
  124. {
  125. int timeout, int_status;
  126. for (timeout = 0; timeout < 100; timeout++) {
  127. udelay(100);
  128. int_status = readl(&zynq_i2c->interrupt_status);
  129. if (int_status & mask)
  130. break;
  131. }
  132. #ifdef DEBUG
  133. zynq_i2c_debug_status(zynq_i2c);
  134. #endif
  135. /* Clear interrupt status flags */
  136. writel(int_status & mask, &zynq_i2c->interrupt_status);
  137. return int_status & mask;
  138. }
  139. /*
  140. * I2C probe called by cmd_i2c when doing 'i2c probe'.
  141. * Begin read, nak data byte, end.
  142. */
  143. static int zynq_i2c_probe(struct i2c_adapter *adap, u8 dev)
  144. {
  145. struct zynq_i2c_registers *zynq_i2c = i2c_select(adap);
  146. /* Attempt to read a byte */
  147. setbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_CLR_FIFO |
  148. ZYNQ_I2C_CONTROL_RW);
  149. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD);
  150. writel(0xFF, &zynq_i2c->interrupt_status);
  151. writel(dev, &zynq_i2c->address);
  152. writel(1, &zynq_i2c->transfer_size);
  153. return (zynq_i2c_wait(zynq_i2c, ZYNQ_I2C_INTERRUPT_COMP |
  154. ZYNQ_I2C_INTERRUPT_NACK) &
  155. ZYNQ_I2C_INTERRUPT_COMP) ? 0 : -ETIMEDOUT;
  156. }
  157. /*
  158. * I2C read called by cmd_i2c when doing 'i2c read' and by cmd_eeprom.c
  159. * Begin write, send address byte(s), begin read, receive data bytes, end.
  160. */
  161. static int zynq_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
  162. int alen, u8 *data, int length)
  163. {
  164. u32 status;
  165. u32 i = 0;
  166. u8 *cur_data = data;
  167. struct zynq_i2c_registers *zynq_i2c = i2c_select(adap);
  168. /* Check the hardware can handle the requested bytes */
  169. if ((length < 0) || (length > ZYNQ_I2C_TRANSFERT_SIZE_MAX))
  170. return -EINVAL;
  171. /* Write the register address */
  172. setbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_CLR_FIFO |
  173. ZYNQ_I2C_CONTROL_HOLD);
  174. /*
  175. * Temporarily disable restart (by clearing hold)
  176. * It doesn't seem to work.
  177. */
  178. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD);
  179. writel(0xFF, &zynq_i2c->interrupt_status);
  180. if (alen) {
  181. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_RW);
  182. writel(dev, &zynq_i2c->address);
  183. while (alen--)
  184. writel(addr >> (8 * alen), &zynq_i2c->data);
  185. /* Wait for the address to be sent */
  186. if (!zynq_i2c_wait(zynq_i2c, ZYNQ_I2C_INTERRUPT_COMP)) {
  187. /* Release the bus */
  188. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD);
  189. return -ETIMEDOUT;
  190. }
  191. debug("Device acked address\n");
  192. }
  193. setbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_CLR_FIFO |
  194. ZYNQ_I2C_CONTROL_RW);
  195. /* Start reading data */
  196. writel(dev, &zynq_i2c->address);
  197. writel(length, &zynq_i2c->transfer_size);
  198. /* Wait for data */
  199. do {
  200. status = zynq_i2c_wait(zynq_i2c, ZYNQ_I2C_INTERRUPT_COMP |
  201. ZYNQ_I2C_INTERRUPT_DATA);
  202. if (!status) {
  203. /* Release the bus */
  204. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD);
  205. return -ETIMEDOUT;
  206. }
  207. debug("Read %d bytes\n",
  208. length - readl(&zynq_i2c->transfer_size));
  209. for (; i < length - readl(&zynq_i2c->transfer_size); i++)
  210. *(cur_data++) = readl(&zynq_i2c->data);
  211. } while (readl(&zynq_i2c->transfer_size) != 0);
  212. /* All done... release the bus */
  213. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD);
  214. #ifdef DEBUG
  215. zynq_i2c_debug_status(zynq_i2c);
  216. #endif
  217. return 0;
  218. }
  219. /*
  220. * I2C write called by cmd_i2c when doing 'i2c write' and by cmd_eeprom.c
  221. * Begin write, send address byte(s), send data bytes, end.
  222. */
  223. static int zynq_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
  224. int alen, u8 *data, int length)
  225. {
  226. u8 *cur_data = data;
  227. struct zynq_i2c_registers *zynq_i2c = i2c_select(adap);
  228. /* Write the register address */
  229. setbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_CLR_FIFO |
  230. ZYNQ_I2C_CONTROL_HOLD);
  231. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_RW);
  232. writel(0xFF, &zynq_i2c->interrupt_status);
  233. writel(dev, &zynq_i2c->address);
  234. if (alen) {
  235. while (alen--)
  236. writel(addr >> (8 * alen), &zynq_i2c->data);
  237. /* Start the tranfer */
  238. if (!zynq_i2c_wait(zynq_i2c, ZYNQ_I2C_INTERRUPT_COMP)) {
  239. /* Release the bus */
  240. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD);
  241. return -ETIMEDOUT;
  242. }
  243. debug("Device acked address\n");
  244. }
  245. while (length--) {
  246. writel(*(cur_data++), &zynq_i2c->data);
  247. if (readl(&zynq_i2c->transfer_size) == ZYNQ_I2C_FIFO_DEPTH) {
  248. if (!zynq_i2c_wait(zynq_i2c, ZYNQ_I2C_INTERRUPT_COMP)) {
  249. /* Release the bus */
  250. clrbits_le32(&zynq_i2c->control,
  251. ZYNQ_I2C_CONTROL_HOLD);
  252. return -ETIMEDOUT;
  253. }
  254. }
  255. }
  256. /* All done... release the bus */
  257. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD);
  258. /* Wait for the address and data to be sent */
  259. if (!zynq_i2c_wait(zynq_i2c, ZYNQ_I2C_INTERRUPT_COMP))
  260. return -ETIMEDOUT;
  261. return 0;
  262. }
  263. static unsigned int zynq_i2c_set_bus_speed(struct i2c_adapter *adap,
  264. unsigned int speed)
  265. {
  266. if (speed != 1000000)
  267. return -EINVAL;
  268. return 0;
  269. }
  270. #ifdef CONFIG_ZYNQ_I2C0
  271. U_BOOT_I2C_ADAP_COMPLETE(zynq_0, zynq_i2c_init, zynq_i2c_probe, zynq_i2c_read,
  272. zynq_i2c_write, zynq_i2c_set_bus_speed,
  273. CONFIG_SYS_I2C_ZYNQ_SPEED, CONFIG_SYS_I2C_ZYNQ_SLAVE,
  274. 0)
  275. #endif
  276. #ifdef CONFIG_ZYNQ_I2C1
  277. U_BOOT_I2C_ADAP_COMPLETE(zynq_1, zynq_i2c_init, zynq_i2c_probe, zynq_i2c_read,
  278. zynq_i2c_write, zynq_i2c_set_bus_speed,
  279. CONFIG_SYS_I2C_ZYNQ_SPEED, CONFIG_SYS_I2C_ZYNQ_SLAVE,
  280. 1)
  281. #endif