omap1510_i2c.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Basic I2C functions
  3. *
  4. * Copyright (c) 2003 Texas Instruments
  5. *
  6. * This package is free software; you can redistribute it and/or
  7. * modify it under the terms of the license found in the file
  8. * named COPYING that should have accompanied this file.
  9. *
  10. * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  11. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  12. * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * Author: Jian Zhang jzhang@ti.com, Texas Instruments
  15. *
  16. * Copyright (c) 2003 Wolfgang Denk, wd@denx.de
  17. * Rewritten to fit into the current U-Boot framework
  18. *
  19. */
  20. #include <common.h>
  21. static void wait_for_bb (void);
  22. static u16 wait_for_pin (void);
  23. void i2c_init (int speed, int slaveadd)
  24. {
  25. u16 scl;
  26. if (inw (I2C_CON) & I2C_CON_EN) {
  27. outw (0, I2C_CON);
  28. udelay (5000);
  29. }
  30. /* 12MHz I2C module clock */
  31. outw (0, I2C_PSC);
  32. outw (I2C_CON_EN, I2C_CON);
  33. outw (0, I2C_SYSTEST);
  34. /* have to enable intrrupts or OMAP i2c module doesn't work */
  35. outw (I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
  36. I2C_IE_NACK_IE | I2C_IE_AL_IE, I2C_IE);
  37. scl = (12000000 / 2) / speed - 6;
  38. outw (scl, I2C_SCLL);
  39. outw (scl, I2C_SCLH);
  40. /* own address */
  41. outw (slaveadd, I2C_OA);
  42. outw (0, I2C_CNT);
  43. udelay (1000);
  44. }
  45. static int i2c_read_byte (u8 devaddr, u8 regoffset, u8 * value)
  46. {
  47. int i2c_error = 0;
  48. u16 status;
  49. /* wait until bus not busy */
  50. wait_for_bb ();
  51. /* one byte only */
  52. outw (1, I2C_CNT);
  53. /* set slave address */
  54. outw (devaddr, I2C_SA);
  55. /* no stop bit needed here */
  56. outw (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX, I2C_CON);
  57. status = wait_for_pin ();
  58. if (status & I2C_STAT_XRDY) {
  59. /* Important: have to use byte access */
  60. *(volatile u8 *) (I2C_DATA) = regoffset;
  61. udelay (20000);
  62. if (inw (I2C_STAT) & I2C_STAT_NACK) {
  63. i2c_error = 1;
  64. }
  65. } else {
  66. i2c_error = 1;
  67. }
  68. if (!i2c_error) {
  69. /* free bus, otherwise we can't use a combined transction */
  70. outw (0, I2C_CON);
  71. while (inw (I2C_STAT) || (inw (I2C_CON) & I2C_CON_MST)) {
  72. udelay (10000);
  73. /* Have to clear pending interrupt to clear I2C_STAT */
  74. inw (I2C_IV);
  75. }
  76. wait_for_bb ();
  77. /* set slave address */
  78. outw (devaddr, I2C_SA);
  79. /* read one byte from slave */
  80. outw (1, I2C_CNT);
  81. /* need stop bit here */
  82. outw (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP,
  83. I2C_CON);
  84. status = wait_for_pin ();
  85. if (status & I2C_STAT_RRDY) {
  86. *value = inw (I2C_DATA);
  87. udelay (20000);
  88. } else {
  89. i2c_error = 1;
  90. }
  91. if (!i2c_error) {
  92. outw (I2C_CON_EN, I2C_CON);
  93. while (inw (I2C_STAT)
  94. || (inw (I2C_CON) & I2C_CON_MST)) {
  95. udelay (10000);
  96. inw (I2C_IV);
  97. }
  98. }
  99. }
  100. return i2c_error;
  101. }
  102. static int i2c_write_byte (u8 devaddr, u8 regoffset, u8 value)
  103. {
  104. int i2c_error = 0;
  105. u16 status;
  106. /* wait until bus not busy */
  107. wait_for_bb ();
  108. /* two bytes */
  109. outw (2, I2C_CNT);
  110. /* set slave address */
  111. outw (devaddr, I2C_SA);
  112. /* stop bit needed here */
  113. outw (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
  114. I2C_CON_STP, I2C_CON);
  115. /* wait until state change */
  116. status = wait_for_pin ();
  117. if (status & I2C_STAT_XRDY) {
  118. /* send out two bytes */
  119. outw ((value << 8) + regoffset, I2C_DATA);
  120. /* must have enough delay to allow BB bit to go low */
  121. udelay (30000);
  122. if (inw (I2C_STAT) & I2C_STAT_NACK) {
  123. i2c_error = 1;
  124. }
  125. } else {
  126. i2c_error = 1;
  127. }
  128. if (!i2c_error) {
  129. outw (I2C_CON_EN, I2C_CON);
  130. while (inw (I2C_STAT) || (inw (I2C_CON) & I2C_CON_MST)) {
  131. udelay (1000);
  132. /* have to read to clear intrrupt */
  133. inw (I2C_IV);
  134. }
  135. }
  136. return i2c_error;
  137. }
  138. int i2c_probe (uchar chip)
  139. {
  140. int res = 1;
  141. if (chip == inw (I2C_OA)) {
  142. return res;
  143. }
  144. /* wait until bus not busy */
  145. wait_for_bb ();
  146. /* try to read one byte */
  147. outw (1, I2C_CNT);
  148. /* set slave address */
  149. outw (chip, I2C_SA);
  150. /* stop bit needed here */
  151. outw (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP, I2C_CON);
  152. /* enough delay for the NACK bit set */
  153. udelay (2000);
  154. if (!(inw (I2C_STAT) & I2C_STAT_NACK)) {
  155. res = 0;
  156. } else {
  157. outw (inw (I2C_CON) | I2C_CON_STP, I2C_CON);
  158. udelay (20);
  159. wait_for_bb ();
  160. }
  161. return res;
  162. }
  163. int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len)
  164. {
  165. int i;
  166. if (alen > 1) {
  167. printf ("I2C read: addr len %d not supported\n", alen);
  168. return 1;
  169. }
  170. if (addr + len > 256) {
  171. printf ("I2C read: address out of range\n");
  172. return 1;
  173. }
  174. for (i = 0; i < len; i++) {
  175. if (i2c_read_byte (chip, addr + i, &buffer[i])) {
  176. printf ("I2C read: I/O error\n");
  177. i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  178. return 1;
  179. }
  180. }
  181. return 0;
  182. }
  183. int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
  184. {
  185. int i;
  186. if (alen > 1) {
  187. printf ("I2C read: addr len %d not supported\n", alen);
  188. return 1;
  189. }
  190. if (addr + len > 256) {
  191. printf ("I2C read: address out of range\n");
  192. return 1;
  193. }
  194. for (i = 0; i < len; i++) {
  195. if (i2c_write_byte (chip, addr + i, buffer[i])) {
  196. printf ("I2C read: I/O error\n");
  197. i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  198. return 1;
  199. }
  200. }
  201. return 0;
  202. }
  203. static void wait_for_bb (void)
  204. {
  205. int timeout = 10;
  206. while ((inw (I2C_STAT) & I2C_STAT_BB) && timeout--) {
  207. inw (I2C_IV);
  208. udelay (1000);
  209. }
  210. if (timeout <= 0) {
  211. printf ("timed out in wait_for_bb: I2C_STAT=%x\n",
  212. inw (I2C_STAT));
  213. }
  214. }
  215. static u16 wait_for_pin (void)
  216. {
  217. u16 status, iv;
  218. int timeout = 10;
  219. do {
  220. udelay (1000);
  221. status = inw (I2C_STAT);
  222. iv = inw (I2C_IV);
  223. } while (!iv &&
  224. !(status &
  225. (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
  226. I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
  227. I2C_STAT_AL)) && timeout--);
  228. if (timeout <= 0) {
  229. printf ("timed out in wait_for_pin: I2C_STAT=%x\n",
  230. inw (I2C_STAT));
  231. }
  232. return status;
  233. }