Home | History | Annotate | Line # | Download | only in gumstix
gxio.c revision 1.3
      1 /*	$NetBSD: gxio.c,v 1.3 2007/01/18 10:06:47 kiyohara Exp $ */
      2 /*
      3  * Copyright (C) 2005, 2006, 2007 WIDE Project and SOUM Corporation.
      4  * All rights reserved.
      5  *
      6  * Written by Takashi Kiyohara and Susumu Miki for WIDE Project and SOUM
      7  * Corporation.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. Neither the name of the project nor the name of SOUM Corporation
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT and SOUM CORPORATION ``AS IS''
     22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT AND SOUM CORPORATION
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: gxio.c,v 1.3 2007/01/18 10:06:47 kiyohara Exp $");
     35 
     36 #include "opt_gxio.h"
     37 
     38 #include <sys/param.h>
     39 #include <sys/device.h>
     40 #include <sys/errno.h>
     41 
     42 #include <sys/systm.h>
     43 
     44 #include <machine/bootconfig.h>
     45 
     46 #include <arm/xscale/pxa2x0cpu.h>
     47 #include <arm/xscale/pxa2x0reg.h>
     48 #include <arm/xscale/pxa2x0var.h>
     49 #include <arm/xscale/pxa2x0_gpio.h>
     50 #include <evbarm/gumstix/gumstixvar.h>
     51 
     52 #include "locators.h"
     53 
     54 
     55 struct gpioconf {
     56 	int pin;
     57 	u_int value;
     58 };
     59 struct gxioconf {
     60 	const char *name;
     61 	void (*config)(struct gxio_softc *);
     62 };
     63 
     64 static int gxiomatch(struct device *, struct cfdata *, void *);
     65 static void gxioattach(struct device *, struct device *, void *);
     66 static int gxiosearch(struct device *, struct cfdata *, const int *, void *);
     67 static int gxioprint(void *, const char *);
     68 
     69 void gxio_config_pin(void);
     70 static void
     71     gxio_config_gpio(struct gxio_softc *, const struct gxioconf *, char *);
     72 static void cfstix_config(struct gxio_softc *);
     73 static void etherstix_config(struct gxio_softc *);
     74 static void netcf_config(struct gxio_softc *);
     75 static void netduommc_config(struct gxio_softc *);
     76 static void netduo_config(struct gxio_softc *);
     77 static void netmmc_config(struct gxio_softc *);
     78 
     79 CFATTACH_DECL(
     80     gxio, sizeof(struct gxio_softc), gxiomatch, gxioattach, NULL, NULL);
     81 
     82 char busheader[MAX_BOOT_STRING];
     83 
     84 struct gpioconf gpioconf[] = {
     85 	/* Bluetooth module configuration */
     86 	{  7, GPIO_OUT | GPIO_SET },	/* power on */
     87 	{ 12, GPIO_ALT_FN_1_OUT },	/* 32kHz out. required by SingleStone */
     88 
     89 	/* AC97 configuration */
     90 #if 1
     91 	/* this configuration set by pxaacu_attach()::pxa2x0_ac97.c */
     92 #else
     93 	/* Don't reorder */
     94 	{ 31, GPIO_ALT_FN_2_OUT },	/* SYNC */
     95 	{ 30, GPIO_ALT_FN_2_OUT },	/* SDATA_OUT */
     96 	{ 28, GPIO_ALT_FN_1_IN },	/* BITCLK */
     97 	{ 29, GPIO_ALT_FN_1_IN },	/* SDATA_IN0 */
     98 #endif
     99 
    100 	/* FFUART configuration : is connected only TXD/RXD */
    101 	{ 34, GPIO_ALT_FN_1_IN },	/* FFRXD */
    102 	{ 39, GPIO_ALT_FN_2_OUT },	/* FFTXD */
    103 
    104 #ifndef GXIO_BLUETOOTH_ON_HWUART
    105 	/* BTUART configuration */
    106 	{ 42, GPIO_ALT_FN_1_IN },	/* BTRXD */
    107 	{ 43, GPIO_ALT_FN_2_OUT },	/* BTTXD */
    108 	{ 44, GPIO_ALT_FN_1_IN },	/* BTCST */
    109 	{ 45, GPIO_ALT_FN_2_OUT },	/* BTRST */
    110 #else
    111 	/* HWUART configuration */
    112 	{ 42, GPIO_ALT_FN_3_IN },	/* HWRXD */
    113 	{ 43, GPIO_ALT_FN_3_OUT },	/* HWTXD */
    114 	{ 44, GPIO_ALT_FN_3_IN },	/* HWCST */
    115 	{ 45, GPIO_ALT_FN_3_OUT },	/* HWRST */
    116 #endif
    117 
    118 	/* STUART configuration : is connected only TXD/RXD */
    119 	{ 46, GPIO_ALT_FN_2_IN },	/* RXD */
    120 	{ 47, GPIO_ALT_FN_1_OUT },	/* TXD */
    121 
    122 #ifndef GXIO_BLUETOOTH_ON_HWUART
    123 	/* HWUART configuration */
    124 	{ 48, GPIO_ALT_FN_1_OUT },	/* HWTXD */
    125 	{ 49, GPIO_ALT_FN_1_IN },	/* HWRXD */
    126 	{ 50, GPIO_ALT_FN_1_IN },	/* HWCTS */
    127 	{ 51, GPIO_ALT_FN_1_OUT },	/* HWRTS */
    128 #endif
    129 
    130 	{ -1 }
    131 };
    132 
    133 static const struct gxioconf busheader_conf[] = {
    134 	{ "cfstix",		cfstix_config },
    135 	{ "etherstix",		etherstix_config },
    136 	{ "netcf",		netcf_config },
    137 	{ "netduo-mmc",		netduommc_config },
    138 	{ "netduo",		netduo_config },
    139 	{ "netmmc",		netmmc_config },
    140 	{ "wifistix",		cfstix_config },
    141 	{ NULL }
    142 };
    143 
    144 
    145 /* ARGSUSED */
    146 static int
    147 gxiomatch(struct device *parent, struct cfdata *match, void *aux)
    148 {
    149 	bus_space_tag_t iot = &pxa2x0_bs_tag;
    150 	bus_space_handle_t ioh;
    151 
    152 	if (bus_space_map(iot,
    153 	    PXA2X0_MEMCTL_BASE, PXA2X0_MEMCTL_SIZE, 0, &ioh))
    154 		return (0);
    155 	bus_space_unmap(iot, ioh, PXA2X0_MEMCTL_SIZE);
    156 
    157 	/* nothing */
    158 	return (1);
    159 }
    160 
    161 /* ARGSUSED */
    162 static void
    163 gxioattach(struct device *parent, struct device *self, void *aux)
    164 {
    165 	struct gxio_softc *sc = device_private(self);
    166 
    167 	aprint_normal("\n");
    168 	aprint_naive("\n");
    169 
    170 	sc->sc_iot = &pxa2x0_bs_tag;
    171 
    172 	if (bus_space_map(sc->sc_iot,
    173 	    PXA2X0_MEMCTL_BASE, PXA2X0_MEMCTL_SIZE, 0, &sc->sc_ioh))
    174 		return;
    175 
    176 	/* configuration for GPIOs of busheader side */
    177 	gxio_config_gpio(sc, busheader_conf, busheader);
    178 
    179 	/*
    180 	 *  Attach each gumstix expantion of busheader side devices
    181 	 */
    182 	config_search_ia(gxiosearch, self, "gxio", NULL);
    183 }
    184 
    185 /* ARGSUSED */
    186 static int
    187 gxiosearch(
    188     struct device *parent, struct cfdata *cf, const int *ldesc, void *aux)
    189 {
    190 	struct gxio_softc *sc = device_private(parent);
    191 	struct gxio_attach_args gxa;
    192 
    193 	gxa.gxa_sc = sc;
    194 	gxa.gxa_iot = sc->sc_iot;
    195 	gxa.gxa_addr = cf->cf_loc[GXIOCF_ADDR];
    196 	gxa.gxa_gpirq = cf->cf_loc[GXIOCF_GPIRQ];
    197 
    198 	if (config_match(parent, cf, &gxa))
    199 		config_attach(parent, cf, &gxa, gxioprint);
    200 
    201 	return (0);
    202 }
    203 
    204 /* ARGSUSED */
    205 static int
    206 gxioprint(void *aux, const char *name)
    207 {
    208 	struct gxio_attach_args *gxa = (struct gxio_attach_args *)aux;
    209 
    210 	if (gxa->gxa_addr != GXIOCF_ADDR_DEFAULT)
    211 		printf(" addr 0x%lx", gxa->gxa_addr);
    212 	if (gxa->gxa_gpirq > 0)
    213 		printf(" gpirq %d", gxa->gxa_gpirq);
    214 	return (UNCONF);
    215 }
    216 
    217 
    218 /*
    219  * configure for GPIO pin and expansion boards.
    220  */
    221 void
    222 gxio_config_pin()
    223 {
    224 	int i;
    225 
    226 	/* XXX: turn off for power of bluetooth module */
    227 	pxa2x0_gpio_set_function(7, GPIO_OUT | GPIO_CLR);
    228 	delay(100);
    229 
    230 	for (i = 0; gpioconf[i].pin != -1; i++)
    231 		pxa2x0_gpio_set_function(gpioconf[i].pin, gpioconf[i].value);
    232 }
    233 
    234 static void
    235 gxio_config_gpio(struct gxio_softc *sc,
    236     const struct gxioconf *gxioconflist, char *expansion)
    237 {
    238 	int i, rv;
    239 
    240 	for (i = 0; i < strlen(expansion); i++)
    241 		expansion[i] = tolower(expansion[i]);
    242 	for (i = 0; gxioconflist[i].name != NULL; i++) {
    243 		rv = strncmp(expansion, gxioconflist[i].name,
    244 		    strlen(gxioconflist[i].name) + 1);
    245 		if (rv == 0) {
    246 			gxioconflist[i].config(sc);
    247 			break;
    248 		}
    249 	}
    250 }
    251 
    252 
    253 static void
    254 cfstix_config(struct gxio_softc *sc)
    255 {
    256 	u_int gpio, npoe_fn;
    257 
    258 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, MEMCTL_MECR,
    259 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, MEMCTL_MECR) & ~MECR_NOS);
    260 
    261 	pxa2x0_gpio_set_function(8, GPIO_OUT | GPIO_SET);	/* RESET */
    262 	delay(50);
    263 	pxa2x0_gpio_set_function(8, GPIO_OUT | GPIO_CLR);
    264 
    265 	pxa2x0_gpio_set_function(4, GPIO_IN); 		    /* nBVD1/~nSTSCHG */
    266 	pxa2x0_gpio_set_function(27, GPIO_IN);			/* ~INPACK */
    267 	pxa2x0_gpio_set_function(11, GPIO_IN);			/* nPCD1 */
    268 	pxa2x0_gpio_set_function(26, GPIO_IN);			/* PRDY/~IRQ */
    269 
    270 	for (gpio = 48, npoe_fn = 0; gpio <= 53 ; gpio++)
    271 		npoe_fn |= pxa2x0_gpio_get_function(gpio);
    272 	npoe_fn &= GPIO_SET;
    273 
    274 	pxa2x0_gpio_set_function(48, GPIO_ALT_FN_2_OUT | npoe_fn); /* nPOE */
    275 	pxa2x0_gpio_set_function(49, GPIO_ALT_FN_2_OUT);	/* nPWE */
    276 	pxa2x0_gpio_set_function(50, GPIO_ALT_FN_2_OUT);	/* nPIOR */
    277 	pxa2x0_gpio_set_function(51, GPIO_ALT_FN_2_OUT);	/* nPIOW */
    278 	pxa2x0_gpio_set_function(52, GPIO_ALT_FN_2_OUT);	/* nPCE1 */
    279 	pxa2x0_gpio_set_function(54, GPIO_ALT_FN_2_OUT);	/* pSKTSEL */
    280 	pxa2x0_gpio_set_function(55, GPIO_ALT_FN_2_OUT);	/* nPREG */
    281 	pxa2x0_gpio_set_function(56, GPIO_ALT_FN_1_IN);		/* nPWAIT */
    282 	pxa2x0_gpio_set_function(57, GPIO_ALT_FN_1_IN);		/* nIOIS16 */
    283 }
    284 
    285 /* ARGSUSED */
    286 static void
    287 etherstix_config(struct gxio_softc *sc)
    288 {
    289 
    290 	pxa2x0_gpio_set_function(49, GPIO_ALT_FN_2_OUT);	/* nPWE */
    291 	pxa2x0_gpio_set_function(15, GPIO_ALT_FN_2_OUT);	/* nCS 1 */
    292 	pxa2x0_gpio_set_function(80, GPIO_OUT | GPIO_SET);	/* RESET 1 */
    293 	delay(1);
    294 	pxa2x0_gpio_set_function(80, GPIO_OUT | GPIO_CLR);
    295 	delay(50000);
    296 }
    297 
    298 static void
    299 netcf_config(struct gxio_softc *sc)
    300 {
    301 
    302 	etherstix_config(sc);
    303 	cfstix_config(sc);
    304 }
    305 
    306 static void
    307 netduommc_config(struct gxio_softc *sc)
    308 {
    309 
    310 	netduo_config(sc);
    311 
    312 	/* mmc not yet... */
    313 }
    314 
    315 static void
    316 netduo_config(struct gxio_softc *sc)
    317 {
    318 
    319 	etherstix_config(sc);
    320 
    321 	pxa2x0_gpio_set_function(78, GPIO_ALT_FN_2_OUT);	/* nCS 2 */
    322 	pxa2x0_gpio_set_function(52, GPIO_OUT | GPIO_SET);	/* RESET 2 */
    323 	delay(1);
    324 	pxa2x0_gpio_set_function(52, GPIO_OUT | GPIO_CLR);
    325 	delay(50000);
    326 }
    327 
    328 static void
    329 netmmc_config(struct gxio_softc *sc)
    330 {
    331 
    332 	etherstix_config(sc);
    333 
    334 	/* mmc not yet... */
    335 }
    336