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