xref: /netbsd-src/sys/arch/sparc64/dev/auxio.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: auxio.c,v 1.26 2016/10/14 11:33:08 mlelstv 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.26 2016/10/14 11:33:08 mlelstv 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 static uint32_t	auxio_read_led(struct auxio_softc *);
57 static void	auxio_write_led(struct auxio_softc *, uint32_t);
58 void	auxio_attach_common(struct auxio_softc *);
59 
60 extern struct cfdriver auxio_cd;
61 
62 static __inline__ uint32_t
63 auxio_read_led(struct auxio_softc *sc)
64 {
65 	uint32_t led;
66 
67 	if (sc->sc_flags & AUXIO_EBUS)
68 		led = le32toh(bus_space_read_4(sc->sc_tag, sc->sc_led, 0));
69 	else
70 		led = bus_space_read_1(sc->sc_tag, sc->sc_led, 0);
71 
72 	return led;
73 }
74 
75 static __inline__ void
76 auxio_write_led(struct auxio_softc *sc, uint32_t led)
77 {
78 
79 	if (sc->sc_flags & AUXIO_EBUS)
80 		bus_space_write_4(sc->sc_tag, sc->sc_led, 0, htole32(led));
81 	else
82 		bus_space_write_1(sc->sc_tag, sc->sc_led, 0, led);
83 }
84 
85 #ifdef BLINK
86 static callout_t blink_ch;
87 static void auxio_blink(void *);
88 
89 /* let someone disable it if it's already turned on; XXX sysctl? */
90 int do_blink = 1;
91 
92 static void
93 auxio_blink(void *x)
94 {
95 	struct auxio_softc *sc = x;
96 	int s;
97 	uint32_t led;
98 
99 	if (do_blink == 0)
100 		return;
101 
102 	mutex_enter(&sc->sc_lock);
103 	led = auxio_read_led(sc);
104 	led = led ^ AUXIO_LED_LED;
105 	auxio_write_led(sc, led);
106 	mutex_exit(&sc->sc_lock);
107 
108 	/*
109 	 * Blink rate is:
110 	 *	full cycle every second if completely idle (loadav = 0)
111 	 *	full cycle every 2 seconds if loadav = 1
112 	 *	full cycle every 3 seconds if loadav = 2
113 	 * etc.
114 	 */
115 	s = (((averunnable.ldavg[0] + FSCALE) * hz) >> (FSHIFT + 1));
116 	callout_reset(&blink_ch, s, auxio_blink, sc);
117 }
118 #endif
119 
120 void
121 auxio_attach_common(struct auxio_softc *sc)
122 {
123 	static int do_once = 1;
124 
125 	/* only start one blinker */
126 	if (do_once) {
127 		mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH);
128 #ifdef BLINK
129 		callout_init(&blink_ch, CALLOUT_MPSAFE);
130 		auxio_blink(sc);
131 #endif
132 		do_once = 0;
133 	}
134 	printf("\n");
135 }
136 
137 int
138 auxio_fd_control(u_int32_t bits)
139 {
140 	struct auxio_softc *sc;
141 	u_int32_t led;
142 
143 	if (auxio_cd.cd_ndevs == 0) {
144 		return ENXIO;
145 	}
146 
147 	/*
148 	 * XXX This does not handle > 1 auxio correctly.
149 	 * We'll assume the floppy drive is tied to first auxio found.
150 	 */
151 	sc = device_lookup_private(&auxio_cd, 0);
152 	if (!sc) {
153 		return ENXIO;
154 	}
155 
156 	mutex_enter(&sc->sc_lock);
157 	led = auxio_read_led(sc);
158 	led = (led & ~AUXIO_LED_FLOPPY_MASK) | bits;
159 	auxio_write_led(sc, led);
160 	mutex_exit(&sc->sc_lock);
161 
162 	return 0;
163 }
164