s3c24x0_i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 I2C_WRITE 0
  37. #define I2C_READ 1
  38. #define I2C_OK 0
  39. #define I2C_NOK 1
  40. #define I2C_NACK 2
  41. #define I2C_NOK_LA 3 /* Lost arbitration */
  42. #define I2C_NOK_TOUT 4 /* time out */
  43. #define I2CSTAT_BSY 0x20 /* Busy bit */
  44. #define I2CSTAT_NACK 0x01 /* Nack bit */
  45. #define I2CCON_IRPND 0x10 /* Interrupt pending bit */
  46. #define I2C_MODE_MT 0xC0 /* Master Transmit Mode */
  47. #define I2C_MODE_MR 0x80 /* Master Receive Mode */
  48. #define I2C_START_STOP 0x20 /* START / STOP */
  49. #define I2C_TXRX_ENA 0x10 /* I2C Tx/Rx enable */
  50. #define I2C_TIMEOUT 1 /* 1 seconde */
  51. static int GetI2CSDA(void)
  52. {
  53. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  54. #ifdef CONFIG_S3C2410
  55. return (gpio->GPEDAT & 0x8000) >> 15;
  56. #endif
  57. #ifdef CONFIG_S3C2400
  58. return (gpio->PGDAT & 0x0020) >> 5;
  59. #endif
  60. }
  61. #if 0
  62. static void SetI2CSDA(int x)
  63. {
  64. rGPEDAT = (rGPEDAT & ~0x8000) | (x&1) << 15;
  65. }
  66. #endif
  67. static void SetI2CSCL(int x)
  68. {
  69. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  70. #ifdef CONFIG_S3C2410
  71. gpio->GPEDAT = (gpio->GPEDAT & ~0x4000) | (x&1) << 14;
  72. #endif
  73. #ifdef CONFIG_S3C2400
  74. gpio->PGDAT = (gpio->PGDAT & ~0x0040) | (x&1) << 6;
  75. #endif
  76. }
  77. static int WaitForXfer(void)
  78. {
  79. S3C24X0_I2C * const i2c = S3C24X0_GetBase_I2C();
  80. int i, status;
  81. i = I2C_TIMEOUT * 1000;
  82. status = i2c->IICCON;
  83. while ((i > 0) && !(status & I2CCON_IRPND)) {
  84. udelay(1000);
  85. status = i2c->IICCON;
  86. i--;
  87. }
  88. return(status & I2CCON_IRPND) ? I2C_OK : I2C_NOK_TOUT;
  89. }
  90. static int IsACK(void)
  91. {
  92. S3C24X0_I2C * const i2c = S3C24X0_GetBase_I2C();
  93. return(!(i2c->IICSTAT & I2CSTAT_NACK));
  94. }
  95. static void ReadWriteByte(void)
  96. {
  97. S3C24X0_I2C * const i2c = S3C24X0_GetBase_I2C();
  98. i2c->IICCON &= ~I2CCON_IRPND;
  99. }
  100. void i2c_init (int speed, int slaveadd)
  101. {
  102. S3C24X0_I2C * const i2c = S3C24X0_GetBase_I2C();
  103. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  104. ulong freq, pres = 16, div;
  105. int i, status;
  106. /* wait for some time to give previous transfer a chance to finish */
  107. i = I2C_TIMEOUT * 1000;
  108. status = i2c->IICSTAT;
  109. while ((i > 0) && (status & I2CSTAT_BSY)) {
  110. udelay(1000);
  111. status = i2c->IICSTAT;
  112. i--;
  113. }
  114. if ((status & I2CSTAT_BSY) || GetI2CSDA() == 0) {
  115. #ifdef CONFIG_S3C2410
  116. ulong old_gpecon = gpio->GPECON;
  117. #endif
  118. #ifdef CONFIG_S3C2400
  119. ulong old_gpecon = gpio->PGCON;
  120. #endif
  121. /* bus still busy probably by (most) previously interrupted transfer */
  122. #ifdef CONFIG_S3C2410
  123. /* set I2CSDA and I2CSCL (GPE15, GPE14) to GPIO */
  124. gpio->GPECON = (gpio->GPECON & ~0xF0000000) | 0x10000000;
  125. #endif
  126. #ifdef CONFIG_S3C2400
  127. /* set I2CSDA and I2CSCL (PG5, PG6) to GPIO */
  128. gpio->PGCON = (gpio->PGCON & ~0x00003c00) | 0x00000c00;
  129. #endif
  130. /* toggle I2CSCL until bus idle */
  131. SetI2CSCL(0); udelay(1000);
  132. i = 10;
  133. while ((i > 0) && (GetI2CSDA() != 1)) {
  134. SetI2CSCL(1); udelay(1000);
  135. SetI2CSCL(0); udelay(1000);
  136. i--;
  137. }
  138. SetI2CSCL(1); udelay(1000);
  139. /* restore pin functions */
  140. #ifdef CONFIG_S3C2410
  141. gpio->GPECON = old_gpecon;
  142. #endif
  143. #ifdef CONFIG_S3C2400
  144. gpio->PGCON = old_gpecon;
  145. #endif
  146. }
  147. /* calculate prescaler and divisor values */
  148. freq = get_PCLK();
  149. if ((freq / pres / (16+1)) > speed)
  150. /* set prescaler to 512 */
  151. pres = 512;
  152. div = 0;
  153. while ((freq / pres / (div+1)) > speed)
  154. div++;
  155. /* set prescaler, divisor according to freq, also set
  156. ACKGEN, IRQ */
  157. i2c->IICCON = (div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0);
  158. /* init to SLAVE REVEIVE and set slaveaddr */
  159. i2c->IICSTAT = 0;
  160. i2c->IICADD = slaveadd;
  161. /* program Master Transmit (and implicit STOP) */
  162. i2c->IICSTAT = I2C_MODE_MT | I2C_TXRX_ENA;
  163. }
  164. /*
  165. cmd_type is 0 for write 1 for read.
  166. addr_len can take any value from 0-255, it is only limited
  167. by the char, we could make it larger if needed. If it is
  168. 0 we skip the address write cycle.
  169. */
  170. static
  171. int i2c_transfer(unsigned char cmd_type,
  172. unsigned char chip,
  173. unsigned char addr[],
  174. unsigned char addr_len,
  175. unsigned char data[],
  176. unsigned short data_len)
  177. {
  178. S3C24X0_I2C * const i2c = S3C24X0_GetBase_I2C();
  179. int i, status, result;
  180. if (data == 0 || data_len == 0) {
  181. /*Don't support data transfer of no length or to address 0*/
  182. printf( "i2c_transfer: bad call\n" );
  183. return I2C_NOK;
  184. }
  185. /*CheckDelay(); */
  186. /* Check I2C bus idle */
  187. i = I2C_TIMEOUT * 1000;
  188. status = i2c->IICSTAT;
  189. while ((i > 0) && (status & I2CSTAT_BSY)) {
  190. udelay(1000);
  191. status = i2c->IICSTAT;
  192. i--;
  193. }
  194. if (status & I2CSTAT_BSY) {
  195. result = I2C_NOK_TOUT;
  196. return(result);
  197. }
  198. i2c->IICCON |= 0x80;
  199. result = I2C_OK;
  200. switch (cmd_type) {
  201. case I2C_WRITE:
  202. if (addr && addr_len) {
  203. i2c->IICDS = chip;
  204. /* send START */
  205. i2c->IICSTAT = I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP;
  206. i = 0;
  207. while ((i < addr_len) && (result == I2C_OK)) {
  208. result = WaitForXfer();
  209. i2c->IICDS = addr[i];
  210. ReadWriteByte();
  211. i++;
  212. }
  213. i = 0;
  214. while ((i < data_len) && (result == I2C_OK)) {
  215. result = WaitForXfer();
  216. i2c->IICDS = data[i];
  217. ReadWriteByte();
  218. i++;
  219. }
  220. } else {
  221. i2c->IICDS = chip;
  222. /* send START */
  223. i2c->IICSTAT = I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP;
  224. i = 0;
  225. while ((i < data_len) && (result = I2C_OK)) {
  226. result = WaitForXfer();
  227. i2c->IICDS = data[i];
  228. ReadWriteByte();
  229. i++;
  230. }
  231. }
  232. if (result == I2C_OK)
  233. result = WaitForXfer();
  234. /* send STOP */
  235. i2c->IICSTAT = I2C_MODE_MR | I2C_TXRX_ENA;
  236. ReadWriteByte();
  237. break;
  238. case I2C_READ:
  239. if (addr && addr_len) {
  240. i2c->IICSTAT = I2C_MODE_MT | I2C_TXRX_ENA;
  241. i2c->IICDS = chip;
  242. /* send START */
  243. i2c->IICSTAT |= I2C_START_STOP;
  244. result = WaitForXfer();
  245. if (IsACK()) {
  246. i = 0;
  247. while ((i < addr_len) && (result == I2C_OK)) {
  248. i2c->IICDS = addr[i];
  249. ReadWriteByte();
  250. result = WaitForXfer();
  251. i++;
  252. }
  253. i2c->IICDS = chip;
  254. /* resend START */
  255. i2c->IICSTAT = I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP;
  256. ReadWriteByte();
  257. result = WaitForXfer();
  258. i = 0;
  259. while ((i < data_len) && (result == I2C_OK)) {
  260. /* disable ACK for final READ */
  261. if (i == data_len - 1)
  262. i2c->IICCON &= ~0x80;
  263. ReadWriteByte();
  264. result = WaitForXfer();
  265. data[i] = i2c->IICDS;
  266. i++;
  267. }
  268. } else {
  269. result = I2C_NACK;
  270. }
  271. } else {
  272. i2c->IICSTAT = I2C_MODE_MR | I2C_TXRX_ENA;
  273. i2c->IICDS = chip;
  274. /* send START */
  275. i2c->IICSTAT |= I2C_START_STOP;
  276. result = WaitForXfer();
  277. if (IsACK()) {
  278. i = 0;
  279. while ((i < data_len) && (result == I2C_OK)) {
  280. /* disable ACK for final READ */
  281. if (i == data_len - 1)
  282. i2c->IICCON &= ~0x80;
  283. ReadWriteByte();
  284. result = WaitForXfer();
  285. data[i] = i2c->IICDS;
  286. i++;
  287. }
  288. } else {
  289. result = I2C_NACK;
  290. }
  291. }
  292. /* send STOP */
  293. i2c->IICSTAT = I2C_MODE_MR | I2C_TXRX_ENA;
  294. ReadWriteByte();
  295. break;
  296. default:
  297. printf( "i2c_transfer: bad call\n" );
  298. result = I2C_NOK;
  299. break;
  300. }
  301. return (result);
  302. }
  303. int i2c_probe (uchar chip)
  304. {
  305. uchar buf[1];
  306. buf[0] = 0;
  307. /*
  308. * What is needed is to send the chip address and verify that the
  309. * address was <ACK>ed (i.e. there was a chip at that address which
  310. * drove the data line low).
  311. */
  312. return(i2c_transfer (I2C_READ, chip << 1, 0, 0, buf, 1) != I2C_OK);
  313. }
  314. int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len)
  315. {
  316. uchar xaddr[4];
  317. int ret;
  318. if ( alen > 4 ) {
  319. printf ("I2C read: 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. if( (ret = i2c_transfer(I2C_READ, chip<<1, &xaddr[4-alen], alen, buffer, len )) != 0) {
  344. printf( "I2c read: failed %d\n", ret);
  345. return 1;
  346. }
  347. return 0;
  348. }
  349. int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
  350. {
  351. uchar xaddr[4];
  352. if ( alen > 4 ) {
  353. printf ("I2C write: addr len %d not supported\n", alen);
  354. return 1;
  355. }
  356. if ( alen > 0 ) {
  357. xaddr[0] = (addr >> 24) & 0xFF;
  358. xaddr[1] = (addr >> 16) & 0xFF;
  359. xaddr[2] = (addr >> 8) & 0xFF;
  360. xaddr[3] = addr & 0xFF;
  361. }
  362. #ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW
  363. /*
  364. * EEPROM chips that implement "address overflow" are ones
  365. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  366. * address and the extra bits end up in the "chip address"
  367. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  368. * four 256 byte chips.
  369. *
  370. * Note that we consider the length of the address field to
  371. * still be one byte because the extra address bits are
  372. * hidden in the chip address.
  373. */
  374. if( alen > 0 )
  375. chip |= ((addr >> (alen * 8)) & CFG_I2C_EEPROM_ADDR_OVERFLOW);
  376. #endif
  377. return (i2c_transfer(I2C_WRITE, chip<<1, &xaddr[4-alen], alen, buffer, len ) != 0);
  378. }
  379. #endif /* CONFIG_HARD_I2C */
  380. #endif /* CONFIG_DRIVER_S3C24X0_I2C */