ddk750_swi2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*******************************************************************
  2. *
  3. * Copyright (c) 2007 by Silicon Motion, Inc. (SMI)
  4. *
  5. * All rights are reserved. Reproduction or in part is prohibited
  6. * without the written consent of the copyright owner.
  7. *
  8. * swi2c.c --- SM750/SM718 DDK
  9. * This file contains the source code for I2C using software
  10. * implementation.
  11. *
  12. *******************************************************************/
  13. #include "ddk750_help.h"
  14. #include "ddk750_reg.h"
  15. #include "ddk750_swi2c.h"
  16. #include "ddk750_power.h"
  17. /*******************************************************************
  18. * I2C Software Master Driver:
  19. * ===========================
  20. * Each i2c cycle is split into 4 sections. Each of these section marks
  21. * a point in time where the SCL or SDA may be changed.
  22. *
  23. * 1 Cycle == | Section I. | Section 2. | Section 3. | Section 4. |
  24. * +-------------+-------------+-------------+-------------+
  25. * | SCL set LOW |SCL no change| SCL set HIGH|SCL no change|
  26. *
  27. * ____________ _____________
  28. * SCL == XXXX _____________ ____________ /
  29. *
  30. * I.e. the SCL may only be changed in section 1. and section 3. while
  31. * the SDA may only be changed in section 2. and section 4. The table
  32. * below gives the changes for these 2 lines in the varios sections.
  33. *
  34. * Section changes Table:
  35. * ======================
  36. * blank = no change, L = set bit LOW, H = set bit HIGH
  37. *
  38. * | 1.| 2.| 3.| 4.|
  39. * ---------------+---+---+---+---+
  40. * Tx Start SDA | | H | | L |
  41. * SCL | L | | H | |
  42. * ---------------+---+---+---+---+
  43. * Tx Stop SDA | | L | | H |
  44. * SCL | L | | H | |
  45. * ---------------+---+---+---+---+
  46. * Tx bit H SDA | | H | | |
  47. * SCL | L | | H | |
  48. * ---------------+---+---+---+---+
  49. * Tx bit L SDA | | L | | |
  50. * SCL | L | | H | |
  51. * ---------------+---+---+---+---+
  52. *
  53. ******************************************************************/
  54. /* GPIO pins used for this I2C. It ranges from 0 to 63. */
  55. static unsigned char sw_i2c_clk_gpio = DEFAULT_I2C_SCL;
  56. static unsigned char sw_i2c_data_gpio = DEFAULT_I2C_SDA;
  57. /*
  58. * Below is the variable declaration for the GPIO pin register usage
  59. * for the i2c Clock and i2c Data.
  60. *
  61. * Note:
  62. * Notice that the GPIO usage for the i2c clock and i2c Data are
  63. * separated. This is to make this code flexible enough when
  64. * two separate GPIO pins for the clock and data are located
  65. * in two different GPIO register set (worst case).
  66. */
  67. /* i2c Clock GPIO Register usage */
  68. static unsigned long sw_i2c_clk_gpio_mux_reg = GPIO_MUX;
  69. static unsigned long sw_i2c_clk_gpio_data_reg = GPIO_DATA;
  70. static unsigned long sw_i2c_clk_gpio_data_dir_reg = GPIO_DATA_DIRECTION;
  71. /* i2c Data GPIO Register usage */
  72. static unsigned long sw_i2c_data_gpio_mux_reg = GPIO_MUX;
  73. static unsigned long sw_i2c_data_gpio_data_reg = GPIO_DATA;
  74. static unsigned long sw_i2c_data_gpio_data_dir_reg = GPIO_DATA_DIRECTION;
  75. /*
  76. * This function puts a delay between command
  77. */
  78. static void sw_i2c_wait(void)
  79. {
  80. /* find a bug:
  81. * peekIO method works well before suspend/resume
  82. * but after suspend, peekIO(0x3ce,0x61) & 0x10
  83. * always be non-zero,which makes the while loop
  84. * never finish.
  85. * use non-ultimate for loop below is safe
  86. * */
  87. /* Change wait algorithm to use PCI bus clock,
  88. it's more reliable than counter loop ..
  89. write 0x61 to 0x3ce and read from 0x3cf
  90. */
  91. int i, tmp;
  92. for (i = 0; i < 600; i++) {
  93. tmp = i;
  94. tmp += i;
  95. }
  96. }
  97. /*
  98. * This function set/reset the SCL GPIO pin
  99. *
  100. * Parameters:
  101. * value - Bit value to set to the SCL or SDA (0 = low, 1 = high)
  102. *
  103. * Notes:
  104. * When setting SCL to high, just set the GPIO as input where the pull up
  105. * resistor will pull the signal up. Do not use software to pull up the
  106. * signal because the i2c will fail when other device try to drive the
  107. * signal due to SM50x will drive the signal to always high.
  108. */
  109. static void sw_i2c_scl(unsigned char value)
  110. {
  111. unsigned long gpio_data;
  112. unsigned long gpio_dir;
  113. gpio_dir = PEEK32(sw_i2c_clk_gpio_data_dir_reg);
  114. if (value) { /* High */
  115. /*
  116. * Set direction as input. This will automatically
  117. * pull the signal up.
  118. */
  119. gpio_dir &= ~(1 << sw_i2c_clk_gpio);
  120. POKE32(sw_i2c_clk_gpio_data_dir_reg, gpio_dir);
  121. } else { /* Low */
  122. /* Set the signal down */
  123. gpio_data = PEEK32(sw_i2c_clk_gpio_data_reg);
  124. gpio_data &= ~(1 << sw_i2c_clk_gpio);
  125. POKE32(sw_i2c_clk_gpio_data_reg, gpio_data);
  126. /* Set direction as output */
  127. gpio_dir |= (1 << sw_i2c_clk_gpio);
  128. POKE32(sw_i2c_clk_gpio_data_dir_reg, gpio_dir);
  129. }
  130. }
  131. /*
  132. * This function set/reset the SDA GPIO pin
  133. *
  134. * Parameters:
  135. * value - Bit value to set to the SCL or SDA (0 = low, 1 = high)
  136. *
  137. * Notes:
  138. * When setting SCL to high, just set the GPIO as input where the pull up
  139. * resistor will pull the signal up. Do not use software to pull up the
  140. * signal because the i2c will fail when other device try to drive the
  141. * signal due to SM50x will drive the signal to always high.
  142. */
  143. static void sw_i2c_sda(unsigned char value)
  144. {
  145. unsigned long gpio_data;
  146. unsigned long gpio_dir;
  147. gpio_dir = PEEK32(sw_i2c_data_gpio_data_dir_reg);
  148. if (value) { /* High */
  149. /*
  150. * Set direction as input. This will automatically
  151. * pull the signal up.
  152. */
  153. gpio_dir &= ~(1 << sw_i2c_data_gpio);
  154. POKE32(sw_i2c_data_gpio_data_dir_reg, gpio_dir);
  155. } else { /* Low */
  156. /* Set the signal down */
  157. gpio_data = PEEK32(sw_i2c_data_gpio_data_reg);
  158. gpio_data &= ~(1 << sw_i2c_data_gpio);
  159. POKE32(sw_i2c_data_gpio_data_reg, gpio_data);
  160. /* Set direction as output */
  161. gpio_dir |= (1 << sw_i2c_data_gpio);
  162. POKE32(sw_i2c_data_gpio_data_dir_reg, gpio_dir);
  163. }
  164. }
  165. /*
  166. * This function read the data from the SDA GPIO pin
  167. *
  168. * Return Value:
  169. * The SDA data bit sent by the Slave
  170. */
  171. static unsigned char sw_i2c_read_sda(void)
  172. {
  173. unsigned long gpio_dir;
  174. unsigned long gpio_data;
  175. unsigned long dir_mask = 1 << sw_i2c_data_gpio;
  176. /* Make sure that the direction is input (High) */
  177. gpio_dir = PEEK32(sw_i2c_data_gpio_data_dir_reg);
  178. if ((gpio_dir & dir_mask) != ~dir_mask) {
  179. gpio_dir &= ~(1 << sw_i2c_data_gpio);
  180. POKE32(sw_i2c_data_gpio_data_dir_reg, gpio_dir);
  181. }
  182. /* Now read the SDA line */
  183. gpio_data = PEEK32(sw_i2c_data_gpio_data_reg);
  184. if (gpio_data & (1 << sw_i2c_data_gpio))
  185. return 1;
  186. else
  187. return 0;
  188. }
  189. /*
  190. * This function sends ACK signal
  191. */
  192. static void sw_i2c_ack(void)
  193. {
  194. return; /* Single byte read is ok without it. */
  195. }
  196. /*
  197. * This function sends the start command to the slave device
  198. */
  199. static void sw_i2c_start(void)
  200. {
  201. /* Start I2C */
  202. sw_i2c_sda(1);
  203. sw_i2c_scl(1);
  204. sw_i2c_sda(0);
  205. }
  206. /*
  207. * This function sends the stop command to the slave device
  208. */
  209. static void sw_i2c_stop(void)
  210. {
  211. /* Stop the I2C */
  212. sw_i2c_scl(1);
  213. sw_i2c_sda(0);
  214. sw_i2c_sda(1);
  215. }
  216. /*
  217. * This function writes one byte to the slave device
  218. *
  219. * Parameters:
  220. * data - Data to be write to the slave device
  221. *
  222. * Return Value:
  223. * 0 - Success
  224. * -1 - Fail to write byte
  225. */
  226. static long sw_i2c_write_byte(unsigned char data)
  227. {
  228. unsigned char value = data;
  229. int i;
  230. /* Sending the data bit by bit */
  231. for (i = 0; i < 8; i++) {
  232. /* Set SCL to low */
  233. sw_i2c_scl(0);
  234. /* Send data bit */
  235. if ((value & 0x80) != 0)
  236. sw_i2c_sda(1);
  237. else
  238. sw_i2c_sda(0);
  239. sw_i2c_wait();
  240. /* Toggle clk line to one */
  241. sw_i2c_scl(1);
  242. sw_i2c_wait();
  243. /* Shift byte to be sent */
  244. value = value << 1;
  245. }
  246. /* Set the SCL Low and SDA High (prepare to get input) */
  247. sw_i2c_scl(0);
  248. sw_i2c_sda(1);
  249. /* Set the SCL High for ack */
  250. sw_i2c_wait();
  251. sw_i2c_scl(1);
  252. sw_i2c_wait();
  253. /* Read SDA, until SDA==0 */
  254. for (i = 0; i < 0xff; i++) {
  255. if (!sw_i2c_read_sda())
  256. break;
  257. sw_i2c_scl(0);
  258. sw_i2c_wait();
  259. sw_i2c_scl(1);
  260. sw_i2c_wait();
  261. }
  262. /* Set the SCL Low and SDA High */
  263. sw_i2c_scl(0);
  264. sw_i2c_sda(1);
  265. if (i < 0xff)
  266. return 0;
  267. else
  268. return -1;
  269. }
  270. /*
  271. * This function reads one byte from the slave device
  272. *
  273. * Parameters:
  274. * ack - Flag to indicate either to send the acknowledge
  275. * message to the slave device or not
  276. *
  277. * Return Value:
  278. * One byte data read from the Slave device
  279. */
  280. static unsigned char sw_i2c_read_byte(unsigned char ack)
  281. {
  282. int i;
  283. unsigned char data = 0;
  284. for (i = 7; i >= 0; i--) {
  285. /* Set the SCL to Low and SDA to High (Input) */
  286. sw_i2c_scl(0);
  287. sw_i2c_sda(1);
  288. sw_i2c_wait();
  289. /* Set the SCL High */
  290. sw_i2c_scl(1);
  291. sw_i2c_wait();
  292. /* Read data bits from SDA */
  293. data |= (sw_i2c_read_sda() << i);
  294. }
  295. if (ack)
  296. sw_i2c_ack();
  297. /* Set the SCL Low and SDA High */
  298. sw_i2c_scl(0);
  299. sw_i2c_sda(1);
  300. return data;
  301. }
  302. /*
  303. * This function initializes GPIO port for SW I2C communication.
  304. *
  305. * Parameters:
  306. * clk_gpio - The GPIO pin to be used as i2c SCL
  307. * data_gpio - The GPIO pin to be used as i2c SDA
  308. *
  309. * Return Value:
  310. * -1 - Fail to initialize the i2c
  311. * 0 - Success
  312. */
  313. static long sm750le_i2c_init(unsigned char clk_gpio,
  314. unsigned char data_gpio)
  315. {
  316. int i;
  317. /* Initialize the GPIO pin for the i2c Clock Register */
  318. sw_i2c_clk_gpio_data_reg = GPIO_DATA_SM750LE;
  319. sw_i2c_clk_gpio_data_dir_reg = GPIO_DATA_DIRECTION_SM750LE;
  320. /* Initialize the Clock GPIO Offset */
  321. sw_i2c_clk_gpio = clk_gpio;
  322. /* Initialize the GPIO pin for the i2c Data Register */
  323. sw_i2c_data_gpio_data_reg = GPIO_DATA_SM750LE;
  324. sw_i2c_data_gpio_data_dir_reg = GPIO_DATA_DIRECTION_SM750LE;
  325. /* Initialize the Data GPIO Offset */
  326. sw_i2c_data_gpio = data_gpio;
  327. /* Note that SM750LE don't have GPIO MUX and power is always on */
  328. /* Clear the i2c lines. */
  329. for (i = 0; i < 9; i++)
  330. sw_i2c_stop();
  331. return 0;
  332. }
  333. /*
  334. * This function initializes the i2c attributes and bus
  335. *
  336. * Parameters:
  337. * clk_gpio - The GPIO pin to be used as i2c SCL
  338. * data_gpio - The GPIO pin to be used as i2c SDA
  339. *
  340. * Return Value:
  341. * -1 - Fail to initialize the i2c
  342. * 0 - Success
  343. */
  344. long sm750_sw_i2c_init(
  345. unsigned char clk_gpio,
  346. unsigned char data_gpio
  347. )
  348. {
  349. int i;
  350. /*
  351. * Return 0 if the GPIO pins to be used is out of range. The
  352. * range is only from [0..63]
  353. */
  354. if ((clk_gpio > 31) || (data_gpio > 31))
  355. return -1;
  356. if (getChipType() == SM750LE)
  357. return sm750le_i2c_init(clk_gpio, data_gpio);
  358. /* Initialize the GPIO pin for the i2c Clock Register */
  359. sw_i2c_clk_gpio_mux_reg = GPIO_MUX;
  360. sw_i2c_clk_gpio_data_reg = GPIO_DATA;
  361. sw_i2c_clk_gpio_data_dir_reg = GPIO_DATA_DIRECTION;
  362. /* Initialize the Clock GPIO Offset */
  363. sw_i2c_clk_gpio = clk_gpio;
  364. /* Initialize the GPIO pin for the i2c Data Register */
  365. sw_i2c_data_gpio_mux_reg = GPIO_MUX;
  366. sw_i2c_data_gpio_data_reg = GPIO_DATA;
  367. sw_i2c_data_gpio_data_dir_reg = GPIO_DATA_DIRECTION;
  368. /* Initialize the Data GPIO Offset */
  369. sw_i2c_data_gpio = data_gpio;
  370. /* Enable the GPIO pins for the i2c Clock and Data (GPIO MUX) */
  371. POKE32(sw_i2c_clk_gpio_mux_reg,
  372. PEEK32(sw_i2c_clk_gpio_mux_reg) & ~(1 << sw_i2c_clk_gpio));
  373. POKE32(sw_i2c_data_gpio_mux_reg,
  374. PEEK32(sw_i2c_data_gpio_mux_reg) & ~(1 << sw_i2c_data_gpio));
  375. /* Enable GPIO power */
  376. enableGPIO(1);
  377. /* Clear the i2c lines. */
  378. for (i = 0; i < 9; i++)
  379. sw_i2c_stop();
  380. return 0;
  381. }
  382. /*
  383. * This function reads the slave device's register
  384. *
  385. * Parameters:
  386. * addr - i2c Slave device address which register
  387. * to be read from
  388. * reg - Slave device's register to be read
  389. *
  390. * Return Value:
  391. * Register value
  392. */
  393. unsigned char sm750_sw_i2c_read_reg(
  394. unsigned char addr,
  395. unsigned char reg
  396. )
  397. {
  398. unsigned char data;
  399. /* Send the Start signal */
  400. sw_i2c_start();
  401. /* Send the device address */
  402. sw_i2c_write_byte(addr);
  403. /* Send the register index */
  404. sw_i2c_write_byte(reg);
  405. /* Get the bus again and get the data from the device read address */
  406. sw_i2c_start();
  407. sw_i2c_write_byte(addr + 1);
  408. data = sw_i2c_read_byte(1);
  409. /* Stop swI2C and release the bus */
  410. sw_i2c_stop();
  411. return data;
  412. }
  413. /*
  414. * This function writes a value to the slave device's register
  415. *
  416. * Parameters:
  417. * addr - i2c Slave device address which register
  418. * to be written
  419. * reg - Slave device's register to be written
  420. * data - Data to be written to the register
  421. *
  422. * Result:
  423. * 0 - Success
  424. * -1 - Fail
  425. */
  426. long sm750_sw_i2c_write_reg(
  427. unsigned char addr,
  428. unsigned char reg,
  429. unsigned char data
  430. )
  431. {
  432. long ret = 0;
  433. /* Send the Start signal */
  434. sw_i2c_start();
  435. /* Send the device address and read the data. All should return success
  436. in order for the writing processed to be successful
  437. */
  438. if ((sw_i2c_write_byte(addr) != 0) ||
  439. (sw_i2c_write_byte(reg) != 0) ||
  440. (sw_i2c_write_byte(data) != 0)) {
  441. ret = -1;
  442. }
  443. /* Stop i2c and release the bus */
  444. sw_i2c_stop();
  445. return ret;
  446. }