ki2c.c revision 1.8.18.1 1 /* $NetBSD: ki2c.c,v 1.8.18.1 2007/12/09 16:03:55 reinoud 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 <uvm/uvm_extern.h>
37 #include <machine/autoconf.h>
38
39 #include <macppc/dev/ki2cvar.h>
40
41 int ki2c_match(struct device *, struct cfdata *, void *);
42 void ki2c_attach(struct device *, struct device *, void *);
43 inline u_int ki2c_readreg(struct ki2c_softc *, int);
44 inline void ki2c_writereg(struct ki2c_softc *, int, u_int);
45 u_int ki2c_getmode(struct ki2c_softc *);
46 void ki2c_setmode(struct ki2c_softc *, u_int);
47 u_int ki2c_getspeed(struct ki2c_softc *);
48 void ki2c_setspeed(struct ki2c_softc *, u_int);
49 int ki2c_intr(struct ki2c_softc *);
50 int ki2c_poll(struct ki2c_softc *, int);
51 int ki2c_start(struct ki2c_softc *, int, int, void *, int);
52 int ki2c_read(struct ki2c_softc *, int, int, void *, int);
53 int ki2c_write(struct ki2c_softc *, int, int, void *, int);
54 int ki2c_print __P((void *, const char *));
55
56 /* I2C glue */
57 static int ki2c_i2c_acquire_bus(void *, int);
58 static void ki2c_i2c_release_bus(void *, int);
59 static int ki2c_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
60 void *, size_t, int);
61
62
63 struct cfattach ki2c_ca = {
64 "ki2c", {}, sizeof(struct ki2c_softc), ki2c_match, ki2c_attach
65 };
66
67 int
68 ki2c_match(parent, match, aux)
69 struct device *parent;
70 struct cfdata *match;
71 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(parent, self, aux)
83 struct device *parent;
84 struct device *self;
85 void *aux;
86 {
87 struct ki2c_softc *sc = (struct ki2c_softc *)self;
88 struct confargs *ca = aux;
89 int node = ca->ca_node;
90 int rate, child, namelen, i2cbus;
91 struct ki2c_confargs ka;
92 struct i2cbus_attach_args iba;
93
94 char name[32];
95 u_int reg[20];
96
97 ca->ca_reg[0] += ca->ca_baseaddr;
98
99 if (OF_getprop(node, "AAPL,i2c-rate", &rate, 4) != 4) {
100 printf(": cannot get i2c-rate\n");
101 return;
102 }
103 if (OF_getprop(node, "AAPL,address", &sc->sc_reg, 4) != 4) {
104 printf(": unable to find i2c address\n");
105 return;
106 }
107 if (OF_getprop(node, "AAPL,address-step", &sc->sc_regstep, 4) != 4) {
108 printf(": unable to find i2c address step\n");
109 return;
110 }
111
112 printf("\n");
113
114 ki2c_writereg(sc, STATUS, 0);
115 ki2c_writereg(sc, ISR, 0);
116 ki2c_writereg(sc, IER, 0);
117
118 ki2c_setmode(sc, I2C_STDSUBMODE);
119 ki2c_setspeed(sc, I2C_100kHz); /* XXX rate */
120
121 mutex_init(&sc->sc_buslock, MUTEX_DEFAULT, IPL_NONE);
122 ki2c_writereg(sc, IER,I2C_INT_DATA|I2C_INT_ADDR|I2C_INT_STOP);
123
124 /* fill in the i2c tag */
125 sc->sc_i2c.ic_cookie = sc;
126 sc->sc_i2c.ic_acquire_bus = ki2c_i2c_acquire_bus;
127 sc->sc_i2c.ic_release_bus = ki2c_i2c_release_bus;
128 sc->sc_i2c.ic_send_start = NULL;
129 sc->sc_i2c.ic_send_stop = NULL;
130 sc->sc_i2c.ic_initiate_xfer = NULL;
131 sc->sc_i2c.ic_read_byte = NULL;
132 sc->sc_i2c.ic_write_byte = NULL;
133 sc->sc_i2c.ic_exec = ki2c_i2c_exec;
134
135 iba.iba_tag = &sc->sc_i2c;
136 (void) config_found_ia(&sc->sc_dev, "i2cbus", &iba, iicbus_print);
137
138 /*
139 * newer OF puts I2C devices under 'i2c-bus' instead of attaching them
140 * directly to the ki2c node so we just check if we have a child named
141 * 'i2c-bus' and if so we attach its children, not ours
142 */
143 i2cbus = 0;
144 child = OF_child(node);
145 while ((child != 0) && (i2cbus == 0)) {
146 OF_getprop(child, "name", name, sizeof(name));
147 if (strcmp(name, "i2c-bus") == 0)
148 i2cbus = child;
149 child = OF_peer(child);
150 }
151 if (i2cbus == 0)
152 i2cbus = node;
153
154 for (child = OF_child(i2cbus); child; child = OF_peer(child)) {
155 namelen = OF_getprop(child, "name", name, sizeof(name));
156 if (namelen < 0)
157 continue;
158 if (namelen >= sizeof(name))
159 continue;
160
161 name[namelen] = 0;
162 ka.ka_name = name;
163 ka.ka_node = child;
164 if (OF_getprop(child, "reg", reg, sizeof(reg))>0) {
165 ka.ka_addr = reg[0];
166 ka.ka_tag = &sc->sc_i2c;
167 config_found_ia(self, "ki2c", &ka, ki2c_print);
168 }
169 }
170 }
171
172 int
173 ki2c_print(aux, ki2c)
174 void *aux;
175 const char *ki2c;
176 {
177 struct ki2c_confargs *ka = aux;
178
179 if (ki2c) {
180 aprint_normal("%s at %s", ka->ka_name, ki2c);
181 aprint_normal(" address 0x%x", ka->ka_addr);
182 }
183 return UNCONF;
184 }
185
186 u_int
187 ki2c_readreg(sc, reg)
188 struct ki2c_softc *sc;
189 int reg;
190 {
191 u_char *addr = sc->sc_reg + sc->sc_regstep * reg;
192
193 return *addr;
194 }
195
196 void
197 ki2c_writereg(sc, reg, val)
198 struct ki2c_softc *sc;
199 int reg;
200 u_int val;
201 {
202 u_char *addr = sc->sc_reg + sc->sc_regstep * reg;
203
204 *addr = val;
205 __asm volatile ("eieio");
206 delay(10);
207 }
208
209 u_int
210 ki2c_getmode(sc)
211 struct ki2c_softc *sc;
212 {
213 return ki2c_readreg(sc, MODE) & I2C_MODE;
214 }
215
216 void
217 ki2c_setmode(sc, mode)
218 struct ki2c_softc *sc;
219 u_int mode;
220 {
221 u_int x;
222
223 KASSERT((mode & ~I2C_MODE) == 0);
224 x = ki2c_readreg(sc, MODE);
225 x &= ~I2C_MODE;
226 x |= mode;
227 ki2c_writereg(sc, MODE, x);
228 }
229
230 u_int
231 ki2c_getspeed(sc)
232 struct ki2c_softc *sc;
233 {
234 return ki2c_readreg(sc, MODE) & I2C_SPEED;
235 }
236
237 void
238 ki2c_setspeed(sc, speed)
239 struct ki2c_softc *sc;
240 u_int speed;
241 {
242 u_int x;
243
244 KASSERT((speed & ~I2C_SPEED) == 0);
245 x = ki2c_readreg(sc, MODE);
246 x &= ~I2C_SPEED;
247 x |= speed;
248 ki2c_writereg(sc, MODE, x);
249 }
250
251 int
252 ki2c_intr(sc)
253 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(sc, timo)
320 struct ki2c_softc *sc;
321 int timo;
322 {
323 while (sc->sc_flags & I2C_BUSY) {
324 if (ki2c_readreg(sc, ISR))
325 ki2c_intr(sc);
326 timo -= 100;
327 if (timo < 0) {
328 printf("i2c_poll: timeout\n");
329 return -1;
330 }
331 delay(100);
332 }
333 return 0;
334 }
335
336 int
337 ki2c_start(sc, addr, subaddr, data, len)
338 struct ki2c_softc *sc;
339 int addr, subaddr, len;
340 void *data;
341 {
342 int rw = (sc->sc_flags & I2C_READING) ? 1 : 0;
343 int timo, x;
344
345 KASSERT((addr & 1) == 0);
346
347 sc->sc_data = data;
348 sc->sc_resid = len;
349 sc->sc_flags |= I2C_BUSY;
350
351 timo = 1000 + len * 200;
352
353 /* XXX TAS3001 sometimes takes 50ms to finish writing registers. */
354 /* if (addr == 0x68) */
355 timo += 100000;
356
357 ki2c_writereg(sc, ADDR, addr | rw);
358 ki2c_writereg(sc, SUBADDR, subaddr);
359
360 x = ki2c_readreg(sc, CONTROL) | I2C_CT_ADDR;
361 ki2c_writereg(sc, CONTROL, x);
362
363 if (ki2c_poll(sc, timo))
364 return -1;
365 if (sc->sc_flags & I2C_ERROR) {
366 printf("I2C_ERROR\n");
367 return -1;
368 }
369 return 0;
370 }
371
372 int
373 ki2c_read(sc, addr, subaddr, data, len)
374 struct ki2c_softc *sc;
375 int addr, subaddr, len;
376 void *data;
377 {
378 sc->sc_flags = I2C_READING;
379 #ifdef KI2C_DEBUG
380 printf("ki2c_read: %02x %d\n", addr, len);
381 #endif
382 return ki2c_start(sc, addr, subaddr, data, len);
383 }
384
385 int
386 ki2c_write(sc, addr, subaddr, data, len)
387 struct ki2c_softc *sc;
388 int addr, subaddr, len;
389 void *data;
390 {
391 sc->sc_flags = 0;
392 #ifdef KI2C_DEBUG
393 printf("ki2c_write: %02x %d\n",addr,len);
394 #endif
395 return ki2c_start(sc, addr, subaddr, data, len);
396 }
397
398 static int
399 ki2c_i2c_acquire_bus(void *cookie, int flags)
400 {
401 struct ki2c_softc *sc = cookie;
402
403 mutex_enter(&sc->sc_buslock);
404 return 0;
405 }
406
407 static void
408 ki2c_i2c_release_bus(void *cookie, int flags)
409 {
410 struct ki2c_softc *sc = cookie;
411
412 mutex_exit(&sc->sc_buslock);
413 }
414
415 int
416 ki2c_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
417 size_t cmdlen, void *vbuf, size_t buflen, int flags)
418 {
419 struct ki2c_softc *sc = cookie;
420
421 /* we handle the subaddress stuff ourselves */
422 ki2c_setmode(sc, I2C_STDMODE);
423
424 if (ki2c_write(sc, addr, 0, __UNCONST(vcmd), cmdlen) !=0 )
425 return -1;
426 if (I2C_OP_READ_P(op))
427 {
428 if (ki2c_read(sc, addr, 0, vbuf, buflen) !=0 )
429 return -1;
430 }
431 return 0;
432 }
433