]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/at91/at91.c
MFV r276568:
[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 "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(void *t, 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(void *t, bus_space_handle_t h, bus_size_t size)
81 {
82         vm_offset_t va, endva;
83
84         if (t == 0)
85                 return;
86         va = trunc_page((vm_offset_t)t);
87         if (va >= AT91_BASE && va <= AT91_BASE + 0xff00000)
88                 return;
89         endva = round_page((vm_offset_t)t + size);
90
91         /* Free the kernel virtual mapping. */
92         kva_free(va, endva - va);
93 }
94
95 static int
96 at91_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset,
97     bus_size_t size, bus_space_handle_t *nbshp)
98 {
99
100         *nbshp = bsh + offset;
101         return (0);
102 }
103
104 static void
105 at91_barrier(void *t, bus_space_handle_t bsh, bus_size_t size, bus_size_t b,
106     int a)
107 {
108 }
109
110 struct arm32_dma_range *
111 bus_dma_get_range(void)
112 {
113
114         return (NULL);
115 }
116
117 int
118 bus_dma_get_range_nb(void)
119 {
120         return (0);
121 }
122
123 bs_protos(generic);
124 bs_protos(generic_armv4);
125
126 struct bus_space at91_bs_tag = {
127         /* cookie */
128         (void *) 0,
129
130         /* mapping/unmapping */
131         at91_bs_map,
132         at91_bs_unmap,
133         at91_bs_subregion,
134
135         /* allocation/deallocation */
136         NULL,
137         NULL,
138
139         /* barrier */
140         at91_barrier,
141
142         /* read (single) */
143         generic_bs_r_1,
144         generic_armv4_bs_r_2,
145         generic_bs_r_4,
146         NULL,
147
148         /* read multiple */
149         generic_bs_rm_1,
150         generic_armv4_bs_rm_2,
151         generic_bs_rm_4,
152         NULL,
153
154         /* read region */
155         generic_bs_rr_1,
156         generic_armv4_bs_rr_2,
157         generic_bs_rr_4,
158         NULL,
159
160         /* write (single) */
161         generic_bs_w_1,
162         generic_armv4_bs_w_2,
163         generic_bs_w_4,
164         NULL,
165
166         /* write multiple */
167         generic_bs_wm_1,
168         generic_armv4_bs_wm_2,
169         generic_bs_wm_4,
170         NULL,
171
172         /* write region */
173         NULL,
174         generic_armv4_bs_wr_2,
175         generic_bs_wr_4,
176         NULL,
177
178         /* set multiple */
179         NULL,
180         NULL,
181         NULL,
182         NULL,
183
184         /* set region */
185         NULL,
186         generic_armv4_bs_sr_2,
187         generic_bs_sr_4,
188         NULL,
189
190         /* copy */
191         NULL,
192         generic_armv4_bs_c_2,
193         NULL,
194         NULL,
195
196         /* read (single) stream */
197         generic_bs_r_1,
198         generic_armv4_bs_r_2,
199         generic_bs_r_4,
200         NULL,
201
202         /* read multiple stream */
203         generic_bs_rm_1,
204         generic_armv4_bs_rm_2,
205         generic_bs_rm_4,
206         NULL,
207
208         /* read region stream */
209         generic_bs_rr_1,
210         generic_armv4_bs_rr_2,
211         generic_bs_rr_4,
212         NULL,
213
214         /* write (single) stream */
215         generic_bs_w_1,
216         generic_armv4_bs_w_2,
217         generic_bs_w_4,
218         NULL,
219
220         /* write multiple stream */
221         generic_bs_wm_1,
222         generic_armv4_bs_wm_2,
223         generic_bs_wm_4,
224         NULL,
225
226         /* write region stream */
227         NULL,
228         generic_armv4_bs_wr_2,
229         generic_bs_wr_4,
230         NULL,
231 };
232
233 #ifndef FDT
234
235 static struct at91_softc *at91_softc;
236
237 static void at91_eoi(void *);
238
239 static int
240 at91_probe(device_t dev)
241 {
242
243         device_set_desc(dev, soc_info.name);
244         return (BUS_PROBE_NOWILDCARD);
245 }
246
247 static void
248 at91_identify(driver_t *drv, device_t parent)
249 {
250         
251         BUS_ADD_CHILD(parent, 0, "atmelarm", 0);
252 }
253
254 static void
255 at91_cpu_add_builtin_children(device_t dev, const struct cpu_devs *walker)
256 {
257         int i;
258
259         for (i = 0; walker->name; i++, walker++) {
260                 at91_add_child(dev, i, walker->name, walker->unit,
261                     walker->mem_base, walker->mem_len, walker->irq0,
262                     walker->irq1, walker->irq2);
263         }
264 }
265
266 static int
267 at91_attach(device_t dev)
268 {
269         struct at91_softc *sc = device_get_softc(dev);
270
271         arm_post_filter = at91_eoi;
272
273         at91_softc = sc;
274         sc->sc_st = &at91_bs_tag;
275         sc->sc_sh = AT91_BASE;
276         sc->sc_aic_sh = AT91_BASE + AT91_SYS_BASE;
277         sc->dev = dev;
278
279         sc->sc_irq_rman.rm_type = RMAN_ARRAY;
280         sc->sc_irq_rman.rm_descr = "AT91 IRQs";
281         if (rman_init(&sc->sc_irq_rman) != 0 ||
282             rman_manage_region(&sc->sc_irq_rman, 1, 31) != 0)
283                 panic("at91_attach: failed to set up IRQ rman");
284
285         sc->sc_mem_rman.rm_type = RMAN_ARRAY;
286         sc->sc_mem_rman.rm_descr = "AT91 Memory";
287         if (rman_init(&sc->sc_mem_rman) != 0)
288                 panic("at91_attach: failed to set up memory rman");
289         /*
290          * Manage the physical space, defined as being everything that isn't
291          * DRAM.
292          */
293         if (rman_manage_region(&sc->sc_mem_rman, 0, PHYSADDR - 1) != 0)
294                 panic("at91_attach: failed to set up memory rman");
295         if (rman_manage_region(&sc->sc_mem_rman, PHYSADDR + (256 << 20),
296             0xfffffffful) != 0)
297                 panic("at91_attach: failed to set up memory rman");
298
299         /*
300          * Add this device's children...
301          */
302         at91_cpu_add_builtin_children(dev, soc_info.soc_data->soc_children);
303         soc_info.soc_data->soc_clock_init();
304
305         bus_generic_probe(dev);
306         bus_generic_attach(dev);
307         enable_interrupts(PSR_I | PSR_F);
308         return (0);
309 }
310
311 static struct resource *
312 at91_alloc_resource(device_t dev, device_t child, int type, int *rid,
313     u_long start, u_long end, u_long count, u_int flags)
314 {
315         struct at91_softc *sc = device_get_softc(dev);
316         struct resource_list_entry *rle;
317         struct at91_ivar *ivar = device_get_ivars(child);
318         struct resource_list *rl = &ivar->resources;
319         bus_space_handle_t bsh;
320
321         if (device_get_parent(child) != dev)
322                 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
323                     type, rid, start, end, count, flags));
324         
325         rle = resource_list_find(rl, type, *rid);
326         if (rle == NULL)
327                 return (NULL);
328         if (rle->res)
329                 panic("Resource rid %d type %d already in use", *rid, type);
330         if (start == 0UL && end == ~0UL) {
331                 start = rle->start;
332                 count = ulmax(count, rle->count);
333                 end = ulmax(rle->end, start + count - 1);
334         }
335         switch (type)
336         {
337         case SYS_RES_IRQ:
338                 rle->res = rman_reserve_resource(&sc->sc_irq_rman,
339                     start, end, count, flags, child);
340                 break;
341         case SYS_RES_MEMORY:
342                 rle->res = rman_reserve_resource(&sc->sc_mem_rman,
343                     start, end, count, flags, child);
344                 if (rle->res != NULL) {
345                         bus_space_map(&at91_bs_tag, start,
346                             rman_get_size(rle->res), 0, &bsh);
347                         rman_set_bustag(rle->res, &at91_bs_tag);
348                         rman_set_bushandle(rle->res, bsh);
349                 }
350                 break;
351         }
352         if (rle->res) {
353                 rle->start = rman_get_start(rle->res);
354                 rle->end = rman_get_end(rle->res);
355                 rle->count = count;
356                 rman_set_rid(rle->res, *rid);
357         }
358         return (rle->res);
359 }
360
361 static struct resource_list *
362 at91_get_resource_list(device_t dev, device_t child)
363 {
364         struct at91_ivar *ivar;
365
366         ivar = device_get_ivars(child);
367         return (&(ivar->resources));
368 }
369
370 static int
371 at91_release_resource(device_t dev, device_t child, int type,
372     int rid, struct resource *r)
373 {
374         struct resource_list *rl;
375         struct resource_list_entry *rle;
376
377         rl = at91_get_resource_list(dev, child);
378         if (rl == NULL)
379                 return (EINVAL);
380         rle = resource_list_find(rl, type, rid);
381         if (rle == NULL)
382                 return (EINVAL);
383         rman_release_resource(r);
384         rle->res = NULL;
385         return (0);
386 }
387
388 static int
389 at91_setup_intr(device_t dev, device_t child,
390     struct resource *ires, int flags, driver_filter_t *filt,
391     driver_intr_t *intr, void *arg, void **cookiep)
392 {
393         int error;
394
395         if (rman_get_start(ires) == AT91_IRQ_SYSTEM && filt == NULL)
396                 panic("All system interrupt ISRs must be FILTER");
397         error = BUS_SETUP_INTR(device_get_parent(dev), child, ires, flags,
398             filt, intr, arg, cookiep);
399         if (error)
400                 return (error);
401
402         return (0);
403 }
404
405 static int
406 at91_teardown_intr(device_t dev, device_t child, struct resource *res,
407     void *cookie)
408 {
409         struct at91_softc *sc = device_get_softc(dev);
410
411         bus_space_write_4(sc->sc_st, sc->sc_aic_sh, IC_IDCR,
412             1 << rman_get_start(res));
413         return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, res, cookie));
414 }
415
416 static int
417 at91_activate_resource(device_t bus, device_t child, int type, int rid,
418     struct resource *r)
419 {
420 #if 0
421         u_long p;
422         int error;
423         
424         if (type == SYS_RES_MEMORY) {
425                 error = bus_space_map(rman_get_bustag(r),
426                     rman_get_bushandle(r), rman_get_size(r), 0, &p);
427                 if (error)
428                         return (error);
429                 rman_set_bushandle(r, p);
430         }
431 #endif  
432         return (rman_activate_resource(r));
433 }
434
435 static int
436 at91_print_child(device_t dev, device_t child)
437 {
438         struct at91_ivar *ivars;
439         struct resource_list *rl;
440         int retval = 0;
441
442         ivars = device_get_ivars(child);
443         rl = &ivars->resources;
444
445         retval += bus_print_child_header(dev, child);
446
447         retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
448         retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
449         retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
450         if (device_get_flags(dev))
451                 retval += printf(" flags %#x", device_get_flags(dev));
452
453         retval += bus_print_child_footer(dev, child);
454
455         return (retval);
456 }
457
458 static void
459 at91_eoi(void *unused)
460 {
461         bus_space_write_4(at91_softc->sc_st, at91_softc->sc_aic_sh,
462             IC_EOICR, 0);
463 }
464
465 void
466 at91_add_child(device_t dev, int prio, const char *name, int unit,
467     bus_addr_t addr, bus_size_t size, int irq0, int irq1, int irq2)
468 {
469         device_t kid;
470         struct at91_ivar *ivar;
471
472         kid = device_add_child_ordered(dev, prio, name, unit);
473         if (kid == NULL) {
474             printf("Can't add child %s%d ordered\n", name, unit);
475             return;
476         }
477         ivar = malloc(sizeof(*ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
478         if (ivar == NULL) {
479                 device_delete_child(dev, kid);
480                 printf("Can't add alloc ivar\n");
481                 return;
482         }
483         device_set_ivars(kid, ivar);
484         resource_list_init(&ivar->resources);
485         if (irq0 != -1) {
486                 bus_set_resource(kid, SYS_RES_IRQ, 0, irq0, 1);
487                 if (irq0 != AT91_IRQ_SYSTEM)
488                         at91_pmc_clock_add(device_get_nameunit(kid), irq0, 0);
489         }
490         if (irq1 != 0)
491                 bus_set_resource(kid, SYS_RES_IRQ, 1, irq1, 1);
492         if (irq2 != 0)
493                 bus_set_resource(kid, SYS_RES_IRQ, 2, irq2, 1);
494         /*
495          * Special case for on-board devices. These have their address
496          * defined relative to AT91_PA_BASE in all the register files we
497          * have. We could change this, but that's a lot of effort which
498          * will be obsoleted when FDT arrives.
499          */
500         if (addr != 0 && addr < 0x10000000 && addr >= 0x0f000000) 
501                 addr += AT91_PA_BASE;
502         if (addr != 0)
503                 bus_set_resource(kid, SYS_RES_MEMORY, 0, addr, size);
504 }
505
506 static device_method_t at91_methods[] = {
507         DEVMETHOD(device_probe, at91_probe),
508         DEVMETHOD(device_attach, at91_attach),
509         DEVMETHOD(device_identify, at91_identify),
510
511         DEVMETHOD(bus_alloc_resource, at91_alloc_resource),
512         DEVMETHOD(bus_setup_intr, at91_setup_intr),
513         DEVMETHOD(bus_teardown_intr, at91_teardown_intr),
514         DEVMETHOD(bus_activate_resource, at91_activate_resource),
515         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
516         DEVMETHOD(bus_get_resource_list,at91_get_resource_list),
517         DEVMETHOD(bus_set_resource,     bus_generic_rl_set_resource),
518         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
519         DEVMETHOD(bus_release_resource, at91_release_resource),
520         DEVMETHOD(bus_print_child,      at91_print_child),
521
522         {0, 0},
523 };
524
525 static driver_t at91_driver = {
526         "atmelarm",
527         at91_methods,
528         sizeof(struct at91_softc),
529 };
530
531 static devclass_t at91_devclass;
532
533 DRIVER_MODULE(atmelarm, nexus, at91_driver, at91_devclass, 0, 0);
534 #endif