wdc_pcmcia.c revision 1.37 1 /* $NetBSD: wdc_pcmcia.c,v 1.37 2000/12/20 09:56:34 haya Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum, by Onno van der Linden and by Manuel Bouyer.
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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/param.h>
40 #include <sys/device.h>
41 #include <sys/malloc.h>
42 #include <sys/systm.h>
43
44 #include <machine/bus.h>
45 #include <machine/intr.h>
46
47 #include <dev/pcmcia/pcmciareg.h>
48 #include <dev/pcmcia/pcmciavar.h>
49 #include <dev/pcmcia/pcmciadevs.h>
50
51 #include <dev/ata/atavar.h>
52 #include <dev/ic/wdcvar.h>
53
54 #define WDC_PCMCIA_REG_NPORTS 8
55 #define WDC_PCMCIA_AUXREG_OFFSET (WDC_PCMCIA_REG_NPORTS + 6)
56 #define WDC_PCMCIA_AUXREG_NPORTS 2
57
58 struct wdc_pcmcia_softc {
59 struct wdc_softc sc_wdcdev;
60 struct channel_softc *wdc_chanptr;
61 struct channel_softc wdc_channel;
62 struct pcmcia_io_handle sc_pioh;
63 struct pcmcia_io_handle sc_auxpioh;
64 int sc_iowindow;
65 int sc_auxiowindow;
66 void *sc_ih;
67 struct pcmcia_function *sc_pf;
68 int sc_flags;
69 #define WDC_PCMCIA_ATTACH 0x0001
70 };
71
72 static int wdc_pcmcia_match __P((struct device *, struct cfdata *, void *));
73 static void wdc_pcmcia_attach __P((struct device *, struct device *, void *));
74 static int wdc_pcmcia_detach __P((struct device *, int));
75
76 struct cfattach wdc_pcmcia_ca = {
77 sizeof(struct wdc_pcmcia_softc), wdc_pcmcia_match, wdc_pcmcia_attach,
78 wdc_pcmcia_detach, wdcactivate
79 };
80
81 struct wdc_pcmcia_product {
82 u_int32_t wpp_vendor; /* vendor ID */
83 u_int32_t wpp_product; /* product ID */
84 int wpp_quirk_flag; /* Quirk flags */
85 #define WDC_PCMCIA_NO_EXTRA_RESETS 0x02 /* Only reset ctrl once */
86 const char *wpp_cis_info[4]; /* XXX necessary? */
87 const char *wpp_name; /* product name */
88 } wdc_pcmcia_products[] = {
89
90 { /* PCMCIA_VENDOR_DIGITAL XXX */ 0x0100,
91 PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM,
92 0, { NULL, "Digital Mobile Media CD-ROM", NULL, NULL },
93 PCMCIA_STR_DIGITAL_MOBILE_MEDIA_CDROM },
94
95 { PCMCIA_VENDOR_IBM,
96 PCMCIA_PRODUCT_IBM_PORTABLE_CDROM,
97 0, { NULL, "PCMCIA Portable CD-ROM Drive", NULL, NULL },
98 PCMCIA_STR_IBM_PORTABLE_CDROM },
99
100 /* The TEAC IDE/Card II is used on the Sony Vaio */
101 { PCMCIA_VENDOR_TEAC,
102 PCMCIA_PRODUCT_TEAC_IDECARDII,
103 WDC_PCMCIA_NO_EXTRA_RESETS,
104 PCMCIA_CIS_TEAC_IDECARDII,
105 PCMCIA_STR_TEAC_IDECARDII },
106
107 /*
108 * EXP IDE/ATAPI DVD Card use with some DVD players.
109 * Does not have a vendor ID or product ID.
110 */
111 { -1,
112 -1,
113 0,
114 PCMCIA_CIS_EXP_EXPMULTIMEDIA,
115 PCMCIA_STR_EXP_EXPMULTIMEDIA },
116
117 /* Mobile Dock 2, neither vendor ID nor product ID */
118 { -1, -1, 0,
119 { "SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", NULL, NULL},
120 "SHUTTLE TECHNOLOGY IDE/ATAPI Adapter"
121 },
122
123 /* Toshiba Portege 3110 CD, neither vendor ID nor product ID */
124 { -1, -1, 0,
125 { "FREECOM", "PCCARD-IDE", NULL, NULL},
126 "FREECOM PCCARD-IDE"
127 },
128
129 /* Random CD-ROM, (badged AMACOM), neither vendor ID nor product ID */
130 { -1, -1, 0,
131 { "PCMCIA", "CD-ROM", NULL, NULL},
132 "PCMCIA CD-ROM"
133 },
134
135 /* IO DATA CBIDE2, with neither vendor ID nor product ID */
136 { -1, -1, 0,
137 PCMCIA_CIS_IODATA_CBIDE2,
138 PCMCIA_STR_IODATA_CBIDE2
139 },
140
141 { 0, 0, 0, { NULL, NULL, NULL, NULL}, NULL }
142 };
143
144 struct wdc_pcmcia_product *
145 wdc_pcmcia_lookup __P((struct pcmcia_attach_args *));
146
147 int wdc_pcmcia_enable __P((void *, int));
148
149
150
151 struct wdc_pcmcia_product *
152 wdc_pcmcia_lookup(pa)
153 struct pcmcia_attach_args *pa;
154 {
155 struct wdc_pcmcia_product *wpp;
156 int i, cis_match;
157
158 for (wpp = wdc_pcmcia_products; wpp->wpp_name != NULL; wpp++)
159 if ((wpp->wpp_vendor == -1 ||
160 pa->manufacturer == wpp->wpp_vendor) &&
161 (wpp->wpp_product == -1 ||
162 pa->product == wpp->wpp_product)) {
163 cis_match = 1;
164 for (i = 0; i < 4; i++) {
165 if (!(wpp->wpp_cis_info[i] == NULL ||
166 (pa->card->cis1_info[i] != NULL &&
167 strcmp(pa->card->cis1_info[i],
168 wpp->wpp_cis_info[i]) == 0)))
169 cis_match = 0;
170 }
171 if (cis_match)
172 return (wpp);
173 }
174
175 return (NULL);
176 }
177
178 static int
179 wdc_pcmcia_match(parent, match, aux)
180 struct device *parent;
181 struct cfdata *match;
182 void *aux;
183 {
184 struct pcmcia_attach_args *pa = aux;
185
186 if (pa->pf->function == PCMCIA_FUNCTION_DISK &&
187 pa->pf->pf_funce_disk_interface == PCMCIA_TPLFE_DDI_PCCARD_ATA) {
188 return 10;
189 }
190
191 if (wdc_pcmcia_lookup(pa) != NULL)
192 return (1);
193
194 return (0);
195 }
196
197 static void
198 wdc_pcmcia_attach(parent, self, aux)
199 struct device *parent;
200 struct device *self;
201 void *aux;
202 {
203 struct wdc_pcmcia_softc *sc = (void *)self;
204 struct pcmcia_attach_args *pa = aux;
205 struct pcmcia_config_entry *cfe;
206 struct wdc_pcmcia_product *wpp;
207 int quirks;
208
209 sc->sc_pf = pa->pf;
210
211 for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
212 cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
213 if (cfe->num_iospace != 1 && cfe->num_iospace != 2)
214 continue;
215
216 if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
217 cfe->iospace[0].length,
218 cfe->iospace[0].start == 0 ? cfe->iospace[0].length : 0,
219 &sc->sc_pioh))
220 continue;
221
222 if (cfe->num_iospace == 2) {
223 if (!pcmcia_io_alloc(pa->pf, cfe->iospace[1].start,
224 cfe->iospace[1].length, 0, &sc->sc_auxpioh))
225 break;
226 } else /* num_iospace == 1 */ {
227 sc->sc_auxpioh.iot = sc->sc_pioh.iot;
228 if (!bus_space_subregion(sc->sc_pioh.iot,
229 sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET,
230 WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh))
231 break;
232 }
233 pcmcia_io_free(pa->pf, &sc->sc_pioh);
234 }
235
236 if (cfe == NULL) {
237 printf(": can't handle card info\n");
238 goto no_config_entry;
239 }
240
241 /* Enable the card. */
242 pcmcia_function_init(pa->pf, cfe);
243 if (pcmcia_function_enable(pa->pf)) {
244 printf(": function enable failed\n");
245 goto enable_failed;
246 }
247
248 wpp = wdc_pcmcia_lookup(pa);
249 if (wpp != NULL)
250 quirks = wpp->wpp_quirk_flag;
251 else
252 quirks = 0;
253
254 if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
255 sc->sc_pioh.size, &sc->sc_pioh, &sc->sc_iowindow)) {
256 printf(": can't map first I/O space\n");
257 goto iomap_failed;
258 }
259
260 if (cfe->num_iospace <= 1)
261 sc->sc_auxiowindow = -1;
262 else if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
263 sc->sc_auxpioh.size, &sc->sc_auxpioh, &sc->sc_auxiowindow)) {
264 printf(": can't map second I/O space\n");
265 goto iomapaux_failed;
266 }
267
268 if ((wpp != NULL) && (wpp->wpp_name != NULL))
269 printf(": %s", wpp->wpp_name);
270
271 printf("\n");
272
273 sc->wdc_channel.cmd_iot = sc->sc_pioh.iot;
274 sc->wdc_channel.cmd_ioh = sc->sc_pioh.ioh;
275 sc->wdc_channel.ctl_iot = sc->sc_auxpioh.iot;
276 sc->wdc_channel.ctl_ioh = sc->sc_auxpioh.ioh;
277 sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
278 sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_ioh;
279 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32;
280 sc->sc_wdcdev.PIO_cap = 0;
281 sc->wdc_chanptr = &sc->wdc_channel;
282 sc->sc_wdcdev.channels = &sc->wdc_chanptr;
283 sc->sc_wdcdev.nchannels = 1;
284 sc->wdc_channel.channel = 0;
285 sc->wdc_channel.wdc = &sc->sc_wdcdev;
286 sc->wdc_channel.ch_queue = malloc(sizeof(struct channel_queue),
287 M_DEVBUF, M_NOWAIT);
288 if (sc->wdc_channel.ch_queue == NULL) {
289 printf("%s: can't allocate memory for command queue\n",
290 sc->sc_wdcdev.sc_dev.dv_xname);
291 goto ch_queue_alloc_failed;
292 }
293 if (quirks & WDC_PCMCIA_NO_EXTRA_RESETS)
294 sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
295
296 /* We can enable and disable the controller. */
297 sc->sc_wdcdev.sc_atapi_adapter._generic.scsipi_enable =
298 wdc_pcmcia_enable;
299
300 sc->sc_flags |= WDC_PCMCIA_ATTACH;
301 wdcattach(&sc->wdc_channel); /* should return an error XXX */
302 sc->sc_flags &= ~WDC_PCMCIA_ATTACH;
303 return;
304
305 ch_queue_alloc_failed:
306 /* Unmap our aux i/o window. */
307 if (sc->sc_auxiowindow != -1)
308 pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
309
310 iomapaux_failed:
311 /* Unmap our i/o window. */
312 pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
313
314 iomap_failed:
315 /* Disable the function */
316 pcmcia_function_disable(sc->sc_pf);
317
318 enable_failed:
319 /* Unmap our i/o space. */
320 pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
321 if (cfe->num_iospace == 2)
322 pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
323
324 no_config_entry:
325 sc->sc_iowindow = -1;
326 }
327
328 int
329 wdc_pcmcia_detach(self, flags)
330 struct device *self;
331 int flags;
332 {
333 struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
334 int error;
335
336 if (sc->sc_iowindow == -1)
337 /* Nothing to detach */
338 return (0);
339
340 if ((error = wdcdetach(self, flags)) != 0)
341 return (error);
342
343 if (sc->wdc_channel.ch_queue != NULL)
344 free(sc->wdc_channel.ch_queue, M_DEVBUF);
345
346 /* Unmap our i/o window and i/o space. */
347 pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
348 pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
349 if (sc->sc_auxiowindow != -1) {
350 pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
351 pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
352 }
353
354 return (0);
355 }
356
357 int
358 wdc_pcmcia_enable(arg, onoff)
359 void *arg;
360 int onoff;
361 {
362 struct wdc_pcmcia_softc *sc = arg;
363
364 if (onoff) {
365 /* See the comment in aic_pcmcia_enable */
366 if ((sc->sc_flags & WDC_PCMCIA_ATTACH) == 0) {
367 /* Establish the interrupt handler. */
368 sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
369 wdcintr, &sc->wdc_channel);
370 if (sc->sc_ih == NULL) {
371 printf("%s: "
372 "couldn't establish interrupt handler\n",
373 sc->sc_wdcdev.sc_dev.dv_xname);
374 return (EIO);
375 }
376
377 if (pcmcia_function_enable(sc->sc_pf)) {
378 printf("%s: couldn't enable PCMCIA function\n",
379 sc->sc_wdcdev.sc_dev.dv_xname);
380 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
381 return (EIO);
382 }
383 wdcreset(&sc->wdc_channel, VERBOSE);
384 }
385 } else {
386 pcmcia_function_disable(sc->sc_pf);
387 if ((sc->sc_flags & WDC_PCMCIA_ATTACH) == 0)
388 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
389 }
390
391 return (0);
392 }
393