soft_i2c.c 9.4 KB

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