exynos_hs_i2c.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * Copyright (c) 2016, Google Inc
  3. *
  4. * (C) Copyright 2002
  5. * David Mueller, ELSOFT AG, d.mueller@elsoft.ch
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <i2c.h>
  12. #include <asm/arch/clk.h>
  13. #include <asm/arch/cpu.h>
  14. #include <asm/arch/pinmux.h>
  15. #include "s3c24x0_i2c.h"
  16. DECLARE_GLOBAL_DATA_PTR;
  17. /* HSI2C-specific register description */
  18. /* I2C_CTL Register bits */
  19. #define HSI2C_FUNC_MODE_I2C (1u << 0)
  20. #define HSI2C_MASTER (1u << 3)
  21. #define HSI2C_RXCHON (1u << 6) /* Write/Send */
  22. #define HSI2C_TXCHON (1u << 7) /* Read/Receive */
  23. #define HSI2C_SW_RST (1u << 31)
  24. /* I2C_FIFO_CTL Register bits */
  25. #define HSI2C_RXFIFO_EN (1u << 0)
  26. #define HSI2C_TXFIFO_EN (1u << 1)
  27. #define HSI2C_TXFIFO_TRIGGER_LEVEL (0x20 << 16)
  28. #define HSI2C_RXFIFO_TRIGGER_LEVEL (0x20 << 4)
  29. /* I2C_TRAILING_CTL Register bits */
  30. #define HSI2C_TRAILING_COUNT (0xff)
  31. /* I2C_INT_EN Register bits */
  32. #define HSI2C_TX_UNDERRUN_EN (1u << 2)
  33. #define HSI2C_TX_OVERRUN_EN (1u << 3)
  34. #define HSI2C_RX_UNDERRUN_EN (1u << 4)
  35. #define HSI2C_RX_OVERRUN_EN (1u << 5)
  36. #define HSI2C_INT_TRAILING_EN (1u << 6)
  37. #define HSI2C_INT_I2C_EN (1u << 9)
  38. #define HSI2C_INT_ERROR_MASK (HSI2C_TX_UNDERRUN_EN |\
  39. HSI2C_TX_OVERRUN_EN |\
  40. HSI2C_RX_UNDERRUN_EN |\
  41. HSI2C_RX_OVERRUN_EN |\
  42. HSI2C_INT_TRAILING_EN)
  43. /* I2C_CONF Register bits */
  44. #define HSI2C_AUTO_MODE (1u << 31)
  45. #define HSI2C_10BIT_ADDR_MODE (1u << 30)
  46. #define HSI2C_HS_MODE (1u << 29)
  47. /* I2C_AUTO_CONF Register bits */
  48. #define HSI2C_READ_WRITE (1u << 16)
  49. #define HSI2C_STOP_AFTER_TRANS (1u << 17)
  50. #define HSI2C_MASTER_RUN (1u << 31)
  51. /* I2C_TIMEOUT Register bits */
  52. #define HSI2C_TIMEOUT_EN (1u << 31)
  53. /* I2C_TRANS_STATUS register bits */
  54. #define HSI2C_MASTER_BUSY (1u << 17)
  55. #define HSI2C_SLAVE_BUSY (1u << 16)
  56. #define HSI2C_TIMEOUT_AUTO (1u << 4)
  57. #define HSI2C_NO_DEV (1u << 3)
  58. #define HSI2C_NO_DEV_ACK (1u << 2)
  59. #define HSI2C_TRANS_ABORT (1u << 1)
  60. #define HSI2C_TRANS_SUCCESS (1u << 0)
  61. #define HSI2C_TRANS_ERROR_MASK (HSI2C_TIMEOUT_AUTO |\
  62. HSI2C_NO_DEV | HSI2C_NO_DEV_ACK |\
  63. HSI2C_TRANS_ABORT)
  64. #define HSI2C_TRANS_FINISHED_MASK (HSI2C_TRANS_ERROR_MASK | HSI2C_TRANS_SUCCESS)
  65. /* I2C_FIFO_STAT Register bits */
  66. #define HSI2C_RX_FIFO_EMPTY (1u << 24)
  67. #define HSI2C_RX_FIFO_FULL (1u << 23)
  68. #define HSI2C_TX_FIFO_EMPTY (1u << 8)
  69. #define HSI2C_TX_FIFO_FULL (1u << 7)
  70. #define HSI2C_RX_FIFO_LEVEL(x) (((x) >> 16) & 0x7f)
  71. #define HSI2C_TX_FIFO_LEVEL(x) ((x) & 0x7f)
  72. #define HSI2C_SLV_ADDR_MAS(x) ((x & 0x3ff) << 10)
  73. #define HSI2C_TIMEOUT_US 10000 /* 10 ms, finer granularity */
  74. /*
  75. * Wait for transfer completion.
  76. *
  77. * This function reads the interrupt status register waiting for the INT_I2C
  78. * bit to be set, which indicates copletion of a transaction.
  79. *
  80. * @param i2c: pointer to the appropriate register bank
  81. *
  82. * @return: I2C_OK in case of successful completion, I2C_NOK_TIMEOUT in case
  83. * the status bits do not get set in time, or an approrpiate error
  84. * value in case of transfer errors.
  85. */
  86. static int hsi2c_wait_for_trx(struct exynos5_hsi2c *i2c)
  87. {
  88. int i = HSI2C_TIMEOUT_US;
  89. while (i-- > 0) {
  90. u32 int_status = readl(&i2c->usi_int_stat);
  91. if (int_status & HSI2C_INT_I2C_EN) {
  92. u32 trans_status = readl(&i2c->usi_trans_status);
  93. /* Deassert pending interrupt. */
  94. writel(int_status, &i2c->usi_int_stat);
  95. if (trans_status & HSI2C_NO_DEV_ACK) {
  96. debug("%s: no ACK from device\n", __func__);
  97. return I2C_NACK;
  98. }
  99. if (trans_status & HSI2C_NO_DEV) {
  100. debug("%s: no device\n", __func__);
  101. return I2C_NOK;
  102. }
  103. if (trans_status & HSI2C_TRANS_ABORT) {
  104. debug("%s: arbitration lost\n", __func__);
  105. return I2C_NOK_LA;
  106. }
  107. if (trans_status & HSI2C_TIMEOUT_AUTO) {
  108. debug("%s: device timed out\n", __func__);
  109. return I2C_NOK_TOUT;
  110. }
  111. return I2C_OK;
  112. }
  113. udelay(1);
  114. }
  115. debug("%s: transaction timeout!\n", __func__);
  116. return I2C_NOK_TOUT;
  117. }
  118. static int hsi2c_get_clk_details(struct s3c24x0_i2c_bus *i2c_bus)
  119. {
  120. struct exynos5_hsi2c *hsregs = i2c_bus->hsregs;
  121. ulong clkin;
  122. unsigned int op_clk = i2c_bus->clock_frequency;
  123. unsigned int i = 0, utemp0 = 0, utemp1 = 0;
  124. unsigned int t_ftl_cycle;
  125. #if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
  126. clkin = get_i2c_clk();
  127. #else
  128. clkin = get_PCLK();
  129. #endif
  130. /* FPCLK / FI2C =
  131. * (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2) + 8 + 2 * FLT_CYCLE
  132. * uTemp0 = (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2)
  133. * uTemp1 = (TSCLK_L + TSCLK_H + 2)
  134. * uTemp2 = TSCLK_L + TSCLK_H
  135. */
  136. t_ftl_cycle = (readl(&hsregs->usi_conf) >> 16) & 0x7;
  137. utemp0 = (clkin / op_clk) - 8 - 2 * t_ftl_cycle;
  138. /* CLK_DIV max is 256 */
  139. for (i = 0; i < 256; i++) {
  140. utemp1 = utemp0 / (i + 1);
  141. if ((utemp1 < 512) && (utemp1 > 4)) {
  142. i2c_bus->clk_cycle = utemp1 - 2;
  143. i2c_bus->clk_div = i;
  144. return 0;
  145. }
  146. }
  147. return -EINVAL;
  148. }
  149. static void hsi2c_ch_init(struct s3c24x0_i2c_bus *i2c_bus)
  150. {
  151. struct exynos5_hsi2c *hsregs = i2c_bus->hsregs;
  152. unsigned int t_sr_release;
  153. unsigned int n_clkdiv;
  154. unsigned int t_start_su, t_start_hd;
  155. unsigned int t_stop_su;
  156. unsigned int t_data_su, t_data_hd;
  157. unsigned int t_scl_l, t_scl_h;
  158. u32 i2c_timing_s1;
  159. u32 i2c_timing_s2;
  160. u32 i2c_timing_s3;
  161. u32 i2c_timing_sla;
  162. n_clkdiv = i2c_bus->clk_div;
  163. t_scl_l = i2c_bus->clk_cycle / 2;
  164. t_scl_h = i2c_bus->clk_cycle / 2;
  165. t_start_su = t_scl_l;
  166. t_start_hd = t_scl_l;
  167. t_stop_su = t_scl_l;
  168. t_data_su = t_scl_l / 2;
  169. t_data_hd = t_scl_l / 2;
  170. t_sr_release = i2c_bus->clk_cycle;
  171. i2c_timing_s1 = t_start_su << 24 | t_start_hd << 16 | t_stop_su << 8;
  172. i2c_timing_s2 = t_data_su << 24 | t_scl_l << 8 | t_scl_h << 0;
  173. i2c_timing_s3 = n_clkdiv << 16 | t_sr_release << 0;
  174. i2c_timing_sla = t_data_hd << 0;
  175. writel(HSI2C_TRAILING_COUNT, &hsregs->usi_trailing_ctl);
  176. /* Clear to enable Timeout */
  177. clrsetbits_le32(&hsregs->usi_timeout, HSI2C_TIMEOUT_EN, 0);
  178. /* set AUTO mode */
  179. writel(readl(&hsregs->usi_conf) | HSI2C_AUTO_MODE, &hsregs->usi_conf);
  180. /* Enable completion conditions' reporting. */
  181. writel(HSI2C_INT_I2C_EN, &hsregs->usi_int_en);
  182. /* Enable FIFOs */
  183. writel(HSI2C_RXFIFO_EN | HSI2C_TXFIFO_EN, &hsregs->usi_fifo_ctl);
  184. /* Currently operating in Fast speed mode. */
  185. writel(i2c_timing_s1, &hsregs->usi_timing_fs1);
  186. writel(i2c_timing_s2, &hsregs->usi_timing_fs2);
  187. writel(i2c_timing_s3, &hsregs->usi_timing_fs3);
  188. writel(i2c_timing_sla, &hsregs->usi_timing_sla);
  189. }
  190. /* SW reset for the high speed bus */
  191. static void exynos5_i2c_reset(struct s3c24x0_i2c_bus *i2c_bus)
  192. {
  193. struct exynos5_hsi2c *i2c = i2c_bus->hsregs;
  194. u32 i2c_ctl;
  195. /* Set and clear the bit for reset */
  196. i2c_ctl = readl(&i2c->usi_ctl);
  197. i2c_ctl |= HSI2C_SW_RST;
  198. writel(i2c_ctl, &i2c->usi_ctl);
  199. i2c_ctl = readl(&i2c->usi_ctl);
  200. i2c_ctl &= ~HSI2C_SW_RST;
  201. writel(i2c_ctl, &i2c->usi_ctl);
  202. /* Initialize the configure registers */
  203. hsi2c_ch_init(i2c_bus);
  204. }
  205. /*
  206. * Poll the appropriate bit of the fifo status register until the interface is
  207. * ready to process the next byte or timeout expires.
  208. *
  209. * In addition to the FIFO status register this function also polls the
  210. * interrupt status register to be able to detect unexpected transaction
  211. * completion.
  212. *
  213. * When FIFO is ready to process the next byte, this function returns I2C_OK.
  214. * If in course of polling the INT_I2C assertion is detected, the function
  215. * returns I2C_NOK. If timeout happens before any of the above conditions is
  216. * met - the function returns I2C_NOK_TOUT;
  217. * @param i2c: pointer to the appropriate i2c register bank.
  218. * @param rx_transfer: set to True if the receive transaction is in progress.
  219. * @return: as described above.
  220. */
  221. static unsigned hsi2c_poll_fifo(struct exynos5_hsi2c *i2c, bool rx_transfer)
  222. {
  223. u32 fifo_bit = rx_transfer ? HSI2C_RX_FIFO_EMPTY : HSI2C_TX_FIFO_FULL;
  224. int i = HSI2C_TIMEOUT_US;
  225. while (readl(&i2c->usi_fifo_stat) & fifo_bit) {
  226. if (readl(&i2c->usi_int_stat) & HSI2C_INT_I2C_EN) {
  227. /*
  228. * There is a chance that assertion of
  229. * HSI2C_INT_I2C_EN and deassertion of
  230. * HSI2C_RX_FIFO_EMPTY happen simultaneously. Let's
  231. * give FIFO status priority and check it one more
  232. * time before reporting interrupt. The interrupt will
  233. * be reported next time this function is called.
  234. */
  235. if (rx_transfer &&
  236. !(readl(&i2c->usi_fifo_stat) & fifo_bit))
  237. break;
  238. return I2C_NOK;
  239. }
  240. if (!i--) {
  241. debug("%s: FIFO polling timeout!\n", __func__);
  242. return I2C_NOK_TOUT;
  243. }
  244. udelay(1);
  245. }
  246. return I2C_OK;
  247. }
  248. /*
  249. * Preapre hsi2c transaction, either read or write.
  250. *
  251. * Set up transfer as described in section 27.5.1.2 'I2C Channel Auto Mode' of
  252. * the 5420 UM.
  253. *
  254. * @param i2c: pointer to the appropriate i2c register bank.
  255. * @param chip: slave address on the i2c bus (with read/write bit exlcuded)
  256. * @param len: number of bytes expected to be sent or received
  257. * @param rx_transfer: set to true for receive transactions
  258. * @param: issue_stop: set to true if i2c stop condition should be generated
  259. * after this transaction.
  260. * @return: I2C_NOK_TOUT in case the bus remained busy for HSI2C_TIMEOUT_US,
  261. * I2C_OK otherwise.
  262. */
  263. static int hsi2c_prepare_transaction(struct exynos5_hsi2c *i2c,
  264. u8 chip,
  265. u16 len,
  266. bool rx_transfer,
  267. bool issue_stop)
  268. {
  269. u32 conf;
  270. conf = len | HSI2C_MASTER_RUN;
  271. if (issue_stop)
  272. conf |= HSI2C_STOP_AFTER_TRANS;
  273. /* Clear to enable Timeout */
  274. writel(readl(&i2c->usi_timeout) & ~HSI2C_TIMEOUT_EN, &i2c->usi_timeout);
  275. /* Set slave address */
  276. writel(HSI2C_SLV_ADDR_MAS(chip), &i2c->i2c_addr);
  277. if (rx_transfer) {
  278. /* i2c master, read transaction */
  279. writel((HSI2C_RXCHON | HSI2C_FUNC_MODE_I2C | HSI2C_MASTER),
  280. &i2c->usi_ctl);
  281. /* read up to len bytes, stop after transaction is finished */
  282. writel(conf | HSI2C_READ_WRITE, &i2c->usi_auto_conf);
  283. } else {
  284. /* i2c master, write transaction */
  285. writel((HSI2C_TXCHON | HSI2C_FUNC_MODE_I2C | HSI2C_MASTER),
  286. &i2c->usi_ctl);
  287. /* write up to len bytes, stop after transaction is finished */
  288. writel(conf, &i2c->usi_auto_conf);
  289. }
  290. /* Reset all pending interrupt status bits we care about, if any */
  291. writel(HSI2C_INT_I2C_EN, &i2c->usi_int_stat);
  292. return I2C_OK;
  293. }
  294. /*
  295. * Wait while i2c bus is settling down (mostly stop gets completed).
  296. */
  297. static int hsi2c_wait_while_busy(struct exynos5_hsi2c *i2c)
  298. {
  299. int i = HSI2C_TIMEOUT_US;
  300. while (readl(&i2c->usi_trans_status) & HSI2C_MASTER_BUSY) {
  301. if (!i--) {
  302. debug("%s: bus busy\n", __func__);
  303. return I2C_NOK_TOUT;
  304. }
  305. udelay(1);
  306. }
  307. return I2C_OK;
  308. }
  309. static int hsi2c_write(struct exynos5_hsi2c *i2c,
  310. unsigned char chip,
  311. unsigned char addr[],
  312. unsigned char alen,
  313. unsigned char data[],
  314. unsigned short len,
  315. bool issue_stop)
  316. {
  317. int i, rv = 0;
  318. if (!(len + alen)) {
  319. /* Writes of zero length not supported in auto mode. */
  320. debug("%s: zero length writes not supported\n", __func__);
  321. return I2C_NOK;
  322. }
  323. rv = hsi2c_prepare_transaction
  324. (i2c, chip, len + alen, false, issue_stop);
  325. if (rv != I2C_OK)
  326. return rv;
  327. /* Move address, if any, and the data, if any, into the FIFO. */
  328. for (i = 0; i < alen; i++) {
  329. rv = hsi2c_poll_fifo(i2c, false);
  330. if (rv != I2C_OK) {
  331. debug("%s: address write failed\n", __func__);
  332. goto write_error;
  333. }
  334. writel(addr[i], &i2c->usi_txdata);
  335. }
  336. for (i = 0; i < len; i++) {
  337. rv = hsi2c_poll_fifo(i2c, false);
  338. if (rv != I2C_OK) {
  339. debug("%s: data write failed\n", __func__);
  340. goto write_error;
  341. }
  342. writel(data[i], &i2c->usi_txdata);
  343. }
  344. rv = hsi2c_wait_for_trx(i2c);
  345. write_error:
  346. if (issue_stop) {
  347. int tmp_ret = hsi2c_wait_while_busy(i2c);
  348. if (rv == I2C_OK)
  349. rv = tmp_ret;
  350. }
  351. writel(HSI2C_FUNC_MODE_I2C, &i2c->usi_ctl); /* done */
  352. return rv;
  353. }
  354. static int hsi2c_read(struct exynos5_hsi2c *i2c,
  355. unsigned char chip,
  356. unsigned char addr[],
  357. unsigned char alen,
  358. unsigned char data[],
  359. unsigned short len)
  360. {
  361. int i, rv, tmp_ret;
  362. bool drop_data = false;
  363. if (!len) {
  364. /* Reads of zero length not supported in auto mode. */
  365. debug("%s: zero length read adjusted\n", __func__);
  366. drop_data = true;
  367. len = 1;
  368. }
  369. if (alen) {
  370. /* Internal register adress needs to be written first. */
  371. rv = hsi2c_write(i2c, chip, addr, alen, NULL, 0, false);
  372. if (rv != I2C_OK)
  373. return rv;
  374. }
  375. rv = hsi2c_prepare_transaction(i2c, chip, len, true, true);
  376. if (rv != I2C_OK)
  377. return rv;
  378. for (i = 0; i < len; i++) {
  379. rv = hsi2c_poll_fifo(i2c, true);
  380. if (rv != I2C_OK)
  381. goto read_err;
  382. if (drop_data)
  383. continue;
  384. data[i] = readl(&i2c->usi_rxdata);
  385. }
  386. rv = hsi2c_wait_for_trx(i2c);
  387. read_err:
  388. tmp_ret = hsi2c_wait_while_busy(i2c);
  389. if (rv == I2C_OK)
  390. rv = tmp_ret;
  391. writel(HSI2C_FUNC_MODE_I2C, &i2c->usi_ctl); /* done */
  392. return rv;
  393. }
  394. static int exynos_hs_i2c_xfer(struct udevice *dev, struct i2c_msg *msg,
  395. int nmsgs)
  396. {
  397. struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
  398. struct exynos5_hsi2c *hsregs = i2c_bus->hsregs;
  399. int ret;
  400. for (; nmsgs > 0; nmsgs--, msg++) {
  401. if (msg->flags & I2C_M_RD) {
  402. ret = hsi2c_read(hsregs, msg->addr, 0, 0, msg->buf,
  403. msg->len);
  404. } else {
  405. ret = hsi2c_write(hsregs, msg->addr, 0, 0, msg->buf,
  406. msg->len, true);
  407. }
  408. if (ret) {
  409. exynos5_i2c_reset(i2c_bus);
  410. return -EREMOTEIO;
  411. }
  412. }
  413. return 0;
  414. }
  415. static int s3c24x0_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
  416. {
  417. struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
  418. i2c_bus->clock_frequency = speed;
  419. if (hsi2c_get_clk_details(i2c_bus))
  420. return -EFAULT;
  421. hsi2c_ch_init(i2c_bus);
  422. return 0;
  423. }
  424. static int s3c24x0_i2c_probe(struct udevice *dev, uint chip, uint chip_flags)
  425. {
  426. struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
  427. uchar buf[1];
  428. int ret;
  429. buf[0] = 0;
  430. /*
  431. * What is needed is to send the chip address and verify that the
  432. * address was <ACK>ed (i.e. there was a chip at that address which
  433. * drove the data line low).
  434. */
  435. ret = hsi2c_read(i2c_bus->hsregs, chip, 0, 0, buf, 1);
  436. return ret != I2C_OK;
  437. }
  438. static int s3c_i2c_ofdata_to_platdata(struct udevice *dev)
  439. {
  440. const void *blob = gd->fdt_blob;
  441. struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
  442. int node;
  443. node = dev->of_offset;
  444. i2c_bus->hsregs = (struct exynos5_hsi2c *)dev_get_addr(dev);
  445. i2c_bus->id = pinmux_decode_periph_id(blob, node);
  446. i2c_bus->clock_frequency = fdtdec_get_int(blob, node,
  447. "clock-frequency", 100000);
  448. i2c_bus->node = node;
  449. i2c_bus->bus_num = dev->seq;
  450. exynos_pinmux_config(i2c_bus->id, PINMUX_FLAG_HS_MODE);
  451. i2c_bus->active = true;
  452. return 0;
  453. }
  454. static const struct dm_i2c_ops exynos_hs_i2c_ops = {
  455. .xfer = exynos_hs_i2c_xfer,
  456. .probe_chip = s3c24x0_i2c_probe,
  457. .set_bus_speed = s3c24x0_i2c_set_bus_speed,
  458. };
  459. static const struct udevice_id exynos_hs_i2c_ids[] = {
  460. { .compatible = "samsung,exynos5-hsi2c" },
  461. { }
  462. };
  463. U_BOOT_DRIVER(hs_i2c) = {
  464. .name = "i2c_s3c_hs",
  465. .id = UCLASS_I2C,
  466. .of_match = exynos_hs_i2c_ids,
  467. .ofdata_to_platdata = s3c_i2c_ofdata_to_platdata,
  468. .priv_auto_alloc_size = sizeof(struct s3c24x0_i2c_bus),
  469. .ops = &exynos_hs_i2c_ops,
  470. };