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