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