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