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