s3c24x0_i2c.c 14 KB

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