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