s3c24x0_i2c.c 14 KB

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