i2c.c 12 KB

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