1 | /* |
---|
2 | * Copyright (c) 2018 embedded brains GmbH |
---|
3 | * |
---|
4 | * Redistribution and use in source and binary forms, with or without |
---|
5 | * modification, are permitted provided that the following conditions |
---|
6 | * are met: |
---|
7 | * 1. Redistributions of source code must retain the above copyright |
---|
8 | * notice, this list of conditions and the following disclaimer. |
---|
9 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer in the |
---|
11 | * documentation and/or other materials provided with the distribution. |
---|
12 | * |
---|
13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
14 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
16 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
17 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
18 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
19 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
20 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
21 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
22 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
23 | * SUCH DAMAGE. |
---|
24 | */ |
---|
25 | |
---|
26 | #include <bsp/bootcard.h> |
---|
27 | #include <bsp/fatal.h> |
---|
28 | #include <bsp/fdt.h> |
---|
29 | #include <bsp/irq-generic.h> |
---|
30 | #include <bsp/riscv.h> |
---|
31 | |
---|
32 | #include <libfdt.h> |
---|
33 | |
---|
34 | void *riscv_fdt_get_address(const void *fdt, int node) |
---|
35 | { |
---|
36 | int parent; |
---|
37 | int ac; |
---|
38 | int len; |
---|
39 | const uint32_t *reg; |
---|
40 | uint64_t addr; |
---|
41 | |
---|
42 | parent = fdt_parent_offset(fdt, node); |
---|
43 | if (parent < 0) { |
---|
44 | return NULL; |
---|
45 | } |
---|
46 | |
---|
47 | ac = fdt_address_cells(fdt, parent); |
---|
48 | if (ac != 1 && ac != 2) { |
---|
49 | return NULL; |
---|
50 | } |
---|
51 | |
---|
52 | reg = fdt_getprop(fdt, node, "reg", &len); |
---|
53 | if (reg == NULL || len < ac) { |
---|
54 | return NULL; |
---|
55 | } |
---|
56 | |
---|
57 | addr = 0; |
---|
58 | |
---|
59 | while (ac > 0) { |
---|
60 | addr = (addr << 32) | fdt32_to_cpu(*reg); |
---|
61 | ++reg; |
---|
62 | --ac; |
---|
63 | } |
---|
64 | |
---|
65 | #if __riscv_xlen < 64 |
---|
66 | if (addr > 0xffffffff) { |
---|
67 | return NULL; |
---|
68 | } |
---|
69 | #endif |
---|
70 | |
---|
71 | return (void *)(uintptr_t) addr; |
---|
72 | } |
---|
73 | |
---|
74 | #ifdef RTEMS_SMP |
---|
75 | uint32_t riscv_hart_count; |
---|
76 | |
---|
77 | uint32_t riscv_hart_phandles[CPU_MAXIMUM_PROCESSORS]; |
---|
78 | |
---|
79 | static void riscv_find_harts(void) |
---|
80 | { |
---|
81 | const void *fdt; |
---|
82 | int node; |
---|
83 | uint32_t max_hart_index; |
---|
84 | |
---|
85 | fdt = bsp_fdt_get(); |
---|
86 | max_hart_index = 0; |
---|
87 | node = -1; |
---|
88 | |
---|
89 | while ((node = fdt_node_offset_by_compatible(fdt, node, "riscv")) >= 0) { |
---|
90 | int subnode; |
---|
91 | const uint32_t *val; |
---|
92 | int len; |
---|
93 | uint32_t phandle; |
---|
94 | uint32_t hart_index; |
---|
95 | |
---|
96 | val = fdt_getprop(fdt, node, "reg", &len); |
---|
97 | if (val == NULL || len != 4) { |
---|
98 | bsp_fatal(RISCV_FATAL_INVALID_HART_REG_IN_DEVICE_TREE); |
---|
99 | } |
---|
100 | |
---|
101 | hart_index = fdt32_to_cpu(val[0]); |
---|
102 | |
---|
103 | if (hart_index >= CPU_MAXIMUM_PROCESSORS) { |
---|
104 | continue; |
---|
105 | } |
---|
106 | |
---|
107 | if (hart_index > max_hart_index) { |
---|
108 | max_hart_index = hart_index; |
---|
109 | } |
---|
110 | |
---|
111 | phandle = 0; |
---|
112 | |
---|
113 | fdt_for_each_subnode(subnode, fdt, node) { |
---|
114 | int propoff; |
---|
115 | bool interrupt_controller; |
---|
116 | uint32_t potential_phandle; |
---|
117 | |
---|
118 | interrupt_controller = false; |
---|
119 | potential_phandle = 0; |
---|
120 | |
---|
121 | fdt_for_each_property_offset(propoff, fdt, subnode) { |
---|
122 | const char *name; |
---|
123 | |
---|
124 | val = fdt_getprop_by_offset(fdt, propoff, &name, &len); |
---|
125 | if (val != NULL) { |
---|
126 | if (strcmp(name, "interrupt-controller") == 0) { |
---|
127 | interrupt_controller = true; |
---|
128 | } else if (len == 4 && strcmp(name, "phandle") == 0) { |
---|
129 | potential_phandle = fdt32_to_cpu(val[0]); |
---|
130 | } |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | if (interrupt_controller) { |
---|
135 | phandle = potential_phandle; |
---|
136 | break; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | riscv_hart_phandles[hart_index] = phandle; |
---|
141 | } |
---|
142 | |
---|
143 | riscv_hart_count = max_hart_index + 1; |
---|
144 | } |
---|
145 | |
---|
146 | uint32_t riscv_get_hart_index_by_phandle(uint32_t phandle) |
---|
147 | { |
---|
148 | uint32_t hart_index; |
---|
149 | |
---|
150 | for (hart_index = 0; hart_index < riscv_hart_count; ++hart_index) { |
---|
151 | if (riscv_hart_phandles[hart_index] == phandle) { |
---|
152 | return hart_index; |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | return UINT32_MAX; |
---|
157 | } |
---|
158 | #endif |
---|
159 | |
---|
160 | void bsp_start(void) |
---|
161 | { |
---|
162 | #ifdef RTEMS_SMP |
---|
163 | riscv_find_harts(); |
---|
164 | #endif |
---|
165 | bsp_interrupt_initialize(); |
---|
166 | } |
---|