xref: /netbsd-src/sys/arch/hpcmips/vr/cmu.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: cmu.c,v 1.4 2001/06/11 05:24:06 enami Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999 SASAKI Takesi
5  * Copyright (c) 1999,2000 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/param.h>
39 #include <sys/systm.h>
40 #include <sys/device.h>
41 
42 #include <mips/cpuregs.h>
43 
44 #include <machine/bus.h>
45 
46 #include "opt_vr41xx.h"
47 #include <hpcmips/vr/vr.h>
48 #include <hpcmips/vr/vrcpudef.h>
49 #include <hpcmips/vr/vripreg.h>
50 #include <hpcmips/vr/vripvar.h>
51 
52 #include <hpcmips/vr/cmureg.h>
53 
54 #include <machine/config_hook.h>
55 
56 struct vrcmu_softc {
57 	struct device sc_dev;
58 	bus_space_tag_t sc_iot;
59 	bus_space_handle_t sc_ioh;
60 	config_hook_tag sc_hardpower;
61 	int sc_save;
62 };
63 
64 int	vrcmu_match __P((struct device *, struct cfdata *, void *));
65 void	vrcmu_attach __P((struct device *, struct device *, void *));
66 int	vrcmu_supply __P((vrcmu_chipset_tag_t, u_int16_t, int));
67 int	vrcmu_hardpower __P((void *, int, long, void *));
68 
69 struct vrcmu_function_tag vrcmu_functions = {
70 	vrcmu_supply
71 };
72 
73 struct cfattach vrcmu_ca = {
74 	sizeof(struct vrcmu_softc), vrcmu_match, vrcmu_attach
75 };
76 
77 int
78 vrcmu_match(parent, cf, aux)
79 	struct device *parent;
80 	struct cfdata *cf;
81 	void *aux;
82 {
83 
84 	return (2);		/* 1st attach group of vrip */
85 }
86 
87 void
88 vrcmu_attach(parent, self, aux)
89 	struct device *parent;
90 	struct device *self;
91 	void *aux;
92 {
93 	struct vrip_attach_args *va = aux;
94 	struct vrcmu_softc *sc = (void *)self;
95 
96 	sc->sc_iot = va->va_iot;
97 	if (bus_space_map(sc->sc_iot, va->va_addr, va->va_size,
98 	    0 /* no flags */, &sc->sc_ioh)) {
99 		printf(": can't map i/o space\n");
100 		return;
101 	}
102 	printf ("\n");
103 
104 	vrip_cmu_function_register(va->va_vc, &vrcmu_functions, self);
105 	sc->sc_hardpower = config_hook(CONFIG_HOOK_PMEVENT,
106 	    CONFIG_HOOK_PMEVENT_HARDPOWER,
107 	    CONFIG_HOOK_SHARE,
108 	    vrcmu_hardpower, sc);
109 }
110 
111 /* For serial console */
112 void
113 __vrcmu_supply(mask, onoff)
114 	u_int16_t mask;
115 	int onoff;
116 {
117 	u_int16_t reg;
118 	u_int32_t addr;
119 
120 	addr = MIPS_PHYS_TO_KSEG1(VRIP_CMU_ADDR);
121 	reg = *((volatile u_int16_t *)addr);
122 	if (onoff)
123 		reg |= mask;
124 	else
125 		reg &= ~mask;
126 	*((volatile u_int16_t *)addr) = reg;
127 }
128 
129 int
130 vrcmu_supply(cc, mask, onoff)
131 	vrcmu_chipset_tag_t cc;
132 	u_int16_t mask;
133 	int onoff;
134 {
135 	struct vrcmu_softc *sc = (void *)cc;
136 	u_int16_t reg;
137 
138 	reg = bus_space_read_2(sc->sc_iot, sc->sc_ioh, 0);
139 #ifdef VRCMU_VERBOSE
140 	printf("cmu register(enter):");
141 	bitdisp16(reg);
142 #endif
143 	if (onoff)
144 		reg |= mask;
145 	else
146 		reg &= ~mask;
147 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0, reg);
148 #ifdef VRCMU_VERBOSE
149 	printf("cmu register(exit) :");
150 	bitdisp16(reg);
151 #endif
152 	return (0);
153 }
154 
155 int
156 vrcmu_hardpower(ctx, type, id, msg)
157 	void *ctx;
158 	int type;
159 	long id;
160 	void *msg;
161 {
162 	struct vrcmu_softc *sc = ctx;
163 	int why = (int)msg;
164 
165 	switch (why) {
166 	case PWR_STANDBY:
167 	case PWR_SUSPEND:
168 		sc->sc_save = bus_space_read_2(sc->sc_iot, sc->sc_ioh, 0);
169 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0, 0);
170 		break;
171 	case PWR_RESUME:
172 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0, sc->sc_save);
173 		break;
174 	}
175 	return (0);
176 }
177