i2c.c 6.6 KB

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