xref: /netbsd-src/sys/arch/hpcmips/vr/cmu.c (revision cbab9cadce21ae72fac13910001079fff214cc29)
1 /*	$NetBSD: cmu.c,v 1.14 2012/10/27 17:17:55 chs Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999 SASAKI Takesi
5  * Copyright (c) 1999, 2000, 2002 PocketBSD Project. All rights reserved.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the PocketBSD project
19  *	and its contributors.
20  * 4. Neither the name of the project nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  */
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: cmu.c,v 1.14 2012/10/27 17:17:55 chs Exp $");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 
45 #include <mips/cpuregs.h>
46 
47 #include <machine/bus.h>
48 #include <machine/debug.h>
49 
50 #include "opt_vr41xx.h"
51 #include <hpcmips/vr/vr.h>
52 #include <hpcmips/vr/vrcpudef.h>
53 #include <hpcmips/vr/vripif.h>
54 #include <hpcmips/vr/vripreg.h>
55 
56 #include <hpcmips/vr/cmureg.h>
57 
58 #include <machine/config_hook.h>
59 
60 struct vrcmu_softc {
61 	bus_space_tag_t sc_iot;
62 	bus_space_handle_t sc_ioh;
63 	config_hook_tag sc_hardpower;
64 	int sc_save;
65 	struct vrcmu_chipset_tag sc_chipset;
66 };
67 
68 int	vrcmu_match(device_t, cfdata_t, void *);
69 void	vrcmu_attach(device_t, device_t, void *);
70 int	vrcmu_supply(vrcmu_chipset_tag_t, u_int16_t, int);
71 int	vrcmu_hardpower(void *, int, long, void *);
72 
73 CFATTACH_DECL_NEW(vrcmu, sizeof(struct vrcmu_softc),
74     vrcmu_match, vrcmu_attach, NULL, NULL);
75 
76 int
vrcmu_match(device_t parent,cfdata_t cf,void * aux)77 vrcmu_match(device_t parent, cfdata_t cf, void *aux)
78 {
79 
80 	return (2);		/* 1st attach group of vrip */
81 }
82 
83 void
vrcmu_attach(device_t parent,device_t self,void * aux)84 vrcmu_attach(device_t parent, device_t self, void *aux)
85 {
86 	struct vrip_attach_args *va = aux;
87 	struct vrcmu_softc *sc = device_private(self);
88 
89 	sc->sc_iot = va->va_iot;
90 	sc->sc_chipset.cc_sc = sc;
91 	sc->sc_chipset.cc_clock = vrcmu_supply;
92 	if (bus_space_map(sc->sc_iot, va->va_addr, va->va_size,
93 	    0 /* no flags */, &sc->sc_ioh)) {
94 		printf(": can't map i/o space\n");
95 		return;
96 	}
97 	printf ("\n");
98 
99 	vrip_register_cmu(va->va_vc, &sc->sc_chipset);
100 	sc->sc_hardpower = config_hook(CONFIG_HOOK_PMEVENT,
101 	    CONFIG_HOOK_PMEVENT_HARDPOWER,
102 	    CONFIG_HOOK_SHARE,
103 	    vrcmu_hardpower, sc);
104 }
105 
106 int
vrcmu_supply(vrcmu_chipset_tag_t cc,u_int16_t mask,int onoff)107 vrcmu_supply(vrcmu_chipset_tag_t cc, u_int16_t mask, int onoff)
108 {
109 	struct vrcmu_softc *sc = cc->cc_sc;
110 	u_int16_t reg;
111 
112 	reg = bus_space_read_2(sc->sc_iot, sc->sc_ioh, 0);
113 #ifdef VRCMU_VERBOSE
114 	printf("cmu register(enter):");
115 	dbg_bit_print(reg);
116 #endif
117 	if (onoff)
118 		reg |= mask;
119 	else
120 		reg &= ~mask;
121 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0, reg);
122 #ifdef VRCMU_VERBOSE
123 	printf("cmu register(exit) :");
124 	dbg_bit_print(reg);
125 #endif
126 	return (0);
127 }
128 
129 int
vrcmu_hardpower(void * ctx,int type,long id,void * msg)130 vrcmu_hardpower(void *ctx, int type, long id, void *msg)
131 {
132 	struct vrcmu_softc *sc = ctx;
133 	int why = (int)msg;
134 
135 	switch (why) {
136 	case PWR_STANDBY:
137 	case PWR_SUSPEND:
138 		sc->sc_save = bus_space_read_2(sc->sc_iot, sc->sc_ioh, 0);
139 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0, 0);
140 		break;
141 	case PWR_RESUME:
142 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0, sc->sc_save);
143 		break;
144 	}
145 	return (0);
146 }
147