Home | History | Annotate | Line # | Download | only in nslu2
nslu2_iic.c revision 1.1
      1 /*	$NetBSD: nslu2_iic.c,v 1.1 2006/02/28 20:40:33 scw Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Steve C. Woodford.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/kernel.h>
     42 #include <sys/device.h>
     43 #include <sys/lock.h>
     44 
     45 #include <machine/bus.h>
     46 
     47 #include <dev/i2c/i2cvar.h>
     48 #include <dev/i2c/i2c_bitbang.h>
     49 
     50 #include <arm/xscale/ixp425reg.h>
     51 #include <arm/xscale/ixp425var.h>
     52 
     53 #include <evbarm/nslu2/nslu2reg.h>
     54 
     55 struct slugiic_softc {
     56 	struct device sc_dev;
     57 	struct i2c_controller sc_ic;
     58 	struct i2c_bitbang_ops sc_ibo;
     59 	struct lock sc_lock;
     60 	uint32_t sc_dirout;
     61 };
     62 
     63 static int
     64 slugiic_acquire_bus(void *arg, int flags)
     65 {
     66 	struct slugiic_softc *sc = arg;
     67 
     68 	if (flags & I2C_F_POLL)
     69 		return (0);
     70 
     71 	return (lockmgr(&sc->sc_lock, LK_EXCLUSIVE, NULL));
     72 }
     73 
     74 static void
     75 slugiic_release_bus(void *arg, int flags)
     76 {
     77 	struct slugiic_softc *sc = arg;
     78 
     79 	if (flags & I2C_F_POLL)
     80 		return;
     81 
     82 	(void) lockmgr(&sc->sc_lock, LK_RELEASE, NULL);
     83 }
     84 
     85 static int
     86 slugiic_send_start(void *arg, int flags)
     87 {
     88 	struct slugiic_softc *sc = arg;
     89 
     90 	return (i2c_bitbang_send_start(sc, flags, &sc->sc_ibo));
     91 }
     92 
     93 static int
     94 slugiic_send_stop(void *arg, int flags)
     95 {
     96 	struct slugiic_softc *sc = arg;
     97 
     98 	return (i2c_bitbang_send_stop(sc, flags, &sc->sc_ibo));
     99 }
    100 
    101 static int
    102 slugiic_initiate_xfer(void *arg, i2c_addr_t addr, int flags)
    103 {
    104 	struct slugiic_softc *sc = arg;
    105 
    106 	return (i2c_bitbang_initiate_xfer(sc, addr, flags, &sc->sc_ibo));
    107 }
    108 
    109 static int
    110 slugiic_read_byte(void *arg, uint8_t *vp, int flags)
    111 {
    112 	struct slugiic_softc *sc = arg;
    113 
    114 	return (i2c_bitbang_read_byte(sc, vp, flags, &sc->sc_ibo));
    115 }
    116 
    117 static int
    118 slugiic_write_byte(void *arg, uint8_t v, int flags)
    119 {
    120 	struct slugiic_softc *sc = arg;
    121 
    122 	return (i2c_bitbang_write_byte(sc, v, flags, &sc->sc_ibo));
    123 }
    124 
    125 static void
    126 slugiic_set_dir(void *arg, uint32_t bits)
    127 {
    128 	struct slugiic_softc *sc = arg;
    129 	uint32_t reg;
    130 	int s;
    131 
    132 	if (sc->sc_dirout == (bits ^ GPIO_I2C_SDA_BIT))
    133 		return;
    134 
    135 	s = splhigh();
    136 
    137 	sc->sc_dirout = bits ^ GPIO_I2C_SDA_BIT;
    138 
    139 	reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOER);
    140 	reg &= ~GPIO_I2C_SDA_BIT;
    141 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOER, reg | bits);
    142 
    143 	splx(s);
    144 }
    145 
    146 static void
    147 slugiic_set_bits(void *arg, uint32_t bits)
    148 {
    149 	struct slugiic_softc *sc = arg;
    150 	uint32_t reg;
    151 	int s;
    152 
    153 	if (sc->sc_dirout == 0 && !(bits & GPIO_I2C_SDA_BIT))
    154 		bits |= GPIO_I2C_SDA_BIT;
    155 
    156 	s = splhigh();
    157 
    158 	reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
    159 	reg &= ~(GPIO_I2C_SDA_BIT | GPIO_I2C_SCL_BIT);
    160 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg | bits);
    161 
    162 	splx(s);
    163 }
    164 
    165 static uint32_t
    166 slugiic_read_bits(void *arg)
    167 {
    168 	uint32_t reg;
    169 
    170 	reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPINR);
    171 	return (reg & (GPIO_I2C_SDA_BIT | GPIO_I2C_SCL_BIT));
    172 }
    173 
    174 static void
    175 slugiic_deferred_attach(struct device *device)
    176 {
    177 	struct slugiic_softc *sc = (struct slugiic_softc *)device;
    178 	struct i2cbus_attach_args iba;
    179 	uint32_t reg;
    180 
    181 	reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
    182 	reg |= GPIO_I2C_SDA_BIT | GPIO_I2C_SCL_BIT;
    183 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg);
    184 
    185 	reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOER);
    186 	reg &= ~GPIO_I2C_SCL_BIT;
    187 	reg |= GPIO_I2C_SDA_BIT;
    188 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOER, reg);
    189 
    190 	iba.iba_name = "iic";
    191 	iba.iba_tag = &sc->sc_ic;
    192 	(void) config_found(&sc->sc_dev, &iba, iicbus_print);
    193 }
    194 
    195 static int
    196 slugiic_match(struct device *parent, struct cfdata *cf, void *arg)
    197 {
    198 
    199 	return (1);
    200 }
    201 
    202 static void
    203 slugiic_attach(struct device *parent, struct device *self, void *arg)
    204 {
    205 	struct slugiic_softc *sc = (struct slugiic_softc *)self;
    206 
    207 	aprint_naive("\n");
    208 	aprint_normal(": I2C bus\n");
    209 
    210 	sc->sc_ic.ic_cookie = sc;
    211 	sc->sc_ic.ic_acquire_bus = slugiic_acquire_bus;
    212 	sc->sc_ic.ic_release_bus = slugiic_release_bus;
    213 	sc->sc_ic.ic_exec = NULL;
    214 	sc->sc_ic.ic_send_start = slugiic_send_start;
    215 	sc->sc_ic.ic_send_stop = slugiic_send_stop;
    216 	sc->sc_ic.ic_initiate_xfer = slugiic_initiate_xfer;
    217 	sc->sc_ic.ic_read_byte = slugiic_read_byte;
    218 	sc->sc_ic.ic_write_byte = slugiic_write_byte;
    219 
    220 	sc->sc_ibo.ibo_set_dir = slugiic_set_dir;
    221 	sc->sc_ibo.ibo_set_bits = slugiic_set_bits;
    222 	sc->sc_ibo.ibo_read_bits = slugiic_read_bits;
    223 	sc->sc_ibo.ibo_bits[I2C_BIT_SDA] = GPIO_I2C_SDA_BIT;
    224 	sc->sc_ibo.ibo_bits[I2C_BIT_SCL] = GPIO_I2C_SCL_BIT;
    225 	sc->sc_ibo.ibo_bits[I2C_BIT_OUTPUT] = 0;
    226 	sc->sc_ibo.ibo_bits[I2C_BIT_INPUT] = GPIO_I2C_SDA_BIT;
    227 
    228 	lockinit(&sc->sc_lock, PRIBIO|PCATCH, "slugiiclk", 0, 0);
    229 
    230 	sc->sc_dirout = 0;
    231 
    232 	/*
    233 	 * Defer until ixp425_softc has been initialised
    234 	 */
    235 	config_interrupts(self, slugiic_deferred_attach);
    236 }
    237 
    238 CFATTACH_DECL(slugiic, sizeof(struct slugiic_softc),
    239     slugiic_match, slugiic_attach, NULL, NULL);
    240