soft_i2c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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_AVR32)
  24. #include <asm/arch/portmux.h>
  25. #endif
  26. #if defined(CONFIG_AT91FAMILY)
  27. #include <asm/io.h>
  28. #include <asm/arch/hardware.h>
  29. #include <asm/arch/at91_pio.h>
  30. #ifdef CONFIG_ATMEL_LEGACY
  31. #include <asm/arch/gpio.h>
  32. #endif
  33. #endif
  34. #if defined(CONFIG_MPC852T) || defined(CONFIG_MPC866)
  35. #include <asm/io.h>
  36. #endif
  37. #include <i2c.h>
  38. #if defined(CONFIG_SOFT_I2C_GPIO_SCL)
  39. # include <asm/gpio.h>
  40. # ifndef I2C_GPIO_SYNC
  41. # define I2C_GPIO_SYNC
  42. # endif
  43. # ifndef I2C_INIT
  44. # define I2C_INIT \
  45. do { \
  46. gpio_request(CONFIG_SOFT_I2C_GPIO_SCL, "soft_i2c"); \
  47. gpio_request(CONFIG_SOFT_I2C_GPIO_SDA, "soft_i2c"); \
  48. } while (0)
  49. # endif
  50. # ifndef I2C_ACTIVE
  51. # define I2C_ACTIVE do { } while (0)
  52. # endif
  53. # ifndef I2C_TRISTATE
  54. # define I2C_TRISTATE do { } while (0)
  55. # endif
  56. # ifndef I2C_READ
  57. # define I2C_READ gpio_get_value(CONFIG_SOFT_I2C_GPIO_SDA)
  58. # endif
  59. # ifndef I2C_SDA
  60. # define I2C_SDA(bit) \
  61. do { \
  62. if (bit) \
  63. gpio_direction_input(CONFIG_SOFT_I2C_GPIO_SDA); \
  64. else \
  65. gpio_direction_output(CONFIG_SOFT_I2C_GPIO_SDA, 0); \
  66. I2C_GPIO_SYNC; \
  67. } while (0)
  68. # endif
  69. # ifndef I2C_SCL
  70. # define I2C_SCL(bit) \
  71. do { \
  72. gpio_direction_output(CONFIG_SOFT_I2C_GPIO_SCL, bit); \
  73. I2C_GPIO_SYNC; \
  74. } while (0)
  75. # endif
  76. # ifndef I2C_DELAY
  77. # define I2C_DELAY udelay(5) /* 1/4 I2C clock duration */
  78. # endif
  79. #endif
  80. /* #define DEBUG_I2C */
  81. DECLARE_GLOBAL_DATA_PTR;
  82. #ifndef I2C_SOFT_DECLARATIONS
  83. # if defined(CONFIG_MPC8260)
  84. # define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = \
  85. ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT);
  86. # else
  87. # define I2C_SOFT_DECLARATIONS
  88. # endif
  89. #endif
  90. #if !defined(CONFIG_SYS_I2C_SOFT_SPEED)
  91. #define CONFIG_SYS_I2C_SOFT_SPEED CONFIG_SYS_I2C_SPEED
  92. #endif
  93. #if !defined(CONFIG_SYS_I2C_SOFT_SLAVE)
  94. #define CONFIG_SYS_I2C_SOFT_SLAVE CONFIG_SYS_I2C_SLAVE
  95. #endif
  96. /*-----------------------------------------------------------------------
  97. * Definitions
  98. */
  99. #define RETRIES 0
  100. #define I2C_ACK 0 /* PD_SDA level to ack a byte */
  101. #define I2C_NOACK 1 /* PD_SDA level to noack a byte */
  102. #ifdef DEBUG_I2C
  103. #define PRINTD(fmt,args...) do { \
  104. printf (fmt ,##args); \
  105. } while (0)
  106. #else
  107. #define PRINTD(fmt,args...)
  108. #endif
  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. /*-----------------------------------------------------------------------
  234. * if ack == I2C_ACK, ACK the byte so can continue reading, else
  235. * send I2C_NOACK to end the read.
  236. */
  237. static uchar read_byte(int ack)
  238. {
  239. I2C_SOFT_DECLARATIONS /* intentional without ';' */
  240. int data;
  241. int j;
  242. /*
  243. * Read 8 bits, MSB first.
  244. */
  245. I2C_TRISTATE;
  246. I2C_SDA(1);
  247. data = 0;
  248. for(j = 0; j < 8; j++) {
  249. I2C_SCL(0);
  250. I2C_DELAY;
  251. I2C_SCL(1);
  252. I2C_DELAY;
  253. data <<= 1;
  254. data |= I2C_READ;
  255. I2C_DELAY;
  256. }
  257. send_ack(ack);
  258. return(data);
  259. }
  260. /*-----------------------------------------------------------------------
  261. * Initialization
  262. */
  263. static void soft_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
  264. {
  265. #if defined(CONFIG_SYS_I2C_INIT_BOARD)
  266. /* call board specific i2c bus reset routine before accessing the */
  267. /* environment, which might be in a chip on that bus. For details */
  268. /* about this problem see doc/I2C_Edge_Conditions. */
  269. i2c_init_board();
  270. #else
  271. /*
  272. * WARNING: Do NOT save speed in a static variable: if the
  273. * I2C routines are called before RAM is initialized (to read
  274. * the DIMM SPD, for instance), RAM won't be usable and your
  275. * system will crash.
  276. */
  277. send_reset ();
  278. #endif
  279. }
  280. /*-----------------------------------------------------------------------
  281. * Probe to see if a chip is present. Also good for checking for the
  282. * completion of EEPROM writes since the chip stops responding until
  283. * the write completes (typically 10mSec).
  284. */
  285. static int soft_i2c_probe(struct i2c_adapter *adap, uint8_t addr)
  286. {
  287. int rc;
  288. /*
  289. * perform 1 byte write transaction with just address byte
  290. * (fake write)
  291. */
  292. send_start();
  293. rc = write_byte ((addr << 1) | 0);
  294. send_stop();
  295. return (rc ? 1 : 0);
  296. }
  297. /*-----------------------------------------------------------------------
  298. * Read bytes
  299. */
  300. static int soft_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
  301. int alen, uchar *buffer, int len)
  302. {
  303. int shift;
  304. PRINTD("i2c_read: chip %02X addr %02X alen %d buffer %p len %d\n",
  305. chip, addr, alen, buffer, len);
  306. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  307. /*
  308. * EEPROM chips that implement "address overflow" are ones
  309. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  310. * address and the extra bits end up in the "chip address"
  311. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  312. * four 256 byte chips.
  313. *
  314. * Note that we consider the length of the address field to
  315. * still be one byte because the extra address bits are
  316. * hidden in the chip address.
  317. */
  318. chip |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  319. PRINTD("i2c_read: fix addr_overflow: chip %02X addr %02X\n",
  320. chip, addr);
  321. #endif
  322. /*
  323. * Do the addressing portion of a write cycle to set the
  324. * chip's address pointer. If the address length is zero,
  325. * don't do the normal write cycle to set the address pointer,
  326. * there is no address pointer in this chip.
  327. */
  328. send_start();
  329. if(alen > 0) {
  330. if(write_byte(chip << 1)) { /* write cycle */
  331. send_stop();
  332. PRINTD("i2c_read, no chip responded %02X\n", chip);
  333. return(1);
  334. }
  335. shift = (alen-1) * 8;
  336. while(alen-- > 0) {
  337. if(write_byte(addr >> shift)) {
  338. PRINTD("i2c_read, address not <ACK>ed\n");
  339. return(1);
  340. }
  341. shift -= 8;
  342. }
  343. /* Some I2C chips need a stop/start sequence here,
  344. * other chips don't work with a full stop and need
  345. * only a start. Default behaviour is to send the
  346. * stop/start sequence.
  347. */
  348. #ifdef CONFIG_SOFT_I2C_READ_REPEATED_START
  349. send_start();
  350. #else
  351. send_stop();
  352. send_start();
  353. #endif
  354. }
  355. /*
  356. * Send the chip address again, this time for a read cycle.
  357. * Then read the data. On the last byte, we do a NACK instead
  358. * of an ACK(len == 0) to terminate the read.
  359. */
  360. write_byte((chip << 1) | 1); /* read cycle */
  361. while(len-- > 0) {
  362. *buffer++ = read_byte(len == 0);
  363. }
  364. send_stop();
  365. return(0);
  366. }
  367. /*-----------------------------------------------------------------------
  368. * Write bytes
  369. */
  370. static int soft_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
  371. int alen, uchar *buffer, int len)
  372. {
  373. int shift, failures = 0;
  374. PRINTD("i2c_write: chip %02X addr %02X alen %d buffer %p len %d\n",
  375. chip, addr, alen, buffer, len);
  376. send_start();
  377. if(write_byte(chip << 1)) { /* write cycle */
  378. send_stop();
  379. PRINTD("i2c_write, no chip responded %02X\n", chip);
  380. return(1);
  381. }
  382. shift = (alen-1) * 8;
  383. while(alen-- > 0) {
  384. if(write_byte(addr >> shift)) {
  385. PRINTD("i2c_write, address not <ACK>ed\n");
  386. return(1);
  387. }
  388. shift -= 8;
  389. }
  390. while(len-- > 0) {
  391. if(write_byte(*buffer++)) {
  392. failures++;
  393. }
  394. }
  395. send_stop();
  396. return(failures);
  397. }
  398. /*
  399. * Register soft i2c adapters
  400. */
  401. U_BOOT_I2C_ADAP_COMPLETE(soft00, soft_i2c_init, soft_i2c_probe,
  402. soft_i2c_read, soft_i2c_write, NULL,
  403. CONFIG_SYS_I2C_SOFT_SPEED, CONFIG_SYS_I2C_SOFT_SLAVE,
  404. 0)
  405. #if defined(I2C_SOFT_DECLARATIONS2)
  406. U_BOOT_I2C_ADAP_COMPLETE(soft01, soft_i2c_init, soft_i2c_probe,
  407. soft_i2c_read, soft_i2c_write, NULL,
  408. CONFIG_SYS_I2C_SOFT_SPEED_2,
  409. CONFIG_SYS_I2C_SOFT_SLAVE_2,
  410. 1)
  411. #endif
  412. #if defined(I2C_SOFT_DECLARATIONS3)
  413. U_BOOT_I2C_ADAP_COMPLETE(soft02, soft_i2c_init, soft_i2c_probe,
  414. soft_i2c_read, soft_i2c_write, NULL,
  415. CONFIG_SYS_I2C_SOFT_SPEED_3,
  416. CONFIG_SYS_I2C_SOFT_SLAVE_3,
  417. 2)
  418. #endif
  419. #if defined(I2C_SOFT_DECLARATIONS4)
  420. U_BOOT_I2C_ADAP_COMPLETE(soft03, soft_i2c_init, soft_i2c_probe,
  421. soft_i2c_read, soft_i2c_write, NULL,
  422. CONFIG_SYS_I2C_SOFT_SPEED_4,
  423. CONFIG_SYS_I2C_SOFT_SLAVE_4,
  424. 3)
  425. #endif
  426. #if defined(I2C_SOFT_DECLARATIONS5)
  427. U_BOOT_I2C_ADAP_COMPLETE(soft04, soft_i2c_init, soft_i2c_probe,
  428. soft_i2c_read, soft_i2c_write, NULL,
  429. CONFIG_SYS_I2C_SOFT_SPEED_5,
  430. CONFIG_SYS_I2C_SOFT_SLAVE_5,
  431. 4)
  432. #endif
  433. #if defined(I2C_SOFT_DECLARATIONS6)
  434. U_BOOT_I2C_ADAP_COMPLETE(soft05, soft_i2c_init, soft_i2c_probe,
  435. soft_i2c_read, soft_i2c_write, NULL,
  436. CONFIG_SYS_I2C_SOFT_SPEED_6,
  437. CONFIG_SYS_I2C_SOFT_SLAVE_6,
  438. 5)
  439. #endif
  440. #if defined(I2C_SOFT_DECLARATIONS7)
  441. U_BOOT_I2C_ADAP_COMPLETE(soft06, soft_i2c_init, soft_i2c_probe,
  442. soft_i2c_read, soft_i2c_write, NULL,
  443. CONFIG_SYS_I2C_SOFT_SPEED_7,
  444. CONFIG_SYS_I2C_SOFT_SLAVE_7,
  445. 6)
  446. #endif
  447. #if defined(I2C_SOFT_DECLARATIONS8)
  448. U_BOOT_I2C_ADAP_COMPLETE(soft07, soft_i2c_init, soft_i2c_probe,
  449. soft_i2c_read, soft_i2c_write, NULL,
  450. CONFIG_SYS_I2C_SOFT_SPEED_8,
  451. CONFIG_SYS_I2C_SOFT_SLAVE_8,
  452. 7)
  453. #endif
  454. #if defined(I2C_SOFT_DECLARATIONS9)
  455. U_BOOT_I2C_ADAP_COMPLETE(soft08, soft_i2c_init, soft_i2c_probe,
  456. soft_i2c_read, soft_i2c_write, NULL,
  457. CONFIG_SYS_I2C_SOFT_SPEED_9,
  458. CONFIG_SYS_I2C_SOFT_SLAVE_9,
  459. 8)
  460. #endif
  461. #if defined(I2C_SOFT_DECLARATIONS10)
  462. U_BOOT_I2C_ADAP_COMPLETE(soft09, soft_i2c_init, soft_i2c_probe,
  463. soft_i2c_read, soft_i2c_write, NULL,
  464. CONFIG_SYS_I2C_SOFT_SPEED_10,
  465. CONFIG_SYS_I2C_SOFT_SLAVE_10,
  466. 9)
  467. #endif
  468. #if defined(I2C_SOFT_DECLARATIONS11)
  469. U_BOOT_I2C_ADAP_COMPLETE(soft10, soft_i2c_init, soft_i2c_probe,
  470. soft_i2c_read, soft_i2c_write, NULL,
  471. CONFIG_SYS_I2C_SOFT_SPEED_11,
  472. CONFIG_SYS_I2C_SOFT_SLAVE_11,
  473. 10)
  474. #endif
  475. #if defined(I2C_SOFT_DECLARATIONS12)
  476. U_BOOT_I2C_ADAP_COMPLETE(soft11, soft_i2c_init, soft_i2c_probe,
  477. soft_i2c_read, soft_i2c_write, NULL,
  478. CONFIG_SYS_I2C_SOFT_SPEED_12,
  479. CONFIG_SYS_I2C_SOFT_SLAVE_12,
  480. 11)
  481. #endif