fdc_acpi.c revision 1.41 1 /* $NetBSD: fdc_acpi.c,v 1.41 2010/08/07 09:55:59 jruoho 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.41 2010/08/07 09:55:59 jruoho Exp $");
35
36 #include <sys/param.h>
37 #include <sys/device.h>
38 #include <sys/disk.h>
39 #include <sys/systm.h>
40
41 #if NRND > 0
42 #include <sys/rnd.h>
43 #endif
44
45 #include <dev/acpi/acpireg.h>
46 #include <dev/acpi/acpivar.h>
47
48 #include <dev/isa/isadmavar.h>
49 #include <dev/isa/fdcvar.h>
50 #include <dev/isa/fdvar.h>
51 #include <dev/isa/fdreg.h>
52
53 #include <dev/acpi/fdc_acpireg.h>
54
55 #define _COMPONENT ACPI_RESOURCE_COMPONENT
56 ACPI_MODULE_NAME ("fdc_acpi")
57
58 static int fdc_acpi_match(device_t, cfdata_t, void *);
59 static void fdc_acpi_attach(device_t, device_t, void *);
60
61 struct fdc_acpi_softc {
62 struct fdc_softc sc_fdc;
63 bus_space_handle_t sc_baseioh;
64 struct acpi_devnode *sc_node; /* ACPI devnode */
65 };
66
67 static int fdc_acpi_enumerate(struct fdc_acpi_softc *);
68 static void fdc_acpi_getknownfds(struct fdc_acpi_softc *);
69
70 static const struct fd_type *fdc_acpi_nvtotype(const char *, int, int);
71
72 CFATTACH_DECL_NEW(fdc_acpi, sizeof(struct fdc_acpi_softc), fdc_acpi_match,
73 fdc_acpi_attach, NULL, NULL);
74
75 /*
76 * Supported device IDs
77 */
78
79 static const char * const fdc_acpi_ids[] = {
80 "PNP07??", /* PC standard floppy disk controller */
81 NULL
82 };
83
84 /*
85 * fdc_acpi_match: autoconf(9) match routine
86 */
87 static int
88 fdc_acpi_match(device_t parent, cfdata_t match, void *aux)
89 {
90 struct acpi_attach_args *aa = aux;
91
92 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
93 return 0;
94
95 return acpi_match_hid(aa->aa_node->ad_devinfo, fdc_acpi_ids);
96 }
97
98 /*
99 * fdc_acpi_attach: autoconf(9) attach routine
100 */
101 static void
102 fdc_acpi_attach(device_t parent, device_t self, void *aux)
103 {
104 struct fdc_acpi_softc *asc = device_private(self);
105 struct fdc_softc *sc = &asc->sc_fdc;
106 struct acpi_attach_args *aa = aux;
107 struct acpi_io *io, *ctlio;
108 struct acpi_irq *irq;
109 struct acpi_drq *drq;
110 struct acpi_resources res;
111 ACPI_STATUS rv;
112
113 sc->sc_dev = self;
114 sc->sc_ic = aa->aa_ic;
115 asc->sc_node = aa->aa_node;
116
117 /* parse resources */
118 rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
119 &res, &acpi_resource_parse_ops_default);
120 if (ACPI_FAILURE(rv))
121 return;
122
123 /* find our i/o registers */
124 io = acpi_res_io(&res, 0);
125 if (io == NULL) {
126 aprint_error_dev(sc->sc_dev,
127 "unable to find i/o register resource\n");
128 goto out;
129 }
130
131 /* find our IRQ */
132 irq = acpi_res_irq(&res, 0);
133 if (irq == NULL) {
134 aprint_error_dev(sc->sc_dev, "unable to find irq resource\n");
135 goto out;
136 }
137
138 /* find our DRQ */
139 drq = acpi_res_drq(&res, 0);
140 if (drq == NULL) {
141 aprint_error_dev(sc->sc_dev, "unable to find drq resource\n");
142 goto out;
143 }
144 sc->sc_drq = drq->ar_drq;
145
146 sc->sc_iot = aa->aa_iot;
147 if (bus_space_map(sc->sc_iot, io->ar_base, io->ar_length,
148 0, &asc->sc_baseioh)) {
149 aprint_error_dev(sc->sc_dev, "can't map i/o space\n");
150 goto out;
151 }
152
153 switch (io->ar_length) {
154 case 4:
155 sc->sc_ioh = asc->sc_baseioh;
156 break;
157 case 6:
158 if (bus_space_subregion(sc->sc_iot, asc->sc_baseioh, 2, 4,
159 &sc->sc_ioh)) {
160 aprint_error_dev(sc->sc_dev,
161 "unable to subregion i/o space\n");
162 goto out;
163 }
164 break;
165 default:
166 aprint_error_dev(sc->sc_dev,
167 "unknown size: %d of io mapping\n", io->ar_length);
168 goto out;
169 }
170
171 /*
172 * omitting the controller I/O port. (One has to exist for there to
173 * be a working fdc). Just try and force the mapping in.
174 */
175 ctlio = acpi_res_io(&res, 1);
176 if (ctlio == NULL) {
177 if (bus_space_map(sc->sc_iot, io->ar_base + io->ar_length + 1,
178 1, 0, &sc->sc_fdctlioh)) {
179 aprint_error_dev(sc->sc_dev,
180 "unable to force map ctl i/o space\n");
181 goto out;
182 }
183 aprint_verbose_dev(sc->sc_dev,
184 "ctl io %x did't probe. Forced attach\n",
185 io->ar_base + io->ar_length + 1);
186 } else {
187 if (bus_space_map(sc->sc_iot, ctlio->ar_base, ctlio->ar_length,
188 0, &sc->sc_fdctlioh)) {
189 aprint_error_dev(sc->sc_dev,
190 "unable to map ctl i/o space\n");
191 goto out;
192 }
193 }
194
195 sc->sc_ih = isa_intr_establish(aa->aa_ic, irq->ar_irq,
196 (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL,
197 IPL_BIO, fdcintr, sc);
198
199 /* Setup direct configuration of floppy drives */
200 sc->sc_present = fdc_acpi_enumerate(asc);
201 if (sc->sc_present >= 0) {
202 sc->sc_known = 1;
203 fdc_acpi_getknownfds(asc);
204 } else {
205 /*
206 * XXX if there is no _FDE control method, attempt to
207 * probe without pnp
208 */
209 aprint_debug_dev(sc->sc_dev,
210 "unable to enumerate, attempting normal probe\n");
211 }
212
213 fdcattach(sc);
214
215 out:
216 acpi_resource_cleanup(&res);
217 }
218
219 static int
220 fdc_acpi_enumerate(struct fdc_acpi_softc *asc)
221 {
222 struct fdc_softc *sc = &asc->sc_fdc;
223 ACPI_OBJECT *fde;
224 ACPI_BUFFER abuf;
225 ACPI_STATUS rv;
226 uint32_t *p;
227 int i, drives = -1;
228
229 rv = acpi_eval_struct(asc->sc_node->ad_handle, "_FDE", &abuf);
230
231 if (ACPI_FAILURE(rv)) {
232 aprint_normal_dev(sc->sc_dev, "failed to evaluate _FDE: %s\n",
233 AcpiFormatException(rv));
234 return drives;
235 }
236 fde = abuf.Pointer;
237 if (fde->Type != ACPI_TYPE_BUFFER) {
238 aprint_error_dev(sc->sc_dev, "expected BUFFER, got %u\n",
239 fde->Type);
240 goto out;
241 }
242 if (fde->Buffer.Length < 5 * sizeof(uint32_t)) {
243 aprint_error_dev(sc->sc_dev,
244 "expected buffer len of %lu, got %u\n",
245 (unsigned long)(5 * sizeof(uint32_t)), fde->Buffer.Length);
246 goto out;
247 }
248
249 p = (uint32_t *)fde->Buffer.Pointer;
250
251 /*
252 * Indexes 0 through 3 are each uint32_t booleans. True if a drive
253 * is present.
254 */
255 drives = 0;
256 for (i = 0; i < 4; i++) {
257 if (p[i]) drives |= (1 << i);
258 aprint_normal_dev(sc->sc_dev, "drive %d %sattached\n", i,
259 p[i] ? "" : "not ");
260 }
261
262 /*
263 * p[4] reports tape presence. Possible values:
264 * 0 - Unknown if device is present
265 * 1 - Device is present
266 * 2 - Device is never present
267 * >2 - Reserved
268 *
269 * we don't currently use this.
270 */
271
272 out:
273 ACPI_FREE(abuf.Pointer);
274 return drives;
275 }
276
277 static void
278 fdc_acpi_getknownfds(struct fdc_acpi_softc *asc)
279 {
280 struct fdc_softc *sc = &asc->sc_fdc;
281 ACPI_OBJECT *fdi, *e;
282 ACPI_BUFFER abuf;
283 ACPI_STATUS rv;
284 int i;
285
286 for (i = 0; i < 4; i++) {
287 if ((sc->sc_present & (1 << i)) == 0)
288 continue;
289 rv = acpi_eval_struct(asc->sc_node->ad_handle, "_FDI", &abuf);
290 if (ACPI_FAILURE(rv)) {
291 aprint_normal_dev(sc->sc_dev,
292 "failed to evaluate _FDI: %s on drive %d\n",
293 AcpiFormatException(rv), i);
294 /* XXX if _FDI fails, assume 1.44MB floppy */
295 sc->sc_knownfds[i] = &fdc_acpi_fdtypes[0];
296 continue;
297 }
298 fdi = abuf.Pointer;
299 if (fdi->Type != ACPI_TYPE_PACKAGE) {
300 aprint_error_dev(sc->sc_dev,
301 "expected PACKAGE, got %u\n", fdi->Type);
302 goto out;
303 }
304 e = fdi->Package.Elements;
305 sc->sc_knownfds[i] = fdc_acpi_nvtotype(
306 device_xname(sc->sc_dev),
307 e[1].Integer.Value, e[0].Integer.Value);
308
309 /* if fdc_acpi_nvtotype returns NULL, don't attach drive */
310 if (!sc->sc_knownfds[i])
311 sc->sc_present &= ~(1 << i);
312
313 out:
314 ACPI_FREE(abuf.Pointer);
315 }
316 }
317
318 static const struct fd_type *
319 fdc_acpi_nvtotype(const char *fdc, int nvraminfo, int drive)
320 {
321 int type;
322
323 type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
324 switch (type) {
325 case ACPI_FDC_DISKETTE_NONE:
326 return NULL;
327 case ACPI_FDC_DISKETTE_12M:
328 return &fdc_acpi_fdtypes[1];
329 case ACPI_FDC_DISKETTE_TYPE5:
330 case ACPI_FDC_DISKETTE_TYPE6:
331 case ACPI_FDC_DISKETTE_144M:
332 return &fdc_acpi_fdtypes[0];
333 case ACPI_FDC_DISKETTE_360K:
334 return &fdc_acpi_fdtypes[3];
335 case ACPI_FDC_DISKETTE_720K:
336 return &fdc_acpi_fdtypes[4];
337 default:
338 aprint_normal("%s: drive %d: unknown device type 0x%x\n",
339 fdc, drive, type);
340 return NULL;
341 }
342 }
343