1 /* $NetBSD: vrled.c,v 1.6.6.3 2004/09/21 13:16:14 skrll Exp $ */
4 * Copyright (c) 2000 SATO Kazumi. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: vrled.c,v 1.6.6.3 2004/09/21 13:16:14 skrll Exp $");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/device.h>
34 #include <sys/reboot.h>
36 #include <machine/bus.h>
37 #include <machine/config_hook.h>
39 #include <hpcmips/vr/vripif.h>
40 #include <hpcmips/vr/vrledvar.h>
41 #include <hpcmips/vr/vrledreg.h>
45 #ifndef VRLEDDEBUG_CONF
46 #define VRLEDDEBUG_CONF 0
47 #endif /* VRLEDDEBUG_CONF */
48 int vrleddebug
= VRLEDDEBUG_CONF
;
49 #define DPRINTF(arg) if (vrleddebug) printf arg;
50 #define VPRINTF(arg) if (bootverbose||vrleddebug) printf arg;
51 #else /* VRLEDDEBUG */
53 #define VPRINTF(arg) if (bootverbose) printf arg;
54 #endif /* VRLEDDEBUG */
56 static int vrledmatch(struct device
*, struct cfdata
*, void *);
57 static void vrledattach(struct device
*, struct device
*, void *);
59 static void vrled_write(struct vrled_softc
*, int, unsigned short);
60 static unsigned short vrled_read(struct vrled_softc
*, int);
62 static void vrled_stop(struct vrled_softc
*);
63 static void vrled_on(struct vrled_softc
*);
64 static void vrled_blink(struct vrled_softc
*);
65 static void vrled_flash(struct vrled_softc
*);
66 static void vrled_change_state(struct vrled_softc
*);
67 static int vrled_event(void *, int, long, void *);
69 int vrled_intr(void *);
71 CFATTACH_DECL(vrled
, sizeof(struct vrled_softc
),
72 vrledmatch
, vrledattach
, NULL
, NULL
);
74 struct vrled_softc
*this_led
;
77 vrled_write(struct vrled_softc
*sc
, int port
, unsigned short val
)
80 bus_space_write_2(sc
->sc_iot
, sc
->sc_ioh
, port
, val
);
83 static inline unsigned short
84 vrled_read(struct vrled_softc
*sc
, int port
)
87 return (bus_space_read_2(sc
->sc_iot
, sc
->sc_ioh
, port
));
91 vrledmatch(struct device
*parent
, struct cfdata
*cf
, void *aux
)
98 vrledattach(struct device
*parent
, struct device
*self
, void *aux
)
100 struct vrled_softc
*sc
= (struct vrled_softc
*)self
;
101 struct vrip_attach_args
*va
= aux
;
103 bus_space_tag_t iot
= va
->va_iot
;
104 bus_space_handle_t ioh
;
106 if (bus_space_map(iot
, va
->va_addr
, 1, 0, &ioh
)) {
107 printf(": can't map bus space\n");
114 if (!(sc
->sc_handler
=
115 vrip_intr_establish(va
->va_vc
, va
->va_unit
, 0, IPL_TTY
,
117 printf (": can't map interrupt line.\n");
122 /* clear interrupt status */
123 vrled_write(sc
, LEDINT_REG_W
, LEDINT_ALL
);
126 sc
->sc_state_cnt
= 1;
127 vrled_write(sc
, LEDASTC_REG_W
, 1); /* 1time */
128 vrled_write(sc
, LEDCNT_REG_W
, LEDCNT_AUTOSTOP
);
131 sc
->sc_hook
= config_hook(CONFIG_HOOK_SET
,
132 CONFIG_HOOK_LED
, CONFIG_HOOK_SHARE
, vrled_event
, sc
);
139 * LED interrupt handler.
143 vrled_intr(void *arg
)
145 struct vrled_softc
*sc
= arg
;
146 unsigned int intstat
;
148 intstat
= vrled_read(sc
, LEDINT_REG_W
);
149 /* clear interrupt status */
150 vrled_write(sc
, LEDINT_REG_W
, intstat
);
151 if (intstat
&LEDINT_AUTOSTOP
) {
152 vrled_change_state(sc
);
162 vrled_stop(struct vrled_softc
*sc
)
164 vrled_write(sc
, LEDHTS_REG_W
, LEDHTS_DIV16SEC
);
165 vrled_write(sc
, LEDLTS_REG_W
, LEDLTS_4SEC
);
166 vrled_write(sc
, LEDASTC_REG_W
, 2); /* 2time */
167 vrled_write(sc
, LEDCNT_REG_W
, LEDCNT_AUTOSTOP
);
169 sc
->sc_state
= LEDOFF
;
170 sc
->sc_next
= LEDOFF
;
178 vrled_on(struct vrled_softc
*sc
)
180 vrled_write(sc
, LEDHTS_REG_W
, LEDHTS_SEC
);
181 vrled_write(sc
, LEDLTS_REG_W
, LEDLTS_DIV16SEC
);
182 vrled_write(sc
, LEDASTC_REG_W
, 2); /* 2time */
183 vrled_write(sc
, LEDCNT_REG_W
, LEDCNT_AUTOSTOP
|LEDCNT_BLINK
);
185 sc
->sc_state
= LEDON
;
194 vrled_blink(struct vrled_softc
*sc
)
199 switch (sc
->sc_next
) {
201 ledhts
= LEDHTS_DIV2SEC
;
202 ledlts
= LEDLTS_DIV2SEC
;
213 vrled_write(sc
, LEDHTS_REG_W
, ledhts
);
214 vrled_write(sc
, LEDLTS_REG_W
, ledlts
);
215 vrled_write(sc
, LEDASTC_REG_W
, 2); /* 2time */
216 vrled_write(sc
, LEDCNT_REG_W
, LEDCNT_AUTOSTOP
);
217 vrled_write(sc
, LEDCNT_REG_W
, LEDCNT_AUTOSTOP
|LEDCNT_BLINK
);
219 sc
->sc_state
= sc
->sc_next
;
227 vrled_flash(struct vrled_softc
*sc
)
232 switch (sc
->sc_next
) {
234 ledhts
= LEDHTS_DIV16SEC
;
235 ledlts
= LEDLTS_DIV16SEC
;
238 ledhts
= LEDHTS_DIV8SEC
;
239 ledlts
= LEDLTS_DIV8SEC
;
242 ledhts
= LEDHTS_DIV4SEC
;
243 ledlts
= LEDLTS_DIV4SEC
;
246 ledhts
= LEDHTS_DIV2SEC
;
247 ledlts
= LEDLTS_DIV2SEC
;
254 vrled_write(sc
, LEDHTS_REG_W
, ledhts
);
255 vrled_write(sc
, LEDLTS_REG_W
, ledlts
);
256 vrled_write(sc
, LEDASTC_REG_W
, 2); /* 2time */
257 vrled_write(sc
, LEDCNT_REG_W
, LEDCNT_AUTOSTOP
|LEDCNT_BLINK
);
259 sc
->sc_state
= sc
->sc_next
;
260 sc
->sc_next
= LEDOFF
;
261 sc
->sc_state_cnt
= 1;
269 vrled_change_state(struct vrled_softc
*sc
)
272 switch (sc
->sc_next
) {
300 vrled_set_state(struct vrled_softc
*sc
, vrled_status state
)
305 ledstate
= vrled_read(sc
, LEDCNT_REG_W
);
306 if (ledstate
&LEDCNT_BLINK
) { /* currently processing */
307 if (sc
->sc_next
== state
)
309 switch (sc
->sc_next
) {
344 sc
->sc_next
= LEDOFF
;
350 vrled_change_state(sc
);
354 * LED config hook events
358 vrled_event(void *ctx
, int type
, long id
, void *msg
)
360 struct vrled_softc
*sc
= (struct vrled_softc
*)ctx
;
361 int why
=*(int *)msg
;
363 if (type
!= CONFIG_HOOK_SET
364 || id
!= CONFIG_HOOK_LED
)
370 case CONFIG_HOOK_LED_OFF
:
371 vrled_set_state(sc
, LEDOFF
);
373 case CONFIG_HOOK_LED_ON
:
374 vrled_set_state(sc
, LEDON
);
376 case CONFIG_HOOK_LED_FLASH
:
377 vrled_set_state(sc
, LED8DIVF
);
379 case CONFIG_HOOK_LED_FLASH2
:
380 vrled_set_state(sc
, LED4DIVF
);
382 case CONFIG_HOOK_LED_FLASH5
:
383 vrled_set_state(sc
, LED2DIVF
);
385 case CONFIG_HOOK_LED_BLINK
:
386 vrled_set_state(sc
, LED1SB
);
388 case CONFIG_HOOK_LED_BLINK2
:
389 vrled_set_state(sc
, LED2SB
);
392 vrled_set_state(sc
, LEDOFF
);