s3c24x0_i2c.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * (C) Copyright 2002
  3. * David Mueller, ELSOFT AG, d.mueller@elsoft.ch
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /* This code should work for both the S3C2400 and the S3C2410
  24. * as they seem to have the same I2C controller inside.
  25. * The different address mapping is handled by the s3c24xx.h files below.
  26. */
  27. #include <common.h>
  28. #ifdef CONFIG_DRIVER_S3C24X0_I2C
  29. #if defined(CONFIG_S3C2400)
  30. #include <s3c2400.h>
  31. #elif defined(CONFIG_S3C2410)
  32. #include <s3c2410.h>
  33. #endif
  34. #include <i2c.h>
  35. #ifdef CONFIG_HARD_I2C
  36. #define IIC_WRITE 0
  37. #define IIC_READ 1
  38. #define IIC_OK 0
  39. #define IIC_NOK 1
  40. #define IIC_NACK 2
  41. #define IIC_NOK_LA 3 /* Lost arbitration */
  42. #define IIC_NOK_TOUT 4 /* time out */
  43. #define IICSTAT_BSY 0x20 /* Busy bit */
  44. #define IICSTAT_NACK 0x01 /* Nack bit */
  45. #define IICCON_IRPND 0x10 /* Interrupt pending bit */
  46. #define IIC_MODE_MT 0xC0 /* Master Transmit Mode */
  47. #define IIC_MODE_MR 0x80 /* Master Receive Mode */
  48. #define IIC_START_STOP 0x20 /* START / STOP */
  49. #define IIC_TXRX_ENA 0x10 /* I2C Tx/Rx enable */
  50. #define IIC_TIMEOUT 1 /* 1 seconde */
  51. static int GetIICSDA(void)
  52. {
  53. return (rGPEDAT & 0x8000) >> 15;
  54. }
  55. #if 0
  56. static void SetIICSDA(int x)
  57. {
  58. rGPEDAT = (rGPEDAT & ~0x8000) | (x&1) << 15;
  59. }
  60. #endif
  61. static void SetIICSCL(int x)
  62. {
  63. rGPEDAT = (rGPEDAT & ~0x4000) | (x&1) << 14;
  64. }
  65. static int WaitForXfer(void)
  66. {
  67. int i, status;
  68. i = IIC_TIMEOUT * 1000;
  69. status = rIICCON;
  70. while ((i > 0) && !(status & IICCON_IRPND)) {
  71. udelay(1000);
  72. status = rIICCON;
  73. i--;
  74. }
  75. return(status & IICCON_IRPND) ? IIC_OK : IIC_NOK_TOUT;
  76. }
  77. static int IsACK(void)
  78. {
  79. return(!(rIICSTAT & IICSTAT_NACK));
  80. }
  81. static void ReadWriteByte(void)
  82. {
  83. rIICCON &= ~IICCON_IRPND;
  84. }
  85. void i2c_init (int speed, int slaveadd)
  86. {
  87. ulong freq, pres = 16, div;
  88. int i, status;
  89. /* wait for some time to give previous transfer a chance to finish */
  90. i = IIC_TIMEOUT * 1000;
  91. status = rIICSTAT;
  92. while ((i > 0) && (status & IICSTAT_BSY)) {
  93. udelay(1000);
  94. status = rIICSTAT;
  95. i--;
  96. }
  97. if ((status & IICSTAT_BSY) || GetIICSDA() == 0) {
  98. ulong old_gpecon = rGPECON;
  99. /* bus still busy probably by (most) previously interrupted transfer */
  100. /* set IICSDA and IICSCL (GPE15, GPE14) to GPIO */
  101. rGPECON = (rGPECON & ~0xF0000000) | 0x10000000;
  102. /* toggle IICSCL until bus idle */
  103. SetIICSCL(0); udelay(1000);
  104. i = 10;
  105. while ((i > 0) && (GetIICSDA() != 1)) {
  106. SetIICSCL(1); udelay(1000);
  107. SetIICSCL(0); udelay(1000);
  108. i--;
  109. }
  110. SetIICSCL(1); udelay(1000);
  111. /* restore pin functions */
  112. rGPECON = old_gpecon;
  113. }
  114. /* calculate prescaler and divisor values */
  115. freq = get_PCLK();
  116. if ((freq / pres / (16+1)) > speed)
  117. /* set prescaler to 512 */
  118. pres = 512;
  119. div = 0;
  120. while ((freq / pres / (div+1)) > speed)
  121. div++;
  122. /* set prescaler, divisor according to freq, also set
  123. ACKGEN, IRQ */
  124. rIICCON = (div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0);
  125. /* init to SLAVE REVEIVE and set slaveaddr */
  126. rIICSTAT = 0;
  127. rIICADD = slaveadd;
  128. /* program Master Transmit (and implicit STOP) */
  129. rIICSTAT = IIC_MODE_MT | IIC_TXRX_ENA;
  130. }
  131. /*
  132. cmd_type is 0 for write 1 for read.
  133. addr_len can take any value from 0-255, it is only limited
  134. by the char, we could make it larger if needed. If it is
  135. 0 we skip the address write cycle.
  136. */
  137. static
  138. int i2c_transfer(unsigned char cmd_type,
  139. unsigned char chip,
  140. unsigned char addr[],
  141. unsigned char addr_len,
  142. unsigned char data[],
  143. unsigned short data_len)
  144. {
  145. int i, status, result;
  146. if (data == 0 || data_len == 0) {
  147. /*Don't support data transfer of no length or to address 0*/
  148. printf( "i2c_transfer: bad call\n" );
  149. return IIC_NOK;
  150. }
  151. //CheckDelay();
  152. /* Check I2C bus idle */
  153. i = IIC_TIMEOUT * 1000;
  154. status = rIICSTAT;
  155. while ((i > 0) && (status & IICSTAT_BSY)) {
  156. udelay(1000);
  157. status = rIICSTAT;
  158. i--;
  159. }
  160. if (status & IICSTAT_BSY) {
  161. result = IIC_NOK_TOUT;
  162. return(result);
  163. }
  164. rIICCON |= 0x80;
  165. result = IIC_OK;
  166. switch (cmd_type) {
  167. case IIC_WRITE:
  168. if (addr && addr_len) {
  169. rIICDS = chip;
  170. /* send START */
  171. rIICSTAT = IIC_MODE_MT | IIC_TXRX_ENA | IIC_START_STOP;
  172. i = 0;
  173. while ((i < addr_len) && (result == IIC_OK)) {
  174. result = WaitForXfer();
  175. rIICDS = addr[i];
  176. ReadWriteByte();
  177. i++;
  178. }
  179. i = 0;
  180. while ((i < data_len) && (result == IIC_OK)) {
  181. result = WaitForXfer();
  182. rIICDS = data[i];
  183. ReadWriteByte();
  184. i++;
  185. }
  186. } else {
  187. rIICDS = chip;
  188. /* send START */
  189. rIICSTAT = IIC_MODE_MT | IIC_TXRX_ENA | IIC_START_STOP;
  190. i = 0;
  191. while ((i < data_len) && (result = IIC_OK)) {
  192. result = WaitForXfer();
  193. rIICDS = data[i];
  194. ReadWriteByte();
  195. i++;
  196. }
  197. }
  198. if (result == IIC_OK)
  199. result = WaitForXfer();
  200. /* send STOP */
  201. rIICSTAT = IIC_MODE_MR | IIC_TXRX_ENA;
  202. ReadWriteByte();
  203. break;
  204. case IIC_READ:
  205. if (addr && addr_len) {
  206. rIICSTAT = IIC_MODE_MT | IIC_TXRX_ENA;
  207. rIICDS = chip;
  208. /* send START */
  209. rIICSTAT |= IIC_START_STOP;
  210. result = WaitForXfer();
  211. if (IsACK()) {
  212. i = 0;
  213. while ((i < addr_len) && (result == IIC_OK)) {
  214. rIICDS = addr[i];
  215. ReadWriteByte();
  216. result = WaitForXfer();
  217. i++;
  218. }
  219. rIICDS = chip;
  220. /* resend START */
  221. rIICSTAT = IIC_MODE_MR | IIC_TXRX_ENA | IIC_START_STOP;
  222. ReadWriteByte();
  223. result = WaitForXfer();
  224. i = 0;
  225. while ((i < data_len) && (result == IIC_OK)) {
  226. /* disable ACK for final READ */
  227. if (i == data_len - 1)
  228. rIICCON &= ~0x80;
  229. ReadWriteByte();
  230. result = WaitForXfer();
  231. data[i] = rIICDS;
  232. i++;
  233. }
  234. } else {
  235. result = IIC_NACK;
  236. }
  237. } else {
  238. rIICSTAT = IIC_MODE_MR | IIC_TXRX_ENA;
  239. rIICDS = chip;
  240. /* send START */
  241. rIICSTAT |= IIC_START_STOP;
  242. result = WaitForXfer();
  243. if (IsACK()) {
  244. i = 0;
  245. while ((i < data_len) && (result == IIC_OK)) {
  246. /* disable ACK for final READ */
  247. if (i == data_len - 1)
  248. rIICCON &= ~0x80;
  249. ReadWriteByte();
  250. result = WaitForXfer();
  251. data[i] = rIICDS;
  252. i++;
  253. }
  254. } else {
  255. result = IIC_NACK;
  256. }
  257. }
  258. /* send STOP */
  259. rIICSTAT = IIC_MODE_MR | IIC_TXRX_ENA;
  260. ReadWriteByte();
  261. break;
  262. default:
  263. printf( "i2c_transfer: bad call\n" );
  264. result = IIC_NOK;
  265. break;
  266. }
  267. return (result);
  268. }
  269. int i2c_probe (uchar chip)
  270. {
  271. uchar buf[1];
  272. buf[0] = 0;
  273. /*
  274. * What is needed is to send the chip address and verify that the
  275. * address was <ACK>ed (i.e. there was a chip at that address which
  276. * drove the data line low).
  277. */
  278. return(i2c_transfer (IIC_READ, chip << 1, 0, 0, buf, 1) != IIC_OK);
  279. }
  280. int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len)
  281. {
  282. uchar xaddr[4];
  283. int ret;
  284. if ( alen > 4 ) {
  285. printf ("I2C read: addr len %d not supported\n", alen);
  286. return 1;
  287. }
  288. if ( alen > 0 ) {
  289. xaddr[0] = (addr >> 24) & 0xFF;
  290. xaddr[1] = (addr >> 16) & 0xFF;
  291. xaddr[2] = (addr >> 8) & 0xFF;
  292. xaddr[3] = addr & 0xFF;
  293. }
  294. #ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW
  295. /*
  296. * EEPROM chips that implement "address overflow" are ones
  297. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  298. * address and the extra bits end up in the "chip address"
  299. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  300. * four 256 byte chips.
  301. *
  302. * Note that we consider the length of the address field to
  303. * still be one byte because the extra address bits are
  304. * hidden in the chip address.
  305. */
  306. if( alen > 0 )
  307. chip |= ((addr >> (alen * 8)) & CFG_I2C_EEPROM_ADDR_OVERFLOW);
  308. #endif
  309. if( (ret = i2c_transfer(IIC_READ, chip<<1, &xaddr[4-alen], alen, buffer, len )) != 0) {
  310. printf( "I2c read: failed %d\n", ret);
  311. return 1;
  312. }
  313. return 0;
  314. }
  315. int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
  316. {
  317. uchar xaddr[4];
  318. if ( alen > 4 ) {
  319. printf ("I2C write: addr len %d not supported\n", alen);
  320. return 1;
  321. }
  322. if ( alen > 0 ) {
  323. xaddr[0] = (addr >> 24) & 0xFF;
  324. xaddr[1] = (addr >> 16) & 0xFF;
  325. xaddr[2] = (addr >> 8) & 0xFF;
  326. xaddr[3] = addr & 0xFF;
  327. }
  328. #ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW
  329. /*
  330. * EEPROM chips that implement "address overflow" are ones
  331. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  332. * address and the extra bits end up in the "chip address"
  333. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  334. * four 256 byte chips.
  335. *
  336. * Note that we consider the length of the address field to
  337. * still be one byte because the extra address bits are
  338. * hidden in the chip address.
  339. */
  340. if( alen > 0 )
  341. chip |= ((addr >> (alen * 8)) & CFG_I2C_EEPROM_ADDR_OVERFLOW);
  342. #endif
  343. return (i2c_transfer(IIC_WRITE, chip<<1, &xaddr[4-alen], alen, buffer, len ) != 0);
  344. }
  345. #endif /* CONFIG_HARD_I2C */
  346. #endif /* CONFIG_DRIVER_S3C24X0_I2C */