xref: /netbsd-src/sys/arch/sun3/sun3x/vme.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: vme.c,v 1.13 2007/02/03 16:58:08 tsutsui 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 Gordon W. Ross.
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/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: vme.c,v 1.13 2007/02/03 16:58:08 tsutsui Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45 
46 #include <uvm/uvm_extern.h>
47 
48 #define _SUN68K_BUS_DMA_PRIVATE
49 #include <machine/autoconf.h>
50 #include <machine/bus.h>
51 #include <machine/dvma.h>
52 #include <machine/pmap.h>
53 
54 #include <sun3/sun3x/vme.h>
55 
56 /* Does this machine have a VME bus? */
57 extern int cpu_has_vme;
58 
59 /*
60  * Convert vme unit number to bus type,
61  * but only for supported bus types.
62  * (See autoconf.h and vme.h)
63  */
64 #define VME_UNITS	6
65 static const struct {
66 	int bustype;
67 	const char *name;
68 	int pmtype;
69 	vaddr_t base;
70 	vaddr_t mask;
71 } vme_info[VME_UNITS] = {
72 	{ BUS_VME16D16, "A16/D16", PMAP_VME16, VME16_BASE, VME16_MASK },
73 	{ BUS_VME16D32, "A16/D32", PMAP_VME32, VME16_BASE, VME16_MASK },
74 	{ BUS_VME24D16, "A24/D16", PMAP_VME16, VME24_BASE, VME24_MASK },
75 	{ BUS_VME24D32, "A24/D32", PMAP_VME32, VME24_BASE, VME24_MASK },
76 	{ BUS_VME32D16, "A32/D16", PMAP_VME16, VME32_BASE, VME32_MASK },
77 	{ BUS_VME32D32, "A32/D32", PMAP_VME32, VME32_BASE, VME32_MASK },
78 };
79 
80 static int  vme_match(struct device *, struct cfdata *, void *);
81 static void vme_attach(struct device *, struct device *, void *);
82 
83 struct vme_softc {
84 	struct device	sc_dev;
85 	bus_space_tag_t	sc_bustag;
86 	bus_dma_tag_t	sc_dmatag;
87 	int		sc_bustype;
88 };
89 
90 CFATTACH_DECL(vme, sizeof(struct vme_softc),
91     vme_match, vme_attach, NULL, NULL);
92 
93 static int vme_bus_map(bus_space_tag_t, bus_type_t, bus_addr_t, bus_size_t,
94     int, vaddr_t, bus_space_handle_t *);
95 static paddr_t vme_bus_mmap(bus_space_tag_t, bus_type_t, bus_addr_t,
96     off_t, int, int);
97 static int vme_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *, bus_size_t,
98     struct proc *, int);
99 
100 static struct sun68k_bus_space_tag vme_space_tag = {
101 	NULL,				/* cookie */
102 	NULL,				/* parent bus space tag */
103 	vme_bus_map,			/* bus_space_map */
104 	NULL,				/* bus_space_unmap */
105 	NULL,				/* bus_space_subregion */
106 	NULL,				/* bus_space_barrier */
107 	vme_bus_mmap,			/* bus_space_mmap */
108 	NULL,				/* bus_intr_establish */
109 	NULL,				/* bus_space_peek_N */
110 	NULL				/* bus_space_poke_N */
111 };
112 
113 static struct sun68k_bus_dma_tag vme_dma_tag;
114 
115 static int
116 vme_match(struct device *parent, struct cfdata *cf, void *aux)
117 {
118 	struct confargs *ca = aux;
119 	int unit;
120 
121 	if (cpu_has_vme == 0)
122 		return (0);
123 
124 	unit = cf->cf_unit;
125 	if (unit >= VME_UNITS)
126 		return (0);
127 
128 	if (ca->ca_bustype != vme_info[unit].bustype)
129 		return (0);
130 
131 	return (1);
132 }
133 
134 static void
135 vme_attach(struct device *parent, struct device *self, void *args)
136 {
137 	struct confargs *ca = aux;
138 	struct vme_softc *sc = (void *)self;
139 	struct confargs vmea;
140 	int unit;
141 
142 	unit = device_unit(self);
143 	printf(": (%s)\n", vme_info[unit].name);
144 
145 	sc->sc_bustag = ca->ca_bustag;
146 	sc->sc_dmatag = ca->ca_dmatag;
147 	sc->sc_bustype = unit;
148 
149 	vme_space_tag.cookie = sc;
150 	vme_space_tag.parent = sc->sc_bustag;
151 
152 	vme_dma_tag = *sc->sc_dmatag;
153 	vme_dma_tag._cookie = sc;
154 	vme_dma_tag._dmamap_load = vme_dmamap_load;
155 
156 	vmea = *ca;
157 	vmea.ca_bustag = &vme_space_tag;
158 	vmea.ca_dmatag = &vme_dma_tag;
159 
160 	/* We know ca_bustype == BUS_VMExx */
161 	config_search_ia(bus_scan, self, "vme", args);
162 }
163 
164 int
165 vme_bus_map(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr,
166     bus_size_t size, int flags, vaddr_t vaddr, bus_space_handle_t *hp)
167 {
168 	struct vme_softc *sc = t->cookie;
169 	paddr_t pa;
170 	int bustype, pmtype;
171 
172 	bustype = sc->sc_bustype;
173 	pa = paddr;
174 	pa &= vme_info[bustype].mask;
175 	pa |= vme_info[bustype].base;
176 	pmtype = vme_info[bustype].pmtype;
177 
178 	return bus_space_map2(sc->sc_bustag, pmtype, pa, size,
179 	    flags | _SUN68K_BUS_MAP_USE_PROM, vaddr, hp);
180 }
181 
182 paddr_t
183 vme_bus_mmap(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr, off_t off,
184     int prot, int flags)
185 {
186 	struct vme_softc *sc = t->cookie;
187 	paddr_t pa;
188 	int bustype, pmtype;
189 
190 	bustype = sc->sc_bustype;
191 	pa = paddr;
192 	pa &= vme_info[bustype].mask;
193 	pa |= vme_info[bustype].base;
194 	pmtype = vme_info[bustype].pmtype;
195 
196 	return bus_space_mmap2(sc->sc_bustag, pmtype, pa, off, prot, flags);
197 }
198 
199 static int
200 vme_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
201     bus_size_t buflen, struct proc *p, int flags)
202 {
203 	int error;
204 
205 	error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
206 	if (error == 0)
207 		map->dm_segs[0].ds_addr &= DVMA_VME_SLAVE_MASK;
208 	return error;
209 }
210