sec.c revision 1.4.4.2 1 1.4.4.2 ad /* $NetBSD: sec.c,v 1.4.4.2 2006/11/18 21:34:46 ad Exp $ */
2 1.4.4.2 ad
3 1.4.4.2 ad /*-
4 1.4.4.2 ad * Copyright (c) 2000, 2001, 2006 Ben Harris
5 1.4.4.2 ad * All rights reserved.
6 1.4.4.2 ad *
7 1.4.4.2 ad * Redistribution and use in source and binary forms, with or without
8 1.4.4.2 ad * modification, are permitted provided that the following conditions
9 1.4.4.2 ad * are met:
10 1.4.4.2 ad * 1. Redistributions of source code must retain the above copyright
11 1.4.4.2 ad * notice, this list of conditions and the following disclaimer.
12 1.4.4.2 ad * 2. Redistributions in binary form must reproduce the above copyright
13 1.4.4.2 ad * notice, this list of conditions and the following disclaimer in the
14 1.4.4.2 ad * documentation and/or other materials provided with the distribution.
15 1.4.4.2 ad * 3. The name of the author may not be used to endorse or promote products
16 1.4.4.2 ad * derived from this software without specific prior written permission.
17 1.4.4.2 ad *
18 1.4.4.2 ad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.4.4.2 ad * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.4.4.2 ad * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.4.4.2 ad * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.4.4.2 ad * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.4.4.2 ad * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.4.4.2 ad * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.4.4.2 ad * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.4.4.2 ad * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.4.4.2 ad * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.4.4.2 ad */
29 1.4.4.2 ad /*
30 1.4.4.2 ad * sec.c -- driver for Acorn SCSI expansion cards (AKA30, AKA31, AKA32)
31 1.4.4.2 ad *
32 1.4.4.2 ad * These cards are documented in:
33 1.4.4.2 ad * Acorn Archimedes 500 series / Acorn R200 series Technical Reference Manual
34 1.4.4.2 ad * Published by Acorn Computers Limited
35 1.4.4.2 ad * ISBN 1 85250 086 7
36 1.4.4.2 ad * Part number 0486,052
37 1.4.4.2 ad * Issue 1, November 1990
38 1.4.4.2 ad */
39 1.4.4.2 ad
40 1.4.4.2 ad #include <sys/cdefs.h>
41 1.4.4.2 ad __KERNEL_RCSID(0, "$NetBSD: sec.c,v 1.4.4.2 2006/11/18 21:34:46 ad Exp $");
42 1.4.4.2 ad
43 1.4.4.2 ad #include <sys/param.h>
44 1.4.4.2 ad
45 1.4.4.2 ad #include <sys/buf.h>
46 1.4.4.2 ad #include <sys/device.h>
47 1.4.4.2 ad #include <sys/malloc.h>
48 1.4.4.2 ad #include <sys/reboot.h> /* For bootverbose */
49 1.4.4.2 ad #include <sys/syslog.h>
50 1.4.4.2 ad #include <sys/systm.h>
51 1.4.4.2 ad
52 1.4.4.2 ad #include <dev/scsipi/scsi_all.h>
53 1.4.4.2 ad #include <dev/scsipi/scsipi_all.h>
54 1.4.4.2 ad #include <dev/scsipi/scsiconf.h>
55 1.4.4.2 ad
56 1.4.4.2 ad #include <machine/bus.h>
57 1.4.4.2 ad
58 1.4.4.2 ad #include <dev/ic/wd33c93reg.h>
59 1.4.4.2 ad #include <dev/ic/wd33c93var.h>
60 1.4.4.2 ad #include <dev/ic/nec71071reg.h>
61 1.4.4.2 ad
62 1.4.4.2 ad #include <dev/podulebus/podulebus.h>
63 1.4.4.2 ad #include <dev/podulebus/podules.h>
64 1.4.4.2 ad #include <dev/podulebus/powerromreg.h>
65 1.4.4.2 ad #include <dev/podulebus/secreg.h>
66 1.4.4.2 ad
67 1.4.4.2 ad #include "opt_ddb.h"
68 1.4.4.2 ad
69 1.4.4.2 ad struct sec_softc {
70 1.4.4.2 ad struct wd33c93_softc sc_sbic;
71 1.4.4.2 ad bus_space_tag_t sc_pod_t;
72 1.4.4.2 ad bus_space_handle_t sc_pod_h;
73 1.4.4.2 ad bus_space_tag_t sc_mod_t;
74 1.4.4.2 ad bus_space_handle_t sc_mod_h;
75 1.4.4.2 ad void *sc_ih;
76 1.4.4.2 ad struct evcnt sc_intrcnt;
77 1.4.4.2 ad uint8_t sc_mpr;
78 1.4.4.2 ad
79 1.4.4.2 ad /* Details of the current DMA transfer */
80 1.4.4.2 ad boolean_t sc_dmaactive;
81 1.4.4.2 ad caddr_t sc_dmaaddr;
82 1.4.4.2 ad int sc_dmaoff;
83 1.4.4.2 ad size_t sc_dmalen;
84 1.4.4.2 ad boolean_t sc_dmain;
85 1.4.4.2 ad /* Details of the current block within the above transfer */
86 1.4.4.2 ad size_t sc_dmablk;
87 1.4.4.2 ad };
88 1.4.4.2 ad
89 1.4.4.2 ad #define SEC_DMABLK 16384
90 1.4.4.2 ad #define SEC_NBLKS 3
91 1.4.4.2 ad #define SEC_DMAMODE MODE_TMODE_DMD
92 1.4.4.2 ad
93 1.4.4.2 ad /* autoconfiguration glue */
94 1.4.4.2 ad static int sec_match(struct device *, struct cfdata *, void *);
95 1.4.4.2 ad static void sec_attach(struct device *, struct device *, void *);
96 1.4.4.2 ad
97 1.4.4.2 ad /* shutdown hook */
98 1.4.4.2 ad static void sec_shutdown(void *);
99 1.4.4.2 ad
100 1.4.4.2 ad /* callbacks from MI WD33C93 driver */
101 1.4.4.2 ad static int sec_dmasetup(struct wd33c93_softc *, caddr_t *, size_t *, int,
102 1.4.4.2 ad size_t *);
103 1.4.4.2 ad static int sec_dmago(struct wd33c93_softc *);
104 1.4.4.2 ad static void sec_dmastop(struct wd33c93_softc *);
105 1.4.4.2 ad static void sec_reset(struct wd33c93_softc *);
106 1.4.4.2 ad
107 1.4.4.2 ad static int sec_intr(void *);
108 1.4.4.2 ad static int sec_dmatc(struct sec_softc *sc);
109 1.4.4.2 ad
110 1.4.4.2 ad void sec_dumpdma(void *arg);
111 1.4.4.2 ad
112 1.4.4.2 ad CFATTACH_DECL(sec, sizeof(struct sec_softc),
113 1.4.4.2 ad sec_match, sec_attach, NULL, NULL);
114 1.4.4.2 ad
115 1.4.4.2 ad static inline void
116 1.4.4.2 ad sec_setpage(struct sec_softc *sc, int page)
117 1.4.4.2 ad {
118 1.4.4.2 ad
119 1.4.4.2 ad sc->sc_mpr = (sc->sc_mpr & ~SEC_MPR_PAGE) | page;
120 1.4.4.2 ad bus_space_write_1(sc->sc_pod_t, sc->sc_pod_h, SEC_MPR, sc->sc_mpr);
121 1.4.4.2 ad }
122 1.4.4.2 ad
123 1.4.4.2 ad static inline void
124 1.4.4.2 ad sec_cli(struct sec_softc *sc)
125 1.4.4.2 ad {
126 1.4.4.2 ad
127 1.4.4.2 ad bus_space_write_1(sc->sc_pod_t, sc->sc_pod_h, SEC_CLRINT, 0);
128 1.4.4.2 ad }
129 1.4.4.2 ad
130 1.4.4.2 ad static inline void
131 1.4.4.2 ad dmac_write(struct sec_softc *sc, int reg, uint8_t val)
132 1.4.4.2 ad {
133 1.4.4.2 ad
134 1.4.4.2 ad bus_space_write_1(sc->sc_mod_t, sc->sc_mod_h,
135 1.4.4.2 ad SEC_DMAC + DMAC(reg), val);
136 1.4.4.2 ad }
137 1.4.4.2 ad
138 1.4.4.2 ad static inline uint8_t
139 1.4.4.2 ad dmac_read(struct sec_softc *sc, int reg)
140 1.4.4.2 ad {
141 1.4.4.2 ad
142 1.4.4.2 ad return bus_space_read_1(sc->sc_mod_t, sc->sc_mod_h,
143 1.4.4.2 ad SEC_DMAC + DMAC(reg));
144 1.4.4.2 ad }
145 1.4.4.2 ad
146 1.4.4.2 ad static int
147 1.4.4.2 ad sec_match(struct device *parent, struct cfdata *cf, void *aux)
148 1.4.4.2 ad {
149 1.4.4.2 ad struct podulebus_attach_args *pa = aux;
150 1.4.4.2 ad
151 1.4.4.2 ad /* Standard ROM, skipping the MCS card that used the same ID. */
152 1.4.4.2 ad if (pa->pa_product == PODULE_ACORN_SCSI &&
153 1.4.4.2 ad strncmp(pa->pa_descr, "MCS", 3) != 0)
154 1.4.4.2 ad return 1;
155 1.4.4.2 ad
156 1.4.4.2 ad /* PowerROM */
157 1.4.4.2 ad if (pa->pa_product == PODULE_ALSYSTEMS_SCSI &&
158 1.4.4.2 ad podulebus_initloader(pa) == 0 &&
159 1.4.4.2 ad podloader_callloader(pa, 0, 0) == PRID_ACORN_SCSI1)
160 1.4.4.2 ad return 1;
161 1.4.4.2 ad
162 1.4.4.2 ad return 0;
163 1.4.4.2 ad }
164 1.4.4.2 ad
165 1.4.4.2 ad static void
166 1.4.4.2 ad sec_attach(struct device *parent, struct device *self, void *aux)
167 1.4.4.2 ad {
168 1.4.4.2 ad struct podulebus_attach_args *pa = aux;
169 1.4.4.2 ad struct sec_softc *sc = device_private(self);
170 1.4.4.2 ad int i;
171 1.4.4.2 ad
172 1.4.4.2 ad /* Set up bus spaces */
173 1.4.4.2 ad sc->sc_pod_t = pa->pa_fast_t;
174 1.4.4.2 ad bus_space_map(pa->pa_fast_t, pa->pa_fast_base, 0x1000, 0,
175 1.4.4.2 ad &sc->sc_pod_h);
176 1.4.4.2 ad sc->sc_mod_t = pa->pa_mod_t;
177 1.4.4.2 ad bus_space_map(pa->pa_mod_t, pa->pa_mod_base, 0x1000, 0,
178 1.4.4.2 ad &sc->sc_mod_h);
179 1.4.4.2 ad
180 1.4.4.2 ad sc->sc_sbic.sc_regt = sc->sc_mod_t;
181 1.4.4.2 ad bus_space_subregion(sc->sc_mod_t, sc->sc_mod_h, SEC_SBIC,
182 1.4.4.2 ad 0x1000 - SEC_SBIC, &sc->sc_sbic.sc_regh);
183 1.4.4.2 ad
184 1.4.4.2 ad sc->sc_sbic.sc_id = 7;
185 1.4.4.2 ad sc->sc_sbic.sc_clkfreq = SEC_CLKFREQ;
186 1.4.4.2 ad sc->sc_sbic.sc_dmamode = SBIC_CTL_BURST_DMA;
187 1.4.4.2 ad
188 1.4.4.2 ad sc->sc_sbic.sc_adapter.adapt_request = wd33c93_scsi_request;
189 1.4.4.2 ad sc->sc_sbic.sc_adapter.adapt_minphys = minphys;
190 1.4.4.2 ad
191 1.4.4.2 ad sc->sc_sbic.sc_dmasetup = sec_dmasetup;
192 1.4.4.2 ad sc->sc_sbic.sc_dmago = sec_dmago;
193 1.4.4.2 ad sc->sc_sbic.sc_dmastop = sec_dmastop;
194 1.4.4.2 ad sc->sc_sbic.sc_reset = sec_reset;
195 1.4.4.2 ad
196 1.4.4.2 ad sc->sc_mpr = 0;
197 1.4.4.2 ad bus_space_write_1(sc->sc_pod_t, sc->sc_pod_h, SEC_MPR, sc->sc_mpr);
198 1.4.4.2 ad
199 1.4.4.2 ad for (i = 0; i < SEC_NPAGES; i++) {
200 1.4.4.2 ad sec_setpage(sc, i);
201 1.4.4.2 ad bus_space_set_region_2(sc->sc_mod_t, sc->sc_mod_h,
202 1.4.4.2 ad SEC_SRAM, 0, SEC_PAGESIZE / 2);
203 1.4.4.2 ad }
204 1.4.4.2 ad
205 1.4.4.2 ad wd33c93_attach(&sc->sc_sbic);
206 1.4.4.2 ad
207 1.4.4.2 ad evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
208 1.4.4.2 ad self->dv_xname, "intr");
209 1.4.4.2 ad sc->sc_ih = podulebus_irq_establish(pa->pa_ih, IPL_BIO, sec_intr,
210 1.4.4.2 ad sc, &sc->sc_intrcnt);
211 1.4.4.2 ad sec_cli(sc);
212 1.4.4.2 ad sc->sc_mpr |= SEC_MPR_IE;
213 1.4.4.2 ad bus_space_write_1(sc->sc_pod_t, sc->sc_pod_h, SEC_MPR, sc->sc_mpr);
214 1.4.4.2 ad shutdownhook_establish(sec_shutdown, sc);
215 1.4.4.2 ad }
216 1.4.4.2 ad
217 1.4.4.2 ad /*
218 1.4.4.2 ad * Before reboot, reset the page register to 0 so that RISC OS can see
219 1.4.4.2 ad * the podule ROM.
220 1.4.4.2 ad */
221 1.4.4.2 ad static void
222 1.4.4.2 ad sec_shutdown(void *cookie)
223 1.4.4.2 ad {
224 1.4.4.2 ad struct sec_softc *sc = cookie;
225 1.4.4.2 ad
226 1.4.4.2 ad sec_setpage(sc, 0);
227 1.4.4.2 ad }
228 1.4.4.2 ad
229 1.4.4.2 ad static void
230 1.4.4.2 ad sec_copyin(struct sec_softc *sc, void *dest, int src, size_t size)
231 1.4.4.2 ad {
232 1.4.4.2 ad uint16_t tmp, *wptr;
233 1.4.4.2 ad int cnt, extra_byte;
234 1.4.4.2 ad
235 1.4.4.2 ad KASSERT(src >= 0);
236 1.4.4.2 ad KASSERT(src + size <= SEC_MEMSIZE);
237 1.4.4.2 ad if (src % 2 != 0) {
238 1.4.4.2 ad /*
239 1.4.4.2 ad * There's a stray byte at the start. Read the word
240 1.4.4.2 ad * containing it.
241 1.4.4.2 ad */
242 1.4.4.2 ad sec_setpage(sc, src / SEC_PAGESIZE);
243 1.4.4.2 ad tmp = bus_space_read_2(sc->sc_mod_t, sc->sc_mod_h,
244 1.4.4.2 ad SEC_SRAM + (src % SEC_PAGESIZE / 2));
245 1.4.4.2 ad *(uint8_t *)dest = tmp >> 8;
246 1.4.4.2 ad dest = ((uint8_t *)dest) + 1;
247 1.4.4.2 ad src++; size--;
248 1.4.4.2 ad }
249 1.4.4.2 ad KASSERT(src % 2 == 0);
250 1.4.4.2 ad KASSERT(ALIGNED_POINTER(dest, uint16_t));
251 1.4.4.2 ad wptr = dest;
252 1.4.4.2 ad extra_byte = size % 2;
253 1.4.4.2 ad size -= extra_byte;
254 1.4.4.2 ad while (size > 0) {
255 1.4.4.2 ad cnt = SEC_PAGESIZE - src % SEC_PAGESIZE;
256 1.4.4.2 ad if (cnt > size)
257 1.4.4.2 ad cnt = size;
258 1.4.4.2 ad sec_setpage(sc, src / SEC_PAGESIZE);
259 1.4.4.2 ad /* bus ops are in words */
260 1.4.4.2 ad bus_space_read_region_2(sc->sc_mod_t, sc->sc_mod_h,
261 1.4.4.2 ad SEC_SRAM + src % SEC_PAGESIZE / 2, wptr, cnt / 2);
262 1.4.4.2 ad src += cnt;
263 1.4.4.2 ad wptr += cnt / 2;
264 1.4.4.2 ad size -= cnt;
265 1.4.4.2 ad }
266 1.4.4.2 ad if (extra_byte) {
267 1.4.4.2 ad sec_setpage(sc, src / SEC_PAGESIZE);
268 1.4.4.2 ad *(u_int8_t *)wptr =
269 1.4.4.2 ad bus_space_read_2(sc->sc_mod_t, sc->sc_mod_h,
270 1.4.4.2 ad SEC_SRAM + src % SEC_PAGESIZE / 2) & 0xff;
271 1.4.4.2 ad }
272 1.4.4.2 ad }
273 1.4.4.2 ad
274 1.4.4.2 ad static void
275 1.4.4.2 ad sec_copyout(struct sec_softc *sc, const void *src, int dest, size_t size)
276 1.4.4.2 ad {
277 1.4.4.2 ad int cnt, extra_byte;
278 1.4.4.2 ad const uint16_t *wptr;
279 1.4.4.2 ad uint16_t tmp;
280 1.4.4.2 ad
281 1.4.4.2 ad KASSERT(dest >= 0);
282 1.4.4.2 ad KASSERT(dest + size <= SEC_MEMSIZE);
283 1.4.4.2 ad if (dest % 2 != 0) {
284 1.4.4.2 ad /*
285 1.4.4.2 ad * There's a stray byte at the start. Read the word
286 1.4.4.2 ad * containing it.
287 1.4.4.2 ad */
288 1.4.4.2 ad sec_setpage(sc, dest / SEC_PAGESIZE);
289 1.4.4.2 ad tmp = bus_space_read_2(sc->sc_mod_t, sc->sc_mod_h,
290 1.4.4.2 ad SEC_SRAM + (dest % SEC_PAGESIZE / 2));
291 1.4.4.2 ad tmp &= 0xff;
292 1.4.4.2 ad tmp |= *(uint8_t const *)src << 8;
293 1.4.4.2 ad bus_space_write_2(sc->sc_mod_t, sc->sc_mod_h,
294 1.4.4.2 ad SEC_SRAM + (dest % SEC_PAGESIZE / 2), tmp);
295 1.4.4.2 ad src = ((uint8_t const *)src) + 1;
296 1.4.4.2 ad dest++; size--;
297 1.4.4.2 ad }
298 1.4.4.2 ad KASSERT(dest % 2 == 0);
299 1.4.4.2 ad KASSERT(ALIGNED_POINTER(src, uint16_t));
300 1.4.4.2 ad wptr = src;
301 1.4.4.2 ad extra_byte = size % 2;
302 1.4.4.2 ad size -= extra_byte;
303 1.4.4.2 ad while (size > 0) {
304 1.4.4.2 ad cnt = SEC_PAGESIZE - dest % SEC_PAGESIZE;
305 1.4.4.2 ad if (cnt > size)
306 1.4.4.2 ad cnt = size;
307 1.4.4.2 ad sec_setpage(sc, dest / SEC_PAGESIZE);
308 1.4.4.2 ad /* bus ops are in words */
309 1.4.4.2 ad bus_space_write_region_2(sc->sc_mod_t, sc->sc_mod_h,
310 1.4.4.2 ad dest % SEC_PAGESIZE / 2, wptr, cnt / 2);
311 1.4.4.2 ad wptr += cnt / 2;
312 1.4.4.2 ad dest += cnt;
313 1.4.4.2 ad size -= cnt;
314 1.4.4.2 ad }
315 1.4.4.2 ad if (extra_byte) {
316 1.4.4.2 ad /*
317 1.4.4.2 ad * There's a stray byte at the end. Read the word
318 1.4.4.2 ad * containing it.
319 1.4.4.2 ad */
320 1.4.4.2 ad sec_setpage(sc, dest / SEC_PAGESIZE);
321 1.4.4.2 ad tmp = bus_space_read_2(sc->sc_mod_t, sc->sc_mod_h,
322 1.4.4.2 ad SEC_SRAM + (dest % SEC_PAGESIZE / 2));
323 1.4.4.2 ad tmp &= 0xff00;
324 1.4.4.2 ad tmp |= *(uint8_t const *)wptr;
325 1.4.4.2 ad bus_space_write_2(sc->sc_mod_t, sc->sc_mod_h,
326 1.4.4.2 ad SEC_SRAM + (dest % SEC_PAGESIZE / 2), tmp);
327 1.4.4.2 ad }
328 1.4.4.2 ad }
329 1.4.4.2 ad
330 1.4.4.2 ad static void
331 1.4.4.2 ad sec_dmablk(struct sec_softc *sc, int blk)
332 1.4.4.2 ad {
333 1.4.4.2 ad int off;
334 1.4.4.2 ad size_t len;
335 1.4.4.2 ad
336 1.4.4.2 ad KASSERT(blk >= 0);
337 1.4.4.2 ad KASSERT(blk * SEC_DMABLK < sc->sc_dmalen);
338 1.4.4.2 ad off = (blk % SEC_NBLKS) * SEC_DMABLK + sc->sc_dmaoff;
339 1.4.4.2 ad len = MIN(SEC_DMABLK, sc->sc_dmalen - (blk * SEC_DMABLK));
340 1.4.4.2 ad dmac_write(sc, NEC71071_ADDRLO, off & 0xff);
341 1.4.4.2 ad dmac_write(sc, NEC71071_ADDRMID, off >> 8);
342 1.4.4.2 ad dmac_write(sc, NEC71071_ADDRHI, 0);
343 1.4.4.2 ad /*
344 1.4.4.2 ad * "Note: The number of DMA transfer cycles is actually the
345 1.4.4.2 ad * value of the current count register + 1. Therefore, when
346 1.4.4.2 ad * programming the count register, specify the number of DMA
347 1.4.4.2 ad * transfers minus one." -- uPD71071 datasheet
348 1.4.4.2 ad */
349 1.4.4.2 ad dmac_write(sc, NEC71071_COUNTLO, (len - 1) & 0xff);
350 1.4.4.2 ad dmac_write(sc, NEC71071_COUNTHI, (len - 1) >> 8);
351 1.4.4.2 ad }
352 1.4.4.2 ad
353 1.4.4.2 ad static void
354 1.4.4.2 ad sec_copyoutblk(struct sec_softc *sc, int blk)
355 1.4.4.2 ad {
356 1.4.4.2 ad int off;
357 1.4.4.2 ad size_t len;
358 1.4.4.2 ad
359 1.4.4.2 ad KASSERT(blk >= 0);
360 1.4.4.2 ad KASSERT(blk * SEC_DMABLK < sc->sc_dmalen);
361 1.4.4.2 ad KASSERT(!sc->sc_dmain);
362 1.4.4.2 ad off = (blk % SEC_NBLKS) * SEC_DMABLK + sc->sc_dmaoff;
363 1.4.4.2 ad len = MIN(SEC_DMABLK, sc->sc_dmalen - (blk * SEC_DMABLK));
364 1.4.4.2 ad sec_copyout(sc, sc->sc_dmaaddr + (blk * SEC_DMABLK), off, len);
365 1.4.4.2 ad }
366 1.4.4.2 ad
367 1.4.4.2 ad static void
368 1.4.4.2 ad sec_copyinblk(struct sec_softc *sc, int blk)
369 1.4.4.2 ad {
370 1.4.4.2 ad int off;
371 1.4.4.2 ad size_t len;
372 1.4.4.2 ad
373 1.4.4.2 ad KASSERT(blk >= 0);
374 1.4.4.2 ad KASSERT(blk * SEC_DMABLK < sc->sc_dmalen);
375 1.4.4.2 ad KASSERT(sc->sc_dmain);
376 1.4.4.2 ad off = (blk % SEC_NBLKS) * SEC_DMABLK + sc->sc_dmaoff;
377 1.4.4.2 ad len = MIN(SEC_DMABLK, sc->sc_dmalen - (blk * SEC_DMABLK));
378 1.4.4.2 ad sec_copyin(sc, sc->sc_dmaaddr + (blk * SEC_DMABLK), off, len);
379 1.4.4.2 ad }
380 1.4.4.2 ad
381 1.4.4.2 ad static int
382 1.4.4.2 ad sec_dmasetup(struct wd33c93_softc *sc_sbic, caddr_t *addr, size_t *len,
383 1.4.4.2 ad int datain, size_t *dmasize)
384 1.4.4.2 ad {
385 1.4.4.2 ad struct sec_softc *sc = (struct sec_softc *)sc_sbic;
386 1.4.4.2 ad uint8_t mode;
387 1.4.4.2 ad
388 1.4.4.2 ad sc->sc_dmaaddr = *addr;
389 1.4.4.2 ad sc->sc_dmaoff = ALIGNED_POINTER(*addr, uint16_t) ? 0 : 1;
390 1.4.4.2 ad sc->sc_dmalen = *len;
391 1.4.4.2 ad sc->sc_dmain = datain;
392 1.4.4.2 ad sc->sc_dmablk = 0;
393 1.4.4.2 ad mode = SEC_DMAMODE | (datain ? MODE_TDIR_IOTM : MODE_TDIR_MTIO);
394 1.4.4.2 ad /* Program first block into DMAC and queue up second. */
395 1.4.4.2 ad dmac_write(sc, NEC71071_CHANNEL, 0);
396 1.4.4.2 ad if (!sc->sc_dmain)
397 1.4.4.2 ad sec_copyoutblk(sc, 0);
398 1.4.4.2 ad sec_dmablk(sc, 0);
399 1.4.4.2 ad /* Mode control register */
400 1.4.4.2 ad dmac_write(sc, NEC71071_MODE, mode);
401 1.4.4.2 ad return sc->sc_dmalen;
402 1.4.4.2 ad }
403 1.4.4.2 ad
404 1.4.4.2 ad static int
405 1.4.4.2 ad sec_dmago(struct wd33c93_softc *sc_sbic)
406 1.4.4.2 ad {
407 1.4.4.2 ad struct sec_softc *sc = (struct sec_softc *)sc_sbic;
408 1.4.4.2 ad
409 1.4.4.2 ad dmac_write(sc, NEC71071_MASK, 0xe);
410 1.4.4.2 ad sc->sc_dmaactive = TRUE;
411 1.4.4.2 ad if (!sc->sc_dmain && sc->sc_dmalen > SEC_DMABLK)
412 1.4.4.2 ad sec_copyoutblk(sc, 1);
413 1.4.4.2 ad return sc->sc_dmalen;
414 1.4.4.2 ad }
415 1.4.4.2 ad
416 1.4.4.2 ad static void
417 1.4.4.2 ad sec_dmastop(struct wd33c93_softc *sc_sbic)
418 1.4.4.2 ad {
419 1.4.4.2 ad struct sec_softc *sc = (struct sec_softc *)sc_sbic;
420 1.4.4.2 ad
421 1.4.4.2 ad dmac_write(sc, NEC71071_MASK, 0xf);
422 1.4.4.2 ad if (sc->sc_dmaactive && sc->sc_dmain)
423 1.4.4.2 ad sec_copyinblk(sc, sc->sc_dmablk);
424 1.4.4.2 ad sc->sc_dmaactive = FALSE;
425 1.4.4.2 ad }
426 1.4.4.2 ad
427 1.4.4.2 ad /*
428 1.4.4.2 ad * Reset the SCSI bus, and incidentally the SBIC and DMAC.
429 1.4.4.2 ad */
430 1.4.4.2 ad static void
431 1.4.4.2 ad sec_reset(struct wd33c93_softc *sc_sbic)
432 1.4.4.2 ad {
433 1.4.4.2 ad struct sec_softc *sc = (struct sec_softc *)sc_sbic;
434 1.4.4.2 ad uint8_t asr, csr;
435 1.4.4.2 ad
436 1.4.4.2 ad bus_space_write_1(sc->sc_pod_t, sc->sc_pod_h, SEC_MPR,
437 1.4.4.2 ad sc->sc_mpr | SEC_MPR_UR);
438 1.4.4.2 ad DELAY(7);
439 1.4.4.2 ad bus_space_write_1(sc->sc_pod_t, sc->sc_pod_h, SEC_MPR, sc->sc_mpr);
440 1.4.4.2 ad /* Wait for and clear the reset-complete interrupt */
441 1.4.4.2 ad do
442 1.4.4.2 ad GET_SBIC_asr(sc_sbic, asr);
443 1.4.4.2 ad while (!(asr & SBIC_ASR_INT));
444 1.4.4.2 ad GET_SBIC_csr(sc_sbic, csr);
445 1.4.4.2 ad dmac_write(sc, NEC71071_DCTRL1, DCTRL1_CMP | DCTRL1_RQL);
446 1.4.4.2 ad dmac_write(sc, NEC71071_DCTRL2, 0);
447 1.4.4.2 ad sec_cli(sc);
448 1.4.4.2 ad }
449 1.4.4.2 ad
450 1.4.4.2 ad static int
451 1.4.4.2 ad sec_intr(void *arg)
452 1.4.4.2 ad {
453 1.4.4.2 ad struct sec_softc *sc = arg;
454 1.4.4.2 ad u_int8_t isr;
455 1.4.4.2 ad
456 1.4.4.2 ad isr = bus_space_read_1(sc->sc_pod_t, sc->sc_pod_h, SEC_ISR);
457 1.4.4.2 ad if (!(isr & SEC_ISR_IRQ))
458 1.4.4.2 ad return 0;
459 1.4.4.2 ad if (isr & SEC_ISR_DMAC)
460 1.4.4.2 ad sec_dmatc(sc);
461 1.4.4.2 ad if (isr & SEC_ISR_SBIC)
462 1.4.4.2 ad wd33c93_intr(&sc->sc_sbic);
463 1.4.4.2 ad return 1;
464 1.4.4.2 ad }
465 1.4.4.2 ad
466 1.4.4.2 ad static int
467 1.4.4.2 ad sec_dmatc(struct sec_softc *sc)
468 1.4.4.2 ad {
469 1.4.4.2 ad
470 1.4.4.2 ad sec_cli(sc);
471 1.4.4.2 ad /* DMAC finished block n-1 and is now working on block n */
472 1.4.4.2 ad sc->sc_dmablk++;
473 1.4.4.2 ad if (sc->sc_dmalen > sc->sc_dmablk * SEC_DMABLK) {
474 1.4.4.2 ad dmac_write(sc, NEC71071_CHANNEL, 0);
475 1.4.4.2 ad sec_dmablk(sc, sc->sc_dmablk);
476 1.4.4.2 ad dmac_write(sc, NEC71071_MASK, 0xe);
477 1.4.4.2 ad if (!sc->sc_dmain &&
478 1.4.4.2 ad sc->sc_dmalen > (sc->sc_dmablk + 1) * SEC_DMABLK)
479 1.4.4.2 ad sec_copyoutblk(sc, sc->sc_dmablk + 1);
480 1.4.4.2 ad } else {
481 1.4.4.2 ad /* All blocks fully processed. */
482 1.4.4.2 ad sc->sc_dmaactive = FALSE;
483 1.4.4.2 ad }
484 1.4.4.2 ad if (sc->sc_dmain)
485 1.4.4.2 ad sec_copyinblk(sc, sc->sc_dmablk - 1);
486 1.4.4.2 ad return 1;
487 1.4.4.2 ad }
488 1.4.4.2 ad
489 1.4.4.2 ad #ifdef DDB
490 1.4.4.2 ad void
491 1.4.4.2 ad sec_dumpdma(void *arg)
492 1.4.4.2 ad {
493 1.4.4.2 ad struct sec_softc *sc = arg;
494 1.4.4.2 ad
495 1.4.4.2 ad dmac_write(sc, NEC71071_CHANNEL, 0);
496 1.4.4.2 ad printf("%s: DMA state: cur count %02x%02x cur addr %02x%02x%02x ",
497 1.4.4.2 ad sc->sc_sbic.sc_dev.dv_xname,
498 1.4.4.2 ad dmac_read(sc, NEC71071_COUNTHI), dmac_read(sc, NEC71071_COUNTLO),
499 1.4.4.2 ad dmac_read(sc, NEC71071_ADDRHI), dmac_read(sc, NEC71071_ADDRMID),
500 1.4.4.2 ad dmac_read(sc, NEC71071_ADDRLO));
501 1.4.4.2 ad dmac_write(sc, NEC71071_CHANNEL, 0 | CHANNEL_WBASE);
502 1.4.4.2 ad printf("base count %02x%02x base addr %02x%02x%02x\n",
503 1.4.4.2 ad dmac_read(sc, NEC71071_COUNTHI), dmac_read(sc, NEC71071_COUNTLO),
504 1.4.4.2 ad dmac_read(sc, NEC71071_ADDRHI), dmac_read(sc, NEC71071_ADDRMID),
505 1.4.4.2 ad dmac_read(sc, NEC71071_ADDRLO));
506 1.4.4.2 ad printf("%s: DMA state: dctrl %1x%02x mode %02x status %02x req %02x "
507 1.4.4.2 ad "mask %02x\n",
508 1.4.4.2 ad sc->sc_sbic.sc_dev.dv_xname, dmac_read(sc, NEC71071_DCTRL2),
509 1.4.4.2 ad dmac_read(sc, NEC71071_DCTRL1), dmac_read(sc, NEC71071_MODE),
510 1.4.4.2 ad dmac_read(sc, NEC71071_STATUS), dmac_read(sc, NEC71071_REQUEST),
511 1.4.4.2 ad dmac_read(sc, NEC71071_MASK));
512 1.4.4.2 ad printf("%s: soft DMA state: %zd@%p%s%d\n", sc->sc_sbic.sc_dev.dv_xname,
513 1.4.4.2 ad sc->sc_dmalen, sc->sc_dmaaddr, sc->sc_dmain ? "<-" : "->",
514 1.4.4.2 ad sc->sc_dmaoff);
515 1.4.4.2 ad }
516 1.4.4.2 ad
517 1.4.4.2 ad void sec_dumpall(void); /* Call from DDB */
518 1.4.4.2 ad
519 1.4.4.2 ad extern struct cfdriver sec_cd;
520 1.4.4.2 ad
521 1.4.4.2 ad void sec_dumpall(void)
522 1.4.4.2 ad {
523 1.4.4.2 ad int i;
524 1.4.4.2 ad
525 1.4.4.2 ad for (i = 0; i < sec_cd.cd_ndevs; ++i)
526 1.4.4.2 ad if (sec_cd.cd_devs[i])
527 1.4.4.2 ad sec_dumpdma(sec_cd.cd_devs[i]);
528 1.4.4.2 ad }
529 1.4.4.2 ad #endif
530