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
|
/* $OpenBSD: ti_iic.c,v 1.7 2016/07/27 11:45:02 patrick Exp $ */
/* $NetBSD: ti_iic.c,v 1.4 2013/04/25 13:04:27 rkujawa Exp $ */
/*
* Copyright (c) 2013 Manuel Bouyer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*-
* Copyright (c) 2012 Jared D. McNeill <jmcneill@invisible.ca>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/rwlock.h>
#include <machine/bus.h>
#include <machine/intr.h>
#include <machine/fdt.h>
#include <dev/i2c/i2cvar.h>
#include <armv7/armv7/armv7var.h>
#include <armv7/omap/prcmvar.h>
#include <armv7/omap/ti_iicreg.h>
#include <armv7/omap/sitara_cm.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/fdt.h>
#ifndef AM335X_I2C_SLAVE_ADDR
#define AM335X_I2C_SLAVE_ADDR 0x01
#endif
#ifdef I2CDEBUG
#define DPRINTF(args) printf args
#else
#define DPRINTF(args)
#endif
/* operation in progress */
typedef enum {
TI_I2CREAD,
TI_I2CWRITE,
TI_I2CDONE,
TI_I2CERROR
} ti_i2cop_t;
struct ti_iic_softc {
struct device sc_dev;
struct i2c_controller sc_ic;
struct rwlock sc_buslock;
struct device *sc_i2cdev;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
void *sc_ih;
int sc_node;
ti_i2cop_t sc_op;
int sc_buflen;
int sc_bufidx;
char *sc_buf;
int sc_rxthres;
int sc_txthres;
};
#define I2C_READ_REG(sc, reg) \
bus_space_read_2((sc)->sc_iot, (sc)->sc_ioh, (reg))
#define I2C_READ_DATA(sc) \
bus_space_read_1((sc)->sc_iot, (sc)->sc_ioh, AM335X_I2C_DATA);
#define I2C_WRITE_REG(sc, reg, val) \
bus_space_write_2((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
#define I2C_WRITE_DATA(sc, val) \
bus_space_write_1((sc)->sc_iot, (sc)->sc_ioh, AM335X_I2C_DATA, (val))
#define DEVNAME(sc) ((sc)->sc_dev.dv_xname)
int ti_iic_match(struct device *, void *, void *);
void ti_iic_attach(struct device *, struct device *, void *);
int ti_iic_intr(void *);
int ti_iic_acquire_bus(void *, int);
void ti_iic_release_bus(void *, int);
int ti_iic_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t, void *,
size_t, int);
void ti_iic_scan(struct device *, struct i2cbus_attach_args *, void *);
int ti_iic_reset(struct ti_iic_softc *);
int ti_iic_op(struct ti_iic_softc *, i2c_addr_t, ti_i2cop_t, uint8_t *,
size_t, int);
void ti_iic_handle_intr(struct ti_iic_softc *, uint32_t);
void ti_iic_do_read(struct ti_iic_softc *, uint32_t);
void ti_iic_do_write(struct ti_iic_softc *, uint32_t);
int ti_iic_wait(struct ti_iic_softc *, uint16_t, uint16_t, int);
uint32_t ti_iic_stat(struct ti_iic_softc *, uint32_t);
int ti_iic_flush(struct ti_iic_softc *);
struct cfattach tiiic_ca = {
sizeof (struct ti_iic_softc), ti_iic_match, ti_iic_attach
};
struct cfdriver tiiic_cd = {
NULL, "tiiic", DV_DULL
};
int
ti_iic_match(struct device *parent, void *match, void *aux)
{
struct fdt_attach_args *faa = aux;
return OF_is_compatible(faa->fa_node, "ti,omap4-i2c");
}
void
ti_iic_attach(struct device *parent, struct device *self, void *aux)
{
struct ti_iic_softc *sc = (struct ti_iic_softc *)self;
struct fdt_attach_args *faa = aux;
struct i2cbus_attach_args iba;
uint16_t rev;
int irq, unit, len;
char hwmods[128];
if (faa->fa_nreg != 1 || (faa->fa_nintr != 1 && faa->fa_nintr != 3))
return;
sc->sc_iot = faa->fa_iot;
sc->sc_node = faa->fa_node;
if (faa->fa_nintr == 1)
irq = faa->fa_intr[0];
else
irq = faa->fa_intr[1];
unit = 0;
if ((len = OF_getprop(faa->fa_node, "ti,hwmods", hwmods,
sizeof(hwmods))) == 5) {
if (!strncmp(hwmods, "i2c", 3) &&
(hwmods[3] > '0') && (hwmods[3] <= '9'))
unit = hwmods[3] - '1';
}
rw_init(&sc->sc_buslock, "tiiilk");
sc->sc_rxthres = sc->sc_txthres = 4;
if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
faa->fa_reg[0].size, 0, &sc->sc_ioh))
panic("%s: bus_space_map failed!", DEVNAME(sc));
sitara_cm_pinctrlbyname(faa->fa_node, "default");
sc->sc_ih = arm_intr_establish(irq, IPL_NET,
ti_iic_intr, sc, DEVNAME(sc));
prcm_enablemodule(PRCM_I2C0 + unit);
rev = I2C_READ_REG(sc, AM335X_I2C_REVNB_LO);
printf(" rev %d.%d\n",
(int)I2C_REVNB_LO_MAJOR(rev),
(int)I2C_REVNB_LO_MINOR(rev));
ti_iic_reset(sc);
ti_iic_flush(sc);
sc->sc_ic.ic_cookie = sc;
sc->sc_ic.ic_acquire_bus = ti_iic_acquire_bus;
sc->sc_ic.ic_release_bus = ti_iic_release_bus;
sc->sc_ic.ic_exec = ti_iic_exec;
bzero(&iba, sizeof iba);
iba.iba_name = "iic";
iba.iba_tag = &sc->sc_ic;
iba.iba_bus_scan = ti_iic_scan;
iba.iba_bus_scan_arg = &sc->sc_node;
(void) config_found(&sc->sc_dev, &iba, iicbus_print);
}
int
ti_iic_intr(void *arg)
{
struct ti_iic_softc *sc = arg;
uint32_t stat;
DPRINTF(("ti_iic_intr\n"));
stat = I2C_READ_REG(sc, AM335X_I2C_IRQSTATUS);
I2C_WRITE_REG(sc, AM335X_I2C_IRQSTATUS, stat);
DPRINTF(("ti_iic_intr pre handle sc->sc_op eq %#x\n", sc->sc_op));
ti_iic_handle_intr(sc, stat);
if (sc->sc_op == TI_I2CERROR || sc->sc_op == TI_I2CDONE) {
DPRINTF(("ti_iic_intr post handle sc->sc_op %#x\n", sc->sc_op));
wakeup(&sc->sc_dev);
}
DPRINTF(("ti_iic_intr status 0x%x\n", stat));
return 1;
}
int
ti_iic_acquire_bus(void *opaque, int flags)
{
struct ti_iic_softc *sc = opaque;
if (flags & I2C_F_POLL)
return 0;
return (rw_enter(&sc->sc_buslock, RW_WRITE));
}
void
ti_iic_release_bus(void *opaque, int flags)
{
struct ti_iic_softc *sc = opaque;
if (flags & I2C_F_POLL)
return;
rw_exit(&sc->sc_buslock);
}
int
ti_iic_exec(void *opaque, i2c_op_t op, i2c_addr_t addr,
const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
{
struct ti_iic_softc *sc = opaque;
int err;
DPRINTF(("ti_iic_exec: op 0x%x cmdlen %zd len %zd flags 0x%x\n",
op, cmdlen, len, flags));
#define __UNCONST(a) ((void *)(unsigned long)(const void *)(a))
if (cmdlen > 0) {
err = ti_iic_op(sc, addr, TI_I2CWRITE, __UNCONST(cmdbuf),
cmdlen, (I2C_OP_READ_P(op) ? 0 : I2C_F_STOP) | flags);
if (err)
goto done;
}
if (I2C_OP_STOP_P(op))
flags |= I2C_F_STOP;
/*
* I2C controller doesn't allow for zero-byte transfers.
*/
if (len == 0)
goto done;
if (I2C_OP_READ_P(op))
err = ti_iic_op(sc, addr, TI_I2CREAD, buf, len, flags);
else
err = ti_iic_op(sc, addr, TI_I2CWRITE, buf, len, flags);
done:
if (err)
ti_iic_reset(sc);
ti_iic_flush(sc);
DPRINTF(("ti_iic_exec: done %d\n", err));
return err;
}
int
ti_iic_reset(struct ti_iic_softc *sc)
{
uint32_t psc, scll, sclh;
int i;
DPRINTF(("ti_iic_reset\n"));
/* Disable */
I2C_WRITE_REG(sc, AM335X_I2C_CON, 0);
/* Soft reset */
I2C_WRITE_REG(sc, AM335X_I2C_SYSC, I2C_SYSC_SRST);
delay(1000);
/* enable so that we can check for reset complete */
I2C_WRITE_REG(sc, AM335X_I2C_CON, I2C_CON_EN);
delay(1000);
for (i = 0; i < 1000; i++) { /* 1s delay for reset */
if (I2C_READ_REG(sc, AM335X_I2C_SYSS) & I2C_SYSS_RDONE)
break;
}
/* Disable again */
I2C_WRITE_REG(sc, AM335X_I2C_CON, 0);
delay(50000);
if (i >= 1000) {
printf("%s: couldn't reset module\n", DEVNAME(sc));
return 1;
}
/* XXX standard speed only */
psc = 3;
scll = 53;
sclh = 55;
/* Clocks */
I2C_WRITE_REG(sc, AM335X_I2C_PSC, psc);
I2C_WRITE_REG(sc, AM335X_I2C_SCLL, scll);
I2C_WRITE_REG(sc, AM335X_I2C_SCLH, sclh);
/* Own I2C address */
I2C_WRITE_REG(sc, AM335X_I2C_OA, AM335X_I2C_SLAVE_ADDR);
/* 5 bytes fifo */
I2C_WRITE_REG(sc, AM335X_I2C_BUF,
I2C_BUF_RXTRSH(sc->sc_rxthres) | I2C_BUF_TXTRSH(sc->sc_txthres));
/* Enable */
I2C_WRITE_REG(sc, AM335X_I2C_CON, I2C_CON_EN);
return 0;
}
int
ti_iic_op(struct ti_iic_softc *sc, i2c_addr_t addr, ti_i2cop_t op,
uint8_t *buf, size_t buflen, int flags)
{
uint16_t con, stat, mask;
int err, retry;
KASSERT(op == TI_I2CREAD || op == TI_I2CWRITE);
DPRINTF(("ti_iic_op: addr %#x op %#x buf %p buflen %#x flags %#x\n",
addr, op, buf, (unsigned int) buflen, flags));
mask = I2C_IRQSTATUS_ARDY | I2C_IRQSTATUS_NACK | I2C_IRQSTATUS_AL;
if (op == TI_I2CREAD)
mask |= I2C_IRQSTATUS_RDR | I2C_IRQSTATUS_RRDY;
else
mask |= I2C_IRQSTATUS_XDR | I2C_IRQSTATUS_XRDY;
err = ti_iic_wait(sc, I2C_IRQSTATUS_BB, 0, flags);
if (err) {
DPRINTF(("ti_iic_op: wait error %d\n", err));
return err;
}
con = I2C_CON_EN;
con |= I2C_CON_MST;
con |= I2C_CON_STT;
if (flags & I2C_F_STOP)
con |= I2C_CON_STP;
if (addr & ~0x7f)
con |= I2C_CON_XSA;
if (op == TI_I2CWRITE)
con |= I2C_CON_TRX;
sc->sc_op = op;
sc->sc_buf = buf;
sc->sc_buflen = buflen;
sc->sc_bufidx = 0;
I2C_WRITE_REG(sc,
AM335X_I2C_CON, I2C_CON_EN | I2C_CON_MST | I2C_CON_STP);
DPRINTF(("ti_iic_op: op %d con 0x%x ", op, con));
I2C_WRITE_REG(sc, AM335X_I2C_CNT, buflen);
I2C_WRITE_REG(sc, AM335X_I2C_SA, (addr & I2C_SA_MASK));
DPRINTF(("SA 0x%x len %d\n",
I2C_READ_REG(sc, AM335X_I2C_SA), I2C_READ_REG(sc, AM335X_I2C_CNT)));
if ((flags & I2C_F_POLL) == 0) {
/* clear any pending interrupt */
I2C_WRITE_REG(sc, AM335X_I2C_IRQSTATUS,
I2C_READ_REG(sc, AM335X_I2C_IRQSTATUS));
/* and enable */
I2C_WRITE_REG(sc, AM335X_I2C_IRQENABLE_SET, mask);
}
/* start transfer */
I2C_WRITE_REG(sc, AM335X_I2C_CON, con);
if ((flags & I2C_F_POLL) == 0) {
/* and wait for completion */
DPRINTF(("ti_iic_op waiting, op %#x\n", sc->sc_op));
while (sc->sc_op == op) {
if (tsleep(&sc->sc_dev, PWAIT, "tiiic", 500)
== EWOULDBLOCK) {
/* timeout */
op = TI_I2CERROR;
}
}
DPRINTF(("ti_iic_op waiting done, op %#x\n", sc->sc_op));
/* disable interrupts */
I2C_WRITE_REG(sc, AM335X_I2C_IRQENABLE_CLR, 0xffff);
} else {
/* poll for completion */
DPRINTF(("ti_iic_op polling, op %x\n", sc->sc_op));
while (sc->sc_op == op) {
stat = ti_iic_stat(sc, mask);
DPRINTF(("ti_iic_op stat 0x%x\n", stat));
if (stat == 0) /* timeout */
sc->sc_op = TI_I2CERROR;
else
ti_iic_handle_intr(sc, stat);
I2C_WRITE_REG(sc, AM335X_I2C_IRQSTATUS, stat);
}
DPRINTF(("ti_iic_op polling done, op now %x\n", sc->sc_op));
}
retry = 10000;
I2C_WRITE_REG(sc, AM335X_I2C_CON, 0);
while (I2C_READ_REG(sc, AM335X_I2C_CON) & I2C_CON_MST) {
delay(100);
if (--retry == 0)
break;
}
return (sc->sc_op == TI_I2CDONE) ? 0 : EIO;
}
void
ti_iic_handle_intr(struct ti_iic_softc *sc, uint32_t stat)
{
KASSERT(stat != 0);
DPRINTF(("ti_iic_handle_intr stat %#x\n", stat));
if (stat & (I2C_IRQSTATUS_NACK|I2C_IRQSTATUS_AL)) {
sc->sc_op = TI_I2CERROR;
return;
}
if (stat & I2C_IRQSTATUS_ARDY) {
sc->sc_op = TI_I2CDONE;
return;
}
if (sc->sc_op == TI_I2CREAD)
ti_iic_do_read(sc, stat);
else if (sc->sc_op == TI_I2CWRITE)
ti_iic_do_write(sc, stat);
else
return;
}
void
ti_iic_do_read(struct ti_iic_softc *sc, uint32_t stat)
{
int len = 0;
DPRINTF(("ti_iic_do_read stat %#x\n", stat));
if (stat & I2C_IRQSTATUS_RDR) {
len = I2C_READ_REG(sc, AM335X_I2C_BUFSTAT);
len = I2C_BUFSTAT_RXSTAT(len);
DPRINTF(("ti_iic_do_read receive drain len %d left %d\n",
len, I2C_READ_REG(sc, AM335X_I2C_CNT)));
} else if (stat & I2C_IRQSTATUS_RRDY) {
len = sc->sc_rxthres + 1;
DPRINTF(("ti_iic_do_read receive len %d left %d\n",
len, I2C_READ_REG(sc, AM335X_I2C_CNT)));
}
for (;
sc->sc_bufidx < sc->sc_buflen && len > 0;
sc->sc_bufidx++, len--) {
sc->sc_buf[sc->sc_bufidx] = I2C_READ_DATA(sc);
DPRINTF(("ti_iic_do_read got b[%d]=0x%x\n", sc->sc_bufidx,
sc->sc_buf[sc->sc_bufidx]));
}
DPRINTF(("ti_iic_do_read done\n"));
}
void
ti_iic_do_write(struct ti_iic_softc *sc, uint32_t stat)
{
int len = 0;
DPRINTF(("ti_iic_do_write stat %#x\n", stat));
if (stat & I2C_IRQSTATUS_XDR) {
len = I2C_READ_REG(sc, AM335X_I2C_BUFSTAT);
len = I2C_BUFSTAT_TXSTAT(len);
DPRINTF(("ti_iic_do_write xmit drain len %d left %d\n",
len, I2C_READ_REG(sc, AM335X_I2C_CNT)));
} else if (stat & I2C_IRQSTATUS_XRDY) {
len = sc->sc_txthres + 1;
DPRINTF(("ti_iic_do_write xmit len %d left %d\n",
len, I2C_READ_REG(sc, AM335X_I2C_CNT)));
}
for (;
sc->sc_bufidx < sc->sc_buflen && len > 0;
sc->sc_bufidx++, len--) {
DPRINTF(("ti_iic_do_write send b[%d]=0x%x\n",
sc->sc_bufidx, sc->sc_buf[sc->sc_bufidx]));
I2C_WRITE_DATA(sc, sc->sc_buf[sc->sc_bufidx]);
}
DPRINTF(("ti_iic_do_write done\n"));
}
int
ti_iic_wait(struct ti_iic_softc *sc, uint16_t mask, uint16_t val, int flags)
{
int retry = 10;
uint16_t v;
DPRINTF(("ti_iic_wait mask %#x val %#x flags %#x\n", mask, val, flags));
while (((v = I2C_READ_REG(sc, AM335X_I2C_IRQSTATUS_RAW)) & mask) != val) {
--retry;
if (retry == 0) {
printf("%s: wait timeout, mask=%#x val=%#x stat=%#x\n",
DEVNAME(sc), mask, val, v);
return EBUSY;
}
if (flags & I2C_F_POLL)
delay(50000);
else
tsleep(&sc->sc_dev, PWAIT, "tiiic", 50);
}
DPRINTF(("ti_iic_wait done retry %#x\n", retry));
return 0;
}
uint32_t
ti_iic_stat(struct ti_iic_softc *sc, uint32_t mask)
{
uint32_t v;
int retry = 500;
DPRINTF(("ti_iic_wait mask %#x\n", mask));
while (--retry > 0) {
v = I2C_READ_REG(sc, AM335X_I2C_IRQSTATUS_RAW) & mask;
if (v != 0)
break;
delay(100);
}
DPRINTF(("ti_iic_wait done retry %#x\n", retry));
return v;
}
int
ti_iic_flush(struct ti_iic_softc *sc)
{
DPRINTF(("ti_iic_flush\n"));
#if 0
int retry = 1000;
uint16_t v;
while ((v =
I2C_READ_REG(sc, AM335X_I2C_IRQSTATUS_RAW)) & I2C_IRQSTATUS_RRDY) {
if (--retry == 0) {
printf("%s: flush timeout, stat = %#x\n", DEVNAME(sc), v);
return EBUSY;
}
(void)I2C_READ_DATA(sc);
delay(1000);
}
#endif
I2C_WRITE_REG(sc, AM335X_I2C_CNT, 0);
return 0;
}
void
ti_iic_scan(struct device *self, struct i2cbus_attach_args *iba, void *aux)
{
int iba_node = *(int *)aux;
extern int iic_print(void *, const char *);
struct i2c_attach_args ia;
char name[32];
uint32_t reg[1];
int node;
for (node = OF_child(iba_node); node; node = OF_peer(node)) {
memset(name, 0, sizeof(name));
memset(reg, 0, sizeof(reg));
if (OF_getprop(node, "compatible", name, sizeof(name)) == -1)
continue;
if (name[0] == '\0')
continue;
if (OF_getprop(node, "reg", ®, sizeof(reg)) != sizeof(reg))
continue;
memset(&ia, 0, sizeof(ia));
ia.ia_tag = iba->iba_tag;
ia.ia_addr = bemtoh32(®[0]);
ia.ia_name = name;
ia.ia_cookie = &node;
config_found(self, &ia, iic_print);
}
}
|