soft_i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. #include <asm/io.h>
  31. #endif
  32. #if defined(CONFIG_AT91RM9200) || \
  33. defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \
  34. defined(CONFIG_AT91SAM9263)
  35. #include <asm/io.h>
  36. #include <asm/arch/hardware.h>
  37. #include <asm/arch/at91_pio.h>
  38. #ifdef CONFIG_AT91_LEGACY
  39. #include <asm/arch/gpio.h>
  40. #endif
  41. #endif
  42. #ifdef CONFIG_IXP425 /* only valid for IXP425 */
  43. #include <asm/arch/ixp425.h>
  44. #endif
  45. #ifdef CONFIG_LPC2292
  46. #include <asm/arch/hardware.h>
  47. #endif
  48. #if defined(CONFIG_MPC852T) || defined(CONFIG_MPC866)
  49. #include <asm/io.h>
  50. #endif
  51. #include <i2c.h>
  52. #if defined(CONFIG_SOFT_I2C_GPIO_SCL)
  53. # include <asm/gpio.h>
  54. # ifndef I2C_GPIO_SYNC
  55. # define I2C_GPIO_SYNC
  56. # endif
  57. # ifndef I2C_INIT
  58. # define I2C_INIT \
  59. do { \
  60. gpio_request(CONFIG_SOFT_I2C_GPIO_SCL, "soft_i2c"); \
  61. gpio_request(CONFIG_SOFT_I2C_GPIO_SDA, "soft_i2c"); \
  62. } while (0)
  63. # endif
  64. # ifndef I2C_ACTIVE
  65. # define I2C_ACTIVE do { } while (0)
  66. # endif
  67. # ifndef I2C_TRISTATE
  68. # define I2C_TRISTATE do { } while (0)
  69. # endif
  70. # ifndef I2C_READ
  71. # define I2C_READ gpio_get_value(CONFIG_SOFT_I2C_GPIO_SDA)
  72. # endif
  73. # ifndef I2C_SDA
  74. # define I2C_SDA(bit) \
  75. do { \
  76. if (bit) \
  77. gpio_direction_input(CONFIG_SOFT_I2C_GPIO_SDA); \
  78. else \
  79. gpio_direction_output(CONFIG_SOFT_I2C_GPIO_SDA, 0); \
  80. I2C_GPIO_SYNC; \
  81. } while (0)
  82. # endif
  83. # ifndef I2C_SCL
  84. # define I2C_SCL(bit) \
  85. do { \
  86. gpio_direction_output(CONFIG_SOFT_I2C_GPIO_SCL, bit); \
  87. I2C_GPIO_SYNC; \
  88. } while (0)
  89. # endif
  90. # ifndef I2C_DELAY
  91. # define I2C_DELAY udelay(5) /* 1/4 I2C clock duration */
  92. # endif
  93. #endif
  94. /* #define DEBUG_I2C */
  95. #ifdef DEBUG_I2C
  96. DECLARE_GLOBAL_DATA_PTR;
  97. #endif
  98. /*-----------------------------------------------------------------------
  99. * Definitions
  100. */
  101. #define RETRIES 0
  102. #define I2C_ACK 0 /* PD_SDA level to ack a byte */
  103. #define I2C_NOACK 1 /* PD_SDA level to noack a byte */
  104. #ifdef DEBUG_I2C
  105. #define PRINTD(fmt,args...) do { \
  106. if (gd->have_console) \
  107. printf (fmt ,##args); \
  108. } while (0)
  109. #else
  110. #define PRINTD(fmt,args...)
  111. #endif
  112. #if defined(CONFIG_I2C_MULTI_BUS)
  113. static unsigned int i2c_bus_num __attribute__ ((section (".data"))) = 0;
  114. #endif /* CONFIG_I2C_MULTI_BUS */
  115. /*-----------------------------------------------------------------------
  116. * Local functions
  117. */
  118. #if !defined(CONFIG_SYS_I2C_INIT_BOARD)
  119. static void send_reset (void);
  120. #endif
  121. static void send_start (void);
  122. static void send_stop (void);
  123. static void send_ack (int);
  124. static int write_byte (uchar byte);
  125. static uchar read_byte (int);
  126. #if !defined(CONFIG_SYS_I2C_INIT_BOARD)
  127. /*-----------------------------------------------------------------------
  128. * Send a reset sequence consisting of 9 clocks with the data signal high
  129. * to clock any confused device back into an idle state. Also send a
  130. * <stop> at the end of the sequence for belts & suspenders.
  131. */
  132. static void send_reset(void)
  133. {
  134. I2C_SOFT_DECLARATIONS /* intentional without ';' */
  135. int j;
  136. I2C_SCL(1);
  137. I2C_SDA(1);
  138. #ifdef I2C_INIT
  139. I2C_INIT;
  140. #endif
  141. I2C_TRISTATE;
  142. for(j = 0; j < 9; j++) {
  143. I2C_SCL(0);
  144. I2C_DELAY;
  145. I2C_DELAY;
  146. I2C_SCL(1);
  147. I2C_DELAY;
  148. I2C_DELAY;
  149. }
  150. send_stop();
  151. I2C_TRISTATE;
  152. }
  153. #endif
  154. /*-----------------------------------------------------------------------
  155. * START: High -> Low on SDA while SCL is High
  156. */
  157. static void send_start(void)
  158. {
  159. I2C_SOFT_DECLARATIONS /* intentional without ';' */
  160. I2C_DELAY;
  161. I2C_SDA(1);
  162. I2C_ACTIVE;
  163. I2C_DELAY;
  164. I2C_SCL(1);
  165. I2C_DELAY;
  166. I2C_SDA(0);
  167. I2C_DELAY;
  168. }
  169. /*-----------------------------------------------------------------------
  170. * STOP: Low -> High on SDA while SCL is High
  171. */
  172. static void send_stop(void)
  173. {
  174. I2C_SOFT_DECLARATIONS /* intentional without ';' */
  175. I2C_SCL(0);
  176. I2C_DELAY;
  177. I2C_SDA(0);
  178. I2C_ACTIVE;
  179. I2C_DELAY;
  180. I2C_SCL(1);
  181. I2C_DELAY;
  182. I2C_SDA(1);
  183. I2C_DELAY;
  184. I2C_TRISTATE;
  185. }
  186. /*-----------------------------------------------------------------------
  187. * ack should be I2C_ACK or I2C_NOACK
  188. */
  189. static void send_ack(int ack)
  190. {
  191. I2C_SOFT_DECLARATIONS /* intentional without ';' */
  192. I2C_SCL(0);
  193. I2C_DELAY;
  194. I2C_ACTIVE;
  195. I2C_SDA(ack);
  196. I2C_DELAY;
  197. I2C_SCL(1);
  198. I2C_DELAY;
  199. I2C_DELAY;
  200. I2C_SCL(0);
  201. I2C_DELAY;
  202. }
  203. /*-----------------------------------------------------------------------
  204. * Send 8 bits and look for an acknowledgement.
  205. */
  206. static int write_byte(uchar data)
  207. {
  208. I2C_SOFT_DECLARATIONS /* intentional without ';' */
  209. int j;
  210. int nack;
  211. I2C_ACTIVE;
  212. for(j = 0; j < 8; j++) {
  213. I2C_SCL(0);
  214. I2C_DELAY;
  215. I2C_SDA(data & 0x80);
  216. I2C_DELAY;
  217. I2C_SCL(1);
  218. I2C_DELAY;
  219. I2C_DELAY;
  220. data <<= 1;
  221. }
  222. /*
  223. * Look for an <ACK>(negative logic) and return it.
  224. */
  225. I2C_SCL(0);
  226. I2C_DELAY;
  227. I2C_SDA(1);
  228. I2C_TRISTATE;
  229. I2C_DELAY;
  230. I2C_SCL(1);
  231. I2C_DELAY;
  232. I2C_DELAY;
  233. nack = I2C_READ;
  234. I2C_SCL(0);
  235. I2C_DELAY;
  236. I2C_ACTIVE;
  237. return(nack); /* not a nack is an ack */
  238. }
  239. #if defined(CONFIG_I2C_MULTI_BUS)
  240. /*
  241. * Functions for multiple I2C bus handling
  242. */
  243. unsigned int i2c_get_bus_num(void)
  244. {
  245. return i2c_bus_num;
  246. }
  247. int i2c_set_bus_num(unsigned int bus)
  248. {
  249. #if defined(CONFIG_I2C_MUX)
  250. if (bus < CONFIG_SYS_MAX_I2C_BUS) {
  251. i2c_bus_num = bus;
  252. } else {
  253. int ret;
  254. ret = i2x_mux_select_mux(bus);
  255. if (ret == 0)
  256. i2c_bus_num = bus;
  257. else
  258. return ret;
  259. }
  260. #else
  261. if (bus >= CONFIG_SYS_MAX_I2C_BUS)
  262. return -1;
  263. i2c_bus_num = bus;
  264. #endif
  265. return 0;
  266. }
  267. #endif
  268. /*-----------------------------------------------------------------------
  269. * if ack == I2C_ACK, ACK the byte so can continue reading, else
  270. * send I2C_NOACK to end the read.
  271. */
  272. static uchar read_byte(int ack)
  273. {
  274. I2C_SOFT_DECLARATIONS /* intentional without ';' */
  275. int data;
  276. int j;
  277. /*
  278. * Read 8 bits, MSB first.
  279. */
  280. I2C_TRISTATE;
  281. I2C_SDA(1);
  282. data = 0;
  283. for(j = 0; j < 8; j++) {
  284. I2C_SCL(0);
  285. I2C_DELAY;
  286. I2C_SCL(1);
  287. I2C_DELAY;
  288. data <<= 1;
  289. data |= I2C_READ;
  290. I2C_DELAY;
  291. }
  292. send_ack(ack);
  293. return(data);
  294. }
  295. /*=====================================================================*/
  296. /* Public Functions */
  297. /*=====================================================================*/
  298. /*-----------------------------------------------------------------------
  299. * Initialization
  300. */
  301. void i2c_init (int speed, int slaveaddr)
  302. {
  303. #if defined(CONFIG_SYS_I2C_INIT_BOARD)
  304. /* call board specific i2c bus reset routine before accessing the */
  305. /* environment, which might be in a chip on that bus. For details */
  306. /* about this problem see doc/I2C_Edge_Conditions. */
  307. i2c_init_board();
  308. #else
  309. /*
  310. * WARNING: Do NOT save speed in a static variable: if the
  311. * I2C routines are called before RAM is initialized (to read
  312. * the DIMM SPD, for instance), RAM won't be usable and your
  313. * system will crash.
  314. */
  315. send_reset ();
  316. #endif
  317. }
  318. /*-----------------------------------------------------------------------
  319. * Probe to see if a chip is present. Also good for checking for the
  320. * completion of EEPROM writes since the chip stops responding until
  321. * the write completes (typically 10mSec).
  322. */
  323. int i2c_probe(uchar addr)
  324. {
  325. int rc;
  326. /*
  327. * perform 1 byte write transaction with just address byte
  328. * (fake write)
  329. */
  330. send_start();
  331. rc = write_byte ((addr << 1) | 0);
  332. send_stop();
  333. return (rc ? 1 : 0);
  334. }
  335. /*-----------------------------------------------------------------------
  336. * Read bytes
  337. */
  338. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  339. {
  340. int shift;
  341. PRINTD("i2c_read: chip %02X addr %02X alen %d buffer %p len %d\n",
  342. chip, addr, alen, buffer, len);
  343. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  344. /*
  345. * EEPROM chips that implement "address overflow" are ones
  346. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  347. * address and the extra bits end up in the "chip address"
  348. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  349. * four 256 byte chips.
  350. *
  351. * Note that we consider the length of the address field to
  352. * still be one byte because the extra address bits are
  353. * hidden in the chip address.
  354. */
  355. chip |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  356. PRINTD("i2c_read: fix addr_overflow: chip %02X addr %02X\n",
  357. chip, addr);
  358. #endif
  359. /*
  360. * Do the addressing portion of a write cycle to set the
  361. * chip's address pointer. If the address length is zero,
  362. * don't do the normal write cycle to set the address pointer,
  363. * there is no address pointer in this chip.
  364. */
  365. send_start();
  366. if(alen > 0) {
  367. if(write_byte(chip << 1)) { /* write cycle */
  368. send_stop();
  369. PRINTD("i2c_read, no chip responded %02X\n", chip);
  370. return(1);
  371. }
  372. shift = (alen-1) * 8;
  373. while(alen-- > 0) {
  374. if(write_byte(addr >> shift)) {
  375. PRINTD("i2c_read, address not <ACK>ed\n");
  376. return(1);
  377. }
  378. shift -= 8;
  379. }
  380. /* Some I2C chips need a stop/start sequence here,
  381. * other chips don't work with a full stop and need
  382. * only a start. Default behaviour is to send the
  383. * stop/start sequence.
  384. */
  385. #ifdef CONFIG_SOFT_I2C_READ_REPEATED_START
  386. send_start();
  387. #else
  388. send_stop();
  389. send_start();
  390. #endif
  391. }
  392. /*
  393. * Send the chip address again, this time for a read cycle.
  394. * Then read the data. On the last byte, we do a NACK instead
  395. * of an ACK(len == 0) to terminate the read.
  396. */
  397. write_byte((chip << 1) | 1); /* read cycle */
  398. while(len-- > 0) {
  399. *buffer++ = read_byte(len == 0);
  400. }
  401. send_stop();
  402. return(0);
  403. }
  404. /*-----------------------------------------------------------------------
  405. * Write bytes
  406. */
  407. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  408. {
  409. int shift, failures = 0;
  410. PRINTD("i2c_write: chip %02X addr %02X alen %d buffer %p len %d\n",
  411. chip, addr, alen, buffer, len);
  412. send_start();
  413. if(write_byte(chip << 1)) { /* write cycle */
  414. send_stop();
  415. PRINTD("i2c_write, no chip responded %02X\n", chip);
  416. return(1);
  417. }
  418. shift = (alen-1) * 8;
  419. while(alen-- > 0) {
  420. if(write_byte(addr >> shift)) {
  421. PRINTD("i2c_write, address not <ACK>ed\n");
  422. return(1);
  423. }
  424. shift -= 8;
  425. }
  426. while(len-- > 0) {
  427. if(write_byte(*buffer++)) {
  428. failures++;
  429. }
  430. }
  431. send_stop();
  432. return(failures);
  433. }