ki2c.c revision 1.3 1 /* $NetBSD: ki2c.c,v 1.3 2005/08/10 14:26:46 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
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;
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_name = "iic";
135 iba.iba_tag = &sc->sc_i2c;
136 (void) config_found(&sc->sc_dev, &iba, iicbus_print);
137
138
139 for (child = OF_child(node); child; child = OF_peer(child)) {
140 namelen = OF_getprop(child, "name", name, sizeof(name));
141 if (namelen < 0)
142 continue;
143 if (namelen >= sizeof(name))
144 continue;
145
146 name[namelen] = 0;
147 ka.ka_name = name;
148 ka.ka_node = child;
149 if (OF_getprop(child, "reg", reg, sizeof(reg))>0) {
150 ka.ka_addr = reg[0];
151 ka.ka_tag = &sc->sc_i2c;
152 config_found(self, &ka, ki2c_print);
153 }
154 }
155 }
156
157 int
158 ki2c_print(aux, ki2c)
159 void *aux;
160 const char *ki2c;
161 {
162 struct ki2c_confargs *ka = aux;
163
164 if (ki2c) {
165 aprint_normal("%s at %s", ka->ka_name, ki2c);
166 aprint_normal(" address 0x%x", ka->ka_addr);
167 }
168 return UNCONF;
169 }
170
171 u_int
172 ki2c_readreg(sc, reg)
173 struct ki2c_softc *sc;
174 int reg;
175 {
176 u_char *addr = sc->sc_reg + sc->sc_regstep * reg;
177
178 return *addr;
179 }
180
181 void
182 ki2c_writereg(sc, reg, val)
183 struct ki2c_softc *sc;
184 int reg;
185 u_int val;
186 {
187 u_char *addr = sc->sc_reg + sc->sc_regstep * reg;
188
189 *addr = val;
190 asm volatile ("eieio");
191 delay(10);
192 }
193
194 u_int
195 ki2c_getmode(sc)
196 struct ki2c_softc *sc;
197 {
198 return ki2c_readreg(sc, MODE) & I2C_MODE;
199 }
200
201 void
202 ki2c_setmode(sc, mode)
203 struct ki2c_softc *sc;
204 u_int mode;
205 {
206 u_int x;
207
208 KASSERT((mode & ~I2C_MODE) == 0);
209 x = ki2c_readreg(sc, MODE);
210 x &= ~I2C_MODE;
211 x |= mode;
212 ki2c_writereg(sc, MODE, x);
213 }
214
215 u_int
216 ki2c_getspeed(sc)
217 struct ki2c_softc *sc;
218 {
219 return ki2c_readreg(sc, MODE) & I2C_SPEED;
220 }
221
222 void
223 ki2c_setspeed(sc, speed)
224 struct ki2c_softc *sc;
225 u_int speed;
226 {
227 u_int x;
228
229 KASSERT((speed & ~I2C_SPEED) == 0);
230 x = ki2c_readreg(sc, MODE);
231 x &= ~I2C_SPEED;
232 x |= speed;
233 ki2c_writereg(sc, MODE, x);
234 }
235
236 int
237 ki2c_intr(sc)
238 struct ki2c_softc *sc;
239 {
240 u_int isr, x;
241
242 isr = ki2c_readreg(sc, ISR);
243 if (isr & I2C_INT_ADDR) {
244 #if 0
245 if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
246 /* No slave responded. */
247 sc->sc_flags |= I2C_ERROR;
248 goto out;
249 }
250 #endif
251
252 if (sc->sc_flags & I2C_READING) {
253 if (sc->sc_resid > 1) {
254 x = ki2c_readreg(sc, CONTROL);
255 x |= I2C_CT_AAK;
256 ki2c_writereg(sc, CONTROL, x);
257 }
258 } else {
259 ki2c_writereg(sc, DATA, *sc->sc_data++);
260 sc->sc_resid--;
261 }
262 }
263
264 if (isr & I2C_INT_DATA) {
265 if (sc->sc_flags & I2C_READING) {
266 *sc->sc_data++ = ki2c_readreg(sc, DATA);
267 sc->sc_resid--;
268
269 if (sc->sc_resid == 0) { /* Completed */
270 ki2c_writereg(sc, CONTROL, 0);
271 goto out;
272 }
273 } else {
274 #if 0
275 if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
276 /* No slave responded. */
277 sc->sc_flags |= I2C_ERROR;
278 goto out;
279 }
280 #endif
281
282 if (sc->sc_resid == 0) {
283 x = ki2c_readreg(sc, CONTROL) | I2C_CT_STOP;
284 ki2c_writereg(sc, CONTROL, x);
285 } else {
286 ki2c_writereg(sc, DATA, *sc->sc_data++);
287 sc->sc_resid--;
288 }
289 }
290 }
291
292 out:
293 if (isr & I2C_INT_STOP) {
294 ki2c_writereg(sc, CONTROL, 0);
295 sc->sc_flags &= ~I2C_BUSY;
296 }
297
298 ki2c_writereg(sc, ISR, isr);
299
300 return 1;
301 }
302
303 int
304 ki2c_poll(sc, timo)
305 struct ki2c_softc *sc;
306 int timo;
307 {
308 while (sc->sc_flags & I2C_BUSY) {
309 if (ki2c_readreg(sc, ISR))
310 ki2c_intr(sc);
311 timo -= 100;
312 if (timo < 0) {
313 printf("i2c_poll: timeout\n");
314 return -1;
315 }
316 delay(100);
317 }
318 return 0;
319 }
320
321 int
322 ki2c_start(sc, addr, subaddr, data, len)
323 struct ki2c_softc *sc;
324 int addr, subaddr, len;
325 void *data;
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(sc, addr, subaddr, data, len)
359 struct ki2c_softc *sc;
360 int addr, subaddr, len;
361 void *data;
362 {
363 sc->sc_flags = I2C_READING;
364 #ifdef KI2C_DEBUG
365 printf("ki2c_read: %02x %d\n", addr, len);
366 #endif
367 return ki2c_start(sc, addr, subaddr, data, len);
368 }
369
370 int
371 ki2c_write(sc, addr, subaddr, data, len)
372 struct ki2c_softc *sc;
373 int addr, subaddr, len;
374 void *data;
375 {
376 sc->sc_flags = 0;
377 #ifdef KI2C_DEBUG
378 printf("ki2c_write: %02x %d\n",addr,len);
379 #endif
380 return ki2c_start(sc, addr, subaddr, data, len);
381 }
382
383 static int
384 ki2c_i2c_acquire_bus(void *cookie, int flags)
385 {
386 struct ki2c_softc *sc = cookie;
387
388 return (lockmgr(&sc->sc_buslock, LK_EXCLUSIVE, NULL));
389 }
390
391 static void
392 ki2c_i2c_release_bus(void *cookie, int flags)
393 {
394 struct ki2c_softc *sc = cookie;
395
396 (void) lockmgr(&sc->sc_buslock, LK_RELEASE, NULL);
397 }
398
399 int
400 ki2c_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
401 size_t cmdlen, void *vbuf, size_t buflen, int flags)
402 {
403 struct ki2c_softc *sc = cookie;
404
405 /* we handle the subaddress stuff ourselves */
406 ki2c_setmode(sc, I2C_STDMODE);
407
408 if (ki2c_write(sc, addr, 0, __UNCONST(vcmd), cmdlen) !=0 )
409 return -1;
410 if (I2C_OP_READ_P(op))
411 {
412 if (ki2c_read(sc, addr, 0, vbuf, buflen) !=0 )
413 return -1;
414 }
415 return 0;
416 }
417