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