s3c24x0_i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. #include <asm/arch/s3c24x0_cpu.h>
  29. #include <asm/io.h>
  30. #include <i2c.h>
  31. #ifdef CONFIG_HARD_I2C
  32. #define I2C_WRITE 0
  33. #define I2C_READ 1
  34. #define I2C_OK 0
  35. #define I2C_NOK 1
  36. #define I2C_NACK 2
  37. #define I2C_NOK_LA 3 /* Lost arbitration */
  38. #define I2C_NOK_TOUT 4 /* time out */
  39. #define I2CSTAT_BSY 0x20 /* Busy bit */
  40. #define I2CSTAT_NACK 0x01 /* Nack bit */
  41. #define I2CCON_IRPND 0x10 /* Interrupt pending bit */
  42. #define I2C_MODE_MT 0xC0 /* Master Transmit Mode */
  43. #define I2C_MODE_MR 0x80 /* Master Receive Mode */
  44. #define I2C_START_STOP 0x20 /* START / STOP */
  45. #define I2C_TXRX_ENA 0x10 /* I2C Tx/Rx enable */
  46. #define I2C_TIMEOUT 1 /* 1 second */
  47. static int GetI2CSDA(void)
  48. {
  49. struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
  50. #ifdef CONFIG_S3C2410
  51. return (readl(&gpio->GPEDAT) & 0x8000) >> 15;
  52. #endif
  53. #ifdef CONFIG_S3C2400
  54. return (readl(&gpio->PGDAT) & 0x0020) >> 5;
  55. #endif
  56. }
  57. #if 0
  58. static void SetI2CSDA(int x)
  59. {
  60. rGPEDAT = (rGPEDAT & ~0x8000) | (x & 1) << 15;
  61. }
  62. #endif
  63. static void SetI2CSCL(int x)
  64. {
  65. struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
  66. #ifdef CONFIG_S3C2410
  67. writel((readl(&gpio->GPEDAT) & ~0x4000) | (x & 1) << 14, &gpio->GPEDAT);
  68. #endif
  69. #ifdef CONFIG_S3C2400
  70. writel((readl(&gpio->PGDAT) & ~0x0040) | (x & 1) << 6, &gpio->PGDAT);
  71. #endif
  72. }
  73. static int WaitForXfer(void)
  74. {
  75. struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c();
  76. int i;
  77. i = I2C_TIMEOUT * 10000;
  78. while (!(readl(&i2c->IICCON) & I2CCON_IRPND) && (i > 0)) {
  79. udelay(100);
  80. i--;
  81. }
  82. return (readl(&i2c->IICCON) & I2CCON_IRPND) ? I2C_OK : I2C_NOK_TOUT;
  83. }
  84. static int IsACK(void)
  85. {
  86. struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c();
  87. return !(readl(&i2c->IICSTAT) & I2CSTAT_NACK);
  88. }
  89. static void ReadWriteByte(void)
  90. {
  91. struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c();
  92. writel(readl(&i2c->IICCON) & ~I2CCON_IRPND, &i2c->IICCON);
  93. }
  94. void i2c_init(int speed, int slaveadd)
  95. {
  96. struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c();
  97. struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
  98. ulong freq, pres = 16, div;
  99. int i;
  100. /* wait for some time to give previous transfer a chance to finish */
  101. i = I2C_TIMEOUT * 1000;
  102. while ((readl(&i2c->IICSTAT) && I2CSTAT_BSY) && (i > 0)) {
  103. udelay(1000);
  104. i--;
  105. }
  106. if ((readl(&i2c->IICSTAT) & I2CSTAT_BSY) || GetI2CSDA() == 0) {
  107. #ifdef CONFIG_S3C2410
  108. ulong old_gpecon = readl(&gpio->GPECON);
  109. #endif
  110. #ifdef CONFIG_S3C2400
  111. ulong old_gpecon = readl(&gpio->PGCON);
  112. #endif
  113. /* bus still busy probably by (most) previously interrupted
  114. transfer */
  115. #ifdef CONFIG_S3C2410
  116. /* set I2CSDA and I2CSCL (GPE15, GPE14) to GPIO */
  117. writel((readl(&gpio->GPECON) & ~0xF0000000) | 0x10000000,
  118. &gpio->GPECON);
  119. #endif
  120. #ifdef CONFIG_S3C2400
  121. /* set I2CSDA and I2CSCL (PG5, PG6) to GPIO */
  122. writel((readl(&gpio->PGCON) & ~0x00003c00) | 0x00001000,
  123. &gpio->PGCON);
  124. #endif
  125. /* toggle I2CSCL until bus idle */
  126. SetI2CSCL(0);
  127. udelay(1000);
  128. i = 10;
  129. while ((i > 0) && (GetI2CSDA() != 1)) {
  130. SetI2CSCL(1);
  131. udelay(1000);
  132. SetI2CSCL(0);
  133. udelay(1000);
  134. i--;
  135. }
  136. SetI2CSCL(1);
  137. udelay(1000);
  138. /* restore pin functions */
  139. #ifdef CONFIG_S3C2410
  140. writel(old_gpecon, &gpio->GPECON);
  141. #endif
  142. #ifdef CONFIG_S3C2400
  143. writel(old_gpecon, &gpio->PGCON);
  144. #endif
  145. }
  146. /* calculate prescaler and divisor values */
  147. freq = get_PCLK();
  148. if ((freq / pres / (16 + 1)) > speed)
  149. /* set prescaler to 512 */
  150. pres = 512;
  151. div = 0;
  152. while ((freq / pres / (div + 1)) > speed)
  153. div++;
  154. /* set prescaler, divisor according to freq, also set
  155. * ACKGEN, IRQ */
  156. writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->IICCON);
  157. /* init to SLAVE REVEIVE and set slaveaddr */
  158. writel(0, &i2c->IICSTAT);
  159. writel(slaveadd, &i2c->IICADD);
  160. /* program Master Transmit (and implicit STOP) */
  161. writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->IICSTAT);
  162. }
  163. /*
  164. * cmd_type is 0 for write, 1 for read.
  165. *
  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[], unsigned short data_len)
  176. {
  177. struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c();
  178. int i, result;
  179. if (data == 0 || data_len == 0) {
  180. /*Don't support data transfer of no length or to address 0 */
  181. printf("i2c_transfer: bad call\n");
  182. return I2C_NOK;
  183. }
  184. /* Check I2C bus idle */
  185. i = I2C_TIMEOUT * 1000;
  186. while ((readl(&i2c->IICSTAT) & I2CSTAT_BSY) && (i > 0)) {
  187. udelay(1000);
  188. i--;
  189. }
  190. if (readl(&i2c->IICSTAT) & I2CSTAT_BSY)
  191. return I2C_NOK_TOUT;
  192. writel(readl(&i2c->IICCON) | 0x80, &i2c->IICCON);
  193. result = I2C_OK;
  194. switch (cmd_type) {
  195. case I2C_WRITE:
  196. if (addr && addr_len) {
  197. writel(chip, &i2c->IICDS);
  198. /* send START */
  199. writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
  200. &i2c->IICSTAT);
  201. i = 0;
  202. while ((i < addr_len) && (result == I2C_OK)) {
  203. result = WaitForXfer();
  204. writel(addr[i], &i2c->IICDS);
  205. ReadWriteByte();
  206. i++;
  207. }
  208. i = 0;
  209. while ((i < data_len) && (result == I2C_OK)) {
  210. result = WaitForXfer();
  211. writel(data[i], &i2c->IICDS);
  212. ReadWriteByte();
  213. i++;
  214. }
  215. } else {
  216. writel(chip, &i2c->IICDS);
  217. /* send START */
  218. writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
  219. &i2c->IICSTAT);
  220. i = 0;
  221. while ((i < data_len) && (result = I2C_OK)) {
  222. result = WaitForXfer();
  223. writel(data[i], &i2c->IICDS);
  224. ReadWriteByte();
  225. i++;
  226. }
  227. }
  228. if (result == I2C_OK)
  229. result = WaitForXfer();
  230. /* send STOP */
  231. writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->IICSTAT);
  232. ReadWriteByte();
  233. break;
  234. case I2C_READ:
  235. if (addr && addr_len) {
  236. writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->IICSTAT);
  237. writel(chip, &i2c->IICDS);
  238. /* send START */
  239. writel(readl(&i2c->IICSTAT) | I2C_START_STOP,
  240. &i2c->IICSTAT);
  241. result = WaitForXfer();
  242. if (IsACK()) {
  243. i = 0;
  244. while ((i < addr_len) && (result == I2C_OK)) {
  245. writel(addr[i], &i2c->IICDS);
  246. ReadWriteByte();
  247. result = WaitForXfer();
  248. i++;
  249. }
  250. writel(chip, &i2c->IICDS);
  251. /* resend START */
  252. writel(I2C_MODE_MR | I2C_TXRX_ENA |
  253. I2C_START_STOP, &i2c->IICSTAT);
  254. ReadWriteByte();
  255. result = WaitForXfer();
  256. i = 0;
  257. while ((i < data_len) && (result == I2C_OK)) {
  258. /* disable ACK for final READ */
  259. if (i == data_len - 1)
  260. writel(readl(&i2c->IICCON)
  261. & ~0x80, &i2c->IICCON);
  262. ReadWriteByte();
  263. result = WaitForXfer();
  264. data[i] = readl(&i2c->IICDS);
  265. i++;
  266. }
  267. } else {
  268. result = I2C_NACK;
  269. }
  270. } else {
  271. writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->IICSTAT);
  272. writel(chip, &i2c->IICDS);
  273. /* send START */
  274. writel(readl(&i2c->IICSTAT) | I2C_START_STOP,
  275. &i2c->IICSTAT);
  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. writel(readl(&i2c->IICCON) &
  283. ~0x80, &i2c->IICCON);
  284. ReadWriteByte();
  285. result = WaitForXfer();
  286. data[i] = readl(&i2c->IICDS);
  287. i++;
  288. }
  289. } else {
  290. result = I2C_NACK;
  291. }
  292. }
  293. /* send STOP */
  294. writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->IICSTAT);
  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)) &
  343. CONFIG_SYS_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 CONFIG_SYS_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)) &
  380. CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  381. #endif
  382. return (i2c_transfer
  383. (I2C_WRITE, chip << 1, &xaddr[4 - alen], alen, buffer,
  384. len) != 0);
  385. }
  386. #endif /* CONFIG_HARD_I2C */