xref: /netbsd-src/sys/dev/pcmcia/esp_pcmcia.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: esp_pcmcia.c,v 1.33 2007/10/19 12:01:04 ad 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.33 2007/10/19 12:01:04 ad 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 <sys/bus.h>
48 #include <sys/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 *, void **,
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(struct device *parent, struct cfdata *match,
137     void *aux)
138 {
139 	struct pcmcia_attach_args *pa = aux;
140 
141 	if (pcmcia_product_lookup(pa, esp_pcmcia_products, esp_pcmcia_nproducts,
142 	    sizeof(esp_pcmcia_products[0]), NULL))
143 		return (1);
144 	return (0);
145 }
146 
147 int
148 esp_pcmcia_validate_config(cfe)
149 	struct pcmcia_config_entry *cfe;
150 {
151 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
152 	    cfe->num_memspace != 0 ||
153 	    cfe->num_iospace != 1)
154 		return (EINVAL);
155 	return (0);
156 }
157 
158 void
159 esp_pcmcia_attach(struct device *parent, struct device *self,
160     void *aux)
161 {
162 	struct esp_pcmcia_softc *esc = (void *)self;
163 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
164 	struct pcmcia_attach_args *pa = aux;
165 	struct pcmcia_config_entry *cfe;
166 	struct pcmcia_function *pf = pa->pf;
167 	int error;
168 
169 	esc->sc_pf = pf;
170 
171 	error = pcmcia_function_configure(pf, esp_pcmcia_validate_config);
172 	if (error) {
173 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
174 		    error);
175 		return;
176 	}
177 
178 	cfe = pf->cfe;
179 	esc->sc_iot = cfe->iospace[0].handle.iot;
180 	esc->sc_ioh = cfe->iospace[0].handle.ioh;
181 	esp_pcmcia_init(esc);
182 
183 	printf("%s", self->dv_xname);
184 
185 	sc->sc_adapter.adapt_minphys = minphys;
186 	sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
187 	sc->sc_adapter.adapt_enable = esp_pcmcia_enable;
188 
189 	ncr53c9x_attach(sc);
190 	esc->sc_state = ESP_PCMCIA_ATTACHED;
191 }
192 
193 void
194 esp_pcmcia_init(esc)
195 	struct esp_pcmcia_softc *esc;
196 {
197 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
198 
199 	/* id 7, clock 40M, parity ON, sync OFF, fast ON, slow ON */
200 
201 	sc->sc_glue = &esp_pcmcia_glue;
202 
203 #ifdef ESP_PCMCIA_POLL
204 	callout_init(&esc->sc_poll_ch, 0);
205 #endif
206 
207 	sc->sc_rev = NCR_VARIANT_ESP406;
208 	sc->sc_id = 7;
209 	sc->sc_freq = 40;
210 	/* try -PARENB -SLOW */
211 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB | NCRCFG1_SLOW;
212 	/* try +FE */
213 	sc->sc_cfg2 = NCRCFG2_SCSI2;
214 	/* try -IDM -FSCSI -FCLK */
215 	sc->sc_cfg3 = NCRESPCFG3_CDB | NCRESPCFG3_FCLK | NCRESPCFG3_IDM |
216 	    NCRESPCFG3_FSCSI;
217 	sc->sc_cfg4 = NCRCFG4_ACTNEG;
218 	/* try +INTP */
219 	sc->sc_cfg5 = NCRCFG5_CRS1 | NCRCFG5_AADDR | NCRCFG5_PTRINC;
220 	sc->sc_minsync = 0;
221 	sc->sc_maxxfer = 64 * 1024;
222 }
223 
224 int
225 esp_pcmcia_detach(self, flags)
226 	struct device *self;
227 	int flags;
228 {
229 	struct esp_pcmcia_softc *sc = (void *)self;
230 	int error;
231 
232 	if (sc->sc_state != ESP_PCMCIA_ATTACHED)
233 		return (0);
234 
235 	error = ncr53c9x_detach(&sc->sc_ncr53c9x, flags);
236 	if (error)
237 		return (error);
238 
239 	pcmcia_function_unconfigure(sc->sc_pf);
240 
241 	return (0);
242 }
243 
244 int
245 esp_pcmcia_enable(arg, onoff)
246 	struct device *arg;
247 	int onoff;
248 {
249 	struct esp_pcmcia_softc *sc = (void *)arg;
250 	int error;
251 
252 	if (onoff) {
253 #ifdef ESP_PCMCIA_POLL
254 		callout_reset(&sc->sc_poll_ch, 1, esp_pcmcia_poll, sc);
255 #else
256 		/* Establish the interrupt handler. */
257 		sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
258 		    ncr53c9x_intr, &sc->sc_ncr53c9x);
259 		if (!sc->sc_ih)
260 			return (EIO);
261 #endif
262 
263 		error = pcmcia_function_enable(sc->sc_pf);
264 		if (error) {
265 			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
266 			sc->sc_ih = 0;
267 			return (error);
268 		}
269 
270 		/* Initialize only chip.  */
271 		ncr53c9x_init(&sc->sc_ncr53c9x, 0);
272 	} else {
273 		pcmcia_function_disable(sc->sc_pf);
274 #ifdef ESP_PCMCIA_POLL
275 		callout_stop(&sc->sc_poll_ch);
276 #else
277 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
278 		sc->sc_ih = 0;
279 #endif
280 	}
281 
282 	return (0);
283 }
284 
285 #ifdef ESP_PCMCIA_POLL
286 void
287 esp_pcmcia_poll(arg)
288 	void *arg;
289 {
290 	struct esp_pcmcia_softc *esc = arg;
291 
292 	(void) ncr53c9x_intr(&esc->sc_ncr53c9x);
293 	callout_reset(&esc->sc_poll_ch, 1, esp_pcmcia_poll, esc);
294 }
295 #endif
296 
297 /*
298  * Glue functions.
299  */
300 u_char
301 esp_pcmcia_read_reg(sc, reg)
302 	struct ncr53c9x_softc *sc;
303 	int reg;
304 {
305 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
306 	u_char v;
307 
308 	v = bus_space_read_1(esc->sc_iot, esc->sc_ioh, reg);
309 	return v;
310 }
311 
312 void
313 esp_pcmcia_write_reg(sc, reg, val)
314 	struct ncr53c9x_softc *sc;
315 	int reg;
316 	u_char val;
317 {
318 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
319 	u_char v = val;
320 
321 	if (reg == NCR_CMD && v == (NCRCMD_TRANS|NCRCMD_DMA))
322 		v = NCRCMD_TRANS;
323 	bus_space_write_1(esc->sc_iot, esc->sc_ioh, reg, v);
324 }
325 
326 int
327 esp_pcmcia_dma_isintr(sc)
328 	struct ncr53c9x_softc *sc;
329 {
330 
331 	return NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT;
332 }
333 
334 void
335 esp_pcmcia_dma_reset(sc)
336 	struct ncr53c9x_softc *sc;
337 {
338 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
339 
340 	esc->sc_active = 0;
341 	esc->sc_tc = 0;
342 }
343 
344 int
345 esp_pcmcia_dma_intr(sc)
346 	struct ncr53c9x_softc *sc;
347 {
348 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
349 	u_char	*p;
350 	u_int	espphase, espstat, espintr;
351 	int	cnt;
352 
353 	if (esc->sc_active == 0) {
354 		printf("%s: dma_intr--inactive DMA\n", sc->sc_dev.dv_xname);
355 		return -1;
356 	}
357 
358 	if ((sc->sc_espintr & NCRINTR_BS) == 0) {
359 		esc->sc_active = 0;
360 		return 0;
361 	}
362 
363 	cnt = *esc->sc_pdmalen;
364 	if (*esc->sc_pdmalen == 0) {
365 		printf("%s: data interrupt, but no count left\n",
366 		    sc->sc_dev.dv_xname);
367 	}
368 
369 	p = *esc->sc_dmaaddr;
370 	espphase = sc->sc_phase;
371 	espstat = (u_int) sc->sc_espstat;
372 	espintr = (u_int) sc->sc_espintr;
373 	do {
374 		if (esc->sc_datain) {
375 			*p++ = NCR_READ_REG(sc, NCR_FIFO);
376 			cnt--;
377 			if (espphase == DATA_IN_PHASE)
378 				NCR_WRITE_REG(sc, NCR_CMD, NCRCMD_TRANS);
379 			else
380 				esc->sc_active = 0;
381 		} else {
382 			if (espphase == DATA_OUT_PHASE ||
383 			    espphase == MESSAGE_OUT_PHASE) {
384 				NCR_WRITE_REG(sc, NCR_FIFO, *p++);
385 				cnt--;
386 				NCR_WRITE_REG(sc, NCR_CMD, NCRCMD_TRANS);
387 			} else
388 				esc->sc_active = 0;
389 		}
390 
391 		if (esc->sc_active) {
392 			while (!(NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT));
393 			espstat = NCR_READ_REG(sc, NCR_STAT);
394 			espintr = NCR_READ_REG(sc, NCR_INTR);
395 			espphase = (espintr & NCRINTR_DIS)
396 				    ? /* Disconnected */ BUSFREE_PHASE
397 				    : espstat & PHASE_MASK;
398 		}
399 	} while (esc->sc_active && espintr);
400 	sc->sc_phase = espphase;
401 	sc->sc_espstat = (u_char) espstat;
402 	sc->sc_espintr = (u_char) espintr;
403 	*esc->sc_dmaaddr = p;
404 	*esc->sc_pdmalen = cnt;
405 
406 	if (*esc->sc_pdmalen == 0)
407 		esc->sc_tc = NCRSTAT_TC;
408 	sc->sc_espstat |= esc->sc_tc;
409 	return 0;
410 }
411 
412 int
413 esp_pcmcia_dma_setup(sc, addr, len, datain, dmasize)
414 	struct ncr53c9x_softc *sc;
415 	void **addr;
416 	size_t *len;
417 	int datain;
418 	size_t *dmasize;
419 {
420 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
421 
422 	esc->sc_dmaaddr = (void *)addr;
423 	esc->sc_pdmalen = len;
424 	esc->sc_datain = datain;
425 	esc->sc_dmasize = *dmasize;
426 	esc->sc_tc = 0;
427 
428 	return 0;
429 }
430 
431 void
432 esp_pcmcia_dma_go(sc)
433 	struct ncr53c9x_softc *sc;
434 {
435 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
436 
437 	esc->sc_active = 1;
438 }
439 
440 void
441 esp_pcmcia_dma_stop(struct ncr53c9x_softc *sc)
442 {
443 }
444 
445 int
446 esp_pcmcia_dma_isactive(sc)
447 	struct ncr53c9x_softc *sc;
448 {
449 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
450 
451 	return (esc->sc_active);
452 }
453