blob: f77a9d1a1ffd888a12dcc654e6e0ede940c25647 [file] [log] [blame]
wdenk1fe2c702003-03-06 21:55:29 +00001/*
2 * (C) Copyright 2002
3 * David Mueller, ELSOFT AG, d.mueller@elsoft.ch
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk1fe2c702003-03-06 21:55:29 +00006 */
7
8/* This code should work for both the S3C2400 and the S3C2410
9 * as they seem to have the same I2C controller inside.
10 * The different address mapping is handled by the s3c24xx.h files below.
11 */
12
13#include <common.h>
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +000014#include <fdtdec.h>
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +000015#if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000016#include <asm/arch/clk.h>
17#include <asm/arch/cpu.h>
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +000018#include <asm/arch/pinmux.h>
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000019#else
kevin.morfitt@fearnside-systems.co.uke0d81312009-11-17 18:30:34 +090020#include <asm/arch/s3c24x0_cpu.h>
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000021#endif
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090022#include <asm/io.h>
wdenk1fe2c702003-03-06 21:55:29 +000023#include <i2c.h>
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +000024#include "s3c24x0_i2c.h"
wdenk1fe2c702003-03-06 21:55:29 +000025
26#ifdef CONFIG_HARD_I2C
27
wdenk7539dea2003-06-19 23:01:32 +000028#define I2C_WRITE 0
29#define I2C_READ 1
wdenk1fe2c702003-03-06 21:55:29 +000030
wdenk7539dea2003-06-19 23:01:32 +000031#define I2C_OK 0
32#define I2C_NOK 1
33#define I2C_NACK 2
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090034#define I2C_NOK_LA 3 /* Lost arbitration */
35#define I2C_NOK_TOUT 4 /* time out */
wdenk1fe2c702003-03-06 21:55:29 +000036
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +053037/* HSI2C specific register description */
38
39/* I2C_CTL Register bits */
40#define HSI2C_FUNC_MODE_I2C (1u << 0)
41#define HSI2C_MASTER (1u << 3)
42#define HSI2C_RXCHON (1u << 6) /* Write/Send */
43#define HSI2C_TXCHON (1u << 7) /* Read/Receive */
44#define HSI2C_SW_RST (1u << 31)
45
46/* I2C_FIFO_CTL Register bits */
47#define HSI2C_RXFIFO_EN (1u << 0)
48#define HSI2C_TXFIFO_EN (1u << 1)
49#define HSI2C_TXFIFO_TRIGGER_LEVEL (0x20 << 16)
50#define HSI2C_RXFIFO_TRIGGER_LEVEL (0x20 << 4)
51
52/* I2C_TRAILING_CTL Register bits */
53#define HSI2C_TRAILING_COUNT (0xff)
54
55/* I2C_INT_EN Register bits */
56#define HSI2C_TX_UNDERRUN_EN (1u << 2)
57#define HSI2C_TX_OVERRUN_EN (1u << 3)
58#define HSI2C_RX_UNDERRUN_EN (1u << 4)
59#define HSI2C_RX_OVERRUN_EN (1u << 5)
60#define HSI2C_INT_TRAILING_EN (1u << 6)
61#define HSI2C_INT_I2C_EN (1u << 9)
62
63#define HSI2C_INT_ERROR_MASK (HSI2C_TX_UNDERRUN_EN |\
64 HSI2C_TX_OVERRUN_EN |\
65 HSI2C_RX_UNDERRUN_EN |\
66 HSI2C_RX_OVERRUN_EN |\
67 HSI2C_INT_TRAILING_EN)
68
69/* I2C_CONF Register bits */
70#define HSI2C_AUTO_MODE (1u << 31)
71#define HSI2C_10BIT_ADDR_MODE (1u << 30)
72#define HSI2C_HS_MODE (1u << 29)
73
74/* I2C_AUTO_CONF Register bits */
75#define HSI2C_READ_WRITE (1u << 16)
76#define HSI2C_STOP_AFTER_TRANS (1u << 17)
77#define HSI2C_MASTER_RUN (1u << 31)
78
79/* I2C_TIMEOUT Register bits */
80#define HSI2C_TIMEOUT_EN (1u << 31)
81
82/* I2C_TRANS_STATUS register bits */
83#define HSI2C_MASTER_BUSY (1u << 17)
84#define HSI2C_SLAVE_BUSY (1u << 16)
85#define HSI2C_TIMEOUT_AUTO (1u << 4)
86#define HSI2C_NO_DEV (1u << 3)
87#define HSI2C_NO_DEV_ACK (1u << 2)
88#define HSI2C_TRANS_ABORT (1u << 1)
89#define HSI2C_TRANS_SUCCESS (1u << 0)
90#define HSI2C_TRANS_ERROR_MASK (HSI2C_TIMEOUT_AUTO |\
91 HSI2C_NO_DEV | HSI2C_NO_DEV_ACK |\
92 HSI2C_TRANS_ABORT)
93#define HSI2C_TRANS_FINISHED_MASK (HSI2C_TRANS_ERROR_MASK | HSI2C_TRANS_SUCCESS)
94
95
96/* I2C_FIFO_STAT Register bits */
97#define HSI2C_RX_FIFO_EMPTY (1u << 24)
98#define HSI2C_RX_FIFO_FULL (1u << 23)
99#define HSI2C_TX_FIFO_EMPTY (1u << 8)
100#define HSI2C_TX_FIFO_FULL (1u << 7)
101#define HSI2C_RX_FIFO_LEVEL(x) (((x) >> 16) & 0x7f)
102#define HSI2C_TX_FIFO_LEVEL(x) ((x) & 0x7f)
103
104#define HSI2C_SLV_ADDR_MAS(x) ((x & 0x3ff) << 10)
105
106/* S3C I2C Controller bits */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900107#define I2CSTAT_BSY 0x20 /* Busy bit */
108#define I2CSTAT_NACK 0x01 /* Nack bit */
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000109#define I2CCON_ACKGEN 0x80 /* Acknowledge generation */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900110#define I2CCON_IRPND 0x10 /* Interrupt pending bit */
111#define I2C_MODE_MT 0xC0 /* Master Transmit Mode */
112#define I2C_MODE_MR 0x80 /* Master Receive Mode */
113#define I2C_START_STOP 0x20 /* START / STOP */
114#define I2C_TXRX_ENA 0x10 /* I2C Tx/Rx enable */
wdenk1fe2c702003-03-06 21:55:29 +0000115
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530116#define I2C_TIMEOUT_MS 1000 /* 1 second */
wdenk1fe2c702003-03-06 21:55:29 +0000117
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530118#define HSI2C_TIMEOUT_US 100000 /* 100 ms, finer granularity */
119
120
121/* To support VCMA9 boards and other who dont define max_i2c_num */
122#ifndef CONFIG_MAX_I2C_NUM
123#define CONFIG_MAX_I2C_NUM 1
124#endif
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000125
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +0000126/*
127 * For SPL boot some boards need i2c before SDRAM is initialised so force
128 * variables to live in SRAM
129 */
130static unsigned int g_current_bus __attribute__((section(".data")));
131static struct s3c24x0_i2c_bus i2c_bus[CONFIG_MAX_I2C_NUM]
132 __attribute__((section(".data")));
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530133
134/**
135 * Get a pointer to the given bus index
136 *
137 * @bus_idx: Bus index to look up
138 * @return pointer to bus, or NULL if invalid or not available
139 */
140static struct s3c24x0_i2c_bus *get_bus(unsigned int bus_idx)
141{
142 if (bus_idx < ARRAY_SIZE(i2c_bus)) {
143 struct s3c24x0_i2c_bus *bus;
144
145 bus = &i2c_bus[bus_idx];
146 if (bus->active)
147 return bus;
148 }
149
150 debug("Undefined bus: %d\n", bus_idx);
151 return NULL;
152}
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000153
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +0000154#if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
wdenk7539dea2003-06-19 23:01:32 +0000155static int GetI2CSDA(void)
wdenk1fe2c702003-03-06 21:55:29 +0000156{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900157 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
wdenk7539dea2003-06-19 23:01:32 +0000158
wdenkca9bc762003-07-15 07:45:49 +0000159#ifdef CONFIG_S3C2410
C Nauman383c43e2010-10-26 23:04:31 +0900160 return (readl(&gpio->gpedat) & 0x8000) >> 15;
wdenkca9bc762003-07-15 07:45:49 +0000161#endif
162#ifdef CONFIG_S3C2400
C Nauman383c43e2010-10-26 23:04:31 +0900163 return (readl(&gpio->pgdat) & 0x0020) >> 5;
wdenkca9bc762003-07-15 07:45:49 +0000164#endif
wdenk1fe2c702003-03-06 21:55:29 +0000165}
166
wdenk7539dea2003-06-19 23:01:32 +0000167static void SetI2CSCL(int x)
wdenk1fe2c702003-03-06 21:55:29 +0000168{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900169 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
wdenk7539dea2003-06-19 23:01:32 +0000170
wdenkca9bc762003-07-15 07:45:49 +0000171#ifdef CONFIG_S3C2410
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000172 writel((readl(&gpio->gpedat) & ~0x4000) |
173 (x & 1) << 14, &gpio->gpedat);
wdenkca9bc762003-07-15 07:45:49 +0000174#endif
175#ifdef CONFIG_S3C2400
C Nauman383c43e2010-10-26 23:04:31 +0900176 writel((readl(&gpio->pgdat) & ~0x0040) | (x & 1) << 6, &gpio->pgdat);
wdenkca9bc762003-07-15 07:45:49 +0000177#endif
wdenk1fe2c702003-03-06 21:55:29 +0000178}
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000179#endif
wdenk1fe2c702003-03-06 21:55:29 +0000180
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530181/*
182 * Wait til the byte transfer is completed.
183 *
184 * @param i2c- pointer to the appropriate i2c register bank.
185 * @return I2C_OK, if transmission was ACKED
186 * I2C_NACK, if transmission was NACKED
187 * I2C_NOK_TIMEOUT, if transaction did not complete in I2C_TIMEOUT_MS
188 */
189
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000190static int WaitForXfer(struct s3c24x0_i2c *i2c)
wdenk1fe2c702003-03-06 21:55:29 +0000191{
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530192 ulong start_time = get_timer(0);
wdenk1fe2c702003-03-06 21:55:29 +0000193
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530194 do {
195 if (readl(&i2c->iiccon) & I2CCON_IRPND)
196 return (readl(&i2c->iicstat) & I2CSTAT_NACK) ?
197 I2C_NACK : I2C_OK;
198 } while (get_timer(start_time) < I2C_TIMEOUT_MS);
wdenk1fe2c702003-03-06 21:55:29 +0000199
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530200 return I2C_NOK_TOUT;
wdenk1fe2c702003-03-06 21:55:29 +0000201}
202
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530203/*
204 * Wait for transfer completion.
205 *
206 * This function reads the interrupt status register waiting for the INT_I2C
207 * bit to be set, which indicates copletion of a transaction.
208 *
209 * @param i2c: pointer to the appropriate register bank
210 *
211 * @return: I2C_OK in case of successful completion, I2C_NOK_TIMEOUT in case
212 * the status bits do not get set in time, or an approrpiate error
213 * value in case of transfer errors.
214 */
215static int hsi2c_wait_for_trx(struct exynos5_hsi2c *i2c)
216{
217 int i = HSI2C_TIMEOUT_US;
218
219 while (i-- > 0) {
220 u32 int_status = readl(&i2c->usi_int_stat);
221
222 if (int_status & HSI2C_INT_I2C_EN) {
223 u32 trans_status = readl(&i2c->usi_trans_status);
224
225 /* Deassert pending interrupt. */
226 writel(int_status, &i2c->usi_int_stat);
227
228 if (trans_status & HSI2C_NO_DEV_ACK) {
229 debug("%s: no ACK from device\n", __func__);
230 return I2C_NACK;
231 }
232 if (trans_status & HSI2C_NO_DEV) {
233 debug("%s: no device\n", __func__);
234 return I2C_NOK;
235 }
236 if (trans_status & HSI2C_TRANS_ABORT) {
237 debug("%s: arbitration lost\n", __func__);
238 return I2C_NOK_LA;
239 }
240 if (trans_status & HSI2C_TIMEOUT_AUTO) {
241 debug("%s: device timed out\n", __func__);
242 return I2C_NOK_TOUT;
243 }
244 return I2C_OK;
245 }
246 udelay(1);
247 }
248 debug("%s: transaction timeout!\n", __func__);
249 return I2C_NOK_TOUT;
250}
251
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000252static void ReadWriteByte(struct s3c24x0_i2c *i2c)
wdenk1fe2c702003-03-06 21:55:29 +0000253{
C Nauman383c43e2010-10-26 23:04:31 +0900254 writel(readl(&i2c->iiccon) & ~I2CCON_IRPND, &i2c->iiccon);
wdenk1fe2c702003-03-06 21:55:29 +0000255}
256
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000257static struct s3c24x0_i2c *get_base_i2c(void)
258{
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +0000259#ifdef CONFIG_EXYNOS4
260 struct s3c24x0_i2c *i2c = (struct s3c24x0_i2c *)(samsung_get_base_i2c()
261 + (EXYNOS4_I2C_SPACING
262 * g_current_bus));
263 return i2c;
264#elif defined CONFIG_EXYNOS5
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000265 struct s3c24x0_i2c *i2c = (struct s3c24x0_i2c *)(samsung_get_base_i2c()
266 + (EXYNOS5_I2C_SPACING
267 * g_current_bus));
268 return i2c;
269#else
270 return s3c24x0_get_base_i2c();
271#endif
272}
273
274static void i2c_ch_init(struct s3c24x0_i2c *i2c, int speed, int slaveadd)
275{
276 ulong freq, pres = 16, div;
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +0000277#if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000278 freq = get_i2c_clk();
279#else
280 freq = get_PCLK();
281#endif
282 /* calculate prescaler and divisor values */
283 if ((freq / pres / (16 + 1)) > speed)
284 /* set prescaler to 512 */
285 pres = 512;
286
287 div = 0;
288 while ((freq / pres / (div + 1)) > speed)
289 div++;
290
291 /* set prescaler, divisor according to freq, also set ACKGEN, IRQ */
292 writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon);
293
294 /* init to SLAVE REVEIVE and set slaveaddr */
295 writel(0, &i2c->iicstat);
296 writel(slaveadd, &i2c->iicadd);
297 /* program Master Transmit (and implicit STOP) */
298 writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
299}
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530300
301#ifdef CONFIG_I2C_MULTI_BUS
302static int hsi2c_get_clk_details(struct s3c24x0_i2c_bus *i2c_bus)
303{
304 struct exynos5_hsi2c *hsregs = i2c_bus->hsregs;
305 ulong clkin;
306 unsigned int op_clk = i2c_bus->clock_frequency;
307 unsigned int i = 0, utemp0 = 0, utemp1 = 0;
308 unsigned int t_ftl_cycle;
309
310#if defined CONFIG_EXYNOS5
311 clkin = get_i2c_clk();
312#endif
313 /* FPCLK / FI2C =
314 * (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2) + 8 + 2 * FLT_CYCLE
315 * uTemp0 = (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2)
316 * uTemp1 = (TSCLK_L + TSCLK_H + 2)
317 * uTemp2 = TSCLK_L + TSCLK_H
318 */
319 t_ftl_cycle = (readl(&hsregs->usi_conf) >> 16) & 0x7;
320 utemp0 = (clkin / op_clk) - 8 - 2 * t_ftl_cycle;
321
322 /* CLK_DIV max is 256 */
323 for (i = 0; i < 256; i++) {
324 utemp1 = utemp0 / (i + 1);
325 if ((utemp1 < 512) && (utemp1 > 4)) {
326 i2c_bus->clk_cycle = utemp1 - 2;
327 i2c_bus->clk_div = i;
328 return 0;
329 }
330 }
331 return -1;
332}
333#endif
334
335static void hsi2c_ch_init(struct s3c24x0_i2c_bus *i2c_bus)
336{
337 struct exynos5_hsi2c *hsregs = i2c_bus->hsregs;
338 unsigned int t_sr_release;
339 unsigned int n_clkdiv;
340 unsigned int t_start_su, t_start_hd;
341 unsigned int t_stop_su;
342 unsigned int t_data_su, t_data_hd;
343 unsigned int t_scl_l, t_scl_h;
344 u32 i2c_timing_s1;
345 u32 i2c_timing_s2;
346 u32 i2c_timing_s3;
347 u32 i2c_timing_sla;
348
349 n_clkdiv = i2c_bus->clk_div;
350 t_scl_l = i2c_bus->clk_cycle / 2;
351 t_scl_h = i2c_bus->clk_cycle / 2;
352 t_start_su = t_scl_l;
353 t_start_hd = t_scl_l;
354 t_stop_su = t_scl_l;
355 t_data_su = t_scl_l / 2;
356 t_data_hd = t_scl_l / 2;
357 t_sr_release = i2c_bus->clk_cycle;
358
359 i2c_timing_s1 = t_start_su << 24 | t_start_hd << 16 | t_stop_su << 8;
360 i2c_timing_s2 = t_data_su << 24 | t_scl_l << 8 | t_scl_h << 0;
361 i2c_timing_s3 = n_clkdiv << 16 | t_sr_release << 0;
362 i2c_timing_sla = t_data_hd << 0;
363
364 writel(HSI2C_TRAILING_COUNT, &hsregs->usi_trailing_ctl);
365
366 /* Clear to enable Timeout */
367 clrsetbits_le32(&hsregs->usi_timeout, HSI2C_TIMEOUT_EN, 0);
368
369 /* set AUTO mode */
370 writel(readl(&hsregs->usi_conf) | HSI2C_AUTO_MODE, &hsregs->usi_conf);
371
372 /* Enable completion conditions' reporting. */
373 writel(HSI2C_INT_I2C_EN, &hsregs->usi_int_en);
374
375 /* Enable FIFOs */
376 writel(HSI2C_RXFIFO_EN | HSI2C_TXFIFO_EN, &hsregs->usi_fifo_ctl);
377
378 /* Currently operating in Fast speed mode. */
379 writel(i2c_timing_s1, &hsregs->usi_timing_fs1);
380 writel(i2c_timing_s2, &hsregs->usi_timing_fs2);
381 writel(i2c_timing_s3, &hsregs->usi_timing_fs3);
382 writel(i2c_timing_sla, &hsregs->usi_timing_sla);
383}
384
385/* SW reset for the high speed bus */
386static void exynos5_i2c_reset(struct s3c24x0_i2c_bus *i2c_bus)
387{
388 struct exynos5_hsi2c *i2c = i2c_bus->hsregs;
389 u32 i2c_ctl;
390
391 /* Set and clear the bit for reset */
392 i2c_ctl = readl(&i2c->usi_ctl);
393 i2c_ctl |= HSI2C_SW_RST;
394 writel(i2c_ctl, &i2c->usi_ctl);
395
396 i2c_ctl = readl(&i2c->usi_ctl);
397 i2c_ctl &= ~HSI2C_SW_RST;
398 writel(i2c_ctl, &i2c->usi_ctl);
399
400 /* Initialize the configure registers */
401 hsi2c_ch_init(i2c_bus);
402}
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000403
Rajeshwari Shindec07c11d2012-07-23 21:23:54 +0000404/*
405 * MULTI BUS I2C support
406 */
407
408#ifdef CONFIG_I2C_MULTI_BUS
409int i2c_set_bus_num(unsigned int bus)
410{
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530411 struct s3c24x0_i2c_bus *i2c_bus;
Rajeshwari Shindec07c11d2012-07-23 21:23:54 +0000412
Simon Glass2ccb6f42013-10-15 16:02:10 +0530413 i2c_bus = get_bus(bus);
414 if (!i2c_bus)
Rajeshwari Shindec07c11d2012-07-23 21:23:54 +0000415 return -1;
Rajeshwari Shindec07c11d2012-07-23 21:23:54 +0000416 g_current_bus = bus;
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530417
418 if (i2c_bus->is_highspeed) {
419 if (hsi2c_get_clk_details(i2c_bus))
420 return -1;
421 hsi2c_ch_init(i2c_bus);
422 } else {
423 i2c_ch_init(i2c_bus->regs, i2c_bus->clock_frequency,
424 CONFIG_SYS_I2C_SLAVE);
425 }
Rajeshwari Shindec07c11d2012-07-23 21:23:54 +0000426
427 return 0;
428}
429
430unsigned int i2c_get_bus_num(void)
431{
432 return g_current_bus;
433}
434#endif
435
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900436void i2c_init(int speed, int slaveadd)
wdenk1fe2c702003-03-06 21:55:29 +0000437{
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000438 struct s3c24x0_i2c *i2c;
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +0000439#if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900440 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000441#endif
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530442 ulong start_time = get_timer(0);
wdenk1fe2c702003-03-06 21:55:29 +0000443
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000444 /* By default i2c channel 0 is the current bus */
445 g_current_bus = 0;
446 i2c = get_base_i2c();
wdenk1fe2c702003-03-06 21:55:29 +0000447
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530448 /*
449 * In case the previous transfer is still going, wait to give it a
450 * chance to finish.
451 */
452 while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
453 if (get_timer(start_time) > I2C_TIMEOUT_MS) {
454 printf("%s: I2C bus busy for %p\n", __func__,
455 &i2c->iicstat);
456 return;
457 }
wdenk49c3f672003-10-08 22:33:00 +0000458 }
wdenk1fe2c702003-03-06 21:55:29 +0000459
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +0000460#if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530461 int i;
462
C Nauman383c43e2010-10-26 23:04:31 +0900463 if ((readl(&i2c->iicstat) & I2CSTAT_BSY) || GetI2CSDA() == 0) {
wdenkca9bc762003-07-15 07:45:49 +0000464#ifdef CONFIG_S3C2410
C Nauman383c43e2010-10-26 23:04:31 +0900465 ulong old_gpecon = readl(&gpio->gpecon);
wdenkca9bc762003-07-15 07:45:49 +0000466#endif
467#ifdef CONFIG_S3C2400
C Nauman383c43e2010-10-26 23:04:31 +0900468 ulong old_gpecon = readl(&gpio->pgcon);
wdenkca9bc762003-07-15 07:45:49 +0000469#endif
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900470 /* bus still busy probably by (most) previously interrupted
471 transfer */
wdenk1fe2c702003-03-06 21:55:29 +0000472
wdenkca9bc762003-07-15 07:45:49 +0000473#ifdef CONFIG_S3C2410
wdenk49c3f672003-10-08 22:33:00 +0000474 /* set I2CSDA and I2CSCL (GPE15, GPE14) to GPIO */
C Nauman383c43e2010-10-26 23:04:31 +0900475 writel((readl(&gpio->gpecon) & ~0xF0000000) | 0x10000000,
476 &gpio->gpecon);
wdenkca9bc762003-07-15 07:45:49 +0000477#endif
478#ifdef CONFIG_S3C2400
wdenk49c3f672003-10-08 22:33:00 +0000479 /* set I2CSDA and I2CSCL (PG5, PG6) to GPIO */
C Nauman383c43e2010-10-26 23:04:31 +0900480 writel((readl(&gpio->pgcon) & ~0x00003c00) | 0x00001000,
481 &gpio->pgcon);
wdenkca9bc762003-07-15 07:45:49 +0000482#endif
wdenk1fe2c702003-03-06 21:55:29 +0000483
wdenk49c3f672003-10-08 22:33:00 +0000484 /* toggle I2CSCL until bus idle */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900485 SetI2CSCL(0);
486 udelay(1000);
wdenk49c3f672003-10-08 22:33:00 +0000487 i = 10;
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900488 while ((i > 0) && (GetI2CSDA() != 1)) {
489 SetI2CSCL(1);
490 udelay(1000);
491 SetI2CSCL(0);
492 udelay(1000);
wdenk49c3f672003-10-08 22:33:00 +0000493 i--;
494 }
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900495 SetI2CSCL(1);
496 udelay(1000);
wdenk1fe2c702003-03-06 21:55:29 +0000497
wdenk49c3f672003-10-08 22:33:00 +0000498 /* restore pin functions */
wdenkca9bc762003-07-15 07:45:49 +0000499#ifdef CONFIG_S3C2410
C Nauman383c43e2010-10-26 23:04:31 +0900500 writel(old_gpecon, &gpio->gpecon);
wdenkca9bc762003-07-15 07:45:49 +0000501#endif
502#ifdef CONFIG_S3C2400
C Nauman383c43e2010-10-26 23:04:31 +0900503 writel(old_gpecon, &gpio->pgcon);
wdenkca9bc762003-07-15 07:45:49 +0000504#endif
wdenk49c3f672003-10-08 22:33:00 +0000505 }
Piotr Wilczekb35cd1c2012-11-20 02:19:05 +0000506#endif /* #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5) */
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000507 i2c_ch_init(i2c, speed, slaveadd);
wdenk1fe2c702003-03-06 21:55:29 +0000508}
509
510/*
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530511 * Poll the appropriate bit of the fifo status register until the interface is
512 * ready to process the next byte or timeout expires.
513 *
514 * In addition to the FIFO status register this function also polls the
515 * interrupt status register to be able to detect unexpected transaction
516 * completion.
517 *
518 * When FIFO is ready to process the next byte, this function returns I2C_OK.
519 * If in course of polling the INT_I2C assertion is detected, the function
520 * returns I2C_NOK. If timeout happens before any of the above conditions is
521 * met - the function returns I2C_NOK_TOUT;
522
523 * @param i2c: pointer to the appropriate i2c register bank.
524 * @param rx_transfer: set to True if the receive transaction is in progress.
525 * @return: as described above.
526 */
527static unsigned hsi2c_poll_fifo(struct exynos5_hsi2c *i2c, bool rx_transfer)
528{
529 u32 fifo_bit = rx_transfer ? HSI2C_RX_FIFO_EMPTY : HSI2C_TX_FIFO_FULL;
530 int i = HSI2C_TIMEOUT_US;
531
532 while (readl(&i2c->usi_fifo_stat) & fifo_bit) {
533 if (readl(&i2c->usi_int_stat) & HSI2C_INT_I2C_EN) {
534 /*
535 * There is a chance that assertion of
536 * HSI2C_INT_I2C_EN and deassertion of
537 * HSI2C_RX_FIFO_EMPTY happen simultaneously. Let's
538 * give FIFO status priority and check it one more
539 * time before reporting interrupt. The interrupt will
540 * be reported next time this function is called.
541 */
542 if (rx_transfer &&
543 !(readl(&i2c->usi_fifo_stat) & fifo_bit))
544 break;
545 return I2C_NOK;
546 }
547 if (!i--) {
548 debug("%s: FIFO polling timeout!\n", __func__);
549 return I2C_NOK_TOUT;
550 }
551 udelay(1);
552 }
553 return I2C_OK;
554}
555
556/*
557 * Preapre hsi2c transaction, either read or write.
558 *
559 * Set up transfer as described in section 27.5.1.2 'I2C Channel Auto Mode' of
560 * the 5420 UM.
561 *
562 * @param i2c: pointer to the appropriate i2c register bank.
563 * @param chip: slave address on the i2c bus (with read/write bit exlcuded)
564 * @param len: number of bytes expected to be sent or received
565 * @param rx_transfer: set to true for receive transactions
566 * @param: issue_stop: set to true if i2c stop condition should be generated
567 * after this transaction.
568 * @return: I2C_NOK_TOUT in case the bus remained busy for HSI2C_TIMEOUT_US,
569 * I2C_OK otherwise.
570 */
571static int hsi2c_prepare_transaction(struct exynos5_hsi2c *i2c,
572 u8 chip,
573 u16 len,
574 bool rx_transfer,
575 bool issue_stop)
576{
577 u32 conf;
578
579 conf = len | HSI2C_MASTER_RUN;
580
581 if (issue_stop)
582 conf |= HSI2C_STOP_AFTER_TRANS;
583
584 /* Clear to enable Timeout */
585 writel(readl(&i2c->usi_timeout) & ~HSI2C_TIMEOUT_EN, &i2c->usi_timeout);
586
587 /* Set slave address */
588 writel(HSI2C_SLV_ADDR_MAS(chip), &i2c->i2c_addr);
589
590 if (rx_transfer) {
591 /* i2c master, read transaction */
592 writel((HSI2C_RXCHON | HSI2C_FUNC_MODE_I2C | HSI2C_MASTER),
593 &i2c->usi_ctl);
594
595 /* read up to len bytes, stop after transaction is finished */
596 writel(conf | HSI2C_READ_WRITE, &i2c->usi_auto_conf);
597 } else {
598 /* i2c master, write transaction */
599 writel((HSI2C_TXCHON | HSI2C_FUNC_MODE_I2C | HSI2C_MASTER),
600 &i2c->usi_ctl);
601
602 /* write up to len bytes, stop after transaction is finished */
603 writel(conf, &i2c->usi_auto_conf);
604 }
605
606 /* Reset all pending interrupt status bits we care about, if any */
607 writel(HSI2C_INT_I2C_EN, &i2c->usi_int_stat);
608
609 return I2C_OK;
610}
611
612/*
613 * Wait while i2c bus is settling down (mostly stop gets completed).
614 */
615static int hsi2c_wait_while_busy(struct exynos5_hsi2c *i2c)
616{
617 int i = HSI2C_TIMEOUT_US;
618
619 while (readl(&i2c->usi_trans_status) & HSI2C_MASTER_BUSY) {
620 if (!i--) {
621 debug("%s: bus busy\n", __func__);
622 return I2C_NOK_TOUT;
623 }
624 udelay(1);
625 }
626 return I2C_OK;
627}
628
629static int hsi2c_write(struct exynos5_hsi2c *i2c,
630 unsigned char chip,
631 unsigned char addr[],
632 unsigned char alen,
633 unsigned char data[],
634 unsigned short len,
635 bool issue_stop)
636{
637 int i, rv = 0;
638
639 if (!(len + alen)) {
640 /* Writes of zero length not supported in auto mode. */
641 debug("%s: zero length writes not supported\n", __func__);
642 return I2C_NOK;
643 }
644
645 rv = hsi2c_prepare_transaction
646 (i2c, chip, len + alen, false, issue_stop);
647 if (rv != I2C_OK)
648 return rv;
649
650 /* Move address, if any, and the data, if any, into the FIFO. */
651 for (i = 0; i < alen; i++) {
652 rv = hsi2c_poll_fifo(i2c, false);
653 if (rv != I2C_OK) {
654 debug("%s: address write failed\n", __func__);
655 goto write_error;
656 }
657 writel(addr[i], &i2c->usi_txdata);
658 }
659
660 for (i = 0; i < len; i++) {
661 rv = hsi2c_poll_fifo(i2c, false);
662 if (rv != I2C_OK) {
663 debug("%s: data write failed\n", __func__);
664 goto write_error;
665 }
666 writel(data[i], &i2c->usi_txdata);
667 }
668
669 rv = hsi2c_wait_for_trx(i2c);
670
671 write_error:
672 if (issue_stop) {
673 int tmp_ret = hsi2c_wait_while_busy(i2c);
674 if (rv == I2C_OK)
675 rv = tmp_ret;
676 }
677
678 writel(HSI2C_FUNC_MODE_I2C, &i2c->usi_ctl); /* done */
679 return rv;
680}
681
682static int hsi2c_read(struct exynos5_hsi2c *i2c,
683 unsigned char chip,
684 unsigned char addr[],
685 unsigned char alen,
686 unsigned char data[],
687 unsigned short len)
688{
689 int i, rv, tmp_ret;
690 bool drop_data = false;
691
692 if (!len) {
693 /* Reads of zero length not supported in auto mode. */
694 debug("%s: zero length read adjusted\n", __func__);
695 drop_data = true;
696 len = 1;
697 }
698
699 if (alen) {
700 /* Internal register adress needs to be written first. */
701 rv = hsi2c_write(i2c, chip, addr, alen, NULL, 0, false);
702 if (rv != I2C_OK)
703 return rv;
704 }
705
706 rv = hsi2c_prepare_transaction(i2c, chip, len, true, true);
707
708 if (rv != I2C_OK)
709 return rv;
710
711 for (i = 0; i < len; i++) {
712 rv = hsi2c_poll_fifo(i2c, true);
713 if (rv != I2C_OK)
714 goto read_err;
715 if (drop_data)
716 continue;
717 data[i] = readl(&i2c->usi_rxdata);
718 }
719
720 rv = hsi2c_wait_for_trx(i2c);
721
722 read_err:
723 tmp_ret = hsi2c_wait_while_busy(i2c);
724 if (rv == I2C_OK)
725 rv = tmp_ret;
726
727 writel(HSI2C_FUNC_MODE_I2C, &i2c->usi_ctl); /* done */
728 return rv;
729}
730
731/*
wdenk49c3f672003-10-08 22:33:00 +0000732 * cmd_type is 0 for write, 1 for read.
733 *
734 * addr_len can take any value from 0-255, it is only limited
735 * by the char, we could make it larger if needed. If it is
736 * 0 we skip the address write cycle.
737 */
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000738static int i2c_transfer(struct s3c24x0_i2c *i2c,
739 unsigned char cmd_type,
740 unsigned char chip,
741 unsigned char addr[],
742 unsigned char addr_len,
743 unsigned char data[],
744 unsigned short data_len)
wdenk1fe2c702003-03-06 21:55:29 +0000745{
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530746 int i = 0, result;
747 ulong start_time = get_timer(0);
wdenk1fe2c702003-03-06 21:55:29 +0000748
wdenk49c3f672003-10-08 22:33:00 +0000749 if (data == 0 || data_len == 0) {
750 /*Don't support data transfer of no length or to address 0 */
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000751 debug("i2c_transfer: bad call\n");
wdenk49c3f672003-10-08 22:33:00 +0000752 return I2C_NOK;
753 }
wdenk1fe2c702003-03-06 21:55:29 +0000754
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530755 while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
756 if (get_timer(start_time) > I2C_TIMEOUT_MS)
757 return I2C_NOK_TOUT;
wdenk49c3f672003-10-08 22:33:00 +0000758 }
wdenk1fe2c702003-03-06 21:55:29 +0000759
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000760 writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon);
wdenk1fe2c702003-03-06 21:55:29 +0000761
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530762 /* Get the slave chip address going */
763 writel(chip, &i2c->iicds);
764 if ((cmd_type == I2C_WRITE) || (addr && addr_len))
765 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
766 &i2c->iicstat);
767 else
768 writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
769 &i2c->iicstat);
770
771 /* Wait for chip address to transmit. */
772 result = WaitForXfer(i2c);
773 if (result != I2C_OK)
774 goto bailout;
wdenk1fe2c702003-03-06 21:55:29 +0000775
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530776 /* If register address needs to be transmitted - do it now. */
777 if (addr && addr_len) {
778 while ((i < addr_len) && (result == I2C_OK)) {
779 writel(addr[i++], &i2c->iicds);
780 ReadWriteByte(i2c);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000781 result = WaitForXfer(i2c);
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530782 }
783 i = 0;
784 if (result != I2C_OK)
785 goto bailout;
786 }
wdenk1fe2c702003-03-06 21:55:29 +0000787
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530788 switch (cmd_type) {
789 case I2C_WRITE:
790 while ((i < data_len) && (result == I2C_OK)) {
791 writel(data[i++], &i2c->iicds);
792 ReadWriteByte(i2c);
793 result = WaitForXfer(i2c);
794 }
wdenk49c3f672003-10-08 22:33:00 +0000795 break;
wdenk1fe2c702003-03-06 21:55:29 +0000796
wdenk7539dea2003-06-19 23:01:32 +0000797 case I2C_READ:
wdenk49c3f672003-10-08 22:33:00 +0000798 if (addr && addr_len) {
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530799 /*
800 * Register address has been sent, now send slave chip
801 * address again to start the actual read transaction.
802 */
C Nauman383c43e2010-10-26 23:04:31 +0900803 writel(chip, &i2c->iicds);
wdenk1fe2c702003-03-06 21:55:29 +0000804
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530805 /* Generate a re-START. */
Rajeshwari Shindee076adf2013-02-19 02:19:45 +0000806 writel(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
807 &i2c->iicstat);
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530808 ReadWriteByte(i2c);
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000809 result = WaitForXfer(i2c);
wdenk49c3f672003-10-08 22:33:00 +0000810
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530811 if (result != I2C_OK)
812 goto bailout;
wdenk1fe2c702003-03-06 21:55:29 +0000813 }
wdenk1fe2c702003-03-06 21:55:29 +0000814
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530815 while ((i < data_len) && (result == I2C_OK)) {
816 /* disable ACK for final READ */
817 if (i == data_len - 1)
818 writel(readl(&i2c->iiccon)
819 & ~I2CCON_ACKGEN,
820 &i2c->iiccon);
821 ReadWriteByte(i2c);
822 result = WaitForXfer(i2c);
823 data[i++] = readl(&i2c->iicds);
824 }
825 if (result == I2C_NACK)
826 result = I2C_OK; /* Normal terminated read. */
wdenk49c3f672003-10-08 22:33:00 +0000827 break;
wdenk1fe2c702003-03-06 21:55:29 +0000828
829 default:
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000830 debug("i2c_transfer: bad call\n");
wdenk49c3f672003-10-08 22:33:00 +0000831 result = I2C_NOK;
832 break;
833 }
wdenk1fe2c702003-03-06 21:55:29 +0000834
Naveen Krishna Ch40e1e7b2013-10-15 16:01:43 +0530835bailout:
836 /* Send STOP. */
837 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
838 ReadWriteByte(i2c);
839
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000840 return result;
wdenk1fe2c702003-03-06 21:55:29 +0000841}
842
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900843int i2c_probe(uchar chip)
wdenk1fe2c702003-03-06 21:55:29 +0000844{
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530845 struct s3c24x0_i2c_bus *i2c_bus;
wdenk49c3f672003-10-08 22:33:00 +0000846 uchar buf[1];
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530847 int ret;
wdenk1fe2c702003-03-06 21:55:29 +0000848
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530849 i2c_bus = get_bus(g_current_bus);
850 if (!i2c_bus)
851 return -1;
wdenk49c3f672003-10-08 22:33:00 +0000852 buf[0] = 0;
wdenk1fe2c702003-03-06 21:55:29 +0000853
wdenk49c3f672003-10-08 22:33:00 +0000854 /*
855 * What is needed is to send the chip address and verify that the
856 * address was <ACK>ed (i.e. there was a chip at that address which
857 * drove the data line low).
858 */
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530859 if (i2c_bus->is_highspeed) {
860 ret = hsi2c_read(i2c_bus->hsregs,
861 chip, 0, 0, buf, 1);
862 } else {
863 ret = i2c_transfer(i2c_bus->regs,
864 I2C_READ, chip << 1, 0, 0, buf, 1);
865 }
866
867
868 return ret != I2C_OK;
wdenk1fe2c702003-03-06 21:55:29 +0000869}
870
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900871int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
wdenk1fe2c702003-03-06 21:55:29 +0000872{
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530873 struct s3c24x0_i2c_bus *i2c_bus;
wdenk49c3f672003-10-08 22:33:00 +0000874 uchar xaddr[4];
875 int ret;
wdenk1fe2c702003-03-06 21:55:29 +0000876
wdenk49c3f672003-10-08 22:33:00 +0000877 if (alen > 4) {
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000878 debug("I2C read: addr len %d not supported\n", alen);
wdenk49c3f672003-10-08 22:33:00 +0000879 return 1;
880 }
wdenk1fe2c702003-03-06 21:55:29 +0000881
wdenk49c3f672003-10-08 22:33:00 +0000882 if (alen > 0) {
883 xaddr[0] = (addr >> 24) & 0xFF;
884 xaddr[1] = (addr >> 16) & 0xFF;
885 xaddr[2] = (addr >> 8) & 0xFF;
886 xaddr[3] = addr & 0xFF;
887 }
wdenk1fe2c702003-03-06 21:55:29 +0000888
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200889#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
wdenk49c3f672003-10-08 22:33:00 +0000890 /*
891 * EEPROM chips that implement "address overflow" are ones
892 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
893 * address and the extra bits end up in the "chip address"
894 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
895 * four 256 byte chips.
896 *
897 * Note that we consider the length of the address field to
898 * still be one byte because the extra address bits are
899 * hidden in the chip address.
900 */
901 if (alen > 0)
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900902 chip |= ((addr >> (alen * 8)) &
903 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
wdenk1fe2c702003-03-06 21:55:29 +0000904#endif
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530905 i2c_bus = get_bus(g_current_bus);
906 if (!i2c_bus)
907 return -1;
908
909 if (i2c_bus->is_highspeed)
910 ret = hsi2c_read(i2c_bus->hsregs, chip, &xaddr[4 - alen],
911 alen, buffer, len);
912 else
913 ret = i2c_transfer(i2c_bus->regs, I2C_READ, chip << 1,
914 &xaddr[4 - alen], alen, buffer, len);
915
916 if (ret) {
917 if (i2c_bus->is_highspeed)
918 exynos5_i2c_reset(i2c_bus);
919 debug("I2c read failed %d\n", ret);
wdenk49c3f672003-10-08 22:33:00 +0000920 return 1;
921 }
922 return 0;
wdenk1fe2c702003-03-06 21:55:29 +0000923}
924
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900925int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
wdenk1fe2c702003-03-06 21:55:29 +0000926{
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530927 struct s3c24x0_i2c_bus *i2c_bus;
wdenk49c3f672003-10-08 22:33:00 +0000928 uchar xaddr[4];
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530929 int ret;
wdenk1fe2c702003-03-06 21:55:29 +0000930
wdenk49c3f672003-10-08 22:33:00 +0000931 if (alen > 4) {
Rajeshwari Shinde4b4480a2012-07-23 21:23:53 +0000932 debug("I2C write: addr len %d not supported\n", alen);
wdenk49c3f672003-10-08 22:33:00 +0000933 return 1;
934 }
wdenk1fe2c702003-03-06 21:55:29 +0000935
wdenk49c3f672003-10-08 22:33:00 +0000936 if (alen > 0) {
937 xaddr[0] = (addr >> 24) & 0xFF;
938 xaddr[1] = (addr >> 16) & 0xFF;
939 xaddr[2] = (addr >> 8) & 0xFF;
940 xaddr[3] = addr & 0xFF;
941 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200942#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
wdenk49c3f672003-10-08 22:33:00 +0000943 /*
944 * EEPROM chips that implement "address overflow" are ones
945 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
946 * address and the extra bits end up in the "chip address"
947 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
948 * four 256 byte chips.
949 *
950 * Note that we consider the length of the address field to
951 * still be one byte because the extra address bits are
952 * hidden in the chip address.
953 */
954 if (alen > 0)
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900955 chip |= ((addr >> (alen * 8)) &
956 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
wdenk1fe2c702003-03-06 21:55:29 +0000957#endif
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530958 i2c_bus = get_bus(g_current_bus);
959 if (!i2c_bus)
960 return -1;
961
962 if (i2c_bus->is_highspeed)
963 ret = hsi2c_write(i2c_bus->hsregs, chip, &xaddr[4 - alen],
964 alen, buffer, len, true);
965 else
966 ret = i2c_transfer(i2c_bus->regs, I2C_WRITE, chip << 1,
967 &xaddr[4 - alen], alen, buffer, len);
968
969 if (ret != 0) {
970 if (i2c_bus->is_highspeed)
971 exynos5_i2c_reset(i2c_bus);
972 return 1;
973 } else {
974 return 0;
975 }
wdenk1fe2c702003-03-06 21:55:29 +0000976}
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +0000977
Amara8b35732013-07-10 10:42:29 +0530978#ifdef CONFIG_OF_CONTROL
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530979static void process_nodes(const void *blob, int node_list[], int count,
980 int is_highspeed)
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +0000981{
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530982 struct s3c24x0_i2c_bus *bus;
Amar8733d752013-04-04 02:27:06 -0400983 int i;
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +0000984
985 for (i = 0; i < count; i++) {
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +0000986 int node = node_list[i];
987
988 if (node <= 0)
989 continue;
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530990
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +0000991 bus = &i2c_bus[i];
Simon Glass2ccb6f42013-10-15 16:02:10 +0530992 bus->active = true;
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +0530993 bus->is_highspeed = is_highspeed;
994
995 if (is_highspeed)
996 bus->hsregs = (struct exynos5_hsi2c *)
997 fdtdec_get_addr(blob, node, "reg");
998 else
999 bus->regs = (struct s3c24x0_i2c *)
1000 fdtdec_get_addr(blob, node, "reg");
1001
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +00001002 bus->id = pinmux_decode_periph_id(blob, node);
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +05301003 bus->clock_frequency = fdtdec_get_int(blob, node,
1004 "clock-frequency",
1005 CONFIG_SYS_I2C_SPEED);
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +00001006 bus->node = node;
Simon Glass2ccb6f42013-10-15 16:02:10 +05301007 bus->bus_num = i;
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +00001008 exynos_pinmux_config(bus->id, 0);
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +05301009
1010 /* Mark position as used */
1011 node_list[i] = -1;
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +00001012 }
1013}
1014
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +05301015void board_i2c_init(const void *blob)
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +00001016{
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +05301017 int node_list[CONFIG_MAX_I2C_NUM];
1018 int count;
Simon Glass2ccb6f42013-10-15 16:02:10 +05301019
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +05301020 /* First get the normal i2c ports */
1021 count = fdtdec_find_aliases_for_id(blob, "i2c",
1022 COMPAT_SAMSUNG_S3C2440_I2C, node_list,
1023 CONFIG_MAX_I2C_NUM);
1024 process_nodes(blob, node_list, count, 0);
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +00001025
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +05301026 /* Now look for high speed i2c ports */
1027 count = fdtdec_find_aliases_for_id(blob, "i2c",
1028 COMPAT_SAMSUNG_EXYNOS5_I2C, node_list,
1029 CONFIG_MAX_I2C_NUM);
1030 process_nodes(blob, node_list, count, 1);
1031
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +00001032}
1033
1034int i2c_get_bus_num_fdt(int node)
1035{
1036 int i;
1037
Simon Glass2ccb6f42013-10-15 16:02:10 +05301038 for (i = 0; i < ARRAY_SIZE(i2c_bus); i++) {
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +00001039 if (node == i2c_bus[i].node)
1040 return i;
1041 }
1042
1043 debug("%s: Can't find any matched I2C bus\n", __func__);
1044 return -1;
1045}
1046
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +05301047#ifdef CONFIG_I2C_MULTI_BUS
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +00001048int i2c_reset_port_fdt(const void *blob, int node)
1049{
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +05301050 struct s3c24x0_i2c_bus *i2c_bus;
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +00001051 int bus;
1052
1053 bus = i2c_get_bus_num_fdt(node);
1054 if (bus < 0) {
1055 debug("could not get bus for node %d\n", node);
1056 return -1;
1057 }
1058
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +05301059 i2c_bus = get_bus(bus);
1060 if (!i2c_bus) {
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +00001061 debug("get_bus() failed for node node %d\n", node);
1062 return -1;
1063 }
1064
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +05301065 if (i2c_bus->is_highspeed) {
1066 if (hsi2c_get_clk_details(i2c_bus))
1067 return -1;
1068 hsi2c_ch_init(i2c_bus);
1069 } else {
1070 i2c_ch_init(i2c_bus->regs, i2c_bus->clock_frequency,
1071 CONFIG_SYS_I2C_SLAVE);
1072 }
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +00001073
1074 return 0;
1075}
1076#endif
Naveen Krishna Ch64a191f2013-10-15 16:02:44 +05301077#endif
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +00001078
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +09001079#endif /* CONFIG_HARD_I2C */