i2c.c 12 KB

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