nslu2_leds.c revision 1.4 1 1.4 scw /* $NetBSD: nslu2_leds.c,v 1.4 2006/12/10 10:23:37 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.4 scw __KERNEL_RCSID(0, "$NetBSD: nslu2_leds.c,v 1.4 2006/12/10 10:23:37 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.4 scw #include <sys/proc.h>
48 1.4 scw
49 1.4 scw #include <machine/intr.h>
50 1.4 scw
51 1.4 scw #include <dev/usb/usb.h>
52 1.4 scw #include <dev/usb/usbcdc.h>
53 1.4 scw #include <dev/usb/usbdi.h> /* XXX: For IPL_USB */
54 1.1 scw
55 1.1 scw #include <arm/xscale/ixp425var.h>
56 1.1 scw
57 1.1 scw #include <evbarm/nslu2/nslu2reg.h>
58 1.1 scw
59 1.4 scw #define SLUGLED_FLASH_LEN (hz/8) /* How many ticks an LED stays lit */
60 1.1 scw
61 1.4 scw #define LEDBITS_USB0 (1u << GPIO_LED_DISK1)
62 1.4 scw #define LEDBITS_USB1 (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.4 scw void *sc_tmr_ih;
74 1.4 scw struct callout sc_usb0;
75 1.4 scw void *sc_usb0_ih;
76 1.4 scw struct callout sc_usb1;
77 1.4 scw void *sc_usb1_ih;
78 1.4 scw struct callout sc_usb2;
79 1.4 scw void *sc_usb2_ih;
80 1.1 scw };
81 1.1 scw
82 1.1 scw static int slugled_attached;
83 1.1 scw
84 1.1 scw static void
85 1.1 scw slugled_callout(void *arg)
86 1.1 scw {
87 1.4 scw uint32_t reg, bit;
88 1.4 scw int is;
89 1.4 scw
90 1.4 scw bit = (uint32_t)(uintptr_t)arg;
91 1.4 scw
92 1.4 scw is = disable_interrupts(I32_bit);
93 1.4 scw reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
94 1.4 scw GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg | bit);
95 1.4 scw restore_interrupts(is);
96 1.4 scw }
97 1.4 scw
98 1.4 scw static int
99 1.4 scw slugled_intr0(void *arg)
100 1.4 scw {
101 1.4 scw struct slugled_softc *sc = arg;
102 1.4 scw uint32_t reg;
103 1.4 scw int is;
104 1.4 scw
105 1.4 scw is = disable_interrupts(I32_bit);
106 1.4 scw reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
107 1.4 scw reg &= ~LEDBITS_USB0;
108 1.4 scw GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg);
109 1.4 scw restore_interrupts(is);
110 1.4 scw
111 1.4 scw callout_schedule(&sc->sc_usb0, SLUGLED_FLASH_LEN);
112 1.4 scw
113 1.4 scw return (1);
114 1.4 scw }
115 1.4 scw
116 1.4 scw static int
117 1.4 scw slugled_intr1(void *arg)
118 1.4 scw {
119 1.4 scw struct slugled_softc *sc = arg;
120 1.4 scw uint32_t reg;
121 1.4 scw int is;
122 1.4 scw
123 1.4 scw is = disable_interrupts(I32_bit);
124 1.4 scw reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
125 1.4 scw reg &= ~LEDBITS_USB1;
126 1.4 scw GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg);
127 1.4 scw restore_interrupts(is);
128 1.4 scw
129 1.4 scw callout_schedule(&sc->sc_usb1, SLUGLED_FLASH_LEN);
130 1.4 scw
131 1.4 scw return (1);
132 1.4 scw }
133 1.4 scw
134 1.4 scw static int
135 1.4 scw slugled_intr2(void *arg)
136 1.4 scw {
137 1.1 scw struct slugled_softc *sc = arg;
138 1.4 scw uint32_t reg;
139 1.4 scw int is;
140 1.4 scw
141 1.4 scw is = disable_interrupts(I32_bit);
142 1.4 scw reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
143 1.4 scw reg &= ~(LEDBITS_USB0 | LEDBITS_USB1);
144 1.4 scw GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg);
145 1.4 scw restore_interrupts(is);
146 1.4 scw
147 1.4 scw callout_schedule(&sc->sc_usb2, SLUGLED_FLASH_LEN);
148 1.1 scw
149 1.4 scw return (1);
150 1.4 scw }
151 1.1 scw
152 1.4 scw static int
153 1.4 scw slugled_tmr(void *arg)
154 1.4 scw {
155 1.4 scw struct clockframe *frame = arg;
156 1.4 scw uint32_t reg, bit;
157 1.4 scw int is;
158 1.4 scw
159 1.4 scw if (CLKF_INTR(frame) || sched_whichqs || curlwp)
160 1.4 scw bit = LEDBITS_STATUS;
161 1.4 scw else
162 1.4 scw bit = 0;
163 1.4 scw
164 1.4 scw is = disable_interrupts(I32_bit);
165 1.4 scw reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
166 1.4 scw reg &= ~LEDBITS_STATUS;
167 1.4 scw GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg | bit);
168 1.4 scw restore_interrupts(is);
169 1.1 scw
170 1.4 scw return (1);
171 1.1 scw }
172 1.1 scw
173 1.1 scw static void
174 1.1 scw slugled_shutdown(void *arg)
175 1.1 scw {
176 1.1 scw struct slugled_softc *sc = arg;
177 1.1 scw uint32_t reg;
178 1.1 scw int s;
179 1.1 scw
180 1.4 scw ixp425_intr_disestablish(sc->sc_usb0_ih);
181 1.4 scw ixp425_intr_disestablish(sc->sc_usb1_ih);
182 1.4 scw ixp425_intr_disestablish(sc->sc_tmr_ih);
183 1.4 scw
184 1.4 scw /* Cancel the callouts */
185 1.1 scw s = splsoftclock();
186 1.4 scw callout_stop(&sc->sc_usb0);
187 1.4 scw callout_stop(&sc->sc_usb1);
188 1.1 scw splx(s);
189 1.1 scw
190 1.4 scw /* Turn off the disk LEDs, and set Ready/Status to amber */
191 1.1 scw s = splhigh();
192 1.1 scw reg = GPIO_CONF_READ_4(ixp425_softc,IXP425_GPIO_GPOUTR);
193 1.4 scw reg |= LEDBITS_USB0 | LEDBITS_USB1 | LEDBITS_STATUS | LEDBITS_READY;
194 1.1 scw GPIO_CONF_WRITE_4(ixp425_softc,IXP425_GPIO_GPOUTR, reg);
195 1.1 scw splx(s);
196 1.1 scw }
197 1.1 scw
198 1.1 scw static void
199 1.1 scw slugled_defer(struct device *self)
200 1.1 scw {
201 1.1 scw struct slugled_softc *sc = (struct slugled_softc *) self;
202 1.1 scw struct ixp425_softc *ixsc = ixp425_softc;
203 1.1 scw uint32_t reg;
204 1.4 scw int s;
205 1.1 scw
206 1.1 scw s = splhigh();
207 1.1 scw
208 1.1 scw /* Configure LED GPIO pins as output */
209 1.1 scw reg = GPIO_CONF_READ_4(ixsc, IXP425_GPIO_GPOER);
210 1.4 scw reg &= ~(LEDBITS_USB0 | LEDBITS_USB1);
211 1.1 scw reg &= ~(LEDBITS_READY | LEDBITS_STATUS);
212 1.1 scw GPIO_CONF_WRITE_4(ixsc, IXP425_GPIO_GPOER, reg);
213 1.1 scw
214 1.4 scw /* All LEDs off */
215 1.1 scw reg = GPIO_CONF_READ_4(ixsc, IXP425_GPIO_GPOUTR);
216 1.4 scw reg |= LEDBITS_USB0 | LEDBITS_USB1;
217 1.4 scw reg &= ~(LEDBITS_STATUS | LEDBITS_READY);
218 1.1 scw GPIO_CONF_WRITE_4(ixsc, IXP425_GPIO_GPOUTR, reg);
219 1.1 scw
220 1.1 scw splx(s);
221 1.1 scw
222 1.1 scw if (shutdownhook_establish(slugled_shutdown, sc) == NULL)
223 1.1 scw aprint_error("%s: WARNING - Failed to register shutdown hook\n",
224 1.1 scw sc->sc_dev.dv_xname);
225 1.1 scw
226 1.4 scw callout_init(&sc->sc_usb0);
227 1.4 scw callout_setfunc(&sc->sc_usb0, slugled_callout,
228 1.4 scw (void *)(uintptr_t)LEDBITS_USB0);
229 1.4 scw
230 1.4 scw callout_init(&sc->sc_usb1);
231 1.4 scw callout_setfunc(&sc->sc_usb1, slugled_callout,
232 1.4 scw (void *)(uintptr_t)LEDBITS_USB1);
233 1.4 scw
234 1.4 scw callout_init(&sc->sc_usb2);
235 1.4 scw callout_setfunc(&sc->sc_usb2, slugled_callout,
236 1.4 scw (void *)(uintptr_t)(LEDBITS_USB0 | LEDBITS_USB1));
237 1.4 scw
238 1.4 scw sc->sc_usb0_ih = ixp425_intr_establish(PCI_INT_A, IPL_USB,
239 1.4 scw slugled_intr0, sc);
240 1.4 scw KDASSERT(sc->sc_usb0_ih != NULL);
241 1.4 scw sc->sc_usb1_ih = ixp425_intr_establish(PCI_INT_B, IPL_USB,
242 1.4 scw slugled_intr1, sc);
243 1.4 scw KDASSERT(sc->sc_usb1_ih != NULL);
244 1.4 scw sc->sc_usb2_ih = ixp425_intr_establish(PCI_INT_C, IPL_USB,
245 1.4 scw slugled_intr2, sc);
246 1.4 scw KDASSERT(sc->sc_usb2_ih != NULL);
247 1.4 scw
248 1.4 scw sc->sc_tmr_ih = ixp425_intr_establish(IXP425_INT_TMR0, IPL_CLOCK,
249 1.4 scw slugled_tmr, NULL);
250 1.4 scw KDASSERT(sc->sc_tmr_ih != NULL);
251 1.1 scw }
252 1.1 scw
253 1.1 scw static int
254 1.1 scw slugled_match(struct device *parent, struct cfdata *match, void *aux)
255 1.1 scw {
256 1.1 scw
257 1.1 scw return (slugled_attached == 0);
258 1.1 scw }
259 1.1 scw
260 1.1 scw static void
261 1.1 scw slugled_attach(struct device *parent, struct device *self, void *aux)
262 1.1 scw {
263 1.1 scw
264 1.1 scw aprint_normal(": LED support\n");
265 1.1 scw
266 1.1 scw slugled_attached = 1;
267 1.1 scw
268 1.1 scw config_interrupts(self, slugled_defer);
269 1.1 scw }
270 1.1 scw
271 1.1 scw CFATTACH_DECL(slugled, sizeof(struct slugled_softc),
272 1.1 scw slugled_match, slugled_attach, NULL, NULL);
273