Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / powermng / pc98 / pmc / 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 withough 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: src/sys/pc98/pc98/pmc.c,v 1.2.2.1 2001/11/01 09:42:29 nyan Exp $
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/bus.h>
38 #include <sys/types.h>
39 #include <sys/conf.h>
40 #include <sys/reboot.h>
41
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 #include <sys/rman.h>
45  
46 #include <isa/isavar.h>
47
48 struct pmc_isa_softc {
49         struct  resource        *port_res;
50         eventhandler_tag        evt;
51         int                     flags;
52 };
53
54 static int      pmc_isa_alloc_resources __P((device_t));
55 static void     pmc_isa_release_resources __P((device_t));
56 static int      pmc_isa_probe __P((device_t));
57 static int      pmc_isa_attach __P((device_t));
58 static int      pmc_isa_detach __P((device_t));
59
60 #define PMC_ISA_PORT            0x8f0
61 #define PMC_ISA_PORTSIZE        4
62
63 #define sc_inw(sc, port) \
64         bus_space_read_2(rman_get_bustag((sc)->port_res), \
65                 rman_get_bushandle((sc)->port_res), (port))
66
67 #define sc_outw(sc, port, value) \
68         bus_space_write_2(rman_get_bustag((sc)->port_res), \
69                 rman_get_bushandle((sc)->port_res), (port), (value))
70
71 static void
72 pmc_poweroff(void *arg, int howto)
73 {
74         struct pmc_isa_softc *sc = (struct pmc_isa_softc *)arg;
75
76         if (!sc->flags) {
77                 outb(0x5e8e, inb(0x5e8e) & ~0x11);      /* FDD LED off */
78         }
79
80         if (!(howto & RB_POWEROFF)) {
81                 return;
82         }
83
84         sc_outw(sc, 0, 0x0044);
85         sc_outw(sc, 2, 1 << 10);
86 #if 1
87         /* for 9801NS/T */
88         sc_outw(sc, 0, 0xf00a);
89         sc_outw(sc, 2, 1 << 9);
90 #endif
91 }
92
93 static int
94 pmc_isa_alloc_resources(device_t dev)
95 {
96         struct pmc_isa_softc *sc = device_get_softc(dev);
97         int     rid;
98
99         bzero(sc, sizeof(*sc));
100
101         rid = 0;
102         sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
103                                           0ul, ~0ul, PMC_ISA_PORTSIZE,
104                                           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);