xref: /netbsd-src/sys/dev/acpi/fdc_acpi.c (revision 4e6df137e8e14049b5a701d249962c480449c141)
1 /* $NetBSD: fdc_acpi.c,v 1.38 2010/03/05 14:00:17 jruoho Exp $ */
2 
3 /*
4  * Copyright (c) 2002 Jared D. McNeill <jmcneill@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.38 2010/03/05 14:00:17 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 #ifdef ACPI_FDC_DEBUG
210 		aprint_debug_dev(sc->sc_dev,
211 		    "unable to enumerate, attempting normal probe\n");
212 #endif
213 	}
214 
215 	fdcattach(sc);
216 
217  out:
218 	acpi_resource_cleanup(&res);
219 }
220 
221 static int
222 fdc_acpi_enumerate(struct fdc_acpi_softc *asc)
223 {
224 	struct fdc_softc *sc = &asc->sc_fdc;
225 	ACPI_OBJECT *fde;
226 	ACPI_BUFFER abuf;
227 	ACPI_STATUS rv;
228 	UINT32 *p;
229 	int i, drives = -1;
230 
231 	rv = acpi_eval_struct(asc->sc_node->ad_handle, "_FDE", &abuf);
232 	if (ACPI_FAILURE(rv)) {
233 #ifdef ACPI_FDC_DEBUG
234 		aprint_normal_dev(sc->sc_dev, "failed to evaluate _FDE: %s\n",
235 		    AcpiFormatException(rv));
236 #endif
237 		return drives;
238 	}
239 	fde = (ACPI_OBJECT *)abuf.Pointer;
240 	if (fde->Type != ACPI_TYPE_BUFFER) {
241 		aprint_error_dev(sc->sc_dev, "expected BUFFER, got %u\n",
242 		    fde->Type);
243 		goto out;
244 	}
245 	if (fde->Buffer.Length < 5 * sizeof(UINT32)) {
246 		aprint_error_dev(sc->sc_dev,
247 		    "expected buffer len of %lu, got %u\n",
248 		    (unsigned long)(5 * sizeof(UINT32)), fde->Buffer.Length);
249 		goto out;
250 	}
251 
252 	p = (UINT32 *) fde->Buffer.Pointer;
253 
254 	/*
255 	 * Indexes 0 through 3 are each UINT32 booleans. True if a drive
256 	 * is present.
257 	 */
258 	drives = 0;
259 	for (i = 0; i < 4; i++) {
260 		if (p[i]) drives |= (1 << i);
261 #ifdef ACPI_FDC_DEBUG
262 		aprint_normal_dev(sc->sc_dev, "drive %d %sattached\n", i,
263 		    p[i] ? "" : "not ");
264 #endif
265 	}
266 
267 	/*
268 	 * p[4] reports tape presence. Possible values:
269 	 * 	0	- Unknown if device is present
270 	 *	1	- Device is present
271 	 *	2	- Device is never present
272 	 *	>2	- Reserved
273 	 *
274 	 * we don't currently use this.
275 	 */
276 
277 out:
278 	ACPI_FREE(abuf.Pointer);
279 	return drives;
280 }
281 
282 static void
283 fdc_acpi_getknownfds(struct fdc_acpi_softc *asc)
284 {
285 	struct fdc_softc *sc = &asc->sc_fdc;
286 	ACPI_OBJECT *fdi, *e;
287 	ACPI_BUFFER abuf;
288 	ACPI_STATUS rv;
289 	int i;
290 
291 	for (i = 0; i < 4; i++) {
292 		if ((sc->sc_present & (1 << i)) == 0)
293 			continue;
294 		rv = acpi_eval_struct(asc->sc_node->ad_handle, "_FDI", &abuf);
295 		if (ACPI_FAILURE(rv)) {
296 #ifdef ACPI_FDC_DEBUG
297 			aprint_normal_dev(sc->sc_dev,
298 			    "failed to evaluate _FDI: %s on drive %d\n",
299 			    AcpiFormatException(rv), i);
300 #endif
301 			/* XXX if _FDI fails, assume 1.44MB floppy */
302 			sc->sc_knownfds[i] = &fdc_acpi_fdtypes[0];
303 			continue;
304 		}
305 		fdi = (ACPI_OBJECT *)abuf.Pointer;
306 		if (fdi->Type != ACPI_TYPE_PACKAGE) {
307 			aprint_error_dev(sc->sc_dev,
308 			    "expected PACKAGE, got %u\n", fdi->Type);
309 			goto out;
310 		}
311 		e = fdi->Package.Elements;
312 		sc->sc_knownfds[i] = fdc_acpi_nvtotype(
313 		    device_xname(sc->sc_dev),
314 		    e[1].Integer.Value, e[0].Integer.Value);
315 
316 		/* if fdc_acpi_nvtotype returns NULL, don't attach drive */
317 		if (!sc->sc_knownfds[i])
318 			sc->sc_present &= ~(1 << i);
319 
320 out:
321 		ACPI_FREE(abuf.Pointer);
322 	}
323 }
324 
325 static const struct fd_type *
326 fdc_acpi_nvtotype(const char *fdc, int nvraminfo, int drive)
327 {
328 	int type;
329 
330 	type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
331 	switch (type) {
332 	case ACPI_FDC_DISKETTE_NONE:
333 		return NULL;
334 	case ACPI_FDC_DISKETTE_12M:
335 		return &fdc_acpi_fdtypes[1];
336 	case ACPI_FDC_DISKETTE_TYPE5:
337 	case ACPI_FDC_DISKETTE_TYPE6:
338 	case ACPI_FDC_DISKETTE_144M:
339 		return &fdc_acpi_fdtypes[0];
340 	case ACPI_FDC_DISKETTE_360K:
341 		return &fdc_acpi_fdtypes[3];
342 	case ACPI_FDC_DISKETTE_720K:
343 		return &fdc_acpi_fdtypes[4];
344 	default:
345 #ifdef ACPI_FDC_DEBUG
346 		aprint_normal("%s: drive %d: unknown device type 0x%x\n",
347 		    fdc, drive, type);
348 #endif
349 		return NULL;
350 	}
351 }
352