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