s3c24x0_i2c.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  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. /* To support VCMA9 boards and other who dont define max_i2c_num */
  100. #ifndef CONFIG_MAX_I2C_NUM
  101. #define CONFIG_MAX_I2C_NUM 1
  102. #endif
  103. DECLARE_GLOBAL_DATA_PTR;
  104. /*
  105. * For SPL boot some boards need i2c before SDRAM is initialised so force
  106. * variables to live in SRAM
  107. */
  108. #ifdef CONFIG_SYS_I2C
  109. static struct s3c24x0_i2c_bus i2c_bus[CONFIG_MAX_I2C_NUM]
  110. __attribute__((section(".data")));
  111. #endif
  112. enum exynos_i2c_type {
  113. EXYNOS_I2C_STD,
  114. EXYNOS_I2C_HS,
  115. };
  116. #ifdef CONFIG_SYS_I2C
  117. /**
  118. * Get a pointer to the given bus index
  119. *
  120. * @bus_idx: Bus index to look up
  121. * @return pointer to bus, or NULL if invalid or not available
  122. */
  123. static struct s3c24x0_i2c_bus *get_bus(unsigned int bus_idx)
  124. {
  125. if (bus_idx < ARRAY_SIZE(i2c_bus)) {
  126. struct s3c24x0_i2c_bus *bus;
  127. bus = &i2c_bus[bus_idx];
  128. if (bus->active)
  129. return bus;
  130. }
  131. debug("Undefined bus: %d\n", bus_idx);
  132. return NULL;
  133. }
  134. #endif
  135. #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
  136. static int GetI2CSDA(void)
  137. {
  138. struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
  139. #ifdef CONFIG_S3C2410
  140. return (readl(&gpio->gpedat) & 0x8000) >> 15;
  141. #endif
  142. #ifdef CONFIG_S3C2400
  143. return (readl(&gpio->pgdat) & 0x0020) >> 5;
  144. #endif
  145. }
  146. static void SetI2CSCL(int x)
  147. {
  148. struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
  149. #ifdef CONFIG_S3C2410
  150. writel((readl(&gpio->gpedat) & ~0x4000) |
  151. (x & 1) << 14, &gpio->gpedat);
  152. #endif
  153. #ifdef CONFIG_S3C2400
  154. writel((readl(&gpio->pgdat) & ~0x0040) | (x & 1) << 6, &gpio->pgdat);
  155. #endif
  156. }
  157. #endif
  158. /*
  159. * Wait til the byte transfer is completed.
  160. *
  161. * @param i2c- pointer to the appropriate i2c register bank.
  162. * @return I2C_OK, if transmission was ACKED
  163. * I2C_NACK, if transmission was NACKED
  164. * I2C_NOK_TIMEOUT, if transaction did not complete in I2C_TIMEOUT_MS
  165. */
  166. static int WaitForXfer(struct s3c24x0_i2c *i2c)
  167. {
  168. ulong start_time = get_timer(0);
  169. do {
  170. if (readl(&i2c->iiccon) & I2CCON_IRPND)
  171. return (readl(&i2c->iicstat) & I2CSTAT_NACK) ?
  172. I2C_NACK : I2C_OK;
  173. } while (get_timer(start_time) < I2C_TIMEOUT_MS);
  174. return I2C_NOK_TOUT;
  175. }
  176. /*
  177. * Wait for transfer completion.
  178. *
  179. * This function reads the interrupt status register waiting for the INT_I2C
  180. * bit to be set, which indicates copletion of a transaction.
  181. *
  182. * @param i2c: pointer to the appropriate register bank
  183. *
  184. * @return: I2C_OK in case of successful completion, I2C_NOK_TIMEOUT in case
  185. * the status bits do not get set in time, or an approrpiate error
  186. * value in case of transfer errors.
  187. */
  188. static int hsi2c_wait_for_trx(struct exynos5_hsi2c *i2c)
  189. {
  190. int i = HSI2C_TIMEOUT_US;
  191. while (i-- > 0) {
  192. u32 int_status = readl(&i2c->usi_int_stat);
  193. if (int_status & HSI2C_INT_I2C_EN) {
  194. u32 trans_status = readl(&i2c->usi_trans_status);
  195. /* Deassert pending interrupt. */
  196. writel(int_status, &i2c->usi_int_stat);
  197. if (trans_status & HSI2C_NO_DEV_ACK) {
  198. debug("%s: no ACK from device\n", __func__);
  199. return I2C_NACK;
  200. }
  201. if (trans_status & HSI2C_NO_DEV) {
  202. debug("%s: no device\n", __func__);
  203. return I2C_NOK;
  204. }
  205. if (trans_status & HSI2C_TRANS_ABORT) {
  206. debug("%s: arbitration lost\n", __func__);
  207. return I2C_NOK_LA;
  208. }
  209. if (trans_status & HSI2C_TIMEOUT_AUTO) {
  210. debug("%s: device timed out\n", __func__);
  211. return I2C_NOK_TOUT;
  212. }
  213. return I2C_OK;
  214. }
  215. udelay(1);
  216. }
  217. debug("%s: transaction timeout!\n", __func__);
  218. return I2C_NOK_TOUT;
  219. }
  220. static void ReadWriteByte(struct s3c24x0_i2c *i2c)
  221. {
  222. writel(readl(&i2c->iiccon) & ~I2CCON_IRPND, &i2c->iiccon);
  223. }
  224. #ifdef CONFIG_SYS_I2C
  225. static struct s3c24x0_i2c *get_base_i2c(int bus)
  226. {
  227. #ifdef CONFIG_EXYNOS4
  228. struct s3c24x0_i2c *i2c = (struct s3c24x0_i2c *)(samsung_get_base_i2c()
  229. + (EXYNOS4_I2C_SPACING
  230. * bus));
  231. return i2c;
  232. #elif defined CONFIG_EXYNOS5
  233. struct s3c24x0_i2c *i2c = (struct s3c24x0_i2c *)(samsung_get_base_i2c()
  234. + (EXYNOS5_I2C_SPACING
  235. * bus));
  236. return i2c;
  237. #else
  238. return s3c24x0_get_base_i2c();
  239. #endif
  240. }
  241. #endif
  242. static void i2c_ch_init(struct s3c24x0_i2c *i2c, int speed, int slaveadd)
  243. {
  244. ulong freq, pres = 16, div;
  245. #if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
  246. freq = get_i2c_clk();
  247. #else
  248. freq = get_PCLK();
  249. #endif
  250. /* calculate prescaler and divisor values */
  251. if ((freq / pres / (16 + 1)) > speed)
  252. /* set prescaler to 512 */
  253. pres = 512;
  254. div = 0;
  255. while ((freq / pres / (div + 1)) > speed)
  256. div++;
  257. /* set prescaler, divisor according to freq, also set ACKGEN, IRQ */
  258. writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon);
  259. /* init to SLAVE REVEIVE and set slaveaddr */
  260. writel(0, &i2c->iicstat);
  261. writel(slaveadd, &i2c->iicadd);
  262. /* program Master Transmit (and implicit STOP) */
  263. writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
  264. }
  265. static int hsi2c_get_clk_details(struct s3c24x0_i2c_bus *i2c_bus)
  266. {
  267. struct exynos5_hsi2c *hsregs = i2c_bus->hsregs;
  268. ulong clkin;
  269. unsigned int op_clk = i2c_bus->clock_frequency;
  270. unsigned int i = 0, utemp0 = 0, utemp1 = 0;
  271. unsigned int t_ftl_cycle;
  272. #if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
  273. clkin = get_i2c_clk();
  274. #else
  275. clkin = get_PCLK();
  276. #endif
  277. /* FPCLK / FI2C =
  278. * (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2) + 8 + 2 * FLT_CYCLE
  279. * uTemp0 = (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2)
  280. * uTemp1 = (TSCLK_L + TSCLK_H + 2)
  281. * uTemp2 = TSCLK_L + TSCLK_H
  282. */
  283. t_ftl_cycle = (readl(&hsregs->usi_conf) >> 16) & 0x7;
  284. utemp0 = (clkin / op_clk) - 8 - 2 * t_ftl_cycle;
  285. /* CLK_DIV max is 256 */
  286. for (i = 0; i < 256; i++) {
  287. utemp1 = utemp0 / (i + 1);
  288. if ((utemp1 < 512) && (utemp1 > 4)) {
  289. i2c_bus->clk_cycle = utemp1 - 2;
  290. i2c_bus->clk_div = i;
  291. return 0;
  292. }
  293. }
  294. return -EINVAL;
  295. }
  296. static void hsi2c_ch_init(struct s3c24x0_i2c_bus *i2c_bus)
  297. {
  298. struct exynos5_hsi2c *hsregs = i2c_bus->hsregs;
  299. unsigned int t_sr_release;
  300. unsigned int n_clkdiv;
  301. unsigned int t_start_su, t_start_hd;
  302. unsigned int t_stop_su;
  303. unsigned int t_data_su, t_data_hd;
  304. unsigned int t_scl_l, t_scl_h;
  305. u32 i2c_timing_s1;
  306. u32 i2c_timing_s2;
  307. u32 i2c_timing_s3;
  308. u32 i2c_timing_sla;
  309. n_clkdiv = i2c_bus->clk_div;
  310. t_scl_l = i2c_bus->clk_cycle / 2;
  311. t_scl_h = i2c_bus->clk_cycle / 2;
  312. t_start_su = t_scl_l;
  313. t_start_hd = t_scl_l;
  314. t_stop_su = t_scl_l;
  315. t_data_su = t_scl_l / 2;
  316. t_data_hd = t_scl_l / 2;
  317. t_sr_release = i2c_bus->clk_cycle;
  318. i2c_timing_s1 = t_start_su << 24 | t_start_hd << 16 | t_stop_su << 8;
  319. i2c_timing_s2 = t_data_su << 24 | t_scl_l << 8 | t_scl_h << 0;
  320. i2c_timing_s3 = n_clkdiv << 16 | t_sr_release << 0;
  321. i2c_timing_sla = t_data_hd << 0;
  322. writel(HSI2C_TRAILING_COUNT, &hsregs->usi_trailing_ctl);
  323. /* Clear to enable Timeout */
  324. clrsetbits_le32(&hsregs->usi_timeout, HSI2C_TIMEOUT_EN, 0);
  325. /* set AUTO mode */
  326. writel(readl(&hsregs->usi_conf) | HSI2C_AUTO_MODE, &hsregs->usi_conf);
  327. /* Enable completion conditions' reporting. */
  328. writel(HSI2C_INT_I2C_EN, &hsregs->usi_int_en);
  329. /* Enable FIFOs */
  330. writel(HSI2C_RXFIFO_EN | HSI2C_TXFIFO_EN, &hsregs->usi_fifo_ctl);
  331. /* Currently operating in Fast speed mode. */
  332. writel(i2c_timing_s1, &hsregs->usi_timing_fs1);
  333. writel(i2c_timing_s2, &hsregs->usi_timing_fs2);
  334. writel(i2c_timing_s3, &hsregs->usi_timing_fs3);
  335. writel(i2c_timing_sla, &hsregs->usi_timing_sla);
  336. }
  337. /* SW reset for the high speed bus */
  338. static void exynos5_i2c_reset(struct s3c24x0_i2c_bus *i2c_bus)
  339. {
  340. struct exynos5_hsi2c *i2c = i2c_bus->hsregs;
  341. u32 i2c_ctl;
  342. /* Set and clear the bit for reset */
  343. i2c_ctl = readl(&i2c->usi_ctl);
  344. i2c_ctl |= HSI2C_SW_RST;
  345. writel(i2c_ctl, &i2c->usi_ctl);
  346. i2c_ctl = readl(&i2c->usi_ctl);
  347. i2c_ctl &= ~HSI2C_SW_RST;
  348. writel(i2c_ctl, &i2c->usi_ctl);
  349. /* Initialize the configure registers */
  350. hsi2c_ch_init(i2c_bus);
  351. }
  352. #ifdef CONFIG_SYS_I2C
  353. static void s3c24x0_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
  354. {
  355. struct s3c24x0_i2c *i2c;
  356. struct s3c24x0_i2c_bus *bus;
  357. #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
  358. struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
  359. #endif
  360. ulong start_time = get_timer(0);
  361. i2c = get_base_i2c(adap->hwadapnr);
  362. bus = &i2c_bus[adap->hwadapnr];
  363. if (!bus)
  364. return;
  365. /*
  366. * In case the previous transfer is still going, wait to give it a
  367. * chance to finish.
  368. */
  369. while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
  370. if (get_timer(start_time) > I2C_TIMEOUT_MS) {
  371. printf("%s: I2C bus busy for %p\n", __func__,
  372. &i2c->iicstat);
  373. return;
  374. }
  375. }
  376. #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
  377. int i;
  378. if ((readl(&i2c->iicstat) & I2CSTAT_BSY) || GetI2CSDA() == 0) {
  379. #ifdef CONFIG_S3C2410
  380. ulong old_gpecon = readl(&gpio->gpecon);
  381. #endif
  382. #ifdef CONFIG_S3C2400
  383. ulong old_gpecon = readl(&gpio->pgcon);
  384. #endif
  385. /* bus still busy probably by (most) previously interrupted
  386. transfer */
  387. #ifdef CONFIG_S3C2410
  388. /* set I2CSDA and I2CSCL (GPE15, GPE14) to GPIO */
  389. writel((readl(&gpio->gpecon) & ~0xF0000000) | 0x10000000,
  390. &gpio->gpecon);
  391. #endif
  392. #ifdef CONFIG_S3C2400
  393. /* set I2CSDA and I2CSCL (PG5, PG6) to GPIO */
  394. writel((readl(&gpio->pgcon) & ~0x00003c00) | 0x00001000,
  395. &gpio->pgcon);
  396. #endif
  397. /* toggle I2CSCL until bus idle */
  398. SetI2CSCL(0);
  399. udelay(1000);
  400. i = 10;
  401. while ((i > 0) && (GetI2CSDA() != 1)) {
  402. SetI2CSCL(1);
  403. udelay(1000);
  404. SetI2CSCL(0);
  405. udelay(1000);
  406. i--;
  407. }
  408. SetI2CSCL(1);
  409. udelay(1000);
  410. /* restore pin functions */
  411. #ifdef CONFIG_S3C2410
  412. writel(old_gpecon, &gpio->gpecon);
  413. #endif
  414. #ifdef CONFIG_S3C2400
  415. writel(old_gpecon, &gpio->pgcon);
  416. #endif
  417. }
  418. #endif /* #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5) */
  419. i2c_ch_init(i2c, speed, slaveadd);
  420. bus->active = true;
  421. bus->regs = i2c;
  422. }
  423. #endif /* CONFIG_SYS_I2C */
  424. /*
  425. * Poll the appropriate bit of the fifo status register until the interface is
  426. * ready to process the next byte or timeout expires.
  427. *
  428. * In addition to the FIFO status register this function also polls the
  429. * interrupt status register to be able to detect unexpected transaction
  430. * completion.
  431. *
  432. * When FIFO is ready to process the next byte, this function returns I2C_OK.
  433. * If in course of polling the INT_I2C assertion is detected, the function
  434. * returns I2C_NOK. If timeout happens before any of the above conditions is
  435. * met - the function returns I2C_NOK_TOUT;
  436. * @param i2c: pointer to the appropriate i2c register bank.
  437. * @param rx_transfer: set to True if the receive transaction is in progress.
  438. * @return: as described above.
  439. */
  440. static unsigned hsi2c_poll_fifo(struct exynos5_hsi2c *i2c, bool rx_transfer)
  441. {
  442. u32 fifo_bit = rx_transfer ? HSI2C_RX_FIFO_EMPTY : HSI2C_TX_FIFO_FULL;
  443. int i = HSI2C_TIMEOUT_US;
  444. while (readl(&i2c->usi_fifo_stat) & fifo_bit) {
  445. if (readl(&i2c->usi_int_stat) & HSI2C_INT_I2C_EN) {
  446. /*
  447. * There is a chance that assertion of
  448. * HSI2C_INT_I2C_EN and deassertion of
  449. * HSI2C_RX_FIFO_EMPTY happen simultaneously. Let's
  450. * give FIFO status priority and check it one more
  451. * time before reporting interrupt. The interrupt will
  452. * be reported next time this function is called.
  453. */
  454. if (rx_transfer &&
  455. !(readl(&i2c->usi_fifo_stat) & fifo_bit))
  456. break;
  457. return I2C_NOK;
  458. }
  459. if (!i--) {
  460. debug("%s: FIFO polling timeout!\n", __func__);
  461. return I2C_NOK_TOUT;
  462. }
  463. udelay(1);
  464. }
  465. return I2C_OK;
  466. }
  467. /*
  468. * Preapre hsi2c transaction, either read or write.
  469. *
  470. * Set up transfer as described in section 27.5.1.2 'I2C Channel Auto Mode' of
  471. * the 5420 UM.
  472. *
  473. * @param i2c: pointer to the appropriate i2c register bank.
  474. * @param chip: slave address on the i2c bus (with read/write bit exlcuded)
  475. * @param len: number of bytes expected to be sent or received
  476. * @param rx_transfer: set to true for receive transactions
  477. * @param: issue_stop: set to true if i2c stop condition should be generated
  478. * after this transaction.
  479. * @return: I2C_NOK_TOUT in case the bus remained busy for HSI2C_TIMEOUT_US,
  480. * I2C_OK otherwise.
  481. */
  482. static int hsi2c_prepare_transaction(struct exynos5_hsi2c *i2c,
  483. u8 chip,
  484. u16 len,
  485. bool rx_transfer,
  486. bool issue_stop)
  487. {
  488. u32 conf;
  489. conf = len | HSI2C_MASTER_RUN;
  490. if (issue_stop)
  491. conf |= HSI2C_STOP_AFTER_TRANS;
  492. /* Clear to enable Timeout */
  493. writel(readl(&i2c->usi_timeout) & ~HSI2C_TIMEOUT_EN, &i2c->usi_timeout);
  494. /* Set slave address */
  495. writel(HSI2C_SLV_ADDR_MAS(chip), &i2c->i2c_addr);
  496. if (rx_transfer) {
  497. /* i2c master, read transaction */
  498. writel((HSI2C_RXCHON | HSI2C_FUNC_MODE_I2C | HSI2C_MASTER),
  499. &i2c->usi_ctl);
  500. /* read up to len bytes, stop after transaction is finished */
  501. writel(conf | HSI2C_READ_WRITE, &i2c->usi_auto_conf);
  502. } else {
  503. /* i2c master, write transaction */
  504. writel((HSI2C_TXCHON | HSI2C_FUNC_MODE_I2C | HSI2C_MASTER),
  505. &i2c->usi_ctl);
  506. /* write up to len bytes, stop after transaction is finished */
  507. writel(conf, &i2c->usi_auto_conf);
  508. }
  509. /* Reset all pending interrupt status bits we care about, if any */
  510. writel(HSI2C_INT_I2C_EN, &i2c->usi_int_stat);
  511. return I2C_OK;
  512. }
  513. /*
  514. * Wait while i2c bus is settling down (mostly stop gets completed).
  515. */
  516. static int hsi2c_wait_while_busy(struct exynos5_hsi2c *i2c)
  517. {
  518. int i = HSI2C_TIMEOUT_US;
  519. while (readl(&i2c->usi_trans_status) & HSI2C_MASTER_BUSY) {
  520. if (!i--) {
  521. debug("%s: bus busy\n", __func__);
  522. return I2C_NOK_TOUT;
  523. }
  524. udelay(1);
  525. }
  526. return I2C_OK;
  527. }
  528. static int hsi2c_write(struct exynos5_hsi2c *i2c,
  529. unsigned char chip,
  530. unsigned char addr[],
  531. unsigned char alen,
  532. unsigned char data[],
  533. unsigned short len,
  534. bool issue_stop)
  535. {
  536. int i, rv = 0;
  537. if (!(len + alen)) {
  538. /* Writes of zero length not supported in auto mode. */
  539. debug("%s: zero length writes not supported\n", __func__);
  540. return I2C_NOK;
  541. }
  542. rv = hsi2c_prepare_transaction
  543. (i2c, chip, len + alen, false, issue_stop);
  544. if (rv != I2C_OK)
  545. return rv;
  546. /* Move address, if any, and the data, if any, into the FIFO. */
  547. for (i = 0; i < alen; i++) {
  548. rv = hsi2c_poll_fifo(i2c, false);
  549. if (rv != I2C_OK) {
  550. debug("%s: address write failed\n", __func__);
  551. goto write_error;
  552. }
  553. writel(addr[i], &i2c->usi_txdata);
  554. }
  555. for (i = 0; i < len; i++) {
  556. rv = hsi2c_poll_fifo(i2c, false);
  557. if (rv != I2C_OK) {
  558. debug("%s: data write failed\n", __func__);
  559. goto write_error;
  560. }
  561. writel(data[i], &i2c->usi_txdata);
  562. }
  563. rv = hsi2c_wait_for_trx(i2c);
  564. write_error:
  565. if (issue_stop) {
  566. int tmp_ret = hsi2c_wait_while_busy(i2c);
  567. if (rv == I2C_OK)
  568. rv = tmp_ret;
  569. }
  570. writel(HSI2C_FUNC_MODE_I2C, &i2c->usi_ctl); /* done */
  571. return rv;
  572. }
  573. static int hsi2c_read(struct exynos5_hsi2c *i2c,
  574. unsigned char chip,
  575. unsigned char addr[],
  576. unsigned char alen,
  577. unsigned char data[],
  578. unsigned short len)
  579. {
  580. int i, rv, tmp_ret;
  581. bool drop_data = false;
  582. if (!len) {
  583. /* Reads of zero length not supported in auto mode. */
  584. debug("%s: zero length read adjusted\n", __func__);
  585. drop_data = true;
  586. len = 1;
  587. }
  588. if (alen) {
  589. /* Internal register adress needs to be written first. */
  590. rv = hsi2c_write(i2c, chip, addr, alen, NULL, 0, false);
  591. if (rv != I2C_OK)
  592. return rv;
  593. }
  594. rv = hsi2c_prepare_transaction(i2c, chip, len, true, true);
  595. if (rv != I2C_OK)
  596. return rv;
  597. for (i = 0; i < len; i++) {
  598. rv = hsi2c_poll_fifo(i2c, true);
  599. if (rv != I2C_OK)
  600. goto read_err;
  601. if (drop_data)
  602. continue;
  603. data[i] = readl(&i2c->usi_rxdata);
  604. }
  605. rv = hsi2c_wait_for_trx(i2c);
  606. read_err:
  607. tmp_ret = hsi2c_wait_while_busy(i2c);
  608. if (rv == I2C_OK)
  609. rv = tmp_ret;
  610. writel(HSI2C_FUNC_MODE_I2C, &i2c->usi_ctl); /* done */
  611. return rv;
  612. }
  613. #ifdef CONFIG_SYS_I2C
  614. static unsigned int s3c24x0_i2c_set_bus_speed(struct i2c_adapter *adap,
  615. unsigned int speed)
  616. #else
  617. static int s3c24x0_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
  618. #endif
  619. {
  620. struct s3c24x0_i2c_bus *i2c_bus;
  621. #ifdef CONFIG_SYS_I2C
  622. i2c_bus = get_bus(adap->hwadapnr);
  623. if (!i2c_bus)
  624. return -EFAULT;
  625. #else
  626. i2c_bus = dev_get_priv(dev);
  627. #endif
  628. i2c_bus->clock_frequency = speed;
  629. if (i2c_bus->is_highspeed) {
  630. if (hsi2c_get_clk_details(i2c_bus))
  631. return -EFAULT;
  632. hsi2c_ch_init(i2c_bus);
  633. } else {
  634. i2c_ch_init(i2c_bus->regs, i2c_bus->clock_frequency,
  635. CONFIG_SYS_I2C_S3C24X0_SLAVE);
  636. }
  637. return 0;
  638. }
  639. /*
  640. * cmd_type is 0 for write, 1 for read.
  641. *
  642. * addr_len can take any value from 0-255, it is only limited
  643. * by the char, we could make it larger if needed. If it is
  644. * 0 we skip the address write cycle.
  645. */
  646. static int i2c_transfer(struct s3c24x0_i2c *i2c,
  647. unsigned char cmd_type,
  648. unsigned char chip,
  649. unsigned char addr[],
  650. unsigned char addr_len,
  651. unsigned char data[],
  652. unsigned short data_len)
  653. {
  654. int i = 0, result;
  655. ulong start_time = get_timer(0);
  656. if (data == 0 || data_len == 0) {
  657. /*Don't support data transfer of no length or to address 0 */
  658. debug("i2c_transfer: bad call\n");
  659. return I2C_NOK;
  660. }
  661. while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
  662. if (get_timer(start_time) > I2C_TIMEOUT_MS)
  663. return I2C_NOK_TOUT;
  664. }
  665. writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon);
  666. /* Get the slave chip address going */
  667. writel(chip, &i2c->iicds);
  668. if ((cmd_type == I2C_WRITE) || (addr && addr_len))
  669. writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
  670. &i2c->iicstat);
  671. else
  672. writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
  673. &i2c->iicstat);
  674. /* Wait for chip address to transmit. */
  675. result = WaitForXfer(i2c);
  676. if (result != I2C_OK)
  677. goto bailout;
  678. /* If register address needs to be transmitted - do it now. */
  679. if (addr && addr_len) {
  680. while ((i < addr_len) && (result == I2C_OK)) {
  681. writel(addr[i++], &i2c->iicds);
  682. ReadWriteByte(i2c);
  683. result = WaitForXfer(i2c);
  684. }
  685. i = 0;
  686. if (result != I2C_OK)
  687. goto bailout;
  688. }
  689. switch (cmd_type) {
  690. case I2C_WRITE:
  691. while ((i < data_len) && (result == I2C_OK)) {
  692. writel(data[i++], &i2c->iicds);
  693. ReadWriteByte(i2c);
  694. result = WaitForXfer(i2c);
  695. }
  696. break;
  697. case I2C_READ:
  698. if (addr && addr_len) {
  699. /*
  700. * Register address has been sent, now send slave chip
  701. * address again to start the actual read transaction.
  702. */
  703. writel(chip, &i2c->iicds);
  704. /* Generate a re-START. */
  705. writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
  706. &i2c->iicstat);
  707. ReadWriteByte(i2c);
  708. result = WaitForXfer(i2c);
  709. if (result != I2C_OK)
  710. goto bailout;
  711. }
  712. while ((i < data_len) && (result == I2C_OK)) {
  713. /* disable ACK for final READ */
  714. if (i == data_len - 1)
  715. writel(readl(&i2c->iiccon)
  716. & ~I2CCON_ACKGEN,
  717. &i2c->iiccon);
  718. ReadWriteByte(i2c);
  719. result = WaitForXfer(i2c);
  720. data[i++] = readl(&i2c->iicds);
  721. }
  722. if (result == I2C_NACK)
  723. result = I2C_OK; /* Normal terminated read. */
  724. break;
  725. default:
  726. debug("i2c_transfer: bad call\n");
  727. result = I2C_NOK;
  728. break;
  729. }
  730. bailout:
  731. /* Send STOP. */
  732. writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
  733. ReadWriteByte(i2c);
  734. return result;
  735. }
  736. #ifdef CONFIG_SYS_I2C
  737. static int s3c24x0_i2c_probe(struct i2c_adapter *adap, uchar chip)
  738. #else
  739. static int s3c24x0_i2c_probe(struct udevice *dev, uint chip, uint chip_flags)
  740. #endif
  741. {
  742. struct s3c24x0_i2c_bus *i2c_bus;
  743. uchar buf[1];
  744. int ret;
  745. #ifdef CONFIG_SYS_I2C
  746. i2c_bus = get_bus(adap->hwadapnr);
  747. if (!i2c_bus)
  748. return -EFAULT;
  749. #else
  750. i2c_bus = dev_get_priv(dev);
  751. #endif
  752. buf[0] = 0;
  753. /*
  754. * What is needed is to send the chip address and verify that the
  755. * address was <ACK>ed (i.e. there was a chip at that address which
  756. * drove the data line low).
  757. */
  758. if (i2c_bus->is_highspeed) {
  759. ret = hsi2c_read(i2c_bus->hsregs,
  760. chip, 0, 0, buf, 1);
  761. } else {
  762. ret = i2c_transfer(i2c_bus->regs,
  763. I2C_READ, chip << 1, 0, 0, buf, 1);
  764. }
  765. return ret != I2C_OK;
  766. }
  767. #ifdef CONFIG_SYS_I2C
  768. static int s3c24x0_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
  769. int alen, uchar *buffer, int len)
  770. {
  771. struct s3c24x0_i2c_bus *i2c_bus;
  772. uchar xaddr[4];
  773. int ret;
  774. i2c_bus = get_bus(adap->hwadapnr);
  775. if (!i2c_bus)
  776. return -EFAULT;
  777. if (alen > 4) {
  778. debug("I2C read: addr len %d not supported\n", alen);
  779. return -EADDRNOTAVAIL;
  780. }
  781. if (alen > 0) {
  782. xaddr[0] = (addr >> 24) & 0xFF;
  783. xaddr[1] = (addr >> 16) & 0xFF;
  784. xaddr[2] = (addr >> 8) & 0xFF;
  785. xaddr[3] = addr & 0xFF;
  786. }
  787. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  788. /*
  789. * EEPROM chips that implement "address overflow" are ones
  790. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  791. * address and the extra bits end up in the "chip address"
  792. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  793. * four 256 byte chips.
  794. *
  795. * Note that we consider the length of the address field to
  796. * still be one byte because the extra address bits are
  797. * hidden in the chip address.
  798. */
  799. if (alen > 0)
  800. chip |= ((addr >> (alen * 8)) &
  801. CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  802. #endif
  803. if (i2c_bus->is_highspeed)
  804. ret = hsi2c_read(i2c_bus->hsregs, chip, &xaddr[4 - alen],
  805. alen, buffer, len);
  806. else
  807. ret = i2c_transfer(i2c_bus->regs, I2C_READ, chip << 1,
  808. &xaddr[4 - alen], alen, buffer, len);
  809. if (ret) {
  810. if (i2c_bus->is_highspeed)
  811. exynos5_i2c_reset(i2c_bus);
  812. debug("I2c read failed %d\n", ret);
  813. return -EIO;
  814. }
  815. return 0;
  816. }
  817. static int s3c24x0_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
  818. int alen, uchar *buffer, int len)
  819. {
  820. struct s3c24x0_i2c_bus *i2c_bus;
  821. uchar xaddr[4];
  822. int ret;
  823. i2c_bus = get_bus(adap->hwadapnr);
  824. if (!i2c_bus)
  825. return -EFAULT;
  826. if (alen > 4) {
  827. debug("I2C write: addr len %d not supported\n", alen);
  828. return -EINVAL;
  829. }
  830. if (alen > 0) {
  831. xaddr[0] = (addr >> 24) & 0xFF;
  832. xaddr[1] = (addr >> 16) & 0xFF;
  833. xaddr[2] = (addr >> 8) & 0xFF;
  834. xaddr[3] = addr & 0xFF;
  835. }
  836. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  837. /*
  838. * EEPROM chips that implement "address overflow" are ones
  839. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  840. * address and the extra bits end up in the "chip address"
  841. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  842. * four 256 byte chips.
  843. *
  844. * Note that we consider the length of the address field to
  845. * still be one byte because the extra address bits are
  846. * hidden in the chip address.
  847. */
  848. if (alen > 0)
  849. chip |= ((addr >> (alen * 8)) &
  850. CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  851. #endif
  852. if (i2c_bus->is_highspeed)
  853. ret = hsi2c_write(i2c_bus->hsregs, chip, &xaddr[4 - alen],
  854. alen, buffer, len, true);
  855. else
  856. ret = i2c_transfer(i2c_bus->regs, I2C_WRITE, chip << 1,
  857. &xaddr[4 - alen], alen, buffer, len);
  858. if (ret != 0) {
  859. if (i2c_bus->is_highspeed)
  860. exynos5_i2c_reset(i2c_bus);
  861. return 1;
  862. } else {
  863. return 0;
  864. }
  865. }
  866. #ifdef CONFIG_OF_CONTROL
  867. static void process_nodes(const void *blob, int node_list[], int count,
  868. int is_highspeed)
  869. {
  870. struct s3c24x0_i2c_bus *bus;
  871. int i, flags;
  872. for (i = 0; i < count; i++) {
  873. int node = node_list[i];
  874. if (node <= 0)
  875. continue;
  876. bus = &i2c_bus[i];
  877. bus->active = true;
  878. bus->is_highspeed = is_highspeed;
  879. if (is_highspeed) {
  880. flags = PINMUX_FLAG_HS_MODE;
  881. bus->hsregs = (struct exynos5_hsi2c *)
  882. fdtdec_get_addr(blob, node, "reg");
  883. } else {
  884. flags = 0;
  885. bus->regs = (struct s3c24x0_i2c *)
  886. fdtdec_get_addr(blob, node, "reg");
  887. }
  888. bus->id = pinmux_decode_periph_id(blob, node);
  889. bus->clock_frequency = fdtdec_get_int(blob, node,
  890. "clock-frequency",
  891. CONFIG_SYS_I2C_S3C24X0_SPEED);
  892. bus->node = node;
  893. bus->bus_num = i;
  894. exynos_pinmux_config(PERIPH_ID_I2C0 + bus->id, flags);
  895. /* Mark position as used */
  896. node_list[i] = -1;
  897. }
  898. }
  899. void board_i2c_init(const void *blob)
  900. {
  901. int node_list[CONFIG_MAX_I2C_NUM];
  902. int count;
  903. /* First get the normal i2c ports */
  904. count = fdtdec_find_aliases_for_id(blob, "i2c",
  905. COMPAT_SAMSUNG_S3C2440_I2C, node_list,
  906. CONFIG_MAX_I2C_NUM);
  907. process_nodes(blob, node_list, count, 0);
  908. /* Now look for high speed i2c ports */
  909. count = fdtdec_find_aliases_for_id(blob, "i2c",
  910. COMPAT_SAMSUNG_EXYNOS5_I2C, node_list,
  911. CONFIG_MAX_I2C_NUM);
  912. process_nodes(blob, node_list, count, 1);
  913. }
  914. int i2c_get_bus_num_fdt(int node)
  915. {
  916. int i;
  917. for (i = 0; i < ARRAY_SIZE(i2c_bus); i++) {
  918. if (node == i2c_bus[i].node)
  919. return i;
  920. }
  921. debug("%s: Can't find any matched I2C bus\n", __func__);
  922. return -EINVAL;
  923. }
  924. int i2c_reset_port_fdt(const void *blob, int node)
  925. {
  926. struct s3c24x0_i2c_bus *i2c_bus;
  927. int bus;
  928. bus = i2c_get_bus_num_fdt(node);
  929. if (bus < 0) {
  930. debug("could not get bus for node %d\n", node);
  931. return bus;
  932. }
  933. i2c_bus = get_bus(bus);
  934. if (!i2c_bus) {
  935. debug("get_bus() failed for node %d\n", node);
  936. return -EFAULT;
  937. }
  938. if (i2c_bus->is_highspeed) {
  939. if (hsi2c_get_clk_details(i2c_bus))
  940. return -EINVAL;
  941. hsi2c_ch_init(i2c_bus);
  942. } else {
  943. i2c_ch_init(i2c_bus->regs, i2c_bus->clock_frequency,
  944. CONFIG_SYS_I2C_S3C24X0_SLAVE);
  945. }
  946. return 0;
  947. }
  948. #endif /* CONFIG_OF_CONTROL */
  949. #ifdef CONFIG_EXYNOS5
  950. static void exynos_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
  951. {
  952. /* This will override the speed selected in the fdt for that port */
  953. debug("i2c_init(speed=%u, slaveaddr=0x%x)\n", speed, slaveaddr);
  954. if (i2c_set_bus_speed(speed))
  955. error("i2c_init: failed to init bus for speed = %d", speed);
  956. }
  957. #endif /* CONFIG_EXYNOS5 */
  958. /*
  959. * Register s3c24x0 i2c adapters
  960. */
  961. #if defined(CONFIG_EXYNOS5420)
  962. U_BOOT_I2C_ADAP_COMPLETE(i2c00, s3c24x0_i2c_init, s3c24x0_i2c_probe,
  963. s3c24x0_i2c_read, s3c24x0_i2c_write,
  964. s3c24x0_i2c_set_bus_speed,
  965. CONFIG_SYS_I2C_S3C24X0_SPEED,
  966. CONFIG_SYS_I2C_S3C24X0_SLAVE, 0)
  967. U_BOOT_I2C_ADAP_COMPLETE(i2c01, s3c24x0_i2c_init, s3c24x0_i2c_probe,
  968. s3c24x0_i2c_read, s3c24x0_i2c_write,
  969. s3c24x0_i2c_set_bus_speed,
  970. CONFIG_SYS_I2C_S3C24X0_SPEED,
  971. CONFIG_SYS_I2C_S3C24X0_SLAVE, 1)
  972. U_BOOT_I2C_ADAP_COMPLETE(i2c02, s3c24x0_i2c_init, s3c24x0_i2c_probe,
  973. s3c24x0_i2c_read, s3c24x0_i2c_write,
  974. s3c24x0_i2c_set_bus_speed,
  975. CONFIG_SYS_I2C_S3C24X0_SPEED,
  976. CONFIG_SYS_I2C_S3C24X0_SLAVE, 2)
  977. U_BOOT_I2C_ADAP_COMPLETE(i2c03, exynos_i2c_init, s3c24x0_i2c_probe,
  978. s3c24x0_i2c_read, s3c24x0_i2c_write,
  979. s3c24x0_i2c_set_bus_speed,
  980. CONFIG_SYS_I2C_S3C24X0_SPEED,
  981. CONFIG_SYS_I2C_S3C24X0_SLAVE, 3)
  982. U_BOOT_I2C_ADAP_COMPLETE(i2c04, exynos_i2c_init, s3c24x0_i2c_probe,
  983. s3c24x0_i2c_read, s3c24x0_i2c_write,
  984. s3c24x0_i2c_set_bus_speed,
  985. CONFIG_SYS_I2C_S3C24X0_SPEED,
  986. CONFIG_SYS_I2C_S3C24X0_SLAVE, 4)
  987. U_BOOT_I2C_ADAP_COMPLETE(i2c05, exynos_i2c_init, s3c24x0_i2c_probe,
  988. s3c24x0_i2c_read, s3c24x0_i2c_write,
  989. s3c24x0_i2c_set_bus_speed,
  990. CONFIG_SYS_I2C_S3C24X0_SPEED,
  991. CONFIG_SYS_I2C_S3C24X0_SLAVE, 5)
  992. U_BOOT_I2C_ADAP_COMPLETE(i2c06, exynos_i2c_init, s3c24x0_i2c_probe,
  993. s3c24x0_i2c_read, s3c24x0_i2c_write,
  994. s3c24x0_i2c_set_bus_speed,
  995. CONFIG_SYS_I2C_S3C24X0_SPEED,
  996. CONFIG_SYS_I2C_S3C24X0_SLAVE, 6)
  997. U_BOOT_I2C_ADAP_COMPLETE(i2c07, exynos_i2c_init, s3c24x0_i2c_probe,
  998. s3c24x0_i2c_read, s3c24x0_i2c_write,
  999. s3c24x0_i2c_set_bus_speed,
  1000. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1001. CONFIG_SYS_I2C_S3C24X0_SLAVE, 7)
  1002. U_BOOT_I2C_ADAP_COMPLETE(i2c08, exynos_i2c_init, s3c24x0_i2c_probe,
  1003. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1004. s3c24x0_i2c_set_bus_speed,
  1005. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1006. CONFIG_SYS_I2C_S3C24X0_SLAVE, 8)
  1007. U_BOOT_I2C_ADAP_COMPLETE(i2c09, exynos_i2c_init, s3c24x0_i2c_probe,
  1008. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1009. s3c24x0_i2c_set_bus_speed,
  1010. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1011. CONFIG_SYS_I2C_S3C24X0_SLAVE, 9)
  1012. U_BOOT_I2C_ADAP_COMPLETE(i2c10, exynos_i2c_init, s3c24x0_i2c_probe,
  1013. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1014. s3c24x0_i2c_set_bus_speed,
  1015. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1016. CONFIG_SYS_I2C_S3C24X0_SLAVE, 10)
  1017. #elif defined(CONFIG_EXYNOS5250)
  1018. U_BOOT_I2C_ADAP_COMPLETE(i2c00, exynos_i2c_init, s3c24x0_i2c_probe,
  1019. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1020. s3c24x0_i2c_set_bus_speed,
  1021. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1022. CONFIG_SYS_I2C_S3C24X0_SLAVE, 0)
  1023. U_BOOT_I2C_ADAP_COMPLETE(i2c01, exynos_i2c_init, s3c24x0_i2c_probe,
  1024. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1025. s3c24x0_i2c_set_bus_speed,
  1026. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1027. CONFIG_SYS_I2C_S3C24X0_SLAVE, 1)
  1028. U_BOOT_I2C_ADAP_COMPLETE(i2c02, exynos_i2c_init, s3c24x0_i2c_probe,
  1029. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1030. s3c24x0_i2c_set_bus_speed,
  1031. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1032. CONFIG_SYS_I2C_S3C24X0_SLAVE, 2)
  1033. U_BOOT_I2C_ADAP_COMPLETE(i2c03, exynos_i2c_init, s3c24x0_i2c_probe,
  1034. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1035. s3c24x0_i2c_set_bus_speed,
  1036. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1037. CONFIG_SYS_I2C_S3C24X0_SLAVE, 3)
  1038. U_BOOT_I2C_ADAP_COMPLETE(i2c04, s3c24x0_i2c_init, s3c24x0_i2c_probe,
  1039. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1040. s3c24x0_i2c_set_bus_speed,
  1041. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1042. CONFIG_SYS_I2C_S3C24X0_SLAVE, 4)
  1043. U_BOOT_I2C_ADAP_COMPLETE(i2c05, s3c24x0_i2c_init, s3c24x0_i2c_probe,
  1044. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1045. s3c24x0_i2c_set_bus_speed,
  1046. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1047. CONFIG_SYS_I2C_S3C24X0_SLAVE, 5)
  1048. U_BOOT_I2C_ADAP_COMPLETE(i2c06, s3c24x0_i2c_init, s3c24x0_i2c_probe,
  1049. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1050. s3c24x0_i2c_set_bus_speed,
  1051. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1052. CONFIG_SYS_I2C_S3C24X0_SLAVE, 6)
  1053. U_BOOT_I2C_ADAP_COMPLETE(i2c07, s3c24x0_i2c_init, s3c24x0_i2c_probe,
  1054. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1055. s3c24x0_i2c_set_bus_speed,
  1056. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1057. CONFIG_SYS_I2C_S3C24X0_SLAVE, 7)
  1058. U_BOOT_I2C_ADAP_COMPLETE(i2c08, s3c24x0_i2c_init, s3c24x0_i2c_probe,
  1059. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1060. s3c24x0_i2c_set_bus_speed,
  1061. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1062. CONFIG_SYS_I2C_S3C24X0_SLAVE, 8)
  1063. U_BOOT_I2C_ADAP_COMPLETE(i2c09, s3c24x0_i2c_init, s3c24x0_i2c_probe,
  1064. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1065. s3c24x0_i2c_set_bus_speed,
  1066. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1067. CONFIG_SYS_I2C_S3C24X0_SLAVE, 9)
  1068. U_BOOT_I2C_ADAP_COMPLETE(s3c10, s3c24x0_i2c_init, s3c24x0_i2c_probe,
  1069. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1070. s3c24x0_i2c_set_bus_speed,
  1071. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1072. CONFIG_SYS_I2C_S3C24X0_SLAVE, 10)
  1073. #elif defined(CONFIG_EXYNOS4)
  1074. U_BOOT_I2C_ADAP_COMPLETE(i2c00, s3c24x0_i2c_init, s3c24x0_i2c_probe,
  1075. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1076. s3c24x0_i2c_set_bus_speed,
  1077. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1078. CONFIG_SYS_I2C_S3C24X0_SLAVE, 0)
  1079. U_BOOT_I2C_ADAP_COMPLETE(i2c01, s3c24x0_i2c_init, s3c24x0_i2c_probe,
  1080. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1081. s3c24x0_i2c_set_bus_speed,
  1082. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1083. CONFIG_SYS_I2C_S3C24X0_SLAVE, 1)
  1084. U_BOOT_I2C_ADAP_COMPLETE(i2c02, s3c24x0_i2c_init, s3c24x0_i2c_probe,
  1085. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1086. s3c24x0_i2c_set_bus_speed,
  1087. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1088. CONFIG_SYS_I2C_S3C24X0_SLAVE, 2)
  1089. U_BOOT_I2C_ADAP_COMPLETE(i2c03, s3c24x0_i2c_init, s3c24x0_i2c_probe,
  1090. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1091. s3c24x0_i2c_set_bus_speed,
  1092. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1093. CONFIG_SYS_I2C_S3C24X0_SLAVE, 3)
  1094. U_BOOT_I2C_ADAP_COMPLETE(i2c04, s3c24x0_i2c_init, s3c24x0_i2c_probe,
  1095. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1096. s3c24x0_i2c_set_bus_speed,
  1097. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1098. CONFIG_SYS_I2C_S3C24X0_SLAVE, 4)
  1099. U_BOOT_I2C_ADAP_COMPLETE(i2c05, s3c24x0_i2c_init, s3c24x0_i2c_probe,
  1100. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1101. s3c24x0_i2c_set_bus_speed,
  1102. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1103. CONFIG_SYS_I2C_S3C24X0_SLAVE, 5)
  1104. U_BOOT_I2C_ADAP_COMPLETE(i2c06, s3c24x0_i2c_init, s3c24x0_i2c_probe,
  1105. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1106. s3c24x0_i2c_set_bus_speed,
  1107. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1108. CONFIG_SYS_I2C_S3C24X0_SLAVE, 6)
  1109. U_BOOT_I2C_ADAP_COMPLETE(i2c07, s3c24x0_i2c_init, s3c24x0_i2c_probe,
  1110. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1111. s3c24x0_i2c_set_bus_speed,
  1112. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1113. CONFIG_SYS_I2C_S3C24X0_SLAVE, 7)
  1114. U_BOOT_I2C_ADAP_COMPLETE(i2c08, s3c24x0_i2c_init, s3c24x0_i2c_probe,
  1115. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1116. s3c24x0_i2c_set_bus_speed,
  1117. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1118. CONFIG_SYS_I2C_S3C24X0_SLAVE, 8)
  1119. #else
  1120. U_BOOT_I2C_ADAP_COMPLETE(s3c0, s3c24x0_i2c_init, s3c24x0_i2c_probe,
  1121. s3c24x0_i2c_read, s3c24x0_i2c_write,
  1122. s3c24x0_i2c_set_bus_speed,
  1123. CONFIG_SYS_I2C_S3C24X0_SPEED,
  1124. CONFIG_SYS_I2C_S3C24X0_SLAVE, 0)
  1125. #endif
  1126. #endif /* CONFIG_SYS_I2C */
  1127. #ifdef CONFIG_DM_I2C
  1128. static int i2c_write_data(struct s3c24x0_i2c_bus *i2c_bus, uchar chip,
  1129. uchar *buffer, int len, bool end_with_repeated_start)
  1130. {
  1131. int ret;
  1132. if (i2c_bus->is_highspeed) {
  1133. ret = hsi2c_write(i2c_bus->hsregs, chip, 0, 0,
  1134. buffer, len, true);
  1135. if (ret)
  1136. exynos5_i2c_reset(i2c_bus);
  1137. } else {
  1138. ret = i2c_transfer(i2c_bus->regs, I2C_WRITE,
  1139. chip << 1, 0, 0, buffer, len);
  1140. }
  1141. return ret != I2C_OK;
  1142. }
  1143. static int i2c_read_data(struct s3c24x0_i2c_bus *i2c_bus, uchar chip,
  1144. uchar *buffer, int len)
  1145. {
  1146. int ret;
  1147. if (i2c_bus->is_highspeed) {
  1148. ret = hsi2c_read(i2c_bus->hsregs, chip, 0, 0, buffer, len);
  1149. if (ret)
  1150. exynos5_i2c_reset(i2c_bus);
  1151. } else {
  1152. ret = i2c_transfer(i2c_bus->regs, I2C_READ,
  1153. chip << 1, 0, 0, buffer, len);
  1154. }
  1155. return ret != I2C_OK;
  1156. }
  1157. static int s3c24x0_i2c_xfer(struct udevice *dev, struct i2c_msg *msg,
  1158. int nmsgs)
  1159. {
  1160. struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
  1161. int ret;
  1162. for (; nmsgs > 0; nmsgs--, msg++) {
  1163. bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD);
  1164. if (msg->flags & I2C_M_RD) {
  1165. ret = i2c_read_data(i2c_bus, msg->addr, msg->buf,
  1166. msg->len);
  1167. } else {
  1168. ret = i2c_write_data(i2c_bus, msg->addr, msg->buf,
  1169. msg->len, next_is_read);
  1170. }
  1171. if (ret)
  1172. return -EREMOTEIO;
  1173. }
  1174. return 0;
  1175. }
  1176. static int s3c_i2c_ofdata_to_platdata(struct udevice *dev)
  1177. {
  1178. const void *blob = gd->fdt_blob;
  1179. struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
  1180. int node, flags;
  1181. i2c_bus->is_highspeed = dev->of_id->data;
  1182. node = dev->of_offset;
  1183. if (i2c_bus->is_highspeed) {
  1184. flags = PINMUX_FLAG_HS_MODE;
  1185. i2c_bus->hsregs = (struct exynos5_hsi2c *)
  1186. fdtdec_get_addr(blob, node, "reg");
  1187. } else {
  1188. flags = 0;
  1189. i2c_bus->regs = (struct s3c24x0_i2c *)
  1190. fdtdec_get_addr(blob, node, "reg");
  1191. }
  1192. i2c_bus->id = pinmux_decode_periph_id(blob, node);
  1193. i2c_bus->clock_frequency = fdtdec_get_int(blob, node,
  1194. "clock-frequency",
  1195. CONFIG_SYS_I2C_S3C24X0_SPEED);
  1196. i2c_bus->node = node;
  1197. i2c_bus->bus_num = dev->seq;
  1198. exynos_pinmux_config(i2c_bus->id, flags);
  1199. i2c_bus->active = true;
  1200. return 0;
  1201. }
  1202. static const struct dm_i2c_ops s3c_i2c_ops = {
  1203. .xfer = s3c24x0_i2c_xfer,
  1204. .probe_chip = s3c24x0_i2c_probe,
  1205. .set_bus_speed = s3c24x0_i2c_set_bus_speed,
  1206. };
  1207. static const struct udevice_id s3c_i2c_ids[] = {
  1208. { .compatible = "samsung,s3c2440-i2c", .data = EXYNOS_I2C_STD },
  1209. { .compatible = "samsung,exynos5-hsi2c", .data = EXYNOS_I2C_HS },
  1210. { }
  1211. };
  1212. U_BOOT_DRIVER(i2c_s3c) = {
  1213. .name = "i2c_s3c",
  1214. .id = UCLASS_I2C,
  1215. .of_match = s3c_i2c_ids,
  1216. .ofdata_to_platdata = s3c_i2c_ofdata_to_platdata,
  1217. .per_child_auto_alloc_size = sizeof(struct dm_i2c_chip),
  1218. .priv_auto_alloc_size = sizeof(struct s3c24x0_i2c_bus),
  1219. .ops = &s3c_i2c_ops,
  1220. };
  1221. #endif /* CONFIG_DM_I2C */