soft_i2c.c 12 KB

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