PageRenderTime 26ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/i2c/s3c24x0_i2c.c

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