nexus.c revision 129198
1129198Scognet/*
2129198Scognet * Copyright 1998 Massachusetts Institute of Technology
3129198Scognet *
4129198Scognet * Permission to use, copy, modify, and distribute this software and
5129198Scognet * its documentation for any purpose and without fee is hereby
6129198Scognet * granted, provided that both the above copyright notice and this
7129198Scognet * permission notice appear in all copies, that both the above
8129198Scognet * copyright notice and this permission notice appear in all
9129198Scognet * supporting documentation, and that the name of M.I.T. not be used
10129198Scognet * in advertising or publicity pertaining to distribution of the
11129198Scognet * software without specific, written prior permission.  M.I.T. makes
12129198Scognet * no representations about the suitability of this software for any
13129198Scognet * purpose.  It is provided "as is" without express or implied
14129198Scognet * warranty.
15129198Scognet *
16129198Scognet * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17129198Scognet * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18129198Scognet * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19129198Scognet * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20129198Scognet * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21129198Scognet * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22129198Scognet * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23129198Scognet * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24129198Scognet * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25129198Scognet * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26129198Scognet * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27129198Scognet * SUCH DAMAGE.
28129198Scognet *
29129198Scognet */
30129198Scognet
31129198Scognet/*
32129198Scognet * This code implements a `root nexus' for Arm Architecture
33129198Scognet * machines.  The function of the root nexus is to serve as an
34129198Scognet * attachment point for both processors and buses, and to manage
35129198Scognet * resources which are common to all of them.  In particular,
36129198Scognet * this code implements the core resource managers for interrupt
37129198Scognet * requests, DMA requests (which rightfully should be a part of the
38129198Scognet * ISA code but it's easier to do it here for now), I/O port addresses,
39129198Scognet * and I/O memory address space.
40129198Scognet */
41129198Scognet
42129198Scognet#include <sys/cdefs.h>
43129198Scognet__FBSDID("$FreeBSD: head/sys/arm/arm/nexus.c 129198 2004-05-14 11:46:45Z cognet $");
44129198Scognet
45129198Scognet#include <sys/param.h>
46129198Scognet#include <sys/systm.h>
47129198Scognet#include <sys/bus.h>
48129198Scognet#include <sys/kernel.h>
49129198Scognet#include <sys/malloc.h>
50129198Scognet#include <sys/module.h>
51129198Scognet#include <machine/bus.h>
52129198Scognet#include <sys/rman.h>
53129198Scognet#include <sys/interrupt.h>
54129198Scognet
55129198Scognet#include <machine/vmparam.h>
56129198Scognet#include <machine/pcb.h>
57129198Scognet#include <vm/vm.h>
58129198Scognet#include <vm/pmap.h>
59129198Scognet#include <machine/pmap.h>
60129198Scognet
61129198Scognet#include <machine/resource.h>
62129198Scognet#include <machine/intr.h>
63129198Scognet
64129198Scognetstatic MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
65129198Scognet
66129198Scognetstruct nexus_device {
67129198Scognet	struct resource_list	nx_resources;
68129198Scognet};
69129198Scognet
70129198Scognet#define DEVTONX(dev)	((struct nexus_device *)device_get_ivars(dev))
71129198Scognet
72129198Scognetstatic struct rman mem_rman;
73129198Scognet
74129198Scognetstatic	int nexus_probe(device_t);
75129198Scognetstatic	int nexus_attach(device_t);
76129198Scognetstatic	int nexus_print_child(device_t, device_t);
77129198Scognetstatic	device_t nexus_add_child(device_t, int, const char *, int);
78129198Scognetstatic	struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
79129198Scognet	u_long, u_long, u_long, u_int);
80129198Scognetstatic	int nexus_activate_resource(device_t, device_t, int, int,
81129198Scognet	struct resource *);
82129198Scognetstatic int
83129198Scognetnexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
84129198Scognet        driver_intr_t *intr, void *arg, void **cookiep);
85129198Scognetstatic device_method_t nexus_methods[] = {
86129198Scognet	/* Device interface */
87129198Scognet	DEVMETHOD(device_probe,		nexus_probe),
88129198Scognet	DEVMETHOD(device_attach,	nexus_attach),
89129198Scognet	/* Bus interface */
90129198Scognet	DEVMETHOD(bus_print_child,	nexus_print_child),
91129198Scognet	DEVMETHOD(bus_add_child,	nexus_add_child),
92129198Scognet	DEVMETHOD(bus_alloc_resource,	nexus_alloc_resource),
93129198Scognet	DEVMETHOD(bus_activate_resource,	nexus_activate_resource),
94129198Scognet	DEVMETHOD(bus_setup_intr,	nexus_setup_intr),
95129198Scognet	{ 0, 0 }
96129198Scognet};
97129198Scognet
98129198Scognetstatic driver_t nexus_driver = {
99129198Scognet	"nexus",
100129198Scognet	nexus_methods,
101129198Scognet	1			/* no softc */
102129198Scognet};
103129198Scognetstatic devclass_t nexus_devclass;
104129198Scognet
105129198Scognetstatic int
106129198Scognetnexus_probe(device_t dev)
107129198Scognet{
108129198Scognet	device_quiet(dev);	/* suppress attach message for neatness */
109129198Scognet
110129198Scognet	mem_rman.rm_start = 0;
111129198Scognet	mem_rman.rm_end = ~0u;
112129198Scognet	mem_rman.rm_type = RMAN_ARRAY;
113129198Scognet	mem_rman.rm_descr = "I/O memory addresses";
114129198Scognet	if (rman_init(&mem_rman)
115129198Scognet		|| rman_manage_region(&mem_rman, 0, ~0u))
116129198Scognet		panic("nexus_probe mem_rman");
117129198Scognet
118129198Scognet	return (0);
119129198Scognet	return bus_generic_probe(dev);
120129198Scognet}
121129198Scognet
122129198Scognetstatic int
123129198Scognetnexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
124129198Scognet    driver_intr_t *intr, void *arg, void **cookiep)
125129198Scognet{
126129198Scognet	arm_setup_irqhandler(device_get_nameunit(child),
127129198Scognet	    intr, arg, res->r_start, flags, cookiep);
128129198Scognet	return (0);
129129198Scognet}
130129198Scognet
131129198Scognet
132129198Scognetstatic int
133129198Scognetnexus_attach(device_t dev)
134129198Scognet{
135129198Scognet	/*
136129198Scognet	 * First, deal with the children we know about already
137129198Scognet	 */
138129198Scognet	printf("avant\n");
139129198Scognet	bus_generic_probe(dev);
140129198Scognet	bus_generic_attach(dev);
141129198Scognet	printf("nexus_attach\n");
142129198Scognet
143129198Scognet	return 0;
144129198Scognet}
145129198Scognet
146129198Scognet
147129198Scognetstatic int
148129198Scognetnexus_print_child(device_t bus, device_t child)
149129198Scognet{
150129198Scognet	int retval = 0;
151129198Scognet
152129198Scognet	retval += bus_print_child_header(bus, child);
153129198Scognet	retval += printf(" on motherboard\n");	/* XXX "motherboard", ick */
154129198Scognet
155129198Scognet	return (retval);
156129198Scognet}
157129198Scognet
158129198Scognet
159129198Scognetstatic device_t
160129198Scognetnexus_add_child(device_t bus, int order, const char *name, int unit)
161129198Scognet{
162129198Scognet	device_t	child;
163129198Scognet	struct nexus_device *ndev;
164129198Scognet
165129198Scognet	ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
166129198Scognet	if (!ndev)
167129198Scognet		return(0);
168129198Scognet	resource_list_init(&ndev->nx_resources);
169129198Scognet
170129198Scognet	child = device_add_child_ordered(bus, order, name, unit);
171129198Scognet
172129198Scognet	/* should we free this in nexus_child_detached? */
173129198Scognet	device_set_ivars(child, ndev);
174129198Scognet
175129198Scognet	return(child);
176129198Scognet}
177129198Scognet
178129198Scognet
179129198Scognet/*
180129198Scognet * Allocate a resource on behalf of child.  NB: child is usually going to be a
181129198Scognet * child of one of our descendants, not a direct child of nexus0.
182129198Scognet * (Exceptions include footbridge.)
183129198Scognet */
184129198Scognet#define ARM_BUS_SPACE_MEM 1
185129198Scognetstatic struct resource *
186129198Scognetnexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
187129198Scognet	u_long start, u_long end, u_long count, u_int flags)
188129198Scognet{
189129198Scognet	struct resource *rv;
190129198Scognet	struct rman *rm;
191129198Scognet	int needactivate = flags & RF_ACTIVE;
192129198Scognet
193129198Scognet	switch (type) {
194129198Scognet	case SYS_RES_MEMORY:
195129198Scognet		rm = &mem_rman;
196129198Scognet		break;
197129198Scognet
198129198Scognet	default:
199129198Scognet		return 0;
200129198Scognet	}
201129198Scognet
202129198Scognet	rv = rman_reserve_resource(rm, start, end, count, flags, child);
203129198Scognet	if (rv == 0)
204129198Scognet		return 0;
205129198Scognet
206129198Scognet	rman_set_bustag(rv, (void*)ARM_BUS_SPACE_MEM);
207129198Scognet	rman_set_bushandle(rv, rv->r_start);
208129198Scognet
209129198Scognet	if (needactivate) {
210129198Scognet		if (bus_activate_resource(child, type, *rid, rv)) {
211129198Scognet			rman_release_resource(rv);
212129198Scognet			return 0;
213129198Scognet		}
214129198Scognet	}
215129198Scognet
216129198Scognet	return rv;
217129198Scognet}
218129198Scognet
219129198Scognet
220129198Scognetstatic int
221129198Scognetnexus_activate_resource(device_t bus, device_t child, int type, int rid,
222129198Scognet	struct resource *r)
223129198Scognet{
224129198Scognet	/*
225129198Scognet	 * If this is a memory resource, map it into the kernel.
226129198Scognet	 */
227129198Scognet	if (rman_get_bustag(r) == (void*)ARM_BUS_SPACE_MEM) {
228129198Scognet		caddr_t vaddr = 0;
229129198Scognet		u_int32_t paddr;
230129198Scognet		u_int32_t psize;
231129198Scognet		u_int32_t poffs;
232129198Scognet
233129198Scognet		paddr = rman_get_start(r);
234129198Scognet		psize = rman_get_size(r);
235129198Scognet		poffs = paddr - trunc_page(paddr);
236129198Scognet		vaddr = (caddr_t) pmap_mapdev(paddr-poffs, psize+poffs) + poffs;
237129198Scognet		rman_set_virtual(r, vaddr);
238129198Scognet		rman_set_bushandle(r, (bus_space_handle_t) vaddr);
239129198Scognet	}
240129198Scognet	return (rman_activate_resource(r));
241129198Scognet}
242129198Scognet
243129198ScognetDRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
244