mv_i2c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 <asm/io.h>
  21. #ifdef CONFIG_HARD_I2C
  22. #include <i2c.h>
  23. #include "mv_i2c.h"
  24. #ifdef DEBUG_I2C
  25. #define PRINTD(x) printf x
  26. #else
  27. #define PRINTD(x)
  28. #endif
  29. /* All transfers are described by this data structure */
  30. struct i2c_msg {
  31. u8 condition;
  32. u8 acknack;
  33. u8 direction;
  34. u8 data;
  35. };
  36. struct mv_i2c {
  37. u32 ibmr;
  38. u32 pad0;
  39. u32 idbr;
  40. u32 pad1;
  41. u32 icr;
  42. u32 pad2;
  43. u32 isr;
  44. u32 pad3;
  45. u32 isar;
  46. };
  47. static struct mv_i2c *base;
  48. static void i2c_board_init(struct mv_i2c *base)
  49. {
  50. #ifdef CONFIG_SYS_I2C_INIT_BOARD
  51. u32 icr;
  52. /*
  53. * call board specific i2c bus reset routine before accessing the
  54. * environment, which might be in a chip on that bus. For details
  55. * about this problem see doc/I2C_Edge_Conditions.
  56. *
  57. * disable I2C controller first, otherwhise it thinks we want to
  58. * talk to the slave port...
  59. */
  60. icr = readl(&base->icr);
  61. writel(readl(&base->icr) & ~(ICR_SCLE | ICR_IUE), &base->icr);
  62. i2c_init_board();
  63. writel(icr, &base->icr);
  64. #endif
  65. }
  66. #ifdef CONFIG_I2C_MULTI_BUS
  67. static u32 i2c_regs[CONFIG_MV_I2C_NUM] = CONFIG_MV_I2C_REG;
  68. static unsigned int bus_initialized[CONFIG_MV_I2C_NUM];
  69. static unsigned int current_bus;
  70. int i2c_set_bus_num(unsigned int bus)
  71. {
  72. if ((bus < 0) || (bus >= CONFIG_MV_I2C_NUM)) {
  73. printf("Bad bus: %d\n", bus);
  74. return -1;
  75. }
  76. base = (struct mv_i2c *)i2c_regs[bus];
  77. current_bus = bus;
  78. if (!bus_initialized[current_bus]) {
  79. i2c_board_init(base);
  80. bus_initialized[current_bus] = 1;
  81. }
  82. return 0;
  83. }
  84. unsigned int i2c_get_bus_num(void)
  85. {
  86. return current_bus;
  87. }
  88. #endif
  89. /*
  90. * i2c_reset: - reset the host controller
  91. *
  92. */
  93. static void i2c_reset(void)
  94. {
  95. writel(readl(&base->icr) & ~ICR_IUE, &base->icr); /* disable unit */
  96. writel(readl(&base->icr) | ICR_UR, &base->icr); /* reset the unit */
  97. udelay(100);
  98. writel(readl(&base->icr) & ~ICR_IUE, &base->icr); /* disable unit */
  99. i2c_clk_enable();
  100. writel(CONFIG_SYS_I2C_SLAVE, &base->isar); /* set our slave address */
  101. writel(I2C_ICR_INIT, &base->icr); /* set control reg values */
  102. writel(I2C_ISR_INIT, &base->isr); /* set clear interrupt bits */
  103. writel(readl(&base->icr) | ICR_IUE, &base->icr); /* enable unit */
  104. udelay(100);
  105. }
  106. /*
  107. * i2c_isr_set_cleared: - wait until certain bits of the I2C status register
  108. * are set and cleared
  109. *
  110. * @return: 1 in case of success, 0 means timeout (no match within 10 ms).
  111. */
  112. static int i2c_isr_set_cleared(unsigned long set_mask,
  113. unsigned long cleared_mask)
  114. {
  115. int timeout = 1000, isr;
  116. do {
  117. isr = readl(&base->isr);
  118. udelay(10);
  119. if (timeout-- < 0)
  120. return 0;
  121. } while (((isr & set_mask) != set_mask)
  122. || ((isr & cleared_mask) != 0));
  123. return 1;
  124. }
  125. /*
  126. * i2c_transfer: - Transfer one byte over the i2c bus
  127. *
  128. * This function can tranfer a byte over the i2c bus in both directions.
  129. * It is used by the public API functions.
  130. *
  131. * @return: 0: transfer successful
  132. * -1: message is empty
  133. * -2: transmit timeout
  134. * -3: ACK missing
  135. * -4: receive timeout
  136. * -5: illegal parameters
  137. * -6: bus is busy and couldn't be aquired
  138. */
  139. int i2c_transfer(struct i2c_msg *msg)
  140. {
  141. int ret;
  142. if (!msg)
  143. goto transfer_error_msg_empty;
  144. switch (msg->direction) {
  145. case I2C_WRITE:
  146. /* check if bus is not busy */
  147. if (!i2c_isr_set_cleared(0, ISR_IBB))
  148. goto transfer_error_bus_busy;
  149. /* start transmission */
  150. writel(readl(&base->icr) & ~ICR_START, &base->icr);
  151. writel(readl(&base->icr) & ~ICR_STOP, &base->icr);
  152. writel(msg->data, &base->idbr);
  153. if (msg->condition == I2C_COND_START)
  154. writel(readl(&base->icr) | ICR_START, &base->icr);
  155. if (msg->condition == I2C_COND_STOP)
  156. writel(readl(&base->icr) | ICR_STOP, &base->icr);
  157. if (msg->acknack == I2C_ACKNAK_SENDNAK)
  158. writel(readl(&base->icr) | ICR_ACKNAK, &base->icr);
  159. if (msg->acknack == I2C_ACKNAK_SENDACK)
  160. writel(readl(&base->icr) & ~ICR_ACKNAK, &base->icr);
  161. writel(readl(&base->icr) & ~ICR_ALDIE, &base->icr);
  162. writel(readl(&base->icr) | ICR_TB, &base->icr);
  163. /* transmit register empty? */
  164. if (!i2c_isr_set_cleared(ISR_ITE, 0))
  165. goto transfer_error_transmit_timeout;
  166. /* clear 'transmit empty' state */
  167. writel(readl(&base->isr) | ISR_ITE, &base->isr);
  168. /* wait for ACK from slave */
  169. if (msg->acknack == I2C_ACKNAK_WAITACK)
  170. if (!i2c_isr_set_cleared(0, ISR_ACKNAK))
  171. goto transfer_error_ack_missing;
  172. break;
  173. case I2C_READ:
  174. /* check if bus is not busy */
  175. if (!i2c_isr_set_cleared(0, ISR_IBB))
  176. goto transfer_error_bus_busy;
  177. /* start receive */
  178. writel(readl(&base->icr) & ~ICR_START, &base->icr);
  179. writel(readl(&base->icr) & ~ICR_STOP, &base->icr);
  180. if (msg->condition == I2C_COND_START)
  181. writel(readl(&base->icr) | ICR_START, &base->icr);
  182. if (msg->condition == I2C_COND_STOP)
  183. writel(readl(&base->icr) | ICR_STOP, &base->icr);
  184. if (msg->acknack == I2C_ACKNAK_SENDNAK)
  185. writel(readl(&base->icr) | ICR_ACKNAK, &base->icr);
  186. if (msg->acknack == I2C_ACKNAK_SENDACK)
  187. writel(readl(&base->icr) & ~ICR_ACKNAK, &base->icr);
  188. writel(readl(&base->icr) & ~ICR_ALDIE, &base->icr);
  189. writel(readl(&base->icr) | ICR_TB, &base->icr);
  190. /* receive register full? */
  191. if (!i2c_isr_set_cleared(ISR_IRF, 0))
  192. goto transfer_error_receive_timeout;
  193. msg->data = readl(&base->idbr);
  194. /* clear 'receive empty' state */
  195. writel(readl(&base->isr) | ISR_IRF, &base->isr);
  196. break;
  197. default:
  198. goto transfer_error_illegal_param;
  199. }
  200. return 0;
  201. transfer_error_msg_empty:
  202. PRINTD(("i2c_transfer: error: 'msg' is empty\n"));
  203. ret = -1; goto i2c_transfer_finish;
  204. transfer_error_transmit_timeout:
  205. PRINTD(("i2c_transfer: error: transmit timeout\n"));
  206. ret = -2; goto i2c_transfer_finish;
  207. transfer_error_ack_missing:
  208. PRINTD(("i2c_transfer: error: ACK missing\n"));
  209. ret = -3; goto i2c_transfer_finish;
  210. transfer_error_receive_timeout:
  211. PRINTD(("i2c_transfer: error: receive timeout\n"));
  212. ret = -4; goto i2c_transfer_finish;
  213. transfer_error_illegal_param:
  214. PRINTD(("i2c_transfer: error: illegal parameters\n"));
  215. ret = -5; goto i2c_transfer_finish;
  216. transfer_error_bus_busy:
  217. PRINTD(("i2c_transfer: error: bus is busy\n"));
  218. ret = -6; goto i2c_transfer_finish;
  219. i2c_transfer_finish:
  220. PRINTD(("i2c_transfer: ISR: 0x%04x\n", readl(&base->isr)));
  221. i2c_reset();
  222. return ret;
  223. }
  224. /* ------------------------------------------------------------------------ */
  225. /* API Functions */
  226. /* ------------------------------------------------------------------------ */
  227. void i2c_init(int speed, int slaveaddr)
  228. {
  229. #ifdef CONFIG_I2C_MULTI_BUS
  230. current_bus = 0;
  231. base = (struct mv_i2c *)i2c_regs[current_bus];
  232. #else
  233. base = (struct mv_i2c *)CONFIG_MV_I2C_REG;
  234. #endif
  235. i2c_board_init(base);
  236. }
  237. /*
  238. * i2c_probe: - Test if a chip answers for a given i2c address
  239. *
  240. * @chip: address of the chip which is searched for
  241. * @return: 0 if a chip was found, -1 otherwhise
  242. */
  243. int i2c_probe(uchar chip)
  244. {
  245. struct i2c_msg msg;
  246. i2c_reset();
  247. msg.condition = I2C_COND_START;
  248. msg.acknack = I2C_ACKNAK_WAITACK;
  249. msg.direction = I2C_WRITE;
  250. msg.data = (chip << 1) + 1;
  251. if (i2c_transfer(&msg))
  252. return -1;
  253. msg.condition = I2C_COND_STOP;
  254. msg.acknack = I2C_ACKNAK_SENDNAK;
  255. msg.direction = I2C_READ;
  256. msg.data = 0x00;
  257. if (i2c_transfer(&msg))
  258. return -1;
  259. return 0;
  260. }
  261. /*
  262. * i2c_read: - Read multiple bytes from an i2c device
  263. *
  264. * The higher level routines take into account that this function is only
  265. * called with len < page length of the device (see configuration file)
  266. *
  267. * @chip: address of the chip which is to be read
  268. * @addr: i2c data address within the chip
  269. * @alen: length of the i2c data address (1..2 bytes)
  270. * @buffer: where to write the data
  271. * @len: how much byte do we want to read
  272. * @return: 0 in case of success
  273. */
  274. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  275. {
  276. struct i2c_msg msg;
  277. u8 addr_bytes[3]; /* lowest...highest byte of data address */
  278. PRINTD(("i2c_read(chip=0x%02x, addr=0x%02x, alen=0x%02x, "
  279. "len=0x%02x)\n", chip, addr, alen, len));
  280. i2c_reset();
  281. /* dummy chip address write */
  282. PRINTD(("i2c_read: dummy chip address write\n"));
  283. msg.condition = I2C_COND_START;
  284. msg.acknack = I2C_ACKNAK_WAITACK;
  285. msg.direction = I2C_WRITE;
  286. msg.data = (chip << 1);
  287. msg.data &= 0xFE;
  288. if (i2c_transfer(&msg))
  289. return -1;
  290. /*
  291. * send memory address bytes;
  292. * alen defines how much bytes we have to send.
  293. */
  294. /*addr &= ((1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)-1); */
  295. addr_bytes[0] = (u8)((addr >> 0) & 0x000000FF);
  296. addr_bytes[1] = (u8)((addr >> 8) & 0x000000FF);
  297. addr_bytes[2] = (u8)((addr >> 16) & 0x000000FF);
  298. while (--alen >= 0) {
  299. PRINTD(("i2c_read: send memory word address byte %1d\n", alen));
  300. msg.condition = I2C_COND_NORMAL;
  301. msg.acknack = I2C_ACKNAK_WAITACK;
  302. msg.direction = I2C_WRITE;
  303. msg.data = addr_bytes[alen];
  304. if (i2c_transfer(&msg))
  305. return -1;
  306. }
  307. /* start read sequence */
  308. PRINTD(("i2c_read: start read sequence\n"));
  309. msg.condition = I2C_COND_START;
  310. msg.acknack = I2C_ACKNAK_WAITACK;
  311. msg.direction = I2C_WRITE;
  312. msg.data = (chip << 1);
  313. msg.data |= 0x01;
  314. if (i2c_transfer(&msg))
  315. return -1;
  316. /* read bytes; send NACK at last byte */
  317. while (len--) {
  318. if (len == 0) {
  319. msg.condition = I2C_COND_STOP;
  320. msg.acknack = I2C_ACKNAK_SENDNAK;
  321. } else {
  322. msg.condition = I2C_COND_NORMAL;
  323. msg.acknack = I2C_ACKNAK_SENDACK;
  324. }
  325. msg.direction = I2C_READ;
  326. msg.data = 0x00;
  327. if (i2c_transfer(&msg))
  328. return -1;
  329. *buffer = msg.data;
  330. PRINTD(("i2c_read: reading byte (0x%08x)=0x%02x\n",
  331. (unsigned int)buffer, *buffer));
  332. buffer++;
  333. }
  334. i2c_reset();
  335. return 0;
  336. }
  337. /*
  338. * i2c_write: - Write multiple bytes to an i2c device
  339. *
  340. * The higher level routines take into account that this function is only
  341. * called with len < page length of the device (see configuration file)
  342. *
  343. * @chip: address of the chip which is to be written
  344. * @addr: i2c data address within the chip
  345. * @alen: length of the i2c data address (1..2 bytes)
  346. * @buffer: where to find the data to be written
  347. * @len: how much byte do we want to read
  348. * @return: 0 in case of success
  349. */
  350. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  351. {
  352. struct i2c_msg msg;
  353. u8 addr_bytes[3]; /* lowest...highest byte of data address */
  354. PRINTD(("i2c_write(chip=0x%02x, addr=0x%02x, alen=0x%02x, "
  355. "len=0x%02x)\n", chip, addr, alen, len));
  356. i2c_reset();
  357. /* chip address write */
  358. PRINTD(("i2c_write: chip address write\n"));
  359. msg.condition = I2C_COND_START;
  360. msg.acknack = I2C_ACKNAK_WAITACK;
  361. msg.direction = I2C_WRITE;
  362. msg.data = (chip << 1);
  363. msg.data &= 0xFE;
  364. if (i2c_transfer(&msg))
  365. return -1;
  366. /*
  367. * send memory address bytes;
  368. * alen defines how much bytes we have to send.
  369. */
  370. addr_bytes[0] = (u8)((addr >> 0) & 0x000000FF);
  371. addr_bytes[1] = (u8)((addr >> 8) & 0x000000FF);
  372. addr_bytes[2] = (u8)((addr >> 16) & 0x000000FF);
  373. while (--alen >= 0) {
  374. PRINTD(("i2c_write: send memory word address\n"));
  375. msg.condition = I2C_COND_NORMAL;
  376. msg.acknack = I2C_ACKNAK_WAITACK;
  377. msg.direction = I2C_WRITE;
  378. msg.data = addr_bytes[alen];
  379. if (i2c_transfer(&msg))
  380. return -1;
  381. }
  382. /* write bytes; send NACK at last byte */
  383. while (len--) {
  384. PRINTD(("i2c_write: writing byte (0x%08x)=0x%02x\n",
  385. (unsigned int)buffer, *buffer));
  386. if (len == 0)
  387. msg.condition = I2C_COND_STOP;
  388. else
  389. msg.condition = I2C_COND_NORMAL;
  390. msg.acknack = I2C_ACKNAK_WAITACK;
  391. msg.direction = I2C_WRITE;
  392. msg.data = *(buffer++);
  393. if (i2c_transfer(&msg))
  394. return -1;
  395. }
  396. i2c_reset();
  397. return 0;
  398. }
  399. #endif /* CONFIG_HARD_I2C */