Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / pcmcia / wdc_pcmcia.c
blob2ff7cae642639a921a534a853878b54049f2bda5
1 /* $NetBSD: wdc_pcmcia.c,v 1.116 2009/05/12 14:42:19 cegger Exp $ */
3 /*-
4 * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: wdc_pcmcia.c,v 1.116 2009/05/12 14:42:19 cegger Exp $");
35 #include <sys/param.h>
36 #include <sys/device.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/systm.h>
40 #include <sys/proc.h>
42 #include <sys/bus.h>
43 #include <sys/intr.h>
45 #include <dev/pcmcia/pcmciareg.h>
46 #include <dev/pcmcia/pcmciavar.h>
47 #include <dev/pcmcia/pcmciadevs.h>
49 #include <dev/ic/wdcreg.h>
50 #include <dev/ata/atavar.h>
51 #include <dev/ic/wdcvar.h>
53 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
54 #define bus_space_write_multi_stream_2 bus_space_write_multi_2
55 #define bus_space_write_multi_stream_4 bus_space_write_multi_4
56 #define bus_space_read_multi_stream_2 bus_space_read_multi_2
57 #define bus_space_read_multi_stream_4 bus_space_read_multi_4
58 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
60 #define WDC_PCMCIA_REG_NPORTS 8
61 #define WDC_PCMCIA_AUXREG_OFFSET (WDC_PCMCIA_REG_NPORTS + 6)
62 #define WDC_PCMCIA_AUXREG_NPORTS 2
64 struct wdc_pcmcia_softc {
65 struct wdc_softc sc_wdcdev;
66 struct ata_channel *wdc_chanlist[1];
67 struct ata_channel ata_channel;
68 struct ata_queue wdc_chqueue;
69 struct wdc_regs wdc_regs;
71 struct pcmcia_function *sc_pf;
72 void *sc_ih;
74 int sc_state;
75 #define WDC_PCMCIA_ATTACHED 3
78 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
79 #define bus_space_read_region_stream_2 bus_space_read_region_2
80 #define bus_space_read_region_stream_4 bus_space_read_region_4
81 #define bus_space_write_region_stream_2 bus_space_write_region_2
82 #define bus_space_write_region_stream_4 bus_space_write_region_4
83 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
85 static int wdc_pcmcia_match(device_t, cfdata_t, void *);
86 static int wdc_pcmcia_validate_config_io(struct pcmcia_config_entry *);
87 static int wdc_pcmcia_validate_config_memory(struct pcmcia_config_entry *);
88 static void wdc_pcmcia_attach(device_t, device_t, void *);
89 static int wdc_pcmcia_detach(device_t, int);
91 CFATTACH_DECL_NEW(wdc_pcmcia, sizeof(struct wdc_pcmcia_softc),
92 wdc_pcmcia_match, wdc_pcmcia_attach, wdc_pcmcia_detach, NULL);
94 static const struct wdc_pcmcia_product {
95 struct pcmcia_product wdc_product;
96 int wdc_ndrive;
97 } wdc_pcmcia_products[] = {
98 { { PCMCIA_VENDOR_DIGITAL,
99 PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM,
100 {NULL, "Digital Mobile Media CD-ROM", NULL, NULL} }, 2 },
102 { { PCMCIA_VENDOR_IBM,
103 PCMCIA_PRODUCT_IBM_PORTABLE_CDROM,
104 {NULL, "PCMCIA Portable CD-ROM Drive", NULL, NULL} }, 2 },
106 /* The TEAC IDE/Card II is used on the Sony Vaio */
107 { { PCMCIA_VENDOR_TEAC,
108 PCMCIA_PRODUCT_TEAC_IDECARDII,
109 PCMCIA_CIS_TEAC_IDECARDII }, 2 },
112 * A fujitsu rebranded panasonic drive that reports
113 * itself as function "scsi", disk interface 0
115 { { PCMCIA_VENDOR_PANASONIC,
116 PCMCIA_PRODUCT_PANASONIC_KXLC005,
117 PCMCIA_CIS_PANASONIC_KXLC005 }, 2 },
119 { { PCMCIA_VENDOR_SANDISK,
120 PCMCIA_PRODUCT_SANDISK_SDCFB,
121 PCMCIA_CIS_SANDISK_SDCFB }, 1 },
124 * EXP IDE/ATAPI DVD Card use with some DVD players.
125 * Does not have a vendor ID or product ID.
127 { { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
128 PCMCIA_CIS_EXP_EXPMULTIMEDIA }, 2 },
130 /* Mobile Dock 2, neither vendor ID nor product ID */
131 { { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
132 {"SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", NULL, NULL} }, 2 },
134 /* Toshiba Portege 3110 CD, neither vendor ID nor product ID */
135 { { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
136 {"FREECOM", "PCCARD-IDE", NULL, NULL} }, 2 },
138 /* Random CD-ROM, (badged AMACOM), neither vendor ID nor product ID */
139 { { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
140 {"PCMCIA", "CD-ROM", NULL, NULL} }, 2 },
142 /* IO DATA CBIDE2, with neither vendor ID nor product ID */
143 { { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
144 PCMCIA_CIS_IODATA_CBIDE2 }, 2 },
146 /* TOSHIBA PA2673U(IODATA_CBIDE2 OEM), */
147 /* with neither vendor ID nor product ID */
148 { { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
149 PCMCIA_CIS_TOSHIBA_CBIDE2 }, 2 },
152 * Novac PCMCIA-IDE Card for HD530P IDE Box,
153 * with neither vendor ID nor product ID
155 { { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
156 {"PCMCIA", "PnPIDE", NULL, NULL} }, 2 },
158 { { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
159 {"PCMCIA", "IDE CARD", NULL, NULL} }, 2 },
162 static const size_t wdc_pcmcia_nproducts =
163 sizeof(wdc_pcmcia_products) / sizeof(wdc_pcmcia_products[0]);
165 static int wdc_pcmcia_enable(device_t, int);
166 static void wdc_pcmcia_datain_memory(struct ata_channel *, int, void *,
167 size_t);
168 static void wdc_pcmcia_dataout_memory(struct ata_channel *, int, void *,
169 size_t);
171 static int
172 wdc_pcmcia_match(device_t parent, cfdata_t match, void *aux)
174 struct pcmcia_attach_args *pa = aux;
176 if (pa->pf->function == PCMCIA_FUNCTION_DISK &&
177 pa->pf->pf_funce_disk_interface == PCMCIA_TPLFE_DDI_PCCARD_ATA)
178 return (1);
179 if (pcmcia_product_lookup(pa, wdc_pcmcia_products, wdc_pcmcia_nproducts,
180 sizeof(wdc_pcmcia_products[0]), NULL))
181 return (2);
182 return (0);
185 static int
186 wdc_pcmcia_validate_config_io(struct pcmcia_config_entry *cfe)
188 if (cfe->iftype != PCMCIA_IFTYPE_IO ||
189 cfe->num_iospace < 1 || cfe->num_iospace > 2)
190 return (EINVAL);
191 cfe->num_memspace = 0;
192 return (0);
195 static int
196 wdc_pcmcia_validate_config_memory(struct pcmcia_config_entry *cfe)
198 if (cfe->iftype != PCMCIA_IFTYPE_MEMORY ||
199 cfe->num_memspace > 1 ||
200 cfe->memspace[0].length < 2048)
201 return (EINVAL);
202 cfe->num_iospace = 0;
203 return (0);
206 static void
207 wdc_pcmcia_attach(device_t parent, device_t self, void *aux)
209 struct wdc_pcmcia_softc *sc = device_private(self);
210 struct pcmcia_attach_args *pa = aux;
211 struct pcmcia_config_entry *cfe;
212 struct wdc_regs *wdr;
213 const struct wdc_pcmcia_product *wdcp;
214 bus_size_t offset;
215 int i;
216 int error;
218 aprint_naive("\n");
220 sc->sc_wdcdev.sc_atac.atac_dev = self;
221 sc->sc_pf = pa->pf;
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_dev(self, "configure failed, error=%d\n", error);
231 return;
234 cfe = pa->pf->cfe;
235 sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
237 sc->sc_wdcdev.regs = wdr = &sc->wdc_regs;
239 if (cfe->iftype == PCMCIA_IFTYPE_MEMORY) {
240 wdr->cmd_iot = cfe->memspace[0].handle.memt;
241 wdr->cmd_baseioh = cfe->memspace[0].handle.memh;
242 offset = cfe->memspace[0].offset;
243 wdr->ctl_iot = cfe->memspace[0].handle.memt;
244 if (bus_space_subregion(cfe->memspace[0].handle.memt,
245 cfe->memspace[0].handle.memh,
246 offset + WDC_PCMCIA_AUXREG_OFFSET, WDC_PCMCIA_AUXREG_NPORTS,
247 &wdr->ctl_ioh))
248 goto fail;
249 } else {
250 wdr->cmd_iot = cfe->iospace[0].handle.iot;
251 wdr->cmd_baseioh = cfe->iospace[0].handle.ioh;
252 offset = 0;
253 if (cfe->num_iospace == 1) {
254 wdr->ctl_iot = cfe->iospace[0].handle.iot;
255 if (bus_space_subregion(cfe->iospace[0].handle.iot,
256 cfe->iospace[0].handle.ioh,
257 WDC_PCMCIA_AUXREG_OFFSET, WDC_PCMCIA_AUXREG_NPORTS,
258 &wdr->ctl_ioh))
259 goto fail;
260 } else {
261 wdr->ctl_iot = cfe->iospace[1].handle.iot;
262 wdr->ctl_ioh = cfe->iospace[1].handle.ioh;
266 for (i = 0; i < WDC_PCMCIA_REG_NPORTS; i++) {
267 if (bus_space_subregion(wdr->cmd_iot,
268 wdr->cmd_baseioh,
269 offset + i, i == 0 ? 4 : 1,
270 &wdr->cmd_iohs[i]) != 0) {
271 aprint_error_dev(self, "can't subregion I/O space\n");
272 goto fail;
276 if (cfe->iftype == PCMCIA_IFTYPE_MEMORY) {
277 aprint_normal_dev(self, "memory mapped mode\n");
278 wdr->data32iot = cfe->memspace[0].handle.memt;
279 if (bus_space_subregion(cfe->memspace[0].handle.memt,
280 cfe->memspace[0].handle.memh, offset + 1024, 1024,
281 &wdr->data32ioh))
282 goto fail;
283 sc->sc_wdcdev.datain_pio = wdc_pcmcia_datain_memory;
284 sc->sc_wdcdev.dataout_pio = wdc_pcmcia_dataout_memory;
285 sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_NOIRQ;
286 } else {
287 aprint_normal_dev(self, "i/o mapped mode\n");
288 wdr->data32iot = wdr->cmd_iot;
289 wdr->data32ioh = wdr->cmd_iohs[wd_data];
290 sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA32;
293 sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
294 sc->wdc_chanlist[0] = &sc->ata_channel;
295 sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanlist;
296 sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
297 sc->ata_channel.ch_channel = 0;
298 sc->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
299 sc->ata_channel.ch_queue = &sc->wdc_chqueue;
300 wdcp = pcmcia_product_lookup(pa, wdc_pcmcia_products,
301 wdc_pcmcia_nproducts, sizeof(wdc_pcmcia_products[0]), NULL);
302 sc->ata_channel.ch_ndrive = wdcp ? wdcp->wdc_ndrive : 2;
303 wdc_init_shadow_regs(&sc->ata_channel);
305 error = wdc_pcmcia_enable(self, 1);
306 if (error)
307 goto fail;
309 /* We can enable and disable the controller. */
310 sc->sc_wdcdev.sc_atac.atac_atapi_adapter._generic.adapt_enable =
311 wdc_pcmcia_enable;
312 sc->sc_wdcdev.sc_atac.atac_atapi_adapter._generic.adapt_refcnt = 1;
315 * Some devices needs some more delay after power up to stabilize
316 * and probe properly, so give them half a second.
317 * See PR 25659 for details.
319 config_pending_incr();
320 tsleep(wdc_pcmcia_attach, PWAIT, "wdcattach", hz / 2);
322 wdcattach(&sc->ata_channel);
324 if (!pmf_device_register(self, NULL, NULL))
325 aprint_error_dev(self, "unable to establish power handler\n");
327 config_pending_decr();
328 ata_delref(&sc->ata_channel);
329 sc->sc_state = WDC_PCMCIA_ATTACHED;
330 return;
332 fail:
333 pcmcia_function_unconfigure(pa->pf);
336 static int
337 wdc_pcmcia_detach(device_t self, int flags)
339 struct wdc_pcmcia_softc *sc = device_private(self);
340 int error;
342 if (sc->sc_state != WDC_PCMCIA_ATTACHED)
343 return (0);
345 pmf_device_deregister(self);
347 if ((error = wdcdetach(self, flags)) != 0)
348 return (error);
350 pcmcia_function_unconfigure(sc->sc_pf);
352 return (0);
355 static int
356 wdc_pcmcia_enable(device_t self, int onoff)
358 struct wdc_pcmcia_softc *sc = device_private(self);
359 int error;
361 #if 1
363 * XXX temporary kludge: we need to allow enabling while (cold)
364 * for some hpc* ports which attach pcmcia devices too early.
365 * This is problematic because pcmcia code uses tsleep() in
366 * the attach code path, but it seems to work somehow.
368 if (doing_shutdown)
369 return (EIO);
370 #else
371 if (cold || doing_shutdown)
372 return (EIO);
373 #endif
375 if (onoff) {
376 /* Establish the interrupt handler. */
377 sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
378 wdcintr, &sc->ata_channel);
379 if (!sc->sc_ih)
380 return (EIO);
382 error = pcmcia_function_enable(sc->sc_pf);
383 if (error) {
384 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
385 sc->sc_ih = 0;
386 return (error);
388 } else {
389 pcmcia_function_disable(sc->sc_pf);
390 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
391 sc->sc_ih = 0;
394 return (0);
397 static void
398 wdc_pcmcia_datain_memory(struct ata_channel *chp, int flags, void *buf,
399 size_t len)
401 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
403 while (len > 0) {
404 size_t n;
406 n = min(len, 1024);
407 if ((flags & DRIVE_CAP32) && (n & 3) == 0)
408 bus_space_read_region_stream_4(wdr->data32iot,
409 wdr->data32ioh, 0, buf, n >> 2);
410 else
411 bus_space_read_region_stream_2(wdr->data32iot,
412 wdr->data32ioh, 0, buf, n >> 1);
413 buf = (char *)buf + n;
414 len -= n;
418 static void
419 wdc_pcmcia_dataout_memory(struct ata_channel *chp, int flags, void *buf,
420 size_t len)
422 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
424 while (len > 0) {
425 size_t n;
427 n = min(len, 1024);
428 if ((flags & DRIVE_CAP32) && (n & 3) == 0)
429 bus_space_write_region_stream_4(wdr->data32iot,
430 wdr->data32ioh, 0, buf, n >> 2);
431 else
432 bus_space_write_region_stream_2(wdr->data32iot,
433 wdr->data32ioh, 0, buf, n >> 1);
434 buf = (char *)buf + n;
435 len -= n;