xref: /freebsd/sys/arm/arm/nexus.c (revision 564df56098e0b4afb0e45e9bc22b6168b3271071)
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 
68 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
69 
70 struct nexus_device {
71 	struct resource_list	nx_resources;
72 };
73 
74 #define DEVTONX(dev)	((struct nexus_device *)device_get_ivars(dev))
75 
76 static struct rman mem_rman;
77 static struct rman irq_rman;
78 
79 static device_probe_t		nexus_probe;
80 static device_attach_t		nexus_attach;
81 
82 static bus_add_child_t		nexus_add_child;
83 static bus_print_child_t	nexus_print_child;
84 
85 static bus_activate_resource_t	nexus_activate_resource;
86 static bus_deactivate_resource_t nexus_deactivate_resource;
87 static bus_get_rman_t		nexus_get_rman;
88 static bus_map_resource_t	nexus_map_resource;
89 static bus_unmap_resource_t	nexus_unmap_resource;
90 
91 #ifdef SMP
92 static bus_bind_intr_t		nexus_bind_intr;
93 #endif
94 static bus_config_intr_t	nexus_config_intr;
95 static bus_describe_intr_t	nexus_describe_intr;
96 static bus_setup_intr_t		nexus_setup_intr;
97 static bus_teardown_intr_t	nexus_teardown_intr;
98 
99 static bus_get_bus_tag_t	nexus_get_bus_tag;
100 static bus_get_dma_tag_t	nexus_get_dma_tag;
101 
102 #ifdef FDT
103 static 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  */
111 static bus_dma_tag_t nexus_dma_tag;
112 
113 static device_method_t nexus_methods[] = {
114 	/* Device interface */
115 	DEVMETHOD(device_probe,		nexus_probe),
116 	DEVMETHOD(device_attach,	nexus_attach),
117 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
118 
119 	/* Bus interface */
120 	DEVMETHOD(bus_add_child,	nexus_add_child),
121 	DEVMETHOD(bus_print_child,	nexus_print_child),
122 	DEVMETHOD(bus_activate_resource, nexus_activate_resource),
123 	DEVMETHOD(bus_adjust_resource,	bus_generic_rman_adjust_resource),
124 	DEVMETHOD(bus_alloc_resource,	bus_generic_rman_alloc_resource),
125 	DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
126 	DEVMETHOD(bus_get_rman,		nexus_get_rman),
127 	DEVMETHOD(bus_map_resource,	nexus_map_resource),
128 	DEVMETHOD(bus_release_resource,	bus_generic_rman_release_resource),
129 	DEVMETHOD(bus_unmap_resource,	nexus_unmap_resource),
130 #ifdef SMP
131 	DEVMETHOD(bus_bind_intr,	nexus_bind_intr),
132 #endif
133 	DEVMETHOD(bus_config_intr,	nexus_config_intr),
134 	DEVMETHOD(bus_describe_intr,	nexus_describe_intr),
135 	DEVMETHOD(bus_setup_intr,	nexus_setup_intr),
136 	DEVMETHOD(bus_teardown_intr,	nexus_teardown_intr),
137 	DEVMETHOD(bus_get_bus_tag,	nexus_get_bus_tag),
138 	DEVMETHOD(bus_get_dma_tag,	nexus_get_dma_tag),
139 #ifdef FDT
140 	/* ofw_bus interface */
141 	DEVMETHOD(ofw_bus_map_intr,	nexus_ofw_map_intr),
142 #endif
143 	DEVMETHOD_END
144 };
145 
146 static driver_t nexus_driver = {
147 	"nexus",
148 	nexus_methods,
149 	1			/* no softc */
150 };
151 
152 EARLY_DRIVER_MODULE(nexus, root, nexus_driver, 0, 0,
153     BUS_PASS_BUS + BUS_PASS_ORDER_EARLY);
154 
155 static int
nexus_probe(device_t dev)156 nexus_probe(device_t dev)
157 {
158 
159 	device_quiet(dev);	/* suppress attach message for neatness */
160 
161 	return (BUS_PROBE_DEFAULT);
162 }
163 
164 static int
nexus_attach(device_t dev)165 nexus_attach(device_t dev)
166 {
167 
168 	mem_rman.rm_start = 0;
169 	mem_rman.rm_end = BUS_SPACE_MAXADDR;
170 	mem_rman.rm_type = RMAN_ARRAY;
171 	mem_rman.rm_descr = "I/O memory addresses";
172 	if (rman_init(&mem_rman) ||
173 	    rman_manage_region(&mem_rman, 0, BUS_SPACE_MAXADDR))
174 		panic("nexus_probe mem_rman");
175 	irq_rman.rm_start = 0;
176 	irq_rman.rm_end = ~0;
177 	irq_rman.rm_type = RMAN_ARRAY;
178 	irq_rman.rm_descr = "Interrupts";
179 	if (rman_init(&irq_rman) || rman_manage_region(&irq_rman, 0, ~0))
180 		panic("nexus_attach irq_rman");
181 
182 	/* First, add ofwbus0. */
183 	device_add_child(dev, "ofwbus", 0);
184 
185 	/*
186 	 * Next, deal with the children we know about already.
187 	 */
188 	bus_identify_children(dev);
189 	bus_attach_children(dev);
190 
191 	return (0);
192 }
193 
194 static int
nexus_print_child(device_t bus,device_t child)195 nexus_print_child(device_t bus, device_t child)
196 {
197 	int retval = 0;
198 
199 	retval += bus_print_child_header(bus, child);
200 	retval += printf("\n");
201 
202 	return (retval);
203 }
204 
205 static device_t
nexus_add_child(device_t bus,u_int order,const char * name,int unit)206 nexus_add_child(device_t bus, u_int order, const char *name, int unit)
207 {
208 	device_t child;
209 	struct nexus_device *ndev;
210 
211 	ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
212 	if (!ndev)
213 		return (0);
214 	resource_list_init(&ndev->nx_resources);
215 
216 	child = device_add_child_ordered(bus, order, name, unit);
217 
218 	device_set_ivars(child, ndev);
219 
220 	return (child);
221 }
222 
223 static struct rman *
nexus_get_rman(device_t bus,int type,u_int flags)224 nexus_get_rman(device_t bus, int type, u_int flags)
225 {
226 	switch (type) {
227 	case SYS_RES_IRQ:
228 		return (&irq_rman);
229 	case SYS_RES_MEMORY:
230 	case SYS_RES_IOPORT:
231 		return (&mem_rman);
232 	default:
233 		return (NULL);
234 	}
235 }
236 
237 static bus_space_tag_t
nexus_get_bus_tag(device_t bus __unused,device_t child __unused)238 nexus_get_bus_tag(device_t bus __unused, device_t child __unused)
239 {
240 
241 #ifdef FDT
242 	return (fdtbus_bs_tag);
243 #else
244 	return ((void *)1);
245 #endif
246 }
247 
248 static bus_dma_tag_t
nexus_get_dma_tag(device_t dev,device_t child)249 nexus_get_dma_tag(device_t dev, device_t child)
250 {
251 
252 	return (nexus_dma_tag);
253 }
254 
255 void
nexus_set_dma_tag(bus_dma_tag_t tag)256 nexus_set_dma_tag(bus_dma_tag_t tag)
257 {
258 
259 	nexus_dma_tag = tag;
260 }
261 
262 static int
nexus_config_intr(device_t dev,int irq,enum intr_trigger trig,enum intr_polarity pol)263 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
264     enum intr_polarity pol)
265 {
266 	int ret = ENODEV;
267 
268 	device_printf(dev, "bus_config_intr is obsolete and not supported!\n");
269 	ret = EOPNOTSUPP;
270 	return (ret);
271 }
272 
273 static int
nexus_setup_intr(device_t dev,device_t child,struct resource * res,int flags,driver_filter_t * filt,driver_intr_t * intr,void * arg,void ** cookiep)274 nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
275     driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
276 {
277 
278 	if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
279 		flags |= INTR_EXCL;
280 
281 	return (intr_setup_irq(child, res, filt, intr, arg, flags, cookiep));
282 }
283 
284 static int
nexus_teardown_intr(device_t dev,device_t child,struct resource * r,void * ih)285 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
286 {
287 
288 	return (intr_teardown_irq(child, r, ih));
289 }
290 
291 static int
nexus_describe_intr(device_t dev,device_t child,struct resource * irq,void * cookie,const char * descr)292 nexus_describe_intr(device_t dev, device_t child, struct resource *irq,
293     void *cookie, const char *descr)
294 {
295 
296 	return (intr_describe_irq(child, irq, cookie, descr));
297 }
298 
299 #ifdef SMP
300 static int
nexus_bind_intr(device_t dev,device_t child,struct resource * irq,int cpu)301 nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
302 {
303 
304 	return (intr_bind_irq(child, irq, cpu));
305 }
306 #endif
307 
308 static int
nexus_activate_resource(device_t bus,device_t child,struct resource * r)309 nexus_activate_resource(device_t bus, device_t child, struct resource *r)
310 {
311 	int err;
312 
313 	switch (rman_get_type(r)) {
314 	case SYS_RES_MEMORY:
315 	case SYS_RES_IOPORT:
316 		return (bus_generic_rman_activate_resource(bus, child, r));
317 	case SYS_RES_IRQ:
318 		err = rman_activate_resource(r);
319 		if (err != 0)
320 			return (err);
321 		err = intr_activate_irq(child, r);
322 		if (err != 0) {
323 			rman_deactivate_resource(r);
324 			return (err);
325 		}
326 		return (0);
327 	default:
328 		return (EINVAL);
329 	}
330 }
331 
332 static int
nexus_map_resource(device_t bus,device_t child,struct resource * r,struct resource_map_request * argsp,struct resource_map * map)333 nexus_map_resource(device_t bus, device_t child, struct resource *r,
334     struct resource_map_request *argsp, struct resource_map *map)
335 {
336 	struct resource_map_request args;
337 	rman_res_t length, start;
338 	int error;
339 
340 	/* Resources must be active to be mapped. */
341 	if (!(rman_get_flags(r) & RF_ACTIVE))
342 		return (ENXIO);
343 
344 	switch (rman_get_type(r)) {
345 	case SYS_RES_MEMORY:
346 	case SYS_RES_IOPORT:
347 		break;
348 	default:
349 		return (EINVAL);
350 	}
351 
352 	resource_init_map_request(&args);
353 	error = resource_validate_map_request(r, argsp, &args, &start, &length);
354 	if (error)
355 		return (error);
356 
357 #ifdef FDT
358 	error = bus_space_map(fdtbus_bs_tag, start, length, 0,
359 	    &map->r_bushandle);
360 	if (error)
361 		return (error);
362 	map->r_bustag = fdtbus_bs_tag;
363 	map->r_vaddr = (void *)map->r_bushandle;
364 #else
365 	map->r_vaddr = pmap_mapdev(start, length);
366 	if (map->r_vaddr == NULL)
367 		return (ENOMEM);
368 	map->r_bustag = (void *)1;
369 	map->r_bushandle = (bus_space_handle_t)map->r_vaddr;
370 #endif
371 	map->r_size = length;
372 	return (0);
373 }
374 
375 static int
nexus_unmap_resource(device_t bus,device_t child,struct resource * r,struct resource_map * map)376 nexus_unmap_resource(device_t bus, device_t child, struct resource *r,
377     struct resource_map *map)
378 {
379 
380 	switch (rman_get_type(r)) {
381 	case SYS_RES_MEMORY:
382 	case SYS_RES_IOPORT:
383 #ifdef FDT
384 		bus_space_unmap(map->r_bustag, map->r_bushandle, map->r_size);
385 #else
386 		pmap_unmapdev(map->r_vaddr, map->r_size);
387 #endif
388 		return (0);
389 	default:
390 		return (EINVAL);
391 	}
392 }
393 
394 static int
nexus_deactivate_resource(device_t bus,device_t child,struct resource * r)395 nexus_deactivate_resource(device_t bus, device_t child, struct resource *r)
396 {
397 	int error;
398 
399 	switch (rman_get_type(r)) {
400 	case SYS_RES_MEMORY:
401 	case SYS_RES_IOPORT:
402 		return (bus_generic_rman_deactivate_resource(bus, child, r));
403 	case SYS_RES_IRQ:
404 		error = rman_deactivate_resource(r);
405 		if (error)
406 			return (error);
407 		intr_deactivate_irq(child, r);
408 		return (0);
409 	default:
410 		return (EINVAL);
411 	}
412 }
413 
414 #ifdef FDT
415 static int
nexus_ofw_map_intr(device_t dev,device_t child,phandle_t iparent,int icells,pcell_t * intr)416 nexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, int icells,
417     pcell_t *intr)
418 {
419 	u_int irq;
420 	struct intr_map_data_fdt *fdt_data;
421 	size_t len;
422 
423 	len = sizeof(*fdt_data) + icells * sizeof(pcell_t);
424 	fdt_data = (struct intr_map_data_fdt *)intr_alloc_map_data(
425 	    INTR_MAP_DATA_FDT, len, M_WAITOK | M_ZERO);
426 	fdt_data->iparent = iparent;
427 	fdt_data->ncells = icells;
428 	memcpy(fdt_data->cells, intr, icells * sizeof(pcell_t));
429 	irq = intr_map_irq(NULL, iparent, (struct intr_map_data *)fdt_data);
430 	return (irq);
431 }
432 #endif /* FDT */
433