xref: /netbsd/sys/dev/pcmcia/esp_pcmcia.c (revision bf9ec67e)
1 /*	$NetBSD: esp_pcmcia.c,v 1.10 2001/11/13 07:26:32 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 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.10 2001/11/13 07:26:32 lukem 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_io_handle sc_pcioh;	/* PCMCIA i/o space info */
74 	int sc_io_window;			/* our i/o window */
75 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
76 	void *sc_ih;				/* interrupt handler */
77 #ifdef ESP_PCMCIA_POLL
78 	struct callout sc_poll_ch;
79 #endif
80 	int sc_flags;
81 #define	ESP_PCMCIA_ATTACHED	1		/* attach completed */
82 #define ESP_PCMCIA_ATTACHING	2		/* attach in progress */
83 };
84 
85 int	esp_pcmcia_match __P((struct device *, struct cfdata *, void *));
86 void	esp_pcmcia_attach __P((struct device *, struct device *, void *));
87 void	esp_pcmcia_init __P((struct esp_pcmcia_softc *));
88 int	esp_pcmcia_detach __P((struct device *, int));
89 int	esp_pcmcia_enable __P((void *, int));
90 
91 struct cfattach esp_pcmcia_ca = {
92 	sizeof(struct esp_pcmcia_softc), esp_pcmcia_match, esp_pcmcia_attach,
93 	esp_pcmcia_detach
94 };
95 
96 /*
97  * Functions and the switch for the MI code.
98  */
99 #ifdef ESP_PCMCIA_POLL
100 void	esp_pcmcia_poll __P((void *));
101 #endif
102 u_char	esp_pcmcia_read_reg __P((struct ncr53c9x_softc *, int));
103 void	esp_pcmcia_write_reg __P((struct ncr53c9x_softc *, int, u_char));
104 int	esp_pcmcia_dma_isintr __P((struct ncr53c9x_softc *));
105 void	esp_pcmcia_dma_reset __P((struct ncr53c9x_softc *));
106 int	esp_pcmcia_dma_intr __P((struct ncr53c9x_softc *));
107 int	esp_pcmcia_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
108 	    size_t *, int, size_t *));
109 void	esp_pcmcia_dma_go __P((struct ncr53c9x_softc *));
110 void	esp_pcmcia_dma_stop __P((struct ncr53c9x_softc *));
111 int	esp_pcmcia_dma_isactive __P((struct ncr53c9x_softc *));
112 
113 struct ncr53c9x_glue esp_pcmcia_glue = {
114 	esp_pcmcia_read_reg,
115 	esp_pcmcia_write_reg,
116 	esp_pcmcia_dma_isintr,
117 	esp_pcmcia_dma_reset,
118 	esp_pcmcia_dma_intr,
119 	esp_pcmcia_dma_setup,
120 	esp_pcmcia_dma_go,
121 	esp_pcmcia_dma_stop,
122 	esp_pcmcia_dma_isactive,
123 	NULL,			/* gl_clear_latched_intr */
124 };
125 
126 const struct pcmcia_product esp_pcmcia_products[] = {
127 	{ PCMCIA_STR_PANASONIC_KXLC002,		PCMCIA_VENDOR_PANASONIC,
128 	  PCMCIA_PRODUCT_PANASONIC_KXLC002,	0 },
129 
130 	{ NULL }
131 };
132 
133 int
134 esp_pcmcia_match(parent, match, aux)
135 	struct device *parent;
136 	struct cfdata *match;
137 	void *aux;
138 {
139 	struct pcmcia_attach_args *pa = aux;
140 
141 	if (pcmcia_product_lookup(pa, esp_pcmcia_products,
142 	    sizeof esp_pcmcia_products[0], NULL) != NULL)
143 		return (1);
144 	return (0);
145 }
146 
147 void
148 esp_pcmcia_attach(parent, self, aux)
149 	struct device *parent, *self;
150 	void *aux;
151 {
152 	struct esp_pcmcia_softc *esc = (void *)self;
153 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
154 	struct pcmcia_attach_args *pa = aux;
155 	struct pcmcia_config_entry *cfe;
156 	struct pcmcia_function *pf = pa->pf;
157 	const struct pcmcia_product *pp;
158 
159 	esc->sc_pf = pf;
160 
161 	for (cfe = SIMPLEQ_FIRST(&pf->cfe_head); cfe != NULL;
162 	    cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
163 		if (cfe->num_memspace != 0 ||
164 		    cfe->num_iospace != 1)
165 			continue;
166 
167 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
168 		    cfe->iospace[0].length, 0, &esc->sc_pcioh) == 0)
169 			break;
170 	}
171 
172 	if (cfe == 0) {
173 		printf(": can't alloc i/o space\n");
174 		goto no_config_entry;
175 	}
176 
177 	/* Enable the card. */
178 	pcmcia_function_init(pf, cfe);
179 	if (pcmcia_function_enable(pf)) {
180 		printf(": function enable failed\n");
181 		goto enable_failed;
182 	}
183 
184 	/* Map in the I/O space */
185 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0, esc->sc_pcioh.size,
186 	    &esc->sc_pcioh, &esc->sc_io_window)) {
187 		printf(": can't map i/o space\n");
188 		goto iomap_failed;
189 	}
190 
191 	pp = pcmcia_product_lookup(pa, esp_pcmcia_products,
192 	    sizeof esp_pcmcia_products[0], NULL);
193 	if (pp == NULL) {
194 		printf("\n");
195 		panic("esp_pcmcia_attach: impossible");
196 	}
197 
198 	printf(": %s\n", pp->pp_name);
199 
200 	esp_pcmcia_init(esc);
201 
202 	/*
203 	 *  Initialize nca board itself.
204 	 */
205 	esc->sc_flags |= ESP_PCMCIA_ATTACHING;
206 	sc->sc_adapter.adapt_minphys = minphys;
207 	sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
208 	ncr53c9x_attach(sc);
209 	esc->sc_flags &= ~ESP_PCMCIA_ATTACHING;
210 	esc->sc_flags |= ESP_PCMCIA_ATTACHED;
211 	return;
212 
213 iomap_failed:
214 	/* Disable the device. */
215 	pcmcia_function_disable(esc->sc_pf);
216 
217 enable_failed:
218 	/* Unmap our I/O space. */
219 	pcmcia_io_free(esc->sc_pf, &esc->sc_pcioh);
220 
221 no_config_entry:
222 	return;
223 }
224 
225 void
226 esp_pcmcia_init(esc)
227 	struct esp_pcmcia_softc *esc;
228 {
229 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
230 	bus_space_tag_t iot = esc->sc_pcioh.iot;
231 	bus_space_handle_t ioh = esc->sc_pcioh.ioh;
232 
233 	/* id 7, clock 40M, parity ON, sync OFF, fast ON, slow ON */
234 
235 	sc->sc_glue = &esp_pcmcia_glue;
236 
237 #ifdef ESP_PCMCIA_POLL
238 	callout_init(&esc->sc_poll_ch);
239 #endif
240 
241 	sc->sc_rev = NCR_VARIANT_ESP406;
242 	sc->sc_id = 7;
243 	sc->sc_freq = 40;
244 	/* try -PARENB -SLOW */
245 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB | NCRCFG1_SLOW;
246 	/* try +FE */
247 	sc->sc_cfg2 = NCRCFG2_SCSI2;
248 	/* try -IDM -FSCSI -FCLK */
249 	sc->sc_cfg3 = NCRESPCFG3_CDB | NCRESPCFG3_FCLK | NCRESPCFG3_IDM |
250 	    NCRESPCFG3_FSCSI;
251 	sc->sc_cfg4 = NCRCFG4_ACTNEG;
252 	/* try +INTP */
253 	sc->sc_cfg5 = NCRCFG5_CRS1 | NCRCFG5_AADDR | NCRCFG5_PTRINC;
254 	sc->sc_minsync = 0;
255 	sc->sc_maxxfer = 64 * 1024;
256 
257 	bus_space_write_1(iot, ioh, NCR_CFG5, sc->sc_cfg5);
258 
259 	bus_space_write_1(iot, ioh, NCR_PIOI, 0);
260 	bus_space_write_1(iot, ioh, NCR_PSTAT, 0);
261 	bus_space_write_1(iot, ioh, 0x09, 0x24);
262 
263 	bus_space_write_1(iot, ioh, NCR_CFG4, sc->sc_cfg4);
264 }
265 
266 int
267 esp_pcmcia_detach(self, flags)
268 	struct device *self;
269 	int flags;
270 {
271 	struct esp_pcmcia_softc *esc = (void *)self;
272 	int error;
273 
274 	if ((esc->sc_flags & ESP_PCMCIA_ATTACHED) == 0) {
275 		/* Nothing to detach. */
276 		return (0);
277 	}
278 
279 	error = ncr53c9x_detach(&esc->sc_ncr53c9x, flags);
280 	if (error)
281 		return (error);
282 
283 	/* Unmap our i/o window and i/o space. */
284 	pcmcia_io_unmap(esc->sc_pf, esc->sc_io_window);
285 	pcmcia_io_free(esc->sc_pf, &esc->sc_pcioh);
286 
287 	return (0);
288 }
289 
290 int
291 esp_pcmcia_enable(arg, onoff)
292 	void *arg;
293 	int onoff;
294 {
295 	struct esp_pcmcia_softc *esc = arg;
296 
297 	if (onoff) {
298 #ifdef ESP_PCMCIA_POLL
299 		callout_reset(&esc->sc_poll_ch, 1, esp_pcmcia_poll, esc);
300 #else
301 		/* Establish the interrupt handler. */
302 		esc->sc_ih = pcmcia_intr_establish(esc->sc_pf, IPL_BIO,
303 		    ncr53c9x_intr, &esc->sc_ncr53c9x);
304 		if (esc->sc_ih == NULL) {
305 			printf("%s: couldn't establish interrupt handler\n",
306 			    esc->sc_ncr53c9x.sc_dev.dv_xname);
307 			return (EIO);
308 		}
309 #endif
310 
311 		/*
312 		 * If attach is in progress, we know that card power is
313 		 * enabled and chip will be initialized later.
314 		 * Otherwise, enable and reset now.
315 		 */
316 		if ((esc->sc_flags & ESP_PCMCIA_ATTACHING) == 0) {
317 			if (pcmcia_function_enable(esc->sc_pf)) {
318 				printf("%s: couldn't enable PCMCIA function\n",
319 				    esc->sc_ncr53c9x.sc_dev.dv_xname);
320 				pcmcia_intr_disestablish(esc->sc_pf,
321 				    esc->sc_ih);
322 				return (EIO);
323 			}
324 
325 			/* Initialize only chip.  */
326 			ncr53c9x_init(&esc->sc_ncr53c9x, 0);
327 		}
328 	} else {
329 		pcmcia_function_disable(esc->sc_pf);
330 #ifdef ESP_PCMCIA_POLL
331 		callout_stop(&esc->sc_poll_ch);
332 #else
333 		pcmcia_intr_disestablish(esc->sc_pf, esc->sc_ih);
334 #endif
335 	}
336 
337 	return (0);
338 }
339 
340 #ifdef ESP_PCMCIA_POLL
341 void
342 esp_pcmcia_poll(arg)
343 	void *arg;
344 {
345 	struct esp_pcmcia_softc *esc = arg;
346 
347 	(void) ncr53c9x_intr(&esc->sc_ncr53c9x);
348 	callout_reset(&esc->sc_poll_ch, 1, esp_pcmcia_poll, esc);
349 }
350 #endif
351 
352 /*
353  * Glue functions.
354  */
355 u_char
356 esp_pcmcia_read_reg(sc, reg)
357 	struct ncr53c9x_softc *sc;
358 	int reg;
359 {
360 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
361 	u_char v;
362 
363 	v = bus_space_read_1(esc->sc_pcioh.iot, esc->sc_pcioh.ioh, reg);
364 	return v;
365 }
366 
367 void
368 esp_pcmcia_write_reg(sc, reg, val)
369 	struct ncr53c9x_softc *sc;
370 	int reg;
371 	u_char val;
372 {
373 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
374 	u_char v = val;
375 
376 	if (reg == NCR_CMD && v == (NCRCMD_TRANS|NCRCMD_DMA))
377 		v = NCRCMD_TRANS;
378 	bus_space_write_1(esc->sc_pcioh.iot, esc->sc_pcioh.ioh, reg, v);
379 }
380 
381 int
382 esp_pcmcia_dma_isintr(sc)
383 	struct ncr53c9x_softc *sc;
384 {
385 
386 	return NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT;
387 }
388 
389 void
390 esp_pcmcia_dma_reset(sc)
391 	struct ncr53c9x_softc *sc;
392 {
393 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
394 
395 	esc->sc_active = 0;
396 	esc->sc_tc = 0;
397 }
398 
399 int
400 esp_pcmcia_dma_intr(sc)
401 	struct ncr53c9x_softc *sc;
402 {
403 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
404 	u_char	*p;
405 	u_int	espphase, espstat, espintr;
406 	int	cnt;
407 
408 	if (esc->sc_active == 0) {
409 		printf("%s: dma_intr--inactive DMA\n", sc->sc_dev.dv_xname);
410 		return -1;
411 	}
412 
413 	if ((sc->sc_espintr & NCRINTR_BS) == 0) {
414 		esc->sc_active = 0;
415 		return 0;
416 	}
417 
418 	cnt = *esc->sc_pdmalen;
419 	if (*esc->sc_pdmalen == 0) {
420 		printf("%s: data interrupt, but no count left\n",
421 		    sc->sc_dev.dv_xname);
422 	}
423 
424 	p = *esc->sc_dmaaddr;
425 	espphase = sc->sc_phase;
426 	espstat = (u_int) sc->sc_espstat;
427 	espintr = (u_int) sc->sc_espintr;
428 	do {
429 		if (esc->sc_datain) {
430 			*p++ = NCR_READ_REG(sc, NCR_FIFO);
431 			cnt--;
432 			if (espphase == DATA_IN_PHASE)
433 				NCR_WRITE_REG(sc, NCR_CMD, NCRCMD_TRANS);
434 			else
435 				esc->sc_active = 0;
436 	 	} else {
437 			if (espphase == DATA_OUT_PHASE ||
438 			    espphase == MESSAGE_OUT_PHASE) {
439 				NCR_WRITE_REG(sc, NCR_FIFO, *p++);
440 				cnt--;
441 				NCR_WRITE_REG(sc, NCR_CMD, NCRCMD_TRANS);
442 			} else
443 				esc->sc_active = 0;
444 		}
445 
446 		if (esc->sc_active) {
447 			while (!(NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT));
448 			espstat = NCR_READ_REG(sc, NCR_STAT);
449 			espintr = NCR_READ_REG(sc, NCR_INTR);
450 			espphase = (espintr & NCRINTR_DIS)
451 				    ? /* Disconnected */ BUSFREE_PHASE
452 				    : espstat & PHASE_MASK;
453 		}
454 	} while (esc->sc_active && espintr);
455 	sc->sc_phase = espphase;
456 	sc->sc_espstat = (u_char) espstat;
457 	sc->sc_espintr = (u_char) espintr;
458 	*esc->sc_dmaaddr = p;
459 	*esc->sc_pdmalen = cnt;
460 
461 	if (*esc->sc_pdmalen == 0)
462 		esc->sc_tc = NCRSTAT_TC;
463 	sc->sc_espstat |= esc->sc_tc;
464 	return 0;
465 }
466 
467 int
468 esp_pcmcia_dma_setup(sc, addr, len, datain, dmasize)
469 	struct ncr53c9x_softc *sc;
470 	caddr_t *addr;
471 	size_t *len;
472 	int datain;
473 	size_t *dmasize;
474 {
475 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
476 
477 	esc->sc_dmaaddr = addr;
478 	esc->sc_pdmalen = len;
479 	esc->sc_datain = datain;
480 	esc->sc_dmasize = *dmasize;
481 	esc->sc_tc = 0;
482 
483 	return 0;
484 }
485 
486 void
487 esp_pcmcia_dma_go(sc)
488 	struct ncr53c9x_softc *sc;
489 {
490 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
491 
492 	esc->sc_active = 1;
493 }
494 
495 void
496 esp_pcmcia_dma_stop(sc)
497 	struct ncr53c9x_softc *sc;
498 {
499 }
500 
501 int
502 esp_pcmcia_dma_isactive(sc)
503 	struct ncr53c9x_softc *sc;
504 {
505 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
506 
507 	return (esc->sc_active);
508 }
509