wdc_pcmcia.c revision 1.32 1 /* $NetBSD: wdc_pcmcia.c,v 1.32 2000/04/01 17:52:14 bouyer 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 <vm/vm.h>
45
46 #include <machine/bus.h>
47 #include <machine/intr.h>
48
49 #include <dev/pcmcia/pcmciareg.h>
50 #include <dev/pcmcia/pcmciavar.h>
51 #include <dev/pcmcia/pcmciadevs.h>
52
53 #include <dev/ata/atavar.h>
54 #include <dev/ic/wdcvar.h>
55
56 #define WDC_PCMCIA_REG_NPORTS 8
57 #define WDC_PCMCIA_AUXREG_OFFSET (WDC_PCMCIA_REG_NPORTS + 6)
58 #define WDC_PCMCIA_AUXREG_NPORTS 2
59
60 struct wdc_pcmcia_softc {
61 struct wdc_softc sc_wdcdev;
62 struct channel_softc *wdc_chanptr;
63 struct channel_softc wdc_channel;
64 struct pcmcia_io_handle sc_pioh;
65 struct pcmcia_io_handle sc_auxpioh;
66 int sc_iowindow;
67 int sc_auxiowindow;
68 void *sc_ih;
69 struct pcmcia_function *sc_pf;
70 int sc_flags;
71 #define WDC_PCMCIA_ATTACH 0x0001
72 };
73
74 static int wdc_pcmcia_match __P((struct device *, struct cfdata *, void *));
75 static void wdc_pcmcia_attach __P((struct device *, struct device *, void *));
76 static int wdc_pcmcia_detach __P((struct device *, int));
77
78 struct cfattach wdc_pcmcia_ca = {
79 sizeof(struct wdc_pcmcia_softc), wdc_pcmcia_match, wdc_pcmcia_attach,
80 wdc_pcmcia_detach, wdcactivate
81 };
82
83 struct wdc_pcmcia_product {
84 u_int32_t wpp_vendor; /* vendor ID */
85 u_int32_t wpp_product; /* product ID */
86 int wpp_quirk_flag; /* Quirk flags */
87 #define WDC_PCMCIA_NO_EXTRA_RESETS 0x02 /* Only reset ctrl once */
88 const char *wpp_cis_info[4]; /* XXX necessary? */
89 const char *wpp_name; /* product name */
90 } wdc_pcmcia_products[] = {
91
92 { /* PCMCIA_VENDOR_DIGITAL XXX */ 0x0100,
93 PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM,
94 0, { NULL, "Digital Mobile Media CD-ROM", NULL, NULL },
95 PCMCIA_STR_DIGITAL_MOBILE_MEDIA_CDROM },
96
97 { PCMCIA_VENDOR_IBM,
98 PCMCIA_PRODUCT_IBM_PORTABLE_CDROM,
99 0, { NULL, "PCMCIA Portable CD-ROM Drive", NULL, NULL },
100 PCMCIA_STR_IBM_PORTABLE_CDROM },
101
102 /* The TEAC IDE/Card II is used on the Sony Vaio */
103 { PCMCIA_VENDOR_TEAC,
104 PCMCIA_PRODUCT_TEAC_IDECARDII,
105 WDC_PCMCIA_NO_EXTRA_RESETS,
106 PCMCIA_CIS_TEAC_IDECARDII,
107 PCMCIA_STR_TEAC_IDECARDII },
108
109 /*
110 * EXP IDE/ATAPI DVD Card use with some DVD players.
111 * Does not have a vendor ID or product ID.
112 */
113 { -1,
114 -1,
115 0,
116 PCMCIA_CIS_EXP_EXPMULTIMEDIA,
117 PCMCIA_STR_EXP_EXPMULTIMEDIA },
118
119 /* Mobile Dock 2, which doesn't have vendor ID nor product ID */
120 { -1, -1, 0,
121 { "SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", NULL, NULL},
122 "SHUTTLE TECHNOLOGY IDE/ATAPI Adapter"
123 },
124
125 { 0, 0, 0, { NULL, NULL, NULL, NULL}, NULL }
126 };
127
128 struct wdc_pcmcia_disk_device_interface_args {
129 int ddi_type; /* interface type */
130 int ddi_reqfn; /* function we are requesting iftype */
131 int ddi_curfn; /* function we are currently parsing in CIS */
132 };
133
134 int wdc_pcmcia_disk_device_interface_callback __P((struct pcmcia_tuple *,
135 void *));
136 int wdc_pcmcia_disk_device_interface __P((struct pcmcia_function *));
137 struct wdc_pcmcia_product *
138 wdc_pcmcia_lookup __P((struct pcmcia_attach_args *));
139
140 int wdc_pcmcia_enable __P((void *, int));
141
142 int
143 wdc_pcmcia_disk_device_interface_callback(tuple, arg)
144 struct pcmcia_tuple *tuple;
145 void *arg;
146 {
147 struct wdc_pcmcia_disk_device_interface_args *ddi = arg;
148
149 switch (tuple->code) {
150 case PCMCIA_CISTPL_FUNCID:
151 ddi->ddi_curfn++;
152 break;
153
154 case PCMCIA_CISTPL_FUNCE:
155 if (ddi->ddi_reqfn != ddi->ddi_curfn)
156 break;
157
158 /* subcode (disk device interface), data (interface type) */
159 if (tuple->length < 2)
160 break;
161
162 /* check type */
163 if (pcmcia_tuple_read_1(tuple, 0) !=
164 PCMCIA_TPLFE_TYPE_DISK_DEVICE_INTERFACE)
165 break;
166
167 ddi->ddi_type = pcmcia_tuple_read_1(tuple, 1);
168 return (1);
169 }
170 return (0);
171 }
172
173 int
174 wdc_pcmcia_disk_device_interface(pf)
175 struct pcmcia_function *pf;
176 {
177 struct wdc_pcmcia_disk_device_interface_args ddi;
178
179 ddi.ddi_reqfn = pf->number;
180 ddi.ddi_curfn = -1;
181 if (pcmcia_scan_cis((struct device *)pf->sc,
182 wdc_pcmcia_disk_device_interface_callback, &ddi) > 0)
183 return (ddi.ddi_type);
184 else
185 return (-1);
186 }
187
188 struct wdc_pcmcia_product *
189 wdc_pcmcia_lookup(pa)
190 struct pcmcia_attach_args *pa;
191 {
192 struct wdc_pcmcia_product *wpp;
193 int i, cis_match;
194
195 for (wpp = wdc_pcmcia_products; wpp->wpp_name != NULL; wpp++)
196 if ((wpp->wpp_vendor == -1 ||
197 pa->manufacturer == wpp->wpp_vendor) &&
198 (wpp->wpp_product == -1 ||
199 pa->product == wpp->wpp_product)) {
200 cis_match = 1;
201 for (i = 0; i < 4; i++) {
202 if (!(wpp->wpp_cis_info[i] == NULL ||
203 (pa->card->cis1_info[i] != NULL &&
204 strcmp(pa->card->cis1_info[i],
205 wpp->wpp_cis_info[i]) == 0)))
206 cis_match = 0;
207 }
208 if (cis_match)
209 return (wpp);
210 }
211
212 return (NULL);
213 }
214
215 static int
216 wdc_pcmcia_match(parent, match, aux)
217 struct device *parent;
218 struct cfdata *match;
219 void *aux;
220 {
221 struct pcmcia_attach_args *pa = aux;
222 struct pcmcia_softc *sc;
223 int iftype;
224
225 if (wdc_pcmcia_lookup(pa) != NULL)
226 return (1);
227
228 if (pa->pf->function == PCMCIA_FUNCTION_DISK) {
229 sc = pa->pf->sc;
230
231 pcmcia_chip_socket_enable(sc->pct, sc->pch);
232 iftype = wdc_pcmcia_disk_device_interface(pa->pf);
233 pcmcia_chip_socket_disable(sc->pct, sc->pch);
234
235 if (iftype == PCMCIA_TPLFE_DDI_PCCARD_ATA)
236 return (1);
237 }
238
239 return (0);
240 }
241
242 static void
243 wdc_pcmcia_attach(parent, self, aux)
244 struct device *parent;
245 struct device *self;
246 void *aux;
247 {
248 struct wdc_pcmcia_softc *sc = (void *)self;
249 struct pcmcia_attach_args *pa = aux;
250 struct pcmcia_config_entry *cfe;
251 struct wdc_pcmcia_product *wpp;
252 int quirks;
253
254 sc->sc_pf = pa->pf;
255
256 for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
257 cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
258 if (cfe->num_iospace != 1 && cfe->num_iospace != 2)
259 continue;
260
261 if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
262 cfe->iospace[0].length,
263 cfe->iospace[0].start == 0 ? cfe->iospace[0].length : 0,
264 &sc->sc_pioh))
265 continue;
266
267 if (cfe->num_iospace == 2) {
268 if (!pcmcia_io_alloc(pa->pf, cfe->iospace[1].start,
269 cfe->iospace[1].length, 0, &sc->sc_auxpioh))
270 break;
271 } else /* num_iospace == 1 */ {
272 sc->sc_auxpioh.iot = sc->sc_pioh.iot;
273 if (!bus_space_subregion(sc->sc_pioh.iot,
274 sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET,
275 WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh))
276 break;
277 }
278 pcmcia_io_free(pa->pf, &sc->sc_pioh);
279 }
280
281 if (cfe == NULL) {
282 printf(": can't handle card info\n");
283 goto no_config_entry;
284 }
285
286 /* Enable the card. */
287 pcmcia_function_init(pa->pf, cfe);
288 if (pcmcia_function_enable(pa->pf)) {
289 printf(": function enable failed\n");
290 goto enable_failed;
291 }
292
293 wpp = wdc_pcmcia_lookup(pa);
294 if (wpp != NULL)
295 quirks = wpp->wpp_quirk_flag;
296 else
297 quirks = 0;
298
299 if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
300 sc->sc_pioh.size, &sc->sc_pioh, &sc->sc_iowindow)) {
301 printf(": can't map first I/O space\n");
302 goto iomap_failed;
303 }
304
305 if (cfe->num_iospace <= 1)
306 sc->sc_auxiowindow = -1;
307 else if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
308 sc->sc_auxpioh.size, &sc->sc_auxpioh, &sc->sc_auxiowindow)) {
309 printf(": can't map second I/O space\n");
310 goto iomapaux_failed;
311 }
312
313 if ((wpp != NULL) && (wpp->wpp_name != NULL))
314 printf(": %s", wpp->wpp_name);
315
316 printf("\n");
317
318 sc->wdc_channel.cmd_iot = sc->sc_pioh.iot;
319 sc->wdc_channel.cmd_ioh = sc->sc_pioh.ioh;
320 sc->wdc_channel.ctl_iot = sc->sc_auxpioh.iot;
321 sc->wdc_channel.ctl_ioh = sc->sc_auxpioh.ioh;
322 sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
323 sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_ioh;
324 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32;
325 sc->sc_wdcdev.PIO_cap = 0;
326 sc->wdc_chanptr = &sc->wdc_channel;
327 sc->sc_wdcdev.channels = &sc->wdc_chanptr;
328 sc->sc_wdcdev.nchannels = 1;
329 sc->wdc_channel.channel = 0;
330 sc->wdc_channel.wdc = &sc->sc_wdcdev;
331 sc->wdc_channel.ch_queue = malloc(sizeof(struct channel_queue),
332 M_DEVBUF, M_NOWAIT);
333 if (sc->wdc_channel.ch_queue == NULL) {
334 printf("%s: can't allocate memory for command queue\n",
335 sc->sc_wdcdev.sc_dev.dv_xname);
336 goto ch_queue_alloc_failed;
337 }
338 if (quirks & WDC_PCMCIA_NO_EXTRA_RESETS)
339 sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
340
341 /* We can enable and disable the controller. */
342 sc->sc_wdcdev.sc_atapi_adapter._generic.scsipi_enable =
343 wdc_pcmcia_enable;
344
345 sc->sc_flags |= WDC_PCMCIA_ATTACH;
346 wdcattach(&sc->wdc_channel); /* should return an error XXX */
347 sc->sc_flags &= ~WDC_PCMCIA_ATTACH;
348 return;
349
350 ch_queue_alloc_failed:
351 /* Unmap our aux i/o window. */
352 if (sc->sc_auxiowindow != -1)
353 pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
354
355 iomapaux_failed:
356 /* Unmap our i/o window. */
357 pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
358
359 iomap_failed:
360 /* Disable the function */
361 pcmcia_function_disable(sc->sc_pf);
362
363 enable_failed:
364 /* Unmap our i/o space. */
365 pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
366 if (cfe->num_iospace == 2)
367 pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
368
369 no_config_entry:
370 sc->sc_iowindow = -1;
371 }
372
373 int
374 wdc_pcmcia_detach(self, flags)
375 struct device *self;
376 int flags;
377 {
378 struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
379 int error;
380
381 if (sc->sc_iowindow == -1)
382 /* Nothing to detach */
383 return (0);
384
385 if ((error = wdcdetach(self, flags)) != 0)
386 return (error);
387
388 if (sc->wdc_channel.ch_queue != NULL)
389 free(sc->wdc_channel.ch_queue, M_DEVBUF);
390
391 /* Unmap our i/o window and i/o space. */
392 pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
393 pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
394 if (sc->sc_auxiowindow != -1) {
395 pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
396 pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
397 }
398
399 return (0);
400 }
401
402 int
403 wdc_pcmcia_enable(arg, onoff)
404 void *arg;
405 int onoff;
406 {
407 struct wdc_pcmcia_softc *sc = arg;
408
409 if (onoff) {
410 /* See the comment in aic_pcmcia_enable */
411 if ((sc->sc_flags & WDC_PCMCIA_ATTACH) == 0) {
412 /* Establish the interrupt handler. */
413 sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
414 wdcintr, &sc->wdc_channel);
415 if (sc->sc_ih == NULL) {
416 printf("%s: "
417 "couldn't establish interrupt handler\n",
418 sc->sc_wdcdev.sc_dev.dv_xname);
419 return (EIO);
420 }
421
422 if (pcmcia_function_enable(sc->sc_pf)) {
423 printf("%s: couldn't enable PCMCIA function\n",
424 sc->sc_wdcdev.sc_dev.dv_xname);
425 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
426 return (EIO);
427 }
428 wdcreset(&sc->wdc_channel, VERBOSE);
429 }
430 } else {
431 pcmcia_function_disable(sc->sc_pf);
432 if ((sc->sc_flags & WDC_PCMCIA_ATTACH) == 0)
433 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
434 }
435
436 return (0);
437 }
438