s3c44b0_i2c.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * (C) Copyright 2004
  3. * DAVE Srl
  4. * http://www.dave-tech.it
  5. * http://www.wawnet.biz
  6. * mailto:info@wawnet.biz
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #include <asm/hardware.h>
  29. /*
  30. * Initialization, must be called once on start up, may be called
  31. * repeatedly to change the speed and slave addresses.
  32. */
  33. void i2c_init(int speed, int slaveaddr)
  34. {
  35. /*
  36. setting up I2C support
  37. */
  38. unsigned int save_F,save_PF,rIICCON,rPCONA,rPDATA,rPCONF,rPUPF;
  39. save_F = PCONF;
  40. save_PF = PUPF;
  41. rPCONF = ((save_F & ~(0xF))| 0xa);
  42. rPUPF = (save_PF | 0x3);
  43. PCONF = rPCONF; /*PF0:IICSCL, PF1:IICSDA*/
  44. PUPF = rPUPF; /* Disable pull-up */
  45. /* Configuring pin for WC pin of EEprom */
  46. rPCONA = PCONA;
  47. rPCONA &= ~(1<<9);
  48. PCONA = rPCONA;
  49. rPDATA = PDATA;
  50. rPDATA &= ~(1<<9);
  51. PDATA = rPDATA;
  52. /*
  53. Enable ACK, IICCLK=MCLK/16, enable interrupt
  54. 75MHz/16/(12+1) = 390625 Hz
  55. */
  56. rIICCON=(1<<7)|(0<<6)|(1<<5)|(0xC);
  57. IICCON = rIICCON;
  58. IICADD = slaveaddr;
  59. }
  60. /*
  61. * Probe the given I2C chip address. Returns 0 if a chip responded,
  62. * not 0 on failure.
  63. */
  64. int i2c_probe(uchar chip)
  65. {
  66. /*
  67. not implemented
  68. */
  69. printf("i2c_probe chip %d\n", (int) chip);
  70. return -1;
  71. }
  72. /*
  73. * Read/Write interface:
  74. * chip: I2C chip address, range 0..127
  75. * addr: Memory (register) address within the chip
  76. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  77. * memories, 0 for register type devices with only one
  78. * register)
  79. * buffer: Where to read/write the data
  80. * len: How many bytes to read/write
  81. *
  82. * Returns: 0 on success, not 0 on failure
  83. */
  84. #define S3C44B0X_rIIC_INTPEND (1<<4)
  85. #define S3C44B0X_rIIC_LAST_RECEIV_BIT (1<<0)
  86. #define S3C44B0X_rIIC_INTERRUPT_ENABLE (1<<5)
  87. #define S3C44B0_IIC_TIMEOUT 100
  88. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  89. {
  90. int k, j, temp;
  91. u32 rIICSTAT;
  92. /*
  93. send the device offset
  94. */
  95. rIICSTAT = 0xD0;
  96. IICSTAT = rIICSTAT;
  97. IICDS = chip; /* this is a write operation... */
  98. rIICSTAT |= (1<<5);
  99. IICSTAT = rIICSTAT;
  100. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  101. temp = IICCON;
  102. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  103. break;
  104. udelay(2000);
  105. }
  106. if (k==S3C44B0_IIC_TIMEOUT)
  107. return -1;
  108. /* wait and check ACK */
  109. temp = IICSTAT;
  110. if ((temp & S3C44B0X_rIIC_LAST_RECEIV_BIT) == S3C44B0X_rIIC_LAST_RECEIV_BIT )
  111. return -1;
  112. IICDS = addr;
  113. IICCON = IICCON & ~(S3C44B0X_rIIC_INTPEND);
  114. /* wait and check ACK */
  115. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  116. temp = IICCON;
  117. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  118. break;
  119. udelay(2000);
  120. }
  121. if (k==S3C44B0_IIC_TIMEOUT)
  122. return -1;
  123. temp = IICSTAT;
  124. if ((temp & S3C44B0X_rIIC_LAST_RECEIV_BIT) == S3C44B0X_rIIC_LAST_RECEIV_BIT )
  125. return -1;
  126. /*
  127. now we can start with the read operation...
  128. */
  129. IICDS = chip | 0x01; /* this is a read operation... */
  130. rIICSTAT = 0x90; /*master recv*/
  131. rIICSTAT |= (1<<5);
  132. IICSTAT = rIICSTAT;
  133. IICCON = IICCON & ~(S3C44B0X_rIIC_INTPEND);
  134. /* wait and check ACK */
  135. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  136. temp = IICCON;
  137. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  138. break;
  139. udelay(2000);
  140. }
  141. if (k==S3C44B0_IIC_TIMEOUT)
  142. return -1;
  143. temp = IICSTAT;
  144. if ((temp & S3C44B0X_rIIC_LAST_RECEIV_BIT) == S3C44B0X_rIIC_LAST_RECEIV_BIT )
  145. return -1;
  146. for (j=0; j<len-1; j++) {
  147. /*clear pending bit to resume */
  148. temp = IICCON & ~(S3C44B0X_rIIC_INTPEND);
  149. IICCON = temp;
  150. /* wait and check ACK */
  151. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  152. temp = IICCON;
  153. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  154. break;
  155. udelay(2000);
  156. }
  157. if (k==S3C44B0_IIC_TIMEOUT)
  158. return -1;
  159. buffer[j] = IICDS; /*save readed data*/
  160. } /*end for(j)*/
  161. /*
  162. reading the last data
  163. unset ACK generation
  164. */
  165. temp = IICCON & ~(S3C44B0X_rIIC_INTPEND | (1<<7));
  166. IICCON = temp;
  167. /* wait but NOT check ACK */
  168. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  169. temp = IICCON;
  170. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  171. break;
  172. udelay(2000);
  173. }
  174. if (k==S3C44B0_IIC_TIMEOUT)
  175. return -1;
  176. buffer[j] = IICDS; /*save readed data*/
  177. rIICSTAT = 0x90; /*master recv*/
  178. /* Write operation Terminate sending STOP */
  179. IICSTAT = rIICSTAT;
  180. /*Clear Int Pending Bit to RESUME*/
  181. temp = IICCON;
  182. IICCON = temp & (~S3C44B0X_rIIC_INTPEND);
  183. IICCON = IICCON | (1<<7); /*restore ACK generation*/
  184. return 0;
  185. }
  186. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  187. {
  188. int j, k;
  189. u32 rIICSTAT, temp;
  190. /*
  191. send the device offset
  192. */
  193. rIICSTAT = 0xD0;
  194. IICSTAT = rIICSTAT;
  195. IICDS = chip; /* this is a write operation... */
  196. rIICSTAT |= (1<<5);
  197. IICSTAT = rIICSTAT;
  198. IICCON = IICCON & ~(S3C44B0X_rIIC_INTPEND);
  199. /* wait and check ACK */
  200. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  201. temp = IICCON;
  202. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  203. break;
  204. udelay(2000);
  205. }
  206. if (k==S3C44B0_IIC_TIMEOUT)
  207. return -1;
  208. temp = IICSTAT;
  209. if ((temp & S3C44B0X_rIIC_LAST_RECEIV_BIT) == S3C44B0X_rIIC_LAST_RECEIV_BIT )
  210. return -1;
  211. IICDS = addr;
  212. IICCON = IICCON & ~(S3C44B0X_rIIC_INTPEND);
  213. /* wait and check ACK */
  214. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  215. temp = IICCON;
  216. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  217. break;
  218. udelay(2000);
  219. }
  220. if (k==S3C44B0_IIC_TIMEOUT)
  221. return -1;
  222. temp = IICSTAT;
  223. if ((temp & S3C44B0X_rIIC_LAST_RECEIV_BIT) == S3C44B0X_rIIC_LAST_RECEIV_BIT )
  224. return -1;
  225. /*
  226. now we can start with the read write operation
  227. */
  228. for (j=0; j<len; j++) {
  229. IICDS = buffer[j]; /*prerare data to write*/
  230. /*clear pending bit to resume*/
  231. temp = IICCON & ~(S3C44B0X_rIIC_INTPEND);
  232. IICCON = temp;
  233. /* wait but NOT check ACK */
  234. for(k=0; k<S3C44B0_IIC_TIMEOUT; k++) {
  235. temp = IICCON;
  236. if( (temp & S3C44B0X_rIIC_INTPEND) == S3C44B0X_rIIC_INTPEND)
  237. break;
  238. udelay(2000);
  239. }
  240. if (k==S3C44B0_IIC_TIMEOUT)
  241. return -1;
  242. } /* end for(j) */
  243. /* sending stop to terminate */
  244. rIICSTAT = 0xD0; /*master send*/
  245. IICSTAT = rIICSTAT;
  246. /*Clear Int Pending Bit to RESUME*/
  247. temp = IICCON;
  248. IICCON = temp & (~S3C44B0X_rIIC_INTPEND);
  249. return 0;
  250. }