xref: /freebsd/sys/dev/fdc/fdc_isa.c (revision 27df52f99937ece59b5eabd8d26997b7b1633970)
1 /*-
2  * Copyright (c) 2004 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  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    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 FOR
20  * 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 disk controller"}, /* PNP0700 */
53 	{0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
54 	{0}
55 };
56 
57 int
58 fdc_isa_alloc_resources(device_t dev, struct fdc_data *fdc)
59 {
60 	int nports = 6;
61 
62 	fdc->fdc_dev = dev;
63 	fdc->rid_ioport = 0;
64 	fdc->rid_irq = 0;
65 	fdc->rid_drq = 0;
66 	fdc->rid_ctl = 1;
67 
68 	/*
69 	 * On standard ISA, we don't just use an 8 port range
70 	 * (e.g. 0x3f0-0x3f7) since that covers an IDE control
71 	 * register at 0x3f6.  So, on older hardware, we use
72 	 * 0x3f0-0x3f5 and 0x3f7.  However, some BIOSs omit the
73 	 * control port, while others start at 0x3f2.  Of the latter,
74 	 * sometimes we have two resources, other times we have one.
75 	 * We have to deal with the following cases:
76 	 *
77 	 * 1:	0x3f0-0x3f5			# very rare
78 	 * 2:	0x3f0				# hints -> 0x3f0-0x3f5,0x3f7
79 	 * 3:	0x3f0-0x3f5,0x3f7		# Most common
80 	 * 4:	0x3f2-0x3f5,0x3f7		# Second most common
81 	 * 5:	0x3f2-0x3f5			# implies 0x3f7 too.
82 	 * 6:	0x3f2-0x3f3,0x3f4-0x3f5,0x3f7	# becoming common
83 	 * 7:	0x3f2-0x3f3,0x3f4-0x3f5		# rare
84 	 *
85 	 * The following code is generic for any value of 0x3fx :-)
86 	 */
87 
88 	/*
89 	 * First, allocated the main range of ports.  In the best of
90 	 * worlds, this is 4 or 6 ports.  In others, well, that's
91 	 * why this function is so complicated.
92 	 */
93 	fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
94 	    &fdc->rid_ioport, 0ul, ~0ul, nports, RF_ACTIVE);
95 	if (fdc->res_ioport == 0) {
96 		device_printf(dev, "cannot allocate I/O port (%d ports)\n",
97 		    nports);
98 		return (ENXIO);
99 	}
100 	fdc->portt = rman_get_bustag(fdc->res_ioport);
101 	fdc->porth = rman_get_bushandle(fdc->res_ioport);
102 
103 	/*
104 	 * Handle cases 4-7 above
105 	 */
106 	fdc->port_off = -(fdc->porth & 0x7);
107 
108 	/*
109 	 * Deal with case 6 and 7: FDSTS and FDSATA are in rid 1.
110 	 */
111 	if (rman_get_size(fdc->res_ioport) == 2) {
112 		fdc->rid_sts = 1;
113 		fdc->res_sts = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
114 		    &fdc->rid_sts, RF_ACTIVE);
115 		if (fdc->res_sts == NULL) {
116 			device_printf(dev, "Can't alloc rid 1");
117 			fdc_release_resources(fdc);
118 			return (ENXIO);
119 		}
120 		fdc->rid_ctl++;
121 		fdc->sts_off = -4;
122 		fdc->stst = rman_get_bustag(fdc->res_sts);
123 		fdc->stsh = rman_get_bushandle(fdc->res_sts);
124 	} else {
125 		fdc->res_sts = NULL;
126 		fdc->sts_off = fdc->port_off;
127 		fdc->stst = fdc->portt;
128 		fdc->stsh = fdc->porth;
129 	}
130 
131 	/*
132 	 * allocate the control port.  For cases 1, 2, 5 and 7, we
133 	 * fake it from the ioports resource.  XXX IS THIS THE RIGHT THING
134 	 * TO DO, OR SHOULD WE CREATE A NEW RID? (I think we need a new rid)
135 	 */
136 	fdc->res_ctl = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
137 	    &fdc->rid_ctl, RF_ACTIVE);
138 	if (fdc->res_ctl == NULL) {
139 		fdc->ctl_off = 7 + fdc->port_off;
140 		fdc->res_ctl = NULL;
141 		fdc->ctlt = fdc->portt;
142 		fdc->ctlh = fdc->porth;
143 	} else {
144 		fdc->ctl_off = 0;
145 		fdc->ctlt = rman_get_bustag(fdc->res_ctl);
146 		fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
147 	}
148 
149 	fdc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &fdc->rid_irq,
150 	    RF_ACTIVE | RF_SHAREABLE);
151 	if (fdc->res_irq == 0) {
152 		device_printf(dev, "cannot reserve interrupt line\n");
153 		return (ENXIO);
154 	}
155 
156 	if ((fdc->flags & FDC_NODMA) == 0) {
157 		fdc->res_drq = bus_alloc_resource_any(dev, SYS_RES_DRQ,
158 		    &fdc->rid_drq, RF_ACTIVE | RF_SHAREABLE);
159 		if (fdc->res_drq == 0) {
160 			device_printf(dev, "cannot reserve DMA request line\n");
161 			/* This is broken and doesn't work for ISA case */
162 			fdc->flags |= FDC_NODMA;
163 		} else
164 			fdc->dmachan = rman_get_start(fdc->res_drq);
165 	}
166 
167 	return (0);
168 }
169 
170 static int
171 fdc_isa_probe(device_t dev)
172 {
173 	int	error;
174 	struct	fdc_data *fdc;
175 
176 	fdc = device_get_softc(dev);
177 	fdc->fdc_dev = dev;
178 
179 	/* Check pnp ids */
180 	error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
181 	if (error == ENXIO)
182 		return (ENXIO);
183 
184 	/* Attempt to allocate our resources for the duration of the probe */
185 	error = fdc_isa_alloc_resources(dev, fdc);
186 	if (error == 0)
187 		error = fdc_initial_reset(dev, fdc);
188 
189 	fdc_release_resources(fdc);
190 	return (error);
191 }
192 
193 static int
194 fdc_isa_attach(device_t dev)
195 {
196 	struct	fdc_data *fdc;
197 	int error;
198 
199 	fdc = device_get_softc(dev);
200 	error = fdc_isa_alloc_resources(dev, fdc);
201 	if (error == 0)
202 		error = fdc_attach(dev);
203 	if (error == 0)
204 		error = fdc_hints_probe(dev);
205 	if (error)
206 		fdc_release_resources(fdc);
207 	return (error);
208 }
209 
210 static device_method_t fdc_methods[] = {
211 	/* Device interface */
212 	DEVMETHOD(device_probe,		fdc_isa_probe),
213 	DEVMETHOD(device_attach,	fdc_isa_attach),
214 	DEVMETHOD(device_detach,	fdc_detach),
215 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
216 	DEVMETHOD(device_suspend,	bus_generic_suspend),
217 	DEVMETHOD(device_resume,	bus_generic_resume),
218 
219 	/* Bus interface */
220 	DEVMETHOD(bus_print_child,	fdc_print_child),
221 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
222 	DEVMETHOD(bus_write_ivar,       fdc_write_ivar),
223 	/* Our children never use any other bus interface methods. */
224 
225 	{ 0, 0 }
226 };
227 
228 static driver_t fdc_driver = {
229 	"fdc",
230 	fdc_methods,
231 	sizeof(struct fdc_data)
232 };
233 
234 DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
235