1 /*        $NetBSD: dma_sbus.c,v 1.40 2022/09/25 18:03:04 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Paul Kranenburg.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1994 Peter Galbavy.  All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 3. All advertising materials mentioning features or use of this software
44  *    must display the following acknowledgement:
45  *        This product includes software developed by Peter Galbavy.
46  * 4. The name of the author may not be used to endorse or promote products
47  *    derived from this software without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59  */
60 
61 #include <sys/cdefs.h>
62 __KERNEL_RCSID(0, "$NetBSD: dma_sbus.c,v 1.40 2022/09/25 18:03:04 thorpej Exp $");
63 
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/errno.h>
68 #include <sys/device.h>
69 
70 #include <sys/bus.h>
71 #include <sys/intr.h>
72 #include <machine/autoconf.h>
73 
74 #include <dev/sbus/sbusvar.h>
75 
76 #include <dev/ic/lsi64854reg.h>
77 #include <dev/ic/lsi64854var.h>
78 
79 struct dma_softc {
80           struct lsi64854_softc         sc_lsi64854;        /* base device */
81           /* possible sbus specific stuff here */
82 };
83 
84 int       dmamatch_sbus(device_t, cfdata_t, void *);
85 void      dmaattach_sbus(device_t, device_t, void *);
86 
87 int       dmaprint_sbus(void *, const char *);
88 
89 void      *dmabus_intr_establish(
90                     bus_space_tag_t,
91                     int,                          /*bus interrupt priority*/
92                     int,                          /*`device class' level*/
93                     int (*)(void *),    /*handler*/
94                     void *,                       /*handler arg*/
95                     void (*) (void));   /*optional fast trap handler*/
96 
97 CFATTACH_DECL_NEW(dma_sbus, sizeof(struct dma_softc),
98     dmamatch_sbus, dmaattach_sbus, NULL, NULL);
99 
100 CFATTACH_DECL_NEW(ledma, sizeof(struct dma_softc),
101     dmamatch_sbus, dmaattach_sbus, NULL, NULL);
102 
103 int
dmaprint_sbus(void * aux,const char * busname)104 dmaprint_sbus(void *aux, const char *busname)
105 {
106           struct sbus_attach_args *sa = aux;
107           bus_space_tag_t t = sa->sa_bustag;
108           struct dma_softc *sc = t->cookie;
109 
110           sa->sa_bustag = sc->sc_lsi64854.sc_bustag;        /* XXX */
111           sbus_print(aux, busname);     /* XXX */
112           sa->sa_bustag = t;            /* XXX */
113           return UNCONF;
114 }
115 
116 int
dmamatch_sbus(device_t parent,cfdata_t cf,void * aux)117 dmamatch_sbus(device_t parent, cfdata_t cf, void *aux)
118 {
119           struct sbus_attach_args *sa = aux;
120 
121           return strcmp(cf->cf_name, sa->sa_name) == 0 ||
122               strcmp("espdma", sa->sa_name) == 0;
123 }
124 
125 void
dmaattach_sbus(device_t parent,device_t self,void * aux)126 dmaattach_sbus(device_t parent, device_t self, void *aux)
127 {
128           struct dma_softc *dsc = device_private(self);
129           struct lsi64854_softc *sc = &dsc->sc_lsi64854;
130           struct sbus_attach_args *sa = aux;
131           struct sbus_softc *sbsc = device_private(parent);
132           bus_space_tag_t sbt;
133           int sbusburst, burst;
134           int node;
135 
136           node = sa->sa_node;
137 
138           sc->sc_dev = self;
139           sc->sc_bustag = sa->sa_bustag;
140           sc->sc_dmatag = sa->sa_dmatag;
141 
142           /* Map registers */
143           if (sa->sa_npromvaddrs) {
144                     sbus_promaddr_to_handle(sa->sa_bustag,
145                         sa->sa_promvaddrs[0], &sc->sc_regs);
146           } else {
147                     if (sbus_bus_map(sa->sa_bustag,
148                         sa->sa_slot, sa->sa_offset, sa->sa_size,
149                         0, &sc->sc_regs) != 0) {
150                               aprint_error(": cannot map registers\n");
151                               return;
152                     }
153           }
154 
155           /*
156            * Get transfer burst size from PROM and plug it into the
157            * controller registers. This is needed on the Sun4m; do
158            * others need it too?
159            */
160           sbusburst = sbsc->sc_burst;
161           if (sbusburst == 0)
162                     sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
163 
164           burst = prom_getpropint(node,"burst-sizes", -1);
165           if (burst == -1)
166                     /* take SBus burst sizes */
167                     burst = sbusburst;
168 
169           /* Clamp at parent's burst sizes */
170           burst &= sbusburst;
171           sc->sc_burst = (burst & SBUS_BURST_32) ? 32 :
172                            (burst & SBUS_BURST_16) ? 16 : 0;
173 
174           if (device_is_a(self, "ledma")) {
175                     char *cabletype;
176                     uint32_t csr;
177                     /*
178                      * Check to see which cable type is currently active and
179                      * set the appropriate bit in the ledma csr so that it
180                      * gets used. If we didn't netboot, the PROM won't have
181                      * the "cable-selection" property; default to TP and then
182                      * the user can change it via a "media" option to ifconfig.
183                      */
184                     cabletype = prom_getpropstring(node, "cable-selection");
185                     csr = L64854_GCSR(sc);
186                     if (strcmp(cabletype, "tpe") == 0) {
187                               csr |= E_TP_AUI;
188                     } else if (strcmp(cabletype, "aui") == 0) {
189                               csr &= ~E_TP_AUI;
190                     } else {
191                               /* assume TP if nothing there */
192                               csr |= E_TP_AUI;
193                     }
194                     L64854_SCSR(sc, csr);
195                     delay(20000);       /* manual says we need a 20ms delay */
196                     sc->sc_channel = L64854_CHANNEL_ENET;
197           } else {
198                     sc->sc_channel = L64854_CHANNEL_SCSI;
199           }
200 
201           if ((sbt = bus_space_tag_alloc(sc->sc_bustag, dsc)) == NULL) {
202                     aprint_error(": out of memory\n");
203                     return;
204           }
205           sbt->sparc_intr_establish = dmabus_intr_establish;
206           lsi64854_attach(sc);
207 
208           /* Attach children */
209           devhandle_t selfh = device_handle(self);
210           for (node = firstchild(sa->sa_node); node; node = nextsibling(node)) {
211                     struct sbus_attach_args sax;
212                     sbus_setup_attach_args(sbsc, sbt, sc->sc_dmatag, node, &sax);
213                     (void)config_found(self, (void *)&sax, dmaprint_sbus,
214                         CFARGS(.devhandle = prom_node_to_devhandle(selfh, node)));
215                     sbus_destroy_attach_args(&sax);
216           }
217 }
218 
219 void *
dmabus_intr_establish(bus_space_tag_t t,int pri,int level,int (* handler)(void *),void * arg,void (* fastvec)(void))220 dmabus_intr_establish(bus_space_tag_t t, int pri, int level,
221     int (*handler)(void *), void *arg, void (*fastvec)(void))
222 {
223           struct lsi64854_softc *sc = t->cookie;
224 
225           /* XXX - for now only le; do esp later */
226           if (sc->sc_channel == L64854_CHANNEL_ENET) {
227                     sc->sc_intrchain = handler;
228                     sc->sc_intrchainarg = arg;
229                     handler = lsi64854_enet_intr;
230                     arg = sc;
231           }
232           return bus_intr_establish(sc->sc_bustag, pri, level, handler, arg);
233 }
234