i2c.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #include <common.h>
  2. #include <mpc8xx.h>
  3. #include <malloc.h>
  4. #include <galileo/gt64260R.h>
  5. #include <galileo/core.h>
  6. #define MAX_I2C_RETRYS 10
  7. #define I2C_DELAY 1000 /* Should be at least the # of MHz of Tclk */
  8. #undef DEBUG_I2C
  9. #ifdef DEBUG_I2C
  10. #define DP(x) x
  11. #else
  12. #define DP(x)
  13. #endif
  14. /* Assuming that there is only one master on the bus (us) */
  15. static void
  16. i2c_init(int speed, int slaveaddr)
  17. {
  18. unsigned int n, m, freq, margin, power;
  19. unsigned int actualFreq, actualN = 0, actualM = 0;
  20. unsigned int control, status;
  21. unsigned int minMargin = 0xffffffff;
  22. unsigned int tclk = 125000000;
  23. DP(puts("i2c_init\n"));
  24. for (n = 0 ; n < 8 ; n++) {
  25. for (m = 0 ; m < 16 ; m++) {
  26. power = 2<<n; /* power = 2^(n+1) */
  27. freq = tclk/(10*(m+1)*power);
  28. if (speed > freq)
  29. margin = speed - freq;
  30. else
  31. margin = freq - speed;
  32. if (margin < minMargin) {
  33. minMargin = margin;
  34. actualFreq = freq;
  35. actualN = n;
  36. actualM = m;
  37. }
  38. }
  39. }
  40. DP(puts("setup i2c bus\n"));
  41. /* Setup bus */
  42. GT_REG_WRITE(I2C_SOFT_RESET, 0);
  43. DP(puts("udelay...\n"));
  44. udelay(I2C_DELAY);
  45. DP(puts("set baudrate\n"));
  46. GT_REG_WRITE(I2C_STATUS_BAUDE_RATE, (actualM << 3) | actualN);
  47. GT_REG_WRITE(I2C_CONTROL, (0x1 << 2) | (0x1 << 6));
  48. udelay(I2C_DELAY * 10);
  49. DP(puts("read control, baudrate\n"));
  50. GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
  51. GT_REG_READ(I2C_CONTROL, &control);
  52. }
  53. static uchar
  54. i2c_start(void)
  55. {
  56. unsigned int control, status;
  57. int count = 0;
  58. DP(puts("i2c_start\n"));
  59. /* Set the start bit */
  60. GT_REG_READ(I2C_CONTROL, &control);
  61. control |= (0x1 << 5);
  62. GT_REG_WRITE(I2C_CONTROL, control);
  63. GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
  64. count = 0;
  65. while ((status & 0xff) != 0x08) {
  66. udelay(I2C_DELAY);
  67. if (count > 20) {
  68. GT_REG_WRITE(I2C_CONTROL, (0x1 << 4)); /*stop*/
  69. return status;
  70. }
  71. GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
  72. count++;
  73. }
  74. return 0;
  75. }
  76. static uchar
  77. i2c_select_device(uchar dev_addr, uchar read, int ten_bit)
  78. {
  79. unsigned int status, data, bits = 7;
  80. int count = 0;
  81. DP(puts("i2c_select_device\n"));
  82. /* Output slave address */
  83. if (ten_bit)
  84. bits = 10;
  85. data = (dev_addr << 1);
  86. /* set the read bit */
  87. data |= read;
  88. GT_REG_WRITE(I2C_DATA, data);
  89. /* assert the address */
  90. RESET_REG_BITS(I2C_CONTROL, BIT3);
  91. udelay(I2C_DELAY);
  92. GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
  93. count = 0;
  94. while (((status & 0xff) != 0x40) && ((status & 0xff) != 0x18)) {
  95. udelay(I2C_DELAY);
  96. if (count > 20) {
  97. GT_REG_WRITE(I2C_CONTROL, (0x1 << 4)); /*stop*/
  98. return status;
  99. }
  100. GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
  101. count++;
  102. }
  103. if (bits == 10) {
  104. printf("10 bit I2C addressing not yet implemented\n");
  105. return 0xff;
  106. }
  107. return 0;
  108. }
  109. static uchar
  110. i2c_get_data(uchar *return_data, int len) {
  111. unsigned int data, status = 0;
  112. int count = 0;
  113. DP(puts("i2c_get_data\n"));
  114. while (len) {
  115. /* Get and return the data */
  116. RESET_REG_BITS(I2C_CONTROL, (0x1 << 3));
  117. udelay(I2C_DELAY * 5);
  118. GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
  119. count++;
  120. while ((status & 0xff) != 0x50) {
  121. udelay(I2C_DELAY);
  122. if (count > 2) {
  123. GT_REG_WRITE(I2C_CONTROL, (0x1 << 4)); /*stop*/
  124. return 0;
  125. }
  126. GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
  127. count++;
  128. }
  129. GT_REG_READ(I2C_DATA, &data);
  130. len--;
  131. *return_data = (uchar)data;
  132. return_data++;
  133. }
  134. RESET_REG_BITS(I2C_CONTROL, BIT2|BIT3);
  135. while ((status & 0xff) != 0x58) {
  136. udelay(I2C_DELAY);
  137. if (count > 200) {
  138. GT_REG_WRITE(I2C_CONTROL, (0x1 << 4)); /*stop*/
  139. return status;
  140. }
  141. GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
  142. count++;
  143. }
  144. GT_REG_WRITE(I2C_CONTROL, (0x1 << 4)); /* stop */
  145. return 0;
  146. }
  147. static uchar
  148. i2c_write_data(unsigned int data, int len)
  149. {
  150. unsigned int status;
  151. int count = 0;
  152. DP(puts("i2c_write_data\n"));
  153. if (len > 4)
  154. return -1;
  155. while (len) {
  156. /* Set and assert the data */
  157. GT_REG_WRITE(I2C_DATA, (unsigned int)data);
  158. RESET_REG_BITS(I2C_CONTROL, (0x1 << 3));
  159. udelay(I2C_DELAY);
  160. GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
  161. count++;
  162. while ((status & 0xff) != 0x28) {
  163. udelay(I2C_DELAY);
  164. if (count > 20) {
  165. GT_REG_WRITE(I2C_CONTROL, (0x1 << 4)); /*stop*/
  166. return status;
  167. }
  168. GT_REG_READ(I2C_STATUS_BAUDE_RATE, &status);
  169. count++;
  170. }
  171. len--;
  172. }
  173. GT_REG_WRITE(I2C_CONTROL, (0x1 << 3) | (0x1 << 4));
  174. GT_REG_WRITE(I2C_CONTROL, (0x1 << 4));
  175. udelay(I2C_DELAY * 10);
  176. return 0;
  177. }
  178. static uchar
  179. i2c_set_dev_offset(uchar dev_addr, unsigned int offset, int ten_bit)
  180. {
  181. uchar status;
  182. DP(puts("i2c_set_dev_offset\n"));
  183. status = i2c_select_device(dev_addr, 0, ten_bit);
  184. if (status) {
  185. #ifdef DEBUG_I2C
  186. printf("Failed to select device setting offset: 0x%02x\n",
  187. status);
  188. #endif
  189. return status;
  190. }
  191. status = i2c_write_data(offset, 1);
  192. if (status) {
  193. #ifdef DEBUG_I2C
  194. printf("Failed to write data: 0x%02x\n", status);
  195. #endif
  196. return status;
  197. }
  198. return 0;
  199. }
  200. uchar
  201. i2c_read(uchar dev_addr, unsigned int offset, int len, uchar *data,
  202. int ten_bit)
  203. {
  204. uchar status = 0;
  205. unsigned int i2cFreq = 400000;
  206. DP(puts("i2c_read\n"));
  207. i2c_init(i2cFreq, 0);
  208. status = i2c_start();
  209. if (status) {
  210. #ifdef DEBUG_I2C
  211. printf("Transaction start failed: 0x%02x\n", status);
  212. #endif
  213. return status;
  214. }
  215. status = i2c_set_dev_offset(dev_addr, 0, 0);
  216. if (status) {
  217. #ifdef DEBUG_I2C
  218. printf("Failed to set offset: 0x%02x\n", status);
  219. #endif
  220. return status;
  221. }
  222. i2c_init(i2cFreq, 0);
  223. status = i2c_start();
  224. if (status) {
  225. #ifdef DEBUG_I2C
  226. printf("Transaction restart failed: 0x%02x\n", status);
  227. #endif
  228. return status;
  229. }
  230. status = i2c_select_device(dev_addr, 1, ten_bit);
  231. if (status) {
  232. #ifdef DEBUG_I2C
  233. printf("Address not acknowledged: 0x%02x\n", status);
  234. #endif
  235. return status;
  236. }
  237. status = i2c_get_data(data, len);
  238. if (status) {
  239. #ifdef DEBUG_I2C
  240. printf("Data not received: 0x%02x\n", status);
  241. #endif
  242. return status;
  243. }
  244. return 0;
  245. }