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