wdc_pcmcia.c revision 1.80 1 /* $NetBSD: wdc_pcmcia.c,v 1.80 2004/08/10 16:04:16 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2003, 2004 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.80 2004/08/10 16:04:16 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 bus_size_t offset;
239 int i;
240 int error;
241
242 aprint_normal("\n");
243 sc->sc_pf = pa->pf;
244
245 /*XXXmem16|common*/
246 error = pcmcia_function_configure(pa->pf, wdc_pcmcia_validate_config);
247 if (error) {
248 aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
249 error);
250 return;
251 }
252
253 cfe = pa->pf->cfe;
254 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
255 if (cfe->iftype == PCMCIA_IFTYPE_MEMORY) {
256 sc->sc_pmemh.memt = cfe->memspace[0].handle.memt;
257 sc->sc_pmemh.memh = cfe->memspace[0].handle.memh;
258 offset = cfe->memspace[0].offset;
259
260 sc->sc_auxpmemh.memt = sc->sc_pmemh.memt;
261 if (bus_space_subregion(sc->sc_pmemh.memt,
262 sc->sc_pmemh.memh, WDC_PCMCIA_AUXREG_OFFSET + offset,
263 WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpmemh.memh))
264 goto fail;
265
266 aprint_normal("%s: memory mapped mode\n", self->dv_xname);
267 sc->wdc_channel.cmd_iot = sc->sc_pmemh.memt;
268 sc->wdc_channel.cmd_baseioh = sc->sc_pmemh.memh;
269 sc->wdc_channel.ctl_iot = sc->sc_auxpmemh.memt;
270 sc->wdc_channel.ctl_ioh = sc->sc_auxpmemh.memh;
271 } else {
272 sc->sc_pioh.iot = cfe->iospace[0].handle.iot;
273 sc->sc_pioh.ioh = cfe->iospace[0].handle.ioh;
274 offset = 0;
275
276 if (cfe->num_iospace == 1) {
277 sc->sc_auxpioh.iot = sc->sc_pioh.iot;
278 if (bus_space_subregion(sc->sc_pioh.iot,
279 sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET,
280 WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh))
281 goto fail;
282 } else {
283 sc->sc_auxpioh.iot = cfe->iospace[1].handle.iot;
284 sc->sc_auxpioh.ioh = cfe->iospace[1].handle.ioh;
285 }
286
287 aprint_normal("%s: i/o mapped mode\n", self->dv_xname);
288 sc->wdc_channel.cmd_iot = sc->sc_pioh.iot;
289 sc->wdc_channel.cmd_baseioh = sc->sc_pioh.ioh;
290 sc->wdc_channel.ctl_iot = sc->sc_auxpioh.iot;
291 sc->wdc_channel.ctl_ioh = sc->sc_auxpioh.ioh;
292 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32;
293 }
294
295 error = wdc_pcmcia_enable(self, 1);
296 if (error)
297 goto fail;
298
299 for (i = 0; i < WDC_PCMCIA_REG_NPORTS; i++) {
300 if (bus_space_subregion(sc->wdc_channel.cmd_iot,
301 sc->wdc_channel.cmd_baseioh,
302 offset + i, i == 0 ? 4 : 1,
303 &sc->wdc_channel.cmd_iohs[i]) != 0) {
304 aprint_error("%s: can't subregion I/O space\n",
305 self->dv_xname);
306 goto fail2;
307 }
308 }
309 wdc_init_shadow_regs(&sc->wdc_channel);
310 sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
311 sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_iohs[0];
312 sc->sc_wdcdev.PIO_cap = 0;
313 sc->wdc_chanlist[0] = &sc->wdc_channel;
314 sc->sc_wdcdev.channels = sc->wdc_chanlist;
315 sc->sc_wdcdev.nchannels = 1;
316 sc->wdc_channel.ch_channel = 0;
317 sc->wdc_channel.ch_wdc = &sc->sc_wdcdev;
318 sc->wdc_channel.ch_queue = &sc->wdc_chqueue;
319
320 /* We can enable and disable the controller. */
321 sc->sc_wdcdev.sc_atapi_adapter._generic.adapt_enable =
322 wdc_pcmcia_enable;
323
324 sc->sc_state = WDC_PCMCIA_ATTACH1;
325 wdcattach(&sc->wdc_channel);
326 if (sc->sc_state == WDC_PCMCIA_ATTACH1)
327 wdc_pcmcia_enable(self, 0);
328 sc->sc_state = WDC_PCMCIA_ATTACHED;
329 return;
330
331 fail2:
332 wdc_pcmcia_enable(self, 0);
333 fail:
334 pcmcia_function_unconfigure(pa->pf);
335 }
336
337 int
338 wdc_pcmcia_detach(self, flags)
339 struct device *self;
340 int flags;
341 {
342 struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
343 int error;
344
345 if (sc->sc_state != WDC_PCMCIA_ATTACHED)
346 return (0);
347
348 if ((error = wdcdetach(self, flags)) != 0)
349 return (error);
350
351 pcmcia_function_unconfigure(sc->sc_pf);
352
353 return (0);
354 }
355
356 int
357 wdc_pcmcia_enable(self, onoff)
358 struct device *self;
359 int onoff;
360 {
361 struct wdc_pcmcia_softc *sc = (void *)self;
362 int error;
363
364 if (onoff) {
365 /*
366 * If the WDC_PCMCIA_ATTACH flag is set, we've already
367 * enabled the card in the attach routine, so don't
368 * re-enable it here (to save power cycle time). Clear
369 * the flag, though, so that the next disable/enable
370 * will do the right thing.
371 */
372 if (sc->sc_state == WDC_PCMCIA_ATTACH1) {
373 sc->sc_state = WDC_PCMCIA_ATTACH2;
374 } else {
375 /* Establish the interrupt handler. */
376 sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
377 wdcintr, &sc->wdc_channel);
378 if (!sc->sc_ih)
379 return (EIO);
380
381 error = pcmcia_function_enable(sc->sc_pf);
382 if (error) {
383 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
384 sc->sc_ih = 0;
385 return (error);
386 }
387 }
388 } else {
389 pcmcia_function_disable(sc->sc_pf);
390 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
391 sc->sc_ih = 0;
392 }
393
394 return (0);
395 }
396