FreeBSD/Linux Kernel Cross Reference
sys/arm/at91/at91.c
1 /*-
2 * Copyright (c) 2005 Olivier Houchard. All rights reserved.
3 * Copyright (c) 2010 Greg Ansley. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include "opt_platform.h"
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38
39 #include <vm/vm.h>
40 #include <vm/vm_kern.h>
41 #include <vm/pmap.h>
42 #include <vm/vm_page.h>
43 #include <vm/vm_extern.h>
44
45 #include <machine/armreg.h>
46 #define _ARM32_BUS_DMA_PRIVATE
47 #include <machine/bus.h>
48 #include <machine/devmap.h>
49 #include <machine/intr.h>
50
51 #include <arm/at91/at91var.h>
52 #include <arm/at91/at91_pmcvar.h>
53 #include <arm/at91/at91_aicreg.h>
54
55 uint32_t at91_master_clock;
56
57 static int
58 at91_bs_map(bus_space_tag_t tag, bus_addr_t bpa, bus_size_t size, int flags,
59 bus_space_handle_t *bshp)
60 {
61 vm_paddr_t pa, endpa;
62
63 pa = trunc_page(bpa);
64 if (pa >= AT91_PA_BASE + 0xff00000) {
65 *bshp = bpa - AT91_PA_BASE + AT91_BASE;
66 return (0);
67 }
68 if (pa >= AT91_BASE + 0xff00000) {
69 *bshp = bpa;
70 return (0);
71 }
72 endpa = round_page(bpa + size);
73
74 *bshp = (vm_offset_t)pmap_mapdev(pa, endpa - pa) + (bpa - pa);
75
76 return (0);
77 }
78
79 static void
80 at91_bs_unmap(bus_space_tag_t tag, bus_space_handle_t h, bus_size_t size)
81 {
82 vm_offset_t va;
83
84 va = (vm_offset_t)h;
85 if (va >= AT91_BASE && va <= AT91_BASE + 0xff00000)
86 return;
87 pmap_unmapdev(va, size);
88 }
89
90 static int
91 at91_bs_subregion(bus_space_tag_t tag, bus_space_handle_t bsh, bus_size_t offset,
92 bus_size_t size, bus_space_handle_t *nbshp)
93 {
94
95 *nbshp = bsh + offset;
96 return (0);
97 }
98
99 static void
100 at91_barrier(bus_space_tag_t tag, bus_space_handle_t bsh, bus_size_t size, bus_size_t b,
101 int a)
102 {
103 }
104
105 struct arm32_dma_range *
106 bus_dma_get_range(void)
107 {
108
109 return (NULL);
110 }
111
112 int
113 bus_dma_get_range_nb(void)
114 {
115 return (0);
116 }
117
118 bs_protos(generic);
119
120 struct bus_space at91_bs_tag = {
121 /* privdata is whatever the implementer wants; unused in base tag */
122 .bs_privdata = NULL,
123
124 /* mapping/unmapping */
125 .bs_map = at91_bs_map,
126 .bs_unmap = at91_bs_unmap,
127 .bs_subregion = at91_bs_subregion,
128
129 /* allocation/deallocation */
130 .bs_alloc = generic_bs_alloc,
131 .bs_free = generic_bs_free,
132
133 /* barrier */
134 .bs_barrier = at91_barrier,
135
136 /* read (single) */
137 .bs_r_1 = NULL, /* Use inline code in bus.h */
138 .bs_r_2 = NULL, /* Use inline code in bus.h */
139 .bs_r_4 = NULL, /* Use inline code in bus.h */
140 .bs_r_8 = NULL, /* Use inline code in bus.h */
141
142 /* read multiple */
143 .bs_rm_1 = generic_bs_rm_1,
144 .bs_rm_2 = generic_bs_rm_2,
145 .bs_rm_4 = generic_bs_rm_4,
146 .bs_rm_8 = BS_UNIMPLEMENTED,
147
148 /* read region */
149 .bs_rr_1 = generic_bs_rr_1,
150 .bs_rr_2 = generic_bs_rr_2,
151 .bs_rr_4 = generic_bs_rr_4,
152 .bs_rr_8 = BS_UNIMPLEMENTED,
153
154 /* write (single) */
155 .bs_w_1 = NULL, /* Use inline code in bus.h */
156 .bs_w_2 = NULL, /* Use inline code in bus.h */
157 .bs_w_4 = NULL, /* Use inline code in bus.h */
158 .bs_w_8 = NULL, /* Use inline code in bus.h */
159
160 /* write multiple */
161 .bs_wm_1 = generic_bs_wm_1,
162 .bs_wm_2 = generic_bs_wm_2,
163 .bs_wm_4 = generic_bs_wm_4,
164 .bs_wm_8 = BS_UNIMPLEMENTED,
165
166 /* write region */
167 .bs_wr_1 = generic_bs_wr_1,
168 .bs_wr_2 = generic_bs_wr_2,
169 .bs_wr_4 = generic_bs_wr_4,
170 .bs_wr_8 = BS_UNIMPLEMENTED,
171
172 /* set multiple */
173 .bs_sm_1 = BS_UNIMPLEMENTED,
174 .bs_sm_2 = BS_UNIMPLEMENTED,
175 .bs_sm_4 = BS_UNIMPLEMENTED,
176 .bs_sm_8 = BS_UNIMPLEMENTED,
177
178 /* set region */
179 .bs_sr_1 = generic_bs_sr_1,
180 .bs_sr_2 = generic_bs_sr_2,
181 .bs_sr_4 = generic_bs_sr_4,
182 .bs_sr_8 = BS_UNIMPLEMENTED,
183
184 /* copy */
185 .bs_c_1 = BS_UNIMPLEMENTED,
186 .bs_c_2 = generic_bs_c_2,
187 .bs_c_4 = BS_UNIMPLEMENTED,
188 .bs_c_8 = BS_UNIMPLEMENTED,
189
190 /* read stream (single) */
191 .bs_r_1_s = NULL, /* Use inline code in bus.h */
192 .bs_r_2_s = NULL, /* Use inline code in bus.h */
193 .bs_r_4_s = NULL, /* Use inline code in bus.h */
194 .bs_r_8_s = NULL, /* Use inline code in bus.h */
195
196 /* read multiple stream */
197 .bs_rm_1_s = generic_bs_rm_1,
198 .bs_rm_2_s = generic_bs_rm_2,
199 .bs_rm_4_s = generic_bs_rm_4,
200 .bs_rm_8_s = BS_UNIMPLEMENTED,
201
202 /* read region stream */
203 .bs_rr_1_s = generic_bs_rr_1,
204 .bs_rr_2_s = generic_bs_rr_2,
205 .bs_rr_4_s = generic_bs_rr_4,
206 .bs_rr_8_s = BS_UNIMPLEMENTED,
207
208 /* write stream (single) */
209 .bs_w_1_s = NULL, /* Use inline code in bus.h */
210 .bs_w_2_s = NULL, /* Use inline code in bus.h */
211 .bs_w_4_s = NULL, /* Use inline code in bus.h */
212 .bs_w_8_s = NULL, /* Use inline code in bus.h */
213
214 /* write multiple stream */
215 .bs_wm_1_s = generic_bs_wm_1,
216 .bs_wm_2_s = generic_bs_wm_2,
217 .bs_wm_4_s = generic_bs_wm_4,
218 .bs_wm_8_s = BS_UNIMPLEMENTED,
219
220 /* write region stream */
221 .bs_wr_1_s = generic_bs_wr_1,
222 .bs_wr_2_s = generic_bs_wr_2,
223 .bs_wr_4_s = generic_bs_wr_4,
224 .bs_wr_8_s = BS_UNIMPLEMENTED,
225 };
226
227 #ifndef FDT
228
229 static struct at91_softc *at91_softc;
230
231 static void at91_eoi(void *);
232
233 static int
234 at91_probe(device_t dev)
235 {
236
237 device_set_desc(dev, soc_info.name);
238 return (BUS_PROBE_NOWILDCARD);
239 }
240
241 static void
242 at91_identify(driver_t *drv, device_t parent)
243 {
244
245 BUS_ADD_CHILD(parent, 0, "atmelarm", 0);
246 }
247
248 static void
249 at91_cpu_add_builtin_children(device_t dev, const struct cpu_devs *walker)
250 {
251 int i;
252
253 for (i = 1; walker->name; i++, walker++) {
254 at91_add_child(dev, i, walker->name, walker->unit,
255 walker->mem_base, walker->mem_len, walker->irq0,
256 walker->irq1, walker->irq2);
257 }
258 }
259
260 static int
261 at91_attach(device_t dev)
262 {
263 struct at91_softc *sc = device_get_softc(dev);
264
265 arm_post_filter = at91_eoi;
266
267 at91_softc = sc;
268 sc->sc_st = &at91_bs_tag;
269 sc->sc_sh = AT91_BASE;
270 sc->sc_aic_sh = AT91_BASE + AT91_SYS_BASE;
271 sc->dev = dev;
272
273 sc->sc_irq_rman.rm_type = RMAN_ARRAY;
274 sc->sc_irq_rman.rm_descr = "AT91 IRQs";
275 if (rman_init(&sc->sc_irq_rman) != 0 ||
276 rman_manage_region(&sc->sc_irq_rman, 1, 31) != 0)
277 panic("at91_attach: failed to set up IRQ rman");
278
279 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
280 sc->sc_mem_rman.rm_descr = "AT91 Memory";
281 if (rman_init(&sc->sc_mem_rman) != 0)
282 panic("at91_attach: failed to set up memory rman");
283 /*
284 * Manage the physical space, defined as being everything that isn't
285 * DRAM.
286 */
287 if (rman_manage_region(&sc->sc_mem_rman, 0, PHYSADDR - 1) != 0)
288 panic("at91_attach: failed to set up memory rman");
289 if (rman_manage_region(&sc->sc_mem_rman, PHYSADDR + (256 << 20),
290 0xfffffffful) != 0)
291 panic("at91_attach: failed to set up memory rman");
292
293 /*
294 * Add this device's children...
295 */
296 at91_cpu_add_builtin_children(dev, soc_info.soc_data->soc_children);
297 soc_info.soc_data->soc_clock_init();
298
299 bus_generic_probe(dev);
300 bus_generic_attach(dev);
301 enable_interrupts(PSR_I | PSR_F);
302 return (0);
303 }
304
305 static struct resource *
306 at91_alloc_resource(device_t dev, device_t child, int type, int *rid,
307 u_long start, u_long end, u_long count, u_int flags)
308 {
309 struct at91_softc *sc = device_get_softc(dev);
310 struct resource_list_entry *rle;
311 struct at91_ivar *ivar = device_get_ivars(child);
312 struct resource_list *rl = &ivar->resources;
313 bus_space_handle_t bsh;
314
315 if (device_get_parent(child) != dev)
316 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
317 type, rid, start, end, count, flags));
318
319 rle = resource_list_find(rl, type, *rid);
320 if (rle == NULL)
321 return (NULL);
322 if (rle->res)
323 panic("Resource rid %d type %d already in use", *rid, type);
324 if (start == 0UL && end == ~0UL) {
325 start = rle->start;
326 count = ulmax(count, rle->count);
327 end = ulmax(rle->end, start + count - 1);
328 }
329 switch (type)
330 {
331 case SYS_RES_IRQ:
332 rle->res = rman_reserve_resource(&sc->sc_irq_rman,
333 start, end, count, flags, child);
334 break;
335 case SYS_RES_MEMORY:
336 rle->res = rman_reserve_resource(&sc->sc_mem_rman,
337 start, end, count, flags, child);
338 if (rle->res != NULL) {
339 bus_space_map(&at91_bs_tag, start,
340 rman_get_size(rle->res), 0, &bsh);
341 rman_set_bustag(rle->res, &at91_bs_tag);
342 rman_set_bushandle(rle->res, bsh);
343 }
344 break;
345 }
346 if (rle->res) {
347 rle->start = rman_get_start(rle->res);
348 rle->end = rman_get_end(rle->res);
349 rle->count = count;
350 rman_set_rid(rle->res, *rid);
351 }
352 return (rle->res);
353 }
354
355 static struct resource_list *
356 at91_get_resource_list(device_t dev, device_t child)
357 {
358 struct at91_ivar *ivar;
359
360 ivar = device_get_ivars(child);
361 return (&(ivar->resources));
362 }
363
364 static int
365 at91_release_resource(device_t dev, device_t child, int type,
366 int rid, struct resource *r)
367 {
368 struct resource_list *rl;
369 struct resource_list_entry *rle;
370
371 rl = at91_get_resource_list(dev, child);
372 if (rl == NULL)
373 return (EINVAL);
374 rle = resource_list_find(rl, type, rid);
375 if (rle == NULL)
376 return (EINVAL);
377 rman_release_resource(r);
378 rle->res = NULL;
379 return (0);
380 }
381
382 static int
383 at91_setup_intr(device_t dev, device_t child,
384 struct resource *ires, int flags, driver_filter_t *filt,
385 driver_intr_t *intr, void *arg, void **cookiep)
386 {
387 int error;
388
389 if (rman_get_start(ires) == AT91_IRQ_SYSTEM && filt == NULL)
390 panic("All system interrupt ISRs must be FILTER");
391 error = BUS_SETUP_INTR(device_get_parent(dev), child, ires, flags,
392 filt, intr, arg, cookiep);
393 if (error)
394 return (error);
395
396 return (0);
397 }
398
399 static int
400 at91_teardown_intr(device_t dev, device_t child, struct resource *res,
401 void *cookie)
402 {
403 struct at91_softc *sc = device_get_softc(dev);
404
405 bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_IDCR,
406 1 << rman_get_start(res));
407 return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, res, cookie));
408 }
409
410 static int
411 at91_activate_resource(device_t bus, device_t child, int type, int rid,
412 struct resource *r)
413 {
414 #if 0
415 u_long p;
416 int error;
417
418 if (type == SYS_RES_MEMORY) {
419 error = bus_space_map(rman_get_bustag(r),
420 rman_get_bushandle(r), rman_get_size(r), 0, &p);
421 if (error)
422 return (error);
423 rman_set_bushandle(r, p);
424 }
425 #endif
426 return (rman_activate_resource(r));
427 }
428
429 static int
430 at91_print_child(device_t dev, device_t child)
431 {
432 struct at91_ivar *ivars;
433 struct resource_list *rl;
434 int retval = 0;
435
436 ivars = device_get_ivars(child);
437 rl = &ivars->resources;
438
439 retval += bus_print_child_header(dev, child);
440
441 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
442 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
443 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
444 if (device_get_flags(dev))
445 retval += printf(" flags %#x", device_get_flags(dev));
446
447 retval += bus_print_child_footer(dev, child);
448
449 return (retval);
450 }
451
452 static void
453 at91_eoi(void *unused)
454 {
455 bus_space_write_4(at91_softc->sc_st, at91_softc->sc_aic_sh,
456 IC_EOICR, 0);
457 }
458
459 void
460 at91_add_child(device_t dev, int prio, const char *name, int unit,
461 bus_addr_t addr, bus_size_t size, int irq0, int irq1, int irq2)
462 {
463 device_t kid;
464 struct at91_ivar *ivar;
465
466 kid = device_add_child_ordered(dev, prio, name, unit);
467 if (kid == NULL) {
468 printf("Can't add child %s%d ordered\n", name, unit);
469 return;
470 }
471 ivar = malloc(sizeof(*ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
472 if (ivar == NULL) {
473 device_delete_child(dev, kid);
474 printf("Can't add alloc ivar\n");
475 return;
476 }
477 device_set_ivars(kid, ivar);
478 resource_list_init(&ivar->resources);
479 if (irq0 != -1) {
480 bus_set_resource(kid, SYS_RES_IRQ, 0, irq0, 1);
481 if (irq0 != AT91_IRQ_SYSTEM)
482 at91_pmc_clock_add(device_get_nameunit(kid), irq0, 0);
483 }
484 if (irq1 != 0)
485 bus_set_resource(kid, SYS_RES_IRQ, 1, irq1, 1);
486 if (irq2 != 0)
487 bus_set_resource(kid, SYS_RES_IRQ, 2, irq2, 1);
488 /*
489 * Special case for on-board devices. These have their address
490 * defined relative to AT91_PA_BASE in all the register files we
491 * have. We could change this, but that's a lot of effort which
492 * will be obsoleted when FDT arrives.
493 */
494 if (addr != 0 && addr < 0x10000000 && addr >= 0x0f000000)
495 addr += AT91_PA_BASE;
496 if (addr != 0)
497 bus_set_resource(kid, SYS_RES_MEMORY, 0, addr, size);
498 }
499
500 static device_method_t at91_methods[] = {
501 DEVMETHOD(device_probe, at91_probe),
502 DEVMETHOD(device_attach, at91_attach),
503 DEVMETHOD(device_identify, at91_identify),
504
505 DEVMETHOD(bus_alloc_resource, at91_alloc_resource),
506 DEVMETHOD(bus_setup_intr, at91_setup_intr),
507 DEVMETHOD(bus_teardown_intr, at91_teardown_intr),
508 DEVMETHOD(bus_activate_resource, at91_activate_resource),
509 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
510 DEVMETHOD(bus_get_resource_list,at91_get_resource_list),
511 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
512 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
513 DEVMETHOD(bus_release_resource, at91_release_resource),
514 DEVMETHOD(bus_print_child, at91_print_child),
515
516 {0, 0},
517 };
518
519 static driver_t at91_driver = {
520 "atmelarm",
521 at91_methods,
522 sizeof(struct at91_softc),
523 };
524
525 static devclass_t at91_devclass;
526
527 DRIVER_MODULE(atmelarm, nexus, at91_driver, at91_devclass, 0, 0);
528 #endif
Cache object: 47771cd864a475fb3ae750356336b475
|