fdc_pcmcia.c revision 1.19 1 1.19 cube /* $NetBSD: fdc_pcmcia.c,v 1.19 2008/03/16 00:58:56 cube Exp $ */
2 1.1 christos
3 1.3 christos /*-
4 1.12 mycroft * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
5 1.3 christos * All rights reserved.
6 1.3 christos *
7 1.3 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.3 christos * by Christos Zoulas.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos * 3. All advertising materials mentioning features or use of this software
19 1.1 christos * must display the following acknowledgement:
20 1.3 christos * This product includes software developed by the NetBSD
21 1.3 christos * Foundation, Inc. and its contributors.
22 1.3 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.3 christos * contributors may be used to endorse or promote products derived
24 1.3 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.3 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.3 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.3 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.3 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.3 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.3 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.3 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.3 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.3 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.3 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.3 christos * POSSIBILITY OF SUCH DAMAGE.
37 1.1 christos */
38 1.5 lukem
39 1.5 lukem #include <sys/cdefs.h>
40 1.19 cube __KERNEL_RCSID(0, "$NetBSD: fdc_pcmcia.c,v 1.19 2008/03/16 00:58:56 cube Exp $");
41 1.1 christos
42 1.1 christos #include <sys/param.h>
43 1.1 christos #include <sys/systm.h>
44 1.1 christos #include <sys/conf.h>
45 1.1 christos #include <sys/device.h>
46 1.2 christos #include <sys/disk.h>
47 1.2 christos #include <sys/buf.h>
48 1.1 christos
49 1.18 ad #include <sys/bus.h>
50 1.18 ad #include <sys/intr.h>
51 1.1 christos
52 1.1 christos #include <dev/pcmcia/pcmciareg.h>
53 1.1 christos #include <dev/pcmcia/pcmciavar.h>
54 1.2 christos #include <dev/pcmcia/pcmciadevs.h>
55 1.1 christos
56 1.14 mycroft #include <dev/isa/isavar.h>
57 1.14 mycroft
58 1.14 mycroft #include <dev/isa/fdreg.h>
59 1.14 mycroft #include <dev/isa/fdcvar.h>
60 1.1 christos
61 1.1 christos struct fdc_pcmcia_softc {
62 1.1 christos struct fdc_softc sc_fdc; /* real "fdc" softc */
63 1.1 christos
64 1.1 christos struct pcmcia_function *sc_pf; /* our PCMCIA function */
65 1.1 christos };
66 1.1 christos
67 1.19 cube int fdc_pcmcia_match(device_t, cfdata_t, void *);
68 1.15 perry int fdc_pcmcia_validate_config(struct pcmcia_config_entry *);
69 1.19 cube void fdc_pcmcia_attach(device_t, device_t, void *);
70 1.15 perry static void fdc_conf(struct fdc_softc *);
71 1.1 christos
72 1.19 cube CFATTACH_DECL_NEW(fdc_pcmcia, sizeof(struct fdc_pcmcia_softc),
73 1.14 mycroft fdc_pcmcia_match, fdc_pcmcia_attach, NULL, NULL);
74 1.14 mycroft
75 1.14 mycroft const struct pcmcia_product fdc_pcmcia_products[] = {
76 1.14 mycroft { PCMCIA_VENDOR_YEDATA, PCMCIA_PRODUCT_YEDATA_EXTERNAL_FDD,
77 1.14 mycroft PCMCIA_CIS_YEDATA_EXTERNAL_FDD },
78 1.14 mycroft };
79 1.14 mycroft const size_t fdc_pcmcia_nproducts =
80 1.14 mycroft sizeof(fdc_pcmcia_products) / sizeof(fdc_pcmcia_products[0]);
81 1.1 christos
82 1.1 christos static void
83 1.19 cube fdc_conf(struct fdc_softc *fdc)
84 1.1 christos {
85 1.1 christos bus_space_tag_t iot = fdc->sc_iot;
86 1.1 christos bus_space_handle_t ioh = fdc->sc_ioh;
87 1.1 christos int n;
88 1.1 christos
89 1.1 christos /* Figure out what we have */
90 1.16 perry if (out_fdc_cmd(iot, ioh, FDC_CMD_VERSION) == -1 ||
91 1.1 christos (n = fdcresult(fdc, 1)) != 1)
92 1.1 christos return;
93 1.1 christos
94 1.1 christos /* Nec765 or equivalent */
95 1.1 christos if (FDC_ST0(fdc->sc_status[0]) == FDC_ST0_INVL)
96 1.1 christos return;
97 1.1 christos
98 1.1 christos #if 0
99 1.1 christos /* ns8477 check */
100 1.16 perry if (out_fdc_cmd(iot, ioh, FDC_CMD_NSC) == -1 ||
101 1.1 christos (n = fdcresult(fdc, 1)) != 1) {
102 1.1 christos printf("NSC command failed\n");
103 1.1 christos return;
104 1.1 christos }
105 1.1 christos else
106 1.1 christos printf("Version %x\n", fdc->sc_status[0]);
107 1.1 christos #endif
108 1.1 christos
109 1.16 perry if (out_fdc_cmd(iot, ioh, FDC_CMD_DUMPREG) == -1 ||
110 1.1 christos (n = fdcresult(fdc, -1)) == -1)
111 1.1 christos return;
112 1.1 christos
113 1.1 christos /*
114 1.1 christos * Expect 10 bytes of status; one means that it did not
115 1.1 christos * understand the command
116 1.1 christos */
117 1.1 christos if (n == 1)
118 1.1 christos return;
119 1.1 christos
120 1.1 christos /*
121 1.1 christos * Configure controller to use FIFO and 8 bytes of FIFO threshold
122 1.1 christos */
123 1.1 christos (void)out_fdc_cmd(iot, ioh, FDC_CMD_CONFIGURE);
124 1.1 christos (void)out_fdc(iot, ioh, 0x00); /* doc says 0 */
125 1.1 christos (void)out_fdc(iot, ioh, 8); /* FIFO is active low. */
126 1.1 christos (void)out_fdc(iot, ioh, fdc->sc_status[9]); /* same comp */
127 1.1 christos /* No result phase */
128 1.1 christos
129 1.1 christos /* Lock this configuration */
130 1.1 christos if (out_fdc_cmd(iot, ioh, FDC_CMD_LOCK(FDC_CMD_FLAGS_LOCK)) == -1 ||
131 1.1 christos fdcresult(fdc, 1) != 1)
132 1.1 christos return;
133 1.1 christos }
134 1.1 christos
135 1.1 christos int
136 1.19 cube fdc_pcmcia_match(device_t parent, cfdata_t match, void *aux)
137 1.1 christos {
138 1.1 christos struct pcmcia_attach_args *pa = aux;
139 1.1 christos
140 1.14 mycroft if (pcmcia_product_lookup(pa, fdc_pcmcia_products, fdc_pcmcia_nproducts,
141 1.14 mycroft sizeof(fdc_pcmcia_products[0]), NULL))
142 1.14 mycroft return (1);
143 1.14 mycroft return (0);
144 1.1 christos }
145 1.1 christos
146 1.1 christos void
147 1.19 cube fdc_pcmcia_attach(device_t parent, device_t self, void *aux)
148 1.1 christos {
149 1.19 cube struct fdc_pcmcia_softc *psc = device_private(self);
150 1.1 christos struct fdc_softc *fdc = &psc->sc_fdc;
151 1.1 christos struct pcmcia_attach_args *pa = aux;
152 1.1 christos struct pcmcia_config_entry *cfe;
153 1.1 christos struct pcmcia_function *pf = pa->pf;
154 1.1 christos struct fdc_attach_args fa;
155 1.14 mycroft int error;
156 1.1 christos
157 1.19 cube fdc->sc_dev = self;
158 1.1 christos psc->sc_pf = pf;
159 1.1 christos
160 1.14 mycroft error = pcmcia_function_configure(pf, fdc_pcmcia_validate_config);
161 1.14 mycroft if (error) {
162 1.19 cube aprint_error_dev(self, "configure failed, error=%d\n", error);
163 1.14 mycroft return;
164 1.14 mycroft }
165 1.14 mycroft
166 1.14 mycroft cfe = pf->cfe;
167 1.14 mycroft fdc->sc_iot = cfe->iospace[0].handle.iot;
168 1.14 mycroft fdc->sc_iot = cfe->iospace[0].handle.ioh;
169 1.1 christos
170 1.14 mycroft if (pcmcia_function_enable(pf))
171 1.14 mycroft goto fail;
172 1.2 christos
173 1.4 christos fdc->sc_flags = FDC_HEADSETTLE;
174 1.1 christos fdc->sc_state = DEVIDLE;
175 1.1 christos TAILQ_INIT(&fdc->sc_drives);
176 1.1 christos
177 1.1 christos if (!fdcfind(fdc->sc_iot, fdc->sc_ioh, 1))
178 1.19 cube aprint_error_dev(self, "coundn't find fdc\n");
179 1.1 christos
180 1.1 christos fdc_conf(fdc);
181 1.1 christos
182 1.1 christos /* Establish the interrupt handler. */
183 1.1 christos fdc->sc_ih = pcmcia_intr_establish(pa->pf, IPL_BIO, fdchwintr, fdc);
184 1.14 mycroft if (!fdc->sc_ih)
185 1.14 mycroft goto fail;
186 1.1 christos
187 1.1 christos /* physical limit: four drives per controller. */
188 1.1 christos for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
189 1.1 christos if (fa.fa_drive < 2)
190 1.1 christos fa.fa_deftype = &fd_types[0];
191 1.1 christos else
192 1.1 christos fa.fa_deftype = NULL; /* unknown */
193 1.1 christos (void)config_found(self, (void *)&fa, fdprint);
194 1.1 christos }
195 1.14 mycroft
196 1.14 mycroft return;
197 1.14 mycroft
198 1.14 mycroft fail:
199 1.14 mycroft pcmcia_function_unconfigure(pf);
200 1.1 christos }
201