Home | History | Annotate | Line # | Download | only in iomd
iomdiic.c revision 1.1
      1  1.1  thorpej /*	$NetBSD: iomdiic.c,v 1.1 2003/10/06 16:11:19 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 #include <sys/param.h>
     39  1.1  thorpej #include <sys/device.h>
     40  1.1  thorpej #include <sys/kernel.h>
     41  1.1  thorpej #include <sys/systm.h>
     42  1.1  thorpej #include <sys/lock.h>
     43  1.1  thorpej 
     44  1.1  thorpej #include <machine/bus.h>
     45  1.1  thorpej #include <machine/cpu.h>
     46  1.1  thorpej 
     47  1.1  thorpej #include <arm/iomd/iomdreg.h>
     48  1.1  thorpej #include <arm/iomd/iomdvar.h>
     49  1.1  thorpej 
     50  1.1  thorpej #include <dev/i2c/i2cvar.h>
     51  1.1  thorpej #include <dev/i2c/i2c_bitbang.h>
     52  1.1  thorpej 
     53  1.1  thorpej #include <arm/iomd/iomdiicvar.h>
     54  1.1  thorpej 
     55  1.1  thorpej struct iomdiic_softc {
     56  1.1  thorpej 	struct device sc_dev;
     57  1.1  thorpej 	bus_space_tag_t sc_st;
     58  1.1  thorpej 	bus_space_handle_t sc_sh;
     59  1.1  thorpej 
     60  1.1  thorpej 	struct i2c_controller sc_i2c;
     61  1.1  thorpej 	struct lock sc_buslock;
     62  1.1  thorpej 
     63  1.1  thorpej 	/*
     64  1.1  thorpej 	 * The SDA pin is open-drain, so we make it an input by
     65  1.1  thorpej 	 * writing a 1 to it.
     66  1.1  thorpej 	 */
     67  1.1  thorpej 	uint8_t sc_iomd_iocr;
     68  1.1  thorpej };
     69  1.1  thorpej 
     70  1.1  thorpej static int	iomdiic_acquire_bus(void *, int);
     71  1.1  thorpej static void	iomdiic_release_bus(void *, int);
     72  1.1  thorpej 
     73  1.1  thorpej static int	iomdiic_send_start(void *, int);
     74  1.1  thorpej static int	iomdiic_send_stop(void *, int);
     75  1.1  thorpej static int	iomdiic_initiate_xfer(void *, i2c_addr_t, int);
     76  1.1  thorpej static int	iomdiic_read_byte(void *, uint8_t *, int);
     77  1.1  thorpej static int	iomdiic_write_byte(void *, uint8_t, int);
     78  1.1  thorpej 
     79  1.1  thorpej #define	IOMDIIC_READ	 *(__volatile uint8_t *)(IOMD_ADDRESS(IOMD_IOCR))
     80  1.1  thorpej #define	IOMDIIC_WRITE(x) *(__volatile uint8_t *)(IOMD_ADDRESS(IOMD_IOCR)) = (x)
     81  1.1  thorpej 
     82  1.1  thorpej #define	IOMD_IOCR_SDA		0x01
     83  1.1  thorpej #define	IOMD_IOCR_SCL		0x02
     84  1.1  thorpej 
     85  1.1  thorpej static void
     86  1.1  thorpej iomdiic_bb_set_bits(void *cookie, uint32_t bits)
     87  1.1  thorpej {
     88  1.1  thorpej 	struct iomdiic_softc *sc = cookie;
     89  1.1  thorpej 
     90  1.1  thorpej 	IOMDIIC_WRITE((IOMDIIC_READ & ~(IOMD_IOCR_SDA|IOMD_IOCR_SCL)) |
     91  1.1  thorpej 	    sc->sc_iomd_iocr | bits);
     92  1.1  thorpej }
     93  1.1  thorpej 
     94  1.1  thorpej static void
     95  1.1  thorpej iomdiic_bb_set_dir(void *cookie, uint32_t bits)
     96  1.1  thorpej {
     97  1.1  thorpej 	struct iomdiic_softc *sc = cookie;
     98  1.1  thorpej 
     99  1.1  thorpej 	sc->sc_iomd_iocr = bits;
    100  1.1  thorpej }
    101  1.1  thorpej 
    102  1.1  thorpej static uint32_t
    103  1.1  thorpej iomdiic_bb_read_bits(void *cookie)
    104  1.1  thorpej {
    105  1.1  thorpej 
    106  1.1  thorpej 	return (IOMDIIC_READ);
    107  1.1  thorpej }
    108  1.1  thorpej 
    109  1.1  thorpej static const struct i2c_bitbang_ops iomdiic_bbops = {
    110  1.1  thorpej 	iomdiic_bb_set_bits,
    111  1.1  thorpej 	iomdiic_bb_set_dir,
    112  1.1  thorpej 	iomdiic_bb_read_bits,
    113  1.1  thorpej 	{
    114  1.1  thorpej 		IOMD_IOCR_SDA,		/* SDA */
    115  1.1  thorpej 		IOMD_IOCR_SCL,		/* SCL */
    116  1.1  thorpej 		0,			/* SDA is output */
    117  1.1  thorpej 		IOMD_IOCR_SDA,		/* SDA is input */
    118  1.1  thorpej 	}
    119  1.1  thorpej };
    120  1.1  thorpej 
    121  1.1  thorpej static int
    122  1.1  thorpej iomdiic_match(struct device *parent, struct cfdata *cf, void *aux)
    123  1.1  thorpej {
    124  1.1  thorpej 
    125  1.1  thorpej 	return (1);
    126  1.1  thorpej }
    127  1.1  thorpej 
    128  1.1  thorpej static void
    129  1.1  thorpej iomdiic_attach(struct device *parent, struct device *self, void *aux)
    130  1.1  thorpej {
    131  1.1  thorpej 	struct iomdiic_softc *sc = (void *) self;
    132  1.1  thorpej 	struct i2cbus_attach_args iba;
    133  1.1  thorpej 
    134  1.1  thorpej 	printf("\n");
    135  1.1  thorpej 
    136  1.1  thorpej 	lockinit(&sc->sc_buslock, PRIBIO|PCATCH, "iomdiiclk", 0, 0);
    137  1.1  thorpej 
    138  1.1  thorpej 	sc->sc_i2c.ic_cookie = sc;
    139  1.1  thorpej 	sc->sc_i2c.ic_acquire_bus = iomdiic_acquire_bus;
    140  1.1  thorpej 	sc->sc_i2c.ic_release_bus = iomdiic_release_bus;
    141  1.1  thorpej 	sc->sc_i2c.ic_send_start = iomdiic_send_start;
    142  1.1  thorpej 	sc->sc_i2c.ic_send_stop = iomdiic_send_stop;
    143  1.1  thorpej 	sc->sc_i2c.ic_initiate_xfer = iomdiic_initiate_xfer;
    144  1.1  thorpej 	sc->sc_i2c.ic_read_byte = iomdiic_read_byte;
    145  1.1  thorpej 	sc->sc_i2c.ic_write_byte = iomdiic_write_byte;
    146  1.1  thorpej 
    147  1.1  thorpej 	iba.iba_name = "iic";
    148  1.1  thorpej 	iba.iba_tag = &sc->sc_i2c;
    149  1.1  thorpej 	(void) config_found(&sc->sc_dev, &iba, iicbus_print);
    150  1.1  thorpej }
    151  1.1  thorpej 
    152  1.1  thorpej CFATTACH_DECL(iomdiic, sizeof(struct iomdiic_softc),
    153  1.1  thorpej     iomdiic_match, iomdiic_attach, NULL, NULL);
    154  1.1  thorpej 
    155  1.1  thorpej i2c_tag_t
    156  1.1  thorpej iomdiic_bootstrap_cookie(void)
    157  1.1  thorpej {
    158  1.1  thorpej 	static struct iomdiic_softc sc;
    159  1.1  thorpej 
    160  1.1  thorpej 	/* XXX Yuck. */
    161  1.1  thorpej 	strcpy(sc.sc_dev.dv_xname, "iomdiicboot");
    162  1.1  thorpej 
    163  1.1  thorpej 	sc.sc_i2c.ic_cookie = &sc;
    164  1.1  thorpej 	sc.sc_i2c.ic_acquire_bus = iomdiic_acquire_bus;
    165  1.1  thorpej 	sc.sc_i2c.ic_release_bus = iomdiic_release_bus;
    166  1.1  thorpej 	sc.sc_i2c.ic_send_start = iomdiic_send_start;
    167  1.1  thorpej 	sc.sc_i2c.ic_send_stop = iomdiic_send_stop;
    168  1.1  thorpej 	sc.sc_i2c.ic_initiate_xfer = iomdiic_initiate_xfer;
    169  1.1  thorpej 	sc.sc_i2c.ic_read_byte = iomdiic_read_byte;
    170  1.1  thorpej 	sc.sc_i2c.ic_write_byte = iomdiic_write_byte;
    171  1.1  thorpej 
    172  1.1  thorpej 	return ((void *) &sc.sc_i2c);
    173  1.1  thorpej }
    174  1.1  thorpej 
    175  1.1  thorpej static int
    176  1.1  thorpej iomdiic_acquire_bus(void *cookie, int flags)
    177  1.1  thorpej {
    178  1.1  thorpej 	struct iomdiic_softc *sc = cookie;
    179  1.1  thorpej 
    180  1.1  thorpej 	/* XXX What should we do for the polling case? */
    181  1.1  thorpej 	if (flags & I2C_F_POLL)
    182  1.1  thorpej 		return (0);
    183  1.1  thorpej 
    184  1.1  thorpej 	return (lockmgr(&sc->sc_buslock, LK_EXCLUSIVE, NULL));
    185  1.1  thorpej }
    186  1.1  thorpej 
    187  1.1  thorpej static void
    188  1.1  thorpej iomdiic_release_bus(void *cookie, int flags)
    189  1.1  thorpej {
    190  1.1  thorpej 	struct iomdiic_softc *sc = cookie;
    191  1.1  thorpej 
    192  1.1  thorpej 	/* XXX See above. */
    193  1.1  thorpej 	if (flags & I2C_F_POLL)
    194  1.1  thorpej 		return;
    195  1.1  thorpej 
    196  1.1  thorpej 	(void) lockmgr(&sc->sc_buslock, LK_RELEASE, NULL);
    197  1.1  thorpej }
    198  1.1  thorpej 
    199  1.1  thorpej static int
    200  1.1  thorpej iomdiic_send_start(void *cookie, int flags)
    201  1.1  thorpej {
    202  1.1  thorpej 
    203  1.1  thorpej 	return (i2c_bitbang_send_start(cookie, flags, &iomdiic_bbops));
    204  1.1  thorpej }
    205  1.1  thorpej 
    206  1.1  thorpej static int
    207  1.1  thorpej iomdiic_send_stop(void *cookie, int flags)
    208  1.1  thorpej {
    209  1.1  thorpej 
    210  1.1  thorpej 	return (i2c_bitbang_send_stop(cookie, flags, &iomdiic_bbops));
    211  1.1  thorpej }
    212  1.1  thorpej 
    213  1.1  thorpej static int
    214  1.1  thorpej iomdiic_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
    215  1.1  thorpej {
    216  1.1  thorpej 
    217  1.1  thorpej 	return (i2c_bitbang_initiate_xfer(cookie, addr, flags, &iomdiic_bbops));
    218  1.1  thorpej }
    219  1.1  thorpej 
    220  1.1  thorpej static int
    221  1.1  thorpej iomdiic_read_byte(void *cookie, uint8_t *bytep, int flags)
    222  1.1  thorpej {
    223  1.1  thorpej 
    224  1.1  thorpej 	return (i2c_bitbang_read_byte(cookie, bytep, flags, &iomdiic_bbops));
    225  1.1  thorpej }
    226  1.1  thorpej 
    227  1.1  thorpej static int
    228  1.1  thorpej iomdiic_write_byte(void *cookie, uint8_t byte, int flags)
    229  1.1  thorpej {
    230  1.1  thorpej 
    231  1.1  thorpej 	return (i2c_bitbang_write_byte(cookie, byte, flags, &iomdiic_bbops));
    232  1.1  thorpej }
    233