xref: /netbsd-src/sys/arch/prep/pnpbus/fdc_pnpbus.c (revision 7991f5a7b8fc83a3d55dc2a1767cca3b84103969)
1 /*	$NetBSD: fdc_pnpbus.c,v 1.6 2021/07/24 21:31:34 andvar Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Tim Rightnour
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * PNPBIOS attachment for the PC Floppy Controller driver.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: fdc_pnpbus.c,v 1.6 2021/07/24 21:31:34 andvar Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/callout.h>
42 #include <sys/device.h>
43 #include <sys/buf.h>
44 #include <sys/queue.h>
45 
46 #include <sys/bus.h>
47 #include <machine/intr.h>
48 #include <machine/isa_machdep.h>
49 
50 #include <dev/isa/isavar.h>
51 #include <dev/isa/isadmavar.h>
52 
53 #include <dev/isa/fdcvar.h>
54 
55 #include <prep/pnpbus/pnpbusvar.h>
56 
57 static int	fdc_pnpbus_match(device_t, cfdata_t, void *);
58 static void	fdc_pnpbus_attach(device_t, device_t, void *);
59 
60 struct fdc_pnpbus_softc {
61         struct fdc_softc sc_fdc;        /* base fdc device */
62 
63         bus_space_handle_t sc_baseioh;  /* base I/O handle */
64 };
65 
66 CFATTACH_DECL2_NEW(fdc_pnpbus, sizeof(struct fdc_pnpbus_softc),
67     fdc_pnpbus_match, fdc_pnpbus_attach, NULL, NULL, NULL, NULL);
68 
69 static int
fdc_pnpbus_match(device_t parent,cfdata_t match,void * aux)70 fdc_pnpbus_match(device_t parent, cfdata_t match, void *aux)
71 {
72 	struct pnpbus_dev_attach_args *pna = aux;
73 	int ret = 0;
74 
75 	if (strcmp(pna->pna_devid, "PNP0700") == 0)
76 		ret = 1;
77 
78 	if (ret)
79 		pnpbus_scan(pna, pna->pna_ppc_dev);
80 
81 	return ret;
82 }
83 
84 static void
fdc_pnpbus_attach(device_t parent,device_t self,void * aux)85 fdc_pnpbus_attach(device_t parent, device_t self, void *aux)
86 {
87         struct fdc_pnpbus_softc *pdc = device_private(self);
88 	struct fdc_softc *fdc = &pdc->sc_fdc;
89 	struct pnpbus_dev_attach_args *pna = aux;
90         int size, base;
91 
92 	fdc->sc_dev = self;
93 	fdc->sc_ic = pna->pna_ic;
94 
95 	if (pnpbus_io_map(&pna->pna_res, 0, &fdc->sc_iot, &pdc->sc_baseioh)) {
96 		aprint_error_dev(self, "unable to map I/O space\n");
97 		return;
98 	}
99 
100 	/*
101          * Start accounting for "odd" pnpbus's. Some probe as 4 ports,
102          * some as 6 and some don't give the ctl port back.
103          */
104 
105         if (pnpbus_getioport(&pna->pna_res, 0, &base, &size)) {
106                 aprint_error_dev(self, "can't get iobase size\n");
107                 return;
108         }
109 
110         switch (size) {
111 
112         /* Easy case. This matches right up with what the fdc code expects. */
113         case 4:
114                 fdc->sc_ioh = pdc->sc_baseioh;
115                 break;
116 
117         /* Map a subregion so this all lines up with the fdc code. */
118         case 6:
119                 if (bus_space_subregion(fdc->sc_iot, pdc->sc_baseioh, 2, 4,
120                     &fdc->sc_ioh)) {
121                         aprint_error_dev(self,
122 			    "unable to subregion I/O space\n");
123                         return;
124                 }
125                 break;
126         default:
127                 aprint_error_dev(self, "unknown size: %d of io mapping\n", size);
128                 return;
129         }
130 
131         /*
132          * XXX: This is bad. If the pnpbus claims only 1 I/O range then it's
133          * omitting the controller I/O port. (One has to exist for there to
134          * be a working fdc). Just try and force the mapping in.
135          */
136 
137         if (pna->pna_res.numio == 1) {
138                 if (bus_space_map(fdc->sc_iot, base + size + 1, 1, 0,
139                     &fdc->sc_fdctlioh)) {
140                         aprint_error_dev(self,
141 			    "unable to force map CTL I/O space\n");
142                         return;
143                 }
144         } else if (pnpbus_io_map(&pna->pna_res, 1, &fdc->sc_iot,
145 		   &fdc->sc_fdctlioh)) {
146                 aprint_error_dev(self, "unable to map CTL I/O space\n");
147 		return;
148 	}
149 
150 	if (pnpbus_getdmachan(&pna->pna_res, 0, &fdc->sc_drq)) {
151 		aprint_error_dev(self, "unable to get DMA channel\n");
152 		return;
153 	}
154 
155         if (pna->pna_res.numio == 1)
156                 aprint_normal_dev(self,
157 		    "ctl io %x didn't probe. Forced attach\n", base + size + 1);
158 
159 	aprint_normal("\n");
160 
161 	/* The 7043-140 gets the type wrong, so override to edge always */
162 	fdc->sc_ih = pnpbus_intr_establish(0, IPL_BIO, IST_EDGE, fdcintr, fdc,
163 	    &pna->pna_res);
164 
165 	fdcattach(fdc);
166 }
167