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