s3c24x0_i2c.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  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. #if defined(CONFIG_S3C2410) || defined(CONFIG_S3C2440)
  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. #if defined(CONFIG_S3C2410) || defined(CONFIG_S3C2440)
  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 read_write_byte(struct s3c24x0_i2c *i2c)
  221. {
  222. clrbits_le32(&i2c->iiccon, I2CCON_IRPND);
  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. #if defined(CONFIG_S3C2410) || defined(CONFIG_S3C2440)
  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. #if defined(CONFIG_S3C2410) || defined(CONFIG_S3C2440)
  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. #if defined(CONFIG_S3C2410) || defined(CONFIG_S3C2440)
  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. read_write_byte(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. read_write_byte(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. read_write_byte(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. read_write_byte(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. read_write_byte(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. #if CONFIG_IS_ENABLED(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(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_IS_ENABLED(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 exynos_hs_i2c_xfer(struct udevice *dev, struct i2c_msg *msg,
  1129. int nmsgs)
  1130. {
  1131. struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
  1132. struct exynos5_hsi2c *hsregs = i2c_bus->hsregs;
  1133. int ret;
  1134. for (; nmsgs > 0; nmsgs--, msg++) {
  1135. if (msg->flags & I2C_M_RD) {
  1136. ret = hsi2c_read(hsregs, msg->addr, 0, 0, msg->buf,
  1137. msg->len);
  1138. } else {
  1139. ret = hsi2c_write(hsregs, msg->addr, 0, 0, msg->buf,
  1140. msg->len, true);
  1141. }
  1142. if (ret) {
  1143. exynos5_i2c_reset(i2c_bus);
  1144. return -EREMOTEIO;
  1145. }
  1146. }
  1147. return 0;
  1148. }
  1149. static int s3c24x0_do_msg(struct s3c24x0_i2c_bus *i2c_bus, struct i2c_msg *msg,
  1150. int seq)
  1151. {
  1152. struct s3c24x0_i2c *i2c = i2c_bus->regs;
  1153. bool is_read = msg->flags & I2C_M_RD;
  1154. uint status;
  1155. uint addr;
  1156. int ret, i;
  1157. if (!seq)
  1158. setbits_le32(&i2c->iiccon, I2CCON_ACKGEN);
  1159. /* Get the slave chip address going */
  1160. addr = msg->addr << 1;
  1161. writel(addr, &i2c->iicds);
  1162. status = I2C_TXRX_ENA | I2C_START_STOP;
  1163. if (is_read)
  1164. status |= I2C_MODE_MR;
  1165. else
  1166. status |= I2C_MODE_MT;
  1167. writel(status, &i2c->iicstat);
  1168. if (seq)
  1169. read_write_byte(i2c);
  1170. /* Wait for chip address to transmit */
  1171. ret = WaitForXfer(i2c);
  1172. if (ret)
  1173. goto err;
  1174. if (is_read) {
  1175. for (i = 0; !ret && i < msg->len; i++) {
  1176. /* disable ACK for final READ */
  1177. if (i == msg->len - 1)
  1178. clrbits_le32(&i2c->iiccon, I2CCON_ACKGEN);
  1179. read_write_byte(i2c);
  1180. ret = WaitForXfer(i2c);
  1181. msg->buf[i] = readl(&i2c->iicds);
  1182. }
  1183. if (ret == I2C_NACK)
  1184. ret = I2C_OK; /* Normal terminated read */
  1185. } else {
  1186. for (i = 0; !ret && i < msg->len; i++) {
  1187. writel(msg->buf[i], &i2c->iicds);
  1188. read_write_byte(i2c);
  1189. ret = WaitForXfer(i2c);
  1190. }
  1191. }
  1192. err:
  1193. return ret;
  1194. }
  1195. static int s3c24x0_i2c_xfer(struct udevice *dev, struct i2c_msg *msg,
  1196. int nmsgs)
  1197. {
  1198. struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
  1199. struct s3c24x0_i2c *i2c = i2c_bus->regs;
  1200. ulong start_time;
  1201. int ret, i;
  1202. start_time = get_timer(0);
  1203. while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
  1204. if (get_timer(start_time) > I2C_TIMEOUT_MS) {
  1205. debug("Timeout\n");
  1206. return -ETIMEDOUT;
  1207. }
  1208. }
  1209. for (ret = 0, i = 0; !ret && i < nmsgs; i++)
  1210. ret = s3c24x0_do_msg(i2c_bus, &msg[i], i);
  1211. /* Send STOP */
  1212. writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
  1213. read_write_byte(i2c);
  1214. return ret ? -EREMOTEIO : 0;
  1215. }
  1216. static int s3c_i2c_ofdata_to_platdata(struct udevice *dev)
  1217. {
  1218. const void *blob = gd->fdt_blob;
  1219. struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
  1220. int node, flags;
  1221. i2c_bus->is_highspeed = dev_get_driver_data(dev);
  1222. node = dev->of_offset;
  1223. if (i2c_bus->is_highspeed) {
  1224. flags = PINMUX_FLAG_HS_MODE;
  1225. i2c_bus->hsregs = (struct exynos5_hsi2c *)dev_get_addr(dev);
  1226. } else {
  1227. flags = 0;
  1228. i2c_bus->regs = (struct s3c24x0_i2c *)dev_get_addr(dev);
  1229. }
  1230. i2c_bus->id = pinmux_decode_periph_id(blob, node);
  1231. i2c_bus->clock_frequency = fdtdec_get_int(blob, node,
  1232. "clock-frequency", 100000);
  1233. i2c_bus->node = node;
  1234. i2c_bus->bus_num = dev->seq;
  1235. exynos_pinmux_config(i2c_bus->id, flags);
  1236. i2c_bus->active = true;
  1237. return 0;
  1238. }
  1239. static const struct dm_i2c_ops s3c_i2c_ops = {
  1240. .xfer = s3c24x0_i2c_xfer,
  1241. .probe_chip = s3c24x0_i2c_probe,
  1242. .set_bus_speed = s3c24x0_i2c_set_bus_speed,
  1243. };
  1244. static const struct udevice_id s3c_i2c_ids[] = {
  1245. { .compatible = "samsung,s3c2440-i2c", .data = EXYNOS_I2C_STD },
  1246. { }
  1247. };
  1248. U_BOOT_DRIVER(i2c_s3c) = {
  1249. .name = "i2c_s3c",
  1250. .id = UCLASS_I2C,
  1251. .of_match = s3c_i2c_ids,
  1252. .ofdata_to_platdata = s3c_i2c_ofdata_to_platdata,
  1253. .per_child_auto_alloc_size = sizeof(struct dm_i2c_chip),
  1254. .priv_auto_alloc_size = sizeof(struct s3c24x0_i2c_bus),
  1255. .ops = &s3c_i2c_ops,
  1256. };
  1257. /*
  1258. * TODO(sjg@chromium.org): Move this to a separate file when everything uses
  1259. * driver model
  1260. */
  1261. static const struct dm_i2c_ops exynos_hs_i2c_ops = {
  1262. .xfer = exynos_hs_i2c_xfer,
  1263. .probe_chip = s3c24x0_i2c_probe,
  1264. .set_bus_speed = s3c24x0_i2c_set_bus_speed,
  1265. };
  1266. static const struct udevice_id exynos_hs_i2c_ids[] = {
  1267. { .compatible = "samsung,exynos5-hsi2c", .data = EXYNOS_I2C_HS },
  1268. { }
  1269. };
  1270. U_BOOT_DRIVER(hs_i2c) = {
  1271. .name = "i2c_s3c_hs",
  1272. .id = UCLASS_I2C,
  1273. .of_match = exynos_hs_i2c_ids,
  1274. .ofdata_to_platdata = s3c_i2c_ofdata_to_platdata,
  1275. .per_child_auto_alloc_size = sizeof(struct dm_i2c_chip),
  1276. .priv_auto_alloc_size = sizeof(struct s3c24x0_i2c_bus),
  1277. .ops = &exynos_hs_i2c_ops,
  1278. };
  1279. #endif /* CONFIG_DM_I2C */