davinci_i2c.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * TI DaVinci (TMS320DM644x) I2C driver.
  3. *
  4. * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
  5. *
  6. * --------------------------------------------------------
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <i2c.h>
  28. #include <asm/arch/hardware.h>
  29. #include <asm/arch/i2c_defs.h>
  30. #define CHECK_NACK() \
  31. do {\
  32. if (tmp & (I2C_TIMEOUT | I2C_STAT_NACK)) {\
  33. REG(I2C_CON) = 0;\
  34. return(1);\
  35. }\
  36. } while (0)
  37. static int wait_for_bus(void)
  38. {
  39. int stat, timeout;
  40. REG(I2C_STAT) = 0xffff;
  41. for (timeout = 0; timeout < 10; timeout++) {
  42. if (!((stat = REG(I2C_STAT)) & I2C_STAT_BB)) {
  43. REG(I2C_STAT) = 0xffff;
  44. return(0);
  45. }
  46. REG(I2C_STAT) = stat;
  47. udelay(50000);
  48. }
  49. REG(I2C_STAT) = 0xffff;
  50. return(1);
  51. }
  52. static int poll_i2c_irq(int mask)
  53. {
  54. int stat, timeout;
  55. for (timeout = 0; timeout < 10; timeout++) {
  56. udelay(1000);
  57. stat = REG(I2C_STAT);
  58. if (stat & mask) {
  59. return(stat);
  60. }
  61. }
  62. REG(I2C_STAT) = 0xffff;
  63. return(stat | I2C_TIMEOUT);
  64. }
  65. void flush_rx(void)
  66. {
  67. while (1) {
  68. if (!(REG(I2C_STAT) & I2C_STAT_RRDY))
  69. break;
  70. REG(I2C_DRR);
  71. REG(I2C_STAT) = I2C_STAT_RRDY;
  72. udelay(1000);
  73. }
  74. }
  75. void i2c_init(int speed, int slaveadd)
  76. {
  77. u_int32_t div, psc;
  78. if (REG(I2C_CON) & I2C_CON_EN) {
  79. REG(I2C_CON) = 0;
  80. udelay (50000);
  81. }
  82. psc = 2;
  83. div = (CONFIG_SYS_HZ_CLOCK / ((psc + 1) * speed)) - 10; /* SCLL + SCLH */
  84. REG(I2C_PSC) = psc; /* 27MHz / (2 + 1) = 9MHz */
  85. REG(I2C_SCLL) = (div * 50) / 100; /* 50% Duty */
  86. REG(I2C_SCLH) = div - REG(I2C_SCLL);
  87. REG(I2C_OA) = slaveadd;
  88. REG(I2C_CNT) = 0;
  89. /* Interrupts must be enabled or I2C module won't work */
  90. REG(I2C_IE) = I2C_IE_SCD_IE | I2C_IE_XRDY_IE |
  91. I2C_IE_RRDY_IE | I2C_IE_ARDY_IE | I2C_IE_NACK_IE;
  92. /* Now enable I2C controller (get it out of reset) */
  93. REG(I2C_CON) = I2C_CON_EN;
  94. udelay(1000);
  95. }
  96. int i2c_set_bus_speed(unsigned int speed)
  97. {
  98. i2c_init(speed, CONFIG_SYS_I2C_SLAVE);
  99. return 0;
  100. }
  101. int i2c_probe(u_int8_t chip)
  102. {
  103. int rc = 1;
  104. if (chip == REG(I2C_OA)) {
  105. return(rc);
  106. }
  107. REG(I2C_CON) = 0;
  108. if (wait_for_bus()) {return(1);}
  109. /* try to read one byte from current (or only) address */
  110. REG(I2C_CNT) = 1;
  111. REG(I2C_SA) = chip;
  112. REG(I2C_CON) = (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP);
  113. udelay (50000);
  114. if (!(REG(I2C_STAT) & I2C_STAT_NACK)) {
  115. rc = 0;
  116. flush_rx();
  117. REG(I2C_STAT) = 0xffff;
  118. } else {
  119. REG(I2C_STAT) = 0xffff;
  120. REG(I2C_CON) |= I2C_CON_STP;
  121. udelay(20000);
  122. if (wait_for_bus()) {return(1);}
  123. }
  124. flush_rx();
  125. REG(I2C_STAT) = 0xffff;
  126. REG(I2C_CNT) = 0;
  127. return(rc);
  128. }
  129. int i2c_read(u_int8_t chip, u_int32_t addr, int alen, u_int8_t *buf, int len)
  130. {
  131. u_int32_t tmp;
  132. int i;
  133. if ((alen < 0) || (alen > 2)) {
  134. printf("%s(): bogus address length %x\n", __FUNCTION__, alen);
  135. return(1);
  136. }
  137. if (wait_for_bus()) {return(1);}
  138. if (alen != 0) {
  139. /* Start address phase */
  140. tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX;
  141. REG(I2C_CNT) = alen;
  142. REG(I2C_SA) = chip;
  143. REG(I2C_CON) = tmp;
  144. tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK);
  145. CHECK_NACK();
  146. switch (alen) {
  147. case 2:
  148. /* Send address MSByte */
  149. if (tmp & I2C_STAT_XRDY) {
  150. REG(I2C_DXR) = (addr >> 8) & 0xff;
  151. } else {
  152. REG(I2C_CON) = 0;
  153. return(1);
  154. }
  155. tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK);
  156. CHECK_NACK();
  157. /* No break, fall through */
  158. case 1:
  159. /* Send address LSByte */
  160. if (tmp & I2C_STAT_XRDY) {
  161. REG(I2C_DXR) = addr & 0xff;
  162. } else {
  163. REG(I2C_CON) = 0;
  164. return(1);
  165. }
  166. tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK | I2C_STAT_ARDY);
  167. CHECK_NACK();
  168. if (!(tmp & I2C_STAT_ARDY)) {
  169. REG(I2C_CON) = 0;
  170. return(1);
  171. }
  172. }
  173. }
  174. /* Address phase is over, now read 'len' bytes and stop */
  175. tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP;
  176. REG(I2C_CNT) = len & 0xffff;
  177. REG(I2C_SA) = chip;
  178. REG(I2C_CON) = tmp;
  179. for (i = 0; i < len; i++) {
  180. tmp = poll_i2c_irq(I2C_STAT_RRDY | I2C_STAT_NACK | I2C_STAT_ROVR);
  181. CHECK_NACK();
  182. if (tmp & I2C_STAT_RRDY) {
  183. buf[i] = REG(I2C_DRR);
  184. } else {
  185. REG(I2C_CON) = 0;
  186. return(1);
  187. }
  188. }
  189. tmp = poll_i2c_irq(I2C_STAT_SCD | I2C_STAT_NACK);
  190. CHECK_NACK();
  191. if (!(tmp & I2C_STAT_SCD)) {
  192. REG(I2C_CON) = 0;
  193. return(1);
  194. }
  195. flush_rx();
  196. REG(I2C_STAT) = 0xffff;
  197. REG(I2C_CNT) = 0;
  198. REG(I2C_CON) = 0;
  199. return(0);
  200. }
  201. int i2c_write(u_int8_t chip, u_int32_t addr, int alen, u_int8_t *buf, int len)
  202. {
  203. u_int32_t tmp;
  204. int i;
  205. if ((alen < 0) || (alen > 2)) {
  206. printf("%s(): bogus address length %x\n", __FUNCTION__, alen);
  207. return(1);
  208. }
  209. if (len < 0) {
  210. printf("%s(): bogus length %x\n", __FUNCTION__, len);
  211. return(1);
  212. }
  213. if (wait_for_bus()) {return(1);}
  214. /* Start address phase */
  215. tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX | I2C_CON_STP;
  216. REG(I2C_CNT) = (alen == 0) ? len & 0xffff : (len & 0xffff) + alen;
  217. REG(I2C_SA) = chip;
  218. REG(I2C_CON) = tmp;
  219. switch (alen) {
  220. case 2:
  221. /* Send address MSByte */
  222. tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK);
  223. CHECK_NACK();
  224. if (tmp & I2C_STAT_XRDY) {
  225. REG(I2C_DXR) = (addr >> 8) & 0xff;
  226. } else {
  227. REG(I2C_CON) = 0;
  228. return(1);
  229. }
  230. /* No break, fall through */
  231. case 1:
  232. /* Send address LSByte */
  233. tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK);
  234. CHECK_NACK();
  235. if (tmp & I2C_STAT_XRDY) {
  236. REG(I2C_DXR) = addr & 0xff;
  237. } else {
  238. REG(I2C_CON) = 0;
  239. return(1);
  240. }
  241. }
  242. for (i = 0; i < len; i++) {
  243. tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK);
  244. CHECK_NACK();
  245. if (tmp & I2C_STAT_XRDY) {
  246. REG(I2C_DXR) = buf[i];
  247. } else {
  248. return(1);
  249. }
  250. }
  251. tmp = poll_i2c_irq(I2C_STAT_SCD | I2C_STAT_NACK);
  252. CHECK_NACK();
  253. if (!(tmp & I2C_STAT_SCD)) {
  254. REG(I2C_CON) = 0;
  255. return(1);
  256. }
  257. flush_rx();
  258. REG(I2C_STAT) = 0xffff;
  259. REG(I2C_CNT) = 0;
  260. REG(I2C_CON) = 0;
  261. return(0);
  262. }