1 /* $OpenBSD: upa.c,v 1.8 2008/01/17 22:53:18 kettenis Exp $ */ 2 3 /* 4 * Copyright (c) 2002 Jason L. Wright (jason@thought.net) 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Effort sponsored in part by the Defense Advanced Research Projects 29 * Agency (DARPA) and Air Force Research Laboratory, Air Force 30 * Materiel Command, USAF, under agreement number F30602-01-2-0537. 31 * 32 */ 33 34 #include <sys/types.h> 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/device.h> 39 #include <sys/conf.h> 40 #include <sys/malloc.h> 41 42 #include <sys/bus.h> 43 #include <machine/autoconf.h> 44 #include <machine/openfirm.h> 45 46 struct upa_range { 47 u_int64_t ur_space; 48 u_int64_t ur_addr; 49 u_int64_t ur_len; 50 }; 51 52 struct upa_softc { 53 device_t sc_dev; 54 bus_space_tag_t sc_bt; 55 bus_space_handle_t sc_reg[3]; 56 struct upa_range *sc_range; 57 int sc_node; 58 int sc_nrange; 59 bus_space_tag_t sc_cbt; 60 }; 61 62 int upa_match(device_t, cfdata_t, void *); 63 void upa_attach(device_t, device_t, void *); 64 65 CFATTACH_DECL_NEW(upa, sizeof(struct upa_softc), 66 upa_match, upa_attach, NULL, NULL); 67 68 int upa_print(void *, const char *); 69 bus_space_tag_t upa_alloc_bus_tag(struct upa_softc *); 70 int upa_bus_map(bus_space_tag_t, bus_addr_t, 71 bus_size_t, int, vaddr_t, bus_space_handle_t *); 72 paddr_t upa_bus_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int); 73 74 int 75 upa_match(device_t parent, cfdata_t match, void *aux) 76 { 77 struct mainbus_attach_args *ma = aux; 78 79 if (strcmp(ma->ma_name, "upa") == 0) 80 return (1); 81 82 return (0); 83 } 84 85 void 86 upa_attach(device_t parent, device_t self, void *aux) 87 { 88 struct upa_softc *sc = device_private(self); 89 struct mainbus_attach_args *ma = aux; 90 int i, node; 91 92 sc->sc_dev = self; 93 sc->sc_bt = ma->ma_bustag; 94 sc->sc_node = ma->ma_node; 95 96 for (i = 0; i < 3; i++) { 97 if (i >= ma->ma_nreg) { 98 printf(": no register %d\n", i); 99 return; 100 } 101 if (bus_space_map(sc->sc_bt, ma->ma_reg[i].ur_paddr, 102 ma->ma_reg[i].ur_len, 0, &sc->sc_reg[i])) { 103 printf(": failed to map reg%d\n", i); 104 return; 105 } 106 } 107 108 if (prom_getprop(sc->sc_node, "ranges", sizeof(struct upa_range), 109 &sc->sc_nrange, (void **)&sc->sc_range)) 110 panic("upa: can't get ranges"); 111 112 printf("\n"); 113 114 sc->sc_cbt = upa_alloc_bus_tag(sc); 115 116 for (node = OF_child(sc->sc_node); node; node = OF_peer(node)) { 117 char buf[32]; 118 struct mainbus_attach_args map; 119 120 bzero(&map, sizeof(map)); 121 if (OF_getprop(node, "device_type", buf, sizeof(buf)) <= 0) 122 continue; 123 if (prom_getprop(node, "reg", sizeof(*map.ma_reg), 124 &map.ma_nreg, (void **)&map.ma_reg) != 0) 125 continue; 126 if (OF_getprop(node, "name", buf, sizeof(buf)) <= 0) 127 continue; 128 map.ma_node = node; 129 map.ma_name = buf; 130 map.ma_bustag = sc->sc_cbt; 131 map.ma_dmatag = ma->ma_dmatag; 132 config_found(sc->sc_dev, &map, upa_print); 133 } 134 } 135 136 int 137 upa_print(void *args, const char *name) 138 { 139 struct mainbus_attach_args *ma = args; 140 141 if (name) 142 printf("\"%s\" at %s", ma->ma_name, name); 143 return (UNCONF); 144 } 145 146 bus_space_tag_t 147 upa_alloc_bus_tag(struct upa_softc *sc) 148 { 149 struct sparc_bus_space_tag *bt; 150 151 bt = malloc(sizeof(*bt), M_DEVBUF, M_NOWAIT | M_ZERO); 152 if (bt == NULL) 153 panic("upa: couldn't alloc bus tag"); 154 155 *bt = *sc->sc_bt; 156 bt->cookie = sc; 157 bt->parent = sc->sc_bt; 158 bt->sparc_bus_map = upa_bus_map; 159 bt->sparc_bus_mmap = upa_bus_mmap; 160 161 return bt; 162 } 163 164 int 165 upa_bus_map(bus_space_tag_t t, bus_addr_t offset, 166 bus_size_t size, int flags, vaddr_t v, bus_space_handle_t *hp) 167 { 168 struct upa_softc *sc = t->cookie; 169 int i; 170 171 if (t->parent == 0 || t->parent->sparc_bus_map == 0) { 172 printf("\n__upa_bus_map: invalid parent"); 173 return (EINVAL); 174 } 175 176 t = t->parent; 177 178 for (i = 0; i < sc->sc_nrange; i++) { 179 if (offset < sc->sc_range[i].ur_space) 180 continue; 181 if (offset >= (sc->sc_range[i].ur_space + 182 sc->sc_range[i].ur_space)) 183 continue; 184 break; 185 } 186 if (i == sc->sc_nrange) 187 return (EINVAL); 188 189 offset -= sc->sc_range[i].ur_space; 190 offset += sc->sc_range[i].ur_addr; 191 192 return ((*t->sparc_bus_map)(t, offset, size, flags, v, hp)); 193 } 194 195 paddr_t 196 upa_bus_mmap(bus_space_tag_t t, bus_addr_t paddr, off_t off, int prot, 197 int flags) 198 { 199 struct upa_softc *sc = t->cookie; 200 int i; 201 202 if (t->parent == 0 || t->parent->sparc_bus_map == 0) { 203 printf("\n__upa_bus_map: invalid parent"); 204 return (EINVAL); 205 } 206 207 t = t->parent; 208 209 for (i = 0; i < sc->sc_nrange; i++) { 210 if (paddr + off < sc->sc_range[i].ur_space) 211 continue; 212 if (paddr + off >= (sc->sc_range[i].ur_space + 213 sc->sc_range[i].ur_space)) 214 continue; 215 break; 216 } 217 if (i == sc->sc_nrange) 218 return (EINVAL); 219 220 paddr -= sc->sc_range[i].ur_space; 221 paddr += sc->sc_range[i].ur_addr; 222 223 return ((*t->sparc_bus_mmap)(t, paddr, off, prot, flags)); 224 } 225