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