soft_i2c.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * (C) Copyright 2001, 2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. * This has been changed substantially by Gerald Van Baren, Custom IDEAS,
  24. * vanbaren@cideas.com. It was heavily influenced by LiMon, written by
  25. * Neil Russell.
  26. */
  27. #include <common.h>
  28. #ifdef CONFIG_MPC8260 /* only valid for MPC8260 */
  29. #include <ioports.h>
  30. #endif
  31. #ifdef CONFIG_AT91RM9200 /* need this for the at91rm9200 */
  32. #include <asm/io.h>
  33. #include <asm/arch/hardware.h>
  34. #endif
  35. #ifdef CONFIG_IXP425 /* only valid for IXP425 */
  36. #include <asm/arch/ixp425.h>
  37. #endif
  38. #ifdef CONFIG_LPC2292
  39. #include <asm/arch/hardware.h>
  40. #endif
  41. #include <i2c.h>
  42. /* #define DEBUG_I2C */
  43. #ifdef DEBUG_I2C
  44. DECLARE_GLOBAL_DATA_PTR;
  45. #endif
  46. /*-----------------------------------------------------------------------
  47. * Definitions
  48. */
  49. #define RETRIES 0
  50. #define I2C_ACK 0 /* PD_SDA level to ack a byte */
  51. #define I2C_NOACK 1 /* PD_SDA level to noack a byte */
  52. #ifdef DEBUG_I2C
  53. #define PRINTD(fmt,args...) do { \
  54. if (gd->have_console) \
  55. printf (fmt ,##args); \
  56. } while (0)
  57. #else
  58. #define PRINTD(fmt,args...)
  59. #endif
  60. /*-----------------------------------------------------------------------
  61. * Local functions
  62. */
  63. static void send_reset (void);
  64. static void send_start (void);
  65. static void send_stop (void);
  66. static void send_ack (int);
  67. static int write_byte (uchar byte);
  68. static uchar read_byte (int);
  69. /*-----------------------------------------------------------------------
  70. * Send a reset sequence consisting of 9 clocks with the data signal high
  71. * to clock any confused device back into an idle state. Also send a
  72. * <stop> at the end of the sequence for belts & suspenders.
  73. */
  74. static void send_reset(void)
  75. {
  76. #ifdef CONFIG_MPC8260
  77. volatile ioport_t *iop = ioport_addr((immap_t *)CFG_IMMR, I2C_PORT);
  78. #endif
  79. #ifdef CONFIG_8xx
  80. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  81. #endif
  82. int j;
  83. I2C_SCL(1);
  84. I2C_SDA(1);
  85. #ifdef I2C_INIT
  86. I2C_INIT;
  87. #endif
  88. I2C_TRISTATE;
  89. for(j = 0; j < 9; j++) {
  90. I2C_SCL(0);
  91. I2C_DELAY;
  92. I2C_DELAY;
  93. I2C_SCL(1);
  94. I2C_DELAY;
  95. I2C_DELAY;
  96. }
  97. send_stop();
  98. I2C_TRISTATE;
  99. }
  100. /*-----------------------------------------------------------------------
  101. * START: High -> Low on SDA while SCL is High
  102. */
  103. static void send_start(void)
  104. {
  105. #ifdef CONFIG_MPC8260
  106. volatile ioport_t *iop = ioport_addr((immap_t *)CFG_IMMR, I2C_PORT);
  107. #endif
  108. #ifdef CONFIG_8xx
  109. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  110. #endif
  111. I2C_DELAY;
  112. I2C_SDA(1);
  113. I2C_ACTIVE;
  114. I2C_DELAY;
  115. I2C_SCL(1);
  116. I2C_DELAY;
  117. I2C_SDA(0);
  118. I2C_DELAY;
  119. }
  120. /*-----------------------------------------------------------------------
  121. * STOP: Low -> High on SDA while SCL is High
  122. */
  123. static void send_stop(void)
  124. {
  125. #ifdef CONFIG_MPC8260
  126. volatile ioport_t *iop = ioport_addr((immap_t *)CFG_IMMR, I2C_PORT);
  127. #endif
  128. #ifdef CONFIG_8xx
  129. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  130. #endif
  131. I2C_SCL(0);
  132. I2C_DELAY;
  133. I2C_SDA(0);
  134. I2C_ACTIVE;
  135. I2C_DELAY;
  136. I2C_SCL(1);
  137. I2C_DELAY;
  138. I2C_SDA(1);
  139. I2C_DELAY;
  140. I2C_TRISTATE;
  141. }
  142. /*-----------------------------------------------------------------------
  143. * ack should be I2C_ACK or I2C_NOACK
  144. */
  145. static void send_ack(int ack)
  146. {
  147. #ifdef CONFIG_MPC8260
  148. volatile ioport_t *iop = ioport_addr((immap_t *)CFG_IMMR, I2C_PORT);
  149. #endif
  150. #ifdef CONFIG_8xx
  151. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  152. #endif
  153. I2C_SCL(0);
  154. I2C_DELAY;
  155. I2C_ACTIVE;
  156. I2C_SDA(ack);
  157. I2C_DELAY;
  158. I2C_SCL(1);
  159. I2C_DELAY;
  160. I2C_DELAY;
  161. I2C_SCL(0);
  162. I2C_DELAY;
  163. }
  164. /*-----------------------------------------------------------------------
  165. * Send 8 bits and look for an acknowledgement.
  166. */
  167. static int write_byte(uchar data)
  168. {
  169. #ifdef CONFIG_MPC8260
  170. volatile ioport_t *iop = ioport_addr((immap_t *)CFG_IMMR, I2C_PORT);
  171. #endif
  172. #ifdef CONFIG_8xx
  173. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  174. #endif
  175. int j;
  176. int nack;
  177. I2C_ACTIVE;
  178. for(j = 0; j < 8; j++) {
  179. I2C_SCL(0);
  180. I2C_DELAY;
  181. I2C_SDA(data & 0x80);
  182. I2C_DELAY;
  183. I2C_SCL(1);
  184. I2C_DELAY;
  185. I2C_DELAY;
  186. data <<= 1;
  187. }
  188. /*
  189. * Look for an <ACK>(negative logic) and return it.
  190. */
  191. I2C_SCL(0);
  192. I2C_DELAY;
  193. I2C_SDA(1);
  194. I2C_TRISTATE;
  195. I2C_DELAY;
  196. I2C_SCL(1);
  197. I2C_DELAY;
  198. I2C_DELAY;
  199. nack = I2C_READ;
  200. I2C_SCL(0);
  201. I2C_DELAY;
  202. I2C_ACTIVE;
  203. return(nack); /* not a nack is an ack */
  204. }
  205. /*-----------------------------------------------------------------------
  206. * if ack == I2C_ACK, ACK the byte so can continue reading, else
  207. * send I2C_NOACK to end the read.
  208. */
  209. static uchar read_byte(int ack)
  210. {
  211. #ifdef CONFIG_MPC8260
  212. volatile ioport_t *iop = ioport_addr((immap_t *)CFG_IMMR, I2C_PORT);
  213. #endif
  214. #ifdef CONFIG_8xx
  215. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  216. #endif
  217. int data;
  218. int j;
  219. /*
  220. * Read 8 bits, MSB first.
  221. */
  222. I2C_TRISTATE;
  223. I2C_SDA(1);
  224. data = 0;
  225. for(j = 0; j < 8; j++) {
  226. I2C_SCL(0);
  227. I2C_DELAY;
  228. I2C_SCL(1);
  229. I2C_DELAY;
  230. data <<= 1;
  231. data |= I2C_READ;
  232. I2C_DELAY;
  233. }
  234. send_ack(ack);
  235. return(data);
  236. }
  237. /*=====================================================================*/
  238. /* Public Functions */
  239. /*=====================================================================*/
  240. /*-----------------------------------------------------------------------
  241. * Initialization
  242. */
  243. void i2c_init (int speed, int slaveaddr)
  244. {
  245. /*
  246. * WARNING: Do NOT save speed in a static variable: if the
  247. * I2C routines are called before RAM is initialized (to read
  248. * the DIMM SPD, for instance), RAM won't be usable and your
  249. * system will crash.
  250. */
  251. send_reset ();
  252. }
  253. /*-----------------------------------------------------------------------
  254. * Probe to see if a chip is present. Also good for checking for the
  255. * completion of EEPROM writes since the chip stops responding until
  256. * the write completes (typically 10mSec).
  257. */
  258. int i2c_probe(uchar addr)
  259. {
  260. int rc;
  261. /*
  262. * perform 1 byte write transaction with just address byte
  263. * (fake write)
  264. */
  265. send_start();
  266. rc = write_byte ((addr << 1) | 0);
  267. send_stop();
  268. return (rc ? 1 : 0);
  269. }
  270. /*-----------------------------------------------------------------------
  271. * Read bytes
  272. */
  273. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  274. {
  275. int shift;
  276. PRINTD("i2c_read: chip %02X addr %02X alen %d buffer %p len %d\n",
  277. chip, addr, alen, buffer, len);
  278. #ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW
  279. /*
  280. * EEPROM chips that implement "address overflow" are ones
  281. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  282. * address and the extra bits end up in the "chip address"
  283. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  284. * four 256 byte chips.
  285. *
  286. * Note that we consider the length of the address field to
  287. * still be one byte because the extra address bits are
  288. * hidden in the chip address.
  289. */
  290. chip |= ((addr >> (alen * 8)) & CFG_I2C_EEPROM_ADDR_OVERFLOW);
  291. PRINTD("i2c_read: fix addr_overflow: chip %02X addr %02X\n",
  292. chip, addr);
  293. #endif
  294. /*
  295. * Do the addressing portion of a write cycle to set the
  296. * chip's address pointer. If the address length is zero,
  297. * don't do the normal write cycle to set the address pointer,
  298. * there is no address pointer in this chip.
  299. */
  300. send_start();
  301. if(alen > 0) {
  302. if(write_byte(chip << 1)) { /* write cycle */
  303. send_stop();
  304. PRINTD("i2c_read, no chip responded %02X\n", chip);
  305. return(1);
  306. }
  307. shift = (alen-1) * 8;
  308. while(alen-- > 0) {
  309. if(write_byte(addr >> shift)) {
  310. PRINTD("i2c_read, address not <ACK>ed\n");
  311. return(1);
  312. }
  313. shift -= 8;
  314. }
  315. send_stop(); /* reportedly some chips need a full stop */
  316. send_start();
  317. }
  318. /*
  319. * Send the chip address again, this time for a read cycle.
  320. * Then read the data. On the last byte, we do a NACK instead
  321. * of an ACK(len == 0) to terminate the read.
  322. */
  323. write_byte((chip << 1) | 1); /* read cycle */
  324. while(len-- > 0) {
  325. *buffer++ = read_byte(len == 0);
  326. }
  327. send_stop();
  328. return(0);
  329. }
  330. /*-----------------------------------------------------------------------
  331. * Write bytes
  332. */
  333. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  334. {
  335. int shift, failures = 0;
  336. PRINTD("i2c_write: chip %02X addr %02X alen %d buffer %p len %d\n",
  337. chip, addr, alen, buffer, len);
  338. send_start();
  339. if(write_byte(chip << 1)) { /* write cycle */
  340. send_stop();
  341. PRINTD("i2c_write, no chip responded %02X\n", chip);
  342. return(1);
  343. }
  344. shift = (alen-1) * 8;
  345. while(alen-- > 0) {
  346. if(write_byte(addr >> shift)) {
  347. PRINTD("i2c_write, address not <ACK>ed\n");
  348. return(1);
  349. }
  350. shift -= 8;
  351. }
  352. while(len-- > 0) {
  353. if(write_byte(*buffer++)) {
  354. failures++;
  355. }
  356. }
  357. send_stop();
  358. return(failures);
  359. }
  360. /*-----------------------------------------------------------------------
  361. * Read a register
  362. */
  363. uchar i2c_reg_read(uchar i2c_addr, uchar reg)
  364. {
  365. uchar buf;
  366. i2c_read(i2c_addr, reg, 1, &buf, 1);
  367. return(buf);
  368. }
  369. /*-----------------------------------------------------------------------
  370. * Write a register
  371. */
  372. void i2c_reg_write(uchar i2c_addr, uchar reg, uchar val)
  373. {
  374. i2c_write(i2c_addr, reg, 1, &val, 1);
  375. }