s3c24x0_i2c.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 second */
  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 * 10000;
  82. status = i2c->IICCON;
  83. while ((i > 0) && !(status & I2CCON_IRPND)) {
  84. udelay (100);
  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);
  132. udelay (1000);
  133. i = 10;
  134. while ((i > 0) && (GetI2CSDA () != 1)) {
  135. SetI2CSCL (1);
  136. udelay (1000);
  137. SetI2CSCL (0);
  138. udelay (1000);
  139. i--;
  140. }
  141. SetI2CSCL (1);
  142. udelay (1000);
  143. /* restore pin functions */
  144. #ifdef CONFIG_S3C2410
  145. gpio->GPECON = old_gpecon;
  146. #endif
  147. #ifdef CONFIG_S3C2400
  148. gpio->PGCON = old_gpecon;
  149. #endif
  150. }
  151. /* calculate prescaler and divisor values */
  152. freq = get_PCLK ();
  153. if ((freq / pres / (16 + 1)) > speed)
  154. /* set prescaler to 512 */
  155. pres = 512;
  156. div = 0;
  157. while ((freq / pres / (div + 1)) > speed)
  158. div++;
  159. /* set prescaler, divisor according to freq, also set
  160. * ACKGEN, IRQ */
  161. i2c->IICCON = (div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0);
  162. /* init to SLAVE REVEIVE and set slaveaddr */
  163. i2c->IICSTAT = 0;
  164. i2c->IICADD = slaveadd;
  165. /* program Master Transmit (and implicit STOP) */
  166. i2c->IICSTAT = I2C_MODE_MT | I2C_TXRX_ENA;
  167. }
  168. /*
  169. * cmd_type is 0 for write, 1 for read.
  170. *
  171. * addr_len can take any value from 0-255, it is only limited
  172. * by the char, we could make it larger if needed. If it is
  173. * 0 we skip the address write cycle.
  174. */
  175. static
  176. int i2c_transfer (unsigned char cmd_type,
  177. unsigned char chip,
  178. unsigned char addr[],
  179. unsigned char addr_len,
  180. unsigned char data[], unsigned short data_len)
  181. {
  182. S3C24X0_I2C *const i2c = S3C24X0_GetBase_I2C ();
  183. int i, status, result;
  184. if (data == 0 || data_len == 0) {
  185. /*Don't support data transfer of no length or to address 0 */
  186. printf ("i2c_transfer: bad call\n");
  187. return I2C_NOK;
  188. }
  189. /* Check I2C bus idle */
  190. i = I2C_TIMEOUT * 1000;
  191. status = i2c->IICSTAT;
  192. while ((i > 0) && (status & I2CSTAT_BSY)) {
  193. udelay (1000);
  194. status = i2c->IICSTAT;
  195. i--;
  196. }
  197. if (status & I2CSTAT_BSY)
  198. return I2C_NOK_TOUT;
  199. i2c->IICCON |= 0x80;
  200. result = I2C_OK;
  201. switch (cmd_type) {
  202. case I2C_WRITE:
  203. if (addr && addr_len) {
  204. i2c->IICDS = chip;
  205. /* send START */
  206. i2c->IICSTAT = I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP;
  207. i = 0;
  208. while ((i < addr_len) && (result == I2C_OK)) {
  209. result = WaitForXfer ();
  210. i2c->IICDS = addr[i];
  211. ReadWriteByte ();
  212. i++;
  213. }
  214. i = 0;
  215. while ((i < data_len) && (result == I2C_OK)) {
  216. result = WaitForXfer ();
  217. i2c->IICDS = data[i];
  218. ReadWriteByte ();
  219. i++;
  220. }
  221. } else {
  222. i2c->IICDS = chip;
  223. /* send START */
  224. i2c->IICSTAT = I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP;
  225. i = 0;
  226. while ((i < data_len) && (result = I2C_OK)) {
  227. result = WaitForXfer ();
  228. i2c->IICDS = data[i];
  229. ReadWriteByte ();
  230. i++;
  231. }
  232. }
  233. if (result == I2C_OK)
  234. result = WaitForXfer ();
  235. /* send STOP */
  236. i2c->IICSTAT = I2C_MODE_MR | I2C_TXRX_ENA;
  237. ReadWriteByte ();
  238. break;
  239. case I2C_READ:
  240. if (addr && addr_len) {
  241. i2c->IICSTAT = I2C_MODE_MT | I2C_TXRX_ENA;
  242. i2c->IICDS = chip;
  243. /* send START */
  244. i2c->IICSTAT |= I2C_START_STOP;
  245. result = WaitForXfer ();
  246. if (IsACK ()) {
  247. i = 0;
  248. while ((i < addr_len) && (result == I2C_OK)) {
  249. i2c->IICDS = addr[i];
  250. ReadWriteByte ();
  251. result = WaitForXfer ();
  252. i++;
  253. }
  254. i2c->IICDS = chip;
  255. /* resend START */
  256. i2c->IICSTAT = I2C_MODE_MR | I2C_TXRX_ENA |
  257. I2C_START_STOP;
  258. ReadWriteByte ();
  259. result = WaitForXfer ();
  260. i = 0;
  261. while ((i < data_len) && (result == I2C_OK)) {
  262. /* disable ACK for final READ */
  263. if (i == data_len - 1)
  264. i2c->IICCON &= ~0x80;
  265. ReadWriteByte ();
  266. result = WaitForXfer ();
  267. data[i] = i2c->IICDS;
  268. i++;
  269. }
  270. } else {
  271. result = I2C_NACK;
  272. }
  273. } else {
  274. i2c->IICSTAT = I2C_MODE_MR | I2C_TXRX_ENA;
  275. i2c->IICDS = chip;
  276. /* send START */
  277. i2c->IICSTAT |= I2C_START_STOP;
  278. result = WaitForXfer ();
  279. if (IsACK ()) {
  280. i = 0;
  281. while ((i < data_len) && (result == I2C_OK)) {
  282. /* disable ACK for final READ */
  283. if (i == data_len - 1)
  284. i2c->IICCON &= ~0x80;
  285. ReadWriteByte ();
  286. result = WaitForXfer ();
  287. data[i] = i2c->IICDS;
  288. i++;
  289. }
  290. } else {
  291. result = I2C_NACK;
  292. }
  293. }
  294. /* send STOP */
  295. i2c->IICSTAT = I2C_MODE_MR | I2C_TXRX_ENA;
  296. ReadWriteByte ();
  297. break;
  298. default:
  299. printf ("i2c_transfer: bad call\n");
  300. result = I2C_NOK;
  301. break;
  302. }
  303. return (result);
  304. }
  305. int i2c_probe (uchar chip)
  306. {
  307. uchar buf[1];
  308. buf[0] = 0;
  309. /*
  310. * What is needed is to send the chip address and verify that the
  311. * address was <ACK>ed (i.e. there was a chip at that address which
  312. * drove the data line low).
  313. */
  314. return (i2c_transfer (I2C_READ, chip << 1, 0, 0, buf, 1) != I2C_OK);
  315. }
  316. int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len)
  317. {
  318. uchar xaddr[4];
  319. int ret;
  320. if (alen > 4) {
  321. printf ("I2C read: addr len %d not supported\n", alen);
  322. return 1;
  323. }
  324. if (alen > 0) {
  325. xaddr[0] = (addr >> 24) & 0xFF;
  326. xaddr[1] = (addr >> 16) & 0xFF;
  327. xaddr[2] = (addr >> 8) & 0xFF;
  328. xaddr[3] = addr & 0xFF;
  329. }
  330. #ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW
  331. /*
  332. * EEPROM chips that implement "address overflow" are ones
  333. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  334. * address and the extra bits end up in the "chip address"
  335. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  336. * four 256 byte chips.
  337. *
  338. * Note that we consider the length of the address field to
  339. * still be one byte because the extra address bits are
  340. * hidden in the chip address.
  341. */
  342. if (alen > 0)
  343. chip |= ((addr >> (alen * 8)) & CFG_I2C_EEPROM_ADDR_OVERFLOW);
  344. #endif
  345. if ((ret =
  346. i2c_transfer (I2C_READ, chip << 1, &xaddr[4 - alen], alen,
  347. buffer, len)) != 0) {
  348. printf ("I2c read: failed %d\n", ret);
  349. return 1;
  350. }
  351. return 0;
  352. }
  353. int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
  354. {
  355. uchar xaddr[4];
  356. if (alen > 4) {
  357. printf ("I2C write: addr len %d not supported\n", alen);
  358. return 1;
  359. }
  360. if (alen > 0) {
  361. xaddr[0] = (addr >> 24) & 0xFF;
  362. xaddr[1] = (addr >> 16) & 0xFF;
  363. xaddr[2] = (addr >> 8) & 0xFF;
  364. xaddr[3] = addr & 0xFF;
  365. }
  366. #ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW
  367. /*
  368. * EEPROM chips that implement "address overflow" are ones
  369. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  370. * address and the extra bits end up in the "chip address"
  371. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  372. * four 256 byte chips.
  373. *
  374. * Note that we consider the length of the address field to
  375. * still be one byte because the extra address bits are
  376. * hidden in the chip address.
  377. */
  378. if (alen > 0)
  379. chip |= ((addr >> (alen * 8)) & CFG_I2C_EEPROM_ADDR_OVERFLOW);
  380. #endif
  381. return (i2c_transfer
  382. (I2C_WRITE, chip << 1, &xaddr[4 - alen], alen, buffer,
  383. len) != 0);
  384. }
  385. #endif /* CONFIG_HARD_I2C */
  386. #endif /* CONFIG_DRIVER_S3C24X0_I2C */