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