davinci_i2c.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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_set_bus_speed(unsigned int speed)
  98. {
  99. i2c_init(speed, CONFIG_SYS_I2C_SLAVE);
  100. return 0;
  101. }
  102. int i2c_probe(u_int8_t chip)
  103. {
  104. int rc = 1;
  105. if (chip == REG(I2C_OA)) {
  106. return(rc);
  107. }
  108. REG(I2C_CON) = 0;
  109. if (wait_for_bus()) {return(1);}
  110. /* try to read one byte from current (or only) address */
  111. REG(I2C_CNT) = 1;
  112. REG(I2C_SA) = chip;
  113. REG(I2C_CON) = (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP);
  114. udelay (50000);
  115. if (!(REG(I2C_STAT) & I2C_STAT_NACK)) {
  116. rc = 0;
  117. flush_rx();
  118. REG(I2C_STAT) = 0xffff;
  119. } else {
  120. REG(I2C_STAT) = 0xffff;
  121. REG(I2C_CON) |= I2C_CON_STP;
  122. udelay(20000);
  123. if (wait_for_bus()) {return(1);}
  124. }
  125. flush_rx();
  126. REG(I2C_STAT) = 0xffff;
  127. REG(I2C_CNT) = 0;
  128. return(rc);
  129. }
  130. int i2c_read(u_int8_t chip, u_int32_t addr, int alen, u_int8_t *buf, int len)
  131. {
  132. u_int32_t tmp;
  133. int i;
  134. if ((alen < 0) || (alen > 2)) {
  135. printf("%s(): bogus address length %x\n", __FUNCTION__, alen);
  136. return(1);
  137. }
  138. if (wait_for_bus()) {return(1);}
  139. if (alen != 0) {
  140. /* Start address phase */
  141. tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX;
  142. REG(I2C_CNT) = alen;
  143. REG(I2C_SA) = chip;
  144. REG(I2C_CON) = tmp;
  145. tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK);
  146. CHECK_NACK();
  147. switch (alen) {
  148. case 2:
  149. /* Send address MSByte */
  150. if (tmp & I2C_STAT_XRDY) {
  151. REG(I2C_DXR) = (addr >> 8) & 0xff;
  152. } else {
  153. REG(I2C_CON) = 0;
  154. return(1);
  155. }
  156. tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK);
  157. CHECK_NACK();
  158. /* No break, fall through */
  159. case 1:
  160. /* Send address LSByte */
  161. if (tmp & I2C_STAT_XRDY) {
  162. REG(I2C_DXR) = addr & 0xff;
  163. } else {
  164. REG(I2C_CON) = 0;
  165. return(1);
  166. }
  167. tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK | I2C_STAT_ARDY);
  168. CHECK_NACK();
  169. if (!(tmp & I2C_STAT_ARDY)) {
  170. REG(I2C_CON) = 0;
  171. return(1);
  172. }
  173. }
  174. }
  175. /* Address phase is over, now read 'len' bytes and stop */
  176. tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP;
  177. REG(I2C_CNT) = len & 0xffff;
  178. REG(I2C_SA) = chip;
  179. REG(I2C_CON) = tmp;
  180. for (i = 0; i < len; i++) {
  181. tmp = poll_i2c_irq(I2C_STAT_RRDY | I2C_STAT_NACK | I2C_STAT_ROVR);
  182. CHECK_NACK();
  183. if (tmp & I2C_STAT_RRDY) {
  184. buf[i] = REG(I2C_DRR);
  185. } else {
  186. REG(I2C_CON) = 0;
  187. return(1);
  188. }
  189. }
  190. tmp = poll_i2c_irq(I2C_STAT_SCD | I2C_STAT_NACK);
  191. CHECK_NACK();
  192. if (!(tmp & I2C_STAT_SCD)) {
  193. REG(I2C_CON) = 0;
  194. return(1);
  195. }
  196. flush_rx();
  197. REG(I2C_STAT) = 0xffff;
  198. REG(I2C_CNT) = 0;
  199. REG(I2C_CON) = 0;
  200. return(0);
  201. }
  202. int i2c_write(u_int8_t chip, u_int32_t addr, int alen, u_int8_t *buf, int len)
  203. {
  204. u_int32_t tmp;
  205. int i;
  206. if ((alen < 0) || (alen > 2)) {
  207. printf("%s(): bogus address length %x\n", __FUNCTION__, alen);
  208. return(1);
  209. }
  210. if (len < 0) {
  211. printf("%s(): bogus length %x\n", __FUNCTION__, len);
  212. return(1);
  213. }
  214. if (wait_for_bus()) {return(1);}
  215. /* Start address phase */
  216. tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX | I2C_CON_STP;
  217. REG(I2C_CNT) = (alen == 0) ? len & 0xffff : (len & 0xffff) + alen;
  218. REG(I2C_SA) = chip;
  219. REG(I2C_CON) = tmp;
  220. switch (alen) {
  221. case 2:
  222. /* Send address MSByte */
  223. tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK);
  224. CHECK_NACK();
  225. if (tmp & I2C_STAT_XRDY) {
  226. REG(I2C_DXR) = (addr >> 8) & 0xff;
  227. } else {
  228. REG(I2C_CON) = 0;
  229. return(1);
  230. }
  231. /* No break, fall through */
  232. case 1:
  233. /* Send address LSByte */
  234. tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK);
  235. CHECK_NACK();
  236. if (tmp & I2C_STAT_XRDY) {
  237. REG(I2C_DXR) = addr & 0xff;
  238. } else {
  239. REG(I2C_CON) = 0;
  240. return(1);
  241. }
  242. }
  243. for (i = 0; i < len; i++) {
  244. tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK);
  245. CHECK_NACK();
  246. if (tmp & I2C_STAT_XRDY) {
  247. REG(I2C_DXR) = buf[i];
  248. } else {
  249. return(1);
  250. }
  251. }
  252. tmp = poll_i2c_irq(I2C_STAT_SCD | I2C_STAT_NACK);
  253. CHECK_NACK();
  254. if (!(tmp & I2C_STAT_SCD)) {
  255. REG(I2C_CON) = 0;
  256. return(1);
  257. }
  258. flush_rx();
  259. REG(I2C_STAT) = 0xffff;
  260. REG(I2C_CNT) = 0;
  261. REG(I2C_CON) = 0;
  262. return(0);
  263. }