zynq_i2c.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  26. * MA 02110-1301 USA
  27. */
  28. #include <common.h>
  29. #include <asm/io.h>
  30. #include <i2c.h>
  31. #include <asm/errno.h>
  32. #include <asm/arch/hardware.h>
  33. /* i2c register set */
  34. struct zynq_i2c_registers {
  35. u32 control;
  36. u32 status;
  37. u32 address;
  38. u32 data;
  39. u32 interrupt_status;
  40. u32 transfer_size;
  41. u32 slave_mon_pause;
  42. u32 time_out;
  43. u32 interrupt_mask;
  44. u32 interrupt_enable;
  45. u32 interrupt_disable;
  46. };
  47. /* Control register fields */
  48. #define ZYNQ_I2C_CONTROL_RW 0x00000001
  49. #define ZYNQ_I2C_CONTROL_MS 0x00000002
  50. #define ZYNQ_I2C_CONTROL_NEA 0x00000004
  51. #define ZYNQ_I2C_CONTROL_ACKEN 0x00000008
  52. #define ZYNQ_I2C_CONTROL_HOLD 0x00000010
  53. #define ZYNQ_I2C_CONTROL_SLVMON 0x00000020
  54. #define ZYNQ_I2C_CONTROL_CLR_FIFO 0x00000040
  55. #define ZYNQ_I2C_CONTROL_DIV_B_SHIFT 8
  56. #define ZYNQ_I2C_CONTROL_DIV_B_MASK 0x00003F00
  57. #define ZYNQ_I2C_CONTROL_DIV_A_SHIFT 14
  58. #define ZYNQ_I2C_CONTROL_DIV_A_MASK 0x0000C000
  59. /* Status register values */
  60. #define ZYNQ_I2C_STATUS_RXDV 0x00000020
  61. #define ZYNQ_I2C_STATUS_TXDV 0x00000040
  62. #define ZYNQ_I2C_STATUS_RXOVF 0x00000080
  63. #define ZYNQ_I2C_STATUS_BA 0x00000100
  64. /* Interrupt register fields */
  65. #define ZYNQ_I2C_INTERRUPT_COMP 0x00000001
  66. #define ZYNQ_I2C_INTERRUPT_DATA 0x00000002
  67. #define ZYNQ_I2C_INTERRUPT_NACK 0x00000004
  68. #define ZYNQ_I2C_INTERRUPT_TO 0x00000008
  69. #define ZYNQ_I2C_INTERRUPT_SLVRDY 0x00000010
  70. #define ZYNQ_I2C_INTERRUPT_RXOVF 0x00000020
  71. #define ZYNQ_I2C_INTERRUPT_TXOVF 0x00000040
  72. #define ZYNQ_I2C_INTERRUPT_RXUNF 0x00000080
  73. #define ZYNQ_I2C_INTERRUPT_ARBLOST 0x00000200
  74. #define ZYNQ_I2C_FIFO_DEPTH 16
  75. #define ZYNQ_I2C_TRANSFERT_SIZE_MAX 255 /* Controller transfer limit */
  76. #if defined(CONFIG_ZYNQ_I2C0)
  77. # define ZYNQ_I2C_BASE ZYNQ_I2C_BASEADDR0
  78. #else
  79. # define ZYNQ_I2C_BASE ZYNQ_I2C_BASEADDR1
  80. #endif
  81. static struct zynq_i2c_registers *zynq_i2c =
  82. (struct zynq_i2c_registers *)ZYNQ_I2C_BASE;
  83. /* I2C init called by cmd_i2c when doing 'i2c reset'. */
  84. void i2c_init(int requested_speed, int slaveadd)
  85. {
  86. /* 111MHz / ( (3 * 17) * 22 ) = ~100KHz */
  87. writel((16 << ZYNQ_I2C_CONTROL_DIV_B_SHIFT) |
  88. (2 << ZYNQ_I2C_CONTROL_DIV_A_SHIFT), &zynq_i2c->control);
  89. /* Enable master mode, ack, and 7-bit addressing */
  90. setbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_MS |
  91. ZYNQ_I2C_CONTROL_ACKEN | ZYNQ_I2C_CONTROL_NEA);
  92. }
  93. #ifdef DEBUG
  94. static void zynq_i2c_debug_status(void)
  95. {
  96. int int_status;
  97. int status;
  98. int_status = readl(&zynq_i2c->interrupt_status);
  99. status = readl(&zynq_i2c->status);
  100. if (int_status || status) {
  101. debug("Status: ");
  102. if (int_status & ZYNQ_I2C_INTERRUPT_COMP)
  103. debug("COMP ");
  104. if (int_status & ZYNQ_I2C_INTERRUPT_DATA)
  105. debug("DATA ");
  106. if (int_status & ZYNQ_I2C_INTERRUPT_NACK)
  107. debug("NACK ");
  108. if (int_status & ZYNQ_I2C_INTERRUPT_TO)
  109. debug("TO ");
  110. if (int_status & ZYNQ_I2C_INTERRUPT_SLVRDY)
  111. debug("SLVRDY ");
  112. if (int_status & ZYNQ_I2C_INTERRUPT_RXOVF)
  113. debug("RXOVF ");
  114. if (int_status & ZYNQ_I2C_INTERRUPT_TXOVF)
  115. debug("TXOVF ");
  116. if (int_status & ZYNQ_I2C_INTERRUPT_RXUNF)
  117. debug("RXUNF ");
  118. if (int_status & ZYNQ_I2C_INTERRUPT_ARBLOST)
  119. debug("ARBLOST ");
  120. if (status & ZYNQ_I2C_STATUS_RXDV)
  121. debug("RXDV ");
  122. if (status & ZYNQ_I2C_STATUS_TXDV)
  123. debug("TXDV ");
  124. if (status & ZYNQ_I2C_STATUS_RXOVF)
  125. debug("RXOVF ");
  126. if (status & ZYNQ_I2C_STATUS_BA)
  127. debug("BA ");
  128. debug("TS%d ", readl(&zynq_i2c->transfer_size));
  129. debug("\n");
  130. }
  131. }
  132. #endif
  133. /* Wait for an interrupt */
  134. static u32 zynq_i2c_wait(u32 mask)
  135. {
  136. int timeout, int_status;
  137. for (timeout = 0; timeout < 100; timeout++) {
  138. udelay(100);
  139. int_status = readl(&zynq_i2c->interrupt_status);
  140. if (int_status & mask)
  141. break;
  142. }
  143. #ifdef DEBUG
  144. zynq_i2c_debug_status();
  145. #endif
  146. /* Clear interrupt status flags */
  147. writel(int_status & mask, &zynq_i2c->interrupt_status);
  148. return int_status & mask;
  149. }
  150. /*
  151. * I2C probe called by cmd_i2c when doing 'i2c probe'.
  152. * Begin read, nak data byte, end.
  153. */
  154. int i2c_probe(u8 dev)
  155. {
  156. /* Attempt to read a byte */
  157. setbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_CLR_FIFO |
  158. ZYNQ_I2C_CONTROL_RW);
  159. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD);
  160. writel(0xFF, &zynq_i2c->interrupt_status);
  161. writel(dev, &zynq_i2c->address);
  162. writel(1, &zynq_i2c->transfer_size);
  163. return (zynq_i2c_wait(ZYNQ_I2C_INTERRUPT_COMP |
  164. ZYNQ_I2C_INTERRUPT_NACK) &
  165. ZYNQ_I2C_INTERRUPT_COMP) ? 0 : -ETIMEDOUT;
  166. }
  167. /*
  168. * I2C read called by cmd_i2c when doing 'i2c read' and by cmd_eeprom.c
  169. * Begin write, send address byte(s), begin read, receive data bytes, end.
  170. */
  171. int i2c_read(u8 dev, uint addr, int alen, u8 *data, int length)
  172. {
  173. u32 status;
  174. u32 i = 0;
  175. u8 *cur_data = data;
  176. /* Check the hardware can handle the requested bytes */
  177. if ((length < 0) || (length > ZYNQ_I2C_TRANSFERT_SIZE_MAX))
  178. return -EINVAL;
  179. /* Write the register address */
  180. setbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_CLR_FIFO |
  181. ZYNQ_I2C_CONTROL_HOLD);
  182. /*
  183. * Temporarily disable restart (by clearing hold)
  184. * It doesn't seem to work.
  185. */
  186. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_RW |
  187. ZYNQ_I2C_CONTROL_HOLD);
  188. writel(0xFF, &zynq_i2c->interrupt_status);
  189. while (alen--)
  190. writel(addr >> (8*alen), &zynq_i2c->data);
  191. writel(dev, &zynq_i2c->address);
  192. /* Wait for the address to be sent */
  193. if (!zynq_i2c_wait(ZYNQ_I2C_INTERRUPT_COMP)) {
  194. /* Release the bus */
  195. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD);
  196. return -ETIMEDOUT;
  197. }
  198. debug("Device acked address\n");
  199. setbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_CLR_FIFO |
  200. ZYNQ_I2C_CONTROL_RW);
  201. /* Start reading data */
  202. writel(dev, &zynq_i2c->address);
  203. writel(length, &zynq_i2c->transfer_size);
  204. /* Wait for data */
  205. do {
  206. status = zynq_i2c_wait(ZYNQ_I2C_INTERRUPT_COMP |
  207. ZYNQ_I2C_INTERRUPT_DATA);
  208. if (!status) {
  209. /* Release the bus */
  210. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD);
  211. return -ETIMEDOUT;
  212. }
  213. debug("Read %d bytes\n",
  214. length - readl(&zynq_i2c->transfer_size));
  215. for (; i < length - readl(&zynq_i2c->transfer_size); i++)
  216. *(cur_data++) = readl(&zynq_i2c->data);
  217. } while (readl(&zynq_i2c->transfer_size) != 0);
  218. /* All done... release the bus */
  219. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD);
  220. #ifdef DEBUG
  221. zynq_i2c_debug_status();
  222. #endif
  223. return 0;
  224. }
  225. /*
  226. * I2C write called by cmd_i2c when doing 'i2c write' and by cmd_eeprom.c
  227. * Begin write, send address byte(s), send data bytes, end.
  228. */
  229. int i2c_write(u8 dev, uint addr, int alen, u8 *data, int length)
  230. {
  231. u8 *cur_data = data;
  232. /* Write the register address */
  233. setbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_CLR_FIFO |
  234. ZYNQ_I2C_CONTROL_HOLD);
  235. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_RW);
  236. writel(0xFF, &zynq_i2c->interrupt_status);
  237. while (alen--)
  238. writel(addr >> (8*alen), &zynq_i2c->data);
  239. /* Start the tranfer */
  240. writel(dev, &zynq_i2c->address);
  241. if (!zynq_i2c_wait(ZYNQ_I2C_INTERRUPT_COMP)) {
  242. /* Release the bus */
  243. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD);
  244. return -ETIMEDOUT;
  245. }
  246. debug("Device acked address\n");
  247. while (length--) {
  248. writel(*(cur_data++), &zynq_i2c->data);
  249. if (readl(&zynq_i2c->transfer_size) == ZYNQ_I2C_FIFO_DEPTH) {
  250. if (!zynq_i2c_wait(ZYNQ_I2C_INTERRUPT_COMP)) {
  251. /* Release the bus */
  252. clrbits_le32(&zynq_i2c->control,
  253. ZYNQ_I2C_CONTROL_HOLD);
  254. return -ETIMEDOUT;
  255. }
  256. }
  257. }
  258. /* All done... release the bus */
  259. clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD);
  260. /* Wait for the address and data to be sent */
  261. if (!zynq_i2c_wait(ZYNQ_I2C_INTERRUPT_COMP))
  262. return -ETIMEDOUT;
  263. return 0;
  264. }
  265. int i2c_set_bus_num(unsigned int bus)
  266. {
  267. /* Only support bus 0 */
  268. if (bus > 0)
  269. return -1;
  270. return 0;
  271. }
  272. unsigned int i2c_get_bus_num(void)
  273. {
  274. /* Only support bus 0 */
  275. return 0;
  276. }