soft_i2c.c 13 KB

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