xref: /netbsd/sys/dev/pcmcia/fdc_pcmcia.c (revision c8d61b77)
1 /*	$NetBSD: fdc_pcmcia.c,v 1.23 2021/12/08 20:50:02 andvar Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
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 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: fdc_pcmcia.c,v 1.23 2021/12/08 20:50:02 andvar Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/conf.h>
38 #include <sys/device.h>
39 #include <sys/disk.h>
40 #include <sys/buf.h>
41 
42 #include <sys/bus.h>
43 #include <sys/intr.h>
44 
45 #include <dev/pcmcia/pcmciareg.h>
46 #include <dev/pcmcia/pcmciavar.h>
47 #include <dev/pcmcia/pcmciadevs.h>
48 
49 #include <dev/isa/isavar.h>
50 
51 #include <dev/isa/fdreg.h>
52 #include <dev/isa/fdcvar.h>
53 
54 struct fdc_pcmcia_softc {
55 	struct fdc_softc sc_fdc;		/* real "fdc" softc */
56 
57 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
58 };
59 
60 int fdc_pcmcia_match(device_t, cfdata_t, void *);
61 int fdc_pcmcia_validate_config(struct pcmcia_config_entry *);
62 void fdc_pcmcia_attach(device_t, device_t, void *);
63 static void fdc_conf(struct fdc_softc *);
64 
65 CFATTACH_DECL_NEW(fdc_pcmcia, sizeof(struct fdc_pcmcia_softc),
66     fdc_pcmcia_match, fdc_pcmcia_attach, NULL, NULL);
67 
68 const struct pcmcia_product fdc_pcmcia_products[] = {
69 	{ PCMCIA_VENDOR_YEDATA, PCMCIA_PRODUCT_YEDATA_EXTERNAL_FDD,
70 	  PCMCIA_CIS_YEDATA_EXTERNAL_FDD },
71 };
72 const size_t fdc_pcmcia_nproducts =
73     sizeof(fdc_pcmcia_products) / sizeof(fdc_pcmcia_products[0]);
74 
75 static void
fdc_conf(struct fdc_softc * fdc)76 fdc_conf(struct fdc_softc *fdc)
77 {
78 	bus_space_tag_t iot = fdc->sc_iot;
79 	bus_space_handle_t ioh = fdc->sc_ioh;
80 	int n;
81 
82 	/* Figure out what we have */
83 	if (out_fdc_cmd(iot, ioh, FDC_CMD_VERSION) == -1 ||
84 	    (n = fdcresult(fdc, 1)) != 1)
85 		return;
86 
87 	/* Nec765 or equivalent */
88 	if (FDC_ST0(fdc->sc_status[0]) == FDC_ST0_INVL)
89 		return;
90 
91 #if 0
92 	/* ns8477 check */
93 	if (out_fdc_cmd(iot, ioh, FDC_CMD_NSC) == -1 ||
94 	    (n = fdcresult(fdc, 1)) != 1) {
95 		printf("NSC command failed\n");
96 		return;
97 	}
98 	else
99 		printf("Version %x\n", fdc->sc_status[0]);
100 #endif
101 
102 	if (out_fdc_cmd(iot, ioh, FDC_CMD_DUMPREG) == -1 ||
103 	    (n = fdcresult(fdc, -1)) == -1)
104 		return;
105 
106 	/*
107          * Expect 10 bytes of status; one means that it did not
108 	 * understand the command
109 	 */
110 	if (n == 1)
111 		return;
112 
113 	/*
114 	 * Configure controller to use FIFO and 8 bytes of FIFO threshold
115 	 */
116 	(void)out_fdc_cmd(iot, ioh, FDC_CMD_CONFIGURE);
117 	(void)out_fdc(iot, ioh, 0x00);	/* doc says 0 */
118 	(void)out_fdc(iot, ioh, 8);	/* FIFO is active low. */
119 	(void)out_fdc(iot, ioh, fdc->sc_status[9]); /* same comp */
120 	/* No result phase */
121 
122 	/* Lock this configuration */
123 	if (out_fdc_cmd(iot, ioh, FDC_CMD_LOCK(FDC_CMD_FLAGS_LOCK)) == -1 ||
124 	    fdcresult(fdc, 1) != 1)
125 		return;
126 }
127 
128 int
fdc_pcmcia_match(device_t parent,cfdata_t match,void * aux)129 fdc_pcmcia_match(device_t parent, cfdata_t match, void *aux)
130 {
131 	struct pcmcia_attach_args *pa = aux;
132 
133 	if (pcmcia_product_lookup(pa, fdc_pcmcia_products, fdc_pcmcia_nproducts,
134 	    sizeof(fdc_pcmcia_products[0]), NULL))
135 		return (1);
136 	return (0);
137 }
138 
139 void
fdc_pcmcia_attach(device_t parent,device_t self,void * aux)140 fdc_pcmcia_attach(device_t parent, device_t self, void *aux)
141 {
142 	struct fdc_pcmcia_softc *psc = device_private(self);
143 	struct fdc_softc *fdc = &psc->sc_fdc;
144 	struct pcmcia_attach_args *pa = aux;
145 	struct pcmcia_config_entry *cfe;
146 	struct pcmcia_function *pf = pa->pf;
147 	struct fdc_attach_args fa;
148 	int error;
149 
150 	fdc->sc_dev = self;
151 	psc->sc_pf = pf;
152 
153 	error = pcmcia_function_configure(pf, fdc_pcmcia_validate_config);
154         if (error) {
155                 aprint_error_dev(self, "configure failed, error=%d\n", error);
156                 return;
157         }
158 
159 	cfe = pf->cfe;
160 	fdc->sc_iot = cfe->iospace[0].handle.iot;
161 	fdc->sc_iot = cfe->iospace[0].handle.ioh;
162 
163 	if (pcmcia_function_enable(pf))
164 		goto fail;
165 
166 	fdc->sc_flags = FDC_HEADSETTLE;
167 	fdc->sc_state = DEVIDLE;
168 	TAILQ_INIT(&fdc->sc_drives);
169 
170 	if (!fdcfind(fdc->sc_iot, fdc->sc_ioh, 1))
171 		aprint_error_dev(self, "couldn't find fdc\n");
172 
173 	fdc_conf(fdc);
174 
175 	/* Establish the interrupt handler. */
176 	fdc->sc_ih = pcmcia_intr_establish(pa->pf, IPL_BIO, fdchwintr, fdc);
177 	if (!fdc->sc_ih)
178 		goto fail;
179 
180 	/* physical limit: four drives per controller. */
181 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
182 		if (fa.fa_drive < 2)
183 			fa.fa_deftype = &fd_types[0];
184 		else
185 			fa.fa_deftype = NULL;		/* unknown */
186 		(void)config_found(self, (void *)&fa, fdprint, CFARGS_NONE);
187 	}
188 
189 	return;
190 
191 fail:
192 	pcmcia_function_unconfigure(pf);
193 }
194