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