1 1.27 thorpej /* $NetBSD: mace.c,v 1.27 2023/12/20 15:29:07 thorpej Exp $ */ 2 1.1 sekiya 3 1.1 sekiya /* 4 1.1 sekiya * Copyright (c) 2003 Christopher Sekiya 5 1.1 sekiya * Copyright (c) 2002,2003 Rafal K. Boni 6 1.1 sekiya * Copyright (c) 2000 Soren S. Jorvang 7 1.1 sekiya * All rights reserved. 8 1.1 sekiya * 9 1.1 sekiya * Redistribution and use in source and binary forms, with or without 10 1.1 sekiya * modification, are permitted provided that the following conditions 11 1.1 sekiya * are met: 12 1.1 sekiya * 1. Redistributions of source code must retain the above copyright 13 1.1 sekiya * notice, this list of conditions and the following disclaimer. 14 1.1 sekiya * 2. Redistributions in binary form must reproduce the above copyright 15 1.1 sekiya * notice, this list of conditions and the following disclaimer in the 16 1.1 sekiya * documentation and/or other materials provided with the distribution. 17 1.1 sekiya * 3. All advertising materials mentioning features or use of this software 18 1.1 sekiya * must display the following acknowledgement: 19 1.1 sekiya * This product includes software developed for the 20 1.1 sekiya * NetBSD Project. See http://www.NetBSD.org/ for 21 1.1 sekiya * information about NetBSD. 22 1.1 sekiya * 4. The name of the author may not be used to endorse or promote products 23 1.1 sekiya * derived from this software without specific prior written permission. 24 1.1 sekiya * 25 1.1 sekiya * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 26 1.1 sekiya * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 27 1.1 sekiya * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 28 1.1 sekiya * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 29 1.1 sekiya * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 30 1.1 sekiya * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 31 1.1 sekiya * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 32 1.1 sekiya * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 1.1 sekiya * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34 1.1 sekiya * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 1.1 sekiya */ 36 1.1 sekiya 37 1.1 sekiya /* 38 1.1 sekiya * O2 MACE 39 1.1 sekiya * 40 1.1 sekiya * The MACE is weird -- although it is a 32-bit device, writes only seem to 41 1.1 sekiya * work properly if they are 64-bit-at-once writes (at least, out in ISA 42 1.1 sekiya * space and probably MEC space -- the PCI stuff seems to be okay with _4). 43 1.1 sekiya * Therefore, the _8* routines are used even though the top 32 bits are 44 1.1 sekiya * thrown away. 45 1.1 sekiya */ 46 1.1 sekiya 47 1.1 sekiya #include <sys/cdefs.h> 48 1.27 thorpej __KERNEL_RCSID(0, "$NetBSD: mace.c,v 1.27 2023/12/20 15:29:07 thorpej Exp $"); 49 1.1 sekiya 50 1.1 sekiya #include <sys/param.h> 51 1.1 sekiya #include <sys/systm.h> 52 1.1 sekiya #include <sys/device.h> 53 1.1 sekiya #include <sys/callout.h> 54 1.1 sekiya #include <sys/mbuf.h> 55 1.1 sekiya #include <sys/kernel.h> 56 1.1 sekiya #include <sys/socket.h> 57 1.1 sekiya #include <sys/ioctl.h> 58 1.1 sekiya #include <sys/errno.h> 59 1.1 sekiya #include <sys/syslog.h> 60 1.1 sekiya 61 1.1 sekiya #include <uvm/uvm_extern.h> 62 1.1 sekiya 63 1.17 dyoung #include <sys/bus.h> 64 1.1 sekiya #include <machine/cpu.h> 65 1.1 sekiya #include <machine/locore.h> 66 1.1 sekiya #include <machine/autoconf.h> 67 1.1 sekiya #include <machine/machtype.h> 68 1.1 sekiya 69 1.1 sekiya #include <sgimips/mace/macevar.h> 70 1.1 sekiya #include <sgimips/mace/macereg.h> 71 1.1 sekiya #include <sgimips/dev/crimevar.h> 72 1.1 sekiya #include <sgimips/dev/crimereg.h> 73 1.1 sekiya 74 1.1 sekiya #include "locators.h" 75 1.1 sekiya 76 1.1 sekiya #define MACE_NINTR 32 /* actually only 8, but interrupts are shared */ 77 1.1 sekiya 78 1.1 sekiya struct { 79 1.1 sekiya unsigned int irq; 80 1.1 sekiya unsigned int intrmask; 81 1.1 sekiya int (*func)(void *); 82 1.1 sekiya void *arg; 83 1.4 tsutsui struct evcnt evcnt; 84 1.4 tsutsui char evname[32]; 85 1.1 sekiya } maceintrtab[MACE_NINTR]; 86 1.1 sekiya 87 1.1 sekiya struct mace_softc { 88 1.18 macallan device_t sc_dev; 89 1.1 sekiya 90 1.1 sekiya bus_space_tag_t iot; 91 1.1 sekiya bus_space_handle_t ioh; 92 1.1 sekiya bus_dma_tag_t dmat; /* 32KB ring buffers, 4KB segments, for ISA */ 93 1.1 sekiya int nsegs; 94 1.1 sekiya bus_dma_segment_t seg; 95 1.1 sekiya bus_dmamap_t map; 96 1.1 sekiya 97 1.1 sekiya void *isa_ringbuffer; 98 1.1 sekiya }; 99 1.1 sekiya 100 1.19 chs static int mace_match(device_t, cfdata_t, void *); 101 1.19 chs static void mace_attach(device_t, device_t, void *); 102 1.1 sekiya static int mace_print(void *, const char *); 103 1.19 chs static int mace_search(device_t, cfdata_t, const int *, void *); 104 1.1 sekiya 105 1.18 macallan CFATTACH_DECL_NEW(mace, sizeof(struct mace_softc), 106 1.1 sekiya mace_match, mace_attach, NULL, NULL); 107 1.1 sekiya 108 1.21 macallan static void mace_isa_bus_mem_init(bus_space_tag_t, void *); 109 1.21 macallan 110 1.21 macallan static struct mips_bus_space mace_isa_mbst; 111 1.21 macallan bus_space_tag_t mace_isa_memt = NULL; 112 1.21 macallan static int mace_isa_init = 0; 113 1.21 macallan 114 1.1 sekiya #if defined(BLINK) 115 1.12 ad static callout_t mace_blink_ch; 116 1.1 sekiya static void mace_blink(void *); 117 1.1 sekiya #endif 118 1.1 sekiya 119 1.1 sekiya static int 120 1.18 macallan mace_match(device_t parent, struct cfdata *match, void *aux) 121 1.1 sekiya { 122 1.1 sekiya 123 1.1 sekiya /* 124 1.1 sekiya * The MACE is in the O2. 125 1.1 sekiya */ 126 1.1 sekiya if (mach_type == MACH_SGI_IP32) 127 1.15 tsutsui return 1; 128 1.1 sekiya 129 1.15 tsutsui return 0; 130 1.1 sekiya } 131 1.1 sekiya 132 1.21 macallan void 133 1.21 macallan mace_init_bus(void) 134 1.21 macallan { 135 1.21 macallan if (mace_isa_init == 1) 136 1.21 macallan return; 137 1.21 macallan mace_isa_init = 1; 138 1.21 macallan mace_isa_bus_mem_init(&mace_isa_mbst, NULL); 139 1.21 macallan mace_isa_memt = &mace_isa_mbst; 140 1.21 macallan } 141 1.21 macallan 142 1.1 sekiya static void 143 1.18 macallan mace_attach(device_t parent, device_t self, void *aux) 144 1.1 sekiya { 145 1.18 macallan struct mace_softc *sc = device_private(self); 146 1.1 sekiya struct mainbus_attach_args *ma = aux; 147 1.15 tsutsui uint32_t scratch; 148 1.1 sekiya 149 1.18 macallan sc->sc_dev = self; 150 1.12 ad #ifdef BLINK 151 1.12 ad callout_init(&mace_blink_ch, 0); 152 1.12 ad #endif 153 1.12 ad 154 1.21 macallan sc->iot = normal_memt; /* for mace registers */ 155 1.1 sekiya sc->dmat = &sgimips_default_bus_dma_tag; 156 1.1 sekiya 157 1.1 sekiya if (bus_space_map(sc->iot, ma->ma_addr, 0, 158 1.1 sekiya BUS_SPACE_MAP_LINEAR, &sc->ioh)) 159 1.1 sekiya panic("mace_attach: could not allocate memory\n"); 160 1.1 sekiya 161 1.1 sekiya aprint_normal("\n"); 162 1.1 sekiya 163 1.19 chs aprint_debug("%s: isa sts %#"PRIx64"\n", device_xname(self), 164 1.1 sekiya bus_space_read_8(sc->iot, sc->ioh, MACE_ISA_INT_STATUS)); 165 1.19 chs aprint_debug("%s: isa msk %#"PRIx64"\n", device_xname(self), 166 1.1 sekiya bus_space_read_8(sc->iot, sc->ioh, MACE_ISA_INT_MASK)); 167 1.1 sekiya 168 1.21 macallan mace_init_bus(); 169 1.21 macallan 170 1.1 sekiya /* 171 1.10 jmcneill * Turn on most ISA interrupts. These are actually masked and 172 1.1 sekiya * registered via the CRIME, as the MACE ISA interrupt mask is 173 1.1 sekiya * really whacky and nigh on impossible to map to a sane autoconfig 174 1.11 jmcneill * scheme. We do, however, turn off the count/compare timer and RTC 175 1.10 jmcneill * interrupts as they are unused and conflict with the PS/2 176 1.10 jmcneill * keyboard and mouse interrupts. 177 1.1 sekiya */ 178 1.1 sekiya 179 1.11 jmcneill bus_space_write_8(sc->iot, sc->ioh, MACE_ISA_INT_MASK, 0xffff0aff); 180 1.1 sekiya bus_space_write_8(sc->iot, sc->ioh, MACE_ISA_INT_STATUS, 0); 181 1.1 sekiya 182 1.1 sekiya /* set up LED for solid green or blink, if that's your fancy */ 183 1.1 sekiya scratch = bus_space_read_8(sc->iot, sc->ioh, MACE_ISA_FLASH_NIC_REG); 184 1.1 sekiya scratch |= MACE_ISA_LED_RED; 185 1.1 sekiya scratch &= ~(MACE_ISA_LED_GREEN); 186 1.1 sekiya bus_space_write_8(sc->iot, sc->ioh, MACE_ISA_FLASH_NIC_REG, scratch); 187 1.1 sekiya 188 1.1 sekiya #if defined(BLINK) 189 1.1 sekiya mace_blink(sc); 190 1.1 sekiya #endif 191 1.1 sekiya 192 1.1 sekiya /* Initialize the maceintr elements to sane values */ 193 1.1 sekiya for (scratch = 0; scratch < MACE_NINTR; scratch++) { 194 1.1 sekiya maceintrtab[scratch].func = NULL; 195 1.1 sekiya maceintrtab[scratch].irq = 0; 196 1.1 sekiya } 197 1.1 sekiya 198 1.24 thorpej config_search(self, NULL, 199 1.25 thorpej CFARGS(.search = mace_search)); 200 1.1 sekiya } 201 1.1 sekiya 202 1.1 sekiya 203 1.1 sekiya static int 204 1.1 sekiya mace_print(void *aux, const char *pnp) 205 1.1 sekiya { 206 1.1 sekiya struct mace_attach_args *maa = aux; 207 1.1 sekiya 208 1.1 sekiya if (pnp != 0) 209 1.1 sekiya return QUIET; 210 1.1 sekiya 211 1.1 sekiya if (maa->maa_offset != MACECF_OFFSET_DEFAULT) 212 1.1 sekiya aprint_normal(" offset 0x%lx", maa->maa_offset); 213 1.1 sekiya if (maa->maa_intr != MACECF_INTR_DEFAULT) 214 1.1 sekiya aprint_normal(" intr %d", maa->maa_intr); 215 1.1 sekiya if (maa->maa_offset != MACECF_INTRMASK_DEFAULT) 216 1.1 sekiya aprint_normal(" intrmask 0x%x", maa->maa_intrmask); 217 1.1 sekiya 218 1.1 sekiya return UNCONF; 219 1.1 sekiya } 220 1.1 sekiya 221 1.1 sekiya static int 222 1.19 chs mace_search(device_t parent, struct cfdata *cf, const int *ldesc, void *aux) 223 1.1 sekiya { 224 1.18 macallan struct mace_softc *sc = device_private(parent); 225 1.1 sekiya struct mace_attach_args maa; 226 1.1 sekiya int tryagain; 227 1.1 sekiya 228 1.1 sekiya do { 229 1.1 sekiya maa.maa_offset = cf->cf_loc[MACECF_OFFSET]; 230 1.1 sekiya maa.maa_intr = cf->cf_loc[MACECF_INTR]; 231 1.1 sekiya maa.maa_intrmask = cf->cf_loc[MACECF_INTRMASK]; 232 1.21 macallan maa.maa_st = normal_memt; 233 1.1 sekiya maa.maa_sh = sc->ioh; /* XXX */ 234 1.1 sekiya maa.maa_dmat = &sgimips_default_bus_dma_tag; 235 1.1 sekiya maa.isa_ringbuffer = sc->isa_ringbuffer; 236 1.1 sekiya 237 1.1 sekiya tryagain = 0; 238 1.24 thorpej if (config_probe(parent, cf, &maa)) { 239 1.25 thorpej config_attach(parent, cf, &maa, mace_print, CFARGS_NONE); 240 1.1 sekiya tryagain = (cf->cf_fstate == FSTATE_STAR); 241 1.1 sekiya } 242 1.1 sekiya 243 1.1 sekiya } while (tryagain); 244 1.1 sekiya 245 1.1 sekiya return 0; 246 1.1 sekiya } 247 1.1 sekiya 248 1.1 sekiya void * 249 1.1 sekiya mace_intr_establish(int intr, int level, int (*func)(void *), void *arg) 250 1.1 sekiya { 251 1.1 sekiya int i; 252 1.1 sekiya 253 1.5 sekiya if (intr < 0 || intr >= 16) 254 1.1 sekiya panic("invalid interrupt number"); 255 1.1 sekiya 256 1.1 sekiya for (i = 0; i < MACE_NINTR; i++) 257 1.2 tsutsui if (maceintrtab[i].func == NULL) { 258 1.2 tsutsui maceintrtab[i].func = func; 259 1.2 tsutsui maceintrtab[i].arg = arg; 260 1.2 tsutsui maceintrtab[i].irq = (1 << intr); 261 1.2 tsutsui maceintrtab[i].intrmask = level; 262 1.4 tsutsui snprintf(maceintrtab[i].evname, 263 1.4 tsutsui sizeof(maceintrtab[i].evname), 264 1.22 macallan "intr %d lv 0x%x", intr, level); 265 1.4 tsutsui evcnt_attach_dynamic(&maceintrtab[i].evcnt, 266 1.4 tsutsui EVCNT_TYPE_INTR, NULL, 267 1.4 tsutsui "mace", maceintrtab[i].evname); 268 1.1 sekiya break; 269 1.1 sekiya } 270 1.1 sekiya 271 1.1 sekiya crime_intr_mask(intr); 272 1.8 tsutsui aprint_debug("mace: established interrupt %d (level %x)\n", 273 1.1 sekiya intr, level); 274 1.2 tsutsui return (void *)&maceintrtab[i]; 275 1.1 sekiya } 276 1.1 sekiya 277 1.1 sekiya void 278 1.5 sekiya mace_intr_disestablish(void *cookie) 279 1.5 sekiya { 280 1.5 sekiya int intr = -1, level = 0, irq = 0, i; 281 1.5 sekiya 282 1.5 sekiya for (i = 0; i < MACE_NINTR; i++) 283 1.5 sekiya if (&maceintrtab[i] == cookie) { 284 1.5 sekiya evcnt_detach(&maceintrtab[i].evcnt); 285 1.5 sekiya for (intr = 0; 286 1.15 tsutsui maceintrtab[i].irq == (1 << intr); intr++); 287 1.5 sekiya level = maceintrtab[i].intrmask; 288 1.5 sekiya irq = maceintrtab[i].irq; 289 1.5 sekiya 290 1.5 sekiya maceintrtab[i].irq = 0; 291 1.5 sekiya maceintrtab[i].intrmask = 0; 292 1.5 sekiya maceintrtab[i].func = NULL; 293 1.5 sekiya maceintrtab[i].arg = NULL; 294 1.15 tsutsui memset(&maceintrtab[i].evcnt, 0, sizeof (struct evcnt)); 295 1.15 tsutsui memset(&maceintrtab[i].evname, 0, 296 1.5 sekiya sizeof (maceintrtab[i].evname)); 297 1.5 sekiya break; 298 1.5 sekiya } 299 1.5 sekiya if (intr == -1) 300 1.5 sekiya panic("mace: lost maceintrtab"); 301 1.5 sekiya 302 1.14 sekiya /* do not do an unmask when irq is shared. */ 303 1.5 sekiya for (i = 0; i < MACE_NINTR; i++) 304 1.26 mrg if (maceintrtab[i].func != NULL && maceintrtab[i].irq == irq) 305 1.5 sekiya break; 306 1.5 sekiya if (i == MACE_NINTR) 307 1.5 sekiya crime_intr_unmask(intr); 308 1.8 tsutsui aprint_debug("mace: disestablished interrupt %d (level %x)\n", 309 1.5 sekiya intr, level); 310 1.5 sekiya } 311 1.5 sekiya 312 1.5 sekiya void 313 1.1 sekiya mace_intr(int irqs) 314 1.1 sekiya { 315 1.20 mrg uint64_t isa_irq; 316 1.1 sekiya int i; 317 1.1 sekiya 318 1.1 sekiya /* irq 4 is the ISA cascade interrupt. Must handle with care. */ 319 1.1 sekiya if (irqs & (1 << 4)) { 320 1.23 macallan isa_irq = mips3_ld(MIPS_PHYS_TO_KSEG1(MACE_BASE 321 1.1 sekiya + MACE_ISA_INT_STATUS)); 322 1.1 sekiya for (i = 0; i < MACE_NINTR; i++) { 323 1.1 sekiya if ((maceintrtab[i].irq == (1 << 4)) && 324 1.1 sekiya (isa_irq & maceintrtab[i].intrmask)) { 325 1.1 sekiya (maceintrtab[i].func)(maceintrtab[i].arg); 326 1.4 tsutsui maceintrtab[i].evcnt.ev_count++; 327 1.1 sekiya } 328 1.1 sekiya } 329 1.1 sekiya irqs &= ~(1 << 4); 330 1.1 sekiya } 331 1.1 sekiya 332 1.1 sekiya for (i = 0; i < MACE_NINTR; i++) 333 1.4 tsutsui if ((irqs & maceintrtab[i].irq)) { 334 1.1 sekiya (maceintrtab[i].func)(maceintrtab[i].arg); 335 1.4 tsutsui maceintrtab[i].evcnt.ev_count++; 336 1.4 tsutsui } 337 1.1 sekiya } 338 1.1 sekiya 339 1.1 sekiya #if defined(BLINK) 340 1.1 sekiya static void 341 1.1 sekiya mace_blink(void *self) 342 1.1 sekiya { 343 1.19 chs struct mace_softc *sc = device_private(self); 344 1.1 sekiya register int s; 345 1.1 sekiya int value; 346 1.1 sekiya 347 1.1 sekiya s = splhigh(); 348 1.1 sekiya value = bus_space_read_8(sc->iot, sc->ioh, MACE_ISA_FLASH_NIC_REG); 349 1.1 sekiya value ^= MACE_ISA_LED_GREEN; 350 1.1 sekiya bus_space_write_8(sc->iot, sc->ioh, MACE_ISA_FLASH_NIC_REG, value); 351 1.1 sekiya splx(s); 352 1.1 sekiya /* 353 1.1 sekiya * Blink rate is: 354 1.1 sekiya * full cycle every second if completely idle (loadav = 0) 355 1.1 sekiya * full cycle every 2 seconds if loadav = 1 356 1.1 sekiya * full cycle every 3 seconds if loadav = 2 357 1.1 sekiya * etc. 358 1.1 sekiya */ 359 1.1 sekiya s = (((averunnable.ldavg[0] + FSCALE) * hz) >> (FSHIFT + 1)); 360 1.1 sekiya callout_reset(&mace_blink_ch, s, mace_blink, sc); 361 1.1 sekiya 362 1.1 sekiya } 363 1.1 sekiya #endif 364 1.21 macallan 365 1.21 macallan #define CHIP mace_isa 366 1.21 macallan #define CHIP_MEM /* defined */ 367 1.21 macallan #define CHIP_ALIGN_STRIDE 8 368 1.21 macallan #define CHIP_ACCESS_SIZE 8 369 1.21 macallan #define CHIP_W1_BUS_START(v) 0x00000000UL 370 1.21 macallan #define CHIP_W1_BUS_END(v) 0xffffffffUL 371 1.21 macallan #define CHIP_W1_SYS_START(v) 0x00000000UL 372 1.21 macallan #define CHIP_W1_SYS_END(v) 0xffffffffUL 373 1.21 macallan 374 1.21 macallan #include <mips/mips/bus_space_alignstride_chipdep.c> 375