obio.c revision 1.31
1/*	$NetBSD: obio.c,v 1.31 2008/04/28 20:23:38 martin 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 and 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 *
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.31 2008/04/28 20:23:38 martin 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#define _SUN68K_BUS_DMA_PRIVATE
42#include <machine/autoconf.h>
43#include <machine/bus.h>
44#include <machine/dvma.h>
45#include <machine/mon.h>
46#include <machine/pte.h>
47
48#include <sun3/sun3/machdep.h>
49#include <sun3/sun3x/obio.h>
50
51static int	obio_match(struct device *, struct cfdata *, void *);
52static void	obio_attach(struct device *, struct device *, void *);
53static int	obio_print(void *, const char *);
54static int	obio_submatch(struct device *, struct cfdata *,
55			      const int *, void *);
56
57struct obio_softc {
58	struct device	sc_dev;
59	bus_space_tag_t	sc_bustag;
60	bus_dma_tag_t	sc_dmatag;
61};
62CFATTACH_DECL(obio, sizeof(struct obio_softc),
63    obio_match, obio_attach, NULL, NULL);
64
65static int obio_attached;
66
67static 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 *);
69static paddr_t obio_bus_mmap(bus_space_tag_t, bus_type_t, bus_addr_t,
70    off_t, int, int);
71static int obio_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *, bus_size_t,
72    struct proc *, int);
73
74static struct sun68k_bus_space_tag obio_space_tag = {
75	NULL,				/* cookie */
76	NULL,				/* parent bus space tag */
77	obio_bus_map,			/* bus_space_map */
78	NULL,				/* bus_space_unmap */
79	NULL,				/* bus_space_subregion */
80	NULL,				/* bus_space_barrier */
81	obio_bus_mmap,			/* bus_space_mmap */
82	NULL,				/* bus_intr_establish */
83	NULL,				/* bus_space_peek_N */
84	NULL				/* bus_space_poke_N */
85};
86
87static struct sun68k_bus_dma_tag obio_dma_tag;
88
89static int
90obio_match(struct device *parent, struct cfdata *cf, void *aux)
91{
92	struct confargs *ca = aux;
93
94	if (obio_attached)
95		return 0;
96
97	if (ca->ca_bustype != BUS_OBIO)
98		return 0;
99
100	if (ca->ca_name != NULL && strcmp(cf->cf_name, ca->ca_name) != 0)
101		return 0;
102
103	return 1;
104}
105
106/*
107 * We need control over the order of attachment on OBIO,
108 * so do "direct" style autoconfiguration with addresses
109 * from the list below.  OBIO addresses are fixed forever.
110 *
111 * Warning: This whole list is very carefully ordered!
112 * In general, anything not already shown here should
113 * be added at or near the end.
114 */
115static paddr_t obio_alist[] = {
116
117	/* This is used by the Ethernet and SCSI drivers. */
118	OBIO_IOMMU,
119
120	/* Misc. registers - needed by many things */
121	OBIO_ENABLEREG,
122	OBIO_BUSERRREG,
123	OBIO_DIAGREG,	/* leds.c */
124	OBIO_IDPROM1,	/* idprom.c (3/470) */
125	OBIO_MEMREG,	/* memerr.c */
126	OBIO_INTERREG,	/* intreg.c */
127
128	/* Zilog UARTs */
129	OBIO_ZS_KBD_MS,
130	OBIO_ZS_TTY_AB,
131
132	/* eeprom.c */
133	OBIO_EEPROM,
134
135	/* Note: This must come after OBIO_IDPROM1. */
136	OBIO_IDPROM2,	/* idprom.c (3/80) */
137
138	/* Note: Must probe for the Intersil first! */
139	OBIO_CLOCK1,	/* clock.c (3/470) */
140	OBIO_CLOCK2,	/* clock.c (3/80) */
141
142	OBIO_INTEL_ETHER,
143	OBIO_LANCE_ETHER,
144
145	/* Need esp DMA before SCSI. */
146	OBIO_EMULEX_DMA,  /* 3/80 only */
147	OBIO_EMULEX_SCSI, /* 3/80 only */
148
149	/* Memory subsystem */
150	OBIO_PCACHE_TAGS,
151	OBIO_ECCPARREG,
152	OBIO_IOC_TAGS,
153	OBIO_IOC_FLUSH,
154
155	OBIO_FDC,	/* floppy disk (3/80) */
156	OBIO_PRINTER_PORT, /* printer port (3/80) */
157};
158#define OBIO_ALIST_LEN	__arraycount(obio_alist)
159
160static void
161obio_attach(struct device *parent, struct device *self, void *aux)
162{
163	struct confargs *ca = aux;
164	struct obio_softc *sc = (void *)self;
165	struct confargs oba;
166	int i;
167
168	obio_attached = 1;
169
170	printf("\n");
171
172	sc->sc_bustag = ca->ca_bustag;
173	sc->sc_dmatag = ca->ca_dmatag;
174
175	obio_space_tag.cookie = sc;
176	obio_space_tag.parent = sc->sc_bustag;
177
178	obio_dma_tag = *sc->sc_dmatag;
179	obio_dma_tag._cookie = sc;
180	obio_dma_tag._dmamap_load = obio_dmamap_load;
181
182	oba = *ca;
183	oba.ca_bustag = &obio_space_tag;
184	oba.ca_dmatag = &obio_dma_tag;
185
186	/* Configure these in the order listed above. */
187	for (i = 0; i < OBIO_ALIST_LEN; i++) {
188		/* Our parent set ca->ca_bustype already. */
189		oba.ca_paddr = obio_alist[i];
190		/* These are filled-in by obio_submatch. */
191		oba.ca_intpri = -1;
192		oba.ca_intvec = -1;
193		(void)config_found_sm_loc(self, "obio", NULL, &oba, obio_print,
194		    obio_submatch);
195	}
196}
197
198/*
199 * Print out the confargs.  The (parent) name is non-NULL
200 * when there was no match found by config_found().
201 */
202static int
203obio_print(void *args, const char *name)
204{
205
206	/* Be quiet about empty OBIO locations. */
207	if (name)
208		return QUIET;
209
210	/* Otherwise do the usual. */
211	return bus_print(args, name);
212}
213
214int
215obio_submatch(struct device *parent, struct cfdata *cf, const int *ldesc,
216    void *aux)
217{
218	struct confargs *ca = aux;
219
220	/*
221	 * Note that a defaulted address locator can never match
222	 * the value of ca->ca_paddr set by the obio_attach loop.
223	 * Without this diagnostic, any device with a defaulted
224	 * address locator would always be silently unmatched.
225	 * Therefore, just disallow default addresses on OBIO.
226	 */
227#ifdef	DIAGNOSTIC
228	if (cf->cf_paddr == -1)
229		panic("%s: invalid address for: %s%d",
230		    __func__, cf->cf_name, cf->cf_unit);
231#endif
232
233	/*
234	 * Note that obio_attach calls config_found_sm() with
235	 * this function as the "submatch" and ca->ca_paddr
236	 * set to each of the possible OBIO locations, so we
237	 * want to reject any unmatched address here.
238	 */
239	if (cf->cf_paddr != ca->ca_paddr)
240		return 0;
241
242	/*
243	 * Copy the locators into our confargs for the child.
244	 * Note: ca->ca_bustype was set by our parent driver
245	 * (mainbus) and ca->ca_paddr was set by obio_attach.
246	 */
247	ca->ca_intpri = cf->cf_intpri;
248	ca->ca_intvec = cf->cf_intvec;
249
250	/* Now call the match function of the potential child. */
251	return config_match(parent, cf, aux);
252}
253
254
255/*****************************************************************/
256
257/*
258 * This is our record of "interesting" OBIO mappings that
259 * the PROM has left in the virtual space reserved for it.
260 * Each row of the array holds a virtual address and the
261 * physical address it maps to (if found).
262 */
263static struct prom_map {
264	paddr_t pa;
265	vaddr_t va;
266} prom_mappings[] = {
267	{ OBIO_ENABLEREG, 0 },	/* regs: Sys ENA, Bus ERR, etc. */
268	{ OBIO_ZS_KBD_MS, 0 },	/* Keyboard and Mouse */
269	{ OBIO_ZS_TTY_AB, 0 },	/* Serial Ports */
270	{ OBIO_EEPROM,    0 },	/* EEPROM/IDPROM/clock */
271};
272#define PROM_MAP_CNT	__arraycount(prom_mappings)
273
274/*
275 * Find a virtual address for a device at physical address 'pa'.
276 * If one is found among the mappings already made by the PROM
277 * at power-up time, use it and return 0. Otherwise return errno
278 * as a sign that a mapping will have to be created.
279 */
280int
281find_prom_map(paddr_t pa, bus_type_t iospace, int sz, vaddr_t *vap)
282{
283	int i;
284	vsize_t off;
285
286	off = pa & PGOFSET;
287	pa -= off;
288	sz += off;
289
290	/* The saved mappings are all one page long. */
291	if (sz > PAGE_SIZE)
292		return EINVAL;
293
294	/* Linear search for it.  The list is short. */
295	for (i = 0; i < PROM_MAP_CNT; i++) {
296		if (pa == prom_mappings[i].pa) {
297			*vap = prom_mappings[i].va + off;
298			return 0;
299		}
300	}
301	return ENOENT;
302}
303
304/*
305 * Search the PROM page tables for OBIO mappings that
306 * we would like to borrow.
307 */
308static void
309save_prom_mappings(void)
310{
311	int *mon_pte;
312	vaddr_t va;
313	paddr_t pa;
314	int i;
315
316	/* Note: mon_ctbl[0] maps SUN3X_MON_KDB_BASE */
317	mon_pte = *romVectorPtr->monptaddr;
318
319	for (va = SUN3X_MON_KDB_BASE; va < SUN3X_MONEND;
320	    va += PAGE_SIZE, mon_pte++) {
321		/* Is this a valid mapping to OBIO? */
322		/* XXX - Some macros would be nice... */
323		if ((*mon_pte & 0xF0000003) != 0x60000001)
324			continue;
325
326		/* Yes it is.  Is this a mapping we want? */
327		pa = *mon_pte & MMU_SHORT_PTE_BASEADDR;
328		for (i = 0; i < PROM_MAP_CNT; i++) {
329			if (pa != prom_mappings[i].pa)
330				continue;
331			/* Yes, we want it.  Save the va? */
332			if (prom_mappings[i].va == 0) {
333				prom_mappings[i].va = va;
334			}
335		}
336	}
337}
338
339/*
340 * These are all the OBIO address that are required early in
341 * the life of the kernel.  All are less than one page long.
342 * This function should make any required mappings that we
343 * were not able to find among the PROM monitor's mappings.
344 */
345static void
346make_required_mappings(void)
347{
348	int i;
349
350	for (i = 0; i < PROM_MAP_CNT; i++) {
351		if (prom_mappings[i].va == 0) {
352			/*
353			 * Actually, the PROM always has all the
354			 * "required" mappings we need, (smile)
355			 * but this makes sure that is true.
356			 */
357			mon_printf("obio: no mapping for pa=0x%x\n",
358			    prom_mappings[i].pa);
359			sunmon_abort();  /* Ancient PROM? */
360		}
361	}
362}
363
364
365/*
366 * Find mappings for devices that are needed before autoconfiguration.
367 * We first look for and record any useful PROM mappings, then call
368 * the "init" functions for drivers that we need to use before the
369 * normal autoconfiguration calls configure().  Warning: this is
370 * called before pmap_bootstrap, so no allocation allowed!
371 */
372void
373obio_init(void)
374{
375
376	save_prom_mappings();
377	make_required_mappings();
378
379	enable_init();
380
381	/*
382	 * Find the interrupt reg mapping and turn off the
383	 * interrupts, otherwise the PROM clock interrupt
384	 * would poll the zs and toggle some LEDs...
385	 */
386	intreg_init();
387}
388
389int
390obio_bus_map(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr,
391    bus_size_t size, int flags, vaddr_t vaddr, bus_space_handle_t *hp)
392{
393	struct obio_softc *sc = t->cookie;
394
395	return bus_space_map2(sc->sc_bustag, PMAP_OBIO, paddr, size,
396	    flags | _SUN68K_BUS_MAP_USE_PROM, vaddr, hp);
397}
398
399paddr_t
400obio_bus_mmap(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr, off_t off,
401    int prot, int flags)
402{
403	struct obio_softc *sc = t->cookie;
404
405	return bus_space_mmap2(sc->sc_bustag, PMAP_OBIO, paddr, off, prot,
406	    flags);
407}
408
409static int
410obio_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
411    bus_size_t buflen, struct proc *p, int flags)
412{
413	int error;
414
415	error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
416	if (error == 0)
417		map->dm_segs[0].ds_addr &= DVMA_OBIO_SLAVE_MASK;
418	return error;
419}
420
421