Home | History | Annotate | Line # | Download | only in adm5120
      1 /* $NetBSD: adm5120_extio.c,v 1.8 2021/08/07 16:18:58 thorpej 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.8 2021/08/07 16:18:58 thorpej Exp $");
     69 
     70 #include <sys/param.h>
     71 #include <sys/systm.h>
     72 #include <sys/device.h>
     73 
     74 #include <sys/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(device_t, cfdata_t, void *);
     98 static void	extio_attach(device_t, device_t, void *);
     99 static int	extio_submatch(device_t, cfdata_t, const int *, void *);
    100 static int	extio_print(void *, const char *);
    101 
    102 CFATTACH_DECL_NEW(extio, sizeof(struct extio_softc),
    103     extio_match, extio_attach, NULL, NULL);
    104 
    105 /* There can be only one. */
    106 int	extio_found;
    107 
    108 struct extiodev {
    109 	const char	*ed_name;
    110 	bus_addr_t	ed_addr;
    111 	int		ed_irq;
    112 	uint32_t	ed_gpio_mask;
    113 	int		ed_cfio;
    114 };
    115 
    116 struct extiodev extiodevs[] = {
    117 	{"wdc",		ADM5120_BASE_EXTIO0,	0,	__BIT(4),	1},
    118 	{NULL,		0,			0,	0x0,		0},
    119 };
    120 
    121 static int
    122 extio_match(device_t parent, cfdata_t match, void *aux)
    123 {
    124 	return !extio_found;
    125 }
    126 
    127 static void
    128 extio_attach_args_create(struct extio_attach_args *ea, struct extiodev *ed,
    129     void *gpio, bus_space_tag_t st)
    130 {
    131 	ea->ea_name = ed->ed_name;
    132 	ea->ea_addr = ed->ed_addr;
    133 	ea->ea_irq = ed->ed_irq;
    134 	ea->ea_st = st;
    135 	ea->ea_gpio = gpio;
    136 	ea->ea_gpio_mask = ed->ed_gpio_mask;
    137 	ea->ea_cfio = ed->ed_cfio;
    138 }
    139 
    140 static void
    141 extio_mpmc_dump(struct extio_softc *sc)
    142 {
    143 	EXTIO_DPRINTF("%s: regs:\n"
    144 	    "  ctl 0x%08" PRIx32 "\n"
    145 	    "  sts 0x%08" PRIx32 "\n"
    146 	    "   sc 0x%08" PRIx32 "\n"
    147 	    "  sww 0x%08" PRIx32 "\n"
    148 	    "  swo 0x%08" PRIx32 "\n"
    149 	    "  swr 0x%08" PRIx32 "\n"
    150 	    "  swp 0x%08" PRIx32 "\n"
    151 	    " swwr 0x%08" PRIx32 "\n"
    152 	    "  swt 0x%08" PRIx32 "\n", __func__,
    153 	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_CONTROL),
    154 	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_STATUS),
    155 	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SC(2)),
    156 	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWW(2)),
    157 	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWO(2)),
    158 	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWR(2)),
    159 	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWP(2)),
    160 	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWWR(2)),
    161 	    bus_space_read_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWT(2)));
    162 }
    163 
    164 static void
    165 extio_mpmc_init(struct extio_softc *sc)
    166 {
    167 	int i, s;
    168 #if 0
    169 	uint32_t control;
    170 #endif
    171 	uint32_t status;
    172 
    173 	/* Map MultiPort Memory Controller */
    174 	if (bus_space_map(sc->sc_obiot, ADM5120_BASE_MPMC, 0x280, 0,
    175 	                  &sc->sc_mpmch) != 0) {
    176 		aprint_error_dev(sc->sc_dev, "unable to map MPMC\n");
    177 		return;
    178 	}
    179 
    180 	extio_mpmc_dump(sc);
    181 
    182 #if 0
    183 	control = bus_space_read_4(sc->sc_obiot, sc->sc_mpmch,
    184 	    ADM5120_MPMC_CONTROL) | ADM5120_MPMC_CONTROL_DWB;
    185 	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_CONTROL,
    186 	    control);
    187 #endif
    188 
    189 	s = splhigh();
    190 	/* I wait for MPMC to become idle, and then I enter low-power mode
    191 	 * so that I can safely set the static configuration.
    192 	 */
    193 	for (i = 1000; --i > 0; ) {
    194 		status = bus_space_read_4(sc->sc_obiot, sc->sc_mpmch,
    195 		    ADM5120_MPMC_STATUS);
    196 		if ((status &
    197 		     (ADM5120_MPMC_STATUS_WBS|ADM5120_MPMC_STATUS_BU)) == 0)
    198 			break;
    199 		delay(10);
    200 	}
    201 
    202 	if (i == 0) {
    203 		aprint_error_dev(sc->sc_dev,
    204 		    "timeout waiting for MPMC idle\n");
    205 		splx(s);
    206 		return;
    207 	} else
    208 		EXTIO_DPRINTF("%s: MPMC idle\n", device_xname(sc->sc_dev));
    209 
    210 #if 0
    211 	control = bus_space_read_4(sc->sc_obiot, sc->sc_mpmch,
    212 	    ADM5120_MPMC_CONTROL) | ADM5120_MPMC_CONTROL_ME |
    213 	    ADM5120_MPMC_CONTROL_LPM;
    214 	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_CONTROL,
    215 	    control);
    216 #endif
    217 
    218 	/*
    219 	 * Configure external I/O to suit the CompactFlash card.
    220 	 *
    221 	 * Static Configuration 2
    222 	 *
    223 	 * 1 Enable 'async page mode four'.
    224 	 * 2 'Byte lane state' bits for active low for both read & write.
    225 	 * 3 No buffer, no write protection.
    226 	 * 4 No extended wait.
    227 	 * 5 Active low chip select.
    228 	 * 7 8-bit memory width.
    229 	 */
    230 	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SC(2),
    231 	    ADM5120_MPMC_SC_BLS|ADM5120_MPMC_SC_PM|ADM5120_MPMC_SC_MW_8B);
    232 
    233 	/*
    234 	 * Static Wait Wen 2: after asserting chip select, wait 3 HCLK cycles
    235 	 * before asserting write enable.
    236 	 */
    237 	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWW(2),
    238 	    __SHIFTIN(2, ADM5120_MPMC_SWW_WWE));
    239 
    240 	/*
    241 	 * Static Wait Oen 2: after selecting chip select, wait 3 HCLK cycles
    242 	 * before asserting output enable.
    243 	 */
    244 	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWO(2),
    245 	    __SHIFTIN(3, ADM5120_MPMC_SWO_WOE));
    246 
    247 	/*
    248 	 * Static Wait Rd 2: set wait state time to 27 HCLK cycles.
    249 	 */
    250 	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWR(2),
    251 	    __SHIFTIN(26, ADM5120_MPMC_SWR_NMRW));
    252 
    253 	/*
    254 	 * Static Wait Wait Page 2: set wait state time to 30 HCLK cycles.
    255 	 */
    256 	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWP(2),
    257 	    __SHIFTIN(29, ADM5120_MPMC_SWP_WPS));
    258 
    259 	/*
    260 	 * Static Wait Wait Wr 2: set wait state time to 22 HCLK cycles.
    261 	 */
    262 	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWWR(2),
    263 	    __SHIFTIN(20, ADM5120_MPMC_SWWR_WWS));
    264 
    265 	/*
    266 	 * Static Wait Wait Turn 2: 10 HCLK cycles for turnaround.
    267 	 */
    268 	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_SWT(2),
    269 	    __SHIFTIN(9, ADM5120_MPMC_SWT_WAITTURN));
    270 
    271 #if 0
    272 	/* Leave low-power mode. */
    273 	control = bus_space_read_4(sc->sc_obiot, sc->sc_mpmch,
    274 	    ADM5120_MPMC_CONTROL) &
    275 	    ~(ADM5120_MPMC_CONTROL_LPM|ADM5120_MPMC_CONTROL_DWB);
    276 	bus_space_write_4(sc->sc_obiot, sc->sc_mpmch, ADM5120_MPMC_CONTROL,
    277 	    control);
    278 	splx(s);
    279 #endif
    280 
    281 	extio_mpmc_dump(sc);
    282 }
    283 
    284 static void
    285 extio_attach(device_t parent, device_t self, void *aux)
    286 {
    287 	struct extio_softc *sc = device_private(self);
    288 	struct mainbus_attach_args *ma = (struct mainbus_attach_args *)aux;
    289 	struct extio_attach_args ea;
    290 	struct extiodev *ed;
    291 	struct adm5120_config *admc = &adm5120_configuration;
    292 
    293 	extio_found = 1;
    294 	printf("\n");
    295 
    296 	sc->sc_dev = self;
    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 		aprint_error_dev(sc->sc_dev, "failed to map GPIO[1:2]\n");
    322 	}
    323 	EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
    324 	gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 0, GPIO_PIN_INPUT);
    325 	gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 1, GPIO_PIN_OUTPUT);
    326 	gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 2, GPIO_PIN_INPUT);
    327 	gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 3, GPIO_PIN_OUTPUT);
    328 	gpio_pin_ctl(sc->sc_gpio, &sc->sc_pm, 4, GPIO_PIN_INPUT);
    329 	gpio_pin_write(sc->sc_gpio, &sc->sc_pm, 1, 0);
    330 	gpio_pin_write(sc->sc_gpio, &sc->sc_pm, 3, 0);
    331 
    332 	EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
    333 
    334 	if (gpio_pin_read(sc->sc_gpio, &sc->sc_pm, 0) == GPIO_PIN_HIGH) {
    335 		EXTIO_DPRINTF("%s: WAIT# inactive\n",
    336 		    device_xname(sc->sc_dev));
    337 		bus_space_write_4(sc->sc_obiot, sc->sc_gpioh, ADM5120_GPIO2,
    338 		    ADM5120_GPIO2_EW | ADM5120_GPIO2_CSX0 | ADM5120_GPIO2_CSX1);
    339 	} else {
    340 		aprint_error_dev(sc->sc_dev, "WAIT# active; may be stuck\n");
    341 		bus_space_write_4(sc->sc_obiot, sc->sc_gpioh, ADM5120_GPIO2,
    342 		    ADM5120_GPIO2_CSX0 | ADM5120_GPIO2_CSX1);
    343 	}
    344 	EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
    345 
    346 	/* Map MultiPort Memory Controller */
    347 	if (bus_space_map(sc->sc_obiot, ADM5120_BASE_MPMC, 0x280, 0,
    348 	                  &sc->sc_mpmch) != 0) {
    349 		aprint_error_dev(sc->sc_dev, "unable to map MPMC\n");
    350 		return;
    351 	}
    352 
    353 	extio_mpmc_init(sc);
    354 	EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
    355 
    356 	/* Program 5120 for level interrupts on GPIO[4] (INTX1).  (Not yet.)
    357 	 *
    358 	 * Map interrupt.  (Not yet.  In the mean time, use flags 0x1000 in
    359 	 * kernel configuration so that wdc(4) will expect no interrupts.)
    360 	 */
    361 
    362 	cfio_bus_mem_init(&sc->sc_cfio, &admc->extio_space);
    363 
    364 	for (ed = extiodevs; ed->ed_name != NULL; ed++) {
    365 		EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
    366 		extio_attach_args_create(&ea, ed, sc->sc_gpio,
    367 		    (ed->ed_cfio) ? &sc->sc_cfio : &admc->extio_space);
    368 		EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
    369 		config_found(self, &ea, extio_print,
    370 		    CFARGS(.submatch = extio_submatch,
    371 			   .iattr = "extio"));
    372 	}
    373 	EXTIO_DPRINTF("%s: %d\n", __func__, __LINE__);
    374 	extio_mpmc_dump(sc);
    375 }
    376 
    377 static int
    378 extio_submatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    379 {
    380 	struct extio_attach_args *ea = aux;
    381 
    382 	if (cf->cf_loc[EXTIOCF_CFIO] != EXTIOCF_CFIO_DEFAULT &&
    383 	    cf->cf_loc[EXTIOCF_CFIO] != ea->ea_cfio)
    384 		return 0;
    385 
    386 	if (cf->cf_loc[EXTIOCF_GPIO_MASK] != EXTIOCF_GPIO_MASK_DEFAULT &&
    387 	    cf->cf_loc[EXTIOCF_GPIO_MASK] != ea->ea_gpio_mask)
    388 		return 0;
    389 
    390 	if (cf->cf_loc[EXTIOCF_IRQ] != EXTIOCF_IRQ_DEFAULT &&
    391 	    cf->cf_loc[EXTIOCF_IRQ] != ea->ea_irq)
    392 		return 0;
    393 
    394 	if (cf->cf_loc[EXTIOCF_ADDR] != EXTIOCF_ADDR_DEFAULT &&
    395 	    cf->cf_loc[EXTIOCF_ADDR] != ea->ea_addr)
    396 		return 0;
    397 
    398 	return config_match(parent, cf, aux);
    399 }
    400 
    401 static int
    402 extio_print(void *aux, const char *pnp)
    403 {
    404 	struct extio_attach_args *ea = aux;
    405 
    406 	if (pnp != NULL)
    407 		aprint_normal("%s at %s", ea->ea_name, pnp);
    408 	if (ea->ea_cfio != EXTIOCF_CFIO_DEFAULT)
    409 		aprint_normal(" cfio");
    410 	if (ea->ea_addr != EXTIOCF_ADDR_DEFAULT)
    411 		aprint_normal(" addr 0x%"PRIxBUSADDR, ea->ea_addr);
    412 	if (ea->ea_gpio_mask != EXTIOCF_GPIO_MASK_DEFAULT)
    413 		aprint_normal(" gpio_mask 0x%02x", ea->ea_gpio_mask);
    414 
    415 	return UNCONF;
    416 }
    417