davinci_i2c.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. int dummy;
  68. while (1) {
  69. if (!(REG(I2C_STAT) & I2C_STAT_RRDY))
  70. break;
  71. dummy = REG(I2C_DRR);
  72. REG(I2C_STAT) = I2C_STAT_RRDY;
  73. udelay(1000);
  74. }
  75. }
  76. void i2c_init(int speed, int slaveadd)
  77. {
  78. u_int32_t div, psc;
  79. if (REG(I2C_CON) & I2C_CON_EN) {
  80. REG(I2C_CON) = 0;
  81. udelay (50000);
  82. }
  83. psc = 2;
  84. div = (CONFIG_SYS_HZ_CLOCK / ((psc + 1) * speed)) - 10; /* SCLL + SCLH */
  85. REG(I2C_PSC) = psc; /* 27MHz / (2 + 1) = 9MHz */
  86. REG(I2C_SCLL) = (div * 50) / 100; /* 50% Duty */
  87. REG(I2C_SCLH) = div - REG(I2C_SCLL);
  88. REG(I2C_OA) = slaveadd;
  89. REG(I2C_CNT) = 0;
  90. /* Interrupts must be enabled or I2C module won't work */
  91. REG(I2C_IE) = I2C_IE_SCD_IE | I2C_IE_XRDY_IE |
  92. I2C_IE_RRDY_IE | I2C_IE_ARDY_IE | I2C_IE_NACK_IE;
  93. /* Now enable I2C controller (get it out of reset) */
  94. REG(I2C_CON) = I2C_CON_EN;
  95. udelay(1000);
  96. }
  97. int i2c_probe(u_int8_t chip)
  98. {
  99. int rc = 1;
  100. if (chip == REG(I2C_OA)) {
  101. return(rc);
  102. }
  103. REG(I2C_CON) = 0;
  104. if (wait_for_bus()) {return(1);}
  105. /* try to read one byte from current (or only) address */
  106. REG(I2C_CNT) = 1;
  107. REG(I2C_SA) = chip;
  108. REG(I2C_CON) = (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP);
  109. udelay (50000);
  110. if (!(REG(I2C_STAT) & I2C_STAT_NACK)) {
  111. rc = 0;
  112. flush_rx();
  113. REG(I2C_STAT) = 0xffff;
  114. } else {
  115. REG(I2C_STAT) = 0xffff;
  116. REG(I2C_CON) |= I2C_CON_STP;
  117. udelay(20000);
  118. if (wait_for_bus()) {return(1);}
  119. }
  120. flush_rx();
  121. REG(I2C_STAT) = 0xffff;
  122. REG(I2C_CNT) = 0;
  123. return(rc);
  124. }
  125. int i2c_read(u_int8_t chip, u_int32_t addr, int alen, u_int8_t *buf, int len)
  126. {
  127. u_int32_t tmp;
  128. int i;
  129. if ((alen < 0) || (alen > 2)) {
  130. printf("%s(): bogus address length %x\n", __FUNCTION__, alen);
  131. return(1);
  132. }
  133. if (wait_for_bus()) {return(1);}
  134. if (alen != 0) {
  135. /* Start address phase */
  136. tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX;
  137. REG(I2C_CNT) = alen;
  138. REG(I2C_SA) = chip;
  139. REG(I2C_CON) = tmp;
  140. tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK);
  141. CHECK_NACK();
  142. switch (alen) {
  143. case 2:
  144. /* Send address MSByte */
  145. if (tmp & I2C_STAT_XRDY) {
  146. REG(I2C_DXR) = (addr >> 8) & 0xff;
  147. } else {
  148. REG(I2C_CON) = 0;
  149. return(1);
  150. }
  151. tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK);
  152. CHECK_NACK();
  153. /* No break, fall through */
  154. case 1:
  155. /* Send address LSByte */
  156. if (tmp & I2C_STAT_XRDY) {
  157. REG(I2C_DXR) = addr & 0xff;
  158. } else {
  159. REG(I2C_CON) = 0;
  160. return(1);
  161. }
  162. tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK | I2C_STAT_ARDY);
  163. CHECK_NACK();
  164. if (!(tmp & I2C_STAT_ARDY)) {
  165. REG(I2C_CON) = 0;
  166. return(1);
  167. }
  168. }
  169. }
  170. /* Address phase is over, now read 'len' bytes and stop */
  171. tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP;
  172. REG(I2C_CNT) = len & 0xffff;
  173. REG(I2C_SA) = chip;
  174. REG(I2C_CON) = tmp;
  175. for (i = 0; i < len; i++) {
  176. tmp = poll_i2c_irq(I2C_STAT_RRDY | I2C_STAT_NACK | I2C_STAT_ROVR);
  177. CHECK_NACK();
  178. if (tmp & I2C_STAT_RRDY) {
  179. buf[i] = REG(I2C_DRR);
  180. } else {
  181. REG(I2C_CON) = 0;
  182. return(1);
  183. }
  184. }
  185. tmp = poll_i2c_irq(I2C_STAT_SCD | I2C_STAT_NACK);
  186. CHECK_NACK();
  187. if (!(tmp & I2C_STAT_SCD)) {
  188. REG(I2C_CON) = 0;
  189. return(1);
  190. }
  191. flush_rx();
  192. REG(I2C_STAT) = 0xffff;
  193. REG(I2C_CNT) = 0;
  194. REG(I2C_CON) = 0;
  195. return(0);
  196. }
  197. int i2c_write(u_int8_t chip, u_int32_t addr, int alen, u_int8_t *buf, int len)
  198. {
  199. u_int32_t tmp;
  200. int i;
  201. if ((alen < 0) || (alen > 2)) {
  202. printf("%s(): bogus address length %x\n", __FUNCTION__, alen);
  203. return(1);
  204. }
  205. if (len < 0) {
  206. printf("%s(): bogus length %x\n", __FUNCTION__, len);
  207. return(1);
  208. }
  209. if (wait_for_bus()) {return(1);}
  210. /* Start address phase */
  211. tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX | I2C_CON_STP;
  212. REG(I2C_CNT) = (alen == 0) ? len & 0xffff : (len & 0xffff) + alen;
  213. REG(I2C_SA) = chip;
  214. REG(I2C_CON) = tmp;
  215. switch (alen) {
  216. case 2:
  217. /* Send address MSByte */
  218. tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK);
  219. CHECK_NACK();
  220. if (tmp & I2C_STAT_XRDY) {
  221. REG(I2C_DXR) = (addr >> 8) & 0xff;
  222. } else {
  223. REG(I2C_CON) = 0;
  224. return(1);
  225. }
  226. /* No break, fall through */
  227. case 1:
  228. /* Send address LSByte */
  229. tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK);
  230. CHECK_NACK();
  231. if (tmp & I2C_STAT_XRDY) {
  232. REG(I2C_DXR) = addr & 0xff;
  233. } else {
  234. REG(I2C_CON) = 0;
  235. return(1);
  236. }
  237. }
  238. for (i = 0; i < len; i++) {
  239. tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK);
  240. CHECK_NACK();
  241. if (tmp & I2C_STAT_XRDY) {
  242. REG(I2C_DXR) = buf[i];
  243. } else {
  244. return(1);
  245. }
  246. }
  247. tmp = poll_i2c_irq(I2C_STAT_SCD | I2C_STAT_NACK);
  248. CHECK_NACK();
  249. if (!(tmp & I2C_STAT_SCD)) {
  250. REG(I2C_CON) = 0;
  251. return(1);
  252. }
  253. flush_rx();
  254. REG(I2C_STAT) = 0xffff;
  255. REG(I2C_CNT) = 0;
  256. REG(I2C_CON) = 0;
  257. return(0);
  258. }