s3c24x0_i2c.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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. /* HSI2C specific register description */
  32. /* I2C_CTL Register bits */
  33. #define HSI2C_FUNC_MODE_I2C (1u << 0)
  34. #define HSI2C_MASTER (1u << 3)
  35. #define HSI2C_RXCHON (1u << 6) /* Write/Send */
  36. #define HSI2C_TXCHON (1u << 7) /* Read/Receive */
  37. #define HSI2C_SW_RST (1u << 31)
  38. /* I2C_FIFO_CTL Register bits */
  39. #define HSI2C_RXFIFO_EN (1u << 0)
  40. #define HSI2C_TXFIFO_EN (1u << 1)
  41. #define HSI2C_TXFIFO_TRIGGER_LEVEL (0x20 << 16)
  42. #define HSI2C_RXFIFO_TRIGGER_LEVEL (0x20 << 4)
  43. /* I2C_TRAILING_CTL Register bits */
  44. #define HSI2C_TRAILING_COUNT (0xff)
  45. /* I2C_INT_EN Register bits */
  46. #define HSI2C_TX_UNDERRUN_EN (1u << 2)
  47. #define HSI2C_TX_OVERRUN_EN (1u << 3)
  48. #define HSI2C_RX_UNDERRUN_EN (1u << 4)
  49. #define HSI2C_RX_OVERRUN_EN (1u << 5)
  50. #define HSI2C_INT_TRAILING_EN (1u << 6)
  51. #define HSI2C_INT_I2C_EN (1u << 9)
  52. #define HSI2C_INT_ERROR_MASK (HSI2C_TX_UNDERRUN_EN |\
  53. HSI2C_TX_OVERRUN_EN |\
  54. HSI2C_RX_UNDERRUN_EN |\
  55. HSI2C_RX_OVERRUN_EN |\
  56. HSI2C_INT_TRAILING_EN)
  57. /* I2C_CONF Register bits */
  58. #define HSI2C_AUTO_MODE (1u << 31)
  59. #define HSI2C_10BIT_ADDR_MODE (1u << 30)
  60. #define HSI2C_HS_MODE (1u << 29)
  61. /* I2C_AUTO_CONF Register bits */
  62. #define HSI2C_READ_WRITE (1u << 16)
  63. #define HSI2C_STOP_AFTER_TRANS (1u << 17)
  64. #define HSI2C_MASTER_RUN (1u << 31)
  65. /* I2C_TIMEOUT Register bits */
  66. #define HSI2C_TIMEOUT_EN (1u << 31)
  67. /* I2C_TRANS_STATUS register bits */
  68. #define HSI2C_MASTER_BUSY (1u << 17)
  69. #define HSI2C_SLAVE_BUSY (1u << 16)
  70. #define HSI2C_TIMEOUT_AUTO (1u << 4)
  71. #define HSI2C_NO_DEV (1u << 3)
  72. #define HSI2C_NO_DEV_ACK (1u << 2)
  73. #define HSI2C_TRANS_ABORT (1u << 1)
  74. #define HSI2C_TRANS_SUCCESS (1u << 0)
  75. #define HSI2C_TRANS_ERROR_MASK (HSI2C_TIMEOUT_AUTO |\
  76. HSI2C_NO_DEV | HSI2C_NO_DEV_ACK |\
  77. HSI2C_TRANS_ABORT)
  78. #define HSI2C_TRANS_FINISHED_MASK (HSI2C_TRANS_ERROR_MASK | HSI2C_TRANS_SUCCESS)
  79. /* I2C_FIFO_STAT Register bits */
  80. #define HSI2C_RX_FIFO_EMPTY (1u << 24)
  81. #define HSI2C_RX_FIFO_FULL (1u << 23)
  82. #define HSI2C_TX_FIFO_EMPTY (1u << 8)
  83. #define HSI2C_TX_FIFO_FULL (1u << 7)
  84. #define HSI2C_RX_FIFO_LEVEL(x) (((x) >> 16) & 0x7f)
  85. #define HSI2C_TX_FIFO_LEVEL(x) ((x) & 0x7f)
  86. #define HSI2C_SLV_ADDR_MAS(x) ((x & 0x3ff) << 10)
  87. /* S3C I2C Controller bits */
  88. #define I2CSTAT_BSY 0x20 /* Busy bit */
  89. #define I2CSTAT_NACK 0x01 /* Nack bit */
  90. #define I2CCON_ACKGEN 0x80 /* Acknowledge generation */
  91. #define I2CCON_IRPND 0x10 /* Interrupt pending bit */
  92. #define I2C_MODE_MT 0xC0 /* Master Transmit Mode */
  93. #define I2C_MODE_MR 0x80 /* Master Receive Mode */
  94. #define I2C_START_STOP 0x20 /* START / STOP */
  95. #define I2C_TXRX_ENA 0x10 /* I2C Tx/Rx enable */
  96. #define I2C_TIMEOUT_MS 1000 /* 1 second */
  97. #define HSI2C_TIMEOUT_US 100000 /* 100 ms, finer granularity */
  98. /* To support VCMA9 boards and other who dont define max_i2c_num */
  99. #ifndef CONFIG_MAX_I2C_NUM
  100. #define CONFIG_MAX_I2C_NUM 1
  101. #endif
  102. /*
  103. * For SPL boot some boards need i2c before SDRAM is initialised so force
  104. * variables to live in SRAM
  105. */
  106. static unsigned int g_current_bus __attribute__((section(".data")));
  107. static struct s3c24x0_i2c_bus i2c_bus[CONFIG_MAX_I2C_NUM]
  108. __attribute__((section(".data")));
  109. /**
  110. * Get a pointer to the given bus index
  111. *
  112. * @bus_idx: Bus index to look up
  113. * @return pointer to bus, or NULL if invalid or not available
  114. */
  115. static struct s3c24x0_i2c_bus *get_bus(unsigned int bus_idx)
  116. {
  117. if (bus_idx < ARRAY_SIZE(i2c_bus)) {
  118. struct s3c24x0_i2c_bus *bus;
  119. bus = &i2c_bus[bus_idx];
  120. if (bus->active)
  121. return bus;
  122. }
  123. debug("Undefined bus: %d\n", bus_idx);
  124. return NULL;
  125. }
  126. #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
  127. static int GetI2CSDA(void)
  128. {
  129. struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
  130. #ifdef CONFIG_S3C2410
  131. return (readl(&gpio->gpedat) & 0x8000) >> 15;
  132. #endif
  133. #ifdef CONFIG_S3C2400
  134. return (readl(&gpio->pgdat) & 0x0020) >> 5;
  135. #endif
  136. }
  137. static void SetI2CSCL(int x)
  138. {
  139. struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
  140. #ifdef CONFIG_S3C2410
  141. writel((readl(&gpio->gpedat) & ~0x4000) |
  142. (x & 1) << 14, &gpio->gpedat);
  143. #endif
  144. #ifdef CONFIG_S3C2400
  145. writel((readl(&gpio->pgdat) & ~0x0040) | (x & 1) << 6, &gpio->pgdat);
  146. #endif
  147. }
  148. #endif
  149. /*
  150. * Wait til the byte transfer is completed.
  151. *
  152. * @param i2c- pointer to the appropriate i2c register bank.
  153. * @return I2C_OK, if transmission was ACKED
  154. * I2C_NACK, if transmission was NACKED
  155. * I2C_NOK_TIMEOUT, if transaction did not complete in I2C_TIMEOUT_MS
  156. */
  157. static int WaitForXfer(struct s3c24x0_i2c *i2c)
  158. {
  159. ulong start_time = get_timer(0);
  160. do {
  161. if (readl(&i2c->iiccon) & I2CCON_IRPND)
  162. return (readl(&i2c->iicstat) & I2CSTAT_NACK) ?
  163. I2C_NACK : I2C_OK;
  164. } while (get_timer(start_time) < I2C_TIMEOUT_MS);
  165. return I2C_NOK_TOUT;
  166. }
  167. /*
  168. * Wait for transfer completion.
  169. *
  170. * This function reads the interrupt status register waiting for the INT_I2C
  171. * bit to be set, which indicates copletion of a transaction.
  172. *
  173. * @param i2c: pointer to the appropriate register bank
  174. *
  175. * @return: I2C_OK in case of successful completion, I2C_NOK_TIMEOUT in case
  176. * the status bits do not get set in time, or an approrpiate error
  177. * value in case of transfer errors.
  178. */
  179. static int hsi2c_wait_for_trx(struct exynos5_hsi2c *i2c)
  180. {
  181. int i = HSI2C_TIMEOUT_US;
  182. while (i-- > 0) {
  183. u32 int_status = readl(&i2c->usi_int_stat);
  184. if (int_status & HSI2C_INT_I2C_EN) {
  185. u32 trans_status = readl(&i2c->usi_trans_status);
  186. /* Deassert pending interrupt. */
  187. writel(int_status, &i2c->usi_int_stat);
  188. if (trans_status & HSI2C_NO_DEV_ACK) {
  189. debug("%s: no ACK from device\n", __func__);
  190. return I2C_NACK;
  191. }
  192. if (trans_status & HSI2C_NO_DEV) {
  193. debug("%s: no device\n", __func__);
  194. return I2C_NOK;
  195. }
  196. if (trans_status & HSI2C_TRANS_ABORT) {
  197. debug("%s: arbitration lost\n", __func__);
  198. return I2C_NOK_LA;
  199. }
  200. if (trans_status & HSI2C_TIMEOUT_AUTO) {
  201. debug("%s: device timed out\n", __func__);
  202. return I2C_NOK_TOUT;
  203. }
  204. return I2C_OK;
  205. }
  206. udelay(1);
  207. }
  208. debug("%s: transaction timeout!\n", __func__);
  209. return I2C_NOK_TOUT;
  210. }
  211. static void ReadWriteByte(struct s3c24x0_i2c *i2c)
  212. {
  213. writel(readl(&i2c->iiccon) & ~I2CCON_IRPND, &i2c->iiccon);
  214. }
  215. static struct s3c24x0_i2c *get_base_i2c(void)
  216. {
  217. #ifdef CONFIG_EXYNOS4
  218. struct s3c24x0_i2c *i2c = (struct s3c24x0_i2c *)(samsung_get_base_i2c()
  219. + (EXYNOS4_I2C_SPACING
  220. * g_current_bus));
  221. return i2c;
  222. #elif defined CONFIG_EXYNOS5
  223. struct s3c24x0_i2c *i2c = (struct s3c24x0_i2c *)(samsung_get_base_i2c()
  224. + (EXYNOS5_I2C_SPACING
  225. * g_current_bus));
  226. return i2c;
  227. #else
  228. return s3c24x0_get_base_i2c();
  229. #endif
  230. }
  231. static void i2c_ch_init(struct s3c24x0_i2c *i2c, int speed, int slaveadd)
  232. {
  233. ulong freq, pres = 16, div;
  234. #if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
  235. freq = get_i2c_clk();
  236. #else
  237. freq = get_PCLK();
  238. #endif
  239. /* calculate prescaler and divisor values */
  240. if ((freq / pres / (16 + 1)) > speed)
  241. /* set prescaler to 512 */
  242. pres = 512;
  243. div = 0;
  244. while ((freq / pres / (div + 1)) > speed)
  245. div++;
  246. /* set prescaler, divisor according to freq, also set ACKGEN, IRQ */
  247. writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon);
  248. /* init to SLAVE REVEIVE and set slaveaddr */
  249. writel(0, &i2c->iicstat);
  250. writel(slaveadd, &i2c->iicadd);
  251. /* program Master Transmit (and implicit STOP) */
  252. writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
  253. }
  254. #ifdef CONFIG_I2C_MULTI_BUS
  255. static int hsi2c_get_clk_details(struct s3c24x0_i2c_bus *i2c_bus)
  256. {
  257. struct exynos5_hsi2c *hsregs = i2c_bus->hsregs;
  258. ulong clkin;
  259. unsigned int op_clk = i2c_bus->clock_frequency;
  260. unsigned int i = 0, utemp0 = 0, utemp1 = 0;
  261. unsigned int t_ftl_cycle;
  262. #if defined CONFIG_EXYNOS5
  263. clkin = get_i2c_clk();
  264. #endif
  265. /* FPCLK / FI2C =
  266. * (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2) + 8 + 2 * FLT_CYCLE
  267. * uTemp0 = (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2)
  268. * uTemp1 = (TSCLK_L + TSCLK_H + 2)
  269. * uTemp2 = TSCLK_L + TSCLK_H
  270. */
  271. t_ftl_cycle = (readl(&hsregs->usi_conf) >> 16) & 0x7;
  272. utemp0 = (clkin / op_clk) - 8 - 2 * t_ftl_cycle;
  273. /* CLK_DIV max is 256 */
  274. for (i = 0; i < 256; i++) {
  275. utemp1 = utemp0 / (i + 1);
  276. if ((utemp1 < 512) && (utemp1 > 4)) {
  277. i2c_bus->clk_cycle = utemp1 - 2;
  278. i2c_bus->clk_div = i;
  279. return 0;
  280. }
  281. }
  282. return -1;
  283. }
  284. #endif
  285. static void hsi2c_ch_init(struct s3c24x0_i2c_bus *i2c_bus)
  286. {
  287. struct exynos5_hsi2c *hsregs = i2c_bus->hsregs;
  288. unsigned int t_sr_release;
  289. unsigned int n_clkdiv;
  290. unsigned int t_start_su, t_start_hd;
  291. unsigned int t_stop_su;
  292. unsigned int t_data_su, t_data_hd;
  293. unsigned int t_scl_l, t_scl_h;
  294. u32 i2c_timing_s1;
  295. u32 i2c_timing_s2;
  296. u32 i2c_timing_s3;
  297. u32 i2c_timing_sla;
  298. n_clkdiv = i2c_bus->clk_div;
  299. t_scl_l = i2c_bus->clk_cycle / 2;
  300. t_scl_h = i2c_bus->clk_cycle / 2;
  301. t_start_su = t_scl_l;
  302. t_start_hd = t_scl_l;
  303. t_stop_su = t_scl_l;
  304. t_data_su = t_scl_l / 2;
  305. t_data_hd = t_scl_l / 2;
  306. t_sr_release = i2c_bus->clk_cycle;
  307. i2c_timing_s1 = t_start_su << 24 | t_start_hd << 16 | t_stop_su << 8;
  308. i2c_timing_s2 = t_data_su << 24 | t_scl_l << 8 | t_scl_h << 0;
  309. i2c_timing_s3 = n_clkdiv << 16 | t_sr_release << 0;
  310. i2c_timing_sla = t_data_hd << 0;
  311. writel(HSI2C_TRAILING_COUNT, &hsregs->usi_trailing_ctl);
  312. /* Clear to enable Timeout */
  313. clrsetbits_le32(&hsregs->usi_timeout, HSI2C_TIMEOUT_EN, 0);
  314. /* set AUTO mode */
  315. writel(readl(&hsregs->usi_conf) | HSI2C_AUTO_MODE, &hsregs->usi_conf);
  316. /* Enable completion conditions' reporting. */
  317. writel(HSI2C_INT_I2C_EN, &hsregs->usi_int_en);
  318. /* Enable FIFOs */
  319. writel(HSI2C_RXFIFO_EN | HSI2C_TXFIFO_EN, &hsregs->usi_fifo_ctl);
  320. /* Currently operating in Fast speed mode. */
  321. writel(i2c_timing_s1, &hsregs->usi_timing_fs1);
  322. writel(i2c_timing_s2, &hsregs->usi_timing_fs2);
  323. writel(i2c_timing_s3, &hsregs->usi_timing_fs3);
  324. writel(i2c_timing_sla, &hsregs->usi_timing_sla);
  325. }
  326. /* SW reset for the high speed bus */
  327. static void exynos5_i2c_reset(struct s3c24x0_i2c_bus *i2c_bus)
  328. {
  329. struct exynos5_hsi2c *i2c = i2c_bus->hsregs;
  330. u32 i2c_ctl;
  331. /* Set and clear the bit for reset */
  332. i2c_ctl = readl(&i2c->usi_ctl);
  333. i2c_ctl |= HSI2C_SW_RST;
  334. writel(i2c_ctl, &i2c->usi_ctl);
  335. i2c_ctl = readl(&i2c->usi_ctl);
  336. i2c_ctl &= ~HSI2C_SW_RST;
  337. writel(i2c_ctl, &i2c->usi_ctl);
  338. /* Initialize the configure registers */
  339. hsi2c_ch_init(i2c_bus);
  340. }
  341. /*
  342. * MULTI BUS I2C support
  343. */
  344. #ifdef CONFIG_I2C_MULTI_BUS
  345. int i2c_set_bus_num(unsigned int bus)
  346. {
  347. struct s3c24x0_i2c_bus *i2c_bus;
  348. i2c_bus = get_bus(bus);
  349. if (!i2c_bus)
  350. return -1;
  351. g_current_bus = bus;
  352. if (i2c_bus->is_highspeed) {
  353. if (hsi2c_get_clk_details(i2c_bus))
  354. return -1;
  355. hsi2c_ch_init(i2c_bus);
  356. } else {
  357. i2c_ch_init(i2c_bus->regs, i2c_bus->clock_frequency,
  358. CONFIG_SYS_I2C_SLAVE);
  359. }
  360. return 0;
  361. }
  362. unsigned int i2c_get_bus_num(void)
  363. {
  364. return g_current_bus;
  365. }
  366. #endif
  367. void i2c_init(int speed, int slaveadd)
  368. {
  369. struct s3c24x0_i2c *i2c;
  370. #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
  371. struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
  372. #endif
  373. ulong start_time = get_timer(0);
  374. /* By default i2c channel 0 is the current bus */
  375. g_current_bus = 0;
  376. i2c = get_base_i2c();
  377. /*
  378. * In case the previous transfer is still going, wait to give it a
  379. * chance to finish.
  380. */
  381. while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
  382. if (get_timer(start_time) > I2C_TIMEOUT_MS) {
  383. printf("%s: I2C bus busy for %p\n", __func__,
  384. &i2c->iicstat);
  385. return;
  386. }
  387. }
  388. #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
  389. int i;
  390. if ((readl(&i2c->iicstat) & I2CSTAT_BSY) || GetI2CSDA() == 0) {
  391. #ifdef CONFIG_S3C2410
  392. ulong old_gpecon = readl(&gpio->gpecon);
  393. #endif
  394. #ifdef CONFIG_S3C2400
  395. ulong old_gpecon = readl(&gpio->pgcon);
  396. #endif
  397. /* bus still busy probably by (most) previously interrupted
  398. transfer */
  399. #ifdef CONFIG_S3C2410
  400. /* set I2CSDA and I2CSCL (GPE15, GPE14) to GPIO */
  401. writel((readl(&gpio->gpecon) & ~0xF0000000) | 0x10000000,
  402. &gpio->gpecon);
  403. #endif
  404. #ifdef CONFIG_S3C2400
  405. /* set I2CSDA and I2CSCL (PG5, PG6) to GPIO */
  406. writel((readl(&gpio->pgcon) & ~0x00003c00) | 0x00001000,
  407. &gpio->pgcon);
  408. #endif
  409. /* toggle I2CSCL until bus idle */
  410. SetI2CSCL(0);
  411. udelay(1000);
  412. i = 10;
  413. while ((i > 0) && (GetI2CSDA() != 1)) {
  414. SetI2CSCL(1);
  415. udelay(1000);
  416. SetI2CSCL(0);
  417. udelay(1000);
  418. i--;
  419. }
  420. SetI2CSCL(1);
  421. udelay(1000);
  422. /* restore pin functions */
  423. #ifdef CONFIG_S3C2410
  424. writel(old_gpecon, &gpio->gpecon);
  425. #endif
  426. #ifdef CONFIG_S3C2400
  427. writel(old_gpecon, &gpio->pgcon);
  428. #endif
  429. }
  430. #endif /* #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5) */
  431. i2c_ch_init(i2c, speed, slaveadd);
  432. }
  433. /*
  434. * Poll the appropriate bit of the fifo status register until the interface is
  435. * ready to process the next byte or timeout expires.
  436. *
  437. * In addition to the FIFO status register this function also polls the
  438. * interrupt status register to be able to detect unexpected transaction
  439. * completion.
  440. *
  441. * When FIFO is ready to process the next byte, this function returns I2C_OK.
  442. * If in course of polling the INT_I2C assertion is detected, the function
  443. * returns I2C_NOK. If timeout happens before any of the above conditions is
  444. * met - the function returns I2C_NOK_TOUT;
  445. * @param i2c: pointer to the appropriate i2c register bank.
  446. * @param rx_transfer: set to True if the receive transaction is in progress.
  447. * @return: as described above.
  448. */
  449. static unsigned hsi2c_poll_fifo(struct exynos5_hsi2c *i2c, bool rx_transfer)
  450. {
  451. u32 fifo_bit = rx_transfer ? HSI2C_RX_FIFO_EMPTY : HSI2C_TX_FIFO_FULL;
  452. int i = HSI2C_TIMEOUT_US;
  453. while (readl(&i2c->usi_fifo_stat) & fifo_bit) {
  454. if (readl(&i2c->usi_int_stat) & HSI2C_INT_I2C_EN) {
  455. /*
  456. * There is a chance that assertion of
  457. * HSI2C_INT_I2C_EN and deassertion of
  458. * HSI2C_RX_FIFO_EMPTY happen simultaneously. Let's
  459. * give FIFO status priority and check it one more
  460. * time before reporting interrupt. The interrupt will
  461. * be reported next time this function is called.
  462. */
  463. if (rx_transfer &&
  464. !(readl(&i2c->usi_fifo_stat) & fifo_bit))
  465. break;
  466. return I2C_NOK;
  467. }
  468. if (!i--) {
  469. debug("%s: FIFO polling timeout!\n", __func__);
  470. return I2C_NOK_TOUT;
  471. }
  472. udelay(1);
  473. }
  474. return I2C_OK;
  475. }
  476. /*
  477. * Preapre hsi2c transaction, either read or write.
  478. *
  479. * Set up transfer as described in section 27.5.1.2 'I2C Channel Auto Mode' of
  480. * the 5420 UM.
  481. *
  482. * @param i2c: pointer to the appropriate i2c register bank.
  483. * @param chip: slave address on the i2c bus (with read/write bit exlcuded)
  484. * @param len: number of bytes expected to be sent or received
  485. * @param rx_transfer: set to true for receive transactions
  486. * @param: issue_stop: set to true if i2c stop condition should be generated
  487. * after this transaction.
  488. * @return: I2C_NOK_TOUT in case the bus remained busy for HSI2C_TIMEOUT_US,
  489. * I2C_OK otherwise.
  490. */
  491. static int hsi2c_prepare_transaction(struct exynos5_hsi2c *i2c,
  492. u8 chip,
  493. u16 len,
  494. bool rx_transfer,
  495. bool issue_stop)
  496. {
  497. u32 conf;
  498. conf = len | HSI2C_MASTER_RUN;
  499. if (issue_stop)
  500. conf |= HSI2C_STOP_AFTER_TRANS;
  501. /* Clear to enable Timeout */
  502. writel(readl(&i2c->usi_timeout) & ~HSI2C_TIMEOUT_EN, &i2c->usi_timeout);
  503. /* Set slave address */
  504. writel(HSI2C_SLV_ADDR_MAS(chip), &i2c->i2c_addr);
  505. if (rx_transfer) {
  506. /* i2c master, read transaction */
  507. writel((HSI2C_RXCHON | HSI2C_FUNC_MODE_I2C | HSI2C_MASTER),
  508. &i2c->usi_ctl);
  509. /* read up to len bytes, stop after transaction is finished */
  510. writel(conf | HSI2C_READ_WRITE, &i2c->usi_auto_conf);
  511. } else {
  512. /* i2c master, write transaction */
  513. writel((HSI2C_TXCHON | HSI2C_FUNC_MODE_I2C | HSI2C_MASTER),
  514. &i2c->usi_ctl);
  515. /* write up to len bytes, stop after transaction is finished */
  516. writel(conf, &i2c->usi_auto_conf);
  517. }
  518. /* Reset all pending interrupt status bits we care about, if any */
  519. writel(HSI2C_INT_I2C_EN, &i2c->usi_int_stat);
  520. return I2C_OK;
  521. }
  522. /*
  523. * Wait while i2c bus is settling down (mostly stop gets completed).
  524. */
  525. static int hsi2c_wait_while_busy(struct exynos5_hsi2c *i2c)
  526. {
  527. int i = HSI2C_TIMEOUT_US;
  528. while (readl(&i2c->usi_trans_status) & HSI2C_MASTER_BUSY) {
  529. if (!i--) {
  530. debug("%s: bus busy\n", __func__);
  531. return I2C_NOK_TOUT;
  532. }
  533. udelay(1);
  534. }
  535. return I2C_OK;
  536. }
  537. static int hsi2c_write(struct exynos5_hsi2c *i2c,
  538. unsigned char chip,
  539. unsigned char addr[],
  540. unsigned char alen,
  541. unsigned char data[],
  542. unsigned short len,
  543. bool issue_stop)
  544. {
  545. int i, rv = 0;
  546. if (!(len + alen)) {
  547. /* Writes of zero length not supported in auto mode. */
  548. debug("%s: zero length writes not supported\n", __func__);
  549. return I2C_NOK;
  550. }
  551. rv = hsi2c_prepare_transaction
  552. (i2c, chip, len + alen, false, issue_stop);
  553. if (rv != I2C_OK)
  554. return rv;
  555. /* Move address, if any, and the data, if any, into the FIFO. */
  556. for (i = 0; i < alen; i++) {
  557. rv = hsi2c_poll_fifo(i2c, false);
  558. if (rv != I2C_OK) {
  559. debug("%s: address write failed\n", __func__);
  560. goto write_error;
  561. }
  562. writel(addr[i], &i2c->usi_txdata);
  563. }
  564. for (i = 0; i < len; i++) {
  565. rv = hsi2c_poll_fifo(i2c, false);
  566. if (rv != I2C_OK) {
  567. debug("%s: data write failed\n", __func__);
  568. goto write_error;
  569. }
  570. writel(data[i], &i2c->usi_txdata);
  571. }
  572. rv = hsi2c_wait_for_trx(i2c);
  573. write_error:
  574. if (issue_stop) {
  575. int tmp_ret = hsi2c_wait_while_busy(i2c);
  576. if (rv == I2C_OK)
  577. rv = tmp_ret;
  578. }
  579. writel(HSI2C_FUNC_MODE_I2C, &i2c->usi_ctl); /* done */
  580. return rv;
  581. }
  582. static int hsi2c_read(struct exynos5_hsi2c *i2c,
  583. unsigned char chip,
  584. unsigned char addr[],
  585. unsigned char alen,
  586. unsigned char data[],
  587. unsigned short len)
  588. {
  589. int i, rv, tmp_ret;
  590. bool drop_data = false;
  591. if (!len) {
  592. /* Reads of zero length not supported in auto mode. */
  593. debug("%s: zero length read adjusted\n", __func__);
  594. drop_data = true;
  595. len = 1;
  596. }
  597. if (alen) {
  598. /* Internal register adress needs to be written first. */
  599. rv = hsi2c_write(i2c, chip, addr, alen, NULL, 0, false);
  600. if (rv != I2C_OK)
  601. return rv;
  602. }
  603. rv = hsi2c_prepare_transaction(i2c, chip, len, true, true);
  604. if (rv != I2C_OK)
  605. return rv;
  606. for (i = 0; i < len; i++) {
  607. rv = hsi2c_poll_fifo(i2c, true);
  608. if (rv != I2C_OK)
  609. goto read_err;
  610. if (drop_data)
  611. continue;
  612. data[i] = readl(&i2c->usi_rxdata);
  613. }
  614. rv = hsi2c_wait_for_trx(i2c);
  615. read_err:
  616. tmp_ret = hsi2c_wait_while_busy(i2c);
  617. if (rv == I2C_OK)
  618. rv = tmp_ret;
  619. writel(HSI2C_FUNC_MODE_I2C, &i2c->usi_ctl); /* done */
  620. return rv;
  621. }
  622. /*
  623. * cmd_type is 0 for write, 1 for read.
  624. *
  625. * addr_len can take any value from 0-255, it is only limited
  626. * by the char, we could make it larger if needed. If it is
  627. * 0 we skip the address write cycle.
  628. */
  629. static int i2c_transfer(struct s3c24x0_i2c *i2c,
  630. unsigned char cmd_type,
  631. unsigned char chip,
  632. unsigned char addr[],
  633. unsigned char addr_len,
  634. unsigned char data[],
  635. unsigned short data_len)
  636. {
  637. int i = 0, result;
  638. ulong start_time = get_timer(0);
  639. if (data == 0 || data_len == 0) {
  640. /*Don't support data transfer of no length or to address 0 */
  641. debug("i2c_transfer: bad call\n");
  642. return I2C_NOK;
  643. }
  644. while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
  645. if (get_timer(start_time) > I2C_TIMEOUT_MS)
  646. return I2C_NOK_TOUT;
  647. }
  648. writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon);
  649. /* Get the slave chip address going */
  650. writel(chip, &i2c->iicds);
  651. if ((cmd_type == I2C_WRITE) || (addr && addr_len))
  652. writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
  653. &i2c->iicstat);
  654. else
  655. writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
  656. &i2c->iicstat);
  657. /* Wait for chip address to transmit. */
  658. result = WaitForXfer(i2c);
  659. if (result != I2C_OK)
  660. goto bailout;
  661. /* If register address needs to be transmitted - do it now. */
  662. if (addr && addr_len) {
  663. while ((i < addr_len) && (result == I2C_OK)) {
  664. writel(addr[i++], &i2c->iicds);
  665. ReadWriteByte(i2c);
  666. result = WaitForXfer(i2c);
  667. }
  668. i = 0;
  669. if (result != I2C_OK)
  670. goto bailout;
  671. }
  672. switch (cmd_type) {
  673. case I2C_WRITE:
  674. while ((i < data_len) && (result == I2C_OK)) {
  675. writel(data[i++], &i2c->iicds);
  676. ReadWriteByte(i2c);
  677. result = WaitForXfer(i2c);
  678. }
  679. break;
  680. case I2C_READ:
  681. if (addr && addr_len) {
  682. /*
  683. * Register address has been sent, now send slave chip
  684. * address again to start the actual read transaction.
  685. */
  686. writel(chip, &i2c->iicds);
  687. /* Generate a re-START. */
  688. writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
  689. &i2c->iicstat);
  690. ReadWriteByte(i2c);
  691. result = WaitForXfer(i2c);
  692. if (result != I2C_OK)
  693. goto bailout;
  694. }
  695. while ((i < data_len) && (result == I2C_OK)) {
  696. /* disable ACK for final READ */
  697. if (i == data_len - 1)
  698. writel(readl(&i2c->iiccon)
  699. & ~I2CCON_ACKGEN,
  700. &i2c->iiccon);
  701. ReadWriteByte(i2c);
  702. result = WaitForXfer(i2c);
  703. data[i++] = readl(&i2c->iicds);
  704. }
  705. if (result == I2C_NACK)
  706. result = I2C_OK; /* Normal terminated read. */
  707. break;
  708. default:
  709. debug("i2c_transfer: bad call\n");
  710. result = I2C_NOK;
  711. break;
  712. }
  713. bailout:
  714. /* Send STOP. */
  715. writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
  716. ReadWriteByte(i2c);
  717. return result;
  718. }
  719. int i2c_probe(uchar chip)
  720. {
  721. struct s3c24x0_i2c_bus *i2c_bus;
  722. uchar buf[1];
  723. int ret;
  724. i2c_bus = get_bus(g_current_bus);
  725. if (!i2c_bus)
  726. return -1;
  727. buf[0] = 0;
  728. /*
  729. * What is needed is to send the chip address and verify that the
  730. * address was <ACK>ed (i.e. there was a chip at that address which
  731. * drove the data line low).
  732. */
  733. if (i2c_bus->is_highspeed) {
  734. ret = hsi2c_read(i2c_bus->hsregs,
  735. chip, 0, 0, buf, 1);
  736. } else {
  737. ret = i2c_transfer(i2c_bus->regs,
  738. I2C_READ, chip << 1, 0, 0, buf, 1);
  739. }
  740. return ret != I2C_OK;
  741. }
  742. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  743. {
  744. struct s3c24x0_i2c_bus *i2c_bus;
  745. uchar xaddr[4];
  746. int ret;
  747. if (alen > 4) {
  748. debug("I2C read: addr len %d not supported\n", alen);
  749. return 1;
  750. }
  751. if (alen > 0) {
  752. xaddr[0] = (addr >> 24) & 0xFF;
  753. xaddr[1] = (addr >> 16) & 0xFF;
  754. xaddr[2] = (addr >> 8) & 0xFF;
  755. xaddr[3] = addr & 0xFF;
  756. }
  757. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  758. /*
  759. * EEPROM chips that implement "address overflow" are ones
  760. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  761. * address and the extra bits end up in the "chip address"
  762. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  763. * four 256 byte chips.
  764. *
  765. * Note that we consider the length of the address field to
  766. * still be one byte because the extra address bits are
  767. * hidden in the chip address.
  768. */
  769. if (alen > 0)
  770. chip |= ((addr >> (alen * 8)) &
  771. CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  772. #endif
  773. i2c_bus = get_bus(g_current_bus);
  774. if (!i2c_bus)
  775. return -1;
  776. if (i2c_bus->is_highspeed)
  777. ret = hsi2c_read(i2c_bus->hsregs, chip, &xaddr[4 - alen],
  778. alen, buffer, len);
  779. else
  780. ret = i2c_transfer(i2c_bus->regs, I2C_READ, chip << 1,
  781. &xaddr[4 - alen], alen, buffer, len);
  782. if (ret) {
  783. if (i2c_bus->is_highspeed)
  784. exynos5_i2c_reset(i2c_bus);
  785. debug("I2c read failed %d\n", ret);
  786. return 1;
  787. }
  788. return 0;
  789. }
  790. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  791. {
  792. struct s3c24x0_i2c_bus *i2c_bus;
  793. uchar xaddr[4];
  794. int ret;
  795. if (alen > 4) {
  796. debug("I2C write: addr len %d not supported\n", alen);
  797. return 1;
  798. }
  799. if (alen > 0) {
  800. xaddr[0] = (addr >> 24) & 0xFF;
  801. xaddr[1] = (addr >> 16) & 0xFF;
  802. xaddr[2] = (addr >> 8) & 0xFF;
  803. xaddr[3] = addr & 0xFF;
  804. }
  805. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  806. /*
  807. * EEPROM chips that implement "address overflow" are ones
  808. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  809. * address and the extra bits end up in the "chip address"
  810. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  811. * four 256 byte chips.
  812. *
  813. * Note that we consider the length of the address field to
  814. * still be one byte because the extra address bits are
  815. * hidden in the chip address.
  816. */
  817. if (alen > 0)
  818. chip |= ((addr >> (alen * 8)) &
  819. CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  820. #endif
  821. i2c_bus = get_bus(g_current_bus);
  822. if (!i2c_bus)
  823. return -1;
  824. if (i2c_bus->is_highspeed)
  825. ret = hsi2c_write(i2c_bus->hsregs, chip, &xaddr[4 - alen],
  826. alen, buffer, len, true);
  827. else
  828. ret = i2c_transfer(i2c_bus->regs, I2C_WRITE, chip << 1,
  829. &xaddr[4 - alen], alen, buffer, len);
  830. if (ret != 0) {
  831. if (i2c_bus->is_highspeed)
  832. exynos5_i2c_reset(i2c_bus);
  833. return 1;
  834. } else {
  835. return 0;
  836. }
  837. }
  838. #ifdef CONFIG_OF_CONTROL
  839. static void process_nodes(const void *blob, int node_list[], int count,
  840. int is_highspeed)
  841. {
  842. struct s3c24x0_i2c_bus *bus;
  843. int i;
  844. for (i = 0; i < count; i++) {
  845. int node = node_list[i];
  846. if (node <= 0)
  847. continue;
  848. bus = &i2c_bus[i];
  849. bus->active = true;
  850. bus->is_highspeed = is_highspeed;
  851. if (is_highspeed)
  852. bus->hsregs = (struct exynos5_hsi2c *)
  853. fdtdec_get_addr(blob, node, "reg");
  854. else
  855. bus->regs = (struct s3c24x0_i2c *)
  856. fdtdec_get_addr(blob, node, "reg");
  857. bus->id = pinmux_decode_periph_id(blob, node);
  858. bus->clock_frequency = fdtdec_get_int(blob, node,
  859. "clock-frequency",
  860. CONFIG_SYS_I2C_SPEED);
  861. bus->node = node;
  862. bus->bus_num = i;
  863. exynos_pinmux_config(bus->id, 0);
  864. /* Mark position as used */
  865. node_list[i] = -1;
  866. }
  867. }
  868. void board_i2c_init(const void *blob)
  869. {
  870. int node_list[CONFIG_MAX_I2C_NUM];
  871. int count;
  872. /* First get the normal i2c ports */
  873. count = fdtdec_find_aliases_for_id(blob, "i2c",
  874. COMPAT_SAMSUNG_S3C2440_I2C, node_list,
  875. CONFIG_MAX_I2C_NUM);
  876. process_nodes(blob, node_list, count, 0);
  877. /* Now look for high speed i2c ports */
  878. count = fdtdec_find_aliases_for_id(blob, "i2c",
  879. COMPAT_SAMSUNG_EXYNOS5_I2C, node_list,
  880. CONFIG_MAX_I2C_NUM);
  881. process_nodes(blob, node_list, count, 1);
  882. }
  883. int i2c_get_bus_num_fdt(int node)
  884. {
  885. int i;
  886. for (i = 0; i < ARRAY_SIZE(i2c_bus); i++) {
  887. if (node == i2c_bus[i].node)
  888. return i;
  889. }
  890. debug("%s: Can't find any matched I2C bus\n", __func__);
  891. return -1;
  892. }
  893. #ifdef CONFIG_I2C_MULTI_BUS
  894. int i2c_reset_port_fdt(const void *blob, int node)
  895. {
  896. struct s3c24x0_i2c_bus *i2c_bus;
  897. int bus;
  898. bus = i2c_get_bus_num_fdt(node);
  899. if (bus < 0) {
  900. debug("could not get bus for node %d\n", node);
  901. return -1;
  902. }
  903. i2c_bus = get_bus(bus);
  904. if (!i2c_bus) {
  905. debug("get_bus() failed for node node %d\n", node);
  906. return -1;
  907. }
  908. if (i2c_bus->is_highspeed) {
  909. if (hsi2c_get_clk_details(i2c_bus))
  910. return -1;
  911. hsi2c_ch_init(i2c_bus);
  912. } else {
  913. i2c_ch_init(i2c_bus->regs, i2c_bus->clock_frequency,
  914. CONFIG_SYS_I2C_SLAVE);
  915. }
  916. return 0;
  917. }
  918. #endif
  919. #endif
  920. #endif /* CONFIG_HARD_I2C */