auxio.c revision 1.24 1 /* $NetBSD: auxio.c,v 1.24 2015/08/24 23:32:07 mrg Exp $ */
2
3 /*
4 * Copyright (c) 2000, 2001, 2015 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 /*
30 * AUXIO registers support on the sbus & ebus2, used for the floppy driver
31 * and to control the system LED, for the BLINK option.
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: auxio.c,v 1.24 2015/08/24 23:32:07 mrg Exp $");
36
37 #include "opt_auxio.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/callout.h>
43 #include <sys/errno.h>
44 #include <sys/device.h>
45 #include <sys/malloc.h>
46
47 #include <machine/autoconf.h>
48 #include <machine/cpu.h>
49
50 #include <dev/ebus/ebusreg.h>
51 #include <dev/ebus/ebusvar.h>
52 #include <dev/sbus/sbusvar.h>
53 #include <sparc64/dev/auxioreg.h>
54 #include <sparc64/dev/auxiovar.h>
55
56 /*
57 * on sun4u, auxio exists with one register (LED) on the sbus, and 5
58 * registers on the ebus2 (pci) (LED, PCIMODE, FREQUENCY, SCSI
59 * OSCILLATOR, and TEMP SENSE.
60 */
61
62 struct auxio_softc {
63 device_t sc_dev;
64
65 kmutex_t sc_lock;
66
67 /* parent's tag */
68 bus_space_tag_t sc_tag;
69
70 /* handles to the various auxio register sets */
71 bus_space_handle_t sc_led;
72 bus_space_handle_t sc_pci;
73 bus_space_handle_t sc_freq;
74 bus_space_handle_t sc_scsi;
75 bus_space_handle_t sc_temp;
76
77 int sc_flags;
78 #define AUXIO_LEDONLY 0x1 // only sc_led is valid
79 #define AUXIO_EBUS 0x2
80 };
81
82 #define AUXIO_ROM_NAME "auxio"
83
84
85 static uint32_t auxio_read_led(struct auxio_softc *);
86 static void auxio_write_led(struct auxio_softc *, uint32_t);
87 static void auxio_attach_common(struct auxio_softc *);
88 static int auxio_ebus_match(device_t, cfdata_t, void *);
89 static void auxio_ebus_attach(device_t, device_t, void *);
90 static int auxio_sbus_match(device_t, cfdata_t, void *);
91 static void auxio_sbus_attach(device_t, device_t, void *);
92
93 CFATTACH_DECL_NEW(auxio_ebus, sizeof(struct auxio_softc),
94 auxio_ebus_match, auxio_ebus_attach, NULL, NULL);
95
96 CFATTACH_DECL_NEW(auxio_sbus, sizeof(struct auxio_softc),
97 auxio_sbus_match, auxio_sbus_attach, NULL, NULL);
98
99 extern struct cfdriver auxio_cd;
100
101 static __inline__ uint32_t
102 auxio_read_led(struct auxio_softc *sc)
103 {
104 uint32_t led;
105
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
111 return led;
112 }
113
114 static __inline__ void
115 auxio_write_led(struct auxio_softc *sc, uint32_t led)
116 {
117
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 }
123
124 #ifdef BLINK
125 static callout_t blink_ch;
126 static void auxio_blink(void *);
127
128 /* let someone disable it if it's already turned on; XXX sysctl? */
129 int do_blink = 1;
130
131 static void
132 auxio_blink(void *x)
133 {
134 struct auxio_softc *sc = x;
135 int s;
136 uint32_t led;
137
138 if (do_blink == 0)
139 return;
140
141 mutex_enter(&sc->sc_lock);
142 led = auxio_read_led(sc);
143 led = led ^ AUXIO_LED_LED;
144 auxio_write_led(sc, led);
145 mutex_exit(&sc->sc_lock);
146
147 /*
148 * Blink rate is:
149 * full cycle every second if completely idle (loadav = 0)
150 * full cycle every 2 seconds if loadav = 1
151 * full cycle every 3 seconds if loadav = 2
152 * etc.
153 */
154 s = (((averunnable.ldavg[0] + FSCALE) * hz) >> (FSHIFT + 1));
155 callout_reset(&blink_ch, s, auxio_blink, sc);
156 }
157 #endif
158
159 static void
160 auxio_attach_common(struct auxio_softc *sc)
161 {
162 #ifdef BLINK
163 static int do_once = 1;
164
165 /* only start one blinker */
166 if (do_once) {
167 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH);
168 callout_init(&blink_ch, CALLOUT_MPSAFE);
169 auxio_blink(sc);
170 do_once = 0;
171 }
172 #endif
173 printf("\n");
174 }
175
176 static int
177 auxio_ebus_match(device_t parent, cfdata_t cf, void *aux)
178 {
179 struct ebus_attach_args *ea = aux;
180
181 return (strcmp(AUXIO_ROM_NAME, ea->ea_name) == 0);
182 }
183
184 static void
185 auxio_ebus_attach(device_t parent, device_t self, void *aux)
186 {
187 struct auxio_softc *sc = device_private(self);
188 struct ebus_attach_args *ea = aux;
189
190 sc->sc_dev = self;
191 sc->sc_tag = ea->ea_bustag;
192
193 if (ea->ea_nreg < 1) {
194 printf(": no registers??\n");
195 return;
196 }
197
198 sc->sc_flags = AUXIO_EBUS;
199 if (ea->ea_nreg != 5) {
200 printf(": not 5 (%d) registers, only setting led",
201 ea->ea_nreg);
202 sc->sc_flags |= AUXIO_LEDONLY;
203 } else if (ea->ea_nvaddr == 5) {
204 sparc_promaddr_to_handle(sc->sc_tag,
205 ea->ea_vaddr[1], &sc->sc_pci);
206 sparc_promaddr_to_handle(sc->sc_tag,
207 ea->ea_vaddr[2], &sc->sc_freq);
208 sparc_promaddr_to_handle(sc->sc_tag,
209 ea->ea_vaddr[3], &sc->sc_scsi);
210 sparc_promaddr_to_handle(sc->sc_tag,
211 ea->ea_vaddr[4], &sc->sc_temp);
212 } else {
213 bus_space_map(sc->sc_tag, EBUS_ADDR_FROM_REG(&ea->ea_reg[1]),
214 ea->ea_reg[1].size, 0, &sc->sc_pci);
215 bus_space_map(sc->sc_tag, EBUS_ADDR_FROM_REG(&ea->ea_reg[2]),
216 ea->ea_reg[2].size, 0, &sc->sc_freq);
217 bus_space_map(sc->sc_tag, EBUS_ADDR_FROM_REG(&ea->ea_reg[3]),
218 ea->ea_reg[3].size, 0, &sc->sc_scsi);
219 bus_space_map(sc->sc_tag, EBUS_ADDR_FROM_REG(&ea->ea_reg[4]),
220 ea->ea_reg[4].size, 0, &sc->sc_temp);
221 }
222
223 if (ea->ea_nvaddr > 0) {
224 sparc_promaddr_to_handle(sc->sc_tag,
225 ea->ea_vaddr[0], &sc->sc_led);
226 } else {
227 bus_space_map(sc->sc_tag, EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
228 ea->ea_reg[0].size, 0, &sc->sc_led);
229 }
230
231 auxio_attach_common(sc);
232 }
233
234 static int
235 auxio_sbus_match(device_t parent, cfdata_t cf, void *aux)
236 {
237 struct sbus_attach_args *sa = aux;
238
239 return strcmp(AUXIO_ROM_NAME, sa->sa_name) == 0;
240 }
241
242 static void
243 auxio_sbus_attach(device_t parent, device_t self, void *aux)
244 {
245 struct auxio_softc *sc = device_private(self);
246 struct sbus_attach_args *sa = aux;
247
248 sc->sc_dev = self;
249 sc->sc_tag = sa->sa_bustag;
250
251 if (sa->sa_nreg < 1) {
252 printf(": no registers??\n");
253 return;
254 }
255
256 if (sa->sa_nreg != 1) {
257 printf(": not 1 (%d/%d) registers??", sa->sa_nreg,
258 sa->sa_npromvaddrs);
259 return;
260 }
261
262 /* sbus auxio only has one set of registers */
263 sc->sc_flags = AUXIO_LEDONLY;
264 if (sa->sa_npromvaddrs > 0) {
265 sbus_promaddr_to_handle(sc->sc_tag,
266 sa->sa_promvaddr, &sc->sc_led);
267 } else {
268 sbus_bus_map(sc->sc_tag, sa->sa_slot, sa->sa_offset,
269 sa->sa_size, 0, &sc->sc_led);
270 }
271
272 auxio_attach_common(sc);
273 }
274
275 int
276 auxio_fd_control(u_int32_t bits)
277 {
278 struct auxio_softc *sc;
279 u_int32_t led;
280
281 if (auxio_cd.cd_ndevs == 0) {
282 return ENXIO;
283 }
284
285 /*
286 * XXX This does not handle > 1 auxio correctly.
287 * We'll assume the floppy drive is tied to first auxio found.
288 */
289 sc = device_lookup_private(&auxio_cd, 0);
290 if (!sc) {
291 return ENXIO;
292 }
293
294 mutex_enter(&sc->sc_lock);
295 led = auxio_read_led(sc);
296 led = (led & ~AUXIO_LED_FLOPPY_MASK) | bits;
297 auxio_write_led(sc, led);
298 mutex_exit(&sc->sc_lock);
299
300 return 0;
301 }
302