]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/arm/at91/at91.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / 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 <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
36
37 #include <vm/vm.h>
38 #include <vm/vm_kern.h>
39 #include <vm/pmap.h>
40 #include <vm/vm_page.h>
41 #include <vm/vm_extern.h>
42
43 #define _ARM32_BUS_DMA_PRIVATE
44 #include <machine/bus.h>
45 #include <machine/intr.h>
46
47 #include <arm/at91/at91var.h>
48 #include <arm/at91/at91_pmcvar.h>
49 #include <arm/at91/at91_aicreg.h>
50
51 static struct at91_softc *at91_softc;
52
53 static void at91_eoi(void *);
54
55 extern const struct pmap_devmap at91_devmap[];
56
57 uint32_t at91_master_clock;
58
59 static int
60 at91_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int flags,
61     bus_space_handle_t *bshp)
62 {
63         vm_paddr_t pa, endpa;
64
65         pa = trunc_page(bpa);
66         if (pa >= AT91_PA_BASE + 0xff00000) {
67                 *bshp = pa - AT91_PA_BASE + AT91_BASE;
68                 return (0);
69         }
70         if (pa >= AT91_BASE + 0xff00000)
71                 return (0);
72         endpa = round_page(bpa + size);
73
74         *bshp = (vm_offset_t)pmap_mapdev(pa, endpa - pa);
75
76         return (0);
77 }
78
79 static void
80 at91_bs_unmap(void *t, bus_space_handle_t h, bus_size_t size)
81 {
82         vm_offset_t va, endva;
83
84         va = trunc_page((vm_offset_t)t);
85         endva = va + round_page(size);
86
87         /* Free the kernel virtual mapping. */
88         kva_free(va, endva - va);
89 }
90
91 static int
92 at91_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset,
93     bus_size_t size, bus_space_handle_t *nbshp)
94 {
95
96         *nbshp = bsh + offset;
97         return (0);
98 }
99
100 static void
101 at91_barrier(void *t, bus_space_handle_t bsh, bus_size_t size, bus_size_t b,
102     int a)
103 {
104 }
105
106 struct arm32_dma_range *
107 bus_dma_get_range(void)
108 {
109
110         return (NULL);
111 }
112
113 int
114 bus_dma_get_range_nb(void)
115 {
116         return (0);
117 }
118
119 bs_protos(generic);
120 bs_protos(generic_armv4);
121
122 struct bus_space at91_bs_tag = {
123         /* cookie */
124         (void *) 0,
125
126         /* mapping/unmapping */
127         at91_bs_map,
128         at91_bs_unmap,
129         at91_bs_subregion,
130
131         /* allocation/deallocation */
132         NULL,
133         NULL,
134
135         /* barrier */
136         at91_barrier,
137
138         /* read (single) */
139         generic_bs_r_1,
140         generic_armv4_bs_r_2,
141         generic_bs_r_4,
142         NULL,
143
144         /* read multiple */
145         generic_bs_rm_1,
146         generic_armv4_bs_rm_2,
147         generic_bs_rm_4,
148         NULL,
149
150         /* read region */
151         generic_bs_rr_1,
152         generic_armv4_bs_rr_2,
153         generic_bs_rr_4,
154         NULL,
155
156         /* write (single) */
157         generic_bs_w_1,
158         generic_armv4_bs_w_2,
159         generic_bs_w_4,
160         NULL,
161
162         /* write multiple */
163         generic_bs_wm_1,
164         generic_armv4_bs_wm_2,
165         generic_bs_wm_4,
166         NULL,
167
168         /* write region */
169         NULL,
170         generic_armv4_bs_wr_2,
171         generic_bs_wr_4,
172         NULL,
173
174         /* set multiple */
175         NULL,
176         NULL,
177         NULL,
178         NULL,
179
180         /* set region */
181         NULL,
182         generic_armv4_bs_sr_2,
183         generic_bs_sr_4,
184         NULL,
185
186         /* copy */
187         NULL,
188         generic_armv4_bs_c_2,
189         NULL,
190         NULL,
191
192         /* read (single) stream */
193         generic_bs_r_1,
194         generic_armv4_bs_r_2,
195         generic_bs_r_4,
196         NULL,
197
198         /* read multiple stream */
199         generic_bs_rm_1,
200         generic_armv4_bs_rm_2,
201         generic_bs_rm_4,
202         NULL,
203
204         /* read region stream */
205         generic_bs_rr_1,
206         generic_armv4_bs_rr_2,
207         generic_bs_rr_4,
208         NULL,
209
210         /* write (single) stream */
211         generic_bs_w_1,
212         generic_armv4_bs_w_2,
213         generic_bs_w_4,
214         NULL,
215
216         /* write multiple stream */
217         generic_bs_wm_1,
218         generic_armv4_bs_wm_2,
219         generic_bs_wm_4,
220         NULL,
221
222         /* write region stream */
223         NULL,
224         generic_armv4_bs_wr_2,
225         generic_bs_wr_4,
226         NULL,
227 };
228
229 static int
230 at91_probe(device_t dev)
231 {
232
233         device_set_desc(dev, "AT91 device bus");
234         arm_post_filter = at91_eoi;
235         return (0);
236 }
237
238 static void
239 at91_identify(driver_t *drv, device_t parent)
240 {
241         
242         BUS_ADD_CHILD(parent, 0, "atmelarm", 0);
243 }
244
245 static void
246 at91_cpu_add_builtin_children(device_t dev, const struct cpu_devs *walker)
247 {
248         int i;
249
250         for (i = 1; walker->name; i++, walker++) {
251                 at91_add_child(dev, i, walker->name, walker->unit,
252                     walker->mem_base, walker->mem_len, walker->irq0,
253                     walker->irq1, walker->irq2);
254         }
255 }
256
257 static int
258 at91_attach(device_t dev)
259 {
260         struct at91_softc *sc = device_get_softc(dev);
261         const struct pmap_devmap *pdevmap;
262         int i;
263
264         at91_softc = sc;
265         sc->sc_st = &at91_bs_tag;
266         sc->sc_sh = AT91_BASE;
267         sc->sc_aic_sh = AT91_BASE + AT91_SYS_BASE;
268         sc->dev = dev;
269
270         sc->sc_irq_rman.rm_type = RMAN_ARRAY;
271         sc->sc_irq_rman.rm_descr = "AT91 IRQs";
272         if (rman_init(&sc->sc_irq_rman) != 0 ||
273             rman_manage_region(&sc->sc_irq_rman, 1, 31) != 0)
274                 panic("at91_attach: failed to set up IRQ rman");
275
276         sc->sc_mem_rman.rm_type = RMAN_ARRAY;
277         sc->sc_mem_rman.rm_descr = "AT91 Memory";
278         if (rman_init(&sc->sc_mem_rman) != 0)
279                 panic("at91_attach: failed to set up memory rman");
280         for (pdevmap = at91_devmap; pdevmap->pd_va != 0; pdevmap++) {
281                 if (rman_manage_region(&sc->sc_mem_rman, pdevmap->pd_va,
282                     pdevmap->pd_va + pdevmap->pd_size - 1) != 0)
283                         panic("at91_attach: failed to set up memory rman");
284         }
285
286         /*
287          * Setup the interrupt table.
288          */
289         if (soc_info.soc_data == NULL || soc_info.soc_data->soc_irq_prio == NULL)
290                 panic("Interrupt priority table missing\n");
291         for (i = 0; i < 32; i++) {
292                 bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_SVR +
293                     i * 4, i);
294                 /* Priority. */
295                 bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_SMR + i * 4,
296                     soc_info.soc_data->soc_irq_prio[i]);
297                 if (i < 8)
298                         bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_EOICR,
299                             1);
300         }
301
302         bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_SPU, 32);
303         /* No debug. */
304         bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_DCR, 0);
305         /* Disable and clear all interrupts. */
306         bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_IDCR, 0xffffffff);
307         bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_ICCR, 0xffffffff);
308
309         /*
310          * Add this device's children...
311          */
312         at91_cpu_add_builtin_children(dev, soc_info.soc_data->soc_children);
313         soc_info.soc_data->soc_clock_init();
314
315         bus_generic_probe(dev);
316         bus_generic_attach(dev);
317         enable_interrupts(I32_bit | F32_bit);
318         return (0);
319 }
320
321 static struct resource *
322 at91_alloc_resource(device_t dev, device_t child, int type, int *rid,
323     u_long start, u_long end, u_long count, u_int flags)
324 {
325         struct at91_softc *sc = device_get_softc(dev);
326         struct resource_list_entry *rle;
327         struct at91_ivar *ivar = device_get_ivars(child);
328         struct resource_list *rl = &ivar->resources;
329
330         if (device_get_parent(child) != dev)
331                 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
332                     type, rid, start, end, count, flags));
333         
334         rle = resource_list_find(rl, type, *rid);
335         if (rle == NULL)
336                 return (NULL);
337         if (rle->res)
338                 panic("Resource rid %d type %d already in use", *rid, type);
339         if (start == 0UL && end == ~0UL) {
340                 start = rle->start;
341                 count = ulmax(count, rle->count);
342                 end = ulmax(rle->end, start + count - 1);
343         }
344         switch (type)
345         {
346         case SYS_RES_IRQ:
347                 rle->res = rman_reserve_resource(&sc->sc_irq_rman,
348                     start, end, count, flags, child);
349                 break;
350         case SYS_RES_MEMORY:
351                 rle->res = rman_reserve_resource(&sc->sc_mem_rman,
352                     start, end, count, flags, child);
353                 if (rle->res != NULL) {
354                         rman_set_bustag(rle->res, &at91_bs_tag);
355                         rman_set_bushandle(rle->res, start);
356                 }
357                 break;
358         }
359         if (rle->res) {
360                 rle->start = rman_get_start(rle->res);
361                 rle->end = rman_get_end(rle->res);
362                 rle->count = count;
363                 rman_set_rid(rle->res, *rid);
364         }
365         return (rle->res);
366 }
367
368 static struct resource_list *
369 at91_get_resource_list(device_t dev, device_t child)
370 {
371         struct at91_ivar *ivar;
372
373         ivar = device_get_ivars(child);
374         return (&(ivar->resources));
375 }
376
377 static int
378 at91_release_resource(device_t dev, device_t child, int type,
379     int rid, struct resource *r)
380 {
381         struct resource_list *rl;
382         struct resource_list_entry *rle;
383
384         rl = at91_get_resource_list(dev, child);
385         if (rl == NULL)
386                 return (EINVAL);
387         rle = resource_list_find(rl, type, rid);
388         if (rle == NULL)
389                 return (EINVAL);
390         rman_release_resource(r);
391         rle->res = NULL;
392         return (0);
393 }
394
395 static int
396 at91_setup_intr(device_t dev, device_t child,
397     struct resource *ires, int flags, driver_filter_t *filt,
398     driver_intr_t *intr, void *arg, void **cookiep)
399 {
400         int error;
401
402         if (rman_get_start(ires) == AT91_IRQ_SYSTEM && filt == NULL)
403                 panic("All system interrupt ISRs must be FILTER");
404         error = BUS_SETUP_INTR(device_get_parent(dev), child, ires, flags,
405             filt, intr, arg, cookiep);
406         if (error)
407                 return (error);
408
409         return (0);
410 }
411
412 static int
413 at91_teardown_intr(device_t dev, device_t child, struct resource *res,
414     void *cookie)
415 {
416         struct at91_softc *sc = device_get_softc(dev);
417
418         bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_IDCR,
419             1 << rman_get_start(res));
420         return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, res, cookie));
421 }
422
423 static int
424 at91_activate_resource(device_t bus, device_t child, int type, int rid,
425     struct resource *r)
426 {
427 #if 0
428         u_long p;
429         int error;
430         
431         if (type == SYS_RES_MEMORY) {
432                 error = bus_space_map(rman_get_bustag(r),
433                     rman_get_bushandle(r), rman_get_size(r), 0, &p);
434                 if (error)
435                         return (error);
436                 rman_set_bushandle(r, p);
437         }
438 #endif  
439         return (rman_activate_resource(r));
440 }
441
442 static int
443 at91_print_child(device_t dev, device_t child)
444 {
445         struct at91_ivar *ivars;
446         struct resource_list *rl;
447         int retval = 0;
448
449         ivars = device_get_ivars(child);
450         rl = &ivars->resources;
451
452         retval += bus_print_child_header(dev, child);
453
454         retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
455         retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
456         retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
457         if (device_get_flags(dev))
458                 retval += printf(" flags %#x", device_get_flags(dev));
459
460         retval += bus_print_child_footer(dev, child);
461
462         return (retval);
463 }
464
465 void
466 arm_mask_irq(uintptr_t nb)
467 {
468         
469         bus_space_write_4(at91_softc->sc_st,
470             at91_softc->sc_aic_sh, IC_IDCR, 1 << nb);
471 }
472
473 int
474 arm_get_next_irq(int last __unused)
475 {
476         int status;
477         int irq;
478         
479         irq = bus_space_read_4(at91_softc->sc_st,
480             at91_softc->sc_aic_sh, IC_IVR);
481         status = bus_space_read_4(at91_softc->sc_st,
482             at91_softc->sc_aic_sh, IC_ISR);
483         if (status == 0) {
484                 bus_space_write_4(at91_softc->sc_st,
485                     at91_softc->sc_aic_sh, IC_EOICR, 1);
486                 return (-1);
487         }
488         return (irq);
489 }
490
491 void
492 arm_unmask_irq(uintptr_t nb)
493 {
494         
495         bus_space_write_4(at91_softc->sc_st,
496         at91_softc->sc_aic_sh, IC_IECR, 1 << nb);
497         bus_space_write_4(at91_softc->sc_st, at91_softc->sc_aic_sh,
498             IC_EOICR, 0);
499 }
500
501 static void
502 at91_eoi(void *unused)
503 {
504         bus_space_write_4(at91_softc->sc_st, at91_softc->sc_aic_sh,
505             IC_EOICR, 0);
506 }
507
508 void
509 at91_add_child(device_t dev, int prio, const char *name, int unit,
510     bus_addr_t addr, bus_size_t size, int irq0, int irq1, int irq2)
511 {
512         device_t kid;
513         struct at91_ivar *ivar;
514
515         kid = device_add_child_ordered(dev, prio, name, unit);
516         if (kid == NULL) {
517             printf("Can't add child %s%d ordered\n", name, unit);
518             return;
519         }
520         ivar = malloc(sizeof(*ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
521         if (ivar == NULL) {
522                 device_delete_child(dev, kid);
523                 printf("Can't add alloc ivar\n");
524                 return;
525         }
526         device_set_ivars(kid, ivar);
527         resource_list_init(&ivar->resources);
528         if (irq0 != -1) {
529                 bus_set_resource(kid, SYS_RES_IRQ, 0, irq0, 1);
530                 if (irq0 != AT91_IRQ_SYSTEM)
531                         at91_pmc_clock_add(device_get_nameunit(kid), irq0, 0);
532         }
533         if (irq1 != 0)
534                 bus_set_resource(kid, SYS_RES_IRQ, 1, irq1, 1);
535         if (irq2 != 0)
536                 bus_set_resource(kid, SYS_RES_IRQ, 2, irq2, 1);
537         if (addr != 0 && addr < AT91_BASE) 
538                 addr += AT91_BASE;
539         if (addr != 0)
540                 bus_set_resource(kid, SYS_RES_MEMORY, 0, addr, size);
541 }
542
543 static device_method_t at91_methods[] = {
544         DEVMETHOD(device_probe, at91_probe),
545         DEVMETHOD(device_attach, at91_attach),
546         DEVMETHOD(device_identify, at91_identify),
547
548         DEVMETHOD(bus_alloc_resource, at91_alloc_resource),
549         DEVMETHOD(bus_setup_intr, at91_setup_intr),
550         DEVMETHOD(bus_teardown_intr, at91_teardown_intr),
551         DEVMETHOD(bus_activate_resource, at91_activate_resource),
552         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
553         DEVMETHOD(bus_get_resource_list,at91_get_resource_list),
554         DEVMETHOD(bus_set_resource,     bus_generic_rl_set_resource),
555         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
556         DEVMETHOD(bus_release_resource, at91_release_resource),
557         DEVMETHOD(bus_print_child,      at91_print_child),
558
559         {0, 0},
560 };
561
562 static driver_t at91_driver = {
563         "atmelarm",
564         at91_methods,
565         sizeof(struct at91_softc),
566 };
567
568 static devclass_t at91_devclass;
569
570 DRIVER_MODULE(atmelarm, nexus, at91_driver, at91_devclass, 0, 0);