s3c24x0_i2c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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_EXYNOS5
  29. #include <asm/arch/clk.h>
  30. #include <asm/arch/cpu.h>
  31. #else
  32. #include <asm/arch/s3c24x0_cpu.h>
  33. #endif
  34. #include <asm/io.h>
  35. #include <i2c.h>
  36. #include "s3c24x0_i2c.h"
  37. #ifdef CONFIG_HARD_I2C
  38. #define I2C_WRITE 0
  39. #define I2C_READ 1
  40. #define I2C_OK 0
  41. #define I2C_NOK 1
  42. #define I2C_NACK 2
  43. #define I2C_NOK_LA 3 /* Lost arbitration */
  44. #define I2C_NOK_TOUT 4 /* time out */
  45. #define I2CSTAT_BSY 0x20 /* Busy bit */
  46. #define I2CSTAT_NACK 0x01 /* Nack bit */
  47. #define I2CCON_ACKGEN 0x80 /* Acknowledge generation */
  48. #define I2CCON_IRPND 0x10 /* Interrupt pending bit */
  49. #define I2C_MODE_MT 0xC0 /* Master Transmit Mode */
  50. #define I2C_MODE_MR 0x80 /* Master Receive Mode */
  51. #define I2C_START_STOP 0x20 /* START / STOP */
  52. #define I2C_TXRX_ENA 0x10 /* I2C Tx/Rx enable */
  53. #define I2C_TIMEOUT 1 /* 1 second */
  54. static unsigned int g_current_bus; /* Stores Current I2C Bus */
  55. #ifndef CONFIG_EXYNOS5
  56. static int GetI2CSDA(void)
  57. {
  58. struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
  59. #ifdef CONFIG_S3C2410
  60. return (readl(&gpio->gpedat) & 0x8000) >> 15;
  61. #endif
  62. #ifdef CONFIG_S3C2400
  63. return (readl(&gpio->pgdat) & 0x0020) >> 5;
  64. #endif
  65. }
  66. #if 0
  67. static void SetI2CSDA(int x)
  68. {
  69. rGPEDAT = (rGPEDAT & ~0x8000) | (x & 1) << 15;
  70. }
  71. #endif
  72. static void SetI2CSCL(int x)
  73. {
  74. struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
  75. #ifdef CONFIG_S3C2410
  76. writel((readl(&gpio->gpedat) & ~0x4000) |
  77. (x & 1) << 14, &gpio->gpedat);
  78. #endif
  79. #ifdef CONFIG_S3C2400
  80. writel((readl(&gpio->pgdat) & ~0x0040) | (x & 1) << 6, &gpio->pgdat);
  81. #endif
  82. }
  83. #endif
  84. static int WaitForXfer(struct s3c24x0_i2c *i2c)
  85. {
  86. int i;
  87. i = I2C_TIMEOUT * 10000;
  88. while (!(readl(&i2c->iiccon) & I2CCON_IRPND) && (i > 0)) {
  89. udelay(100);
  90. i--;
  91. }
  92. return (readl(&i2c->iiccon) & I2CCON_IRPND) ? I2C_OK : I2C_NOK_TOUT;
  93. }
  94. static int IsACK(struct s3c24x0_i2c *i2c)
  95. {
  96. return !(readl(&i2c->iicstat) & I2CSTAT_NACK);
  97. }
  98. static void ReadWriteByte(struct s3c24x0_i2c *i2c)
  99. {
  100. writel(readl(&i2c->iiccon) & ~I2CCON_IRPND, &i2c->iiccon);
  101. }
  102. static struct s3c24x0_i2c *get_base_i2c(void)
  103. {
  104. #ifdef CONFIG_EXYNOS5
  105. struct s3c24x0_i2c *i2c = (struct s3c24x0_i2c *)(samsung_get_base_i2c()
  106. + (EXYNOS5_I2C_SPACING
  107. * g_current_bus));
  108. return i2c;
  109. #else
  110. return s3c24x0_get_base_i2c();
  111. #endif
  112. }
  113. static void i2c_ch_init(struct s3c24x0_i2c *i2c, int speed, int slaveadd)
  114. {
  115. ulong freq, pres = 16, div;
  116. #ifdef CONFIG_EXYNOS5
  117. freq = get_i2c_clk();
  118. #else
  119. freq = get_PCLK();
  120. #endif
  121. /* calculate prescaler and divisor values */
  122. if ((freq / pres / (16 + 1)) > speed)
  123. /* set prescaler to 512 */
  124. pres = 512;
  125. div = 0;
  126. while ((freq / pres / (div + 1)) > speed)
  127. div++;
  128. /* set prescaler, divisor according to freq, also set ACKGEN, IRQ */
  129. writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon);
  130. /* init to SLAVE REVEIVE and set slaveaddr */
  131. writel(0, &i2c->iicstat);
  132. writel(slaveadd, &i2c->iicadd);
  133. /* program Master Transmit (and implicit STOP) */
  134. writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
  135. }
  136. /*
  137. * MULTI BUS I2C support
  138. */
  139. #ifdef CONFIG_I2C_MULTI_BUS
  140. int i2c_set_bus_num(unsigned int bus)
  141. {
  142. struct s3c24x0_i2c *i2c;
  143. if ((bus < 0) || (bus >= CONFIG_MAX_I2C_NUM)) {
  144. debug("Bad bus: %d\n", bus);
  145. return -1;
  146. }
  147. g_current_bus = bus;
  148. i2c = get_base_i2c();
  149. i2c_ch_init(i2c, CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  150. return 0;
  151. }
  152. unsigned int i2c_get_bus_num(void)
  153. {
  154. return g_current_bus;
  155. }
  156. #endif
  157. void i2c_init(int speed, int slaveadd)
  158. {
  159. struct s3c24x0_i2c *i2c;
  160. #ifndef CONFIG_EXYNOS5
  161. struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
  162. #endif
  163. int i;
  164. /* By default i2c channel 0 is the current bus */
  165. g_current_bus = 0;
  166. i2c = get_base_i2c();
  167. /* wait for some time to give previous transfer a chance to finish */
  168. i = I2C_TIMEOUT * 1000;
  169. while ((readl(&i2c->iicstat) & I2CSTAT_BSY) && (i > 0)) {
  170. udelay(1000);
  171. i--;
  172. }
  173. #ifndef CONFIG_EXYNOS5
  174. if ((readl(&i2c->iicstat) & I2CSTAT_BSY) || GetI2CSDA() == 0) {
  175. #ifdef CONFIG_S3C2410
  176. ulong old_gpecon = readl(&gpio->gpecon);
  177. #endif
  178. #ifdef CONFIG_S3C2400
  179. ulong old_gpecon = readl(&gpio->pgcon);
  180. #endif
  181. /* bus still busy probably by (most) previously interrupted
  182. transfer */
  183. #ifdef CONFIG_S3C2410
  184. /* set I2CSDA and I2CSCL (GPE15, GPE14) to GPIO */
  185. writel((readl(&gpio->gpecon) & ~0xF0000000) | 0x10000000,
  186. &gpio->gpecon);
  187. #endif
  188. #ifdef CONFIG_S3C2400
  189. /* set I2CSDA and I2CSCL (PG5, PG6) to GPIO */
  190. writel((readl(&gpio->pgcon) & ~0x00003c00) | 0x00001000,
  191. &gpio->pgcon);
  192. #endif
  193. /* toggle I2CSCL until bus idle */
  194. SetI2CSCL(0);
  195. udelay(1000);
  196. i = 10;
  197. while ((i > 0) && (GetI2CSDA() != 1)) {
  198. SetI2CSCL(1);
  199. udelay(1000);
  200. SetI2CSCL(0);
  201. udelay(1000);
  202. i--;
  203. }
  204. SetI2CSCL(1);
  205. udelay(1000);
  206. /* restore pin functions */
  207. #ifdef CONFIG_S3C2410
  208. writel(old_gpecon, &gpio->gpecon);
  209. #endif
  210. #ifdef CONFIG_S3C2400
  211. writel(old_gpecon, &gpio->pgcon);
  212. #endif
  213. }
  214. #endif /* #ifndef CONFIG_EXYNOS5 */
  215. i2c_ch_init(i2c, speed, slaveadd);
  216. }
  217. /*
  218. * cmd_type is 0 for write, 1 for read.
  219. *
  220. * addr_len can take any value from 0-255, it is only limited
  221. * by the char, we could make it larger if needed. If it is
  222. * 0 we skip the address write cycle.
  223. */
  224. static int i2c_transfer(struct s3c24x0_i2c *i2c,
  225. unsigned char cmd_type,
  226. unsigned char chip,
  227. unsigned char addr[],
  228. unsigned char addr_len,
  229. unsigned char data[],
  230. unsigned short data_len)
  231. {
  232. int i, result;
  233. if (data == 0 || data_len == 0) {
  234. /*Don't support data transfer of no length or to address 0 */
  235. debug("i2c_transfer: bad call\n");
  236. return I2C_NOK;
  237. }
  238. /* Check I2C bus idle */
  239. i = I2C_TIMEOUT * 1000;
  240. while ((readl(&i2c->iicstat) & I2CSTAT_BSY) && (i > 0)) {
  241. udelay(1000);
  242. i--;
  243. }
  244. if (readl(&i2c->iicstat) & I2CSTAT_BSY)
  245. return I2C_NOK_TOUT;
  246. writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon);
  247. result = I2C_OK;
  248. switch (cmd_type) {
  249. case I2C_WRITE:
  250. if (addr && addr_len) {
  251. writel(chip, &i2c->iicds);
  252. /* send START */
  253. writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
  254. &i2c->iicstat);
  255. i = 0;
  256. while ((i < addr_len) && (result == I2C_OK)) {
  257. result = WaitForXfer(i2c);
  258. writel(addr[i], &i2c->iicds);
  259. ReadWriteByte(i2c);
  260. i++;
  261. }
  262. i = 0;
  263. while ((i < data_len) && (result == I2C_OK)) {
  264. result = WaitForXfer(i2c);
  265. writel(data[i], &i2c->iicds);
  266. ReadWriteByte(i2c);
  267. i++;
  268. }
  269. } else {
  270. writel(chip, &i2c->iicds);
  271. /* send START */
  272. writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
  273. &i2c->iicstat);
  274. i = 0;
  275. while ((i < data_len) && (result = I2C_OK)) {
  276. result = WaitForXfer(i2c);
  277. writel(data[i], &i2c->iicds);
  278. ReadWriteByte(i2c);
  279. i++;
  280. }
  281. }
  282. if (result == I2C_OK)
  283. result = WaitForXfer(i2c);
  284. /* send STOP */
  285. writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
  286. ReadWriteByte(i2c);
  287. break;
  288. case I2C_READ:
  289. if (addr && addr_len) {
  290. writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
  291. writel(chip, &i2c->iicds);
  292. /* send START */
  293. writel(readl(&i2c->iicstat) | I2C_START_STOP,
  294. &i2c->iicstat);
  295. result = WaitForXfer(i2c);
  296. if (IsACK(i2c)) {
  297. i = 0;
  298. while ((i < addr_len) && (result == I2C_OK)) {
  299. writel(addr[i], &i2c->iicds);
  300. ReadWriteByte(i2c);
  301. result = WaitForXfer(i2c);
  302. i++;
  303. }
  304. writel(chip, &i2c->iicds);
  305. /* resend START */
  306. writel(I2C_MODE_MR | I2C_TXRX_ENA |
  307. I2C_START_STOP, &i2c->iicstat);
  308. ReadWriteByte(i2c);
  309. result = WaitForXfer(i2c);
  310. i = 0;
  311. while ((i < data_len) && (result == I2C_OK)) {
  312. /* disable ACK for final READ */
  313. if (i == data_len - 1)
  314. writel(readl(&i2c->iiccon)
  315. & ~I2CCON_ACKGEN,
  316. &i2c->iiccon);
  317. ReadWriteByte(i2c);
  318. result = WaitForXfer(i2c);
  319. data[i] = readl(&i2c->iicds);
  320. i++;
  321. }
  322. } else {
  323. result = I2C_NACK;
  324. }
  325. } else {
  326. writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
  327. writel(chip, &i2c->iicds);
  328. /* send START */
  329. writel(readl(&i2c->iicstat) | I2C_START_STOP,
  330. &i2c->iicstat);
  331. result = WaitForXfer(i2c);
  332. if (IsACK(i2c)) {
  333. i = 0;
  334. while ((i < data_len) && (result == I2C_OK)) {
  335. /* disable ACK for final READ */
  336. if (i == data_len - 1)
  337. writel(readl(&i2c->iiccon) &
  338. ~I2CCON_ACKGEN,
  339. &i2c->iiccon);
  340. ReadWriteByte(i2c);
  341. result = WaitForXfer(i2c);
  342. data[i] = readl(&i2c->iicds);
  343. i++;
  344. }
  345. } else {
  346. result = I2C_NACK;
  347. }
  348. }
  349. /* send STOP */
  350. writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
  351. ReadWriteByte(i2c);
  352. break;
  353. default:
  354. debug("i2c_transfer: bad call\n");
  355. result = I2C_NOK;
  356. break;
  357. }
  358. return result;
  359. }
  360. int i2c_probe(uchar chip)
  361. {
  362. struct s3c24x0_i2c *i2c;
  363. uchar buf[1];
  364. i2c = get_base_i2c();
  365. buf[0] = 0;
  366. /*
  367. * What is needed is to send the chip address and verify that the
  368. * address was <ACK>ed (i.e. there was a chip at that address which
  369. * drove the data line low).
  370. */
  371. return i2c_transfer(i2c, I2C_READ, chip << 1, 0, 0, buf, 1) != I2C_OK;
  372. }
  373. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  374. {
  375. struct s3c24x0_i2c *i2c;
  376. uchar xaddr[4];
  377. int ret;
  378. if (alen > 4) {
  379. debug("I2C read: addr len %d not supported\n", alen);
  380. return 1;
  381. }
  382. if (alen > 0) {
  383. xaddr[0] = (addr >> 24) & 0xFF;
  384. xaddr[1] = (addr >> 16) & 0xFF;
  385. xaddr[2] = (addr >> 8) & 0xFF;
  386. xaddr[3] = addr & 0xFF;
  387. }
  388. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  389. /*
  390. * EEPROM chips that implement "address overflow" are ones
  391. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  392. * address and the extra bits end up in the "chip address"
  393. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  394. * four 256 byte chips.
  395. *
  396. * Note that we consider the length of the address field to
  397. * still be one byte because the extra address bits are
  398. * hidden in the chip address.
  399. */
  400. if (alen > 0)
  401. chip |= ((addr >> (alen * 8)) &
  402. CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  403. #endif
  404. i2c = get_base_i2c();
  405. ret = i2c_transfer(i2c, I2C_READ, chip << 1, &xaddr[4 - alen], alen,
  406. buffer, len);
  407. if (ret != 0) {
  408. debug("I2c read: failed %d\n", ret);
  409. return 1;
  410. }
  411. return 0;
  412. }
  413. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  414. {
  415. struct s3c24x0_i2c *i2c;
  416. uchar xaddr[4];
  417. if (alen > 4) {
  418. debug("I2C write: addr len %d not supported\n", alen);
  419. return 1;
  420. }
  421. if (alen > 0) {
  422. xaddr[0] = (addr >> 24) & 0xFF;
  423. xaddr[1] = (addr >> 16) & 0xFF;
  424. xaddr[2] = (addr >> 8) & 0xFF;
  425. xaddr[3] = addr & 0xFF;
  426. }
  427. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  428. /*
  429. * EEPROM chips that implement "address overflow" are ones
  430. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  431. * address and the extra bits end up in the "chip address"
  432. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  433. * four 256 byte chips.
  434. *
  435. * Note that we consider the length of the address field to
  436. * still be one byte because the extra address bits are
  437. * hidden in the chip address.
  438. */
  439. if (alen > 0)
  440. chip |= ((addr >> (alen * 8)) &
  441. CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  442. #endif
  443. i2c = get_base_i2c();
  444. return (i2c_transfer
  445. (i2c, I2C_WRITE, chip << 1, &xaddr[4 - alen], alen, buffer,
  446. len) != 0);
  447. }
  448. #endif /* CONFIG_HARD_I2C */