Home | History | Annotate | Line # | Download | only in at91
at91twi.c revision 1.1.16.1
      1  1.1.16.1  mjf /*	$Id: at91twi.c,v 1.1.16.1 2008/09/28 10:39:49 mjf Exp $	*/
      2  1.1.16.1  mjf /*	$NetBSD: at91twi.c,v 1.1.16.1 2008/09/28 10:39:49 mjf Exp $	*/
      3  1.1.16.1  mjf 
      4  1.1.16.1  mjf /*-
      5  1.1.16.1  mjf  * Copyright (c) 2007 Embedtronics Oy. All rights reserved.
      6  1.1.16.1  mjf  *
      7  1.1.16.1  mjf  * Based on arch/macppc/dev/ki2c.c,
      8  1.1.16.1  mjf  * Copyright (c) 2001 Tsubai Masanari.  All rights reserved.
      9  1.1.16.1  mjf  *
     10  1.1.16.1  mjf  * Redistribution and use in source and binary forms, with or without
     11  1.1.16.1  mjf  * modification, are permitted provided that the following conditions
     12  1.1.16.1  mjf  * are met:
     13  1.1.16.1  mjf  * 1. Redistributions of source code must retain the above copyright
     14  1.1.16.1  mjf  *    notice, this list of conditions and the following disclaimer.
     15  1.1.16.1  mjf  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1.16.1  mjf  *    notice, this list of conditions and the following disclaimer in the
     17  1.1.16.1  mjf  *    documentation and/or other materials provided with the distribution.
     18  1.1.16.1  mjf  * 3. The name of the author may not be used to endorse or promote products
     19  1.1.16.1  mjf  *    derived from this software without specific prior written permission.
     20  1.1.16.1  mjf  *
     21  1.1.16.1  mjf  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1.16.1  mjf  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1.16.1  mjf  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1.16.1  mjf  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1.16.1  mjf  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.1.16.1  mjf  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.1.16.1  mjf  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.1.16.1  mjf  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.1.16.1  mjf  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     30  1.1.16.1  mjf  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1.16.1  mjf  */
     32  1.1.16.1  mjf 
     33  1.1.16.1  mjf #include <sys/cdefs.h>
     34  1.1.16.1  mjf __KERNEL_RCSID(0, "$NetBSD: at91twi.c,v 1.1.16.1 2008/09/28 10:39:49 mjf Exp $");
     35  1.1.16.1  mjf 
     36  1.1.16.1  mjf #include <sys/param.h>
     37  1.1.16.1  mjf #include <sys/device.h>
     38  1.1.16.1  mjf #include <sys/systm.h>
     39  1.1.16.1  mjf 
     40  1.1.16.1  mjf #include <machine/bus.h>
     41  1.1.16.1  mjf #include <sys/lock.h>
     42  1.1.16.1  mjf 
     43  1.1.16.1  mjf #include <arm/at91/at91var.h>
     44  1.1.16.1  mjf #include <arm/at91/at91reg.h>
     45  1.1.16.1  mjf 
     46  1.1.16.1  mjf #include <dev/i2c/i2cvar.h>
     47  1.1.16.1  mjf #include <arm/at91/at91twivar.h>
     48  1.1.16.1  mjf #include <arm/at91/at91twireg.h>
     49  1.1.16.1  mjf 
     50  1.1.16.1  mjf int at91twi_match(device_t, cfdata_t, void *);
     51  1.1.16.1  mjf void at91twi_attach(device_t, device_t, void *);
     52  1.1.16.1  mjf inline u_int at91twi_readreg(struct at91twi_softc *, int);
     53  1.1.16.1  mjf inline void at91twi_writereg(struct at91twi_softc *, int, u_int);
     54  1.1.16.1  mjf int at91twi_intr(void *);
     55  1.1.16.1  mjf int at91twi_poll(struct at91twi_softc *, int, int);
     56  1.1.16.1  mjf int at91twi_start(struct at91twi_softc *, int, void *, int, int);
     57  1.1.16.1  mjf int at91twi_read(struct at91twi_softc *, int, void *, int, int);
     58  1.1.16.1  mjf int at91twi_write(struct at91twi_softc *, int, void *, int, int);
     59  1.1.16.1  mjf 
     60  1.1.16.1  mjf /* I2C glue */
     61  1.1.16.1  mjf static int at91twi_i2c_acquire_bus(void *, int);
     62  1.1.16.1  mjf static void at91twi_i2c_release_bus(void *, int);
     63  1.1.16.1  mjf static int at91twi_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
     64  1.1.16.1  mjf 		    void *, size_t, int);
     65  1.1.16.1  mjf 
     66  1.1.16.1  mjf 
     67  1.1.16.1  mjf CFATTACH_DECL_NEW(at91twi, sizeof(struct at91twi_softc),
     68  1.1.16.1  mjf 	at91twi_match, at91twi_attach, NULL, NULL);
     69  1.1.16.1  mjf 
     70  1.1.16.1  mjf int
     71  1.1.16.1  mjf at91twi_match(device_t parent, cfdata_t match, void *aux)
     72  1.1.16.1  mjf {
     73  1.1.16.1  mjf 	if (strcmp(match->cf_name, "at91twi") == 0)
     74  1.1.16.1  mjf 		return 2;
     75  1.1.16.1  mjf 	return 0;
     76  1.1.16.1  mjf }
     77  1.1.16.1  mjf 
     78  1.1.16.1  mjf void
     79  1.1.16.1  mjf at91twi_attach(device_t parent, device_t self, void *aux)
     80  1.1.16.1  mjf {
     81  1.1.16.1  mjf 	struct at91twi_softc *sc = device_private(self);
     82  1.1.16.1  mjf 	struct at91bus_attach_args *sa = aux;
     83  1.1.16.1  mjf 	struct i2cbus_attach_args iba;
     84  1.1.16.1  mjf 	unsigned ckdiv, cxdiv;
     85  1.1.16.1  mjf 
     86  1.1.16.1  mjf 	// gather attach data:
     87  1.1.16.1  mjf 	sc->sc_dev = self;
     88  1.1.16.1  mjf 	sc->sc_iot = sa->sa_iot;
     89  1.1.16.1  mjf 	sc->sc_pid = sa->sa_pid;
     90  1.1.16.1  mjf 
     91  1.1.16.1  mjf 	if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0, &sc->sc_ioh))
     92  1.1.16.1  mjf 		panic("%s: Cannot map registers", self->dv_xname);
     93  1.1.16.1  mjf 
     94  1.1.16.1  mjf 	printf(": I2C controller\n");
     95  1.1.16.1  mjf 
     96  1.1.16.1  mjf 	/* initialize I2C controller */
     97  1.1.16.1  mjf 	at91_peripheral_clock(sc->sc_pid, 1);
     98  1.1.16.1  mjf 
     99  1.1.16.1  mjf 	at91twi_writereg(sc, TWI_CR, TWI_CR_SWRST);
    100  1.1.16.1  mjf 	delay(1000);
    101  1.1.16.1  mjf #if 1
    102  1.1.16.1  mjf 	// target to 100 kHz
    103  1.1.16.1  mjf 	for (ckdiv = 0; ckdiv < 8; ckdiv++) {
    104  1.1.16.1  mjf 		if ((cxdiv = (AT91_MSTCLK / (1U << ckdiv)) / (2 * 50000U)) < 256) {
    105  1.1.16.1  mjf 			goto found_ckdiv;
    106  1.1.16.1  mjf 		}
    107  1.1.16.1  mjf 	}
    108  1.1.16.1  mjf 	panic("%s: Cannot calculate clock divider!", __FUNCTION__);
    109  1.1.16.1  mjf 
    110  1.1.16.1  mjf found_ckdiv:
    111  1.1.16.1  mjf #else
    112  1.1.16.1  mjf 	ckdiv = 5; cxdiv = 0xFF;
    113  1.1.16.1  mjf #endif
    114  1.1.16.1  mjf 	at91twi_writereg(sc, TWI_CWGR, (ckdiv << 16) | (cxdiv << 8) | cxdiv);
    115  1.1.16.1  mjf 	at91twi_writereg(sc, TWI_CR, TWI_CR_MSEN);
    116  1.1.16.1  mjf 
    117  1.1.16.1  mjf //#ifdef AT91TWI_DEBUG
    118  1.1.16.1  mjf 	printf("%s: ckdiv=%d cxdiv=%d CWGR=0x%08X SR=0x%08X\n", self->dv_xname, ckdiv, cxdiv, at91twi_readreg(sc, TWI_CWGR), at91twi_readreg(sc, TWI_SR));
    119  1.1.16.1  mjf //#endif
    120  1.1.16.1  mjf 
    121  1.1.16.1  mjf 	/* initialize rest */
    122  1.1.16.1  mjf 	mutex_init(&sc->sc_buslock, MUTEX_DEFAULT, IPL_NONE);
    123  1.1.16.1  mjf 	sc->sc_ih = at91_intr_establish(sc->sc_pid, IPL_SERIAL, INTR_HIGH_LEVEL,
    124  1.1.16.1  mjf 					at91twi_intr, sc);
    125  1.1.16.1  mjf 
    126  1.1.16.1  mjf 	/* fill in the i2c tag */
    127  1.1.16.1  mjf 	sc->sc_i2c.ic_cookie = sc;
    128  1.1.16.1  mjf 	sc->sc_i2c.ic_acquire_bus = at91twi_i2c_acquire_bus;
    129  1.1.16.1  mjf 	sc->sc_i2c.ic_release_bus = at91twi_i2c_release_bus;
    130  1.1.16.1  mjf 	sc->sc_i2c.ic_send_start = NULL;
    131  1.1.16.1  mjf 	sc->sc_i2c.ic_send_stop = NULL;
    132  1.1.16.1  mjf 	sc->sc_i2c.ic_initiate_xfer = NULL;
    133  1.1.16.1  mjf 	sc->sc_i2c.ic_read_byte = NULL;
    134  1.1.16.1  mjf 	sc->sc_i2c.ic_write_byte = NULL;
    135  1.1.16.1  mjf 	sc->sc_i2c.ic_exec = at91twi_i2c_exec;
    136  1.1.16.1  mjf 
    137  1.1.16.1  mjf 	iba.iba_tag = &sc->sc_i2c;
    138  1.1.16.1  mjf 	(void) config_found_ia(sc->sc_dev, "i2cbus", &iba, iicbus_print);
    139  1.1.16.1  mjf }
    140  1.1.16.1  mjf 
    141  1.1.16.1  mjf u_int
    142  1.1.16.1  mjf at91twi_readreg(sc, reg)
    143  1.1.16.1  mjf 	struct at91twi_softc *sc;
    144  1.1.16.1  mjf 	int reg;
    145  1.1.16.1  mjf {
    146  1.1.16.1  mjf 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, reg);
    147  1.1.16.1  mjf }
    148  1.1.16.1  mjf 
    149  1.1.16.1  mjf void
    150  1.1.16.1  mjf at91twi_writereg(sc, reg, val)
    151  1.1.16.1  mjf 	struct at91twi_softc *sc;
    152  1.1.16.1  mjf 	int reg;
    153  1.1.16.1  mjf 	u_int val;
    154  1.1.16.1  mjf {
    155  1.1.16.1  mjf 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, reg, val);
    156  1.1.16.1  mjf }
    157  1.1.16.1  mjf 
    158  1.1.16.1  mjf int
    159  1.1.16.1  mjf at91twi_intr(void *arg)
    160  1.1.16.1  mjf {
    161  1.1.16.1  mjf 	struct at91twi_softc *sc = arg;
    162  1.1.16.1  mjf 	u_int sr, isr, imr;
    163  1.1.16.1  mjf 
    164  1.1.16.1  mjf 	sr = at91twi_readreg(sc, TWI_SR);
    165  1.1.16.1  mjf 	imr = at91twi_readreg(sc, TWI_IMR);
    166  1.1.16.1  mjf 	isr = sr & imr;
    167  1.1.16.1  mjf 
    168  1.1.16.1  mjf 	if (!isr) {
    169  1.1.16.1  mjf #ifdef AT91TWI_DEBUG
    170  1.1.16.1  mjf //		printf("%s(%s): interrupts are disabled (sr=%08X imr=%08X)\n", __FUNCTION__, device_xname(sc->sc_dev), sr, imr);
    171  1.1.16.1  mjf #endif
    172  1.1.16.1  mjf 		return 0;
    173  1.1.16.1  mjf 	}
    174  1.1.16.1  mjf 
    175  1.1.16.1  mjf 	if (isr & TWI_SR_TXCOMP) {
    176  1.1.16.1  mjf 		// transmission has completed!
    177  1.1.16.1  mjf 		if (sr & (TWI_SR_NACK | TWI_SR_UNRE | TWI_SR_OVRE)) {
    178  1.1.16.1  mjf 			// failed!
    179  1.1.16.1  mjf #ifdef AT91TWI_DEBUG
    180  1.1.16.1  mjf 			printf("%s(%s): FAILED (sr=%08X)\n", __FUNCTION__,
    181  1.1.16.1  mjf 			       device_xname(sc->sc_dev), sr);
    182  1.1.16.1  mjf #endif
    183  1.1.16.1  mjf 			sc->sc_flags |= I2C_ERROR;
    184  1.1.16.1  mjf 		} else {
    185  1.1.16.1  mjf #ifdef AT91TWI_DEBUG
    186  1.1.16.1  mjf 			printf("%s(%s): SUCCESS (sr=%08X)\n", __FUNCTION__,
    187  1.1.16.1  mjf 			       device_xname(sc->sc_dev), sr);
    188  1.1.16.1  mjf #endif
    189  1.1.16.1  mjf 		}
    190  1.1.16.1  mjf 		if (sc->sc_flags & I2C_READING && sr & TWI_SR_RXRDY) {
    191  1.1.16.1  mjf 			*sc->sc_data++ = at91twi_readreg(sc, TWI_RHR);
    192  1.1.16.1  mjf 			sc->sc_resid--;
    193  1.1.16.1  mjf 		}
    194  1.1.16.1  mjf 		sc->sc_flags &= ~I2C_BUSY;
    195  1.1.16.1  mjf 		at91twi_writereg(sc, TWI_IDR, -1);
    196  1.1.16.1  mjf 		goto out;
    197  1.1.16.1  mjf 	}
    198  1.1.16.1  mjf 
    199  1.1.16.1  mjf 	if (isr & TWI_SR_TXRDY) {
    200  1.1.16.1  mjf 		if (--sc->sc_resid > 0)
    201  1.1.16.1  mjf 			at91twi_writereg(sc, TWI_THR, *sc->sc_data++);
    202  1.1.16.1  mjf 	}
    203  1.1.16.1  mjf 
    204  1.1.16.1  mjf 	if (isr & TWI_SR_RXRDY) {
    205  1.1.16.1  mjf 		// data has been received
    206  1.1.16.1  mjf 		*sc->sc_data++ = at91twi_readreg(sc, TWI_RHR);
    207  1.1.16.1  mjf 		sc->sc_resid--;
    208  1.1.16.1  mjf 	}
    209  1.1.16.1  mjf 
    210  1.1.16.1  mjf 	if (isr & (TWI_SR_TXRDY | TWI_SR_RXRDY) && sc->sc_resid <= 0) {
    211  1.1.16.1  mjf 		// all bytes have been transmitted, send stop condition
    212  1.1.16.1  mjf 		at91twi_writereg(sc, TWI_IDR, TWI_SR_RXRDY | TWI_SR_TXRDY);
    213  1.1.16.1  mjf 		at91twi_writereg(sc, TWI_CR, TWI_CR_STOP);
    214  1.1.16.1  mjf 	}
    215  1.1.16.1  mjf out:
    216  1.1.16.1  mjf 	return 1;
    217  1.1.16.1  mjf }
    218  1.1.16.1  mjf 
    219  1.1.16.1  mjf int
    220  1.1.16.1  mjf at91twi_poll(sc, timo, flags)
    221  1.1.16.1  mjf 	struct at91twi_softc *sc;
    222  1.1.16.1  mjf 	int timo, flags;
    223  1.1.16.1  mjf {
    224  1.1.16.1  mjf 
    225  1.1.16.1  mjf 	timo = 1000000U;
    226  1.1.16.1  mjf 
    227  1.1.16.1  mjf 	while (sc->sc_flags & I2C_BUSY) {
    228  1.1.16.1  mjf 		if (timo < 0) {
    229  1.1.16.1  mjf 			printf("i2c_poll: timeout\n");
    230  1.1.16.1  mjf 			return -1;
    231  1.1.16.1  mjf 		}
    232  1.1.16.1  mjf 		if (flags & I2C_F_POLL) {
    233  1.1.16.1  mjf 			at91_intr_poll(sc->sc_ih, 1);
    234  1.1.16.1  mjf 			delay(1);
    235  1.1.16.1  mjf 			timo--;
    236  1.1.16.1  mjf 		} else {
    237  1.1.16.1  mjf 			delay(100); // @@@ sleep!?
    238  1.1.16.1  mjf 			timo -= 100;
    239  1.1.16.1  mjf 		}
    240  1.1.16.1  mjf 	}
    241  1.1.16.1  mjf 	return 0;
    242  1.1.16.1  mjf }
    243  1.1.16.1  mjf 
    244  1.1.16.1  mjf int
    245  1.1.16.1  mjf at91twi_start(struct at91twi_softc *sc, int addr, void *data, int len,
    246  1.1.16.1  mjf 	int flags)
    247  1.1.16.1  mjf {
    248  1.1.16.1  mjf 	int rd = (sc->sc_flags & I2C_READING);
    249  1.1.16.1  mjf 	int timo, s;
    250  1.1.16.1  mjf 
    251  1.1.16.1  mjf 	KASSERT((addr & 1) == 0);
    252  1.1.16.1  mjf 
    253  1.1.16.1  mjf 	sc->sc_data = data;
    254  1.1.16.1  mjf 	sc->sc_resid = len;
    255  1.1.16.1  mjf 	sc->sc_flags |= I2C_BUSY;
    256  1.1.16.1  mjf 
    257  1.1.16.1  mjf 	timo = 1000 + len * 200;
    258  1.1.16.1  mjf 
    259  1.1.16.1  mjf 	s = splserial();
    260  1.1.16.1  mjf 	// if writing, queue first byte immediately
    261  1.1.16.1  mjf 	if (!rd)
    262  1.1.16.1  mjf 		at91twi_writereg(sc, TWI_THR, *sc->sc_data++);
    263  1.1.16.1  mjf 	// if there's just one byte to transmit, we must set STOP-bit too
    264  1.1.16.1  mjf 	if (sc->sc_resid == 1) {
    265  1.1.16.1  mjf 		at91twi_writereg(sc, TWI_IER, TWI_SR_TXCOMP);
    266  1.1.16.1  mjf 		at91twi_writereg(sc, TWI_CR, TWI_CR_START | TWI_CR_STOP);
    267  1.1.16.1  mjf 	} else {
    268  1.1.16.1  mjf 		at91twi_writereg(sc, TWI_IER, TWI_SR_TXCOMP
    269  1.1.16.1  mjf 				  | (rd ? TWI_SR_RXRDY : TWI_SR_TXRDY));
    270  1.1.16.1  mjf 		at91twi_writereg(sc, TWI_CR, TWI_CR_START);
    271  1.1.16.1  mjf 	}
    272  1.1.16.1  mjf 	splx(s);
    273  1.1.16.1  mjf 
    274  1.1.16.1  mjf 	if (at91twi_poll(sc, timo, flags))
    275  1.1.16.1  mjf 		return -1;
    276  1.1.16.1  mjf 	if (sc->sc_flags & I2C_ERROR) {
    277  1.1.16.1  mjf 		printf("I2C_ERROR\n");
    278  1.1.16.1  mjf 		return -1;
    279  1.1.16.1  mjf 	}
    280  1.1.16.1  mjf 	return 0;
    281  1.1.16.1  mjf }
    282  1.1.16.1  mjf 
    283  1.1.16.1  mjf int
    284  1.1.16.1  mjf at91twi_read(sc, addr, data, len, flags)
    285  1.1.16.1  mjf 	struct at91twi_softc *sc;
    286  1.1.16.1  mjf 	int addr, len;
    287  1.1.16.1  mjf 	void *data;
    288  1.1.16.1  mjf 	int flags;
    289  1.1.16.1  mjf {
    290  1.1.16.1  mjf 	sc->sc_flags = I2C_READING;
    291  1.1.16.1  mjf 	#ifdef AT91TWI_DEBUG
    292  1.1.16.1  mjf 		printf("at91twi_read: %02x %d\n", addr, len);
    293  1.1.16.1  mjf 	#endif
    294  1.1.16.1  mjf 	return at91twi_start(sc, addr, data, len, flags);
    295  1.1.16.1  mjf }
    296  1.1.16.1  mjf 
    297  1.1.16.1  mjf int
    298  1.1.16.1  mjf at91twi_write(sc, addr, data, len, flags)
    299  1.1.16.1  mjf 	struct at91twi_softc *sc;
    300  1.1.16.1  mjf 	int addr, len;
    301  1.1.16.1  mjf 	void *data;
    302  1.1.16.1  mjf 	int flags;
    303  1.1.16.1  mjf {
    304  1.1.16.1  mjf 	sc->sc_flags = 0;
    305  1.1.16.1  mjf 	#ifdef AT91TWI_DEBUG
    306  1.1.16.1  mjf 		printf("at91twi_write: %02x %d\n", addr, len);
    307  1.1.16.1  mjf 	#endif
    308  1.1.16.1  mjf 	return at91twi_start(sc, addr, data, len, flags);
    309  1.1.16.1  mjf }
    310  1.1.16.1  mjf 
    311  1.1.16.1  mjf static int
    312  1.1.16.1  mjf at91twi_i2c_acquire_bus(void *cookie, int flags)
    313  1.1.16.1  mjf {
    314  1.1.16.1  mjf 	struct at91twi_softc *sc = cookie;
    315  1.1.16.1  mjf 
    316  1.1.16.1  mjf 	if (flags & I2C_F_POLL)
    317  1.1.16.1  mjf 		return 0;
    318  1.1.16.1  mjf 
    319  1.1.16.1  mjf 	mutex_enter(&sc->sc_buslock);
    320  1.1.16.1  mjf 	return 0;
    321  1.1.16.1  mjf }
    322  1.1.16.1  mjf 
    323  1.1.16.1  mjf static void
    324  1.1.16.1  mjf at91twi_i2c_release_bus(void *cookie, int flags)
    325  1.1.16.1  mjf {
    326  1.1.16.1  mjf 	struct at91twi_softc *sc = cookie;
    327  1.1.16.1  mjf 
    328  1.1.16.1  mjf 	if (flags & I2C_F_POLL)
    329  1.1.16.1  mjf 		return;
    330  1.1.16.1  mjf 
    331  1.1.16.1  mjf 	mutex_exit(&sc->sc_buslock);
    332  1.1.16.1  mjf }
    333  1.1.16.1  mjf 
    334  1.1.16.1  mjf int
    335  1.1.16.1  mjf at91twi_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
    336  1.1.16.1  mjf     size_t cmdlen, void *vbuf, size_t buflen, int flags)
    337  1.1.16.1  mjf {
    338  1.1.16.1  mjf 	struct at91twi_softc *sc = cookie;
    339  1.1.16.1  mjf 
    340  1.1.16.1  mjf 	if (I2C_OP_READ_P(op))
    341  1.1.16.1  mjf 	{
    342  1.1.16.1  mjf 		u_int iadr = 0;
    343  1.1.16.1  mjf 		if (vcmd) {
    344  1.1.16.1  mjf 			const uint8_t *cmd = (const uint8_t *)vcmd;
    345  1.1.16.1  mjf 			if (cmdlen > 3) {
    346  1.1.16.1  mjf 				// we're in trouble..
    347  1.1.16.1  mjf 				return -1;
    348  1.1.16.1  mjf 			}
    349  1.1.16.1  mjf 			iadr = cmd[0];
    350  1.1.16.1  mjf 			if (cmdlen > 1) {
    351  1.1.16.1  mjf 				iadr <<= 8;
    352  1.1.16.1  mjf 				iadr |= cmd[1];
    353  1.1.16.1  mjf 			}
    354  1.1.16.1  mjf 			if (cmdlen > 2) {
    355  1.1.16.1  mjf 				iadr <<= 8;
    356  1.1.16.1  mjf 				iadr |= cmd[2];
    357  1.1.16.1  mjf 			}
    358  1.1.16.1  mjf 		}
    359  1.1.16.1  mjf 		at91twi_writereg(sc, TWI_MMR, (addr << 16) | TWI_MMR_MREAD | (cmdlen << 8));
    360  1.1.16.1  mjf 		if (cmdlen > 0) {
    361  1.1.16.1  mjf 	#ifdef AT91TWI_DEBUG
    362  1.1.16.1  mjf 			printf("at91twi_read: %02x iadr=%08X mmr=%08X\n",
    363  1.1.16.1  mjf 			       addr, iadr, at91twi_readreg(sc, TWI_MMR));
    364  1.1.16.1  mjf 	#endif
    365  1.1.16.1  mjf 			at91twi_writereg(sc, TWI_IADR, iadr);
    366  1.1.16.1  mjf 		}
    367  1.1.16.1  mjf 		if (at91twi_read(sc, addr, vbuf, buflen, flags) != 0)
    368  1.1.16.1  mjf 			return -1;
    369  1.1.16.1  mjf 	} else if (vcmd) {
    370  1.1.16.1  mjf 		at91twi_writereg(sc, TWI_MMR, addr << 16);
    371  1.1.16.1  mjf 		if (at91twi_write(sc, addr, __UNCONST(vcmd), cmdlen, flags) !=0)
    372  1.1.16.1  mjf 			return -1;
    373  1.1.16.1  mjf 	}
    374  1.1.16.1  mjf 	return 0;
    375  1.1.16.1  mjf }
    376