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