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