ki2c.c revision 1.10 1 /* $NetBSD: ki2c.c,v 1.10 2007/10/17 19:55:18 garbled 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
34 #include <dev/ofw/openfirm.h>
35 #include <uvm/uvm_extern.h>
36 #include <machine/autoconf.h>
37
38 #include <macppc/dev/ki2cvar.h>
39
40 int ki2c_match(struct device *, struct cfdata *, void *);
41 void ki2c_attach(struct device *, struct device *, void *);
42 inline u_int ki2c_readreg(struct ki2c_softc *, int);
43 inline void ki2c_writereg(struct ki2c_softc *, int, u_int);
44 u_int ki2c_getmode(struct ki2c_softc *);
45 void ki2c_setmode(struct ki2c_softc *, u_int);
46 u_int ki2c_getspeed(struct ki2c_softc *);
47 void ki2c_setspeed(struct ki2c_softc *, u_int);
48 int ki2c_intr(struct ki2c_softc *);
49 int ki2c_poll(struct ki2c_softc *, int);
50 int ki2c_start(struct ki2c_softc *, int, int, void *, int);
51 int ki2c_read(struct ki2c_softc *, int, int, void *, int);
52 int ki2c_write(struct ki2c_softc *, int, int, void *, int);
53 int ki2c_print __P((void *, const char *));
54
55 /* I2C glue */
56 static int ki2c_i2c_acquire_bus(void *, int);
57 static void ki2c_i2c_release_bus(void *, int);
58 static int ki2c_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
59 void *, size_t, int);
60
61
62 CFATTACH_DECL(ki2c, sizeof(struct ki2c_softc), ki2c_match, ki2c_attach,
63 NULL, NULL);
64
65 int
66 ki2c_match(parent, match, aux)
67 struct device *parent;
68 struct cfdata *match;
69 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(parent, self, aux)
81 struct device *parent;
82 struct device *self;
83 void *aux;
84 {
85 struct ki2c_softc *sc = (struct ki2c_softc *)self;
86 struct confargs *ca = aux;
87 int node = ca->ca_node;
88 int rate, child, namelen, i2cbus;
89 struct ki2c_confargs ka;
90 struct i2cbus_attach_args iba;
91
92 char name[32];
93 u_int reg[20];
94
95 ca->ca_reg[0] += ca->ca_baseaddr;
96
97 if (OF_getprop(node, "AAPL,i2c-rate", &rate, 4) != 4) {
98 printf(": cannot get i2c-rate\n");
99 return;
100 }
101 if (OF_getprop(node, "AAPL,address", &sc->sc_reg, 4) != 4) {
102 printf(": unable to find i2c address\n");
103 return;
104 }
105 if (OF_getprop(node, "AAPL,address-step", &sc->sc_regstep, 4) != 4) {
106 printf(": unable to find i2c address step\n");
107 return;
108 }
109
110 printf("\n");
111
112 ki2c_writereg(sc, STATUS, 0);
113 ki2c_writereg(sc, ISR, 0);
114 ki2c_writereg(sc, IER, 0);
115
116 ki2c_setmode(sc, I2C_STDSUBMODE);
117 ki2c_setspeed(sc, I2C_100kHz); /* XXX rate */
118
119 lockinit(&sc->sc_buslock, PRIBIO|PCATCH, sc->sc_dev.dv_xname, 0, 0);
120 ki2c_writereg(sc, IER,I2C_INT_DATA|I2C_INT_ADDR|I2C_INT_STOP);
121
122 /* fill in the i2c tag */
123 sc->sc_i2c.ic_cookie = sc;
124 sc->sc_i2c.ic_acquire_bus = ki2c_i2c_acquire_bus;
125 sc->sc_i2c.ic_release_bus = ki2c_i2c_release_bus;
126 sc->sc_i2c.ic_send_start = NULL;
127 sc->sc_i2c.ic_send_stop = NULL;
128 sc->sc_i2c.ic_initiate_xfer = NULL;
129 sc->sc_i2c.ic_read_byte = NULL;
130 sc->sc_i2c.ic_write_byte = NULL;
131 sc->sc_i2c.ic_exec = ki2c_i2c_exec;
132
133 iba.iba_tag = &sc->sc_i2c;
134 (void) config_found_ia(&sc->sc_dev, "i2cbus", &iba, iicbus_print);
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 for (child = OF_child(i2cbus); child; child = OF_peer(child)) {
153 int ok = 0;
154 namelen = OF_getprop(child, "name", name, sizeof(name));
155 if (namelen < 0)
156 continue;
157 if (namelen >= sizeof(name))
158 continue;
159
160 name[namelen] = 0;
161 ka.ka_name = name;
162 ka.ka_node = child;
163 ok = OF_getprop(child, "reg", reg, sizeof(reg));
164 if (ok <= 0) {
165 ok = OF_getprop(child, "i2c-address", reg,
166 sizeof(reg));
167 }
168 if (ok > 0) {
169 ka.ka_addr = reg[0];
170 ka.ka_tag = &sc->sc_i2c;
171 config_found_ia(self, "ki2c", &ka, ki2c_print);
172 }
173 #ifdef DIAGNOSTIC
174 else {
175 printf("%s: device (%s) has no reg or i2c-address property.\n",
176 sc->sc_dev.dv_xname, name);
177 }
178 #endif
179 }
180 }
181
182 int
183 ki2c_print(aux, ki2c)
184 void *aux;
185 const char *ki2c;
186 {
187 struct ki2c_confargs *ka = aux;
188
189 if (ki2c) {
190 aprint_normal("%s at %s", ka->ka_name, ki2c);
191 aprint_normal(" address 0x%x", ka->ka_addr);
192 }
193 return UNCONF;
194 }
195
196 u_int
197 ki2c_readreg(sc, reg)
198 struct ki2c_softc *sc;
199 int reg;
200 {
201 u_char *addr = sc->sc_reg + sc->sc_regstep * reg;
202
203 return *addr;
204 }
205
206 void
207 ki2c_writereg(sc, reg, val)
208 struct ki2c_softc *sc;
209 int reg;
210 u_int val;
211 {
212 u_char *addr = sc->sc_reg + sc->sc_regstep * reg;
213
214 *addr = val;
215 __asm volatile ("eieio");
216 delay(10);
217 }
218
219 u_int
220 ki2c_getmode(sc)
221 struct ki2c_softc *sc;
222 {
223 return ki2c_readreg(sc, MODE) & I2C_MODE;
224 }
225
226 void
227 ki2c_setmode(sc, mode)
228 struct ki2c_softc *sc;
229 u_int mode;
230 {
231 u_int x;
232
233 KASSERT((mode & ~I2C_MODE) == 0);
234 x = ki2c_readreg(sc, MODE);
235 x &= ~I2C_MODE;
236 x |= mode;
237 ki2c_writereg(sc, MODE, x);
238 }
239
240 u_int
241 ki2c_getspeed(sc)
242 struct ki2c_softc *sc;
243 {
244 return ki2c_readreg(sc, MODE) & I2C_SPEED;
245 }
246
247 void
248 ki2c_setspeed(sc, speed)
249 struct ki2c_softc *sc;
250 u_int speed;
251 {
252 u_int x;
253
254 KASSERT((speed & ~I2C_SPEED) == 0);
255 x = ki2c_readreg(sc, MODE);
256 x &= ~I2C_SPEED;
257 x |= speed;
258 ki2c_writereg(sc, MODE, x);
259 }
260
261 int
262 ki2c_intr(sc)
263 struct ki2c_softc *sc;
264 {
265 u_int isr, x;
266
267 isr = ki2c_readreg(sc, ISR);
268 if (isr & I2C_INT_ADDR) {
269 #if 0
270 if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
271 /* No slave responded. */
272 sc->sc_flags |= I2C_ERROR;
273 goto out;
274 }
275 #endif
276
277 if (sc->sc_flags & I2C_READING) {
278 if (sc->sc_resid > 1) {
279 x = ki2c_readreg(sc, CONTROL);
280 x |= I2C_CT_AAK;
281 ki2c_writereg(sc, CONTROL, x);
282 }
283 } else {
284 ki2c_writereg(sc, DATA, *sc->sc_data++);
285 sc->sc_resid--;
286 }
287 }
288
289 if (isr & I2C_INT_DATA) {
290 if (sc->sc_flags & I2C_READING) {
291 *sc->sc_data++ = ki2c_readreg(sc, DATA);
292 sc->sc_resid--;
293
294 if (sc->sc_resid == 0) { /* Completed */
295 ki2c_writereg(sc, CONTROL, 0);
296 goto out;
297 }
298 } else {
299 #if 0
300 if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
301 /* No slave responded. */
302 sc->sc_flags |= I2C_ERROR;
303 goto out;
304 }
305 #endif
306
307 if (sc->sc_resid == 0) {
308 x = ki2c_readreg(sc, CONTROL) | I2C_CT_STOP;
309 ki2c_writereg(sc, CONTROL, x);
310 } else {
311 ki2c_writereg(sc, DATA, *sc->sc_data++);
312 sc->sc_resid--;
313 }
314 }
315 }
316
317 out:
318 if (isr & I2C_INT_STOP) {
319 ki2c_writereg(sc, CONTROL, 0);
320 sc->sc_flags &= ~I2C_BUSY;
321 }
322
323 ki2c_writereg(sc, ISR, isr);
324
325 return 1;
326 }
327
328 int
329 ki2c_poll(sc, timo)
330 struct ki2c_softc *sc;
331 int timo;
332 {
333 while (sc->sc_flags & I2C_BUSY) {
334 if (ki2c_readreg(sc, ISR))
335 ki2c_intr(sc);
336 timo -= 100;
337 if (timo < 0) {
338 printf("i2c_poll: timeout\n");
339 return -1;
340 }
341 delay(100);
342 }
343 return 0;
344 }
345
346 int
347 ki2c_start(sc, addr, subaddr, data, len)
348 struct ki2c_softc *sc;
349 int addr, subaddr, len;
350 void *data;
351 {
352 int rw = (sc->sc_flags & I2C_READING) ? 1 : 0;
353 int timo, x;
354
355 KASSERT((addr & 1) == 0);
356
357 sc->sc_data = data;
358 sc->sc_resid = len;
359 sc->sc_flags |= I2C_BUSY;
360
361 timo = 1000 + len * 200;
362
363 /* XXX TAS3001 sometimes takes 50ms to finish writing registers. */
364 /* if (addr == 0x68) */
365 timo += 100000;
366
367 ki2c_writereg(sc, ADDR, addr | rw);
368 ki2c_writereg(sc, SUBADDR, subaddr);
369
370 x = ki2c_readreg(sc, CONTROL) | I2C_CT_ADDR;
371 ki2c_writereg(sc, CONTROL, x);
372
373 if (ki2c_poll(sc, timo))
374 return -1;
375 if (sc->sc_flags & I2C_ERROR) {
376 printf("I2C_ERROR\n");
377 return -1;
378 }
379 return 0;
380 }
381
382 int
383 ki2c_read(sc, addr, subaddr, data, len)
384 struct ki2c_softc *sc;
385 int addr, subaddr, len;
386 void *data;
387 {
388 sc->sc_flags = I2C_READING;
389 #ifdef KI2C_DEBUG
390 printf("ki2c_read: %02x %d\n", addr, len);
391 #endif
392 return ki2c_start(sc, addr, subaddr, data, len);
393 }
394
395 int
396 ki2c_write(sc, addr, subaddr, data, len)
397 struct ki2c_softc *sc;
398 int addr, subaddr, len;
399 void *data;
400 {
401 sc->sc_flags = 0;
402 #ifdef KI2C_DEBUG
403 printf("ki2c_write: %02x %d\n",addr,len);
404 #endif
405 return ki2c_start(sc, addr, subaddr, data, len);
406 }
407
408 static int
409 ki2c_i2c_acquire_bus(void *cookie, int flags)
410 {
411 struct ki2c_softc *sc = cookie;
412
413 return (lockmgr(&sc->sc_buslock, LK_EXCLUSIVE, NULL));
414 }
415
416 static void
417 ki2c_i2c_release_bus(void *cookie, int flags)
418 {
419 struct ki2c_softc *sc = cookie;
420
421 (void) lockmgr(&sc->sc_buslock, LK_RELEASE, NULL);
422 }
423
424 int
425 ki2c_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
426 size_t cmdlen, void *vbuf, size_t buflen, int flags)
427 {
428 struct ki2c_softc *sc = cookie;
429
430 /* we handle the subaddress stuff ourselves */
431 ki2c_setmode(sc, I2C_STDMODE);
432
433 if (ki2c_write(sc, addr, 0, __UNCONST(vcmd), cmdlen) !=0 )
434 return -1;
435
436 if (I2C_OP_READ_P(op)) {
437 if (ki2c_read(sc, addr, 0, vbuf, buflen) !=0 )
438 return -1;
439 }
440 return 0;
441 }
442