esp.c revision 1.10 1 1.10 gwr /* $NetBSD: esp.c,v 1.10 1999/04/08 04:46:41 gwr Exp $ */
2 1.1 jeremy
3 1.3 gwr /*-
4 1.3 gwr * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 1.3 gwr * All rights reserved.
6 1.1 jeremy *
7 1.3 gwr * This code is derived from software contributed to The NetBSD Foundation
8 1.3 gwr * by Jeremy Cooper and Gordon W. Ross
9 1.1 jeremy *
10 1.1 jeremy * Redistribution and use in source and binary forms, with or without
11 1.1 jeremy * modification, are permitted provided that the following conditions
12 1.1 jeremy * are met:
13 1.1 jeremy * 1. Redistributions of source code must retain the above copyright
14 1.1 jeremy * notice, this list of conditions and the following disclaimer.
15 1.1 jeremy * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 jeremy * notice, this list of conditions and the following disclaimer in the
17 1.1 jeremy * documentation and/or other materials provided with the distribution.
18 1.1 jeremy * 3. All advertising materials mentioning features or use of this software
19 1.1 jeremy * must display the following acknowledgement:
20 1.3 gwr * This product includes software developed by the NetBSD
21 1.3 gwr * Foundation, Inc. and its contributors.
22 1.3 gwr * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.3 gwr * contributors may be used to endorse or promote products derived
24 1.3 gwr * from this software without specific prior written permission.
25 1.1 jeremy *
26 1.3 gwr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.3 gwr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.3 gwr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.3 gwr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.3 gwr * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.3 gwr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.3 gwr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.3 gwr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.3 gwr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.3 gwr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 jeremy * POSSIBILITY OF SUCH DAMAGE.
37 1.1 jeremy */
38 1.1 jeremy
39 1.1 jeremy /*
40 1.3 gwr * "Front end" glue for the ncr53c9x chip, formerly known as the
41 1.3 gwr * Emulex SCSI Processor (ESP) which is what we actually have.
42 1.1 jeremy */
43 1.1 jeremy
44 1.1 jeremy #include <sys/types.h>
45 1.1 jeremy #include <sys/param.h>
46 1.1 jeremy #include <sys/systm.h>
47 1.1 jeremy #include <sys/kernel.h>
48 1.1 jeremy #include <sys/errno.h>
49 1.1 jeremy #include <sys/device.h>
50 1.1 jeremy #include <sys/buf.h>
51 1.1 jeremy
52 1.5 bouyer #include <dev/scsipi/scsi_all.h>
53 1.5 bouyer #include <dev/scsipi/scsipi_all.h>
54 1.5 bouyer #include <dev/scsipi/scsiconf.h>
55 1.5 bouyer #include <dev/scsipi/scsi_message.h>
56 1.1 jeremy
57 1.1 jeremy #include <machine/autoconf.h>
58 1.3 gwr
59 1.3 gwr #include <dev/ic/ncr53c9xreg.h>
60 1.3 gwr #include <dev/ic/ncr53c9xvar.h>
61 1.3 gwr
62 1.7 gwr #include <sun3/dev/dmareg.h>
63 1.7 gwr #include <sun3/dev/dmavar.h>
64 1.1 jeremy
65 1.1 jeremy #define ESP_REG_SIZE (12*4)
66 1.1 jeremy
67 1.3 gwr struct esp_softc {
68 1.3 gwr struct ncr53c9x_softc sc_ncr53c9x; /* glue to MI code */
69 1.3 gwr volatile u_char *sc_reg; /* the registers */
70 1.3 gwr struct dma_softc *sc_dma; /* pointer to my dma */
71 1.3 gwr };
72 1.1 jeremy
73 1.3 gwr static int espmatch __P((struct device *, struct cfdata *, void *));
74 1.3 gwr static void espattach __P((struct device *, struct device *, void *));
75 1.1 jeremy
76 1.1 jeremy struct cfattach esp_ca = {
77 1.1 jeremy sizeof(struct esp_softc), espmatch, espattach
78 1.1 jeremy };
79 1.1 jeremy
80 1.10 gwr static struct scsipi_device esp_dev = {
81 1.1 jeremy NULL, /* Use default error handler */
82 1.1 jeremy NULL, /* have a queue, served by this */
83 1.1 jeremy NULL, /* have no async handler */
84 1.1 jeremy NULL, /* Use default 'done' routine */
85 1.1 jeremy };
86 1.1 jeremy
87 1.3 gwr /*
88 1.3 gwr * Functions and the switch for the MI code.
89 1.3 gwr */
90 1.10 gwr static u_char esp_read_reg __P((struct ncr53c9x_softc *, int));
91 1.10 gwr static void esp_write_reg __P((struct ncr53c9x_softc *, int, u_char));
92 1.10 gwr static int esp_dma_isintr __P((struct ncr53c9x_softc *));
93 1.10 gwr static void esp_dma_reset __P((struct ncr53c9x_softc *));
94 1.10 gwr static int esp_dma_intr __P((struct ncr53c9x_softc *));
95 1.10 gwr static int esp_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
96 1.10 gwr size_t *, int, size_t *));
97 1.10 gwr static void esp_dma_go __P((struct ncr53c9x_softc *));
98 1.10 gwr static void esp_dma_stop __P((struct ncr53c9x_softc *));
99 1.10 gwr static int esp_dma_isactive __P((struct ncr53c9x_softc *));
100 1.3 gwr
101 1.3 gwr static struct ncr53c9x_glue esp_glue = {
102 1.3 gwr esp_read_reg,
103 1.3 gwr esp_write_reg,
104 1.3 gwr esp_dma_isintr,
105 1.3 gwr esp_dma_reset,
106 1.3 gwr esp_dma_intr,
107 1.3 gwr esp_dma_setup,
108 1.3 gwr esp_dma_go,
109 1.3 gwr esp_dma_stop,
110 1.3 gwr esp_dma_isactive,
111 1.3 gwr NULL, /* gl_clear_latched_intr */
112 1.3 gwr };
113 1.3 gwr
114 1.3 gwr static int
115 1.1 jeremy espmatch(parent, cf, aux)
116 1.1 jeremy struct device *parent;
117 1.1 jeremy struct cfdata *cf;
118 1.1 jeremy void *aux;
119 1.1 jeremy {
120 1.1 jeremy struct confargs *ca = aux;
121 1.1 jeremy
122 1.1 jeremy /*
123 1.1 jeremy * Check for the esp registers.
124 1.1 jeremy */
125 1.1 jeremy if (bus_peek(ca->ca_bustype,
126 1.3 gwr ca->ca_paddr + (NCR_STAT * 4), 1) == -1)
127 1.1 jeremy return (0);
128 1.1 jeremy
129 1.1 jeremy /* If default ipl, fill it in. */
130 1.1 jeremy if (ca->ca_intpri == -1)
131 1.1 jeremy ca->ca_intpri = 2;
132 1.1 jeremy
133 1.1 jeremy return (1);
134 1.1 jeremy }
135 1.1 jeremy
136 1.3 gwr static void
137 1.1 jeremy espattach(parent, self, aux)
138 1.1 jeremy struct device *parent, *self;
139 1.1 jeremy void *aux;
140 1.1 jeremy {
141 1.10 gwr struct confargs *ca = aux;
142 1.3 gwr struct esp_softc *esc = (void *)self;
143 1.3 gwr struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
144 1.1 jeremy
145 1.1 jeremy /*
146 1.3 gwr * Set up glue for MI code early; we use some of it here.
147 1.1 jeremy */
148 1.3 gwr sc->sc_glue = &esp_glue;
149 1.3 gwr
150 1.3 gwr /*
151 1.3 gwr * Map in the ESP registers.
152 1.3 gwr */
153 1.10 gwr esc->sc_reg =
154 1.10 gwr bus_mapin(ca->ca_bustype, ca->ca_paddr, ESP_REG_SIZE);
155 1.1 jeremy
156 1.3 gwr /* Other settings */
157 1.1 jeremy sc->sc_id = 7;
158 1.1 jeremy sc->sc_freq = 20; /* The 3/80 esp runs at 20 Mhz */
159 1.1 jeremy
160 1.1 jeremy /*
161 1.3 gwr * Hook up the DMA driver.
162 1.3 gwr */
163 1.10 gwr esc->sc_dma = espdmafind(sc->sc_dev.dv_unit);
164 1.3 gwr esc->sc_dma->sc_esp = sc; /* Point back to us */
165 1.3 gwr
166 1.3 gwr /*
167 1.3 gwr * XXX More of this should be in ncr53c9x_attach(), but
168 1.3 gwr * XXX should we really poke around the chip that much in
169 1.3 gwr * XXX the MI code? Think about this more...
170 1.3 gwr */
171 1.3 gwr
172 1.3 gwr /*
173 1.1 jeremy * It is necessary to try to load the 2nd config register here,
174 1.3 gwr * to find out what rev the esp chip is, else the ncr53c9x_reset
175 1.1 jeremy * will not set up the defaults correctly.
176 1.1 jeremy */
177 1.3 gwr sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
178 1.3 gwr sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_RPE;
179 1.3 gwr sc->sc_cfg3 = NCRCFG3_CDB;
180 1.3 gwr NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
181 1.3 gwr
182 1.3 gwr if ((NCR_READ_REG(sc, NCR_CFG2) & ~NCRCFG2_RSVD) !=
183 1.3 gwr (NCRCFG2_SCSI2 | NCRCFG2_RPE)) {
184 1.3 gwr sc->sc_rev = NCR_VARIANT_ESP100;
185 1.1 jeremy } else {
186 1.3 gwr sc->sc_cfg2 = NCRCFG2_SCSI2;
187 1.3 gwr NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
188 1.1 jeremy sc->sc_cfg3 = 0;
189 1.3 gwr NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
190 1.3 gwr sc->sc_cfg3 = (NCRCFG3_CDB | NCRCFG3_FCLK);
191 1.3 gwr NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
192 1.3 gwr if (NCR_READ_REG(sc, NCR_CFG3) !=
193 1.3 gwr (NCRCFG3_CDB | NCRCFG3_FCLK)) {
194 1.3 gwr sc->sc_rev = NCR_VARIANT_ESP100A;
195 1.1 jeremy } else {
196 1.3 gwr /* NCRCFG2_FE enables > 64K transfers */
197 1.3 gwr sc->sc_cfg2 |= NCRCFG2_FE;
198 1.1 jeremy sc->sc_cfg3 = 0;
199 1.3 gwr NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
200 1.3 gwr sc->sc_rev = NCR_VARIANT_ESP200;
201 1.1 jeremy }
202 1.1 jeremy }
203 1.1 jeremy
204 1.1 jeremy /*
205 1.3 gwr * XXX minsync and maxxfer _should_ be set up in MI code,
206 1.3 gwr * XXX but it appears to have some dependency on what sort
207 1.3 gwr * XXX of DMA we're hooked up to, etc.
208 1.1 jeremy */
209 1.1 jeremy
210 1.1 jeremy /*
211 1.1 jeremy * This is the value used to start sync negotiations
212 1.3 gwr * Note that the NCR register "SYNCTP" is programmed
213 1.1 jeremy * in "clocks per byte", and has a minimum value of 4.
214 1.1 jeremy * The SCSI period used in negotiation is one-fourth
215 1.1 jeremy * of the time (in nanoseconds) needed to transfer one byte.
216 1.1 jeremy * Since the chip's clock is given in MHz, we have the following
217 1.1 jeremy * formula: 4 * period = (1000 / freq) * 4
218 1.1 jeremy */
219 1.1 jeremy sc->sc_minsync = 1000 / sc->sc_freq;
220 1.1 jeremy
221 1.1 jeremy /*
222 1.1 jeremy * Alas, we must now modify the value a bit, because it's
223 1.1 jeremy * only valid when can switch on FASTCLK and FASTSCSI bits
224 1.1 jeremy * in config register 3...
225 1.1 jeremy */
226 1.1 jeremy switch (sc->sc_rev) {
227 1.3 gwr case NCR_VARIANT_ESP100:
228 1.3 gwr sc->sc_maxxfer = 64 * 1024;
229 1.1 jeremy sc->sc_minsync = 0; /* No synch on old chip? */
230 1.1 jeremy break;
231 1.3 gwr
232 1.3 gwr case NCR_VARIANT_ESP100A:
233 1.1 jeremy sc->sc_maxxfer = 64 * 1024;
234 1.3 gwr /* Min clocks/byte is 5 */
235 1.3 gwr sc->sc_minsync = ncr53c9x_cpb2stp(sc, 5);
236 1.1 jeremy break;
237 1.3 gwr
238 1.3 gwr case NCR_VARIANT_ESP200:
239 1.1 jeremy sc->sc_maxxfer = 16 * 1024 * 1024;
240 1.1 jeremy /* XXX - do actually set FAST* bits */
241 1.3 gwr break;
242 1.1 jeremy }
243 1.1 jeremy
244 1.1 jeremy /* and the interuppts */
245 1.3 gwr isr_add_autovect((void*)ncr53c9x_intr, sc, ca->ca_intpri);
246 1.1 jeremy evcnt_attach(&sc->sc_dev, "intr", &sc->sc_intrcnt);
247 1.1 jeremy
248 1.3 gwr /* Do the common parts of attachment. */
249 1.9 thorpej sc->sc_adapter.scsipi_cmd = ncr53c9x_scsi_cmd;
250 1.9 thorpej sc->sc_adapter.scsipi_minphys = minphys;
251 1.9 thorpej ncr53c9x_attach(sc, &esp_dev);
252 1.10 gwr
253 1.10 gwr #if 0
254 1.10 gwr /* XXX - This doesn't work yet. Not sure why... */
255 1.10 gwr /* Turn on target selection using the `dma' method */
256 1.10 gwr ncr53c9x_dmaselect = 1; /* XXX - OK? */
257 1.10 gwr #endif
258 1.1 jeremy }
259 1.1 jeremy
260 1.1 jeremy
261 1.1 jeremy /*
262 1.3 gwr * Glue functions.
263 1.1 jeremy */
264 1.1 jeremy
265 1.3 gwr u_char
266 1.3 gwr esp_read_reg(sc, reg)
267 1.3 gwr struct ncr53c9x_softc *sc;
268 1.3 gwr int reg;
269 1.1 jeremy {
270 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc;
271 1.1 jeremy
272 1.3 gwr return (esc->sc_reg[reg * 4]);
273 1.1 jeremy }
274 1.1 jeremy
275 1.1 jeremy void
276 1.3 gwr esp_write_reg(sc, reg, val)
277 1.3 gwr struct ncr53c9x_softc *sc;
278 1.3 gwr int reg;
279 1.3 gwr u_char val;
280 1.1 jeremy {
281 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc;
282 1.1 jeremy
283 1.10 gwr esc->sc_reg[reg * 4] = val;
284 1.1 jeremy }
285 1.1 jeremy
286 1.3 gwr int
287 1.3 gwr esp_dma_isintr(sc)
288 1.3 gwr struct ncr53c9x_softc *sc;
289 1.1 jeremy {
290 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc;
291 1.10 gwr u_int32_t csr;
292 1.1 jeremy
293 1.10 gwr csr = DMACSR(esc->sc_dma);
294 1.10 gwr return (csr & (D_INT_PEND|D_ERR_PEND));
295 1.1 jeremy }
296 1.1 jeremy
297 1.1 jeremy void
298 1.3 gwr esp_dma_reset(sc)
299 1.3 gwr struct ncr53c9x_softc *sc;
300 1.1 jeremy {
301 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc;
302 1.1 jeremy
303 1.3 gwr dma_reset(esc->sc_dma);
304 1.1 jeremy }
305 1.1 jeremy
306 1.1 jeremy int
307 1.3 gwr esp_dma_intr(sc)
308 1.3 gwr struct ncr53c9x_softc *sc;
309 1.1 jeremy {
310 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc;
311 1.1 jeremy
312 1.3 gwr return (espdmaintr(esc->sc_dma));
313 1.1 jeremy }
314 1.1 jeremy
315 1.1 jeremy int
316 1.3 gwr esp_dma_setup(sc, addr, len, datain, dmasize)
317 1.3 gwr struct ncr53c9x_softc *sc;
318 1.3 gwr caddr_t *addr;
319 1.3 gwr size_t *len;
320 1.3 gwr int datain;
321 1.3 gwr size_t *dmasize;
322 1.1 jeremy {
323 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc;
324 1.1 jeremy
325 1.3 gwr return (dma_setup(esc->sc_dma, addr, len, datain, dmasize));
326 1.1 jeremy }
327 1.1 jeremy
328 1.1 jeremy void
329 1.3 gwr esp_dma_go(sc)
330 1.3 gwr struct ncr53c9x_softc *sc;
331 1.1 jeremy {
332 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc;
333 1.1 jeremy
334 1.3 gwr /* Start DMA */
335 1.3 gwr DMACSR(esc->sc_dma) |= D_EN_DMA;
336 1.3 gwr esc->sc_dma->sc_active = 1;
337 1.1 jeremy }
338 1.1 jeremy
339 1.1 jeremy void
340 1.3 gwr esp_dma_stop(sc)
341 1.3 gwr struct ncr53c9x_softc *sc;
342 1.1 jeremy {
343 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc;
344 1.1 jeremy
345 1.3 gwr DMACSR(esc->sc_dma) &= ~D_EN_DMA;
346 1.1 jeremy }
347 1.1 jeremy
348 1.1 jeremy int
349 1.3 gwr esp_dma_isactive(sc)
350 1.3 gwr struct ncr53c9x_softc *sc;
351 1.1 jeremy {
352 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc;
353 1.1 jeremy
354 1.3 gwr return (esc->sc_dma->sc_active);
355 1.1 jeremy }
356