1/*-
2 * Copyright 1998 Massachusetts Institute of Technology
3 *
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all
9 * supporting documentation, and that the name of M.I.T. not be used
10 * in advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission.  M.I.T. makes
12 * no representations about the suitability of this software for any
13 * purpose.  It is provided "as is" without express or implied
14 * warranty.
15 *
16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30
31/*
32 * This code implements a `root nexus' for Arm Architecture
33 * machines.  The function of the root nexus is to serve as an
34 * attachment point for both processors and buses, and to manage
35 * resources which are common to all of them.  In particular,
36 * this code implements the core resource managers for interrupt
37 * requests and I/O memory address space.
38 */
39
40#include "opt_platform.h"
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/bus.h>
45#include <sys/interrupt.h>
46#include <sys/kernel.h>
47#include <sys/malloc.h>
48#include <sys/module.h>
49#include <sys/rman.h>
50
51#include <vm/vm.h>
52#include <vm/pmap.h>
53
54#include <machine/bus.h>
55#include <machine/pcb.h>
56#include <machine/intr.h>
57#include <machine/resource.h>
58#include <machine/vmparam.h>
59
60#include <arm/arm/nexusvar.h>
61
62#ifdef FDT
63#include <machine/fdt.h>
64#include <dev/ofw/ofw_bus_subr.h>
65#include "ofw_bus_if.h"
66#endif
67
68static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
69
70struct nexus_device {
71	struct resource_list	nx_resources;
72};
73
74#define DEVTONX(dev)	((struct nexus_device *)device_get_ivars(dev))
75
76static struct rman mem_rman;
77static struct rman irq_rman;
78
79static device_probe_t		nexus_probe;
80static device_attach_t		nexus_attach;
81
82static bus_add_child_t		nexus_add_child;
83static bus_print_child_t	nexus_print_child;
84
85static bus_activate_resource_t	nexus_activate_resource;
86static bus_deactivate_resource_t nexus_deactivate_resource;
87static bus_get_rman_t		nexus_get_rman;
88static bus_map_resource_t	nexus_map_resource;
89static bus_unmap_resource_t	nexus_unmap_resource;
90
91#ifdef SMP
92static bus_bind_intr_t		nexus_bind_intr;
93#endif
94static bus_config_intr_t	nexus_config_intr;
95static bus_describe_intr_t	nexus_describe_intr;
96static bus_setup_intr_t		nexus_setup_intr;
97static bus_teardown_intr_t	nexus_teardown_intr;
98
99static bus_get_bus_tag_t	nexus_get_bus_tag;
100static bus_get_dma_tag_t	nexus_get_dma_tag;
101
102#ifdef FDT
103static ofw_bus_map_intr_t	nexus_ofw_map_intr;
104#endif
105
106/*
107 * Normally NULL (which results in defaults which are handled in
108 * busdma_machdep), platform init code can use nexus_set_dma_tag() to set this
109 * to a tag that will be inherited by all busses and devices on the platform.
110 */
111static bus_dma_tag_t nexus_dma_tag;
112
113static device_method_t nexus_methods[] = {
114	/* Device interface */
115	DEVMETHOD(device_probe,		nexus_probe),
116	DEVMETHOD(device_attach,	nexus_attach),
117
118	/* Bus interface */
119	DEVMETHOD(bus_add_child,	nexus_add_child),
120	DEVMETHOD(bus_print_child,	nexus_print_child),
121	DEVMETHOD(bus_activate_resource, nexus_activate_resource),
122	DEVMETHOD(bus_adjust_resource,	bus_generic_rman_adjust_resource),
123	DEVMETHOD(bus_alloc_resource,	bus_generic_rman_alloc_resource),
124	DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
125	DEVMETHOD(bus_get_rman,		nexus_get_rman),
126	DEVMETHOD(bus_map_resource,	nexus_map_resource),
127	DEVMETHOD(bus_release_resource,	bus_generic_rman_release_resource),
128	DEVMETHOD(bus_unmap_resource,	nexus_unmap_resource),
129#ifdef SMP
130	DEVMETHOD(bus_bind_intr,	nexus_bind_intr),
131#endif
132	DEVMETHOD(bus_config_intr,	nexus_config_intr),
133	DEVMETHOD(bus_describe_intr,	nexus_describe_intr),
134	DEVMETHOD(bus_setup_intr,	nexus_setup_intr),
135	DEVMETHOD(bus_teardown_intr,	nexus_teardown_intr),
136	DEVMETHOD(bus_get_bus_tag,	nexus_get_bus_tag),
137	DEVMETHOD(bus_get_dma_tag,	nexus_get_dma_tag),
138#ifdef FDT
139	/* ofw_bus interface */
140	DEVMETHOD(ofw_bus_map_intr,	nexus_ofw_map_intr),
141#endif
142	DEVMETHOD_END
143};
144
145static driver_t nexus_driver = {
146	"nexus",
147	nexus_methods,
148	1			/* no softc */
149};
150
151EARLY_DRIVER_MODULE(nexus, root, nexus_driver, 0, 0,
152    BUS_PASS_BUS + BUS_PASS_ORDER_EARLY);
153
154static int
155nexus_probe(device_t dev)
156{
157
158	device_quiet(dev);	/* suppress attach message for neatness */
159
160	return (BUS_PROBE_DEFAULT);
161}
162
163static int
164nexus_attach(device_t dev)
165{
166
167	mem_rman.rm_start = 0;
168	mem_rman.rm_end = BUS_SPACE_MAXADDR;
169	mem_rman.rm_type = RMAN_ARRAY;
170	mem_rman.rm_descr = "I/O memory addresses";
171	if (rman_init(&mem_rman) ||
172	    rman_manage_region(&mem_rman, 0, BUS_SPACE_MAXADDR))
173		panic("nexus_probe mem_rman");
174	irq_rman.rm_start = 0;
175	irq_rman.rm_end = ~0;
176	irq_rman.rm_type = RMAN_ARRAY;
177	irq_rman.rm_descr = "Interrupts";
178	if (rman_init(&irq_rman) || rman_manage_region(&irq_rman, 0, ~0))
179		panic("nexus_attach irq_rman");
180
181	/* First, add ofwbus0. */
182	device_add_child(dev, "ofwbus", 0);
183
184	/*
185	 * Next, deal with the children we know about already.
186	 */
187	bus_generic_probe(dev);
188	bus_generic_attach(dev);
189
190	return (0);
191}
192
193static int
194nexus_print_child(device_t bus, device_t child)
195{
196	int retval = 0;
197
198	retval += bus_print_child_header(bus, child);
199	retval += printf("\n");
200
201	return (retval);
202}
203
204static device_t
205nexus_add_child(device_t bus, u_int order, const char *name, int unit)
206{
207	device_t child;
208	struct nexus_device *ndev;
209
210	ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
211	if (!ndev)
212		return (0);
213	resource_list_init(&ndev->nx_resources);
214
215	child = device_add_child_ordered(bus, order, name, unit);
216
217	device_set_ivars(child, ndev);
218
219	return (child);
220}
221
222static struct rman *
223nexus_get_rman(device_t bus, int type, u_int flags)
224{
225	switch (type) {
226	case SYS_RES_IRQ:
227		return (&irq_rman);
228	case SYS_RES_MEMORY:
229	case SYS_RES_IOPORT:
230		return (&mem_rman);
231	default:
232		return (NULL);
233	}
234}
235
236static bus_space_tag_t
237nexus_get_bus_tag(device_t bus __unused, device_t child __unused)
238{
239
240#ifdef FDT
241	return (fdtbus_bs_tag);
242#else
243	return ((void *)1);
244#endif
245}
246
247static bus_dma_tag_t
248nexus_get_dma_tag(device_t dev, device_t child)
249{
250
251	return (nexus_dma_tag);
252}
253
254void
255nexus_set_dma_tag(bus_dma_tag_t tag)
256{
257
258	nexus_dma_tag = tag;
259}
260
261static int
262nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
263    enum intr_polarity pol)
264{
265	int ret = ENODEV;
266
267	device_printf(dev, "bus_config_intr is obsolete and not supported!\n");
268	ret = EOPNOTSUPP;
269	return (ret);
270}
271
272static int
273nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
274    driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
275{
276
277	if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
278		flags |= INTR_EXCL;
279
280	return (intr_setup_irq(child, res, filt, intr, arg, flags, cookiep));
281}
282
283static int
284nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
285{
286
287	return (intr_teardown_irq(child, r, ih));
288}
289
290static int
291nexus_describe_intr(device_t dev, device_t child, struct resource *irq,
292    void *cookie, const char *descr)
293{
294
295	return (intr_describe_irq(child, irq, cookie, descr));
296}
297
298#ifdef SMP
299static int
300nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
301{
302
303	return (intr_bind_irq(child, irq, cpu));
304}
305#endif
306
307static int
308nexus_activate_resource(device_t bus, device_t child, struct resource *r)
309{
310	int err;
311
312	switch (rman_get_type(r)) {
313	case SYS_RES_MEMORY:
314	case SYS_RES_IOPORT:
315		return (bus_generic_rman_activate_resource(bus, child, r));
316	case SYS_RES_IRQ:
317		err = rman_activate_resource(r);
318		if (err != 0)
319			return (err);
320		err = intr_activate_irq(child, r);
321		if (err != 0) {
322			rman_deactivate_resource(r);
323			return (err);
324		}
325		return (0);
326	default:
327		return (EINVAL);
328	}
329}
330
331static int
332nexus_map_resource(device_t bus, device_t child, struct resource *r,
333    struct resource_map_request *argsp, struct resource_map *map)
334{
335	struct resource_map_request args;
336	rman_res_t length, start;
337	int error;
338
339	/* Resources must be active to be mapped. */
340	if (!(rman_get_flags(r) & RF_ACTIVE))
341		return (ENXIO);
342
343	switch (rman_get_type(r)) {
344	case SYS_RES_MEMORY:
345	case SYS_RES_IOPORT:
346		break;
347	default:
348		return (EINVAL);
349	}
350
351	resource_init_map_request(&args);
352	error = resource_validate_map_request(r, argsp, &args, &start, &length);
353	if (error)
354		return (error);
355
356#ifdef FDT
357	error = bus_space_map(fdtbus_bs_tag, start, length, 0,
358	    &map->r_bushandle);
359	if (error)
360		return (error);
361	map->r_bustag = fdtbus_bs_tag;
362	map->r_vaddr = (void *)map->r_bushandle;
363#else
364	map->r_vaddr = pmap_mapdev(start, length);
365	if (map->r_vaddr == NULL)
366		return (ENOMEM);
367	map->r_bustag = (void *)1;
368	map->r_bushandle = (bus_space_handle_t)map->r_vaddr;
369#endif
370	map->r_size = length;
371	return (0);
372}
373
374static int
375nexus_unmap_resource(device_t bus, device_t child, struct resource *r,
376    struct resource_map *map)
377{
378
379	switch (rman_get_type(r)) {
380	case SYS_RES_MEMORY:
381	case SYS_RES_IOPORT:
382#ifdef FDT
383		bus_space_unmap(map->r_bustag, map->r_bushandle, map->r_size);
384#else
385		pmap_unmapdev(map->r_vaddr, map->r_size);
386#endif
387		return (0);
388	default:
389		return (EINVAL);
390	}
391}
392
393static int
394nexus_deactivate_resource(device_t bus, device_t child, struct resource *r)
395{
396	int error;
397
398	switch (rman_get_type(r)) {
399	case SYS_RES_MEMORY:
400	case SYS_RES_IOPORT:
401		return (bus_generic_rman_deactivate_resource(bus, child, r));
402	case SYS_RES_IRQ:
403		error = rman_deactivate_resource(r);
404		if (error)
405			return (error);
406		intr_deactivate_irq(child, r);
407		return (0);
408	default:
409		return (EINVAL);
410	}
411}
412
413#ifdef FDT
414static int
415nexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, int icells,
416    pcell_t *intr)
417{
418	u_int irq;
419	struct intr_map_data_fdt *fdt_data;
420	size_t len;
421
422	len = sizeof(*fdt_data) + icells * sizeof(pcell_t);
423	fdt_data = (struct intr_map_data_fdt *)intr_alloc_map_data(
424	    INTR_MAP_DATA_FDT, len, M_WAITOK | M_ZERO);
425	fdt_data->iparent = iparent;
426	fdt_data->ncells = icells;
427	memcpy(fdt_data->cells, intr, icells * sizeof(pcell_t));
428	irq = intr_map_irq(NULL, iparent, (struct intr_map_data *)fdt_data);
429	return (irq);
430}
431#endif /* FDT */
432