s3c24x0_i2c.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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_MR | I2C_TXRX_ENA, &i2c->iicstat);
  296. ReadWriteByte(i2c);
  297. break;
  298. case I2C_READ:
  299. if (addr && addr_len) {
  300. writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
  301. writel(chip, &i2c->iicds);
  302. /* send START */
  303. writel(readl(&i2c->iicstat) | I2C_START_STOP,
  304. &i2c->iicstat);
  305. result = WaitForXfer(i2c);
  306. if (IsACK(i2c)) {
  307. i = 0;
  308. while ((i < addr_len) && (result == I2C_OK)) {
  309. writel(addr[i], &i2c->iicds);
  310. ReadWriteByte(i2c);
  311. result = WaitForXfer(i2c);
  312. i++;
  313. }
  314. writel(chip, &i2c->iicds);
  315. /* resend START */
  316. writel(I2C_MODE_MR | I2C_TXRX_ENA |
  317. I2C_START_STOP, &i2c->iicstat);
  318. ReadWriteByte(i2c);
  319. result = WaitForXfer(i2c);
  320. i = 0;
  321. while ((i < data_len) && (result == I2C_OK)) {
  322. /* disable ACK for final READ */
  323. if (i == data_len - 1)
  324. writel(readl(&i2c->iiccon)
  325. & ~I2CCON_ACKGEN,
  326. &i2c->iiccon);
  327. ReadWriteByte(i2c);
  328. result = WaitForXfer(i2c);
  329. data[i] = readl(&i2c->iicds);
  330. i++;
  331. }
  332. } else {
  333. result = I2C_NACK;
  334. }
  335. } else {
  336. writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
  337. writel(chip, &i2c->iicds);
  338. /* send START */
  339. writel(readl(&i2c->iicstat) | I2C_START_STOP,
  340. &i2c->iicstat);
  341. result = WaitForXfer(i2c);
  342. if (IsACK(i2c)) {
  343. i = 0;
  344. while ((i < data_len) && (result == I2C_OK)) {
  345. /* disable ACK for final READ */
  346. if (i == data_len - 1)
  347. writel(readl(&i2c->iiccon) &
  348. ~I2CCON_ACKGEN,
  349. &i2c->iiccon);
  350. ReadWriteByte(i2c);
  351. result = WaitForXfer(i2c);
  352. data[i] = readl(&i2c->iicds);
  353. i++;
  354. }
  355. } else {
  356. result = I2C_NACK;
  357. }
  358. }
  359. /* send STOP */
  360. writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
  361. ReadWriteByte(i2c);
  362. break;
  363. default:
  364. debug("i2c_transfer: bad call\n");
  365. result = I2C_NOK;
  366. break;
  367. }
  368. return result;
  369. }
  370. int i2c_probe(uchar chip)
  371. {
  372. struct s3c24x0_i2c *i2c;
  373. uchar buf[1];
  374. i2c = get_base_i2c();
  375. buf[0] = 0;
  376. /*
  377. * What is needed is to send the chip address and verify that the
  378. * address was <ACK>ed (i.e. there was a chip at that address which
  379. * drove the data line low).
  380. */
  381. return i2c_transfer(i2c, I2C_READ, chip << 1, 0, 0, buf, 1) != I2C_OK;
  382. }
  383. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  384. {
  385. struct s3c24x0_i2c *i2c;
  386. uchar xaddr[4];
  387. int ret;
  388. if (alen > 4) {
  389. debug("I2C read: addr len %d not supported\n", alen);
  390. return 1;
  391. }
  392. if (alen > 0) {
  393. xaddr[0] = (addr >> 24) & 0xFF;
  394. xaddr[1] = (addr >> 16) & 0xFF;
  395. xaddr[2] = (addr >> 8) & 0xFF;
  396. xaddr[3] = addr & 0xFF;
  397. }
  398. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  399. /*
  400. * EEPROM chips that implement "address overflow" are ones
  401. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  402. * address and the extra bits end up in the "chip address"
  403. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  404. * four 256 byte chips.
  405. *
  406. * Note that we consider the length of the address field to
  407. * still be one byte because the extra address bits are
  408. * hidden in the chip address.
  409. */
  410. if (alen > 0)
  411. chip |= ((addr >> (alen * 8)) &
  412. CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  413. #endif
  414. i2c = get_base_i2c();
  415. ret = i2c_transfer(i2c, I2C_READ, chip << 1, &xaddr[4 - alen], alen,
  416. buffer, len);
  417. if (ret != 0) {
  418. debug("I2c read: failed %d\n", ret);
  419. return 1;
  420. }
  421. return 0;
  422. }
  423. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  424. {
  425. struct s3c24x0_i2c *i2c;
  426. uchar xaddr[4];
  427. if (alen > 4) {
  428. debug("I2C write: addr len %d not supported\n", alen);
  429. return 1;
  430. }
  431. if (alen > 0) {
  432. xaddr[0] = (addr >> 24) & 0xFF;
  433. xaddr[1] = (addr >> 16) & 0xFF;
  434. xaddr[2] = (addr >> 8) & 0xFF;
  435. xaddr[3] = addr & 0xFF;
  436. }
  437. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  438. /*
  439. * EEPROM chips that implement "address overflow" are ones
  440. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  441. * address and the extra bits end up in the "chip address"
  442. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  443. * four 256 byte chips.
  444. *
  445. * Note that we consider the length of the address field to
  446. * still be one byte because the extra address bits are
  447. * hidden in the chip address.
  448. */
  449. if (alen > 0)
  450. chip |= ((addr >> (alen * 8)) &
  451. CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  452. #endif
  453. i2c = get_base_i2c();
  454. return (i2c_transfer
  455. (i2c, I2C_WRITE, chip << 1, &xaddr[4 - alen], alen, buffer,
  456. len) != 0);
  457. }
  458. #ifdef CONFIG_OF_CONTROL
  459. void board_i2c_init(const void *blob)
  460. {
  461. int node_list[CONFIG_MAX_I2C_NUM];
  462. int count, i;
  463. count = fdtdec_find_aliases_for_id(blob, "i2c",
  464. COMPAT_SAMSUNG_S3C2440_I2C, node_list,
  465. CONFIG_MAX_I2C_NUM);
  466. for (i = 0; i < count; i++) {
  467. struct s3c24x0_i2c_bus *bus;
  468. int node = node_list[i];
  469. if (node <= 0)
  470. continue;
  471. bus = &i2c_bus[i];
  472. bus->regs = (struct s3c24x0_i2c *)
  473. fdtdec_get_addr(blob, node, "reg");
  474. bus->id = pinmux_decode_periph_id(blob, node);
  475. bus->node = node;
  476. bus->bus_num = i2c_busses++;
  477. exynos_pinmux_config(bus->id, 0);
  478. }
  479. }
  480. static struct s3c24x0_i2c_bus *get_bus(unsigned int bus_idx)
  481. {
  482. if (bus_idx < i2c_busses)
  483. return &i2c_bus[bus_idx];
  484. debug("Undefined bus: %d\n", bus_idx);
  485. return NULL;
  486. }
  487. int i2c_get_bus_num_fdt(int node)
  488. {
  489. int i;
  490. for (i = 0; i < i2c_busses; i++) {
  491. if (node == i2c_bus[i].node)
  492. return i;
  493. }
  494. debug("%s: Can't find any matched I2C bus\n", __func__);
  495. return -1;
  496. }
  497. int i2c_reset_port_fdt(const void *blob, int node)
  498. {
  499. struct s3c24x0_i2c_bus *i2c;
  500. int bus;
  501. bus = i2c_get_bus_num_fdt(node);
  502. if (bus < 0) {
  503. debug("could not get bus for node %d\n", node);
  504. return -1;
  505. }
  506. i2c = get_bus(bus);
  507. if (!i2c) {
  508. debug("get_bus() failed for node node %d\n", node);
  509. return -1;
  510. }
  511. i2c_ch_init(i2c->regs, CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  512. return 0;
  513. }
  514. #endif
  515. #endif /* CONFIG_HARD_I2C */