soft_i2c.c 12 KB

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