Home | History | Annotate | Line # | Download | only in iomd
iomdiic.c revision 1.2.4.3
      1  1.2.4.3  skrll /*	$NetBSD: iomdiic.c,v 1.2.4.3 2004/09/18 14:32:32 skrll Exp $	*/
      2  1.2.4.2  skrll 
      3  1.2.4.2  skrll /*
      4  1.2.4.2  skrll  * Copyright (c) 2003 Wasabi Systems, Inc.
      5  1.2.4.2  skrll  * All rights reserved.
      6  1.2.4.2  skrll  *
      7  1.2.4.2  skrll  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  1.2.4.2  skrll  *
      9  1.2.4.2  skrll  * Redistribution and use in source and binary forms, with or without
     10  1.2.4.2  skrll  * modification, are permitted provided that the following conditions
     11  1.2.4.2  skrll  * are met:
     12  1.2.4.2  skrll  * 1. Redistributions of source code must retain the above copyright
     13  1.2.4.2  skrll  *    notice, this list of conditions and the following disclaimer.
     14  1.2.4.2  skrll  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.2.4.2  skrll  *    notice, this list of conditions and the following disclaimer in the
     16  1.2.4.2  skrll  *    documentation and/or other materials provided with the distribution.
     17  1.2.4.2  skrll  * 3. All advertising materials mentioning features or use of this software
     18  1.2.4.2  skrll  *    must display the following acknowledgement:
     19  1.2.4.2  skrll  *      This product includes software developed for the NetBSD Project by
     20  1.2.4.2  skrll  *      Wasabi Systems, Inc.
     21  1.2.4.2  skrll  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  1.2.4.2  skrll  *    or promote products derived from this software without specific prior
     23  1.2.4.2  skrll  *    written permission.
     24  1.2.4.2  skrll  *
     25  1.2.4.2  skrll  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  1.2.4.2  skrll  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  1.2.4.2  skrll  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  1.2.4.2  skrll  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  1.2.4.2  skrll  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  1.2.4.2  skrll  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  1.2.4.2  skrll  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  1.2.4.2  skrll  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  1.2.4.2  skrll  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  1.2.4.2  skrll  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  1.2.4.2  skrll  * POSSIBILITY OF SUCH DAMAGE.
     36  1.2.4.2  skrll  */
     37  1.2.4.2  skrll 
     38  1.2.4.2  skrll #include <sys/param.h>
     39  1.2.4.2  skrll #include <sys/device.h>
     40  1.2.4.2  skrll #include <sys/kernel.h>
     41  1.2.4.2  skrll #include <sys/systm.h>
     42  1.2.4.2  skrll #include <sys/lock.h>
     43  1.2.4.2  skrll 
     44  1.2.4.2  skrll #include <machine/bus.h>
     45  1.2.4.2  skrll #include <machine/cpu.h>
     46  1.2.4.2  skrll 
     47  1.2.4.2  skrll #include <arm/iomd/iomdreg.h>
     48  1.2.4.2  skrll #include <arm/iomd/iomdvar.h>
     49  1.2.4.2  skrll 
     50  1.2.4.2  skrll #include <dev/i2c/i2cvar.h>
     51  1.2.4.2  skrll #include <dev/i2c/i2c_bitbang.h>
     52  1.2.4.2  skrll 
     53  1.2.4.2  skrll #include <arm/iomd/iomdiicvar.h>
     54  1.2.4.2  skrll 
     55  1.2.4.2  skrll struct iomdiic_softc {
     56  1.2.4.2  skrll 	struct device sc_dev;
     57  1.2.4.2  skrll 	bus_space_tag_t sc_st;
     58  1.2.4.2  skrll 	bus_space_handle_t sc_sh;
     59  1.2.4.2  skrll 
     60  1.2.4.2  skrll 	struct i2c_controller sc_i2c;
     61  1.2.4.2  skrll 	struct lock sc_buslock;
     62  1.2.4.2  skrll 
     63  1.2.4.2  skrll 	/*
     64  1.2.4.2  skrll 	 * The SDA pin is open-drain, so we make it an input by
     65  1.2.4.2  skrll 	 * writing a 1 to it.
     66  1.2.4.2  skrll 	 */
     67  1.2.4.2  skrll 	uint8_t sc_iomd_iocr;
     68  1.2.4.2  skrll };
     69  1.2.4.2  skrll 
     70  1.2.4.2  skrll static int	iomdiic_acquire_bus(void *, int);
     71  1.2.4.2  skrll static void	iomdiic_release_bus(void *, int);
     72  1.2.4.2  skrll 
     73  1.2.4.2  skrll static int	iomdiic_send_start(void *, int);
     74  1.2.4.2  skrll static int	iomdiic_send_stop(void *, int);
     75  1.2.4.2  skrll static int	iomdiic_initiate_xfer(void *, i2c_addr_t, int);
     76  1.2.4.2  skrll static int	iomdiic_read_byte(void *, uint8_t *, int);
     77  1.2.4.2  skrll static int	iomdiic_write_byte(void *, uint8_t, int);
     78  1.2.4.2  skrll 
     79  1.2.4.2  skrll #define	IOMDIIC_READ	 *(__volatile uint8_t *)(IOMD_ADDRESS(IOMD_IOCR))
     80  1.2.4.2  skrll #define	IOMDIIC_WRITE(x) *(__volatile uint8_t *)(IOMD_ADDRESS(IOMD_IOCR)) = (x)
     81  1.2.4.2  skrll 
     82  1.2.4.2  skrll #define	IOMD_IOCR_SDA		0x01
     83  1.2.4.2  skrll #define	IOMD_IOCR_SCL		0x02
     84  1.2.4.2  skrll 
     85  1.2.4.2  skrll static void
     86  1.2.4.2  skrll iomdiic_bb_set_bits(void *cookie, uint32_t bits)
     87  1.2.4.2  skrll {
     88  1.2.4.2  skrll 	struct iomdiic_softc *sc = cookie;
     89  1.2.4.2  skrll 
     90  1.2.4.2  skrll 	IOMDIIC_WRITE((IOMDIIC_READ & ~(IOMD_IOCR_SDA|IOMD_IOCR_SCL)) |
     91  1.2.4.2  skrll 	    sc->sc_iomd_iocr | bits);
     92  1.2.4.2  skrll }
     93  1.2.4.2  skrll 
     94  1.2.4.2  skrll static void
     95  1.2.4.2  skrll iomdiic_bb_set_dir(void *cookie, uint32_t bits)
     96  1.2.4.2  skrll {
     97  1.2.4.2  skrll 	struct iomdiic_softc *sc = cookie;
     98  1.2.4.2  skrll 
     99  1.2.4.2  skrll 	sc->sc_iomd_iocr = bits;
    100  1.2.4.2  skrll }
    101  1.2.4.2  skrll 
    102  1.2.4.2  skrll static uint32_t
    103  1.2.4.2  skrll iomdiic_bb_read_bits(void *cookie)
    104  1.2.4.2  skrll {
    105  1.2.4.2  skrll 
    106  1.2.4.2  skrll 	return (IOMDIIC_READ);
    107  1.2.4.2  skrll }
    108  1.2.4.2  skrll 
    109  1.2.4.2  skrll static const struct i2c_bitbang_ops iomdiic_bbops = {
    110  1.2.4.2  skrll 	iomdiic_bb_set_bits,
    111  1.2.4.2  skrll 	iomdiic_bb_set_dir,
    112  1.2.4.2  skrll 	iomdiic_bb_read_bits,
    113  1.2.4.2  skrll 	{
    114  1.2.4.2  skrll 		IOMD_IOCR_SDA,		/* SDA */
    115  1.2.4.2  skrll 		IOMD_IOCR_SCL,		/* SCL */
    116  1.2.4.2  skrll 		0,			/* SDA is output */
    117  1.2.4.2  skrll 		IOMD_IOCR_SDA,		/* SDA is input */
    118  1.2.4.2  skrll 	}
    119  1.2.4.2  skrll };
    120  1.2.4.2  skrll 
    121  1.2.4.2  skrll static int
    122  1.2.4.2  skrll iomdiic_match(struct device *parent, struct cfdata *cf, void *aux)
    123  1.2.4.2  skrll {
    124  1.2.4.2  skrll 	struct iic_attach_args *ia = aux;
    125  1.2.4.2  skrll 
    126  1.2.4.2  skrll 	if (strcmp(ia->ia_name, "iic") == 0) return 1;
    127  1.2.4.2  skrll 	return 0;
    128  1.2.4.2  skrll }
    129  1.2.4.2  skrll 
    130  1.2.4.2  skrll static void
    131  1.2.4.2  skrll iomdiic_attach(struct device *parent, struct device *self, void *aux)
    132  1.2.4.2  skrll {
    133  1.2.4.2  skrll 	struct iomdiic_softc *sc = (void *) self;
    134  1.2.4.2  skrll 	struct i2cbus_attach_args iba;
    135  1.2.4.2  skrll 
    136  1.2.4.2  skrll 	printf("\n");
    137  1.2.4.2  skrll 
    138  1.2.4.2  skrll 	lockinit(&sc->sc_buslock, PRIBIO|PCATCH, "iomdiiclk", 0, 0);
    139  1.2.4.2  skrll 
    140  1.2.4.2  skrll 	sc->sc_i2c.ic_cookie = sc;
    141  1.2.4.2  skrll 	sc->sc_i2c.ic_acquire_bus = iomdiic_acquire_bus;
    142  1.2.4.2  skrll 	sc->sc_i2c.ic_release_bus = iomdiic_release_bus;
    143  1.2.4.2  skrll 	sc->sc_i2c.ic_send_start = iomdiic_send_start;
    144  1.2.4.2  skrll 	sc->sc_i2c.ic_send_stop = iomdiic_send_stop;
    145  1.2.4.2  skrll 	sc->sc_i2c.ic_initiate_xfer = iomdiic_initiate_xfer;
    146  1.2.4.2  skrll 	sc->sc_i2c.ic_read_byte = iomdiic_read_byte;
    147  1.2.4.2  skrll 	sc->sc_i2c.ic_write_byte = iomdiic_write_byte;
    148  1.2.4.2  skrll 
    149  1.2.4.2  skrll 	iba.iba_name = "iic";
    150  1.2.4.2  skrll 	iba.iba_tag = &sc->sc_i2c;
    151  1.2.4.2  skrll 	(void) config_found(&sc->sc_dev, &iba, iicbus_print);
    152  1.2.4.2  skrll }
    153  1.2.4.2  skrll 
    154  1.2.4.2  skrll CFATTACH_DECL(iomdiic, sizeof(struct iomdiic_softc),
    155  1.2.4.2  skrll     iomdiic_match, iomdiic_attach, NULL, NULL);
    156  1.2.4.2  skrll 
    157  1.2.4.2  skrll i2c_tag_t
    158  1.2.4.2  skrll iomdiic_bootstrap_cookie(void)
    159  1.2.4.2  skrll {
    160  1.2.4.2  skrll 	static struct iomdiic_softc sc;
    161  1.2.4.2  skrll 
    162  1.2.4.2  skrll 	/* XXX Yuck. */
    163  1.2.4.2  skrll 	strcpy(sc.sc_dev.dv_xname, "iomdiicboot");
    164  1.2.4.2  skrll 
    165  1.2.4.2  skrll 	sc.sc_i2c.ic_cookie = &sc;
    166  1.2.4.2  skrll 	sc.sc_i2c.ic_acquire_bus = iomdiic_acquire_bus;
    167  1.2.4.2  skrll 	sc.sc_i2c.ic_release_bus = iomdiic_release_bus;
    168  1.2.4.2  skrll 	sc.sc_i2c.ic_send_start = iomdiic_send_start;
    169  1.2.4.2  skrll 	sc.sc_i2c.ic_send_stop = iomdiic_send_stop;
    170  1.2.4.2  skrll 	sc.sc_i2c.ic_initiate_xfer = iomdiic_initiate_xfer;
    171  1.2.4.2  skrll 	sc.sc_i2c.ic_read_byte = iomdiic_read_byte;
    172  1.2.4.2  skrll 	sc.sc_i2c.ic_write_byte = iomdiic_write_byte;
    173  1.2.4.2  skrll 
    174  1.2.4.2  skrll 	return ((void *) &sc.sc_i2c);
    175  1.2.4.2  skrll }
    176  1.2.4.2  skrll 
    177  1.2.4.2  skrll static int
    178  1.2.4.2  skrll iomdiic_acquire_bus(void *cookie, int flags)
    179  1.2.4.2  skrll {
    180  1.2.4.2  skrll 	struct iomdiic_softc *sc = cookie;
    181  1.2.4.2  skrll 
    182  1.2.4.2  skrll 	/* XXX What should we do for the polling case? */
    183  1.2.4.2  skrll 	if (flags & I2C_F_POLL)
    184  1.2.4.2  skrll 		return (0);
    185  1.2.4.2  skrll 
    186  1.2.4.2  skrll 	return (lockmgr(&sc->sc_buslock, LK_EXCLUSIVE, NULL));
    187  1.2.4.2  skrll }
    188  1.2.4.2  skrll 
    189  1.2.4.2  skrll static void
    190  1.2.4.2  skrll iomdiic_release_bus(void *cookie, int flags)
    191  1.2.4.2  skrll {
    192  1.2.4.2  skrll 	struct iomdiic_softc *sc = cookie;
    193  1.2.4.2  skrll 
    194  1.2.4.2  skrll 	/* XXX See above. */
    195  1.2.4.2  skrll 	if (flags & I2C_F_POLL)
    196  1.2.4.2  skrll 		return;
    197  1.2.4.2  skrll 
    198  1.2.4.2  skrll 	(void) lockmgr(&sc->sc_buslock, LK_RELEASE, NULL);
    199  1.2.4.2  skrll }
    200  1.2.4.2  skrll 
    201  1.2.4.2  skrll static int
    202  1.2.4.2  skrll iomdiic_send_start(void *cookie, int flags)
    203  1.2.4.2  skrll {
    204  1.2.4.2  skrll 
    205  1.2.4.2  skrll 	return (i2c_bitbang_send_start(cookie, flags, &iomdiic_bbops));
    206  1.2.4.2  skrll }
    207  1.2.4.2  skrll 
    208  1.2.4.2  skrll static int
    209  1.2.4.2  skrll iomdiic_send_stop(void *cookie, int flags)
    210  1.2.4.2  skrll {
    211  1.2.4.2  skrll 
    212  1.2.4.2  skrll 	return (i2c_bitbang_send_stop(cookie, flags, &iomdiic_bbops));
    213  1.2.4.2  skrll }
    214  1.2.4.2  skrll 
    215  1.2.4.2  skrll static int
    216  1.2.4.2  skrll iomdiic_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
    217  1.2.4.2  skrll {
    218  1.2.4.2  skrll 
    219  1.2.4.2  skrll 	return (i2c_bitbang_initiate_xfer(cookie, addr, flags, &iomdiic_bbops));
    220  1.2.4.2  skrll }
    221  1.2.4.2  skrll 
    222  1.2.4.2  skrll static int
    223  1.2.4.2  skrll iomdiic_read_byte(void *cookie, uint8_t *bytep, int flags)
    224  1.2.4.2  skrll {
    225  1.2.4.2  skrll 
    226  1.2.4.2  skrll 	return (i2c_bitbang_read_byte(cookie, bytep, flags, &iomdiic_bbops));
    227  1.2.4.2  skrll }
    228  1.2.4.2  skrll 
    229  1.2.4.2  skrll static int
    230  1.2.4.2  skrll iomdiic_write_byte(void *cookie, uint8_t byte, int flags)
    231  1.2.4.2  skrll {
    232  1.2.4.2  skrll 
    233  1.2.4.2  skrll 	return (i2c_bitbang_write_byte(cookie, byte, flags, &iomdiic_bbops));
    234  1.2.4.2  skrll }
    235