Home | History | Annotate | Line # | Download | only in dev
gpiic_opb.c revision 1.8
      1  1.8      matt /*	$NetBSD: gpiic_opb.c,v 1.8 2011/06/17 19:03:02 matt Exp $	*/
      2  1.1       scw 
      3  1.1       scw /*
      4  1.1       scw  * Copyright 2002, 2003 Wasabi Systems, Inc.
      5  1.1       scw  * All rights reserved.
      6  1.1       scw  *
      7  1.1       scw  * Written by Steve C. Woodford for Wasabi Systems, Inc.
      8  1.1       scw  *
      9  1.1       scw  * Redistribution and use in source and binary forms, with or without
     10  1.1       scw  * modification, are permitted provided that the following conditions
     11  1.1       scw  * are met:
     12  1.1       scw  * 1. Redistributions of source code must retain the above copyright
     13  1.1       scw  *    notice, this list of conditions and the following disclaimer.
     14  1.1       scw  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1       scw  *    notice, this list of conditions and the following disclaimer in the
     16  1.1       scw  *    documentation and/or other materials provided with the distribution.
     17  1.1       scw  * 3. All advertising materials mentioning features or use of this software
     18  1.1       scw  *    must display the following acknowledgement:
     19  1.1       scw  *      This product includes software developed for the NetBSD Project by
     20  1.1       scw  *      Wasabi Systems, Inc.
     21  1.1       scw  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  1.1       scw  *    or promote products derived from this software without specific prior
     23  1.1       scw  *    written permission.
     24  1.1       scw  *
     25  1.1       scw  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  1.1       scw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  1.1       scw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  1.1       scw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  1.1       scw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  1.1       scw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  1.1       scw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  1.1       scw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  1.1       scw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  1.1       scw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  1.1       scw  * POSSIBILITY OF SUCH DAMAGE.
     36  1.1       scw  */
     37  1.1       scw 
     38  1.1       scw #include "locators.h"
     39  1.1       scw 
     40  1.1       scw #include <sys/param.h>
     41  1.1       scw #include <sys/systm.h>
     42  1.1       scw #include <sys/device.h>
     43  1.1       scw #include <sys/errno.h>
     44  1.5        ad #include <sys/mutex.h>
     45  1.5        ad #include <sys/cpu.h>
     46  1.1       scw 
     47  1.2       scw #include <dev/i2c/i2cvar.h>
     48  1.2       scw #include <dev/i2c/i2c_bitbang.h>
     49  1.1       scw 
     50  1.1       scw #include <powerpc/ibm4xx/dev/opbvar.h>
     51  1.1       scw #include <powerpc/ibm4xx/dev/gpiicreg.h>
     52  1.1       scw 
     53  1.1       scw struct gpiic_softc {
     54  1.8      matt 	device_t sc_dev;
     55  1.1       scw 	bus_space_tag_t sc_bust;
     56  1.1       scw 	bus_space_handle_t sc_bush;
     57  1.1       scw 	uint8_t sc_txen;
     58  1.6   tsutsui 	uint8_t sc_tx;
     59  1.1       scw 	struct i2c_controller sc_i2c;
     60  1.1       scw 	struct i2c_bitbang_ops sc_bops;
     61  1.5        ad 	kmutex_t sc_buslock;
     62  1.1       scw };
     63  1.1       scw 
     64  1.8      matt static int	gpiic_match(device_t, cfdata_t, void *);
     65  1.8      matt static void	gpiic_attach(device_t, device_t, void *);
     66  1.1       scw 
     67  1.8      matt CFATTACH_DECL_NEW(gpiic, sizeof(struct gpiic_softc),
     68  1.1       scw     gpiic_match, gpiic_attach, NULL, NULL);
     69  1.1       scw 
     70  1.1       scw static int	gpiic_acquire_bus(void *, int);
     71  1.1       scw static void	gpiic_release_bus(void *, int);
     72  1.1       scw static int	gpiic_send_start(void *, int);
     73  1.1       scw static int	gpiic_send_stop(void *, int);
     74  1.1       scw static int	gpiic_initiate_xfer(void *, i2c_addr_t, int);
     75  1.1       scw static int	gpiic_read_byte(void *, uint8_t *, int);
     76  1.1       scw static int	gpiic_write_byte(void *, uint8_t, int);
     77  1.1       scw static void	gpiic_set_dir(void *, uint32_t);
     78  1.1       scw static void	gpiic_set_bits(void *, uint32_t);
     79  1.1       scw static uint32_t	gpiic_read_bits(void *);
     80  1.1       scw 
     81  1.1       scw static int
     82  1.8      matt gpiic_match(device_t parent, cfdata_t cf, void *args)
     83  1.1       scw {
     84  1.8      matt 	struct opb_attach_args * const oaa = args;
     85  1.1       scw 
     86  1.1       scw 	if (strcmp(oaa->opb_name, cf->cf_name) != 0)
     87  1.1       scw 		return 0;
     88  1.1       scw 
     89  1.1       scw 	return (1);
     90  1.1       scw }
     91  1.1       scw 
     92  1.1       scw static void
     93  1.8      matt gpiic_attach(device_t parent, device_t self, void *args)
     94  1.1       scw {
     95  1.8      matt 	struct gpiic_softc * const sc = device_private(self);
     96  1.8      matt 	struct opb_attach_args * const oaa = args;
     97  1.1       scw 	struct i2cbus_attach_args iba;
     98  1.1       scw 
     99  1.1       scw 	aprint_naive(": IIC controller\n");
    100  1.1       scw 	aprint_normal(": On-Chip IIC controller\n");
    101  1.1       scw 
    102  1.8      matt 	sc->sc_dev = self;
    103  1.1       scw 	sc->sc_bust = oaa->opb_bt;
    104  1.1       scw 
    105  1.1       scw 	bus_space_map(sc->sc_bust, oaa->opb_addr, IIC_NREG, 0, &sc->sc_bush);
    106  1.1       scw 
    107  1.5        ad 	mutex_init(&sc->sc_buslock, MUTEX_DEFAULT, IPL_NONE);
    108  1.1       scw 
    109  1.1       scw 	sc->sc_txen = 0;
    110  1.6   tsutsui 	sc->sc_tx = IIC_DIRECTCNTL_SCC | IIC_DIRECTCNTL_SDAC;
    111  1.1       scw 	sc->sc_i2c.ic_cookie = sc;
    112  1.1       scw 	sc->sc_i2c.ic_acquire_bus = gpiic_acquire_bus;
    113  1.1       scw 	sc->sc_i2c.ic_release_bus = gpiic_release_bus;
    114  1.1       scw 	sc->sc_i2c.ic_exec = NULL;
    115  1.1       scw 	sc->sc_i2c.ic_send_start = gpiic_send_start;
    116  1.1       scw 	sc->sc_i2c.ic_send_stop = gpiic_send_stop;
    117  1.1       scw 	sc->sc_i2c.ic_initiate_xfer = gpiic_initiate_xfer;
    118  1.1       scw 	sc->sc_i2c.ic_read_byte = gpiic_read_byte;
    119  1.1       scw 	sc->sc_i2c.ic_write_byte = gpiic_write_byte;
    120  1.1       scw 
    121  1.1       scw 	sc->sc_bops.ibo_set_dir = gpiic_set_dir;
    122  1.1       scw 	sc->sc_bops.ibo_set_bits = gpiic_set_bits;
    123  1.1       scw 	sc->sc_bops.ibo_read_bits = gpiic_read_bits;
    124  1.1       scw 	sc->sc_bops.ibo_bits[I2C_BIT_SDA] = IIC_DIRECTCNTL_SDAC;
    125  1.1       scw 	sc->sc_bops.ibo_bits[I2C_BIT_SCL] = IIC_DIRECTCNTL_SCC;
    126  1.1       scw 	sc->sc_bops.ibo_bits[I2C_BIT_OUTPUT] = 1;
    127  1.1       scw 	sc->sc_bops.ibo_bits[I2C_BIT_INPUT] = 0;
    128  1.1       scw 
    129  1.1       scw 	/*
    130  1.1       scw 	 * Put the controller into Soft Reset. This allows us to
    131  1.1       scw 	 * manually bit-bang the I2C clock/data lines.
    132  1.1       scw 	 */
    133  1.1       scw 	bus_space_write_1(sc->sc_bust, sc->sc_bush, IIC_XTCNTLSS,
    134  1.1       scw 	    IIC_XTCNTLSS_SRST);
    135  1.1       scw 	delay(10);
    136  1.1       scw 	bus_space_write_1(sc->sc_bust, sc->sc_bush, IIC_DIRECTCNTL,
    137  1.1       scw 	    IIC_DIRECTCNTL_SCC | IIC_DIRECTCNTL_SDAC);
    138  1.1       scw 
    139  1.7  kiyohara 	memset(&iba, 0, sizeof(iba));
    140  1.1       scw 	iba.iba_tag = &sc->sc_i2c;
    141  1.8      matt 	(void) config_found_ia(self, "i2cbus", &iba, iicbus_print);
    142  1.1       scw }
    143  1.1       scw 
    144  1.1       scw static int
    145  1.1       scw gpiic_acquire_bus(void *arg, int flags)
    146  1.1       scw {
    147  1.8      matt 	struct gpiic_softc * const sc = arg;
    148  1.1       scw 
    149  1.1       scw 	if (flags & I2C_F_POLL)
    150  1.1       scw 		return (0);
    151  1.1       scw 
    152  1.5        ad 	mutex_enter(&sc->sc_buslock);
    153  1.5        ad 	return (0);
    154  1.1       scw }
    155  1.1       scw 
    156  1.1       scw static void
    157  1.1       scw gpiic_release_bus(void *arg, int flags)
    158  1.1       scw {
    159  1.8      matt 	struct gpiic_softc * const sc = arg;
    160  1.1       scw 
    161  1.1       scw 	if (flags & I2C_F_POLL)
    162  1.1       scw 		return;
    163  1.1       scw 
    164  1.5        ad 	mutex_exit(&sc->sc_buslock);
    165  1.1       scw }
    166  1.1       scw 
    167  1.1       scw static int
    168  1.1       scw gpiic_send_start(void *arg, int flags)
    169  1.1       scw {
    170  1.8      matt 	struct gpiic_softc * const sc = arg;
    171  1.1       scw 
    172  1.1       scw 	return (i2c_bitbang_send_start(sc, flags, &sc->sc_bops));
    173  1.1       scw }
    174  1.1       scw 
    175  1.1       scw static int
    176  1.1       scw gpiic_send_stop(void *arg, int flags)
    177  1.1       scw {
    178  1.8      matt 	struct gpiic_softc * const sc = arg;
    179  1.1       scw 
    180  1.1       scw 	return (i2c_bitbang_send_stop(sc, flags, &sc->sc_bops));
    181  1.1       scw }
    182  1.1       scw 
    183  1.1       scw static int
    184  1.1       scw gpiic_initiate_xfer(void *arg, i2c_addr_t addr, int flags)
    185  1.1       scw {
    186  1.8      matt 	struct gpiic_softc * const sc = arg;
    187  1.1       scw 
    188  1.1       scw 	return (i2c_bitbang_initiate_xfer(sc, addr, flags, &sc->sc_bops));
    189  1.1       scw }
    190  1.1       scw 
    191  1.1       scw static int
    192  1.1       scw gpiic_read_byte(void *arg, uint8_t *vp, int flags)
    193  1.1       scw {
    194  1.8      matt 	struct gpiic_softc * const sc = arg;
    195  1.1       scw 
    196  1.1       scw 	return (i2c_bitbang_read_byte(sc, vp, flags, &sc->sc_bops));
    197  1.1       scw }
    198  1.1       scw 
    199  1.1       scw static int
    200  1.1       scw gpiic_write_byte(void *arg, uint8_t v, int flags)
    201  1.1       scw {
    202  1.8      matt 	struct gpiic_softc * const sc = arg;
    203  1.1       scw 
    204  1.1       scw 	return (i2c_bitbang_write_byte(sc, v, flags, &sc->sc_bops));
    205  1.1       scw }
    206  1.1       scw 
    207  1.1       scw static void
    208  1.1       scw gpiic_set_dir(void *arg, uint32_t bits)
    209  1.1       scw {
    210  1.8      matt 	struct gpiic_softc * const sc = arg;
    211  1.6   tsutsui 	uint8_t tx, txen;
    212  1.1       scw 
    213  1.6   tsutsui 	txen = (uint8_t)bits;
    214  1.6   tsutsui 	if (sc->sc_txen == txen)
    215  1.6   tsutsui 		return;
    216  1.6   tsutsui 
    217  1.6   tsutsui 	sc->sc_txen = txen;
    218  1.6   tsutsui 
    219  1.6   tsutsui 	tx = sc->sc_tx;
    220  1.6   tsutsui 	if (sc->sc_txen == 0)
    221  1.6   tsutsui 		tx |= IIC_DIRECTCNTL_SDAC;
    222  1.6   tsutsui 	bus_space_write_1(sc->sc_bust, sc->sc_bush, IIC_DIRECTCNTL, tx);
    223  1.1       scw }
    224  1.1       scw 
    225  1.1       scw static void
    226  1.1       scw gpiic_set_bits(void *arg, uint32_t bits)
    227  1.1       scw {
    228  1.8      matt 	struct gpiic_softc * const sc = arg;
    229  1.1       scw 
    230  1.6   tsutsui 	sc->sc_tx = (uint8_t)bits;
    231  1.6   tsutsui 	if (sc->sc_txen == 0)
    232  1.1       scw 		bits |= IIC_DIRECTCNTL_SDAC;
    233  1.1       scw 
    234  1.1       scw 	bus_space_write_1(sc->sc_bust, sc->sc_bush, IIC_DIRECTCNTL, bits);
    235  1.1       scw }
    236  1.1       scw 
    237  1.1       scw static uint32_t
    238  1.1       scw gpiic_read_bits(void *arg)
    239  1.1       scw {
    240  1.8      matt 	struct gpiic_softc * const sc = arg;
    241  1.1       scw 	uint8_t rv;
    242  1.1       scw 
    243  1.1       scw 	rv = bus_space_read_1(sc->sc_bust, sc->sc_bush, IIC_DIRECTCNTL) << 2;
    244  1.1       scw 	rv &= (IIC_DIRECTCNTL_SCC | IIC_DIRECTCNTL_SDAC);
    245  1.1       scw 
    246  1.1       scw 	return ((uint32_t)rv);
    247  1.1       scw }
    248