Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / macppc / dev / ki2c.c
blob634c29d2f83952e609ae74a7b7a2ecd51ba13171
1 /* $NetBSD: ki2c.c,v 1.14 2009/03/14 15:36:09 dsl Exp $ */
2 /* Id: ki2c.c,v 1.7 2002/10/05 09:56:05 tsubai Exp */
4 /*-
5 * Copyright (c) 2001 Tsubai Masanari. All rights reserved.
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.
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.
30 #include <sys/param.h>
31 #include <sys/device.h>
32 #include <sys/systm.h>
33 #include <sys/mutex.h>
35 #include <dev/ofw/openfirm.h>
36 #include <uvm/uvm_extern.h>
37 #include <machine/autoconf.h>
39 #include <macppc/dev/ki2cvar.h>
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(void *, const char *);
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);
63 CFATTACH_DECL(ki2c, sizeof(struct ki2c_softc), ki2c_match, ki2c_attach,
64 NULL, NULL);
66 int
67 ki2c_match(struct device *parent, struct cfdata *match, void *aux)
69 struct confargs *ca = aux;
71 if (strcmp(ca->ca_name, "i2c") == 0)
72 return 1;
74 return 0;
77 void
78 ki2c_attach(struct device *parent, struct device *self, void *aux)
80 struct ki2c_softc *sc = (struct ki2c_softc *)self;
81 struct confargs *ca = aux;
82 int node = ca->ca_node;
83 int rate, child, namelen, i2cbus;
84 struct ki2c_confargs ka;
85 struct i2cbus_attach_args iba;
87 char name[32];
88 u_int reg[20];
90 ca->ca_reg[0] += ca->ca_baseaddr;
92 if (OF_getprop(node, "AAPL,i2c-rate", &rate, 4) != 4) {
93 printf(": cannot get i2c-rate\n");
94 return;
96 if (OF_getprop(node, "AAPL,address", &sc->sc_reg, 4) != 4) {
97 printf(": unable to find i2c address\n");
98 return;
100 if (OF_getprop(node, "AAPL,address-step", &sc->sc_regstep, 4) != 4) {
101 printf(": unable to find i2c address step\n");
102 return;
105 printf("\n");
107 ki2c_writereg(sc, STATUS, 0);
108 ki2c_writereg(sc, ISR, 0);
109 ki2c_writereg(sc, IER, 0);
111 ki2c_setmode(sc, I2C_STDSUBMODE);
112 ki2c_setspeed(sc, I2C_100kHz); /* XXX rate */
114 mutex_init(&sc->sc_buslock, MUTEX_DEFAULT, IPL_NONE);
115 ki2c_writereg(sc, IER,I2C_INT_DATA|I2C_INT_ADDR|I2C_INT_STOP);
117 /* fill in the i2c tag */
118 sc->sc_i2c.ic_cookie = sc;
119 sc->sc_i2c.ic_acquire_bus = ki2c_i2c_acquire_bus;
120 sc->sc_i2c.ic_release_bus = ki2c_i2c_release_bus;
121 sc->sc_i2c.ic_send_start = NULL;
122 sc->sc_i2c.ic_send_stop = NULL;
123 sc->sc_i2c.ic_initiate_xfer = NULL;
124 sc->sc_i2c.ic_read_byte = NULL;
125 sc->sc_i2c.ic_write_byte = NULL;
126 sc->sc_i2c.ic_exec = ki2c_i2c_exec;
128 iba.iba_tag = &sc->sc_i2c;
129 (void) config_found_ia(&sc->sc_dev, "i2cbus", &iba, iicbus_print);
132 * newer OF puts I2C devices under 'i2c-bus' instead of attaching them
133 * directly to the ki2c node so we just check if we have a child named
134 * 'i2c-bus' and if so we attach its children, not ours
136 i2cbus = 0;
137 child = OF_child(node);
138 while ((child != 0) && (i2cbus == 0)) {
139 OF_getprop(child, "name", name, sizeof(name));
140 if (strcmp(name, "i2c-bus") == 0)
141 i2cbus = child;
142 child = OF_peer(child);
144 if (i2cbus == 0)
145 i2cbus = node;
147 for (child = OF_child(i2cbus); child; child = OF_peer(child)) {
148 int ok = 0;
149 namelen = OF_getprop(child, "name", name, sizeof(name));
150 if (namelen < 0)
151 continue;
152 if (namelen >= sizeof(name))
153 continue;
155 name[namelen] = 0;
156 ka.ka_name = name;
157 ka.ka_node = child;
158 ok = OF_getprop(child, "reg", reg, sizeof(reg));
159 if (ok <= 0) {
160 ok = OF_getprop(child, "i2c-address", reg,
161 sizeof(reg));
163 if (ok > 0) {
164 ka.ka_addr = reg[0];
165 ka.ka_tag = &sc->sc_i2c;
166 config_found_ia(self, "ki2c", &ka, ki2c_print);
168 #ifdef DIAGNOSTIC
169 else {
170 printf("%s: device (%s) has no reg or i2c-address property.\n",
171 sc->sc_dev.dv_xname, name);
173 #endif
178 ki2c_print(void *aux, const char *ki2c)
180 struct ki2c_confargs *ka = aux;
182 if (ki2c) {
183 aprint_normal("%s at %s", ka->ka_name, ki2c);
184 aprint_normal(" address 0x%x", ka->ka_addr);
186 return UNCONF;
189 u_int
190 ki2c_readreg(struct ki2c_softc *sc, int reg)
192 u_char *addr = sc->sc_reg + sc->sc_regstep * reg;
194 return *addr;
197 void
198 ki2c_writereg(struct ki2c_softc *sc, int reg, u_int val)
200 u_char *addr = sc->sc_reg + sc->sc_regstep * reg;
202 *addr = val;
203 __asm volatile ("eieio");
204 delay(10);
207 u_int
208 ki2c_getmode(struct ki2c_softc *sc)
210 return ki2c_readreg(sc, MODE) & I2C_MODE;
213 void
214 ki2c_setmode(struct ki2c_softc *sc, u_int mode)
216 u_int x;
218 KASSERT((mode & ~I2C_MODE) == 0);
219 x = ki2c_readreg(sc, MODE);
220 x &= ~I2C_MODE;
221 x |= mode;
222 ki2c_writereg(sc, MODE, x);
225 u_int
226 ki2c_getspeed(struct ki2c_softc *sc)
228 return ki2c_readreg(sc, MODE) & I2C_SPEED;
231 void
232 ki2c_setspeed(struct ki2c_softc *sc, u_int speed)
234 u_int x;
236 KASSERT((speed & ~I2C_SPEED) == 0);
237 x = ki2c_readreg(sc, MODE);
238 x &= ~I2C_SPEED;
239 x |= speed;
240 ki2c_writereg(sc, MODE, x);
244 ki2c_intr(struct ki2c_softc *sc)
246 u_int isr, x;
248 isr = ki2c_readreg(sc, ISR);
249 if (isr & I2C_INT_ADDR) {
250 #if 0
251 if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
252 /* No slave responded. */
253 sc->sc_flags |= I2C_ERROR;
254 goto out;
256 #endif
258 if (sc->sc_flags & I2C_READING) {
259 if (sc->sc_resid > 1) {
260 x = ki2c_readreg(sc, CONTROL);
261 x |= I2C_CT_AAK;
262 ki2c_writereg(sc, CONTROL, x);
264 } else {
265 ki2c_writereg(sc, DATA, *sc->sc_data++);
266 sc->sc_resid--;
270 if (isr & I2C_INT_DATA) {
271 if (sc->sc_flags & I2C_READING) {
272 *sc->sc_data++ = ki2c_readreg(sc, DATA);
273 sc->sc_resid--;
275 if (sc->sc_resid == 0) { /* Completed */
276 ki2c_writereg(sc, CONTROL, 0);
277 goto out;
279 } else {
280 #if 0
281 if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
282 /* No slave responded. */
283 sc->sc_flags |= I2C_ERROR;
284 goto out;
286 #endif
288 if (sc->sc_resid == 0) {
289 x = ki2c_readreg(sc, CONTROL) | I2C_CT_STOP;
290 ki2c_writereg(sc, CONTROL, x);
291 } else {
292 ki2c_writereg(sc, DATA, *sc->sc_data++);
293 sc->sc_resid--;
298 out:
299 if (isr & I2C_INT_STOP) {
300 ki2c_writereg(sc, CONTROL, 0);
301 sc->sc_flags &= ~I2C_BUSY;
304 ki2c_writereg(sc, ISR, isr);
306 return 1;
310 ki2c_poll(struct ki2c_softc *sc, int timo)
312 while (sc->sc_flags & I2C_BUSY) {
313 if (ki2c_readreg(sc, ISR))
314 ki2c_intr(sc);
315 timo -= 100;
316 if (timo < 0) {
317 printf("i2c_poll: timeout\n");
318 return -1;
320 delay(100);
322 return 0;
326 ki2c_start(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
328 int rw = (sc->sc_flags & I2C_READING) ? 1 : 0;
329 int timo, x;
331 KASSERT((addr & 1) == 0);
333 sc->sc_data = data;
334 sc->sc_resid = len;
335 sc->sc_flags |= I2C_BUSY;
337 timo = 1000 + len * 200;
339 /* XXX TAS3001 sometimes takes 50ms to finish writing registers. */
340 /* if (addr == 0x68) */
341 timo += 100000;
343 ki2c_writereg(sc, ADDR, addr | rw);
344 ki2c_writereg(sc, SUBADDR, subaddr);
346 x = ki2c_readreg(sc, CONTROL) | I2C_CT_ADDR;
347 ki2c_writereg(sc, CONTROL, x);
349 if (ki2c_poll(sc, timo))
350 return -1;
351 if (sc->sc_flags & I2C_ERROR) {
352 printf("I2C_ERROR\n");
353 return -1;
355 return 0;
359 ki2c_read(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
361 sc->sc_flags = I2C_READING;
362 #ifdef KI2C_DEBUG
363 printf("ki2c_read: %02x %d\n", addr, len);
364 #endif
365 return ki2c_start(sc, addr, subaddr, data, len);
369 ki2c_write(struct ki2c_softc *sc, int addr, int subaddr, void *data, int len)
371 sc->sc_flags = 0;
372 #ifdef KI2C_DEBUG
373 printf("ki2c_write: %02x %d\n",addr,len);
374 #endif
375 return ki2c_start(sc, addr, subaddr, data, len);
378 static int
379 ki2c_i2c_acquire_bus(void *cookie, int flags)
381 struct ki2c_softc *sc = cookie;
383 mutex_enter(&sc->sc_buslock);
384 return 0;
387 static void
388 ki2c_i2c_release_bus(void *cookie, int flags)
390 struct ki2c_softc *sc = cookie;
392 mutex_exit(&sc->sc_buslock);
396 ki2c_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
397 size_t cmdlen, void *vbuf, size_t buflen, int flags)
399 struct ki2c_softc *sc = cookie;
400 int i;
401 size_t w_len;
402 uint8_t *wp;
403 uint8_t wrbuf[I2C_EXEC_MAX_CMDLEN + I2C_EXEC_MAX_CMDLEN];
406 * We don't have any idea if the ki2c controller can execute
407 * i2c quick_{read,write} operations, so if someone tries one,
408 * return an error.
410 if (cmdlen == 0 && buflen == 0)
411 return -1;
413 /* we handle the subaddress stuff ourselves */
414 ki2c_setmode(sc, I2C_STDMODE);
416 /* Write-buffer defaults to vcmd */
417 wp = (uint8_t *)(__UNCONST(vcmd));
418 w_len = cmdlen;
421 * Concatenate vcmd and vbuf for write operations
423 * Drivers written specifically for ki2c might already do this,
424 * but "generic" i2c drivers still provide separate arguments
425 * for the cmd and buf parts of iic_smbus_write_{byte,word}.
427 if (I2C_OP_WRITE_P(op) && buflen != 0) {
428 if (cmdlen == 0) {
429 wp = (uint8_t *)vbuf;
430 w_len = buflen;
431 } else {
432 KASSERT((cmdlen + buflen) <= sizeof(wrbuf));
433 wp = (uint8_t *)(__UNCONST(vcmd));
434 w_len = 0;
435 for (i = 0; i < cmdlen; i++)
436 wrbuf[w_len++] = *wp++;
437 wp = (uint8_t *)vbuf;
438 for (i = 0; i < buflen; i++)
439 wrbuf[w_len++] = *wp++;
440 wp = wrbuf;
444 if (ki2c_write(sc, addr, 0, wp, w_len) !=0 )
445 return -1;
447 if (I2C_OP_READ_P(op)) {
448 if (ki2c_read(sc, addr, 0, vbuf, buflen) !=0 )
449 return -1;
451 return 0;