i2c.c 5.7 KB

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