xref: /netbsd-src/sys/dev/pcmcia/esp_pcmcia.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: esp_pcmcia.c,v 1.27 2005/12/11 12:23:23 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000, 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.
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: esp_pcmcia.c,v 1.27 2005/12/11 12:23:23 christos Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45 #include <sys/buf.h>
46 
47 #include <machine/bus.h>
48 #include <machine/intr.h>
49 
50 #include <dev/scsipi/scsi_all.h>
51 #include <dev/scsipi/scsipi_all.h>
52 #include <dev/scsipi/scsiconf.h>
53 
54 #include <dev/pcmcia/pcmciareg.h>
55 #include <dev/pcmcia/pcmciavar.h>
56 #include <dev/pcmcia/pcmciadevs.h>
57 
58 #include <dev/ic/ncr53c9xreg.h>
59 #include <dev/ic/ncr53c9xvar.h>
60 
61 struct esp_pcmcia_softc {
62 	struct ncr53c9x_softc	sc_ncr53c9x;	/* glue to MI code */
63 
64 	int		sc_active;		/* Pseudo-DMA state vars */
65 	int		sc_tc;
66 	int		sc_datain;
67 	size_t		sc_dmasize;
68 	size_t		sc_dmatrans;
69 	char		**sc_dmaaddr;
70 	size_t		*sc_pdmalen;
71 
72 	/* PCMCIA-specific goo. */
73 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
74 	void *sc_ih;				/* interrupt handler */
75 #ifdef ESP_PCMCIA_POLL
76 	struct callout sc_poll_ch;
77 #endif
78 	bus_space_tag_t	sc_iot;
79 	bus_space_handle_t sc_ioh;
80 
81 	int sc_state;
82 #define ESP_PCMCIA_ATTACHED	3
83 };
84 
85 int	esp_pcmcia_match(struct device *, struct cfdata *, void *);
86 int	esp_pcmcia_validate_config(struct pcmcia_config_entry *);
87 void	esp_pcmcia_attach(struct device *, struct device *, void *);
88 void	esp_pcmcia_init(struct esp_pcmcia_softc *);
89 int	esp_pcmcia_detach(struct device *, int);
90 int	esp_pcmcia_enable(struct device *, int);
91 
92 CFATTACH_DECL(esp_pcmcia, sizeof(struct esp_pcmcia_softc),
93     esp_pcmcia_match, esp_pcmcia_attach, esp_pcmcia_detach, NULL);
94 
95 /*
96  * Functions and the switch for the MI code.
97  */
98 #ifdef ESP_PCMCIA_POLL
99 void	esp_pcmcia_poll(void *);
100 #endif
101 u_char	esp_pcmcia_read_reg(struct ncr53c9x_softc *, int);
102 void	esp_pcmcia_write_reg(struct ncr53c9x_softc *, int, u_char);
103 int	esp_pcmcia_dma_isintr(struct ncr53c9x_softc *);
104 void	esp_pcmcia_dma_reset(struct ncr53c9x_softc *);
105 int	esp_pcmcia_dma_intr(struct ncr53c9x_softc *);
106 int	esp_pcmcia_dma_setup(struct ncr53c9x_softc *, caddr_t *,
107 	    size_t *, int, size_t *);
108 void	esp_pcmcia_dma_go(struct ncr53c9x_softc *);
109 void	esp_pcmcia_dma_stop(struct ncr53c9x_softc *);
110 int	esp_pcmcia_dma_isactive(struct ncr53c9x_softc *);
111 
112 const struct ncr53c9x_glue esp_pcmcia_glue = {
113 	esp_pcmcia_read_reg,
114 	esp_pcmcia_write_reg,
115 	esp_pcmcia_dma_isintr,
116 	esp_pcmcia_dma_reset,
117 	esp_pcmcia_dma_intr,
118 	esp_pcmcia_dma_setup,
119 	esp_pcmcia_dma_go,
120 	esp_pcmcia_dma_stop,
121 	esp_pcmcia_dma_isactive,
122 	NULL,			/* gl_clear_latched_intr */
123 };
124 
125 const struct pcmcia_product esp_pcmcia_products[] = {
126 	{ PCMCIA_VENDOR_PANASONIC, PCMCIA_PRODUCT_PANASONIC_KXLC002,
127 	  PCMCIA_CIS_PANASONIC_KXLC002 },
128 
129 	{ PCMCIA_VENDOR_RATOC, PCMCIA_PRODUCT_RATOC_REX_9530,
130 	  PCMCIA_CIS_RATOC_REX_9530 },
131 };
132 const size_t esp_pcmcia_nproducts =
133     sizeof(esp_pcmcia_products) / sizeof(esp_pcmcia_products[0]);
134 
135 int
136 esp_pcmcia_match(parent, match, aux)
137 	struct device *parent;
138 	struct cfdata *match;
139 	void *aux;
140 {
141 	struct pcmcia_attach_args *pa = aux;
142 
143 	if (pcmcia_product_lookup(pa, esp_pcmcia_products, esp_pcmcia_nproducts,
144 	    sizeof(esp_pcmcia_products[0]), NULL))
145 		return (1);
146 	return (0);
147 }
148 
149 int
150 esp_pcmcia_validate_config(cfe)
151 	struct pcmcia_config_entry *cfe;
152 {
153 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
154 	    cfe->num_memspace != 0 ||
155 	    cfe->num_iospace != 1)
156 		return (EINVAL);
157 	return (0);
158 }
159 
160 void
161 esp_pcmcia_attach(parent, self, aux)
162 	struct device *parent, *self;
163 	void *aux;
164 {
165 	struct esp_pcmcia_softc *esc = (void *)self;
166 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
167 	struct pcmcia_attach_args *pa = aux;
168 	struct pcmcia_config_entry *cfe;
169 	struct pcmcia_function *pf = pa->pf;
170 	int error;
171 
172 	esc->sc_pf = pf;
173 
174 	error = pcmcia_function_configure(pf, esp_pcmcia_validate_config);
175 	if (error) {
176 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
177 		    error);
178 		return;
179 	}
180 
181 	cfe = pf->cfe;
182 	esc->sc_iot = cfe->iospace[0].handle.iot;
183 	esc->sc_ioh = cfe->iospace[0].handle.ioh;
184 	esp_pcmcia_init(esc);
185 
186 	error = esp_pcmcia_enable(self, 1);
187 	if (error)
188 		goto fail;
189 
190 	sc->sc_adapter.adapt_minphys = minphys;
191 	sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
192 	sc->sc_adapter.adapt_enable = esp_pcmcia_enable;
193 	sc->sc_adapter.adapt_refcnt = 1;
194 
195 	ncr53c9x_attach(sc);
196 	scsipi_adapter_delref(&sc->sc_adapter);
197 	esc->sc_state = ESP_PCMCIA_ATTACHED;
198 	return;
199 
200 fail:
201 	pcmcia_function_unconfigure(pf);
202 }
203 
204 void
205 esp_pcmcia_init(esc)
206 	struct esp_pcmcia_softc *esc;
207 {
208 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
209 
210 	/* id 7, clock 40M, parity ON, sync OFF, fast ON, slow ON */
211 
212 	sc->sc_glue = &esp_pcmcia_glue;
213 
214 #ifdef ESP_PCMCIA_POLL
215 	callout_init(&esc->sc_poll_ch);
216 #endif
217 
218 	sc->sc_rev = NCR_VARIANT_ESP406;
219 	sc->sc_id = 7;
220 	sc->sc_freq = 40;
221 	/* try -PARENB -SLOW */
222 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB | NCRCFG1_SLOW;
223 	/* try +FE */
224 	sc->sc_cfg2 = NCRCFG2_SCSI2;
225 	/* try -IDM -FSCSI -FCLK */
226 	sc->sc_cfg3 = NCRESPCFG3_CDB | NCRESPCFG3_FCLK | NCRESPCFG3_IDM |
227 	    NCRESPCFG3_FSCSI;
228 	sc->sc_cfg4 = NCRCFG4_ACTNEG;
229 	/* try +INTP */
230 	sc->sc_cfg5 = NCRCFG5_CRS1 | NCRCFG5_AADDR | NCRCFG5_PTRINC;
231 	sc->sc_minsync = 0;
232 	sc->sc_maxxfer = 64 * 1024;
233 }
234 
235 int
236 esp_pcmcia_detach(self, flags)
237 	struct device *self;
238 	int flags;
239 {
240 	struct esp_pcmcia_softc *sc = (void *)self;
241 	int error;
242 
243 	if (sc->sc_state != ESP_PCMCIA_ATTACHED)
244 		return (0);
245 
246 	error = ncr53c9x_detach(&sc->sc_ncr53c9x, flags);
247 	if (error)
248 		return (error);
249 
250 	pcmcia_function_unconfigure(sc->sc_pf);
251 
252 	return (0);
253 }
254 
255 int
256 esp_pcmcia_enable(arg, onoff)
257 	struct device *arg;
258 	int onoff;
259 {
260 	struct esp_pcmcia_softc *sc = (void *)arg;
261 	int error;
262 
263 	if (onoff) {
264 #ifdef ESP_PCMCIA_POLL
265 		callout_reset(&sc->sc_poll_ch, 1, esp_pcmcia_poll, sc);
266 #else
267 		/* Establish the interrupt handler. */
268 		sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
269 		    ncr53c9x_intr, &sc->sc_ncr53c9x);
270 		if (!sc->sc_ih)
271 			return (EIO);
272 #endif
273 
274 		error = pcmcia_function_enable(sc->sc_pf);
275 		if (error) {
276 			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
277 			sc->sc_ih = 0;
278 			return (error);
279 		}
280 
281 		/* Initialize only chip.  */
282 		ncr53c9x_init(&sc->sc_ncr53c9x, 0);
283 	} else {
284 		pcmcia_function_disable(sc->sc_pf);
285 #ifdef ESP_PCMCIA_POLL
286 		callout_stop(&sc->sc_poll_ch);
287 #else
288 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
289 		sc->sc_ih = 0;
290 #endif
291 	}
292 
293 	return (0);
294 }
295 
296 #ifdef ESP_PCMCIA_POLL
297 void
298 esp_pcmcia_poll(arg)
299 	void *arg;
300 {
301 	struct esp_pcmcia_softc *esc = arg;
302 
303 	(void) ncr53c9x_intr(&esc->sc_ncr53c9x);
304 	callout_reset(&esc->sc_poll_ch, 1, esp_pcmcia_poll, esc);
305 }
306 #endif
307 
308 /*
309  * Glue functions.
310  */
311 u_char
312 esp_pcmcia_read_reg(sc, reg)
313 	struct ncr53c9x_softc *sc;
314 	int reg;
315 {
316 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
317 	u_char v;
318 
319 	v = bus_space_read_1(esc->sc_iot, esc->sc_ioh, reg);
320 	return v;
321 }
322 
323 void
324 esp_pcmcia_write_reg(sc, reg, val)
325 	struct ncr53c9x_softc *sc;
326 	int reg;
327 	u_char val;
328 {
329 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
330 	u_char v = val;
331 
332 	if (reg == NCR_CMD && v == (NCRCMD_TRANS|NCRCMD_DMA))
333 		v = NCRCMD_TRANS;
334 	bus_space_write_1(esc->sc_iot, esc->sc_ioh, reg, v);
335 }
336 
337 int
338 esp_pcmcia_dma_isintr(sc)
339 	struct ncr53c9x_softc *sc;
340 {
341 
342 	return NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT;
343 }
344 
345 void
346 esp_pcmcia_dma_reset(sc)
347 	struct ncr53c9x_softc *sc;
348 {
349 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
350 
351 	esc->sc_active = 0;
352 	esc->sc_tc = 0;
353 }
354 
355 int
356 esp_pcmcia_dma_intr(sc)
357 	struct ncr53c9x_softc *sc;
358 {
359 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
360 	u_char	*p;
361 	u_int	espphase, espstat, espintr;
362 	int	cnt;
363 
364 	if (esc->sc_active == 0) {
365 		printf("%s: dma_intr--inactive DMA\n", sc->sc_dev.dv_xname);
366 		return -1;
367 	}
368 
369 	if ((sc->sc_espintr & NCRINTR_BS) == 0) {
370 		esc->sc_active = 0;
371 		return 0;
372 	}
373 
374 	cnt = *esc->sc_pdmalen;
375 	if (*esc->sc_pdmalen == 0) {
376 		printf("%s: data interrupt, but no count left\n",
377 		    sc->sc_dev.dv_xname);
378 	}
379 
380 	p = *esc->sc_dmaaddr;
381 	espphase = sc->sc_phase;
382 	espstat = (u_int) sc->sc_espstat;
383 	espintr = (u_int) sc->sc_espintr;
384 	do {
385 		if (esc->sc_datain) {
386 			*p++ = NCR_READ_REG(sc, NCR_FIFO);
387 			cnt--;
388 			if (espphase == DATA_IN_PHASE)
389 				NCR_WRITE_REG(sc, NCR_CMD, NCRCMD_TRANS);
390 			else
391 				esc->sc_active = 0;
392 		} else {
393 			if (espphase == DATA_OUT_PHASE ||
394 			    espphase == MESSAGE_OUT_PHASE) {
395 				NCR_WRITE_REG(sc, NCR_FIFO, *p++);
396 				cnt--;
397 				NCR_WRITE_REG(sc, NCR_CMD, NCRCMD_TRANS);
398 			} else
399 				esc->sc_active = 0;
400 		}
401 
402 		if (esc->sc_active) {
403 			while (!(NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT));
404 			espstat = NCR_READ_REG(sc, NCR_STAT);
405 			espintr = NCR_READ_REG(sc, NCR_INTR);
406 			espphase = (espintr & NCRINTR_DIS)
407 				    ? /* Disconnected */ BUSFREE_PHASE
408 				    : espstat & PHASE_MASK;
409 		}
410 	} while (esc->sc_active && espintr);
411 	sc->sc_phase = espphase;
412 	sc->sc_espstat = (u_char) espstat;
413 	sc->sc_espintr = (u_char) espintr;
414 	*esc->sc_dmaaddr = p;
415 	*esc->sc_pdmalen = cnt;
416 
417 	if (*esc->sc_pdmalen == 0)
418 		esc->sc_tc = NCRSTAT_TC;
419 	sc->sc_espstat |= esc->sc_tc;
420 	return 0;
421 }
422 
423 int
424 esp_pcmcia_dma_setup(sc, addr, len, datain, dmasize)
425 	struct ncr53c9x_softc *sc;
426 	caddr_t *addr;
427 	size_t *len;
428 	int datain;
429 	size_t *dmasize;
430 {
431 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
432 
433 	esc->sc_dmaaddr = addr;
434 	esc->sc_pdmalen = len;
435 	esc->sc_datain = datain;
436 	esc->sc_dmasize = *dmasize;
437 	esc->sc_tc = 0;
438 
439 	return 0;
440 }
441 
442 void
443 esp_pcmcia_dma_go(sc)
444 	struct ncr53c9x_softc *sc;
445 {
446 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
447 
448 	esc->sc_active = 1;
449 }
450 
451 void
452 esp_pcmcia_dma_stop(sc)
453 	struct ncr53c9x_softc *sc;
454 {
455 }
456 
457 int
458 esp_pcmcia_dma_isactive(sc)
459 	struct ncr53c9x_softc *sc;
460 {
461 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
462 
463 	return (esc->sc_active);
464 }
465