soft_i2c.c 11 KB

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