Home | History | Annotate | Line # | Download | only in armadillo
armadillo9_iic.c revision 1.3
      1 /*	$NetBSD: armadillo9_iic.c,v 1.3 2006/06/26 18:21:39 drochner Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005 HAMAJIMA Katsuomi. All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: armadillo9_iic.c,v 1.3 2006/06/26 18:21:39 drochner Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/systm.h>
     33 #include <sys/kernel.h>
     34 #include <sys/device.h>
     35 #include <sys/lock.h>
     36 #include <dev/i2c/i2cvar.h>
     37 #include <dev/i2c/i2c_bitbang.h>
     38 #include <arm/ep93xx/epsocvar.h>
     39 #include <arm/ep93xx/epgpiovar.h>
     40 #include <evbarm/armadillo/armadillo9var.h>
     41 
     42 #include "seeprom.h"
     43 #if NSEEPROM > 0
     44 #include <net/if.h>
     45 #include <net/if_ether.h>
     46 #include <dev/i2c/at24cxxvar.h>
     47 #endif
     48 
     49 struct armadillo9iic_softc {
     50 	struct device		sc_dev;
     51 	struct i2c_controller	sc_i2c;
     52 	struct lock		sc_buslock;
     53 	int			sc_port;
     54 	int			sc_sda;
     55 	int			sc_scl;
     56 	struct epgpio_softc	*sc_gpio;
     57 };
     58 
     59 static int armadillo9iic_match(struct device *, struct cfdata *, void *);
     60 static void armadillo9iic_attach(struct device *, struct device *, void *);
     61 
     62 static int armadillo9iic_acquire_bus(void *, int);
     63 static void armadillo9iic_release_bus(void *, int);
     64 static int armadillo9iic_send_start(void *, int);
     65 static int armadillo9iic_send_stop(void *, int);
     66 static int armadillo9iic_initiate_xfer(void *, uint16_t, int);
     67 static int armadillo9iic_read_byte(void *, uint8_t *, int);
     68 static int armadillo9iic_write_byte(void *, uint8_t, int);
     69 
     70 static void armadillo9iic_bb_set_bits(void *, uint32_t);
     71 static void armadillo9iic_bb_set_dir(void *, uint32_t);
     72 static uint32_t armadillo9iic_bb_read_bits(void *);
     73 
     74 CFATTACH_DECL(armadillo9iic, sizeof(struct armadillo9iic_softc),
     75 	      armadillo9iic_match, armadillo9iic_attach, NULL, NULL);
     76 
     77 static struct i2c_bitbang_ops armadillo9iic_bbops = {
     78 	armadillo9iic_bb_set_bits,
     79 	armadillo9iic_bb_set_dir,
     80 	armadillo9iic_bb_read_bits,
     81 	{ 0, 0, 0, 0 }	/* set in attach() */
     82 };
     83 
     84 int
     85 armadillo9iic_match(struct device *parent, struct cfdata *cf, void *aux)
     86 {
     87 	return 2;
     88 }
     89 
     90 void
     91 armadillo9iic_attach(struct device *parent, struct device *self, void *aux)
     92 {
     93 	struct armadillo9iic_softc *sc = (struct armadillo9iic_softc*)self;
     94 	struct i2cbus_attach_args iba;
     95 #if NSEEPROM > 0
     96 	struct epgpio_attach_args *ga = aux;
     97 #endif
     98 	lockinit(&sc->sc_buslock, PRIBIO|PCATCH, "armadillo9iiclk", 0, 0);
     99 
    100 	sc->sc_port = ga->ga_port;
    101 	sc->sc_sda = ga->ga_bit1;
    102 	sc->sc_scl = ga->ga_bit2;
    103 	sc->sc_gpio = (struct epgpio_softc *)parent;
    104 
    105 	armadillo9iic_bbops.ibo_bits[I2C_BIT_SDA] = (1<<sc->sc_sda);
    106 	armadillo9iic_bbops.ibo_bits[I2C_BIT_SCL] = (1<<sc->sc_scl);
    107 	armadillo9iic_bbops.ibo_bits[I2C_BIT_OUTPUT] = sc->sc_sda;
    108 	armadillo9iic_bbops.ibo_bits[I2C_BIT_INPUT] = 0;
    109 
    110 	sc->sc_i2c.ic_cookie = sc;
    111 	sc->sc_i2c.ic_acquire_bus = armadillo9iic_acquire_bus;
    112 	sc->sc_i2c.ic_release_bus = armadillo9iic_release_bus;
    113 	sc->sc_i2c.ic_send_start = armadillo9iic_send_start;
    114 	sc->sc_i2c.ic_send_stop = armadillo9iic_send_stop;
    115 	sc->sc_i2c.ic_initiate_xfer = armadillo9iic_initiate_xfer;
    116 	sc->sc_i2c.ic_read_byte = armadillo9iic_read_byte;
    117 	sc->sc_i2c.ic_write_byte = armadillo9iic_write_byte;
    118 
    119 	iba.iba_tag = &sc->sc_i2c;
    120 
    121 	epgpio_in(sc->sc_gpio, sc->sc_port, sc->sc_sda);
    122 	epgpio_out(sc->sc_gpio, sc->sc_port, sc->sc_scl);
    123 	epgpio_set(sc->sc_gpio, sc->sc_port, sc->sc_scl);
    124 
    125 	printf("\n");
    126 
    127 	config_found_ia(&sc->sc_dev, "i2cbus", &iba, iicbus_print);
    128 
    129 #if NSEEPROM > 0
    130 	/* read mac address */
    131 	/* XXX This should probably be done elsewhere, earlier in bootstrap. */
    132 	if (seeprom_bootstrap_read(&sc->sc_i2c, 0x50, 0x00, 128,
    133 				   armadillo9_ethaddr, ETHER_ADDR_LEN) != 0) {
    134 		printf("%s: WARNING: unable to read MAC address from SEEPROM\n",
    135 		    sc->sc_dev.dv_xname);
    136 	}
    137 #endif
    138 }
    139 
    140 int
    141 armadillo9iic_acquire_bus(void *cookie, int flags)
    142 {
    143 	struct armadillo9iic_softc *sc = cookie;
    144 
    145 	/* XXX What should we do for the polling case? */
    146 	if (flags & I2C_F_POLL)
    147 		return 0;
    148 
    149 	return lockmgr(&sc->sc_buslock, LK_EXCLUSIVE, NULL);
    150 }
    151 
    152 void
    153 armadillo9iic_release_bus(void *cookie, int flags)
    154 {
    155 	struct armadillo9iic_softc *sc = cookie;
    156 
    157 	/* XXX See above. */
    158 	if (flags & I2C_F_POLL)
    159 		return;
    160 
    161 	lockmgr(&sc->sc_buslock, LK_RELEASE, NULL);
    162 }
    163 
    164 int
    165 armadillo9iic_send_start(void *cookie, int flags)
    166 {
    167 	return i2c_bitbang_send_start(cookie, flags, &armadillo9iic_bbops);
    168 }
    169 
    170 int
    171 armadillo9iic_send_stop(void *cookie, int flags)
    172 {
    173 	return i2c_bitbang_send_stop(cookie, flags, &armadillo9iic_bbops);
    174 }
    175 
    176 int
    177 armadillo9iic_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
    178 {
    179 	return i2c_bitbang_initiate_xfer(cookie, addr, flags,
    180 					 &armadillo9iic_bbops);
    181 }
    182 
    183 int
    184 armadillo9iic_read_byte(void *cookie, uint8_t *bytep, int flags)
    185 {
    186 	return i2c_bitbang_read_byte(cookie, bytep, flags,
    187 				     &armadillo9iic_bbops);
    188 }
    189 
    190 int
    191 armadillo9iic_write_byte(void *cookie, uint8_t byte, int flags)
    192 {
    193 	return i2c_bitbang_write_byte(cookie, byte, flags,
    194 				      &armadillo9iic_bbops);
    195 }
    196 
    197 void
    198 armadillo9iic_bb_set_bits(void *cookie, uint32_t bits)
    199 {
    200 	struct armadillo9iic_softc *sc = cookie;
    201 
    202 	if (bits & (1 << sc->sc_sda))
    203 		epgpio_set(sc->sc_gpio, sc->sc_port, sc->sc_sda);
    204 	else
    205 		epgpio_clear(sc->sc_gpio, sc->sc_port, sc->sc_sda);
    206 
    207 	if (bits & (1 << sc->sc_scl))
    208 		epgpio_set(sc->sc_gpio, sc->sc_port, sc->sc_scl);
    209 	else
    210 		epgpio_clear(sc->sc_gpio, sc->sc_port, sc->sc_scl);
    211 }
    212 
    213 void
    214 armadillo9iic_bb_set_dir(void *cookie, uint32_t bits)
    215 {
    216 	struct armadillo9iic_softc *sc = cookie;
    217 
    218 	if(bits)
    219 		epgpio_out(sc->sc_gpio, sc->sc_port, sc->sc_sda);
    220 	else
    221 		epgpio_in(sc->sc_gpio, sc->sc_port, sc->sc_sda);
    222 }
    223 
    224 uint32_t
    225 armadillo9iic_bb_read_bits(void *cookie)
    226 {
    227 	struct armadillo9iic_softc *sc = cookie;
    228 
    229 	return epgpio_read(sc->sc_gpio, sc->sc_port, sc->sc_sda) << sc->sc_sda;
    230 }
    231