mv_i2c.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * (C) Copyright 2000
  3. * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
  4. *
  5. * (C) Copyright 2000 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  6. * Marius Groeger <mgroeger@sysgo.de>
  7. *
  8. * (C) Copyright 2003 Pengutronix e.K.
  9. * Robert Schwebel <r.schwebel@pengutronix.de>
  10. *
  11. * (C) Copyright 2011 Marvell Inc.
  12. * Lei Wen <leiwen@marvell.com>
  13. *
  14. * SPDX-License-Identifier: GPL-2.0+
  15. *
  16. * Back ported to the 8xx platform (from the 8260 platform) by
  17. * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
  18. */
  19. #include <common.h>
  20. #include <dm.h>
  21. #include <i2c.h>
  22. #include <asm/io.h>
  23. #include "mv_i2c.h"
  24. /* All transfers are described by this data structure */
  25. struct mv_i2c_msg {
  26. u8 condition;
  27. u8 acknack;
  28. u8 direction;
  29. u8 data;
  30. };
  31. #ifdef CONFIG_ARMADA_3700
  32. /* Armada 3700 has no padding between the registers */
  33. struct mv_i2c {
  34. u32 ibmr;
  35. u32 idbr;
  36. u32 icr;
  37. u32 isr;
  38. u32 isar;
  39. };
  40. #else
  41. struct mv_i2c {
  42. u32 ibmr;
  43. u32 pad0;
  44. u32 idbr;
  45. u32 pad1;
  46. u32 icr;
  47. u32 pad2;
  48. u32 isr;
  49. u32 pad3;
  50. u32 isar;
  51. };
  52. #endif
  53. /*
  54. * Dummy implementation that can be overwritten by a board
  55. * specific function
  56. */
  57. __weak void i2c_clk_enable(void)
  58. {
  59. }
  60. /*
  61. * i2c_reset: - reset the host controller
  62. *
  63. */
  64. static void i2c_reset(struct mv_i2c *base)
  65. {
  66. writel(readl(&base->icr) & ~ICR_IUE, &base->icr); /* disable unit */
  67. writel(readl(&base->icr) | ICR_UR, &base->icr); /* reset the unit */
  68. udelay(100);
  69. writel(readl(&base->icr) & ~ICR_IUE, &base->icr); /* disable unit */
  70. i2c_clk_enable();
  71. writel(CONFIG_SYS_I2C_SLAVE, &base->isar); /* set our slave address */
  72. writel(I2C_ICR_INIT, &base->icr); /* set control reg values */
  73. writel(I2C_ISR_INIT, &base->isr); /* set clear interrupt bits */
  74. writel(readl(&base->icr) | ICR_IUE, &base->icr); /* enable unit */
  75. udelay(100);
  76. }
  77. /*
  78. * i2c_isr_set_cleared: - wait until certain bits of the I2C status register
  79. * are set and cleared
  80. *
  81. * @return: 1 in case of success, 0 means timeout (no match within 10 ms).
  82. */
  83. static int i2c_isr_set_cleared(struct mv_i2c *base, unsigned long set_mask,
  84. unsigned long cleared_mask)
  85. {
  86. int timeout = 1000, isr;
  87. do {
  88. isr = readl(&base->isr);
  89. udelay(10);
  90. if (timeout-- < 0)
  91. return 0;
  92. } while (((isr & set_mask) != set_mask)
  93. || ((isr & cleared_mask) != 0));
  94. return 1;
  95. }
  96. /*
  97. * i2c_transfer: - Transfer one byte over the i2c bus
  98. *
  99. * This function can tranfer a byte over the i2c bus in both directions.
  100. * It is used by the public API functions.
  101. *
  102. * @return: 0: transfer successful
  103. * -1: message is empty
  104. * -2: transmit timeout
  105. * -3: ACK missing
  106. * -4: receive timeout
  107. * -5: illegal parameters
  108. * -6: bus is busy and couldn't be aquired
  109. */
  110. static int i2c_transfer(struct mv_i2c *base, struct mv_i2c_msg *msg)
  111. {
  112. int ret;
  113. if (!msg)
  114. goto transfer_error_msg_empty;
  115. switch (msg->direction) {
  116. case I2C_WRITE:
  117. /* check if bus is not busy */
  118. if (!i2c_isr_set_cleared(base, 0, ISR_IBB))
  119. goto transfer_error_bus_busy;
  120. /* start transmission */
  121. writel(readl(&base->icr) & ~ICR_START, &base->icr);
  122. writel(readl(&base->icr) & ~ICR_STOP, &base->icr);
  123. writel(msg->data, &base->idbr);
  124. if (msg->condition == I2C_COND_START)
  125. writel(readl(&base->icr) | ICR_START, &base->icr);
  126. if (msg->condition == I2C_COND_STOP)
  127. writel(readl(&base->icr) | ICR_STOP, &base->icr);
  128. if (msg->acknack == I2C_ACKNAK_SENDNAK)
  129. writel(readl(&base->icr) | ICR_ACKNAK, &base->icr);
  130. if (msg->acknack == I2C_ACKNAK_SENDACK)
  131. writel(readl(&base->icr) & ~ICR_ACKNAK, &base->icr);
  132. writel(readl(&base->icr) & ~ICR_ALDIE, &base->icr);
  133. writel(readl(&base->icr) | ICR_TB, &base->icr);
  134. /* transmit register empty? */
  135. if (!i2c_isr_set_cleared(base, ISR_ITE, 0))
  136. goto transfer_error_transmit_timeout;
  137. /* clear 'transmit empty' state */
  138. writel(readl(&base->isr) | ISR_ITE, &base->isr);
  139. /* wait for ACK from slave */
  140. if (msg->acknack == I2C_ACKNAK_WAITACK)
  141. if (!i2c_isr_set_cleared(base, 0, ISR_ACKNAK))
  142. goto transfer_error_ack_missing;
  143. break;
  144. case I2C_READ:
  145. /* check if bus is not busy */
  146. if (!i2c_isr_set_cleared(base, 0, ISR_IBB))
  147. goto transfer_error_bus_busy;
  148. /* start receive */
  149. writel(readl(&base->icr) & ~ICR_START, &base->icr);
  150. writel(readl(&base->icr) & ~ICR_STOP, &base->icr);
  151. if (msg->condition == I2C_COND_START)
  152. writel(readl(&base->icr) | ICR_START, &base->icr);
  153. if (msg->condition == I2C_COND_STOP)
  154. writel(readl(&base->icr) | ICR_STOP, &base->icr);
  155. if (msg->acknack == I2C_ACKNAK_SENDNAK)
  156. writel(readl(&base->icr) | ICR_ACKNAK, &base->icr);
  157. if (msg->acknack == I2C_ACKNAK_SENDACK)
  158. writel(readl(&base->icr) & ~ICR_ACKNAK, &base->icr);
  159. writel(readl(&base->icr) & ~ICR_ALDIE, &base->icr);
  160. writel(readl(&base->icr) | ICR_TB, &base->icr);
  161. /* receive register full? */
  162. if (!i2c_isr_set_cleared(base, ISR_IRF, 0))
  163. goto transfer_error_receive_timeout;
  164. msg->data = readl(&base->idbr);
  165. /* clear 'receive empty' state */
  166. writel(readl(&base->isr) | ISR_IRF, &base->isr);
  167. break;
  168. default:
  169. goto transfer_error_illegal_param;
  170. }
  171. return 0;
  172. transfer_error_msg_empty:
  173. debug("i2c_transfer: error: 'msg' is empty\n");
  174. ret = -1;
  175. goto i2c_transfer_finish;
  176. transfer_error_transmit_timeout:
  177. debug("i2c_transfer: error: transmit timeout\n");
  178. ret = -2;
  179. goto i2c_transfer_finish;
  180. transfer_error_ack_missing:
  181. debug("i2c_transfer: error: ACK missing\n");
  182. ret = -3;
  183. goto i2c_transfer_finish;
  184. transfer_error_receive_timeout:
  185. debug("i2c_transfer: error: receive timeout\n");
  186. ret = -4;
  187. goto i2c_transfer_finish;
  188. transfer_error_illegal_param:
  189. debug("i2c_transfer: error: illegal parameters\n");
  190. ret = -5;
  191. goto i2c_transfer_finish;
  192. transfer_error_bus_busy:
  193. debug("i2c_transfer: error: bus is busy\n");
  194. ret = -6;
  195. goto i2c_transfer_finish;
  196. i2c_transfer_finish:
  197. debug("i2c_transfer: ISR: 0x%04x\n", readl(&base->isr));
  198. i2c_reset(base);
  199. return ret;
  200. }
  201. static int __i2c_read(struct mv_i2c *base, uchar chip, u8 *addr, int alen,
  202. uchar *buffer, int len)
  203. {
  204. struct mv_i2c_msg msg;
  205. debug("i2c_read(chip=0x%02x, addr=0x%02x, alen=0x%02x, "
  206. "len=0x%02x)\n", chip, *addr, alen, len);
  207. i2c_reset(base);
  208. /* dummy chip address write */
  209. debug("i2c_read: dummy chip address write\n");
  210. msg.condition = I2C_COND_START;
  211. msg.acknack = I2C_ACKNAK_WAITACK;
  212. msg.direction = I2C_WRITE;
  213. msg.data = (chip << 1);
  214. msg.data &= 0xFE;
  215. if (i2c_transfer(base, &msg))
  216. return -1;
  217. /*
  218. * send memory address bytes;
  219. * alen defines how much bytes we have to send.
  220. */
  221. while (--alen >= 0) {
  222. debug("i2c_read: send address byte %02x (alen=%d)\n",
  223. *addr, alen);
  224. msg.condition = I2C_COND_NORMAL;
  225. msg.acknack = I2C_ACKNAK_WAITACK;
  226. msg.direction = I2C_WRITE;
  227. msg.data = *(addr++);
  228. if (i2c_transfer(base, &msg))
  229. return -1;
  230. }
  231. /* start read sequence */
  232. debug("i2c_read: start read sequence\n");
  233. msg.condition = I2C_COND_START;
  234. msg.acknack = I2C_ACKNAK_WAITACK;
  235. msg.direction = I2C_WRITE;
  236. msg.data = (chip << 1);
  237. msg.data |= 0x01;
  238. if (i2c_transfer(base, &msg))
  239. return -1;
  240. /* read bytes; send NACK at last byte */
  241. while (len--) {
  242. if (len == 0) {
  243. msg.condition = I2C_COND_STOP;
  244. msg.acknack = I2C_ACKNAK_SENDNAK;
  245. } else {
  246. msg.condition = I2C_COND_NORMAL;
  247. msg.acknack = I2C_ACKNAK_SENDACK;
  248. }
  249. msg.direction = I2C_READ;
  250. msg.data = 0x00;
  251. if (i2c_transfer(base, &msg))
  252. return -1;
  253. *buffer = msg.data;
  254. debug("i2c_read: reading byte (%p)=0x%02x\n",
  255. buffer, *buffer);
  256. buffer++;
  257. }
  258. i2c_reset(base);
  259. return 0;
  260. }
  261. static int __i2c_write(struct mv_i2c *base, uchar chip, u8 *addr, int alen,
  262. uchar *buffer, int len)
  263. {
  264. struct mv_i2c_msg msg;
  265. debug("i2c_write(chip=0x%02x, addr=0x%02x, alen=0x%02x, "
  266. "len=0x%02x)\n", chip, *addr, alen, len);
  267. i2c_reset(base);
  268. /* chip address write */
  269. debug("i2c_write: chip address write\n");
  270. msg.condition = I2C_COND_START;
  271. msg.acknack = I2C_ACKNAK_WAITACK;
  272. msg.direction = I2C_WRITE;
  273. msg.data = (chip << 1);
  274. msg.data &= 0xFE;
  275. if (i2c_transfer(base, &msg))
  276. return -1;
  277. /*
  278. * send memory address bytes;
  279. * alen defines how much bytes we have to send.
  280. */
  281. while (--alen >= 0) {
  282. debug("i2c_read: send address byte %02x (alen=%d)\n",
  283. *addr, alen);
  284. msg.condition = I2C_COND_NORMAL;
  285. msg.acknack = I2C_ACKNAK_WAITACK;
  286. msg.direction = I2C_WRITE;
  287. msg.data = *(addr++);
  288. if (i2c_transfer(base, &msg))
  289. return -1;
  290. }
  291. /* write bytes; send NACK at last byte */
  292. while (len--) {
  293. debug("i2c_write: writing byte (%p)=0x%02x\n",
  294. buffer, *buffer);
  295. if (len == 0)
  296. msg.condition = I2C_COND_STOP;
  297. else
  298. msg.condition = I2C_COND_NORMAL;
  299. msg.acknack = I2C_ACKNAK_WAITACK;
  300. msg.direction = I2C_WRITE;
  301. msg.data = *(buffer++);
  302. if (i2c_transfer(base, &msg))
  303. return -1;
  304. }
  305. i2c_reset(base);
  306. return 0;
  307. }
  308. #ifndef CONFIG_DM_I2C
  309. static struct mv_i2c *base_glob;
  310. static void i2c_board_init(struct mv_i2c *base)
  311. {
  312. #ifdef CONFIG_SYS_I2C_INIT_BOARD
  313. u32 icr;
  314. /*
  315. * call board specific i2c bus reset routine before accessing the
  316. * environment, which might be in a chip on that bus. For details
  317. * about this problem see doc/I2C_Edge_Conditions.
  318. *
  319. * disable I2C controller first, otherwhise it thinks we want to
  320. * talk to the slave port...
  321. */
  322. icr = readl(&base->icr);
  323. writel(readl(&base->icr) & ~(ICR_SCLE | ICR_IUE), &base->icr);
  324. i2c_init_board();
  325. writel(icr, &base->icr);
  326. #endif
  327. }
  328. #ifdef CONFIG_I2C_MULTI_BUS
  329. static unsigned long i2c_regs[CONFIG_MV_I2C_NUM] = CONFIG_MV_I2C_REG;
  330. static unsigned int bus_initialized[CONFIG_MV_I2C_NUM];
  331. static unsigned int current_bus;
  332. int i2c_set_bus_num(unsigned int bus)
  333. {
  334. if ((bus < 0) || (bus >= CONFIG_MV_I2C_NUM)) {
  335. printf("Bad bus: %d\n", bus);
  336. return -1;
  337. }
  338. base_glob = (struct mv_i2c *)i2c_regs[bus];
  339. current_bus = bus;
  340. if (!bus_initialized[current_bus]) {
  341. i2c_board_init(base_glob);
  342. bus_initialized[current_bus] = 1;
  343. }
  344. return 0;
  345. }
  346. unsigned int i2c_get_bus_num(void)
  347. {
  348. return current_bus;
  349. }
  350. #endif
  351. /* API Functions */
  352. void i2c_init(int speed, int slaveaddr)
  353. {
  354. #ifdef CONFIG_I2C_MULTI_BUS
  355. current_bus = 0;
  356. base_glob = (struct mv_i2c *)i2c_regs[current_bus];
  357. #else
  358. base_glob = (struct mv_i2c *)CONFIG_MV_I2C_REG;
  359. #endif
  360. i2c_board_init(base_glob);
  361. }
  362. static int __i2c_probe_chip(struct mv_i2c *base, uchar chip)
  363. {
  364. struct mv_i2c_msg msg;
  365. i2c_reset(base);
  366. msg.condition = I2C_COND_START;
  367. msg.acknack = I2C_ACKNAK_WAITACK;
  368. msg.direction = I2C_WRITE;
  369. msg.data = (chip << 1) + 1;
  370. if (i2c_transfer(base, &msg))
  371. return -1;
  372. msg.condition = I2C_COND_STOP;
  373. msg.acknack = I2C_ACKNAK_SENDNAK;
  374. msg.direction = I2C_READ;
  375. msg.data = 0x00;
  376. if (i2c_transfer(base, &msg))
  377. return -1;
  378. return 0;
  379. }
  380. /*
  381. * i2c_probe: - Test if a chip answers for a given i2c address
  382. *
  383. * @chip: address of the chip which is searched for
  384. * @return: 0 if a chip was found, -1 otherwhise
  385. */
  386. int i2c_probe(uchar chip)
  387. {
  388. return __i2c_probe_chip(base_glob, chip);
  389. }
  390. /*
  391. * i2c_read: - Read multiple bytes from an i2c device
  392. *
  393. * The higher level routines take into account that this function is only
  394. * called with len < page length of the device (see configuration file)
  395. *
  396. * @chip: address of the chip which is to be read
  397. * @addr: i2c data address within the chip
  398. * @alen: length of the i2c data address (1..2 bytes)
  399. * @buffer: where to write the data
  400. * @len: how much byte do we want to read
  401. * @return: 0 in case of success
  402. */
  403. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  404. {
  405. u8 addr_bytes[4];
  406. addr_bytes[0] = (addr >> 0) & 0xFF;
  407. addr_bytes[1] = (addr >> 8) & 0xFF;
  408. addr_bytes[2] = (addr >> 16) & 0xFF;
  409. addr_bytes[3] = (addr >> 24) & 0xFF;
  410. return __i2c_read(base_glob, chip, addr_bytes, alen, buffer, len);
  411. }
  412. /*
  413. * i2c_write: - Write multiple bytes to an i2c device
  414. *
  415. * The higher level routines take into account that this function is only
  416. * called with len < page length of the device (see configuration file)
  417. *
  418. * @chip: address of the chip which is to be written
  419. * @addr: i2c data address within the chip
  420. * @alen: length of the i2c data address (1..2 bytes)
  421. * @buffer: where to find the data to be written
  422. * @len: how much byte do we want to read
  423. * @return: 0 in case of success
  424. */
  425. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  426. {
  427. u8 addr_bytes[4];
  428. addr_bytes[0] = (addr >> 0) & 0xFF;
  429. addr_bytes[1] = (addr >> 8) & 0xFF;
  430. addr_bytes[2] = (addr >> 16) & 0xFF;
  431. addr_bytes[3] = (addr >> 24) & 0xFF;
  432. return __i2c_write(base_glob, chip, addr_bytes, alen, buffer, len);
  433. }
  434. #else /* CONFIG_DM_I2C */
  435. struct mv_i2c_priv {
  436. struct mv_i2c *base;
  437. };
  438. static int mv_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
  439. {
  440. struct mv_i2c_priv *i2c = dev_get_priv(bus);
  441. struct i2c_msg *dmsg, *omsg, dummy;
  442. memset(&dummy, 0, sizeof(struct i2c_msg));
  443. /*
  444. * We expect either two messages (one with an offset and one with the
  445. * actual data) or one message (just data or offset/data combined)
  446. */
  447. if (nmsgs > 2 || nmsgs == 0) {
  448. debug("%s: Only one or two messages are supported.", __func__);
  449. return -1;
  450. }
  451. omsg = nmsgs == 1 ? &dummy : msg;
  452. dmsg = nmsgs == 1 ? msg : msg + 1;
  453. if (dmsg->flags & I2C_M_RD)
  454. return __i2c_read(i2c->base, dmsg->addr, omsg->buf,
  455. omsg->len, dmsg->buf, dmsg->len);
  456. else
  457. return __i2c_write(i2c->base, dmsg->addr, omsg->buf,
  458. omsg->len, dmsg->buf, dmsg->len);
  459. }
  460. static int mv_i2c_probe(struct udevice *bus)
  461. {
  462. struct mv_i2c_priv *priv = dev_get_priv(bus);
  463. priv->base = (void *)dev_get_addr_ptr(bus);
  464. return 0;
  465. }
  466. static const struct dm_i2c_ops mv_i2c_ops = {
  467. .xfer = mv_i2c_xfer,
  468. };
  469. static const struct udevice_id mv_i2c_ids[] = {
  470. { .compatible = "marvell,armada-3700-i2c" },
  471. { }
  472. };
  473. U_BOOT_DRIVER(i2c_mv) = {
  474. .name = "i2c_mv",
  475. .id = UCLASS_I2C,
  476. .of_match = mv_i2c_ids,
  477. .probe = mv_i2c_probe,
  478. .priv_auto_alloc_size = sizeof(struct mv_i2c_priv),
  479. .ops = &mv_i2c_ops,
  480. };
  481. #endif /* CONFIG_DM_I2C */