PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/i2c/soft_i2c.c

https://github.com/lilstevie/uboot-tegra
C | 484 lines | 311 code | 53 blank | 120 comment | 25 complexity | 653f999b5781be204c84f888f24337fb MD5 | raw file
  1. /*
  2. * (C) Copyright 2001, 2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. * This has been changed substantially by Gerald Van Baren, Custom IDEAS,
  24. * vanbaren@cideas.com. It was heavily influenced by LiMon, written by
  25. * Neil Russell.
  26. */
  27. #include <common.h>
  28. #ifdef CONFIG_MPC8260 /* only valid for MPC8260 */
  29. #include <ioports.h>
  30. #include <asm/io.h>
  31. #endif
  32. #if defined(CONFIG_AT91FAMILY)
  33. #include <asm/io.h>
  34. #include <asm/arch/hardware.h>
  35. #include <asm/arch/at91_pio.h>
  36. #ifdef CONFIG_AT91_LEGACY
  37. #include <asm/arch/gpio.h>
  38. #endif
  39. #endif
  40. #ifdef CONFIG_IXP425 /* only valid for IXP425 */
  41. #include <asm/arch/ixp425.h>
  42. #endif
  43. #ifdef CONFIG_LPC2292
  44. #include <asm/arch/hardware.h>
  45. #endif
  46. #if defined(CONFIG_MPC852T) || defined(CONFIG_MPC866)
  47. #include <asm/io.h>
  48. #endif
  49. #include <i2c.h>
  50. #if defined(CONFIG_SOFT_I2C_GPIO_SCL)
  51. # include <asm/gpio.h>
  52. # ifndef I2C_GPIO_SYNC
  53. # define I2C_GPIO_SYNC
  54. # endif
  55. # ifndef I2C_INIT
  56. # define I2C_INIT \
  57. do { \
  58. gpio_request(CONFIG_SOFT_I2C_GPIO_SCL, "soft_i2c"); \
  59. gpio_request(CONFIG_SOFT_I2C_GPIO_SDA, "soft_i2c"); \
  60. } while (0)
  61. # endif
  62. # ifndef I2C_ACTIVE
  63. # define I2C_ACTIVE do { } while (0)
  64. # endif
  65. # ifndef I2C_TRISTATE
  66. # define I2C_TRISTATE do { } while (0)
  67. # endif
  68. # ifndef I2C_READ
  69. # define I2C_READ gpio_get_value(CONFIG_SOFT_I2C_GPIO_SDA)
  70. # endif
  71. # ifndef I2C_SDA
  72. # define I2C_SDA(bit) \
  73. do { \
  74. if (bit) \
  75. gpio_direction_input(CONFIG_SOFT_I2C_GPIO_SDA); \
  76. else \
  77. gpio_direction_output(CONFIG_SOFT_I2C_GPIO_SDA, 0); \
  78. I2C_GPIO_SYNC; \
  79. } while (0)
  80. # endif
  81. # ifndef I2C_SCL
  82. # define I2C_SCL(bit) \
  83. do { \
  84. gpio_direction_output(CONFIG_SOFT_I2C_GPIO_SCL, bit); \
  85. I2C_GPIO_SYNC; \
  86. } while (0)
  87. # endif
  88. # ifndef I2C_DELAY
  89. # define I2C_DELAY udelay(5) /* 1/4 I2C clock duration */
  90. # endif
  91. #endif
  92. /* #define DEBUG_I2C */
  93. #ifdef DEBUG_I2C
  94. DECLARE_GLOBAL_DATA_PTR;
  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. if (gd->have_console) \
  105. printf (fmt ,##args); \
  106. } while (0)
  107. #else
  108. #define PRINTD(fmt,args...)
  109. #endif
  110. #if defined(CONFIG_I2C_MULTI_BUS)
  111. static unsigned int i2c_bus_num __attribute__ ((section (".data"))) = 0;
  112. #endif /* CONFIG_I2C_MULTI_BUS */
  113. /*-----------------------------------------------------------------------
  114. * Local functions
  115. */
  116. #if !defined(CONFIG_SYS_I2C_INIT_BOARD)
  117. static void send_reset (void);
  118. #endif
  119. static void send_start (void);
  120. static void send_stop (void);
  121. static void send_ack (int);
  122. static int write_byte (uchar byte);
  123. static uchar read_byte (int);
  124. #if !defined(CONFIG_SYS_I2C_INIT_BOARD)
  125. /*-----------------------------------------------------------------------
  126. * Send a reset sequence consisting of 9 clocks with the data signal high
  127. * to clock any confused device back into an idle state. Also send a
  128. * <stop> at the end of the sequence for belts & suspenders.
  129. */
  130. static void send_reset(void)
  131. {
  132. I2C_SOFT_DECLARATIONS /* intentional without ';' */
  133. int j;
  134. I2C_SCL(1);
  135. I2C_SDA(1);
  136. #ifdef I2C_INIT
  137. I2C_INIT;
  138. #endif
  139. I2C_TRISTATE;
  140. for(j = 0; j < 9; j++) {
  141. I2C_SCL(0);
  142. I2C_DELAY;
  143. I2C_DELAY;
  144. I2C_SCL(1);
  145. I2C_DELAY;
  146. I2C_DELAY;
  147. }
  148. send_stop();
  149. I2C_TRISTATE;
  150. }
  151. #endif
  152. /*-----------------------------------------------------------------------
  153. * START: High -> Low on SDA while SCL is High
  154. */
  155. static void send_start(void)
  156. {
  157. I2C_SOFT_DECLARATIONS /* intentional without ';' */
  158. I2C_DELAY;
  159. I2C_SDA(1);
  160. I2C_ACTIVE;
  161. I2C_DELAY;
  162. I2C_SCL(1);
  163. I2C_DELAY;
  164. I2C_SDA(0);
  165. I2C_DELAY;
  166. }
  167. /*-----------------------------------------------------------------------
  168. * STOP: Low -> High on SDA while SCL is High
  169. */
  170. static void send_stop(void)
  171. {
  172. I2C_SOFT_DECLARATIONS /* intentional without ';' */
  173. I2C_SCL(0);
  174. I2C_DELAY;
  175. I2C_SDA(0);
  176. I2C_ACTIVE;
  177. I2C_DELAY;
  178. I2C_SCL(1);
  179. I2C_DELAY;
  180. I2C_SDA(1);
  181. I2C_DELAY;
  182. I2C_TRISTATE;
  183. }
  184. /*-----------------------------------------------------------------------
  185. * ack should be I2C_ACK or I2C_NOACK
  186. */
  187. static void send_ack(int ack)
  188. {
  189. I2C_SOFT_DECLARATIONS /* intentional without ';' */
  190. I2C_SCL(0);
  191. I2C_DELAY;
  192. I2C_ACTIVE;
  193. I2C_SDA(ack);
  194. I2C_DELAY;
  195. I2C_SCL(1);
  196. I2C_DELAY;
  197. I2C_DELAY;
  198. I2C_SCL(0);
  199. I2C_DELAY;
  200. }
  201. /*-----------------------------------------------------------------------
  202. * Send 8 bits and look for an acknowledgement.
  203. */
  204. static int write_byte(uchar data)
  205. {
  206. I2C_SOFT_DECLARATIONS /* intentional without ';' */
  207. int j;
  208. int nack;
  209. I2C_ACTIVE;
  210. for(j = 0; j < 8; j++) {
  211. I2C_SCL(0);
  212. I2C_DELAY;
  213. I2C_SDA(data & 0x80);
  214. I2C_DELAY;
  215. I2C_SCL(1);
  216. I2C_DELAY;
  217. I2C_DELAY;
  218. data <<= 1;
  219. }
  220. /*
  221. * Look for an <ACK>(negative logic) and return it.
  222. */
  223. I2C_SCL(0);
  224. I2C_DELAY;
  225. I2C_SDA(1);
  226. I2C_TRISTATE;
  227. I2C_DELAY;
  228. I2C_SCL(1);
  229. I2C_DELAY;
  230. I2C_DELAY;
  231. nack = I2C_READ;
  232. I2C_SCL(0);
  233. I2C_DELAY;
  234. I2C_ACTIVE;
  235. return(nack); /* not a nack is an ack */
  236. }
  237. #if defined(CONFIG_I2C_MULTI_BUS)
  238. /*
  239. * Functions for multiple I2C bus handling
  240. */
  241. unsigned int i2c_get_bus_num(void)
  242. {
  243. return i2c_bus_num;
  244. }
  245. int i2c_set_bus_num(unsigned int bus)
  246. {
  247. #if defined(CONFIG_I2C_MUX)
  248. if (bus < CONFIG_SYS_MAX_I2C_BUS) {
  249. i2c_bus_num = bus;
  250. } else {
  251. int ret;
  252. ret = i2x_mux_select_mux(bus);
  253. if (ret == 0)
  254. i2c_bus_num = bus;
  255. else
  256. return ret;
  257. }
  258. #else
  259. if (bus >= CONFIG_SYS_MAX_I2C_BUS)
  260. return -1;
  261. i2c_bus_num = bus;
  262. #endif
  263. return 0;
  264. }
  265. #endif
  266. /*-----------------------------------------------------------------------
  267. * if ack == I2C_ACK, ACK the byte so can continue reading, else
  268. * send I2C_NOACK to end the read.
  269. */
  270. static uchar read_byte(int ack)
  271. {
  272. I2C_SOFT_DECLARATIONS /* intentional without ';' */
  273. int data;
  274. int j;
  275. /*
  276. * Read 8 bits, MSB first.
  277. */
  278. I2C_TRISTATE;
  279. I2C_SDA(1);
  280. data = 0;
  281. for(j = 0; j < 8; j++) {
  282. I2C_SCL(0);
  283. I2C_DELAY;
  284. I2C_SCL(1);
  285. I2C_DELAY;
  286. data <<= 1;
  287. data |= I2C_READ;
  288. I2C_DELAY;
  289. }
  290. send_ack(ack);
  291. return(data);
  292. }
  293. /*=====================================================================*/
  294. /* Public Functions */
  295. /*=====================================================================*/
  296. /*-----------------------------------------------------------------------
  297. * Initialization
  298. */
  299. void i2c_init (int speed, int slaveaddr)
  300. {
  301. #if defined(CONFIG_SYS_I2C_INIT_BOARD)
  302. /* call board specific i2c bus reset routine before accessing the */
  303. /* environment, which might be in a chip on that bus. For details */
  304. /* about this problem see doc/I2C_Edge_Conditions. */
  305. i2c_init_board();
  306. #else
  307. /*
  308. * WARNING: Do NOT save speed in a static variable: if the
  309. * I2C routines are called before RAM is initialized (to read
  310. * the DIMM SPD, for instance), RAM won't be usable and your
  311. * system will crash.
  312. */
  313. send_reset ();
  314. #endif
  315. }
  316. /*-----------------------------------------------------------------------
  317. * Probe to see if a chip is present. Also good for checking for the
  318. * completion of EEPROM writes since the chip stops responding until
  319. * the write completes (typically 10mSec).
  320. */
  321. int i2c_probe(uchar addr)
  322. {
  323. int rc;
  324. /*
  325. * perform 1 byte write transaction with just address byte
  326. * (fake write)
  327. */
  328. send_start();
  329. rc = write_byte ((addr << 1) | 0);
  330. send_stop();
  331. return (rc ? 1 : 0);
  332. }
  333. /*-----------------------------------------------------------------------
  334. * Read bytes
  335. */
  336. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  337. {
  338. int shift;
  339. PRINTD("i2c_read: chip %02X addr %02X alen %d buffer %p len %d\n",
  340. chip, addr, alen, buffer, len);
  341. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  342. /*
  343. * EEPROM chips that implement "address overflow" are ones
  344. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  345. * address and the extra bits end up in the "chip address"
  346. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  347. * four 256 byte chips.
  348. *
  349. * Note that we consider the length of the address field to
  350. * still be one byte because the extra address bits are
  351. * hidden in the chip address.
  352. */
  353. chip |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  354. PRINTD("i2c_read: fix addr_overflow: chip %02X addr %02X\n",
  355. chip, addr);
  356. #endif
  357. /*
  358. * Do the addressing portion of a write cycle to set the
  359. * chip's address pointer. If the address length is zero,
  360. * don't do the normal write cycle to set the address pointer,
  361. * there is no address pointer in this chip.
  362. */
  363. send_start();
  364. if(alen > 0) {
  365. if(write_byte(chip << 1)) { /* write cycle */
  366. send_stop();
  367. PRINTD("i2c_read, no chip responded %02X\n", chip);
  368. return(1);
  369. }
  370. shift = (alen-1) * 8;
  371. while(alen-- > 0) {
  372. if(write_byte(addr >> shift)) {
  373. PRINTD("i2c_read, address not <ACK>ed\n");
  374. return(1);
  375. }
  376. shift -= 8;
  377. }
  378. /* Some I2C chips need a stop/start sequence here,
  379. * other chips don't work with a full stop and need
  380. * only a start. Default behaviour is to send the
  381. * stop/start sequence.
  382. */
  383. #ifdef CONFIG_SOFT_I2C_READ_REPEATED_START
  384. send_start();
  385. #else
  386. send_stop();
  387. send_start();
  388. #endif
  389. }
  390. /*
  391. * Send the chip address again, this time for a read cycle.
  392. * Then read the data. On the last byte, we do a NACK instead
  393. * of an ACK(len == 0) to terminate the read.
  394. */
  395. write_byte((chip << 1) | 1); /* read cycle */
  396. while(len-- > 0) {
  397. *buffer++ = read_byte(len == 0);
  398. }
  399. send_stop();
  400. return(0);
  401. }
  402. /*-----------------------------------------------------------------------
  403. * Write bytes
  404. */
  405. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  406. {
  407. int shift, failures = 0;
  408. PRINTD("i2c_write: chip %02X addr %02X alen %d buffer %p len %d\n",
  409. chip, addr, alen, buffer, len);
  410. send_start();
  411. if(write_byte(chip << 1)) { /* write cycle */
  412. send_stop();
  413. PRINTD("i2c_write, no chip responded %02X\n", chip);
  414. return(1);
  415. }
  416. shift = (alen-1) * 8;
  417. while(alen-- > 0) {
  418. if(write_byte(addr >> shift)) {
  419. PRINTD("i2c_write, address not <ACK>ed\n");
  420. return(1);
  421. }
  422. shift -= 8;
  423. }
  424. while(len-- > 0) {
  425. if(write_byte(*buffer++)) {
  426. failures++;
  427. }
  428. }
  429. send_stop();
  430. return(failures);
  431. }