Home | History | Annotate | Line # | Download | only in rmi
rmixl_iobus.c revision 1.3
      1 /*	$NetBSD: rmixl_iobus.c,v 1.3 2011/07/01 19:01:31 dyoung Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Cliff Neighbors
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * RMI Peripherals IO Bus support
     34  * - interface to NOR, NAND, PCMCIA Memory controlers, &etc.
     35  * - manages the 10 Chip Selects
     36  * - manages the "Flash" interrupts
     37  * - manages the "Flash" errors
     38  */
     39 
     40 /*
     41  * iobus control registers are accessed as 32 bits.
     42  * ALEn and CLEn NAND control registers are defined as 8 bits wide
     43  * but that seems to be a documentation error.
     44  *
     45  * iobus data access may be as 1 or 2 or 4 bytes, even if device is 1 byte wide;
     46  * the controller will sequence the bytes, in big-endian order.
     47  */
     48 
     49 #include <sys/cdefs.h>
     50 __KERNEL_RCSID(0, "$NetBSD: rmixl_iobus.c,v 1.3 2011/07/01 19:01:31 dyoung Exp $");
     51 
     52 #include "locators.h"
     53 
     54 #include <sys/param.h>
     55 #include <sys/systm.h>
     56 #include <sys/device.h>
     57 
     58 #include <sys/bus.h>
     59 
     60 #include <mips/rmi/rmixlreg.h>
     61 #include <mips/rmi/rmixlvar.h>
     62 #include <mips/rmi/rmixl_intr.h>
     63 #include <mips/rmi/rmixl_obiovar.h>
     64 #include <mips/rmi/rmixl_iobusvar.h>
     65 // #include <mips/rmi/rmixl_gpiovar.h>
     66 
     67 typedef struct {
     68 	bool		cs_allocated;
     69 	uint32_t	cs_addr; /* base address on the Peripherals I/O Bus */
     70 	uint32_t	cs_mask; /* address mask on the Peripherals I/O Bus */
     71 	uint32_t	cs_dev_parm;
     72 } rmixl_iobus_csconfig_t;
     73 
     74 typedef struct rmixl_iobus_softc {
     75 	device_t		sc_dev;
     76 	bus_space_tag_t		sc_obio_bst;	/* for iobus device controler access */
     77 	bus_space_handle_t	sc_obio_bsh;	/*  "   "     "      "         "     */
     78 	bus_addr_t		sc_obio_addr;
     79 	bus_size_t		sc_obio_size;
     80 	bus_space_tag_t		sc_iobus_bst;	/* for iobus access */
     81 	rmixl_iobus_csconfig_t	sc_csconfig[RMIXL_FLASH_NCS];
     82 } rmixl_iobus_softc_t;
     83 
     84 
     85 static int	rmixl_iobus_match(device_t, cfdata_t, void *);
     86 static void	rmixl_iobus_attach(device_t, device_t, void *);
     87 static void	rmixl_iobus_csconfig_init(struct rmixl_iobus_softc *);
     88 static int  	rmixl_iobus_print(void *, const char *);
     89 static int  	rmixl_iobus_search(device_t, cfdata_t, const int *, void *);
     90 #ifdef NOTYET
     91 static int      rmixl_iobus_intr(void *);
     92 #endif
     93 
     94 #ifdef RMIXL_IOBUS_DEBUG
     95 rmixl_iobus_softc_t *rmixl_iobus_sc;
     96 #endif
     97 
     98 
     99 CFATTACH_DECL_NEW(rmixl_iobus, sizeof (rmixl_iobus_softc_t),
    100     rmixl_iobus_match, rmixl_iobus_attach, NULL, NULL);
    101 
    102 int
    103 rmixl_iobus_match(device_t parent, cfdata_t match, void *aux)
    104 {
    105 	struct obio_attach_args *obio = aux;
    106 
    107         if (obio->obio_addr == RMIXL_IO_DEV_FLASH)
    108 		return rmixl_probe_4((volatile uint32_t *)
    109 			RMIXL_IOREG_VADDR(obio->obio_addr));
    110 
    111         return 0;
    112 }
    113 
    114 void
    115 rmixl_iobus_attach(device_t parent, device_t self, void *aux)
    116 {
    117 	rmixl_iobus_softc_t *sc = device_private(self);
    118 	struct obio_attach_args *obio = aux;
    119 	struct rmixl_config *rcp = &rmixl_configuration;
    120 	uint64_t r;
    121 	int err;
    122 
    123 #ifdef RMIXL_IOBUS_DEBUG
    124 	rmixl_iobus_sc = sc;
    125 #endif
    126 	sc->sc_dev = self;
    127 	sc->sc_obio_bst = obio->obio_eb_bst;
    128 	sc->sc_obio_addr = obio->obio_addr;
    129 	sc->sc_obio_size = 0x1000;
    130 
    131 	err = bus_space_map(sc->sc_obio_bst, sc->sc_obio_addr,
    132 		sc->sc_obio_size, 0, &sc->sc_obio_bsh);
    133 	if (err != 0) {
    134 		aprint_error_dev(self,
    135 			"bus space map err %d, iobus space\n", err);
    136 		return;
    137 	}
    138 
    139 	r = RMIXL_IOREG_READ(RMIXL_SBC_FLASH_BAR);
    140 	KASSERT((r & 1) != 0);	/* BAR is enabled */
    141 	rcp->rc_flash_pbase = RMIXL_FLASH_BAR_TO_BA(r);
    142 	rcp->rc_flash_mask  = RMIXL_FLASH_BAR_TO_MASK(r);
    143 
    144 	aprint_normal("\n");
    145 	aprint_debug_dev(self,
    146 		"Flash BAR pbase %#" PRIx64 " mask %#" PRIx64 "\n",
    147 		rcp->rc_flash_pbase, rcp->rc_flash_mask);
    148 
    149 	/* initialize iobus bus space */
    150 	rmixl_iobus_bus_mem_init(&rcp->rc_iobus_memt, rcp);
    151 	sc->sc_iobus_bst = (bus_space_tag_t)&rcp->rc_iobus_memt;
    152 
    153 	/* disable all Flsah interupts */
    154 	bus_space_write_4(sc->sc_obio_bst, sc->sc_obio_bsh,
    155 		RMIXL_FLASH_INT_MASK, 0);
    156 
    157 	/* write-1-to-clear Flash interrupt status */
    158 	bus_space_write_4(sc->sc_obio_bst, sc->sc_obio_bsh,
    159 		RMIXL_FLASH_INT_STATUS, ~0);
    160 
    161 	rmixl_iobus_csconfig_init(sc);
    162 
    163 	/* attach any children */
    164 	config_search_ia(rmixl_iobus_search, self, "rmixl_iobus", NULL);
    165 }
    166 
    167 static void
    168 rmixl_iobus_csconfig_init(struct rmixl_iobus_softc *sc)
    169 {
    170 	rmixl_iobus_csconfig_t *cs = &sc->sc_csconfig[0];
    171 
    172 	for (int i=0; i < RMIXL_FLASH_NCS; i++) {
    173 		memset(cs, 0, sizeof(rmixl_iobus_csconfig_t));
    174 		cs->cs_addr = bus_space_read_4(sc->sc_obio_bst, sc->sc_obio_bsh,
    175 				RMIXL_FLASH_CSBASE_ADDRn(i)) << 16;
    176 		cs->cs_mask = bus_space_read_4(sc->sc_obio_bst, sc->sc_obio_bsh,
    177 				RMIXL_FLASH_CSADDR_MASKn(i)) << 16;
    178 		cs->cs_mask |= __BITS(15,0);
    179 		cs->cs_dev_parm = bus_space_read_4(sc->sc_obio_bst, sc->sc_obio_bsh,
    180 				RMIXL_FLASH_CSDEV_PARMn(i));
    181 		aprint_debug_dev(sc->sc_dev,
    182 			"CS#%d: addr 0x%08x mask 0x%08x parm 0x%08x\n",
    183 			i, cs->cs_addr, cs->cs_mask, cs->cs_dev_parm);
    184 		cs++;
    185 	}
    186 }
    187 
    188 
    189 static int
    190 rmixl_iobus_print(void *aux, const char *pnp)
    191 {
    192 	struct rmixl_iobus_attach_args *ia = aux;
    193 
    194 	if (ia->ia_cs != RMIXL_IOBUSCF_CS_DEFAULT)
    195 		aprint_normal(" CS#%d", ia->ia_cs);
    196 	if (ia->ia_iobus_addr != RMIXL_IOBUSCF_ADDR_DEFAULT) {
    197 		aprint_normal(" addr %#" PRIxBUSADDR, ia->ia_iobus_addr);
    198 		if (ia->ia_iobus_size != RMIXL_IOBUSCF_SIZE_DEFAULT)
    199 			aprint_normal("-%#" PRIxBUSSIZE,
    200 				ia->ia_iobus_addr + (ia->ia_iobus_size - 1));
    201 	}
    202 	if (ia->ia_iobus_intr != RMIXL_IOBUSCF_INTR_DEFAULT)
    203 		aprint_normal(" intr %d", ia->ia_iobus_intr);
    204 
    205 	return UNCONF;
    206 }
    207 
    208 static int
    209 rmixl_iobus_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    210 {
    211 	struct rmixl_iobus_softc *sc = device_private(parent);
    212 	struct rmixl_iobus_attach_args ia;
    213 	rmixl_iobus_csconfig_t *cs;
    214 
    215 	ia.ia_obio_bst = sc->sc_obio_bst;
    216 	ia.ia_obio_bsh = sc->sc_obio_bsh;
    217 	ia.ia_iobus_bst = sc->sc_iobus_bst;
    218 	ia.ia_iobus_addr = (bus_addr_t)cf->cf_loc[RMIXL_IOBUSCF_ADDR];
    219 	ia.ia_iobus_size = (bus_size_t)cf->cf_loc[RMIXL_IOBUSCF_SIZE];
    220 	ia.ia_iobus_intr = cf->cf_loc[RMIXL_IOBUSCF_INTR];
    221 	ia.ia_cs = cf->cf_loc[RMIXL_IOBUSCF_CS];
    222 
    223 	if (ia.ia_cs != RMIXL_IOBUSCF_CS_DEFAULT) {
    224 		/* CS is configured */
    225 		cs = &sc->sc_csconfig[ia.ia_cs];
    226 
    227 		/* ensure exclusive use of chip select */
    228 		if (cs->cs_allocated) {
    229 			aprint_error_dev(parent, "CS#%d already allocated\n",
    230 				ia.ia_cs);
    231 			return 0;
    232 		}
    233 		if (ia.ia_iobus_addr != RMIXL_IOBUSCF_ADDR_DEFAULT) {
    234 			if (ia.ia_iobus_addr != cs->cs_addr) {
    235 				/*
    236 				 * both CS and addr are configured,
    237 				 * ensure they match
    238 				 */
    239 				aprint_error_dev(parent,
    240 					"CS#%d addr 0x%08x mismatch cf_loc "
    241 					"addr 0x%08" PRIxBUSADDR "\n",
    242 					ia.ia_cs, cs->cs_addr, ia.ia_iobus_addr);
    243 				return 0;
    244 			}
    245 		} else {
    246 			/* no addr configured, pull from CS */
    247 			ia.ia_iobus_addr = cs->cs_addr;
    248 		}
    249 	} else {
    250 		/* addr is configured, CS is not; search for matching CS */
    251 		bool found = false;
    252 		cs = &sc->sc_csconfig[0];
    253 		for (int i=0; i < RMIXL_FLASH_NCS; i++) {
    254 			if (cs->cs_allocated)
    255 				continue;
    256 			if (cs->cs_addr == ia.ia_iobus_addr) {
    257 				ia.ia_cs = i;
    258 				found = true;
    259 				break;
    260 			}
    261 			cs++;
    262 		}
    263 		if (! found) {
    264 			aprint_error_dev(parent, "no CS for addr 0x%08"
    265 				PRIxBUSADDR "\n", ia.ia_iobus_addr);
    266 			return 0;
    267 		}
    268 	}
    269 
    270 	if (ia.ia_iobus_size != RMIXL_IOBUSCF_SIZE_DEFAULT) {
    271 		/* ensure size fits w/ CS mask */
    272 		if ((ia.ia_iobus_size - 1) > (bus_size_t)cs->cs_mask) {
    273 			aprint_error_dev(parent, "size %#" PRIxBUSSIZE
    274 				" exceeds CS#%d mask 0x%08x\n",
    275 				ia.ia_iobus_size, ia.ia_cs, cs->cs_mask);
    276 		}
    277 	} else {
    278 		/* size not configured, pull from CS */
    279 		ia.ia_iobus_size = (bus_size_t)cs->cs_mask + 1;
    280 	}
    281 
    282 	ia.ia_dev_parm = cs->cs_dev_parm;
    283 
    284 	if (config_match(parent, cf, &ia) > 0) {
    285 		cs->cs_allocated = true;
    286 		config_attach(parent, cf, &ia, rmixl_iobus_print);
    287 	}
    288 
    289 	return 0;
    290 }
    291 
    292 
    293 #ifdef NOTYET
    294 
    295 void
    296 rmixl_iobus_intr_disestablish(void *uh, void *ih)
    297 {
    298 	rmixl_iobus_softc_t *sc = uh;
    299 	u_int intr;
    300 
    301 	for (intr=0; intr <= RMIXL_UB_INTERRUPT_MAX; intr++) {
    302 		if (ih == &sc->sc_dispatch[intr]) {
    303 			uint32_t r;
    304 
    305 			/* disable this interrupt in the usb interface */
    306 			r = bus_space_read_4(sc->sc_obio_bst, sc->sc_obio_bsh,
    307 				RMIXL_USB_INTERRUPT_ENABLE);
    308 			r &= 1 << intr;
    309 			bus_space_write_4(sc->sc_obio_bst, sc->sc_obio_bsh,
    310 				RMIXL_USB_INTERRUPT_ENABLE, r);
    311 
    312 			/* free the dispatch slot */
    313 			sc->sc_dispatch[intr].func = NULL;
    314 			sc->sc_dispatch[intr].arg = NULL;
    315 
    316 			break;
    317 		}
    318 	}
    319 }
    320 
    321 void *
    322 rmixl_iobus_intr_establish(void *uh, u_int intr, int (func)(void *), void *arg)
    323 {
    324 	rmixl_iobus_softc_t *sc = uh;
    325 	uint32_t r;
    326 	void *ih = NULL;
    327 	int s;
    328 
    329 	s = splusb();
    330 
    331 	if (intr > RMIXL_UB_INTERRUPT_MAX) {
    332 		aprint_error_dev(sc->sc_dev, "invalid intr %d\n", intr);
    333 		goto out;
    334 	}
    335 
    336 	if (sc->sc_dispatch[intr].func != NULL) {
    337 		aprint_error_dev(sc->sc_dev, "intr %dq busy\n", intr);
    338 		goto out;
    339 	}
    340 
    341 	sc->sc_dispatch[intr].func = func;
    342 	sc->sc_dispatch[intr].arg = arg;
    343 	ih = &sc->sc_dispatch[intr];
    344 
    345 	/* enable this interrupt in the usb interface */
    346 	r = bus_space_read_4(sc->sc_obio_bst, sc->sc_obio_bsh,
    347 		RMIXL_USB_INTERRUPT_ENABLE);
    348 	r |= 1 << intr;
    349 	bus_space_write_4(sc->sc_obio_bst, sc->sc_obio_bsh,
    350 		RMIXL_USB_INTERRUPT_ENABLE, r);
    351 
    352  out:
    353 	splx(s);
    354 	return ih;
    355 }
    356 
    357 static int
    358 rmixl_iobus_intr(void *arg)
    359 {
    360 	rmixl_iobus_softc_t *sc = arg;
    361 	uint32_t r;
    362 	int intr;
    363 	int rv = 0;
    364 
    365 	r = bus_space_read_4(sc->sc_obio_bst, sc->sc_obio_bsh,
    366 		RMIXL_USB_INTERRUPT_STATUS);
    367 	if (r != 0) {
    368 		for (intr=0; intr <= RMIXL_UB_INTERRUPT_MAX; intr++) {
    369 			uint32_t bit = 1 << intr;
    370 			if ((r & bit) != 0) {
    371 				int (*f)(void *) = sc->sc_dispatch[intr].func;
    372 				void *a = sc->sc_dispatch[intr].arg;
    373 				if (f != NULL) {
    374 					(void)(*f)(a);
    375 					sc->sc_dispatch[intr].count.ev_count++;
    376 					rv = 1;
    377 				}
    378 			}
    379 		}
    380 	}
    381 
    382 	return rv;
    383 }
    384 
    385 #endif	/* NOTYET */
    386