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