xref: /netbsd/sys/arch/sun2/sun2/obio.c (revision 6550d01e)
1 /*	$NetBSD: obio.c,v 1.18 2008/07/06 13:29:50 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 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: obio.c,v 1.18 2008/07/06 13:29:50 tsutsui 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 #include <machine/pte.h>
44 
45 #include <sun2/sun2/control.h>
46 #include <sun2/sun2/machdep.h>
47 
48 /* Does this machine have a Multibus? */
49 extern int cpu_has_multibus;
50 
51 static int  obio_match(device_t, cfdata_t, void *);
52 static void obio_attach(device_t, device_t, void *);
53 
54 struct obio_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(obio, sizeof(struct obio_softc),
61     obio_match, obio_attach, NULL, NULL);
62 
63 static int obio_attached;
64 
65 static	paddr_t obio_bus_mmap(bus_space_tag_t, bus_type_t, bus_addr_t, off_t,
66 	    int, int);
67 static	int _obio_bus_map(bus_space_tag_t, bus_type_t, bus_addr_t, bus_size_t,
68 	    int, vaddr_t, bus_space_handle_t *);
69 static	int _obio_addr_bad(bus_space_tag_t, bus_space_handle_t, bus_size_t,
70 	    size_t);
71 static	int _obio_bus_peek(bus_space_tag_t, bus_space_handle_t, bus_size_t,
72 	    size_t, void *);
73 static	int _obio_bus_poke(bus_space_tag_t, bus_space_handle_t, bus_size_t,
74 	    size_t, uint32_t);
75 
76 static struct sun68k_bus_space_tag obio_space_tag = {
77 	NULL,				/* cookie */
78 	NULL,				/* parent bus tag */
79 	_obio_bus_map,			/* bus_space_map */
80 	NULL,				/* bus_space_unmap */
81 	NULL,				/* bus_space_subregion */
82 	NULL,				/* bus_space_barrier */
83 	obio_bus_mmap,			/* bus_space_mmap */
84 	NULL,				/* bus_intr_establish */
85 	_obio_bus_peek,			/* bus_space_peek_N */
86 	_obio_bus_poke			/* bus_space_poke_N */
87 };
88 
89 static int
90 obio_match(device_t parent, cfdata_t cf, void *aux)
91 {
92 	struct mainbus_attach_args *ma = aux;
93 
94 	if (obio_attached)
95 		return 0;
96 
97 	return ma->ma_name == NULL || strcmp(cf->cf_name, ma->ma_name) == 0;
98 }
99 
100 static void
101 obio_attach(device_t parent, device_t self, void *aux)
102 {
103 	struct mainbus_attach_args *ma = aux;
104 	struct obio_softc *sc = device_private(self);
105 	struct obio_attach_args oba;
106 	const char *const *cpp;
107 	static const char *const special[] = {
108 		/* find these first */
109 		NULL
110 	};
111 
112 	obio_attached = 1;
113 
114 	sc->sc_dev = self;
115 	aprint_normal("\n");
116 
117 	sc->sc_bustag = ma->ma_bustag;
118 	sc->sc_dmatag = ma->ma_dmatag;
119 
120 	obio_space_tag.cookie = sc;
121 	obio_space_tag.parent = sc->sc_bustag;
122 
123 	/*
124 	 * Prepare the skeleton attach arguments for our devices.
125 	 * The values we give in the locators are indications to
126 	 * sun68k_bus_search about which locators must and must not
127 	 * be defined.
128 	 */
129 	oba = *ma;
130 	oba.oba_bustag = &obio_space_tag;
131 	oba.oba_paddr = LOCATOR_REQUIRED;
132 	oba.oba_pri = LOCATOR_OPTIONAL;
133 
134 	/* Find all `early' obio devices */
135 	for (cpp = special; *cpp != NULL; cpp++) {
136 		oba.oba_name = *cpp;
137 		config_search_ia(sun68k_bus_search, self, "obio", &oba);
138 	}
139 
140 	/* Find all other obio devices */
141 	oba.oba_name = NULL;
142 	config_search_ia(sun68k_bus_search, self, "obio", &oba);
143 }
144 
145 int
146 _obio_bus_map(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr,
147     bus_size_t size, int flags, vaddr_t vaddr, bus_space_handle_t *hp)
148 {
149 	struct obio_softc *sc = t->cookie;
150 
151 	return bus_space_map2(sc->sc_bustag, PMAP_OBIO, paddr, size,
152 	    flags | _SUN68K_BUS_MAP_USE_PROM, vaddr, hp);
153 }
154 
155 paddr_t
156 obio_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 obio_softc *sc = t->cookie;
160 
161 	return bus_space_mmap2(sc->sc_bustag, PMAP_OBIO, paddr, off,
162 	    prot, flags);
163 }
164 
165 /*
166  * The sun2 obio bus doesn't give bus errors, so we check on
167  * probed obio physical addresses to make sure they're ok.
168  */
169 int
170 _obio_addr_bad(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o, size_t s)
171 {
172 	u_int pte;
173 	paddr_t pa;
174 
175 	/* Get the physical address for this page. */
176 	pte = get_pte((vaddr_t) (h + o));
177 	if ((pte & PG_VALID) == 0)
178 		return -1;
179 	pa = PG_PA(pte);
180 
181 	/*
182 	 * Return nonzero if it's bad.  All sun2 Multibus
183 	 * machines have all obio devices between 0x2000
184 	 * and 0x4000, and all sun2 VME machines have all
185 	 * obio devices outside of this range.
186 	 */
187 	return (!!cpu_has_multibus) != (pa >= 0x2000 && pa < 0x4000);
188 }
189 
190 int
191 _obio_bus_peek(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o, size_t s,
192     void *vp)
193 {
194 	struct obio_softc *sc = t->cookie;
195 
196 	return _obio_addr_bad(t, h, o, s) ||
197 	    _bus_space_peek(sc->sc_bustag, h, o, s, vp);
198 }
199 
200 int
201 _obio_bus_poke(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o, size_t s,
202     uint32_t v)
203 {
204 	struct obio_softc *sc = t->cookie;
205 
206 	return _obio_addr_bad(t, h, o, s) ||
207 	    _bus_space_poke(sc->sc_bustag, h, o, s, v);
208 }
209