ki2c.c revision 1.34 1 /* $NetBSD: ki2c.c,v 1.34 2025/06/30 10:17:16 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
38 #include "opt_ki2c.h"
39 #include <macppc/dev/ki2cvar.h>
40
41 #ifdef KI2C_DEBUG
42 #define DPRINTF printf
43 #else
44 #define DPRINTF while (0) printf
45 #endif
46
47 #define KI2C_EXEC_MAX_CMDLEN 32
48 #define KI2C_EXEC_MAX_BUFLEN 32
49
50 int ki2c_match(device_t, cfdata_t, void *);
51 void ki2c_attach(device_t, device_t, void *);
52 inline uint8_t ki2c_readreg(struct ki2c_softc *, int);
53 inline void ki2c_writereg(struct ki2c_softc *, int, uint8_t);
54 u_int ki2c_getmode(struct ki2c_softc *);
55 void ki2c_setmode(struct ki2c_softc *, u_int);
56 u_int ki2c_getspeed(struct ki2c_softc *);
57 void ki2c_setspeed(struct ki2c_softc *, u_int);
58 int ki2c_intr(struct ki2c_softc *);
59 int ki2c_poll(struct ki2c_softc *, int);
60 int ki2c_start(struct ki2c_softc *, int, int, void *, int);
61 int ki2c_read(struct ki2c_softc *, int, int, void *, int);
62 int ki2c_write(struct ki2c_softc *, int, int, void *, int);
63
64 /* I2C glue */
65 static int ki2c_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
66 void *, size_t, int);
67
68
69 CFATTACH_DECL_NEW(ki2c, sizeof(struct ki2c_softc), ki2c_match, ki2c_attach,
70 NULL, NULL);
71
72 int
73 ki2c_match(device_t parent, cfdata_t match, void *aux)
74 {
75 struct confargs *ca = aux;
76
77 if (strcmp(ca->ca_name, "i2c") == 0)
78 return 1;
79
80 return 0;
81 }
82
83 void
84 ki2c_attach(device_t parent, device_t self, void *aux)
85 {
86 struct ki2c_softc *sc = device_private(self);
87 struct confargs *ca = aux;
88 int node = ca->ca_node;
89 uint32_t addr, channel, reg;
90 int rate, child, /*namelen,*/ i2cbus[2] = {0, 0};
91 struct i2cbus_attach_args iba;
92 prop_dictionary_t dict = device_properties(self);
93 prop_array_t cfg;
94 int devs, devc;
95 char compat[256], num[8], descr[32];
96 prop_dictionary_t dev;
97 prop_data_t data;
98 char name[32];
99
100 sc->sc_dev = self;
101 sc->sc_tag = ca->ca_tag;
102 ca->ca_reg[0] += ca->ca_baseaddr;
103
104 if (OF_getprop(node, "AAPL,i2c-rate", &rate, 4) != 4) {
105 aprint_error(": cannot get i2c-rate\n");
106 return;
107 }
108 if (OF_getprop(node, "AAPL,address", &addr, 4) != 4) {
109 aprint_error(": unable to find i2c address\n");
110 return;
111 }
112 if (bus_space_map(sc->sc_tag, addr, PAGE_SIZE, 0, &sc->sc_bh) != 0) {
113 aprint_error_dev(sc->sc_dev, "failed to map registers\n");
114 return;
115 }
116
117 if (OF_getprop(node, "AAPL,address-step", &sc->sc_regstep, 4) != 4) {
118 aprint_error(": unable to find i2c address step\n");
119 return;
120 }
121
122 printf("\n");
123
124 ki2c_writereg(sc, STATUS, 0);
125 ki2c_writereg(sc, ISR, 0);
126 ki2c_writereg(sc, IER, 0);
127
128 ki2c_setmode(sc, I2C_STDSUBMODE);
129 ki2c_setspeed(sc, I2C_100kHz); /* XXX rate */
130
131 ki2c_writereg(sc, IER,I2C_INT_DATA|I2C_INT_ADDR|I2C_INT_STOP);
132
133 cfg = prop_array_create();
134 prop_dictionary_set(dict, "i2c-child-devices", cfg);
135 prop_object_release(cfg);
136
137 /*
138 * newer OF puts I2C devices under 'i2c-bus' instead of attaching them
139 * directly to the ki2c node so we just check if we have a child named
140 * 'i2c-bus' and if so we attach its children, not ours
141 *
142 * XXX
143 * should probably check for multiple i2c-bus children
144 */
145
146 int found_busnode = 0;
147 channel = 0;
148 child = OF_child(node);
149 while (child != 0) {
150 OF_getprop(child, "name", name, sizeof(name));
151 if (strcmp(name, "i2c-bus") == 0) {
152 OF_getprop(child, "reg", &channel, sizeof(channel));
153 i2cbus[channel] = child;
154 DPRINTF("found channel %x\n", channel);
155 found_busnode = 1;
156 }
157 child = OF_peer(child);
158 }
159 if (found_busnode == 0)
160 i2cbus[0] = node;
161
162 for (channel = 0; channel < 2; channel++) {
163 devs = OF_child(i2cbus[channel]);
164 while (devs != 0) {
165 if (OF_getprop(devs, "name", name, 32) <= 0)
166 goto skip;
167 if (OF_getprop(devs, "compatible", compat, 256) <= 0) {
168 /* some i2c device nodes don't have 'compatible' */
169 memset(compat, 0, 256);
170 strncpy(compat, name, 256);
171 }
172 if (OF_getprop(devs, "reg", &addr, 4) <= 0)
173 if (OF_getprop(devs, "i2c-address", &addr, 4) <= 0)
174 goto skip;
175 addr |= channel << 8;
176 addr = addr >> 1;
177 DPRINTF("-> %s@%x\n", name, addr);
178 dev = prop_dictionary_create();
179 prop_dictionary_set_string(dev, "name", name);
180 data = prop_data_create_copy(compat, strlen(compat)+1);
181 prop_dictionary_set(dev, "compatible", data);
182 prop_object_release(data);
183 prop_dictionary_set_uint32(dev, "addr", addr);
184 prop_dictionary_set_uint64(dev, "cookie", devs);
185 /* look for location info for sensors */
186 devc = OF_child(devs);
187 if (devc == 0) {
188 /* old style name info */
189 uint32_t ids[4];
190 int len = OF_getprop(devs, "hwsensor-id", ids, 16);
191 int i = 0, idx = 0;
192 char buffer[256];
193 memset(buffer, 0, 256);
194 OF_getprop(devs, "hwsensor-location", buffer, 256);
195 while (len > 0) {
196 reg = ids[i];
197 strcpy(descr, &buffer[idx]);
198 idx += strlen(descr) + 1;
199 DPRINTF("found '%s' at %02x\n", descr, reg);
200 snprintf(num, 7, "s%02x", i);
201 prop_dictionary_set_string(dev, num, descr);
202 i++;
203 len -= 4;
204 }
205 } else {
206 while (devc != 0) {
207 if (OF_getprop(devc, "reg", ®, 4) < 4) goto nope;
208 if (OF_getprop(devc, "location", descr, 32) <= 0)
209 goto nope;
210 }
211 DPRINTF("found '%s' at %02x\n", descr, reg);
212 snprintf(num, 7, "s%02x", reg);
213 prop_dictionary_set_string(dev, num, descr);
214 nope:
215 devc = OF_peer(devc);
216 }
217
218 prop_array_add(cfg, dev);
219 prop_object_release(dev);
220 skip:
221 devs = OF_peer(devs);
222 }
223 }
224
225 /* fill in the i2c tag */
226 iic_tag_init(&sc->sc_i2c);
227 sc->sc_i2c.ic_cookie = sc;
228 sc->sc_i2c.ic_exec = ki2c_i2c_exec;
229
230 memset(&iba, 0, sizeof(iba));
231 iba.iba_tag = &sc->sc_i2c;
232 config_found(sc->sc_dev, &iba, iicbus_print, CFARGS_NONE);
233
234 }
235
236 uint8_t
237 ki2c_readreg(struct ki2c_softc *sc, int reg)
238 {
239
240 return bus_space_read_1(sc->sc_tag, sc->sc_bh, sc->sc_regstep * reg);
241 }
242
243 void
244 ki2c_writereg(struct ki2c_softc *sc, int reg, uint8_t val)
245 {
246
247 bus_space_write_1(sc->sc_tag, sc->sc_bh, reg * sc->sc_regstep, val);
248 delay(10);
249 }
250
251 u_int
252 ki2c_getmode(struct ki2c_softc *sc)
253 {
254 return ki2c_readreg(sc, MODE) & I2C_MODE;
255 }
256
257 void
258 ki2c_setmode(struct ki2c_softc *sc, u_int mode)
259 {
260 ki2c_writereg(sc, MODE, mode);
261 }
262
263 u_int
264 ki2c_getspeed(struct ki2c_softc *sc)
265 {
266 return ki2c_readreg(sc, MODE) & I2C_SPEED;
267 }
268
269 void
270 ki2c_setspeed(struct ki2c_softc *sc, u_int speed)
271 {
272 u_int x;
273
274 KASSERT((speed & ~I2C_SPEED) == 0);
275 x = ki2c_readreg(sc, MODE);
276 x &= ~I2C_SPEED;
277 x |= speed;
278 ki2c_writereg(sc, MODE, x);
279 }
280
281 int
282 ki2c_intr(struct ki2c_softc *sc)
283 {
284 u_int isr, x;
285
286 isr = ki2c_readreg(sc, ISR);
287 if (isr & I2C_INT_ADDR) {
288 #if 0
289 if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
290 /* No slave responded. */
291 sc->sc_flags |= I2C_ERROR;
292 goto out;
293 }
294 #endif
295
296 if (sc->sc_flags & I2C_READING) {
297 if (sc->sc_resid > 1) {
298 x = ki2c_readreg(sc, CONTROL);
299 x |= I2C_CT_AAK;
300 ki2c_writereg(sc, CONTROL, x);
301 }
302 } else {
303 ki2c_writereg(sc, DATA, *sc->sc_data++);
304 sc->sc_resid--;
305 }
306 }
307
308 if (isr & I2C_INT_DATA) {
309 if (sc->sc_flags & I2C_READING) {
310 *sc->sc_data++ = ki2c_readreg(sc, DATA);
311 sc->sc_resid--;
312
313 if (sc->sc_resid == 0) { /* Completed */
314 ki2c_writereg(sc, CONTROL, 0);
315 goto out;
316 }
317 } else {
318 #if 0
319 if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
320 /* No slave responded. */
321 sc->sc_flags |= I2C_ERROR;
322 goto out;
323 }
324 #endif
325
326 if (sc->sc_resid == 0) {
327 x = ki2c_readreg(sc, CONTROL) | I2C_CT_STOP;
328 ki2c_writereg(sc, CONTROL, x);
329 } else {
330 ki2c_writereg(sc, DATA, *sc->sc_data++);
331 sc->sc_resid--;
332 }
333 }
334 }
335
336 out:
337 if (isr & I2C_INT_STOP) {
338 ki2c_writereg(sc, CONTROL, 0);
339 sc->sc_flags &= ~I2C_BUSY;
340 }
341
342 ki2c_writereg(sc, ISR, isr);
343
344 return 1;
345 }
346
347 int
348 ki2c_poll(struct ki2c_softc *sc, int timo)
349 {
350 while (sc->sc_flags & I2C_BUSY) {
351 if (ki2c_readreg(sc, ISR))
352 ki2c_intr(sc);
353 timo -= 100;
354 if (timo < 0) {
355 DPRINTF("i2c_poll: timeout\n");
356 return -1;
357 }
358 delay(100);
359 }
360 return 0;
361 }
362
363 int
364 ki2c_start(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
365 {
366 int rw = (sc->sc_flags & I2C_READING) ? 1 : 0;
367 int timo, x;
368
369 KASSERT((addr & 1) == 0);
370
371 sc->sc_data = data;
372 sc->sc_resid = len;
373 sc->sc_flags |= I2C_BUSY;
374
375 timo = 1000 + len * 200;
376
377 /* XXX TAS3001 sometimes takes 50ms to finish writing registers. */
378 /* if (addr == 0x68) */
379 timo += 100000;
380
381 ki2c_writereg(sc, ADDR, addr | rw);
382 ki2c_writereg(sc, SUBADDR, subaddr);
383
384 x = ki2c_readreg(sc, CONTROL) | I2C_CT_ADDR;
385 ki2c_writereg(sc, CONTROL, x);
386
387 if (ki2c_poll(sc, timo))
388 return -1;
389 if (sc->sc_flags & I2C_ERROR) {
390 DPRINTF("I2C_ERROR\n");
391 return -1;
392 }
393 return 0;
394 }
395
396 int
397 ki2c_read(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
398 {
399 sc->sc_flags = I2C_READING;
400 DPRINTF("ki2c_read: %02x %d\n", addr, len);
401 return ki2c_start(sc, addr, subaddr, data, len);
402 }
403
404 int
405 ki2c_write(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
406 {
407 sc->sc_flags = 0;
408 DPRINTF("ki2c_write: %02x %d\n",addr,len);
409 return ki2c_start(sc, addr, subaddr, data, len);
410 }
411
412 int
413 ki2c_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
414 size_t cmdlen, void *vbuf, size_t buflen, int flags)
415 {
416 struct ki2c_softc *sc = cookie;
417 int i;
418 size_t w_len;
419 uint8_t *wp;
420 uint8_t wrbuf[KI2C_EXEC_MAX_CMDLEN + KI2C_EXEC_MAX_CMDLEN];
421 uint8_t channel;
422
423 /*
424 * We don't have any idea if the ki2c controller can execute
425 * i2c quick_{read,write} operations, so if someone tries one,
426 * return an error.
427 */
428 if (cmdlen == 0 && buflen == 0)
429 return -1;
430
431 /*
432 * Transaction could be much larger now. Bail if it exceeds our
433 * small combining buffer, we don't expect such devices.
434 */
435 if (cmdlen + buflen > sizeof(wrbuf))
436 return -1;
437
438 channel = (addr & 0xf80) ? 0x10 : 0x00;
439 addr &= 0x7f;
440
441
442 /* we handle the subaddress stuff ourselves */
443 ki2c_setmode(sc, channel | I2C_STDMODE);
444 ki2c_setspeed(sc, I2C_50kHz);
445
446 /* Write-buffer defaults to vcmd */
447 wp = (uint8_t *)(__UNCONST(vcmd));
448 w_len = cmdlen;
449
450 /*
451 * Concatenate vcmd and vbuf for write operations
452 *
453 * Drivers written specifically for ki2c might already do this,
454 * but "generic" i2c drivers still provide separate arguments
455 * for the cmd and buf parts of iic_smbus_write_{byte,word}.
456 */
457 if (I2C_OP_WRITE_P(op) && buflen != 0) {
458 if (cmdlen == 0) {
459 wp = (uint8_t *)vbuf;
460 w_len = buflen;
461 } else {
462 KASSERT((cmdlen + buflen) <= sizeof(wrbuf));
463 wp = (uint8_t *)(__UNCONST(vcmd));
464 w_len = 0;
465 for (i = 0; i < cmdlen; i++)
466 wrbuf[w_len++] = *wp++;
467 wp = (uint8_t *)vbuf;
468 for (i = 0; i < buflen; i++)
469 wrbuf[w_len++] = *wp++;
470 wp = wrbuf;
471 }
472 }
473
474 if (w_len > 0)
475 if (ki2c_write(sc, addr << 1, 0, wp, w_len) !=0 )
476 return -1;
477
478 if (I2C_OP_READ_P(op)) {
479 if (ki2c_read(sc, addr << 1, 0, vbuf, buflen) !=0 )
480 return -1;
481 }
482 return 0;
483 }
484