ki2c.c revision 1.38 1 /* $NetBSD: ki2c.c,v 1.38 2025/07/07 01:14:51 macallan Exp $ */
2 /* Id: ki2c.c,v 1.7 2002/10/05 09:56:05 tsubai Exp */
3
4 /*-
5 * Copyright (c) 2001 Tsubai Masanari. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <sys/param.h>
31 #include <sys/device.h>
32 #include <sys/systm.h>
33 #include <sys/mutex.h>
34
35 #include <dev/ofw/openfirm.h>
36 #include <machine/autoconf.h>
37 #include <powerpc/pic/picvar.h>
38
39 #include "opt_ki2c.h"
40 #include <macppc/dev/ki2cvar.h>
41
42 #ifdef KI2C_DEBUG
43 #define DPRINTF printf
44 #else
45 #define DPRINTF while (0) printf
46 #endif
47
48 #define KI2C_EXEC_MAX_CMDLEN 32
49 #define KI2C_EXEC_MAX_BUFLEN 32
50
51 int ki2c_match(device_t, cfdata_t, void *);
52 void ki2c_attach(device_t, device_t, void *);
53 inline uint8_t ki2c_readreg(struct ki2c_softc *, int);
54 inline void ki2c_writereg(struct ki2c_softc *, int, uint8_t);
55 u_int ki2c_getmode(struct ki2c_softc *);
56 void ki2c_setmode(struct ki2c_softc *, u_int);
57 u_int ki2c_getspeed(struct ki2c_softc *);
58 void ki2c_setspeed(struct ki2c_softc *, u_int);
59 int ki2c_intr(void *);
60 int ki2c_poll(struct ki2c_softc *, int);
61 int ki2c_start(struct ki2c_softc *, int, int, void *, int);
62 int ki2c_read(struct ki2c_softc *, int, int, void *, int);
63 int ki2c_write(struct ki2c_softc *, int, int, void *, int);
64
65 /* I2C glue */
66 static int ki2c_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
67 void *, size_t, int);
68
69
70 CFATTACH_DECL_NEW(ki2c, sizeof(struct ki2c_softc), ki2c_match, ki2c_attach,
71 NULL, NULL);
72
73 int
74 ki2c_match(device_t parent, cfdata_t match, void *aux)
75 {
76 struct confargs *ca = aux;
77
78 if (strcmp(ca->ca_name, "i2c") == 0)
79 return 1;
80
81 return 0;
82 }
83
84 void
85 ki2c_attach(device_t parent, device_t self, void *aux)
86 {
87 struct ki2c_softc *sc = device_private(self);
88 struct confargs *ca = aux;
89 int node = ca->ca_node;
90 uint32_t addr, channel, reg, intr[2];
91 int rate, child, /*namelen,*/ i2cbus[2] = {0, 0};
92 struct i2cbus_attach_args iba;
93 prop_dictionary_t dict = device_properties(self);
94 prop_array_t cfg;
95 int devs, devc, intrparent;;
96 char compat[256], num[8], descr[32];
97 prop_dictionary_t dev;
98 prop_data_t data;
99 char name[32], intr_xname[32];
100 uint32_t picbase;
101
102 sc->sc_dev = self;
103 sc->sc_tag = ca->ca_tag;
104 ca->ca_reg[0] += ca->ca_baseaddr;
105
106 if (OF_getprop(node, "AAPL,i2c-rate", &rate, 4) != 4) {
107 aprint_error(": cannot get i2c-rate\n");
108 return;
109 }
110 if (OF_getprop(node, "AAPL,address", &addr, 4) != 4) {
111 aprint_error(": unable to find i2c address\n");
112 return;
113 }
114 if (bus_space_map(sc->sc_tag, addr, PAGE_SIZE, 0, &sc->sc_bh) != 0) {
115 aprint_error_dev(sc->sc_dev, "failed to map registers\n");
116 return;
117 }
118
119 if (OF_getprop(node, "AAPL,address-step", &sc->sc_regstep, 4) != 4) {
120 aprint_error(": unable to find i2c address step\n");
121 return;
122 }
123
124 if(OF_getprop(node, "interrupts", intr, 8) != 8) {
125 aprint_error(": can't find interrupt\n");
126 return;
127 }
128
129 /*
130 * on some G5 we have two openpics, one in mac-io, one in /u3
131 * in order to get interrupts we need to know which one we're
132 * connected to
133 */
134 sc->sc_poll = 0;
135
136 if(OF_getprop(node, "interrupt-parent", &intrparent, 4) == 4) {
137 uint32_t preg[8];
138 struct pic_ops *pic;
139
140 sc->sc_poll = 1;
141 if(OF_getprop(intrparent, "reg", preg, 8) > 4) {
142 /* now look for a pic with that base... */
143 picbase = preg[0];
144 if ((picbase & 0x80000000) == 0) {
145 /* some OF versions have the openpic's reg as
146 * an offset into mac-io just to be annoying */
147 int mio = OF_parent(intrparent);
148 if (OF_getprop(mio, "ranges", preg, 20) == 20)
149 picbase += preg[3];
150 }
151 DPRINTF("PIC base %08x\n", picbase);
152 pic = find_pic_by_cookie((void *)picbase);
153 if (pic != NULL) {
154 sc->sc_poll = 0;
155 intr[0] += pic->pic_intrbase;
156 }
157 }
158 }
159
160 if (sc->sc_poll) {
161 aprint_normal(" polling");
162 } else aprint_normal(" irq %d", intr[0]);
163
164 printf("\n");
165
166 ki2c_writereg(sc, STATUS, 0);
167 ki2c_writereg(sc, ISR, 0);
168 ki2c_writereg(sc, IER, 0);
169
170 ki2c_setmode(sc, I2C_STDSUBMODE);
171 ki2c_setspeed(sc, I2C_100kHz); /* XXX rate */
172
173 ki2c_writereg(sc, IER,I2C_INT_DATA|I2C_INT_ADDR|I2C_INT_STOP);
174
175 cfg = prop_array_create();
176 prop_dictionary_set(dict, "i2c-child-devices", cfg);
177 prop_object_release(cfg);
178
179 /*
180 * newer OF puts I2C devices under 'i2c-bus' instead of attaching them
181 * directly to the ki2c node so we just check if we have a child named
182 * 'i2c-bus' and if so we attach its children, not ours
183 *
184 * XXX
185 * should probably check for multiple i2c-bus children
186 */
187
188 int found_busnode = 0;
189 channel = 0;
190 child = OF_child(node);
191 while (child != 0) {
192 OF_getprop(child, "name", name, sizeof(name));
193 if (strcmp(name, "i2c-bus") == 0) {
194 OF_getprop(child, "reg", &channel, sizeof(channel));
195 i2cbus[channel] = child;
196 DPRINTF("found channel %x\n", channel);
197 found_busnode = 1;
198 }
199 child = OF_peer(child);
200 }
201 if (found_busnode == 0)
202 i2cbus[0] = node;
203
204 for (channel = 0; channel < 2; channel++) {
205 devs = OF_child(i2cbus[channel]);
206 while (devs != 0) {
207 if (OF_getprop(devs, "name", name, 32) <= 0)
208 goto skip;
209 if (OF_getprop(devs, "compatible", compat, 256) <= 0) {
210 /* some i2c device nodes don't have 'compatible' */
211 memset(compat, 0, 256);
212 strncpy(compat, name, 256);
213 }
214 if (OF_getprop(devs, "reg", &addr, 4) <= 0)
215 if (OF_getprop(devs, "i2c-address", &addr, 4) <= 0)
216 goto skip;
217 addr |= channel << 8;
218 addr = addr >> 1;
219 DPRINTF("-> %s@%x\n", name, addr);
220 dev = prop_dictionary_create();
221 prop_dictionary_set_string(dev, "name", name);
222 data = prop_data_create_copy(compat, strlen(compat)+1);
223 prop_dictionary_set(dev, "compatible", data);
224 prop_object_release(data);
225 prop_dictionary_set_uint32(dev, "addr", addr);
226 prop_dictionary_set_uint64(dev, "cookie", devs);
227 /* look for location info for sensors */
228 devc = OF_child(devs);
229 if (devc == 0) {
230 /* old style name info */
231 uint32_t ids[4];
232 int len = OF_getprop(devs, "hwsensor-id", ids, 16);
233 int i = 0, idx = 0;
234 char buffer[256];
235 memset(buffer, 0, 256);
236 OF_getprop(devs, "hwsensor-location", buffer, 256);
237 while (len > 0) {
238 reg = ids[i];
239 strcpy(descr, &buffer[idx]);
240 idx += strlen(descr) + 1;
241 DPRINTF("found '%s' at %02x\n", descr, reg);
242 snprintf(num, 7, "s%02x", i);
243 prop_dictionary_set_string(dev, num, descr);
244 i++;
245 len -= 4;
246 }
247 } else {
248 while (devc != 0) {
249 if (OF_getprop(devc, "reg", ®, 4) < 4) goto nope;
250 if (OF_getprop(devc, "location", descr, 32) <= 0)
251 goto nope;
252 DPRINTF("found '%s' at %02x\n", descr, reg);
253 snprintf(num, 7, "s%02x", reg);
254 prop_dictionary_set_string(dev, num, descr);
255 nope:
256 devc = OF_peer(devc);
257 }
258 }
259
260 prop_array_add(cfg, dev);
261 prop_object_release(dev);
262 skip:
263 devs = OF_peer(devs);
264 }
265 }
266
267 cv_init(&sc->sc_todev, device_xname(self));
268 mutex_init(&sc->sc_todevmtx, MUTEX_DEFAULT, IPL_NONE);
269
270 if(sc->sc_poll == 0) {
271 snprintf(intr_xname, sizeof(intr_xname), "%s intr", device_xname(self));
272 intr_establish_xname(intr[0], (intr[1] & 1) ? IST_LEVEL : IST_EDGE,
273 IPL_BIO, ki2c_intr, sc, intr_xname);
274
275 ki2c_writereg(sc, IER, I2C_INT_DATA | I2C_INT_ADDR| I2C_INT_STOP);
276 }
277
278 /* fill in the i2c tag */
279 iic_tag_init(&sc->sc_i2c);
280 sc->sc_i2c.ic_cookie = sc;
281 sc->sc_i2c.ic_exec = ki2c_i2c_exec;
282
283 memset(&iba, 0, sizeof(iba));
284 iba.iba_tag = &sc->sc_i2c;
285 config_found(sc->sc_dev, &iba, iicbus_print, CFARGS_NONE);
286
287 }
288
289 uint8_t
290 ki2c_readreg(struct ki2c_softc *sc, int reg)
291 {
292
293 return bus_space_read_1(sc->sc_tag, sc->sc_bh, sc->sc_regstep * reg);
294 }
295
296 void
297 ki2c_writereg(struct ki2c_softc *sc, int reg, uint8_t val)
298 {
299
300 bus_space_write_1(sc->sc_tag, sc->sc_bh, reg * sc->sc_regstep, val);
301 delay(10);
302 }
303
304 u_int
305 ki2c_getmode(struct ki2c_softc *sc)
306 {
307 return ki2c_readreg(sc, MODE) & I2C_MODE;
308 }
309
310 void
311 ki2c_setmode(struct ki2c_softc *sc, u_int mode)
312 {
313 ki2c_writereg(sc, MODE, mode);
314 }
315
316 u_int
317 ki2c_getspeed(struct ki2c_softc *sc)
318 {
319 return ki2c_readreg(sc, MODE) & I2C_SPEED;
320 }
321
322 void
323 ki2c_setspeed(struct ki2c_softc *sc, u_int speed)
324 {
325 u_int x;
326
327 KASSERT((speed & ~I2C_SPEED) == 0);
328 x = ki2c_readreg(sc, MODE);
329 x &= ~I2C_SPEED;
330 x |= speed;
331 ki2c_writereg(sc, MODE, x);
332 }
333
334 int
335 ki2c_intr(void *cookie)
336 {
337 struct ki2c_softc *sc = cookie;
338 u_int isr, x;
339 isr = ki2c_readreg(sc, ISR);
340 if (isr & I2C_INT_ADDR) {
341 #if 0
342 if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
343 /* No slave responded. */
344 sc->sc_flags |= I2C_ERROR;
345 goto out;
346 }
347 #endif
348
349 if (sc->sc_flags & I2C_READING) {
350 if (sc->sc_resid > 1) {
351 x = ki2c_readreg(sc, CONTROL);
352 x |= I2C_CT_AAK;
353 ki2c_writereg(sc, CONTROL, x);
354 }
355 } else {
356 ki2c_writereg(sc, DATA, *sc->sc_data++);
357 sc->sc_resid--;
358 }
359 }
360
361 if (isr & I2C_INT_DATA) {
362 if (sc->sc_flags & I2C_READING) {
363 *sc->sc_data++ = ki2c_readreg(sc, DATA);
364 sc->sc_resid--;
365
366 if (sc->sc_resid == 0) { /* Completed */
367 ki2c_writereg(sc, CONTROL, 0);
368 goto out;
369 }
370 } else {
371 #if 0
372 if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
373 /* No slave responded. */
374 sc->sc_flags |= I2C_ERROR;
375 goto out;
376 }
377 #endif
378
379 if (sc->sc_resid == 0) {
380 x = ki2c_readreg(sc, CONTROL) | I2C_CT_STOP;
381 ki2c_writereg(sc, CONTROL, x);
382 } else {
383 ki2c_writereg(sc, DATA, *sc->sc_data++);
384 sc->sc_resid--;
385 }
386 }
387 }
388
389 out:
390 if (isr & I2C_INT_STOP) {
391 ki2c_writereg(sc, CONTROL, 0);
392 sc->sc_flags &= ~I2C_BUSY;
393 cv_signal(&sc->sc_todev);
394 }
395
396 ki2c_writereg(sc, ISR, isr);
397
398 return 1;
399 }
400
401 int
402 ki2c_poll(struct ki2c_softc *sc, int timo)
403 {
404 int bail = 0;
405 while (sc->sc_flags & I2C_BUSY) {
406 if ((cold) || (bail > 10) || (sc->sc_poll)) {
407 if (ki2c_readreg(sc, ISR))
408 ki2c_intr(sc);
409 timo -= 10;
410 if (timo < 0) {
411 DPRINTF("i2c_poll: timeout\n");
412 return -1;
413 }
414 delay(10);
415 } else {
416 mutex_enter(&sc->sc_todevmtx);
417 cv_timedwait_sig(&sc->sc_todev, &sc->sc_todevmtx, hz/10);
418 mutex_exit(&sc->sc_todevmtx);
419 bail++;
420 }
421 }
422 return 0;
423 }
424
425 int
426 ki2c_start(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
427 {
428 int rw = (sc->sc_flags & I2C_READING) ? 1 : 0;
429 int timo, x;
430
431 KASSERT((addr & 1) == 0);
432
433 sc->sc_data = data;
434 sc->sc_resid = len;
435 sc->sc_flags |= I2C_BUSY;
436
437 timo = 1000 + len * 200;
438
439 /* XXX TAS3001 sometimes takes 50ms to finish writing registers. */
440 /* if (addr == 0x68) */
441 timo += 100000;
442
443 ki2c_writereg(sc, ADDR, addr | rw);
444 ki2c_writereg(sc, SUBADDR, subaddr);
445
446 x = ki2c_readreg(sc, CONTROL) | I2C_CT_ADDR;
447 ki2c_writereg(sc, CONTROL, x);
448
449 if (ki2c_poll(sc, timo))
450 return -1;
451 if (sc->sc_flags & I2C_ERROR) {
452 DPRINTF("I2C_ERROR\n");
453 return -1;
454 }
455 return 0;
456 }
457
458 int
459 ki2c_read(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
460 {
461 sc->sc_flags = I2C_READING;
462 DPRINTF("ki2c_read: %02x %d\n", addr, len);
463 return ki2c_start(sc, addr, subaddr, data, len);
464 }
465
466 int
467 ki2c_write(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
468 {
469 sc->sc_flags = 0;
470 DPRINTF("ki2c_write: %02x %d\n",addr,len);
471 return ki2c_start(sc, addr, subaddr, data, len);
472 }
473
474 int
475 ki2c_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
476 size_t cmdlen, void *vbuf, size_t buflen, int flags)
477 {
478 struct ki2c_softc *sc = cookie;
479 int i;
480 size_t w_len;
481 uint8_t *wp;
482 uint8_t wrbuf[KI2C_EXEC_MAX_CMDLEN + KI2C_EXEC_MAX_CMDLEN];
483 uint8_t channel;
484
485 /*
486 * We don't have any idea if the ki2c controller can execute
487 * i2c quick_{read,write} operations, so if someone tries one,
488 * return an error.
489 */
490 if (cmdlen == 0 && buflen == 0)
491 return -1;
492
493 /*
494 * Transaction could be much larger now. Bail if it exceeds our
495 * small combining buffer, we don't expect such devices.
496 */
497 if (cmdlen + buflen > sizeof(wrbuf))
498 return -1;
499
500 channel = (addr & 0xf80) ? 0x10 : 0x00;
501 addr &= 0x7f;
502
503
504 /* we handle the subaddress stuff ourselves */
505 ki2c_setmode(sc, channel | I2C_STDMODE);
506 ki2c_setspeed(sc, I2C_50kHz);
507
508 /* Write-buffer defaults to vcmd */
509 wp = (uint8_t *)(__UNCONST(vcmd));
510 w_len = cmdlen;
511
512 /*
513 * Concatenate vcmd and vbuf for write operations
514 *
515 * Drivers written specifically for ki2c might already do this,
516 * but "generic" i2c drivers still provide separate arguments
517 * for the cmd and buf parts of iic_smbus_write_{byte,word}.
518 */
519 if (I2C_OP_WRITE_P(op) && buflen != 0) {
520 if (cmdlen == 0) {
521 wp = (uint8_t *)vbuf;
522 w_len = buflen;
523 } else {
524 KASSERT((cmdlen + buflen) <= sizeof(wrbuf));
525 wp = (uint8_t *)(__UNCONST(vcmd));
526 w_len = 0;
527 for (i = 0; i < cmdlen; i++)
528 wrbuf[w_len++] = *wp++;
529 wp = (uint8_t *)vbuf;
530 for (i = 0; i < buflen; i++)
531 wrbuf[w_len++] = *wp++;
532 wp = wrbuf;
533 }
534 }
535
536 if (w_len > 0)
537 if (ki2c_write(sc, addr << 1, 0, wp, w_len) !=0 )
538 return -1;
539
540 if (I2C_OP_READ_P(op)) {
541 if (ki2c_read(sc, addr << 1, 0, vbuf, buflen) !=0 )
542 return -1;
543 }
544 return 0;
545 }
546