xref: /netbsd-src/sys/arch/macppc/dev/ki2c.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: ki2c.c,v 1.11 2007/12/06 17:00:33 ad 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 CFATTACH_DECL(ki2c, sizeof(struct ki2c_softc), ki2c_match, ki2c_attach,
64 	NULL, NULL);
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 	mutex_init(&sc->sc_buslock, MUTEX_DEFAULT, IPL_NONE);
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 		int ok = 0;
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 		ok = OF_getprop(child, "reg", reg, sizeof(reg));
165 		if (ok <= 0) {
166 			ok = OF_getprop(child, "i2c-address", reg,
167 			    sizeof(reg));
168 		}
169 		if (ok > 0) {
170 			ka.ka_addr = reg[0];
171 			ka.ka_tag = &sc->sc_i2c;
172 			config_found_ia(self, "ki2c", &ka, ki2c_print);
173 		}
174 #ifdef DIAGNOSTIC
175 		else {
176 			printf("%s: device (%s) has no reg or i2c-address property.\n",
177 			    sc->sc_dev.dv_xname, name);
178 		}
179 #endif
180 	}
181 }
182 
183 int
184 ki2c_print(aux, ki2c)
185 	void *aux;
186 	const char *ki2c;
187 {
188 	struct ki2c_confargs *ka = aux;
189 
190 	if (ki2c) {
191 		aprint_normal("%s at %s", ka->ka_name, ki2c);
192 		aprint_normal(" address 0x%x", ka->ka_addr);
193 	}
194 	return UNCONF;
195 }
196 
197 u_int
198 ki2c_readreg(sc, reg)
199 	struct ki2c_softc *sc;
200 	int reg;
201 {
202 	u_char *addr = sc->sc_reg + sc->sc_regstep * reg;
203 
204 	return *addr;
205 }
206 
207 void
208 ki2c_writereg(sc, reg, val)
209 	struct ki2c_softc *sc;
210 	int reg;
211 	u_int val;
212 {
213 	u_char *addr = sc->sc_reg + sc->sc_regstep * reg;
214 
215 	*addr = val;
216 	__asm volatile ("eieio");
217 	delay(10);
218 }
219 
220 u_int
221 ki2c_getmode(sc)
222 	struct ki2c_softc *sc;
223 {
224 	return ki2c_readreg(sc, MODE) & I2C_MODE;
225 }
226 
227 void
228 ki2c_setmode(sc, mode)
229 	struct ki2c_softc *sc;
230 	u_int mode;
231 {
232 	u_int x;
233 
234 	KASSERT((mode & ~I2C_MODE) == 0);
235 	x = ki2c_readreg(sc, MODE);
236 	x &= ~I2C_MODE;
237 	x |= mode;
238 	ki2c_writereg(sc, MODE, x);
239 }
240 
241 u_int
242 ki2c_getspeed(sc)
243 	struct ki2c_softc *sc;
244 {
245 	return ki2c_readreg(sc, MODE) & I2C_SPEED;
246 }
247 
248 void
249 ki2c_setspeed(sc, speed)
250 	struct ki2c_softc *sc;
251 	u_int speed;
252 {
253 	u_int x;
254 
255 	KASSERT((speed & ~I2C_SPEED) == 0);
256 	x = ki2c_readreg(sc, MODE);
257 	x &= ~I2C_SPEED;
258 	x |= speed;
259 	ki2c_writereg(sc, MODE, x);
260 }
261 
262 int
263 ki2c_intr(sc)
264 	struct ki2c_softc *sc;
265 {
266 	u_int isr, x;
267 
268 	isr = ki2c_readreg(sc, ISR);
269 	if (isr & I2C_INT_ADDR) {
270 #if 0
271 		if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
272 			/* No slave responded. */
273 			sc->sc_flags |= I2C_ERROR;
274 			goto out;
275 		}
276 #endif
277 
278 		if (sc->sc_flags & I2C_READING) {
279 			if (sc->sc_resid > 1) {
280 				x = ki2c_readreg(sc, CONTROL);
281 				x |= I2C_CT_AAK;
282 				ki2c_writereg(sc, CONTROL, x);
283 			}
284 		} else {
285 			ki2c_writereg(sc, DATA, *sc->sc_data++);
286 			sc->sc_resid--;
287 		}
288 	}
289 
290 	if (isr & I2C_INT_DATA) {
291 		if (sc->sc_flags & I2C_READING) {
292 			*sc->sc_data++ = ki2c_readreg(sc, DATA);
293 			sc->sc_resid--;
294 
295 			if (sc->sc_resid == 0) {	/* Completed */
296 				ki2c_writereg(sc, CONTROL, 0);
297 				goto out;
298 			}
299 		} else {
300 #if 0
301 			if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
302 				/* No slave responded. */
303 				sc->sc_flags |= I2C_ERROR;
304 				goto out;
305 			}
306 #endif
307 
308 			if (sc->sc_resid == 0) {
309 				x = ki2c_readreg(sc, CONTROL) | I2C_CT_STOP;
310 				ki2c_writereg(sc, CONTROL, x);
311 			} else {
312 				ki2c_writereg(sc, DATA, *sc->sc_data++);
313 				sc->sc_resid--;
314 			}
315 		}
316 	}
317 
318 out:
319 	if (isr & I2C_INT_STOP) {
320 		ki2c_writereg(sc, CONTROL, 0);
321 		sc->sc_flags &= ~I2C_BUSY;
322 	}
323 
324 	ki2c_writereg(sc, ISR, isr);
325 
326 	return 1;
327 }
328 
329 int
330 ki2c_poll(sc, timo)
331 	struct ki2c_softc *sc;
332 	int timo;
333 {
334 	while (sc->sc_flags & I2C_BUSY) {
335 		if (ki2c_readreg(sc, ISR))
336 			ki2c_intr(sc);
337 		timo -= 100;
338 		if (timo < 0) {
339 			printf("i2c_poll: timeout\n");
340 			return -1;
341 		}
342 		delay(100);
343 	}
344 	return 0;
345 }
346 
347 int
348 ki2c_start(sc, addr, subaddr, data, len)
349 	struct ki2c_softc *sc;
350 	int addr, subaddr, len;
351 	void *data;
352 {
353 	int rw = (sc->sc_flags & I2C_READING) ? 1 : 0;
354 	int timo, x;
355 
356 	KASSERT((addr & 1) == 0);
357 
358 	sc->sc_data = data;
359 	sc->sc_resid = len;
360 	sc->sc_flags |= I2C_BUSY;
361 
362 	timo = 1000 + len * 200;
363 
364 	/* XXX TAS3001 sometimes takes 50ms to finish writing registers. */
365 	/* if (addr == 0x68) */
366 		timo += 100000;
367 
368 	ki2c_writereg(sc, ADDR, addr | rw);
369 	ki2c_writereg(sc, SUBADDR, subaddr);
370 
371 	x = ki2c_readreg(sc, CONTROL) | I2C_CT_ADDR;
372 	ki2c_writereg(sc, CONTROL, x);
373 
374 	if (ki2c_poll(sc, timo))
375 		return -1;
376 	if (sc->sc_flags & I2C_ERROR) {
377 		printf("I2C_ERROR\n");
378 		return -1;
379 	}
380 	return 0;
381 }
382 
383 int
384 ki2c_read(sc, addr, subaddr, data, len)
385 	struct ki2c_softc *sc;
386 	int addr, subaddr, len;
387 	void *data;
388 {
389 	sc->sc_flags = I2C_READING;
390 	#ifdef KI2C_DEBUG
391 		printf("ki2c_read: %02x %d\n", addr, len);
392 	#endif
393 	return ki2c_start(sc, addr, subaddr, data, len);
394 }
395 
396 int
397 ki2c_write(sc, addr, subaddr, data, len)
398 	struct ki2c_softc *sc;
399 	int addr, subaddr, len;
400 	void *data;
401 {
402 	sc->sc_flags = 0;
403 	#ifdef KI2C_DEBUG
404 		printf("ki2c_write: %02x %d\n",addr,len);
405 	#endif
406 	return ki2c_start(sc, addr, subaddr, data, len);
407 }
408 
409 static int
410 ki2c_i2c_acquire_bus(void *cookie, int flags)
411 {
412 	struct ki2c_softc *sc = cookie;
413 
414 	mutex_enter(&sc->sc_buslock);
415 	return 0;
416 }
417 
418 static void
419 ki2c_i2c_release_bus(void *cookie, int flags)
420 {
421 	struct ki2c_softc *sc = cookie;
422 
423 	mutex_exit(&sc->sc_buslock);
424 }
425 
426 int
427 ki2c_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
428     size_t cmdlen, void *vbuf, size_t buflen, int flags)
429 {
430 	struct ki2c_softc *sc = cookie;
431 
432 	/* we handle the subaddress stuff ourselves */
433 	ki2c_setmode(sc, I2C_STDMODE);
434 
435 	if (ki2c_write(sc, addr, 0, __UNCONST(vcmd), cmdlen) !=0 )
436 		return -1;
437 
438 	if (I2C_OP_READ_P(op)) {
439 		if (ki2c_read(sc, addr, 0, vbuf, buflen) !=0 )
440 			return -1;
441 	}
442 	return 0;
443 }
444