zynq_i2c.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. #if defined(CONFIG_ZYNQ_I2C0)
  61. # define ZYNQ_I2C_BASE ZYNQ_I2C_BASEADDR0
  62. #else
  63. # define ZYNQ_I2C_BASE ZYNQ_I2C_BASEADDR1
  64. #endif
  65. static struct zynq_i2c_registers *zynq_i2c =
  66. (struct zynq_i2c_registers *)ZYNQ_I2C_BASE;
  67. /* I2C init called by cmd_i2c when doing 'i2c reset'. */
  68. void i2c_init(int requested_speed, int slaveadd)
  69. {
  70. /* 111MHz / ( (3 * 17) * 22 ) = ~100KHz */
  71. writel((16 << ZYNQ_I2C_CONTROL_DIV_B_SHIFT) |
  72. (2 << ZYNQ_I2C_CONTROL_DIV_A_SHIFT), &zynq_i2c->control);
  73. /* Enable master mode, ack, and 7-bit addressing */
  74. setbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_MS |
  75. ZYNQ_I2C_CONTROL_ACKEN | ZYNQ_I2C_CONTROL_NEA);
  76. }
  77. #ifdef DEBUG
  78. static void zynq_i2c_debug_status(void)
  79. {
  80. int int_status;
  81. int status;
  82. int_status = readl(&zynq_i2c->interrupt_status);
  83. status = readl(&zynq_i2c->status);
  84. if (int_status || status) {
  85. debug("Status: ");
  86. if (int_status & ZYNQ_I2C_INTERRUPT_COMP)
  87. debug("COMP ");
  88. if (int_status & ZYNQ_I2C_INTERRUPT_DATA)
  89. debug("DATA ");
  90. if (int_status & ZYNQ_I2C_INTERRUPT_NACK)
  91. debug("NACK ");
  92. if (int_status & ZYNQ_I2C_INTERRUPT_TO)
  93. debug("TO ");
  94. if (int_status & ZYNQ_I2C_INTERRUPT_SLVRDY)
  95. debug("SLVRDY ");
  96. if (int_status & ZYNQ_I2C_INTERRUPT_RXOVF)
  97. debug("RXOVF ");
  98. if (int_status & ZYNQ_I2C_INTERRUPT_TXOVF)
  99. debug("TXOVF ");
  100. if (int_status & ZYNQ_I2C_INTERRUPT_RXUNF)
  101. debug("RXUNF ");
  102. if (int_status & ZYNQ_I2C_INTERRUPT_ARBLOST)
  103. debug("ARBLOST ");
  104. if (status & ZYNQ_I2C_STATUS_RXDV)
  105. debug("RXDV ");
  106. if (status & ZYNQ_I2C_STATUS_TXDV)
  107. debug("TXDV ");
  108. if (status & ZYNQ_I2C_STATUS_RXOVF)
  109. debug("RXOVF ");
  110. if (status & ZYNQ_I2C_STATUS_BA)
  111. debug("BA ");
  112. debug("TS%d ", readl(&zynq_i2c->transfer_size));
  113. debug("\n");
  114. }
  115. }
  116. #endif
  117. /* Wait for an interrupt */
  118. static u32 zynq_i2c_wait(u32 mask)
  119. {
  120. int timeout, int_status;
  121. for (timeout = 0; timeout < 100; timeout++) {
  122. udelay(100);
  123. int_status = readl(&zynq_i2c->interrupt_status);
  124. if (int_status & mask)
  125. break;
  126. }
  127. #ifdef DEBUG
  128. zynq_i2c_debug_status();
  129. #endif
  130. /* Clear interrupt status flags */
  131. writel(int_status & mask, &zynq_i2c->interrupt_status);
  132. return int_status & mask;
  133. }
  134. /*
  135. * I2C probe called by cmd_i2c when doing 'i2c probe'.
  136. * Begin read, nak data byte, end.
  137. */
  138. int i2c_probe(u8 dev)
  139. {
  140. /* Attempt to read a byte */
  141. setbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_CLR_FIFO |
  142. ZYNQ_I2C_CONTROL_RW);
  143. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD);
  144. writel(0xFF, &zynq_i2c->interrupt_status);
  145. writel(dev, &zynq_i2c->address);
  146. writel(1, &zynq_i2c->transfer_size);
  147. return (zynq_i2c_wait(ZYNQ_I2C_INTERRUPT_COMP |
  148. ZYNQ_I2C_INTERRUPT_NACK) &
  149. ZYNQ_I2C_INTERRUPT_COMP) ? 0 : -ETIMEDOUT;
  150. }
  151. /*
  152. * I2C read called by cmd_i2c when doing 'i2c read' and by cmd_eeprom.c
  153. * Begin write, send address byte(s), begin read, receive data bytes, end.
  154. */
  155. int i2c_read(u8 dev, uint addr, int alen, u8 *data, int length)
  156. {
  157. u32 status;
  158. u32 i = 0;
  159. u8 *cur_data = data;
  160. /* Check the hardware can handle the requested bytes */
  161. if ((length < 0) || (length > ZYNQ_I2C_TRANSFERT_SIZE_MAX))
  162. return -EINVAL;
  163. /* Write the register address */
  164. setbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_CLR_FIFO |
  165. ZYNQ_I2C_CONTROL_HOLD);
  166. /*
  167. * Temporarily disable restart (by clearing hold)
  168. * It doesn't seem to work.
  169. */
  170. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_RW |
  171. ZYNQ_I2C_CONTROL_HOLD);
  172. writel(0xFF, &zynq_i2c->interrupt_status);
  173. while (alen--)
  174. writel(addr >> (8*alen), &zynq_i2c->data);
  175. writel(dev, &zynq_i2c->address);
  176. /* Wait for the address to be sent */
  177. if (!zynq_i2c_wait(ZYNQ_I2C_INTERRUPT_COMP)) {
  178. /* Release the bus */
  179. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD);
  180. return -ETIMEDOUT;
  181. }
  182. debug("Device acked address\n");
  183. setbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_CLR_FIFO |
  184. ZYNQ_I2C_CONTROL_RW);
  185. /* Start reading data */
  186. writel(dev, &zynq_i2c->address);
  187. writel(length, &zynq_i2c->transfer_size);
  188. /* Wait for data */
  189. do {
  190. status = zynq_i2c_wait(ZYNQ_I2C_INTERRUPT_COMP |
  191. ZYNQ_I2C_INTERRUPT_DATA);
  192. if (!status) {
  193. /* Release the bus */
  194. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD);
  195. return -ETIMEDOUT;
  196. }
  197. debug("Read %d bytes\n",
  198. length - readl(&zynq_i2c->transfer_size));
  199. for (; i < length - readl(&zynq_i2c->transfer_size); i++)
  200. *(cur_data++) = readl(&zynq_i2c->data);
  201. } while (readl(&zynq_i2c->transfer_size) != 0);
  202. /* All done... release the bus */
  203. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD);
  204. #ifdef DEBUG
  205. zynq_i2c_debug_status();
  206. #endif
  207. return 0;
  208. }
  209. /*
  210. * I2C write called by cmd_i2c when doing 'i2c write' and by cmd_eeprom.c
  211. * Begin write, send address byte(s), send data bytes, end.
  212. */
  213. int i2c_write(u8 dev, uint addr, int alen, u8 *data, int length)
  214. {
  215. u8 *cur_data = data;
  216. /* Write the register address */
  217. setbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_CLR_FIFO |
  218. ZYNQ_I2C_CONTROL_HOLD);
  219. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_RW);
  220. writel(0xFF, &zynq_i2c->interrupt_status);
  221. while (alen--)
  222. writel(addr >> (8*alen), &zynq_i2c->data);
  223. /* Start the tranfer */
  224. writel(dev, &zynq_i2c->address);
  225. if (!zynq_i2c_wait(ZYNQ_I2C_INTERRUPT_COMP)) {
  226. /* Release the bus */
  227. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD);
  228. return -ETIMEDOUT;
  229. }
  230. debug("Device acked address\n");
  231. while (length--) {
  232. writel(*(cur_data++), &zynq_i2c->data);
  233. if (readl(&zynq_i2c->transfer_size) == ZYNQ_I2C_FIFO_DEPTH) {
  234. if (!zynq_i2c_wait(ZYNQ_I2C_INTERRUPT_COMP)) {
  235. /* Release the bus */
  236. clrbits_le32(&zynq_i2c->control,
  237. ZYNQ_I2C_CONTROL_HOLD);
  238. return -ETIMEDOUT;
  239. }
  240. }
  241. }
  242. /* All done... release the bus */
  243. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD);
  244. /* Wait for the address and data to be sent */
  245. if (!zynq_i2c_wait(ZYNQ_I2C_INTERRUPT_COMP))
  246. return -ETIMEDOUT;
  247. return 0;
  248. }
  249. int i2c_set_bus_num(unsigned int bus)
  250. {
  251. /* Only support bus 0 */
  252. if (bus > 0)
  253. return -1;
  254. return 0;
  255. }
  256. unsigned int i2c_get_bus_num(void)
  257. {
  258. /* Only support bus 0 */
  259. return 0;
  260. }