central.c revision 1.3
1/* $NetBSD: central.c,v 1.3 2012/03/18 05:26:58 mrg Exp $ */ 2/* $OpenBSD: central.c,v 1.7 2010/11/11 17:58:23 miod Exp $ */ 3 4/* 5 * Copyright (c) 2004 Jason L. Wright (jason@thought.net) 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30#include <sys/cdefs.h> 31__KERNEL_RCSID(0, "$NetBSD: central.c,v 1.3 2012/03/18 05:26:58 mrg Exp $"); 32 33#include <sys/types.h> 34#include <sys/param.h> 35#include <sys/systm.h> 36#include <sys/kernel.h> 37#include <sys/device.h> 38#include <sys/conf.h> 39#include <sys/malloc.h> 40#include <sys/bus.h> 41 42#include <machine/autoconf.h> 43#include <machine/openfirm.h> 44 45#include <sparc64/dev/centralvar.h> 46 47struct central_softc { 48 bus_space_tag_t sc_bt; 49 bus_space_tag_t sc_cbt; 50 int sc_node; 51 int sc_nrange; 52 struct central_range *sc_range; 53}; 54 55static int central_match(device_t, cfdata_t, void *); 56static void central_attach(device_t, device_t, void *); 57 58CFATTACH_DECL_NEW(central, sizeof(struct central_softc), 59 central_match, central_attach, NULL, NULL); 60 61static int central_print(void *, const char *); 62static int central_get_string(int, const char *, char **); 63 64static bus_space_tag_t central_alloc_bus_tag(struct central_softc *); 65static int _central_bus_map( 66 bus_space_tag_t, 67 bus_addr_t, /*offset*/ 68 bus_size_t, /*size*/ 69 int, /*flags*/ 70 vaddr_t, /* XXX unused -- compat w/sparc */ 71 bus_space_handle_t *); 72 73static int 74central_match(device_t parent, cfdata_t match, void *aux) 75{ 76 struct mainbus_attach_args *ma = aux; 77 78 if (strcmp(ma->ma_name, "central") == 0) 79 return (1); 80 return (0); 81} 82 83static void 84central_attach(device_t parent, device_t self, void *aux) 85{ 86 struct central_softc *sc = device_private(self); 87 struct mainbus_attach_args *ma = aux; 88 int node0, node; 89 90 sc->sc_bt = ma->ma_bustag; 91 sc->sc_node = ma->ma_node; 92 sc->sc_cbt = central_alloc_bus_tag(sc); 93 94 prom_getprop(sc->sc_node, "ranges", sizeof(struct central_range), 95 &sc->sc_nrange, (void **)&sc->sc_range); 96 97 printf("\n"); 98 99 node0 = firstchild(sc->sc_node); 100 for (node = node0; node; node = nextsibling(node)) { 101 struct central_attach_args ca; 102 103 bzero(&ca, sizeof(ca)); 104 ca.ca_node = node; 105 ca.ca_bustag = sc->sc_cbt; 106 if (central_get_string(ca.ca_node, "name", &ca.ca_name)) { 107 printf("can't fetch name for node 0x%x\n", node); 108 continue; 109 } 110 111 prom_getprop(node, "reg", sizeof(struct central_reg), 112 &ca.ca_nreg, (void **)&ca.ca_reg); 113 114 (void)config_found(self, (void *)&ca, central_print); 115 116 if (ca.ca_name != NULL) 117 free(ca.ca_name, M_DEVBUF); 118 } 119} 120 121static int 122central_get_string(int node, const char *name, char **buf) 123{ 124 int len; 125 126 len = prom_getproplen(node, name); 127 if (len < 0) 128 return (len); 129 *buf = (char *)malloc(len + 1, M_DEVBUF, M_NOWAIT); 130 if (*buf == NULL) 131 return (-1); 132 133 if (len != 0) 134 prom_getpropstringA(node, name, *buf, len + 1); 135 (*buf)[len] = '\0'; 136 return (0); 137} 138 139static int 140central_print(void *args, const char *busname) 141{ 142 struct central_attach_args *ca = args; 143 char *class; 144 145 if (busname != NULL) { 146 printf("\"%s\" at %s", ca->ca_name, busname); 147 class = prom_getpropstring(ca->ca_node, "device_type"); 148 if (*class != '\0') 149 printf(" class %s", class); 150 } 151 return (UNCONF); 152} 153 154static bus_space_tag_t 155central_alloc_bus_tag(struct central_softc *sc) 156{ 157 struct sparc_bus_space_tag *bt; 158 159 bt = malloc(sizeof(*bt), M_DEVBUF, M_NOWAIT | M_ZERO); 160 if (bt == NULL) 161 panic("central: couldn't alloc bus tag"); 162 163 bt->cookie = sc; 164 bt->parent = sc->sc_bt; 165#if 0 166 bt->asi = bt->parent->asi; 167 bt->sasi = bt->parent->sasi; 168#endif 169 bt->sparc_bus_map = _central_bus_map; 170 /* XXX bt->sparc_bus_mmap = central_bus_mmap; */ 171 /* XXX bt->sparc_intr_establish = upa_intr_establish; */ 172 return (bt); 173} 174 175static int 176_central_bus_map(bus_space_tag_t t, bus_addr_t addr, bus_size_t size, int flags, 177 vaddr_t v, bus_space_handle_t *hp) 178{ 179 struct central_softc *sc = t->cookie; 180 int64_t slot = BUS_ADDR_IOSPACE(addr); 181 int64_t offset = BUS_ADDR_PADDR(addr); 182 int i; 183 184 if (t->parent == NULL || t->parent->sparc_bus_map == NULL) { 185 printf("\ncentral_bus_map: invalid parent"); 186 return (EINVAL); 187 } 188 189 190 for (i = 0; i < sc->sc_nrange; i++) { 191 bus_addr_t paddr; 192 193 if (sc->sc_range[i].cspace != slot) 194 continue; 195 196 paddr = offset - sc->sc_range[i].coffset; 197 paddr += sc->sc_range[i].poffset; 198 paddr |= ((bus_addr_t)sc->sc_range[i].pspace << 32); 199 200 return bus_space_map(t->parent, paddr, size, flags, hp); 201 } 202 203 return (EINVAL); 204} 205