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