1 /* $NetBSD: auxio.c,v 1.15 2006/02/13 21:47:11 cdi 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.15 2006/02/13 21:47:11 cdi 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 /* let someone disable it if it's already turned on; XXX sysctl? */ 102 int do_blink = 1; 103 104 static void 105 auxio_blink(void *x) 106 { 107 struct auxio_softc *sc = x; 108 int s; 109 uint32_t led; 110 111 if (do_blink == 0) 112 return; 113 114 s = splhigh(); 115 if (sc->sc_flags & AUXIO_EBUS) 116 led = le32toh(bus_space_read_4(sc->sc_tag, sc->sc_led, 0)); 117 else 118 led = bus_space_read_1(sc->sc_tag, sc->sc_led, 0); 119 120 led = led ^ AUXIO_LED_LED; 121 if (sc->sc_flags & AUXIO_EBUS) 122 bus_space_write_4(sc->sc_tag, sc->sc_led, 0, htole32(led)); 123 else 124 bus_space_write_1(sc->sc_tag, sc->sc_led, 0, led); 125 splx(s); 126 127 /* 128 * Blink rate is: 129 * full cycle every second if completely idle (loadav = 0) 130 * full cycle every 2 seconds if loadav = 1 131 * full cycle every 3 seconds if loadav = 2 132 * etc. 133 */ 134 s = (((averunnable.ldavg[0] + FSCALE) * hz) >> (FSHIFT + 1)); 135 callout_reset(&blink_ch, s, auxio_blink, sc); 136 } 137 #endif 138 139 void 140 auxio_attach_common(struct auxio_softc *sc) 141 { 142 #ifdef BLINK 143 static int do_once = 1; 144 145 /* only start one blinker */ 146 if (do_once) { 147 auxio_blink(sc); 148 do_once = 0; 149 } 150 #endif 151 printf("\n"); 152 } 153 154 int 155 auxio_ebus_match(struct device *parent, struct cfdata *cf, void *aux) 156 { 157 struct ebus_attach_args *ea = aux; 158 159 return (strcmp(AUXIO_ROM_NAME, ea->ea_name) == 0); 160 } 161 162 void 163 auxio_ebus_attach(struct device *parent, struct device *self, void *aux) 164 { 165 struct auxio_softc *sc = (struct auxio_softc *)self; 166 struct ebus_attach_args *ea = aux; 167 168 sc->sc_tag = ea->ea_bustag; 169 170 if (ea->ea_nreg < 1) { 171 printf(": no registers??\n"); 172 return; 173 } 174 175 if (ea->ea_nreg != 5) { 176 printf(": not 5 (%d) registers, only setting led", 177 ea->ea_nreg); 178 sc->sc_flags = AUXIO_LEDONLY|AUXIO_EBUS; 179 } else if (ea->ea_nvaddr == 5) { 180 sc->sc_flags = AUXIO_EBUS; 181 182 sparc_promaddr_to_handle(sc->sc_tag, 183 ea->ea_vaddr[1], &sc->sc_pci); 184 sparc_promaddr_to_handle(sc->sc_tag, 185 ea->ea_vaddr[2], &sc->sc_freq); 186 sparc_promaddr_to_handle(sc->sc_tag, 187 ea->ea_vaddr[3], &sc->sc_scsi); 188 sparc_promaddr_to_handle(sc->sc_tag, 189 ea->ea_vaddr[4], &sc->sc_temp); 190 } else { 191 sc->sc_flags = AUXIO_EBUS; 192 bus_space_map(sc->sc_tag, EBUS_ADDR_FROM_REG(&ea->ea_reg[1]), 193 ea->ea_reg[1].size, 0, &sc->sc_pci); 194 bus_space_map(sc->sc_tag, EBUS_ADDR_FROM_REG(&ea->ea_reg[2]), 195 ea->ea_reg[2].size, 0, &sc->sc_freq); 196 bus_space_map(sc->sc_tag, EBUS_ADDR_FROM_REG(&ea->ea_reg[3]), 197 ea->ea_reg[3].size, 0, &sc->sc_scsi); 198 bus_space_map(sc->sc_tag, EBUS_ADDR_FROM_REG(&ea->ea_reg[4]), 199 ea->ea_reg[4].size, 0, &sc->sc_temp); 200 } 201 202 if (ea->ea_nvaddr > 0) { 203 sparc_promaddr_to_handle(sc->sc_tag, 204 ea->ea_vaddr[0], &sc->sc_led); 205 } else { 206 bus_space_map(sc->sc_tag, EBUS_ADDR_FROM_REG(&ea->ea_reg[0]), 207 ea->ea_reg[0].size, 0, &sc->sc_led); 208 } 209 210 auxio_attach_common(sc); 211 } 212 213 int 214 auxio_sbus_match(struct device *parent, struct cfdata *cf, void *aux) 215 { 216 struct sbus_attach_args *sa = aux; 217 218 return (strcmp(AUXIO_ROM_NAME, sa->sa_name) == 0); 219 } 220 221 void 222 auxio_sbus_attach(struct device *parent, struct device *self, void *aux) 223 { 224 struct auxio_softc *sc = (struct auxio_softc *)self; 225 struct sbus_attach_args *sa = aux; 226 227 sc->sc_tag = sa->sa_bustag; 228 229 if (sa->sa_nreg < 1) { 230 printf(": no registers??\n"); 231 return; 232 } 233 234 if (sa->sa_nreg != 1) { 235 printf(": not 1 (%d/%d) registers??", sa->sa_nreg, 236 sa->sa_npromvaddrs); 237 return; 238 } 239 240 /* sbus auxio only has one set of registers */ 241 sc->sc_flags = AUXIO_LEDONLY|AUXIO_SBUS; 242 if (sa->sa_npromvaddrs > 0) { 243 sbus_promaddr_to_handle(sc->sc_tag, 244 sa->sa_promvaddr, &sc->sc_led); 245 } else { 246 sbus_bus_map(sc->sc_tag, sa->sa_slot, sa->sa_offset, 247 sa->sa_size, 0, &sc->sc_led); 248 } 249 250 auxio_attach_common(sc); 251 } 252