mv_i2c.c 12 KB

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