s3c24x0_i2c.c 14 KB

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