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