1 /*        $NetBSD: obio.c,v 1.77 2023/12/20 05:33:18 thorpej Exp $    */
2 
3 /*-
4  * Copyright (c) 1997,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 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.77 2023/12/20 05:33:18 thorpej Exp $");
34 
35 #include "locators.h"
36 
37 #ifdef _KERNEL_OPT
38 #include "sbus.h"
39 #endif
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 
45 #ifdef DEBUG
46 #include <sys/proc.h>
47 #include <sys/syslog.h>
48 #endif
49 
50 #include <uvm/uvm_extern.h>
51 
52 #include <sys/bus.h>
53 #if NSBUS > 0
54 #include <sparc/dev/sbusvar.h>
55 #endif
56 #include <machine/autoconf.h>
57 #include <machine/oldmon.h>
58 #include <machine/cpu.h>
59 #include <machine/ctlreg.h>
60 #include <sparc/sparc/asm.h>
61 #include <sparc/sparc/vaddrs.h>
62 #include <sparc/sparc/cpuvar.h>
63 
64 struct obio4_softc {
65           device_t  sc_dev;             /* base device */
66           bus_space_tag_t     sc_bustag;          /* parent bus tag */
67           bus_dma_tag_t       sc_dmatag;          /* parent bus DMA tag */
68 };
69 
70 union obio_softc {
71           struct    obio4_softc sc_obio;          /* sun4 obio */
72 #if NSBUS > 0
73           struct    sbus_softc sc_sbus; /* sun4m obio is another sbus slot */
74 #endif
75 };
76 
77 
78 /* autoconfiguration driver */
79 static    int obiomatch(device_t, cfdata_t, void *);
80 static    void obioattach(device_t, device_t, void *);
81 
82 CFATTACH_DECL_NEW(obio, sizeof(union obio_softc),
83     obiomatch, obioattach, NULL, NULL);
84 
85 static int obio_attached;
86 
87 /*
88  * This `obio4_busattachargs' data structure only exists to pass down
89  * to obiosearch() the name of a device that must be configured early.
90  */
91 struct obio4_busattachargs {
92           struct mainbus_attach_args    *ma;
93           const char                              *name;
94 };
95 
96 #if defined(SUN4)
97 static    int obioprint(void *, const char *);
98 static    int obiosearch(device_t, struct cfdata *, const int *, void *);
99 static    paddr_t obio_bus_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);
100 static    int _obio_bus_map(bus_space_tag_t, bus_addr_t, bus_size_t, int,
101                                 vaddr_t, bus_space_handle_t *);
102 
103 /* There's at most one obio bus, so we can allocate the bus tag statically */
104 static struct sparc_bus_space_tag obio_space_tag;
105 #endif
106 
107 #if NSBUS > 0
108 /*
109  * Translate obio `interrupts' property value to processor IPL (see sbus.c)
110  * Apparently, the `interrupts' property on obio devices is just
111  * the processor IPL.
112  */
113 static int intr_obio2ipl[] = {
114           0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
115 };
116 #endif
117 
118 static int
obiomatch(device_t parent,struct cfdata * cf,void * aux)119 obiomatch(device_t parent, struct cfdata *cf, void *aux)
120 {
121           struct mainbus_attach_args *ma = aux;
122 
123           if (obio_attached)
124                     return 0;
125 
126           return (strcmp(cf->cf_name, ma->ma_name) == 0);
127 }
128 
129 static void
obioattach(device_t parent,device_t self,void * aux)130 obioattach(device_t parent, device_t self, void *aux)
131 {
132           struct mainbus_attach_args *ma = aux;
133 
134           obio_attached = 1;
135 
136           printf("\n");
137 
138           if (CPU_ISSUN4) {
139 #if defined(SUN4)
140                     union obio_softc *usc = device_private(self);
141                     struct obio4_softc *sc = &usc->sc_obio;
142                     struct obio4_busattachargs oa;
143                     const char *const *cpp;
144                     static const char *const special4[] = {
145                               /* find these first */
146                               "timer",
147                               "dma",              /* need this before `esp', if any */
148                               NULL
149                     };
150 
151                     sc->sc_dev = self;
152                     sc->sc_bustag = ma->ma_bustag;
153                     sc->sc_dmatag = ma->ma_dmatag;
154 
155                     memcpy(&obio_space_tag, sc->sc_bustag, sizeof(obio_space_tag));
156                     obio_space_tag.cookie = sc;
157                     obio_space_tag.parent = sc->sc_bustag;
158                     obio_space_tag.sparc_bus_map = _obio_bus_map;
159                     obio_space_tag.sparc_bus_mmap = obio_bus_mmap;
160 
161                     oa.ma = ma;
162 
163                     /* Find all `early' obio devices */
164                     for (cpp = special4; *cpp != NULL; cpp++) {
165                               oa.name = *cpp;
166                               config_search(self, &oa,
167                                   CFARGS(.search = obiosearch));
168                     }
169 
170                     /* Find all other obio devices */
171                     oa.name = NULL;
172                     config_search(self, &oa,
173                         CFARGS(.search = obiosearch));
174 #endif
175                     return;
176           } else if (CPU_ISSUN4M) {
177 #if NSBUS > 0
178                     /*
179                      * Attach the on-board I/O bus at on a sun4m.
180                      * In this case we treat the obio bus as another sbus slot.
181                      */
182                     union obio_softc *usc = device_private(self);
183                     struct sbus_softc *sc = &usc->sc_sbus;
184 
185                     static const char *const special4m[] = {
186                               /* find these first */
187                               "eeprom",
188                               "counter",
189 #if 0 /* Not all sun4m's have an `auxio' */
190                               "auxio",
191 #endif
192                               "",
193                               /* place device to ignore here */
194                               "interrupt",
195                               NULL
196                     };
197 
198                     sc->sc_dev = self;
199                     sc->sc_bustag = ma->ma_bustag;
200                     sc->sc_dmatag = ma->ma_dmatag;
201                     sc->sc_intr2ipl = intr_obio2ipl;
202 
203                     sbus_attach_common(sc, "obio", ma->ma_node, special4m);
204 #endif
205           } else {
206                     printf("obio on this machine?\n");
207           }
208 }
209 
210 #if defined(SUN4)
211 static int
obioprint(void * args,const char * busname)212 obioprint(void *args, const char *busname)
213 {
214           union obio_attach_args *uoba = args;
215           struct obio4_attach_args *oba = &uoba->uoba_oba4;
216 
217           aprint_normal(" addr 0x%lx", (u_long)BUS_ADDR_PADDR(oba->oba_paddr));
218           if (oba->oba_pri != -1)
219                     aprint_normal(" level %d", oba->oba_pri);
220 
221           return (UNCONF);
222 }
223 
224 static int
_obio_bus_map(bus_space_tag_t t,bus_addr_t ba,bus_size_t size,int flags,vaddr_t va,bus_space_handle_t * hp)225 _obio_bus_map(bus_space_tag_t t, bus_addr_t ba, bus_size_t size, int flags,
226                 vaddr_t va, bus_space_handle_t *hp)
227 {
228 
229           if ((flags & OBIO_BUS_MAP_USE_ROM) != 0 &&
230                obio_find_rom_map(ba, size, hp) == 0)
231                     return (0);
232 
233           return (bus_space_map2(t->parent, ba, size, flags, va, hp));
234 }
235 
236 static paddr_t
obio_bus_mmap(bus_space_tag_t t,bus_addr_t ba,off_t off,int prot,int flags)237 obio_bus_mmap(bus_space_tag_t t, bus_addr_t ba, off_t off, int prot, int flags)
238 {
239 
240           return (bus_space_mmap(t->parent, ba, off, prot, flags));
241 }
242 
243 static int
obiosearch(device_t parent,struct cfdata * cf,const int * ldesc,void * aux)244 obiosearch(device_t parent, struct cfdata *cf, const int *ldesc,
245              void *aux)
246 {
247           struct obio4_busattachargs *oap = aux;
248           union obio_attach_args uoba;
249           struct obio4_attach_args *oba = &uoba.uoba_oba4;
250           int addr;
251 
252           /* Check whether we're looking for a specifically named device */
253           if (oap->name != NULL && strcmp(oap->name, cf->cf_name) != 0)
254                     return (0);
255 
256           /*
257            * Avoid sun4m entries which don't have valid PAs.
258            * no point in even probing them.
259            */
260           addr = cf->cf_loc[OBIOCF_ADDR];
261           if (addr == -1)
262                     return (0);
263 
264           /*
265            * On the 4/100 obio addresses must be mapped at
266            * 0x0YYYYYYY, but alias higher up (we avoid the
267            * alias condition because it causes pmap difficulties)
268            * XXX: We also assume that 4/[23]00 obio addresses
269            * must be 0xZYYYYYYY, where (Z != 0)
270            */
271           if (cpuinfo.cpu_type == CPUTYP_4_100 && (addr & 0xf0000000))
272                     return (0);
273           if (cpuinfo.cpu_type != CPUTYP_4_100 && !(addr & 0xf0000000))
274                     return (0);
275 
276           uoba.uoba_isobio4 = 1;
277           oba->oba_bustag = &obio_space_tag;
278           oba->oba_dmatag = oap->ma->ma_dmatag;
279           oba->oba_paddr = BUS_ADDR(PMAP_OBIO, addr);
280           oba->oba_pri = cf->cf_loc[OBIOCF_LEVEL];
281 
282           if (!config_probe(parent, cf, &uoba))
283                     return (0);
284 
285           config_attach(parent, cf, &uoba, obioprint, CFARGS_NONE);
286           return (1);
287 }
288 
289 
290 /*
291  * If we can find a mapping that was established by the rom, use it.
292  * Else, create a new mapping.
293  */
294 int
obio_find_rom_map(bus_addr_t ba,int len,bus_space_handle_t * hp)295 obio_find_rom_map(bus_addr_t ba, int len, bus_space_handle_t *hp)
296 {
297 #define   getpte(va)                    lda(va, ASI_PTE)
298 
299           u_long    pa, pf;
300           int       pgtype;
301           u_long    va, pte;
302 
303           if (len > PAGE_SIZE)
304                     return (EINVAL);
305 
306           pa = BUS_ADDR_PADDR(ba);
307           pf = pa >> PGSHIFT;
308           pgtype = PMAP_T2PTE_4(PMAP_OBIO);
309 
310           for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += PAGE_SIZE) {
311                     pte = getpte(va);
312                     if ((pte & PG_V) == 0 || (pte & PG_TYPE) != pgtype ||
313                         (pte & PG_PFNUM) != pf)
314                               continue;
315 
316                     /*
317                      * Found entry in PROM's pagetable
318                      * note: preserve page offset
319                      */
320                     *hp = (bus_space_handle_t)(va | (pa & PGOFSET));
321                     return (0);
322           }
323 
324           return (ENOENT);
325 }
326 #endif /* SUN4 */
327