s3c24x0_i2c.c 13 KB

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