xref: /freebsd/sys/dev/fdc/fdc_acpi.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
1 /*-
2  * Copyright (c) 2004 Nate Lawson (SDG)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *	notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *	notice, this list of conditions and the following disclaimer in the
12  *	documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/bio.h>
33 #include <sys/bus.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
36 #include <sys/proc.h>
37 
38 #include "acpi.h"
39 #include <dev/acpica/acpivar.h>
40 #include <dev/fdc/fdcreg.h>
41 #include <dev/fdc/fdcvar.h>
42 
43 static int		fdc_acpi_probe(device_t dev);
44 static int		fdc_acpi_attach(device_t dev);
45 static int		fdc_acpi_probe_children(device_t bus, device_t dev,
46 			    void *fde);
47 static ACPI_STATUS	fdc_acpi_probe_child(ACPI_HANDLE h, device_t *dev,
48 			    int level, void *arg);
49 static void		fdctl_wr_acpi(fdc_p fdc, u_int8_t v);
50 
51 /* Maximum number of child devices of a controller (4 floppy + 1 tape.) */
52 #define ACPI_FDC_MAXDEVS	5
53 
54 /*
55  * Parameters for the tape drive (5th device).  Some BIOS authors use this
56  * for all drives, not just the tape drive (e.g., ASUS K8V).  This isn't
57  * grossly incompatible with the spec since it says the first four devices
58  * are simple booleans.
59  */
60 #define ACPI_FD_UNKNOWN		0
61 #define ACPI_FD_PRESENT		1
62 #define ACPI_FD_NEVER_PRESENT	2
63 
64 /* Temporary buf length for evaluating _FDE and _FDI. */
65 #define ACPI_FDC_BUFLEN		1024
66 
67 /* Context for walking FDC child devices. */
68 struct fdc_walk_ctx {
69 	uint32_t	fd_present[ACPI_FDC_MAXDEVS];
70 	int		index;
71 	device_t	acpi_dev;
72 	device_t	dev;
73 };
74 
75 static void
76 fdctl_wr_acpi(fdc_p fdc, u_int8_t v)
77 {
78 	bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v);
79 }
80 
81 static int
82 fdc_acpi_probe(device_t dev)
83 {
84 	device_t bus;
85 	static char *fdc_ids[] = { "PNP0700", "PNP0701", NULL };
86 
87 	bus = device_get_parent(dev);
88 	if (ACPI_ID_PROBE(bus, dev, fdc_ids) == NULL)
89 		return (ENXIO);
90 
91 	if (ACPI_SUCCESS(ACPI_EVALUATE_OBJECT(bus, dev, "_FDE", NULL, NULL)))
92 		device_set_desc(dev, "floppy drive controller (FDE)");
93 	else
94 		device_set_desc(dev, "floppy drive controller");
95 	return (0);
96 }
97 
98 static int
99 fdc_acpi_attach(device_t dev)
100 {
101 	struct fdc_data *sc;
102 	ACPI_BUFFER buf;
103 	device_t bus;
104 	int error, i, ic_type;
105 	ACPI_OBJECT *obj, *pkg;
106 	ACPI_HANDLE h;
107 	uint32_t *fde;
108 
109 	/* Get our softc and use the same accessor as ISA. */
110 	sc = device_get_softc(dev);
111 	sc->fdc_dev = dev;
112 	sc->fdctl_wr = fdctl_wr_acpi;
113 	sc->flags |= FDC_ISPNP;
114 
115 	/* Initialize variables and get a temporary buffer for _FDE. */
116 	error = ENXIO;
117 	h = acpi_get_handle(dev);
118 	buf.Length = ACPI_FDC_BUFLEN;
119 	buf.Pointer = malloc(buf.Length, M_TEMP, M_NOWAIT | M_ZERO);
120 	if (buf.Pointer == NULL)
121 		goto out;
122 
123 	/* Allocate resources the same as the ISA attachment. */
124 	error = fdc_isa_alloc_resources(dev, sc);
125 	if (error != 0)
126 		goto out;
127 
128 	/* Call common attach code in fdc(4) first. */
129 	error = fdc_attach(dev);
130 	if (error != 0)
131 		goto out;
132 
133 	/* Check that the controller is working and get its type. */
134 	error = fdc_initial_reset(sc);
135 	if (error)
136 		goto out;
137 	if (fd_cmd(sc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
138 		ic_type = (u_char)ic_type;
139 		switch (ic_type) {
140 		case 0x80:
141 			sc->fdct = FDC_NE765;
142 			break;
143 		case 0x81:	/* not mentioned in any hardware doc */
144 		case 0x90:
145 			sc->fdct = FDC_ENHANCED;
146 			break;
147 		default:
148 			sc->fdct = FDC_UNKNOWN;
149 			break;
150 		}
151 	}
152 
153 	/*
154 	 * Enumerate _FDE, which lists floppy drives that are present.  If
155 	 * this fails, fall back to the ISA hints-based probe method.
156 	 */
157 	bus = device_get_parent(dev);
158 	if (ACPI_SUCCESS(ACPI_EVALUATE_OBJECT(bus, dev, "_FDE", NULL, &buf))) {
159 		/*
160 		 * In violation of the spec, systems including the ASUS K8V
161 		 * return a package of five integers instead of a buffer of
162 		 * five 32-bit integers.
163 		 */
164 		fde = (uint32_t *)buf.Pointer;
165 		pkg = (ACPI_OBJECT *)buf.Pointer;
166 		if (pkg->Type == ACPI_TYPE_PACKAGE) {
167 			fde = malloc(pkg->Package.Count * sizeof(uint32_t),
168 			    M_TEMP, M_NOWAIT | M_ZERO);
169 			if (fde == NULL) {
170 				error = ENOMEM;
171 				goto out;
172 			}
173 			for (i = 0; i < pkg->Package.Count; i++) {
174 				obj = &pkg->Package.Elements[i];
175 				if (obj->Type == ACPI_TYPE_INTEGER)
176 					fde[i] = (uint32_t)obj->Integer.Value;
177 			}
178 		}
179 		error = fdc_acpi_probe_children(bus, dev, fde);
180 		if (pkg->Type == ACPI_TYPE_PACKAGE)
181 			free(fde, M_TEMP);
182 	} else
183 		error = fdc_hints_probe(dev);
184 
185 out:
186 	if (buf.Pointer)
187 		free(buf.Pointer, M_TEMP);
188 	if (error != 0)
189 		fdc_release_resources(sc);
190 
191 	return (error);
192 }
193 
194 static int
195 fdc_acpi_probe_children(device_t bus, device_t dev, void *fde)
196 {
197 	struct fdc_walk_ctx *ctx;
198 	devclass_t fd_dc;
199 	int i;
200 
201 	/* Setup the context and walk all child devices. */
202 	ctx = malloc(sizeof(struct fdc_walk_ctx), M_TEMP, M_NOWAIT);
203 	if (ctx == NULL) {
204 		device_printf(dev, "no memory for walking children\n");
205 		return (ENOMEM);
206 	}
207 	bcopy(fde, ctx->fd_present, sizeof(ctx->fd_present));
208 	ctx->index = 0;
209 	ctx->dev = dev;
210 	ctx->acpi_dev = bus;
211 	ACPI_SCAN_CHILDREN(ctx->acpi_dev, dev, 1, fdc_acpi_probe_child,
212 	    ctx);
213 
214 	/* Add any devices not represented by an AML Device handle/node. */
215 	fd_dc = devclass_find("fd");
216 	for (i = 0; i < ACPI_FDC_MAXDEVS; i++)
217 		if (ctx->fd_present[i] == ACPI_FD_PRESENT &&
218 		    devclass_get_device(fd_dc, i) == NULL) {
219 			if (fdc_add_child(dev, "fd", i) == NULL)
220 				device_printf(dev, "fd add failed\n");
221 		}
222 	free(ctx, M_TEMP);
223 
224 	/* Attach any children found during the probe. */
225 	return (bus_generic_attach(dev));
226 }
227 
228 static ACPI_STATUS
229 fdc_acpi_probe_child(ACPI_HANDLE h, device_t *dev, int level, void *arg)
230 {
231 	struct fdc_walk_ctx *ctx;
232 	device_t child;
233 	ACPI_BUFFER buf;
234 	ACPI_OBJECT *pkg, *obj;
235 	ACPI_STATUS status;
236 
237 	ctx = (struct fdc_walk_ctx *)arg;
238 	buf.Pointer = NULL;
239 
240 	/*
241 	 * The first four ints are booleans that indicate whether fd0-3 are
242 	 * present or not.  The last is for a tape device, which we don't
243 	 * bother supporting for now.
244 	 */
245 	if (ctx->index > 3)
246 		return (AE_OK);
247 
248 	/* This device is not present, move on to the next. */
249 	if (ctx->fd_present[ctx->index] != ACPI_FD_PRESENT)
250 		goto out;
251 
252 	/* Create a device for the child with the given index. */
253 	child = fdc_add_child(ctx->dev, "fd", ctx->index);
254 	if (child == NULL)
255 		goto out;
256 	*dev = child;
257 
258 	/* Get temporary buffer for _FDI probe. */
259 	buf.Length = ACPI_FDC_BUFLEN;
260 	buf.Pointer = malloc(buf.Length, M_TEMP, M_NOWAIT | M_ZERO);
261 	if (buf.Pointer == NULL)
262 		goto out;
263 
264 	/* Evaluate _FDI to get drive type to pass to the child. */
265 	status = ACPI_EVALUATE_OBJECT(ctx->acpi_dev, *dev, "_FDI", NULL, &buf);
266 	if (ACPI_FAILURE(status)) {
267 		if (status != AE_NOT_FOUND)
268 			device_printf(ctx->dev, "_FDI failed - %#x\n", status);
269 		goto out;
270 	}
271 	pkg = (ACPI_OBJECT *)buf.Pointer;
272 	if (!ACPI_PKG_VALID(pkg, 16)) {
273 		device_printf(ctx->dev, "invalid _FDI package\n");
274 		goto out;
275 	}
276 	obj = &pkg->Package.Elements[1];
277 	if (obj == NULL || obj->Type != ACPI_TYPE_INTEGER) {
278 		device_printf(ctx->dev, "invalid type object in _FDI\n");
279 		goto out;
280 	}
281 	fdc_set_fdtype(child, obj->Integer.Value);
282 
283 out:
284 	ctx->index++;
285 	if (buf.Pointer)
286 		free(buf.Pointer, M_TEMP);
287 	return (AE_OK);
288 }
289 
290 static device_method_t fdc_acpi_methods[] = {
291 	/* Device interface */
292 	DEVMETHOD(device_probe,		fdc_acpi_probe),
293 	DEVMETHOD(device_attach,	fdc_acpi_attach),
294 	DEVMETHOD(device_detach,	fdc_detach),
295 
296 	/* Bus interface */
297 	DEVMETHOD(bus_print_child,	fdc_print_child),
298 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
299 	DEVMETHOD(bus_write_ivar,	fdc_write_ivar),
300 
301 	{0, 0}
302 };
303 
304 static driver_t fdc_acpi_driver = {
305 	"fdc",
306 	fdc_acpi_methods,
307 	sizeof(struct fdc_data)
308 };
309 
310 DRIVER_MODULE(fdc, acpi, fdc_acpi_driver, fdc_devclass, 0, 0);
311