1 /* $OpenBSD: fhc.c,v 1.20 2015/09/19 21:07:04 semarie Exp $ */ 2 3 /* 4 * Copyright (c) 2004 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 29 #include <sys/types.h> 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/kernel.h> 33 #include <sys/device.h> 34 #include <sys/conf.h> 35 #include <sys/timeout.h> 36 #include <sys/malloc.h> 37 38 #include <machine/bus.h> 39 #include <machine/autoconf.h> 40 #include <machine/openfirm.h> 41 42 #include <sparc64/dev/fhcreg.h> 43 #include <sparc64/dev/fhcvar.h> 44 #include <sparc64/dev/iommureg.h> 45 46 struct cfdriver fhc_cd = { 47 NULL, "fhc", DV_DULL 48 }; 49 50 int fhc_print(void *, const char *); 51 52 bus_space_tag_t fhc_alloc_bus_tag(struct fhc_softc *); 53 int _fhc_bus_map(bus_space_tag_t, bus_space_tag_t, bus_addr_t, bus_size_t, 54 int, bus_space_handle_t *); 55 void *fhc_intr_establish(bus_space_tag_t, bus_space_tag_t, int, int, int, 56 int (*)(void *), void *, const char *); 57 bus_space_handle_t *fhc_find_intr_handle(struct fhc_softc *, int); 58 void fhc_led_blink(void *, int); 59 60 void 61 fhc_attach(struct fhc_softc *sc) 62 { 63 int node0, node; 64 u_int32_t ctrl; 65 66 printf(" board %d: %s\n", sc->sc_board, 67 getpropstring(sc->sc_node, "board-model")); 68 69 sc->sc_cbt = fhc_alloc_bus_tag(sc); 70 71 sc->sc_ign = sc->sc_board << 1; 72 bus_space_write_4(sc->sc_bt, sc->sc_ireg, FHC_I_IGN, sc->sc_ign); 73 sc->sc_ign = bus_space_read_4(sc->sc_bt, sc->sc_ireg, FHC_I_IGN); 74 75 ctrl = bus_space_read_4(sc->sc_bt, sc->sc_preg, FHC_P_CTRL); 76 if (!sc->sc_is_central) 77 ctrl |= FHC_P_CTRL_IXIST; 78 ctrl &= ~(FHC_P_CTRL_AOFF | FHC_P_CTRL_BOFF | FHC_P_CTRL_SLINE); 79 bus_space_write_4(sc->sc_bt, sc->sc_preg, FHC_P_CTRL, ctrl); 80 bus_space_read_4(sc->sc_bt, sc->sc_preg, FHC_P_CTRL); 81 82 /* clear interrupts */ 83 bus_space_write_4(sc->sc_bt, sc->sc_freg, FHC_F_ICLR, 0); 84 bus_space_read_4(sc->sc_bt, sc->sc_freg, FHC_F_ICLR); 85 bus_space_write_4(sc->sc_bt, sc->sc_sreg, FHC_S_ICLR, 0); 86 bus_space_read_4(sc->sc_bt, sc->sc_sreg, FHC_S_ICLR); 87 bus_space_write_4(sc->sc_bt, sc->sc_ureg, FHC_U_ICLR, 0); 88 bus_space_read_4(sc->sc_bt, sc->sc_ureg, FHC_U_ICLR); 89 bus_space_write_4(sc->sc_bt, sc->sc_treg, FHC_T_ICLR, 0); 90 bus_space_read_4(sc->sc_bt, sc->sc_treg, FHC_T_ICLR); 91 92 getprop(sc->sc_node, "ranges", sizeof(struct fhc_range), 93 &sc->sc_nrange, (void **)&sc->sc_range); 94 95 node0 = firstchild(sc->sc_node); 96 for (node = node0; node; node = nextsibling(node)) { 97 struct fhc_attach_args fa; 98 99 if (!checkstatus(node)) 100 continue; 101 102 bzero(&fa, sizeof(fa)); 103 104 fa.fa_node = node; 105 fa.fa_bustag = sc->sc_cbt; 106 107 if (fhc_get_string(fa.fa_node, "name", &fa.fa_name)) { 108 printf("can't fetch name for node 0x%x\n", node); 109 continue; 110 } 111 getprop(node, "reg", sizeof(struct fhc_reg), 112 &fa.fa_nreg, (void **)&fa.fa_reg); 113 getprop(node, "interrupts", sizeof(int), 114 &fa.fa_nintr, (void **)&fa.fa_intr); 115 getprop(node, "address", sizeof(*fa.fa_promvaddrs), 116 &fa.fa_npromvaddrs, (void **)&fa.fa_promvaddrs); 117 118 (void)config_found(&sc->sc_dv, (void *)&fa, fhc_print); 119 120 free(fa.fa_name, M_DEVBUF, 0); 121 free(fa.fa_reg, M_DEVBUF, 0); 122 free(fa.fa_intr, M_DEVBUF, 0); 123 free(fa.fa_promvaddrs, M_DEVBUF, 0); 124 } 125 126 sc->sc_blink.bl_func = fhc_led_blink; 127 sc->sc_blink.bl_arg = sc; 128 blink_led_register(&sc->sc_blink); 129 } 130 131 int 132 fhc_print(void *args, const char *busname) 133 { 134 struct fhc_attach_args *fa = args; 135 char *class; 136 137 if (busname != NULL) { 138 printf("\"%s\" at %s", fa->fa_name, busname); 139 class = getpropstring(fa->fa_node, "device_type"); 140 if (*class != '\0') 141 printf(" class %s", class); 142 } 143 return (UNCONF); 144 } 145 146 int 147 fhc_get_string(int node, char *name, char **buf) 148 { 149 int len; 150 151 len = getproplen(node, name); 152 if (len < 0) 153 return (len); 154 *buf = (char *)malloc(len + 1, M_DEVBUF, M_NOWAIT); 155 if (*buf == NULL) 156 return (-1); 157 158 if (len != 0) 159 getpropstringA(node, name, *buf); 160 (*buf)[len] = '\0'; 161 return (0); 162 } 163 164 bus_space_tag_t 165 fhc_alloc_bus_tag(struct fhc_softc *sc) 166 { 167 struct sparc_bus_space_tag *bt; 168 169 bt = malloc(sizeof(*bt), M_DEVBUF, M_NOWAIT | M_ZERO); 170 if (bt == NULL) 171 panic("fhc: couldn't alloc bus tag"); 172 173 strlcpy(bt->name, sc->sc_dv.dv_xname, sizeof(bt->name)); 174 bt->cookie = sc; 175 bt->parent = sc->sc_bt; 176 bt->asi = bt->parent->asi; 177 bt->sasi = bt->parent->sasi; 178 bt->sparc_bus_map = _fhc_bus_map; 179 /* XXX bt->sparc_bus_mmap = fhc_bus_mmap; */ 180 bt->sparc_intr_establish = fhc_intr_establish; 181 return (bt); 182 } 183 184 int 185 _fhc_bus_map(bus_space_tag_t t, bus_space_tag_t t0, bus_addr_t addr, 186 bus_size_t size, int flags, bus_space_handle_t *hp) 187 { 188 struct fhc_softc *sc = t->cookie; 189 int64_t slot = BUS_ADDR_IOSPACE(addr); 190 int64_t offset = BUS_ADDR_PADDR(addr); 191 int i; 192 193 if (t->parent == NULL || t->parent->sparc_bus_map == NULL) { 194 printf("\n_fhc_bus_map: invalid parent"); 195 return (EINVAL); 196 } 197 198 if (flags & BUS_SPACE_MAP_PROMADDRESS) 199 return ((*t->parent->sparc_bus_map)(t, t0, addr, 200 size, flags, hp)); 201 202 for (i = 0; i < sc->sc_nrange; i++) { 203 bus_addr_t paddr; 204 205 if (sc->sc_range[i].cspace != slot) 206 continue; 207 208 paddr = offset - sc->sc_range[i].coffset; 209 paddr += sc->sc_range[i].poffset; 210 paddr |= ((bus_addr_t)sc->sc_range[i].pspace << 32); 211 212 return ((*t->parent->sparc_bus_map)(t->parent, t0, paddr, 213 size, flags, hp)); 214 } 215 216 return (EINVAL); 217 } 218 219 bus_space_handle_t * 220 fhc_find_intr_handle(struct fhc_softc *sc, int ino) 221 { 222 switch (FHC_INO(ino)) { 223 case FHC_F_INO: 224 return &sc->sc_freg; 225 case FHC_S_INO: 226 return &sc->sc_sreg; 227 case FHC_U_INO: 228 return &sc->sc_ureg; 229 case FHC_T_INO: 230 return &sc->sc_treg; 231 default: 232 break; 233 } 234 235 return (NULL); 236 } 237 238 void * 239 fhc_intr_establish(bus_space_tag_t t, bus_space_tag_t t0, int ihandle, 240 int level, int flags, int (*handler)(void *), void *arg, const char *what) 241 { 242 struct fhc_softc *sc = t->cookie; 243 volatile u_int64_t *intrmapptr = NULL, *intrclrptr = NULL; 244 struct intrhand *ih; 245 long vec; 246 247 if (level == IPL_NONE) 248 level = INTLEV(ihandle); 249 if (level == IPL_NONE) { 250 printf(": no IPL, setting IPL 2.\n"); 251 level = 2; 252 } 253 254 if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) == 0) { 255 bus_space_handle_t *hp; 256 struct fhc_intr_reg *intrregs; 257 258 hp = fhc_find_intr_handle(sc, ihandle); 259 if (hp == NULL) { 260 printf(": can't find intr handle\n"); 261 return (NULL); 262 } 263 264 intrregs = bus_space_vaddr(sc->sc_bt, *hp); 265 intrmapptr = &intrregs->imap; 266 intrclrptr = &intrregs->iclr; 267 vec = ((sc->sc_ign << INTMAP_IGN_SHIFT) & INTMAP_IGN) | 268 INTINO(ihandle); 269 } else 270 vec = INTVEC(ihandle); 271 272 ih = bus_intr_allocate(t0, handler, arg, vec, level, intrmapptr, 273 intrclrptr, what); 274 if (ih == NULL) 275 return (NULL); 276 277 intr_establish(ih->ih_pil, ih); 278 279 if (intrmapptr != NULL) { 280 u_int64_t r; 281 282 r = *intrmapptr; 283 r |= INTMAP_V; 284 *intrmapptr = r; 285 r = *intrmapptr; 286 ih->ih_number |= r & INTMAP_INR; 287 } 288 289 return (ih); 290 } 291 292 void 293 fhc_led_blink(void *vsc, int on) 294 { 295 struct fhc_softc *sc = vsc; 296 int s; 297 u_int32_t r; 298 299 s = splhigh(); 300 r = bus_space_read_4(sc->sc_bt, sc->sc_preg, FHC_P_CTRL); 301 if (on) 302 r |= FHC_P_CTRL_RLED; 303 else 304 r &= ~FHC_P_CTRL_RLED; 305 r &= ~(FHC_P_CTRL_AOFF | FHC_P_CTRL_BOFF | FHC_P_CTRL_SLINE); 306 bus_space_write_4(sc->sc_bt, sc->sc_preg, FHC_P_CTRL, r); 307 bus_space_read_4(sc->sc_bt, sc->sc_preg, FHC_P_CTRL); 308 splx(s); 309 } 310