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