soft_i2c.c 9.3 KB

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