soft_i2c.c 12 KB

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