s3c24x0_i2c.c 10 KB

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