xref: /netbsd-src/sys/arch/macppc/dev/ki2c.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: ki2c.c,v 1.5 2005/12/11 12:18:03 christos 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_name = "iic";
135 	iba.iba_tag = &sc->sc_i2c;
136 	(void) config_found(&sc->sc_dev, &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(self, &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 	return (lockmgr(&sc->sc_buslock, LK_EXCLUSIVE, NULL));
404 }
405 
406 static void
407 ki2c_i2c_release_bus(void *cookie, int flags)
408 {
409 	struct ki2c_softc *sc = cookie;
410 
411 	(void) lockmgr(&sc->sc_buslock, LK_RELEASE, NULL);
412 }
413 
414 int
415 ki2c_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
416     size_t cmdlen, void *vbuf, size_t buflen, int flags)
417 {
418 	struct ki2c_softc *sc = cookie;
419 
420 	/* we handle the subaddress stuff ourselves */
421 	ki2c_setmode(sc, I2C_STDMODE);
422 
423 	if (ki2c_write(sc, addr, 0, __UNCONST(vcmd), cmdlen) !=0 )
424 		return -1;
425 	if (I2C_OP_READ_P(op))
426 	{
427 		if (ki2c_read(sc, addr, 0, vbuf, buflen) !=0 )
428 			return -1;
429 	}
430 	return 0;
431 }
432