Home | History | Annotate | Line # | Download | only in at91
at91twi.c revision 1.2
      1  1.2  matt /*	$Id: at91twi.c,v 1.2 2008/07/03 01:15:38 matt Exp $	*/
      2  1.2  matt /*	$NetBSD: at91twi.c,v 1.2 2008/07/03 01:15:38 matt 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  matt __KERNEL_RCSID(0, "$NetBSD: at91twi.c,v 1.2 2008/07/03 01:15:38 matt 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  matt at91twi_readreg(sc, reg)
    143  1.2  matt 	struct at91twi_softc *sc;
    144  1.2  matt 	int reg;
    145  1.2  matt {
    146  1.2  matt 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, reg);
    147  1.2  matt }
    148  1.2  matt 
    149  1.2  matt void
    150  1.2  matt at91twi_writereg(sc, reg, val)
    151  1.2  matt 	struct at91twi_softc *sc;
    152  1.2  matt 	int reg;
    153  1.2  matt 	u_int val;
    154  1.2  matt {
    155  1.2  matt 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, reg, val);
    156  1.2  matt }
    157  1.2  matt 
    158  1.2  matt int
    159  1.2  matt at91twi_intr(void *arg)
    160  1.2  matt {
    161  1.2  matt 	struct at91twi_softc *sc = arg;
    162  1.2  matt 	u_int sr, isr, imr;
    163  1.2  matt 
    164  1.2  matt 	sr = at91twi_readreg(sc, TWI_SR);
    165  1.2  matt 	imr = at91twi_readreg(sc, TWI_IMR);
    166  1.2  matt 	isr = sr & imr;
    167  1.2  matt 
    168  1.2  matt 	if (!isr) {
    169  1.2  matt #ifdef AT91TWI_DEBUG
    170  1.2  matt //		printf("%s(%s): interrupts are disabled (sr=%08X imr=%08X)\n", __FUNCTION__, device_xname(sc->sc_dev), sr, imr);
    171  1.2  matt #endif
    172  1.2  matt 		return 0;
    173  1.2  matt 	}
    174  1.2  matt 
    175  1.2  matt 	if (isr & TWI_SR_TXCOMP) {
    176  1.2  matt 		// transmission has completed!
    177  1.2  matt 		if (sr & (TWI_SR_NACK | TWI_SR_UNRE | TWI_SR_OVRE)) {
    178  1.2  matt 			// failed!
    179  1.2  matt #ifdef AT91TWI_DEBUG
    180  1.2  matt 			printf("%s(%s): FAILED (sr=%08X)\n", __FUNCTION__,
    181  1.2  matt 			       device_xname(sc->sc_dev), sr);
    182  1.2  matt #endif
    183  1.2  matt 			sc->sc_flags |= I2C_ERROR;
    184  1.2  matt 		} else {
    185  1.2  matt #ifdef AT91TWI_DEBUG
    186  1.2  matt 			printf("%s(%s): SUCCESS (sr=%08X)\n", __FUNCTION__,
    187  1.2  matt 			       device_xname(sc->sc_dev), sr);
    188  1.2  matt #endif
    189  1.2  matt 		}
    190  1.2  matt 		if (sc->sc_flags & I2C_READING && sr & TWI_SR_RXRDY) {
    191  1.2  matt 			*sc->sc_data++ = at91twi_readreg(sc, TWI_RHR);
    192  1.2  matt 			sc->sc_resid--;
    193  1.2  matt 		}
    194  1.2  matt 		sc->sc_flags &= ~I2C_BUSY;
    195  1.2  matt 		at91twi_writereg(sc, TWI_IDR, -1);
    196  1.2  matt 		goto out;
    197  1.2  matt 	}
    198  1.2  matt 
    199  1.2  matt 	if (isr & TWI_SR_TXRDY) {
    200  1.2  matt 		if (--sc->sc_resid > 0)
    201  1.2  matt 			at91twi_writereg(sc, TWI_THR, *sc->sc_data++);
    202  1.2  matt 	}
    203  1.2  matt 
    204  1.2  matt 	if (isr & TWI_SR_RXRDY) {
    205  1.2  matt 		// data has been received
    206  1.2  matt 		*sc->sc_data++ = at91twi_readreg(sc, TWI_RHR);
    207  1.2  matt 		sc->sc_resid--;
    208  1.2  matt 	}
    209  1.2  matt 
    210  1.2  matt 	if (isr & (TWI_SR_TXRDY | TWI_SR_RXRDY) && sc->sc_resid <= 0) {
    211  1.2  matt 		// all bytes have been transmitted, send stop condition
    212  1.2  matt 		at91twi_writereg(sc, TWI_IDR, TWI_SR_RXRDY | TWI_SR_TXRDY);
    213  1.2  matt 		at91twi_writereg(sc, TWI_CR, TWI_CR_STOP);
    214  1.2  matt 	}
    215  1.2  matt out:
    216  1.2  matt 	return 1;
    217  1.2  matt }
    218  1.2  matt 
    219  1.2  matt int
    220  1.2  matt at91twi_poll(sc, timo, flags)
    221  1.2  matt 	struct at91twi_softc *sc;
    222  1.2  matt 	int timo, flags;
    223  1.2  matt {
    224  1.2  matt 
    225  1.2  matt 	timo = 1000000U;
    226  1.2  matt 
    227  1.2  matt 	while (sc->sc_flags & I2C_BUSY) {
    228  1.2  matt 		if (timo < 0) {
    229  1.2  matt 			printf("i2c_poll: timeout\n");
    230  1.2  matt 			return -1;
    231  1.2  matt 		}
    232  1.2  matt 		if (flags & I2C_F_POLL) {
    233  1.2  matt 			at91_intr_poll(sc->sc_ih, 1);
    234  1.2  matt 			delay(1);
    235  1.2  matt 			timo--;
    236  1.2  matt 		} else {
    237  1.2  matt 			delay(100); // @@@ sleep!?
    238  1.2  matt 			timo -= 100;
    239  1.2  matt 		}
    240  1.2  matt 	}
    241  1.2  matt 	return 0;
    242  1.2  matt }
    243  1.2  matt 
    244  1.2  matt int
    245  1.2  matt at91twi_start(struct at91twi_softc *sc, int addr, void *data, int len,
    246  1.2  matt 	int flags)
    247  1.2  matt {
    248  1.2  matt 	int rd = (sc->sc_flags & I2C_READING);
    249  1.2  matt 	int timo, s;
    250  1.2  matt 
    251  1.2  matt 	KASSERT((addr & 1) == 0);
    252  1.2  matt 
    253  1.2  matt 	sc->sc_data = data;
    254  1.2  matt 	sc->sc_resid = len;
    255  1.2  matt 	sc->sc_flags |= I2C_BUSY;
    256  1.2  matt 
    257  1.2  matt 	timo = 1000 + len * 200;
    258  1.2  matt 
    259  1.2  matt 	s = splserial();
    260  1.2  matt 	// if writing, queue first byte immediately
    261  1.2  matt 	if (!rd)
    262  1.2  matt 		at91twi_writereg(sc, TWI_THR, *sc->sc_data++);
    263  1.2  matt 	// if there's just one byte to transmit, we must set STOP-bit too
    264  1.2  matt 	if (sc->sc_resid == 1) {
    265  1.2  matt 		at91twi_writereg(sc, TWI_IER, TWI_SR_TXCOMP);
    266  1.2  matt 		at91twi_writereg(sc, TWI_CR, TWI_CR_START | TWI_CR_STOP);
    267  1.2  matt 	} else {
    268  1.2  matt 		at91twi_writereg(sc, TWI_IER, TWI_SR_TXCOMP
    269  1.2  matt 				  | (rd ? TWI_SR_RXRDY : TWI_SR_TXRDY));
    270  1.2  matt 		at91twi_writereg(sc, TWI_CR, TWI_CR_START);
    271  1.2  matt 	}
    272  1.2  matt 	splx(s);
    273  1.2  matt 
    274  1.2  matt 	if (at91twi_poll(sc, timo, flags))
    275  1.2  matt 		return -1;
    276  1.2  matt 	if (sc->sc_flags & I2C_ERROR) {
    277  1.2  matt 		printf("I2C_ERROR\n");
    278  1.2  matt 		return -1;
    279  1.2  matt 	}
    280  1.2  matt 	return 0;
    281  1.2  matt }
    282  1.2  matt 
    283  1.2  matt int
    284  1.2  matt at91twi_read(sc, addr, data, len, flags)
    285  1.2  matt 	struct at91twi_softc *sc;
    286  1.2  matt 	int addr, len;
    287  1.2  matt 	void *data;
    288  1.2  matt 	int flags;
    289  1.2  matt {
    290  1.2  matt 	sc->sc_flags = I2C_READING;
    291  1.2  matt 	#ifdef AT91TWI_DEBUG
    292  1.2  matt 		printf("at91twi_read: %02x %d\n", addr, len);
    293  1.2  matt 	#endif
    294  1.2  matt 	return at91twi_start(sc, addr, data, len, flags);
    295  1.2  matt }
    296  1.2  matt 
    297  1.2  matt int
    298  1.2  matt at91twi_write(sc, addr, data, len, flags)
    299  1.2  matt 	struct at91twi_softc *sc;
    300  1.2  matt 	int addr, len;
    301  1.2  matt 	void *data;
    302  1.2  matt 	int flags;
    303  1.2  matt {
    304  1.2  matt 	sc->sc_flags = 0;
    305  1.2  matt 	#ifdef AT91TWI_DEBUG
    306  1.2  matt 		printf("at91twi_write: %02x %d\n", addr, len);
    307  1.2  matt 	#endif
    308  1.2  matt 	return at91twi_start(sc, addr, data, len, flags);
    309  1.2  matt }
    310  1.2  matt 
    311  1.2  matt static int
    312  1.2  matt at91twi_i2c_acquire_bus(void *cookie, int flags)
    313  1.2  matt {
    314  1.2  matt 	struct at91twi_softc *sc = cookie;
    315  1.2  matt 
    316  1.2  matt 	if (flags & I2C_F_POLL)
    317  1.2  matt 		return 0;
    318  1.2  matt 
    319  1.2  matt 	mutex_enter(&sc->sc_buslock);
    320  1.2  matt 	return 0;
    321  1.2  matt }
    322  1.2  matt 
    323  1.2  matt static void
    324  1.2  matt at91twi_i2c_release_bus(void *cookie, int flags)
    325  1.2  matt {
    326  1.2  matt 	struct at91twi_softc *sc = cookie;
    327  1.2  matt 
    328  1.2  matt 	if (flags & I2C_F_POLL)
    329  1.2  matt 		return;
    330  1.2  matt 
    331  1.2  matt 	mutex_exit(&sc->sc_buslock);
    332  1.2  matt }
    333  1.2  matt 
    334  1.2  matt int
    335  1.2  matt at91twi_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
    336  1.2  matt     size_t cmdlen, void *vbuf, size_t buflen, int flags)
    337  1.2  matt {
    338  1.2  matt 	struct at91twi_softc *sc = cookie;
    339  1.2  matt 
    340  1.2  matt 	if (I2C_OP_READ_P(op))
    341  1.2  matt 	{
    342  1.2  matt 		u_int iadr = 0;
    343  1.2  matt 		if (vcmd) {
    344  1.2  matt 			const uint8_t *cmd = (const uint8_t *)vcmd;
    345  1.2  matt 			if (cmdlen > 3) {
    346  1.2  matt 				// we're in trouble..
    347  1.2  matt 				return -1;
    348  1.2  matt 			}
    349  1.2  matt 			iadr = cmd[0];
    350  1.2  matt 			if (cmdlen > 1) {
    351  1.2  matt 				iadr <<= 8;
    352  1.2  matt 				iadr |= cmd[1];
    353  1.2  matt 			}
    354  1.2  matt 			if (cmdlen > 2) {
    355  1.2  matt 				iadr <<= 8;
    356  1.2  matt 				iadr |= cmd[2];
    357  1.2  matt 			}
    358  1.2  matt 		}
    359  1.2  matt 		at91twi_writereg(sc, TWI_MMR, (addr << 16) | TWI_MMR_MREAD | (cmdlen << 8));
    360  1.2  matt 		if (cmdlen > 0) {
    361  1.2  matt 	#ifdef AT91TWI_DEBUG
    362  1.2  matt 			printf("at91twi_read: %02x iadr=%08X mmr=%08X\n",
    363  1.2  matt 			       addr, iadr, at91twi_readreg(sc, TWI_MMR));
    364  1.2  matt 	#endif
    365  1.2  matt 			at91twi_writereg(sc, TWI_IADR, iadr);
    366  1.2  matt 		}
    367  1.2  matt 		if (at91twi_read(sc, addr, vbuf, buflen, flags) != 0)
    368  1.2  matt 			return -1;
    369  1.2  matt 	} else if (vcmd) {
    370  1.2  matt 		at91twi_writereg(sc, TWI_MMR, addr << 16);
    371  1.2  matt 		if (at91twi_write(sc, addr, __UNCONST(vcmd), cmdlen, flags) !=0)
    372  1.2  matt 			return -1;
    373  1.2  matt 	}
    374  1.2  matt 	return 0;
    375  1.2  matt }
    376