1 /*        $NetBSD: leds.c,v 1.11 2023/12/20 15:34:46 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  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: leds.c,v 1.11 2023/12/20 15:34:46 thorpej Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/cpu.h>
42 #include <sys/conf.h>
43 #include <sys/device.h>
44 #include <sys/proc.h>
45 
46 #include <machine/sid.h>
47 #include <machine/leds.h>
48 
49 #include "opt_cputype.h"
50 
51 static volatile u_short *diagreg = 0;
52 static u_char led_countdown = 0;
53 static u_char led_px = 0;
54 
55 /*
56  * Initial value is the default pattern set.
57  */
58 struct led_patterns ledpat = {
59           16,       /* divisor */
60           12,       /* patlen */
61           {         /* patterns */
62                     0x03, 0x06, 0x0c, 0x18,
63                     0x30, 0x60, 0xc0, 0x60,
64                     0x30, 0x18, 0x0c, 0x06,
65           }
66 };
67 
68 void
ledsattach(int a)69 ledsattach(int a)
70 {
71           switch (vax_boardtype) {
72 #if VAX46 || VAXANY
73           case VAX_BTYP_46: {
74                     extern struct vs_cpu *ka46_cpu;
75                     diagreg = (volatile u_short *)(&ka46_cpu->vc_diagdsp);
76                     break;
77                     }
78 #endif
79 #if VAX48 || VAXANY
80           case VAX_BTYP_48: {
81                     extern struct vs_cpu *ka48_cpu;
82                     diagreg = (volatile u_short *)(&ka48_cpu->vc_diagdsp);
83                     break;
84                     }
85 #endif
86 #if VAX49 || VAXANY
87           case VAX_BTYP_49:
88                     diagreg = (volatile u_short *)vax_map_physmem(0x25800000, 1);
89                     diagreg += 2;
90                     break;
91 #endif
92           default:
93                     return;
94           }
95 
96           /* Turn on some lights. */
97           leds_intr();
98 }
99 
100 /*
101  * This is called by the clock interrupt.
102  */
103 void
leds_intr(void)104 leds_intr(void)
105 {
106           register u_char i;
107 
108           if (diagreg == 0)
109                     return;
110 
111           if (led_countdown) {
112                     led_countdown--;
113                     return;
114           }
115 
116           led_countdown = ledpat.divisor - 1;
117           i = led_px;
118 
119           *diagreg = ~ledpat.pat[i];
120 
121           i = i+1;
122           if (i == ledpat.patlen)
123                     i = 0;
124           led_px = i;
125 }
126 
127 /*
128  * This is called by mem.c to handle /dev/leds
129  */
130 int
leds_uio(struct uio * uio)131 leds_uio(struct uio *uio)
132 {
133           int cnt, error;
134           int off;  /* NOT off_t */
135           char *va;
136 
137           off = uio->uio_offset;
138           if ((off < 0) || (off > sizeof(ledpat)))
139                     return (EIO);
140 
141           cnt = uimin(uio->uio_resid, (sizeof(ledpat) - off));
142           if (cnt == 0)
143                     return (0); /* EOF */
144 
145 
146           va = (char *)&ledpat;
147           va = va + off;
148           error = uiomove(va, cnt, uio);
149 
150           return (error);
151 }
152 
153