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 */
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
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 *);
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,
63 CFATTACH_DECL(ki2c
, sizeof(struct ki2c_softc
), ki2c_match
, ki2c_attach
,
67 ki2c_match(struct device
*parent
, struct cfdata
*match
, void *aux
)
69 struct confargs
*ca
= aux
;
71 if (strcmp(ca
->ca_name
, "i2c") == 0)
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
;
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");
96 if (OF_getprop(node
, "AAPL,address", &sc
->sc_reg
, 4) != 4) {
97 printf(": unable to find i2c address\n");
100 if (OF_getprop(node
, "AAPL,address-step", &sc
->sc_regstep
, 4) != 4) {
101 printf(": unable to find i2c address step\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
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)
142 child
= OF_peer(child
);
147 for (child
= OF_child(i2cbus
); child
; child
= OF_peer(child
)) {
149 namelen
= OF_getprop(child
, "name", name
, sizeof(name
));
152 if (namelen
>= sizeof(name
))
158 ok
= OF_getprop(child
, "reg", reg
, sizeof(reg
));
160 ok
= OF_getprop(child
, "i2c-address", reg
,
165 ka
.ka_tag
= &sc
->sc_i2c
;
166 config_found_ia(self
, "ki2c", &ka
, ki2c_print
);
170 printf("%s: device (%s) has no reg or i2c-address property.\n",
171 sc
->sc_dev
.dv_xname
, name
);
178 ki2c_print(void *aux
, const char *ki2c
)
180 struct ki2c_confargs
*ka
= aux
;
183 aprint_normal("%s at %s", ka
->ka_name
, ki2c
);
184 aprint_normal(" address 0x%x", ka
->ka_addr
);
190 ki2c_readreg(struct ki2c_softc
*sc
, int reg
)
192 u_char
*addr
= sc
->sc_reg
+ sc
->sc_regstep
* reg
;
198 ki2c_writereg(struct ki2c_softc
*sc
, int reg
, u_int val
)
200 u_char
*addr
= sc
->sc_reg
+ sc
->sc_regstep
* reg
;
203 __asm
volatile ("eieio");
208 ki2c_getmode(struct ki2c_softc
*sc
)
210 return ki2c_readreg(sc
, MODE
) & I2C_MODE
;
214 ki2c_setmode(struct ki2c_softc
*sc
, u_int mode
)
218 KASSERT((mode
& ~I2C_MODE
) == 0);
219 x
= ki2c_readreg(sc
, MODE
);
222 ki2c_writereg(sc
, MODE
, x
);
226 ki2c_getspeed(struct ki2c_softc
*sc
)
228 return ki2c_readreg(sc
, MODE
) & I2C_SPEED
;
232 ki2c_setspeed(struct ki2c_softc
*sc
, u_int speed
)
236 KASSERT((speed
& ~I2C_SPEED
) == 0);
237 x
= ki2c_readreg(sc
, MODE
);
240 ki2c_writereg(sc
, MODE
, x
);
244 ki2c_intr(struct ki2c_softc
*sc
)
248 isr
= ki2c_readreg(sc
, ISR
);
249 if (isr
& I2C_INT_ADDR
) {
251 if ((ki2c_readreg(sc
, STATUS
) & I2C_ST_LASTAAK
) == 0) {
252 /* No slave responded. */
253 sc
->sc_flags
|= I2C_ERROR
;
258 if (sc
->sc_flags
& I2C_READING
) {
259 if (sc
->sc_resid
> 1) {
260 x
= ki2c_readreg(sc
, CONTROL
);
262 ki2c_writereg(sc
, CONTROL
, x
);
265 ki2c_writereg(sc
, DATA
, *sc
->sc_data
++);
270 if (isr
& I2C_INT_DATA
) {
271 if (sc
->sc_flags
& I2C_READING
) {
272 *sc
->sc_data
++ = ki2c_readreg(sc
, DATA
);
275 if (sc
->sc_resid
== 0) { /* Completed */
276 ki2c_writereg(sc
, CONTROL
, 0);
281 if ((ki2c_readreg(sc
, STATUS
) & I2C_ST_LASTAAK
) == 0) {
282 /* No slave responded. */
283 sc
->sc_flags
|= I2C_ERROR
;
288 if (sc
->sc_resid
== 0) {
289 x
= ki2c_readreg(sc
, CONTROL
) | I2C_CT_STOP
;
290 ki2c_writereg(sc
, CONTROL
, x
);
292 ki2c_writereg(sc
, DATA
, *sc
->sc_data
++);
299 if (isr
& I2C_INT_STOP
) {
300 ki2c_writereg(sc
, CONTROL
, 0);
301 sc
->sc_flags
&= ~I2C_BUSY
;
304 ki2c_writereg(sc
, ISR
, isr
);
310 ki2c_poll(struct ki2c_softc
*sc
, int timo
)
312 while (sc
->sc_flags
& I2C_BUSY
) {
313 if (ki2c_readreg(sc
, ISR
))
317 printf("i2c_poll: timeout\n");
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;
331 KASSERT((addr
& 1) == 0);
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) */
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
))
351 if (sc
->sc_flags
& I2C_ERROR
) {
352 printf("I2C_ERROR\n");
359 ki2c_read(struct ki2c_softc
*sc
, int addr
, int subaddr
, void *data
, int len
)
361 sc
->sc_flags
= I2C_READING
;
363 printf("ki2c_read: %02x %d\n", addr
, len
);
365 return ki2c_start(sc
, addr
, subaddr
, data
, len
);
369 ki2c_write(struct ki2c_softc
*sc
, int addr
, int subaddr
, void *data
, int len
)
373 printf("ki2c_write: %02x %d\n",addr
,len
);
375 return ki2c_start(sc
, addr
, subaddr
, data
, len
);
379 ki2c_i2c_acquire_bus(void *cookie
, int flags
)
381 struct ki2c_softc
*sc
= cookie
;
383 mutex_enter(&sc
->sc_buslock
);
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
;
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,
410 if (cmdlen
== 0 && buflen
== 0)
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
));
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) {
429 wp
= (uint8_t *)vbuf
;
432 KASSERT((cmdlen
+ buflen
) <= sizeof(wrbuf
));
433 wp
= (uint8_t *)(__UNCONST(vcmd
));
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
++;
444 if (ki2c_write(sc
, addr
, 0, wp
, w_len
) !=0 )
447 if (I2C_OP_READ_P(op
)) {
448 if (ki2c_read(sc
, addr
, 0, vbuf
, buflen
) !=0 )