Home | History | Annotate | Line # | Download | only in adm5120
adm5120_extio.c revision 1.1
      1 /* $NetBSD: adm5120_extio.c,v 1.1 2007/03/20 08:52:03 dyoung Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 David Young.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or
      7  * without modification, are permitted provided that the following
      8  * conditions 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
     12  *    copyright notice, this list of conditions and the following
     13  *    disclaimer in the documentation and/or other materials provided
     14  *    with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote
     16  *    products derived from this software without specific prior
     17  *    written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
     20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     22  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
     24  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
     26  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
     28  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
     30  * OF SUCH DAMAGE.
     31  */
     32 /*
     33  * Copyright 2002 Wasabi Systems, Inc.
     34  * All rights reserved.
     35  *
     36  * Written by Simon Burge for Wasabi Systems, Inc.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  * 3. All advertising materials mentioning features or use of this software
     47  *    must display the following acknowledgement:
     48  *      This product includes software developed for the NetBSD Project by
     49  *      Wasabi Systems, Inc.
     50  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     51  *    or promote products derived from this software without specific prior
     52  *    written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     56  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     57  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     58  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     59  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     60  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     61  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     62  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     63  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     64  * POSSIBILITY OF SUCH DAMAGE.
     65  */
     66 
     67 #include <sys/cdefs.h>
     68 __KERNEL_RCSID(0, "$NetBSD: adm5120_extio.c,v 1.1 2007/03/20 08:52:03 dyoung Exp $");
     69 
     70 #include <sys/param.h>
     71 #include <sys/systm.h>
     72 #include <sys/device.h>
     73 
     74 #include <machine/bus.h>
     75 
     76 #include <mips/cache.h>
     77 #include <mips/cpuregs.h>
     78 
     79 #include <mips/adm5120/include/adm5120reg.h>
     80 #include <mips/adm5120/include/adm5120var.h>
     81 #include <mips/adm5120/include/adm5120_mainbusvar.h>
     82 #include <mips/adm5120/include/adm5120_extiovar.h>
     83 
     84 #include "locators.h"
     85 
     86 #ifdef EXTIO_DEBUG
     87 int extio_debug = 1;
     88 #define	EXTIO_DPRINTF(__fmt, ...)		\
     89 do {						\
     90 	if (extio_debug)			\
     91 		printf((__fmt), __VA_ARGS__);	\
     92 } while (/*CONSTCOND*/0)
     93 #else /* !EXTIO_DEBUG */
     94 #define	EXTIO_DPRINTF(__fmt, ...)	do { } while (/*CONSTCOND*/0)
     95 #endif /* EXTIO_DEBUG */
     96 
     97 static int	extio_match(struct device *, struct cfdata *, void *);
     98 static void	extio_attach(struct device *, struct device *, void *);
     99 static int	extio_submatch(struct device *, struct cfdata *,
    100 			       const int *, void *);
    101 static int	extio_print(void *, const char *);
    102 
    103 CFATTACH_DECL(extio, sizeof(struct extio_softc),
    104     extio_match, extio_attach, NULL, NULL);
    105 
    106 /* There can be only one. */
    107 int	extio_found;
    108 
    109 struct extiodev {
    110 	const char	*ed_name;
    111 	bus_addr_t	ed_addr;
    112 	int		ed_irq;
    113 	uint32_t	ed_gpio_mask;
    114 	int		ed_cfio;
    115 };
    116 
    117 struct extiodev extiodevs[] = {
    118 	{"wdc",		ADM5120_BASE_EXTIO0,	0,	__BIT(4),	1},
    119 	{NULL,		0,			0,	0x0,		0},
    120 };
    121 
    122 static int
    123 extio_match(struct device *parent, struct cfdata *match, void *aux)
    124 {
    125 	return !extio_found;
    126 }
    127 
    128 static void
    129 extio_attach_args_create(struct extio_attach_args *ea, struct extiodev *ed,
    130     void *gpio, bus_space_tag_t st)
    131 {
    132 	ea->ea_name = ed->ed_name;
    133 	ea->ea_addr = ed->ed_addr;
    134 	ea->ea_irq = ed->ed_irq;
    135 	ea->ea_st = st;
    136 	ea->ea_gpio = gpio;
    137 	ea->ea_gpio_mask = ed->ed_gpio_mask;
    138 	ea->ea_cfio = ed->ed_cfio;
    139 }
    140 
    141 static void
    142 extio_mpmc_dump(struct extio_softc *sc)
    143 {
    144 	EXTIO_DPRINTF("%s: regs:\n"
    145 	    "  ctl 0x%08" PRIx32 "\n"
    146 	    "  sts 0x%08" PRIx32 "\n"
    147 	    "   sc 0x%08" PRIx32 "\n"
    148 	    "  sww 0x%08" PRIx32 "\n"
    149 	    "  swo 0x%08" PRIx32 "\n"
    150 	    "  swr 0x%08" PRIx32 "\n"
    151 	    "  swp 0x%08" PRIx32 "\n"
    152 	    " swwr 0x%08" PRIx32 "\n"
    153 	    "  swt 0x%08" PRIx32 "\n", __func__,
    154 	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_CONTROL),
    155 	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_STATUS),
    156 	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SC(2)),
    157 	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWW(2)),
    158 	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWO(2)),
    159 	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWR(2)),
    160 	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWP(2)),
    161 	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWWR(2)),
    162 	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWT(2)));
    163 }
    164 
    165 static void
    166 extio_mpmc_init(struct extio_softc *sc)
    167 {
    168 	int i, s;
    169 #if 0
    170 	uint32_t control;
    171 #endif
    172 	uint32_t status;
    173 
    174 	/* Map MultiPort Memory Controller */
    175 	if (bus_space_map(sc->sc_obiot, ADM5120_BASE_MPMC, 0x280, 0,
    176 	                  &sc->sc_mpmch) != 0) {
    177 		printf("%s: unable to map MPMC\n", device_xname(&sc->sc_dev));
    178 		return;
    179 	}
    180 
    181 	extio_mpmc_dump(sc);
    182 
    183 #if 0
    184 	control = bus_space_read_4(sc->sc_obiot, sc->sc_mpmch,
    185 	    ADM5120_MPMC_CONTROL) | ADM5120_MPMC_CONTROL_DWB;
    186 	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_CONTROL,
    187 	    control);
    188 #endif
    189 
    190 	s = splhigh();
    191 	/* I wait for MPMC to become idle, and then I enter low-power mode
    192 	 * so that I can safely set the static configuration.
    193 	 */
    194 	for (i = 1000; --i > 0; ) {
    195 		status = bus_space_read_4(sc->sc_obiot, sc->sc_mpmch,
    196 		    ADM5120_MPMC_STATUS);
    197 		if ((status &
    198 		     (ADM5120_MPMC_STATUS_WBS|ADM5120_MPMC_STATUS_BU)) == 0)
    199 			break;
    200 		delay(10);
    201 	}
    202 
    203 	if (i == 0) {
    204 		printf("%s: timeout waiting for MPMC idle\n",
    205 		    device_xname(&sc->sc_dev));
    206 		splx(s);
    207 		return;
    208 	} else
    209 		EXTIO_DPRINTF("%s: MPMC idle\n", device_xname(&sc->sc_dev));
    210 
    211 #if 0
    212 	control = bus_space_read_4(sc->sc_obiot, sc->sc_mpmch,
    213 	    ADM5120_MPMC_CONTROL) | ADM5120_MPMC_CONTROL_ME |
    214 	    ADM5120_MPMC_CONTROL_LPM;
    215 	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_CONTROL,
    216 	    control);
    217 #endif
    218 
    219 	/*
    220 	 * Configure external I/O to suit the CompactFlash card.
    221 	 *
    222 	 * Static Configuration 2
    223 	 *
    224 	 * 1 Enable 'async page mode four'.
    225 	 * 2 'Byte lane state' bits for active low for both read & write.
    226 	 * 3 No buffer, no write protection.
    227 	 * 4 No extended wait.
    228 	 * 5 Active low chip select.
    229 	 * 7 8-bit memory width.
    230 	 */
    231 	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SC(2),
    232 	    ADM5120_MPMC_SC_BLS|ADM5120_MPMC_SC_PM|ADM5120_MPMC_SC_MW_8B);
    233 
    234 	/*
    235 	 * Static Wait Wen 2: after asserting chip select, wait 3 HCLK cycles
    236 	 * before asserting write enable.
    237 	 */
    238 	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWW(2),
    239 	    __SHIFTIN(2, ADM5120_MPMC_SWW_WWE));
    240 
    241 	/*
    242 	 * Static Wait Oen 2: after selecting chip select, wait 3 HCLK cycles
    243 	 * before asserting output enable.
    244 	 */
    245 	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWO(2),
    246 	    __SHIFTIN(3, ADM5120_MPMC_SWO_WOE));
    247 
    248 	/*
    249 	 * Static Wait Rd 2: set wait state time to 27 HCLK cycles.
    250 	 */
    251 	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWR(2),
    252 	    __SHIFTIN(26, ADM5120_MPMC_SWR_NMRW));
    253 
    254 	/*
    255 	 * Static Wait Wait Page 2: set wait state time to 30 HCLK cycles.
    256 	 */
    257 	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWP(2),
    258 	    __SHIFTIN(29, ADM5120_MPMC_SWP_WPS));
    259 
    260 	/*
    261 	 * Static Wait Wait Wr 2: set wait state time to 22 HCLK cycles.
    262 	 */
    263 	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWWR(2),
    264 	    __SHIFTIN(20, ADM5120_MPMC_SWWR_WWS));
    265 
    266 	/*
    267 	 * Static Wait Wait Turn 2: 10 HCLK cycles for turnaround.
    268 	 */
    269 	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWT(2),
    270 	    __SHIFTIN(9, ADM5120_MPMC_SWT_WAITTURN));
    271 
    272 #if 0
    273 	/* Leave low-power mode. */
    274 	control = bus_space_read_4(sc->sc_obiot, sc->sc_mpmch,
    275 	    ADM5120_MPMC_CONTROL) &
    276 	    ~(ADM5120_MPMC_CONTROL_LPM|ADM5120_MPMC_CONTROL_DWB);
    277 	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_CONTROL,
    278 	    control);
    279 	splx(s);
    280 #endif
    281 
    282 	extio_mpmc_dump(sc);
    283 }
    284 
    285 static void
    286 extio_attach(struct device *parent, struct device *self, void *aux)
    287 {
    288 	struct extio_softc *sc = (struct extio_softc *)self;
    289 	struct mainbus_attach_args *ma = (struct mainbus_attach_args *)aux;
    290 	struct extio_attach_args ea;
    291 	struct extiodev *ed;
    292 	struct adm5120_config *admc = &adm5120_configuration;
    293 
    294 	extio_found = 1;
    295 	printf("\n");
    296 
    297 	sc->sc_gpio = ma->ma_gpio;
    298 	sc->sc_obiot = ma->ma_obiot;
    299 	sc->sc_gpioh = ma->ma_gpioh;
    300 
    301 	EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
    302 
    303 	sc->sc_pm.pm_map = &sc->sc_map[0];
    304 
    305 	/* Map GPIO[0] (WAIT#) for input.
    306 	 *
    307 	 * If WAIT# is high (inactive), then enable WAIT# handshake for
    308 	 * EXTIO0 accesses.  Otherwise, assume that WAIT# is
    309 	 * stuck low (active), in which case all accesses would timeout
    310 	 * if we enabled WAIT# handshake.
    311 	 *
    312 	 * Map GPIO[1:2].  Program 5120 to treat GPIO[1:2] as
    313 	 * Chip Select / Interrupt pins for External I/O #0.
    314 	 *
    315 	 * Map GPIO[3:4].  Program 5120 to treat GPIO[3:4] as
    316 	 * Chip Select / Interrupt pins for External I/O #1.
    317 	 *
    318 	 * Use GPIO[4] for interrupts.  (Not yet.)
    319 	 */
    320 	if (gpio_pin_map(sc->sc_gpio, 0, __BITS(0, 4), &sc->sc_pm) != 0) {
    321 		printf("%s: failed to map GPIO[1:2]\n",
    322 		    device_xname(&sc->sc_dev));
    323 	}
    324 	EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
    325 	gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 0, GPIO_PIN_INPUT);
    326 	gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 1, GPIO_PIN_OUTPUT);
    327 	gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 2, GPIO_PIN_INPUT);
    328 	gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 3, GPIO_PIN_OUTPUT);
    329 	gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 4, GPIO_PIN_INPUT);
    330 	gpio_pin_write(sc->sc_gpio, &sc->sc_pm, 1, 0);
    331 	gpio_pin_write(sc->sc_gpio, &sc->sc_pm, 3, 0);
    332 
    333 	EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
    334 
    335 	if (gpio_pin_read(sc->sc_gpio, &sc->sc_pm, 0) == GPIO_PIN_HIGH) {
    336 		EXTIO_DPRINTF("%s: WAIT# inactive\n",
    337 		    device_xname(&sc->sc_dev));
    338 		bus_space_write_4(sc->sc_obiot, sc->sc_gpioh, ADM5120_GPIO2,
    339 		    ADM5120_GPIO2_EW | ADM5120_GPIO2_CSX0 | ADM5120_GPIO2_CSX1);
    340 	} else {
    341 		printf("%s: WAIT# active; may be stuck\n",
    342 		    device_xname(&sc->sc_dev));
    343 		bus_space_write_4(sc->sc_obiot, sc->sc_gpioh, ADM5120_GPIO2,
    344 		    ADM5120_GPIO2_CSX0 | ADM5120_GPIO2_CSX1);
    345 	}
    346 	EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
    347 
    348 	/* Map MultiPort Memory Controller */
    349 	if (bus_space_map(sc->sc_obiot, ADM5120_BASE_MPMC, 0x280, 0,
    350 	                  &sc->sc_mpmch) != 0) {
    351 		printf("%s: unable to map MPMC\n", device_xname(&sc->sc_dev));
    352 		return;
    353 	}
    354 
    355 	extio_mpmc_init(sc);
    356 	EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
    357 
    358 	/* Program 5120 for level interrupts on GPIO[4] (INTX1).  (Not yet.)
    359 	 *
    360 	 * Map interrupt.  (Not yet.  In the mean time, use flags 0x1000 in
    361 	 * kernel configuration so that wdc(4) will expect no interrupts.)
    362 	 */
    363 
    364 	cfio_bus_mem_init(&sc->sc_cfio, &admc->extio_space);
    365 
    366 	for (ed = extiodevs; ed->ed_name != NULL; ed++) {
    367 		EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
    368 		extio_attach_args_create(&ea, ed, sc->sc_gpio,
    369 		    (ed->ed_cfio) ? &sc->sc_cfio : &admc->extio_space);
    370 		EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
    371 		(void)config_found_sm_loc(self, "extio", NULL, &ea, extio_print,
    372 		    extio_submatch);
    373 	}
    374 	EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
    375 	extio_mpmc_dump(sc);
    376 }
    377 
    378 static int
    379 extio_submatch(struct device *parent, struct cfdata *cf,
    380 	       const int *ldesc, void *aux)
    381 {
    382 	struct extio_attach_args *ea = aux;
    383 
    384 	if (cf->cf_loc[EXTIOCF_CFIO] != EXTIOCF_CFIO_DEFAULT &&
    385 	    cf->cf_loc[EXTIOCF_CFIO] != ea->ea_cfio)
    386 		return 0;
    387 
    388 	if (cf->cf_loc[EXTIOCF_GPIO_MASK] != EXTIOCF_GPIO_MASK_DEFAULT &&
    389 	    cf->cf_loc[EXTIOCF_GPIO_MASK] != ea->ea_gpio_mask)
    390 		return 0;
    391 
    392 	if (cf->cf_loc[EXTIOCF_IRQ] != EXTIOCF_IRQ_DEFAULT &&
    393 	    cf->cf_loc[EXTIOCF_IRQ] != ea->ea_irq)
    394 		return 0;
    395 
    396 	if (cf->cf_loc[EXTIOCF_ADDR] != EXTIOCF_ADDR_DEFAULT &&
    397 	    cf->cf_loc[EXTIOCF_ADDR] != ea->ea_addr)
    398 		return 0;
    399 
    400 	return config_match(parent, cf, aux);
    401 }
    402 
    403 static int
    404 extio_print(void *aux, const char *pnp)
    405 {
    406 	struct extio_attach_args *ea = aux;
    407 
    408 	if (pnp != NULL)
    409 		aprint_normal("%s at %s", ea->ea_name, pnp);
    410 	if (ea->ea_cfio != EXTIOCF_CFIO_DEFAULT)
    411 		aprint_normal(" cfio");
    412 	if (ea->ea_addr != EXTIOCF_ADDR_DEFAULT)
    413 		aprint_normal(" addr 0x%lx", ea->ea_addr);
    414 	if (ea->ea_gpio_mask != EXTIOCF_GPIO_MASK_DEFAULT)
    415 		aprint_normal(" gpio_mask 0x%02x", ea->ea_gpio_mask);
    416 
    417 	return UNCONF;
    418 }
    419