xref: /freebsd/sys/dev/fdc/fdc_isa.c (revision 780fb4a2)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2004-2005 M. Warner Losh.
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. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/bio.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/rman.h>
40 #include <sys/systm.h>
41 
42 #include <machine/bus.h>
43 
44 #include <dev/fdc/fdcvar.h>
45 
46 #include <isa/isavar.h>
47 #include <isa/isareg.h>
48 
49 static int fdc_isa_probe(device_t);
50 
51 static struct isa_pnp_id fdc_ids[] = {
52 	{0x0007d041, "PC standard floppy controller"}, /* PNP0700 */
53 	{0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
54 	{0}
55 };
56 
57 /*
58  * On standard ISA, we don't just use an 8 port range
59  * (e.g. 0x3f0-0x3f7) since that covers an IDE control register at
60  * 0x3f6.  So, on older hardware, we use 0x3f0-0x3f5 and 0x3f7.
61  * However, some BIOSs omit the control port, while others start at
62  * 0x3f2.  Of the latter, sometimes we have two resources, other times
63  * we have one.  We have to deal with the following cases:
64  *
65  * 1:	0x3f0-0x3f5			# very rare
66  * 2:	0x3f0				# hints -> 0x3f0-0x3f5,0x3f7
67  * 3:	0x3f0-0x3f5,0x3f7		# Most common
68  * 4:	0x3f2-0x3f5,0x3f7		# Second most common
69  * 5:	0x3f2-0x3f5			# implies 0x3f7 too.
70  * 6:	0x3f2-0x3f3,0x3f4-0x3f5,0x3f7	# becoming common
71  * 7:	0x3f2-0x3f3,0x3f4-0x3f5		# rare
72  * 8:	0x3f0-0x3f1,0x3f2-0x3f3,0x3f4-0x3f5,0x3f7
73  * 9:	0x3f0-0x3f3,0x3f4-0x3f5,0x3f7
74  *
75  * The following code is generic for any value of 0x3fx.  It is also
76  * generic for all the above cases, as well as cases where things are
77  * even weirder.
78  */
79 int
80 fdc_isa_alloc_resources(device_t dev, struct fdc_data *fdc)
81 {
82 	struct resource *res;
83 	int i, j, rid, newrid, nport;
84 	u_long port;
85 
86 	fdc->fdc_dev = dev;
87 	rid = 0;
88 	for (i = 0; i < FDC_MAXREG; i++)
89 		fdc->resio[i] = NULL;
90 
91 	nport = isa_get_logicalid(dev) ? 1 : 6;
92 	for (rid = 0; ; rid++) {
93 		newrid = rid;
94 		res = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &newrid,
95 		    rid == 0 ? nport : 1, RF_ACTIVE);
96 		if (res == NULL)
97 			break;
98 		/*
99 		 * Mask off the upper bits of the register, and sanity
100 		 * check resource ranges.
101 		 */
102 		i = rman_get_start(res) & 0x7;
103 		if (i + rman_get_size(res) - 1 > FDC_MAXREG) {
104 			bus_release_resource(dev, SYS_RES_IOPORT, newrid, res);
105 			return (ENXIO);
106 		}
107 		for (j = 0; j < rman_get_size(res); j++) {
108 			fdc->resio[i + j] = res;
109 			fdc->ridio[i + j] = newrid;
110 			fdc->ioff[i + j] = j;
111 			fdc->ioh[i + j] = rman_get_bushandle(res);
112 		}
113 	}
114 	if (fdc->resio[2] == NULL) {
115 		device_printf(dev, "No FDOUT register!\n");
116 		return (ENXIO);
117 	}
118 	fdc->iot = rman_get_bustag(fdc->resio[2]);
119 	if (fdc->resio[7] == NULL) {
120 		port = (rman_get_start(fdc->resio[2]) & ~0x7) + 7;
121 		newrid = rid;
122 		res = bus_alloc_resource(dev, SYS_RES_IOPORT, &newrid, port,
123 		    port, 1, RF_ACTIVE);
124 		if (res == NULL) {
125 			device_printf(dev, "Faking up FDCTL\n");
126 			fdc->resio[7] = fdc->resio[2];
127 			fdc->ridio[7] = fdc->ridio[2];
128 			fdc->ioff[7] = fdc->ioff[2] + 5;
129 			fdc->ioh[7] = fdc->ioh[2];
130 		} else {
131 			fdc->resio[7] = res;
132 			fdc->ridio[7] = newrid;
133 			fdc->ioff[7] = rman_get_start(res) & 7;
134 			fdc->ioh[7] = rman_get_bushandle(res);
135 		}
136 	}
137 
138 	fdc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &fdc->rid_irq,
139 	    RF_ACTIVE | RF_SHAREABLE);
140 	if (fdc->res_irq == NULL) {
141 		device_printf(dev, "cannot reserve interrupt line\n");
142 		return (ENXIO);
143 	}
144 
145 	if ((fdc->flags & FDC_NODMA) == 0) {
146 		fdc->res_drq = bus_alloc_resource_any(dev, SYS_RES_DRQ,
147 		    &fdc->rid_drq, RF_ACTIVE | RF_SHAREABLE);
148 		if (fdc->res_drq == NULL) {
149 			device_printf(dev, "cannot reserve DMA request line\n");
150 			/* This is broken and doesn't work for ISA case */
151 			fdc->flags |= FDC_NODMA;
152 		} else
153 			fdc->dmachan = rman_get_start(fdc->res_drq);
154 	}
155 
156 	return (0);
157 }
158 
159 static int
160 fdc_isa_probe(device_t dev)
161 {
162 	int	error;
163 	struct	fdc_data *fdc;
164 
165 	fdc = device_get_softc(dev);
166 	fdc->fdc_dev = dev;
167 
168 	/* Check pnp ids */
169 	error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
170 	if (error == ENXIO)
171 		return (ENXIO);
172 
173 	/* Attempt to allocate our resources for the duration of the probe */
174 	error = fdc_isa_alloc_resources(dev, fdc);
175 	if (error == 0)
176 		error = fdc_initial_reset(dev, fdc);
177 
178 	fdc_release_resources(fdc);
179 	return (error);
180 }
181 
182 static int
183 fdc_isa_attach(device_t dev)
184 {
185 	struct	fdc_data *fdc;
186 	int error;
187 
188 	fdc = device_get_softc(dev);
189 	fdc->fdc_dev = dev;
190 	error = fdc_isa_alloc_resources(dev, fdc);
191 	if (error == 0)
192 		error = fdc_attach(dev);
193 	if (error == 0)
194 		error = fdc_hints_probe(dev);
195 	if (error == 0)
196 		fdc_start_worker(dev);
197 	else
198 		fdc_release_resources(fdc);
199 	return (error);
200 }
201 
202 static device_method_t fdc_methods[] = {
203 	/* Device interface */
204 	DEVMETHOD(device_probe,		fdc_isa_probe),
205 	DEVMETHOD(device_attach,	fdc_isa_attach),
206 	DEVMETHOD(device_detach,	fdc_detach),
207 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
208 	DEVMETHOD(device_suspend,	bus_generic_suspend),
209 	DEVMETHOD(device_resume,	bus_generic_resume),
210 
211 	/* Bus interface */
212 	DEVMETHOD(bus_print_child,	fdc_print_child),
213 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
214 	DEVMETHOD(bus_write_ivar,       fdc_write_ivar),
215 	/* Our children never use any other bus interface methods. */
216 
217 	{ 0, 0 }
218 };
219 
220 static driver_t fdc_driver = {
221 	"fdc",
222 	fdc_methods,
223 	sizeof(struct fdc_data)
224 };
225 
226 DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
227 ISA_PNP_INFO(fdc_ids);
228