obio.c revision 1.76
1/*	$NetBSD: obio.c,v 1.76 2021/08/07 16:19:05 thorpej Exp $	*/
2
3/*-
4 * Copyright (c) 1997,1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
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.76 2021/08/07 16:19:05 thorpej Exp $");
34
35#include "locators.h"
36
37#ifdef _KERNEL_OPT
38#include "sbus.h"
39#endif
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/device.h>
44#include <sys/malloc.h>
45
46#ifdef DEBUG
47#include <sys/proc.h>
48#include <sys/syslog.h>
49#endif
50
51#include <uvm/uvm_extern.h>
52
53#include <sys/bus.h>
54#if NSBUS > 0
55#include <sparc/dev/sbusvar.h>
56#endif
57#include <machine/autoconf.h>
58#include <machine/oldmon.h>
59#include <machine/cpu.h>
60#include <machine/ctlreg.h>
61#include <sparc/sparc/asm.h>
62#include <sparc/sparc/vaddrs.h>
63#include <sparc/sparc/cpuvar.h>
64
65struct obio4_softc {
66	device_t	sc_dev;		/* base device */
67	bus_space_tag_t	sc_bustag;	/* parent bus tag */
68	bus_dma_tag_t	sc_dmatag;	/* parent bus DMA tag */
69};
70
71union obio_softc {
72	struct	obio4_softc sc_obio;	/* sun4 obio */
73#if NSBUS > 0
74	struct	sbus_softc sc_sbus;	/* sun4m obio is another sbus slot */
75#endif
76};
77
78
79/* autoconfiguration driver */
80static	int obiomatch(device_t, cfdata_t, void *);
81static	void obioattach(device_t, device_t, void *);
82
83CFATTACH_DECL_NEW(obio, sizeof(union obio_softc),
84    obiomatch, obioattach, NULL, NULL);
85
86static int obio_attached;
87
88/*
89 * This `obio4_busattachargs' data structure only exists to pass down
90 * to obiosearch() the name of a device that must be configured early.
91 */
92struct obio4_busattachargs {
93	struct mainbus_attach_args	*ma;
94	const char			*name;
95};
96
97#if defined(SUN4)
98static	int obioprint(void *, const char *);
99static	int obiosearch(device_t, struct cfdata *, const int *, void *);
100static	paddr_t obio_bus_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);
101static	int _obio_bus_map(bus_space_tag_t, bus_addr_t, bus_size_t, int,
102			  vaddr_t, bus_space_handle_t *);
103
104/* There's at most one obio bus, so we can allocate the bus tag statically */
105static struct sparc_bus_space_tag obio_space_tag;
106#endif
107
108#if NSBUS > 0
109/*
110 * Translate obio `interrupts' property value to processor IPL (see sbus.c)
111 * Apparently, the `interrupts' property on obio devices is just
112 * the processor IPL.
113 */
114static int intr_obio2ipl[] = {
115	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
116};
117#endif
118
119static int
120obiomatch(device_t parent, struct cfdata *cf, void *aux)
121{
122	struct mainbus_attach_args *ma = aux;
123
124	if (obio_attached)
125		return 0;
126
127	return (strcmp(cf->cf_name, ma->ma_name) == 0);
128}
129
130static void
131obioattach(device_t parent, device_t self, void *aux)
132{
133	struct mainbus_attach_args *ma = aux;
134
135	obio_attached = 1;
136
137	printf("\n");
138
139	if (CPU_ISSUN4) {
140#if defined(SUN4)
141		union obio_softc *usc = device_private(self);
142		struct obio4_softc *sc = &usc->sc_obio;
143		struct obio4_busattachargs oa;
144		const char *const *cpp;
145		static const char *const special4[] = {
146			/* find these first */
147			"timer",
148			"dma",		/* need this before `esp', if any */
149			NULL
150		};
151
152		sc->sc_dev = self;
153		sc->sc_bustag = ma->ma_bustag;
154		sc->sc_dmatag = ma->ma_dmatag;
155
156		memcpy(&obio_space_tag, sc->sc_bustag, sizeof(obio_space_tag));
157		obio_space_tag.cookie = sc;
158		obio_space_tag.parent = sc->sc_bustag;
159		obio_space_tag.sparc_bus_map = _obio_bus_map;
160		obio_space_tag.sparc_bus_mmap = obio_bus_mmap;
161
162		oa.ma = ma;
163
164		/* Find all `early' obio devices */
165		for (cpp = special4; *cpp != NULL; cpp++) {
166			oa.name = *cpp;
167			config_search(self, &oa,
168			    CFARGS(.search = obiosearch));
169		}
170
171		/* Find all other obio devices */
172		oa.name = NULL;
173		config_search(self, &oa,
174		    CFARGS(.search = obiosearch));
175#endif
176		return;
177	} else if (CPU_ISSUN4M) {
178#if NSBUS > 0
179		/*
180		 * Attach the on-board I/O bus at on a sun4m.
181		 * In this case we treat the obio bus as another sbus slot.
182		 */
183		union obio_softc *usc = device_private(self);
184		struct sbus_softc *sc = &usc->sc_sbus;
185
186		static const char *const special4m[] = {
187			/* find these first */
188			"eeprom",
189			"counter",
190#if 0 /* Not all sun4m's have an `auxio' */
191			"auxio",
192#endif
193			"",
194			/* place device to ignore here */
195			"interrupt",
196			NULL
197		};
198
199		sc->sc_dev = self;
200		sc->sc_bustag = ma->ma_bustag;
201		sc->sc_dmatag = ma->ma_dmatag;
202		sc->sc_intr2ipl = intr_obio2ipl;
203
204		sbus_attach_common(sc, "obio", ma->ma_node, special4m);
205#endif
206	} else {
207		printf("obio on this machine?\n");
208	}
209}
210
211#if defined(SUN4)
212static int
213obioprint(void *args, const char *busname)
214{
215	union obio_attach_args *uoba = args;
216	struct obio4_attach_args *oba = &uoba->uoba_oba4;
217
218	aprint_normal(" addr 0x%lx", (u_long)BUS_ADDR_PADDR(oba->oba_paddr));
219	if (oba->oba_pri != -1)
220		aprint_normal(" level %d", oba->oba_pri);
221
222	return (UNCONF);
223}
224
225static int
226_obio_bus_map(bus_space_tag_t t, bus_addr_t ba, bus_size_t size, int flags,
227	      vaddr_t va, bus_space_handle_t *hp)
228{
229
230	if ((flags & OBIO_BUS_MAP_USE_ROM) != 0 &&
231	     obio_find_rom_map(ba, size, hp) == 0)
232		return (0);
233
234	return (bus_space_map2(t->parent, ba, size, flags, va, hp));
235}
236
237static paddr_t
238obio_bus_mmap(bus_space_tag_t t, bus_addr_t ba, off_t off, int prot, int flags)
239{
240
241	return (bus_space_mmap(t->parent, ba, off, prot, flags));
242}
243
244static int
245obiosearch(device_t parent, struct cfdata *cf, const int *ldesc,
246	   void *aux)
247{
248	struct obio4_busattachargs *oap = aux;
249	union obio_attach_args uoba;
250	struct obio4_attach_args *oba = &uoba.uoba_oba4;
251	int addr;
252
253	/* Check whether we're looking for a specifically named device */
254	if (oap->name != NULL && strcmp(oap->name, cf->cf_name) != 0)
255		return (0);
256
257	/*
258	 * Avoid sun4m entries which don't have valid PAs.
259	 * no point in even probing them.
260	 */
261	addr = cf->cf_loc[OBIOCF_ADDR];
262	if (addr == -1)
263		return (0);
264
265	/*
266	 * On the 4/100 obio addresses must be mapped at
267	 * 0x0YYYYYYY, but alias higher up (we avoid the
268	 * alias condition because it causes pmap difficulties)
269	 * XXX: We also assume that 4/[23]00 obio addresses
270	 * must be 0xZYYYYYYY, where (Z != 0)
271	 */
272	if (cpuinfo.cpu_type == CPUTYP_4_100 && (addr & 0xf0000000))
273		return (0);
274	if (cpuinfo.cpu_type != CPUTYP_4_100 && !(addr & 0xf0000000))
275		return (0);
276
277	uoba.uoba_isobio4 = 1;
278	oba->oba_bustag = &obio_space_tag;
279	oba->oba_dmatag = oap->ma->ma_dmatag;
280	oba->oba_paddr = BUS_ADDR(PMAP_OBIO, addr);
281	oba->oba_pri = cf->cf_loc[OBIOCF_LEVEL];
282
283	if (!config_probe(parent, cf, &uoba))
284		return (0);
285
286	config_attach(parent, cf, &uoba, obioprint, CFARGS_NONE);
287	return (1);
288}
289
290
291/*
292 * If we can find a mapping that was established by the rom, use it.
293 * Else, create a new mapping.
294 */
295int
296obio_find_rom_map(bus_addr_t ba, int len, bus_space_handle_t *hp)
297{
298#define	getpte(va)		lda(va, ASI_PTE)
299
300	u_long	pa, pf;
301	int	pgtype;
302	u_long	va, pte;
303
304	if (len > PAGE_SIZE)
305		return (EINVAL);
306
307	pa = BUS_ADDR_PADDR(ba);
308	pf = pa >> PGSHIFT;
309	pgtype = PMAP_T2PTE_4(PMAP_OBIO);
310
311	for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += PAGE_SIZE) {
312		pte = getpte(va);
313		if ((pte & PG_V) == 0 || (pte & PG_TYPE) != pgtype ||
314		    (pte & PG_PFNUM) != pf)
315			continue;
316
317		/*
318		 * Found entry in PROM's pagetable
319		 * note: preserve page offset
320		 */
321		*hp = (bus_space_handle_t)(va | (pa & PGOFSET));
322		return (0);
323	}
324
325	return (ENOENT);
326}
327#endif /* SUN4 */
328