summaryrefslogtreecommitdiff
path: root/sys/dev/acpi/fdc_acpi.c
blob: 8af9f30b487ec4258c34fb8b1b3ad67f40e75c19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* $NetBSD: fdc_acpi.c,v 1.47 2021/10/15 19:21:45 jmcneill Exp $ */

/*
 * Copyright (c) 2002 Jared D. McNeill <jmcneill@invisible.ca>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * ACPI attachment for the PC Floppy Controller driver, based on
 * sys/arch/i386/pnpbios/fdc_pnpbios.c by Jason R. Thorpe
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: fdc_acpi.c,v 1.47 2021/10/15 19:21:45 jmcneill Exp $");

#include <sys/param.h>
#include <sys/device.h>
#include <sys/disk.h>
#include <sys/systm.h>

#include <dev/acpi/acpireg.h>
#include <dev/acpi/acpivar.h>
#include <dev/acpi/acpi_intr.h>

#include <dev/isa/isadmavar.h>
#include <dev/isa/fdcvar.h>
#include <dev/isa/fdvar.h>
#include <dev/isa/fdreg.h>

#include <dev/acpi/fdc_acpireg.h>

#define _COMPONENT          ACPI_RESOURCE_COMPONENT
ACPI_MODULE_NAME            ("fdc_acpi")

static int	fdc_acpi_match(device_t, cfdata_t, void *);
static void	fdc_acpi_attach(device_t, device_t, void *);

struct fdc_acpi_softc {
	struct fdc_softc sc_fdc;
	bus_space_handle_t sc_baseioh;
	struct acpi_devnode *sc_node;	/* ACPI devnode */
};

static int	fdc_acpi_enumerate(struct fdc_acpi_softc *);
static void	fdc_acpi_getknownfds(struct fdc_acpi_softc *);

static const struct fd_type *fdc_acpi_nvtotype(const char *, int, int);

CFATTACH_DECL_NEW(fdc_acpi, sizeof(struct fdc_acpi_softc), fdc_acpi_match,
    fdc_acpi_attach, NULL, NULL);

/*
 * Supported device IDs
 */

static const struct device_compatible_entry compat_data[] = {
	{ .compat = "PNP07??" }, /* PC standard floppy disk controller */
	DEVICE_COMPAT_EOL
};

/*
 * fdc_acpi_match: autoconf(9) match routine
 */
static int
fdc_acpi_match(device_t parent, cfdata_t match, void *aux)
{
	struct acpi_attach_args *aa = aux;

	return acpi_compatible_match(aa, compat_data);
}

/*
 * fdc_acpi_attach: autoconf(9) attach routine
 */
static void
fdc_acpi_attach(device_t parent, device_t self, void *aux)
{
	struct fdc_acpi_softc *asc = device_private(self);
	struct fdc_softc *sc = &asc->sc_fdc;
	struct acpi_attach_args *aa = aux;
	struct acpi_io *io, *ctlio;
	struct acpi_drq *drq;
	struct acpi_resources res;
	ACPI_STATUS rv;

	sc->sc_dev = self;
	sc->sc_ic = aa->aa_ic;
	asc->sc_node = aa->aa_node;

	/* parse resources */
	rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
	    &res, &acpi_resource_parse_ops_default);
	if (ACPI_FAILURE(rv))
		return;

	/* find our i/o registers */
	io = acpi_res_io(&res, 0);
	if (io == NULL) {
		aprint_error_dev(sc->sc_dev,
		    "unable to find i/o register resource\n");
		goto out;
	}

	/* find our DRQ */
	drq = acpi_res_drq(&res, 0);
	if (drq == NULL) {
		aprint_error_dev(sc->sc_dev, "unable to find drq resource\n");
		goto out;
	}
	sc->sc_drq = drq->ar_drq;

	sc->sc_iot = aa->aa_iot;
	if (bus_space_map(sc->sc_iot, io->ar_base, io->ar_length,
		    0, &asc->sc_baseioh)) {
		aprint_error_dev(sc->sc_dev, "can't map i/o space\n");
		goto out;
	}

	switch (io->ar_length) {
	case 4:
		sc->sc_ioh = asc->sc_baseioh;
		break;
	case 6:
		if (bus_space_subregion(sc->sc_iot, asc->sc_baseioh, 2, 4,
		    &sc->sc_ioh)) {
			aprint_error_dev(sc->sc_dev,
			    "unable to subregion i/o space\n");
			goto out;
		}
		break;
	default:
		aprint_error_dev(sc->sc_dev,
		    "unknown size: %d of io mapping\n", io->ar_length);
		goto out;
	}

	/*
	 * omitting the controller I/O port. (One has to exist for there to
	 * be a working fdc). Just try and force the mapping in.
	 */
	ctlio = acpi_res_io(&res, 1);
	if (ctlio == NULL) {
		if (bus_space_map(sc->sc_iot, io->ar_base + io->ar_length + 1,
		    1, 0, &sc->sc_fdctlioh)) {
			aprint_error_dev(sc->sc_dev,
			    "unable to force map ctl i/o space\n");
			goto out;
		}
		aprint_verbose_dev(sc->sc_dev,
		    "ctl io %x did't probe. Forced attach\n",
		    io->ar_base + io->ar_length + 1);
	} else {
		if (bus_space_map(sc->sc_iot, ctlio->ar_base, ctlio->ar_length,
		    0, &sc->sc_fdctlioh)) {
			aprint_error_dev(sc->sc_dev,
			    "unable to map ctl i/o space\n");
			goto out;
		}
	}

	sc->sc_ih = acpi_intr_establish(self,
	    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
	    IPL_BIO, false, fdcintr, sc, device_xname(self));
	if (sc->sc_ih == NULL) {
		aprint_error_dev(sc->sc_dev, "unable to establish interrupt\n");
		goto out;
	}

	/* Setup direct configuration of floppy drives */
	sc->sc_present = fdc_acpi_enumerate(asc);
	if (sc->sc_present >= 0) {
		sc->sc_known = 1;
		fdc_acpi_getknownfds(asc);
	} else {
		/*
		 * XXX if there is no _FDE control method, attempt to
		 * probe without pnp
		 */
		aprint_debug_dev(sc->sc_dev,
		    "unable to enumerate, attempting normal probe\n");
	}

	fdcattach(sc);

 out:
	acpi_resource_cleanup(&res);
}

static int
fdc_acpi_enumerate(struct fdc_acpi_softc *asc)
{
	struct fdc_softc *sc = &asc->sc_fdc;
	ACPI_OBJECT *fde;
	ACPI_BUFFER abuf;
	ACPI_STATUS rv;
	uint32_t *p;
	int i, drives = -1;

	rv = acpi_eval_struct(asc->sc_node->ad_handle, "_FDE", &abuf);

	if (ACPI_FAILURE(rv)) {
		aprint_normal_dev(sc->sc_dev, "failed to evaluate _FDE: %s\n",
		    AcpiFormatException(rv));
		return drives;
	}
	fde = abuf.Pointer;
	if (fde->Type != ACPI_TYPE_BUFFER) {
		aprint_error_dev(sc->sc_dev, "expected BUFFER, got %u\n",
		    fde->Type);
		goto out;
	}
	if (fde->Buffer.Length < 5 * sizeof(uint32_t)) {
		aprint_error_dev(sc->sc_dev,
		    "expected buffer len of %lu, got %u\n",
		    (unsigned long)(5 * sizeof(uint32_t)), fde->Buffer.Length);
		goto out;
	}

	p = (uint32_t *)fde->Buffer.Pointer;

	/*
	 * Indexes 0 through 3 are each uint32_t booleans. True if a drive
	 * is present.
	 */
	drives = 0;
	for (i = 0; i < 4; i++) {
		if (p[i]) drives |= (1 << i);
		aprint_debug_dev(sc->sc_dev, "drive %d %sattached\n", i,
		    p[i] ? "" : "not ");
	}

	/*
	 * p[4] reports tape presence. Possible values:
	 * 	0	- Unknown if device is present
	 *	1	- Device is present
	 *	2	- Device is never present
	 *	>2	- Reserved
	 *
	 * we don't currently use this.
	 */

out:
	ACPI_FREE(abuf.Pointer);
	return drives;
}

static void
fdc_acpi_getknownfds(struct fdc_acpi_softc *asc)
{
	struct fdc_softc *sc = &asc->sc_fdc;
	ACPI_OBJECT *fdi, *e;
	ACPI_BUFFER abuf;
	ACPI_STATUS rv;
	int i;

	for (i = 0; i < 4; i++) {
		if ((sc->sc_present & (1 << i)) == 0)
			continue;
		rv = acpi_eval_struct(asc->sc_node->ad_handle, "_FDI", &abuf);
		if (ACPI_FAILURE(rv)) {
			/* if _FDI fails, assume 1.44MB floppy */
			sc->sc_knownfds[i] = &fdc_acpi_fdtypes[0];
			continue;
		}
		fdi = abuf.Pointer;
		if (fdi->Type != ACPI_TYPE_PACKAGE) {
			aprint_error_dev(sc->sc_dev,
			    "_FDI: expected PACKAGE, got %u\n", fdi->Type);
			goto out;
		}
		e = fdi->Package.Elements;
		sc->sc_knownfds[i] = fdc_acpi_nvtotype(
		    device_xname(sc->sc_dev),
		    e[1].Integer.Value, e[0].Integer.Value);

		/* if fdc_acpi_nvtotype returns NULL, don't attach drive */
		if (!sc->sc_knownfds[i])
			sc->sc_present &= ~(1 << i);

out:
		ACPI_FREE(abuf.Pointer);
	}
}

static const struct fd_type *
fdc_acpi_nvtotype(const char *fdc, int nvraminfo, int drive)
{
	int type;

	type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
	switch (type) {
	case ACPI_FDC_DISKETTE_NONE:
		return NULL;
	case ACPI_FDC_DISKETTE_12M:
		return &fdc_acpi_fdtypes[1];
	case ACPI_FDC_DISKETTE_TYPE5:
	case ACPI_FDC_DISKETTE_TYPE6:
	case ACPI_FDC_DISKETTE_144M:
		return &fdc_acpi_fdtypes[0];
	case ACPI_FDC_DISKETTE_360K:
		return &fdc_acpi_fdtypes[3];
	case ACPI_FDC_DISKETTE_720K:
		return &fdc_acpi_fdtypes[4];
	default:
		aprint_normal("%s: drive %d: unknown device type 0x%x\n",
		    fdc, drive, type);
		return NULL;
	}
}