RTEMS
amba.c
1 /*
2  * AMBA Plug & Play Bus Driver
3  *
4  * This driver hook performs bus scanning.
5  *
6  * COPYRIGHT (c) 2011.
7  * Aeroflex Gaisler
8  *
9  * The license and distribution terms for this file may be
10  * found in the file LICENSE in this distribution or at
11  * http://www.rtems.org/license/LICENSE.
12  */
13 
14 #include <bsp.h>
15 #include <bsp/fatal.h>
16 #include <leon.h>
17 #include <grlib/ambapp.h>
18 #include <rtems/sysinit.h>
19 
20 unsigned int leon3_timer_prescaler __attribute__((weak)) = 0;
21 int leon3_timer_core_index __attribute__((weak)) = 0;
22 
23 /* AMBA Plug&Play information description.
24  *
25  * After software has scanned AMBA PnP it builds a tree to make
26  * it easier for drivers to work with the bus architecture.
27  */
28 struct ambapp_bus ambapp_plb;
29 
30 /* If RTEMS_DRVMGR_STARTUP is defined extra code is added that
31  * registers the GRLIB AMBA PnP bus driver as root driver.
32  */
33 #ifdef RTEMS_DRVMGR_STARTUP
34 #include <drvmgr/drvmgr.h>
35 #include <grlib/ambapp_bus_grlib.h>
36 
37 extern void gptimer_register_drv (void);
38 extern void apbuart_cons_register_drv(void);
39 /* All drivers included by BSP, this is overridden by the user by including
40  * the drvmgr_confdefs.h. By default the Timer and UART driver are included.
41  */
42 drvmgr_drv_reg_func drvmgr_drivers[] __attribute__((weak)) =
43 {
44  gptimer_register_drv,
45  apbuart_cons_register_drv,
46  NULL /* End array with NULL */
47 };
48 
49 /* Driver resources configuration for AMBA root bus. It is declared weak
50  * so that the user may override it, if the defualt settings are not
51  * enough.
52  */
53 struct drvmgr_bus_res grlib_drv_resources __attribute__((weak)) =
54 {
55  .next = NULL,
56  .resource =
57  {
58  DRVMGR_RES_EMPTY,
59  }
60 };
61 
62 /* GRLIB AMBA bus configuration (the LEON3 root bus configuration) */
63 struct grlib_config grlib_bus_config =
64 {
65  &ambapp_plb, /* AMBAPP bus setup */
66  &grlib_drv_resources, /* Driver configuration */
67 };
68 #endif
69 
70 rtems_interrupt_lock LEON3_IrqCtrl_Lock =
71  RTEMS_INTERRUPT_LOCK_INITIALIZER("LEON3 IrqCtrl");
72 
73 /* Pointers to Interrupt Controller configuration registers */
74 volatile struct irqmp_regs *LEON3_IrqCtrl_Regs;
75 struct ambapp_dev *LEON3_IrqCtrl_Adev;
76 volatile struct gptimer_regs *LEON3_Timer_Regs;
77 struct ambapp_dev *LEON3_Timer_Adev;
78 
79 /*
80  * amba_initialize
81  *
82  * Must be called just before drivers are initialized.
83  * Used to scan system bus. Probes for AHB masters, AHB slaves and
84  * APB slaves. Addresses to configuration areas of the AHB masters,
85  * AHB slaves, APB slaves and APB master are storeds in
86  * amba_ahb_masters, amba_ahb_slaves and amba.
87  */
88 
89 static void amba_initialize(void)
90 {
91  int icsel;
92  struct ambapp_dev *adev;
93 
94  /* Scan AMBA Plug&Play read-only information. The routine builds a PnP
95  * tree into ambapp_plb in RAM, after this we never access the PnP
96  * information in hardware directly any more.
97  * Since on Processor Local Bus (PLB) memory mapping is 1:1
98  */
99  ambapp_scan(&ambapp_plb, LEON3_IO_AREA, NULL, NULL);
100 
101  /* Find LEON3 Interrupt controller */
102  adev = (void *)ambapp_for_each(&ambapp_plb, (OPTIONS_ALL|OPTIONS_APB_SLVS),
103  VENDOR_GAISLER, GAISLER_IRQMP,
104  ambapp_find_by_idx, NULL);
105  if (adev == NULL) {
106  /* PANIC IRQ controller not found!
107  *
108  * What else can we do but stop ...
109  */
110  bsp_fatal(LEON3_FATAL_NO_IRQMP_CONTROLLER);
111  }
112 
113  LEON3_IrqCtrl_Regs = (volatile struct irqmp_regs *)DEV_TO_APB(adev)->start;
114  LEON3_IrqCtrl_Adev = adev;
115  if ((LEON3_IrqCtrl_Regs->ampctrl >> 28) > 0) {
116  /* IRQ Controller has support for multiple IRQ Controllers, each
117  * CPU can be routed to different Controllers, we find out which
118  * controller by looking at the IRQCTRL Select Register for this CPU.
119  * Each Controller is located at a 4KByte offset.
120  */
121  icsel = LEON3_IrqCtrl_Regs->icsel[LEON3_Cpu_Index/8];
122  icsel = (icsel >> ((7 - (LEON3_Cpu_Index & 0x7)) * 4)) & 0xf;
123  LEON3_IrqCtrl_Regs += icsel;
124  }
125  LEON3_IrqCtrl_Regs->mask[LEON3_Cpu_Index] = 0;
126  LEON3_IrqCtrl_Regs->force[LEON3_Cpu_Index] = 0;
127  LEON3_IrqCtrl_Regs->iclear = 0xffffffff;
128 
129  /* Init Extended IRQ controller if available */
130  leon3_ext_irq_init();
131 
132  /* find GP Timer */
133  adev = (void *)ambapp_for_each(&ambapp_plb, (OPTIONS_ALL|OPTIONS_APB_SLVS),
134  VENDOR_GAISLER, GAISLER_GPTIMER,
135  ambapp_find_by_idx, &leon3_timer_core_index);
136  if (adev) {
137  LEON3_Timer_Regs = (volatile struct gptimer_regs *)DEV_TO_APB(adev)->start;
138  LEON3_Timer_Adev = adev;
139 
140  /* Register AMBA Bus Frequency */
141  ambapp_freq_init(
142  &ambapp_plb,
143  LEON3_Timer_Adev,
144  (LEON3_Timer_Regs->scaler_reload + 1)
145  * LEON3_GPTIMER_0_FREQUENCY_SET_BY_BOOT_LOADER
146  );
147  /* Set user prescaler configuration. Use this to increase accuracy of timer
148  * and accociated services like cpucounter.
149  * Note that minimum value is the number of timer instances present in
150  * GRTIMER/GPTIMER hardware. See HW manual.
151  */
152  if (leon3_timer_prescaler)
153  LEON3_Timer_Regs->scaler_reload = leon3_timer_prescaler;
154  }
155 
156 #ifdef RTEMS_DRVMGR_STARTUP
157  /* Register Root bus, Use GRLIB AMBA PnP bus as root bus for LEON3 */
158  ambapp_grlib_root_register(&grlib_bus_config);
159 #endif
160 }
161 
162 RTEMS_SYSINIT_ITEM(
163  amba_initialize,
164  RTEMS_SYSINIT_BSP_START,
165  RTEMS_SYSINIT_ORDER_SECOND
166 );
ISR lock control.
Definition: isrlock.h:56
LEON3 BSP data types and macros.
#define RTEMS_INTERRUPT_LOCK_INITIALIZER(_name)
%
Definition: intr.h:355