1 /*        $NetBSD: ahsc.c,v 1.40 2021/08/07 16:18:41 thorpej Exp $ */
2 
3 /*
4  * Copyright (c) 1982, 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *        @(#)dma.c
32  */
33 
34 /*
35  * Copyright (c) 1994 Christian E. Hopps
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *        This product includes software developed by the University of
48  *        California, Berkeley and its contributors.
49  * 4. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *        @(#)dma.c
66  */
67 
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: ahsc.c,v 1.40 2021/08/07 16:18:41 thorpej Exp $");
70 
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/kernel.h>
74 #include <sys/device.h>
75 #include <dev/scsipi/scsi_all.h>
76 #include <dev/scsipi/scsipi_all.h>
77 #include <dev/scsipi/scsiconf.h>
78 #include <amiga/amiga/custom.h>
79 #include <amiga/amiga/cc.h>
80 #include <amiga/amiga/cfdev.h>
81 #include <amiga/amiga/device.h>
82 #include <amiga/amiga/isr.h>
83 #include <amiga/dev/dmavar.h>
84 #include <amiga/dev/sbicreg.h>
85 #include <amiga/dev/sbicvar.h>
86 #include <amiga/dev/ahscreg.h>
87 #include <amiga/dev/zbusvar.h>
88 
89 #include <machine/cpu.h>
90 
91 void ahscattach(device_t, device_t, void *);
92 int ahscmatch(device_t, cfdata_t, void *);
93 
94 void ahsc_enintr(struct sbic_softc *);
95 void ahsc_dmastop(struct sbic_softc *);
96 int ahsc_dmanext(struct sbic_softc *);
97 int ahsc_dmaintr(void *);
98 int ahsc_dmago(struct sbic_softc *, char *, int, int);
99 
100 #ifdef DEBUG
101 void ahsc_dump(void);
102 #endif
103 
104 #ifdef DEBUG
105 int       ahsc_dmadebug = 0;
106 #endif
107 
108 CFATTACH_DECL_NEW(ahsc, sizeof(struct sbic_softc),
109     ahscmatch, ahscattach, NULL, NULL);
110 
111 /*
112  * if we are an A3000 we are here.
113  */
114 int
ahscmatch(device_t parent,cfdata_t cf,void * aux)115 ahscmatch(device_t parent, cfdata_t cf, void *aux)
116 {
117 
118           if (is_a3000() && matchname(aux, "ahsc"))
119                     return(1);
120           return(0);
121 }
122 
123 void
ahscattach(device_t parent,device_t self,void * aux)124 ahscattach(device_t parent, device_t self, void *aux)
125 {
126           volatile struct sdmac *rp;
127           struct sbic_softc *sc = device_private(self);
128           struct cfdev *cdp, *ecdp;
129           struct scsipi_adapter *adapt = &sc->sc_adapter;
130           struct scsipi_channel *chan = &sc->sc_channel;
131 
132           sc->sc_dev = self;
133 
134           ecdp = &cfdev[ncfdev];
135 
136           for (cdp = cfdev; cdp < ecdp; cdp++) {
137                     if (cdp->rom.manid == 8738 &&
138                         cdp->rom.prodid == 35)
139                                         break;
140           }
141 
142           sc->sc_cregs = rp = ztwomap(0xdd0000);
143           /*
144            * disable ints and reset bank register
145            */
146           rp->CNTR = CNTR_PDMD;
147           amiga_membarrier();
148           rp->DAWR = DAWR_AHSC;
149           amiga_membarrier();
150           sc->sc_enintr = ahsc_enintr;
151           sc->sc_dmago = ahsc_dmago;
152           sc->sc_dmanext = ahsc_dmanext;
153           sc->sc_dmastop = ahsc_dmastop;
154           sc->sc_dmacmd = 0;
155 
156           /*
157            * everything is a valid DMA address
158            */
159           sc->sc_dmamask = 0;
160 
161           if (cdp < ecdp) {
162                     sc->sc_sbic.sbic_asr_p =  ((vu_char *)rp + 0x43);
163                     sc->sc_sbic.sbic_value_p =  ((vu_char *)rp + 0x47);
164                     printf(": modified for Apollo CPU board\n");
165           } else {
166                     sc->sc_sbic.sbic_asr_p =  ((vu_char *)rp + 0x41);
167                     sc->sc_sbic.sbic_value_p =  ((vu_char *)rp + 0x43);
168                     printf("\n");
169           }
170 
171           sc->sc_clkfreq = sbic_clock_override ? sbic_clock_override : 143;
172 
173           /*
174            * Fill in the scsipi_adapter.
175            */
176           memset(adapt, 0, sizeof(*adapt));
177           adapt->adapt_dev = self;
178           adapt->adapt_nchannels = 1;
179           adapt->adapt_openings = 7;
180           adapt->adapt_max_periph = 1;
181           adapt->adapt_request = sbic_scsipi_request;
182           adapt->adapt_minphys = sbic_minphys;
183 
184           /*
185            * Fill in the scsipi_channel.
186            */
187           memset(chan, 0, sizeof(*chan));
188           chan->chan_adapter = adapt;
189           chan->chan_bustype = &scsi_bustype;
190           chan->chan_channel = 0;
191           chan->chan_ntargets = 8;
192           chan->chan_nluns = 8;
193           chan->chan_id = 7;
194 
195           sbicinit(sc);
196 
197           sc->sc_isr.isr_intr = ahsc_dmaintr;
198           sc->sc_isr.isr_arg = sc;
199           sc->sc_isr.isr_ipl = 2;
200           add_isr (&sc->sc_isr);
201 
202           /*
203            * attach all scsi units on us
204            */
205           config_found(self, chan, scsiprint, CFARGS_NONE);
206 }
207 
208 void
ahsc_enintr(struct sbic_softc * dev)209 ahsc_enintr(struct sbic_softc *dev)
210 {
211           volatile struct sdmac *sdp;
212 
213           sdp = dev->sc_cregs;
214 
215           dev->sc_flags |= SBICF_INTR;
216           sdp->CNTR = CNTR_PDMD | CNTR_INTEN;
217           amiga_membarrier();
218 }
219 
220 int
ahsc_dmago(struct sbic_softc * dev,char * addr,int count,int flags)221 ahsc_dmago(struct sbic_softc *dev, char *addr, int count, int flags)
222 {
223           volatile struct sdmac *sdp;
224 
225           sdp = dev->sc_cregs;
226           /*
227            * Set up the command word based on flags
228            */
229           dev->sc_dmacmd = CNTR_PDMD | CNTR_INTEN;
230           if ((flags & DMAGO_READ) == 0)
231                     dev->sc_dmacmd |= CNTR_DDIR;
232 #ifdef DEBUG
233           if (ahsc_dmadebug & DDB_IO)
234                     printf("ahsc_dmago: cmd %x\n", dev->sc_dmacmd);
235 #endif
236 
237           dev->sc_flags |= SBICF_INTR;
238           sdp->CNTR = dev->sc_dmacmd;
239           amiga_membarrier();
240           sdp->ACR = (u_int) dev->sc_cur->dc_addr;
241           amiga_membarrier();
242           sdp->ST_DMA = 1;
243           amiga_membarrier();
244 
245           return(dev->sc_tcnt);
246 }
247 
248 void
ahsc_dmastop(struct sbic_softc * dev)249 ahsc_dmastop(struct sbic_softc *dev)
250 {
251           volatile struct sdmac *sdp;
252           int s;
253           vu_short istr;
254 
255           sdp = dev->sc_cregs;
256 
257 #ifdef DEBUG
258           if (ahsc_dmadebug & DDB_FOLLOW)
259                     printf("ahsc_dmastop()\n");
260 #endif
261           if (dev->sc_dmacmd) {
262                     s = splbio();
263                     if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
264                               /*
265                                * only FLUSH if terminal count not enabled,
266                                * and reading from peripheral
267                                */
268                               sdp->FLUSH = 1;
269                               amiga_membarrier();
270                               do {
271                                         istr = sdp->ISTR;
272                                         amiga_membarrier();
273                               } while ((istr & ISTR_FE_FLG) == 0);
274                     }
275                     /*
276                      * clear possible interrupt and stop DMA
277                      */
278                     sdp->CINT = 1;
279                     amiga_membarrier();
280                     sdp->SP_DMA = 1;
281                     amiga_membarrier();
282                     dev->sc_dmacmd = 0;
283                     splx(s);
284           }
285 }
286 
287 int
ahsc_dmaintr(void * arg)288 ahsc_dmaintr(void *arg)
289 {
290           struct sbic_softc *dev = arg;
291           volatile struct sdmac *sdp;
292           int stat, found;
293 
294           sdp = dev->sc_cregs;
295           stat = sdp->ISTR;
296           amiga_membarrier();
297 
298           if ((stat & (ISTR_INT_F|ISTR_INT_P)) == 0)
299                     return (0);
300 
301 #ifdef DEBUG
302           if (ahsc_dmadebug & DDB_FOLLOW)
303                     printf("%s: dmaintr 0x%x\n", device_xname(dev->sc_dev), stat);
304 #endif
305 
306           /*
307            * both, SCSI and DMA interrupts arrive here. I chose
308            * arbitrarily that DMA interrupts should have higher
309            * precedence than SCSI interrupts.
310            */
311           found = 0;
312           if (stat & ISTR_E_INT) {
313                     ++found;
314 
315                     sdp->CINT = 1;      /* clear possible interrupt */
316                     amiga_membarrier();
317 
318                     /*
319                      * check for SCSI ints in the same go and
320                      * eventually save an interrupt
321                      */
322           }
323 
324           if (dev->sc_flags & SBICF_INTR && stat & ISTR_INTS)
325                     found += sbicintr(dev);
326           return(found);
327 }
328 
329 
330 int
ahsc_dmanext(struct sbic_softc * dev)331 ahsc_dmanext(struct sbic_softc *dev)
332 {
333           volatile struct sdmac *sdp;
334           vu_short istr;
335 
336           sdp = dev->sc_cregs;
337 
338           if (dev->sc_cur > dev->sc_last) {
339                     /* shouldn't happen !! */
340                     printf("ahsc_dmanext at end !!!\n");
341                     ahsc_dmastop(dev);
342                     return(0);
343           }
344           if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
345                       /*
346                        * only FLUSH if terminal count not enabled,
347                        * and reading from peripheral
348                        */
349                     sdp->FLUSH = 1;
350                     amiga_membarrier();
351                     do {
352                               istr = sdp->ISTR;
353                               amiga_membarrier();
354                     } while ((istr & ISTR_FE_FLG) == 0);
355           }
356           /*
357            * clear possible interrupt and stop DMA
358            */
359           sdp->CINT = 1;      /* clear possible interrupt */
360           amiga_membarrier();
361           sdp->SP_DMA = 1;    /* stop DMA */
362           amiga_membarrier();
363           sdp->CNTR = dev->sc_dmacmd;
364           amiga_membarrier();
365           sdp->ACR = (u_int)dev->sc_cur->dc_addr;
366           amiga_membarrier();
367           sdp->ST_DMA = 1;
368           amiga_membarrier();
369 
370           dev->sc_tcnt = dev->sc_cur->dc_count << 1;
371           return(dev->sc_tcnt);
372 }
373 
374 #ifdef DEBUG
375 void
ahsc_dump(void)376 ahsc_dump(void)
377 {
378           extern struct cfdriver ahsc_cd;
379           struct sbic_softc *sc;
380           int i;
381 
382           for (i = 0; i < ahsc_cd.cd_ndevs; ++i) {
383                     sc = device_lookup_private(&ahsc_cd, i);
384                     if (sc != NULL)
385                               sbic_dump(sc);
386           }
387 }
388 #endif
389