Home | History | Annotate | Line # | Download | only in nslu2
      1 /*	$NetBSD: nslu2_leds.c,v 1.10 2013/08/19 22:26:09 matt 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.10 2013/08/19 22:26:09 matt 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 #include <sys/intr.h>
     42 #include <sys/cpu.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 	void *sc_tmr_ih;
     66 	struct callout sc_usb0;
     67 	void *sc_usb0_ih;
     68 	struct callout sc_usb1;
     69 	void *sc_usb1_ih;
     70 	struct callout sc_usb2;
     71 	void *sc_usb2_ih;
     72 };
     73 
     74 static int slugled_attached;
     75 
     76 static void
     77 slugled_callout(void *arg)
     78 {
     79 	uint32_t reg, bit;
     80 	int is;
     81 
     82 	bit = (uint32_t)(uintptr_t)arg;
     83 
     84 	is = disable_interrupts(I32_bit);
     85 	reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
     86 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg | bit);
     87 	restore_interrupts(is);
     88 }
     89 
     90 static int
     91 slugled_intr0(void *arg)
     92 {
     93 	struct slugled_softc *sc = arg;
     94 	uint32_t reg;
     95 	int is;
     96 
     97 	is = disable_interrupts(I32_bit);
     98 	reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
     99 	reg &= ~LEDBITS_USB0;
    100 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg);
    101 	restore_interrupts(is);
    102 
    103 	callout_schedule(&sc->sc_usb0, SLUGLED_FLASH_LEN);
    104 
    105 	return (1);
    106 }
    107 
    108 static int
    109 slugled_intr1(void *arg)
    110 {
    111 	struct slugled_softc *sc = arg;
    112 	uint32_t reg;
    113 	int is;
    114 
    115 	is = disable_interrupts(I32_bit);
    116 	reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
    117 	reg &= ~LEDBITS_USB1;
    118 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg);
    119 	restore_interrupts(is);
    120 
    121 	callout_schedule(&sc->sc_usb1, SLUGLED_FLASH_LEN);
    122 
    123 	return (1);
    124 }
    125 
    126 static int
    127 slugled_intr2(void *arg)
    128 {
    129 	struct slugled_softc *sc = arg;
    130 	uint32_t reg;
    131 	int is;
    132 
    133 	is = disable_interrupts(I32_bit);
    134 	reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
    135 	reg &= ~(LEDBITS_USB0 | LEDBITS_USB1);
    136 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg);
    137 	restore_interrupts(is);
    138 
    139 	callout_schedule(&sc->sc_usb2, SLUGLED_FLASH_LEN);
    140 
    141 	return (1);
    142 }
    143 
    144 static int
    145 slugled_tmr(void *arg)
    146 {
    147 	struct clockframe *cf = arg;
    148 	uint32_t reg, bit;
    149 	int is;
    150 
    151 	if (CLKF_INTR(cf) || sched_curcpu_runnable_p() ||
    152 	    (curlwp != NULL && curlwp != curcpu()->ci_data.cpu_idlelwp))
    153 		bit = LEDBITS_STATUS;
    154 	else
    155 		bit = 0;
    156 
    157 	is = disable_interrupts(I32_bit);
    158 	reg = GPIO_CONF_READ_4(ixp425_softc, IXP425_GPIO_GPOUTR);
    159 	reg &= ~LEDBITS_STATUS;
    160 	GPIO_CONF_WRITE_4(ixp425_softc, IXP425_GPIO_GPOUTR, reg | bit);
    161 	restore_interrupts(is);
    162 
    163 	return (1);
    164 }
    165 
    166 static void
    167 slugled_shutdown(void *arg)
    168 {
    169 	struct slugled_softc *sc = arg;
    170 	uint32_t reg;
    171 	int s;
    172 
    173 	ixp425_intr_disestablish(sc->sc_usb0_ih);
    174 	ixp425_intr_disestablish(sc->sc_usb1_ih);
    175 	ixp425_intr_disestablish(sc->sc_tmr_ih);
    176 
    177 	/* Cancel the callouts */
    178 	s = splsoftclock();
    179 	callout_stop(&sc->sc_usb0);
    180 	callout_stop(&sc->sc_usb1);
    181 	splx(s);
    182 
    183 	/* Turn off the disk LEDs, and set Ready/Status to amber */
    184 	s = splhigh();
    185 	reg = GPIO_CONF_READ_4(ixp425_softc,IXP425_GPIO_GPOUTR);
    186 	reg |= LEDBITS_USB0 | LEDBITS_USB1 | LEDBITS_STATUS | LEDBITS_READY;
    187 	GPIO_CONF_WRITE_4(ixp425_softc,IXP425_GPIO_GPOUTR, reg);
    188 	splx(s);
    189 }
    190 
    191 static void
    192 slugled_defer(device_t self)
    193 {
    194 	struct slugled_softc *sc = device_private(self);
    195 	struct ixp425_softc *ixsc = ixp425_softc;
    196 	uint32_t reg;
    197 	int s;
    198 
    199 	s = splhigh();
    200 
    201 	/* Configure LED GPIO pins as output */
    202 	reg = GPIO_CONF_READ_4(ixsc, IXP425_GPIO_GPOER);
    203 	reg &= ~(LEDBITS_USB0 | LEDBITS_USB1);
    204 	reg &= ~(LEDBITS_READY | LEDBITS_STATUS);
    205 	GPIO_CONF_WRITE_4(ixsc, IXP425_GPIO_GPOER, reg);
    206 
    207 	/* All LEDs off */
    208 	reg = GPIO_CONF_READ_4(ixsc, IXP425_GPIO_GPOUTR);
    209 	reg |= LEDBITS_USB0 | LEDBITS_USB1;
    210 	reg &= ~(LEDBITS_STATUS | LEDBITS_READY);
    211 	GPIO_CONF_WRITE_4(ixsc, IXP425_GPIO_GPOUTR, reg);
    212 
    213 	splx(s);
    214 
    215 	if (shutdownhook_establish(slugled_shutdown, sc) == NULL)
    216 		aprint_error_dev(self, "WARNING - Failed to register shutdown hook\n");
    217 
    218 	callout_init(&sc->sc_usb0, 0);
    219 	callout_setfunc(&sc->sc_usb0, slugled_callout,
    220 	    (void *)(uintptr_t)LEDBITS_USB0);
    221 
    222 	callout_init(&sc->sc_usb1, 0);
    223 	callout_setfunc(&sc->sc_usb1, slugled_callout,
    224 	    (void *)(uintptr_t)LEDBITS_USB1);
    225 
    226 	callout_init(&sc->sc_usb2, 0);
    227 	callout_setfunc(&sc->sc_usb2, slugled_callout,
    228 	    (void *)(uintptr_t)(LEDBITS_USB0 | LEDBITS_USB1));
    229 
    230 	sc->sc_usb0_ih = ixp425_intr_establish(PCI_INT_A, IPL_USB,
    231 	    slugled_intr0, sc);
    232 	KDASSERT(sc->sc_usb0_ih != NULL);
    233 	sc->sc_usb1_ih = ixp425_intr_establish(PCI_INT_B, IPL_USB,
    234 	    slugled_intr1, sc);
    235 	KDASSERT(sc->sc_usb1_ih != NULL);
    236 	sc->sc_usb2_ih = ixp425_intr_establish(PCI_INT_C, IPL_USB,
    237 	    slugled_intr2, sc);
    238 	KDASSERT(sc->sc_usb2_ih != NULL);
    239 
    240 	sc->sc_tmr_ih = ixp425_intr_establish(IXP425_INT_TMR0, IPL_CLOCK,
    241 	    slugled_tmr, NULL);
    242 	KDASSERT(sc->sc_tmr_ih != NULL);
    243 }
    244 
    245 static int
    246 slugled_match(device_t parent, cfdata_t cf, void *aux)
    247 {
    248 
    249 	return (slugled_attached == 0);
    250 }
    251 
    252 static void
    253 slugled_attach(device_t parent, device_t self, void *aux)
    254 {
    255 
    256 	aprint_normal(": LED support\n");
    257 
    258 	slugled_attached = 1;
    259 
    260 	config_interrupts(self, slugled_defer);
    261 }
    262 
    263 CFATTACH_DECL_NEW(slugled, sizeof(struct slugled_softc),
    264     slugled_match, slugled_attach, NULL, NULL);
    265