soft_i2c.c 9.4 KB

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