Home | History | Annotate | Line # | Download | only in nslu2
nslu2_iic.c revision 1.3.12.1
      1 /*	$NetBSD: nslu2_iic.c,v 1.3.12.1 2008/06/02 13:22:02 mjf 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/kernel.h>
     35 #include <sys/device.h>
     36 #include <sys/mutex.h>
     37 #include <sys/bus.h>
     38 
     39 #include <dev/i2c/i2cvar.h>
     40 #include <dev/i2c/i2c_bitbang.h>
     41 
     42 #include <arm/xscale/ixp425reg.h>
     43 #include <arm/xscale/ixp425var.h>
     44 
     45 #include <evbarm/nslu2/nslu2reg.h>
     46 
     47 struct slugiic_softc {
     48 	struct device sc_dev;
     49 	struct i2c_controller sc_ic;
     50 	struct i2c_bitbang_ops sc_ibo;
     51 	kmutex_t sc_lock;
     52 #ifdef DIAGNOSTIC
     53 	uint32_t sc_dirout;
     54 #endif
     55 };
     56 
     57 static int
     58 slugiic_acquire_bus(void *arg, int flags)
     59 {
     60 	struct slugiic_softc *sc = arg;
     61 
     62 	if (flags & I2C_F_POLL)
     63 		return (0);
     64 
     65 	mutex_enter(&sc->sc_lock);
     66 	return (0);
     67 }
     68 
     69 static void
     70 slugiic_release_bus(void *arg, int flags)
     71 {
     72 	struct slugiic_softc *sc = arg;
     73 
     74 	if (flags & I2C_F_POLL)
     75 		return;
     76 
     77 	mutex_exit(&sc->sc_lock);
     78 }
     79 
     80 static int
     81 slugiic_send_start(void *arg, int flags)
     82 {
     83 	struct slugiic_softc *sc = arg;
     84 
     85 	return (i2c_bitbang_send_start(sc, flags, &sc->sc_ibo));
     86 }
     87 
     88 static int
     89 slugiic_send_stop(void *arg, int flags)
     90 {
     91 	struct slugiic_softc *sc = arg;
     92 
     93 	return (i2c_bitbang_send_stop(sc, flags, &sc->sc_ibo));
     94 }
     95 
     96 static int
     97 slugiic_initiate_xfer(void *arg, i2c_addr_t addr, int flags)
     98 {
     99 	struct slugiic_softc *sc = arg;
    100 
    101 	return (i2c_bitbang_initiate_xfer(sc, addr, flags, &sc->sc_ibo));
    102 }
    103 
    104 static int
    105 slugiic_read_byte(void *arg, uint8_t *vp, int flags)
    106 {
    107 	struct slugiic_softc *sc = arg;
    108 
    109 	return (i2c_bitbang_read_byte(sc, vp, flags, &sc->sc_ibo));
    110 }
    111 
    112 static int
    113 slugiic_write_byte(void *arg, uint8_t v, int flags)
    114 {
    115 	struct slugiic_softc *sc = arg;
    116 
    117 	return (i2c_bitbang_write_byte(sc, v, flags, &sc->sc_ibo));
    118 }
    119 
    120 static void
    121 slugiic_set_dir(void *arg, uint32_t bits)
    122 {
    123 #ifdef DIAGNOSTIC
    124 	struct slugiic_softc *sc = arg;
    125 
    126 	sc->sc_dirout = bits;
    127 #endif
    128 }
    129 
    130 static void
    131 slugiic_set_bits(void *arg, uint32_t bits)
    132 {
    133 	uint32_t reg;
    134 	int s;
    135 
    136 #ifdef DIAGNOSTIC
    137 	struct slugiic_softc *sc = arg;
    138 	if (sc->sc_dirout == 0 && (bits & GPIO_I2C_SDA_BIT) == 0) {
    139 		printf("slugiic_set_bits: SDA low in input mode!\n");
    140 		bits |= GPIO_I2C_SDA_BIT;
    141 	}
    142 #endif
    143 
    144 	s = splhigh();
    145 
    146 	reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
    147 	reg &= ~(GPIO_I2C_SDA_BIT | GPIO_I2C_SCL_BIT);
    148 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg | bits);
    149 
    150 	/*
    151 	 * Enable output only if the SDA/SCL lines are to be driven low.
    152 	 * Otherwise switch to input and allow the pullup resistors
    153 	 * to hold them high.
    154 	 */
    155 	reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOER);
    156 	reg &= ~(GPIO_I2C_SDA_BIT | GPIO_I2C_SCL_BIT);
    157 	reg |= bits & (GPIO_I2C_SDA_BIT | GPIO_I2C_SCL_BIT);
    158 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOER, reg);
    159 
    160 	splx(s);
    161 }
    162 
    163 static uint32_t
    164 slugiic_read_bits(void *arg)
    165 {
    166 	uint32_t reg;
    167 
    168 #ifdef DIAGNOSTIC
    169 	struct slugiic_softc *sc = arg;
    170 	if (sc->sc_dirout)
    171 		printf("slugiic_read_bits: Read in output mode\n");
    172 #endif
    173 
    174 	reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPINR);
    175 	return (reg & (GPIO_I2C_SDA_BIT | GPIO_I2C_SCL_BIT));
    176 }
    177 
    178 static void
    179 slugiic_deferred_attach(struct device *device)
    180 {
    181 	struct slugiic_softc *sc = (struct slugiic_softc *)device;
    182 	struct i2cbus_attach_args iba;
    183 	uint32_t reg;
    184 
    185 	reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
    186 	reg |= GPIO_I2C_SDA_BIT | GPIO_I2C_SCL_BIT;
    187 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg);
    188 
    189 	reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOER);
    190 	reg &= ~GPIO_I2C_SCL_BIT;
    191 	reg |= GPIO_I2C_SDA_BIT;
    192 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOER, reg);
    193 
    194 	iba.iba_tag = &sc->sc_ic;
    195 	(void) config_found_ia(&sc->sc_dev, "i2cbus", &iba, iicbus_print);
    196 }
    197 
    198 static int
    199 slugiic_match(struct device *parent, struct cfdata *cf, void *arg)
    200 {
    201 
    202 	return (1);
    203 }
    204 
    205 static void
    206 slugiic_attach(struct device *parent, struct device *self, void *arg)
    207 {
    208 	struct slugiic_softc *sc = (struct slugiic_softc *)self;
    209 
    210 	aprint_naive("\n");
    211 	aprint_normal(": I2C bus\n");
    212 
    213 	sc->sc_ic.ic_cookie = sc;
    214 	sc->sc_ic.ic_acquire_bus = slugiic_acquire_bus;
    215 	sc->sc_ic.ic_release_bus = slugiic_release_bus;
    216 	sc->sc_ic.ic_exec = NULL;
    217 	sc->sc_ic.ic_send_start = slugiic_send_start;
    218 	sc->sc_ic.ic_send_stop = slugiic_send_stop;
    219 	sc->sc_ic.ic_initiate_xfer = slugiic_initiate_xfer;
    220 	sc->sc_ic.ic_read_byte = slugiic_read_byte;
    221 	sc->sc_ic.ic_write_byte = slugiic_write_byte;
    222 
    223 	sc->sc_ibo.ibo_set_dir = slugiic_set_dir;
    224 	sc->sc_ibo.ibo_set_bits = slugiic_set_bits;
    225 	sc->sc_ibo.ibo_read_bits = slugiic_read_bits;
    226 	sc->sc_ibo.ibo_bits[I2C_BIT_SDA] = GPIO_I2C_SDA_BIT;
    227 	sc->sc_ibo.ibo_bits[I2C_BIT_SCL] = GPIO_I2C_SCL_BIT;
    228 	sc->sc_ibo.ibo_bits[I2C_BIT_OUTPUT] = 1;
    229 	sc->sc_ibo.ibo_bits[I2C_BIT_INPUT] = 0;
    230 
    231 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    232 
    233 #ifdef DIAGNOSTIC
    234 	sc->sc_dirout = 0;
    235 #endif
    236 
    237 	/*
    238 	 * Defer until ixp425_softc has been initialised
    239 	 */
    240 	config_interrupts(self, slugiic_deferred_attach);
    241 }
    242 
    243 CFATTACH_DECL(slugiic, sizeof(struct slugiic_softc),
    244     slugiic_match, slugiic_attach, NULL, NULL);
    245