Home | History | Annotate | Line # | Download | only in vr
vrpmu.c revision 1.7.4.1
      1  1.7.4.1  takemura /*	$NetBSD: vrpmu.c,v 1.7.4.1 2000/08/06 04:32:24 takemura Exp $	*/
      2      1.1  takemura 
      3      1.1  takemura /*
      4      1.1  takemura  * Copyright (c) 1999 M. Warner Losh.  All rights reserved.
      5      1.1  takemura  * Copyright (c) 1999 PocketBSD Project. All rights reserved.
      6      1.1  takemura  *
      7      1.1  takemura  * Redistribution and use in source and binary forms, with or without
      8      1.1  takemura  * modification, are permitted provided that the following conditions
      9      1.1  takemura  * are met:
     10      1.1  takemura  * 1. Redistributions of source code must retain the above copyright
     11      1.1  takemura  *    notice, this list of conditions and the following disclaimer.
     12      1.1  takemura  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  takemura  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  takemura  *    documentation and/or other materials provided with the distribution.
     15      1.1  takemura  *
     16      1.1  takemura  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17      1.1  takemura  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18      1.1  takemura  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19      1.1  takemura  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20      1.1  takemura  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21      1.1  takemura  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22      1.1  takemura  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23      1.1  takemura  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24      1.1  takemura  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25      1.1  takemura  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26      1.1  takemura  * SUCH DAMAGE.
     27      1.1  takemura  */
     28      1.1  takemura 
     29      1.1  takemura #include <sys/param.h>
     30      1.1  takemura #include <sys/systm.h>
     31      1.1  takemura #include <sys/device.h>
     32      1.1  takemura 
     33      1.1  takemura #include <machine/bus.h>
     34      1.4  takemura #include <machine/config_hook.h>
     35      1.1  takemura 
     36      1.1  takemura #include <hpcmips/vr/vripvar.h>
     37      1.1  takemura #include <hpcmips/vr/vrpmuvar.h>
     38      1.1  takemura #include <hpcmips/vr/vrpmureg.h>
     39      1.1  takemura 
     40      1.3      sato #include "vrbcu.h"
     41      1.3      sato #if NVRBCU > 0
     42      1.3      sato #include <hpcmips/vr/bcuvar.h>
     43      1.3      sato #include <hpcmips/vr/bcureg.h>
     44      1.3      sato #endif
     45      1.3      sato 
     46      1.5      sato #ifdef VRPMUDEBUG
     47      1.5      sato #define DEBUG_BOOT	0x1	/* boot time */
     48      1.5      sato #define DEBUG_INTR	0x2	/* intr */
     49      1.6      sato #define DEBUG_IO	0x4	/* I/O */
     50      1.5      sato #ifndef VRPMUDEBUG_CONF
     51      1.5      sato #define VRPMUDEBUG_CONF 0
     52      1.5      sato #endif /* VRPMUDEBUG_CONF */
     53      1.5      sato int vrpmudebug = VRPMUDEBUG_CONF;
     54      1.6      sato #define DPRINTF(flag, arg) if (vrpmudebug&flag) printf arg;
     55      1.6      sato #define DDUMP_INTR2(flag, arg1, arg2) if (vrpmudebug&flag) vrpmu_dump_intr2(arg1,arg2);
     56      1.6      sato #define DDUMP_REGS(flag, arg) if (vrpmudebug&flag) vrpmu_dump_regs(arg);
     57      1.6      sato #else /* VRPMUDEBUG */
     58      1.6      sato #define DPRINTF(flag, arg)
     59      1.6      sato #define DDUMP_INTR2(flag, arg1, arg2)
     60      1.6      sato #define DDUMP_REGS(flag, arg)
     61      1.5      sato #endif /* VRPMUDEBUG */
     62      1.5      sato 
     63      1.1  takemura static int vrpmumatch __P((struct device *, struct cfdata *, void *));
     64      1.1  takemura static void vrpmuattach __P((struct device *, struct device *, void *));
     65      1.1  takemura 
     66      1.1  takemura static void vrpmu_write __P((struct vrpmu_softc *, int, unsigned short));
     67      1.1  takemura static unsigned short vrpmu_read __P((struct vrpmu_softc *, int));
     68      1.1  takemura 
     69      1.1  takemura int vrpmu_intr __P((void *));
     70      1.6      sato void vrpmu_dump_intr __P((void *));
     71      1.6      sato void vrpmu_dump_intr2 __P((unsigned int, unsigned int));
     72      1.6      sato void vrpmu_dump_regs __P((void *));
     73      1.1  takemura 
     74      1.1  takemura struct cfattach vrpmu_ca = {
     75      1.1  takemura 	sizeof(struct vrpmu_softc), vrpmumatch, vrpmuattach
     76      1.1  takemura };
     77      1.1  takemura 
     78      1.2      sato static inline void
     79      1.2      sato vrpmu_write(sc, port, val)
     80      1.2      sato 	struct vrpmu_softc *sc;
     81      1.2      sato 	int port;
     82      1.2      sato 	unsigned short val;
     83      1.2      sato {
     84      1.2      sato 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
     85      1.2      sato }
     86      1.2      sato 
     87      1.2      sato static inline unsigned short
     88      1.2      sato vrpmu_read(sc, port)
     89      1.2      sato 	struct vrpmu_softc *sc;
     90      1.2      sato 	int port;
     91      1.2      sato {
     92      1.2      sato 	return bus_space_read_2(sc->sc_iot, sc->sc_ioh, port);
     93      1.2      sato }
     94      1.2      sato 
     95      1.1  takemura static int
     96      1.1  takemura vrpmumatch(parent, cf, aux)
     97      1.1  takemura 	struct device *parent;
     98      1.1  takemura 	struct cfdata *cf;
     99      1.1  takemura 	void *aux;
    100      1.1  takemura {
    101      1.1  takemura 	return 1;
    102      1.1  takemura }
    103      1.1  takemura 
    104      1.1  takemura static void
    105      1.1  takemura vrpmuattach(parent, self, aux)
    106      1.1  takemura 	struct device *parent;
    107      1.1  takemura 	struct device *self;
    108      1.1  takemura 	void *aux;
    109      1.1  takemura {
    110      1.1  takemura 	struct vrpmu_softc *sc = (struct vrpmu_softc *)self;
    111      1.1  takemura 	struct vrip_attach_args *va = aux;
    112      1.1  takemura 
    113      1.1  takemura 	bus_space_tag_t iot = va->va_iot;
    114      1.1  takemura 	bus_space_handle_t ioh;
    115      1.1  takemura 
    116      1.1  takemura 	if (bus_space_map(iot, va->va_addr, 1, 0, &ioh)) {
    117      1.1  takemura 		printf(": can't map bus space\n");
    118      1.1  takemura 		return;
    119      1.1  takemura 	}
    120      1.1  takemura 
    121      1.1  takemura 	sc->sc_iot = iot;
    122      1.1  takemura 	sc->sc_ioh = ioh;
    123      1.1  takemura 
    124      1.1  takemura 	if (!(sc->sc_handler =
    125      1.1  takemura 	      vrip_intr_establish(va->va_vc, va->va_intr, IPL_TTY,
    126      1.1  takemura 				  vrpmu_intr, sc))) {
    127      1.1  takemura 		printf (": can't map interrupt line.\n");
    128      1.1  takemura 		return;
    129      1.1  takemura 	}
    130      1.1  takemura 
    131      1.1  takemura 	printf("\n");
    132      1.6      sato 	/* dump current intrrupt states */
    133      1.6      sato 	vrpmu_dump_intr(sc);
    134      1.6      sato 	DDUMP_REGS(DEBUG_BOOT, sc);
    135      1.2      sato 	/* clear interrupt status */
    136      1.2      sato 	vrpmu_write(sc, PMUINT_REG_W, PMUINT_ALL);
    137      1.2      sato 	vrpmu_write(sc, PMUINT2_REG_W, PMUINT2_ALL);
    138      1.1  takemura }
    139      1.1  takemura 
    140      1.1  takemura /*
    141      1.2      sato  * dump PMU intr status regs
    142      1.6      sato  *
    143      1.1  takemura  */
    144      1.2      sato void
    145      1.6      sato vrpmu_dump_intr(arg)
    146      1.6      sato 	void *arg;
    147      1.6      sato {
    148      1.6      sato         struct vrpmu_softc *sc = arg;
    149      1.6      sato 	unsigned int intstat1;
    150      1.6      sato 	unsigned int intstat2;
    151      1.6      sato 	intstat1 = vrpmu_read(sc, PMUINT_REG_W);
    152      1.6      sato 	intstat2 = vrpmu_read(sc, PMUINT2_REG_W);
    153      1.6      sato 	vrpmu_dump_intr2(intstat1, intstat2);
    154      1.6      sato 
    155      1.6      sato }
    156      1.6      sato 
    157      1.6      sato /*
    158      1.6      sato  * dump PMU intr status regs
    159      1.6      sato  */
    160      1.6      sato void
    161      1.6      sato vrpmu_dump_intr2(intstat1, intstat2)
    162      1.2      sato unsigned int intstat1, intstat2;
    163      1.1  takemura {
    164      1.2      sato 	if (intstat1 & PMUINT_GPIO3)
    165      1.1  takemura 		printf("vrpmu: GPIO[3] activation\n");
    166      1.2      sato 	if (intstat1 & PMUINT_GPIO2)
    167      1.1  takemura 		printf("vrpmu: GPIO[2] activation\n");
    168      1.2      sato 	if (intstat1 & PMUINT_GPIO1)
    169      1.1  takemura 		printf("vrpmu: GPIO[1] activation\n");
    170      1.2      sato 	if (intstat1 & PMUINT_GPIO0)
    171      1.1  takemura 		printf("vrpmu: GPIO[0] activation\n");
    172      1.1  takemura 
    173      1.2      sato 	if (intstat1 & PMUINT_RTC)
    174      1.1  takemura 		printf("vrpmu: RTC alarm detected\n");
    175      1.2      sato 	if (intstat1 & PMUINT_BATT)
    176      1.1  takemura 		printf("vrpmu: Battery low during activation\n");
    177      1.1  takemura 
    178      1.2      sato 	if (intstat1 & PMUINT_TIMOUTRST)
    179      1.1  takemura 		printf("vrpmu: HAL timer reset\n");
    180      1.2      sato 	if (intstat1 & PMUINT_RTCRST)
    181      1.1  takemura 		printf("vrpmu: RTC reset detected\n");
    182      1.2      sato 	if (intstat1 & PMUINT_RSTSWRST)
    183      1.1  takemura 		printf("vrpmu: RESET switch detected\n");
    184      1.2      sato 	if (intstat1 & PMUINT_DMSWRST)
    185      1.1  takemura 		printf("vrpmu: Deadman's switch detected\n");
    186      1.2      sato 	if (intstat1 & PMUINT_BATTINTR)
    187      1.1  takemura 		printf("vrpmu: Battery low during normal ops\n");
    188      1.2      sato 	if (intstat1 & PMUINT_POWERSW)
    189      1.1  takemura 		printf("vrpmu: POWER switch detected\n");
    190      1.1  takemura 
    191      1.2      sato 	if (intstat2 & PMUINT_GPIO12)
    192      1.1  takemura 		printf("vrpmu: GPIO[12] activation\n");
    193      1.2      sato 	if (intstat2 & PMUINT_GPIO11)
    194      1.1  takemura 		printf("vrpmu: GPIO[11] activation\n");
    195      1.2      sato 	if (intstat2 & PMUINT_GPIO10)
    196      1.1  takemura 		printf("vrpmu: GPIO[10] activation\n");
    197      1.2      sato 	if (intstat2 & PMUINT_GPIO9)
    198      1.1  takemura 		printf("vrpmu: GPIO[9] activation\n");
    199      1.2      sato }
    200      1.2      sato 
    201      1.2      sato /*
    202      1.2      sato  * dump PMU registers
    203      1.2      sato  *
    204      1.2      sato  */
    205      1.2      sato void
    206      1.2      sato vrpmu_dump_regs(arg)
    207      1.2      sato 	void *arg;
    208      1.2      sato {
    209      1.2      sato         struct vrpmu_softc *sc = arg;
    210      1.2      sato 	unsigned int intstat1;
    211      1.2      sato 	unsigned int intstat2;
    212      1.3      sato 	unsigned int reg;
    213      1.3      sato #if NVRBCU > 0
    214      1.3      sato 	int cpuid;
    215      1.3      sato #endif
    216      1.2      sato 	intstat1 = vrpmu_read(sc, PMUINT_REG_W);
    217      1.2      sato 	intstat2 = vrpmu_read(sc, PMUINT2_REG_W);
    218      1.3      sato 
    219      1.6      sato 	/* others? XXXX */
    220      1.6      sato 	reg = vrpmu_read(sc, PMUCNT_REG_W);
    221      1.6      sato 	printf("vrpmu: cnt 0x%x: ", reg);
    222      1.6      sato 	bitdisp16(reg);
    223      1.6      sato 	reg = vrpmu_read(sc, PMUCNT2_REG_W);
    224      1.6      sato 	printf("vrpmu: cnt2 0x%x: ", reg);
    225      1.6      sato 	bitdisp16(reg);
    226      1.3      sato #if NVRBCU > 0
    227      1.6      sato 	cpuid = vrbcu_vrip_getcpuid();
    228      1.6      sato 	if (cpuid >= BCUREVID_RID_4111){
    229      1.6      sato 		reg = vrpmu_read(sc, PMUWAIT_REG_W);
    230      1.6      sato 		printf("vrpmu: wait 0x%x", reg);
    231      1.6      sato 	}
    232      1.6      sato 	if (cpuid >= BCUREVID_RID_4121){
    233      1.6      sato 		reg = vrpmu_read(sc, PMUDIV_REG_W);
    234      1.6      sato 		printf(" div 0x%x", reg);
    235      1.3      sato 	}
    236      1.6      sato 	printf("\n");
    237      1.6      sato #endif /*  NVRBCU > 0 */
    238      1.2      sato }
    239      1.3      sato 
    240      1.2      sato /*
    241      1.2      sato  * PMU interrupt handler.
    242      1.2      sato  * XXX
    243      1.2      sato  *
    244      1.2      sato  * In the following interrupt routine we should actually DO something
    245      1.2      sato  * with the knowledge that we've gained.  For now we just report it.
    246      1.2      sato  */
    247      1.2      sato int
    248      1.2      sato vrpmu_intr(arg)
    249      1.2      sato 	void *arg;
    250      1.2      sato {
    251      1.2      sato         struct vrpmu_softc *sc = arg;
    252      1.2      sato 	unsigned int intstat1;
    253      1.2      sato 	unsigned int intstat2;
    254      1.2      sato 
    255      1.2      sato 	intstat1 = vrpmu_read(sc, PMUINT_REG_W);
    256      1.2      sato 	/* clear interrupt status */
    257      1.2      sato 	vrpmu_write(sc, PMUINT_REG_W, intstat1);
    258      1.2      sato 
    259      1.2      sato 
    260      1.2      sato 	intstat2 = vrpmu_read(sc, PMUINT2_REG_W);
    261      1.2      sato 	/* clear interrupt status */
    262      1.2      sato 	vrpmu_write(sc, PMUINT2_REG_W, intstat2);
    263      1.2      sato 
    264      1.6      sato 	DDUMP_INTR2(DEBUG_INTR, intstat1, intstat2);
    265      1.2      sato 
    266      1.2      sato 	if (intstat1 & PMUINT_GPIO3)
    267      1.2      sato 		;
    268      1.2      sato 	if (intstat1 & PMUINT_GPIO2)
    269      1.2      sato 		;
    270      1.2      sato 	if (intstat1 & PMUINT_GPIO1)
    271      1.2      sato 		;
    272      1.2      sato 	if (intstat1 & PMUINT_GPIO0)
    273      1.2      sato 		;
    274      1.2      sato 
    275      1.2      sato 	if (intstat1 & PMUINT_RTC)
    276      1.2      sato 		;
    277      1.2      sato 	if (intstat1 & PMUINT_BATT)
    278      1.2      sato 		;
    279      1.2      sato 
    280      1.2      sato 	if (intstat1 & PMUINT_TIMOUTRST)
    281      1.2      sato 		;
    282      1.2      sato 	if (intstat1 & PMUINT_RTCRST)
    283      1.2      sato 		;
    284      1.2      sato 	if (intstat1 & PMUINT_RSTSWRST)
    285      1.2      sato 		;
    286      1.2      sato 	if (intstat1 & PMUINT_DMSWRST)
    287      1.2      sato 		;
    288      1.2      sato 	if (intstat1 & PMUINT_BATTINTR)
    289      1.2      sato 		;
    290      1.6      sato 	if (intstat1 & PMUINT_POWERSW) {
    291  1.7.4.1  takemura 		/*
    292  1.7.4.1  takemura 		 * you can't detect when the button is released
    293  1.7.4.1  takemura 		 */
    294  1.7.4.1  takemura 		config_hook_call(CONFIG_HOOK_BUTTONEVENT,
    295  1.7.4.1  takemura 				 CONFIG_HOOK_BUTTONEVENT_POWER,
    296  1.7.4.1  takemura 				 (void*)1 /* on */);
    297      1.4  takemura 		config_hook_call(CONFIG_HOOK_BUTTONEVENT,
    298      1.4  takemura 				 CONFIG_HOOK_BUTTONEVENT_POWER,
    299  1.7.4.1  takemura 				 (void*)0 /* off */);
    300      1.6      sato 	}
    301      1.2      sato 
    302      1.2      sato 	if (intstat2 & PMUINT_GPIO12)
    303      1.2      sato 		;
    304      1.2      sato 	if (intstat2 & PMUINT_GPIO11)
    305      1.2      sato 		;
    306      1.2      sato 	if (intstat2 & PMUINT_GPIO10)
    307      1.2      sato 		;
    308      1.2      sato 	if (intstat2 & PMUINT_GPIO9)
    309      1.2      sato 		;
    310      1.1  takemura 
    311      1.1  takemura 	return 0;
    312      1.1  takemura }
    313