Home | History | Annotate | Line # | Download | only in iomd
iomdiic.c revision 1.4.8.1
      1  1.4.8.1     yamt /*	$NetBSD: iomdiic.c,v 1.4.8.1 2006/08/11 15:41:11 yamt 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.4    perry #define	IOMDIIC_READ	 *(volatile uint8_t *)(IOMD_ADDRESS(IOMD_IOCR))
     80      1.4    perry #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.2  reinoud 	struct iic_attach_args *ia = aux;
    125      1.1  thorpej 
    126      1.2  reinoud 	if (strcmp(ia->ia_name, "iic") == 0) return 1;
    127      1.2  reinoud 	return 0;
    128      1.1  thorpej }
    129      1.1  thorpej 
    130      1.1  thorpej static void
    131      1.1  thorpej iomdiic_attach(struct device *parent, struct device *self, void *aux)
    132      1.1  thorpej {
    133      1.1  thorpej 	struct iomdiic_softc *sc = (void *) self;
    134      1.1  thorpej 	struct i2cbus_attach_args iba;
    135      1.1  thorpej 
    136      1.1  thorpej 	printf("\n");
    137      1.1  thorpej 
    138      1.1  thorpej 	lockinit(&sc->sc_buslock, PRIBIO|PCATCH, "iomdiiclk", 0, 0);
    139      1.1  thorpej 
    140      1.1  thorpej 	sc->sc_i2c.ic_cookie = sc;
    141      1.1  thorpej 	sc->sc_i2c.ic_acquire_bus = iomdiic_acquire_bus;
    142      1.1  thorpej 	sc->sc_i2c.ic_release_bus = iomdiic_release_bus;
    143      1.1  thorpej 	sc->sc_i2c.ic_send_start = iomdiic_send_start;
    144      1.1  thorpej 	sc->sc_i2c.ic_send_stop = iomdiic_send_stop;
    145      1.1  thorpej 	sc->sc_i2c.ic_initiate_xfer = iomdiic_initiate_xfer;
    146      1.1  thorpej 	sc->sc_i2c.ic_read_byte = iomdiic_read_byte;
    147      1.1  thorpej 	sc->sc_i2c.ic_write_byte = iomdiic_write_byte;
    148      1.1  thorpej 
    149      1.1  thorpej 	iba.iba_tag = &sc->sc_i2c;
    150  1.4.8.1     yamt 	(void) config_found_ia(&sc->sc_dev, "i2cbus", &iba, iicbus_print);
    151      1.1  thorpej }
    152      1.1  thorpej 
    153      1.1  thorpej CFATTACH_DECL(iomdiic, sizeof(struct iomdiic_softc),
    154      1.1  thorpej     iomdiic_match, iomdiic_attach, NULL, NULL);
    155      1.1  thorpej 
    156      1.1  thorpej i2c_tag_t
    157      1.1  thorpej iomdiic_bootstrap_cookie(void)
    158      1.1  thorpej {
    159      1.1  thorpej 	static struct iomdiic_softc sc;
    160      1.1  thorpej 
    161      1.1  thorpej 	/* XXX Yuck. */
    162      1.1  thorpej 	strcpy(sc.sc_dev.dv_xname, "iomdiicboot");
    163      1.1  thorpej 
    164      1.1  thorpej 	sc.sc_i2c.ic_cookie = &sc;
    165      1.1  thorpej 	sc.sc_i2c.ic_acquire_bus = iomdiic_acquire_bus;
    166      1.1  thorpej 	sc.sc_i2c.ic_release_bus = iomdiic_release_bus;
    167      1.1  thorpej 	sc.sc_i2c.ic_send_start = iomdiic_send_start;
    168      1.1  thorpej 	sc.sc_i2c.ic_send_stop = iomdiic_send_stop;
    169      1.1  thorpej 	sc.sc_i2c.ic_initiate_xfer = iomdiic_initiate_xfer;
    170      1.1  thorpej 	sc.sc_i2c.ic_read_byte = iomdiic_read_byte;
    171      1.1  thorpej 	sc.sc_i2c.ic_write_byte = iomdiic_write_byte;
    172      1.1  thorpej 
    173      1.1  thorpej 	return ((void *) &sc.sc_i2c);
    174      1.1  thorpej }
    175      1.1  thorpej 
    176      1.1  thorpej static int
    177      1.1  thorpej iomdiic_acquire_bus(void *cookie, int flags)
    178      1.1  thorpej {
    179      1.1  thorpej 	struct iomdiic_softc *sc = cookie;
    180      1.1  thorpej 
    181      1.1  thorpej 	/* XXX What should we do for the polling case? */
    182      1.1  thorpej 	if (flags & I2C_F_POLL)
    183      1.1  thorpej 		return (0);
    184      1.1  thorpej 
    185      1.1  thorpej 	return (lockmgr(&sc->sc_buslock, LK_EXCLUSIVE, NULL));
    186      1.1  thorpej }
    187      1.1  thorpej 
    188      1.1  thorpej static void
    189      1.1  thorpej iomdiic_release_bus(void *cookie, int flags)
    190      1.1  thorpej {
    191      1.1  thorpej 	struct iomdiic_softc *sc = cookie;
    192      1.1  thorpej 
    193      1.1  thorpej 	/* XXX See above. */
    194      1.1  thorpej 	if (flags & I2C_F_POLL)
    195      1.1  thorpej 		return;
    196      1.1  thorpej 
    197      1.1  thorpej 	(void) lockmgr(&sc->sc_buslock, LK_RELEASE, NULL);
    198      1.1  thorpej }
    199      1.1  thorpej 
    200      1.1  thorpej static int
    201      1.1  thorpej iomdiic_send_start(void *cookie, int flags)
    202      1.1  thorpej {
    203      1.1  thorpej 
    204      1.1  thorpej 	return (i2c_bitbang_send_start(cookie, flags, &iomdiic_bbops));
    205      1.1  thorpej }
    206      1.1  thorpej 
    207      1.1  thorpej static int
    208      1.1  thorpej iomdiic_send_stop(void *cookie, int flags)
    209      1.1  thorpej {
    210      1.1  thorpej 
    211      1.1  thorpej 	return (i2c_bitbang_send_stop(cookie, flags, &iomdiic_bbops));
    212      1.1  thorpej }
    213      1.1  thorpej 
    214      1.1  thorpej static int
    215      1.1  thorpej iomdiic_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
    216      1.1  thorpej {
    217      1.1  thorpej 
    218      1.1  thorpej 	return (i2c_bitbang_initiate_xfer(cookie, addr, flags, &iomdiic_bbops));
    219      1.1  thorpej }
    220      1.1  thorpej 
    221      1.1  thorpej static int
    222      1.1  thorpej iomdiic_read_byte(void *cookie, uint8_t *bytep, int flags)
    223      1.1  thorpej {
    224      1.1  thorpej 
    225      1.1  thorpej 	return (i2c_bitbang_read_byte(cookie, bytep, flags, &iomdiic_bbops));
    226      1.1  thorpej }
    227      1.1  thorpej 
    228      1.1  thorpej static int
    229      1.1  thorpej iomdiic_write_byte(void *cookie, uint8_t byte, int flags)
    230      1.1  thorpej {
    231      1.1  thorpej 
    232      1.1  thorpej 	return (i2c_bitbang_write_byte(cookie, byte, flags, &iomdiic_bbops));
    233      1.1  thorpej }
    234