xref: /netbsd/sys/arch/sun2/sun2/mbmem.c (revision bf9ec67e)
1 /*	$NetBSD: mbmem.c,v 1.8 2001/12/15 22:13:11 fredette 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 
43 #include <uvm/uvm_extern.h>
44 
45 #define _SUN68K_BUS_DMA_PRIVATE
46 #include <machine/autoconf.h>
47 #include <machine/pmap.h>
48 #include <machine/dvma.h>
49 
50 #include <sun2/sun2/control.h>
51 #include <sun2/sun2/machdep.h>
52 #include <sun2/sun2/mbmem.h>
53 
54 /* Does this machine have a Multibus? */
55 extern int cpu_has_multibus;
56 
57 static int  mbmem_match __P((struct device *, struct cfdata *, void *));
58 static void mbmem_attach __P((struct device *, struct device *, void *));
59 
60 struct mbmem_softc {
61 	struct device	sc_dev;		/* base device */
62 	bus_space_tag_t	sc_bustag;	/* parent bus tag */
63 	bus_dma_tag_t	sc_dmatag;	/* parent bus dma tag */
64 };
65 
66 struct cfattach mbmem_ca = {
67 	sizeof(struct mbmem_softc), mbmem_match, mbmem_attach
68 };
69 
70 static	paddr_t mbmem_bus_mmap __P((bus_space_tag_t, bus_type_t, bus_addr_t,
71 				off_t, int, int));
72 static	int _mbmem_bus_map __P((bus_space_tag_t, bus_type_t, bus_addr_t,
73 			       bus_size_t, int,
74 			       vaddr_t, bus_space_handle_t *));
75 static	int mbmem_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
76 		    		bus_size_t, struct proc *, int));
77 static	int mbmem_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
78 		    		    bus_dma_segment_t *, int, bus_size_t, int));
79 
80 static struct sun68k_bus_space_tag mbmem_space_tag = {
81 	NULL,				/* cookie */
82 	NULL,				/* parent bus tag */
83 	_mbmem_bus_map,			/* bus_space_map */
84 	NULL,				/* bus_space_unmap */
85 	NULL,				/* bus_space_subregion */
86 	NULL,				/* bus_space_barrier */
87 	mbmem_bus_mmap,			/* bus_space_mmap */
88 	NULL,				/* bus_intr_establish */
89 	NULL,				/* bus_space_peek_N */
90 	NULL				/* bus_space_poke_N */
91 };
92 
93 static struct sun68k_bus_dma_tag mbmem_dma_tag;
94 
95 static int
96 mbmem_match(parent, cf, aux)
97 	struct device *parent;
98 	struct cfdata *cf;
99 	void *aux;
100 {
101 	struct mainbus_attach_args *ma = aux;
102 
103 	return (cpu_has_multibus && (ma->ma_name == NULL || strcmp(cf->cf_driver->cd_name, ma->ma_name) == 0));
104 }
105 
106 static void
107 mbmem_attach(parent, self, aux)
108 	struct device *parent;
109 	struct device *self;
110 	void *aux;
111 {
112 	struct mainbus_attach_args *ma = aux;
113 	struct mbmem_softc *sc = (struct mbmem_softc *)self;
114 	struct mbmem_attach_args mbma;
115 	const char *const *cpp;
116 	static const char *const special[] = {
117 		/* find these first */
118 		NULL
119 	};
120 
121 	/*
122 	 * There is only one mbmem bus
123 	 */
124 	if (self->dv_unit > 0) {
125 		printf(" unsupported\n");
126 		return;
127 	}
128 	printf("\n");
129 
130 	sc->sc_bustag = ma->ma_bustag;
131 	sc->sc_dmatag = ma->ma_dmatag;
132 
133 	mbmem_space_tag.cookie = sc;
134 	mbmem_space_tag.parent = sc->sc_bustag;
135 
136 	mbmem_dma_tag = *sc->sc_dmatag;
137 	mbmem_dma_tag._cookie = sc;
138 	mbmem_dma_tag._dmamap_load = mbmem_dmamap_load;
139 	mbmem_dma_tag._dmamap_load_raw = mbmem_dmamap_load_raw;
140 
141 	/*
142 	 * Prepare the skeleton attach arguments for our devices.
143 	 * The values we give in the locators are indications to
144 	 * sun68k_bus_search about which locators must and must not
145 	 * be defined.
146 	 */
147 	mbma = *ma;
148 	mbma.mbma_bustag = &mbmem_space_tag;
149 	mbma.mbma_dmatag = &mbmem_dma_tag;
150 	mbma.mbma_paddr = LOCATOR_REQUIRED;
151 	mbma.mbma_pri = LOCATOR_OPTIONAL;
152 
153 	/* Find all `early' mbmem devices */
154 	for (cpp = special; *cpp != NULL; cpp++) {
155 		mbma.mbma_name = *cpp;
156 		(void)config_search(sun68k_bus_search, self, &mbma);
157 	}
158 
159 	/* Find all other mbmem devices */
160 	mbma.mbma_name = NULL;
161 	(void)config_search(sun68k_bus_search, self, &mbma);
162 }
163 
164 int
165 _mbmem_bus_map(t, btype, paddr, size, flags, vaddr, hp)
166 	bus_space_tag_t t;
167 	bus_type_t btype;
168 	bus_addr_t paddr;
169 	bus_size_t size;
170 	int	flags;
171 	vaddr_t vaddr;
172 	bus_space_handle_t *hp;
173 {
174 	struct mbmem_softc *sc = t->cookie;
175 
176 	if ((paddr + size) > MBMEM_SIZE)
177 		panic("_mbmem_bus_map: out of range");
178 
179 	return (bus_space_map2(sc->sc_bustag, PMAP_MBMEM, paddr,
180 				size, flags, vaddr, hp));
181 }
182 
183 paddr_t
184 mbmem_bus_mmap(t, btype, paddr, off, prot, flags)
185 	bus_space_tag_t t;
186 	bus_type_t btype;
187 	bus_addr_t paddr;
188 	off_t off;
189 	int prot;
190 	int flags;
191 {
192 	struct mbmem_softc *sc = t->cookie;
193 
194 	return (bus_space_mmap2(sc->sc_bustag, PMAP_MBMEM, paddr, off,
195 				prot, flags));
196 }
197 
198 static int
199 mbmem_dmamap_load(t, map, buf, buflen, p, flags)
200 	bus_dma_tag_t t;
201 	bus_dmamap_t map;
202 	void *buf;
203 	bus_size_t buflen;
204 	struct proc *p;
205 	int flags;
206 {
207 	int error;
208 
209 	error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
210 	if (error == 0)
211 		map->dm_segs[0].ds_addr &= DVMA_MBMEM_SLAVE_MASK;
212 	return (error);
213 }
214 
215 static int
216 mbmem_dmamap_load_raw(t, map, segs, nsegs, size, flags)
217 	bus_dma_tag_t t;
218 	bus_dmamap_t map;
219 	bus_dma_segment_t *segs;
220 	int nsegs;
221 	bus_size_t size;
222 	int flags;
223 {
224 	int error;
225 
226 	error = _bus_dmamap_load_raw(t, map, segs, nsegs, size, flags);
227 	if (error == 0)
228 		map->dm_segs[0].ds_addr &= DVMA_MBMEM_SLAVE_MASK;
229 	return (error);
230 }
231