xref: /netbsd-src/sys/dev/pcmcia/wdc_pcmcia.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: wdc_pcmcia.c,v 1.102 2006/01/11 13:34:33 christos 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.102 2006/01/11 13:34:33 christos 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 pcmcia_product wdc_pcmcia_products[] = {
102 	{ PCMCIA_VENDOR_DIGITAL,
103 	  PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM,
104 	  {NULL, "Digital Mobile Media CD-ROM", NULL, NULL} },
105 
106 	{ PCMCIA_VENDOR_IBM,
107 	  PCMCIA_PRODUCT_IBM_PORTABLE_CDROM,
108 	  {NULL, "PCMCIA Portable CD-ROM Drive", NULL, NULL} },
109 
110 	/* The TEAC IDE/Card II is used on the Sony Vaio */
111 	{ PCMCIA_VENDOR_TEAC,
112 	  PCMCIA_PRODUCT_TEAC_IDECARDII,
113 	  PCMCIA_CIS_TEAC_IDECARDII },
114 
115 	/*
116 	 * A fujitsu rebranded panasonic drive that reports
117 	 * itself as function "scsi", disk interface 0
118 	 */
119 	{ PCMCIA_VENDOR_PANASONIC,
120 	  PCMCIA_PRODUCT_PANASONIC_KXLC005,
121 	  PCMCIA_CIS_PANASONIC_KXLC005 },
122 
123 	{ PCMCIA_VENDOR_SANDISK,
124 	  PCMCIA_PRODUCT_SANDISK_SDCFB,
125 	  PCMCIA_CIS_SANDISK_SDCFB },
126 
127 	/*
128 	 * EXP IDE/ATAPI DVD Card use with some DVD players.
129 	 * Does not have a vendor ID or product ID.
130 	 */
131 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
132 	  PCMCIA_CIS_EXP_EXPMULTIMEDIA },
133 
134 	/* Mobile Dock 2, neither vendor ID nor product ID */
135 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
136 	  {"SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", NULL, NULL} },
137 
138 	/* Toshiba Portege 3110 CD, neither vendor ID nor product ID */
139 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
140 	  {"FREECOM", "PCCARD-IDE", NULL, NULL} },
141 
142 	/* Random CD-ROM, (badged AMACOM), neither vendor ID nor product ID */
143 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
144 	  {"PCMCIA", "CD-ROM", NULL, NULL} },
145 
146 	/* IO DATA CBIDE2, with neither vendor ID nor product ID */
147 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
148 	  PCMCIA_CIS_IODATA_CBIDE2 },
149 
150 	/* TOSHIBA PA2673U(IODATA_CBIDE2 OEM), */
151 	/*  with neither vendor ID nor product ID */
152 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
153 	  PCMCIA_CIS_TOSHIBA_CBIDE2 },
154 
155 	/*
156 	 * Novac PCMCIA-IDE Card for HD530P IDE Box,
157 	 * with neither vendor ID nor product ID
158 	 */
159 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
160 	  {"PCMCIA", "PnPIDE", NULL, NULL} },
161 };
162 static const size_t wdc_pcmcia_nproducts =
163     sizeof(wdc_pcmcia_products) / sizeof(wdc_pcmcia_products[0]);
164 
165 static int	wdc_pcmcia_enable(struct device *, 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);
170 
171 static int
172 wdc_pcmcia_match(struct device *parent, struct cfdata *match, void *aux)
173 {
174 	struct pcmcia_attach_args *pa = aux;
175 
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);
183 }
184 
185 static int
186 wdc_pcmcia_validate_config_io(struct pcmcia_config_entry *cfe)
187 {
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);
193 }
194 
195 static int
196 wdc_pcmcia_validate_config_memory(struct pcmcia_config_entry *cfe)
197 {
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);
204 }
205 
206 static void
207 wdc_pcmcia_attach(struct device *parent, struct device *self, void *aux)
208 {
209 	struct wdc_pcmcia_softc *sc = (void *)self;
210 	struct pcmcia_attach_args *pa = aux;
211 	struct pcmcia_config_entry *cfe;
212 	struct wdc_regs *wdr;
213 	bus_size_t offset;
214 	int i;
215 	int error;
216 
217 	sc->sc_pf = pa->pf;
218 
219 	error = pcmcia_function_configure(pa->pf,
220 	    wdc_pcmcia_validate_config_io);
221 	if (error)
222 		/*XXXmem16|common*/
223 		error = pcmcia_function_configure(pa->pf,
224 		    wdc_pcmcia_validate_config_memory);
225 	if (error) {
226 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
227 		    error);
228 		return;
229 	}
230 
231 	cfe = pa->pf->cfe;
232 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
233 
234 	sc->sc_wdcdev.regs = wdr = &sc->wdc_regs;
235 
236 	if (cfe->iftype == PCMCIA_IFTYPE_MEMORY) {
237 		wdr->cmd_iot = cfe->memspace[0].handle.memt;
238 		wdr->cmd_baseioh = cfe->memspace[0].handle.memh;
239 		offset = cfe->memspace[0].offset;
240 		wdr->ctl_iot = cfe->memspace[0].handle.memt;
241 		if (bus_space_subregion(cfe->memspace[0].handle.memt,
242 		    cfe->memspace[0].handle.memh,
243 		    offset + WDC_PCMCIA_AUXREG_OFFSET, WDC_PCMCIA_AUXREG_NPORTS,
244 		    &wdr->ctl_ioh))
245 			goto fail;
246 	} else {
247 		wdr->cmd_iot = cfe->iospace[0].handle.iot;
248 		wdr->cmd_baseioh = cfe->iospace[0].handle.ioh;
249 		offset = 0;
250 		if (cfe->num_iospace == 1) {
251 			wdr->ctl_iot = cfe->iospace[0].handle.iot;
252 			if (bus_space_subregion(cfe->iospace[0].handle.iot,
253 			    cfe->iospace[0].handle.ioh,
254 			    WDC_PCMCIA_AUXREG_OFFSET, WDC_PCMCIA_AUXREG_NPORTS,
255 			    &wdr->ctl_ioh))
256 				goto fail;
257 		} else {
258 			wdr->ctl_iot = cfe->iospace[1].handle.iot;
259 			wdr->ctl_ioh = cfe->iospace[1].handle.ioh;
260 		}
261 	}
262 
263 	for (i = 0; i < WDC_PCMCIA_REG_NPORTS; i++) {
264 		if (bus_space_subregion(wdr->cmd_iot,
265 		    wdr->cmd_baseioh,
266 		    offset + i, i == 0 ? 4 : 1,
267 		    &wdr->cmd_iohs[i]) != 0) {
268 			aprint_error("%s: can't subregion I/O space\n",
269 			    self->dv_xname);
270 			goto fail;
271 		}
272 	}
273 
274 	if (cfe->iftype == PCMCIA_IFTYPE_MEMORY) {
275 		aprint_normal("%s: memory mapped mode\n", self->dv_xname);
276 		wdr->data32iot = cfe->memspace[0].handle.memt;
277 		if (bus_space_subregion(cfe->memspace[0].handle.memt,
278 		    cfe->memspace[0].handle.memh, offset + 1024, 1024,
279 		    &wdr->data32ioh))
280 			goto fail;
281 		sc->sc_wdcdev.datain_pio = wdc_pcmcia_datain_memory;
282 		sc->sc_wdcdev.dataout_pio = wdc_pcmcia_dataout_memory;
283 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_NOIRQ;
284 	} else {
285 		aprint_normal("%s: i/o mapped mode\n", self->dv_xname);
286 		wdr->data32iot = wdr->cmd_iot;
287 		wdr->data32ioh = wdr->cmd_iohs[wd_data];
288 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA32;
289 	}
290 
291 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
292 	sc->wdc_chanlist[0] = &sc->ata_channel;
293 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanlist;
294 	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
295 	sc->ata_channel.ch_channel = 0;
296 	sc->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
297 	sc->ata_channel.ch_queue = &sc->wdc_chqueue;
298 	wdc_init_shadow_regs(&sc->ata_channel);
299 
300 	error = wdc_pcmcia_enable(self, 1);
301 	if (error)
302 		goto fail;
303 
304 	/* We can enable and disable the controller. */
305 	sc->sc_wdcdev.sc_atac.atac_atapi_adapter._generic.adapt_enable =
306 	    wdc_pcmcia_enable;
307 	sc->sc_wdcdev.sc_atac.atac_atapi_adapter._generic.adapt_refcnt = 1;
308 
309 	/*
310 	 * Some devices needs some more delay after power up to stabilize
311 	 * and probe properly, so give them half a second.
312 	 * See PR 25659 for details.
313 	 */
314 	tsleep(wdc_pcmcia_attach, PWAIT, "wdcattach", hz / 2);
315 
316 	wdcattach(&sc->ata_channel);
317 	ata_delref(&sc->ata_channel);
318 	sc->sc_state = WDC_PCMCIA_ATTACHED;
319 	return;
320 
321 fail:
322 	pcmcia_function_unconfigure(pa->pf);
323 }
324 
325 static int
326 wdc_pcmcia_detach(struct device *self, int flags)
327 {
328 	struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
329 	int error;
330 
331 	if (sc->sc_state != WDC_PCMCIA_ATTACHED)
332 		return (0);
333 
334 	if ((error = wdcdetach(self, flags)) != 0)
335 		return (error);
336 
337 	pcmcia_function_unconfigure(sc->sc_pf);
338 
339 	return (0);
340 }
341 
342 static int
343 wdc_pcmcia_enable(struct device *self, int onoff)
344 {
345 	struct wdc_pcmcia_softc *sc = (void *)self;
346 	int error;
347 
348 	if (onoff) {
349 		/* Establish the interrupt handler. */
350 		sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
351 		    wdcintr, &sc->ata_channel);
352 		if (!sc->sc_ih)
353 			return (EIO);
354 
355 		error = pcmcia_function_enable(sc->sc_pf);
356 		if (error) {
357 			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
358 			sc->sc_ih = 0;
359 			return (error);
360 		}
361 	} else {
362 		pcmcia_function_disable(sc->sc_pf);
363 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
364 		sc->sc_ih = 0;
365 	}
366 
367 	return (0);
368 }
369 
370 static void
371 wdc_pcmcia_datain_memory(struct ata_channel *chp, int flags, void *buf,
372     size_t len)
373 {
374 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
375 
376 	while (len > 0) {
377 		size_t n;
378 
379 		n = min(len, 1024);
380 		if ((flags & DRIVE_CAP32) && (n & 3) == 0)
381 			bus_space_read_region_stream_4(wdr->data32iot,
382 			    wdr->data32ioh, 0, buf, n >> 2);
383 		else
384 			bus_space_read_region_stream_2(wdr->data32iot,
385 			    wdr->data32ioh, 0, buf, n >> 1);
386 		buf = (char *)buf + n;
387 		len -= n;
388 	}
389 }
390 
391 static void
392 wdc_pcmcia_dataout_memory(struct ata_channel *chp, int flags, void *buf,
393     size_t len)
394 {
395 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
396 
397 	while (len > 0) {
398 		size_t n;
399 
400 		n = min(len, 1024);
401 		if ((flags & DRIVE_CAP32) && (n & 3) == 0)
402 			bus_space_write_region_stream_4(wdr->data32iot,
403 			    wdr->data32ioh, 0, buf, n >> 2);
404 		else
405 			bus_space_write_region_stream_2(wdr->data32iot,
406 			    wdr->data32ioh, 0, buf, n >> 1);
407 		buf = (char *)buf + n;
408 		len -= n;
409 	}
410 }
411