esp.c revision 1.5 1 1.5 bouyer /* $NetBSD: esp.c,v 1.5 1997/08/27 11:24:33 bouyer 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/ioctl.h>
50 1.1 jeremy #include <sys/device.h>
51 1.1 jeremy #include <sys/buf.h>
52 1.1 jeremy #include <sys/proc.h>
53 1.1 jeremy #include <sys/user.h>
54 1.1 jeremy #include <sys/queue.h>
55 1.1 jeremy #include <sys/malloc.h>
56 1.1 jeremy
57 1.5 bouyer #include <dev/scsipi/scsi_all.h>
58 1.5 bouyer #include <dev/scsipi/scsipi_all.h>
59 1.5 bouyer #include <dev/scsipi/scsiconf.h>
60 1.5 bouyer #include <dev/scsipi/scsi_message.h>
61 1.1 jeremy
62 1.1 jeremy #include <machine/autoconf.h>
63 1.3 gwr
64 1.3 gwr #include <dev/ic/ncr53c9xreg.h>
65 1.3 gwr #include <dev/ic/ncr53c9xvar.h>
66 1.3 gwr
67 1.1 jeremy #include <sun3x/dev/dmareg.h>
68 1.1 jeremy #include <sun3x/dev/dmavar.h>
69 1.1 jeremy
70 1.1 jeremy #define ESP_REG_SIZE (12*4)
71 1.1 jeremy #define ESP_DMA_OFF 0x1000
72 1.1 jeremy
73 1.3 gwr struct esp_softc {
74 1.3 gwr struct ncr53c9x_softc sc_ncr53c9x; /* glue to MI code */
75 1.3 gwr volatile u_char *sc_reg; /* the registers */
76 1.3 gwr struct dma_softc *sc_dma; /* pointer to my dma */
77 1.3 gwr };
78 1.1 jeremy
79 1.3 gwr static int espmatch __P((struct device *, struct cfdata *, void *));
80 1.3 gwr static void espattach __P((struct device *, struct device *, void *));
81 1.1 jeremy
82 1.1 jeremy struct cfattach esp_ca = {
83 1.1 jeremy sizeof(struct esp_softc), espmatch, espattach
84 1.1 jeremy };
85 1.1 jeremy
86 1.1 jeremy struct cfdriver esp_cd = {
87 1.1 jeremy NULL, "esp", DV_DULL
88 1.1 jeremy };
89 1.1 jeremy
90 1.5 bouyer struct scsipi_adapter esp_switch = {
91 1.3 gwr ncr53c9x_scsi_cmd,
92 1.1 jeremy minphys, /* no max at this level; handled by DMA code */
93 1.1 jeremy NULL,
94 1.1 jeremy NULL,
95 1.1 jeremy };
96 1.1 jeremy
97 1.5 bouyer struct scsipi_device esp_dev = {
98 1.1 jeremy NULL, /* Use default error handler */
99 1.1 jeremy NULL, /* have a queue, served by this */
100 1.1 jeremy NULL, /* have no async handler */
101 1.1 jeremy NULL, /* Use default 'done' routine */
102 1.1 jeremy };
103 1.1 jeremy
104 1.3 gwr /*
105 1.3 gwr * Functions and the switch for the MI code.
106 1.3 gwr */
107 1.3 gwr u_char esp_read_reg __P((struct ncr53c9x_softc *, int));
108 1.3 gwr void esp_write_reg __P((struct ncr53c9x_softc *, int, u_char));
109 1.3 gwr int esp_dma_isintr __P((struct ncr53c9x_softc *));
110 1.3 gwr void esp_dma_reset __P((struct ncr53c9x_softc *));
111 1.3 gwr int esp_dma_intr __P((struct ncr53c9x_softc *));
112 1.3 gwr int esp_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
113 1.3 gwr size_t *, int, size_t *));
114 1.3 gwr void esp_dma_go __P((struct ncr53c9x_softc *));
115 1.3 gwr void esp_dma_stop __P((struct ncr53c9x_softc *));
116 1.3 gwr int esp_dma_isactive __P((struct ncr53c9x_softc *));
117 1.3 gwr
118 1.3 gwr static struct ncr53c9x_glue esp_glue = {
119 1.3 gwr esp_read_reg,
120 1.3 gwr esp_write_reg,
121 1.3 gwr esp_dma_isintr,
122 1.3 gwr esp_dma_reset,
123 1.3 gwr esp_dma_intr,
124 1.3 gwr esp_dma_setup,
125 1.3 gwr esp_dma_go,
126 1.3 gwr esp_dma_stop,
127 1.3 gwr esp_dma_isactive,
128 1.3 gwr NULL, /* gl_clear_latched_intr */
129 1.3 gwr };
130 1.3 gwr
131 1.4 jeremy extern int ncr53c9x_dmaselect; /* Used in dev/ic/ncr53c9x.c */
132 1.4 jeremy
133 1.3 gwr static int
134 1.1 jeremy espmatch(parent, cf, aux)
135 1.1 jeremy struct device *parent;
136 1.1 jeremy struct cfdata *cf;
137 1.1 jeremy void *aux;
138 1.1 jeremy {
139 1.1 jeremy struct confargs *ca = aux;
140 1.1 jeremy
141 1.1 jeremy /*
142 1.1 jeremy * Check for the DMA registers.
143 1.1 jeremy */
144 1.1 jeremy if (bus_peek(ca->ca_bustype,
145 1.1 jeremy ca->ca_paddr + ESP_DMA_OFF, 4) == -1)
146 1.1 jeremy return (0);
147 1.1 jeremy
148 1.1 jeremy /*
149 1.1 jeremy * Check for the esp registers.
150 1.1 jeremy */
151 1.1 jeremy if (bus_peek(ca->ca_bustype,
152 1.3 gwr ca->ca_paddr + (NCR_STAT * 4), 1) == -1)
153 1.1 jeremy return (0);
154 1.1 jeremy
155 1.1 jeremy /* If default ipl, fill it in. */
156 1.1 jeremy if (ca->ca_intpri == -1)
157 1.1 jeremy ca->ca_intpri = 2;
158 1.1 jeremy
159 1.1 jeremy return (1);
160 1.1 jeremy }
161 1.1 jeremy
162 1.1 jeremy /*
163 1.1 jeremy * Attach this instance, and then all the sub-devices
164 1.3 gwr *
165 1.3 gwr * In the SPARC port, the dma code used by the esp driver looks like
166 1.3 gwr * a separate driver, matched and attached by either the esp driver
167 1.3 gwr * or the bus attach function. However it's not completely separate
168 1.3 gwr * in that the sparc esp driver has to go look in dma_cd.cd_devs to
169 1.3 gwr * get the softc for the dma driver, and shares its softc, etc.
170 1.3 gwr *
171 1.3 gwr * The dma module could exist as a separate autoconfig entity, but
172 1.3 gwr * that really does not buy us anything, so why bother with that?
173 1.3 gwr * In the current sun3x port, the dma chip is treated as just an
174 1.3 gwr * extension of the esp driver because that is easier, and the esp
175 1.3 gwr * driver is the only one that uses the dma module.
176 1.1 jeremy */
177 1.3 gwr static void
178 1.1 jeremy espattach(parent, self, aux)
179 1.1 jeremy struct device *parent, *self;
180 1.1 jeremy void *aux;
181 1.1 jeremy {
182 1.1 jeremy register struct confargs *ca = aux;
183 1.3 gwr struct esp_softc *esc = (void *)self;
184 1.3 gwr struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
185 1.1 jeremy
186 1.1 jeremy /*
187 1.3 gwr * Set up glue for MI code early; we use some of it here.
188 1.1 jeremy */
189 1.3 gwr sc->sc_glue = &esp_glue;
190 1.3 gwr
191 1.3 gwr /*
192 1.3 gwr * Map in the ESP registers.
193 1.3 gwr */
194 1.3 gwr esc->sc_reg = (volatile u_char *)
195 1.1 jeremy bus_mapin(ca->ca_bustype, ca->ca_paddr, NBPG);
196 1.1 jeremy
197 1.3 gwr /* Other settings */
198 1.1 jeremy sc->sc_id = 7;
199 1.1 jeremy sc->sc_freq = 20; /* The 3/80 esp runs at 20 Mhz */
200 1.1 jeremy
201 1.1 jeremy /*
202 1.3 gwr * Hook up the DMA driver.
203 1.3 gwr * XXX - Would rather do this later, after the common
204 1.3 gwr * attach function is done printing its line so the DMA
205 1.3 gwr * module can print its revision, but the common attach
206 1.3 gwr * code needs this done first...
207 1.3 gwr * XXX - Move printf back to MD code?
208 1.3 gwr */
209 1.3 gwr esc->sc_dma = malloc(sizeof(struct dma_softc), M_DEVBUF, M_NOWAIT);
210 1.3 gwr if (esc->sc_dma == 0)
211 1.3 gwr panic("espattach: malloc dma_softc");
212 1.3 gwr bzero(esc->sc_dma, sizeof(struct dma_softc));
213 1.3 gwr esc->sc_dma->sc_esp = sc; /* Point back to us */
214 1.3 gwr esc->sc_dma->sc_regs = (struct dma_regs *)
215 1.3 gwr (esc->sc_reg + ESP_DMA_OFF);
216 1.3 gwr
217 1.3 gwr /*
218 1.3 gwr * Simulate an attach call here for compatibility with
219 1.3 gwr * the sparc dma.c module. It does not print anything.
220 1.3 gwr */
221 1.3 gwr dmaattach(self, (struct device *) esc->sc_dma, NULL);
222 1.3 gwr
223 1.3 gwr /*
224 1.3 gwr * XXX More of this should be in ncr53c9x_attach(), but
225 1.3 gwr * XXX should we really poke around the chip that much in
226 1.3 gwr * XXX the MI code? Think about this more...
227 1.3 gwr */
228 1.3 gwr
229 1.3 gwr /*
230 1.1 jeremy * It is necessary to try to load the 2nd config register here,
231 1.3 gwr * to find out what rev the esp chip is, else the ncr53c9x_reset
232 1.1 jeremy * will not set up the defaults correctly.
233 1.1 jeremy */
234 1.3 gwr sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
235 1.3 gwr sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_RPE;
236 1.3 gwr sc->sc_cfg3 = NCRCFG3_CDB;
237 1.3 gwr NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
238 1.3 gwr
239 1.3 gwr if ((NCR_READ_REG(sc, NCR_CFG2) & ~NCRCFG2_RSVD) !=
240 1.3 gwr (NCRCFG2_SCSI2 | NCRCFG2_RPE)) {
241 1.3 gwr sc->sc_rev = NCR_VARIANT_ESP100;
242 1.1 jeremy } else {
243 1.3 gwr sc->sc_cfg2 = NCRCFG2_SCSI2;
244 1.3 gwr NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
245 1.1 jeremy sc->sc_cfg3 = 0;
246 1.3 gwr NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
247 1.3 gwr sc->sc_cfg3 = (NCRCFG3_CDB | NCRCFG3_FCLK);
248 1.3 gwr NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
249 1.3 gwr if (NCR_READ_REG(sc, NCR_CFG3) !=
250 1.3 gwr (NCRCFG3_CDB | NCRCFG3_FCLK)) {
251 1.3 gwr sc->sc_rev = NCR_VARIANT_ESP100A;
252 1.1 jeremy } else {
253 1.3 gwr /* NCRCFG2_FE enables > 64K transfers */
254 1.3 gwr sc->sc_cfg2 |= NCRCFG2_FE;
255 1.1 jeremy sc->sc_cfg3 = 0;
256 1.3 gwr NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
257 1.3 gwr sc->sc_rev = NCR_VARIANT_ESP200;
258 1.1 jeremy }
259 1.1 jeremy }
260 1.1 jeremy
261 1.1 jeremy /*
262 1.3 gwr * XXX minsync and maxxfer _should_ be set up in MI code,
263 1.3 gwr * XXX but it appears to have some dependency on what sort
264 1.3 gwr * XXX of DMA we're hooked up to, etc.
265 1.1 jeremy */
266 1.1 jeremy
267 1.1 jeremy /*
268 1.1 jeremy * This is the value used to start sync negotiations
269 1.3 gwr * Note that the NCR register "SYNCTP" is programmed
270 1.1 jeremy * in "clocks per byte", and has a minimum value of 4.
271 1.1 jeremy * The SCSI period used in negotiation is one-fourth
272 1.1 jeremy * of the time (in nanoseconds) needed to transfer one byte.
273 1.1 jeremy * Since the chip's clock is given in MHz, we have the following
274 1.1 jeremy * formula: 4 * period = (1000 / freq) * 4
275 1.1 jeremy */
276 1.1 jeremy sc->sc_minsync = 1000 / sc->sc_freq;
277 1.1 jeremy
278 1.1 jeremy /*
279 1.1 jeremy * Alas, we must now modify the value a bit, because it's
280 1.1 jeremy * only valid when can switch on FASTCLK and FASTSCSI bits
281 1.1 jeremy * in config register 3...
282 1.1 jeremy */
283 1.1 jeremy switch (sc->sc_rev) {
284 1.3 gwr case NCR_VARIANT_ESP100:
285 1.3 gwr sc->sc_maxxfer = 64 * 1024;
286 1.1 jeremy sc->sc_minsync = 0; /* No synch on old chip? */
287 1.4 jeremy /* Avoid hardware bug by using DMA when selecting targets */
288 1.4 jeremy /* ncr53c9x_dmaselect = 1; */
289 1.1 jeremy break;
290 1.3 gwr
291 1.3 gwr case NCR_VARIANT_ESP100A:
292 1.1 jeremy sc->sc_maxxfer = 64 * 1024;
293 1.3 gwr /* Min clocks/byte is 5 */
294 1.3 gwr sc->sc_minsync = ncr53c9x_cpb2stp(sc, 5);
295 1.1 jeremy break;
296 1.3 gwr
297 1.3 gwr case NCR_VARIANT_ESP200:
298 1.1 jeremy sc->sc_maxxfer = 16 * 1024 * 1024;
299 1.1 jeremy /* XXX - do actually set FAST* bits */
300 1.3 gwr break;
301 1.1 jeremy }
302 1.1 jeremy
303 1.1 jeremy /* and the interuppts */
304 1.3 gwr isr_add_autovect((void*)ncr53c9x_intr, sc, ca->ca_intpri);
305 1.1 jeremy evcnt_attach(&sc->sc_dev, "intr", &sc->sc_intrcnt);
306 1.1 jeremy
307 1.3 gwr /* Do the common parts of attachment. */
308 1.3 gwr ncr53c9x_attach(sc, &esp_switch, &esp_dev);
309 1.1 jeremy }
310 1.1 jeremy
311 1.1 jeremy
312 1.1 jeremy /*
313 1.3 gwr * Glue functions.
314 1.1 jeremy */
315 1.1 jeremy
316 1.3 gwr u_char
317 1.3 gwr esp_read_reg(sc, reg)
318 1.3 gwr struct ncr53c9x_softc *sc;
319 1.3 gwr int reg;
320 1.1 jeremy {
321 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc;
322 1.1 jeremy
323 1.3 gwr return (esc->sc_reg[reg * 4]);
324 1.1 jeremy }
325 1.1 jeremy
326 1.1 jeremy void
327 1.3 gwr esp_write_reg(sc, reg, val)
328 1.3 gwr struct ncr53c9x_softc *sc;
329 1.3 gwr int reg;
330 1.3 gwr u_char val;
331 1.1 jeremy {
332 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc;
333 1.3 gwr u_char v = val;
334 1.1 jeremy
335 1.3 gwr esc->sc_reg[reg * 4] = v;
336 1.1 jeremy }
337 1.1 jeremy
338 1.3 gwr int
339 1.3 gwr esp_dma_isintr(sc)
340 1.3 gwr struct ncr53c9x_softc *sc;
341 1.1 jeremy {
342 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc;
343 1.1 jeremy
344 1.3 gwr return (dma_isintr(esc->sc_dma));
345 1.1 jeremy }
346 1.1 jeremy
347 1.1 jeremy void
348 1.3 gwr esp_dma_reset(sc)
349 1.3 gwr struct ncr53c9x_softc *sc;
350 1.1 jeremy {
351 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc;
352 1.1 jeremy
353 1.3 gwr dma_reset(esc->sc_dma);
354 1.1 jeremy }
355 1.1 jeremy
356 1.1 jeremy int
357 1.3 gwr esp_dma_intr(sc)
358 1.3 gwr struct ncr53c9x_softc *sc;
359 1.1 jeremy {
360 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc;
361 1.1 jeremy
362 1.3 gwr return (espdmaintr(esc->sc_dma));
363 1.1 jeremy }
364 1.1 jeremy
365 1.1 jeremy int
366 1.3 gwr esp_dma_setup(sc, addr, len, datain, dmasize)
367 1.3 gwr struct ncr53c9x_softc *sc;
368 1.3 gwr caddr_t *addr;
369 1.3 gwr size_t *len;
370 1.3 gwr int datain;
371 1.3 gwr size_t *dmasize;
372 1.1 jeremy {
373 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc;
374 1.1 jeremy
375 1.3 gwr return (dma_setup(esc->sc_dma, addr, len, datain, dmasize));
376 1.1 jeremy }
377 1.1 jeremy
378 1.1 jeremy void
379 1.3 gwr esp_dma_go(sc)
380 1.3 gwr struct ncr53c9x_softc *sc;
381 1.1 jeremy {
382 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc;
383 1.1 jeremy
384 1.3 gwr /* Start DMA */
385 1.3 gwr DMACSR(esc->sc_dma) |= D_EN_DMA;
386 1.3 gwr esc->sc_dma->sc_active = 1;
387 1.1 jeremy }
388 1.1 jeremy
389 1.1 jeremy void
390 1.3 gwr esp_dma_stop(sc)
391 1.3 gwr struct ncr53c9x_softc *sc;
392 1.1 jeremy {
393 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc;
394 1.1 jeremy
395 1.3 gwr DMACSR(esc->sc_dma) &= ~D_EN_DMA;
396 1.1 jeremy }
397 1.1 jeremy
398 1.1 jeremy int
399 1.3 gwr esp_dma_isactive(sc)
400 1.3 gwr struct ncr53c9x_softc *sc;
401 1.1 jeremy {
402 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc;
403 1.1 jeremy
404 1.3 gwr return (esc->sc_dma->sc_active);
405 1.1 jeremy }
406