summaryrefslogtreecommitdiff
path: root/drivers/i2c/nx_i2c.c
blob: ca14a0ecac71655ed71cf6c7451988513f97e275 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
#include <common.h>
#include <errno.h>
#include <dm.h>
#include <i2c.h>
#include <log.h>
#include <asm/arch/nexell.h>
#include <asm/arch/reset.h>
#include <asm/arch/clk.h>
#include <asm/arch/nx_gpio.h>
#include <linux/delay.h>

#define I2C_WRITE       0
#define I2C_READ        1

#define I2CSTAT_MTM     0xC0    /* Master Transmit Mode */
#define I2CSTAT_MRM     0x80    /* Master Receive Mode */
#define I2CSTAT_BSY     0x20    /* Read: Bus Busy */
#define I2CSTAT_SS      0x20    /* Write: START (1) / STOP (0) */
#define I2CSTAT_RXTXEN  0x10    /* Rx/Tx enable */
#define I2CSTAT_ABT	0x08	/* Arbitration bit */
#define I2CSTAT_NACK    0x01    /* Nack bit */
#define I2CCON_IRCLR    0x100   /* Interrupt Clear bit  */
#define I2CCON_ACKGEN   0x80    /* Acknowledge generation */
#define I2CCON_TCP256	0x40    /* Tx-clock prescaler: 16 (0) / 256 (1) */
#define I2CCON_IRENB	0x20	/* Interrupt Enable bit  */
#define I2CCON_IRPND    0x10    /* Interrupt pending bit */
#define I2CCON_TCDMSK	0x0F    /* I2C-bus transmit clock divider bit mask */

#ifdef CONFIG_ARCH_S5P6818
#define SDADLY_CLKSTEP	5	/* SDA delay: Reg. val. is multiple of 5 clks */
#define SDADLY_MAX	3	/* SDA delay: Max. reg. value is 3 */
#define I2CLC_FILTER	0x04	/* SDA filter on */
#else
#define STOPCON_CLR	0x01	/* Clock Line Release */
#define STOPCON_DLR	0x02	/* Data Line Release */
#define STOPCON_NAG	0x04	/* not-ackn. generation and data shift cont. */
#endif

#define I2C_TIMEOUT_MS	10      /* 10 ms */

#define I2C_M_NOSTOP	0x100

#define MAX_I2C_NUM 3

#define DEFAULT_SPEED   100000  /* default I2C speed [Hz] */

DECLARE_GLOBAL_DATA_PTR;

struct nx_i2c_regs {
	uint     iiccon;
	uint     iicstat;
	uint     iicadd;
	uint     iicds;
#ifdef CONFIG_ARCH_S5P6818
	/* S5P6818: Offset 0x10 is Line Control Register (SDA-delay, Filter) */
	uint     iiclc;
#else
	/* S5P4418: Offset 0x10 is Stop Control Register */
	uint     iicstopcon;
#endif
};

struct nx_i2c_bus {
	uint bus_num;
	struct nx_i2c_regs *regs;
	uint speed;
	uint target_speed;
#ifdef CONFIG_ARCH_S5P6818
	uint sda_delay;
#else
	/* setup time for Stop condition [us] */
	uint tsu_stop;
#endif
};

/* s5pxx18 i2c must be reset before enabled */
static void i2c_reset(int ch)
{
	int rst_id = RESET_ID_I2C0 + ch;

	nx_rstcon_setrst(rst_id, 0);
	nx_rstcon_setrst(rst_id, 1);
}

static uint i2c_get_clkrate(struct nx_i2c_bus *bus)
{
	struct clk *clk;
	int index = bus->bus_num;
	char name[50] = {0, };

	sprintf(name, "%s.%d", DEV_NAME_I2C, index);
	clk = clk_get((const char *)name);
	if (!clk)
		return -1;

	return clk_get_rate(clk);
}

static uint i2c_set_clk(struct nx_i2c_bus *bus, uint enb)
{
	struct clk *clk;
	char name[50];

	sprintf(name, "%s.%d", DEV_NAME_I2C, bus->bus_num);
	clk = clk_get((const char *)name);
	if (!clk) {
		debug("%s(): clk_get(%s) error!\n",
		      __func__, (const char *)name);
		return -EINVAL;
	}

	clk_disable(clk);
	if (enb)
		clk_enable(clk);

	return 0;
}

#ifdef CONFIG_ARCH_S5P6818
/* Set SDA line delay, not available at S5P4418 */
static int nx_i2c_set_sda_delay(struct nx_i2c_bus *bus)
{
	struct nx_i2c_regs *i2c = bus->regs;
	uint pclk = 0;
	uint t_pclk = 0;
	uint delay = 0;

	/* get input clock of the I2C-controller */
	pclk = i2c_get_clkrate(bus);

	if (bus->sda_delay) {
		/* t_pclk = period time of one pclk [ns] */
		t_pclk = DIV_ROUND_UP(1000, pclk / 1000000);
		/* delay = number of pclks required for sda_delay [ns] */
		delay = DIV_ROUND_UP(bus->sda_delay, t_pclk);
		/* delay = register value (step of 5 clocks) */
		delay = DIV_ROUND_UP(delay, SDADLY_CLKSTEP);
		/* max. possible register value = 3 */
		if (delay > SDADLY_MAX) {
			delay = SDADLY_MAX;
			debug("%s(): sda-delay des.: %dns, sat. to max.: %dns (granularity: %dns)\n",
			      __func__, bus->sda_delay, t_pclk * delay * SDADLY_CLKSTEP,
			      t_pclk * SDADLY_CLKSTEP);
		} else {
			debug("%s(): sda-delay des.: %dns, act.: %dns (granularity: %dns)\n",
			      __func__, bus->sda_delay, t_pclk * delay * SDADLY_CLKSTEP,
			      t_pclk * SDADLY_CLKSTEP);
		}

		delay |= I2CLC_FILTER;
	} else {
		delay = 0;
		debug("%s(): sda-delay = 0\n", __func__);
	}

	delay &= 0x7;
	writel(delay, &i2c->iiclc);

	return 0;
}
#endif

static int nx_i2c_set_bus_speed(struct udevice *dev, uint speed)
{
	struct nx_i2c_bus *bus = dev_get_priv(dev);
	struct nx_i2c_regs *i2c = bus->regs;
	unsigned long pclk, pres = 16, div;

	if (i2c_set_clk(bus, 1))
		return -EINVAL;

	/* get input clock of the I2C-controller */
	pclk = i2c_get_clkrate(bus);

	/* calculate prescaler and divisor values */
	if ((pclk / pres / (16 + 1)) > speed)
		/* prescaler value 16 is too less --> set to 256 */
		pres = 256;

	div = 0;
	/* actual divider = div + 1 */
	while ((pclk / pres / (div + 1)) > speed)
		div++;

	if (div > 0xF) {
		debug("%s(): pres==%ld, div==0x%lx is saturated to 0xF !)\n",
		      __func__, pres, div);
		div = 0xF;
	} else {
		debug("%s(): pres==%ld, div==0x%lx)\n", __func__, pres, div);
	}

	/* set Tx-clock divisor and prescaler values */
	writel((div & I2CCON_TCDMSK) | ((pres == 256) ? I2CCON_TCP256 : 0),
	       &i2c->iiccon);

	/* init to SLAVE REVEIVE and set slaveaddr */
	writel(0, &i2c->iicstat);
	writel(0x00, &i2c->iicadd);

	/* program Master Transmit (and implicit STOP) */
	writel(I2CSTAT_MTM | I2CSTAT_RXTXEN, &i2c->iicstat);

	/* calculate actual I2C speed [Hz] */
	bus->speed = pclk / ((div + 1) * pres);
	debug("%s(): speed des.: %dHz, act.: %dHz\n",
	      __func__, speed, bus->speed);

#ifdef CONFIG_ARCH_S5P6818
	nx_i2c_set_sda_delay(bus);
#else
	/* setup time for Stop condition [us], min. 4us @ 100kHz I2C-clock */
	bus->tsu_stop = DIV_ROUND_UP(400, bus->speed / 1000);
#endif

	if (i2c_set_clk(bus, 0))
		return -EINVAL;
	return 0;
}

static void i2c_process_node(struct udevice *dev)
{
	struct nx_i2c_bus *bus = dev_get_priv(dev);

	bus->target_speed = dev_read_s32_default(dev, "clock-frequency",
						 DEFAULT_SPEED);
#ifdef CONFIG_ARCH_S5P6818
	bus->sda_delay = dev_read_s32_default(dev, "i2c-sda-delay-ns", 0);
#endif
}

static int nx_i2c_probe(struct udevice *dev)
{
	struct nx_i2c_bus *bus = dev_get_priv(dev);
	fdt_addr_t addr;

	/* get regs = i2c base address */
	addr = devfdt_get_addr(dev);
	if (addr == FDT_ADDR_T_NONE)
		return -EINVAL;
	bus->regs = (struct nx_i2c_regs *)addr;

	bus->bus_num = dev->seq;

	/* i2c node parsing */
	i2c_process_node(dev);
	if (!bus->target_speed)
		return -ENODEV;

	/* reset */
	i2c_reset(bus->bus_num);

	return 0;
}

/* i2c bus busy check */
static int i2c_is_busy(struct nx_i2c_regs *i2c)
{
	ulong start_time;

	start_time = get_timer(0);
	while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
		if (get_timer(start_time) > I2C_TIMEOUT_MS) {
			debug("Timeout\n");
			return -EBUSY;
		}
	}
	return 0;
}

/* irq enable/disable functions */
static void i2c_enable_irq(struct nx_i2c_regs *i2c)
{
	unsigned int reg;

	reg = readl(&i2c->iiccon);
	reg |= I2CCON_IRENB;
	writel(reg, &i2c->iiccon);
}

/* irq clear function */
static void i2c_clear_irq(struct nx_i2c_regs *i2c)
{
	unsigned int reg;

	reg = readl(&i2c->iiccon);
	/* reset interrupt pending flag */
	reg &= ~(I2CCON_IRPND);
	/*
	 * Interrupt must also be cleared!
	 * Otherwise linux boot may hang after:
	 *     [    0.436000] NetLabel:  unlabeled traffic allowed by default
	 * Next would be:
	 *     [    0.442000] clocksource: Switched to clocksource source timer
	 */
	reg |= I2CCON_IRCLR;
	writel(reg, &i2c->iiccon);
}

/* ack enable functions */
static void i2c_enable_ack(struct nx_i2c_regs *i2c)
{
	unsigned int reg;

	reg = readl(&i2c->iiccon);
	reg |= I2CCON_ACKGEN;
	writel(reg, &i2c->iiccon);
}

static void i2c_send_stop(struct nx_i2c_bus *bus)
{
	struct nx_i2c_regs *i2c = bus->regs;

	if (IS_ENABLED(CONFIG_ARCH_S5P6818)) {
		unsigned int reg;

		reg = readl(&i2c->iicstat);
		reg |= I2CSTAT_MRM | I2CSTAT_RXTXEN;
		reg &= (~I2CSTAT_SS);

		writel(reg, &i2c->iicstat);
		i2c_clear_irq(i2c);
	} else {  /* S5P4418 */
		writel(STOPCON_NAG, &i2c->iicstopcon);

		i2c_clear_irq(i2c);

		/*
		 * Clock Line Release --> SDC changes from Low to High and
		 * SDA from High to Low
		 */
		writel(STOPCON_CLR, &i2c->iicstopcon);

		/* Hold SDA Low (Setup Time for Stop condition) */
		udelay(bus->tsu_stop);

		i2c_clear_irq(i2c);

		/* Master Receive Mode Stop --> SDA becomes High */
		writel(I2CSTAT_MRM, &i2c->iicstat);
	}
}

static int wait_for_xfer(struct nx_i2c_regs *i2c)
{
	unsigned long start_time = get_timer(0);

	do {
		if (readl(&i2c->iiccon) & I2CCON_IRPND)
			/* return -EREMOTEIO if not Acknowledged, otherwise 0 */
			return (readl(&i2c->iicstat) & I2CSTAT_NACK) ?
				-EREMOTEIO : 0;
	} while (get_timer(start_time) < I2C_TIMEOUT_MS);

	return -ETIMEDOUT;
}

static int i2c_transfer(struct nx_i2c_regs *i2c,
			uchar cmd_type,
			uchar chip_addr,
			uchar addr[],
			uchar addr_len,
			uchar data[],
			unsigned short data_len,
			uint seq)
{
	uint status;
	int i = 0, result;

	/* Note: data_len = 0 is supported for "probe_chip" */

	i2c_enable_irq(i2c);
	i2c_enable_ack(i2c);

	/* Get the slave chip address going */
	/* Enable Rx/Tx */
	writel(I2CSTAT_RXTXEN, &i2c->iicstat);

	writel(chip_addr, &i2c->iicds);
	status = I2CSTAT_RXTXEN | I2CSTAT_SS;
	if (cmd_type == I2C_WRITE || (addr && addr_len))
		status |= I2CSTAT_MTM;
	else
		status |= I2CSTAT_MRM;

	writel(status, &i2c->iicstat);
	if (seq)
		i2c_clear_irq(i2c);

	/* Wait for chip address to transmit. */
	result = wait_for_xfer(i2c);
	if (result) {
		debug("%s: transmitting chip address failed\n", __func__);
		goto bailout;
	}

	/* If register address needs to be transmitted - do it now. */
	if (addr && addr_len) {  /* register addr */
		while ((i < addr_len) && !result) {
			writel(addr[i++], &i2c->iicds);
			i2c_clear_irq(i2c);
			result = wait_for_xfer(i2c);
		}

		i = 0;
		if (result) {
			debug("%s: transmitting register address failed\n",
			      __func__);
			goto bailout;
		}
	}

	switch (cmd_type) {
	case I2C_WRITE:
		while ((i < data_len) && !result) {
			writel(data[i++], &i2c->iicds);
			i2c_clear_irq(i2c);
			result = wait_for_xfer(i2c);
		}
		break;
	case I2C_READ:
		if (addr && addr_len) {
			/*
			 * Register address has been sent, now send slave chip
			 * address again to start the actual read transaction.
			 */
			writel(chip_addr, &i2c->iicds);

			/* Generate a re-START. */
			writel(I2CSTAT_MRM | I2CSTAT_RXTXEN |
			       I2CSTAT_SS, &i2c->iicstat);
			i2c_clear_irq(i2c);
			result = wait_for_xfer(i2c);
			if (result) {
				debug("%s: I2C_READ: sending chip addr. failed\n",
				      __func__);
				goto bailout;
			}
		}

		while ((i < data_len) && !result) {
			/* disable ACK for final READ */
			if (i == data_len - 1)
				clrbits_le32(&i2c->iiccon, I2CCON_ACKGEN);

			i2c_clear_irq(i2c);
			result = wait_for_xfer(i2c);
			data[i++] = readb(&i2c->iicds);
		}

		if (result == -EREMOTEIO)
			 /* Not Acknowledged --> normal terminated read. */
			result = 0;
		else if (result == -ETIMEDOUT)
			debug("%s: I2C_READ: time out\n", __func__);
		else
			debug("%s: I2C_READ: read not terminated with NACK\n",
			      __func__);
		break;

	default:
		debug("%s: bad call\n", __func__);
		result = -EINVAL;
		break;
	}

bailout:
	return result;
}

static int nx_i2c_read(struct udevice *dev, uchar chip_addr, uint addr,
		       uint alen, uchar *buffer, uint len, uint seq)
{
	struct nx_i2c_bus *i2c;
	uchar xaddr[4];
	int ret;

	i2c = dev_get_priv(dev);
	if (!i2c)
		return -EFAULT;

	if (alen > 4) {
		debug("I2C read: addr len %d not supported\n", alen);
		return -EADDRNOTAVAIL;
	}

	if (alen > 0)
		xaddr[0] = (addr >> 24) & 0xFF;

	if (alen > 0) {
		xaddr[0] = (addr >> 24) & 0xFF;
		xaddr[1] = (addr >> 16) & 0xFF;
		xaddr[2] = (addr >> 8) & 0xFF;
		xaddr[3] = addr & 0xFF;
	}

	ret = i2c_transfer(i2c->regs, I2C_READ, chip_addr << 1,
			   &xaddr[4 - alen], alen, buffer, len, seq);

	if (ret) {
		debug("I2C read failed %d\n", ret);
		return -EIO;
	}

	return 0;
}

static int nx_i2c_write(struct udevice *dev, uchar chip_addr, uint addr,
			uint alen, uchar *buffer, uint len, uint seq)
{
	struct nx_i2c_bus *i2c;
	uchar xaddr[4];
	int ret;

	i2c = dev_get_priv(dev);
	if (!i2c)
		return -EFAULT;

	if (alen > 4) {
		debug("I2C write: addr len %d not supported\n", alen);
		return -EINVAL;
	}

	if (alen > 0) {
		xaddr[0] = (addr >> 24) & 0xFF;
		xaddr[1] = (addr >> 16) & 0xFF;
		xaddr[2] = (addr >> 8) & 0xFF;
		xaddr[3] = addr & 0xFF;
	}

	ret = i2c_transfer(i2c->regs, I2C_WRITE, chip_addr << 1,
			   &xaddr[4 - alen], alen, buffer, len, seq);
	if (ret) {
		debug("I2C write failed %d\n", ret);
		return -EIO;
	}

	return 0;
}

static int nx_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
{
	struct nx_i2c_bus *bus = dev_get_priv(dev);
	struct nx_i2c_regs *i2c = bus->regs;
	int ret;
	int i;

	/* The power loss by the clock, only during on/off. */
	ret = i2c_set_clk(bus, 1);

	if (!ret)
		/* Bus State(Busy) check  */
		ret = i2c_is_busy(i2c);
	if (!ret) {
		for (i = 0; i < nmsgs; msg++, i++) {
			if (msg->flags & I2C_M_RD) {
				ret = nx_i2c_read(dev, msg->addr, 0, 0,
						  msg->buf, msg->len, i);
			} else {
				ret = nx_i2c_write(dev, msg->addr, 0, 0,
						   msg->buf, msg->len, i);
			}

			if (ret) {
				debug("i2c_xfer: error sending\n");
				ret = -EREMOTEIO;
			}
		}

		i2c_send_stop(bus);
		if (i2c_set_clk(bus, 0))
			ret = -EINVAL;
	}

	return ret;
};

static int nx_i2c_probe_chip(struct udevice *dev, u32 chip_addr,
			     u32 chip_flags)
{
	int ret;
	struct nx_i2c_bus *bus = dev_get_priv(dev);

	ret = i2c_set_clk(bus, 1);

	if (!ret) {
		/*
		 * Send Chip Address only
		 * --> I2C transfer with data length and address length = 0.
		 * If there is a Slave, i2c_transfer() returns 0 (acknowledge
		 * transfer).
		 * I2C_WRITE must be used in order Master Transmit Mode is
		 * selected. Otherwise (in Master Receive Mode, I2C_READ)
		 * sending the stop condition below is not working (SDA does
		 * not transit to High).
		 */
		ret = i2c_transfer(bus->regs, I2C_WRITE, (uchar)chip_addr << 1,
				   NULL, 0, NULL, 0, 0);

		i2c_send_stop(bus);
		if (i2c_set_clk(bus, 0))
			ret = -EINVAL;
	}

	return ret;
}

static const struct dm_i2c_ops nx_i2c_ops = {
	.xfer		= nx_i2c_xfer,
	.probe_chip	= nx_i2c_probe_chip,
	.set_bus_speed	= nx_i2c_set_bus_speed,
};

static const struct udevice_id nx_i2c_ids[] = {
	{ .compatible = "nexell,s5pxx18-i2c" },
	{ }
};

U_BOOT_DRIVER(i2c_nexell) = {
	.name		= "i2c_nexell",
	.id		= UCLASS_I2C,
	.of_match	= nx_i2c_ids,
	.probe		= nx_i2c_probe,
	.priv_auto_alloc_size	= sizeof(struct nx_i2c_bus),
	.ops		= &nx_i2c_ops,
};