wdc_pcmcia.c revision 1.94 1 1.94 thorpej /* $NetBSD: wdc_pcmcia.c,v 1.94 2004/08/19 23:36:26 thorpej Exp $ */
2 1.1 matt
3 1.8 mycroft /*-
4 1.78 mycroft * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc.
5 1.8 mycroft * All rights reserved.
6 1.1 matt *
7 1.8 mycroft * This code is derived from software contributed to The NetBSD Foundation
8 1.8 mycroft * by Charles M. Hannum, by Onno van der Linden and by Manuel Bouyer.
9 1.1 matt *
10 1.1 matt * Redistribution and use in source and binary forms, with or without
11 1.1 matt * modification, are permitted provided that the following conditions
12 1.1 matt * are met:
13 1.1 matt * 1. Redistributions of source code must retain the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer.
15 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 matt * notice, this list of conditions and the following disclaimer in the
17 1.1 matt * documentation and/or other materials provided with the distribution.
18 1.1 matt * 3. All advertising materials mentioning features or use of this software
19 1.1 matt * must display the following acknowledgement:
20 1.8 mycroft * This product includes software developed by the NetBSD
21 1.8 mycroft * Foundation, Inc. and its contributors.
22 1.8 mycroft * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.8 mycroft * contributors may be used to endorse or promote products derived
24 1.8 mycroft * from this software without specific prior written permission.
25 1.1 matt *
26 1.8 mycroft * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.8 mycroft * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.8 mycroft * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.8 mycroft * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.8 mycroft * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.8 mycroft * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.8 mycroft * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.8 mycroft * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.8 mycroft * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.8 mycroft * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.8 mycroft * POSSIBILITY OF SUCH DAMAGE.
37 1.1 matt */
38 1.44 lukem
39 1.44 lukem #include <sys/cdefs.h>
40 1.94 thorpej __KERNEL_RCSID(0, "$NetBSD: wdc_pcmcia.c,v 1.94 2004/08/19 23:36:26 thorpej Exp $");
41 1.1 matt
42 1.1 matt #include <sys/param.h>
43 1.30 enami #include <sys/device.h>
44 1.30 enami #include <sys/malloc.h>
45 1.1 matt #include <sys/systm.h>
46 1.1 matt
47 1.30 enami #include <machine/bus.h>
48 1.1 matt #include <machine/intr.h>
49 1.1 matt
50 1.14 enami #include <dev/pcmcia/pcmciareg.h>
51 1.1 matt #include <dev/pcmcia/pcmciavar.h>
52 1.9 kenh #include <dev/pcmcia/pcmciadevs.h>
53 1.9 kenh
54 1.64 fvdl #include <dev/ic/wdcreg.h>
55 1.12 bouyer #include <dev/ata/atavar.h>
56 1.1 matt #include <dev/ic/wdcvar.h>
57 1.1 matt
58 1.1 matt #define WDC_PCMCIA_REG_NPORTS 8
59 1.9 kenh #define WDC_PCMCIA_AUXREG_OFFSET (WDC_PCMCIA_REG_NPORTS + 6)
60 1.2 mycroft #define WDC_PCMCIA_AUXREG_NPORTS 2
61 1.1 matt
62 1.1 matt struct wdc_pcmcia_softc {
63 1.1 matt struct wdc_softc sc_wdcdev;
64 1.91 thorpej struct ata_channel *wdc_chanlist[1];
65 1.91 thorpej struct ata_channel ata_channel;
66 1.65 thorpej struct ata_queue wdc_chqueue;
67 1.91 thorpej struct wdc_regs wdc_regs;
68 1.84 mycroft
69 1.84 mycroft struct pcmcia_function *sc_pf;
70 1.1 matt void *sc_ih;
71 1.75 mycroft
72 1.75 mycroft int sc_state;
73 1.75 mycroft #define WDC_PCMCIA_ATTACHED 3
74 1.1 matt };
75 1.1 matt
76 1.94 thorpej static int wdc_pcmcia_match(struct device *, struct cfdata *, void *);
77 1.94 thorpej static int wdc_pcmcia_validate_config_io(struct pcmcia_config_entry *);
78 1.94 thorpej static int wdc_pcmcia_validate_config_memory(struct pcmcia_config_entry *);
79 1.94 thorpej static void wdc_pcmcia_attach(struct device *, struct device *, void *);
80 1.94 thorpej static int wdc_pcmcia_detach(struct device *, int);
81 1.1 matt
82 1.51 thorpej CFATTACH_DECL(wdc_pcmcia, sizeof(struct wdc_pcmcia_softc),
83 1.52 thorpej wdc_pcmcia_match, wdc_pcmcia_attach, wdc_pcmcia_detach, wdcactivate);
84 1.1 matt
85 1.94 thorpej static const struct pcmcia_product wdc_pcmcia_products[] = {
86 1.74 mycroft { PCMCIA_VENDOR_DIGITAL,
87 1.9 kenh PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM,
88 1.73 mycroft {NULL, "Digital Mobile Media CD-ROM", NULL, NULL} },
89 1.19 abs
90 1.19 abs { PCMCIA_VENDOR_IBM,
91 1.24 soren PCMCIA_PRODUCT_IBM_PORTABLE_CDROM,
92 1.73 mycroft {NULL, "PCMCIA Portable CD-ROM Drive", NULL, NULL} },
93 1.9 kenh
94 1.9 kenh /* The TEAC IDE/Card II is used on the Sony Vaio */
95 1.9 kenh { PCMCIA_VENDOR_TEAC,
96 1.9 kenh PCMCIA_PRODUCT_TEAC_IDECARDII,
97 1.69 mycroft PCMCIA_CIS_TEAC_IDECARDII },
98 1.43 christos
99 1.43 christos /*
100 1.43 christos * A fujitsu rebranded panasonic drive that reports
101 1.43 christos * itself as function "scsi", disk interface 0
102 1.43 christos */
103 1.43 christos { PCMCIA_VENDOR_PANASONIC,
104 1.43 christos PCMCIA_PRODUCT_PANASONIC_KXLC005,
105 1.69 mycroft PCMCIA_CIS_PANASONIC_KXLC005 },
106 1.26 augustss
107 1.27 enami /*
108 1.27 enami * EXP IDE/ATAPI DVD Card use with some DVD players.
109 1.27 enami * Does not have a vendor ID or product ID.
110 1.27 enami */
111 1.81 mycroft { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
112 1.69 mycroft PCMCIA_CIS_EXP_EXPMULTIMEDIA },
113 1.23 takemura
114 1.36 abs /* Mobile Dock 2, neither vendor ID nor product ID */
115 1.81 mycroft { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
116 1.69 mycroft {"SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", NULL, NULL} },
117 1.34 abs
118 1.36 abs /* Toshiba Portege 3110 CD, neither vendor ID nor product ID */
119 1.81 mycroft { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
120 1.69 mycroft {"FREECOM", "PCCARD-IDE", NULL, NULL} },
121 1.36 abs
122 1.36 abs /* Random CD-ROM, (badged AMACOM), neither vendor ID nor product ID */
123 1.81 mycroft { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
124 1.69 mycroft {"PCMCIA", "CD-ROM", NULL, NULL} },
125 1.37 haya
126 1.37 haya /* IO DATA CBIDE2, with neither vendor ID nor product ID */
127 1.81 mycroft { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
128 1.69 mycroft PCMCIA_CIS_IODATA_CBIDE2 },
129 1.54 ichiro
130 1.54 ichiro /* TOSHIBA PA2673U(IODATA_CBIDE2 OEM), */
131 1.54 ichiro /* with neither vendor ID nor product ID */
132 1.81 mycroft { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
133 1.69 mycroft PCMCIA_CIS_TOSHIBA_CBIDE2 },
134 1.38 sato
135 1.38 sato /*
136 1.38 sato * Novac PCMCIA-IDE Card for HD530P IDE Box,
137 1.38 sato * with neither vendor ID nor product ID
138 1.38 sato */
139 1.81 mycroft { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
140 1.69 mycroft {"PCMCIA", "PnPIDE", NULL, NULL} },
141 1.9 kenh };
142 1.94 thorpej static const size_t wdc_pcmcia_nproducts =
143 1.81 mycroft sizeof(wdc_pcmcia_products) / sizeof(wdc_pcmcia_products[0]);
144 1.9 kenh
145 1.94 thorpej static int wdc_pcmcia_enable(struct device *, int);
146 1.94 thorpej static void wdc_pcmcia_datain_memory(struct ata_channel *, int, void *,
147 1.94 thorpej size_t);
148 1.94 thorpej static void wdc_pcmcia_dataout_memory(struct ata_channel *, int, void *,
149 1.94 thorpej size_t);
150 1.14 enami
151 1.1 matt static int
152 1.94 thorpej wdc_pcmcia_match(struct device *parent, struct cfdata *match, void *aux)
153 1.1 matt {
154 1.1 matt struct pcmcia_attach_args *pa = aux;
155 1.35 haya
156 1.35 haya if (pa->pf->function == PCMCIA_FUNCTION_DISK &&
157 1.81 mycroft pa->pf->pf_funce_disk_interface == PCMCIA_TPLFE_DDI_PCCARD_ATA)
158 1.9 kenh return (1);
159 1.81 mycroft if (pcmcia_product_lookup(pa, wdc_pcmcia_products, wdc_pcmcia_nproducts,
160 1.81 mycroft sizeof(wdc_pcmcia_products[0]), NULL))
161 1.81 mycroft return (2);
162 1.9 kenh return (0);
163 1.1 matt }
164 1.1 matt
165 1.72 mycroft static int
166 1.94 thorpej wdc_pcmcia_validate_config_io(struct pcmcia_config_entry *cfe)
167 1.72 mycroft {
168 1.87 mycroft if (cfe->iftype != PCMCIA_IFTYPE_IO ||
169 1.87 mycroft cfe->num_iospace < 1 || cfe->num_iospace > 2)
170 1.87 mycroft return (EINVAL);
171 1.87 mycroft cfe->num_memspace = 0;
172 1.87 mycroft return (0);
173 1.87 mycroft }
174 1.87 mycroft
175 1.87 mycroft static int
176 1.94 thorpej wdc_pcmcia_validate_config_memory(struct pcmcia_config_entry *cfe)
177 1.87 mycroft {
178 1.87 mycroft if (cfe->iftype != PCMCIA_IFTYPE_MEMORY ||
179 1.87 mycroft cfe->num_memspace > 1 ||
180 1.87 mycroft cfe->memspace[0].length < 2048)
181 1.72 mycroft return (EINVAL);
182 1.87 mycroft cfe->num_iospace = 0;
183 1.72 mycroft return (0);
184 1.72 mycroft }
185 1.72 mycroft
186 1.1 matt static void
187 1.94 thorpej wdc_pcmcia_attach(struct device *parent, struct device *self, void *aux)
188 1.1 matt {
189 1.1 matt struct wdc_pcmcia_softc *sc = (void *)self;
190 1.1 matt struct pcmcia_attach_args *pa = aux;
191 1.5 enami struct pcmcia_config_entry *cfe;
192 1.91 thorpej struct wdc_regs *wdr;
193 1.72 mycroft bus_size_t offset;
194 1.73 mycroft int i;
195 1.72 mycroft int error;
196 1.1 matt
197 1.15 thorpej sc->sc_pf = pa->pf;
198 1.15 thorpej
199 1.87 mycroft error = pcmcia_function_configure(pa->pf,
200 1.87 mycroft wdc_pcmcia_validate_config_io);
201 1.87 mycroft if (error)
202 1.87 mycroft /*XXXmem16|common*/
203 1.87 mycroft error = pcmcia_function_configure(pa->pf,
204 1.87 mycroft wdc_pcmcia_validate_config_memory);
205 1.72 mycroft if (error) {
206 1.72 mycroft aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
207 1.72 mycroft error);
208 1.72 mycroft return;
209 1.1 matt }
210 1.1 matt
211 1.72 mycroft cfe = pa->pf->cfe;
212 1.72 mycroft sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
213 1.41 uch
214 1.91 thorpej sc->sc_wdcdev.regs = wdr = &sc->wdc_regs;
215 1.91 thorpej
216 1.87 mycroft if (cfe->iftype == PCMCIA_IFTYPE_MEMORY) {
217 1.91 thorpej wdr->cmd_iot = cfe->memspace[0].handle.memt;
218 1.91 thorpej wdr->cmd_baseioh = cfe->memspace[0].handle.memh;
219 1.87 mycroft offset = cfe->memspace[0].offset;
220 1.91 thorpej wdr->ctl_iot = cfe->memspace[0].handle.memt;
221 1.87 mycroft if (bus_space_subregion(cfe->memspace[0].handle.memt,
222 1.87 mycroft cfe->memspace[0].handle.memh,
223 1.87 mycroft offset + WDC_PCMCIA_AUXREG_OFFSET, WDC_PCMCIA_AUXREG_NPORTS,
224 1.91 thorpej &wdr->ctl_ioh))
225 1.72 mycroft goto fail;
226 1.41 uch } else {
227 1.91 thorpej wdr->cmd_iot = cfe->iospace[0].handle.iot;
228 1.91 thorpej wdr->cmd_baseioh = cfe->iospace[0].handle.ioh;
229 1.87 mycroft offset = 0;
230 1.87 mycroft if (cfe->num_iospace == 1) {
231 1.91 thorpej wdr->ctl_iot = cfe->iospace[0].handle.iot;
232 1.87 mycroft if (bus_space_subregion(cfe->iospace[0].handle.iot,
233 1.87 mycroft cfe->iospace[0].handle.ioh,
234 1.87 mycroft WDC_PCMCIA_AUXREG_OFFSET, WDC_PCMCIA_AUXREG_NPORTS,
235 1.91 thorpej &wdr->ctl_ioh))
236 1.87 mycroft goto fail;
237 1.87 mycroft } else {
238 1.91 thorpej wdr->ctl_iot = cfe->iospace[1].handle.iot;
239 1.91 thorpej wdr->ctl_ioh = cfe->iospace[1].handle.ioh;
240 1.87 mycroft }
241 1.25 sommerfe }
242 1.5 enami
243 1.64 fvdl for (i = 0; i < WDC_PCMCIA_REG_NPORTS; i++) {
244 1.91 thorpej if (bus_space_subregion(wdr->cmd_iot,
245 1.91 thorpej wdr->cmd_baseioh,
246 1.64 fvdl offset + i, i == 0 ? 4 : 1,
247 1.91 thorpej &wdr->cmd_iohs[i]) != 0) {
248 1.69 mycroft aprint_error("%s: can't subregion I/O space\n",
249 1.69 mycroft self->dv_xname);
250 1.72 mycroft goto fail2;
251 1.64 fvdl }
252 1.64 fvdl }
253 1.85 mycroft
254 1.87 mycroft if (cfe->iftype == PCMCIA_IFTYPE_MEMORY) {
255 1.87 mycroft aprint_normal("%s: memory mapped mode\n", self->dv_xname);
256 1.91 thorpej wdr->data32iot = cfe->memspace[0].handle.memt;
257 1.85 mycroft if (bus_space_subregion(cfe->memspace[0].handle.memt,
258 1.87 mycroft cfe->memspace[0].handle.memh, offset + 1024, 1024,
259 1.91 thorpej &wdr->data32ioh))
260 1.85 mycroft goto fail;
261 1.87 mycroft sc->sc_wdcdev.datain_pio = wdc_pcmcia_datain_memory;
262 1.87 mycroft sc->sc_wdcdev.dataout_pio = wdc_pcmcia_dataout_memory;
263 1.87 mycroft sc->sc_wdcdev.cap |= WDC_CAPABILITY_NOIRQ;
264 1.85 mycroft } else {
265 1.87 mycroft aprint_normal("%s: i/o mapped mode\n", self->dv_xname);
266 1.91 thorpej wdr->data32iot = wdr->cmd_iot;
267 1.91 thorpej wdr->data32ioh = wdr->cmd_iohs[wd_data];
268 1.87 mycroft sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32;
269 1.85 mycroft }
270 1.85 mycroft
271 1.18 bouyer sc->sc_wdcdev.PIO_cap = 0;
272 1.91 thorpej sc->wdc_chanlist[0] = &sc->ata_channel;
273 1.53 matt sc->sc_wdcdev.channels = sc->wdc_chanlist;
274 1.12 bouyer sc->sc_wdcdev.nchannels = 1;
275 1.91 thorpej sc->ata_channel.ch_channel = 0;
276 1.91 thorpej sc->ata_channel.ch_wdc = &sc->sc_wdcdev;
277 1.91 thorpej sc->ata_channel.ch_queue = &sc->wdc_chqueue;
278 1.92 mycroft wdc_init_shadow_regs(&sc->ata_channel);
279 1.15 thorpej
280 1.90 mycroft error = wdc_pcmcia_enable(self, 1);
281 1.90 mycroft if (error)
282 1.90 mycroft goto fail;
283 1.90 mycroft
284 1.15 thorpej /* We can enable and disable the controller. */
285 1.42 bouyer sc->sc_wdcdev.sc_atapi_adapter._generic.adapt_enable =
286 1.32 bouyer wdc_pcmcia_enable;
287 1.83 mycroft sc->sc_wdcdev.sc_atapi_adapter._generic.adapt_refcnt = 1;
288 1.15 thorpej
289 1.91 thorpej wdcattach(&sc->ata_channel);
290 1.91 thorpej ata_delref(&sc->ata_channel);
291 1.75 mycroft sc->sc_state = WDC_PCMCIA_ATTACHED;
292 1.29 enami return;
293 1.29 enami
294 1.72 mycroft fail2:
295 1.72 mycroft wdc_pcmcia_enable(self, 0);
296 1.72 mycroft fail:
297 1.72 mycroft pcmcia_function_unconfigure(pa->pf);
298 1.21 enami }
299 1.21 enami
300 1.94 thorpej static int
301 1.94 thorpej wdc_pcmcia_detach(struct device *self, int flags)
302 1.21 enami {
303 1.21 enami struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
304 1.21 enami int error;
305 1.29 enami
306 1.75 mycroft if (sc->sc_state != WDC_PCMCIA_ATTACHED)
307 1.29 enami return (0);
308 1.21 enami
309 1.21 enami if ((error = wdcdetach(self, flags)) != 0)
310 1.21 enami return (error);
311 1.21 enami
312 1.72 mycroft pcmcia_function_unconfigure(sc->sc_pf);
313 1.61 briggs
314 1.21 enami return (0);
315 1.15 thorpej }
316 1.15 thorpej
317 1.94 thorpej static int
318 1.94 thorpej wdc_pcmcia_enable(struct device *self, int onoff)
319 1.15 thorpej {
320 1.42 bouyer struct wdc_pcmcia_softc *sc = (void *)self;
321 1.79 mycroft int error;
322 1.15 thorpej
323 1.15 thorpej if (onoff) {
324 1.83 mycroft /* Establish the interrupt handler. */
325 1.83 mycroft sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
326 1.91 thorpej wdcintr, &sc->ata_channel);
327 1.83 mycroft if (!sc->sc_ih)
328 1.83 mycroft return (EIO);
329 1.83 mycroft
330 1.83 mycroft error = pcmcia_function_enable(sc->sc_pf);
331 1.83 mycroft if (error) {
332 1.83 mycroft pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
333 1.83 mycroft sc->sc_ih = 0;
334 1.83 mycroft return (error);
335 1.15 thorpej }
336 1.15 thorpej } else {
337 1.15 thorpej pcmcia_function_disable(sc->sc_pf);
338 1.56 mycroft pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
339 1.72 mycroft sc->sc_ih = 0;
340 1.15 thorpej }
341 1.15 thorpej
342 1.15 thorpej return (0);
343 1.1 matt }
344 1.86 mycroft
345 1.94 thorpej static void
346 1.94 thorpej wdc_pcmcia_datain_memory(struct ata_channel *chp, int flags, void *buf,
347 1.94 thorpej size_t len)
348 1.86 mycroft {
349 1.93 thorpej struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
350 1.86 mycroft
351 1.86 mycroft while (len > 0) {
352 1.86 mycroft size_t n;
353 1.86 mycroft
354 1.86 mycroft n = min(len, 1024);
355 1.88 mycroft if ((flags & DRIVE_CAP32) && (n & 3) == 0)
356 1.91 thorpej bus_space_read_region_stream_4(wdr->data32iot,
357 1.91 thorpej wdr->data32ioh, 0, buf, n >> 2);
358 1.88 mycroft else
359 1.91 thorpej bus_space_read_region_stream_2(wdr->data32iot,
360 1.91 thorpej wdr->data32ioh, 0, buf, n >> 1);
361 1.86 mycroft buf = (char *)buf + n;
362 1.86 mycroft len -= n;
363 1.86 mycroft }
364 1.86 mycroft }
365 1.86 mycroft
366 1.94 thorpej static void
367 1.94 thorpej wdc_pcmcia_dataout_memory(struct ata_channel *chp, int flags, void *buf,
368 1.94 thorpej size_t len)
369 1.86 mycroft {
370 1.93 thorpej struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
371 1.86 mycroft
372 1.86 mycroft while (len > 0) {
373 1.86 mycroft size_t n;
374 1.86 mycroft
375 1.86 mycroft n = min(len, 1024);
376 1.88 mycroft if ((flags & DRIVE_CAP32) && (n & 3) == 0)
377 1.91 thorpej bus_space_write_region_stream_4(wdr->data32iot,
378 1.91 thorpej wdr->data32ioh, 0, buf, n >> 2);
379 1.88 mycroft else
380 1.91 thorpej bus_space_write_region_stream_2(wdr->data32iot,
381 1.91 thorpej wdr->data32ioh, 0, buf, n >> 1);
382 1.86 mycroft buf = (char *)buf + n;
383 1.86 mycroft len -= n;
384 1.86 mycroft }
385 1.86 mycroft }
386