blob: 189ce6d5096af1b802cd6228493169c3f545b657 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb9d7f992016-11-23 06:34:43 -07002/*
3 * Copyright (c) 2016, Google Inc
4 *
5 * (C) Copyright 2002
6 * David Mueller, ELSOFT AG, d.mueller@elsoft.ch
Simon Glassb9d7f992016-11-23 06:34:43 -07007 */
8
Simon Glassb9d7f992016-11-23 06:34:43 -07009#include <dm.h>
10#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glassb9d7f992016-11-23 06:34:43 -070012#include <asm/arch/clk.h>
13#include <asm/arch/cpu.h>
14#include <asm/arch/pinmux.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060016#include <linux/delay.h>
Simon Glassb9d7f992016-11-23 06:34:43 -070017#include "s3c24x0_i2c.h"
18
19DECLARE_GLOBAL_DATA_PTR;
20
21/* HSI2C-specific register description */
22
23/* I2C_CTL Register bits */
24#define HSI2C_FUNC_MODE_I2C (1u << 0)
25#define HSI2C_MASTER (1u << 3)
26#define HSI2C_RXCHON (1u << 6) /* Write/Send */
27#define HSI2C_TXCHON (1u << 7) /* Read/Receive */
28#define HSI2C_SW_RST (1u << 31)
29
30/* I2C_FIFO_CTL Register bits */
31#define HSI2C_RXFIFO_EN (1u << 0)
32#define HSI2C_TXFIFO_EN (1u << 1)
33#define HSI2C_TXFIFO_TRIGGER_LEVEL (0x20 << 16)
34#define HSI2C_RXFIFO_TRIGGER_LEVEL (0x20 << 4)
35
36/* I2C_TRAILING_CTL Register bits */
37#define HSI2C_TRAILING_COUNT (0xff)
38
39/* I2C_INT_EN Register bits */
40#define HSI2C_TX_UNDERRUN_EN (1u << 2)
41#define HSI2C_TX_OVERRUN_EN (1u << 3)
42#define HSI2C_RX_UNDERRUN_EN (1u << 4)
43#define HSI2C_RX_OVERRUN_EN (1u << 5)
44#define HSI2C_INT_TRAILING_EN (1u << 6)
45#define HSI2C_INT_I2C_EN (1u << 9)
46
47#define HSI2C_INT_ERROR_MASK (HSI2C_TX_UNDERRUN_EN |\
48 HSI2C_TX_OVERRUN_EN |\
49 HSI2C_RX_UNDERRUN_EN |\
50 HSI2C_RX_OVERRUN_EN |\
51 HSI2C_INT_TRAILING_EN)
52
53/* I2C_CONF Register bits */
54#define HSI2C_AUTO_MODE (1u << 31)
55#define HSI2C_10BIT_ADDR_MODE (1u << 30)
56#define HSI2C_HS_MODE (1u << 29)
57
58/* I2C_AUTO_CONF Register bits */
59#define HSI2C_READ_WRITE (1u << 16)
60#define HSI2C_STOP_AFTER_TRANS (1u << 17)
61#define HSI2C_MASTER_RUN (1u << 31)
62
63/* I2C_TIMEOUT Register bits */
64#define HSI2C_TIMEOUT_EN (1u << 31)
65
66/* I2C_TRANS_STATUS register bits */
67#define HSI2C_MASTER_BUSY (1u << 17)
68#define HSI2C_SLAVE_BUSY (1u << 16)
69#define HSI2C_TIMEOUT_AUTO (1u << 4)
70#define HSI2C_NO_DEV (1u << 3)
71#define HSI2C_NO_DEV_ACK (1u << 2)
72#define HSI2C_TRANS_ABORT (1u << 1)
73#define HSI2C_TRANS_SUCCESS (1u << 0)
74#define HSI2C_TRANS_ERROR_MASK (HSI2C_TIMEOUT_AUTO |\
75 HSI2C_NO_DEV | HSI2C_NO_DEV_ACK |\
76 HSI2C_TRANS_ABORT)
77#define HSI2C_TRANS_FINISHED_MASK (HSI2C_TRANS_ERROR_MASK | HSI2C_TRANS_SUCCESS)
78
Simon Glassb9d7f992016-11-23 06:34:43 -070079/* I2C_FIFO_STAT Register bits */
80#define HSI2C_RX_FIFO_EMPTY (1u << 24)
81#define HSI2C_RX_FIFO_FULL (1u << 23)
82#define HSI2C_TX_FIFO_EMPTY (1u << 8)
83#define HSI2C_TX_FIFO_FULL (1u << 7)
84#define HSI2C_RX_FIFO_LEVEL(x) (((x) >> 16) & 0x7f)
85#define HSI2C_TX_FIFO_LEVEL(x) ((x) & 0x7f)
86
87#define HSI2C_SLV_ADDR_MAS(x) ((x & 0x3ff) << 10)
88
89#define HSI2C_TIMEOUT_US 10000 /* 10 ms, finer granularity */
90
91/*
92 * Wait for transfer completion.
93 *
94 * This function reads the interrupt status register waiting for the INT_I2C
95 * bit to be set, which indicates copletion of a transaction.
96 *
97 * @param i2c: pointer to the appropriate register bank
98 *
99 * @return: I2C_OK in case of successful completion, I2C_NOK_TIMEOUT in case
100 * the status bits do not get set in time, or an approrpiate error
101 * value in case of transfer errors.
102 */
103static int hsi2c_wait_for_trx(struct exynos5_hsi2c *i2c)
104{
105 int i = HSI2C_TIMEOUT_US;
106
107 while (i-- > 0) {
108 u32 int_status = readl(&i2c->usi_int_stat);
109
110 if (int_status & HSI2C_INT_I2C_EN) {
111 u32 trans_status = readl(&i2c->usi_trans_status);
112
113 /* Deassert pending interrupt. */
114 writel(int_status, &i2c->usi_int_stat);
115
116 if (trans_status & HSI2C_NO_DEV_ACK) {
117 debug("%s: no ACK from device\n", __func__);
118 return I2C_NACK;
119 }
120 if (trans_status & HSI2C_NO_DEV) {
121 debug("%s: no device\n", __func__);
122 return I2C_NOK;
123 }
124 if (trans_status & HSI2C_TRANS_ABORT) {
125 debug("%s: arbitration lost\n", __func__);
126 return I2C_NOK_LA;
127 }
128 if (trans_status & HSI2C_TIMEOUT_AUTO) {
129 debug("%s: device timed out\n", __func__);
130 return I2C_NOK_TOUT;
131 }
132 return I2C_OK;
133 }
134 udelay(1);
135 }
136 debug("%s: transaction timeout!\n", __func__);
137 return I2C_NOK_TOUT;
138}
139
140static int hsi2c_get_clk_details(struct s3c24x0_i2c_bus *i2c_bus)
141{
142 struct exynos5_hsi2c *hsregs = i2c_bus->hsregs;
143 ulong clkin;
144 unsigned int op_clk = i2c_bus->clock_frequency;
145 unsigned int i = 0, utemp0 = 0, utemp1 = 0;
146 unsigned int t_ftl_cycle;
147
Simon Glassb9d7f992016-11-23 06:34:43 -0700148 clkin = get_i2c_clk();
Simon Glassb9d7f992016-11-23 06:34:43 -0700149 /* FPCLK / FI2C =
150 * (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2) + 8 + 2 * FLT_CYCLE
151 * uTemp0 = (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2)
152 * uTemp1 = (TSCLK_L + TSCLK_H + 2)
153 * uTemp2 = TSCLK_L + TSCLK_H
154 */
155 t_ftl_cycle = (readl(&hsregs->usi_conf) >> 16) & 0x7;
156 utemp0 = (clkin / op_clk) - 8 - 2 * t_ftl_cycle;
157
158 /* CLK_DIV max is 256 */
159 for (i = 0; i < 256; i++) {
160 utemp1 = utemp0 / (i + 1);
161 if ((utemp1 < 512) && (utemp1 > 4)) {
162 i2c_bus->clk_cycle = utemp1 - 2;
163 i2c_bus->clk_div = i;
164 return 0;
165 }
166 }
167 return -EINVAL;
168}
169
170static void hsi2c_ch_init(struct s3c24x0_i2c_bus *i2c_bus)
171{
172 struct exynos5_hsi2c *hsregs = i2c_bus->hsregs;
173 unsigned int t_sr_release;
174 unsigned int n_clkdiv;
175 unsigned int t_start_su, t_start_hd;
176 unsigned int t_stop_su;
177 unsigned int t_data_su, t_data_hd;
178 unsigned int t_scl_l, t_scl_h;
179 u32 i2c_timing_s1;
180 u32 i2c_timing_s2;
181 u32 i2c_timing_s3;
182 u32 i2c_timing_sla;
183
184 n_clkdiv = i2c_bus->clk_div;
185 t_scl_l = i2c_bus->clk_cycle / 2;
186 t_scl_h = i2c_bus->clk_cycle / 2;
187 t_start_su = t_scl_l;
188 t_start_hd = t_scl_l;
189 t_stop_su = t_scl_l;
190 t_data_su = t_scl_l / 2;
191 t_data_hd = t_scl_l / 2;
192 t_sr_release = i2c_bus->clk_cycle;
193
194 i2c_timing_s1 = t_start_su << 24 | t_start_hd << 16 | t_stop_su << 8;
195 i2c_timing_s2 = t_data_su << 24 | t_scl_l << 8 | t_scl_h << 0;
196 i2c_timing_s3 = n_clkdiv << 16 | t_sr_release << 0;
197 i2c_timing_sla = t_data_hd << 0;
198
199 writel(HSI2C_TRAILING_COUNT, &hsregs->usi_trailing_ctl);
200
201 /* Clear to enable Timeout */
202 clrsetbits_le32(&hsregs->usi_timeout, HSI2C_TIMEOUT_EN, 0);
203
204 /* set AUTO mode */
205 writel(readl(&hsregs->usi_conf) | HSI2C_AUTO_MODE, &hsregs->usi_conf);
206
207 /* Enable completion conditions' reporting. */
208 writel(HSI2C_INT_I2C_EN, &hsregs->usi_int_en);
209
210 /* Enable FIFOs */
211 writel(HSI2C_RXFIFO_EN | HSI2C_TXFIFO_EN, &hsregs->usi_fifo_ctl);
212
213 /* Currently operating in Fast speed mode. */
214 writel(i2c_timing_s1, &hsregs->usi_timing_fs1);
215 writel(i2c_timing_s2, &hsregs->usi_timing_fs2);
216 writel(i2c_timing_s3, &hsregs->usi_timing_fs3);
217 writel(i2c_timing_sla, &hsregs->usi_timing_sla);
218}
219
220/* SW reset for the high speed bus */
221static void exynos5_i2c_reset(struct s3c24x0_i2c_bus *i2c_bus)
222{
223 struct exynos5_hsi2c *i2c = i2c_bus->hsregs;
224 u32 i2c_ctl;
225
226 /* Set and clear the bit for reset */
227 i2c_ctl = readl(&i2c->usi_ctl);
228 i2c_ctl |= HSI2C_SW_RST;
229 writel(i2c_ctl, &i2c->usi_ctl);
230
231 i2c_ctl = readl(&i2c->usi_ctl);
232 i2c_ctl &= ~HSI2C_SW_RST;
233 writel(i2c_ctl, &i2c->usi_ctl);
234
235 /* Initialize the configure registers */
236 hsi2c_ch_init(i2c_bus);
237}
238
239/*
240 * Poll the appropriate bit of the fifo status register until the interface is
241 * ready to process the next byte or timeout expires.
242 *
243 * In addition to the FIFO status register this function also polls the
244 * interrupt status register to be able to detect unexpected transaction
245 * completion.
246 *
247 * When FIFO is ready to process the next byte, this function returns I2C_OK.
248 * If in course of polling the INT_I2C assertion is detected, the function
249 * returns I2C_NOK. If timeout happens before any of the above conditions is
250 * met - the function returns I2C_NOK_TOUT;
251
252 * @param i2c: pointer to the appropriate i2c register bank.
253 * @param rx_transfer: set to True if the receive transaction is in progress.
254 * @return: as described above.
255 */
256static unsigned hsi2c_poll_fifo(struct exynos5_hsi2c *i2c, bool rx_transfer)
257{
258 u32 fifo_bit = rx_transfer ? HSI2C_RX_FIFO_EMPTY : HSI2C_TX_FIFO_FULL;
259 int i = HSI2C_TIMEOUT_US;
260
261 while (readl(&i2c->usi_fifo_stat) & fifo_bit) {
262 if (readl(&i2c->usi_int_stat) & HSI2C_INT_I2C_EN) {
263 /*
264 * There is a chance that assertion of
265 * HSI2C_INT_I2C_EN and deassertion of
266 * HSI2C_RX_FIFO_EMPTY happen simultaneously. Let's
267 * give FIFO status priority and check it one more
268 * time before reporting interrupt. The interrupt will
269 * be reported next time this function is called.
270 */
271 if (rx_transfer &&
272 !(readl(&i2c->usi_fifo_stat) & fifo_bit))
273 break;
274 return I2C_NOK;
275 }
276 if (!i--) {
277 debug("%s: FIFO polling timeout!\n", __func__);
278 return I2C_NOK_TOUT;
279 }
280 udelay(1);
281 }
282 return I2C_OK;
283}
284
285/*
286 * Preapre hsi2c transaction, either read or write.
287 *
288 * Set up transfer as described in section 27.5.1.2 'I2C Channel Auto Mode' of
289 * the 5420 UM.
290 *
291 * @param i2c: pointer to the appropriate i2c register bank.
292 * @param chip: slave address on the i2c bus (with read/write bit exlcuded)
293 * @param len: number of bytes expected to be sent or received
294 * @param rx_transfer: set to true for receive transactions
295 * @param: issue_stop: set to true if i2c stop condition should be generated
296 * after this transaction.
297 * @return: I2C_NOK_TOUT in case the bus remained busy for HSI2C_TIMEOUT_US,
298 * I2C_OK otherwise.
299 */
300static int hsi2c_prepare_transaction(struct exynos5_hsi2c *i2c,
301 u8 chip,
302 u16 len,
303 bool rx_transfer,
304 bool issue_stop)
305{
306 u32 conf;
307
308 conf = len | HSI2C_MASTER_RUN;
309
310 if (issue_stop)
311 conf |= HSI2C_STOP_AFTER_TRANS;
312
313 /* Clear to enable Timeout */
314 writel(readl(&i2c->usi_timeout) & ~HSI2C_TIMEOUT_EN, &i2c->usi_timeout);
315
316 /* Set slave address */
317 writel(HSI2C_SLV_ADDR_MAS(chip), &i2c->i2c_addr);
318
319 if (rx_transfer) {
320 /* i2c master, read transaction */
321 writel((HSI2C_RXCHON | HSI2C_FUNC_MODE_I2C | HSI2C_MASTER),
322 &i2c->usi_ctl);
323
324 /* read up to len bytes, stop after transaction is finished */
325 writel(conf | HSI2C_READ_WRITE, &i2c->usi_auto_conf);
326 } else {
327 /* i2c master, write transaction */
328 writel((HSI2C_TXCHON | HSI2C_FUNC_MODE_I2C | HSI2C_MASTER),
329 &i2c->usi_ctl);
330
331 /* write up to len bytes, stop after transaction is finished */
332 writel(conf, &i2c->usi_auto_conf);
333 }
334
335 /* Reset all pending interrupt status bits we care about, if any */
336 writel(HSI2C_INT_I2C_EN, &i2c->usi_int_stat);
337
338 return I2C_OK;
339}
340
341/*
342 * Wait while i2c bus is settling down (mostly stop gets completed).
343 */
344static int hsi2c_wait_while_busy(struct exynos5_hsi2c *i2c)
345{
346 int i = HSI2C_TIMEOUT_US;
347
348 while (readl(&i2c->usi_trans_status) & HSI2C_MASTER_BUSY) {
349 if (!i--) {
350 debug("%s: bus busy\n", __func__);
351 return I2C_NOK_TOUT;
352 }
353 udelay(1);
354 }
355 return I2C_OK;
356}
357
358static int hsi2c_write(struct exynos5_hsi2c *i2c,
359 unsigned char chip,
360 unsigned char addr[],
361 unsigned char alen,
362 unsigned char data[],
363 unsigned short len,
364 bool issue_stop)
365{
366 int i, rv = 0;
367
368 if (!(len + alen)) {
369 /* Writes of zero length not supported in auto mode. */
370 debug("%s: zero length writes not supported\n", __func__);
371 return I2C_NOK;
372 }
373
374 rv = hsi2c_prepare_transaction
375 (i2c, chip, len + alen, false, issue_stop);
376 if (rv != I2C_OK)
377 return rv;
378
379 /* Move address, if any, and the data, if any, into the FIFO. */
380 for (i = 0; i < alen; i++) {
381 rv = hsi2c_poll_fifo(i2c, false);
382 if (rv != I2C_OK) {
383 debug("%s: address write failed\n", __func__);
384 goto write_error;
385 }
386 writel(addr[i], &i2c->usi_txdata);
387 }
388
389 for (i = 0; i < len; i++) {
390 rv = hsi2c_poll_fifo(i2c, false);
391 if (rv != I2C_OK) {
392 debug("%s: data write failed\n", __func__);
393 goto write_error;
394 }
395 writel(data[i], &i2c->usi_txdata);
396 }
397
398 rv = hsi2c_wait_for_trx(i2c);
399
400 write_error:
401 if (issue_stop) {
402 int tmp_ret = hsi2c_wait_while_busy(i2c);
403 if (rv == I2C_OK)
404 rv = tmp_ret;
405 }
406
407 writel(HSI2C_FUNC_MODE_I2C, &i2c->usi_ctl); /* done */
408 return rv;
409}
410
411static int hsi2c_read(struct exynos5_hsi2c *i2c,
412 unsigned char chip,
413 unsigned char addr[],
414 unsigned char alen,
415 unsigned char data[],
416 unsigned short len)
417{
418 int i, rv, tmp_ret;
419 bool drop_data = false;
420
421 if (!len) {
422 /* Reads of zero length not supported in auto mode. */
423 debug("%s: zero length read adjusted\n", __func__);
424 drop_data = true;
425 len = 1;
426 }
427
428 if (alen) {
429 /* Internal register adress needs to be written first. */
430 rv = hsi2c_write(i2c, chip, addr, alen, NULL, 0, false);
431 if (rv != I2C_OK)
432 return rv;
433 }
434
435 rv = hsi2c_prepare_transaction(i2c, chip, len, true, true);
436
437 if (rv != I2C_OK)
438 return rv;
439
440 for (i = 0; i < len; i++) {
441 rv = hsi2c_poll_fifo(i2c, true);
442 if (rv != I2C_OK)
443 goto read_err;
444 if (drop_data)
445 continue;
446 data[i] = readl(&i2c->usi_rxdata);
447 }
448
449 rv = hsi2c_wait_for_trx(i2c);
450
451 read_err:
452 tmp_ret = hsi2c_wait_while_busy(i2c);
453 if (rv == I2C_OK)
454 rv = tmp_ret;
455
456 writel(HSI2C_FUNC_MODE_I2C, &i2c->usi_ctl); /* done */
457 return rv;
458}
459
460static int exynos_hs_i2c_xfer(struct udevice *dev, struct i2c_msg *msg,
461 int nmsgs)
462{
463 struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
464 struct exynos5_hsi2c *hsregs = i2c_bus->hsregs;
465 int ret;
466
467 for (; nmsgs > 0; nmsgs--, msg++) {
468 if (msg->flags & I2C_M_RD) {
469 ret = hsi2c_read(hsregs, msg->addr, 0, 0, msg->buf,
470 msg->len);
471 } else {
472 ret = hsi2c_write(hsregs, msg->addr, 0, 0, msg->buf,
473 msg->len, true);
474 }
475 if (ret) {
476 exynos5_i2c_reset(i2c_bus);
477 return -EREMOTEIO;
478 }
479 }
480
481 return 0;
482}
483
484static int s3c24x0_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
485{
486 struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
487
488 i2c_bus->clock_frequency = speed;
489
490 if (hsi2c_get_clk_details(i2c_bus))
491 return -EFAULT;
492 hsi2c_ch_init(i2c_bus);
493
494 return 0;
495}
496
497static int s3c24x0_i2c_probe(struct udevice *dev, uint chip, uint chip_flags)
498{
499 struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
500 uchar buf[1];
501 int ret;
502
503 buf[0] = 0;
504
505 /*
506 * What is needed is to send the chip address and verify that the
507 * address was <ACK>ed (i.e. there was a chip at that address which
508 * drove the data line low).
509 */
510 ret = hsi2c_read(i2c_bus->hsregs, chip, 0, 0, buf, 1);
511
512 return ret != I2C_OK;
513}
514
Simon Glassaad29ae2020-12-03 16:55:21 -0700515static int s3c_i2c_of_to_plat(struct udevice *dev)
Simon Glassb9d7f992016-11-23 06:34:43 -0700516{
517 const void *blob = gd->fdt_blob;
518 struct s3c24x0_i2c_bus *i2c_bus = dev_get_priv(dev);
519 int node;
520
Simon Glassdd79d6e2017-01-17 16:52:55 -0700521 node = dev_of_offset(dev);
Simon Glassb9d7f992016-11-23 06:34:43 -0700522
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900523 i2c_bus->hsregs = dev_read_addr_ptr(dev);
Simon Glassb9d7f992016-11-23 06:34:43 -0700524
525 i2c_bus->id = pinmux_decode_periph_id(blob, node);
526
Simon Glassf0c99c52020-01-23 11:48:22 -0700527 i2c_bus->clock_frequency =
528 dev_read_u32_default(dev, "clock-frequency",
529 I2C_SPEED_STANDARD_RATE);
Simon Glassb9d7f992016-11-23 06:34:43 -0700530 i2c_bus->node = node;
Simon Glass75e534b2020-12-16 21:20:07 -0700531 i2c_bus->bus_num = dev_seq(dev);
Simon Glassb9d7f992016-11-23 06:34:43 -0700532
533 exynos_pinmux_config(i2c_bus->id, PINMUX_FLAG_HS_MODE);
534
535 i2c_bus->active = true;
536
537 return 0;
538}
539
540static const struct dm_i2c_ops exynos_hs_i2c_ops = {
541 .xfer = exynos_hs_i2c_xfer,
542 .probe_chip = s3c24x0_i2c_probe,
543 .set_bus_speed = s3c24x0_i2c_set_bus_speed,
544};
545
546static const struct udevice_id exynos_hs_i2c_ids[] = {
547 { .compatible = "samsung,exynos5-hsi2c" },
548 { }
549};
550
551U_BOOT_DRIVER(hs_i2c) = {
552 .name = "i2c_s3c_hs",
553 .id = UCLASS_I2C,
554 .of_match = exynos_hs_i2c_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700555 .of_to_plat = s3c_i2c_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700556 .priv_auto = sizeof(struct s3c24x0_i2c_bus),
Simon Glassb9d7f992016-11-23 06:34:43 -0700557 .ops = &exynos_hs_i2c_ops,
558};