1 /*        $NetBSD: mbio.c,v 1.21 2021/08/07 16:19:06 thorpej Exp $    */
2 
3 /*-
4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Adam Glass, Gordon W. Ross, and Matthew Fredette.
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: mbio.c,v 1.21 2021/08/07 16:19:06 thorpej Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 
39 #include <uvm/uvm_extern.h>
40 
41 #include <machine/autoconf.h>
42 #include <machine/pmap.h>
43 
44 #include <sun2/sun2/control.h>
45 #include <sun2/sun2/machdep.h>
46 #include <sun2/sun2/mbio.h>
47 
48 /* Does this machine have a Multibus? */
49 extern int cpu_has_multibus;
50 
51 static int  mbio_match(device_t, cfdata_t, void *);
52 static void mbio_attach(device_t, device_t, void *);
53 
54 struct mbio_softc {
55           device_t  sc_dev;             /* base device */
56           bus_space_tag_t     sc_bustag;          /* parent bus tag */
57           bus_dma_tag_t       sc_dmatag;          /* parent bus dma tag */
58 };
59 
60 CFATTACH_DECL_NEW(mbio, sizeof(struct mbio_softc),
61     mbio_match, mbio_attach, NULL, NULL);
62 
63 static int mbio_attached;
64 
65 static    paddr_t mbio_bus_mmap(bus_space_tag_t, bus_type_t, bus_addr_t, off_t,
66               int, int);
67 static    int _mbio_bus_map(bus_space_tag_t, bus_type_t, bus_addr_t, bus_size_t,
68               int, vaddr_t, bus_space_handle_t *);
69 
70 static struct sun68k_bus_space_tag mbio_space_tag = {
71           NULL,                                   /* cookie */
72           NULL,                                   /* parent bus tag */
73           _mbio_bus_map,                          /* bus_space_map */
74           NULL,                                   /* bus_space_unmap */
75           NULL,                                   /* bus_space_subregion */
76           NULL,                                   /* bus_space_barrier */
77           mbio_bus_mmap,                          /* bus_space_mmap */
78           NULL,                                   /* bus_intr_establish */
79           NULL,                                   /* bus_space_peek_N */
80           NULL                                    /* bus_space_poke_N */
81 };
82 
83 static int
mbio_match(device_t parent,cfdata_t cf,void * aux)84 mbio_match(device_t parent, cfdata_t cf, void *aux)
85 {
86           struct mainbus_attach_args *ma = aux;
87 
88           if (mbio_attached)
89                     return 0;
90 
91           return (cpu_has_multibus &&
92               (ma->ma_name == NULL || strcmp(cf->cf_name, ma->ma_name) == 0));
93 }
94 
95 static void
mbio_attach(device_t parent,device_t self,void * aux)96 mbio_attach(device_t parent, device_t self, void *aux)
97 {
98           struct mainbus_attach_args *ma = aux;
99           struct mbio_softc *sc = device_private(self);
100           struct mbio_attach_args mba;
101           const char *const *cpp;
102           static const char *const special[] = {
103                     /* find these first */
104                     NULL
105           };
106 
107           mbio_attached = 1;
108 
109           sc->sc_dev = self;
110           aprint_normal("\n");
111 
112           sc->sc_bustag = ma->ma_bustag;
113           sc->sc_dmatag = ma->ma_dmatag;
114 
115           mbio_space_tag.cookie = sc;
116           mbio_space_tag.parent = sc->sc_bustag;
117 
118           /*
119            * Prepare the skeleton attach arguments for our devices.
120            * The values we give in the locators are indications to
121            * sun68k_bus_search about which locators must and must not
122            * be defined.
123            */
124           mba = *ma;
125           mba.mba_bustag = &mbio_space_tag;
126           mba.mba_paddr = LOCATOR_REQUIRED;
127           mba.mba_pri = LOCATOR_OPTIONAL;
128 
129           /* Find all `early' mbio devices */
130           for (cpp = special; *cpp != NULL; cpp++) {
131                     mba.mba_name = *cpp;
132                     config_search(self, &mba,
133                         CFARGS(.search = sun68k_bus_search));
134           }
135 
136           /* Find all other mbio devices */
137           mba.mba_name = NULL;
138           config_search(self, &mba,
139               CFARGS(.search = sun68k_bus_search));
140 }
141 
142 int
_mbio_bus_map(bus_space_tag_t t,bus_type_t btype,bus_addr_t paddr,bus_size_t size,int flags,vaddr_t vaddr,bus_space_handle_t * hp)143 _mbio_bus_map(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr,
144     bus_size_t size, int flags, vaddr_t vaddr, bus_space_handle_t *hp)
145 {
146           struct mbio_softc *sc = t->cookie;
147 
148           if ((paddr + size) > MBIO_SIZE)
149                     panic("%s: out of range", __func__);
150 
151           return bus_space_map2(sc->sc_bustag, PMAP_MBIO, paddr,
152               size, flags | _SUN68K_BUS_MAP_USE_PROM, vaddr, hp);
153 }
154 
155 paddr_t
mbio_bus_mmap(bus_space_tag_t t,bus_type_t btype,bus_addr_t paddr,off_t off,int prot,int flags)156 mbio_bus_mmap(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr, off_t off,
157     int prot, int flags)
158 {
159           struct mbio_softc *sc = t->cookie;
160 
161           return bus_space_mmap2(sc->sc_bustag, PMAP_MBIO, paddr, off,
162               prot, flags);
163 }
164