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