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