s3c24x0_i2c.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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 <errno.h>
  13. #include <dm.h>
  14. #include <fdtdec.h>
  15. #if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
  16. #include <asm/arch/clk.h>
  17. #include <asm/arch/cpu.h>
  18. #include <asm/arch/pinmux.h>
  19. #else
  20. #include <asm/arch/s3c24x0_cpu.h>
  21. #endif
  22. #include <asm/io.h>
  23. #include <i2c.h>
  24. #include "s3c24x0_i2c.h"
  25. #define I2C_WRITE 0
  26. #define I2C_READ 1
  27. #define I2C_OK 0
  28. #define I2C_NOK 1
  29. #define I2C_NACK 2
  30. #define I2C_NOK_LA 3 /* Lost arbitration */
  31. #define I2C_NOK_TOUT 4 /* time out */
  32. /* HSI2C specific register description */
  33. /* I2C_CTL Register bits */
  34. #define HSI2C_FUNC_MODE_I2C (1u << 0)
  35. #define HSI2C_MASTER (1u << 3)
  36. #define HSI2C_RXCHON (1u << 6) /* Write/Send */
  37. #define HSI2C_TXCHON (1u << 7) /* Read/Receive */
  38. #define HSI2C_SW_RST (1u << 31)
  39. /* I2C_FIFO_CTL Register bits */
  40. #define HSI2C_RXFIFO_EN (1u << 0)
  41. #define HSI2C_TXFIFO_EN (1u << 1)
  42. #define HSI2C_TXFIFO_TRIGGER_LEVEL (0x20 << 16)
  43. #define HSI2C_RXFIFO_TRIGGER_LEVEL (0x20 << 4)
  44. /* I2C_TRAILING_CTL Register bits */
  45. #define HSI2C_TRAILING_COUNT (0xff)
  46. /* I2C_INT_EN Register bits */
  47. #define HSI2C_TX_UNDERRUN_EN (1u << 2)
  48. #define HSI2C_TX_OVERRUN_EN (1u << 3)
  49. #define HSI2C_RX_UNDERRUN_EN (1u << 4)
  50. #define HSI2C_RX_OVERRUN_EN (1u << 5)
  51. #define HSI2C_INT_TRAILING_EN (1u << 6)
  52. #define HSI2C_INT_I2C_EN (1u << 9)
  53. #define HSI2C_INT_ERROR_MASK (HSI2C_TX_UNDERRUN_EN |\
  54. HSI2C_TX_OVERRUN_EN |\
  55. HSI2C_RX_UNDERRUN_EN |\
  56. HSI2C_RX_OVERRUN_EN |\
  57. HSI2C_INT_TRAILING_EN)
  58. /* I2C_CONF Register bits */
  59. #define HSI2C_AUTO_MODE (1u << 31)
  60. #define HSI2C_10BIT_ADDR_MODE (1u << 30)
  61. #define HSI2C_HS_MODE (1u << 29)
  62. /* I2C_AUTO_CONF Register bits */
  63. #define HSI2C_READ_WRITE (1u << 16)
  64. #define HSI2C_STOP_AFTER_TRANS (1u << 17)
  65. #define HSI2C_MASTER_RUN (1u << 31)
  66. /* I2C_TIMEOUT Register bits */
  67. #define HSI2C_TIMEOUT_EN (1u << 31)
  68. /* I2C_TRANS_STATUS register bits */
  69. #define HSI2C_MASTER_BUSY (1u << 17)
  70. #define HSI2C_SLAVE_BUSY (1u << 16)
  71. #define HSI2C_TIMEOUT_AUTO (1u << 4)
  72. #define HSI2C_NO_DEV (1u << 3)
  73. #define HSI2C_NO_DEV_ACK (1u << 2)
  74. #define HSI2C_TRANS_ABORT (1u << 1)
  75. #define HSI2C_TRANS_SUCCESS (1u << 0)
  76. #define HSI2C_TRANS_ERROR_MASK (HSI2C_TIMEOUT_AUTO |\
  77. HSI2C_NO_DEV | HSI2C_NO_DEV_ACK |\
  78. HSI2C_TRANS_ABORT)
  79. #define HSI2C_TRANS_FINISHED_MASK (HSI2C_TRANS_ERROR_MASK | HSI2C_TRANS_SUCCESS)
  80. /* I2C_FIFO_STAT Register bits */
  81. #define HSI2C_RX_FIFO_EMPTY (1u << 24)
  82. #define HSI2C_RX_FIFO_FULL (1u << 23)
  83. #define HSI2C_TX_FIFO_EMPTY (1u << 8)
  84. #define HSI2C_TX_FIFO_FULL (1u << 7)
  85. #define HSI2C_RX_FIFO_LEVEL(x) (((x) >> 16) & 0x7f)
  86. #define HSI2C_TX_FIFO_LEVEL(x) ((x) & 0x7f)
  87. #define HSI2C_SLV_ADDR_MAS(x) ((x & 0x3ff) << 10)
  88. /* S3C I2C Controller bits */
  89. #define I2CSTAT_BSY 0x20 /* Busy bit */
  90. #define I2CSTAT_NACK 0x01 /* Nack bit */
  91. #define I2CCON_ACKGEN 0x80 /* Acknowledge generation */
  92. #define I2CCON_IRPND 0x10 /* Interrupt pending bit */
  93. #define I2C_MODE_MT 0xC0 /* Master Transmit Mode */
  94. #define I2C_MODE_MR 0x80 /* Master Receive Mode */
  95. #define I2C_START_STOP 0x20 /* START / STOP */
  96. #define I2C_TXRX_ENA 0x10 /* I2C Tx/Rx enable */
  97. #define I2C_TIMEOUT_MS 10 /* 10 ms */
  98. #define HSI2C_TIMEOUT_US 10000 /* 10 ms, finer granularity */
  99. DECLARE_GLOBAL_DATA_PTR;
  100. enum exynos_i2c_type {
  101. EXYNOS_I2C_STD,
  102. EXYNOS_I2C_HS,
  103. };
  104. /*
  105. * Wait til the byte transfer is completed.
  106. *
  107. * @param i2c- pointer to the appropriate i2c register bank.
  108. * @return I2C_OK, if transmission was ACKED
  109. * I2C_NACK, if transmission was NACKED
  110. * I2C_NOK_TIMEOUT, if transaction did not complete in I2C_TIMEOUT_MS
  111. */
  112. static int WaitForXfer(struct s3c24x0_i2c *i2c)
  113. {
  114. ulong start_time = get_timer(0);
  115. do {
  116. if (readl(&i2c->iiccon) & I2CCON_IRPND)
  117. return (readl(&i2c->iicstat) & I2CSTAT_NACK) ?
  118. I2C_NACK : I2C_OK;
  119. } while (get_timer(start_time) < I2C_TIMEOUT_MS);
  120. return I2C_NOK_TOUT;
  121. }
  122. /*
  123. * Wait for transfer completion.
  124. *
  125. * This function reads the interrupt status register waiting for the INT_I2C
  126. * bit to be set, which indicates copletion of a transaction.
  127. *
  128. * @param i2c: pointer to the appropriate register bank
  129. *
  130. * @return: I2C_OK in case of successful completion, I2C_NOK_TIMEOUT in case
  131. * the status bits do not get set in time, or an approrpiate error
  132. * value in case of transfer errors.
  133. */
  134. static int hsi2c_wait_for_trx(struct exynos5_hsi2c *i2c)
  135. {
  136. int i = HSI2C_TIMEOUT_US;
  137. while (i-- > 0) {
  138. u32 int_status = readl(&i2c->usi_int_stat);
  139. if (int_status & HSI2C_INT_I2C_EN) {
  140. u32 trans_status = readl(&i2c->usi_trans_status);
  141. /* Deassert pending interrupt. */
  142. writel(int_status, &i2c->usi_int_stat);
  143. if (trans_status & HSI2C_NO_DEV_ACK) {
  144. debug("%s: no ACK from device\n", __func__);
  145. return I2C_NACK;
  146. }
  147. if (trans_status & HSI2C_NO_DEV) {
  148. debug("%s: no device\n", __func__);
  149. return I2C_NOK;
  150. }
  151. if (trans_status & HSI2C_TRANS_ABORT) {
  152. debug("%s: arbitration lost\n", __func__);
  153. return I2C_NOK_LA;
  154. }
  155. if (trans_status & HSI2C_TIMEOUT_AUTO) {
  156. debug("%s: device timed out\n", __func__);
  157. return I2C_NOK_TOUT;
  158. }
  159. return I2C_OK;
  160. }
  161. udelay(1);
  162. }
  163. debug("%s: transaction timeout!\n", __func__);
  164. return I2C_NOK_TOUT;
  165. }
  166. static void read_write_byte(struct s3c24x0_i2c *i2c)
  167. {
  168. clrbits_le32(&i2c->iiccon, I2CCON_IRPND);
  169. }
  170. static void i2c_ch_init(struct s3c24x0_i2c *i2c, int speed, int slaveadd)
  171. {
  172. ulong freq, pres = 16, div;
  173. #if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
  174. freq = get_i2c_clk();
  175. #else
  176. freq = get_PCLK();
  177. #endif
  178. /* calculate prescaler and divisor values */
  179. if ((freq / pres / (16 + 1)) > speed)
  180. /* set prescaler to 512 */
  181. pres = 512;
  182. div = 0;
  183. while ((freq / pres / (div + 1)) > speed)
  184. div++;
  185. /* set prescaler, divisor according to freq, also set ACKGEN, IRQ */
  186. writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon);
  187. /* init to SLAVE REVEIVE and set slaveaddr */
  188. writel(0, &i2c->iicstat);
  189. writel(slaveadd, &i2c->iicadd);
  190. /* program Master Transmit (and implicit STOP) */
  191. writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
  192. }
  193. static int hsi2c_get_clk_details(struct s3c24x0_i2c_bus *i2c_bus)
  194. {
  195. struct exynos5_hsi2c *hsregs = i2c_bus->hsregs;
  196. ulong clkin;
  197. unsigned int op_clk = i2c_bus->clock_frequency;
  198. unsigned int i = 0, utemp0 = 0, utemp1 = 0;
  199. unsigned int t_ftl_cycle;
  200. #if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
  201. clkin = get_i2c_clk();
  202. #else
  203. clkin = get_PCLK();
  204. #endif
  205. /* FPCLK / FI2C =
  206. * (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2) + 8 + 2 * FLT_CYCLE
  207. * uTemp0 = (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2)
  208. * uTemp1 = (TSCLK_L + TSCLK_H + 2)
  209. * uTemp2 = TSCLK_L + TSCLK_H
  210. */
  211. t_ftl_cycle = (readl(&hsregs->usi_conf) >> 16) & 0x7;
  212. utemp0 = (clkin / op_clk) - 8 - 2 * t_ftl_cycle;
  213. /* CLK_DIV max is 256 */
  214. for (i = 0; i < 256; i++) {
  215. utemp1 = utemp0 / (i + 1);
  216. if ((utemp1 < 512) && (utemp1 > 4)) {
  217. i2c_bus->clk_cycle = utemp1 - 2;
  218. i2c_bus->clk_div = i;
  219. return 0;
  220. }
  221. }
  222. return -EINVAL;
  223. }
  224. static void hsi2c_ch_init(struct s3c24x0_i2c_bus *i2c_bus)
  225. {
  226. struct exynos5_hsi2c *hsregs = i2c_bus->hsregs;
  227. unsigned int t_sr_release;
  228. unsigned int n_clkdiv;
  229. unsigned int t_start_su, t_start_hd;
  230. unsigned int t_stop_su;
  231. unsigned int t_data_su, t_data_hd;
  232. unsigned int t_scl_l, t_scl_h;
  233. u32 i2c_timing_s1;
  234. u32 i2c_timing_s2;
  235. u32 i2c_timing_s3;
  236. u32 i2c_timing_sla;
  237. n_clkdiv = i2c_bus->clk_div;
  238. t_scl_l = i2c_bus->clk_cycle / 2;
  239. t_scl_h = i2c_bus->clk_cycle / 2;
  240. t_start_su = t_scl_l;
  241. t_start_hd = t_scl_l;
  242. t_stop_su = t_scl_l;
  243. t_data_su = t_scl_l / 2;
  244. t_data_hd = t_scl_l / 2;
  245. t_sr_release = i2c_bus->clk_cycle;
  246. i2c_timing_s1 = t_start_su << 24 | t_start_hd << 16 | t_stop_su << 8;
  247. i2c_timing_s2 = t_data_su << 24 | t_scl_l << 8 | t_scl_h << 0;
  248. i2c_timing_s3 = n_clkdiv << 16 | t_sr_release << 0;
  249. i2c_timing_sla = t_data_hd << 0;
  250. writel(HSI2C_TRAILING_COUNT, &hsregs->usi_trailing_ctl);
  251. /* Clear to enable Timeout */
  252. clrsetbits_le32(&hsregs->usi_timeout, HSI2C_TIMEOUT_EN, 0);
  253. /* set AUTO mode */
  254. writel(readl(&hsregs->usi_conf) | HSI2C_AUTO_MODE, &hsregs->usi_conf);
  255. /* Enable completion conditions' reporting. */
  256. writel(HSI2C_INT_I2C_EN, &hsregs->usi_int_en);
  257. /* Enable FIFOs */
  258. writel(HSI2C_RXFIFO_EN | HSI2C_TXFIFO_EN, &hsregs->usi_fifo_ctl);
  259. /* Currently operating in Fast speed mode. */
  260. writel(i2c_timing_s1, &hsregs->usi_timing_fs1);
  261. writel(i2c_timing_s2, &hsregs->usi_timing_fs2);
  262. writel(i2c_timing_s3, &hsregs->usi_timing_fs3);
  263. writel(i2c_timing_sla, &hsregs->usi_timing_sla);
  264. }
  265. /* SW reset for the high speed bus */
  266. static void exynos5_i2c_reset(struct s3c24x0_i2c_bus *i2c_bus)
  267. {
  268. struct exynos5_hsi2c *i2c = i2c_bus->hsregs;
  269. u32 i2c_ctl;
  270. /* Set and clear the bit for reset */
  271. i2c_ctl = readl(&i2c->usi_ctl);
  272. i2c_ctl |= HSI2C_SW_RST;
  273. writel(i2c_ctl, &i2c->usi_ctl);
  274. i2c_ctl = readl(&i2c->usi_ctl);
  275. i2c_ctl &= ~HSI2C_SW_RST;
  276. writel(i2c_ctl, &i2c->usi_ctl);
  277. /* Initialize the configure registers */
  278. hsi2c_ch_init(i2c_bus);
  279. }
  280. /*
  281. * Poll the appropriate bit of the fifo status register until the interface is
  282. * ready to process the next byte or timeout expires.
  283. *
  284. * In addition to the FIFO status register this function also polls the
  285. * interrupt status register to be able to detect unexpected transaction
  286. * completion.
  287. *
  288. * When FIFO is ready to process the next byte, this function returns I2C_OK.
  289. * If in course of polling the INT_I2C assertion is detected, the function
  290. * returns I2C_NOK. If timeout happens before any of the above conditions is
  291. * met - the function returns I2C_NOK_TOUT;
  292. * @param i2c: pointer to the appropriate i2c register bank.
  293. * @param rx_transfer: set to True if the receive transaction is in progress.
  294. * @return: as described above.
  295. */
  296. static unsigned hsi2c_poll_fifo(struct exynos5_hsi2c *i2c, bool rx_transfer)
  297. {
  298. u32 fifo_bit = rx_transfer ? HSI2C_RX_FIFO_EMPTY : HSI2C_TX_FIFO_FULL;
  299. int i = HSI2C_TIMEOUT_US;
  300. while (readl(&i2c->usi_fifo_stat) & fifo_bit) {
  301. if (readl(&i2c->usi_int_stat) & HSI2C_INT_I2C_EN) {
  302. /*
  303. * There is a chance that assertion of
  304. * HSI2C_INT_I2C_EN and deassertion of
  305. * HSI2C_RX_FIFO_EMPTY happen simultaneously. Let's
  306. * give FIFO status priority and check it one more
  307. * time before reporting interrupt. The interrupt will
  308. * be reported next time this function is called.
  309. */
  310. if (rx_transfer &&
  311. !(readl(&i2c->usi_fifo_stat) & fifo_bit))
  312. break;
  313. return I2C_NOK;
  314. }
  315. if (!i--) {
  316. debug("%s: FIFO polling timeout!\n", __func__);
  317. return I2C_NOK_TOUT;
  318. }
  319. udelay(1);
  320. }
  321. return I2C_OK;
  322. }
  323. /*
  324. * Preapre hsi2c transaction, either read or write.
  325. *
  326. * Set up transfer as described in section 27.5.1.2 'I2C Channel Auto Mode' of
  327. * the 5420 UM.
  328. *
  329. * @param i2c: pointer to the appropriate i2c register bank.
  330. * @param chip: slave address on the i2c bus (with read/write bit exlcuded)
  331. * @param len: number of bytes expected to be sent or received
  332. * @param rx_transfer: set to true for receive transactions
  333. * @param: issue_stop: set to true if i2c stop condition should be generated
  334. * after this transaction.
  335. * @return: I2C_NOK_TOUT in case the bus remained busy for HSI2C_TIMEOUT_US,
  336. * I2C_OK otherwise.
  337. */
  338. static int hsi2c_prepare_transaction(struct exynos5_hsi2c *i2c,
  339. u8 chip,
  340. u16 len,
  341. bool rx_transfer,
  342. bool issue_stop)
  343. {
  344. u32 conf;
  345. conf = len | HSI2C_MASTER_RUN;
  346. if (issue_stop)
  347. conf |= HSI2C_STOP_AFTER_TRANS;
  348. /* Clear to enable Timeout */
  349. writel(readl(&i2c->usi_timeout) & ~HSI2C_TIMEOUT_EN, &i2c->usi_timeout);
  350. /* Set slave address */
  351. writel(HSI2C_SLV_ADDR_MAS(chip), &i2c->i2c_addr);
  352. if (rx_transfer) {
  353. /* i2c master, read transaction */
  354. writel((HSI2C_RXCHON | HSI2C_FUNC_MODE_I2C | HSI2C_MASTER),
  355. &i2c->usi_ctl);
  356. /* read up to len bytes, stop after transaction is finished */
  357. writel(conf | HSI2C_READ_WRITE, &i2c->usi_auto_conf);
  358. } else {
  359. /* i2c master, write transaction */
  360. writel((HSI2C_TXCHON | HSI2C_FUNC_MODE_I2C | HSI2C_MASTER),
  361. &i2c->usi_ctl);
  362. /* write up to len bytes, stop after transaction is finished */
  363. writel(conf, &i2c->usi_auto_conf);
  364. }
  365. /* Reset all pending interrupt status bits we care about, if any */
  366. writel(HSI2C_INT_I2C_EN, &i2c->usi_int_stat);
  367. return I2C_OK;
  368. }
  369. /*
  370. * Wait while i2c bus is settling down (mostly stop gets completed).
  371. */
  372. static int hsi2c_wait_while_busy(struct exynos5_hsi2c *i2c)
  373. {
  374. int i = HSI2C_TIMEOUT_US;
  375. while (readl(&i2c->usi_trans_status) & HSI2C_MASTER_BUSY) {
  376. if (!i--) {
  377. debug("%s: bus busy\n", __func__);
  378. return I2C_NOK_TOUT;
  379. }
  380. udelay(1);
  381. }
  382. return I2C_OK;
  383. }
  384. static int hsi2c_write(struct exynos5_hsi2c *i2c,
  385. unsigned char chip,
  386. unsigned char addr[],
  387. unsigned char alen,
  388. unsigned char data[],
  389. unsigned short len,
  390. bool issue_stop)
  391. {
  392. int i, rv = 0;
  393. if (!(len + alen)) {
  394. /* Writes of zero length not supported in auto mode. */
  395. debug("%s: zero length writes not supported\n", __func__);
  396. return I2C_NOK;
  397. }
  398. rv = hsi2c_prepare_transaction
  399. (i2c, chip, len + alen, false, issue_stop);
  400. if (rv != I2C_OK)
  401. return rv;
  402. /* Move address, if any, and the data, if any, into the FIFO. */
  403. for (i = 0; i < alen; i++) {
  404. rv = hsi2c_poll_fifo(i2c, false);
  405. if (rv != I2C_OK) {
  406. debug("%s: address write failed\n", __func__);
  407. goto write_error;
  408. }
  409. writel(addr[i], &i2c->usi_txdata);
  410. }
  411. for (i = 0; i < len; i++) {
  412. rv = hsi2c_poll_fifo(i2c, false);
  413. if (rv != I2C_OK) {
  414. debug("%s: data write failed\n", __func__);
  415. goto write_error;
  416. }
  417. writel(data[i], &i2c->usi_txdata);
  418. }
  419. rv = hsi2c_wait_for_trx(i2c);
  420. write_error:
  421. if (issue_stop) {
  422. int tmp_ret = hsi2c_wait_while_busy(i2c);
  423. if (rv == I2C_OK)
  424. rv = tmp_ret;
  425. }
  426. writel(HSI2C_FUNC_MODE_I2C, &i2c->usi_ctl); /* done */
  427. return rv;
  428. }
  429. static int hsi2c_read(struct exynos5_hsi2c *i2c,
  430. unsigned char chip,
  431. unsigned char addr[],
  432. unsigned char alen,
  433. unsigned char data[],
  434. unsigned short len)
  435. {
  436. int i, rv, tmp_ret;
  437. bool drop_data = false;
  438. if (!len) {
  439. /* Reads of zero length not supported in auto mode. */
  440. debug("%s: zero length read adjusted\n", __func__);
  441. drop_data = true;
  442. len = 1;
  443. }
  444. if (alen) {
  445. /* Internal register adress needs to be written first. */
  446. rv = hsi2c_write(i2c, chip, addr, alen, NULL, 0, false);
  447. if (rv != I2C_OK)
  448. return rv;
  449. }
  450. rv = hsi2c_prepare_transaction(i2c, chip, len, true, true);
  451. if (rv != I2C_OK)
  452. return rv;
  453. for (i = 0; i < len; i++) {
  454. rv = hsi2c_poll_fifo(i2c, true);
  455. if (rv != I2C_OK)
  456. goto read_err;
  457. if (drop_data)
  458. continue;
  459. data[i] = readl(&i2c->usi_rxdata);
  460. }
  461. rv = hsi2c_wait_for_trx(i2c);
  462. read_err:
  463. tmp_ret = hsi2c_wait_while_busy(i2c);
  464. if (rv == I2C_OK)
  465. rv = tmp_ret;
  466. writel(HSI2C_FUNC_MODE_I2C, &i2c->usi_ctl); /* done */
  467. return rv;
  468. }
  469. static int s3c24x0_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
  470. {
  471. struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
  472. i2c_bus->clock_frequency = speed;
  473. if (i2c_bus->is_highspeed) {
  474. if (hsi2c_get_clk_details(i2c_bus))
  475. return -EFAULT;
  476. hsi2c_ch_init(i2c_bus);
  477. } else {
  478. i2c_ch_init(i2c_bus->regs, i2c_bus->clock_frequency,
  479. CONFIG_SYS_I2C_S3C24X0_SLAVE);
  480. }
  481. return 0;
  482. }
  483. /*
  484. * cmd_type is 0 for write, 1 for read.
  485. *
  486. * addr_len can take any value from 0-255, it is only limited
  487. * by the char, we could make it larger if needed. If it is
  488. * 0 we skip the address write cycle.
  489. */
  490. static int i2c_transfer(struct s3c24x0_i2c *i2c,
  491. unsigned char cmd_type,
  492. unsigned char chip,
  493. unsigned char addr[],
  494. unsigned char addr_len,
  495. unsigned char data[],
  496. unsigned short data_len)
  497. {
  498. int i = 0, result;
  499. ulong start_time = get_timer(0);
  500. if (data == 0 || data_len == 0) {
  501. /*Don't support data transfer of no length or to address 0 */
  502. debug("i2c_transfer: bad call\n");
  503. return I2C_NOK;
  504. }
  505. while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
  506. if (get_timer(start_time) > I2C_TIMEOUT_MS)
  507. return I2C_NOK_TOUT;
  508. }
  509. writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon);
  510. /* Get the slave chip address going */
  511. writel(chip, &i2c->iicds);
  512. if ((cmd_type == I2C_WRITE) || (addr && addr_len))
  513. writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
  514. &i2c->iicstat);
  515. else
  516. writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
  517. &i2c->iicstat);
  518. /* Wait for chip address to transmit. */
  519. result = WaitForXfer(i2c);
  520. if (result != I2C_OK)
  521. goto bailout;
  522. /* If register address needs to be transmitted - do it now. */
  523. if (addr && addr_len) {
  524. while ((i < addr_len) && (result == I2C_OK)) {
  525. writel(addr[i++], &i2c->iicds);
  526. read_write_byte(i2c);
  527. result = WaitForXfer(i2c);
  528. }
  529. i = 0;
  530. if (result != I2C_OK)
  531. goto bailout;
  532. }
  533. switch (cmd_type) {
  534. case I2C_WRITE:
  535. while ((i < data_len) && (result == I2C_OK)) {
  536. writel(data[i++], &i2c->iicds);
  537. read_write_byte(i2c);
  538. result = WaitForXfer(i2c);
  539. }
  540. break;
  541. case I2C_READ:
  542. if (addr && addr_len) {
  543. /*
  544. * Register address has been sent, now send slave chip
  545. * address again to start the actual read transaction.
  546. */
  547. writel(chip, &i2c->iicds);
  548. /* Generate a re-START. */
  549. writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
  550. &i2c->iicstat);
  551. read_write_byte(i2c);
  552. result = WaitForXfer(i2c);
  553. if (result != I2C_OK)
  554. goto bailout;
  555. }
  556. while ((i < data_len) && (result == I2C_OK)) {
  557. /* disable ACK for final READ */
  558. if (i == data_len - 1)
  559. writel(readl(&i2c->iiccon)
  560. & ~I2CCON_ACKGEN,
  561. &i2c->iiccon);
  562. read_write_byte(i2c);
  563. result = WaitForXfer(i2c);
  564. data[i++] = readl(&i2c->iicds);
  565. }
  566. if (result == I2C_NACK)
  567. result = I2C_OK; /* Normal terminated read. */
  568. break;
  569. default:
  570. debug("i2c_transfer: bad call\n");
  571. result = I2C_NOK;
  572. break;
  573. }
  574. bailout:
  575. /* Send STOP. */
  576. writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
  577. read_write_byte(i2c);
  578. return result;
  579. }
  580. static int s3c24x0_i2c_probe(struct udevice *dev, uint chip, uint chip_flags)
  581. {
  582. struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
  583. uchar buf[1];
  584. int ret;
  585. buf[0] = 0;
  586. /*
  587. * What is needed is to send the chip address and verify that the
  588. * address was <ACK>ed (i.e. there was a chip at that address which
  589. * drove the data line low).
  590. */
  591. if (i2c_bus->is_highspeed) {
  592. ret = hsi2c_read(i2c_bus->hsregs,
  593. chip, 0, 0, buf, 1);
  594. } else {
  595. ret = i2c_transfer(i2c_bus->regs,
  596. I2C_READ, chip << 1, 0, 0, buf, 1);
  597. }
  598. return ret != I2C_OK;
  599. }
  600. static int exynos_hs_i2c_xfer(struct udevice *dev, struct i2c_msg *msg,
  601. int nmsgs)
  602. {
  603. struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
  604. struct exynos5_hsi2c *hsregs = i2c_bus->hsregs;
  605. int ret;
  606. for (; nmsgs > 0; nmsgs--, msg++) {
  607. if (msg->flags & I2C_M_RD) {
  608. ret = hsi2c_read(hsregs, msg->addr, 0, 0, msg->buf,
  609. msg->len);
  610. } else {
  611. ret = hsi2c_write(hsregs, msg->addr, 0, 0, msg->buf,
  612. msg->len, true);
  613. }
  614. if (ret) {
  615. exynos5_i2c_reset(i2c_bus);
  616. return -EREMOTEIO;
  617. }
  618. }
  619. return 0;
  620. }
  621. static int s3c24x0_do_msg(struct s3c24x0_i2c_bus *i2c_bus, struct i2c_msg *msg,
  622. int seq)
  623. {
  624. struct s3c24x0_i2c *i2c = i2c_bus->regs;
  625. bool is_read = msg->flags & I2C_M_RD;
  626. uint status;
  627. uint addr;
  628. int ret, i;
  629. if (!seq)
  630. setbits_le32(&i2c->iiccon, I2CCON_ACKGEN);
  631. /* Get the slave chip address going */
  632. addr = msg->addr << 1;
  633. writel(addr, &i2c->iicds);
  634. status = I2C_TXRX_ENA | I2C_START_STOP;
  635. if (is_read)
  636. status |= I2C_MODE_MR;
  637. else
  638. status |= I2C_MODE_MT;
  639. writel(status, &i2c->iicstat);
  640. if (seq)
  641. read_write_byte(i2c);
  642. /* Wait for chip address to transmit */
  643. ret = WaitForXfer(i2c);
  644. if (ret)
  645. goto err;
  646. if (is_read) {
  647. for (i = 0; !ret && i < msg->len; i++) {
  648. /* disable ACK for final READ */
  649. if (i == msg->len - 1)
  650. clrbits_le32(&i2c->iiccon, I2CCON_ACKGEN);
  651. read_write_byte(i2c);
  652. ret = WaitForXfer(i2c);
  653. msg->buf[i] = readl(&i2c->iicds);
  654. }
  655. if (ret == I2C_NACK)
  656. ret = I2C_OK; /* Normal terminated read */
  657. } else {
  658. for (i = 0; !ret && i < msg->len; i++) {
  659. writel(msg->buf[i], &i2c->iicds);
  660. read_write_byte(i2c);
  661. ret = WaitForXfer(i2c);
  662. }
  663. }
  664. err:
  665. return ret;
  666. }
  667. static int s3c24x0_i2c_xfer(struct udevice *dev, struct i2c_msg *msg,
  668. int nmsgs)
  669. {
  670. struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
  671. struct s3c24x0_i2c *i2c = i2c_bus->regs;
  672. ulong start_time;
  673. int ret, i;
  674. start_time = get_timer(0);
  675. while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
  676. if (get_timer(start_time) > I2C_TIMEOUT_MS) {
  677. debug("Timeout\n");
  678. return -ETIMEDOUT;
  679. }
  680. }
  681. for (ret = 0, i = 0; !ret && i < nmsgs; i++)
  682. ret = s3c24x0_do_msg(i2c_bus, &msg[i], i);
  683. /* Send STOP */
  684. writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
  685. read_write_byte(i2c);
  686. return ret ? -EREMOTEIO : 0;
  687. }
  688. static int s3c_i2c_ofdata_to_platdata(struct udevice *dev)
  689. {
  690. const void *blob = gd->fdt_blob;
  691. struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
  692. int node, flags;
  693. i2c_bus->is_highspeed = dev_get_driver_data(dev);
  694. node = dev->of_offset;
  695. if (i2c_bus->is_highspeed) {
  696. flags = PINMUX_FLAG_HS_MODE;
  697. i2c_bus->hsregs = (struct exynos5_hsi2c *)dev_get_addr(dev);
  698. } else {
  699. flags = 0;
  700. i2c_bus->regs = (struct s3c24x0_i2c *)dev_get_addr(dev);
  701. }
  702. i2c_bus->id = pinmux_decode_periph_id(blob, node);
  703. i2c_bus->clock_frequency = fdtdec_get_int(blob, node,
  704. "clock-frequency", 100000);
  705. i2c_bus->node = node;
  706. i2c_bus->bus_num = dev->seq;
  707. exynos_pinmux_config(i2c_bus->id, flags);
  708. i2c_bus->active = true;
  709. return 0;
  710. }
  711. static const struct dm_i2c_ops s3c_i2c_ops = {
  712. .xfer = s3c24x0_i2c_xfer,
  713. .probe_chip = s3c24x0_i2c_probe,
  714. .set_bus_speed = s3c24x0_i2c_set_bus_speed,
  715. };
  716. static const struct udevice_id s3c_i2c_ids[] = {
  717. { .compatible = "samsung,s3c2440-i2c", .data = EXYNOS_I2C_STD },
  718. { }
  719. };
  720. U_BOOT_DRIVER(i2c_s3c) = {
  721. .name = "i2c_s3c",
  722. .id = UCLASS_I2C,
  723. .of_match = s3c_i2c_ids,
  724. .ofdata_to_platdata = s3c_i2c_ofdata_to_platdata,
  725. .priv_auto_alloc_size = sizeof(struct s3c24x0_i2c_bus),
  726. .ops = &s3c_i2c_ops,
  727. };
  728. /*
  729. * TODO(sjg@chromium.org): Move this to a separate file when everything uses
  730. * driver model
  731. */
  732. static const struct dm_i2c_ops exynos_hs_i2c_ops = {
  733. .xfer = exynos_hs_i2c_xfer,
  734. .probe_chip = s3c24x0_i2c_probe,
  735. .set_bus_speed = s3c24x0_i2c_set_bus_speed,
  736. };
  737. static const struct udevice_id exynos_hs_i2c_ids[] = {
  738. { .compatible = "samsung,exynos5-hsi2c", .data = EXYNOS_I2C_HS },
  739. { }
  740. };
  741. U_BOOT_DRIVER(hs_i2c) = {
  742. .name = "i2c_s3c_hs",
  743. .id = UCLASS_I2C,
  744. .of_match = exynos_hs_i2c_ids,
  745. .ofdata_to_platdata = s3c_i2c_ofdata_to_platdata,
  746. .priv_auto_alloc_size = sizeof(struct s3c24x0_i2c_bus),
  747. .ops = &exynos_hs_i2c_ops,
  748. };