i2c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. /* FIXME: this file is PXA255 specific! What about other XScales? */
  33. #include <common.h>
  34. #include <asm/io.h>
  35. #ifdef CONFIG_HARD_I2C
  36. /*
  37. * - CONFIG_SYS_I2C_SPEED
  38. * - I2C_PXA_SLAVE_ADDR
  39. */
  40. #include <asm/arch/hardware.h>
  41. #include <asm/arch/pxa-regs.h>
  42. #include <i2c.h>
  43. /*#define DEBUG_I2C 1 /###* activate local debugging output */
  44. #define I2C_PXA_SLAVE_ADDR 0x1 /* slave pxa unit address */
  45. #if (CONFIG_SYS_I2C_SPEED == 400000)
  46. #define I2C_ICR_INIT (ICR_FM | ICR_BEIE | ICR_IRFIE | ICR_ITEIE | ICR_GCD | ICR_SCLE)
  47. #else
  48. #define I2C_ICR_INIT (ICR_BEIE | ICR_IRFIE | ICR_ITEIE | ICR_GCD | ICR_SCLE)
  49. #endif
  50. #define I2C_ISR_INIT 0x7FF
  51. #ifdef DEBUG_I2C
  52. #define PRINTD(x) printf x
  53. #else
  54. #define PRINTD(x)
  55. #endif
  56. /* Shall the current transfer have a start/stop condition? */
  57. #define I2C_COND_NORMAL 0
  58. #define I2C_COND_START 1
  59. #define I2C_COND_STOP 2
  60. /* Shall the current transfer be ack/nacked or being waited for it? */
  61. #define I2C_ACKNAK_WAITACK 1
  62. #define I2C_ACKNAK_SENDACK 2
  63. #define I2C_ACKNAK_SENDNAK 4
  64. /* Specify who shall transfer the data (master or slave) */
  65. #define I2C_READ 0
  66. #define I2C_WRITE 1
  67. /* All transfers are described by this data structure */
  68. struct i2c_msg {
  69. u8 condition;
  70. u8 acknack;
  71. u8 direction;
  72. u8 data;
  73. };
  74. /**
  75. * i2c_pxa_reset: - reset the host controller
  76. *
  77. */
  78. static void i2c_reset( void )
  79. {
  80. writel(readl(ICR) & ~ICR_IUE, ICR); /* disable unit */
  81. writel(readl(ICR) | ICR_UR, ICR); /* reset the unit */
  82. udelay(100);
  83. writel(readl(ICR) & ~ICR_IUE, ICR); /* disable unit */
  84. #ifdef CONFIG_CPU_MONAHANS
  85. /* | CKENB_1_PWM1 | CKENB_0_PWM0); */
  86. writel(readl(CKENB) | (CKENB_4_I2C), CKENB);
  87. #else /* CONFIG_CPU_MONAHANS */
  88. /* set the global I2C clock on */
  89. writel(readl(CKEN) | CKEN14_I2C, CKEN);
  90. #endif
  91. writel(I2C_PXA_SLAVE_ADDR, ISAR); /* set our slave address */
  92. writel(I2C_ICR_INIT, ICR); /* set control reg values */
  93. writel(I2C_ISR_INIT, ISR); /* set clear interrupt bits */
  94. writel(readl(ICR) | ICR_IUE, ICR); /* enable unit */
  95. udelay(100);
  96. }
  97. /**
  98. * i2c_isr_set_cleared: - wait until certain bits of the I2C status register
  99. * are set and cleared
  100. *
  101. * @return: 1 in case of success, 0 means timeout (no match within 10 ms).
  102. */
  103. static int i2c_isr_set_cleared( unsigned long set_mask, unsigned long cleared_mask )
  104. {
  105. int timeout = 10000;
  106. while( ((ISR & set_mask)!=set_mask) || ((ISR & cleared_mask)!=0) ){
  107. udelay( 10 );
  108. if( timeout-- < 0 ) 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)) return -1;
  238. msg.condition = I2C_COND_STOP;
  239. msg.acknack = I2C_ACKNAK_SENDNAK;
  240. msg.direction = I2C_READ;
  241. msg.data = 0x00;
  242. if (i2c_transfer(&msg)) return -1;
  243. return 0;
  244. }
  245. /**
  246. * i2c_read: - Read multiple bytes from an i2c device
  247. *
  248. * The higher level routines take into account that this function is only
  249. * called with len < page length of the device (see configuration file)
  250. *
  251. * @chip: address of the chip which is to be read
  252. * @addr: i2c data address within the chip
  253. * @alen: length of the i2c data address (1..2 bytes)
  254. * @buffer: where to write the data
  255. * @len: how much byte do we want to read
  256. * @return: 0 in case of success
  257. */
  258. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  259. {
  260. struct i2c_msg msg;
  261. u8 addr_bytes[3]; /* lowest...highest byte of data address */
  262. int ret;
  263. PRINTD(("i2c_read(chip=0x%02x, addr=0x%02x, alen=0x%02x, len=0x%02x)\n",chip,addr,alen,len));
  264. i2c_reset();
  265. /* dummy chip address write */
  266. PRINTD(("i2c_read: dummy chip address write\n"));
  267. msg.condition = I2C_COND_START;
  268. msg.acknack = I2C_ACKNAK_WAITACK;
  269. msg.direction = I2C_WRITE;
  270. msg.data = (chip << 1);
  271. msg.data &= 0xFE;
  272. if ((ret=i2c_transfer(&msg))) return -1;
  273. /*
  274. * send memory address bytes;
  275. * alen defines how much bytes we have to send.
  276. */
  277. /*addr &= ((1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)-1); */
  278. addr_bytes[0] = (u8)((addr >> 0) & 0x000000FF);
  279. addr_bytes[1] = (u8)((addr >> 8) & 0x000000FF);
  280. addr_bytes[2] = (u8)((addr >> 16) & 0x000000FF);
  281. while (--alen >= 0) {
  282. PRINTD(("i2c_read: send memory word address byte %1d\n",alen));
  283. msg.condition = I2C_COND_NORMAL;
  284. msg.acknack = I2C_ACKNAK_WAITACK;
  285. msg.direction = I2C_WRITE;
  286. msg.data = addr_bytes[alen];
  287. if ((ret=i2c_transfer(&msg))) return -1;
  288. }
  289. /* start read sequence */
  290. PRINTD(("i2c_read: start read sequence\n"));
  291. msg.condition = I2C_COND_START;
  292. msg.acknack = I2C_ACKNAK_WAITACK;
  293. msg.direction = I2C_WRITE;
  294. msg.data = (chip << 1);
  295. msg.data |= 0x01;
  296. if ((ret=i2c_transfer(&msg))) return -1;
  297. /* read bytes; send NACK at last byte */
  298. while (len--) {
  299. if (len==0) {
  300. msg.condition = I2C_COND_STOP;
  301. msg.acknack = I2C_ACKNAK_SENDNAK;
  302. } else {
  303. msg.condition = I2C_COND_NORMAL;
  304. msg.acknack = I2C_ACKNAK_SENDACK;
  305. }
  306. msg.direction = I2C_READ;
  307. msg.data = 0x00;
  308. if ((ret=i2c_transfer(&msg))) return -1;
  309. *buffer = msg.data;
  310. PRINTD(("i2c_read: reading byte (0x%08x)=0x%02x\n",(unsigned int)buffer,*buffer));
  311. buffer++;
  312. }
  313. i2c_reset();
  314. return 0;
  315. }
  316. /**
  317. * i2c_write: - Write multiple bytes to an i2c device
  318. *
  319. * The higher level routines take into account that this function is only
  320. * called with len < page length of the device (see configuration file)
  321. *
  322. * @chip: address of the chip which is to be written
  323. * @addr: i2c data address within the chip
  324. * @alen: length of the i2c data address (1..2 bytes)
  325. * @buffer: where to find the data to be written
  326. * @len: how much byte do we want to read
  327. * @return: 0 in case of success
  328. */
  329. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  330. {
  331. struct i2c_msg msg;
  332. u8 addr_bytes[3]; /* lowest...highest byte of data address */
  333. PRINTD(("i2c_write(chip=0x%02x, addr=0x%02x, alen=0x%02x, len=0x%02x)\n",chip,addr,alen,len));
  334. i2c_reset();
  335. /* chip address write */
  336. PRINTD(("i2c_write: chip address write\n"));
  337. msg.condition = I2C_COND_START;
  338. msg.acknack = I2C_ACKNAK_WAITACK;
  339. msg.direction = I2C_WRITE;
  340. msg.data = (chip << 1);
  341. msg.data &= 0xFE;
  342. if (i2c_transfer(&msg)) return -1;
  343. /*
  344. * send memory address bytes;
  345. * alen defines how much bytes we have to send.
  346. */
  347. addr_bytes[0] = (u8)((addr >> 0) & 0x000000FF);
  348. addr_bytes[1] = (u8)((addr >> 8) & 0x000000FF);
  349. addr_bytes[2] = (u8)((addr >> 16) & 0x000000FF);
  350. while (--alen >= 0) {
  351. PRINTD(("i2c_write: send memory word address\n"));
  352. msg.condition = I2C_COND_NORMAL;
  353. msg.acknack = I2C_ACKNAK_WAITACK;
  354. msg.direction = I2C_WRITE;
  355. msg.data = addr_bytes[alen];
  356. if (i2c_transfer(&msg)) return -1;
  357. }
  358. /* write bytes; send NACK at last byte */
  359. while (len--) {
  360. PRINTD(("i2c_write: writing byte (0x%08x)=0x%02x\n",(unsigned int)buffer,*buffer));
  361. if (len==0)
  362. msg.condition = I2C_COND_STOP;
  363. else
  364. msg.condition = I2C_COND_NORMAL;
  365. msg.acknack = I2C_ACKNAK_WAITACK;
  366. msg.direction = I2C_WRITE;
  367. msg.data = *(buffer++);
  368. if (i2c_transfer(&msg)) return -1;
  369. }
  370. i2c_reset();
  371. return 0;
  372. }
  373. #endif /* CONFIG_HARD_I2C */