xref: /netbsd/sys/arch/sun2/sun2/mbmem.c (revision c4a72b64)
1 /*	$NetBSD: mbmem.c,v 1.12 2002/10/02 16:02:23 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  * 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 CFATTACH_DECL(mbmem, sizeof(struct mbmem_softc),
67     mbmem_match, mbmem_attach, NULL, NULL);
68 
69 static	paddr_t mbmem_bus_mmap __P((bus_space_tag_t, bus_type_t, bus_addr_t,
70 				off_t, int, int));
71 static	int _mbmem_bus_map __P((bus_space_tag_t, bus_type_t, bus_addr_t,
72 			       bus_size_t, int,
73 			       vaddr_t, bus_space_handle_t *));
74 static	int mbmem_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
75 		    		bus_size_t, struct proc *, int));
76 static	int mbmem_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
77 		    		    bus_dma_segment_t *, int, bus_size_t, int));
78 
79 static struct sun68k_bus_space_tag mbmem_space_tag = {
80 	NULL,				/* cookie */
81 	NULL,				/* parent bus tag */
82 	_mbmem_bus_map,			/* bus_space_map */
83 	NULL,				/* bus_space_unmap */
84 	NULL,				/* bus_space_subregion */
85 	NULL,				/* bus_space_barrier */
86 	mbmem_bus_mmap,			/* bus_space_mmap */
87 	NULL,				/* bus_intr_establish */
88 	NULL,				/* bus_space_peek_N */
89 	NULL				/* bus_space_poke_N */
90 };
91 
92 static struct sun68k_bus_dma_tag mbmem_dma_tag;
93 
94 static int
95 mbmem_match(parent, cf, aux)
96 	struct device *parent;
97 	struct cfdata *cf;
98 	void *aux;
99 {
100 	struct mainbus_attach_args *ma = aux;
101 
102 	return (cpu_has_multibus && (ma->ma_name == NULL || strcmp(cf->cf_name, ma->ma_name) == 0));
103 }
104 
105 static void
106 mbmem_attach(parent, self, aux)
107 	struct device *parent;
108 	struct device *self;
109 	void *aux;
110 {
111 	struct mainbus_attach_args *ma = aux;
112 	struct mbmem_softc *sc = (struct mbmem_softc *)self;
113 	struct mbmem_attach_args mbma;
114 	const char *const *cpp;
115 	static const char *const special[] = {
116 		/* find these first */
117 		NULL
118 	};
119 
120 	/*
121 	 * There is only one mbmem bus
122 	 */
123 	if (self->dv_unit > 0) {
124 		printf(" unsupported\n");
125 		return;
126 	}
127 	printf("\n");
128 
129 	sc->sc_bustag = ma->ma_bustag;
130 	sc->sc_dmatag = ma->ma_dmatag;
131 
132 	mbmem_space_tag.cookie = sc;
133 	mbmem_space_tag.parent = sc->sc_bustag;
134 
135 	mbmem_dma_tag = *sc->sc_dmatag;
136 	mbmem_dma_tag._cookie = sc;
137 	mbmem_dma_tag._dmamap_load = mbmem_dmamap_load;
138 	mbmem_dma_tag._dmamap_load_raw = mbmem_dmamap_load_raw;
139 
140 	/*
141 	 * Prepare the skeleton attach arguments for our devices.
142 	 * The values we give in the locators are indications to
143 	 * sun68k_bus_search about which locators must and must not
144 	 * be defined.
145 	 */
146 	mbma = *ma;
147 	mbma.mbma_bustag = &mbmem_space_tag;
148 	mbma.mbma_dmatag = &mbmem_dma_tag;
149 	mbma.mbma_paddr = LOCATOR_REQUIRED;
150 	mbma.mbma_pri = LOCATOR_OPTIONAL;
151 
152 	/* Find all `early' mbmem devices */
153 	for (cpp = special; *cpp != NULL; cpp++) {
154 		mbma.mbma_name = *cpp;
155 		(void)config_search(sun68k_bus_search, self, &mbma);
156 	}
157 
158 	/* Find all other mbmem devices */
159 	mbma.mbma_name = NULL;
160 	(void)config_search(sun68k_bus_search, self, &mbma);
161 }
162 
163 int
164 _mbmem_bus_map(t, btype, paddr, size, flags, vaddr, hp)
165 	bus_space_tag_t t;
166 	bus_type_t btype;
167 	bus_addr_t paddr;
168 	bus_size_t size;
169 	int	flags;
170 	vaddr_t vaddr;
171 	bus_space_handle_t *hp;
172 {
173 	struct mbmem_softc *sc = t->cookie;
174 
175 	if ((paddr + size) > MBMEM_SIZE)
176 		panic("_mbmem_bus_map: out of range");
177 
178 	return (bus_space_map2(sc->sc_bustag, PMAP_MBMEM, paddr,
179 				size, flags, vaddr, hp));
180 }
181 
182 paddr_t
183 mbmem_bus_mmap(t, btype, paddr, off, prot, flags)
184 	bus_space_tag_t t;
185 	bus_type_t btype;
186 	bus_addr_t paddr;
187 	off_t off;
188 	int prot;
189 	int flags;
190 {
191 	struct mbmem_softc *sc = t->cookie;
192 
193 	return (bus_space_mmap2(sc->sc_bustag, PMAP_MBMEM, paddr, off,
194 				prot, flags));
195 }
196 
197 static int
198 mbmem_dmamap_load(t, map, buf, buflen, p, flags)
199 	bus_dma_tag_t t;
200 	bus_dmamap_t map;
201 	void *buf;
202 	bus_size_t buflen;
203 	struct proc *p;
204 	int flags;
205 {
206 	int error;
207 
208 	error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
209 	if (error == 0)
210 		map->dm_segs[0].ds_addr &= DVMA_MBMEM_SLAVE_MASK;
211 	return (error);
212 }
213 
214 static int
215 mbmem_dmamap_load_raw(t, map, segs, nsegs, size, flags)
216 	bus_dma_tag_t t;
217 	bus_dmamap_t map;
218 	bus_dma_segment_t *segs;
219 	int nsegs;
220 	bus_size_t size;
221 	int flags;
222 {
223 	int error;
224 
225 	error = _bus_dmamap_load_raw(t, map, segs, nsegs, size, flags);
226 	if (error == 0)
227 		map->dm_segs[0].ds_addr &= DVMA_MBMEM_SLAVE_MASK;
228 	return (error);
229 }
230