mv_i2c.c 12 KB

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