Home | History | Annotate | Line # | Download | only in nslu2
nslu2_leds.c revision 1.6
      1 /*	$NetBSD: nslu2_leds.c,v 1.6 2007/07/09 20:52:11 ad 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.6 2007/07/09 20:52:11 ad 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_curcpu_runnable_p() ||
    160 	    (curlwp != NULL && curlwp != curcpu()->ci_data.cpu_idlelwp))
    161 		bit = LEDBITS_STATUS;
    162 	else
    163 		bit = 0;
    164 
    165 	is = disable_interrupts(I32_bit);
    166 	reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
    167 	reg &= ~LEDBITS_STATUS;
    168 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg | bit);
    169 	restore_interrupts(is);
    170 
    171 	return (1);
    172 }
    173 
    174 static void
    175 slugled_shutdown(void *arg)
    176 {
    177 	struct slugled_softc *sc = arg;
    178 	uint32_t reg;
    179 	int s;
    180 
    181 	ixp425_intr_disestablish(sc->sc_usb0_ih);
    182 	ixp425_intr_disestablish(sc->sc_usb1_ih);
    183 	ixp425_intr_disestablish(sc->sc_tmr_ih);
    184 
    185 	/* Cancel the callouts */
    186 	s = splsoftclock();
    187 	callout_stop(&sc->sc_usb0);
    188 	callout_stop(&sc->sc_usb1);
    189 	splx(s);
    190 
    191 	/* Turn off the disk LEDs, and set Ready/Status to amber */
    192 	s = splhigh();
    193 	reg = GPIO_CONF_READ_4(ixp425_softc,IXP425_GPIO_GPOUTR);
    194 	reg |= LEDBITS_USB0 | LEDBITS_USB1 | LEDBITS_STATUS | LEDBITS_READY;
    195 	GPIO_CONF_WRITE_4(ixp425_softc,IXP425_GPIO_GPOUTR, reg);
    196 	splx(s);
    197 }
    198 
    199 static void
    200 slugled_defer(struct device *self)
    201 {
    202 	struct slugled_softc *sc = (struct slugled_softc *) self;
    203 	struct ixp425_softc *ixsc = ixp425_softc;
    204 	uint32_t reg;
    205 	int s;
    206 
    207 	s = splhigh();
    208 
    209 	/* Configure LED GPIO pins as output */
    210 	reg = GPIO_CONF_READ_4(ixsc, IXP425_GPIO_GPOER);
    211 	reg &= ~(LEDBITS_USB0 | LEDBITS_USB1);
    212 	reg &= ~(LEDBITS_READY | LEDBITS_STATUS);
    213 	GPIO_CONF_WRITE_4(ixsc, IXP425_GPIO_GPOER, reg);
    214 
    215 	/* All LEDs off */
    216 	reg = GPIO_CONF_READ_4(ixsc, IXP425_GPIO_GPOUTR);
    217 	reg |= LEDBITS_USB0 | LEDBITS_USB1;
    218 	reg &= ~(LEDBITS_STATUS | LEDBITS_READY);
    219 	GPIO_CONF_WRITE_4(ixsc, IXP425_GPIO_GPOUTR, reg);
    220 
    221 	splx(s);
    222 
    223 	if (shutdownhook_establish(slugled_shutdown, sc) == NULL)
    224 		aprint_error("%s: WARNING - Failed to register shutdown hook\n",
    225 		    sc->sc_dev.dv_xname);
    226 
    227 	callout_init(&sc->sc_usb0, 0);
    228 	callout_setfunc(&sc->sc_usb0, slugled_callout,
    229 	    (void *)(uintptr_t)LEDBITS_USB0);
    230 
    231 	callout_init(&sc->sc_usb1, 0);
    232 	callout_setfunc(&sc->sc_usb1, slugled_callout,
    233 	    (void *)(uintptr_t)LEDBITS_USB1);
    234 
    235 	callout_init(&sc->sc_usb2, 0);
    236 	callout_setfunc(&sc->sc_usb2, slugled_callout,
    237 	    (void *)(uintptr_t)(LEDBITS_USB0 | LEDBITS_USB1));
    238 
    239 	sc->sc_usb0_ih = ixp425_intr_establish(PCI_INT_A, IPL_USB,
    240 	    slugled_intr0, sc);
    241 	KDASSERT(sc->sc_usb0_ih != NULL);
    242 	sc->sc_usb1_ih = ixp425_intr_establish(PCI_INT_B, IPL_USB,
    243 	    slugled_intr1, sc);
    244 	KDASSERT(sc->sc_usb1_ih != NULL);
    245 	sc->sc_usb2_ih = ixp425_intr_establish(PCI_INT_C, IPL_USB,
    246 	    slugled_intr2, sc);
    247 	KDASSERT(sc->sc_usb2_ih != NULL);
    248 
    249 	sc->sc_tmr_ih = ixp425_intr_establish(IXP425_INT_TMR0, IPL_CLOCK,
    250 	    slugled_tmr, NULL);
    251 	KDASSERT(sc->sc_tmr_ih != NULL);
    252 }
    253 
    254 static int
    255 slugled_match(struct device *parent, struct cfdata *match, void *aux)
    256 {
    257 
    258 	return (slugled_attached == 0);
    259 }
    260 
    261 static void
    262 slugled_attach(struct device *parent, struct device *self, void *aux)
    263 {
    264 
    265 	aprint_normal(": LED support\n");
    266 
    267 	slugled_attached = 1;
    268 
    269 	config_interrupts(self, slugled_defer);
    270 }
    271 
    272 CFATTACH_DECL(slugled, sizeof(struct slugled_softc),
    273     slugled_match, slugled_attach, NULL, NULL);
    274