fdc_acpi.c revision 1.3 1 1.3 jmcneill /* $NetBSD: fdc_acpi.c,v 1.3 2003/01/09 00:22:39 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*
4 1.1 jmcneill * Copyright (c) 2002 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. The name of the author may not be used to endorse or promote products
13 1.1 jmcneill * derived from this software without specific prior written permission.
14 1.1 jmcneill *
15 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 jmcneill * SUCH DAMAGE.
26 1.1 jmcneill */
27 1.1 jmcneill
28 1.1 jmcneill /*
29 1.1 jmcneill * ACPI attachment for the PC Floppy Controller driver, based on
30 1.1 jmcneill * sys/arch/i386/pnpbios/fdc_pnpbios.c by Jason R. Thorpe
31 1.1 jmcneill */
32 1.1 jmcneill
33 1.1 jmcneill #include <sys/cdefs.h>
34 1.3 jmcneill __KERNEL_RCSID(0, "$NetBSD: fdc_acpi.c,v 1.3 2003/01/09 00:22:39 jmcneill Exp $");
35 1.1 jmcneill
36 1.1 jmcneill #include "rnd.h"
37 1.1 jmcneill
38 1.1 jmcneill #include <sys/param.h>
39 1.1 jmcneill #include <sys/systm.h>
40 1.1 jmcneill #include <sys/callout.h>
41 1.1 jmcneill #include <sys/device.h>
42 1.1 jmcneill #include <sys/buf.h>
43 1.1 jmcneill #include <sys/queue.h>
44 1.3 jmcneill #include <sys/disk.h>
45 1.3 jmcneill #include <sys/dkstat.h>
46 1.1 jmcneill #if NRND > 0
47 1.1 jmcneill #include <sys/rnd.h>
48 1.1 jmcneill #endif
49 1.1 jmcneill
50 1.1 jmcneill #include <machine/bus.h>
51 1.1 jmcneill #include <machine/intr.h>
52 1.1 jmcneill
53 1.1 jmcneill #include <dev/isa/isavar.h>
54 1.1 jmcneill #include <dev/isa/isadmavar.h>
55 1.1 jmcneill
56 1.1 jmcneill #include <dev/acpi/acpica.h>
57 1.1 jmcneill #include <dev/acpi/acpireg.h>
58 1.1 jmcneill #include <dev/acpi/acpivar.h>
59 1.1 jmcneill
60 1.1 jmcneill #include <dev/isa/fdcvar.h>
61 1.3 jmcneill #include <dev/isa/fdvar.h>
62 1.3 jmcneill #include <dev/isa/fdreg.h>
63 1.3 jmcneill
64 1.3 jmcneill #include <dev/acpi/fdc_acpireg.h>
65 1.1 jmcneill
66 1.1 jmcneill int fdc_acpi_match(struct device *, struct cfdata *, void *);
67 1.1 jmcneill void fdc_acpi_attach(struct device *, struct device *, void *);
68 1.1 jmcneill
69 1.1 jmcneill struct fdc_acpi_softc {
70 1.1 jmcneill struct fdc_softc sc_fdc;
71 1.1 jmcneill bus_space_handle_t sc_baseioh;
72 1.3 jmcneill struct acpi_devnode *sc_node; /* ACPI devnode */
73 1.1 jmcneill };
74 1.1 jmcneill
75 1.3 jmcneill static int fdc_acpi_enumerate(struct fdc_acpi_softc *);
76 1.3 jmcneill static void fdc_acpi_getknownfds(struct fdc_acpi_softc *);
77 1.3 jmcneill
78 1.3 jmcneill static const struct fd_type *fdc_acpi_nvtotype(char *, int, int);
79 1.3 jmcneill
80 1.1 jmcneill CFATTACH_DECL(fdc_acpi, sizeof(struct fdc_acpi_softc), fdc_acpi_match,
81 1.1 jmcneill fdc_acpi_attach, NULL, NULL);
82 1.1 jmcneill
83 1.1 jmcneill /*
84 1.1 jmcneill * Supported device IDs
85 1.1 jmcneill */
86 1.1 jmcneill
87 1.1 jmcneill static const char * const fdc_acpi_ids[] = {
88 1.1 jmcneill "PNP0700", /* PC standard floppy disk controller */
89 1.1 jmcneill #if 0 /* XXX do we support this? */
90 1.1 jmcneill "PNP0701", /* Standard floppy controller for MS Device Bay Spec */
91 1.1 jmcneill #endif
92 1.1 jmcneill NULL
93 1.1 jmcneill };
94 1.1 jmcneill
95 1.1 jmcneill /*
96 1.1 jmcneill * fdc_acpi_match: autoconf(9) match routine
97 1.1 jmcneill */
98 1.1 jmcneill int
99 1.1 jmcneill fdc_acpi_match(struct device *parent, struct cfdata *match, void *aux)
100 1.1 jmcneill {
101 1.1 jmcneill struct acpi_attach_args *aa = aux;
102 1.1 jmcneill const char *id;
103 1.1 jmcneill int i;
104 1.1 jmcneill
105 1.1 jmcneill if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
106 1.1 jmcneill return 0;
107 1.1 jmcneill
108 1.1 jmcneill for (i = 0; (id = fdc_acpi_ids[i]) != NULL; ++i) {
109 1.1 jmcneill if (strcmp(aa->aa_node->ad_devinfo.HardwareId, id) == 0)
110 1.1 jmcneill return 1;
111 1.1 jmcneill }
112 1.1 jmcneill
113 1.1 jmcneill /* No matches found */
114 1.1 jmcneill return 0;
115 1.1 jmcneill }
116 1.1 jmcneill
117 1.1 jmcneill /*
118 1.1 jmcneill * fdc_acpi_attach: autoconf(9) attach routine
119 1.1 jmcneill */
120 1.1 jmcneill void
121 1.1 jmcneill fdc_acpi_attach(struct device *parent, struct device *self, void *aux)
122 1.1 jmcneill {
123 1.1 jmcneill struct fdc_acpi_softc *asc = (struct fdc_acpi_softc *)self;
124 1.1 jmcneill struct fdc_softc *sc = &asc->sc_fdc;
125 1.1 jmcneill struct acpi_attach_args *aa = aux;
126 1.1 jmcneill struct acpi_resources res;
127 1.1 jmcneill struct acpi_io *io, *ctlio;
128 1.1 jmcneill struct acpi_irq *irq;
129 1.1 jmcneill struct acpi_drq *drq;
130 1.1 jmcneill ACPI_STATUS rv;
131 1.1 jmcneill
132 1.1 jmcneill printf("\n");
133 1.1 jmcneill
134 1.1 jmcneill sc->sc_ic = aa->aa_ic;
135 1.3 jmcneill asc->sc_node = aa->aa_node;
136 1.1 jmcneill
137 1.1 jmcneill /* parse resources */
138 1.1 jmcneill rv = acpi_resource_parse(&sc->sc_dev, aa->aa_node, &res,
139 1.1 jmcneill &acpi_resource_parse_ops_default);
140 1.1 jmcneill if (rv != AE_OK) {
141 1.1 jmcneill printf("%s: unable to parse resources\n", sc->sc_dev.dv_xname);
142 1.1 jmcneill return;
143 1.1 jmcneill }
144 1.1 jmcneill
145 1.1 jmcneill /* find our i/o registers */
146 1.1 jmcneill io = acpi_res_io(&res, 0);
147 1.1 jmcneill if (io == NULL) {
148 1.1 jmcneill printf("%s: unable to find i/o register resource\n",
149 1.1 jmcneill sc->sc_dev.dv_xname);
150 1.1 jmcneill return;
151 1.1 jmcneill }
152 1.1 jmcneill
153 1.1 jmcneill /* find our IRQ */
154 1.1 jmcneill irq = acpi_res_irq(&res, 0);
155 1.1 jmcneill if (irq == NULL) {
156 1.1 jmcneill printf("%s: unable to find irq resource\n",
157 1.1 jmcneill sc->sc_dev.dv_xname);
158 1.1 jmcneill return;
159 1.1 jmcneill }
160 1.1 jmcneill
161 1.1 jmcneill /* find our DRQ */
162 1.1 jmcneill drq = acpi_res_drq(&res, 0);
163 1.1 jmcneill if (drq == NULL) {
164 1.1 jmcneill printf("%s: unable to find drq resource\n",
165 1.1 jmcneill sc->sc_dev.dv_xname);
166 1.1 jmcneill return;
167 1.1 jmcneill }
168 1.2 jmcneill sc->sc_drq = drq->ar_drq;
169 1.1 jmcneill
170 1.1 jmcneill sc->sc_iot = aa->aa_iot;
171 1.1 jmcneill if (bus_space_map(sc->sc_iot, io->ar_base, io->ar_length,
172 1.1 jmcneill 0, &asc->sc_baseioh)) {
173 1.1 jmcneill printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
174 1.1 jmcneill return;
175 1.1 jmcneill }
176 1.1 jmcneill
177 1.1 jmcneill switch (io->ar_length) {
178 1.1 jmcneill case 4:
179 1.1 jmcneill sc->sc_ioh = asc->sc_baseioh;
180 1.1 jmcneill break;
181 1.1 jmcneill case 6:
182 1.1 jmcneill if (bus_space_subregion(sc->sc_iot, asc->sc_baseioh, 2, 4,
183 1.1 jmcneill &sc->sc_ioh)) {
184 1.1 jmcneill printf("%s: unable to subregion i/o space\n",
185 1.1 jmcneill sc->sc_dev.dv_xname);
186 1.1 jmcneill return;
187 1.1 jmcneill }
188 1.1 jmcneill break;
189 1.1 jmcneill default:
190 1.1 jmcneill printf("%s: unknown size: %d of io mapping\n",
191 1.1 jmcneill sc->sc_dev.dv_xname, io->ar_length);
192 1.1 jmcneill return;
193 1.1 jmcneill }
194 1.1 jmcneill
195 1.1 jmcneill /*
196 1.1 jmcneill * omitting the controller I/O port. (One has to exist for there to
197 1.1 jmcneill * be a working fdc). Just try and force the mapping in.
198 1.1 jmcneill */
199 1.1 jmcneill ctlio = acpi_res_io(&res, 1);
200 1.1 jmcneill if (ctlio == NULL) {
201 1.1 jmcneill if (bus_space_map(sc->sc_iot, io->ar_base + io->ar_length + 1,
202 1.1 jmcneill 1, 0, &sc->sc_fdctlioh)) {
203 1.1 jmcneill printf("%s: unable to force map ctl i/o space\n",
204 1.1 jmcneill sc->sc_dev.dv_xname);
205 1.1 jmcneill return;
206 1.1 jmcneill }
207 1.1 jmcneill printf("%s: ctl io %x did't probe. Forced attach\n",
208 1.1 jmcneill sc->sc_dev.dv_xname, io->ar_base + io->ar_length + 1);
209 1.1 jmcneill } else {
210 1.1 jmcneill if (bus_space_map(sc->sc_iot, ctlio->ar_base, ctlio->ar_length,
211 1.1 jmcneill 0, &sc->sc_fdctlioh)) {
212 1.1 jmcneill printf("%s: unable to map ctl i/o space\n",
213 1.1 jmcneill sc->sc_dev.dv_xname);
214 1.1 jmcneill return;
215 1.1 jmcneill }
216 1.1 jmcneill }
217 1.1 jmcneill
218 1.1 jmcneill sc->sc_ih = isa_intr_establish(aa->aa_ic, irq->ar_irq, 0,
219 1.1 jmcneill IPL_BIO, fdcintr, sc);
220 1.1 jmcneill
221 1.3 jmcneill /* Setup direct configuration of floppy drives */
222 1.3 jmcneill sc->sc_present = fdc_acpi_enumerate(asc);
223 1.3 jmcneill if (sc->sc_present >= 0) {
224 1.3 jmcneill sc->sc_known = 1;
225 1.3 jmcneill fdc_acpi_getknownfds(asc);
226 1.3 jmcneill fdcattach(sc);
227 1.3 jmcneill } else {
228 1.3 jmcneill printf("%s: unable to enumerate floppy drives\n",
229 1.3 jmcneill sc->sc_dev.dv_xname);
230 1.3 jmcneill }
231 1.3 jmcneill }
232 1.3 jmcneill
233 1.3 jmcneill static int
234 1.3 jmcneill fdc_acpi_enumerate(struct fdc_acpi_softc *asc)
235 1.3 jmcneill {
236 1.3 jmcneill struct fdc_softc *sc = &asc->sc_fdc;
237 1.3 jmcneill ACPI_OBJECT *fde;
238 1.3 jmcneill ACPI_BUFFER buf;
239 1.3 jmcneill ACPI_STATUS rv;
240 1.3 jmcneill UINT8 *p;
241 1.3 jmcneill int i, drives = -1;
242 1.3 jmcneill
243 1.3 jmcneill rv = acpi_eval_struct(asc->sc_node->ad_handle, "_FDE", &buf);
244 1.3 jmcneill if (rv != AE_OK) {
245 1.3 jmcneill printf("%s: failed to evalulate _FDE: %x\n",
246 1.3 jmcneill sc->sc_dev.dv_xname, rv);
247 1.3 jmcneill return drives;
248 1.3 jmcneill }
249 1.3 jmcneill fde = (ACPI_OBJECT *)buf.Pointer;
250 1.3 jmcneill if (fde->Type != ACPI_TYPE_BUFFER) {
251 1.3 jmcneill printf("%s: expected BUFFER, got %d\n", sc->sc_dev.dv_xname,
252 1.3 jmcneill fde->Type);
253 1.3 jmcneill goto out;
254 1.3 jmcneill }
255 1.3 jmcneill if (fde->Buffer.Length < 14) {
256 1.3 jmcneill printf("%s: expected buffer len of 14, got %d\n",
257 1.3 jmcneill sc->sc_dev.dv_xname, fde->Buffer.Length);
258 1.3 jmcneill goto out;
259 1.3 jmcneill }
260 1.3 jmcneill
261 1.3 jmcneill p = fde->Buffer.Pointer;
262 1.3 jmcneill /*
263 1.3 jmcneill * Indexes 0 through 3 are each UINT32 booleans. True if a drive
264 1.3 jmcneill * is present.
265 1.3 jmcneill */
266 1.3 jmcneill drives = 0;
267 1.3 jmcneill for (i = 0; i < 4; i++) {
268 1.3 jmcneill if (p[i]) drives |= (1 << i);
269 1.3 jmcneill #ifdef ACPI_FDC_DEBUG
270 1.3 jmcneill printf("%s: drive %d %sattached\n", sc->sc_dev.dv_xname, i,
271 1.3 jmcneill p[i] ? "" : "not ");
272 1.3 jmcneill #endif
273 1.3 jmcneill }
274 1.3 jmcneill
275 1.3 jmcneill /*
276 1.3 jmcneill * XXX p[4] reports tape presence. Possible values:
277 1.3 jmcneill * 0 - Unknown if device is present
278 1.3 jmcneill * 1 - Device is present
279 1.3 jmcneill * 2 - Device is never present
280 1.3 jmcneill * >2 - Reserved
281 1.3 jmcneill * How should we handle this?
282 1.3 jmcneill */
283 1.3 jmcneill
284 1.3 jmcneill out:
285 1.3 jmcneill AcpiOsFree(buf.Pointer);
286 1.3 jmcneill return drives;
287 1.3 jmcneill }
288 1.3 jmcneill
289 1.3 jmcneill static void
290 1.3 jmcneill fdc_acpi_getknownfds(struct fdc_acpi_softc *asc)
291 1.3 jmcneill {
292 1.3 jmcneill struct fdc_softc *sc = &asc->sc_fdc;
293 1.3 jmcneill ACPI_OBJECT *fdi, *e;
294 1.3 jmcneill ACPI_BUFFER buf;
295 1.3 jmcneill ACPI_STATUS rv;
296 1.3 jmcneill int i;
297 1.3 jmcneill
298 1.3 jmcneill for (i = 0; i < 4; i++) {
299 1.3 jmcneill if ((sc->sc_present & (1 << i)) == 0)
300 1.3 jmcneill continue;
301 1.3 jmcneill rv = acpi_eval_struct(asc->sc_node->ad_handle, "_FDI", &buf);
302 1.3 jmcneill if (rv != AE_OK) {
303 1.3 jmcneill printf("%s: failed to evalulate _FDI: %x on drive %d\n",
304 1.3 jmcneill sc->sc_dev.dv_xname, rv, i);
305 1.3 jmcneill continue;
306 1.3 jmcneill }
307 1.3 jmcneill fdi = (ACPI_OBJECT *)buf.Pointer;
308 1.3 jmcneill if (fdi->Type != ACPI_TYPE_PACKAGE) {
309 1.3 jmcneill printf("%s: expected PACKAGE, got %d\n",
310 1.3 jmcneill sc->sc_dev.dv_xname, fdi->Type);
311 1.3 jmcneill goto out;
312 1.3 jmcneill }
313 1.3 jmcneill e = fdi->Package.Elements;
314 1.3 jmcneill sc->sc_knownfds[i] = fdc_acpi_nvtotype(sc->sc_dev.dv_xname,
315 1.3 jmcneill e[1].Integer.Value, e[0].Integer.Value);
316 1.3 jmcneill /* XXX What to do if fdc_acpi_nvtotype returns NULL? */
317 1.3 jmcneill out:
318 1.3 jmcneill AcpiOsFree(buf.Pointer);
319 1.3 jmcneill }
320 1.3 jmcneill }
321 1.3 jmcneill
322 1.3 jmcneill static const struct fd_type *
323 1.3 jmcneill fdc_acpi_nvtotype(char *fdc, int nvraminfo, int drive)
324 1.3 jmcneill {
325 1.3 jmcneill int type;
326 1.3 jmcneill
327 1.3 jmcneill type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
328 1.3 jmcneill switch (type) {
329 1.3 jmcneill case ACPI_FDC_DISKETTE_NONE:
330 1.3 jmcneill return NULL;
331 1.3 jmcneill case ACPI_FDC_DISKETTE_12M:
332 1.3 jmcneill return &fdc_acpi_fdtypes[1];
333 1.3 jmcneill case ACPI_FDC_DISKETTE_TYPE5:
334 1.3 jmcneill case ACPI_FDC_DISKETTE_TYPE6:
335 1.3 jmcneill case ACPI_FDC_DISKETTE_144M:
336 1.3 jmcneill return &fdc_acpi_fdtypes[0];
337 1.3 jmcneill case ACPI_FDC_DISKETTE_360K:
338 1.3 jmcneill return &fdc_acpi_fdtypes[3];
339 1.3 jmcneill case ACPI_FDC_DISKETTE_720K:
340 1.3 jmcneill return &fdc_acpi_fdtypes[4];
341 1.3 jmcneill default:
342 1.3 jmcneill #ifdef ACPI_FDC_DEBUG
343 1.3 jmcneill printf("%s: drive %d: unknown device type 0x%x\n",
344 1.3 jmcneill fdc, drive, type);
345 1.3 jmcneill #endif
346 1.3 jmcneill return NULL;
347 1.3 jmcneill }
348 1.1 jmcneill }
349