1 /* $NetBSD: auxio.c,v 1.11 2003/07/15 03:36:04 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 2000, 2001 Matthew R. Green 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 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 /* 32 * AUXIO registers support on the sbus & ebus2, used for the floppy driver 33 * and to control the system LED, for the BLINK option. 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: auxio.c,v 1.11 2003/07/15 03:36:04 lukem Exp $"); 38 39 #include "opt_auxio.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/callout.h> 45 #include <sys/errno.h> 46 #include <sys/device.h> 47 #include <sys/malloc.h> 48 49 #include <machine/autoconf.h> 50 #include <machine/cpu.h> 51 52 #include <dev/ebus/ebusreg.h> 53 #include <dev/ebus/ebusvar.h> 54 #include <sparc64/dev/sbusvar.h> 55 #include <sparc64/dev/auxioreg.h> 56 57 /* 58 * on sun4u, auxio exists with one register (LED) on the sbus, and 5 59 * registers on the ebus2 (pci) (LED, PCIMODE, FREQUENCY, SCSI 60 * OSCILLATOR, and TEMP SENSE. 61 */ 62 63 struct auxio_softc { 64 struct device sc_dev; 65 66 /* parent's tag */ 67 bus_space_tag_t sc_tag; 68 69 /* handles to the various auxio regsiter sets */ 70 bus_space_handle_t sc_led; 71 bus_space_handle_t sc_pci; 72 bus_space_handle_t sc_freq; 73 bus_space_handle_t sc_scsi; 74 bus_space_handle_t sc_temp; 75 76 int sc_flags; 77 #define AUXIO_LEDONLY 0x1 78 #define AUXIO_EBUS 0x2 79 #define AUXIO_SBUS 0x4 80 }; 81 82 #define AUXIO_ROM_NAME "auxio" 83 84 void auxio_attach_common(struct auxio_softc *); 85 int auxio_ebus_match(struct device *, struct cfdata *, void *); 86 void auxio_ebus_attach(struct device *, struct device *, void *); 87 int auxio_sbus_match(struct device *, struct cfdata *, void *); 88 void auxio_sbus_attach(struct device *, struct device *, void *); 89 90 CFATTACH_DECL(auxio_ebus, sizeof(struct auxio_softc), 91 auxio_ebus_match, auxio_ebus_attach, NULL, NULL); 92 93 CFATTACH_DECL(auxio_sbus, sizeof(struct auxio_softc), 94 auxio_sbus_match, auxio_sbus_attach, NULL, NULL); 95 96 #ifdef BLINK 97 static struct callout blink_ch = CALLOUT_INITIALIZER; 98 99 static void auxio_blink(void *); 100 101 static void 102 auxio_blink(x) 103 void *x; 104 { 105 struct auxio_softc *sc = x; 106 int s; 107 u_int32_t led; 108 109 s = splhigh(); 110 if (sc->sc_flags & AUXIO_EBUS) 111 led = le32toh(bus_space_read_4(sc->sc_tag, sc->sc_led, 0)); 112 else 113 led = bus_space_read_1(sc->sc_tag, sc->sc_led, 0); 114 if (led & AUXIO_LED_LED) 115 led = 0; 116 else 117 led = AUXIO_LED_LED; 118 if (sc->sc_flags & AUXIO_EBUS) 119 bus_space_write_4(sc->sc_tag, sc->sc_led, 0, htole32(led)); 120 else 121 bus_space_write_1(sc->sc_tag, sc->sc_led, 0, led); 122 splx(s); 123 124 /* 125 * Blink rate is: 126 * full cycle every second if completely idle (loadav = 0) 127 * full cycle every 2 seconds if loadav = 1 128 * full cycle every 3 seconds if loadav = 2 129 * etc. 130 */ 131 s = (((averunnable.ldavg[0] + FSCALE) * hz) >> (FSHIFT + 1)); 132 callout_reset(&blink_ch, s, auxio_blink, sc); 133 } 134 #endif 135 136 void 137 auxio_attach_common(sc) 138 struct auxio_softc *sc; 139 { 140 #ifdef BLINK 141 static int do_once = 1; 142 143 /* only start one blinker */ 144 if (do_once) { 145 auxio_blink(sc); 146 do_once = 0; 147 } 148 #endif 149 printf("\n"); 150 } 151 152 int 153 auxio_ebus_match(parent, cf, aux) 154 struct device *parent; 155 struct cfdata *cf; 156 void *aux; 157 { 158 struct ebus_attach_args *ea = aux; 159 160 return (strcmp(AUXIO_ROM_NAME, ea->ea_name) == 0); 161 } 162 163 void 164 auxio_ebus_attach(parent, self, aux) 165 struct device *parent, *self; 166 void *aux; 167 { 168 struct auxio_softc *sc = (struct auxio_softc *)self; 169 struct ebus_attach_args *ea = aux; 170 171 sc->sc_tag = ea->ea_bustag; 172 173 if (ea->ea_nreg < 1) { 174 printf(": no registers??\n"); 175 return; 176 } 177 178 if (ea->ea_nreg != 5) { 179 printf(": not 5 (%d) registers, only setting led", 180 ea->ea_nreg); 181 sc->sc_flags = AUXIO_LEDONLY|AUXIO_EBUS; 182 } else if (ea->ea_nvaddr == 5) { 183 sc->sc_flags = AUXIO_EBUS; 184 185 sparc_promaddr_to_handle(sc->sc_tag, 186 ea->ea_vaddr[1], &sc->sc_pci); 187 sparc_promaddr_to_handle(sc->sc_tag, 188 ea->ea_vaddr[2], &sc->sc_freq); 189 sparc_promaddr_to_handle(sc->sc_tag, 190 ea->ea_vaddr[3], &sc->sc_scsi); 191 sparc_promaddr_to_handle(sc->sc_tag, 192 ea->ea_vaddr[4], &sc->sc_temp); 193 } else { 194 sc->sc_flags = AUXIO_EBUS; 195 bus_space_map(sc->sc_tag, EBUS_ADDR_FROM_REG(&ea->ea_reg[1]), 196 ea->ea_reg[1].size, 0, &sc->sc_pci); 197 bus_space_map(sc->sc_tag, EBUS_ADDR_FROM_REG(&ea->ea_reg[2]), 198 ea->ea_reg[2].size, 0, &sc->sc_freq); 199 bus_space_map(sc->sc_tag, EBUS_ADDR_FROM_REG(&ea->ea_reg[3]), 200 ea->ea_reg[3].size, 0, &sc->sc_scsi); 201 bus_space_map(sc->sc_tag, EBUS_ADDR_FROM_REG(&ea->ea_reg[4]), 202 ea->ea_reg[4].size, 0, &sc->sc_temp); 203 } 204 205 if (ea->ea_nvaddr > 0) { 206 sparc_promaddr_to_handle(sc->sc_tag, 207 ea->ea_vaddr[0], &sc->sc_led); 208 } else { 209 bus_space_map(sc->sc_tag, EBUS_ADDR_FROM_REG(&ea->ea_reg[0]), 210 ea->ea_reg[0].size, 0, &sc->sc_led); 211 } 212 213 auxio_attach_common(sc); 214 } 215 216 int 217 auxio_sbus_match(parent, cf, aux) 218 struct device *parent; 219 struct cfdata *cf; 220 void *aux; 221 { 222 struct sbus_attach_args *sa = aux; 223 224 return (strcmp(AUXIO_ROM_NAME, sa->sa_name) == 0); 225 } 226 227 void 228 auxio_sbus_attach(parent, self, aux) 229 struct device *parent, *self; 230 void *aux; 231 { 232 struct auxio_softc *sc = (struct auxio_softc *)self; 233 struct sbus_attach_args *sa = aux; 234 235 sc->sc_tag = sa->sa_bustag; 236 237 if (sa->sa_nreg < 1) { 238 printf(": no registers??\n"); 239 return; 240 } 241 242 if (sa->sa_nreg != 1) { 243 printf(": not 1 (%d/%d) registers??", sa->sa_nreg, 244 sa->sa_npromvaddrs); 245 return; 246 } 247 248 /* sbus auxio only has one set of registers */ 249 sc->sc_flags = AUXIO_LEDONLY|AUXIO_SBUS; 250 if (sa->sa_npromvaddrs > 0) { 251 sbus_promaddr_to_handle(sc->sc_tag, 252 sa->sa_promvaddr, &sc->sc_led); 253 } else { 254 sbus_bus_map(sc->sc_tag, sa->sa_slot, sa->sa_offset, 255 sa->sa_size, 0, &sc->sc_led); 256 } 257 258 auxio_attach_common(sc); 259 } 260