davinci_i2c.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * TI DaVinci (TMS320DM644x) I2C driver.
  3. *
  4. * (C) Copyright 2012-2014
  5. * Texas Instruments Incorporated, <www.ti.com>
  6. * (C) Copyright 2007 Sergey Kubushyn <ksi@koi8.net>
  7. * --------------------------------------------------------
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. *
  11. * NOTE: This driver should be converted to driver model before June 2017.
  12. * Please see doc/driver-model/i2c-howto.txt for instructions.
  13. */
  14. #include <common.h>
  15. #include <i2c.h>
  16. #include <asm/arch/hardware.h>
  17. #include <asm/arch/i2c_defs.h>
  18. #include <asm/io.h>
  19. #include "davinci_i2c.h"
  20. #define CHECK_NACK() \
  21. do {\
  22. if (tmp & (I2C_TIMEOUT | I2C_STAT_NACK)) {\
  23. REG(&(i2c_base->i2c_con)) = 0;\
  24. return 1;\
  25. } \
  26. } while (0)
  27. static struct i2c_regs *davinci_get_base(struct i2c_adapter *adap);
  28. static int wait_for_bus(struct i2c_adapter *adap)
  29. {
  30. struct i2c_regs *i2c_base = davinci_get_base(adap);
  31. int stat, timeout;
  32. REG(&(i2c_base->i2c_stat)) = 0xffff;
  33. for (timeout = 0; timeout < 10; timeout++) {
  34. stat = REG(&(i2c_base->i2c_stat));
  35. if (!((stat) & I2C_STAT_BB)) {
  36. REG(&(i2c_base->i2c_stat)) = 0xffff;
  37. return 0;
  38. }
  39. REG(&(i2c_base->i2c_stat)) = stat;
  40. udelay(50000);
  41. }
  42. REG(&(i2c_base->i2c_stat)) = 0xffff;
  43. return 1;
  44. }
  45. static int poll_i2c_irq(struct i2c_adapter *adap, int mask)
  46. {
  47. struct i2c_regs *i2c_base = davinci_get_base(adap);
  48. int stat, timeout;
  49. for (timeout = 0; timeout < 10; timeout++) {
  50. udelay(1000);
  51. stat = REG(&(i2c_base->i2c_stat));
  52. if (stat & mask)
  53. return stat;
  54. }
  55. REG(&(i2c_base->i2c_stat)) = 0xffff;
  56. return stat | I2C_TIMEOUT;
  57. }
  58. static void flush_rx(struct i2c_adapter *adap)
  59. {
  60. struct i2c_regs *i2c_base = davinci_get_base(adap);
  61. while (1) {
  62. if (!(REG(&(i2c_base->i2c_stat)) & I2C_STAT_RRDY))
  63. break;
  64. REG(&(i2c_base->i2c_drr));
  65. REG(&(i2c_base->i2c_stat)) = I2C_STAT_RRDY;
  66. udelay(1000);
  67. }
  68. }
  69. static uint davinci_i2c_setspeed(struct i2c_adapter *adap, uint speed)
  70. {
  71. struct i2c_regs *i2c_base = davinci_get_base(adap);
  72. uint32_t div, psc;
  73. psc = 2;
  74. /* SCLL + SCLH */
  75. div = (CONFIG_SYS_HZ_CLOCK / ((psc + 1) * speed)) - 10;
  76. REG(&(i2c_base->i2c_psc)) = psc; /* 27MHz / (2 + 1) = 9MHz */
  77. REG(&(i2c_base->i2c_scll)) = (div * 50) / 100; /* 50% Duty */
  78. REG(&(i2c_base->i2c_sclh)) = div - REG(&(i2c_base->i2c_scll));
  79. adap->speed = speed;
  80. return 0;
  81. }
  82. static void davinci_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
  83. {
  84. struct i2c_regs *i2c_base = davinci_get_base(adap);
  85. if (REG(&(i2c_base->i2c_con)) & I2C_CON_EN) {
  86. REG(&(i2c_base->i2c_con)) = 0;
  87. udelay(50000);
  88. }
  89. davinci_i2c_setspeed(adap, speed);
  90. REG(&(i2c_base->i2c_oa)) = slaveadd;
  91. REG(&(i2c_base->i2c_cnt)) = 0;
  92. /* Interrupts must be enabled or I2C module won't work */
  93. REG(&(i2c_base->i2c_ie)) = I2C_IE_SCD_IE | I2C_IE_XRDY_IE |
  94. I2C_IE_RRDY_IE | I2C_IE_ARDY_IE | I2C_IE_NACK_IE;
  95. /* Now enable I2C controller (get it out of reset) */
  96. REG(&(i2c_base->i2c_con)) = I2C_CON_EN;
  97. udelay(1000);
  98. }
  99. static int davinci_i2c_probe(struct i2c_adapter *adap, uint8_t chip)
  100. {
  101. struct i2c_regs *i2c_base = davinci_get_base(adap);
  102. int rc = 1;
  103. if (chip == REG(&(i2c_base->i2c_oa)))
  104. return rc;
  105. REG(&(i2c_base->i2c_con)) = 0;
  106. if (wait_for_bus(adap))
  107. return 1;
  108. /* try to read one byte from current (or only) address */
  109. REG(&(i2c_base->i2c_cnt)) = 1;
  110. REG(&(i2c_base->i2c_sa)) = chip;
  111. REG(&(i2c_base->i2c_con)) = (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
  112. I2C_CON_STP);
  113. udelay(50000);
  114. if (!(REG(&(i2c_base->i2c_stat)) & I2C_STAT_NACK)) {
  115. rc = 0;
  116. flush_rx(adap);
  117. REG(&(i2c_base->i2c_stat)) = 0xffff;
  118. } else {
  119. REG(&(i2c_base->i2c_stat)) = 0xffff;
  120. REG(&(i2c_base->i2c_con)) |= I2C_CON_STP;
  121. udelay(20000);
  122. if (wait_for_bus(adap))
  123. return 1;
  124. }
  125. flush_rx(adap);
  126. REG(&(i2c_base->i2c_stat)) = 0xffff;
  127. REG(&(i2c_base->i2c_cnt)) = 0;
  128. return rc;
  129. }
  130. static int davinci_i2c_read(struct i2c_adapter *adap, uint8_t chip,
  131. uint32_t addr, int alen, uint8_t *buf, int len)
  132. {
  133. struct i2c_regs *i2c_base = davinci_get_base(adap);
  134. uint32_t tmp;
  135. int i;
  136. if ((alen < 0) || (alen > 2)) {
  137. printf("%s(): bogus address length %x\n", __func__, alen);
  138. return 1;
  139. }
  140. if (wait_for_bus(adap))
  141. return 1;
  142. if (alen != 0) {
  143. /* Start address phase */
  144. tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX;
  145. REG(&(i2c_base->i2c_cnt)) = alen;
  146. REG(&(i2c_base->i2c_sa)) = chip;
  147. REG(&(i2c_base->i2c_con)) = tmp;
  148. tmp = poll_i2c_irq(adap, I2C_STAT_XRDY | I2C_STAT_NACK);
  149. CHECK_NACK();
  150. switch (alen) {
  151. case 2:
  152. /* Send address MSByte */
  153. if (tmp & I2C_STAT_XRDY) {
  154. REG(&(i2c_base->i2c_dxr)) = (addr >> 8) & 0xff;
  155. } else {
  156. REG(&(i2c_base->i2c_con)) = 0;
  157. return 1;
  158. }
  159. tmp = poll_i2c_irq(adap, I2C_STAT_XRDY | I2C_STAT_NACK);
  160. CHECK_NACK();
  161. /* No break, fall through */
  162. case 1:
  163. /* Send address LSByte */
  164. if (tmp & I2C_STAT_XRDY) {
  165. REG(&(i2c_base->i2c_dxr)) = addr & 0xff;
  166. } else {
  167. REG(&(i2c_base->i2c_con)) = 0;
  168. return 1;
  169. }
  170. tmp = poll_i2c_irq(adap, I2C_STAT_XRDY |
  171. I2C_STAT_NACK | I2C_STAT_ARDY);
  172. CHECK_NACK();
  173. if (!(tmp & I2C_STAT_ARDY)) {
  174. REG(&(i2c_base->i2c_con)) = 0;
  175. return 1;
  176. }
  177. }
  178. }
  179. /* Address phase is over, now read 'len' bytes and stop */
  180. tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP;
  181. REG(&(i2c_base->i2c_cnt)) = len & 0xffff;
  182. REG(&(i2c_base->i2c_sa)) = chip;
  183. REG(&(i2c_base->i2c_con)) = tmp;
  184. for (i = 0; i < len; i++) {
  185. tmp = poll_i2c_irq(adap, I2C_STAT_RRDY | I2C_STAT_NACK |
  186. I2C_STAT_ROVR);
  187. CHECK_NACK();
  188. if (tmp & I2C_STAT_RRDY) {
  189. buf[i] = REG(&(i2c_base->i2c_drr));
  190. } else {
  191. REG(&(i2c_base->i2c_con)) = 0;
  192. return 1;
  193. }
  194. }
  195. tmp = poll_i2c_irq(adap, I2C_STAT_SCD | I2C_STAT_NACK);
  196. CHECK_NACK();
  197. if (!(tmp & I2C_STAT_SCD)) {
  198. REG(&(i2c_base->i2c_con)) = 0;
  199. return 1;
  200. }
  201. flush_rx(adap);
  202. REG(&(i2c_base->i2c_stat)) = 0xffff;
  203. REG(&(i2c_base->i2c_cnt)) = 0;
  204. REG(&(i2c_base->i2c_con)) = 0;
  205. return 0;
  206. }
  207. static int davinci_i2c_write(struct i2c_adapter *adap, uint8_t chip,
  208. uint32_t addr, int alen, uint8_t *buf, int len)
  209. {
  210. struct i2c_regs *i2c_base = davinci_get_base(adap);
  211. uint32_t tmp;
  212. int i;
  213. if ((alen < 0) || (alen > 2)) {
  214. printf("%s(): bogus address length %x\n", __func__, alen);
  215. return 1;
  216. }
  217. if (len < 0) {
  218. printf("%s(): bogus length %x\n", __func__, len);
  219. return 1;
  220. }
  221. if (wait_for_bus(adap))
  222. return 1;
  223. /* Start address phase */
  224. tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
  225. I2C_CON_TRX | I2C_CON_STP;
  226. REG(&(i2c_base->i2c_cnt)) = (alen == 0) ?
  227. len & 0xffff : (len & 0xffff) + alen;
  228. REG(&(i2c_base->i2c_sa)) = chip;
  229. REG(&(i2c_base->i2c_con)) = tmp;
  230. switch (alen) {
  231. case 2:
  232. /* Send address MSByte */
  233. tmp = poll_i2c_irq(adap, I2C_STAT_XRDY | I2C_STAT_NACK);
  234. CHECK_NACK();
  235. if (tmp & I2C_STAT_XRDY) {
  236. REG(&(i2c_base->i2c_dxr)) = (addr >> 8) & 0xff;
  237. } else {
  238. REG(&(i2c_base->i2c_con)) = 0;
  239. return 1;
  240. }
  241. /* No break, fall through */
  242. case 1:
  243. /* Send address LSByte */
  244. tmp = poll_i2c_irq(adap, I2C_STAT_XRDY | I2C_STAT_NACK);
  245. CHECK_NACK();
  246. if (tmp & I2C_STAT_XRDY) {
  247. REG(&(i2c_base->i2c_dxr)) = addr & 0xff;
  248. } else {
  249. REG(&(i2c_base->i2c_con)) = 0;
  250. return 1;
  251. }
  252. }
  253. for (i = 0; i < len; i++) {
  254. tmp = poll_i2c_irq(adap, I2C_STAT_XRDY | I2C_STAT_NACK);
  255. CHECK_NACK();
  256. if (tmp & I2C_STAT_XRDY)
  257. REG(&(i2c_base->i2c_dxr)) = buf[i];
  258. else
  259. return 1;
  260. }
  261. tmp = poll_i2c_irq(adap, I2C_STAT_SCD | I2C_STAT_NACK);
  262. CHECK_NACK();
  263. if (!(tmp & I2C_STAT_SCD)) {
  264. REG(&(i2c_base->i2c_con)) = 0;
  265. return 1;
  266. }
  267. flush_rx(adap);
  268. REG(&(i2c_base->i2c_stat)) = 0xffff;
  269. REG(&(i2c_base->i2c_cnt)) = 0;
  270. REG(&(i2c_base->i2c_con)) = 0;
  271. return 0;
  272. }
  273. static struct i2c_regs *davinci_get_base(struct i2c_adapter *adap)
  274. {
  275. switch (adap->hwadapnr) {
  276. #if I2C_BUS_MAX >= 3
  277. case 2:
  278. return (struct i2c_regs *)I2C2_BASE;
  279. #endif
  280. #if I2C_BUS_MAX >= 2
  281. case 1:
  282. return (struct i2c_regs *)I2C1_BASE;
  283. #endif
  284. case 0:
  285. return (struct i2c_regs *)I2C_BASE;
  286. default:
  287. printf("wrong hwadapnr: %d\n", adap->hwadapnr);
  288. }
  289. return NULL;
  290. }
  291. U_BOOT_I2C_ADAP_COMPLETE(davinci_0, davinci_i2c_init, davinci_i2c_probe,
  292. davinci_i2c_read, davinci_i2c_write,
  293. davinci_i2c_setspeed,
  294. CONFIG_SYS_DAVINCI_I2C_SPEED,
  295. CONFIG_SYS_DAVINCI_I2C_SLAVE,
  296. 0)
  297. #if I2C_BUS_MAX >= 2
  298. U_BOOT_I2C_ADAP_COMPLETE(davinci_1, davinci_i2c_init, davinci_i2c_probe,
  299. davinci_i2c_read, davinci_i2c_write,
  300. davinci_i2c_setspeed,
  301. CONFIG_SYS_DAVINCI_I2C_SPEED1,
  302. CONFIG_SYS_DAVINCI_I2C_SLAVE1,
  303. 1)
  304. #endif
  305. #if I2C_BUS_MAX >= 3
  306. U_BOOT_I2C_ADAP_COMPLETE(davinci_2, davinci_i2c_init, davinci_i2c_probe,
  307. davinci_i2c_read, davinci_i2c_write,
  308. davinci_i2c_setspeed,
  309. CONFIG_SYS_DAVINCI_I2C_SPEED2,
  310. CONFIG_SYS_DAVINCI_I2C_SLAVE2,
  311. 2)
  312. #endif