i2c.c 5.8 KB

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