nexus.c revision 182933
1139735Simp/*-
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 182933 2008-09-11 12:36:13Z raj $");
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,
84166901Spiso        driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep);
85147166Scognetstatic int
86147166Scognetnexus_teardown_intr(device_t, device_t, struct resource *, void *);
87147166Scognet
88129198Scognetstatic device_method_t nexus_methods[] = {
89129198Scognet	/* Device interface */
90129198Scognet	DEVMETHOD(device_probe,		nexus_probe),
91129198Scognet	DEVMETHOD(device_attach,	nexus_attach),
92129198Scognet	/* Bus interface */
93129198Scognet	DEVMETHOD(bus_print_child,	nexus_print_child),
94129198Scognet	DEVMETHOD(bus_add_child,	nexus_add_child),
95129198Scognet	DEVMETHOD(bus_alloc_resource,	nexus_alloc_resource),
96129198Scognet	DEVMETHOD(bus_activate_resource,	nexus_activate_resource),
97129198Scognet	DEVMETHOD(bus_setup_intr,	nexus_setup_intr),
98147166Scognet	DEVMETHOD(bus_teardown_intr,	nexus_teardown_intr),
99129198Scognet	{ 0, 0 }
100129198Scognet};
101129198Scognet
102129198Scognetstatic driver_t nexus_driver = {
103129198Scognet	"nexus",
104129198Scognet	nexus_methods,
105129198Scognet	1			/* no softc */
106129198Scognet};
107129198Scognetstatic devclass_t nexus_devclass;
108129198Scognet
109129198Scognetstatic int
110129198Scognetnexus_probe(device_t dev)
111129198Scognet{
112129198Scognet	device_quiet(dev);	/* suppress attach message for neatness */
113129198Scognet
114129198Scognet	mem_rman.rm_start = 0;
115129198Scognet	mem_rman.rm_end = ~0u;
116129198Scognet	mem_rman.rm_type = RMAN_ARRAY;
117129198Scognet	mem_rman.rm_descr = "I/O memory addresses";
118129198Scognet	if (rman_init(&mem_rman)
119129198Scognet		|| rman_manage_region(&mem_rman, 0, ~0u))
120129198Scognet		panic("nexus_probe mem_rman");
121129198Scognet
122129198Scognet	return (0);
123129198Scognet}
124129198Scognet
125129198Scognetstatic int
126129198Scognetnexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
127166901Spiso    driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
128129198Scognet{
129163693Scognet
130177105Sraj	if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
131177105Sraj		flags |= INTR_EXCL;
132177105Sraj
133182933Sraj	arm_setup_irqhandler(device_get_nameunit(child),
134182933Sraj	    filt, intr, arg, rman_get_start(res), flags, cookiep);
135129198Scognet	return (0);
136129198Scognet}
137129198Scognet
138147166Scognetstatic int
139147166Scognetnexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
140147166Scognet{
141129198Scognet
142182933Sraj	return (arm_remove_irqhandler(rman_get_start(r), ih));
143147166Scognet}
144147166Scognet
145129198Scognetstatic int
146129198Scognetnexus_attach(device_t dev)
147129198Scognet{
148129198Scognet	/*
149129198Scognet	 * First, deal with the children we know about already
150129198Scognet	 */
151129198Scognet	bus_generic_probe(dev);
152129198Scognet	bus_generic_attach(dev);
153129198Scognet
154129198Scognet	return 0;
155129198Scognet}
156129198Scognet
157129198Scognet
158129198Scognetstatic int
159129198Scognetnexus_print_child(device_t bus, device_t child)
160129198Scognet{
161129198Scognet	int retval = 0;
162129198Scognet
163129198Scognet	retval += bus_print_child_header(bus, child);
164129198Scognet	retval += printf(" on motherboard\n");	/* XXX "motherboard", ick */
165129198Scognet
166129198Scognet	return (retval);
167129198Scognet}
168129198Scognet
169129198Scognet
170129198Scognetstatic device_t
171129198Scognetnexus_add_child(device_t bus, int order, const char *name, int unit)
172129198Scognet{
173129198Scognet	device_t	child;
174129198Scognet	struct nexus_device *ndev;
175129198Scognet
176129198Scognet	ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
177129198Scognet	if (!ndev)
178129198Scognet		return(0);
179129198Scognet	resource_list_init(&ndev->nx_resources);
180129198Scognet
181129198Scognet	child = device_add_child_ordered(bus, order, name, unit);
182129198Scognet
183129198Scognet	/* should we free this in nexus_child_detached? */
184129198Scognet	device_set_ivars(child, ndev);
185129198Scognet
186129198Scognet	return(child);
187129198Scognet}
188129198Scognet
189129198Scognet
190129198Scognet/*
191129198Scognet * Allocate a resource on behalf of child.  NB: child is usually going to be a
192129198Scognet * child of one of our descendants, not a direct child of nexus0.
193129198Scognet * (Exceptions include footbridge.)
194129198Scognet */
195129198Scognet#define ARM_BUS_SPACE_MEM 1
196129198Scognetstatic struct resource *
197129198Scognetnexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
198129198Scognet	u_long start, u_long end, u_long count, u_int flags)
199129198Scognet{
200129198Scognet	struct resource *rv;
201129198Scognet	struct rman *rm;
202129198Scognet	int needactivate = flags & RF_ACTIVE;
203129198Scognet
204129198Scognet	switch (type) {
205129198Scognet	case SYS_RES_MEMORY:
206129198Scognet		rm = &mem_rman;
207129198Scognet		break;
208129198Scognet
209129198Scognet	default:
210129198Scognet		return 0;
211129198Scognet	}
212129198Scognet
213129198Scognet	rv = rman_reserve_resource(rm, start, end, count, flags, child);
214129198Scognet	if (rv == 0)
215129198Scognet		return 0;
216129198Scognet
217157891Simp	rman_set_rid(rv, *rid);
218129198Scognet	rman_set_bustag(rv, (void*)ARM_BUS_SPACE_MEM);
219150552Scognet	rman_set_bushandle(rv, rman_get_start(rv));
220129198Scognet
221129198Scognet	if (needactivate) {
222129198Scognet		if (bus_activate_resource(child, type, *rid, rv)) {
223129198Scognet			rman_release_resource(rv);
224129198Scognet			return 0;
225129198Scognet		}
226129198Scognet	}
227129198Scognet
228129198Scognet	return rv;
229129198Scognet}
230129198Scognet
231129198Scognet
232129198Scognetstatic int
233129198Scognetnexus_activate_resource(device_t bus, device_t child, int type, int rid,
234129198Scognet	struct resource *r)
235129198Scognet{
236129198Scognet	/*
237129198Scognet	 * If this is a memory resource, map it into the kernel.
238129198Scognet	 */
239129198Scognet	if (rman_get_bustag(r) == (void*)ARM_BUS_SPACE_MEM) {
240129198Scognet		caddr_t vaddr = 0;
241129198Scognet		u_int32_t paddr;
242129198Scognet		u_int32_t psize;
243129198Scognet		u_int32_t poffs;
244129198Scognet
245129198Scognet		paddr = rman_get_start(r);
246129198Scognet		psize = rman_get_size(r);
247129198Scognet		poffs = paddr - trunc_page(paddr);
248129198Scognet		vaddr = (caddr_t) pmap_mapdev(paddr-poffs, psize+poffs) + poffs;
249129198Scognet		rman_set_virtual(r, vaddr);
250129198Scognet		rman_set_bushandle(r, (bus_space_handle_t) vaddr);
251129198Scognet	}
252129198Scognet	return (rman_activate_resource(r));
253129198Scognet}
254129198Scognet
255129198ScognetDRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
256