nslu2_leds.c revision 1.1 1 1.1 scw /* $NetBSD: nslu2_leds.c,v 1.1 2006/02/28 20:40:33 scw Exp $ */
2 1.1 scw
3 1.1 scw /*-
4 1.1 scw * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 1.1 scw * All rights reserved.
6 1.1 scw *
7 1.1 scw * This code is derived from software contributed to The NetBSD Foundation
8 1.1 scw * by Steve C. Woodford.
9 1.1 scw *
10 1.1 scw * Redistribution and use in source and binary forms, with or without
11 1.1 scw * modification, are permitted provided that the following conditions
12 1.1 scw * are met:
13 1.1 scw * 1. Redistributions of source code must retain the above copyright
14 1.1 scw * notice, this list of conditions and the following disclaimer.
15 1.1 scw * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 scw * notice, this list of conditions and the following disclaimer in the
17 1.1 scw * documentation and/or other materials provided with the distribution.
18 1.1 scw * 3. All advertising materials mentioning features or use of this software
19 1.1 scw * must display the following acknowledgement:
20 1.1 scw * This product includes software developed by the NetBSD
21 1.1 scw * Foundation, Inc. and its contributors.
22 1.1 scw * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 scw * contributors may be used to endorse or promote products derived
24 1.1 scw * from this software without specific prior written permission.
25 1.1 scw *
26 1.1 scw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 scw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 scw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 scw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 scw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 scw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 scw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 scw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 scw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 scw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 scw * POSSIBILITY OF SUCH DAMAGE.
37 1.1 scw */
38 1.1 scw
39 1.1 scw #include <sys/cdefs.h>
40 1.1 scw __KERNEL_RCSID(0, "$NetBSD: nslu2_leds.c,v 1.1 2006/02/28 20:40:33 scw Exp $");
41 1.1 scw
42 1.1 scw #include <sys/param.h>
43 1.1 scw #include <sys/systm.h>
44 1.1 scw #include <sys/kernel.h>
45 1.1 scw #include <sys/device.h>
46 1.1 scw #include <sys/callout.h>
47 1.1 scw #include <sys/disk.h>
48 1.1 scw
49 1.1 scw #include <arm/xscale/ixp425var.h>
50 1.1 scw
51 1.1 scw #include <evbarm/nslu2/nslu2reg.h>
52 1.1 scw
53 1.1 scw #define SLUGLED_NLEDS 2
54 1.1 scw #define SLUGLED_DISKNAMES "sd%d" /* XXX: Make this configurable! */
55 1.1 scw
56 1.1 scw #define SLUGLED_POLL (hz/10) /* Poll disks 10 times per second */
57 1.1 scw #define SLUGLED_FLASH_LEN 2 /* How long, in terms of SLUGLED_POLL,
58 1.1 scw to light up an LED */
59 1.1 scw #define SLUGLED_READY_FLASH 3 /* Ditto for flashing Ready LED */
60 1.1 scw
61 1.1 scw #define LEDBITS_DISK0 (1u << GPIO_LED_DISK1)
62 1.1 scw #define LEDBITS_DISK1 (1u << GPIO_LED_DISK2)
63 1.1 scw
64 1.1 scw /*
65 1.1 scw * The Ready/Status bits control a tricolour LED.
66 1.1 scw * Ready is green, status is red.
67 1.1 scw */
68 1.1 scw #define LEDBITS_READY (1u << GPIO_LED_READY)
69 1.1 scw #define LEDBITS_STATUS (1u << GPIO_LED_STATUS)
70 1.1 scw
71 1.1 scw struct slugled_softc {
72 1.1 scw struct device sc_dev;
73 1.1 scw struct callout sc_co;
74 1.1 scw struct {
75 1.1 scw char sc_dk_name[DK_DISKNAMELEN];
76 1.1 scw struct timeval sc_dk_time;
77 1.1 scw int sc_dk_flash; /* Countdown to LED clear */
78 1.1 scw } sc_dk[SLUGLED_NLEDS];
79 1.1 scw uint32_t sc_state;
80 1.1 scw int sc_count;
81 1.1 scw };
82 1.1 scw
83 1.1 scw static int slugled_attached;
84 1.1 scw
85 1.1 scw static void
86 1.1 scw slugled_callout(void *arg)
87 1.1 scw {
88 1.1 scw struct slugled_softc *sc = arg;
89 1.1 scw struct timeval t;
90 1.1 scw struct disk *dk;
91 1.1 scw uint32_t reg, bit, new_state;
92 1.1 scw int s, i, dkbusy;
93 1.1 scw
94 1.1 scw new_state = sc->sc_state;
95 1.1 scw
96 1.1 scw for (i = 0; i < SLUGLED_NLEDS; i++) {
97 1.1 scw bit = i ? LEDBITS_DISK1 : LEDBITS_DISK0;
98 1.1 scw
99 1.1 scw s = splbio();
100 1.1 scw if ((dk = disk_find(sc->sc_dk[i].sc_dk_name)) == NULL) {
101 1.1 scw splx(s);
102 1.1 scw if (sc->sc_dk[i].sc_dk_flash > 0) {
103 1.1 scw new_state |= bit;
104 1.1 scw sc->sc_dk[i].sc_dk_flash = 0;
105 1.1 scw sc->sc_dk[i].sc_dk_time = mono_time;
106 1.1 scw }
107 1.1 scw
108 1.1 scw continue;
109 1.1 scw }
110 1.1 scw
111 1.1 scw dkbusy = dk->dk_busy;
112 1.1 scw t = dk->dk_timestamp;
113 1.1 scw splx(s);
114 1.1 scw
115 1.1 scw if (dkbusy || t.tv_sec != sc->sc_dk[i].sc_dk_time.tv_sec ||
116 1.1 scw t.tv_usec != sc->sc_dk[i].sc_dk_time.tv_usec) {
117 1.1 scw sc->sc_dk[i].sc_dk_flash = SLUGLED_FLASH_LEN;
118 1.1 scw sc->sc_dk[i].sc_dk_time = t;
119 1.1 scw new_state &= ~bit;
120 1.1 scw } else
121 1.1 scw if (sc->sc_dk[i].sc_dk_flash > 0 &&
122 1.1 scw --(sc->sc_dk[i].sc_dk_flash) == 0)
123 1.1 scw new_state |= bit;
124 1.1 scw }
125 1.1 scw
126 1.1 scw if ((++(sc->sc_count) % SLUGLED_READY_FLASH) == 0)
127 1.1 scw new_state ^= LEDBITS_READY;
128 1.1 scw
129 1.1 scw if (sc->sc_state != new_state) {
130 1.1 scw sc->sc_state = new_state;
131 1.1 scw s = splhigh();
132 1.1 scw reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
133 1.1 scw reg &= ~(LEDBITS_DISK0 | LEDBITS_DISK1 | LEDBITS_READY);
134 1.1 scw reg |= new_state;
135 1.1 scw GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg);
136 1.1 scw splx(s);
137 1.1 scw }
138 1.1 scw
139 1.1 scw callout_schedule(&sc->sc_co, SLUGLED_POLL);
140 1.1 scw }
141 1.1 scw
142 1.1 scw static void
143 1.1 scw slugled_shutdown(void *arg)
144 1.1 scw {
145 1.1 scw struct slugled_softc *sc = arg;
146 1.1 scw uint32_t reg;
147 1.1 scw int s;
148 1.1 scw
149 1.1 scw /* Cancel the disk activity callout */
150 1.1 scw s = splsoftclock();
151 1.1 scw callout_stop(&sc->sc_co);
152 1.1 scw splx(s);
153 1.1 scw
154 1.1 scw /* Turn off the disk LEDs, and set Ready/Status to red */
155 1.1 scw s = splhigh();
156 1.1 scw reg = GPIO_CONF_READ_4(ixp425_softc,IXP425_GPIO_GPOUTR);
157 1.1 scw reg &= ~LEDBITS_READY;
158 1.1 scw reg |= LEDBITS_DISK0 | LEDBITS_DISK1 | LEDBITS_STATUS;
159 1.1 scw GPIO_CONF_WRITE_4(ixp425_softc,IXP425_GPIO_GPOUTR, reg);
160 1.1 scw splx(s);
161 1.1 scw }
162 1.1 scw
163 1.1 scw static void
164 1.1 scw slugled_defer(struct device *self)
165 1.1 scw {
166 1.1 scw struct slugled_softc *sc = (struct slugled_softc *) self;
167 1.1 scw struct ixp425_softc *ixsc = ixp425_softc;
168 1.1 scw uint32_t reg;
169 1.1 scw int i, s;
170 1.1 scw
171 1.1 scw s = splhigh();
172 1.1 scw
173 1.1 scw /* Configure LED GPIO pins as output */
174 1.1 scw reg = GPIO_CONF_READ_4(ixsc, IXP425_GPIO_GPOER);
175 1.1 scw reg &= ~(LEDBITS_DISK0 | LEDBITS_DISK1);
176 1.1 scw reg &= ~(LEDBITS_READY | LEDBITS_STATUS);
177 1.1 scw GPIO_CONF_WRITE_4(ixsc, IXP425_GPIO_GPOER, reg);
178 1.1 scw
179 1.1 scw /* All LEDs off, except Ready LED */
180 1.1 scw reg = GPIO_CONF_READ_4(ixsc, IXP425_GPIO_GPOUTR);
181 1.1 scw reg |= LEDBITS_DISK0 | LEDBITS_DISK1 | LEDBITS_READY;
182 1.1 scw reg &= ~LEDBITS_STATUS;
183 1.1 scw GPIO_CONF_WRITE_4(ixsc, IXP425_GPIO_GPOUTR, reg);
184 1.1 scw
185 1.1 scw splx(s);
186 1.1 scw
187 1.1 scw for (i = 0; i < SLUGLED_NLEDS; i++) {
188 1.1 scw sprintf(sc->sc_dk[i].sc_dk_name, SLUGLED_DISKNAMES, i);
189 1.1 scw sc->sc_dk[i].sc_dk_flash = 0;
190 1.1 scw sc->sc_dk[i].sc_dk_time = mono_time;
191 1.1 scw }
192 1.1 scw
193 1.1 scw sc->sc_state = LEDBITS_DISK0 | LEDBITS_DISK1 | LEDBITS_READY;
194 1.1 scw sc->sc_count = 0;
195 1.1 scw
196 1.1 scw if (shutdownhook_establish(slugled_shutdown, sc) == NULL)
197 1.1 scw aprint_error("%s: WARNING - Failed to register shutdown hook\n",
198 1.1 scw sc->sc_dev.dv_xname);
199 1.1 scw
200 1.1 scw callout_init(&sc->sc_co);
201 1.1 scw callout_setfunc(&sc->sc_co, slugled_callout, sc);
202 1.1 scw callout_schedule(&sc->sc_co, SLUGLED_POLL);
203 1.1 scw }
204 1.1 scw
205 1.1 scw static int
206 1.1 scw slugled_match(struct device *parent, struct cfdata *match, void *aux)
207 1.1 scw {
208 1.1 scw
209 1.1 scw return (slugled_attached == 0);
210 1.1 scw }
211 1.1 scw
212 1.1 scw static void
213 1.1 scw slugled_attach(struct device *parent, struct device *self, void *aux)
214 1.1 scw {
215 1.1 scw
216 1.1 scw aprint_normal(": LED support\n");
217 1.1 scw
218 1.1 scw slugled_attached = 1;
219 1.1 scw
220 1.1 scw config_interrupts(self, slugled_defer);
221 1.1 scw }
222 1.1 scw
223 1.1 scw CFATTACH_DECL(slugled, sizeof(struct slugled_softc),
224 1.1 scw slugled_match, slugled_attach, NULL, NULL);
225