s3c24x0_i2c.c 12 KB

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