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