asc_tc.c revision 1.30 1 1.30 yamt /* $NetBSD: asc_tc.c,v 1.30 2007/03/04 15:17:06 yamt Exp $ */
2 1.1 jonathan
3 1.16 simonb /*-
4 1.16 simonb * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.16 simonb * All rights reserved.
6 1.16 simonb *
7 1.16 simonb * This code is derived from software contributed to The NetBSD Foundation
8 1.16 simonb * by Tohru Nishimura.
9 1.1 jonathan *
10 1.16 simonb * Redistribution and use in source and binary forms, with or without
11 1.16 simonb * modification, are permitted provided that the following conditions
12 1.16 simonb * are met:
13 1.16 simonb * 1. Redistributions of source code must retain the above copyright
14 1.16 simonb * notice, this list of conditions and the following disclaimer.
15 1.16 simonb * 2. Redistributions in binary form must reproduce the above copyright
16 1.16 simonb * notice, this list of conditions and the following disclaimer in the
17 1.16 simonb * documentation and/or other materials provided with the distribution.
18 1.16 simonb * 3. All advertising materials mentioning features or use of this software
19 1.16 simonb * must display the following acknowledgement:
20 1.16 simonb * This product includes software developed by the NetBSD
21 1.16 simonb * Foundation, Inc. and its contributors.
22 1.16 simonb * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.16 simonb * contributors may be used to endorse or promote products derived
24 1.16 simonb * from this software without specific prior written permission.
25 1.1 jonathan *
26 1.16 simonb * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.16 simonb * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.16 simonb * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.16 simonb * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.16 simonb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.16 simonb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.16 simonb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.16 simonb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.16 simonb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.16 simonb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.16 simonb * POSSIBILITY OF SUCH DAMAGE.
37 1.1 jonathan */
38 1.1 jonathan
39 1.18 lukem #include <sys/cdefs.h>
40 1.30 yamt __KERNEL_RCSID(0, "$NetBSD: asc_tc.c,v 1.30 2007/03/04 15:17:06 yamt Exp $");
41 1.16 simonb
42 1.1 jonathan #include <sys/param.h>
43 1.1 jonathan #include <sys/systm.h>
44 1.1 jonathan #include <sys/device.h>
45 1.16 simonb #include <sys/buf.h>
46 1.16 simonb
47 1.16 simonb #include <dev/scsipi/scsi_all.h>
48 1.16 simonb #include <dev/scsipi/scsipi_all.h>
49 1.16 simonb #include <dev/scsipi/scsiconf.h>
50 1.16 simonb #include <dev/scsipi/scsi_message.h>
51 1.16 simonb
52 1.16 simonb #include <machine/bus.h>
53 1.16 simonb
54 1.16 simonb #include <dev/ic/ncr53c9xreg.h>
55 1.16 simonb #include <dev/ic/ncr53c9xvar.h>
56 1.10 mrg
57 1.1 jonathan #include <dev/tc/tcvar.h>
58 1.1 jonathan
59 1.16 simonb struct asc_softc {
60 1.16 simonb struct ncr53c9x_softc sc_ncr53c9x; /* glue to MI code */
61 1.16 simonb bus_space_tag_t sc_bst;
62 1.16 simonb bus_space_handle_t sc_bsh;
63 1.16 simonb bus_dma_tag_t sc_dmat;
64 1.16 simonb bus_dmamap_t sc_dmamap;
65 1.30 yamt char **sc_dmaaddr;
66 1.16 simonb size_t *sc_dmalen;
67 1.16 simonb size_t sc_dmasize;
68 1.16 simonb int sc_active; /* DMA active ? */
69 1.16 simonb int sc_ispullup; /* DMA into main memory? */
70 1.1 jonathan
71 1.16 simonb /* XXX XXX XXX */
72 1.30 yamt char *sc_base, *sc_bounce, *sc_target;
73 1.16 simonb };
74 1.1 jonathan
75 1.24 perry static int asc_tc_match(struct device *, struct cfdata *, void *);
76 1.24 perry static void asc_tc_attach(struct device *, struct device *, void *);
77 1.1 jonathan
78 1.21 thorpej CFATTACH_DECL(asc_tc, sizeof(struct asc_softc),
79 1.22 thorpej asc_tc_match, asc_tc_attach, NULL, NULL);
80 1.1 jonathan
81 1.24 perry static u_char asc_read_reg(struct ncr53c9x_softc *, int);
82 1.24 perry static void asc_write_reg(struct ncr53c9x_softc *, int, u_char);
83 1.24 perry static int asc_dma_isintr(struct ncr53c9x_softc *);
84 1.24 perry static void asc_tc_reset(struct ncr53c9x_softc *);
85 1.24 perry static int asc_tc_intr(struct ncr53c9x_softc *);
86 1.29 christos static int asc_tc_setup(struct ncr53c9x_softc *, void **,
87 1.24 perry size_t *, int, size_t *);
88 1.24 perry static void asc_tc_go(struct ncr53c9x_softc *);
89 1.24 perry static void asc_tc_stop(struct ncr53c9x_softc *);
90 1.24 perry static int asc_dma_isactive(struct ncr53c9x_softc *);
91 1.24 perry static void asc_clear_latched_intr(struct ncr53c9x_softc *);
92 1.16 simonb
93 1.17 simonb static struct ncr53c9x_glue asc_tc_glue = {
94 1.16 simonb asc_read_reg,
95 1.16 simonb asc_write_reg,
96 1.16 simonb asc_dma_isintr,
97 1.17 simonb asc_tc_reset,
98 1.17 simonb asc_tc_intr,
99 1.17 simonb asc_tc_setup,
100 1.17 simonb asc_tc_go,
101 1.17 simonb asc_tc_stop,
102 1.16 simonb asc_dma_isactive,
103 1.16 simonb asc_clear_latched_intr,
104 1.1 jonathan };
105 1.1 jonathan
106 1.1 jonathan /*
107 1.16 simonb * Parameters specific to PMAZ-A TC option card.
108 1.1 jonathan */
109 1.16 simonb #define PMAZ_OFFSET_53C94 0x0 /* from module base */
110 1.16 simonb #define PMAZ_OFFSET_DMAR 0x40000 /* DMA Address Register */
111 1.16 simonb #define PMAZ_OFFSET_RAM 0x80000 /* 128KB SRAM buffer */
112 1.16 simonb #define PMAZ_OFFSET_ROM 0xc0000 /* diagnostic ROM */
113 1.16 simonb
114 1.16 simonb #define PMAZ_RAM_SIZE 0x20000 /* 128k (32k*32) */
115 1.16 simonb #define PER_TGT_DMA_SIZE ((PMAZ_RAM_SIZE/7) & ~(sizeof(int)-1))
116 1.16 simonb
117 1.16 simonb #define PMAZ_DMAR_WRITE 0x80000000 /* DMA direction bit */
118 1.16 simonb #define PMAZ_DMAR_MASK 0x1ffff /* 17 bits, 128k */
119 1.16 simonb #define PMAZ_DMA_ADDR(x) ((unsigned long)(x) & PMAZ_DMAR_MASK)
120 1.1 jonathan
121 1.7 mhitch static int
122 1.28 thorpej asc_tc_match(struct device *parent, struct cfdata *cfdata, void *aux)
123 1.1 jonathan {
124 1.16 simonb struct tc_attach_args *d = aux;
125 1.25 perry
126 1.16 simonb if (strncmp("PMAZ-AA ", d->ta_modname, TC_ROM_LLEN))
127 1.1 jonathan return (0);
128 1.1 jonathan
129 1.1 jonathan return (1);
130 1.1 jonathan }
131 1.1 jonathan
132 1.16 simonb static void
133 1.28 thorpej asc_tc_attach(struct device *parent, struct device *self, void *aux)
134 1.1 jonathan {
135 1.16 simonb struct tc_attach_args *ta = aux;
136 1.27 thorpej struct asc_softc *asc = device_private(self);
137 1.16 simonb struct ncr53c9x_softc *sc = &asc->sc_ncr53c9x;
138 1.1 jonathan
139 1.1 jonathan /*
140 1.16 simonb * Set up glue for MI code early; we use some of it here.
141 1.1 jonathan */
142 1.17 simonb sc->sc_glue = &asc_tc_glue;
143 1.16 simonb asc->sc_bst = ta->ta_memt;
144 1.16 simonb asc->sc_dmat = ta->ta_dmat;
145 1.16 simonb if (bus_space_map(asc->sc_bst, ta->ta_addr,
146 1.16 simonb PMAZ_OFFSET_RAM + PMAZ_RAM_SIZE, 0, &asc->sc_bsh)) {
147 1.16 simonb printf("%s: unable to map device\n", sc->sc_dev.dv_xname);
148 1.16 simonb return;
149 1.16 simonb }
150 1.29 christos asc->sc_base = (void *)ta->ta_addr; /* XXX XXX XXX */
151 1.16 simonb
152 1.16 simonb tc_intr_establish(parent, ta->ta_cookie, IPL_BIO, ncr53c9x_intr, sc);
153 1.25 perry
154 1.16 simonb sc->sc_id = 7;
155 1.16 simonb sc->sc_freq = (ta->ta_busspeed) ? 25000000 : 12500000;
156 1.16 simonb
157 1.23 tsutsui /* gimme MHz */
158 1.16 simonb sc->sc_freq /= 1000000;
159 1.1 jonathan
160 1.1 jonathan /*
161 1.16 simonb * XXX More of this should be in ncr53c9x_attach(), but
162 1.16 simonb * XXX should we really poke around the chip that much in
163 1.16 simonb * XXX the MI code? Think about this more...
164 1.1 jonathan */
165 1.1 jonathan
166 1.1 jonathan /*
167 1.16 simonb * Set up static configuration info.
168 1.1 jonathan */
169 1.16 simonb sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
170 1.16 simonb sc->sc_cfg2 = NCRCFG2_SCSI2;
171 1.16 simonb sc->sc_cfg3 = 0;
172 1.16 simonb sc->sc_rev = NCR_VARIANT_NCR53C94;
173 1.7 mhitch
174 1.7 mhitch /*
175 1.16 simonb * XXX minsync and maxxfer _should_ be set up in MI code,
176 1.16 simonb * XXX but it appears to have some dependency on what sort
177 1.16 simonb * XXX of DMA we're hooked up to, etc.
178 1.7 mhitch */
179 1.7 mhitch
180 1.7 mhitch /*
181 1.16 simonb * This is the value used to start sync negotiations
182 1.16 simonb * Note that the NCR register "SYNCTP" is programmed
183 1.16 simonb * in "clocks per byte", and has a minimum value of 4.
184 1.16 simonb * The SCSI period used in negotiation is one-fourth
185 1.16 simonb * of the time (in nanoseconds) needed to transfer one byte.
186 1.16 simonb * Since the chip's clock is given in MHz, we have the following
187 1.16 simonb * formula: 4 * period = (1000 / freq) * 4
188 1.7 mhitch */
189 1.16 simonb sc->sc_minsync = (1000 / sc->sc_freq) * 5 / 4;
190 1.7 mhitch
191 1.16 simonb sc->sc_maxxfer = 64 * 1024;
192 1.1 jonathan
193 1.16 simonb /* Do the common parts of attachment. */
194 1.16 simonb sc->sc_adapter.adapt_minphys = minphys;
195 1.16 simonb sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
196 1.16 simonb ncr53c9x_attach(sc);
197 1.16 simonb }
198 1.16 simonb
199 1.16 simonb static void
200 1.28 thorpej asc_tc_reset(struct ncr53c9x_softc *sc)
201 1.16 simonb {
202 1.16 simonb struct asc_softc *asc = (struct asc_softc *)sc;
203 1.16 simonb
204 1.16 simonb asc->sc_active = 0;
205 1.16 simonb }
206 1.16 simonb
207 1.16 simonb static int
208 1.28 thorpej asc_tc_intr(struct ncr53c9x_softc *sc)
209 1.16 simonb {
210 1.16 simonb struct asc_softc *asc = (struct asc_softc *)sc;
211 1.16 simonb int trans, resid;
212 1.16 simonb
213 1.16 simonb resid = 0;
214 1.16 simonb if (!asc->sc_ispullup &&
215 1.16 simonb (resid = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF)) != 0) {
216 1.17 simonb NCR_DMA(("asc_tc_intr: empty FIFO of %d ", resid));
217 1.16 simonb DELAY(1);
218 1.16 simonb }
219 1.16 simonb
220 1.16 simonb resid += NCR_READ_REG(sc, NCR_TCL);
221 1.16 simonb resid += NCR_READ_REG(sc, NCR_TCM) << 8;
222 1.16 simonb
223 1.16 simonb trans = asc->sc_dmasize - resid;
224 1.16 simonb
225 1.16 simonb if (asc->sc_ispullup)
226 1.16 simonb memcpy(asc->sc_target, asc->sc_bounce, trans);
227 1.16 simonb *asc->sc_dmalen -= trans;
228 1.16 simonb *asc->sc_dmaaddr += trans;
229 1.16 simonb asc->sc_active = 0;
230 1.16 simonb
231 1.16 simonb return (0);
232 1.16 simonb }
233 1.16 simonb
234 1.16 simonb static int
235 1.29 christos asc_tc_setup(struct ncr53c9x_softc *sc, void **addr, size_t *len,
236 1.28 thorpej int datain, size_t *dmasize)
237 1.16 simonb {
238 1.16 simonb struct asc_softc *asc = (struct asc_softc *)sc;
239 1.16 simonb u_int32_t tc_dmar;
240 1.16 simonb size_t size;
241 1.16 simonb
242 1.30 yamt asc->sc_dmaaddr = (char **)addr;
243 1.16 simonb asc->sc_dmalen = len;
244 1.16 simonb asc->sc_ispullup = datain;
245 1.16 simonb
246 1.17 simonb NCR_DMA(("asc_tc_setup: start %ld@%p, %s\n", (long)*asc->sc_dmalen,
247 1.16 simonb *asc->sc_dmaaddr, datain ? "IN" : "OUT"));
248 1.16 simonb
249 1.16 simonb size = *dmasize;
250 1.16 simonb if (size > PER_TGT_DMA_SIZE)
251 1.16 simonb size = PER_TGT_DMA_SIZE;
252 1.16 simonb *dmasize = asc->sc_dmasize = size;
253 1.16 simonb
254 1.17 simonb NCR_DMA(("asc_tc_setup: dmasize = %ld\n", (long)asc->sc_dmasize));
255 1.16 simonb
256 1.16 simonb asc->sc_bounce = asc->sc_base + PMAZ_OFFSET_RAM;
257 1.16 simonb asc->sc_bounce += PER_TGT_DMA_SIZE *
258 1.16 simonb sc->sc_nexus->xs->xs_periph->periph_target;
259 1.16 simonb asc->sc_target = *addr;
260 1.16 simonb
261 1.16 simonb if (!asc->sc_ispullup)
262 1.16 simonb memcpy(asc->sc_bounce, asc->sc_target, size);
263 1.16 simonb
264 1.16 simonb #if 1
265 1.16 simonb if (asc->sc_ispullup)
266 1.16 simonb tc_dmar = PMAZ_DMA_ADDR(asc->sc_bounce);
267 1.16 simonb else
268 1.16 simonb tc_dmar = PMAZ_DMAR_WRITE | PMAZ_DMA_ADDR(asc->sc_bounce);
269 1.16 simonb bus_space_write_4(asc->sc_bst, asc->sc_bsh, PMAZ_OFFSET_DMAR, tc_dmar);
270 1.16 simonb asc->sc_active = 1;
271 1.16 simonb #endif
272 1.16 simonb return (0);
273 1.16 simonb }
274 1.16 simonb
275 1.16 simonb static void
276 1.28 thorpej asc_tc_go(struct ncr53c9x_softc *sc)
277 1.16 simonb {
278 1.16 simonb #if 0
279 1.16 simonb struct asc_softc *asc = (struct asc_softc *)sc;
280 1.16 simonb u_int32_t tc_dmar;
281 1.1 jonathan
282 1.16 simonb if (asc->sc_ispullup)
283 1.16 simonb tc_dmar = PMAZ_DMA_ADDR(asc->sc_bounce);
284 1.16 simonb else
285 1.16 simonb tc_dmar = PMAZ_DMAR_WRITE | PMAZ_DMA_ADDR(asc->sc_bounce);
286 1.16 simonb bus_space_write_4(asc->sc_bst, asc->sc_bsh, PMAZ_OFFSET_DMAR, tc_dmar);
287 1.16 simonb asc->sc_active = 1;
288 1.16 simonb #endif
289 1.1 jonathan }
290 1.1 jonathan
291 1.16 simonb /* NEVER CALLED BY MI 53C9x ENGINE INDEED */
292 1.16 simonb static void
293 1.28 thorpej asc_tc_stop(struct ncr53c9x_softc *sc)
294 1.16 simonb {
295 1.16 simonb #if 0
296 1.16 simonb struct asc_softc *asc = (struct asc_softc *)sc;
297 1.16 simonb
298 1.16 simonb if (asc->sc_ispullup)
299 1.16 simonb memcpy(asc->sc_target, asc->sc_bounce, asc->sc_dmasize);
300 1.16 simonb asc->sc_active = 0;
301 1.16 simonb #endif
302 1.16 simonb }
303 1.1 jonathan
304 1.1 jonathan /*
305 1.16 simonb * Glue functions.
306 1.1 jonathan */
307 1.16 simonb static u_char
308 1.28 thorpej asc_read_reg(struct ncr53c9x_softc *sc, int reg)
309 1.16 simonb {
310 1.16 simonb struct asc_softc *asc = (struct asc_softc *)sc;
311 1.16 simonb u_char v;
312 1.16 simonb
313 1.16 simonb v = bus_space_read_4(asc->sc_bst, asc->sc_bsh,
314 1.16 simonb reg * sizeof(u_int32_t)) & 0xff;
315 1.16 simonb
316 1.16 simonb return (v);
317 1.16 simonb }
318 1.16 simonb
319 1.16 simonb static void
320 1.28 thorpej asc_write_reg(struct ncr53c9x_softc *sc, int reg, u_char val)
321 1.16 simonb {
322 1.16 simonb struct asc_softc *asc = (struct asc_softc *)sc;
323 1.16 simonb
324 1.16 simonb bus_space_write_4(asc->sc_bst, asc->sc_bsh,
325 1.16 simonb reg * sizeof(u_int32_t), val);
326 1.16 simonb }
327 1.16 simonb
328 1.16 simonb static int
329 1.28 thorpej asc_dma_isintr(struct ncr53c9x_softc *sc)
330 1.16 simonb {
331 1.16 simonb return !!(NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT);
332 1.16 simonb }
333 1.16 simonb
334 1.7 mhitch static int
335 1.28 thorpej asc_dma_isactive(struct ncr53c9x_softc *sc)
336 1.1 jonathan {
337 1.16 simonb struct asc_softc *asc = (struct asc_softc *)sc;
338 1.1 jonathan
339 1.16 simonb return (asc->sc_active);
340 1.1 jonathan }
341 1.1 jonathan
342 1.1 jonathan static void
343 1.28 thorpej asc_clear_latched_intr(struct ncr53c9x_softc *sc)
344 1.1 jonathan {
345 1.1 jonathan }
346