nexus.c revision 212413
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.
15182934Sraj *
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 212413 2010-09-10 11:19:03Z avg $");
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);
77212413Savgstatic	device_t nexus_add_child(device_t, u_int, const char *, int);
78129198Scognetstatic	struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
79182934Sraj    u_long, u_long, u_long, u_int);
80129198Scognetstatic	int nexus_activate_resource(device_t, device_t, int, int,
81182934Sraj    struct resource *);
82182934Srajstatic int nexus_setup_intr(device_t dev, device_t child, struct resource *res,
83182934Sraj    int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep);
84182934Srajstatic int nexus_teardown_intr(device_t, device_t, struct resource *, void *);
85147166Scognet
86129198Scognetstatic device_method_t nexus_methods[] = {
87129198Scognet	/* Device interface */
88129198Scognet	DEVMETHOD(device_probe,		nexus_probe),
89129198Scognet	DEVMETHOD(device_attach,	nexus_attach),
90129198Scognet	/* Bus interface */
91129198Scognet	DEVMETHOD(bus_print_child,	nexus_print_child),
92129198Scognet	DEVMETHOD(bus_add_child,	nexus_add_child),
93129198Scognet	DEVMETHOD(bus_alloc_resource,	nexus_alloc_resource),
94129198Scognet	DEVMETHOD(bus_activate_resource,	nexus_activate_resource),
95129198Scognet	DEVMETHOD(bus_setup_intr,	nexus_setup_intr),
96147166Scognet	DEVMETHOD(bus_teardown_intr,	nexus_teardown_intr),
97129198Scognet	{ 0, 0 }
98129198Scognet};
99129198Scognet
100129198Scognetstatic driver_t nexus_driver = {
101129198Scognet	"nexus",
102129198Scognet	nexus_methods,
103129198Scognet	1			/* no softc */
104129198Scognet};
105129198Scognetstatic devclass_t nexus_devclass;
106129198Scognet
107129198Scognetstatic int
108129198Scognetnexus_probe(device_t dev)
109129198Scognet{
110209129Sraj
111129198Scognet	device_quiet(dev);	/* suppress attach message for neatness */
112182934Sraj
113209129Sraj	return (BUS_PROBE_DEFAULT);
114129198Scognet}
115129198Scognet
116129198Scognetstatic int
117129198Scognetnexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
118166901Spiso    driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
119129198Scognet{
120163693Scognet
121177105Sraj	if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
122177105Sraj		flags |= INTR_EXCL;
123177105Sraj
124182933Sraj	arm_setup_irqhandler(device_get_nameunit(child),
125182933Sraj	    filt, intr, arg, rman_get_start(res), flags, cookiep);
126129198Scognet	return (0);
127129198Scognet}
128129198Scognet
129147166Scognetstatic int
130147166Scognetnexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
131147166Scognet{
132129198Scognet
133182933Sraj	return (arm_remove_irqhandler(rman_get_start(r), ih));
134147166Scognet}
135147166Scognet
136129198Scognetstatic int
137129198Scognetnexus_attach(device_t dev)
138129198Scognet{
139182934Sraj
140209232Sraj	mem_rman.rm_start = 0;
141209232Sraj	mem_rman.rm_end = ~0u;
142209232Sraj	mem_rman.rm_type = RMAN_ARRAY;
143209232Sraj	mem_rman.rm_descr = "I/O memory addresses";
144209232Sraj	if (rman_init(&mem_rman) || rman_manage_region(&mem_rman, 0, ~0u))
145209232Sraj		panic("nexus_probe mem_rman");
146209232Sraj
147129198Scognet	/*
148129198Scognet	 * First, deal with the children we know about already
149129198Scognet	 */
150129198Scognet	bus_generic_probe(dev);
151129198Scognet	bus_generic_attach(dev);
152182934Sraj
153182934Sraj	return (0);
154129198Scognet}
155129198Scognet
156129198Scognet
157129198Scognetstatic int
158129198Scognetnexus_print_child(device_t bus, device_t child)
159129198Scognet{
160129198Scognet	int retval = 0;
161182934Sraj
162129198Scognet	retval += bus_print_child_header(bus, child);
163129198Scognet	retval += printf(" on motherboard\n");	/* XXX "motherboard", ick */
164182934Sraj
165129198Scognet	return (retval);
166129198Scognet}
167129198Scognet
168129198Scognetstatic device_t
169212413Savgnexus_add_child(device_t bus, u_int order, const char *name, int unit)
170129198Scognet{
171182934Sraj	device_t child;
172129198Scognet	struct nexus_device *ndev;
173182934Sraj
174129198Scognet	ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
175129198Scognet	if (!ndev)
176182934Sraj		return (0);
177129198Scognet	resource_list_init(&ndev->nx_resources);
178129198Scognet
179129198Scognet	child = device_add_child_ordered(bus, order, name, unit);
180182934Sraj
181129198Scognet	/* should we free this in nexus_child_detached? */
182129198Scognet	device_set_ivars(child, ndev);
183182934Sraj
184182934Sraj	return (child);
185129198Scognet}
186129198Scognet
187129198Scognet
188129198Scognet/*
189129198Scognet * Allocate a resource on behalf of child.  NB: child is usually going to be a
190129198Scognet * child of one of our descendants, not a direct child of nexus0.
191129198Scognet * (Exceptions include footbridge.)
192129198Scognet */
193129198Scognet#define ARM_BUS_SPACE_MEM 1
194129198Scognetstatic struct resource *
195129198Scognetnexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
196182934Sraj    u_long start, u_long end, u_long count, u_int flags)
197129198Scognet{
198129198Scognet	struct resource *rv;
199129198Scognet	struct rman *rm;
200129198Scognet	int needactivate = flags & RF_ACTIVE;
201129198Scognet
202129198Scognet	switch (type) {
203129198Scognet	case SYS_RES_MEMORY:
204129198Scognet		rm = &mem_rman;
205129198Scognet		break;
206182934Sraj
207129198Scognet	default:
208182934Sraj		return (0);
209129198Scognet	}
210129198Scognet
211129198Scognet	rv = rman_reserve_resource(rm, start, end, count, flags, child);
212129198Scognet	if (rv == 0)
213182934Sraj		return (0);
214129198Scognet
215157891Simp	rman_set_rid(rv, *rid);
216129198Scognet	rman_set_bustag(rv, (void*)ARM_BUS_SPACE_MEM);
217182934Sraj	rman_set_bushandle(rv, rman_get_start(rv));
218182934Sraj
219129198Scognet	if (needactivate) {
220129198Scognet		if (bus_activate_resource(child, type, *rid, rv)) {
221129198Scognet			rman_release_resource(rv);
222182934Sraj			return (0);
223129198Scognet		}
224129198Scognet	}
225182934Sraj
226182934Sraj	return (rv);
227129198Scognet}
228129198Scognet
229129198Scognet
230129198Scognetstatic int
231129198Scognetnexus_activate_resource(device_t bus, device_t child, int type, int rid,
232182934Sraj    struct resource *r)
233129198Scognet{
234129198Scognet	/*
235129198Scognet	 * If this is a memory resource, map it into the kernel.
236129198Scognet	 */
237129198Scognet	if (rman_get_bustag(r) == (void*)ARM_BUS_SPACE_MEM) {
238129198Scognet		caddr_t vaddr = 0;
239129198Scognet		u_int32_t paddr;
240129198Scognet		u_int32_t psize;
241129198Scognet		u_int32_t poffs;
242182934Sraj
243129198Scognet		paddr = rman_get_start(r);
244129198Scognet		psize = rman_get_size(r);
245129198Scognet		poffs = paddr - trunc_page(paddr);
246129198Scognet		vaddr = (caddr_t) pmap_mapdev(paddr-poffs, psize+poffs) + poffs;
247129198Scognet		rman_set_virtual(r, vaddr);
248129198Scognet		rman_set_bushandle(r, (bus_space_handle_t) vaddr);
249129198Scognet	}
250129198Scognet	return (rman_activate_resource(r));
251129198Scognet}
252129198Scognet
253129198ScognetDRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
254