FreeBSD/Linux Kernel Cross Reference
sys/pc98/cbus/pmc.c
1 /*-
2 * PMC (Power Management Controller of NEC PC-98Note) Driver
3 *
4 * Copyright (c) 2001 Chiharu Shibata.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * $FreeBSD$
31 */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/eventhandler.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/types.h>
40 #include <sys/conf.h>
41 #include <sys/reboot.h>
42
43 #include <machine/bus.h>
44 #include <machine/resource.h>
45 #include <sys/rman.h>
46
47 #include <isa/isavar.h>
48
49 struct pmc_isa_softc {
50 struct resource *port_res;
51 eventhandler_tag evt;
52 int flags;
53 };
54
55 static int pmc_isa_alloc_resources(device_t);
56 static void pmc_isa_release_resources(device_t);
57 static int pmc_isa_probe(device_t);
58 static int pmc_isa_attach(device_t);
59 static int pmc_isa_detach(device_t);
60
61 #define PMC_ISA_PORT 0x8f0
62 #define PMC_ISA_PORTSIZE 4
63
64 #define sc_inw(sc, port) \
65 bus_space_read_2(rman_get_bustag((sc)->port_res), \
66 rman_get_bushandle((sc)->port_res), (port))
67
68 #define sc_outw(sc, port, value) \
69 bus_space_write_2(rman_get_bustag((sc)->port_res), \
70 rman_get_bushandle((sc)->port_res), (port), (value))
71
72 static void
73 pmc_poweroff(void *arg, int howto)
74 {
75 struct pmc_isa_softc *sc = (struct pmc_isa_softc *)arg;
76
77 if (!sc->flags) {
78 outb(0x5e8e, inb(0x5e8e) & ~0x11); /* FDD LED off */
79 }
80
81 if (!(howto & RB_POWEROFF)) {
82 return;
83 }
84
85 sc_outw(sc, 0, 0x0044);
86 sc_outw(sc, 2, 1 << 10);
87 #if 1
88 /* for 9801NS/T */
89 sc_outw(sc, 0, 0xf00a);
90 sc_outw(sc, 2, 1 << 9);
91 #endif
92 }
93
94 static int
95 pmc_isa_alloc_resources(device_t dev)
96 {
97 struct pmc_isa_softc *sc = device_get_softc(dev);
98 int rid;
99
100 bzero(sc, sizeof(*sc));
101
102 rid = 0;
103 sc->port_res = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid,
104 PMC_ISA_PORTSIZE, RF_ACTIVE);
105 if (sc->port_res == NULL) {
106 return (ENOMEM);
107 }
108
109 return 0;
110 }
111
112 static void
113 pmc_isa_release_resources(device_t dev)
114 {
115 struct pmc_isa_softc *sc = device_get_softc(dev);
116
117 if (sc->port_res != NULL) {
118 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port_res);
119 }
120 sc->port_res = NULL;
121 }
122
123 static int
124 pmc_isa_probe(device_t dev)
125 {
126 struct pmc_isa_softc *sc = device_get_softc(dev);
127 u_int port;
128 u_int16_t save, tmp;
129
130 #if 0
131 if (isa_get_vendorid(dev)) {
132 return ENXIO;
133 }
134 if (device_get_unit(dev) > 0) {
135 printf("pmc: Only one PMC driver supported.\n");
136 return ENXIO;
137 }
138 #endif
139 port = isa_get_port(dev);
140 if (port == -1) {
141 port = PMC_ISA_PORT;
142 }
143 if (bootverbose) {
144 device_printf(dev, "port = 0x%x\n", port);
145 }
146
147 if (bus_set_resource(dev, SYS_RES_IOPORT, 0, port, PMC_ISA_PORTSIZE)) {
148 if (bootverbose) {
149 device_printf(dev, "bus_set_resource failed\n");
150 }
151 return ENXIO;
152 }
153 if (pmc_isa_alloc_resources(dev)) {
154 if (bootverbose) {
155 device_printf(dev, "pmc_isa_alloc_resources failed\n");
156 }
157 return ENXIO;
158 }
159
160 /* Check the existence of PMC */
161 sc_outw(sc, 0, 0x0052);
162 save = sc_inw(sc, 2);
163 tmp = save & ~0x3f;
164 sc_outw(sc, 2, tmp);
165 if (sc_inw(sc, 2) != tmp) {
166 if (bootverbose) {
167 device_printf(dev, "failed to clear index(0x0052)\n");
168 }
169
170 pmc_isa_release_resources(dev);
171 return ENXIO;
172 }
173
174 tmp |= 0x3e;
175 sc_outw(sc, 2, tmp);
176 if (sc_inw(sc, 2) != tmp) {
177 if (bootverbose) {
178 device_printf(dev, "failed to set index(0x0052)\n");
179 }
180
181 pmc_isa_release_resources(dev);
182 return ENXIO;
183 }
184 sc_outw(sc, 2, save);
185
186 pmc_isa_release_resources(dev);
187
188 device_set_desc(dev, "Power Management Controller");
189 return 0;
190 }
191
192 static int
193 pmc_isa_attach(device_t dev)
194 {
195 struct pmc_isa_softc *sc = device_get_softc(dev);
196 int error;
197
198 error = pmc_isa_alloc_resources(dev);
199 if (error) {
200 device_printf(dev, "resource allocation failed\n");
201 return error;
202 }
203
204 /* Power the system off using PMC */
205 sc->evt = EVENTHANDLER_REGISTER(shutdown_final, pmc_poweroff, sc,
206 SHUTDOWN_PRI_LAST);
207 sc->flags = device_get_flags(dev);
208 return 0;
209 }
210
211 static int
212 pmc_isa_detach(device_t dev)
213 {
214 struct pmc_isa_softc *sc = device_get_softc(dev);
215
216 if (bootverbose) {
217 device_printf(dev, "pmc_isa_detach called\n");
218 }
219
220 if (sc->evt != NULL) {
221 EVENTHANDLER_DEREGISTER(shutdown_final, sc->evt);
222 }
223 sc->evt = NULL;
224
225 pmc_isa_release_resources(dev);
226 return 0;
227 }
228
229 static device_method_t pmc_isa_methods[] = {
230 /* Device interface */
231 DEVMETHOD(device_probe, pmc_isa_probe),
232 DEVMETHOD(device_attach, pmc_isa_attach),
233 DEVMETHOD(device_detach, pmc_isa_detach),
234 {0, 0}
235 };
236
237 static driver_t pmc_isa_driver = {
238 "pmc",
239 pmc_isa_methods, sizeof(struct pmc_isa_softc),
240 };
241
242 devclass_t pmc_devclass;
243
244 DRIVER_MODULE(pmc, isa, pmc_isa_driver, pmc_devclass, 0, 0);
Cache object: b883b74d0a84b3e3fc82f3f0b0fa93db
|