Home | History | Annotate | Line # | Download | only in vr
vrled.c revision 1.2.2.1
      1  1.2.2.1  fvdl /*	$NetBSD: vrled.c,v 1.2.2.1 2001/10/01 12:39:27 fvdl 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.1  sato #include <hpcmips/vr/vripvar.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.2.2.1  fvdl static int vrledmatch(struct device *, struct cfdata *, void *);
     54  1.2.2.1  fvdl static void vrledattach(struct device *, struct device *, void *);
     55      1.1  sato 
     56  1.2.2.1  fvdl static void vrled_write(struct vrled_softc *, int, unsigned short);
     57  1.2.2.1  fvdl static unsigned short vrled_read(struct vrled_softc *, int);
     58      1.1  sato 
     59  1.2.2.1  fvdl static void vrled_stop(struct vrled_softc *);
     60  1.2.2.1  fvdl static void vrled_on(struct vrled_softc *);
     61  1.2.2.1  fvdl static void vrled_blink(struct vrled_softc *);
     62  1.2.2.1  fvdl static void vrled_flash(struct vrled_softc *);
     63  1.2.2.1  fvdl static void vrled_change_state(struct vrled_softc *);
     64  1.2.2.1  fvdl static int vrled_event(void *, int, long, void *);
     65      1.1  sato 
     66  1.2.2.1  fvdl int vrled_intr(void *);
     67      1.1  sato 
     68      1.1  sato struct cfattach vrled_ca = {
     69      1.1  sato 	sizeof(struct vrled_softc), vrledmatch, vrledattach
     70      1.1  sato };
     71      1.1  sato 
     72      1.1  sato struct vrled_softc *this_led;
     73      1.1  sato 
     74      1.1  sato static inline void
     75  1.2.2.1  fvdl vrled_write(struct vrled_softc *sc, int port, unsigned short val)
     76      1.1  sato {
     77  1.2.2.1  fvdl 
     78      1.1  sato 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
     79      1.1  sato }
     80      1.1  sato 
     81      1.1  sato static inline unsigned short
     82  1.2.2.1  fvdl vrled_read(struct vrled_softc *sc, int port)
     83      1.1  sato {
     84  1.2.2.1  fvdl 
     85  1.2.2.1  fvdl 	return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, port));
     86      1.1  sato }
     87      1.1  sato 
     88      1.1  sato static int
     89  1.2.2.1  fvdl vrledmatch(struct device *parent, struct cfdata *cf, void *aux)
     90      1.1  sato {
     91  1.2.2.1  fvdl 
     92  1.2.2.1  fvdl 	return (1);
     93      1.1  sato }
     94      1.1  sato 
     95      1.1  sato static void
     96  1.2.2.1  fvdl vrledattach(struct device *parent, struct device *self, void *aux)
     97      1.1  sato {
     98      1.1  sato 	struct vrled_softc *sc = (struct vrled_softc *)self;
     99      1.1  sato 	struct vrip_attach_args *va = aux;
    100      1.1  sato 
    101      1.1  sato 	bus_space_tag_t iot = va->va_iot;
    102      1.1  sato 	bus_space_handle_t ioh;
    103      1.1  sato 
    104      1.1  sato 	if (bus_space_map(iot, va->va_addr, 1, 0, &ioh)) {
    105      1.1  sato 		printf(": can't map bus space\n");
    106      1.1  sato 		return;
    107      1.1  sato 	}
    108      1.1  sato 
    109      1.1  sato 	sc->sc_iot = iot;
    110      1.1  sato 	sc->sc_ioh = ioh;
    111      1.1  sato 
    112      1.1  sato 	if (!(sc->sc_handler =
    113  1.2.2.1  fvdl 	    vrip_intr_establish(va->va_vc, va->va_intr, IPL_TTY,
    114  1.2.2.1  fvdl 		vrled_intr, sc))) {
    115      1.1  sato 		printf (": can't map interrupt line.\n");
    116      1.1  sato 		return;
    117      1.1  sato 	}
    118      1.1  sato 
    119      1.1  sato 	printf("\n");
    120      1.1  sato 	/* clear interrupt status */
    121      1.1  sato 	vrled_write(sc, LEDINT_REG_W, LEDINT_ALL);
    122      1.1  sato 
    123      1.1  sato 	/* basic setup */
    124      1.1  sato 	sc->sc_state_cnt = 1;
    125      1.1  sato 	vrled_write(sc, LEDASTC_REG_W, 1); /* 1time */
    126      1.1  sato 	vrled_write(sc, LEDCNT_REG_W, LEDCNT_AUTOSTOP);
    127      1.1  sato 	vrled_stop(sc);
    128      1.1  sato 
    129      1.2  sato 	sc->sc_hook = config_hook(CONFIG_HOOK_SET,
    130  1.2.2.1  fvdl 	    CONFIG_HOOK_LED, CONFIG_HOOK_SHARE, vrled_event, sc);
    131  1.2.2.1  fvdl 
    132      1.1  sato 	this_led = sc;
    133      1.1  sato }
    134      1.1  sato 
    135      1.1  sato 
    136      1.1  sato /*
    137      1.1  sato  * LED interrupt handler.
    138      1.1  sato  *
    139      1.1  sato  */
    140      1.1  sato int
    141  1.2.2.1  fvdl vrled_intr(void *arg)
    142      1.1  sato {
    143      1.1  sato         struct vrled_softc *sc = arg;
    144      1.1  sato 	unsigned int intstat;
    145      1.1  sato 
    146      1.1  sato 	intstat = vrled_read(sc, LEDINT_REG_W);
    147      1.1  sato 	/* clear interrupt status */
    148      1.1  sato 	vrled_write(sc, LEDINT_REG_W, intstat);
    149      1.1  sato 	if (intstat&LEDINT_AUTOSTOP) {
    150      1.1  sato 		vrled_change_state(sc);
    151      1.1  sato 	}
    152  1.2.2.1  fvdl 	return (0);
    153      1.1  sato }
    154      1.1  sato 
    155      1.1  sato /*
    156      1.1  sato  * LED turn OFF
    157      1.1  sato  *
    158      1.1  sato  */
    159      1.1  sato void
    160  1.2.2.1  fvdl vrled_stop(struct vrled_softc *sc)
    161      1.1  sato {
    162      1.1  sato 	vrled_write(sc, LEDHTS_REG_W, LEDHTS_DIV16SEC);
    163      1.1  sato 	vrled_write(sc, LEDLTS_REG_W, LEDLTS_4SEC);
    164      1.1  sato 	vrled_write(sc, LEDASTC_REG_W, 2); /* 2time */
    165      1.1  sato 	vrled_write(sc, LEDCNT_REG_W, LEDCNT_AUTOSTOP);
    166      1.1  sato 
    167      1.1  sato 	sc->sc_state = LEDOFF;
    168      1.1  sato 	sc->sc_next = LEDOFF;
    169      1.1  sato }
    170      1.1  sato 
    171      1.1  sato /*
    172      1.1  sato  * LED turn ON
    173      1.1  sato  *
    174      1.1  sato  */
    175      1.1  sato void
    176  1.2.2.1  fvdl vrled_on(struct vrled_softc *sc)
    177      1.1  sato {
    178      1.1  sato 	vrled_write(sc, LEDHTS_REG_W, LEDHTS_SEC);
    179      1.1  sato 	vrled_write(sc, LEDLTS_REG_W, LEDLTS_DIV16SEC);
    180      1.1  sato 	vrled_write(sc, LEDASTC_REG_W, 2); /* 2time */
    181      1.1  sato 	vrled_write(sc, LEDCNT_REG_W, LEDCNT_AUTOSTOP|LEDCNT_BLINK);
    182      1.1  sato 
    183      1.1  sato 	sc->sc_state = LEDON;
    184      1.1  sato 	sc->sc_next = LEDON;
    185      1.1  sato }
    186      1.1  sato 
    187      1.1  sato /*
    188      1.1  sato  * LED blink
    189      1.1  sato  *
    190      1.1  sato  */
    191      1.1  sato void
    192  1.2.2.1  fvdl vrled_blink(struct vrled_softc *sc)
    193      1.1  sato {
    194      1.1  sato 	int ledhts;
    195      1.1  sato 	int ledlts;
    196      1.1  sato 
    197      1.1  sato 	switch (sc->sc_next) {
    198      1.1  sato 	case LED1SB:
    199      1.1  sato 		ledhts = LEDHTS_DIV2SEC;
    200      1.1  sato 		ledlts = LEDLTS_DIV2SEC;
    201      1.1  sato 		break;
    202      1.1  sato 	case LED2SB:
    203      1.1  sato 		ledhts = LEDHTS_SEC;
    204      1.1  sato 		ledlts = LEDLTS_SEC;
    205      1.1  sato 		break;
    206      1.1  sato 	default:
    207      1.1  sato 		vrled_stop(sc);
    208      1.1  sato 		return;
    209      1.1  sato 	}
    210      1.1  sato 
    211      1.1  sato 	vrled_write(sc, LEDHTS_REG_W, ledhts);
    212      1.1  sato 	vrled_write(sc, LEDLTS_REG_W, ledlts);
    213      1.1  sato 	vrled_write(sc, LEDASTC_REG_W, 2); /* 2time */
    214      1.1  sato 	vrled_write(sc, LEDCNT_REG_W, LEDCNT_AUTOSTOP);
    215      1.1  sato 	vrled_write(sc, LEDCNT_REG_W, LEDCNT_AUTOSTOP|LEDCNT_BLINK);
    216      1.1  sato 
    217      1.1  sato 	sc->sc_state = sc->sc_next;
    218      1.1  sato }
    219      1.1  sato 
    220      1.1  sato /*
    221      1.1  sato  * LED flash once
    222      1.1  sato  *
    223      1.1  sato  */
    224      1.1  sato void
    225  1.2.2.1  fvdl vrled_flash(struct vrled_softc *sc)
    226      1.1  sato {
    227      1.1  sato 	int ledhts;
    228      1.1  sato 	int ledlts;
    229      1.1  sato 
    230      1.1  sato 	switch (sc->sc_next) {
    231      1.1  sato 	case LED8DIVF:
    232      1.1  sato 		ledhts = LEDHTS_DIV16SEC;
    233      1.1  sato 		ledlts = LEDLTS_DIV16SEC;
    234      1.1  sato 		break;
    235      1.1  sato 	case LED4DIVF:
    236      1.1  sato 		ledhts = LEDHTS_DIV8SEC;
    237      1.1  sato 		ledlts = LEDLTS_DIV8SEC;
    238      1.1  sato 		break;
    239      1.1  sato 	case LED2DIVF:
    240      1.1  sato 		ledhts = LEDHTS_DIV4SEC;
    241      1.1  sato 		ledlts = LEDLTS_DIV4SEC;
    242      1.1  sato 		break;
    243      1.1  sato 	case LED1SF:
    244      1.1  sato 		ledhts = LEDHTS_DIV2SEC;
    245      1.1  sato 		ledlts = LEDLTS_DIV2SEC;
    246      1.1  sato 		break;
    247      1.1  sato 	default:
    248      1.1  sato 		vrled_stop(sc);
    249      1.1  sato 		return;
    250      1.1  sato 	}
    251      1.1  sato 
    252      1.1  sato 	vrled_write(sc, LEDHTS_REG_W, ledhts);
    253      1.1  sato 	vrled_write(sc, LEDLTS_REG_W, ledlts);
    254      1.1  sato 	vrled_write(sc, LEDASTC_REG_W, 2); /* 2time */
    255      1.1  sato 	vrled_write(sc, LEDCNT_REG_W, LEDCNT_AUTOSTOP|LEDCNT_BLINK);
    256      1.1  sato 
    257      1.1  sato 	sc->sc_state = sc->sc_next;
    258      1.1  sato 	sc->sc_next = LEDOFF;
    259      1.1  sato 	sc->sc_state_cnt = 1;
    260      1.1  sato }
    261      1.1  sato 
    262      1.1  sato /*
    263      1.1  sato  * Change LED state
    264      1.1  sato  *
    265      1.1  sato  */
    266      1.1  sato void
    267  1.2.2.1  fvdl vrled_change_state(struct vrled_softc *sc)
    268      1.1  sato {
    269      1.1  sato 
    270      1.1  sato 	switch (sc->sc_next) {
    271      1.1  sato 	case LEDOFF:
    272      1.1  sato 		vrled_stop(sc);
    273      1.1  sato 		break;
    274      1.1  sato 	case LEDON:
    275      1.1  sato 		vrled_on(sc);
    276      1.1  sato 		break;
    277      1.1  sato 	case LED1SB:
    278      1.1  sato 	case LED2SB:
    279      1.1  sato 		vrled_blink(sc);
    280      1.1  sato 		break;
    281      1.1  sato 	case LED8DIVF:
    282      1.1  sato 	case LED4DIVF:
    283      1.1  sato 	case LED2DIVF:
    284      1.1  sato 	case LED1SF:
    285      1.1  sato 		vrled_flash(sc);
    286      1.1  sato 		break;
    287      1.1  sato 	default:
    288      1.1  sato 		vrled_stop(sc);
    289      1.1  sato 		break;
    290      1.1  sato 	}
    291      1.1  sato }
    292      1.1  sato 
    293      1.1  sato /*
    294      1.1  sato  * Set LED state
    295      1.1  sato  *
    296      1.1  sato  */
    297      1.1  sato void
    298  1.2.2.1  fvdl vrled_set_state(struct vrled_softc *sc, vrled_status state)
    299      1.1  sato {
    300      1.1  sato 
    301      1.1  sato 	int ledstate;
    302      1.1  sato 
    303      1.1  sato 	ledstate = vrled_read(sc, LEDCNT_REG_W);
    304      1.1  sato 	if (ledstate&LEDCNT_BLINK) { /* currently processing */
    305      1.1  sato 		if (sc->sc_next == state)
    306      1.1  sato 			sc->sc_state_cnt++;
    307      1.1  sato 		switch (sc->sc_next) {
    308      1.1  sato 		case LEDOFF:
    309      1.1  sato 		case LEDON:
    310      1.1  sato 			sc->sc_next = state;
    311      1.1  sato 			break;
    312      1.1  sato 		case LED8DIVF:
    313      1.1  sato 		case LED4DIVF:
    314      1.1  sato 		case LED2DIVF:
    315      1.1  sato 		case LED1SF:
    316      1.1  sato 			switch (state) {
    317      1.1  sato 			case LEDOFF:
    318      1.1  sato 			case LED8DIVF:
    319      1.1  sato 			case LED4DIVF:
    320      1.1  sato 			case LED2DIVF:
    321      1.1  sato 			case LED1SF:
    322      1.1  sato 				sc->sc_next = state;
    323      1.1  sato 				break;
    324      1.1  sato 			default:
    325      1.1  sato 				break;
    326      1.1  sato 			}
    327      1.1  sato 			break;
    328      1.1  sato 		case LED1SB:
    329      1.1  sato 		case LED2SB:
    330      1.1  sato 			switch (state) {
    331      1.1  sato 			case LEDOFF:
    332      1.1  sato 			case LEDON:
    333      1.1  sato 			case LED1SB:
    334      1.1  sato 			case LED2SB:
    335      1.1  sato 				sc->sc_next = state;
    336      1.1  sato 				break;
    337      1.1  sato 			default:
    338      1.1  sato 				break;
    339      1.1  sato 			}
    340      1.1  sato 			break;
    341      1.1  sato 		default:
    342      1.1  sato 			sc->sc_next = LEDOFF;
    343      1.1  sato 			break;
    344      1.1  sato 		}
    345      1.1  sato 		return;
    346      1.1  sato 	}
    347      1.1  sato 	sc->sc_next = state;
    348      1.1  sato 	vrled_change_state(sc);
    349      1.1  sato }
    350      1.1  sato 
    351      1.1  sato /*
    352      1.1  sato  * LED config hook events
    353      1.1  sato  *
    354      1.1  sato  */
    355      1.1  sato int
    356  1.2.2.1  fvdl vrled_event(void *ctx, int type, long id, void *msg)
    357      1.1  sato {
    358      1.1  sato 	struct vrled_softc *sc = (struct vrled_softc *)ctx;
    359      1.2  sato         int why =*(int *)msg;
    360      1.1  sato 
    361      1.2  sato 	if (type != CONFIG_HOOK_SET
    362  1.2.2.1  fvdl 	    || id != CONFIG_HOOK_LED)
    363  1.2.2.1  fvdl 		return (1);
    364      1.2  sato 	if (msg == NULL)
    365  1.2.2.1  fvdl 		return (1);
    366      1.1  sato 
    367      1.1  sato         switch (why) {
    368      1.2  sato         case CONFIG_HOOK_LED_OFF:
    369      1.1  sato 		vrled_set_state(sc, LEDOFF);
    370      1.1  sato                 break;
    371      1.2  sato         case CONFIG_HOOK_LED_ON:
    372      1.1  sato 		vrled_set_state(sc, LEDON);
    373      1.1  sato                 break;
    374      1.2  sato         case CONFIG_HOOK_LED_FLASH:
    375      1.1  sato 		vrled_set_state(sc, LED8DIVF);
    376      1.1  sato                 break;
    377      1.2  sato         case CONFIG_HOOK_LED_FLASH2:
    378      1.1  sato 		vrled_set_state(sc, LED4DIVF);
    379      1.1  sato                 break;
    380      1.2  sato         case CONFIG_HOOK_LED_FLASH5:
    381      1.1  sato 		vrled_set_state(sc, LED2DIVF);
    382      1.1  sato                 break;
    383      1.2  sato         case CONFIG_HOOK_LED_BLINK:
    384      1.1  sato 		vrled_set_state(sc, LED1SB);
    385      1.1  sato                 break;
    386      1.2  sato         case CONFIG_HOOK_LED_BLINK2:
    387      1.1  sato 		vrled_set_state(sc, LED2SB);
    388      1.1  sato                 break;
    389      1.1  sato 	default:
    390      1.1  sato 		vrled_set_state(sc, LEDOFF);
    391      1.1  sato         }
    392      1.1  sato         return (0);
    393      1.1  sato }
    394      1.1  sato 
    395      1.1  sato /* end */
    396