soft_i2c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * (C) Copyright 2009
  3. * Heiko Schocher, DENX Software Engineering, hs@denx.de.
  4. * Changes for multibus/multiadapter I2C support.
  5. *
  6. * (C) Copyright 2001, 2002
  7. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. *
  11. * This has been changed substantially by Gerald Van Baren, Custom IDEAS,
  12. * vanbaren@cideas.com. It was heavily influenced by LiMon, written by
  13. * Neil Russell.
  14. *
  15. * NOTE: This driver should be converted to driver model before June 2017.
  16. * Please see doc/driver-model/i2c-howto.txt for instructions.
  17. */
  18. #include <common.h>
  19. #if defined(CONFIG_AT91FAMILY)
  20. #include <asm/io.h>
  21. #include <asm/arch/hardware.h>
  22. #include <asm/arch/at91_pio.h>
  23. #ifdef CONFIG_ATMEL_LEGACY
  24. #include <asm/arch/gpio.h>
  25. #endif
  26. #endif
  27. #if defined(CONFIG_8xx)
  28. #include <asm/io.h>
  29. #endif
  30. #include <i2c.h>
  31. #if defined(CONFIG_SOFT_I2C_GPIO_SCL)
  32. # include <asm/gpio.h>
  33. # ifndef I2C_GPIO_SYNC
  34. # define I2C_GPIO_SYNC
  35. # endif
  36. # ifndef I2C_INIT
  37. # define I2C_INIT \
  38. do { \
  39. gpio_request(CONFIG_SOFT_I2C_GPIO_SCL, "soft_i2c"); \
  40. gpio_request(CONFIG_SOFT_I2C_GPIO_SDA, "soft_i2c"); \
  41. } while (0)
  42. # endif
  43. # ifndef I2C_ACTIVE
  44. # define I2C_ACTIVE do { } while (0)
  45. # endif
  46. # ifndef I2C_TRISTATE
  47. # define I2C_TRISTATE do { } while (0)
  48. # endif
  49. # ifndef I2C_READ
  50. # define I2C_READ gpio_get_value(CONFIG_SOFT_I2C_GPIO_SDA)
  51. # endif
  52. # ifndef I2C_SDA
  53. # define I2C_SDA(bit) \
  54. do { \
  55. if (bit) \
  56. gpio_direction_input(CONFIG_SOFT_I2C_GPIO_SDA); \
  57. else \
  58. gpio_direction_output(CONFIG_SOFT_I2C_GPIO_SDA, 0); \
  59. I2C_GPIO_SYNC; \
  60. } while (0)
  61. # endif
  62. # ifndef I2C_SCL
  63. # define I2C_SCL(bit) \
  64. do { \
  65. gpio_direction_output(CONFIG_SOFT_I2C_GPIO_SCL, bit); \
  66. I2C_GPIO_SYNC; \
  67. } while (0)
  68. # endif
  69. # ifndef I2C_DELAY
  70. # define I2C_DELAY udelay(5) /* 1/4 I2C clock duration */
  71. # endif
  72. #endif
  73. /* #define DEBUG_I2C */
  74. DECLARE_GLOBAL_DATA_PTR;
  75. #ifndef I2C_SOFT_DECLARATIONS
  76. # define I2C_SOFT_DECLARATIONS
  77. #endif
  78. #if !defined(CONFIG_SYS_I2C_SOFT_SPEED)
  79. #define CONFIG_SYS_I2C_SOFT_SPEED CONFIG_SYS_I2C_SPEED
  80. #endif
  81. #if !defined(CONFIG_SYS_I2C_SOFT_SLAVE)
  82. #define CONFIG_SYS_I2C_SOFT_SLAVE CONFIG_SYS_I2C_SLAVE
  83. #endif
  84. /*-----------------------------------------------------------------------
  85. * Definitions
  86. */
  87. #define RETRIES 0
  88. #define I2C_ACK 0 /* PD_SDA level to ack a byte */
  89. #define I2C_NOACK 1 /* PD_SDA level to noack a byte */
  90. #ifdef DEBUG_I2C
  91. #define PRINTD(fmt,args...) do { \
  92. printf (fmt ,##args); \
  93. } while (0)
  94. #else
  95. #define PRINTD(fmt,args...)
  96. #endif
  97. /*-----------------------------------------------------------------------
  98. * Local functions
  99. */
  100. #if !defined(CONFIG_SYS_I2C_INIT_BOARD)
  101. static void send_reset (void);
  102. #endif
  103. static void send_start (void);
  104. static void send_stop (void);
  105. static void send_ack (int);
  106. static int write_byte (uchar byte);
  107. static uchar read_byte (int);
  108. #if !defined(CONFIG_SYS_I2C_INIT_BOARD)
  109. /*-----------------------------------------------------------------------
  110. * Send a reset sequence consisting of 9 clocks with the data signal high
  111. * to clock any confused device back into an idle state. Also send a
  112. * <stop> at the end of the sequence for belts & suspenders.
  113. */
  114. static void send_reset(void)
  115. {
  116. I2C_SOFT_DECLARATIONS /* intentional without ';' */
  117. int j;
  118. I2C_SCL(1);
  119. I2C_SDA(1);
  120. #ifdef I2C_INIT
  121. I2C_INIT;
  122. #endif
  123. I2C_TRISTATE;
  124. for(j = 0; j < 9; j++) {
  125. I2C_SCL(0);
  126. I2C_DELAY;
  127. I2C_DELAY;
  128. I2C_SCL(1);
  129. I2C_DELAY;
  130. I2C_DELAY;
  131. }
  132. send_stop();
  133. I2C_TRISTATE;
  134. }
  135. #endif
  136. /*-----------------------------------------------------------------------
  137. * START: High -> Low on SDA while SCL is High
  138. */
  139. static void send_start(void)
  140. {
  141. I2C_SOFT_DECLARATIONS /* intentional without ';' */
  142. I2C_DELAY;
  143. I2C_SDA(1);
  144. I2C_ACTIVE;
  145. I2C_DELAY;
  146. I2C_SCL(1);
  147. I2C_DELAY;
  148. I2C_SDA(0);
  149. I2C_DELAY;
  150. }
  151. /*-----------------------------------------------------------------------
  152. * STOP: Low -> High on SDA while SCL is High
  153. */
  154. static void send_stop(void)
  155. {
  156. I2C_SOFT_DECLARATIONS /* intentional without ';' */
  157. I2C_SCL(0);
  158. I2C_DELAY;
  159. I2C_SDA(0);
  160. I2C_ACTIVE;
  161. I2C_DELAY;
  162. I2C_SCL(1);
  163. I2C_DELAY;
  164. I2C_SDA(1);
  165. I2C_DELAY;
  166. I2C_TRISTATE;
  167. }
  168. /*-----------------------------------------------------------------------
  169. * ack should be I2C_ACK or I2C_NOACK
  170. */
  171. static void send_ack(int ack)
  172. {
  173. I2C_SOFT_DECLARATIONS /* intentional without ';' */
  174. I2C_SCL(0);
  175. I2C_DELAY;
  176. I2C_ACTIVE;
  177. I2C_SDA(ack);
  178. I2C_DELAY;
  179. I2C_SCL(1);
  180. I2C_DELAY;
  181. I2C_DELAY;
  182. I2C_SCL(0);
  183. I2C_DELAY;
  184. }
  185. /*-----------------------------------------------------------------------
  186. * Send 8 bits and look for an acknowledgement.
  187. */
  188. static int write_byte(uchar data)
  189. {
  190. I2C_SOFT_DECLARATIONS /* intentional without ';' */
  191. int j;
  192. int nack;
  193. I2C_ACTIVE;
  194. for(j = 0; j < 8; j++) {
  195. I2C_SCL(0);
  196. I2C_DELAY;
  197. I2C_SDA(data & 0x80);
  198. I2C_DELAY;
  199. I2C_SCL(1);
  200. I2C_DELAY;
  201. I2C_DELAY;
  202. data <<= 1;
  203. }
  204. /*
  205. * Look for an <ACK>(negative logic) and return it.
  206. */
  207. I2C_SCL(0);
  208. I2C_DELAY;
  209. I2C_SDA(1);
  210. I2C_TRISTATE;
  211. I2C_DELAY;
  212. I2C_SCL(1);
  213. I2C_DELAY;
  214. I2C_DELAY;
  215. nack = I2C_READ;
  216. I2C_SCL(0);
  217. I2C_DELAY;
  218. I2C_ACTIVE;
  219. return(nack); /* not a nack is an ack */
  220. }
  221. /*-----------------------------------------------------------------------
  222. * if ack == I2C_ACK, ACK the byte so can continue reading, else
  223. * send I2C_NOACK to end the read.
  224. */
  225. static uchar read_byte(int ack)
  226. {
  227. I2C_SOFT_DECLARATIONS /* intentional without ';' */
  228. int data;
  229. int j;
  230. /*
  231. * Read 8 bits, MSB first.
  232. */
  233. I2C_TRISTATE;
  234. I2C_SDA(1);
  235. data = 0;
  236. for(j = 0; j < 8; j++) {
  237. I2C_SCL(0);
  238. I2C_DELAY;
  239. I2C_SCL(1);
  240. I2C_DELAY;
  241. data <<= 1;
  242. data |= I2C_READ;
  243. I2C_DELAY;
  244. }
  245. send_ack(ack);
  246. return(data);
  247. }
  248. /*-----------------------------------------------------------------------
  249. * Initialization
  250. */
  251. static void soft_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
  252. {
  253. #if defined(CONFIG_SYS_I2C_INIT_BOARD)
  254. /* call board specific i2c bus reset routine before accessing the */
  255. /* environment, which might be in a chip on that bus. For details */
  256. /* about this problem see doc/I2C_Edge_Conditions. */
  257. i2c_init_board();
  258. #else
  259. /*
  260. * WARNING: Do NOT save speed in a static variable: if the
  261. * I2C routines are called before RAM is initialized (to read
  262. * the DIMM SPD, for instance), RAM won't be usable and your
  263. * system will crash.
  264. */
  265. send_reset ();
  266. #endif
  267. }
  268. /*-----------------------------------------------------------------------
  269. * Probe to see if a chip is present. Also good for checking for the
  270. * completion of EEPROM writes since the chip stops responding until
  271. * the write completes (typically 10mSec).
  272. */
  273. static int soft_i2c_probe(struct i2c_adapter *adap, uint8_t addr)
  274. {
  275. int rc;
  276. /*
  277. * perform 1 byte write transaction with just address byte
  278. * (fake write)
  279. */
  280. send_start();
  281. rc = write_byte ((addr << 1) | 0);
  282. send_stop();
  283. return (rc ? 1 : 0);
  284. }
  285. /*-----------------------------------------------------------------------
  286. * Read bytes
  287. */
  288. static int soft_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
  289. int alen, uchar *buffer, int len)
  290. {
  291. int shift;
  292. PRINTD("i2c_read: chip %02X addr %02X alen %d buffer %p len %d\n",
  293. chip, addr, alen, buffer, len);
  294. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  295. /*
  296. * EEPROM chips that implement "address overflow" are ones
  297. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  298. * address and the extra bits end up in the "chip address"
  299. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  300. * four 256 byte chips.
  301. *
  302. * Note that we consider the length of the address field to
  303. * still be one byte because the extra address bits are
  304. * hidden in the chip address.
  305. */
  306. chip |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  307. PRINTD("i2c_read: fix addr_overflow: chip %02X addr %02X\n",
  308. chip, addr);
  309. #endif
  310. /*
  311. * Do the addressing portion of a write cycle to set the
  312. * chip's address pointer. If the address length is zero,
  313. * don't do the normal write cycle to set the address pointer,
  314. * there is no address pointer in this chip.
  315. */
  316. send_start();
  317. if(alen > 0) {
  318. if(write_byte(chip << 1)) { /* write cycle */
  319. send_stop();
  320. PRINTD("i2c_read, no chip responded %02X\n", chip);
  321. return(1);
  322. }
  323. shift = (alen-1) * 8;
  324. while(alen-- > 0) {
  325. if(write_byte(addr >> shift)) {
  326. PRINTD("i2c_read, address not <ACK>ed\n");
  327. return(1);
  328. }
  329. shift -= 8;
  330. }
  331. /* Some I2C chips need a stop/start sequence here,
  332. * other chips don't work with a full stop and need
  333. * only a start. Default behaviour is to send the
  334. * stop/start sequence.
  335. */
  336. #ifdef CONFIG_SOFT_I2C_READ_REPEATED_START
  337. send_start();
  338. #else
  339. send_stop();
  340. send_start();
  341. #endif
  342. }
  343. /*
  344. * Send the chip address again, this time for a read cycle.
  345. * Then read the data. On the last byte, we do a NACK instead
  346. * of an ACK(len == 0) to terminate the read.
  347. */
  348. write_byte((chip << 1) | 1); /* read cycle */
  349. while(len-- > 0) {
  350. *buffer++ = read_byte(len == 0);
  351. }
  352. send_stop();
  353. return(0);
  354. }
  355. /*-----------------------------------------------------------------------
  356. * Write bytes
  357. */
  358. static int soft_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
  359. int alen, uchar *buffer, int len)
  360. {
  361. int shift, failures = 0;
  362. PRINTD("i2c_write: chip %02X addr %02X alen %d buffer %p len %d\n",
  363. chip, addr, alen, buffer, len);
  364. send_start();
  365. if(write_byte(chip << 1)) { /* write cycle */
  366. send_stop();
  367. PRINTD("i2c_write, no chip responded %02X\n", chip);
  368. return(1);
  369. }
  370. shift = (alen-1) * 8;
  371. while(alen-- > 0) {
  372. if(write_byte(addr >> shift)) {
  373. PRINTD("i2c_write, address not <ACK>ed\n");
  374. return(1);
  375. }
  376. shift -= 8;
  377. }
  378. while(len-- > 0) {
  379. if(write_byte(*buffer++)) {
  380. failures++;
  381. }
  382. }
  383. send_stop();
  384. return(failures);
  385. }
  386. /*
  387. * Register soft i2c adapters
  388. */
  389. U_BOOT_I2C_ADAP_COMPLETE(soft00, soft_i2c_init, soft_i2c_probe,
  390. soft_i2c_read, soft_i2c_write, NULL,
  391. CONFIG_SYS_I2C_SOFT_SPEED, CONFIG_SYS_I2C_SOFT_SLAVE,
  392. 0)
  393. #if defined(I2C_SOFT_DECLARATIONS2)
  394. U_BOOT_I2C_ADAP_COMPLETE(soft01, soft_i2c_init, soft_i2c_probe,
  395. soft_i2c_read, soft_i2c_write, NULL,
  396. CONFIG_SYS_I2C_SOFT_SPEED_2,
  397. CONFIG_SYS_I2C_SOFT_SLAVE_2,
  398. 1)
  399. #endif
  400. #if defined(I2C_SOFT_DECLARATIONS3)
  401. U_BOOT_I2C_ADAP_COMPLETE(soft02, soft_i2c_init, soft_i2c_probe,
  402. soft_i2c_read, soft_i2c_write, NULL,
  403. CONFIG_SYS_I2C_SOFT_SPEED_3,
  404. CONFIG_SYS_I2C_SOFT_SLAVE_3,
  405. 2)
  406. #endif
  407. #if defined(I2C_SOFT_DECLARATIONS4)
  408. U_BOOT_I2C_ADAP_COMPLETE(soft03, soft_i2c_init, soft_i2c_probe,
  409. soft_i2c_read, soft_i2c_write, NULL,
  410. CONFIG_SYS_I2C_SOFT_SPEED_4,
  411. CONFIG_SYS_I2C_SOFT_SLAVE_4,
  412. 3)
  413. #endif
  414. #if defined(I2C_SOFT_DECLARATIONS5)
  415. U_BOOT_I2C_ADAP_COMPLETE(soft04, soft_i2c_init, soft_i2c_probe,
  416. soft_i2c_read, soft_i2c_write, NULL,
  417. CONFIG_SYS_I2C_SOFT_SPEED_5,
  418. CONFIG_SYS_I2C_SOFT_SLAVE_5,
  419. 4)
  420. #endif
  421. #if defined(I2C_SOFT_DECLARATIONS6)
  422. U_BOOT_I2C_ADAP_COMPLETE(soft05, soft_i2c_init, soft_i2c_probe,
  423. soft_i2c_read, soft_i2c_write, NULL,
  424. CONFIG_SYS_I2C_SOFT_SPEED_6,
  425. CONFIG_SYS_I2C_SOFT_SLAVE_6,
  426. 5)
  427. #endif
  428. #if defined(I2C_SOFT_DECLARATIONS7)
  429. U_BOOT_I2C_ADAP_COMPLETE(soft06, soft_i2c_init, soft_i2c_probe,
  430. soft_i2c_read, soft_i2c_write, NULL,
  431. CONFIG_SYS_I2C_SOFT_SPEED_7,
  432. CONFIG_SYS_I2C_SOFT_SLAVE_7,
  433. 6)
  434. #endif
  435. #if defined(I2C_SOFT_DECLARATIONS8)
  436. U_BOOT_I2C_ADAP_COMPLETE(soft07, soft_i2c_init, soft_i2c_probe,
  437. soft_i2c_read, soft_i2c_write, NULL,
  438. CONFIG_SYS_I2C_SOFT_SPEED_8,
  439. CONFIG_SYS_I2C_SOFT_SLAVE_8,
  440. 7)
  441. #endif
  442. #if defined(I2C_SOFT_DECLARATIONS9)
  443. U_BOOT_I2C_ADAP_COMPLETE(soft08, soft_i2c_init, soft_i2c_probe,
  444. soft_i2c_read, soft_i2c_write, NULL,
  445. CONFIG_SYS_I2C_SOFT_SPEED_9,
  446. CONFIG_SYS_I2C_SOFT_SLAVE_9,
  447. 8)
  448. #endif
  449. #if defined(I2C_SOFT_DECLARATIONS10)
  450. U_BOOT_I2C_ADAP_COMPLETE(soft09, soft_i2c_init, soft_i2c_probe,
  451. soft_i2c_read, soft_i2c_write, NULL,
  452. CONFIG_SYS_I2C_SOFT_SPEED_10,
  453. CONFIG_SYS_I2C_SOFT_SLAVE_10,
  454. 9)
  455. #endif
  456. #if defined(I2C_SOFT_DECLARATIONS11)
  457. U_BOOT_I2C_ADAP_COMPLETE(soft10, soft_i2c_init, soft_i2c_probe,
  458. soft_i2c_read, soft_i2c_write, NULL,
  459. CONFIG_SYS_I2C_SOFT_SPEED_11,
  460. CONFIG_SYS_I2C_SOFT_SLAVE_11,
  461. 10)
  462. #endif
  463. #if defined(I2C_SOFT_DECLARATIONS12)
  464. U_BOOT_I2C_ADAP_COMPLETE(soft11, soft_i2c_init, soft_i2c_probe,
  465. soft_i2c_read, soft_i2c_write, NULL,
  466. CONFIG_SYS_I2C_SOFT_SPEED_12,
  467. CONFIG_SYS_I2C_SOFT_SLAVE_12,
  468. 11)
  469. #endif