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