wdc_pcmcia.c revision 1.12 1 1.12 bouyer /* $NetBSD: wdc_pcmcia.c,v 1.12 1998/10/12 16:09:22 bouyer Exp $ */
2 1.1 matt
3 1.8 mycroft /*-
4 1.8 mycroft * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.8 mycroft * All rights reserved.
6 1.1 matt *
7 1.8 mycroft * This code is derived from software contributed to The NetBSD Foundation
8 1.8 mycroft * by Charles M. Hannum, by Onno van der Linden and by Manuel Bouyer.
9 1.1 matt *
10 1.1 matt * Redistribution and use in source and binary forms, with or without
11 1.1 matt * modification, are permitted provided that the following conditions
12 1.1 matt * are met:
13 1.1 matt * 1. Redistributions of source code must retain the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer.
15 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 matt * notice, this list of conditions and the following disclaimer in the
17 1.1 matt * documentation and/or other materials provided with the distribution.
18 1.1 matt * 3. All advertising materials mentioning features or use of this software
19 1.1 matt * must display the following acknowledgement:
20 1.8 mycroft * This product includes software developed by the NetBSD
21 1.8 mycroft * Foundation, Inc. and its contributors.
22 1.8 mycroft * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.8 mycroft * contributors may be used to endorse or promote products derived
24 1.8 mycroft * from this software without specific prior written permission.
25 1.1 matt *
26 1.8 mycroft * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.8 mycroft * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.8 mycroft * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.8 mycroft * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.8 mycroft * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.8 mycroft * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.8 mycroft * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.8 mycroft * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.8 mycroft * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.8 mycroft * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.8 mycroft * POSSIBILITY OF SUCH DAMAGE.
37 1.1 matt */
38 1.1 matt
39 1.1 matt #include <sys/param.h>
40 1.1 matt #include <sys/systm.h>
41 1.1 matt #include <sys/kernel.h>
42 1.1 matt #include <sys/conf.h>
43 1.1 matt #include <sys/file.h>
44 1.1 matt #include <sys/stat.h>
45 1.1 matt #include <sys/ioctl.h>
46 1.1 matt #include <sys/buf.h>
47 1.1 matt #include <sys/uio.h>
48 1.1 matt #include <sys/malloc.h>
49 1.1 matt #include <sys/device.h>
50 1.1 matt #include <sys/disklabel.h>
51 1.1 matt #include <sys/disk.h>
52 1.1 matt #include <sys/syslog.h>
53 1.1 matt #include <sys/proc.h>
54 1.1 matt
55 1.1 matt #include <vm/vm.h>
56 1.1 matt
57 1.1 matt #include <machine/cpu.h>
58 1.1 matt #include <machine/intr.h>
59 1.1 matt #include <machine/bus.h>
60 1.1 matt
61 1.1 matt #include <dev/pcmcia/pcmciavar.h>
62 1.9 kenh #include <dev/pcmcia/pcmciadevs.h>
63 1.9 kenh
64 1.1 matt #include <dev/isa/isavar.h>
65 1.12 bouyer #include <dev/ata/atavar.h>
66 1.1 matt #include <dev/ic/wdcvar.h>
67 1.1 matt
68 1.1 matt #define WDC_PCMCIA_REG_NPORTS 8
69 1.9 kenh #define WDC_PCMCIA_AUXREG_OFFSET (WDC_PCMCIA_REG_NPORTS + 6)
70 1.2 mycroft #define WDC_PCMCIA_AUXREG_NPORTS 2
71 1.1 matt
72 1.1 matt struct wdc_pcmcia_softc {
73 1.1 matt struct wdc_softc sc_wdcdev;
74 1.12 bouyer struct channel_softc wdc_channel;
75 1.1 matt struct pcmcia_io_handle sc_pioh;
76 1.1 matt struct pcmcia_io_handle sc_auxpioh;
77 1.1 matt int sc_iowindow;
78 1.1 matt int sc_auxiowindow;
79 1.1 matt void *sc_ih;
80 1.1 matt };
81 1.1 matt
82 1.1 matt static int wdc_pcmcia_match __P((struct device *, struct cfdata *, void *));
83 1.1 matt static void wdc_pcmcia_attach __P((struct device *, struct device *, void *));
84 1.1 matt
85 1.1 matt struct cfattach wdc_pcmcia_ca = {
86 1.1 matt sizeof(struct wdc_pcmcia_softc), wdc_pcmcia_match, wdc_pcmcia_attach
87 1.1 matt };
88 1.1 matt
89 1.9 kenh struct wdc_pcmcia_product {
90 1.9 kenh u_int32_t wpp_vendor; /* vendor ID */
91 1.9 kenh u_int32_t wpp_product; /* product ID */
92 1.9 kenh int wpp_quirk_flag; /* Quirk flags */
93 1.9 kenh #define WDC_PCMCIA_FORCE_16BIT_IO 0x01 /* Don't use PCMCIA_WIDTH_AUTO */
94 1.9 kenh #define WDC_PCMCIA_NO_EXTRA_RESETS 0x02 /* Only reset ctrl once */
95 1.9 kenh const char *wpp_cis_info[4]; /* XXX necessary? */
96 1.9 kenh const char *wpp_name; /* product name */
97 1.9 kenh } wdc_pcmcia_products[] = {
98 1.9 kenh
99 1.9 kenh { /* PCMCIA_VENDOR_DIGITAL XXX */ 0x0100,
100 1.9 kenh PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM,
101 1.9 kenh 0, { NULL, "Digital Mobile Media CD-ROM", NULL, NULL },
102 1.9 kenh PCMCIA_STR_DIGITAL_MOBILE_MEDIA_CDROM },
103 1.9 kenh
104 1.9 kenh { PCMCIA_VENDOR_HAGIWARASYSCOM,
105 1.9 kenh -1, /* XXX */
106 1.9 kenh WDC_PCMCIA_FORCE_16BIT_IO,
107 1.9 kenh { NULL, NULL, NULL, NULL },
108 1.9 kenh "Hagiwara SYS-COM ComactFlash Card" },
109 1.9 kenh
110 1.9 kenh /* CANON FC-8M is also matches. */
111 1.9 kenh { PCMCIA_VENDOR_SANDISK,
112 1.9 kenh PCMCIA_PRODUCT_SANDISK_SDCFB,
113 1.10 enami WDC_PCMCIA_FORCE_16BIT_IO /* _AUTO also works */,
114 1.10 enami { NULL, NULL, NULL, NULL },
115 1.9 kenh PCMCIA_STR_SANDISK_SDCFB },
116 1.9 kenh
117 1.9 kenh /* The TEAC IDE/Card II is used on the Sony Vaio */
118 1.9 kenh { PCMCIA_VENDOR_TEAC,
119 1.9 kenh PCMCIA_PRODUCT_TEAC_IDECARDII,
120 1.9 kenh WDC_PCMCIA_NO_EXTRA_RESETS,
121 1.9 kenh PCMCIA_CIS_TEAC_IDECARDII,
122 1.9 kenh PCMCIA_STR_TEAC_IDECARDII },
123 1.9 kenh
124 1.9 kenh { 0, 0, 0, { NULL, NULL, NULL, NULL}, NULL }
125 1.9 kenh };
126 1.9 kenh
127 1.9 kenh struct wdc_pcmcia_product *
128 1.9 kenh wdc_pcmcia_lookup __P((struct pcmcia_attach_args *));
129 1.9 kenh
130 1.9 kenh struct wdc_pcmcia_product *
131 1.9 kenh wdc_pcmcia_lookup(pa)
132 1.9 kenh struct pcmcia_attach_args *pa;
133 1.9 kenh {
134 1.9 kenh struct wdc_pcmcia_product *wpp;
135 1.9 kenh int i, cis_match;
136 1.9 kenh
137 1.9 kenh for (wpp = wdc_pcmcia_products; wpp->wpp_name != NULL; wpp++)
138 1.9 kenh if ((wpp->wpp_vendor == -1 ||
139 1.9 kenh pa->manufacturer == wpp->wpp_vendor) &&
140 1.9 kenh (wpp->wpp_product == -1 ||
141 1.9 kenh pa->product == wpp->wpp_product)) {
142 1.9 kenh cis_match = 1;
143 1.9 kenh for (i = 0; i < 4; i++) {
144 1.9 kenh if (!(wpp->wpp_cis_info[i] == NULL ||
145 1.9 kenh (pa->card->cis1_info[i] != NULL &&
146 1.9 kenh strcmp(pa->card->cis1_info[i],
147 1.9 kenh wpp->wpp_cis_info[i]) == 0)))
148 1.9 kenh cis_match = 0;
149 1.9 kenh }
150 1.9 kenh if (cis_match)
151 1.9 kenh return(wpp);
152 1.9 kenh }
153 1.9 kenh
154 1.9 kenh return (NULL);
155 1.9 kenh }
156 1.9 kenh
157 1.1 matt static int
158 1.2 mycroft wdc_pcmcia_match(parent, match, aux)
159 1.2 mycroft struct device *parent;
160 1.2 mycroft struct cfdata *match;
161 1.2 mycroft void *aux;
162 1.1 matt {
163 1.1 matt struct pcmcia_attach_args *pa = aux;
164 1.1 matt
165 1.9 kenh if (wdc_pcmcia_lookup(pa) != NULL)
166 1.9 kenh return (1);
167 1.3 matt
168 1.9 kenh return (0);
169 1.1 matt }
170 1.1 matt
171 1.1 matt static void
172 1.2 mycroft wdc_pcmcia_attach(parent, self, aux)
173 1.2 mycroft struct device *parent;
174 1.2 mycroft struct device *self;
175 1.2 mycroft void *aux;
176 1.1 matt {
177 1.1 matt struct wdc_pcmcia_softc *sc = (void *)self;
178 1.1 matt struct pcmcia_attach_args *pa = aux;
179 1.5 enami struct pcmcia_config_entry *cfe;
180 1.9 kenh struct wdc_pcmcia_product *wpp;
181 1.1 matt
182 1.5 enami for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
183 1.5 enami cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
184 1.9 kenh if (cfe->num_iospace != 1 && cfe->num_iospace != 2)
185 1.9 kenh continue;
186 1.9 kenh
187 1.1 matt if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
188 1.9 kenh cfe->iospace[0].length,
189 1.9 kenh cfe->iospace[0].start == 0 ? cfe->iospace[0].length : 0,
190 1.9 kenh &sc->sc_pioh))
191 1.1 matt continue;
192 1.9 kenh
193 1.9 kenh if (cfe->num_iospace == 2) {
194 1.1 matt if (!pcmcia_io_alloc(pa->pf, cfe->iospace[1].start,
195 1.9 kenh cfe->iospace[1].length, 0, &sc->sc_auxpioh))
196 1.1 matt break;
197 1.9 kenh } else /* num_iospace == 1 */ {
198 1.1 matt sc->sc_auxpioh.iot = sc->sc_pioh.iot;
199 1.9 kenh if (!bus_space_subregion(sc->sc_pioh.iot,
200 1.9 kenh sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET,
201 1.9 kenh WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh))
202 1.1 matt break;
203 1.1 matt }
204 1.11 thorpej pcmcia_io_free(pa->pf, &sc->sc_pioh);
205 1.1 matt }
206 1.1 matt
207 1.1 matt if (cfe == NULL) {
208 1.6 enami printf(": can't handle card info\n");
209 1.1 matt return;
210 1.1 matt }
211 1.1 matt
212 1.1 matt /* Enable the card. */
213 1.1 matt pcmcia_function_init(pa->pf, cfe);
214 1.1 matt if (pcmcia_function_enable(pa->pf)) {
215 1.5 enami printf(": function enable failed\n");
216 1.1 matt return;
217 1.1 matt }
218 1.1 matt
219 1.9 kenh /*
220 1.9 kenh * XXX DEC Mobile Media CDROM is not yet tested whether it works
221 1.9 kenh * XXX with PCMCIA_WIDTH_IO16. HAGIWARA SYS-COM HPC-CF32 doesn't
222 1.9 kenh * XXX work with PCMCIA_WIDTH_AUTO.
223 1.9 kenh * XXX CANON FC-8M (SANDISK SDCFB 8M) works for both _AUTO and IO16.
224 1.9 kenh * XXX So, here is temporary work around.
225 1.9 kenh */
226 1.9 kenh wpp = wdc_pcmcia_lookup(pa);
227 1.9 kenh if (wpp == NULL)
228 1.9 kenh panic("wdc_pcmcia_attach: impossible");
229 1.9 kenh
230 1.9 kenh if (pcmcia_io_map(pa->pf,
231 1.9 kenh wpp->wpp_quirk_flag & WDC_PCMCIA_FORCE_16BIT_IO ?
232 1.9 kenh PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_AUTO, 0,
233 1.9 kenh sc->sc_pioh.size, &sc->sc_pioh, &sc->sc_iowindow)) {
234 1.5 enami printf(": can't map first I/O space\n");
235 1.1 matt return;
236 1.1 matt }
237 1.9 kenh
238 1.9 kenh /*
239 1.9 kenh * Currently, # of iospace is 1 except DIGITAL Mobile Media CD-ROM.
240 1.9 kenh * So whether the work around like above is necessary or not
241 1.9 kenh * is unknown. XXX.
242 1.9 kenh */
243 1.9 kenh if (cfe->num_iospace > 1 &&
244 1.9 kenh pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
245 1.9 kenh sc->sc_auxpioh.size, &sc->sc_auxpioh, &sc->sc_auxiowindow)) {
246 1.5 enami printf(": can't map second I/O space\n");
247 1.1 matt return;
248 1.1 matt }
249 1.5 enami
250 1.5 enami printf("\n");
251 1.12 bouyer sc->sc_ih = pcmcia_intr_establish(pa->pf, IPL_BIO, wdcintr,
252 1.12 bouyer &sc->wdc_channel);
253 1.1 matt if (sc->sc_ih == NULL) {
254 1.1 matt printf("%s: couldn't establish interrupt\n", self->dv_xname);
255 1.1 matt return;
256 1.1 matt }
257 1.9 kenh
258 1.12 bouyer sc->wdc_channel.cmd_iot = sc->sc_pioh.iot;
259 1.12 bouyer sc->wdc_channel.cmd_ioh = sc->sc_pioh.ioh;
260 1.12 bouyer sc->wdc_channel.ctl_iot = sc->sc_auxpioh.iot;
261 1.12 bouyer sc->wdc_channel.ctl_ioh = sc->sc_auxpioh.ioh;
262 1.12 bouyer sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
263 1.12 bouyer sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_ioh;
264 1.12 bouyer sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32;
265 1.12 bouyer sc->sc_wdcdev.pio_mode = 0;
266 1.12 bouyer sc->sc_wdcdev.channels = &sc->wdc_channel;
267 1.12 bouyer sc->sc_wdcdev.nchannels = 1;
268 1.12 bouyer sc->wdc_channel.channel = 0;
269 1.12 bouyer sc->wdc_channel.wdc = &sc->sc_wdcdev;
270 1.12 bouyer sc->wdc_channel.ch_queue = malloc(sizeof(struct channel_queue),
271 1.12 bouyer M_DEVBUF, M_NOWAIT);
272 1.12 bouyer if (sc->wdc_channel.ch_queue == NULL) {
273 1.12 bouyer printf("%s: can't allocate memory for command queue",
274 1.12 bouyer sc->sc_wdcdev.sc_dev.dv_xname);
275 1.12 bouyer return;
276 1.12 bouyer }
277 1.9 kenh if (wpp->wpp_quirk_flag & WDC_PCMCIA_NO_EXTRA_RESETS)
278 1.12 bouyer sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
279 1.12 bouyer wdcattach(&sc->wdc_channel);
280 1.1 matt }
281