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