davinci_i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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 <dm.h>
  17. #include <asm/arch/hardware.h>
  18. #include <asm/arch/i2c_defs.h>
  19. #include <asm/io.h>
  20. #include "davinci_i2c.h"
  21. #ifdef CONFIG_DM_I2C
  22. /* Information about i2c controller */
  23. struct i2c_bus {
  24. int id;
  25. uint speed;
  26. struct i2c_regs *regs;
  27. };
  28. #endif
  29. #define CHECK_NACK() \
  30. do {\
  31. if (tmp & (I2C_TIMEOUT | I2C_STAT_NACK)) {\
  32. REG(&(i2c_base->i2c_con)) = 0;\
  33. return 1;\
  34. } \
  35. } while (0)
  36. static int _wait_for_bus(struct i2c_regs *i2c_base)
  37. {
  38. int stat, timeout;
  39. REG(&(i2c_base->i2c_stat)) = 0xffff;
  40. for (timeout = 0; timeout < 10; timeout++) {
  41. stat = REG(&(i2c_base->i2c_stat));
  42. if (!((stat) & I2C_STAT_BB)) {
  43. REG(&(i2c_base->i2c_stat)) = 0xffff;
  44. return 0;
  45. }
  46. REG(&(i2c_base->i2c_stat)) = stat;
  47. udelay(50000);
  48. }
  49. REG(&(i2c_base->i2c_stat)) = 0xffff;
  50. return 1;
  51. }
  52. static int _poll_i2c_irq(struct i2c_regs *i2c_base, int mask)
  53. {
  54. int stat, timeout;
  55. for (timeout = 0; timeout < 10; timeout++) {
  56. udelay(1000);
  57. stat = REG(&(i2c_base->i2c_stat));
  58. if (stat & mask)
  59. return stat;
  60. }
  61. REG(&(i2c_base->i2c_stat)) = 0xffff;
  62. return stat | I2C_TIMEOUT;
  63. }
  64. static void _flush_rx(struct i2c_regs *i2c_base)
  65. {
  66. while (1) {
  67. if (!(REG(&(i2c_base->i2c_stat)) & I2C_STAT_RRDY))
  68. break;
  69. REG(&(i2c_base->i2c_drr));
  70. REG(&(i2c_base->i2c_stat)) = I2C_STAT_RRDY;
  71. udelay(1000);
  72. }
  73. }
  74. static uint _davinci_i2c_setspeed(struct i2c_regs *i2c_base,
  75. uint speed)
  76. {
  77. uint32_t div, psc;
  78. psc = 2;
  79. /* SCLL + SCLH */
  80. div = (CONFIG_SYS_HZ_CLOCK / ((psc + 1) * speed)) - 10;
  81. REG(&(i2c_base->i2c_psc)) = psc; /* 27MHz / (2 + 1) = 9MHz */
  82. REG(&(i2c_base->i2c_scll)) = (div * 50) / 100; /* 50% Duty */
  83. REG(&(i2c_base->i2c_sclh)) = div - REG(&(i2c_base->i2c_scll));
  84. return 0;
  85. }
  86. static void _davinci_i2c_init(struct i2c_regs *i2c_base,
  87. uint speed, int slaveadd)
  88. {
  89. if (REG(&(i2c_base->i2c_con)) & I2C_CON_EN) {
  90. REG(&(i2c_base->i2c_con)) = 0;
  91. udelay(50000);
  92. }
  93. _davinci_i2c_setspeed(i2c_base, speed);
  94. REG(&(i2c_base->i2c_oa)) = slaveadd;
  95. REG(&(i2c_base->i2c_cnt)) = 0;
  96. /* Interrupts must be enabled or I2C module won't work */
  97. REG(&(i2c_base->i2c_ie)) = I2C_IE_SCD_IE | I2C_IE_XRDY_IE |
  98. I2C_IE_RRDY_IE | I2C_IE_ARDY_IE | I2C_IE_NACK_IE;
  99. /* Now enable I2C controller (get it out of reset) */
  100. REG(&(i2c_base->i2c_con)) = I2C_CON_EN;
  101. udelay(1000);
  102. }
  103. static int _davinci_i2c_read(struct i2c_regs *i2c_base, uint8_t chip,
  104. uint32_t addr, int alen, uint8_t *buf, int len)
  105. {
  106. uint32_t tmp;
  107. int i;
  108. if ((alen < 0) || (alen > 2)) {
  109. printf("%s(): bogus address length %x\n", __func__, alen);
  110. return 1;
  111. }
  112. if (_wait_for_bus(i2c_base))
  113. return 1;
  114. if (alen != 0) {
  115. /* Start address phase */
  116. tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX;
  117. REG(&(i2c_base->i2c_cnt)) = alen;
  118. REG(&(i2c_base->i2c_sa)) = chip;
  119. REG(&(i2c_base->i2c_con)) = tmp;
  120. tmp = _poll_i2c_irq(i2c_base, I2C_STAT_XRDY | I2C_STAT_NACK);
  121. CHECK_NACK();
  122. switch (alen) {
  123. case 2:
  124. /* Send address MSByte */
  125. if (tmp & I2C_STAT_XRDY) {
  126. REG(&(i2c_base->i2c_dxr)) = (addr >> 8) & 0xff;
  127. } else {
  128. REG(&(i2c_base->i2c_con)) = 0;
  129. return 1;
  130. }
  131. tmp = _poll_i2c_irq(i2c_base,
  132. I2C_STAT_XRDY | I2C_STAT_NACK);
  133. CHECK_NACK();
  134. /* No break, fall through */
  135. case 1:
  136. /* Send address LSByte */
  137. if (tmp & I2C_STAT_XRDY) {
  138. REG(&(i2c_base->i2c_dxr)) = addr & 0xff;
  139. } else {
  140. REG(&(i2c_base->i2c_con)) = 0;
  141. return 1;
  142. }
  143. tmp = _poll_i2c_irq(i2c_base, I2C_STAT_XRDY |
  144. I2C_STAT_NACK | I2C_STAT_ARDY);
  145. CHECK_NACK();
  146. if (!(tmp & I2C_STAT_ARDY)) {
  147. REG(&(i2c_base->i2c_con)) = 0;
  148. return 1;
  149. }
  150. }
  151. }
  152. /* Address phase is over, now read 'len' bytes and stop */
  153. tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP;
  154. REG(&(i2c_base->i2c_cnt)) = len & 0xffff;
  155. REG(&(i2c_base->i2c_sa)) = chip;
  156. REG(&(i2c_base->i2c_con)) = tmp;
  157. for (i = 0; i < len; i++) {
  158. tmp = _poll_i2c_irq(i2c_base, I2C_STAT_RRDY | I2C_STAT_NACK |
  159. I2C_STAT_ROVR);
  160. CHECK_NACK();
  161. if (tmp & I2C_STAT_RRDY) {
  162. buf[i] = REG(&(i2c_base->i2c_drr));
  163. } else {
  164. REG(&(i2c_base->i2c_con)) = 0;
  165. return 1;
  166. }
  167. }
  168. tmp = _poll_i2c_irq(i2c_base, I2C_STAT_SCD | I2C_STAT_NACK);
  169. CHECK_NACK();
  170. if (!(tmp & I2C_STAT_SCD)) {
  171. REG(&(i2c_base->i2c_con)) = 0;
  172. return 1;
  173. }
  174. _flush_rx(i2c_base);
  175. REG(&(i2c_base->i2c_stat)) = 0xffff;
  176. REG(&(i2c_base->i2c_cnt)) = 0;
  177. REG(&(i2c_base->i2c_con)) = 0;
  178. return 0;
  179. }
  180. static int _davinci_i2c_write(struct i2c_regs *i2c_base, uint8_t chip,
  181. uint32_t addr, int alen, uint8_t *buf, int len)
  182. {
  183. uint32_t tmp;
  184. int i;
  185. if ((alen < 0) || (alen > 2)) {
  186. printf("%s(): bogus address length %x\n", __func__, alen);
  187. return 1;
  188. }
  189. if (len < 0) {
  190. printf("%s(): bogus length %x\n", __func__, len);
  191. return 1;
  192. }
  193. if (_wait_for_bus(i2c_base))
  194. return 1;
  195. /* Start address phase */
  196. tmp = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
  197. I2C_CON_TRX | I2C_CON_STP;
  198. REG(&(i2c_base->i2c_cnt)) = (alen == 0) ?
  199. len & 0xffff : (len & 0xffff) + alen;
  200. REG(&(i2c_base->i2c_sa)) = chip;
  201. REG(&(i2c_base->i2c_con)) = tmp;
  202. switch (alen) {
  203. case 2:
  204. /* Send address MSByte */
  205. tmp = _poll_i2c_irq(i2c_base, I2C_STAT_XRDY | I2C_STAT_NACK);
  206. CHECK_NACK();
  207. if (tmp & I2C_STAT_XRDY) {
  208. REG(&(i2c_base->i2c_dxr)) = (addr >> 8) & 0xff;
  209. } else {
  210. REG(&(i2c_base->i2c_con)) = 0;
  211. return 1;
  212. }
  213. /* No break, fall through */
  214. case 1:
  215. /* Send address LSByte */
  216. tmp = _poll_i2c_irq(i2c_base, I2C_STAT_XRDY | I2C_STAT_NACK);
  217. CHECK_NACK();
  218. if (tmp & I2C_STAT_XRDY) {
  219. REG(&(i2c_base->i2c_dxr)) = addr & 0xff;
  220. } else {
  221. REG(&(i2c_base->i2c_con)) = 0;
  222. return 1;
  223. }
  224. }
  225. for (i = 0; i < len; i++) {
  226. tmp = _poll_i2c_irq(i2c_base, I2C_STAT_XRDY | I2C_STAT_NACK);
  227. CHECK_NACK();
  228. if (tmp & I2C_STAT_XRDY)
  229. REG(&(i2c_base->i2c_dxr)) = buf[i];
  230. else
  231. return 1;
  232. }
  233. tmp = _poll_i2c_irq(i2c_base, I2C_STAT_SCD | I2C_STAT_NACK);
  234. CHECK_NACK();
  235. if (!(tmp & I2C_STAT_SCD)) {
  236. REG(&(i2c_base->i2c_con)) = 0;
  237. return 1;
  238. }
  239. _flush_rx(i2c_base);
  240. REG(&(i2c_base->i2c_stat)) = 0xffff;
  241. REG(&(i2c_base->i2c_cnt)) = 0;
  242. REG(&(i2c_base->i2c_con)) = 0;
  243. return 0;
  244. }
  245. static int _davinci_i2c_probe_chip(struct i2c_regs *i2c_base, uint8_t chip)
  246. {
  247. int rc = 1;
  248. if (chip == REG(&(i2c_base->i2c_oa)))
  249. return rc;
  250. REG(&(i2c_base->i2c_con)) = 0;
  251. if (_wait_for_bus(i2c_base))
  252. return 1;
  253. /* try to read one byte from current (or only) address */
  254. REG(&(i2c_base->i2c_cnt)) = 1;
  255. REG(&(i2c_base->i2c_sa)) = chip;
  256. REG(&(i2c_base->i2c_con)) = (I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
  257. I2C_CON_STP);
  258. udelay(50000);
  259. if (!(REG(&(i2c_base->i2c_stat)) & I2C_STAT_NACK)) {
  260. rc = 0;
  261. _flush_rx(i2c_base);
  262. REG(&(i2c_base->i2c_stat)) = 0xffff;
  263. } else {
  264. REG(&(i2c_base->i2c_stat)) = 0xffff;
  265. REG(&(i2c_base->i2c_con)) |= I2C_CON_STP;
  266. udelay(20000);
  267. if (_wait_for_bus(i2c_base))
  268. return 1;
  269. }
  270. _flush_rx(i2c_base);
  271. REG(&(i2c_base->i2c_stat)) = 0xffff;
  272. REG(&(i2c_base->i2c_cnt)) = 0;
  273. return rc;
  274. }
  275. #ifndef CONFIG_DM_I2C
  276. static struct i2c_regs *davinci_get_base(struct i2c_adapter *adap)
  277. {
  278. switch (adap->hwadapnr) {
  279. #if I2C_BUS_MAX >= 3
  280. case 2:
  281. return (struct i2c_regs *)I2C2_BASE;
  282. #endif
  283. #if I2C_BUS_MAX >= 2
  284. case 1:
  285. return (struct i2c_regs *)I2C1_BASE;
  286. #endif
  287. case 0:
  288. return (struct i2c_regs *)I2C_BASE;
  289. default:
  290. printf("wrong hwadapnr: %d\n", adap->hwadapnr);
  291. }
  292. return NULL;
  293. }
  294. static uint davinci_i2c_setspeed(struct i2c_adapter *adap, uint speed)
  295. {
  296. struct i2c_regs *i2c_base = davinci_get_base(adap);
  297. uint ret;
  298. adap->speed = speed;
  299. ret = _davinci_i2c_setspeed(i2c_base, speed);
  300. return ret;
  301. }
  302. static void davinci_i2c_init(struct i2c_adapter *adap, int speed,
  303. int slaveadd)
  304. {
  305. struct i2c_regs *i2c_base = davinci_get_base(adap);
  306. adap->speed = speed;
  307. _davinci_i2c_init(i2c_base, speed, slaveadd);
  308. return;
  309. }
  310. static int davinci_i2c_read(struct i2c_adapter *adap, uint8_t chip,
  311. uint32_t addr, int alen, uint8_t *buf, int len)
  312. {
  313. struct i2c_regs *i2c_base = davinci_get_base(adap);
  314. return _davinci_i2c_read(i2c_base, chip, addr, alen, buf, len);
  315. }
  316. static int davinci_i2c_write(struct i2c_adapter *adap, uint8_t chip,
  317. uint32_t addr, int alen, uint8_t *buf, int len)
  318. {
  319. struct i2c_regs *i2c_base = davinci_get_base(adap);
  320. return _davinci_i2c_write(i2c_base, chip, addr, alen, buf, len);
  321. }
  322. static int davinci_i2c_probe_chip(struct i2c_adapter *adap, uint8_t chip)
  323. {
  324. struct i2c_regs *i2c_base = davinci_get_base(adap);
  325. return _davinci_i2c_probe_chip(i2c_base, chip);
  326. }
  327. U_BOOT_I2C_ADAP_COMPLETE(davinci_0, davinci_i2c_init, davinci_i2c_probe_chip,
  328. davinci_i2c_read, davinci_i2c_write,
  329. davinci_i2c_setspeed,
  330. CONFIG_SYS_DAVINCI_I2C_SPEED,
  331. CONFIG_SYS_DAVINCI_I2C_SLAVE,
  332. 0)
  333. #if I2C_BUS_MAX >= 2
  334. U_BOOT_I2C_ADAP_COMPLETE(davinci_1, davinci_i2c_init, davinci_i2c_probe_chip,
  335. davinci_i2c_read, davinci_i2c_write,
  336. davinci_i2c_setspeed,
  337. CONFIG_SYS_DAVINCI_I2C_SPEED1,
  338. CONFIG_SYS_DAVINCI_I2C_SLAVE1,
  339. 1)
  340. #endif
  341. #if I2C_BUS_MAX >= 3
  342. U_BOOT_I2C_ADAP_COMPLETE(davinci_2, davinci_i2c_init, davinci_i2c_probe_chip,
  343. davinci_i2c_read, davinci_i2c_write,
  344. davinci_i2c_setspeed,
  345. CONFIG_SYS_DAVINCI_I2C_SPEED2,
  346. CONFIG_SYS_DAVINCI_I2C_SLAVE2,
  347. 2)
  348. #endif
  349. #else /* CONFIG_DM_I2C */
  350. static int davinci_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
  351. int nmsgs)
  352. {
  353. struct i2c_bus *i2c_bus = dev_get_priv(bus);
  354. int ret;
  355. debug("i2c_xfer: %d messages\n", nmsgs);
  356. for (; nmsgs > 0; nmsgs--, msg++) {
  357. debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
  358. if (msg->flags & I2C_M_RD) {
  359. ret = _davinci_i2c_read(i2c_bus->regs, msg->addr,
  360. 0, 0, msg->buf, msg->len);
  361. } else {
  362. ret = _davinci_i2c_write(i2c_bus->regs, msg->addr,
  363. 0, 0, msg->buf, msg->len);
  364. }
  365. if (ret) {
  366. debug("i2c_write: error sending\n");
  367. return -EREMOTEIO;
  368. }
  369. }
  370. return ret;
  371. }
  372. static int davinci_i2c_set_speed(struct udevice *dev, uint speed)
  373. {
  374. struct i2c_bus *i2c_bus = dev_get_priv(dev);
  375. i2c_bus->speed = speed;
  376. return _davinci_i2c_setspeed(i2c_bus->regs, speed);
  377. }
  378. static int davinci_i2c_probe(struct udevice *dev)
  379. {
  380. struct i2c_bus *i2c_bus = dev_get_priv(dev);
  381. i2c_bus->id = dev->seq;
  382. i2c_bus->regs = (struct i2c_regs *)devfdt_get_addr(dev);
  383. i2c_bus->speed = 100000;
  384. _davinci_i2c_init(i2c_bus->regs, i2c_bus->speed, 0);
  385. return 0;
  386. }
  387. static int davinci_i2c_probe_chip(struct udevice *bus, uint chip_addr,
  388. uint chip_flags)
  389. {
  390. struct i2c_bus *i2c_bus = dev_get_priv(bus);
  391. return _davinci_i2c_probe_chip(i2c_bus->regs, chip_addr);
  392. }
  393. static const struct dm_i2c_ops davinci_i2c_ops = {
  394. .xfer = davinci_i2c_xfer,
  395. .probe_chip = davinci_i2c_probe_chip,
  396. .set_bus_speed = davinci_i2c_set_speed,
  397. };
  398. static const struct udevice_id davinci_i2c_ids[] = {
  399. { .compatible = "ti,davinci-i2c"},
  400. { .compatible = "ti,keystone-i2c"},
  401. { }
  402. };
  403. U_BOOT_DRIVER(i2c_davinci) = {
  404. .name = "i2c_davinci",
  405. .id = UCLASS_I2C,
  406. .of_match = davinci_i2c_ids,
  407. .probe = davinci_i2c_probe,
  408. .priv_auto_alloc_size = sizeof(struct i2c_bus),
  409. .ops = &davinci_i2c_ops,
  410. };
  411. #endif /* CONFIG_DM_I2C */