nslu2_leds.c revision 1.4 1 /* $NetBSD: nslu2_leds.c,v 1.4 2006/12/10 10:23:37 scw Exp $ */
2
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Steve C. Woodford.
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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: nslu2_leds.c,v 1.4 2006/12/10 10:23:37 scw Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/device.h>
46 #include <sys/callout.h>
47 #include <sys/proc.h>
48
49 #include <machine/intr.h>
50
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbcdc.h>
53 #include <dev/usb/usbdi.h> /* XXX: For IPL_USB */
54
55 #include <arm/xscale/ixp425var.h>
56
57 #include <evbarm/nslu2/nslu2reg.h>
58
59 #define SLUGLED_FLASH_LEN (hz/8) /* How many ticks an LED stays lit */
60
61 #define LEDBITS_USB0 (1u << GPIO_LED_DISK1)
62 #define LEDBITS_USB1 (1u << GPIO_LED_DISK2)
63
64 /*
65 * The Ready/Status bits control a tricolour LED.
66 * Ready is green, status is red.
67 */
68 #define LEDBITS_READY (1u << GPIO_LED_READY)
69 #define LEDBITS_STATUS (1u << GPIO_LED_STATUS)
70
71 struct slugled_softc {
72 struct device sc_dev;
73 void *sc_tmr_ih;
74 struct callout sc_usb0;
75 void *sc_usb0_ih;
76 struct callout sc_usb1;
77 void *sc_usb1_ih;
78 struct callout sc_usb2;
79 void *sc_usb2_ih;
80 };
81
82 static int slugled_attached;
83
84 static void
85 slugled_callout(void *arg)
86 {
87 uint32_t reg, bit;
88 int is;
89
90 bit = (uint32_t)(uintptr_t)arg;
91
92 is = disable_interrupts(I32_bit);
93 reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
94 GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg | bit);
95 restore_interrupts(is);
96 }
97
98 static int
99 slugled_intr0(void *arg)
100 {
101 struct slugled_softc *sc = arg;
102 uint32_t reg;
103 int is;
104
105 is = disable_interrupts(I32_bit);
106 reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
107 reg &= ~LEDBITS_USB0;
108 GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg);
109 restore_interrupts(is);
110
111 callout_schedule(&sc->sc_usb0, SLUGLED_FLASH_LEN);
112
113 return (1);
114 }
115
116 static int
117 slugled_intr1(void *arg)
118 {
119 struct slugled_softc *sc = arg;
120 uint32_t reg;
121 int is;
122
123 is = disable_interrupts(I32_bit);
124 reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
125 reg &= ~LEDBITS_USB1;
126 GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg);
127 restore_interrupts(is);
128
129 callout_schedule(&sc->sc_usb1, SLUGLED_FLASH_LEN);
130
131 return (1);
132 }
133
134 static int
135 slugled_intr2(void *arg)
136 {
137 struct slugled_softc *sc = arg;
138 uint32_t reg;
139 int is;
140
141 is = disable_interrupts(I32_bit);
142 reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
143 reg &= ~(LEDBITS_USB0 | LEDBITS_USB1);
144 GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg);
145 restore_interrupts(is);
146
147 callout_schedule(&sc->sc_usb2, SLUGLED_FLASH_LEN);
148
149 return (1);
150 }
151
152 static int
153 slugled_tmr(void *arg)
154 {
155 struct clockframe *frame = arg;
156 uint32_t reg, bit;
157 int is;
158
159 if (CLKF_INTR(frame) || sched_whichqs || curlwp)
160 bit = LEDBITS_STATUS;
161 else
162 bit = 0;
163
164 is = disable_interrupts(I32_bit);
165 reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
166 reg &= ~LEDBITS_STATUS;
167 GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg | bit);
168 restore_interrupts(is);
169
170 return (1);
171 }
172
173 static void
174 slugled_shutdown(void *arg)
175 {
176 struct slugled_softc *sc = arg;
177 uint32_t reg;
178 int s;
179
180 ixp425_intr_disestablish(sc->sc_usb0_ih);
181 ixp425_intr_disestablish(sc->sc_usb1_ih);
182 ixp425_intr_disestablish(sc->sc_tmr_ih);
183
184 /* Cancel the callouts */
185 s = splsoftclock();
186 callout_stop(&sc->sc_usb0);
187 callout_stop(&sc->sc_usb1);
188 splx(s);
189
190 /* Turn off the disk LEDs, and set Ready/Status to amber */
191 s = splhigh();
192 reg = GPIO_CONF_READ_4(ixp425_softc,IXP425_GPIO_GPOUTR);
193 reg |= LEDBITS_USB0 | LEDBITS_USB1 | LEDBITS_STATUS | LEDBITS_READY;
194 GPIO_CONF_WRITE_4(ixp425_softc,IXP425_GPIO_GPOUTR, reg);
195 splx(s);
196 }
197
198 static void
199 slugled_defer(struct device *self)
200 {
201 struct slugled_softc *sc = (struct slugled_softc *) self;
202 struct ixp425_softc *ixsc = ixp425_softc;
203 uint32_t reg;
204 int s;
205
206 s = splhigh();
207
208 /* Configure LED GPIO pins as output */
209 reg = GPIO_CONF_READ_4(ixsc, IXP425_GPIO_GPOER);
210 reg &= ~(LEDBITS_USB0 | LEDBITS_USB1);
211 reg &= ~(LEDBITS_READY | LEDBITS_STATUS);
212 GPIO_CONF_WRITE_4(ixsc, IXP425_GPIO_GPOER, reg);
213
214 /* All LEDs off */
215 reg = GPIO_CONF_READ_4(ixsc, IXP425_GPIO_GPOUTR);
216 reg |= LEDBITS_USB0 | LEDBITS_USB1;
217 reg &= ~(LEDBITS_STATUS | LEDBITS_READY);
218 GPIO_CONF_WRITE_4(ixsc, IXP425_GPIO_GPOUTR, reg);
219
220 splx(s);
221
222 if (shutdownhook_establish(slugled_shutdown, sc) == NULL)
223 aprint_error("%s: WARNING - Failed to register shutdown hook\n",
224 sc->sc_dev.dv_xname);
225
226 callout_init(&sc->sc_usb0);
227 callout_setfunc(&sc->sc_usb0, slugled_callout,
228 (void *)(uintptr_t)LEDBITS_USB0);
229
230 callout_init(&sc->sc_usb1);
231 callout_setfunc(&sc->sc_usb1, slugled_callout,
232 (void *)(uintptr_t)LEDBITS_USB1);
233
234 callout_init(&sc->sc_usb2);
235 callout_setfunc(&sc->sc_usb2, slugled_callout,
236 (void *)(uintptr_t)(LEDBITS_USB0 | LEDBITS_USB1));
237
238 sc->sc_usb0_ih = ixp425_intr_establish(PCI_INT_A, IPL_USB,
239 slugled_intr0, sc);
240 KDASSERT(sc->sc_usb0_ih != NULL);
241 sc->sc_usb1_ih = ixp425_intr_establish(PCI_INT_B, IPL_USB,
242 slugled_intr1, sc);
243 KDASSERT(sc->sc_usb1_ih != NULL);
244 sc->sc_usb2_ih = ixp425_intr_establish(PCI_INT_C, IPL_USB,
245 slugled_intr2, sc);
246 KDASSERT(sc->sc_usb2_ih != NULL);
247
248 sc->sc_tmr_ih = ixp425_intr_establish(IXP425_INT_TMR0, IPL_CLOCK,
249 slugled_tmr, NULL);
250 KDASSERT(sc->sc_tmr_ih != NULL);
251 }
252
253 static int
254 slugled_match(struct device *parent, struct cfdata *match, void *aux)
255 {
256
257 return (slugled_attached == 0);
258 }
259
260 static void
261 slugled_attach(struct device *parent, struct device *self, void *aux)
262 {
263
264 aprint_normal(": LED support\n");
265
266 slugled_attached = 1;
267
268 config_interrupts(self, slugled_defer);
269 }
270
271 CFATTACH_DECL(slugled, sizeof(struct slugled_softc),
272 slugled_match, slugled_attach, NULL, NULL);
273