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