s3c24x0_i2c.c 11 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. #if defined(CONFIG_S3C2400)
  29. #include <s3c2400.h>
  30. #elif defined(CONFIG_S3C2410)
  31. #include <s3c2410.h>
  32. #endif
  33. #include <asm/io.h>
  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. struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
  54. #ifdef CONFIG_S3C2410
  55. return (readl(&gpio->GPEDAT) & 0x8000) >> 15;
  56. #endif
  57. #ifdef CONFIG_S3C2400
  58. return (readl(&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. struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
  70. #ifdef CONFIG_S3C2410
  71. writel((readl(&gpio->GPEDAT) & ~0x4000) | (x & 1) << 14, &gpio->GPEDAT);
  72. #endif
  73. #ifdef CONFIG_S3C2400
  74. writel((readl(&gpio->PGDAT) & ~0x0040) | (x & 1) << 6, &gpio->PGDAT);
  75. #endif
  76. }
  77. static int WaitForXfer(void)
  78. {
  79. struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c();
  80. int i;
  81. i = I2C_TIMEOUT * 10000;
  82. while (!(readl(&i2c->IICCON) & I2CCON_IRPND) && (i > 0)) {
  83. udelay(100);
  84. i--;
  85. }
  86. return (readl(&i2c->IICCON) & I2CCON_IRPND) ? I2C_OK : I2C_NOK_TOUT;
  87. }
  88. static int IsACK(void)
  89. {
  90. struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c();
  91. return !(readl(&i2c->IICSTAT) & I2CSTAT_NACK);
  92. }
  93. static void ReadWriteByte(void)
  94. {
  95. struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c();
  96. writel(readl(&i2c->IICCON) & ~I2CCON_IRPND, &i2c->IICCON);
  97. }
  98. void i2c_init(int speed, int slaveadd)
  99. {
  100. struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c();
  101. struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
  102. ulong freq, pres = 16, div;
  103. int i;
  104. /* wait for some time to give previous transfer a chance to finish */
  105. i = I2C_TIMEOUT * 1000;
  106. while ((readl(&i2c->IICSTAT) && I2CSTAT_BSY) && (i > 0)) {
  107. udelay(1000);
  108. i--;
  109. }
  110. if ((readl(&i2c->IICSTAT) & I2CSTAT_BSY) || GetI2CSDA() == 0) {
  111. #ifdef CONFIG_S3C2410
  112. ulong old_gpecon = readl(&gpio->GPECON);
  113. #endif
  114. #ifdef CONFIG_S3C2400
  115. ulong old_gpecon = readl(&gpio->PGCON);
  116. #endif
  117. /* bus still busy probably by (most) previously interrupted
  118. transfer */
  119. #ifdef CONFIG_S3C2410
  120. /* set I2CSDA and I2CSCL (GPE15, GPE14) to GPIO */
  121. writel((readl(&gpio->GPECON) & ~0xF0000000) | 0x10000000,
  122. &gpio->GPECON);
  123. #endif
  124. #ifdef CONFIG_S3C2400
  125. /* set I2CSDA and I2CSCL (PG5, PG6) to GPIO */
  126. writel((readl(&gpio->PGCON) & ~0x00003c00) | 0x00001000,
  127. &gpio->PGCON);
  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. writel(old_gpecon, &gpio->GPECON);
  145. #endif
  146. #ifdef CONFIG_S3C2400
  147. writel(old_gpecon, &gpio->PGCON);
  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. writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->IICCON);
  161. /* init to SLAVE REVEIVE and set slaveaddr */
  162. writel(0, &i2c->IICSTAT);
  163. writel(slaveadd, &i2c->IICADD);
  164. /* program Master Transmit (and implicit STOP) */
  165. writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->IICSTAT);
  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. struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c();
  182. int i, 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. while ((readl(&i2c->IICSTAT) & I2CSTAT_BSY) && (i > 0)) {
  191. udelay(1000);
  192. i--;
  193. }
  194. if (readl(&i2c->IICSTAT) & I2CSTAT_BSY)
  195. return I2C_NOK_TOUT;
  196. writel(readl(&i2c->IICCON) | 0x80, &i2c->IICCON);
  197. result = I2C_OK;
  198. switch (cmd_type) {
  199. case I2C_WRITE:
  200. if (addr && addr_len) {
  201. writel(chip, &i2c->IICDS);
  202. /* send START */
  203. writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
  204. &i2c->IICSTAT);
  205. i = 0;
  206. while ((i < addr_len) && (result == I2C_OK)) {
  207. result = WaitForXfer();
  208. writel(addr[i], &i2c->IICDS);
  209. ReadWriteByte();
  210. i++;
  211. }
  212. i = 0;
  213. while ((i < data_len) && (result == I2C_OK)) {
  214. result = WaitForXfer();
  215. writel(data[i], &i2c->IICDS);
  216. ReadWriteByte();
  217. i++;
  218. }
  219. } else {
  220. writel(chip, &i2c->IICDS);
  221. /* send START */
  222. writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
  223. &i2c->IICSTAT);
  224. i = 0;
  225. while ((i < data_len) && (result = I2C_OK)) {
  226. result = WaitForXfer();
  227. writel(data[i], &i2c->IICDS);
  228. ReadWriteByte();
  229. i++;
  230. }
  231. }
  232. if (result == I2C_OK)
  233. result = WaitForXfer();
  234. /* send STOP */
  235. writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->IICSTAT);
  236. ReadWriteByte();
  237. break;
  238. case I2C_READ:
  239. if (addr && addr_len) {
  240. writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->IICSTAT);
  241. writel(chip, &i2c->IICDS);
  242. /* send START */
  243. writel(readl(&i2c->IICSTAT) | I2C_START_STOP,
  244. &i2c->IICSTAT);
  245. result = WaitForXfer();
  246. if (IsACK()) {
  247. i = 0;
  248. while ((i < addr_len) && (result == I2C_OK)) {
  249. writel(addr[i], &i2c->IICDS);
  250. ReadWriteByte();
  251. result = WaitForXfer();
  252. i++;
  253. }
  254. writel(chip, &i2c->IICDS);
  255. /* resend START */
  256. writel(I2C_MODE_MR | I2C_TXRX_ENA |
  257. I2C_START_STOP, &i2c->IICSTAT);
  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. writel(readl(&i2c->IICCON)
  265. & ~0x80, &i2c->IICCON);
  266. ReadWriteByte();
  267. result = WaitForXfer();
  268. data[i] = readl(&i2c->IICDS);
  269. i++;
  270. }
  271. } else {
  272. result = I2C_NACK;
  273. }
  274. } else {
  275. writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->IICSTAT);
  276. writel(chip, &i2c->IICDS);
  277. /* send START */
  278. writel(readl(&i2c->IICSTAT) | I2C_START_STOP,
  279. &i2c->IICSTAT);
  280. result = WaitForXfer();
  281. if (IsACK()) {
  282. i = 0;
  283. while ((i < data_len) && (result == I2C_OK)) {
  284. /* disable ACK for final READ */
  285. if (i == data_len - 1)
  286. writel(readl(&i2c->IICCON) &
  287. ~0x80, &i2c->IICCON);
  288. ReadWriteByte();
  289. result = WaitForXfer();
  290. data[i] = readl(&i2c->IICDS);
  291. i++;
  292. }
  293. } else {
  294. result = I2C_NACK;
  295. }
  296. }
  297. /* send STOP */
  298. writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->IICSTAT);
  299. ReadWriteByte();
  300. break;
  301. default:
  302. printf("i2c_transfer: bad call\n");
  303. result = I2C_NOK;
  304. break;
  305. }
  306. return (result);
  307. }
  308. int i2c_probe(uchar chip)
  309. {
  310. uchar buf[1];
  311. buf[0] = 0;
  312. /*
  313. * What is needed is to send the chip address and verify that the
  314. * address was <ACK>ed (i.e. there was a chip at that address which
  315. * drove the data line low).
  316. */
  317. return i2c_transfer(I2C_READ, chip << 1, 0, 0, buf, 1) != I2C_OK;
  318. }
  319. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  320. {
  321. uchar xaddr[4];
  322. int ret;
  323. if (alen > 4) {
  324. printf("I2C read: addr len %d not supported\n", alen);
  325. return 1;
  326. }
  327. if (alen > 0) {
  328. xaddr[0] = (addr >> 24) & 0xFF;
  329. xaddr[1] = (addr >> 16) & 0xFF;
  330. xaddr[2] = (addr >> 8) & 0xFF;
  331. xaddr[3] = addr & 0xFF;
  332. }
  333. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  334. /*
  335. * EEPROM chips that implement "address overflow" are ones
  336. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  337. * address and the extra bits end up in the "chip address"
  338. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  339. * four 256 byte chips.
  340. *
  341. * Note that we consider the length of the address field to
  342. * still be one byte because the extra address bits are
  343. * hidden in the chip address.
  344. */
  345. if (alen > 0)
  346. chip |= ((addr >> (alen * 8)) &
  347. CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  348. #endif
  349. if ((ret =
  350. i2c_transfer(I2C_READ, chip << 1, &xaddr[4 - alen], alen,
  351. buffer, len)) != 0) {
  352. printf("I2c read: failed %d\n", ret);
  353. return 1;
  354. }
  355. return 0;
  356. }
  357. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  358. {
  359. uchar xaddr[4];
  360. if (alen > 4) {
  361. printf("I2C write: addr len %d not supported\n", alen);
  362. return 1;
  363. }
  364. if (alen > 0) {
  365. xaddr[0] = (addr >> 24) & 0xFF;
  366. xaddr[1] = (addr >> 16) & 0xFF;
  367. xaddr[2] = (addr >> 8) & 0xFF;
  368. xaddr[3] = addr & 0xFF;
  369. }
  370. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  371. /*
  372. * EEPROM chips that implement "address overflow" are ones
  373. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  374. * address and the extra bits end up in the "chip address"
  375. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  376. * four 256 byte chips.
  377. *
  378. * Note that we consider the length of the address field to
  379. * still be one byte because the extra address bits are
  380. * hidden in the chip address.
  381. */
  382. if (alen > 0)
  383. chip |= ((addr >> (alen * 8)) &
  384. CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  385. #endif
  386. return (i2c_transfer
  387. (I2C_WRITE, chip << 1, &xaddr[4 - alen], alen, buffer,
  388. len) != 0);
  389. }
  390. #endif /* CONFIG_HARD_I2C */