Home | History | Annotate | Line # | Download | only in dev
ebus.c revision 1.3
      1 /*	$NetBSD: ebus.c,v 1.3 2002/02/08 13:10:42 uwe Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1999, 2000 Matthew R. Green
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * EBus support for PCI based SPARC systems (ms-IIep, Ultra).
     33  * EBus is documented in PCIO manual (Sun Part#: 802-7837-01).
     34  */
     35 
     36 #if defined(DEBUG) && !defined(EBUS_DEBUG)
     37 #define EBUS_DEBUG
     38 #endif
     39 
     40 #ifdef EBUS_DEBUG
     41 #define	EDB_PROM	0x01
     42 #define EDB_CHILD	0x02
     43 #define	EDB_INTRMAP	0x04
     44 #define EDB_BUSMAP	0x08
     45 #define EDB_BUSDMA	0x10
     46 #define EDB_INTR	0x20
     47 int ebus_debug = 0;
     48 #define DPRINTF(l, s)   do { if (ebus_debug & l) printf s; } while (0)
     49 #else
     50 #define DPRINTF(l, s)
     51 #endif
     52 
     53 #include <sys/param.h>
     54 #include <sys/conf.h>
     55 #include <sys/device.h>
     56 #include <sys/errno.h>
     57 #include <sys/extent.h>
     58 #include <sys/malloc.h>
     59 #include <sys/systm.h>
     60 #include <sys/time.h>
     61 
     62 #define _SPARC_BUS_DMA_PRIVATE
     63 #include <machine/bus.h>
     64 #include <machine/autoconf.h>
     65 #include <machine/openfirm.h>
     66 
     67 #include <dev/pci/pcivar.h>
     68 #include <dev/pci/pcireg.h>
     69 #include <dev/pci/pcidevs.h>
     70 
     71 #include <dev/ofw/ofw_pci.h>
     72 
     73 /* XXX: convert to use shared <dev/ebus/ebusreg.h> */
     74 #include <sparc/dev/ebusreg.h>
     75 #include <sparc/dev/ebusvar.h>
     76 
     77 
     78 int	ebus_match(struct device *, struct cfdata *, void *);
     79 void	ebus_attach(struct device *, struct device *, void *);
     80 
     81 struct cfattach ebus_ca = {
     82 	sizeof(struct ebus_softc), ebus_match, ebus_attach
     83 };
     84 
     85 
     86 int	ebus_setup_attach_args(struct ebus_softc *, int,
     87 			       struct ebus_attach_args *);
     88 void	ebus_destroy_attach_args(struct ebus_attach_args *);
     89 int	ebus_print(void *, const char *);
     90 
     91 /*
     92  * here are our bus space and bus dma routines.
     93  */
     94 static paddr_t	ebus_bus_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);
     95 static int	_ebus_bus_map(bus_space_tag_t, bus_type_t, bus_addr_t,
     96 			      bus_size_t, int, vaddr_t, bus_space_handle_t *);
     97 static void	*ebus_intr_establish(bus_space_tag_t, int, int, int,
     98 				     int (*)(void *), void *);
     99 
    100 /*
    101  * Working around PROM bogosity.
    102  *
    103  * EBus doesn't have official OFW binding.  sparc64 has a de-facto
    104  * standard but patching it in in prompatch.c and then decoding it
    105  * here would be an overkill for ms-IIep.
    106  *
    107  * So we assume that all ms-IIep based systems use PCIO chip only in
    108  * "motherboard mode" with interrupt lines wired directly to ms-IIep
    109  * interrupt inputs.
    110  *
    111  * Note that this is ineligible for prompatch.c, as we are not
    112  * correcting PROM to conform to some established standard, this hack
    113  * is tied to this version of ebus driver and as such it's better stay
    114  * private to the driver.
    115  */
    116 
    117 struct msiiep_ebus_intr_wiring {
    118 	const char *name;	/* PROM node */
    119 	int line;		/* ms-IIep interrupt input */
    120 };
    121 
    122 static struct msiiep_ebus_intr_wiring krups_ebus_intr_wiring[] = {
    123 	{ "su", 0 }, { "8042", 0 }, { "sound", 3 }
    124 };
    125 
    126 
    127 struct msiiep_known_ebus_wiring {
    128 	const char *model;
    129 	struct msiiep_ebus_intr_wiring *map;
    130 	int mapsize;
    131 };
    132 
    133 #define MSIIEP_MODEL_WIRING(name, map) \
    134 	{ name, map, sizeof(map)/sizeof(map[0]) }
    135 
    136 static struct msiiep_known_ebus_wiring known_models[] = {
    137 	MSIIEP_MODEL_WIRING("SUNW,501-4267", krups_ebus_intr_wiring),
    138 	{ NULL, NULL, 0}
    139 };
    140 
    141 
    142 /*
    143  * XXX: This assumes single EBus.  However I don't think any ms-IIep
    144  * system ever used more than one.  In any case, without looking at a
    145  * system with multiple PCIO chips I don't know how to correctly
    146  * program the driver to handle PROM glitches in them, so for the time
    147  * being just use globals.
    148  */
    149 static struct msiiep_ebus_intr_wiring *wiring_map;
    150 static int wiring_map_size;
    151 
    152 static int ebus_init_wiring_table(struct ebus_softc *);
    153 
    154 
    155 int
    156 ebus_match(parent, match, aux)
    157 	struct device *parent;
    158 	struct cfdata *match;
    159 	void *aux;
    160 {
    161 	struct pci_attach_args *pa = aux;
    162 	char name[10];
    163 	int node;
    164 
    165 	/* Only attach if there's a PROM node. */
    166 	node = PCITAG_NODE(pa->pa_tag);
    167 	if (node == -1)
    168 		return (0);
    169 
    170 	PROM_getpropstringA(node, "name", name, sizeof name);
    171 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE
    172 	    && PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN
    173 	    && PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_EBUS
    174 	    && strcmp(name, "ebus") == 0)
    175 		return (1);
    176 
    177 	return (0);
    178 }
    179 
    180 
    181 static int
    182 ebus_init_wiring_table(sc)
    183 	struct ebus_softc *sc;
    184 {
    185 	struct msiiep_known_ebus_wiring *p;
    186 	char buf[32];
    187 	char *model;
    188 
    189 	if (wiring_map != NULL) {
    190 		printf("%s: global ebus wiring map already initalized\n",
    191 		       sc->sc_dev.dv_xname);
    192 		return (0);
    193 	}
    194 
    195 	model = PROM_getpropstringA(prom_findroot(), "model",
    196 				    buf, sizeof(buf));
    197 	if (model == NULL)
    198 		panic("ebus_init_wiring_table: no \"model\" property");
    199 
    200 	for (p = known_models; p->model != NULL; ++p)
    201 		if (strcmp(model, p->model) == 0) {
    202 			wiring_map = p->map;
    203 			wiring_map_size = p->mapsize;
    204 			return (1);
    205 		}
    206 
    207 	/* not found?  we should have failed in pci_attach_hook then. */
    208 	panic("ebus_init_wiring_table: unknown model %s", model);
    209 }
    210 
    211 
    212 /*
    213  * attach an ebus and all it's children.  this code is modeled
    214  * after the sbus code which does similar things.
    215  */
    216 void
    217 ebus_attach(parent, self, aux)
    218 	struct device *parent, *self;
    219 	void *aux;
    220 {
    221 	struct ebus_softc *sc = (struct ebus_softc *)self;
    222 	struct pci_attach_args *pa = aux;
    223 	struct ebus_attach_args ea;
    224 	int node, error;
    225 	char devinfo[256];
    226 
    227 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    228 	printf(": %s, revision 0x%02x\n",
    229 	       devinfo, PCI_REVISION(pa->pa_class));
    230 
    231 	node = PCITAG_NODE(pa->pa_tag);
    232 	if (node == -1)
    233 		panic("%s: unable to find ebus node", self->dv_xname);
    234 
    235 	if (ebus_init_wiring_table(sc) == 0)
    236 		return;
    237 
    238 	sc->sc_node = node;
    239 	sc->sc_parent = parent;	/* XXX: unused so far */
    240 	sc->sc_bustag = pa->pa_memt; /* EBus only does PCI MEM32 space */
    241 	sc->sc_childbustag = ebus_alloc_bus_tag(sc);
    242 	sc->sc_dmatag = ebus_alloc_dma_tag(sc, pa->pa_dmat);
    243 
    244 	/*
    245 	 * Setup ranges.  The interesting thing is that we use "reg"
    246 	 * not "ranges", since "reg" on ebus has exactly the data we'd
    247 	 * get by processing "ranges".
    248 	 *
    249 	 */
    250 	error = PROM_getprop(node, "reg", sizeof(struct ofw_pci_register),
    251 			     &sc->sc_nreg, (void **)&sc->sc_reg);
    252 	if (error)
    253 		panic("%s: unable to read ebus registers (error %d)",
    254 		      self->dv_xname, error);
    255 
    256 	/*
    257 	 * now attach all our children
    258 	 */
    259 	DPRINTF(EDB_CHILD, ("ebus node %08x, searching children...\n", node));
    260 	for (node = firstchild(node); node; node = nextsibling(node)) {
    261 		char *name = PROM_getpropstring(node, "name");
    262 
    263 		if (ebus_setup_attach_args(sc, node, &ea) != 0) {
    264 			printf("ebus_attach: %s: incomplete\n", name);
    265 			continue;
    266 		}
    267 		DPRINTF(EDB_CHILD,
    268 			("- found child `%s', attaching\n", ea.ea_name));
    269 		(void)config_found(self, &ea, ebus_print);
    270 		ebus_destroy_attach_args(&ea);
    271 	}
    272 }
    273 
    274 int
    275 ebus_setup_attach_args(sc, node, ea)
    276 	struct ebus_softc *sc;
    277 	int node;
    278 	struct ebus_attach_args	*ea;
    279 {
    280 	int n, err;
    281 
    282 	memset(ea, 0, sizeof(struct ebus_attach_args));
    283 
    284 	err = PROM_getprop(node, "name", 1, &n, (void **)&ea->ea_name);
    285 	if (err != 0)
    286 		return (err);
    287 	ea->ea_name[n] = '\0';
    288 
    289 	ea->ea_node = node;
    290 	ea->ea_bustag = sc->sc_childbustag;
    291 	ea->ea_dmatag = sc->sc_dmatag;
    292 
    293 	err = PROM_getprop(node, "reg", sizeof(struct ebus_reg),
    294 			   &ea->ea_nreg, (void **)&ea->ea_reg);
    295 	if (err != 0)
    296 		return (err);
    297 
    298 	err = PROM_getprop(node, "address", sizeof(u_int32_t),
    299 			   &ea->ea_nvaddr, (void **)&ea->ea_vaddr);
    300 	if (err != ENOENT) {
    301 		if (err != 0)
    302 			return (err);
    303 
    304 		if (ea->ea_nreg != ea->ea_nvaddr)
    305 			printf("ebus loses: device %s: %d regs and %d addrs\n",
    306 			       ea->ea_name, ea->ea_nreg, ea->ea_nvaddr);
    307 	} else
    308 		ea->ea_nvaddr = 0;
    309 
    310 	/* XXX: "interrupts" hack */
    311 	for (n = 0; n < wiring_map_size; ++n) {
    312 		struct msiiep_ebus_intr_wiring *w = &wiring_map[n];
    313 		if (strcmp(w->name, ea->ea_name) == 0) {
    314 			ea->ea_intr = malloc(sizeof(u_int32_t),
    315 					     M_DEVBUF, M_NOWAIT);
    316 			ea->ea_intr[0] = w->line;
    317 			ea->ea_nintr = 1;
    318 			break;
    319 		}
    320 	}
    321 
    322 	return (0);
    323 }
    324 
    325 void
    326 ebus_destroy_attach_args(ea)
    327 	struct ebus_attach_args	*ea;
    328 {
    329 
    330 	if (ea->ea_name)
    331 		free((void *)ea->ea_name, M_DEVBUF);
    332 	if (ea->ea_reg)
    333 		free((void *)ea->ea_reg, M_DEVBUF);
    334 	if (ea->ea_intr)
    335 		free((void *)ea->ea_intr, M_DEVBUF);
    336 	if (ea->ea_vaddr)
    337 		free((void *)ea->ea_vaddr, M_DEVBUF);
    338 }
    339 
    340 int
    341 ebus_print(aux, p)
    342 	void *aux;
    343 	const char *p;
    344 {
    345 	struct ebus_attach_args *ea = aux;
    346 	int i;
    347 
    348 	if (p)
    349 		printf("%s at %s", ea->ea_name, p);
    350 	for (i = 0; i < ea->ea_nreg; ++i)
    351 		printf("%s bar %d offset 0x%x", i == 0 ? "" : ",",
    352 		       ea->ea_reg[i].bar, ea->ea_reg[i].offset);
    353 	for (i = 0; i < ea->ea_nintr; ++i)
    354 		printf(" line %d", ea->ea_intr[i]);
    355 	return (UNCONF);
    356 }
    357 
    358 
    359 /*
    360  * bus space and bus dma methods below here
    361  */
    362 
    363 bus_space_tag_t
    364 ebus_alloc_bus_tag(sc)
    365 	struct ebus_softc *sc;
    366 {
    367 	bus_space_tag_t bt;
    368 
    369 	bt = (bus_space_tag_t)
    370 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
    371 	if (bt == NULL)
    372 		panic("unable to allocate ebus bus tag");
    373 
    374 	memset(bt, 0, sizeof *bt);
    375 	bt->cookie = sc;
    376 	bt->parent = sc->sc_bustag;
    377 	bt->sparc_bus_map = _ebus_bus_map;
    378 	bt->sparc_bus_mmap = ebus_bus_mmap;
    379 	bt->sparc_intr_establish = ebus_intr_establish;
    380 	return (bt);
    381 }
    382 
    383 
    384 bus_dma_tag_t
    385 ebus_alloc_dma_tag(sc, pdt)
    386 	struct ebus_softc *sc;
    387 	bus_dma_tag_t pdt;
    388 {
    389 	bus_dma_tag_t dt;
    390 
    391 	dt = (bus_dma_tag_t)
    392 		malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
    393 	if (dt == NULL)
    394 		panic("unable to allocate ebus dma tag");
    395 
    396 	memset(dt, 0, sizeof *dt);
    397 	dt->_cookie = sc;
    398 #define PCOPY(x)	dt->x = pdt->x
    399 	PCOPY(_dmamap_create);
    400 	PCOPY(_dmamap_destroy);
    401 	PCOPY(_dmamap_load);
    402 	PCOPY(_dmamap_load_mbuf);
    403 	PCOPY(_dmamap_load_uio);
    404 	PCOPY(_dmamap_load_raw);
    405 	PCOPY(_dmamap_unload);
    406 	PCOPY(_dmamap_sync);
    407 	PCOPY(_dmamem_alloc);
    408 	PCOPY(_dmamem_free);
    409 	PCOPY(_dmamem_map);
    410 	PCOPY(_dmamem_unmap);
    411 	PCOPY(_dmamem_mmap);
    412 #undef	PCOPY
    413 	return (dt);
    414 }
    415 
    416 /*
    417  * bus space support.  <sparc64/dev/psychoreg.h> has a discussion
    418  * about PCI physical addresses, which also applies to ebus.
    419  */
    420 static int
    421 _ebus_bus_map(t, btype, addr, size, flags, vaddr, hp)
    422 	bus_space_tag_t t;
    423 	bus_type_t btype;	/* unused now that bus_addr_t is 64 bit */
    424 	bus_addr_t addr;	/* encodes bar/offset */
    425 	bus_size_t size;
    426 	int	flags;
    427 	vaddr_t vaddr;
    428 	bus_space_handle_t *hp;
    429 {
    430 	struct ebus_softc *sc = t->cookie;
    431 	u_int bar;
    432 	paddr_t offset;
    433 	int i;
    434 
    435 	bar = BUS_ADDR_IOSPACE(addr);
    436 	offset = BUS_ADDR_PADDR(addr);
    437 
    438 	DPRINTF(EDB_BUSMAP,
    439 		("\n_ebus_bus_map: bar %d offset %08x sz %x flags %x va %p\n",
    440 		 (int)bar, (u_int32_t)offset, (u_int32_t)size,
    441 		 flags, (void *)vaddr));
    442 
    443 	/* EBus only has two BARs */
    444 	if (bar != 0 && bar != 1) {
    445 		DPRINTF(EDB_BUSMAP,
    446 			("\n_ebus_bus_map: impossible bar\n"));
    447 		return (EINVAL);
    448 	}
    449 
    450 	/* XXX: krups: change bar number to the offset in config space */
    451 	bar = PCI_MAPREG_START + bar * sizeof(pcireg_t);
    452 
    453 	/*
    454 	 * Almost all of the interesting ebus children are mapped by
    455 	 * BAR1, the last entry in sc_reg[], so work our way backwards.
    456 	 */
    457 	for (i = sc->sc_nreg - 1; i >= 0; --i) {
    458 		bus_addr_t pciaddr;
    459 		u_int32_t ss;
    460 
    461 		/* EBus only does MEM32 */
    462 		ss  = sc->sc_reg[i].phys_hi & OFW_PCI_PHYS_HI_SPACEMASK;
    463 		if (ss != OFW_PCI_PHYS_HI_SPACE_MEM32)
    464 			continue;
    465 
    466 		if (bar != (sc->sc_reg[i].phys_hi
    467 			    & OFW_PCI_PHYS_HI_REGISTERMASK))
    468 			continue;
    469 
    470 		pciaddr = (bus_addr_t)sc->sc_reg[i].phys_lo + offset;
    471 
    472 		if (pciaddr + size > sc->sc_reg[i].phys_lo
    473 					+ sc->sc_reg[i].size_lo)
    474 			continue;
    475 
    476 		DPRINTF(EDB_BUSMAP,
    477 			("_ebus_bus_map: mapping to PCI addr %x\n",
    478 			 (u_int32_t)pciaddr));
    479 
    480 		/* pass it onto the pci controller */
    481 		return (bus_space_map2(sc->sc_bustag, 0, pciaddr, size,
    482 				       flags, vaddr, hp));
    483 	}
    484 
    485 	DPRINTF(EDB_BUSMAP, (": FAILED\n"));
    486 	return (EINVAL);
    487 }
    488 
    489 static paddr_t
    490 ebus_bus_mmap(t, paddr, off, prot, flags)
    491 	bus_space_tag_t t;
    492 	bus_addr_t paddr;
    493 	off_t off;
    494 	int prot;
    495 	int flags;
    496 {
    497 
    498 	/* XXX: not implemetned yet */
    499 	return (-1);
    500 }
    501 
    502 /*
    503  * Install an interrupt handler for a EBus device.
    504  */
    505 void *
    506 ebus_intr_establish(t, pri, level, flags, handler, arg)
    507 	bus_space_tag_t t;
    508 	int pri;
    509 	int level;
    510 	int flags;
    511 	int (*handler)(void *);
    512 	void *arg;
    513 {
    514 	return (bus_intr_establish(t->parent, pri, level, flags, handler, arg));
    515 }
    516