Home | History | Annotate | Line # | Download | only in xscale
iopi2c.c revision 1.9
      1  1.9   thorpej /*	$NetBSD: iopi2c.c,v 1.9 2019/12/22 23:23:30 thorpej Exp $	*/
      2  1.1   thorpej 
      3  1.1   thorpej /*
      4  1.1   thorpej  * Copyright (c) 2003 Wasabi Systems, Inc.
      5  1.1   thorpej  * All rights reserved.
      6  1.1   thorpej  *
      7  1.1   thorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  1.1   thorpej  *
      9  1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     10  1.1   thorpej  * modification, are permitted provided that the following conditions
     11  1.1   thorpej  * are met:
     12  1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     13  1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     14  1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     16  1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     17  1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     18  1.1   thorpej  *    must display the following acknowledgement:
     19  1.1   thorpej  *	This product includes software developed for the NetBSD Project by
     20  1.1   thorpej  *	Wasabi Systems, Inc.
     21  1.1   thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  1.1   thorpej  *    or promote products derived from this software without specific prior
     23  1.1   thorpej  *    written permission.
     24  1.1   thorpej  *
     25  1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     36  1.1   thorpej  */
     37  1.1   thorpej 
     38  1.1   thorpej /*
     39  1.1   thorpej  * Intel i80321 I/O Processor I2C Controller Unit support.
     40  1.1   thorpej  */
     41  1.1   thorpej 
     42  1.1   thorpej #include <sys/cdefs.h>
     43  1.9   thorpej __KERNEL_RCSID(0, "$NetBSD: iopi2c.c,v 1.9 2019/12/22 23:23:30 thorpej Exp $");
     44  1.1   thorpej 
     45  1.1   thorpej #include <sys/param.h>
     46  1.5        ad #include <sys/mutex.h>
     47  1.1   thorpej #include <sys/systm.h>
     48  1.1   thorpej #include <sys/device.h>
     49  1.1   thorpej #include <sys/kernel.h>
     50  1.1   thorpej 
     51  1.6    dyoung #include <sys/bus.h>
     52  1.1   thorpej #include <machine/intr.h>
     53  1.1   thorpej 
     54  1.1   thorpej #include <dev/i2c/i2cvar.h>
     55  1.1   thorpej 
     56  1.1   thorpej #include <arm/xscale/iopi2creg.h>
     57  1.1   thorpej #include <arm/xscale/iopi2cvar.h>
     58  1.1   thorpej 
     59  1.1   thorpej static int iopiic_send_start(void *, int);
     60  1.1   thorpej static int iopiic_send_stop(void *, int);
     61  1.1   thorpej static int iopiic_initiate_xfer(void *, uint16_t, int);
     62  1.1   thorpej static int iopiic_read_byte(void *, uint8_t *, int);
     63  1.1   thorpej static int iopiic_write_byte(void *, uint8_t, int);
     64  1.1   thorpej 
     65  1.1   thorpej void
     66  1.1   thorpej iopiic_attach(struct iopiic_softc *sc)
     67  1.1   thorpej {
     68  1.1   thorpej 	struct i2cbus_attach_args iba;
     69  1.1   thorpej 
     70  1.9   thorpej 	iic_tag_init(&sc->sc_i2c);
     71  1.1   thorpej 	sc->sc_i2c.ic_cookie = sc;
     72  1.1   thorpej 	sc->sc_i2c.ic_send_start = iopiic_send_start;
     73  1.1   thorpej 	sc->sc_i2c.ic_send_stop = iopiic_send_stop;
     74  1.1   thorpej 	sc->sc_i2c.ic_initiate_xfer = iopiic_initiate_xfer;
     75  1.1   thorpej 	sc->sc_i2c.ic_read_byte = iopiic_read_byte;
     76  1.1   thorpej 	sc->sc_i2c.ic_write_byte = iopiic_write_byte;
     77  1.1   thorpej 
     78  1.8       chs 	memset(&iba, 0, sizeof(iba));
     79  1.1   thorpej 	iba.iba_tag = &sc->sc_i2c;
     80  1.7  jakllsch 	(void) config_found_ia(sc->sc_dev, "i2cbus", &iba, iicbus_print);
     81  1.1   thorpej }
     82  1.1   thorpej 
     83  1.1   thorpej #define	IOPIIC_TIMEOUT		100	/* protocol timeout, in uSecs */
     84  1.1   thorpej 
     85  1.1   thorpej static int
     86  1.1   thorpej iopiic_wait(struct iopiic_softc *sc, int bit, int flags)
     87  1.1   thorpej {
     88  1.1   thorpej 	uint32_t isr;
     89  1.1   thorpej 	int timeout, error=0;
     90  1.1   thorpej 
     91  1.1   thorpej 	/* XXX We never sleep, we always poll.  Fix me. */
     92  1.1   thorpej 
     93  1.1   thorpej 	/*
     94  1.1   thorpej 	 * For some reason, we seem to run into problems if we poll
     95  1.1   thorpej 	 * the ISR while the transfer is in progress--at least on the
     96  1.1   thorpej 	 * i80312.  The condition that we're looking for never seems
     97  1.1   thorpej 	 * to appear on a read, and it's not clear why; perhaps reads
     98  1.1   thorpej 	 * of the I2C register file interfere with its proper operation?
     99  1.1   thorpej 	 * For now, just delay for a while up front.
    100  1.1   thorpej 	 *
    101  1.1   thorpej 	 * We _really_ need this to be interrupt-driven, but a problem
    102  1.1   thorpej 	 * with that is that the i80312 has no way to mask interrupts...
    103  1.1   thorpej 	 * So we need to deal with that.  For DMA and AAU, too, for that
    104  1.1   thorpej 	 * matter.
    105  1.1   thorpej 	 * Note that delay(100) doesn't quite work on the npwr w/ m41t00.
    106  1.1   thorpej 	 */
    107  1.1   thorpej 	delay(110);
    108  1.1   thorpej 	for (timeout = IOPIIC_TIMEOUT; timeout != 0; timeout--) {
    109  1.1   thorpej 		isr = bus_space_read_4(sc->sc_st, sc->sc_sh, IIC_ISR);
    110  1.1   thorpej 		if (isr & (bit | IIC_ISR_BED))
    111  1.1   thorpej 			break;
    112  1.1   thorpej 		delay(1);
    113  1.1   thorpej 	}
    114  1.1   thorpej 
    115  1.1   thorpej 	if (isr & (IIC_ISR_BED | (bit & IIC_ISR_ALD)))
    116  1.1   thorpej 		error = EIO;
    117  1.1   thorpej 	else if (isr & (bit & ~IIC_ISR_ALD))
    118  1.1   thorpej 		error = 0;
    119  1.1   thorpej 	else
    120  1.1   thorpej 		error = ETIMEDOUT;
    121  1.1   thorpej 
    122  1.1   thorpej 	if (error)
    123  1.7  jakllsch 		device_printf(sc->sc_dev,
    124  1.7  jakllsch 		    "iopiic_wait, (%08x) error %d: ISR = 0x%08x\n",
    125  1.7  jakllsch 		    bit, error, isr);
    126  1.1   thorpej 
    127  1.2       scw 	/*
    128  1.2       scw 	 * The IIC_ISR is Read/Clear apart from the bottom 4 bits, which are
    129  1.2       scw 	 * read-only. So simply write back our copy of the ISR to clear any
    130  1.2       scw 	 * latched status.
    131  1.2       scw 	 */
    132  1.2       scw 	bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ISR, isr);
    133  1.1   thorpej 
    134  1.1   thorpej 	return (error);
    135  1.1   thorpej }
    136  1.1   thorpej 
    137  1.1   thorpej static int
    138  1.1   thorpej iopiic_send_start(void *cookie, int flags)
    139  1.1   thorpej {
    140  1.1   thorpej 	struct iopiic_softc *sc = cookie;
    141  1.1   thorpej 
    142  1.1   thorpej 	/*
    143  1.1   thorpej 	 * This may only work in conjunction with a data transfer;
    144  1.1   thorpej 	 * we might need to un-export the "start" primitive.
    145  1.1   thorpej 	 */
    146  1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ICR,
    147  1.1   thorpej 	    sc->sc_icr | IIC_ICR_START);
    148  1.1   thorpej 	delay(IOPIIC_TIMEOUT);
    149  1.1   thorpej 
    150  1.1   thorpej 	return (0);
    151  1.1   thorpej }
    152  1.1   thorpej 
    153  1.1   thorpej static int
    154  1.1   thorpej iopiic_send_stop(void *cookie, int flags)
    155  1.1   thorpej {
    156  1.1   thorpej 	struct iopiic_softc *sc = cookie;
    157  1.1   thorpej 
    158  1.1   thorpej 	/*
    159  1.1   thorpej 	 * The STOP bit is only used in conjunction with
    160  1.1   thorpej 	 * a data transfer, so we need to use MA in this
    161  1.1   thorpej 	 * case.
    162  1.1   thorpej 	 *
    163  1.1   thorpej 	 * Consider adding an I2C_F_STOP so we can
    164  1.1   thorpej 	 * do a read-with-STOP and write-with-STOP.
    165  1.1   thorpej 	 */
    166  1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ICR,
    167  1.1   thorpej 	    sc->sc_icr | IIC_ICR_MA);
    168  1.1   thorpej 	delay(IOPIIC_TIMEOUT);
    169  1.1   thorpej 
    170  1.1   thorpej 	return (0);
    171  1.1   thorpej }
    172  1.1   thorpej 
    173  1.1   thorpej static int
    174  1.1   thorpej iopiic_initiate_xfer(void *cookie, uint16_t addr, int flags)
    175  1.1   thorpej {
    176  1.1   thorpej 	struct iopiic_softc *sc = cookie;
    177  1.1   thorpej 	int error, rd_req = (flags & I2C_F_READ) != 0;
    178  1.1   thorpej 	uint32_t idbr;
    179  1.1   thorpej 
    180  1.1   thorpej 	/* We only support 7-bit addressing. */
    181  1.1   thorpej 	if ((addr & 0x78) == 0x78)
    182  1.1   thorpej 		return (EINVAL);
    183  1.1   thorpej 
    184  1.1   thorpej 	idbr = (addr << 1) | (rd_req ? 1 : 0);
    185  1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_IDBR, idbr);
    186  1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ICR,
    187  1.1   thorpej 	    sc->sc_icr | IIC_ICR_START | IIC_ICR_TB);
    188  1.1   thorpej 
    189  1.1   thorpej 	error = iopiic_wait(sc, IIC_ISR_ITE, flags);
    190  1.1   thorpej #if 0
    191  1.1   thorpej 	if (error)
    192  1.7  jakllsch 		device_printf(sc->sc_dev, "failed to initiate %s xfer\n",
    193  1.1   thorpej 		    rd_req ? "read" : "write");
    194  1.1   thorpej #endif
    195  1.1   thorpej 	return (error);
    196  1.1   thorpej }
    197  1.1   thorpej 
    198  1.1   thorpej static int
    199  1.1   thorpej iopiic_read_byte(void *cookie, uint8_t *bytep, int flags)
    200  1.1   thorpej {
    201  1.1   thorpej 	struct iopiic_softc *sc = cookie;
    202  1.1   thorpej 	int error, last_byte = (flags & I2C_F_LAST) != 0,
    203  1.1   thorpej 	    send_stop = (flags & I2C_F_STOP) != 0;
    204  1.1   thorpej 
    205  1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ICR,
    206  1.1   thorpej 	    sc->sc_icr | IIC_ICR_TB | (last_byte ? IIC_ICR_NACK : 0) |
    207  1.1   thorpej 	    (send_stop ? IIC_ICR_STOP : 0));
    208  1.1   thorpej 	if ((error = iopiic_wait(sc, IIC_ISR_IRF | IIC_ISR_ALD, flags)) == 0)
    209  1.1   thorpej 		*bytep = bus_space_read_4(sc->sc_st, sc->sc_sh, IIC_IDBR);
    210  1.1   thorpej #if 0
    211  1.1   thorpej 	if (error)
    212  1.7  jakllsch 		device_printf(sc->sc_dev, "read byte failed\n");
    213  1.1   thorpej #endif
    214  1.1   thorpej 
    215  1.1   thorpej 	return (error);
    216  1.1   thorpej }
    217  1.1   thorpej 
    218  1.1   thorpej static int
    219  1.1   thorpej iopiic_write_byte(void *cookie, uint8_t byte, int flags)
    220  1.1   thorpej {
    221  1.1   thorpej 	struct iopiic_softc *sc = cookie;
    222  1.1   thorpej 	int error, send_stop = (flags & I2C_F_STOP) != 0;
    223  1.1   thorpej 
    224  1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_IDBR, byte);
    225  1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ICR,
    226  1.1   thorpej 	    sc->sc_icr | IIC_ICR_TB | (send_stop ? IIC_ICR_STOP : 0));
    227  1.1   thorpej 	error = iopiic_wait(sc, IIC_ISR_ITE | IIC_ISR_ALD, flags);
    228  1.1   thorpej 
    229  1.1   thorpej #if 0
    230  1.1   thorpej 	if (error)
    231  1.7  jakllsch 		device_printf(sc->sc_dev, "write byte failed\n");
    232  1.1   thorpej #endif
    233  1.1   thorpej 
    234  1.1   thorpej 	return (error);
    235  1.1   thorpej }
    236