ddk750_hwi2c.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #define USE_HW_I2C
  2. #ifdef USE_HW_I2C
  3. #include "ddk750_help.h"
  4. #include "ddk750_reg.h"
  5. #include "ddk750_hwi2c.h"
  6. #include "ddk750_power.h"
  7. #define MAX_HWI2C_FIFO 16
  8. #define HWI2C_WAIT_TIMEOUT 0xF0000
  9. int sm750_hw_i2c_init(
  10. unsigned char bus_speed_mode
  11. )
  12. {
  13. unsigned int value;
  14. /* Enable GPIO 30 & 31 as IIC clock & data */
  15. value = PEEK32(GPIO_MUX);
  16. value = FIELD_SET(value, GPIO_MUX, 30, I2C) |
  17. FIELD_SET(0, GPIO_MUX, 31, I2C);
  18. POKE32(GPIO_MUX, value);
  19. /* Enable Hardware I2C power.
  20. TODO: Check if we need to enable GPIO power?
  21. */
  22. enableI2C(1);
  23. /* Enable the I2C Controller and set the bus speed mode */
  24. value = PEEK32(I2C_CTRL);
  25. if (bus_speed_mode == 0)
  26. value = FIELD_SET(value, I2C_CTRL, MODE, STANDARD);
  27. else
  28. value = FIELD_SET(value, I2C_CTRL, MODE, FAST);
  29. value = FIELD_SET(value, I2C_CTRL, EN, ENABLE);
  30. POKE32(I2C_CTRL, value);
  31. return 0;
  32. }
  33. void sm750_hw_i2c_close(void)
  34. {
  35. unsigned int value;
  36. /* Disable I2C controller */
  37. value = PEEK32(I2C_CTRL);
  38. value = FIELD_SET(value, I2C_CTRL, EN, DISABLE);
  39. POKE32(I2C_CTRL, value);
  40. /* Disable I2C Power */
  41. enableI2C(0);
  42. /* Set GPIO 30 & 31 back as GPIO pins */
  43. value = PEEK32(GPIO_MUX);
  44. value = FIELD_SET(value, GPIO_MUX, 30, GPIO);
  45. value = FIELD_SET(value, GPIO_MUX, 31, GPIO);
  46. POKE32(GPIO_MUX, value);
  47. }
  48. static long hw_i2c_wait_tx_done(void)
  49. {
  50. unsigned int timeout;
  51. /* Wait until the transfer is completed. */
  52. timeout = HWI2C_WAIT_TIMEOUT;
  53. while ((FIELD_GET(PEEK32(I2C_STATUS),
  54. I2C_STATUS, TX) != I2C_STATUS_TX_COMPLETED) &&
  55. (timeout != 0))
  56. timeout--;
  57. if (timeout == 0)
  58. return (-1);
  59. return 0;
  60. }
  61. /*
  62. * This function writes data to the i2c slave device registers.
  63. *
  64. * Parameters:
  65. * addr - i2c Slave device address
  66. * length - Total number of bytes to be written to the device
  67. * buf - The buffer that contains the data to be written to the
  68. * i2c device.
  69. *
  70. * Return Value:
  71. * Total number of bytes those are actually written.
  72. */
  73. static unsigned int hw_i2c_write_data(
  74. unsigned char addr,
  75. unsigned int length,
  76. unsigned char *buf
  77. )
  78. {
  79. unsigned char count, i;
  80. unsigned int total_bytes = 0;
  81. /* Set the Device Address */
  82. POKE32(I2C_SLAVE_ADDRESS, addr & ~0x01);
  83. /* Write data.
  84. * Note:
  85. * Only 16 byte can be accessed per i2c start instruction.
  86. */
  87. do {
  88. /*
  89. * Reset I2C by writing 0 to I2C_RESET register to
  90. * clear the previous status.
  91. */
  92. POKE32(I2C_RESET, 0);
  93. /* Set the number of bytes to be written */
  94. if (length < MAX_HWI2C_FIFO)
  95. count = length - 1;
  96. else
  97. count = MAX_HWI2C_FIFO - 1;
  98. POKE32(I2C_BYTE_COUNT, count);
  99. /* Move the data to the I2C data register */
  100. for (i = 0; i <= count; i++)
  101. POKE32(I2C_DATA0 + i, *buf++);
  102. /* Start the I2C */
  103. POKE32(I2C_CTRL,
  104. FIELD_SET(PEEK32(I2C_CTRL), I2C_CTRL, CTRL, START));
  105. /* Wait until the transfer is completed. */
  106. if (hw_i2c_wait_tx_done() != 0)
  107. break;
  108. /* Substract length */
  109. length -= (count + 1);
  110. /* Total byte written */
  111. total_bytes += (count + 1);
  112. } while (length > 0);
  113. return total_bytes;
  114. }
  115. /*
  116. * This function reads data from the slave device and stores them
  117. * in the given buffer
  118. *
  119. * Parameters:
  120. * addr - i2c Slave device address
  121. * length - Total number of bytes to be read
  122. * buf - Pointer to a buffer to be filled with the data read
  123. * from the slave device. It has to be the same size as the
  124. * length to make sure that it can keep all the data read.
  125. *
  126. * Return Value:
  127. * Total number of actual bytes read from the slave device
  128. */
  129. static unsigned int hw_i2c_read_data(
  130. unsigned char addr,
  131. unsigned int length,
  132. unsigned char *buf
  133. )
  134. {
  135. unsigned char count, i;
  136. unsigned int total_bytes = 0;
  137. /* Set the Device Address */
  138. POKE32(I2C_SLAVE_ADDRESS, addr | 0x01);
  139. /* Read data and save them to the buffer.
  140. * Note:
  141. * Only 16 byte can be accessed per i2c start instruction.
  142. */
  143. do {
  144. /*
  145. * Reset I2C by writing 0 to I2C_RESET register to
  146. * clear all the status.
  147. */
  148. POKE32(I2C_RESET, 0);
  149. /* Set the number of bytes to be read */
  150. if (length <= MAX_HWI2C_FIFO)
  151. count = length - 1;
  152. else
  153. count = MAX_HWI2C_FIFO - 1;
  154. POKE32(I2C_BYTE_COUNT, count);
  155. /* Start the I2C */
  156. POKE32(I2C_CTRL,
  157. FIELD_SET(PEEK32(I2C_CTRL), I2C_CTRL, CTRL, START));
  158. /* Wait until transaction done. */
  159. if (hw_i2c_wait_tx_done() != 0)
  160. break;
  161. /* Save the data to the given buffer */
  162. for (i = 0; i <= count; i++)
  163. *buf++ = PEEK32(I2C_DATA0 + i);
  164. /* Substract length by 16 */
  165. length -= (count + 1);
  166. /* Number of bytes read. */
  167. total_bytes += (count + 1);
  168. } while (length > 0);
  169. return total_bytes;
  170. }
  171. /*
  172. * This function reads the slave device's register
  173. *
  174. * Parameters:
  175. * deviceAddress - i2c Slave device address which register
  176. * to be read from
  177. * registerIndex - Slave device's register to be read
  178. *
  179. * Return Value:
  180. * Register value
  181. */
  182. unsigned char sm750_hw_i2c_read_reg(
  183. unsigned char addr,
  184. unsigned char reg
  185. )
  186. {
  187. unsigned char value = (0xFF);
  188. if (hw_i2c_write_data(addr, 1, &reg) == 1)
  189. hw_i2c_read_data(addr, 1, &value);
  190. return value;
  191. }
  192. /*
  193. * This function writes a value to the slave device's register
  194. *
  195. * Parameters:
  196. * deviceAddress - i2c Slave device address which register
  197. * to be written
  198. * registerIndex - Slave device's register to be written
  199. * data - Data to be written to the register
  200. *
  201. * Result:
  202. * 0 - Success
  203. * -1 - Fail
  204. */
  205. int sm750_hw_i2c_write_reg(
  206. unsigned char addr,
  207. unsigned char reg,
  208. unsigned char data
  209. )
  210. {
  211. unsigned char value[2];
  212. value[0] = reg;
  213. value[1] = data;
  214. if (hw_i2c_write_data(addr, 2, value) == 2)
  215. return 0;
  216. return (-1);
  217. }
  218. #endif