xref: /netbsd-src/sys/arch/sun3/sun3/leds.c (revision cf80ca28cb9ef8022317ff1af66f5a815972f8ad)
1 /*	$NetBSD: leds.c,v 1.16 2023/12/20 05:18:00 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Gordon W. Ross and der Mouse.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Functions to flash the LEDs with some pattern.
34  * The 3/80 has just one LED on the front panel,
35  * which we turn on during boot and leave alone.
36  * All other Sun3 machines have an 8-position LED
37  * array in which some pattern is animated.
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: leds.c,v 1.16 2023/12/20 05:18:00 thorpej Exp $");
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/device.h>
46 #include <sys/conf.h>
47 #include <sys/buf.h>
48 #include <sys/proc.h>
49 
50 #include <uvm/uvm_extern.h>
51 
52 #include <machine/autoconf.h>
53 #include <machine/idprom.h>
54 #include <machine/leds.h>
55 
56 #include <sun3/sun3/machdep.h>
57 #ifdef	_SUN3_
58 #include <sun3/sun3/control.h>
59 #endif
60 #ifdef	_SUN3X_
61 #include <sun3/sun3x/obio.h>
62 static volatile uint8_t *diagreg;
63 #endif
64 
65 static u_char led_countdown = 0;
66 static u_char led_px = 0;
67 
68 /*
69  * Initial value is the default pattern set. Note, only bit zero
70  * shows on the 3/80, and it is active-high (3/470 is active-low).
71  * We normally want the 3/80's LED to just stay ON, so the 3/80
72  * will change the default patlen to one, causing the LED to keep
73  * using the first byte of the pattern where bit zero is ON.
74  */
75 static struct led_patterns ledpat = {
76 	8,	/* divisor */
77 	8,	/* patlen */
78 	{	/* patterns */
79 		0x0F, 0x1E, 0x3C, 0x78,
80 		0xF0, 0xE1, 0xC3, 0x87,
81 	}
82 };
83 
84 /*
85  * This is called early during startup to find the
86  * diag register (LEDs) and turn on the light(s).
87  */
88 void
leds_init(void)89 leds_init(void)
90 {
91 #ifdef	_SUN3X_
92 	vaddr_t va;
93 
94 	find_prom_map(OBIO_DIAGREG, PMAP_OBIO, 1, &va);
95 	diagreg = (void *)va;
96 	if (cpu_machine_id == ID_SUN3X_80)
97 		ledpat.patlen = 1;
98 #endif	/* SUN3X */
99 
100 	/* Turn on some lights. */
101 	leds_intr();
102 }
103 
104 /*
105  * This is called by the clock interrupt.
106  */
107 void
leds_intr(void)108 leds_intr(void)
109 {
110 	u_char i;
111 
112 	if (led_countdown) {
113 		led_countdown--;
114 		return;
115 	}
116 
117 	led_countdown = ledpat.divisor - 1;
118 	i = led_px;
119 
120 #ifdef	_SUN3X_
121 	*diagreg = (char) ledpat.pat[i];
122 #else
123 	set_control_byte(DIAG_REG, ledpat.pat[i]);
124 #endif
125 
126 	i = i+1;
127 	if (i == ledpat.patlen)
128 		i = 0;
129 	led_px = i;
130 }
131 
132 /*
133  * This is called by mem.c to handle /dev/leds
134  */
135 int
leds_uio(struct uio * uio)136 leds_uio(struct uio *uio)
137 {
138 	int cnt, error;
139 	int off;	/* NOT off_t */
140 	void *va;
141 
142 	off = uio->uio_offset;
143 	if ((off < 0) || (off > sizeof(ledpat)))
144 		return (EIO);
145 
146 	cnt = uimin(uio->uio_resid, (sizeof(ledpat) - off));
147 	if (cnt == 0)
148 		return (0); /* EOF */
149 
150 	va = ((char*)(&ledpat)) + off;
151 	error = uiomove(va, cnt, uio);
152 
153 	return (error);
154 }
155