Home | History | Annotate | Line # | Download | only in at91
at91twi.c revision 1.7.18.1
      1  1.7.18.1  martin /*	$Id: at91twi.c,v 1.7.18.1 2020/04/08 14:07:28 martin Exp $	*/
      2  1.7.18.1  martin /*	$NetBSD: at91twi.c,v 1.7.18.1 2020/04/08 14:07:28 martin 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.7.18.1  martin __KERNEL_RCSID(0, "$NetBSD: at91twi.c,v 1.7.18.1 2020/04/08 14:07:28 martin 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.5  dyoung #include <sys/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_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
     62       1.2    matt 		    void *, size_t, int);
     63       1.2    matt 
     64       1.2    matt 
     65       1.2    matt CFATTACH_DECL_NEW(at91twi, sizeof(struct at91twi_softc),
     66       1.2    matt 	at91twi_match, at91twi_attach, NULL, NULL);
     67       1.2    matt 
     68       1.2    matt int
     69       1.2    matt at91twi_match(device_t parent, cfdata_t match, void *aux)
     70       1.2    matt {
     71       1.2    matt 	if (strcmp(match->cf_name, "at91twi") == 0)
     72       1.2    matt 		return 2;
     73       1.2    matt 	return 0;
     74       1.2    matt }
     75       1.2    matt 
     76       1.2    matt void
     77       1.2    matt at91twi_attach(device_t parent, device_t self, void *aux)
     78       1.2    matt {
     79       1.2    matt 	struct at91twi_softc *sc = device_private(self);
     80       1.2    matt 	struct at91bus_attach_args *sa = aux;
     81       1.2    matt 	struct i2cbus_attach_args iba;
     82       1.2    matt 	unsigned ckdiv, cxdiv;
     83       1.2    matt 
     84       1.2    matt 	// gather attach data:
     85       1.2    matt 	sc->sc_dev = self;
     86       1.2    matt 	sc->sc_iot = sa->sa_iot;
     87       1.2    matt 	sc->sc_pid = sa->sa_pid;
     88       1.2    matt 
     89       1.2    matt 	if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0, &sc->sc_ioh))
     90       1.6     chs 		panic("%s: Cannot map registers", device_xname(self));
     91       1.2    matt 
     92       1.2    matt 	printf(": I2C controller\n");
     93       1.2    matt 
     94       1.2    matt 	/* initialize I2C controller */
     95       1.2    matt 	at91_peripheral_clock(sc->sc_pid, 1);
     96       1.2    matt 
     97       1.2    matt 	at91twi_writereg(sc, TWI_CR, TWI_CR_SWRST);
     98       1.2    matt 	delay(1000);
     99       1.2    matt #if 1
    100       1.2    matt 	// target to 100 kHz
    101       1.2    matt 	for (ckdiv = 0; ckdiv < 8; ckdiv++) {
    102       1.2    matt 		if ((cxdiv = (AT91_MSTCLK / (1U << ckdiv)) / (2 * 50000U)) < 256) {
    103       1.2    matt 			goto found_ckdiv;
    104       1.2    matt 		}
    105       1.2    matt 	}
    106       1.2    matt 	panic("%s: Cannot calculate clock divider!", __FUNCTION__);
    107       1.2    matt 
    108       1.2    matt found_ckdiv:
    109       1.2    matt #else
    110       1.2    matt 	ckdiv = 5; cxdiv = 0xFF;
    111       1.2    matt #endif
    112       1.2    matt 	at91twi_writereg(sc, TWI_CWGR, (ckdiv << 16) | (cxdiv << 8) | cxdiv);
    113       1.2    matt 	at91twi_writereg(sc, TWI_CR, TWI_CR_MSEN);
    114       1.2    matt 
    115       1.2    matt //#ifdef AT91TWI_DEBUG
    116       1.6     chs 	printf("%s: ckdiv=%d cxdiv=%d CWGR=0x%08X SR=0x%08X\n", device_xname(self), ckdiv, cxdiv, at91twi_readreg(sc, TWI_CWGR), at91twi_readreg(sc, TWI_SR));
    117       1.2    matt //#endif
    118       1.2    matt 
    119       1.2    matt 	/* initialize rest */
    120       1.2    matt 	sc->sc_ih = at91_intr_establish(sc->sc_pid, IPL_SERIAL, INTR_HIGH_LEVEL,
    121       1.2    matt 					at91twi_intr, sc);
    122       1.2    matt 
    123       1.2    matt 	/* fill in the i2c tag */
    124  1.7.18.1  martin 	iic_tag_init(&sc->sc_i2c);
    125       1.2    matt 	sc->sc_i2c.ic_cookie = sc;
    126       1.2    matt 	sc->sc_i2c.ic_exec = at91twi_i2c_exec;
    127       1.2    matt 
    128       1.7     chs 	memset(&iba, 0, sizeof(iba));
    129       1.2    matt 	iba.iba_tag = &sc->sc_i2c;
    130       1.2    matt 	(void) config_found_ia(sc->sc_dev, "i2cbus", &iba, iicbus_print);
    131       1.2    matt }
    132       1.2    matt 
    133       1.2    matt u_int
    134       1.3     dsl at91twi_readreg(struct at91twi_softc *sc, int reg)
    135       1.2    matt {
    136       1.2    matt 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, reg);
    137       1.2    matt }
    138       1.2    matt 
    139       1.2    matt void
    140       1.3     dsl at91twi_writereg(struct at91twi_softc *sc, int reg, u_int val)
    141       1.2    matt {
    142       1.2    matt 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, reg, val);
    143       1.2    matt }
    144       1.2    matt 
    145       1.2    matt int
    146       1.2    matt at91twi_intr(void *arg)
    147       1.2    matt {
    148       1.2    matt 	struct at91twi_softc *sc = arg;
    149       1.2    matt 	u_int sr, isr, imr;
    150       1.2    matt 
    151       1.2    matt 	sr = at91twi_readreg(sc, TWI_SR);
    152       1.2    matt 	imr = at91twi_readreg(sc, TWI_IMR);
    153       1.2    matt 	isr = sr & imr;
    154       1.2    matt 
    155       1.2    matt 	if (!isr) {
    156       1.2    matt #ifdef AT91TWI_DEBUG
    157       1.2    matt //		printf("%s(%s): interrupts are disabled (sr=%08X imr=%08X)\n", __FUNCTION__, device_xname(sc->sc_dev), sr, imr);
    158       1.2    matt #endif
    159       1.2    matt 		return 0;
    160       1.2    matt 	}
    161       1.2    matt 
    162       1.2    matt 	if (isr & TWI_SR_TXCOMP) {
    163       1.2    matt 		// transmission has completed!
    164       1.2    matt 		if (sr & (TWI_SR_NACK | TWI_SR_UNRE | TWI_SR_OVRE)) {
    165       1.2    matt 			// failed!
    166       1.2    matt #ifdef AT91TWI_DEBUG
    167       1.2    matt 			printf("%s(%s): FAILED (sr=%08X)\n", __FUNCTION__,
    168       1.2    matt 			       device_xname(sc->sc_dev), sr);
    169       1.2    matt #endif
    170       1.2    matt 			sc->sc_flags |= I2C_ERROR;
    171       1.2    matt 		} else {
    172       1.2    matt #ifdef AT91TWI_DEBUG
    173       1.2    matt 			printf("%s(%s): SUCCESS (sr=%08X)\n", __FUNCTION__,
    174       1.2    matt 			       device_xname(sc->sc_dev), sr);
    175       1.2    matt #endif
    176       1.2    matt 		}
    177       1.2    matt 		if (sc->sc_flags & I2C_READING && sr & TWI_SR_RXRDY) {
    178       1.2    matt 			*sc->sc_data++ = at91twi_readreg(sc, TWI_RHR);
    179       1.2    matt 			sc->sc_resid--;
    180       1.2    matt 		}
    181       1.2    matt 		sc->sc_flags &= ~I2C_BUSY;
    182       1.2    matt 		at91twi_writereg(sc, TWI_IDR, -1);
    183       1.2    matt 		goto out;
    184       1.2    matt 	}
    185       1.2    matt 
    186       1.2    matt 	if (isr & TWI_SR_TXRDY) {
    187       1.2    matt 		if (--sc->sc_resid > 0)
    188       1.2    matt 			at91twi_writereg(sc, TWI_THR, *sc->sc_data++);
    189       1.2    matt 	}
    190       1.2    matt 
    191       1.2    matt 	if (isr & TWI_SR_RXRDY) {
    192       1.2    matt 		// data has been received
    193       1.2    matt 		*sc->sc_data++ = at91twi_readreg(sc, TWI_RHR);
    194       1.2    matt 		sc->sc_resid--;
    195       1.2    matt 	}
    196       1.2    matt 
    197       1.2    matt 	if (isr & (TWI_SR_TXRDY | TWI_SR_RXRDY) && sc->sc_resid <= 0) {
    198       1.2    matt 		// all bytes have been transmitted, send stop condition
    199       1.2    matt 		at91twi_writereg(sc, TWI_IDR, TWI_SR_RXRDY | TWI_SR_TXRDY);
    200       1.2    matt 		at91twi_writereg(sc, TWI_CR, TWI_CR_STOP);
    201       1.2    matt 	}
    202       1.2    matt out:
    203       1.2    matt 	return 1;
    204       1.2    matt }
    205       1.2    matt 
    206       1.2    matt int
    207       1.4     dsl at91twi_poll(struct at91twi_softc *sc, int timo, int flags)
    208       1.2    matt {
    209       1.2    matt 
    210       1.2    matt 	timo = 1000000U;
    211       1.2    matt 
    212       1.2    matt 	while (sc->sc_flags & I2C_BUSY) {
    213       1.2    matt 		if (timo < 0) {
    214       1.2    matt 			printf("i2c_poll: timeout\n");
    215       1.2    matt 			return -1;
    216       1.2    matt 		}
    217       1.2    matt 		if (flags & I2C_F_POLL) {
    218       1.2    matt 			at91_intr_poll(sc->sc_ih, 1);
    219       1.2    matt 			delay(1);
    220       1.2    matt 			timo--;
    221       1.2    matt 		} else {
    222       1.2    matt 			delay(100); // @@@ sleep!?
    223       1.2    matt 			timo -= 100;
    224       1.2    matt 		}
    225       1.2    matt 	}
    226       1.2    matt 	return 0;
    227       1.2    matt }
    228       1.2    matt 
    229       1.2    matt int
    230       1.2    matt at91twi_start(struct at91twi_softc *sc, int addr, void *data, int len,
    231       1.2    matt 	int flags)
    232       1.2    matt {
    233       1.2    matt 	int rd = (sc->sc_flags & I2C_READING);
    234       1.2    matt 	int timo, s;
    235       1.2    matt 
    236       1.2    matt 	KASSERT((addr & 1) == 0);
    237       1.2    matt 
    238       1.2    matt 	sc->sc_data = data;
    239       1.2    matt 	sc->sc_resid = len;
    240       1.2    matt 	sc->sc_flags |= I2C_BUSY;
    241       1.2    matt 
    242       1.2    matt 	timo = 1000 + len * 200;
    243       1.2    matt 
    244       1.2    matt 	s = splserial();
    245       1.2    matt 	// if writing, queue first byte immediately
    246       1.2    matt 	if (!rd)
    247       1.2    matt 		at91twi_writereg(sc, TWI_THR, *sc->sc_data++);
    248       1.2    matt 	// if there's just one byte to transmit, we must set STOP-bit too
    249       1.2    matt 	if (sc->sc_resid == 1) {
    250       1.2    matt 		at91twi_writereg(sc, TWI_IER, TWI_SR_TXCOMP);
    251       1.2    matt 		at91twi_writereg(sc, TWI_CR, TWI_CR_START | TWI_CR_STOP);
    252       1.2    matt 	} else {
    253       1.2    matt 		at91twi_writereg(sc, TWI_IER, TWI_SR_TXCOMP
    254       1.2    matt 				  | (rd ? TWI_SR_RXRDY : TWI_SR_TXRDY));
    255       1.2    matt 		at91twi_writereg(sc, TWI_CR, TWI_CR_START);
    256       1.2    matt 	}
    257       1.2    matt 	splx(s);
    258       1.2    matt 
    259       1.2    matt 	if (at91twi_poll(sc, timo, flags))
    260       1.2    matt 		return -1;
    261       1.2    matt 	if (sc->sc_flags & I2C_ERROR) {
    262       1.2    matt 		printf("I2C_ERROR\n");
    263       1.2    matt 		return -1;
    264       1.2    matt 	}
    265       1.2    matt 	return 0;
    266       1.2    matt }
    267       1.2    matt 
    268       1.2    matt int
    269       1.4     dsl at91twi_read(struct at91twi_softc *sc, int addr, void *data, int len, int flags)
    270       1.2    matt {
    271       1.2    matt 	sc->sc_flags = I2C_READING;
    272       1.2    matt 	#ifdef AT91TWI_DEBUG
    273       1.2    matt 		printf("at91twi_read: %02x %d\n", addr, len);
    274       1.2    matt 	#endif
    275       1.2    matt 	return at91twi_start(sc, addr, data, len, flags);
    276       1.2    matt }
    277       1.2    matt 
    278       1.2    matt int
    279       1.4     dsl at91twi_write(struct at91twi_softc *sc, int addr, void *data, int len, int flags)
    280       1.2    matt {
    281       1.2    matt 	sc->sc_flags = 0;
    282       1.2    matt 	#ifdef AT91TWI_DEBUG
    283       1.2    matt 		printf("at91twi_write: %02x %d\n", addr, len);
    284       1.2    matt 	#endif
    285       1.2    matt 	return at91twi_start(sc, addr, data, len, flags);
    286       1.2    matt }
    287       1.2    matt 
    288       1.2    matt int
    289       1.2    matt at91twi_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
    290       1.2    matt     size_t cmdlen, void *vbuf, size_t buflen, int flags)
    291       1.2    matt {
    292       1.2    matt 	struct at91twi_softc *sc = cookie;
    293       1.2    matt 
    294       1.2    matt 	if (I2C_OP_READ_P(op))
    295       1.2    matt 	{
    296       1.2    matt 		u_int iadr = 0;
    297       1.2    matt 		if (vcmd) {
    298       1.2    matt 			const uint8_t *cmd = (const uint8_t *)vcmd;
    299       1.2    matt 			if (cmdlen > 3) {
    300       1.2    matt 				// we're in trouble..
    301       1.2    matt 				return -1;
    302       1.2    matt 			}
    303       1.2    matt 			iadr = cmd[0];
    304       1.2    matt 			if (cmdlen > 1) {
    305       1.2    matt 				iadr <<= 8;
    306       1.2    matt 				iadr |= cmd[1];
    307       1.2    matt 			}
    308       1.2    matt 			if (cmdlen > 2) {
    309       1.2    matt 				iadr <<= 8;
    310       1.2    matt 				iadr |= cmd[2];
    311       1.2    matt 			}
    312       1.2    matt 		}
    313       1.2    matt 		at91twi_writereg(sc, TWI_MMR, (addr << 16) | TWI_MMR_MREAD | (cmdlen << 8));
    314       1.2    matt 		if (cmdlen > 0) {
    315       1.2    matt 	#ifdef AT91TWI_DEBUG
    316       1.2    matt 			printf("at91twi_read: %02x iadr=%08X mmr=%08X\n",
    317       1.2    matt 			       addr, iadr, at91twi_readreg(sc, TWI_MMR));
    318       1.2    matt 	#endif
    319       1.2    matt 			at91twi_writereg(sc, TWI_IADR, iadr);
    320       1.2    matt 		}
    321       1.2    matt 		if (at91twi_read(sc, addr, vbuf, buflen, flags) != 0)
    322       1.2    matt 			return -1;
    323       1.2    matt 	} else if (vcmd) {
    324       1.2    matt 		at91twi_writereg(sc, TWI_MMR, addr << 16);
    325       1.2    matt 		if (at91twi_write(sc, addr, __UNCONST(vcmd), cmdlen, flags) !=0)
    326       1.2    matt 			return -1;
    327       1.2    matt 	}
    328       1.2    matt 	return 0;
    329       1.2    matt }
    330