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