auxio.c revision 1.3.4.2 1 /* $NetBSD: auxio.c,v 1.3.4.2 2002/04/01 07:42:59 nathanw 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/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/callout.h>
40 #include <sys/errno.h>
41 #include <sys/device.h>
42 #include <sys/malloc.h>
43
44 #include <machine/autoconf.h>
45 #include <machine/cpu.h>
46
47 #include <dev/ebus/ebusreg.h>
48 #include <dev/ebus/ebusvar.h>
49 #include <sparc64/dev/sbusvar.h>
50 #include <sparc64/dev/auxioreg.h>
51
52 /*
53 * on sun4u, auxio exists with one register (LED) on the sbus, and 5
54 * registers on the ebus2 (pci) (LED, PCIMODE, FREQUENCY, SCSI
55 * OSCILLATOR, and TEMP SENSE.
56 */
57
58 struct auxio_softc {
59 struct device sc_dev;
60
61 /* parent's tag */
62 bus_space_tag_t sc_tag;
63
64 /* handles to the various auxio regsiter sets */
65 bus_space_handle_t sc_led;
66 bus_space_handle_t sc_pci;
67 bus_space_handle_t sc_freq;
68 bus_space_handle_t sc_scsi;
69 bus_space_handle_t sc_temp;
70
71 int sc_flags;
72 #define AUXIO_LEDONLY 0x1
73 #define AUXIO_EBUS 0x2
74 #define AUXIO_SBUS 0x4
75 };
76
77 #define AUXIO_ROM_NAME "auxio"
78
79 void auxio_attach_common(struct auxio_softc *);
80 int auxio_ebus_match(struct device *, struct cfdata *, void *);
81 void auxio_ebus_attach(struct device *, struct device *, void *);
82 int auxio_sbus_match(struct device *, struct cfdata *, void *);
83 void auxio_sbus_attach(struct device *, struct device *, void *);
84
85 struct cfattach auxio_ebus_ca = {
86 sizeof(struct auxio_softc), auxio_ebus_match, auxio_ebus_attach
87 };
88 struct cfattach auxio_sbus_ca = {
89 sizeof(struct auxio_softc), auxio_sbus_match, auxio_sbus_attach
90 };
91
92 #ifdef BLINK
93 static struct callout blink_ch = CALLOUT_INITIALIZER;
94
95 static void auxio_blink(void *);
96
97 static void
98 auxio_blink(x)
99 void *x;
100 {
101 struct auxio_softc *sc = x;
102 int s;
103 u_int32_t led;
104
105 s = splhigh();
106 if (sc->sc_flags & AUXIO_EBUS)
107 led = le32toh(bus_space_read_4(sc->sc_tag, sc->sc_led, 0));
108 else
109 led = bus_space_read_1(sc->sc_tag, sc->sc_led, 0);
110 if (led & AUXIO_LED_LED)
111 led = 0;
112 else
113 led = AUXIO_LED_LED;
114 if (sc->sc_flags & AUXIO_EBUS)
115 bus_space_write_4(sc->sc_tag, sc->sc_led, 0, htole32(led));
116 else
117 bus_space_write_1(sc->sc_tag, sc->sc_led, 0, led);
118 splx(s);
119
120 /*
121 * Blink rate is:
122 * full cycle every second if completely idle (loadav = 0)
123 * full cycle every 2 seconds if loadav = 1
124 * full cycle every 3 seconds if loadav = 2
125 * etc.
126 */
127 s = (((averunnable.ldavg[0] + FSCALE) * hz) >> (FSHIFT + 1));
128 callout_reset(&blink_ch, s, auxio_blink, sc);
129 }
130 #endif
131
132 void
133 auxio_attach_common(sc)
134 struct auxio_softc *sc;
135 {
136 #ifdef BLINK
137 static int do_once = 1;
138
139 /* only start one blinker */
140 if (do_once) {
141 auxio_blink(sc);
142 do_once = 0;
143 }
144 #endif
145 printf("\n");
146 }
147
148 int
149 auxio_ebus_match(parent, cf, aux)
150 struct device *parent;
151 struct cfdata *cf;
152 void *aux;
153 {
154 struct ebus_attach_args *ea = aux;
155
156 return (strcmp(AUXIO_ROM_NAME, ea->ea_name) == 0);
157 }
158
159 void
160 auxio_ebus_attach(parent, self, aux)
161 struct device *parent, *self;
162 void *aux;
163 {
164 struct auxio_softc *sc = (struct auxio_softc *)self;
165 struct ebus_attach_args *ea = aux;
166
167 sc->sc_tag = ea->ea_bustag;
168
169 if (ea->ea_nreg < 1) {
170 printf(": no registers??\n");
171 return;
172 }
173
174 if (ea->ea_nreg != 5) {
175 printf(": not 5 (%d) registers, only setting led",
176 ea->ea_nreg);
177 sc->sc_flags = AUXIO_LEDONLY|AUXIO_EBUS;
178 } else if (ea->ea_nvaddr == 5) {
179 sc->sc_flags = AUXIO_EBUS;
180
181 sparc_promaddr_to_handle(sc->sc_tag,
182 ea->ea_vaddr[1], &sc->sc_pci);
183 sparc_promaddr_to_handle(sc->sc_tag,
184 ea->ea_vaddr[2], &sc->sc_freq);
185 sparc_promaddr_to_handle(sc->sc_tag,
186 ea->ea_vaddr[3], &sc->sc_scsi);
187 sparc_promaddr_to_handle(sc->sc_tag,
188 ea->ea_vaddr[4], &sc->sc_temp);
189 } else {
190 sc->sc_flags = AUXIO_EBUS;
191 bus_space_map(sc->sc_tag, EBUS_ADDR_FROM_REG(&ea->ea_reg[1]),
192 ea->ea_reg[1].size, 0, &sc->sc_pci);
193 bus_space_map(sc->sc_tag, EBUS_ADDR_FROM_REG(&ea->ea_reg[2]),
194 ea->ea_reg[2].size, 0, &sc->sc_freq);
195 bus_space_map(sc->sc_tag, EBUS_ADDR_FROM_REG(&ea->ea_reg[3]),
196 ea->ea_reg[3].size, 0, &sc->sc_scsi);
197 bus_space_map(sc->sc_tag, EBUS_ADDR_FROM_REG(&ea->ea_reg[4]),
198 ea->ea_reg[4].size, 0, &sc->sc_temp);
199 }
200
201 if (ea->ea_nvaddr > 0) {
202 sparc_promaddr_to_handle(sc->sc_tag,
203 ea->ea_vaddr[0], &sc->sc_led);
204 } else {
205 bus_space_map(sc->sc_tag, EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
206 ea->ea_reg[0].size, 0, &sc->sc_led);
207 }
208
209 auxio_attach_common(sc);
210 }
211
212 int
213 auxio_sbus_match(parent, cf, aux)
214 struct device *parent;
215 struct cfdata *cf;
216 void *aux;
217 {
218 struct sbus_attach_args *sa = aux;
219
220 return (strcmp(AUXIO_ROM_NAME, sa->sa_name) == 0);
221 }
222
223 void
224 auxio_sbus_attach(parent, self, aux)
225 struct device *parent, *self;
226 void *aux;
227 {
228 struct auxio_softc *sc = (struct auxio_softc *)self;
229 struct sbus_attach_args *sa = aux;
230
231 sc->sc_tag = sa->sa_bustag;
232
233 if (sa->sa_nreg < 1) {
234 printf(": no registers??\n");
235 return;
236 }
237
238 if (sa->sa_nreg != 1) {
239 printf(": not 1 (%d/%d) registers??", sa->sa_nreg,
240 sa->sa_npromvaddrs);
241 return;
242 }
243
244 /* sbus auxio only has one set of registers */
245 sc->sc_flags = AUXIO_LEDONLY|AUXIO_SBUS;
246 if (sa->sa_npromvaddrs > 0) {
247 sbus_promaddr_to_handle(sc->sc_tag,
248 sa->sa_promvaddr, &sc->sc_led);
249 } else {
250 sbus_bus_map(sc->sc_tag, sa->sa_slot, sa->sa_offset,
251 sa->sa_size, 0, &sc->sc_led);
252 }
253
254 auxio_attach_common(sc);
255 }
256