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