xref: /openbsd-src/sys/dev/pcmcia/wdc_pcmcia.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: wdc_pcmcia.c,v 1.31 2014/09/14 14:17:25 jsg Exp $	*/
2 /*	$NetBSD: wdc_pcmcia.c,v 1.19 1999/02/19 21:49:43 abs Exp $ */
3 
4 /*-
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Charles M. Hannum, by Onno van der Linden and by Manuel Bouyer.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/conf.h>
37 #include <sys/file.h>
38 #include <sys/stat.h>
39 #include <sys/ioctl.h>
40 #include <sys/buf.h>
41 #include <sys/uio.h>
42 #include <sys/malloc.h>
43 #include <sys/device.h>
44 #include <sys/disklabel.h>
45 #include <sys/disk.h>
46 #include <sys/syslog.h>
47 
48 #include <machine/cpu.h>
49 #include <machine/intr.h>
50 #include <machine/bus.h>
51 
52 #include <dev/pcmcia/pcmciareg.h>
53 #include <dev/pcmcia/pcmciavar.h>
54 #include <dev/pcmcia/pcmciadevs.h>
55 
56 #include <dev/ata/atavar.h>
57 #include <dev/ic/wdcvar.h>
58 
59 #define WDC_PCMCIA_REG_NPORTS      8
60 #define WDC_PCMCIA_AUXREG_OFFSET   (WDC_PCMCIA_REG_NPORTS + 6)
61 #define WDC_PCMCIA_AUXREG_NPORTS   2
62 
63 struct wdc_pcmcia_softc {
64 	struct wdc_softc sc_wdcdev;
65 	struct channel_softc *wdc_chanptr;
66 	struct channel_softc wdc_channel;
67 	struct pcmcia_io_handle sc_pioh;
68 	struct pcmcia_io_handle sc_auxpioh;
69 	int sc_iowindow;
70 	int sc_auxiowindow;
71 	void *sc_ih;
72 	struct pcmcia_function *sc_pf;
73 	int sc_flags;
74 #define WDC_PCMCIA_ATTACH       0x0001
75 };
76 
77 static int wdc_pcmcia_match(struct device *, void *, void *);
78 static void wdc_pcmcia_attach(struct device *, struct device *, void *);
79 int    wdc_pcmcia_detach(struct device *, int);
80 int    wdc_pcmcia_activate(struct device *, int);
81 
82 struct cfattach wdc_pcmcia_ca = {
83 	sizeof(struct wdc_pcmcia_softc), wdc_pcmcia_match, wdc_pcmcia_attach,
84 	wdc_pcmcia_detach, wdc_pcmcia_activate
85 };
86 
87 struct wdc_pcmcia_product {
88 	u_int16_t	wpp_vendor;	/* vendor ID */
89 	u_int16_t	wpp_product;	/* product ID */
90 	int		wpp_quirk_flag;	/* Quirk flags */
91 #define WDC_PCMCIA_FORCE_16BIT_IO	0x01 /* Don't use PCMCIA_WIDTH_AUTO */
92 #define WDC_PCMCIA_NO_EXTRA_RESETS	0x02 /* Only reset ctrl once */
93 	const char	*wpp_cis_info[4];	/* XXX necessary? */
94 } wdc_pcmcia_pr[] = {
95 
96 	{ /* PCMCIA_VENDOR_DIGITAL XXX */ 0x0100,
97 	  PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM,
98 	  0, { NULL, "Digital Mobile Media CD-ROM", NULL, NULL }, },
99 
100 	{ PCMCIA_VENDOR_IBM, PCMCIA_PRODUCT_IBM_PORTABLE_CDROM,
101 	  0, { NULL, "Portable CD-ROM Drive", NULL, NULL }, },
102 
103 	{ PCMCIA_VENDOR_HAGIWARASYSCOM, PCMCIA_PRODUCT_INVALID,	/* XXX */
104 	  WDC_PCMCIA_FORCE_16BIT_IO, { NULL, NULL, NULL, NULL }, },
105 
106 	/* The TEAC IDE/Card II is used on the Sony Vaio */
107 	{ PCMCIA_VENDOR_TEAC, PCMCIA_PRODUCT_TEAC_IDECARDII,
108 	  WDC_PCMCIA_NO_EXTRA_RESETS, PCMCIA_CIS_TEAC_IDECARDII },
109 
110 	/*
111 	 * EXP IDE/ATAPI DVD Card use with some DVD players.
112 	 * Does not have a vendor ID or product ID.
113 	 */
114 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
115 	  0, PCMCIA_CIS_EXP_EXPMULTIMEDIA },
116 
117 	/* Mobile Dock 2, which doesn't have vendor ID nor product ID */
118 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
119 	  0, PCMCIA_CIS_SHUTTLE_IDE_ATAPI },
120 
121 	/* Archos MiniCD */
122 	{ PCMCIA_VENDOR_ARCHOS, PCMCIA_PRODUCT_ARCHOS_ARC_ATAPI,
123 	  0, PCMCIA_CIS_ARCHOS_ARC_ATAPI },
124 };
125 
126 struct wdc_pcmcia_disk_device_interface_args {
127 	int ddi_type;		/* interface type */
128 	int ddi_reqfn;		/* function we are requesting iftype */
129 	int ddi_curfn;		/* function we are currently parsing in CIS */
130 };
131 
132 int	wdc_pcmcia_disk_device_interface_callback(struct pcmcia_tuple *,
133 	    void *);
134 int	wdc_pcmcia_disk_device_interface(struct pcmcia_function *);
135 struct wdc_pcmcia_product *
136 	wdc_pcmcia_lookup(struct pcmcia_attach_args *);
137 
138 int
139 wdc_pcmcia_disk_device_interface_callback(tuple, arg)
140 	struct pcmcia_tuple *tuple;
141 	void *arg;
142 {
143 	struct wdc_pcmcia_disk_device_interface_args *ddi = arg;
144 
145 	switch (tuple->code) {
146 	case PCMCIA_CISTPL_FUNCID:
147 		ddi->ddi_curfn++;
148 		break;
149 
150 	case PCMCIA_CISTPL_FUNCE:
151 		if (ddi->ddi_reqfn != ddi->ddi_curfn)
152 			break;
153 
154 		/* subcode (disk device interface), data (interface type) */
155 		if (tuple->length < 2)
156 			break;
157 
158 		/* check type */
159 		if (pcmcia_tuple_read_1(tuple, 0) !=
160 		    PCMCIA_TPLFE_TYPE_DISK_DEVICE_INTERFACE)
161 			break;
162 
163 		ddi->ddi_type = pcmcia_tuple_read_1(tuple, 1);
164 		return (1);
165 	}
166 	return (0);
167 }
168 
169 int
170 wdc_pcmcia_disk_device_interface(pf)
171 	struct pcmcia_function *pf;
172 {
173 	struct wdc_pcmcia_disk_device_interface_args ddi;
174 
175 	ddi.ddi_reqfn = pf->number;
176 	ddi.ddi_curfn = -1;
177 	if (pcmcia_scan_cis((struct device *)pf->sc,
178 	    wdc_pcmcia_disk_device_interface_callback, &ddi) > 0)
179 		return (ddi.ddi_type);
180 	else
181 		return (-1);
182 }
183 
184 struct wdc_pcmcia_product *
185 wdc_pcmcia_lookup(pa)
186 	struct pcmcia_attach_args *pa;
187 {
188 	struct wdc_pcmcia_product *wpp;
189 	int i, cis_match;
190 
191 	for (wpp = wdc_pcmcia_pr;
192 	    wpp < &wdc_pcmcia_pr[nitems(wdc_pcmcia_pr)];
193 	    wpp++)
194 		if ((wpp->wpp_vendor == PCMCIA_VENDOR_INVALID ||
195 		     pa->manufacturer == wpp->wpp_vendor) &&
196 		    (wpp->wpp_product == PCMCIA_PRODUCT_INVALID ||
197 		     pa->product == wpp->wpp_product)) {
198 			cis_match = 1;
199 			for (i = 0; i < 4; i++) {
200 				if (!(wpp->wpp_cis_info[i] == NULL ||
201 				      (pa->card->cis1_info[i] != NULL &&
202 				       strcmp(pa->card->cis1_info[i],
203 					      wpp->wpp_cis_info[i]) == 0)))
204 					cis_match = 0;
205 			}
206 			if (cis_match)
207 				return (wpp);
208 		}
209 
210 	return (NULL);
211 }
212 
213 static int
214 wdc_pcmcia_match(parent, match, aux)
215 	struct device *parent;
216 	void *match, *aux;
217 {
218 	struct pcmcia_attach_args *pa = aux;
219 	struct pcmcia_softc *sc;
220 	int iftype;
221 
222 	if (wdc_pcmcia_lookup(pa) != NULL)
223 		return (1);
224 
225 	if (pa->pf->function == PCMCIA_FUNCTION_DISK) {
226 		sc = pa->pf->sc;
227 
228 		pcmcia_chip_socket_enable(sc->pct, sc->pch);
229 		iftype = wdc_pcmcia_disk_device_interface(pa->pf);
230 		pcmcia_chip_socket_disable(sc->pct, sc->pch);
231 
232 		if (iftype == PCMCIA_TPLFE_DDI_PCCARD_ATA)
233 			return (1);
234 	}
235 
236 	return (0);
237 }
238 
239 static void
240 wdc_pcmcia_attach(parent, self, aux)
241 	struct device *parent;
242 	struct device *self;
243 	void *aux;
244 {
245 	struct wdc_pcmcia_softc *sc = (void *)self;
246 	struct pcmcia_attach_args *pa = aux;
247 	struct pcmcia_config_entry *cfe;
248 	struct wdc_pcmcia_product *wpp;
249 	const char *intrstr;
250 	int quirks;
251 
252 	sc->sc_pf = pa->pf;
253 
254 	for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
255 	    cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
256 		if (cfe->num_iospace != 1 && cfe->num_iospace != 2)
257 			continue;
258 
259 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
260 		    cfe->iospace[0].length,
261 		    cfe->iospace[0].start == 0 ? cfe->iospace[0].length : 0,
262 		    &sc->sc_pioh))
263 			continue;
264 
265 		if (cfe->num_iospace == 2) {
266 			if (!pcmcia_io_alloc(pa->pf, cfe->iospace[1].start,
267 			    cfe->iospace[1].length, 0, &sc->sc_auxpioh))
268 				break;
269 		} else /* num_iospace == 1 */ {
270 			sc->sc_auxpioh.iot = sc->sc_pioh.iot;
271 			if (!bus_space_subregion(sc->sc_pioh.iot,
272 			    sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET,
273 			    WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh))
274 				break;
275 		}
276 		pcmcia_io_free(pa->pf, &sc->sc_pioh);
277 	}
278 
279 	if (cfe == NULL) {
280 		printf(": can't handle card info\n");
281 		goto no_config_entry;
282 	}
283 
284 	/* Enable the card. */
285 	pcmcia_function_init(pa->pf, cfe);
286 	if (pcmcia_function_enable(pa->pf)) {
287 		printf(": function enable failed\n");
288 		goto enable_failed;
289 	}
290 
291 	/*
292 	 * XXX  DEC Mobile Media CDROM is not yet tested whether it works
293 	 * XXX  with PCMCIA_WIDTH_IO16.  HAGIWARA SYS-COM HPC-CF32 doesn't
294 	 * XXX  work with PCMCIA_WIDTH_AUTO.
295 	 * XXX  CANON FC-8M (SANDISK SDCFB 8M) works for both _AUTO and IO16.
296 	 * XXX  So, here is temporary work around.
297 	 */
298 	wpp = wdc_pcmcia_lookup(pa);
299 	if (wpp != NULL)
300 		quirks = wpp->wpp_quirk_flag;
301 	else
302 		quirks = 0;
303 
304 	if (pcmcia_io_map(pa->pf, quirks & WDC_PCMCIA_FORCE_16BIT_IO ?
305 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_AUTO, 0,
306 	    sc->sc_pioh.size, &sc->sc_pioh, &sc->sc_iowindow)) {
307 		printf(": can't map first i/o space\n");
308 		goto iomap_failed;
309 	}
310 
311 	/*
312 	 * Currently, # of iospace is 1 except DIGITAL Mobile Media CD-ROM.
313 	 * So whether the work around like above is necessary or not
314 	 * is unknown.  XXX.
315 	 */
316 	if (cfe->num_iospace <= 1)
317 		sc->sc_auxiowindow = -1;
318 	else if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
319 	    sc->sc_auxpioh.size, &sc->sc_auxpioh, &sc->sc_auxiowindow)) {
320 		printf(": can't map second i/o space\n");
321 		goto iomapaux_failed;
322 	}
323 
324 	printf(" port 0x%lx/%lu",
325 	    sc->sc_pioh.addr, (u_long)sc->sc_pioh.size);
326 	if (cfe->num_iospace > 1 && sc->sc_auxpioh.size > 0)
327 		printf(",0x%lx/%lu",
328 		    sc->sc_auxpioh.addr, (u_long)sc->sc_auxpioh.size);
329 
330 	sc->wdc_channel.cmd_iot = sc->sc_pioh.iot;
331 	sc->wdc_channel.cmd_ioh = sc->sc_pioh.ioh;
332 	sc->wdc_channel.ctl_iot = sc->sc_auxpioh.iot;
333 	sc->wdc_channel.ctl_ioh = sc->sc_auxpioh.ioh;
334 	sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
335 	sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_ioh;
336 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32;
337 	sc->sc_wdcdev.PIO_cap = 0;
338 	sc->wdc_chanptr = &sc->wdc_channel;
339 	sc->sc_wdcdev.channels = &sc->wdc_chanptr;
340 	sc->sc_wdcdev.nchannels = 1;
341 	sc->wdc_channel.channel = 0;
342 	sc->wdc_channel.wdc = &sc->sc_wdcdev;
343 	sc->wdc_channel.ch_queue = wdc_alloc_queue();
344 	if (sc->wdc_channel.ch_queue == NULL) {
345 		printf("cannot allocate channel queue\n");
346 		goto ch_queue_alloc_failed;
347 	}
348 	if (quirks & WDC_PCMCIA_NO_EXTRA_RESETS)
349 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
350 
351 	/* Establish the interrupt handler. */
352 	sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO, wdcintr,
353 	    &sc->wdc_channel, sc->sc_wdcdev.sc_dev.dv_xname);
354 	intrstr = pcmcia_intr_string(sc->sc_pf, sc->sc_ih);
355 	if (*intrstr)
356 		printf(": %s", intrstr);
357 
358 	printf("\n");
359 
360 	sc->sc_flags |= WDC_PCMCIA_ATTACH;
361 	wdcattach(&sc->wdc_channel);
362 	wdc_print_current_modes(&sc->wdc_channel);
363 	sc->sc_flags &= ~WDC_PCMCIA_ATTACH;
364 	return;
365 
366  ch_queue_alloc_failed:
367         /* Unmap our aux i/o window. */
368         if (sc->sc_auxiowindow != -1)
369                 pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
370 
371  iomapaux_failed:
372         /* Unmap our i/o window. */
373         pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
374 
375  iomap_failed:
376         /* Disable the function */
377         pcmcia_function_disable(sc->sc_pf);
378 
379  enable_failed:
380         /* Unmap our i/o space. */
381         pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
382         if (cfe->num_iospace == 2)
383                 pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
384 
385  no_config_entry:
386         sc->sc_iowindow = -1;
387 }
388 
389 int
390 wdc_pcmcia_detach(self, flags)
391 	struct device *self;
392 	int  flags;
393 {
394         struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
395         int error;
396 
397         if (sc->sc_iowindow == -1)
398                 /* Nothing to detach */
399                 return (0);
400 
401 	if ((error = wdcdetach(&sc->wdc_channel, flags)) != 0)
402 		return (error);
403 
404         if (sc->wdc_channel.ch_queue != NULL)
405                 wdc_free_queue(sc->wdc_channel.ch_queue);
406 
407         /* Unmap our i/o window and i/o space. */
408         pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
409         pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
410         if (sc->sc_auxiowindow != -1) {
411                 pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
412                 pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
413         }
414 
415         return (0);
416 }
417 
418 int
419 wdc_pcmcia_activate(self, act)
420 	struct device *self;
421 	int act;
422 {
423 	struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
424 	int rv = 0;
425 
426 	if (sc->sc_iowindow == -1)
427 		/* Nothing to activate/deactivate. */
428 		return (0);
429 
430 	switch (act) {
431 	case DVACT_DEACTIVATE:
432 		rv = config_activate_children(self, act);
433 		if (sc->sc_ih)
434 			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
435 		sc->sc_ih = NULL;
436 		pcmcia_function_disable(sc->sc_pf);
437 		break;
438 	case DVACT_RESUME:
439 		pcmcia_function_enable(sc->sc_pf);
440 		sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
441 		    wdcintr, &sc->wdc_channel, sc->sc_wdcdev.sc_dev.dv_xname);
442 		wdcreset(&sc->wdc_channel, VERBOSE);
443 		rv = config_activate_children(self, act);
444 		break;
445 	case DVACT_POWERDOWN:
446 		rv = config_activate_children(self, act);
447 		if (sc->sc_ih)
448 			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
449 		sc->sc_ih = NULL;
450 		pcmcia_function_disable(sc->sc_pf);
451 		break;
452 	default:
453 		rv = config_activate_children(self, act);
454 		break;
455 	}
456 	return (rv);
457 }
458