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