wdc_pcmcia.c revision 1.71 1 /* $NetBSD: wdc_pcmcia.c,v 1.71 2004/08/09 18:11:01 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2003 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/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: wdc_pcmcia.c,v 1.71 2004/08/09 18:11:01 mycroft Exp $");
41
42 #include <sys/param.h>
43 #include <sys/device.h>
44 #include <sys/malloc.h>
45 #include <sys/systm.h>
46
47 #include <machine/bus.h>
48 #include <machine/intr.h>
49
50 #include <dev/pcmcia/pcmciareg.h>
51 #include <dev/pcmcia/pcmciavar.h>
52 #include <dev/pcmcia/pcmciadevs.h>
53
54 #include <dev/ic/wdcreg.h>
55 #include <dev/ata/atavar.h>
56 #include <dev/ic/wdcvar.h>
57
58 #define WDC_PCMCIA_REG_NPORTS 8
59 #define WDC_PCMCIA_AUXREG_OFFSET (WDC_PCMCIA_REG_NPORTS + 6)
60 #define WDC_PCMCIA_AUXREG_NPORTS 2
61
62 struct wdc_pcmcia_softc {
63 struct wdc_softc sc_wdcdev;
64 struct wdc_channel *wdc_chanlist[1];
65 struct wdc_channel wdc_channel;
66 struct ata_queue wdc_chqueue;
67 struct pcmcia_io_handle sc_pioh;
68 struct pcmcia_io_handle sc_auxpioh;
69 struct pcmcia_mem_handle sc_pmembaseh;
70 struct pcmcia_mem_handle sc_pmemh;
71 struct pcmcia_mem_handle sc_auxpmemh;
72 int sc_memwindow;
73 int sc_iowindow;
74 int sc_auxiowindow;
75 void *sc_ih;
76 struct pcmcia_function *sc_pf;
77 int sc_flags;
78 #define WDC_PCMCIA_ATTACH 0x0001
79 #define WDC_PCMCIA_MEMMODE 0x0002
80 };
81
82 static int wdc_pcmcia_match __P((struct device *, struct cfdata *, void *));
83 static void wdc_pcmcia_attach __P((struct device *, struct device *, void *));
84 static int wdc_pcmcia_detach __P((struct device *, int));
85
86 CFATTACH_DECL(wdc_pcmcia, sizeof(struct wdc_pcmcia_softc),
87 wdc_pcmcia_match, wdc_pcmcia_attach, wdc_pcmcia_detach, wdcactivate);
88
89 const struct wdc_pcmcia_product {
90 u_int32_t wpp_vendor; /* vendor ID */
91 u_int32_t wpp_product; /* product ID */
92 int wpp_quirk_flag; /* Quirk flags */
93 #define WDC_PCMCIA_NO_EXTRA_RESETS 0x02 /* Only reset ctrl once */
94 const char *wpp_cis_info[4]; /* XXX necessary? */
95 } wdc_pcmcia_products[] = {
96
97 { /* PCMCIA_VENDOR_DIGITAL XXX */ 0x0100,
98 PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM,
99 0, {NULL, "Digital Mobile Media CD-ROM", NULL, NULL} },
100
101 { PCMCIA_VENDOR_IBM,
102 PCMCIA_PRODUCT_IBM_PORTABLE_CDROM,
103 0, {NULL, "PCMCIA Portable CD-ROM Drive", NULL, NULL} },
104
105 /* The TEAC IDE/Card II is used on the Sony Vaio */
106 { PCMCIA_VENDOR_TEAC,
107 PCMCIA_PRODUCT_TEAC_IDECARDII,
108 WDC_PCMCIA_NO_EXTRA_RESETS,
109 PCMCIA_CIS_TEAC_IDECARDII },
110
111 /*
112 * A fujitsu rebranded panasonic drive that reports
113 * itself as function "scsi", disk interface 0
114 */
115 { PCMCIA_VENDOR_PANASONIC,
116 PCMCIA_PRODUCT_PANASONIC_KXLC005,
117 0,
118 PCMCIA_CIS_PANASONIC_KXLC005 },
119
120 /*
121 * EXP IDE/ATAPI DVD Card use with some DVD players.
122 * Does not have a vendor ID or product ID.
123 */
124 { -1, -1, 0,
125 PCMCIA_CIS_EXP_EXPMULTIMEDIA },
126
127 /* Mobile Dock 2, neither vendor ID nor product ID */
128 { -1, -1, 0,
129 {"SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", NULL, NULL} },
130
131 /* Toshiba Portege 3110 CD, neither vendor ID nor product ID */
132 { -1, -1, 0,
133 {"FREECOM", "PCCARD-IDE", NULL, NULL} },
134
135 /* Random CD-ROM, (badged AMACOM), neither vendor ID nor product ID */
136 { -1, -1, 0,
137 {"PCMCIA", "CD-ROM", NULL, NULL} },
138
139 /* IO DATA CBIDE2, with neither vendor ID nor product ID */
140 { -1, -1, 0,
141 PCMCIA_CIS_IODATA_CBIDE2 },
142
143 /* TOSHIBA PA2673U(IODATA_CBIDE2 OEM), */
144 /* with neither vendor ID nor product ID */
145 { -1, -1, 0,
146 PCMCIA_CIS_TOSHIBA_CBIDE2 },
147
148 /*
149 * Novac PCMCIA-IDE Card for HD530P IDE Box,
150 * with neither vendor ID nor product ID
151 */
152 { -1, -1, 0,
153 {"PCMCIA", "PnPIDE", NULL, NULL} },
154 };
155
156 const struct wdc_pcmcia_product *
157 wdc_pcmcia_lookup __P((struct pcmcia_attach_args *));
158
159 int wdc_pcmcia_enable __P((struct device *, int));
160
161 const struct wdc_pcmcia_product *
162 wdc_pcmcia_lookup(pa)
163 struct pcmcia_attach_args *pa;
164 {
165 const struct wdc_pcmcia_product *wpp;
166 int i, cis_match;
167 int n;
168
169 for (wpp = wdc_pcmcia_products,
170 n = sizeof(wdc_pcmcia_products) / sizeof(wdc_pcmcia_products[0]);
171 n; wpp++, n--) {
172 if ((wpp->wpp_vendor == -1 ||
173 pa->manufacturer == wpp->wpp_vendor) &&
174 (wpp->wpp_product == -1 ||
175 pa->product == wpp->wpp_product)) {
176 cis_match = 1;
177 for (i = 0; i < 4; i++) {
178 if (!(wpp->wpp_cis_info[i] == NULL ||
179 (pa->card->cis1_info[i] != NULL &&
180 strcmp(pa->card->cis1_info[i],
181 wpp->wpp_cis_info[i]) == 0)))
182 cis_match = 0;
183 }
184 if (cis_match)
185 return (wpp);
186 }
187 }
188
189 return (NULL);
190 }
191
192 static int
193 wdc_pcmcia_match(parent, match, aux)
194 struct device *parent;
195 struct cfdata *match;
196 void *aux;
197 {
198 struct pcmcia_attach_args *pa = aux;
199
200 if (pa->pf->function == PCMCIA_FUNCTION_DISK &&
201 pa->pf->pf_funce_disk_interface == PCMCIA_TPLFE_DDI_PCCARD_ATA) {
202 return 10;
203 }
204
205 if (wdc_pcmcia_lookup(pa) != NULL)
206 return (1);
207
208 return (0);
209 }
210
211 static void
212 wdc_pcmcia_attach(parent, self, aux)
213 struct device *parent;
214 struct device *self;
215 void *aux;
216 {
217 struct wdc_pcmcia_softc *sc = (void *)self;
218 struct pcmcia_attach_args *pa = aux;
219 struct pcmcia_config_entry *cfe;
220 const struct wdc_pcmcia_product *wpp;
221 bus_size_t offset = 0;
222 int quirks, i;
223
224 aprint_normal("\n");
225
226 sc->sc_pf = pa->pf;
227
228 SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
229 if (cfe->num_iospace != 1 && cfe->num_iospace != 2)
230 continue;
231
232 if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
233 cfe->iospace[0].length,
234 cfe->iospace[0].start == 0 ? cfe->iospace[0].length : 0,
235 &sc->sc_pioh))
236 continue;
237
238 if (cfe->num_iospace == 2) {
239 if (!pcmcia_io_alloc(pa->pf, cfe->iospace[1].start,
240 cfe->iospace[1].length, 0, &sc->sc_auxpioh))
241 break;
242 } else /* num_iospace == 1 */ {
243 sc->sc_auxpioh.iot = sc->sc_pioh.iot;
244 if (!bus_space_subregion(sc->sc_pioh.iot,
245 sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET,
246 WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh))
247 break;
248 }
249 pcmcia_io_free(pa->pf, &sc->sc_pioh);
250 }
251
252 /*
253 * Compact Flash memory mapped mode
254 * CF+ and CompactFlash Spec. Rev 1.4, 6.1.3 Memory Mapped Addressing.
255 * http://www.compactflash.org/cfspc1_4.pdf
256 */
257 if (cfe == NULL) {
258 SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
259 if (cfe->iftype != PCMCIA_IFTYPE_MEMORY)
260 continue;
261 if (pcmcia_mem_alloc(pa->pf, cfe->memspace[0].length,
262 &sc->sc_pmembaseh) == 0) {
263 sc->sc_flags |= WDC_PCMCIA_MEMMODE;
264 break;
265 }
266 }
267 }
268
269 if (cfe == NULL) {
270 aprint_error("%s: can't handle card info\n", self->dv_xname);
271 goto no_config_entry;
272 }
273
274 /* Enable the card. */
275 pcmcia_function_init(pa->pf, cfe);
276
277 if (sc->sc_flags & WDC_PCMCIA_MEMMODE) {
278 if (pcmcia_mem_map(pa->pf, PCMCIA_MEM_COMMON, 0,
279 sc->sc_pmembaseh.size, &sc->sc_pmembaseh, &offset,
280 &sc->sc_memwindow)) {
281 aprint_error("%s: can't map memory space\n",
282 self->dv_xname);
283 goto map_failed;
284 }
285
286 sc->sc_pmemh.memt = sc->sc_pmembaseh.memt;
287 sc->sc_pmemh.memh = sc->sc_pmembaseh.memh;
288
289 sc->sc_auxpmemh.memt = sc->sc_pmemh.memt;
290 if (bus_space_subregion(sc->sc_pmemh.memt,
291 sc->sc_pmembaseh.memh, WDC_PCMCIA_AUXREG_OFFSET + offset,
292 WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpmemh.memh))
293 goto mapaux_failed;
294
295 aprint_normal("%s: memory mapped mode\n", self->dv_xname);
296 } else {
297 if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, &sc->sc_pioh,
298 &sc->sc_iowindow)) {
299 aprint_error("%s: can't map first I/O space\n",
300 self->dv_xname);
301 goto map_failed;
302 }
303 }
304
305 if (cfe->num_iospace <= 1 || sc->sc_flags & WDC_PCMCIA_MEMMODE)
306 sc->sc_auxiowindow = -1;
307 else if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, &sc->sc_auxpioh,
308 &sc->sc_auxiowindow)) {
309 aprint_error("%s: can't map second I/O space\n",
310 self->dv_xname);
311 goto mapaux_failed;
312 }
313
314 if (wdc_pcmcia_enable(self, 1)) {
315 aprint_error("%s: enable failed\n", self->dv_xname);
316 goto enable_failed;
317 }
318
319 wpp = wdc_pcmcia_lookup(pa);
320 if (wpp != NULL)
321 quirks = wpp->wpp_quirk_flag;
322 else
323 quirks = 0;
324
325 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
326 if (sc->sc_flags & WDC_PCMCIA_MEMMODE) {
327 sc->wdc_channel.cmd_iot = sc->sc_pmemh.memt;
328 sc->wdc_channel.cmd_baseioh = sc->sc_pmemh.memh;
329 sc->wdc_channel.ctl_iot = sc->sc_auxpmemh.memt;
330 sc->wdc_channel.ctl_ioh = sc->sc_auxpmemh.memh;
331 } else {
332 sc->wdc_channel.cmd_iot = sc->sc_pioh.iot;
333 sc->wdc_channel.cmd_baseioh = sc->sc_pioh.ioh;
334 sc->wdc_channel.ctl_iot = sc->sc_auxpioh.iot;
335 sc->wdc_channel.ctl_ioh = sc->sc_auxpioh.ioh;
336 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32;
337 }
338 for (i = 0; i < WDC_PCMCIA_REG_NPORTS; i++) {
339 if (bus_space_subregion(sc->wdc_channel.cmd_iot,
340 sc->wdc_channel.cmd_baseioh,
341 offset + i, i == 0 ? 4 : 1,
342 &sc->wdc_channel.cmd_iohs[i]) != 0) {
343 aprint_error("%s: can't subregion I/O space\n",
344 self->dv_xname);
345 goto mapaux_failed;
346 }
347 }
348 wdc_init_shadow_regs(&sc->wdc_channel);
349 sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
350 sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_iohs[0];
351 sc->sc_wdcdev.PIO_cap = 0;
352 sc->wdc_chanlist[0] = &sc->wdc_channel;
353 sc->sc_wdcdev.channels = sc->wdc_chanlist;
354 sc->sc_wdcdev.nchannels = 1;
355 sc->wdc_channel.ch_channel = 0;
356 sc->wdc_channel.ch_wdc = &sc->sc_wdcdev;
357 sc->wdc_channel.ch_queue = &sc->wdc_chqueue;
358 #if 0
359 if (quirks & WDC_PCMCIA_NO_EXTRA_RESETS)
360 sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
361 #endif
362
363 /* We can enable and disable the controller. */
364 sc->sc_wdcdev.sc_atapi_adapter._generic.adapt_enable =
365 wdc_pcmcia_enable;
366
367 sc->sc_flags |= WDC_PCMCIA_ATTACH;
368 wdcattach(&sc->wdc_channel);
369 return;
370
371 enable_failed:
372 /* Unmap our i/o window. */
373 if (sc->sc_flags & WDC_PCMCIA_MEMMODE)
374 pcmcia_mem_unmap(sc->sc_pf, sc->sc_memwindow);
375 else
376 pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
377
378 mapaux_failed:
379 /* Unmap our i/o space. */
380 if (sc->sc_flags & WDC_PCMCIA_MEMMODE) {
381 pcmcia_mem_free(sc->sc_pf, &sc->sc_pmembaseh);
382 } else {
383 pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
384 if (cfe->num_iospace == 2)
385 pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
386 }
387 map_failed:
388 no_config_entry:
389 sc->sc_iowindow = -1;
390 }
391
392 int
393 wdc_pcmcia_detach(self, flags)
394 struct device *self;
395 int flags;
396 {
397 struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
398 int error;
399
400 if (sc->sc_iowindow == -1)
401 /* Nothing to detach */
402 return (0);
403
404 /*
405 * If the WDC_PCMCIA_ATTACH flag is still set, then we didn't get
406 * a chance * enable/disable the card in the wdc/atabus layer, so
407 * we still need to disable the function here.
408 */
409 if (sc->sc_flags & WDC_PCMCIA_ATTACH) {
410 sc->sc_flags &= ~WDC_PCMCIA_ATTACH;
411 pcmcia_function_disable(sc->sc_pf);
412 }
413
414 if ((error = wdcdetach(self, flags)) != 0)
415 return (error);
416
417 /* Unmap our i/o window and i/o space. */
418 if (sc->sc_flags & WDC_PCMCIA_MEMMODE) {
419 pcmcia_mem_unmap(sc->sc_pf, sc->sc_memwindow);
420 pcmcia_mem_free(sc->sc_pf, &sc->sc_pmembaseh);
421 } else {
422 pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
423 pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
424 if (sc->sc_auxiowindow != -1) {
425 pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
426 pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
427 }
428 }
429
430 return (0);
431 }
432
433 int
434 wdc_pcmcia_enable(self, onoff)
435 struct device *self;
436 int onoff;
437 {
438 struct wdc_pcmcia_softc *sc = (void *)self;
439
440 if (onoff) {
441 /*
442 * If the WDC_PCMCIA_ATTACH flag is set, we've already
443 * enabled the card in the attach routine, so don't
444 * re-enable it here (to save power cycle time). Clear
445 * the flag, though, so that the next disable/enable
446 * will do the right thing.
447 */
448 if (sc->sc_flags & WDC_PCMCIA_ATTACH) {
449 sc->sc_flags &= ~WDC_PCMCIA_ATTACH;
450 } else {
451 /* Establish the interrupt handler. */
452 sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
453 wdcintr, &sc->wdc_channel);
454 if (sc->sc_ih == NULL) {
455 printf("%s: couldn't establish interrupt handler\n",
456 sc->sc_wdcdev.sc_dev.dv_xname);
457 return (EIO);
458 }
459
460 if (pcmcia_function_enable(sc->sc_pf)) {
461 printf("%s: couldn't enable PCMCIA function\n",
462 sc->sc_wdcdev.sc_dev.dv_xname);
463 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
464 return (EIO);
465 }
466 }
467 } else {
468 pcmcia_function_disable(sc->sc_pf);
469 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
470 }
471
472 return (0);
473 }
474