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