Home | History | Annotate | Line # | Download | only in xscale
iopi2c.c revision 1.5
      1  1.5        ad /*	$NetBSD: iopi2c.c,v 1.5 2007/12/06 17:00:32 ad 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.5        ad __KERNEL_RCSID(0, "$NetBSD: iopi2c.c,v 1.5 2007/12/06 17:00:32 ad 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.1   thorpej #include <machine/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_acquire_bus(void *, int);
     60  1.1   thorpej static void iopiic_release_bus(void *, int);
     61  1.1   thorpej 
     62  1.1   thorpej static int iopiic_send_start(void *, int);
     63  1.1   thorpej static int iopiic_send_stop(void *, int);
     64  1.1   thorpej static int iopiic_initiate_xfer(void *, uint16_t, int);
     65  1.1   thorpej static int iopiic_read_byte(void *, uint8_t *, int);
     66  1.1   thorpej static int iopiic_write_byte(void *, uint8_t, int);
     67  1.1   thorpej 
     68  1.1   thorpej void
     69  1.1   thorpej iopiic_attach(struct iopiic_softc *sc)
     70  1.1   thorpej {
     71  1.1   thorpej 	struct i2cbus_attach_args iba;
     72  1.1   thorpej 
     73  1.1   thorpej 	sc->sc_i2c.ic_cookie = sc;
     74  1.1   thorpej 	sc->sc_i2c.ic_acquire_bus = iopiic_acquire_bus;
     75  1.1   thorpej 	sc->sc_i2c.ic_release_bus = iopiic_release_bus;
     76  1.1   thorpej 	sc->sc_i2c.ic_send_start = iopiic_send_start;
     77  1.1   thorpej 	sc->sc_i2c.ic_send_stop = iopiic_send_stop;
     78  1.1   thorpej 	sc->sc_i2c.ic_initiate_xfer = iopiic_initiate_xfer;
     79  1.1   thorpej 	sc->sc_i2c.ic_read_byte = iopiic_read_byte;
     80  1.1   thorpej 	sc->sc_i2c.ic_write_byte = iopiic_write_byte;
     81  1.1   thorpej 
     82  1.1   thorpej 	iba.iba_tag = &sc->sc_i2c;
     83  1.4  drochner 	(void) config_found_ia(&sc->sc_dev, "i2cbus", &iba, iicbus_print);
     84  1.1   thorpej }
     85  1.1   thorpej 
     86  1.1   thorpej static int
     87  1.1   thorpej iopiic_acquire_bus(void *cookie, int flags)
     88  1.1   thorpej {
     89  1.1   thorpej 	struct iopiic_softc *sc = cookie;
     90  1.1   thorpej 
     91  1.1   thorpej 	/* XXX What should we do for the polling case? */
     92  1.1   thorpej 	if (flags & I2C_F_POLL)
     93  1.1   thorpej 		return (0);
     94  1.1   thorpej 
     95  1.5        ad 	mutex_enter(&sc->sc_buslock);
     96  1.5        ad 	return (0);
     97  1.1   thorpej }
     98  1.1   thorpej 
     99  1.1   thorpej static void
    100  1.1   thorpej iopiic_release_bus(void *cookie, int flags)
    101  1.1   thorpej {
    102  1.1   thorpej 	struct iopiic_softc *sc = cookie;
    103  1.1   thorpej 
    104  1.1   thorpej 	/* XXX See above. */
    105  1.1   thorpej 	if (flags & I2C_F_POLL)
    106  1.1   thorpej 		return;
    107  1.1   thorpej 
    108  1.5        ad 	mutex_exit(&sc->sc_buslock);
    109  1.1   thorpej }
    110  1.1   thorpej 
    111  1.1   thorpej #define	IOPIIC_TIMEOUT		100	/* protocol timeout, in uSecs */
    112  1.1   thorpej 
    113  1.1   thorpej static int
    114  1.1   thorpej iopiic_wait(struct iopiic_softc *sc, int bit, int flags)
    115  1.1   thorpej {
    116  1.1   thorpej 	uint32_t isr;
    117  1.1   thorpej 	int timeout, error=0;
    118  1.1   thorpej 
    119  1.1   thorpej 	/* XXX We never sleep, we always poll.  Fix me. */
    120  1.1   thorpej 
    121  1.1   thorpej 	/*
    122  1.1   thorpej 	 * For some reason, we seem to run into problems if we poll
    123  1.1   thorpej 	 * the ISR while the transfer is in progress--at least on the
    124  1.1   thorpej 	 * i80312.  The condition that we're looking for never seems
    125  1.1   thorpej 	 * to appear on a read, and it's not clear why; perhaps reads
    126  1.1   thorpej 	 * of the I2C register file interfere with its proper operation?
    127  1.1   thorpej 	 * For now, just delay for a while up front.
    128  1.1   thorpej 	 *
    129  1.1   thorpej 	 * We _really_ need this to be interrupt-driven, but a problem
    130  1.1   thorpej 	 * with that is that the i80312 has no way to mask interrupts...
    131  1.1   thorpej 	 * So we need to deal with that.  For DMA and AAU, too, for that
    132  1.1   thorpej 	 * matter.
    133  1.1   thorpej 	 * Note that delay(100) doesn't quite work on the npwr w/ m41t00.
    134  1.1   thorpej 	 */
    135  1.1   thorpej 	delay(110);
    136  1.1   thorpej 	for (timeout = IOPIIC_TIMEOUT; timeout != 0; timeout--) {
    137  1.1   thorpej 		isr = bus_space_read_4(sc->sc_st, sc->sc_sh, IIC_ISR);
    138  1.1   thorpej 		if (isr & (bit | IIC_ISR_BED))
    139  1.1   thorpej 			break;
    140  1.1   thorpej 		delay(1);
    141  1.1   thorpej 	}
    142  1.1   thorpej 
    143  1.1   thorpej 	if (isr & (IIC_ISR_BED | (bit & IIC_ISR_ALD)))
    144  1.1   thorpej 		error = EIO;
    145  1.1   thorpej 	else if (isr & (bit & ~IIC_ISR_ALD))
    146  1.1   thorpej 		error = 0;
    147  1.1   thorpej 	else
    148  1.1   thorpej 		error = ETIMEDOUT;
    149  1.1   thorpej 
    150  1.1   thorpej 	if (error)
    151  1.1   thorpej 		printf("%s: iopiic_wait, (%08x) error %d: ISR = 0x%08x\n",
    152  1.1   thorpej 		    sc->sc_dev.dv_xname, bit, error, isr);
    153  1.1   thorpej 
    154  1.2       scw 	/*
    155  1.2       scw 	 * The IIC_ISR is Read/Clear apart from the bottom 4 bits, which are
    156  1.2       scw 	 * read-only. So simply write back our copy of the ISR to clear any
    157  1.2       scw 	 * latched status.
    158  1.2       scw 	 */
    159  1.2       scw 	bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ISR, isr);
    160  1.1   thorpej 
    161  1.1   thorpej 	return (error);
    162  1.1   thorpej }
    163  1.1   thorpej 
    164  1.1   thorpej static int
    165  1.1   thorpej iopiic_send_start(void *cookie, int flags)
    166  1.1   thorpej {
    167  1.1   thorpej 	struct iopiic_softc *sc = cookie;
    168  1.1   thorpej 
    169  1.1   thorpej 	/*
    170  1.1   thorpej 	 * This may only work in conjunction with a data transfer;
    171  1.1   thorpej 	 * we might need to un-export the "start" primitive.
    172  1.1   thorpej 	 */
    173  1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ICR,
    174  1.1   thorpej 	    sc->sc_icr | IIC_ICR_START);
    175  1.1   thorpej 	delay(IOPIIC_TIMEOUT);
    176  1.1   thorpej 
    177  1.1   thorpej 	return (0);
    178  1.1   thorpej }
    179  1.1   thorpej 
    180  1.1   thorpej static int
    181  1.1   thorpej iopiic_send_stop(void *cookie, int flags)
    182  1.1   thorpej {
    183  1.1   thorpej 	struct iopiic_softc *sc = cookie;
    184  1.1   thorpej 
    185  1.1   thorpej 	/*
    186  1.1   thorpej 	 * The STOP bit is only used in conjunction with
    187  1.1   thorpej 	 * a data transfer, so we need to use MA in this
    188  1.1   thorpej 	 * case.
    189  1.1   thorpej 	 *
    190  1.1   thorpej 	 * Consider adding an I2C_F_STOP so we can
    191  1.1   thorpej 	 * do a read-with-STOP and write-with-STOP.
    192  1.1   thorpej 	 */
    193  1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ICR,
    194  1.1   thorpej 	    sc->sc_icr | IIC_ICR_MA);
    195  1.1   thorpej 	delay(IOPIIC_TIMEOUT);
    196  1.1   thorpej 
    197  1.1   thorpej 	return (0);
    198  1.1   thorpej }
    199  1.1   thorpej 
    200  1.1   thorpej static int
    201  1.1   thorpej iopiic_initiate_xfer(void *cookie, uint16_t addr, int flags)
    202  1.1   thorpej {
    203  1.1   thorpej 	struct iopiic_softc *sc = cookie;
    204  1.1   thorpej 	int error, rd_req = (flags & I2C_F_READ) != 0;
    205  1.1   thorpej 	uint32_t idbr;
    206  1.1   thorpej 
    207  1.1   thorpej 	/* We only support 7-bit addressing. */
    208  1.1   thorpej 	if ((addr & 0x78) == 0x78)
    209  1.1   thorpej 		return (EINVAL);
    210  1.1   thorpej 
    211  1.1   thorpej 	idbr = (addr << 1) | (rd_req ? 1 : 0);
    212  1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_IDBR, idbr);
    213  1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ICR,
    214  1.1   thorpej 	    sc->sc_icr | IIC_ICR_START | IIC_ICR_TB);
    215  1.1   thorpej 
    216  1.1   thorpej 	error = iopiic_wait(sc, IIC_ISR_ITE, flags);
    217  1.1   thorpej #if 0
    218  1.1   thorpej 	if (error)
    219  1.1   thorpej 		printf("%s: failed to initiate %s xfer\n", sc->sc_dev.dv_xname,
    220  1.1   thorpej 		    rd_req ? "read" : "write");
    221  1.1   thorpej #endif
    222  1.1   thorpej 	return (error);
    223  1.1   thorpej }
    224  1.1   thorpej 
    225  1.1   thorpej static int
    226  1.1   thorpej iopiic_read_byte(void *cookie, uint8_t *bytep, int flags)
    227  1.1   thorpej {
    228  1.1   thorpej 	struct iopiic_softc *sc = cookie;
    229  1.1   thorpej 	int error, last_byte = (flags & I2C_F_LAST) != 0,
    230  1.1   thorpej 	    send_stop = (flags & I2C_F_STOP) != 0;
    231  1.1   thorpej 
    232  1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ICR,
    233  1.1   thorpej 	    sc->sc_icr | IIC_ICR_TB | (last_byte ? IIC_ICR_NACK : 0) |
    234  1.1   thorpej 	    (send_stop ? IIC_ICR_STOP : 0));
    235  1.1   thorpej 	if ((error = iopiic_wait(sc, IIC_ISR_IRF | IIC_ISR_ALD, flags)) == 0)
    236  1.1   thorpej 		*bytep = bus_space_read_4(sc->sc_st, sc->sc_sh, IIC_IDBR);
    237  1.1   thorpej #if 0
    238  1.1   thorpej 	if (error)
    239  1.1   thorpej 		printf("%s: read byte failed\n", sc->sc_dev.dv_xname);
    240  1.1   thorpej #endif
    241  1.1   thorpej 
    242  1.1   thorpej 	return (error);
    243  1.1   thorpej }
    244  1.1   thorpej 
    245  1.1   thorpej static int
    246  1.1   thorpej iopiic_write_byte(void *cookie, uint8_t byte, int flags)
    247  1.1   thorpej {
    248  1.1   thorpej 	struct iopiic_softc *sc = cookie;
    249  1.1   thorpej 	int error, send_stop = (flags & I2C_F_STOP) != 0;
    250  1.1   thorpej 
    251  1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_IDBR, byte);
    252  1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, IIC_ICR,
    253  1.1   thorpej 	    sc->sc_icr | IIC_ICR_TB | (send_stop ? IIC_ICR_STOP : 0));
    254  1.1   thorpej 	error = iopiic_wait(sc, IIC_ISR_ITE | IIC_ISR_ALD, flags);
    255  1.1   thorpej 
    256  1.1   thorpej #if 0
    257  1.1   thorpej 	if (error)
    258  1.1   thorpej 		printf("%s: write byte failed\n", sc->sc_dev.dv_xname);
    259  1.1   thorpej #endif
    260  1.1   thorpej 
    261  1.1   thorpej 	return (error);
    262  1.1   thorpej }
    263