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