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
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2021 Nuvoton Technology Corp.
*/
#include <clk.h>
#include <dm.h>
#include <i2c.h>
#include <asm/io.h>
#include <linux/iopoll.h>
#include <asm/arch/gcr.h>
#define I2C_FREQ_100K 100000
#define NPCM_I2C_TIMEOUT_MS 10
#define NPCM7XX_I2CSEGCTL_INIT_VAL 0x0333F000
#define NPCM8XX_I2CSEGCTL_INIT_VAL 0x9333F000
/* SCLFRQ min/max field values */
#define SCLFRQ_MIN 10
#define SCLFRQ_MAX 511
/* SMBCTL1 */
#define SMBCTL1_START BIT(0)
#define SMBCTL1_STOP BIT(1)
#define SMBCTL1_INTEN BIT(2)
#define SMBCTL1_ACK BIT(4)
#define SMBCTL1_STASTRE BIT(7)
/* SMBCTL2 */
#define SMBCTL2_ENABLE BIT(0)
/* SMBCTL3 */
#define SMBCTL3_SCL_LVL BIT(7)
#define SMBCTL3_SDA_LVL BIT(6)
/* SMBCST */
#define SMBCST_BB BIT(1)
#define SMBCST_TGSCL BIT(5)
/* SMBST */
#define SMBST_XMIT BIT(0)
#define SMBST_MASTER BIT(1)
#define SMBST_STASTR BIT(3)
#define SMBST_NEGACK BIT(4)
#define SMBST_BER BIT(5)
#define SMBST_SDAST BIT(6)
/* SMBCST3 in bank0 */
#define SMBCST3_EO_BUSY BIT(7)
/* SMBFIF_CTS in bank1 */
#define SMBFIF_CTS_CLR_FIFO BIT(6)
#define SMBFIF_CTL_FIFO_EN BIT(4)
#define SMBCTL3_BNK_SEL BIT(5)
enum {
I2C_ERR_NACK = 1,
I2C_ERR_BER,
I2C_ERR_TIMEOUT,
};
struct smb_bank0_regs {
u8 addr3;
u8 addr7;
u8 addr4;
u8 addr8;
u16 addr5;
u16 addr6;
u8 cst2;
u8 cst3;
u8 ctl4;
u8 ctl5;
u8 scllt;
u8 fif_ctl;
u8 sclht;
};
struct smb_bank1_regs {
u8 fif_cts;
u8 fair_per;
u16 txf_ctl;
u32 t_out;
u8 cst2;
u8 cst3;
u16 txf_sts;
u16 rxf_sts;
u8 rxf_ctl;
};
struct npcm_i2c_regs {
u16 sda;
u16 st;
u16 cst;
u16 ctl1;
u16 addr;
u16 ctl2;
u16 addr2;
u16 ctl3;
union {
struct smb_bank0_regs bank0;
struct smb_bank1_regs bank1;
};
};
struct npcm_i2c_bus {
struct npcm_i2c_regs *reg;
int num;
u32 apb_clk;
u32 freq;
bool started;
};
static void npcm_dump_regs(struct npcm_i2c_bus *bus)
{
struct npcm_i2c_regs *reg = bus->reg;
printf("\n");
printf("SMBST=0x%x\n", readb(®->st));
printf("SMBCST=0x%x\n", readb(®->cst));
printf("SMBCTL1=0x%x\n", readb(®->ctl1));
printf("\n");
}
static int npcm_i2c_check_sda(struct npcm_i2c_bus *bus)
{
struct npcm_i2c_regs *reg = bus->reg;
ulong start_time;
int err = I2C_ERR_TIMEOUT;
u8 val;
start_time = get_timer(0);
/* wait SDAST to be 1 */
while (get_timer(start_time) < NPCM_I2C_TIMEOUT_MS) {
val = readb(®->st);
if (val & SMBST_NEGACK) {
err = I2C_ERR_NACK;
break;
}
if (val & SMBST_BER) {
err = I2C_ERR_BER;
break;
}
if (val & SMBST_SDAST) {
err = 0;
break;
}
}
if (err)
printf("%s: err %d\n", __func__, err);
return err;
}
static int npcm_i2c_send_start(struct npcm_i2c_bus *bus)
{
struct npcm_i2c_regs *reg = bus->reg;
ulong start_time;
int err = I2C_ERR_TIMEOUT;
/* Generate START condition */
setbits_8(®->ctl1, SMBCTL1_START);
start_time = get_timer(0);
while (get_timer(start_time) < NPCM_I2C_TIMEOUT_MS) {
if (readb(®->st) & SMBST_BER)
return I2C_ERR_BER;
if (readb(®->st) & SMBST_MASTER) {
err = 0;
break;
}
}
bus->started = true;
return err;
}
static int npcm_i2c_send_stop(struct npcm_i2c_bus *bus, bool wait)
{
struct npcm_i2c_regs *reg = bus->reg;
ulong start_time;
int err = I2C_ERR_TIMEOUT;
setbits_8(®->ctl1, SMBCTL1_STOP);
/* Clear NEGACK, STASTR and BER bits */
writeb(SMBST_STASTR | SMBST_NEGACK | SMBST_BER, ®->st);
bus->started = false;
if (!wait)
return 0;
start_time = get_timer(0);
while (get_timer(start_time) < NPCM_I2C_TIMEOUT_MS) {
if ((readb(®->ctl1) & SMBCTL1_STOP) == 0) {
err = 0;
break;
}
}
if (err) {
printf("%s: err %d\n", __func__, err);
npcm_dump_regs(bus);
}
return err;
}
static void npcm_i2c_reset(struct npcm_i2c_bus *bus)
{
struct npcm_i2c_regs *reg = bus->reg;
debug("%s: module %d\n", __func__, bus->num);
/* disable & enable SMB moudle */
clrbits_8(®->ctl2, SMBCTL2_ENABLE);
setbits_8(®->ctl2, SMBCTL2_ENABLE);
/* clear BB and status */
writeb(SMBCST_BB, ®->cst);
writeb(0xff, ®->st);
/* select bank 1 */
setbits_8(®->ctl3, SMBCTL3_BNK_SEL);
/* Clear all fifo bits */
writeb(SMBFIF_CTS_CLR_FIFO, ®->bank1.fif_cts);
/* select bank 0 */
clrbits_8(®->ctl3, SMBCTL3_BNK_SEL);
/* clear EOB bit */
writeb(SMBCST3_EO_BUSY, ®->bank0.cst3);
/* single byte mode */
clrbits_8(®->bank0.fif_ctl, SMBFIF_CTL_FIFO_EN);
/* set POLL mode */
writeb(0, ®->ctl1);
}
static void npcm_i2c_recovery(struct npcm_i2c_bus *bus, u32 addr)
{
u8 val;
int iter = 27;
struct npcm_i2c_regs *reg = bus->reg;
int err;
val = readb(®->ctl3);
/* Skip recovery, bus not stucked */
if ((val & SMBCTL3_SCL_LVL) && (val & SMBCTL3_SDA_LVL))
return;
printf("Performing I2C bus %d recovery...\n", bus->num);
/* SCL/SDA are not releaed, perform recovery */
while (1) {
/* toggle SCL line */
writeb(SMBCST_TGSCL, ®->cst);
udelay(20);
val = readb(®->ctl3);
if (val & SMBCTL3_SDA_LVL)
break;
if (iter-- == 0)
break;
}
if (val & SMBCTL3_SDA_LVL) {
writeb((u8)((addr << 1) & 0xff), ®->sda);
err = npcm_i2c_send_start(bus);
if (!err) {
udelay(20);
npcm_i2c_send_stop(bus, false);
udelay(200);
printf("I2C bus %d recovery completed\n",
bus->num);
} else {
printf("%s: send START err %d\n", __func__, err);
}
} else {
printf("Fail to recover I2C bus %d\n", bus->num);
}
npcm_i2c_reset(bus);
}
static int npcm_i2c_send_address(struct npcm_i2c_bus *bus, u8 addr,
bool stall)
{
struct npcm_i2c_regs *reg = bus->reg;
ulong start_time;
u8 val;
/* Stall After Start Enable */
if (stall)
setbits_8(®->ctl1, SMBCTL1_STASTRE);
writeb(addr, ®->sda);
if (stall) {
start_time = get_timer(0);
while (get_timer(start_time) < NPCM_I2C_TIMEOUT_MS) {
if (readb(®->st) & SMBST_STASTR)
break;
if (readb(®->st) & SMBST_BER) {
clrbits_8(®->ctl1, SMBCTL1_STASTRE);
return I2C_ERR_BER;
}
}
}
/* check ACK */
val = readb(®->st);
if (val & SMBST_NEGACK) {
debug("NACK on addr 0x%x\n", addr >> 1);
/* After a Stop condition, writing 1 to NEGACK clears it */
return I2C_ERR_NACK;
}
if (val & SMBST_BER)
return I2C_ERR_BER;
return 0;
}
static int npcm_i2c_read_bytes(struct npcm_i2c_bus *bus, u8 *data, int len)
{
struct npcm_i2c_regs *reg = bus->reg;
u8 val;
int i;
int err = 0;
if (len == 1) {
/* bus should be stalled before receiving last byte */
setbits_8(®->ctl1, SMBCTL1_ACK);
/* clear STASTRE if it is set */
if (readb(®->ctl1) & SMBCTL1_STASTRE) {
writeb(SMBST_STASTR, ®->st);
clrbits_8(®->ctl1, SMBCTL1_STASTRE);
}
npcm_i2c_check_sda(bus);
npcm_i2c_send_stop(bus, false);
*data = readb(®->sda);
/* this must be done to generate STOP condition */
writeb(SMBST_NEGACK, ®->st);
} else {
for (i = 0; i < len; i++) {
/*
* When NEGACK bit is set to 1 after the transmission of a byte,
* SDAST is not set to 1.
*/
if (i != (len - 1)) {
err = npcm_i2c_check_sda(bus);
} else {
err = readb_poll_timeout(®->ctl1, val,
!(val & SMBCTL1_ACK), 100000);
if (err) {
printf("wait nack timeout\n");
err = I2C_ERR_TIMEOUT;
npcm_dump_regs(bus);
}
}
if (err && err != I2C_ERR_TIMEOUT)
break;
if (i == (len - 2)) {
/* set NACK before last byte */
setbits_8(®->ctl1, SMBCTL1_ACK);
}
if (i == (len - 1)) {
/* last byte, send STOP condition */
npcm_i2c_send_stop(bus, false);
*data = readb(®->sda);
writeb(SMBST_NEGACK, ®->st);
break;
}
*data = readb(®->sda);
data++;
}
}
return err;
}
static int npcm_i2c_send_bytes(struct npcm_i2c_bus *bus, u8 *data, int len)
{
struct npcm_i2c_regs *reg = bus->reg;
u8 val;
int i;
int err = 0;
val = readb(®->st);
if (val & SMBST_NEGACK)
return I2C_ERR_NACK;
else if (val & SMBST_BER)
return I2C_ERR_BER;
/* clear STASTRE if it is set */
if (readb(®->ctl1) & SMBCTL1_STASTRE)
clrbits_8(®->ctl1, SMBCTL1_STASTRE);
for (i = 0; i < len; i++) {
err = npcm_i2c_check_sda(bus);
if (err)
break;
writeb(*data, ®->sda);
data++;
}
npcm_i2c_check_sda(bus);
return err;
}
static int npcm_i2c_read(struct npcm_i2c_bus *bus, u32 addr, u8 *data,
u32 len)
{
struct npcm_i2c_regs *reg = bus->reg;
int err;
bool stall;
if (len <= 0)
return -EINVAL;
/* send START condition */
err = npcm_i2c_send_start(bus);
if (err) {
debug("%s: send START err %d\n", __func__, err);
return err;
}
stall = (len == 1) ? true : false;
/* send address byte */
err = npcm_i2c_send_address(bus, (u8)(addr << 1) | 0x1, stall);
if (!err && len)
npcm_i2c_read_bytes(bus, data, len);
if (err == I2C_ERR_NACK) {
/* clear NACK */
writeb(SMBST_NEGACK, ®->st);
}
if (err)
debug("%s: err %d\n", __func__, err);
return err;
}
static int npcm_i2c_write(struct npcm_i2c_bus *bus, u32 addr, u8 *data,
u32 len)
{
struct npcm_i2c_regs *reg = bus->reg;
int err;
bool stall;
/* send START condition */
err = npcm_i2c_send_start(bus);
if (err) {
debug("%s: send START err %d\n", __func__, err);
return err;
}
stall = (len == 0) ? true : false;
/* send address byte */
err = npcm_i2c_send_address(bus, (u8)(addr << 1), stall);
if (!err && len)
err = npcm_i2c_send_bytes(bus, data, len);
/* clear STASTRE if it is set */
if (stall)
clrbits_8(®->ctl1, SMBCTL1_STASTRE);
if (err)
debug("%s: err %d\n", __func__, err);
return err;
}
static int npcm_i2c_xfer(struct udevice *dev,
struct i2c_msg *msg, int nmsgs)
{
struct npcm_i2c_bus *bus = dev_get_priv(dev);
struct npcm_i2c_regs *reg = bus->reg;
int ret = 0, err = 0;
if (nmsgs < 1 || nmsgs > 2) {
printf("%s: commands not support\n", __func__);
return -EREMOTEIO;
}
/* clear ST register */
writeb(0xFF, ®->st);
for ( ; nmsgs > 0; nmsgs--, msg++) {
if (msg->flags & I2C_M_RD)
err = npcm_i2c_read(bus, msg->addr, msg->buf,
msg->len);
else
err = npcm_i2c_write(bus, msg->addr, msg->buf,
msg->len);
if (err) {
debug("i2c_xfer: error %d\n", err);
ret = -EREMOTEIO;
break;
}
}
if (bus->started)
npcm_i2c_send_stop(bus, true);
if (err)
npcm_i2c_recovery(bus, msg->addr);
return ret;
}
static int npcm_i2c_init_clk(struct npcm_i2c_bus *bus, u32 bus_freq)
{
struct npcm_i2c_regs *reg = bus->reg;
u32 freq = bus->apb_clk;
u32 sclfrq;
u8 hldt, val;
/* SCLFRQ = T(SCL)/4/T(CLK) = FREQ(CLK)/4/FREQ(SCL) */
sclfrq = freq / (bus_freq * 4);
if (sclfrq < SCLFRQ_MIN || sclfrq > SCLFRQ_MAX)
return -EINVAL;
if (freq >= 40000000)
hldt = 17;
else if (freq >= 12500000)
hldt = 15;
else
hldt = 7;
val = readb(®->ctl2) & 0x1;
val |= (sclfrq & 0x7F) << 1;
writeb(val, ®->ctl2);
/* clear 400K_MODE bit */
val = readb(®->ctl3) & 0xc;
val |= (sclfrq >> 7) & 0x3;
writeb(val, ®->ctl3);
writeb(hldt, ®->bank0.ctl4);
return 0;
}
static int npcm_i2c_set_bus_speed(struct udevice *dev,
unsigned int speed)
{
struct npcm_i2c_bus *bus = dev_get_priv(dev);
return npcm_i2c_init_clk(bus, speed);
}
static int npcm_i2c_probe(struct udevice *dev)
{
struct npcm_i2c_bus *bus = dev_get_priv(dev);
struct npcm_gcr *gcr = (struct npcm_gcr *)NPCM_GCR_BA;
struct npcm_i2c_regs *reg;
u32 i2csegctl_val = dev_get_driver_data(dev);
struct clk clk;
int ret;
ret = clk_get_by_index(dev, 0, &clk);
if (ret) {
printf("%s: ret %d\n", __func__, ret);
return ret;
}
bus->apb_clk = clk_get_rate(&clk);
if (bus->apb_clk <= 0) {
printf("%s: fail to get rate\n", __func__);
return -EINVAL;
}
bus->num = dev->seq_;
bus->reg = dev_read_addr_ptr(dev);
bus->freq = dev_read_u32_default(dev, "clock-frequency", 100000);
bus->started = false;
reg = bus->reg;
if (npcm_i2c_init_clk(bus, bus->freq)) {
printf("%s: init_clk failed\n", __func__);
return -EINVAL;
}
/* set initial i2csegctl value */
writel(i2csegctl_val, &gcr->i2csegctl);
/* enable SMB module */
setbits_8(®->ctl2, SMBCTL2_ENABLE);
/* select register bank 0 */
clrbits_8(®->ctl3, SMBCTL3_BNK_SEL);
/* single byte mode */
clrbits_8(®->bank0.fif_ctl, SMBFIF_CTL_FIFO_EN);
/* set POLL mode */
writeb(0, ®->ctl1);
printf("I2C bus %d ready. speed=%d, base=0x%x, apb=%u\n",
bus->num, bus->freq, (u32)(uintptr_t)bus->reg, bus->apb_clk);
return 0;
}
static const struct dm_i2c_ops nuvoton_i2c_ops = {
.xfer = npcm_i2c_xfer,
.set_bus_speed = npcm_i2c_set_bus_speed,
};
static const struct udevice_id nuvoton_i2c_of_match[] = {
{ .compatible = "nuvoton,npcm845-i2c", .data = NPCM8XX_I2CSEGCTL_INIT_VAL},
{ .compatible = "nuvoton,npcm750-i2c", .data = NPCM7XX_I2CSEGCTL_INIT_VAL},
{}
};
U_BOOT_DRIVER(npcm_i2c_bus) = {
.name = "npcm-i2c",
.id = UCLASS_I2C,
.of_match = nuvoton_i2c_of_match,
.probe = npcm_i2c_probe,
.priv_auto = sizeof(struct npcm_i2c_bus),
.ops = &nuvoton_i2c_ops,
};
|