mv_i2c.c 13 KB

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