summaryrefslogtreecommitdiff
path: root/src/cpu/samsung/exynos5420/i2c.c
blob: 226862802afcc4d0a650619ad6b4d699ccdc9d11 (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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/*
 * This file is part of the coreboot project.
 *
 * (C) Copyright 2002
 * David Mueller, ELSOFT AG, d.mueller@elsoft.ch
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <console/console.h>
#include <delay.h>
#include <timer.h>
#include <arch/io.h>
#include <device/i2c.h>
#include "clk.h"
#include "i2c.h"
#include "pinmux.h"

#define	I2C_WRITE	0
#define I2C_READ	1

#define I2C_OK		0
#define I2C_NOK		1
#define I2C_NACK	2
#define I2C_NOK_LA	3	/* Lost arbitration */
#define I2C_NOK_TOUT	4	/* time out */

/* HSI2C specific register description */

/* I2C_CTL Register bits */
/* FIXME(dhendrix): do we really need to cast these as unsigned? */
#define HSI2C_FUNC_MODE_I2C		(1u << 0)
#define HSI2C_MASTER			(1u << 3)
#define HSI2C_RXCHON			(1u << 6)	/* Write/Send */
#define HSI2C_TXCHON			(1u << 7)	/* Read/Receive */
#define HSI2C_SW_RST			(1u << 31)

/* I2C_FIFO_STAT Register bits */
#define HSI2C_TX_FIFO_LEVEL		(0x7f << 0)
#define HSI2C_TX_FIFO_FULL		(1u << 7)
#define HSI2C_TX_FIFO_EMPTY		(1u << 8)
#define HSI2C_RX_FIFO_LEVEL		(0x7f << 16)
#define HSI2C_RX_FIFO_FULL		(1u << 23)
#define HSI2C_RX_FIFO_EMPTY		(1u << 24)

/* I2C_FIFO_CTL Register bits */
#define HSI2C_RXFIFO_EN			(1u << 0)
#define HSI2C_TXFIFO_EN			(1u << 1)
#define HSI2C_TXFIFO_TRIGGER_LEVEL	(0x20 << 16)
#define HSI2C_RXFIFO_TRIGGER_LEVEL	(0x20 << 4)

/* I2C_TRAILING_CTL Register bits */
#define HSI2C_TRAILING_COUNT		(0xff)

/* I2C_INT_EN Register bits */
#define HSI2C_INT_TX_ALMOSTEMPTY_EN	(1u << 0)
#define HSI2C_INT_RX_ALMOSTFULL_EN	(1u << 1)
#define HSI2C_INT_TRAILING_EN		(1u << 6)
#define HSI2C_INT_I2C_EN		(1u << 9)

/* I2C_CONF Register bits */
#define HSI2C_AUTO_MODE			(1u << 31)
#define HSI2C_10BIT_ADDR_MODE		(1u << 30)
#define HSI2C_HS_MODE			(1u << 29)

/* I2C_AUTO_CONF Register bits */
#define HSI2C_READ_WRITE		(1u << 16)
#define HSI2C_STOP_AFTER_TRANS		(1u << 17)
#define HSI2C_MASTER_RUN		(1u << 31)

/* I2C_TIMEOUT Register bits */
#define HSI2C_TIMEOUT_EN		(1u << 31)

/* I2C_TRANS_STATUS register bits */
#define HSI2C_MASTER_BUSY		(1u << 17)
#define HSI2C_SLAVE_BUSY		(1u << 16)
#define HSI2C_TIMEOUT_AUTO		(1u << 4)
#define HSI2C_NO_DEV			(1u << 3)
#define HSI2C_NO_DEV_ACK		(1u << 2)
#define HSI2C_TRANS_ABORT		(1u << 1)
#define HSI2C_TRANS_DONE		(1u << 0)

#define HSI2C_SLV_ADDR_MAS(x)		((x & 0x3ff) << 10)

/* S3C I2C Controller bits */
#define I2CSTAT_BSY	0x20	/* Busy bit */
#define I2CSTAT_NACK	0x01	/* Nack bit */
#define I2CCON_ACKGEN	0x80	/* Acknowledge generation */
#define I2CCON_IRPND	0x10	/* Interrupt pending bit */
#define I2C_MODE_MT	0xC0	/* Master Transmit Mode */
#define I2C_MODE_MR	0x80	/* Master Receive Mode */
#define I2C_START_STOP	0x20	/* START / STOP */
#define I2C_TXRX_ENA	0x10	/* I2C Tx/Rx enable */

#define I2C_TIMEOUT_MS 1000		/* 1 second */

#define	HSI2C_TIMEOUT	100

/* The timeouts we live by */
enum {
	I2C_XFER_TIMEOUT_MS	= 35,	/* xfer to complete */
	I2C_INIT_TIMEOUT_MS	= 1000,	/* bus free on init */
	I2C_IDLE_TIMEOUT_MS	= 100,	/* waiting for bus idle */
	I2C_STOP_TIMEOUT_US	= 200,	/* waiting for stop events */
};

static struct s3c24x0_i2c_bus i2c_buses[] = {
	{
		.bus_num = 0,
		.regs = (struct s3c24x0_i2c *)0x12c60000,
		.periph_id = PERIPH_ID_I2C0,
	},
	{
		.bus_num = 1,
		.regs = (struct s3c24x0_i2c *)0x12c70000,
		.periph_id = PERIPH_ID_I2C1,
	},
	{
		.bus_num = 2,
		.regs = (struct s3c24x0_i2c *)0x12c80000,
		.periph_id = PERIPH_ID_I2C2,
	},
	{
		.bus_num = 3,
		.regs = (struct s3c24x0_i2c *)0x12c90000,
		.periph_id = PERIPH_ID_I2C3,
	},
	/* I2C4-I2C10 are part of the USI block */
	{
		.bus_num = 4,
		.hsregs = (struct exynos5_hsi2c *)0x12ca0000,
		.periph_id = PERIPH_ID_I2C4,
		.is_highspeed = 1,
	},
	{
		.bus_num = 5,
		.hsregs = (struct exynos5_hsi2c *)0x12cb0000,
		.periph_id = PERIPH_ID_I2C5,
		.is_highspeed = 1,
	},
	{
		.bus_num = 6,
		.hsregs = (struct exynos5_hsi2c *)0x12cc0000,
		.periph_id = PERIPH_ID_I2C6,
		.is_highspeed = 1,
	},
	{
		.bus_num = 7,
		.hsregs = (struct exynos5_hsi2c *)0x12cd0000,
		.periph_id = PERIPH_ID_I2C7,
		.is_highspeed = 1,
	},
	{
		.bus_num = 8,
		.hsregs = (struct exynos5_hsi2c *)0x12e00000,
		.periph_id = PERIPH_ID_I2C8,
		.is_highspeed = 1,
	},
	{
		.bus_num = 9,
		.hsregs = (struct exynos5_hsi2c *)0x12e10000,
		.periph_id = PERIPH_ID_I2C9,
		.is_highspeed = 1,
	},
	{
		.bus_num = 10,
		.hsregs = (struct exynos5_hsi2c *)0x12e20000,
		.periph_id = PERIPH_ID_I2C10,
		.is_highspeed = 1,
	},
};

/*
 * Wait til the byte transfer is completed.
 *
 * @param i2c- pointer to the appropriate i2c register bank.
 * @return I2C_OK, if transmission was ACKED
 *         I2C_NACK, if transmission was NACKED
 *         I2C_NOK_TIMEOUT, if transaction did not complete in I2C_TIMEOUT_MS
 */

static int WaitForXfer(struct s3c24x0_i2c *i2c)
{
	struct mono_time current, end;

	timer_monotonic_get(&current);
	end = current;
	mono_time_add_usecs(&end, I2C_TIMEOUT_MS * 1000);
	do {
		if (read32(&i2c->iiccon) & I2CCON_IRPND)
			return (read32(&i2c->iicstat) & I2CSTAT_NACK) ?
				I2C_NACK : I2C_OK;
		timer_monotonic_get(&current);
	} while (mono_time_before(&current, &end));

	printk(BIOS_ERR, "%s timed out\n", __func__);
	return I2C_NOK_TOUT;
}

static void ReadWriteByte(struct s3c24x0_i2c *i2c)
{
	writel(read32(&i2c->iiccon) & ~I2CCON_IRPND, &i2c->iiccon);
}

static void i2c_ch_init(struct s3c24x0_i2c_bus *bus, int speed, int slaveadd)
{
	unsigned long freq, pres = 16, div;
	unsigned long val;

	freq = clock_get_periph_rate(bus->periph_id);
	/* calculate prescaler and divisor values */
	if ((freq / pres / (16 + 1)) > speed)
		/* set prescaler to 512 */
		pres = 512;

	div = 0;
	while ((freq / pres / (div + 1)) > speed)
		div++;

	/* set prescaler, divisor according to freq, also set ACKGEN, IRQ */
	val = (div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0);
	write32(val, &bus->regs->iiccon);

	/* init to SLAVE RECEIVE mode and clear I2CADDn */
	write32(0, &bus->regs->iicstat);
	write32(slaveadd, &bus->regs->iicadd);
	/* program Master Transmit (and implicit STOP) */
	write32(I2C_MODE_MT | I2C_TXRX_ENA, &bus->regs->iicstat);
}

static int hsi2c_get_clk_details(struct s3c24x0_i2c_bus *i2c_bus,
		unsigned int bus_freq_hz)
{
	struct exynos5_hsi2c *hsregs = i2c_bus->hsregs;
	unsigned long clkin = clock_get_periph_rate(i2c_bus->periph_id);
	unsigned int i = 0, utemp0 = 0, utemp1 = 0;
	unsigned int t_ftl_cycle;

	/* FPCLK / FI2C =
	 * (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2) + 8 + 2 * FLT_CYCLE
	 * uTemp0 = (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2)
	 * uTemp1 = (TSCLK_L + TSCLK_H + 2)
	 * uTemp2 = TSCLK_L + TSCLK_H
	 */
	t_ftl_cycle = (read32(&hsregs->usi_conf) >> 16) & 0x7;
	utemp0 = (clkin / bus_freq_hz) - 8 - 2 * t_ftl_cycle;

	/* CLK_DIV max is 256 */
	for (i = 0; i < 256; i++) {
		utemp1 = utemp0 / (i + 1);
		if ((utemp1 < 512) && (utemp1 > 4)) {
			i2c_bus->clk_cycle = utemp1 - 2;
			i2c_bus->clk_div = i;
			return 0;
		}
	}
	printk(BIOS_ERR, "%s: failed?\n", __func__);
	return -1;
}

static void hsi2c_ch_init(struct s3c24x0_i2c_bus *i2c_bus,
				unsigned int bus_freq_hz)
{
	struct exynos5_hsi2c *hsregs = i2c_bus->hsregs;
	unsigned int t_sr_release;
	unsigned int n_clkdiv;
	unsigned int t_start_su, t_start_hd;
	unsigned int t_stop_su;
	unsigned int t_data_su, t_data_hd;
	unsigned int t_scl_l, t_scl_h;
	u32 i2c_timing_s1;
	u32 i2c_timing_s2;
	u32 i2c_timing_s3;
	u32 i2c_timing_sla;

	hsi2c_get_clk_details(i2c_bus, bus_freq_hz);

	n_clkdiv = i2c_bus->clk_div;
	t_scl_l = i2c_bus->clk_cycle / 2;
	t_scl_h = i2c_bus->clk_cycle / 2;
	t_start_su = t_scl_l;
	t_start_hd = t_scl_l;
	t_stop_su = t_scl_l;
	t_data_su = t_scl_l / 2;
	t_data_hd = t_scl_l / 2;
	t_sr_release = i2c_bus->clk_cycle;

	i2c_timing_s1 = t_start_su << 24 | t_start_hd << 16 | t_stop_su << 8;
	i2c_timing_s2 = t_data_su << 24 | t_scl_l << 8 | t_scl_h << 0;
	i2c_timing_s3 = n_clkdiv << 16 | t_sr_release << 0;
	i2c_timing_sla = t_data_hd << 0;

	write32(HSI2C_TRAILING_COUNT, &hsregs->usi_trailing_ctl);

	/* Clear to enable Timeout */
	clrsetbits_le32(&hsregs->usi_timeout, HSI2C_TIMEOUT_EN, 0);

	write32(read32(&hsregs->usi_conf) | HSI2C_AUTO_MODE, &hsregs->usi_conf);

	/* Currently operating in Fast speed mode. */
	write32(i2c_timing_s1, &hsregs->usi_timing_fs1);
	write32(i2c_timing_s2, &hsregs->usi_timing_fs2);
	write32(i2c_timing_s3, &hsregs->usi_timing_fs3);
	write32(i2c_timing_sla, &hsregs->usi_timing_sla);

	/* Enable TXFIFO and RXFIFO */
	write32(HSI2C_RXFIFO_EN | HSI2C_TXFIFO_EN, &hsregs->usi_fifo_ctl);

	/* i2c_conf configure */
	write32(readl(&hsregs->usi_conf) | HSI2C_AUTO_MODE, &hsregs->usi_conf);
}

/* SW reset for the high speed bus */
static void i2c_reset(struct s3c24x0_i2c_bus *i2c_bus)
{
	struct exynos5_hsi2c *i2c = i2c_bus->hsregs;
	u32 i2c_ctl;

	/* Set and clear the bit for reset */
	i2c_ctl = read32(&i2c->usi_ctl);
	i2c_ctl |= HSI2C_SW_RST;
	write32(i2c_ctl, &i2c->usi_ctl);

	i2c_ctl = read32(&i2c->usi_ctl);
	i2c_ctl &= ~HSI2C_SW_RST;
	write32(i2c_ctl, &i2c->usi_ctl);

	/* Initialize the configure registers */
	/* FIXME: This just assumes 100KHz as a default bus freq */
	hsi2c_ch_init(i2c_bus, 100000);
}

void i2c_init(unsigned bus_num, int speed, int slaveadd)
{
	struct s3c24x0_i2c_bus *i2c;

	i2c = &i2c_buses[bus_num];

	i2c_reset(i2c);

	if (i2c->is_highspeed)
		hsi2c_ch_init(i2c, speed);
	else
		i2c_ch_init(i2c, speed, slaveadd);
}

/*
 * Check whether the transfer is complete.
 * Return values:
 * 0  - transfer not done
 * 1  - transfer finished successfully
 * -1 - transfer failed
 */
static int hsi2c_check_transfer(struct exynos5_hsi2c *i2c)
{
	uint32_t status = read32(&i2c->usi_trans_status);
	if (status & (HSI2C_TRANS_ABORT | HSI2C_NO_DEV_ACK |
		      HSI2C_NO_DEV | HSI2C_TIMEOUT_AUTO)) {
		if (status & HSI2C_TRANS_ABORT)
			printk(BIOS_ERR,
			       "%s: Transaction aborted.\n", __func__);
		if (status & HSI2C_NO_DEV_ACK)
			printk(BIOS_ERR,
			       "%s: No ack from device.\n", __func__);
		if (status & HSI2C_NO_DEV)
			printk(BIOS_ERR,
			       "%s: No response from device.\n", __func__);
		if (status & HSI2C_TIMEOUT_AUTO)
			printk(BIOS_ERR,
			       "%s: Transaction time out.\n", __func__);
		return -1;
	}
	return !(status & HSI2C_MASTER_BUSY);
}

/*
 * Wait for the transfer to finish.
 * Return values:
 * 0  - transfer not done
 * 1  - transfer finished successfully
 * -1 - transfer failed
 */
static int hsi2c_wait_for_transfer(struct exynos5_hsi2c *i2c)
{
	struct mono_time current, end;

	timer_monotonic_get(&current);
	end = current;
	mono_time_add_usecs(&end, HSI2C_TIMEOUT * 1000);
	while (mono_time_before(&current, &end)) {
		int ret = hsi2c_check_transfer(i2c);
		if (ret)
			return ret;
		udelay(5);
		timer_monotonic_get(&current);
	}
	return 0;
}

static int hsi2c_senddata(struct exynos5_hsi2c *i2c, const uint8_t *data,
			  int len)
{
	while (!hsi2c_check_transfer(i2c) && len) {
		if (!(read32(&i2c->usi_fifo_stat) & HSI2C_TX_FIFO_FULL)) {
			write32(*data++, &i2c->usi_txdata);
			len--;
		}
	}
	return len ? -1 : 0;
}

static int hsi2c_recvdata(struct exynos5_hsi2c *i2c, uint8_t *data, int len)
{
	while (!hsi2c_check_transfer(i2c) && len) {
		if (!(read32(&i2c->usi_fifo_stat) & HSI2C_RX_FIFO_EMPTY)) {
			*data++ = read32(&i2c->usi_rxdata);
			len--;
		}
	}
	return len ? -1 : 0;
}

static int hsi2c_write(struct exynos5_hsi2c *i2c,
			unsigned char chip,
			unsigned char addr[],
			unsigned char alen,
			const uint8_t data[],
			unsigned short len)
{
	uint32_t i2c_auto_conf;

	if (hsi2c_wait_for_transfer(i2c) != 1)
		return -1;

	/* chip address */
	write32(HSI2C_SLV_ADDR_MAS(chip), &i2c->i2c_addr);

	/* usi_ctl enable i2c func, master write configure */
	write32((HSI2C_TXCHON | HSI2C_FUNC_MODE_I2C | HSI2C_MASTER),
							&i2c->usi_ctl);

	/* auto_conf for write length and stop configure */
	i2c_auto_conf = ((len + alen) | HSI2C_STOP_AFTER_TRANS);
	i2c_auto_conf &= ~HSI2C_READ_WRITE;
	/* Master run, start xfer */
	i2c_auto_conf |= HSI2C_MASTER_RUN;
	write32(i2c_auto_conf, &i2c->usi_auto_conf);

	if (hsi2c_senddata(i2c, addr, alen) ||
	    hsi2c_senddata(i2c, data, len) ||
	    hsi2c_wait_for_transfer(i2c) != 1) {
		return -1;
	}

	write32(HSI2C_FUNC_MODE_I2C, &i2c->usi_ctl);
	return 0;
}

static int hsi2c_read(struct exynos5_hsi2c *i2c,
			unsigned char chip,
			unsigned char addr[],
			unsigned char alen,
			uint8_t data[],
			unsigned short len,
			int check)
{
	uint32_t i2c_auto_conf;

	/* start read */
	if (hsi2c_wait_for_transfer(i2c) != 1)
		return -1;

	/* chip address */
	write32(HSI2C_SLV_ADDR_MAS(chip), &i2c->i2c_addr);

	/* usi_ctl enable i2c func, master write configure */
	write32((HSI2C_TXCHON | HSI2C_FUNC_MODE_I2C | HSI2C_MASTER),
							&i2c->usi_ctl);

	/* auto_conf */
	write32(alen | HSI2C_MASTER_RUN | HSI2C_STOP_AFTER_TRANS,
		&i2c->usi_auto_conf);

	if (hsi2c_senddata(i2c, addr, alen) ||
	    hsi2c_wait_for_transfer(i2c) != 1) {
		return -1;
	}

	/* usi_ctl enable i2c func, master WRITE configure */
	write32((HSI2C_RXCHON | HSI2C_FUNC_MODE_I2C | HSI2C_MASTER),
							&i2c->usi_ctl);

	/* auto_conf, length and stop configure */
	i2c_auto_conf = (len | HSI2C_STOP_AFTER_TRANS | HSI2C_READ_WRITE);
	i2c_auto_conf |= HSI2C_MASTER_RUN;
	/* Master run, start xfer */
	write32(i2c_auto_conf, &i2c->usi_auto_conf);

	if (hsi2c_recvdata(i2c, data, len) ||
	    hsi2c_wait_for_transfer(i2c) != 1) {
		return -1;
	}

	write32(HSI2C_FUNC_MODE_I2C, &i2c->usi_ctl);
	return 0;
}

/*
 * cmd_type is 0 for write, 1 for read.
 *
 * addr_len can take any value from 0-255, it is only limited
 * by the char, we could make it larger if needed. If it is
 * 0 we skip the address write cycle.
 */
static int i2c_transfer(struct s3c24x0_i2c *i2c,
			unsigned char cmd_type,
			unsigned char chip,
			unsigned char addr[],
			unsigned char addr_len,
			unsigned char data[],
			unsigned short data_len)
{
	int i = 0, result;
	struct mono_time current, end;

	if (data == 0 || data_len == 0) {
		printk(BIOS_ERR, "i2c_transfer: bad call\n");
		return I2C_NOK;
	}

	timer_monotonic_get(&current);
	end = current;
	mono_time_add_usecs(&end, I2C_TIMEOUT_MS * 1000);
	while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
		if (!mono_time_before(&current, &end)){
			printk(BIOS_ERR, "%s timed out\n", __func__);
			return I2C_NOK_TOUT;
		}
		timer_monotonic_get(&current);
	}

	write32(read32(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon);

	/* Get the slave chip address going */
	write32(chip, &i2c->iicds);
	if ((cmd_type == I2C_WRITE) || (addr && addr_len))
		write32(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
		       &i2c->iicstat);
	else
		write32(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
		       &i2c->iicstat);

	/* Wait for chip address to transmit. */
	result = WaitForXfer(i2c);
	if (result != I2C_OK)
		goto bailout;

	/* If register address needs to be transmitted - do it now. */
	if (addr && addr_len) {
		while ((i < addr_len) && (result == I2C_OK)) {
			write32(addr[i++], &i2c->iicds);
			ReadWriteByte(i2c);
			result = WaitForXfer(i2c);
		}
		i = 0;
		if (result != I2C_OK)
			goto bailout;
	}

	switch (cmd_type) {
	case I2C_WRITE:
		while ((i < data_len) && (result == I2C_OK)) {
			write32(data[i++], &i2c->iicds);
			ReadWriteByte(i2c);
			result = WaitForXfer(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.
			 */
			write32(chip, &i2c->iicds);

			/* Generate a re-START. */
			write32(I2C_MODE_MR | I2C_TXRX_ENA | I2C_START_STOP,
			       &i2c->iicstat);
			ReadWriteByte(i2c);
			result = WaitForXfer(i2c);
			if (result != I2C_OK)
				goto bailout;
		}

		while ((i < data_len) && (result == I2C_OK)) {
			/* disable ACK for final READ */
			if (i == data_len - 1)
				write32(readl(&i2c->iiccon)
				       & ~I2CCON_ACKGEN,
				       &i2c->iiccon);
			ReadWriteByte(i2c);
			result = WaitForXfer(i2c);
			data[i++] = read32(&i2c->iicds);
		}
		if (result == I2C_NACK)
			result = I2C_OK; /* Normal terminated read. */
		break;

	default:
		printk(BIOS_ERR, "i2c_transfer: bad call\n");
		result = I2C_NOK;
		break;
	}

bailout:
	/* Send STOP. */
	write32(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
	ReadWriteByte(i2c);

	return result;
}

int i2c_read(unsigned bus, unsigned chip, unsigned addr,
		unsigned alen, uint8_t *buf, unsigned len)
{
	struct s3c24x0_i2c_bus *i2c;
	unsigned char xaddr[4];
	int ret;

	if (alen > 4) {
		printk(BIOS_ERR, "I2C read: addr len %d not supported\n", alen);
		return 1;
	}

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

	i2c = &i2c_buses[bus];
	if (i2c->is_highspeed)
		ret = hsi2c_read(i2c->hsregs, chip, &xaddr[4 - alen],
						alen, buf, len, 0);
	else
		ret = i2c_transfer(i2c->regs, I2C_READ, chip << 1,
				&xaddr[4 - alen], alen, buf, len);
	if (ret) {
		i2c_reset(i2c);
		printk(BIOS_ERR, "I2C read (bus %02x, chip addr %02x) failed: "
				"%d\n", bus, chip, ret);
		return 1;
	}
	return 0;
}

int i2c_write(unsigned bus, unsigned chip, unsigned addr,
		unsigned alen, const uint8_t *buf, unsigned len)
{
	struct s3c24x0_i2c_bus *i2c;
	unsigned char xaddr[4];
	int ret;

	if (alen > 4) {
		printk(BIOS_ERR, "I2C write: addr len %d not supported\n",
				alen);
		return 1;
	}

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

	i2c = &i2c_buses[bus];
	if (i2c->is_highspeed)
		ret = hsi2c_write(i2c->hsregs, chip, &xaddr[4 - alen],
					alen, buf, len);
	else
		ret = i2c_transfer(i2c->regs, I2C_WRITE, chip << 1,
				&xaddr[4 - alen], alen, (void *)buf, len);


	if (ret != 0) {
		i2c_reset(i2c);
		printk(BIOS_ERR, "I2C write (bus %02x, chip addr %02x) failed: "
				"%d\n", bus, chip, ret);
		return 1;
	}
	return 0;
}