zynq_i2c.c 8.9 KB

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