zynq_i2c.c 8.7 KB

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