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 <machine/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 struct device 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(struct device*, struct cfdata*, void *); 63 void upa_attach(struct device*, struct device*, void *); 64 65 CFATTACH_DECL(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(struct device *parent, struct cfdata *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(struct device *parent, struct device *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_bt = ma->ma_bustag; 93 sc->sc_node = ma->ma_node; 94 95 for (i = 0; i < 3; i++) { 96 if (i >= ma->ma_nreg) { 97 printf(": no register %d\n", i); 98 return; 99 } 100 if (bus_space_map(sc->sc_bt, ma->ma_reg[i].ur_paddr, 101 ma->ma_reg[i].ur_len, 0, &sc->sc_reg[i])) { 102 printf(": failed to map reg%d\n", i); 103 return; 104 } 105 } 106 107 if (prom_getprop(sc->sc_node, "ranges", sizeof(struct upa_range), 108 &sc->sc_nrange, (void **)&sc->sc_range)) 109 panic("upa: can't get ranges"); 110 111 printf("\n"); 112 113 sc->sc_cbt = upa_alloc_bus_tag(sc); 114 115 for (node = OF_child(sc->sc_node); node; node = OF_peer(node)) { 116 char buf[32]; 117 struct mainbus_attach_args map; 118 119 bzero(&map, sizeof(map)); 120 if (OF_getprop(node, "device_type", buf, sizeof(buf)) <= 0) 121 continue; 122 if (prom_getprop(node, "reg", sizeof(*map.ma_reg), 123 &map.ma_nreg, (void **)&map.ma_reg) != 0) 124 continue; 125 if (OF_getprop(node, "name", buf, sizeof(buf)) <= 0) 126 continue; 127 map.ma_node = node; 128 map.ma_name = buf; 129 map.ma_bustag = sc->sc_cbt; 130 map.ma_dmatag = ma->ma_dmatag; 131 config_found(&sc->sc_dev, &map, upa_print); 132 } 133 } 134 135 int 136 upa_print(void *args, const char *name) 137 { 138 struct mainbus_attach_args *ma = args; 139 140 if (name) 141 printf("\"%s\" at %s", ma->ma_name, name); 142 return (UNCONF); 143 } 144 145 bus_space_tag_t 146 upa_alloc_bus_tag(struct upa_softc *sc) 147 { 148 struct sparc_bus_space_tag *bt; 149 150 bt = malloc(sizeof(*bt), M_DEVBUF, M_NOWAIT | M_ZERO); 151 if (bt == NULL) 152 panic("upa: couldn't alloc bus tag"); 153 154 *bt = *sc->sc_bt; 155 bt->cookie = sc; 156 bt->parent = sc->sc_bt; 157 bt->sparc_bus_map = upa_bus_map; 158 bt->sparc_bus_mmap = upa_bus_mmap; 159 160 return bt; 161 } 162 163 int 164 upa_bus_map(bus_space_tag_t t, bus_addr_t offset, 165 bus_size_t size, int flags, vaddr_t v, bus_space_handle_t *hp) 166 { 167 struct upa_softc *sc = t->cookie; 168 int i; 169 170 if (t->parent == 0 || t->parent->sparc_bus_map == 0) { 171 printf("\n__upa_bus_map: invalid parent"); 172 return (EINVAL); 173 } 174 175 t = t->parent; 176 177 for (i = 0; i < sc->sc_nrange; i++) { 178 if (offset < sc->sc_range[i].ur_space) 179 continue; 180 if (offset >= (sc->sc_range[i].ur_space + 181 sc->sc_range[i].ur_space)) 182 continue; 183 break; 184 } 185 if (i == sc->sc_nrange) 186 return (EINVAL); 187 188 offset -= sc->sc_range[i].ur_space; 189 offset += sc->sc_range[i].ur_addr; 190 191 return ((*t->sparc_bus_map)(t, offset, size, flags, v, hp)); 192 } 193 194 paddr_t 195 upa_bus_mmap(bus_space_tag_t t, bus_addr_t paddr, off_t off, int prot, 196 int flags) 197 { 198 struct upa_softc *sc = t->cookie; 199 int i; 200 201 if (t->parent == 0 || t->parent->sparc_bus_map == 0) { 202 printf("\n__upa_bus_map: invalid parent"); 203 return (EINVAL); 204 } 205 206 t = t->parent; 207 208 for (i = 0; i < sc->sc_nrange; i++) { 209 if (paddr + off < sc->sc_range[i].ur_space) 210 continue; 211 if (paddr + off >= (sc->sc_range[i].ur_space + 212 sc->sc_range[i].ur_space)) 213 continue; 214 break; 215 } 216 if (i == sc->sc_nrange) 217 return (EINVAL); 218 219 paddr -= sc->sc_range[i].ur_space; 220 paddr += sc->sc_range[i].ur_addr; 221 222 return ((*t->sparc_bus_mmap)(t, paddr, off, prot, flags)); 223 } 224