Home | History | Annotate | Line # | Download | only in vr
vrled.c revision 1.6
      1  1.6   thorpej /*	$NetBSD: vrled.c,v 1.6 2002/10/02 05:26:56 thorpej Exp $	*/
      2  1.1      sato 
      3  1.1      sato /*
      4  1.1      sato  * Copyright (c) 2000 SATO Kazumi. All rights reserved.
      5  1.1      sato  *
      6  1.1      sato  * Redistribution and use in source and binary forms, with or without
      7  1.1      sato  * modification, are permitted provided that the following conditions
      8  1.1      sato  * are met:
      9  1.1      sato  * 1. Redistributions of source code must retain the above copyright
     10  1.1      sato  *    notice, this list of conditions and the following disclaimer.
     11  1.1      sato  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1      sato  *    notice, this list of conditions and the following disclaimer in the
     13  1.1      sato  *    documentation and/or other materials provided with the distribution.
     14  1.1      sato  *
     15  1.1      sato  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16  1.1      sato  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  1.1      sato  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18  1.1      sato  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.1      sato  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.1      sato  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21  1.1      sato  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1      sato  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.1      sato  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1      sato  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1      sato  * SUCH DAMAGE.
     26  1.1      sato  */
     27  1.1      sato 
     28  1.1      sato #include <sys/param.h>
     29  1.1      sato #include <sys/systm.h>
     30  1.1      sato #include <sys/device.h>
     31  1.1      sato #include <sys/reboot.h>
     32  1.1      sato 
     33  1.1      sato #include <machine/bus.h>
     34  1.1      sato #include <machine/config_hook.h>
     35  1.1      sato 
     36  1.4  takemura #include <hpcmips/vr/vripif.h>
     37  1.1      sato #include <hpcmips/vr/vrledvar.h>
     38  1.1      sato #include <hpcmips/vr/vrledreg.h>
     39  1.1      sato 
     40  1.1      sato 
     41  1.1      sato #ifdef VRLEDDEBUG
     42  1.1      sato #ifndef VRLEDDEBUG_CONF
     43  1.1      sato #define VRLEDDEBUG_CONF 0
     44  1.1      sato #endif /* VRLEDDEBUG_CONF */
     45  1.1      sato int vrleddebug = VRLEDDEBUG_CONF;
     46  1.1      sato #define DPRINTF(arg) if (vrleddebug) printf arg;
     47  1.1      sato #define VPRINTF(arg) if (bootverbose||vrleddebug) printf arg;
     48  1.1      sato #else /* VRLEDDEBUG */
     49  1.1      sato #define DPRINTF(arg)
     50  1.1      sato #define VPRINTF(arg) if (bootverbose) printf arg;
     51  1.1      sato #endif /* VRLEDDEBUG */
     52  1.1      sato 
     53  1.3       uch static int vrledmatch(struct device *, struct cfdata *, void *);
     54  1.3       uch static void vrledattach(struct device *, struct device *, void *);
     55  1.1      sato 
     56  1.3       uch static void vrled_write(struct vrled_softc *, int, unsigned short);
     57  1.3       uch static unsigned short vrled_read(struct vrled_softc *, int);
     58  1.1      sato 
     59  1.3       uch static void vrled_stop(struct vrled_softc *);
     60  1.3       uch static void vrled_on(struct vrled_softc *);
     61  1.3       uch static void vrled_blink(struct vrled_softc *);
     62  1.3       uch static void vrled_flash(struct vrled_softc *);
     63  1.3       uch static void vrled_change_state(struct vrled_softc *);
     64  1.3       uch static int vrled_event(void *, int, long, void *);
     65  1.1      sato 
     66  1.3       uch int vrled_intr(void *);
     67  1.1      sato 
     68  1.6   thorpej CFATTACH_DECL(vrled, sizeof(struct vrled_softc),
     69  1.6   thorpej     vrledmatch, vrledattach, NULL, NULL);
     70  1.1      sato 
     71  1.1      sato struct vrled_softc *this_led;
     72  1.1      sato 
     73  1.1      sato static inline void
     74  1.3       uch vrled_write(struct vrled_softc *sc, int port, unsigned short val)
     75  1.1      sato {
     76  1.3       uch 
     77  1.1      sato 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
     78  1.1      sato }
     79  1.1      sato 
     80  1.1      sato static inline unsigned short
     81  1.3       uch vrled_read(struct vrled_softc *sc, int port)
     82  1.1      sato {
     83  1.3       uch 
     84  1.3       uch 	return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, port));
     85  1.1      sato }
     86  1.1      sato 
     87  1.1      sato static int
     88  1.3       uch vrledmatch(struct device *parent, struct cfdata *cf, void *aux)
     89  1.1      sato {
     90  1.3       uch 
     91  1.3       uch 	return (1);
     92  1.1      sato }
     93  1.1      sato 
     94  1.1      sato static void
     95  1.3       uch vrledattach(struct device *parent, struct device *self, void *aux)
     96  1.1      sato {
     97  1.1      sato 	struct vrled_softc *sc = (struct vrled_softc *)self;
     98  1.1      sato 	struct vrip_attach_args *va = aux;
     99  1.1      sato 
    100  1.1      sato 	bus_space_tag_t iot = va->va_iot;
    101  1.1      sato 	bus_space_handle_t ioh;
    102  1.1      sato 
    103  1.1      sato 	if (bus_space_map(iot, va->va_addr, 1, 0, &ioh)) {
    104  1.1      sato 		printf(": can't map bus space\n");
    105  1.1      sato 		return;
    106  1.1      sato 	}
    107  1.1      sato 
    108  1.1      sato 	sc->sc_iot = iot;
    109  1.1      sato 	sc->sc_ioh = ioh;
    110  1.1      sato 
    111  1.1      sato 	if (!(sc->sc_handler =
    112  1.4  takemura 	    vrip_intr_establish(va->va_vc, va->va_unit, 0, IPL_TTY,
    113  1.3       uch 		vrled_intr, sc))) {
    114  1.1      sato 		printf (": can't map interrupt line.\n");
    115  1.1      sato 		return;
    116  1.1      sato 	}
    117  1.1      sato 
    118  1.1      sato 	printf("\n");
    119  1.1      sato 	/* clear interrupt status */
    120  1.1      sato 	vrled_write(sc, LEDINT_REG_W, LEDINT_ALL);
    121  1.1      sato 
    122  1.1      sato 	/* basic setup */
    123  1.1      sato 	sc->sc_state_cnt = 1;
    124  1.1      sato 	vrled_write(sc, LEDASTC_REG_W, 1); /* 1time */
    125  1.1      sato 	vrled_write(sc, LEDCNT_REG_W, LEDCNT_AUTOSTOP);
    126  1.1      sato 	vrled_stop(sc);
    127  1.1      sato 
    128  1.2      sato 	sc->sc_hook = config_hook(CONFIG_HOOK_SET,
    129  1.3       uch 	    CONFIG_HOOK_LED, CONFIG_HOOK_SHARE, vrled_event, sc);
    130  1.3       uch 
    131  1.1      sato 	this_led = sc;
    132  1.1      sato }
    133  1.1      sato 
    134  1.1      sato 
    135  1.1      sato /*
    136  1.1      sato  * LED interrupt handler.
    137  1.1      sato  *
    138  1.1      sato  */
    139  1.1      sato int
    140  1.3       uch vrled_intr(void *arg)
    141  1.1      sato {
    142  1.1      sato         struct vrled_softc *sc = arg;
    143  1.1      sato 	unsigned int intstat;
    144  1.1      sato 
    145  1.1      sato 	intstat = vrled_read(sc, LEDINT_REG_W);
    146  1.1      sato 	/* clear interrupt status */
    147  1.1      sato 	vrled_write(sc, LEDINT_REG_W, intstat);
    148  1.1      sato 	if (intstat&LEDINT_AUTOSTOP) {
    149  1.1      sato 		vrled_change_state(sc);
    150  1.1      sato 	}
    151  1.3       uch 	return (0);
    152  1.1      sato }
    153  1.1      sato 
    154  1.1      sato /*
    155  1.1      sato  * LED turn OFF
    156  1.1      sato  *
    157  1.1      sato  */
    158  1.1      sato void
    159  1.3       uch vrled_stop(struct vrled_softc *sc)
    160  1.1      sato {
    161  1.1      sato 	vrled_write(sc, LEDHTS_REG_W, LEDHTS_DIV16SEC);
    162  1.1      sato 	vrled_write(sc, LEDLTS_REG_W, LEDLTS_4SEC);
    163  1.1      sato 	vrled_write(sc, LEDASTC_REG_W, 2); /* 2time */
    164  1.1      sato 	vrled_write(sc, LEDCNT_REG_W, LEDCNT_AUTOSTOP);
    165  1.1      sato 
    166  1.1      sato 	sc->sc_state = LEDOFF;
    167  1.1      sato 	sc->sc_next = LEDOFF;
    168  1.1      sato }
    169  1.1      sato 
    170  1.1      sato /*
    171  1.1      sato  * LED turn ON
    172  1.1      sato  *
    173  1.1      sato  */
    174  1.1      sato void
    175  1.3       uch vrled_on(struct vrled_softc *sc)
    176  1.1      sato {
    177  1.1      sato 	vrled_write(sc, LEDHTS_REG_W, LEDHTS_SEC);
    178  1.1      sato 	vrled_write(sc, LEDLTS_REG_W, LEDLTS_DIV16SEC);
    179  1.1      sato 	vrled_write(sc, LEDASTC_REG_W, 2); /* 2time */
    180  1.1      sato 	vrled_write(sc, LEDCNT_REG_W, LEDCNT_AUTOSTOP|LEDCNT_BLINK);
    181  1.1      sato 
    182  1.1      sato 	sc->sc_state = LEDON;
    183  1.1      sato 	sc->sc_next = LEDON;
    184  1.1      sato }
    185  1.1      sato 
    186  1.1      sato /*
    187  1.1      sato  * LED blink
    188  1.1      sato  *
    189  1.1      sato  */
    190  1.1      sato void
    191  1.3       uch vrled_blink(struct vrled_softc *sc)
    192  1.1      sato {
    193  1.1      sato 	int ledhts;
    194  1.1      sato 	int ledlts;
    195  1.1      sato 
    196  1.1      sato 	switch (sc->sc_next) {
    197  1.1      sato 	case LED1SB:
    198  1.1      sato 		ledhts = LEDHTS_DIV2SEC;
    199  1.1      sato 		ledlts = LEDLTS_DIV2SEC;
    200  1.1      sato 		break;
    201  1.1      sato 	case LED2SB:
    202  1.1      sato 		ledhts = LEDHTS_SEC;
    203  1.1      sato 		ledlts = LEDLTS_SEC;
    204  1.1      sato 		break;
    205  1.1      sato 	default:
    206  1.1      sato 		vrled_stop(sc);
    207  1.1      sato 		return;
    208  1.1      sato 	}
    209  1.1      sato 
    210  1.1      sato 	vrled_write(sc, LEDHTS_REG_W, ledhts);
    211  1.1      sato 	vrled_write(sc, LEDLTS_REG_W, ledlts);
    212  1.1      sato 	vrled_write(sc, LEDASTC_REG_W, 2); /* 2time */
    213  1.1      sato 	vrled_write(sc, LEDCNT_REG_W, LEDCNT_AUTOSTOP);
    214  1.1      sato 	vrled_write(sc, LEDCNT_REG_W, LEDCNT_AUTOSTOP|LEDCNT_BLINK);
    215  1.1      sato 
    216  1.1      sato 	sc->sc_state = sc->sc_next;
    217  1.1      sato }
    218  1.1      sato 
    219  1.1      sato /*
    220  1.1      sato  * LED flash once
    221  1.1      sato  *
    222  1.1      sato  */
    223  1.1      sato void
    224  1.3       uch vrled_flash(struct vrled_softc *sc)
    225  1.1      sato {
    226  1.1      sato 	int ledhts;
    227  1.1      sato 	int ledlts;
    228  1.1      sato 
    229  1.1      sato 	switch (sc->sc_next) {
    230  1.1      sato 	case LED8DIVF:
    231  1.1      sato 		ledhts = LEDHTS_DIV16SEC;
    232  1.1      sato 		ledlts = LEDLTS_DIV16SEC;
    233  1.1      sato 		break;
    234  1.1      sato 	case LED4DIVF:
    235  1.1      sato 		ledhts = LEDHTS_DIV8SEC;
    236  1.1      sato 		ledlts = LEDLTS_DIV8SEC;
    237  1.1      sato 		break;
    238  1.1      sato 	case LED2DIVF:
    239  1.1      sato 		ledhts = LEDHTS_DIV4SEC;
    240  1.1      sato 		ledlts = LEDLTS_DIV4SEC;
    241  1.1      sato 		break;
    242  1.1      sato 	case LED1SF:
    243  1.1      sato 		ledhts = LEDHTS_DIV2SEC;
    244  1.1      sato 		ledlts = LEDLTS_DIV2SEC;
    245  1.1      sato 		break;
    246  1.1      sato 	default:
    247  1.1      sato 		vrled_stop(sc);
    248  1.1      sato 		return;
    249  1.1      sato 	}
    250  1.1      sato 
    251  1.1      sato 	vrled_write(sc, LEDHTS_REG_W, ledhts);
    252  1.1      sato 	vrled_write(sc, LEDLTS_REG_W, ledlts);
    253  1.1      sato 	vrled_write(sc, LEDASTC_REG_W, 2); /* 2time */
    254  1.1      sato 	vrled_write(sc, LEDCNT_REG_W, LEDCNT_AUTOSTOP|LEDCNT_BLINK);
    255  1.1      sato 
    256  1.1      sato 	sc->sc_state = sc->sc_next;
    257  1.1      sato 	sc->sc_next = LEDOFF;
    258  1.1      sato 	sc->sc_state_cnt = 1;
    259  1.1      sato }
    260  1.1      sato 
    261  1.1      sato /*
    262  1.1      sato  * Change LED state
    263  1.1      sato  *
    264  1.1      sato  */
    265  1.1      sato void
    266  1.3       uch vrled_change_state(struct vrled_softc *sc)
    267  1.1      sato {
    268  1.1      sato 
    269  1.1      sato 	switch (sc->sc_next) {
    270  1.1      sato 	case LEDOFF:
    271  1.1      sato 		vrled_stop(sc);
    272  1.1      sato 		break;
    273  1.1      sato 	case LEDON:
    274  1.1      sato 		vrled_on(sc);
    275  1.1      sato 		break;
    276  1.1      sato 	case LED1SB:
    277  1.1      sato 	case LED2SB:
    278  1.1      sato 		vrled_blink(sc);
    279  1.1      sato 		break;
    280  1.1      sato 	case LED8DIVF:
    281  1.1      sato 	case LED4DIVF:
    282  1.1      sato 	case LED2DIVF:
    283  1.1      sato 	case LED1SF:
    284  1.1      sato 		vrled_flash(sc);
    285  1.1      sato 		break;
    286  1.1      sato 	default:
    287  1.1      sato 		vrled_stop(sc);
    288  1.1      sato 		break;
    289  1.1      sato 	}
    290  1.1      sato }
    291  1.1      sato 
    292  1.1      sato /*
    293  1.1      sato  * Set LED state
    294  1.1      sato  *
    295  1.1      sato  */
    296  1.1      sato void
    297  1.3       uch vrled_set_state(struct vrled_softc *sc, vrled_status state)
    298  1.1      sato {
    299  1.1      sato 
    300  1.1      sato 	int ledstate;
    301  1.1      sato 
    302  1.1      sato 	ledstate = vrled_read(sc, LEDCNT_REG_W);
    303  1.1      sato 	if (ledstate&LEDCNT_BLINK) { /* currently processing */
    304  1.1      sato 		if (sc->sc_next == state)
    305  1.1      sato 			sc->sc_state_cnt++;
    306  1.1      sato 		switch (sc->sc_next) {
    307  1.1      sato 		case LEDOFF:
    308  1.1      sato 		case LEDON:
    309  1.1      sato 			sc->sc_next = state;
    310  1.1      sato 			break;
    311  1.1      sato 		case LED8DIVF:
    312  1.1      sato 		case LED4DIVF:
    313  1.1      sato 		case LED2DIVF:
    314  1.1      sato 		case LED1SF:
    315  1.1      sato 			switch (state) {
    316  1.1      sato 			case LEDOFF:
    317  1.1      sato 			case LED8DIVF:
    318  1.1      sato 			case LED4DIVF:
    319  1.1      sato 			case LED2DIVF:
    320  1.1      sato 			case LED1SF:
    321  1.1      sato 				sc->sc_next = state;
    322  1.1      sato 				break;
    323  1.1      sato 			default:
    324  1.1      sato 				break;
    325  1.1      sato 			}
    326  1.1      sato 			break;
    327  1.1      sato 		case LED1SB:
    328  1.1      sato 		case LED2SB:
    329  1.1      sato 			switch (state) {
    330  1.1      sato 			case LEDOFF:
    331  1.1      sato 			case LEDON:
    332  1.1      sato 			case LED1SB:
    333  1.1      sato 			case LED2SB:
    334  1.1      sato 				sc->sc_next = state;
    335  1.1      sato 				break;
    336  1.1      sato 			default:
    337  1.1      sato 				break;
    338  1.1      sato 			}
    339  1.1      sato 			break;
    340  1.1      sato 		default:
    341  1.1      sato 			sc->sc_next = LEDOFF;
    342  1.1      sato 			break;
    343  1.1      sato 		}
    344  1.1      sato 		return;
    345  1.1      sato 	}
    346  1.1      sato 	sc->sc_next = state;
    347  1.1      sato 	vrled_change_state(sc);
    348  1.1      sato }
    349  1.1      sato 
    350  1.1      sato /*
    351  1.1      sato  * LED config hook events
    352  1.1      sato  *
    353  1.1      sato  */
    354  1.1      sato int
    355  1.3       uch vrled_event(void *ctx, int type, long id, void *msg)
    356  1.1      sato {
    357  1.1      sato 	struct vrled_softc *sc = (struct vrled_softc *)ctx;
    358  1.2      sato         int why =*(int *)msg;
    359  1.1      sato 
    360  1.2      sato 	if (type != CONFIG_HOOK_SET
    361  1.3       uch 	    || id != CONFIG_HOOK_LED)
    362  1.3       uch 		return (1);
    363  1.2      sato 	if (msg == NULL)
    364  1.3       uch 		return (1);
    365  1.1      sato 
    366  1.1      sato         switch (why) {
    367  1.2      sato         case CONFIG_HOOK_LED_OFF:
    368  1.1      sato 		vrled_set_state(sc, LEDOFF);
    369  1.1      sato                 break;
    370  1.2      sato         case CONFIG_HOOK_LED_ON:
    371  1.1      sato 		vrled_set_state(sc, LEDON);
    372  1.1      sato                 break;
    373  1.2      sato         case CONFIG_HOOK_LED_FLASH:
    374  1.1      sato 		vrled_set_state(sc, LED8DIVF);
    375  1.1      sato                 break;
    376  1.2      sato         case CONFIG_HOOK_LED_FLASH2:
    377  1.1      sato 		vrled_set_state(sc, LED4DIVF);
    378  1.1      sato                 break;
    379  1.2      sato         case CONFIG_HOOK_LED_FLASH5:
    380  1.1      sato 		vrled_set_state(sc, LED2DIVF);
    381  1.1      sato                 break;
    382  1.2      sato         case CONFIG_HOOK_LED_BLINK:
    383  1.1      sato 		vrled_set_state(sc, LED1SB);
    384  1.1      sato                 break;
    385  1.2      sato         case CONFIG_HOOK_LED_BLINK2:
    386  1.1      sato 		vrled_set_state(sc, LED2SB);
    387  1.1      sato                 break;
    388  1.1      sato 	default:
    389  1.1      sato 		vrled_set_state(sc, LEDOFF);
    390  1.1      sato         }
    391  1.1      sato         return (0);
    392  1.1      sato }
    393  1.1      sato 
    394  1.1      sato /* end */
    395