mv_i2c.c 14 KB

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