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