wdc_pcmcia.c revision 1.76 1 /* $NetBSD: wdc_pcmcia.c,v 1.76 2004/08/10 05:22:33 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.76 2004/08/10 05:22:33 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_pmemh;
70 struct pcmcia_mem_handle sc_auxpmemh;
71 void *sc_ih;
72
73 struct pcmcia_function *sc_pf;
74 int sc_state;
75 #define WDC_PCMCIA_ATTACH1 1
76 #define WDC_PCMCIA_ATTACH2 2
77 #define WDC_PCMCIA_ATTACHED 3
78 };
79
80 static int wdc_pcmcia_match __P((struct device *, struct cfdata *, void *));
81 static int wdc_pcmcia_validate_config __P((struct pcmcia_config_entry *));
82 static void wdc_pcmcia_attach __P((struct device *, struct device *, void *));
83 static int wdc_pcmcia_detach __P((struct device *, int));
84
85 CFATTACH_DECL(wdc_pcmcia, sizeof(struct wdc_pcmcia_softc),
86 wdc_pcmcia_match, wdc_pcmcia_attach, wdc_pcmcia_detach, wdcactivate);
87
88 const struct wdc_pcmcia_product {
89 u_int32_t wpp_vendor; /* vendor ID */
90 u_int32_t wpp_product; /* product ID */
91 const char *wpp_cis_info[4]; /* XXX necessary? */
92 } wdc_pcmcia_products[] = {
93
94 { PCMCIA_VENDOR_DIGITAL,
95 PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM,
96 {NULL, "Digital Mobile Media CD-ROM", NULL, NULL} },
97
98 { PCMCIA_VENDOR_IBM,
99 PCMCIA_PRODUCT_IBM_PORTABLE_CDROM,
100 {NULL, "PCMCIA Portable CD-ROM Drive", NULL, NULL} },
101
102 /* The TEAC IDE/Card II is used on the Sony Vaio */
103 { PCMCIA_VENDOR_TEAC,
104 PCMCIA_PRODUCT_TEAC_IDECARDII,
105 PCMCIA_CIS_TEAC_IDECARDII },
106
107 /*
108 * A fujitsu rebranded panasonic drive that reports
109 * itself as function "scsi", disk interface 0
110 */
111 { PCMCIA_VENDOR_PANASONIC,
112 PCMCIA_PRODUCT_PANASONIC_KXLC005,
113 PCMCIA_CIS_PANASONIC_KXLC005 },
114
115 /*
116 * EXP IDE/ATAPI DVD Card use with some DVD players.
117 * Does not have a vendor ID or product ID.
118 */
119 { -1, -1,
120 PCMCIA_CIS_EXP_EXPMULTIMEDIA },
121
122 /* Mobile Dock 2, neither vendor ID nor product ID */
123 { -1, -1,
124 {"SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", NULL, NULL} },
125
126 /* Toshiba Portege 3110 CD, neither vendor ID nor product ID */
127 { -1, -1,
128 {"FREECOM", "PCCARD-IDE", NULL, NULL} },
129
130 /* Random CD-ROM, (badged AMACOM), neither vendor ID nor product ID */
131 { -1, -1,
132 {"PCMCIA", "CD-ROM", NULL, NULL} },
133
134 /* IO DATA CBIDE2, with neither vendor ID nor product ID */
135 { -1, -1,
136 PCMCIA_CIS_IODATA_CBIDE2 },
137
138 /* TOSHIBA PA2673U(IODATA_CBIDE2 OEM), */
139 /* with neither vendor ID nor product ID */
140 { -1, -1,
141 PCMCIA_CIS_TOSHIBA_CBIDE2 },
142
143 /*
144 * Novac PCMCIA-IDE Card for HD530P IDE Box,
145 * with neither vendor ID nor product ID
146 */
147 { -1, -1,
148 {"PCMCIA", "PnPIDE", NULL, NULL} },
149 };
150
151 const struct wdc_pcmcia_product *
152 wdc_pcmcia_lookup __P((struct pcmcia_attach_args *));
153
154 int wdc_pcmcia_enable __P((struct device *, int));
155
156 const struct wdc_pcmcia_product *
157 wdc_pcmcia_lookup(pa)
158 struct pcmcia_attach_args *pa;
159 {
160 const struct wdc_pcmcia_product *wpp;
161 int i, cis_match;
162 int n;
163
164 for (wpp = wdc_pcmcia_products,
165 n = sizeof(wdc_pcmcia_products) / sizeof(wdc_pcmcia_products[0]);
166 n; wpp++, n--) {
167 if ((wpp->wpp_vendor == -1 ||
168 pa->manufacturer == wpp->wpp_vendor) &&
169 (wpp->wpp_product == -1 ||
170 pa->product == wpp->wpp_product)) {
171 cis_match = 1;
172 for (i = 0; i < 4; i++) {
173 if (!(wpp->wpp_cis_info[i] == NULL ||
174 (pa->card->cis1_info[i] != NULL &&
175 strcmp(pa->card->cis1_info[i],
176 wpp->wpp_cis_info[i]) == 0)))
177 cis_match = 0;
178 }
179 if (cis_match)
180 return (wpp);
181 }
182 }
183
184 return (NULL);
185 }
186
187 static int
188 wdc_pcmcia_match(parent, match, aux)
189 struct device *parent;
190 struct cfdata *match;
191 void *aux;
192 {
193 struct pcmcia_attach_args *pa = aux;
194
195 if (pa->pf->function == PCMCIA_FUNCTION_DISK &&
196 pa->pf->pf_funce_disk_interface == PCMCIA_TPLFE_DDI_PCCARD_ATA) {
197 return 10;
198 }
199
200 if (wdc_pcmcia_lookup(pa) != NULL)
201 return (1);
202
203 return (0);
204 }
205
206 static int
207 wdc_pcmcia_validate_config(cfe)
208 struct pcmcia_config_entry *cfe;
209 {
210 switch (cfe->iftype) {
211 #if 0
212 case PCMCIA_IFTYPE_MEMORY:
213 if (cfe->num_iospace != 0 ||
214 cfe->num_memspace != 1)
215 return (EINVAL);
216 break;
217 #endif
218 case PCMCIA_IFTYPE_IO:
219 if (cfe->num_iospace < 1 || cfe->num_iospace > 2)
220 return (EINVAL);
221 cfe->num_memspace = 0;
222 break;
223 default:
224 return (EINVAL);
225 }
226 return (0);
227 }
228
229 static void
230 wdc_pcmcia_attach(parent, self, aux)
231 struct device *parent;
232 struct device *self;
233 void *aux;
234 {
235 struct wdc_pcmcia_softc *sc = (void *)self;
236 struct pcmcia_attach_args *pa = aux;
237 struct pcmcia_config_entry *cfe;
238 const struct wdc_pcmcia_product *wpp;
239 bus_size_t offset;
240 int i;
241 int error;
242
243 aprint_normal("\n");
244 sc->sc_pf = pa->pf;
245
246 /*XXXmem16|common*/
247 error = pcmcia_function_configure(pa->pf, wdc_pcmcia_validate_config);
248 if (error) {
249 aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
250 error);
251 return;
252 }
253
254 cfe = pa->pf->cfe;
255 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
256 if (cfe->iftype == PCMCIA_IFTYPE_MEMORY) {
257 sc->sc_pmemh.memt = cfe->memspace[0].handle.memt;
258 sc->sc_pmemh.memh = cfe->memspace[0].handle.memh;
259 offset = cfe->memspace[0].offset;
260
261 sc->sc_auxpmemh.memt = sc->sc_pmemh.memt;
262 if (bus_space_subregion(sc->sc_pmemh.memt,
263 sc->sc_pmemh.memh, WDC_PCMCIA_AUXREG_OFFSET + offset,
264 WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpmemh.memh))
265 goto fail;
266
267 aprint_normal("%s: memory mapped mode\n", self->dv_xname);
268 sc->wdc_channel.cmd_iot = sc->sc_pmemh.memt;
269 sc->wdc_channel.cmd_baseioh = sc->sc_pmemh.memh;
270 sc->wdc_channel.ctl_iot = sc->sc_auxpmemh.memt;
271 sc->wdc_channel.ctl_ioh = sc->sc_auxpmemh.memh;
272 } else {
273 sc->sc_pioh.iot = cfe->iospace[0].handle.iot;
274 sc->sc_pioh.ioh = cfe->iospace[0].handle.ioh;
275 offset = 0;
276
277 if (cfe->num_iospace == 1) {
278 sc->sc_auxpioh.iot = sc->sc_pioh.iot;
279 if (bus_space_subregion(sc->sc_pioh.iot,
280 sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET,
281 WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh))
282 goto fail;
283 } else {
284 sc->sc_auxpioh.iot = cfe->iospace[1].handle.iot;
285 sc->sc_auxpioh.ioh = cfe->iospace[1].handle.ioh;
286 }
287
288 aprint_normal("%s: i/o mapped mode\n", self->dv_xname);
289 sc->wdc_channel.cmd_iot = sc->sc_pioh.iot;
290 sc->wdc_channel.cmd_baseioh = sc->sc_pioh.ioh;
291 sc->wdc_channel.ctl_iot = sc->sc_auxpioh.iot;
292 sc->wdc_channel.ctl_ioh = sc->sc_auxpioh.ioh;
293 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32;
294 }
295
296 error = wdc_pcmcia_enable(self, 1);
297 if (error) {
298 aprint_error("%s: enable failed, error=%d\n", self->dv_xname,
299 error);
300 goto fail;
301 }
302
303 wpp = wdc_pcmcia_lookup(pa);
304
305 for (i = 0; i < WDC_PCMCIA_REG_NPORTS; i++) {
306 if (bus_space_subregion(sc->wdc_channel.cmd_iot,
307 sc->wdc_channel.cmd_baseioh,
308 offset + i, i == 0 ? 4 : 1,
309 &sc->wdc_channel.cmd_iohs[i]) != 0) {
310 aprint_error("%s: can't subregion I/O space\n",
311 self->dv_xname);
312 goto fail2;
313 }
314 }
315 wdc_init_shadow_regs(&sc->wdc_channel);
316 sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
317 sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_iohs[0];
318 sc->sc_wdcdev.PIO_cap = 0;
319 sc->wdc_chanlist[0] = &sc->wdc_channel;
320 sc->sc_wdcdev.channels = sc->wdc_chanlist;
321 sc->sc_wdcdev.nchannels = 1;
322 sc->wdc_channel.ch_channel = 0;
323 sc->wdc_channel.ch_wdc = &sc->sc_wdcdev;
324 sc->wdc_channel.ch_queue = &sc->wdc_chqueue;
325
326 /* We can enable and disable the controller. */
327 sc->sc_wdcdev.sc_atapi_adapter._generic.adapt_enable =
328 wdc_pcmcia_enable;
329
330 sc->sc_state = WDC_PCMCIA_ATTACH1;
331 wdcattach(&sc->wdc_channel);
332 if (sc->sc_state == WDC_PCMCIA_ATTACH1)
333 wdc_pcmcia_enable(self, 0);
334 sc->sc_state = WDC_PCMCIA_ATTACHED;
335 return;
336
337 fail2:
338 wdc_pcmcia_enable(self, 0);
339 fail:
340 pcmcia_function_unconfigure(pa->pf);
341 }
342
343 int
344 wdc_pcmcia_detach(self, flags)
345 struct device *self;
346 int flags;
347 {
348 struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
349 int error;
350
351 if (sc->sc_state != WDC_PCMCIA_ATTACHED)
352 return (0);
353
354 if ((error = wdcdetach(self, flags)) != 0)
355 return (error);
356
357 pcmcia_function_unconfigure(sc->sc_pf);
358
359 return (0);
360 }
361
362 int
363 wdc_pcmcia_enable(self, onoff)
364 struct device *self;
365 int onoff;
366 {
367 struct wdc_pcmcia_softc *sc = (void *)self;
368
369 if (onoff) {
370 /*
371 * If the WDC_PCMCIA_ATTACH flag is set, we've already
372 * enabled the card in the attach routine, so don't
373 * re-enable it here (to save power cycle time). Clear
374 * the flag, though, so that the next disable/enable
375 * will do the right thing.
376 */
377 if (sc->sc_state == WDC_PCMCIA_ATTACH1) {
378 sc->sc_state = WDC_PCMCIA_ATTACH2;
379 } else {
380 /* Establish the interrupt handler. */
381 sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
382 wdcintr, &sc->wdc_channel);
383 if (sc->sc_ih == NULL) {
384 printf("%s: couldn't establish interrupt handler\n",
385 sc->sc_wdcdev.sc_dev.dv_xname);
386 return (EIO);
387 }
388
389 if (pcmcia_function_enable(sc->sc_pf)) {
390 printf("%s: couldn't enable PCMCIA function\n",
391 sc->sc_wdcdev.sc_dev.dv_xname);
392 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
393 return (EIO);
394 }
395 }
396 } else {
397 pcmcia_function_disable(sc->sc_pf);
398 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
399 sc->sc_ih = 0;
400 }
401
402 return (0);
403 }
404