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