soft_i2c.c 9.3 KB

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