omap1510_i2c.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. #ifdef CONFIG_DRIVER_OMAP1510_I2C
  22. static void wait_for_bb (void);
  23. static u16 wait_for_pin (void);
  24. void i2c_init (int speed, int slaveadd)
  25. {
  26. u16 scl;
  27. if (inw (I2C_CON) & I2C_CON_EN) {
  28. outw (0, I2C_CON);
  29. udelay (5000);
  30. }
  31. /* 12Mhz I2C module clock */
  32. outw (0, I2C_PSC);
  33. outw (I2C_CON_EN, I2C_CON);
  34. outw (0, I2C_SYSTEST);
  35. /* have to enable intrrupts or OMAP i2c module doesn't work */
  36. outw (I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
  37. I2C_IE_NACK_IE | I2C_IE_AL_IE, I2C_IE);
  38. scl = (12000000 / 2) / speed - 6;
  39. outw (scl, I2C_SCLL);
  40. outw (scl, I2C_SCLH);
  41. /* own address */
  42. outw (slaveadd, I2C_OA);
  43. outw (0, I2C_CNT);
  44. udelay (1000);
  45. }
  46. static int i2c_read_byte (u8 devaddr, u8 regoffset, u8 * value)
  47. {
  48. int i2c_error = 0;
  49. u16 status;
  50. /* wait until bus not busy */
  51. wait_for_bb ();
  52. /* one byte only */
  53. outw (1, I2C_CNT);
  54. /* set slave address */
  55. outw (devaddr, I2C_SA);
  56. /* no stop bit needed here */
  57. outw (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX, I2C_CON);
  58. status = wait_for_pin ();
  59. if (status & I2C_STAT_XRDY) {
  60. /* Important: have to use byte access */
  61. *(volatile u8 *) (I2C_DATA) = regoffset;
  62. udelay (20000);
  63. if (inw (I2C_STAT) & I2C_STAT_NACK) {
  64. i2c_error = 1;
  65. }
  66. } else {
  67. i2c_error = 1;
  68. }
  69. if (!i2c_error) {
  70. /* free bus, otherwise we can't use a combined transction */
  71. outw (0, I2C_CON);
  72. while (inw (I2C_STAT) || (inw (I2C_CON) & I2C_CON_MST)) {
  73. udelay (10000);
  74. /* Have to clear pending interrupt to clear I2C_STAT */
  75. inw (I2C_IV);
  76. }
  77. wait_for_bb ();
  78. /* set slave address */
  79. outw (devaddr, I2C_SA);
  80. /* read one byte from slave */
  81. outw (1, I2C_CNT);
  82. /* need stop bit here */
  83. outw (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP,
  84. I2C_CON);
  85. status = wait_for_pin ();
  86. if (status & I2C_STAT_RRDY) {
  87. *value = inw (I2C_DATA);
  88. udelay (20000);
  89. } else {
  90. i2c_error = 1;
  91. }
  92. if (!i2c_error) {
  93. outw (I2C_CON_EN, I2C_CON);
  94. while (inw (I2C_STAT)
  95. || (inw (I2C_CON) & I2C_CON_MST)) {
  96. udelay (10000);
  97. inw (I2C_IV);
  98. }
  99. }
  100. }
  101. return i2c_error;
  102. }
  103. static int i2c_write_byte (u8 devaddr, u8 regoffset, u8 value)
  104. {
  105. int i2c_error = 0;
  106. u16 status;
  107. /* wait until bus not busy */
  108. wait_for_bb ();
  109. /* two bytes */
  110. outw (2, I2C_CNT);
  111. /* set slave address */
  112. outw (devaddr, I2C_SA);
  113. /* stop bit needed here */
  114. outw (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
  115. I2C_CON_STP, I2C_CON);
  116. /* wait until state change */
  117. status = wait_for_pin ();
  118. if (status & I2C_STAT_XRDY) {
  119. /* send out two bytes */
  120. outw ((value << 8) + regoffset, I2C_DATA);
  121. /* must have enough delay to allow BB bit to go low */
  122. udelay (30000);
  123. if (inw (I2C_STAT) & I2C_STAT_NACK) {
  124. i2c_error = 1;
  125. }
  126. } else {
  127. i2c_error = 1;
  128. }
  129. if (!i2c_error) {
  130. outw (I2C_CON_EN, I2C_CON);
  131. while (inw (I2C_STAT) || (inw (I2C_CON) & I2C_CON_MST)) {
  132. udelay (1000);
  133. /* have to read to clear intrrupt */
  134. inw (I2C_IV);
  135. }
  136. }
  137. return i2c_error;
  138. }
  139. int i2c_probe (uchar chip)
  140. {
  141. int res = 1;
  142. if (chip == inw (I2C_OA)) {
  143. return res;
  144. }
  145. /* wait until bus not busy */
  146. wait_for_bb ();
  147. /* try to read one byte */
  148. outw (1, I2C_CNT);
  149. /* set slave address */
  150. outw (chip, I2C_SA);
  151. /* stop bit needed here */
  152. outw (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP, I2C_CON);
  153. /* enough delay for the NACK bit set */
  154. udelay (2000);
  155. if (!(inw (I2C_STAT) & I2C_STAT_NACK)) {
  156. res = 0;
  157. } else {
  158. outw (inw (I2C_CON) | I2C_CON_STP, I2C_CON);
  159. udelay (20);
  160. wait_for_bb ();
  161. }
  162. return res;
  163. }
  164. int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len)
  165. {
  166. int i;
  167. if (alen > 1) {
  168. printf ("I2C read: addr len %d not supported\n", alen);
  169. return 1;
  170. }
  171. if (addr + len > 256) {
  172. printf ("I2C read: address out of range\n");
  173. return 1;
  174. }
  175. for (i = 0; i < len; i++) {
  176. if (i2c_read_byte (chip, addr + i, &buffer[i])) {
  177. printf ("I2C read: I/O error\n");
  178. i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
  179. return 1;
  180. }
  181. }
  182. return 0;
  183. }
  184. int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
  185. {
  186. int i;
  187. if (alen > 1) {
  188. printf ("I2C read: addr len %d not supported\n", alen);
  189. return 1;
  190. }
  191. if (addr + len > 256) {
  192. printf ("I2C read: address out of range\n");
  193. return 1;
  194. }
  195. for (i = 0; i < len; i++) {
  196. if (i2c_write_byte (chip, addr + i, buffer[i])) {
  197. printf ("I2C read: I/O error\n");
  198. i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
  199. return 1;
  200. }
  201. }
  202. return 0;
  203. }
  204. static void wait_for_bb (void)
  205. {
  206. int timeout = 10;
  207. while ((inw (I2C_STAT) & I2C_STAT_BB) && timeout--) {
  208. inw (I2C_IV);
  209. udelay (1000);
  210. }
  211. if (timeout <= 0) {
  212. printf ("timed out in wait_for_bb: I2C_STAT=%x\n",
  213. inw (I2C_STAT));
  214. }
  215. }
  216. static u16 wait_for_pin (void)
  217. {
  218. u16 status, iv;
  219. int timeout = 10;
  220. do {
  221. udelay (1000);
  222. status = inw (I2C_STAT);
  223. iv = inw (I2C_IV);
  224. } while (!iv &&
  225. !(status &
  226. (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
  227. I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
  228. I2C_STAT_AL)) && timeout--);
  229. if (timeout <= 0) {
  230. printf ("timed out in wait_for_pin: I2C_STAT=%x\n",
  231. inw (I2C_STAT));
  232. }
  233. return status;
  234. }
  235. #endif /* CONFIG_DRIVER_OMAP1510_I2C */