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