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