xref: /netbsd-src/sys/dev/pcmcia/wdc_pcmcia.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: wdc_pcmcia.c,v 1.126 2017/10/20 07:06:08 jdolecek 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: wdc_pcmcia.c,v 1.126 2017/10/20 07:06:08 jdolecek Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/device.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/systm.h>
40 #include <sys/proc.h>
41 
42 #include <sys/bus.h>
43 #include <sys/intr.h>
44 
45 #include <dev/pcmcia/pcmciareg.h>
46 #include <dev/pcmcia/pcmciavar.h>
47 #include <dev/pcmcia/pcmciadevs.h>
48 
49 #include <dev/ic/wdcreg.h>
50 #include <dev/ata/atavar.h>
51 #include <dev/ic/wdcvar.h>
52 
53 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
54 #define	bus_space_write_multi_stream_2	bus_space_write_multi_2
55 #define	bus_space_write_multi_stream_4	bus_space_write_multi_4
56 #define	bus_space_read_multi_stream_2	bus_space_read_multi_2
57 #define	bus_space_read_multi_stream_4	bus_space_read_multi_4
58 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
59 
60 #define WDC_PCMCIA_REG_NPORTS      8
61 #define WDC_PCMCIA_AUXREG_OFFSET   (WDC_PCMCIA_REG_NPORTS + 6)
62 #define WDC_PCMCIA_AUXREG_NPORTS   2
63 
64 struct wdc_pcmcia_softc {
65 	struct wdc_softc sc_wdcdev;
66 	struct ata_channel *wdc_chanlist[1];
67 	struct ata_channel ata_channel;
68 	struct wdc_regs wdc_regs;
69 
70 	struct pcmcia_function *sc_pf;
71 	void *sc_ih;
72 
73 	int sc_state;
74 #define WDC_PCMCIA_ATTACHED	3
75 };
76 
77 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
78 #define bus_space_read_region_stream_2 bus_space_read_region_2
79 #define bus_space_read_region_stream_4 bus_space_read_region_4
80 #define bus_space_write_region_stream_2 bus_space_write_region_2
81 #define bus_space_write_region_stream_4 bus_space_write_region_4
82 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
83 
84 static int wdc_pcmcia_match(device_t, cfdata_t, void *);
85 static int wdc_pcmcia_validate_config_io(struct pcmcia_config_entry *);
86 static int wdc_pcmcia_validate_config_memory(struct pcmcia_config_entry *);
87 static void wdc_pcmcia_attach(device_t, device_t, void *);
88 static int wdc_pcmcia_detach(device_t, int);
89 
90 CFATTACH_DECL_NEW(wdc_pcmcia, sizeof(struct wdc_pcmcia_softc),
91     wdc_pcmcia_match, wdc_pcmcia_attach, wdc_pcmcia_detach, NULL);
92 
93 static const struct wdc_pcmcia_product {
94 	struct pcmcia_product wdc_product;
95 	int wdc_ndrive;
96 } wdc_pcmcia_products[] = {
97 	{ { PCMCIA_VENDOR_DIGITAL,
98 	  PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM,
99 	  {NULL, "Digital Mobile Media CD-ROM", NULL, NULL} }, 2 },
100 
101 	{ { PCMCIA_VENDOR_IBM,
102 	  PCMCIA_PRODUCT_IBM_PORTABLE_CDROM,
103 	  {NULL, "PCMCIA Portable CD-ROM Drive", NULL, NULL} }, 2 },
104 
105 	/* The TEAC IDE/Card II is used on the Sony Vaio */
106 	{ { PCMCIA_VENDOR_TEAC,
107 	  PCMCIA_PRODUCT_TEAC_IDECARDII,
108 	  PCMCIA_CIS_TEAC_IDECARDII }, 2 },
109 
110 	/*
111 	 * A fujitsu rebranded panasonic drive that reports
112 	 * itself as function "scsi", disk interface 0
113 	 */
114 	{ { PCMCIA_VENDOR_PANASONIC,
115 	  PCMCIA_PRODUCT_PANASONIC_KXLC005,
116 	  PCMCIA_CIS_PANASONIC_KXLC005 }, 2 },
117 
118 	{ { PCMCIA_VENDOR_SANDISK,
119 	  PCMCIA_PRODUCT_SANDISK_SDCFB,
120 	  PCMCIA_CIS_SANDISK_SDCFB }, 1 },
121 
122 	/*
123 	 * EXP IDE/ATAPI DVD Card use with some DVD players.
124 	 * Does not have a vendor ID or product ID.
125 	 */
126 	{ { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
127 	  PCMCIA_CIS_EXP_EXPMULTIMEDIA }, 2 },
128 
129 	/* Mobile Dock 2, neither vendor ID nor product ID */
130 	{ { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
131 	  {"SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", NULL, NULL} }, 2 },
132 
133 	/* Toshiba Portege 3110 CD, neither vendor ID nor product ID */
134 	{ { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
135 	  {"FREECOM", "PCCARD-IDE", NULL, NULL} }, 2 },
136 
137 	/* Random CD-ROM, (badged AMACOM), neither vendor ID nor product ID */
138 	{ { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
139 	  {"PCMCIA", "CD-ROM", NULL, NULL} }, 2 },
140 
141 	/* IO DATA CBIDE2, with neither vendor ID nor product ID */
142 	{ { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
143 	  PCMCIA_CIS_IODATA_CBIDE2 }, 2 },
144 
145 	/* TOSHIBA PA2673U(IODATA_CBIDE2 OEM), */
146 	/*  with neither vendor ID nor product ID */
147 	{ { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
148 	  PCMCIA_CIS_TOSHIBA_CBIDE2 }, 2 },
149 
150 	/*
151 	 * Novac PCMCIA-IDE Card for HD530P IDE Box,
152 	 * with neither vendor ID nor product ID
153 	 */
154 	{ { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
155 	  {"PCMCIA", "PnPIDE", NULL, NULL} }, 2 },
156 
157 	{ { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
158 	  {"PCMCIA", "IDE CARD", NULL, NULL} }, 2 },
159 
160 };
161 static const size_t wdc_pcmcia_nproducts =
162     sizeof(wdc_pcmcia_products) / sizeof(wdc_pcmcia_products[0]);
163 
164 static int	wdc_pcmcia_enable(device_t, int);
165 static void	wdc_pcmcia_datain_memory(struct ata_channel *, int, void *,
166 					 size_t);
167 static void	wdc_pcmcia_dataout_memory(struct ata_channel *, int, void *,
168 					  size_t);
169 
170 static int
171 wdc_pcmcia_match(device_t parent, cfdata_t match, void *aux)
172 {
173 	struct pcmcia_attach_args *pa = aux;
174 
175 	if (pa->pf->function == PCMCIA_FUNCTION_DISK &&
176 	    pa->pf->pf_funce_disk_interface == PCMCIA_TPLFE_DDI_PCCARD_ATA)
177 		return (1);
178 	if (pcmcia_product_lookup(pa, wdc_pcmcia_products, wdc_pcmcia_nproducts,
179 	    sizeof(wdc_pcmcia_products[0]), NULL))
180 		return (2);
181 	return (0);
182 }
183 
184 static int
185 wdc_pcmcia_validate_config_io(struct pcmcia_config_entry *cfe)
186 {
187 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
188 	    cfe->num_iospace < 1 || cfe->num_iospace > 2)
189 		return (EINVAL);
190 	cfe->num_memspace = 0;
191 	return (0);
192 }
193 
194 static int
195 wdc_pcmcia_validate_config_memory(struct pcmcia_config_entry *cfe)
196 {
197 	if (cfe->iftype != PCMCIA_IFTYPE_MEMORY ||
198 	    cfe->num_memspace > 1 ||
199 	    cfe->memspace[0].length < 2048)
200 		return (EINVAL);
201 	cfe->num_iospace = 0;
202 	return (0);
203 }
204 
205 static void
206 wdc_pcmcia_attach(device_t parent, device_t self, void *aux)
207 {
208 	struct wdc_pcmcia_softc *sc = device_private(self);
209 	struct pcmcia_attach_args *pa = aux;
210 	struct pcmcia_config_entry *cfe;
211 	struct wdc_regs *wdr;
212 	const struct wdc_pcmcia_product *wdcp;
213 	bus_size_t offset;
214 	int i;
215 	int error;
216 
217 	aprint_naive("\n");
218 
219 	sc->sc_wdcdev.sc_atac.atac_dev = self;
220 	sc->sc_pf = pa->pf;
221 
222 	error = pcmcia_function_configure(pa->pf,
223 	    wdc_pcmcia_validate_config_io);
224 	if (error)
225 		/*XXXmem16|common*/
226 		error = pcmcia_function_configure(pa->pf,
227 		    wdc_pcmcia_validate_config_memory);
228 	if (error) {
229 		aprint_error_dev(self, "configure failed, error=%d\n", error);
230 		return;
231 	}
232 
233 	cfe = pa->pf->cfe;
234 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
235 
236 	sc->sc_wdcdev.regs = wdr = &sc->wdc_regs;
237 
238 	if (cfe->iftype == PCMCIA_IFTYPE_MEMORY) {
239 		wdr->cmd_iot = cfe->memspace[0].handle.memt;
240 		wdr->cmd_baseioh = cfe->memspace[0].handle.memh;
241 		offset = cfe->memspace[0].offset;
242 		wdr->ctl_iot = cfe->memspace[0].handle.memt;
243 		if (bus_space_subregion(cfe->memspace[0].handle.memt,
244 		    cfe->memspace[0].handle.memh,
245 		    offset + WDC_PCMCIA_AUXREG_OFFSET, WDC_PCMCIA_AUXREG_NPORTS,
246 		    &wdr->ctl_ioh))
247 			goto fail;
248 	} else {
249 		wdr->cmd_iot = cfe->iospace[0].handle.iot;
250 		wdr->cmd_baseioh = cfe->iospace[0].handle.ioh;
251 		offset = 0;
252 		if (cfe->num_iospace == 1) {
253 			wdr->ctl_iot = cfe->iospace[0].handle.iot;
254 			if (bus_space_subregion(cfe->iospace[0].handle.iot,
255 			    cfe->iospace[0].handle.ioh,
256 			    WDC_PCMCIA_AUXREG_OFFSET, WDC_PCMCIA_AUXREG_NPORTS,
257 			    &wdr->ctl_ioh))
258 				goto fail;
259 		} else {
260 			wdr->ctl_iot = cfe->iospace[1].handle.iot;
261 			wdr->ctl_ioh = cfe->iospace[1].handle.ioh;
262 		}
263 	}
264 
265 	for (i = 0; i < WDC_PCMCIA_REG_NPORTS; i++) {
266 		if (bus_space_subregion(wdr->cmd_iot,
267 		    wdr->cmd_baseioh,
268 		    offset + i, i == 0 ? 4 : 1,
269 		    &wdr->cmd_iohs[i]) != 0) {
270 			aprint_error_dev(self, "can't subregion I/O space\n");
271 			goto fail;
272 		}
273 	}
274 
275 	if (cfe->iftype == PCMCIA_IFTYPE_MEMORY) {
276 		aprint_normal_dev(self, "memory mapped mode\n");
277 		wdr->data32iot = cfe->memspace[0].handle.memt;
278 		if (bus_space_subregion(cfe->memspace[0].handle.memt,
279 		    cfe->memspace[0].handle.memh, offset + 1024, 1024,
280 		    &wdr->data32ioh))
281 			goto fail;
282 		sc->sc_wdcdev.datain_pio = wdc_pcmcia_datain_memory;
283 		sc->sc_wdcdev.dataout_pio = wdc_pcmcia_dataout_memory;
284 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_NOIRQ;
285 	} else {
286 		aprint_normal_dev(self, "i/o mapped mode\n");
287 		wdr->data32iot = wdr->cmd_iot;
288 		wdr->data32ioh = wdr->cmd_iohs[wd_data];
289 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA32;
290 	}
291 
292 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
293 	sc->wdc_chanlist[0] = &sc->ata_channel;
294 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanlist;
295 	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
296 	sc->ata_channel.ch_channel = 0;
297 	sc->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
298 
299 	wdcp = pcmcia_product_lookup(pa, wdc_pcmcia_products,
300 	    wdc_pcmcia_nproducts, sizeof(wdc_pcmcia_products[0]), NULL);
301 	sc->sc_wdcdev.wdc_maxdrives = wdcp ? wdcp->wdc_ndrive : 2;
302 	wdc_init_shadow_regs(wdr);
303 
304 	error = wdc_pcmcia_enable(self, 1);
305 	if (error)
306 		goto fail;
307 
308 	/* We can enable and disable the controller. */
309 	sc->sc_wdcdev.sc_atac.atac_atapi_adapter._generic.adapt_enable =
310 	    wdc_pcmcia_enable;
311 	sc->sc_wdcdev.sc_atac.atac_atapi_adapter._generic.adapt_refcnt = 1;
312 
313 	/*
314 	 * Some devices needs some more delay after power up to stabilize
315 	 * and probe properly, so give them half a second.
316 	 * See PR 25659 for details.
317 	 */
318 	config_pending_incr(self);
319 	tsleep(wdc_pcmcia_attach, PWAIT, "wdcattach", hz / 2);
320 
321 	wdcattach(&sc->ata_channel);
322 
323 	if (!pmf_device_register(self, NULL, NULL))
324 		aprint_error_dev(self, "unable to establish power handler\n");
325 
326 	config_pending_decr(self);
327 	ata_delref(&sc->ata_channel);
328 	sc->sc_state = WDC_PCMCIA_ATTACHED;
329 	return;
330 
331 fail:
332 	pcmcia_function_unconfigure(pa->pf);
333 }
334 
335 static int
336 wdc_pcmcia_detach(device_t self, int flags)
337 {
338 	struct wdc_pcmcia_softc *sc = device_private(self);
339 	int error;
340 
341 	if (sc->sc_state != WDC_PCMCIA_ATTACHED)
342 		return (0);
343 
344 	pmf_device_deregister(self);
345 
346 	if ((error = wdcdetach(self, flags)) != 0)
347 		return (error);
348 
349 	pcmcia_function_unconfigure(sc->sc_pf);
350 
351 	return (0);
352 }
353 
354 static int
355 wdc_pcmcia_enable(device_t self, int onoff)
356 {
357 	struct wdc_pcmcia_softc *sc = device_private(self);
358 	int error;
359 
360 #if 1
361 	/*
362 	 * XXX temporary kludge: we need to allow enabling while (cold)
363 	 * for some hpc* ports which attach pcmcia devices too early.
364 	 * This is problematic because pcmcia code uses tsleep() in
365 	 * the attach code path, but it seems to work somehow.
366 	 */
367 	if (doing_shutdown)
368 		return (EIO);
369 #else
370 	if (cold || doing_shutdown)
371 		return (EIO);
372 #endif
373 
374 	if (onoff) {
375 		/* Establish the interrupt handler. */
376 		sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
377 		    wdcintr, &sc->ata_channel);
378 		if (!sc->sc_ih)
379 			return (EIO);
380 
381 		error = pcmcia_function_enable(sc->sc_pf);
382 		if (error) {
383 			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
384 			sc->sc_ih = 0;
385 			return (error);
386 		}
387 	} else {
388 		pcmcia_function_disable(sc->sc_pf);
389 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
390 		sc->sc_ih = 0;
391 	}
392 
393 	return (0);
394 }
395 
396 static void
397 wdc_pcmcia_datain_memory(struct ata_channel *chp, int flags, void *buf,
398     size_t len)
399 {
400 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
401 
402 	while (len > 0) {
403 		size_t n;
404 
405 		n = min(len, 1024);
406 		if ((flags & ATA_DRIVE_CAP32) && (n & 3) == 0)
407 			bus_space_read_region_stream_4(wdr->data32iot,
408 			    wdr->data32ioh, 0, buf, n >> 2);
409 		else
410 			bus_space_read_region_stream_2(wdr->data32iot,
411 			    wdr->data32ioh, 0, buf, n >> 1);
412 		buf = (char *)buf + n;
413 		len -= n;
414 	}
415 }
416 
417 static void
418 wdc_pcmcia_dataout_memory(struct ata_channel *chp, int flags, void *buf,
419     size_t len)
420 {
421 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
422 
423 	while (len > 0) {
424 		size_t n;
425 
426 		n = min(len, 1024);
427 		if ((flags & ATA_DRIVE_CAP32) && (n & 3) == 0)
428 			bus_space_write_region_stream_4(wdr->data32iot,
429 			    wdr->data32ioh, 0, buf, n >> 2);
430 		else
431 			bus_space_write_region_stream_2(wdr->data32iot,
432 			    wdr->data32ioh, 0, buf, n >> 1);
433 		buf = (char *)buf + n;
434 		len -= n;
435 	}
436 }
437