1 | /*
|
---|
2 | * Copyright (c) 2016 Petr Pavlu
|
---|
3 | * 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 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * @addtogroup arm64virt
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 |
|
---|
34 | /** @file
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <assert.h>
|
---|
38 | #include <stdio.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <stdbool.h>
|
---|
41 | #include <stdlib.h>
|
---|
42 |
|
---|
43 | #include <ddf/driver.h>
|
---|
44 | #include <ddf/log.h>
|
---|
45 | #include <ops/hw_res.h>
|
---|
46 | #include <ops/pio_window.h>
|
---|
47 |
|
---|
48 | #define NAME "arm64virt"
|
---|
49 |
|
---|
50 | enum {
|
---|
51 | arm64virt_ic_distr_membase = 0x08000000,
|
---|
52 | arm64virt_ic_distr_memsize = 0x00001000,
|
---|
53 | arm64virt_ic_cpui_membase = 0x08010000,
|
---|
54 | arm64virt_ic_cpui_memsize = 0x00001004,
|
---|
55 | arm64virt_uart_irq = 33,
|
---|
56 | arm64virt_uart_membase = 0x09000000,
|
---|
57 | arm64virt_uart_memsize = 0x00001000
|
---|
58 | };
|
---|
59 |
|
---|
60 | typedef struct arm64virt_fun {
|
---|
61 | hw_resource_list_t hw_resources;
|
---|
62 | } arm64virt_fun_t;
|
---|
63 |
|
---|
64 | static errno_t arm64virt_dev_add(ddf_dev_t *);
|
---|
65 |
|
---|
66 | static driver_ops_t arm64virt_ops = {
|
---|
67 | .dev_add = &arm64virt_dev_add
|
---|
68 | };
|
---|
69 |
|
---|
70 | static driver_t arm64virt_driver = {
|
---|
71 | .name = NAME,
|
---|
72 | .driver_ops = &arm64virt_ops
|
---|
73 | };
|
---|
74 |
|
---|
75 | static hw_resource_t arm64virt_ic_res[] = {
|
---|
76 | {
|
---|
77 | .type = MEM_RANGE,
|
---|
78 | .res.mem_range = {
|
---|
79 | .address = arm64virt_ic_distr_membase,
|
---|
80 | .size = arm64virt_ic_distr_memsize,
|
---|
81 | .relative = false,
|
---|
82 | .endianness = LITTLE_ENDIAN
|
---|
83 | }
|
---|
84 | },
|
---|
85 | {
|
---|
86 | .type = MEM_RANGE,
|
---|
87 | .res.mem_range = {
|
---|
88 | .address = arm64virt_ic_cpui_membase,
|
---|
89 | .size = arm64virt_ic_cpui_memsize,
|
---|
90 | .relative = false,
|
---|
91 | .endianness = LITTLE_ENDIAN
|
---|
92 | }
|
---|
93 | }
|
---|
94 | };
|
---|
95 |
|
---|
96 | static hw_resource_t arm64virt_uart_res[] = {
|
---|
97 | {
|
---|
98 | .type = MEM_RANGE,
|
---|
99 | .res.mem_range = {
|
---|
100 | .address = arm64virt_uart_membase,
|
---|
101 | .size = arm64virt_uart_memsize,
|
---|
102 | .relative = false,
|
---|
103 | .endianness = LITTLE_ENDIAN
|
---|
104 | }
|
---|
105 | },
|
---|
106 | {
|
---|
107 | .type = INTERRUPT,
|
---|
108 | .res.interrupt = {
|
---|
109 | .irq = arm64virt_uart_irq
|
---|
110 | }
|
---|
111 | }
|
---|
112 | };
|
---|
113 |
|
---|
114 | static pio_window_t arm64virt_pio_window = {
|
---|
115 | .mem = {
|
---|
116 | .base = 0,
|
---|
117 | .size = -1
|
---|
118 | }
|
---|
119 | };
|
---|
120 |
|
---|
121 | static arm64virt_fun_t arm64virt_ic_fun_proto = {
|
---|
122 | .hw_resources = {
|
---|
123 | sizeof(arm64virt_ic_res) / sizeof(arm64virt_ic_res[0]),
|
---|
124 | arm64virt_ic_res
|
---|
125 | },
|
---|
126 | };
|
---|
127 |
|
---|
128 | static arm64virt_fun_t arm64virt_uart_fun_proto = {
|
---|
129 | .hw_resources = {
|
---|
130 | sizeof(arm64virt_uart_res) / sizeof(arm64virt_uart_res[0]),
|
---|
131 | arm64virt_uart_res
|
---|
132 | },
|
---|
133 | };
|
---|
134 |
|
---|
135 | /** Obtain function soft-state from DDF function node. */
|
---|
136 | static arm64virt_fun_t *arm64virt_fun(ddf_fun_t *fnode)
|
---|
137 | {
|
---|
138 | return ddf_fun_data_get(fnode);
|
---|
139 | }
|
---|
140 |
|
---|
141 | static hw_resource_list_t *arm64virt_get_resources(ddf_fun_t *fnode)
|
---|
142 | {
|
---|
143 | arm64virt_fun_t *fun = arm64virt_fun(fnode);
|
---|
144 |
|
---|
145 | assert(fun != NULL);
|
---|
146 | return &fun->hw_resources;
|
---|
147 | }
|
---|
148 |
|
---|
149 | static errno_t arm64virt_enable_interrupt(ddf_fun_t *fun, int irq)
|
---|
150 | {
|
---|
151 | /* TODO */
|
---|
152 | return false;
|
---|
153 | }
|
---|
154 |
|
---|
155 | static pio_window_t *arm64virt_get_pio_window(ddf_fun_t *fnode)
|
---|
156 | {
|
---|
157 | return &arm64virt_pio_window;
|
---|
158 | }
|
---|
159 |
|
---|
160 | static hw_res_ops_t arm64virt_hw_res_ops = {
|
---|
161 | .get_resource_list = &arm64virt_get_resources,
|
---|
162 | .enable_interrupt = &arm64virt_enable_interrupt,
|
---|
163 | };
|
---|
164 |
|
---|
165 | static pio_window_ops_t arm64virt_pio_window_ops = {
|
---|
166 | .get_pio_window = &arm64virt_get_pio_window
|
---|
167 | };
|
---|
168 |
|
---|
169 | static ddf_dev_ops_t arm64virt_fun_ops = {
|
---|
170 | .interfaces = {
|
---|
171 | [HW_RES_DEV_IFACE] = &arm64virt_hw_res_ops,
|
---|
172 | [PIO_WINDOW_DEV_IFACE] = &arm64virt_pio_window_ops
|
---|
173 | }
|
---|
174 | };
|
---|
175 |
|
---|
176 | static errno_t arm64virt_add_fun(ddf_dev_t *dev, const char *name,
|
---|
177 | const char *str_match_id, arm64virt_fun_t *fun_proto)
|
---|
178 | {
|
---|
179 | ddf_msg(LVL_NOTE, "Adding function '%s'.", name);
|
---|
180 |
|
---|
181 | ddf_fun_t *fnode = NULL;
|
---|
182 | errno_t rc;
|
---|
183 |
|
---|
184 | /* Create new device. */
|
---|
185 | fnode = ddf_fun_create(dev, fun_inner, name);
|
---|
186 | if (fnode == NULL) {
|
---|
187 | ddf_msg(LVL_ERROR, "Error creating function '%s'.", name);
|
---|
188 | rc = ENOMEM;
|
---|
189 | goto error;
|
---|
190 | }
|
---|
191 |
|
---|
192 | arm64virt_fun_t *fun = ddf_fun_data_alloc(fnode,
|
---|
193 | sizeof(arm64virt_fun_t));
|
---|
194 | *fun = *fun_proto;
|
---|
195 |
|
---|
196 | /* Add match ID. */
|
---|
197 | rc = ddf_fun_add_match_id(fnode, str_match_id, 100);
|
---|
198 | if (rc != EOK) {
|
---|
199 | ddf_msg(LVL_ERROR, "Error adding match ID to function '%s'.",
|
---|
200 | name);
|
---|
201 | goto error;
|
---|
202 | }
|
---|
203 |
|
---|
204 | /* Set provided operations to the device. */
|
---|
205 | ddf_fun_set_ops(fnode, &arm64virt_fun_ops);
|
---|
206 |
|
---|
207 | /* Register function. */
|
---|
208 | rc = ddf_fun_bind(fnode);
|
---|
209 | if (rc != EOK) {
|
---|
210 | ddf_msg(LVL_ERROR, "Failed binding function '%s'.", name);
|
---|
211 | goto error;
|
---|
212 | }
|
---|
213 |
|
---|
214 | return EOK;
|
---|
215 |
|
---|
216 | error:
|
---|
217 | if (fnode != NULL)
|
---|
218 | ddf_fun_destroy(fnode);
|
---|
219 |
|
---|
220 | return rc;
|
---|
221 | }
|
---|
222 |
|
---|
223 | static errno_t arm64virt_add_functions(ddf_dev_t *dev)
|
---|
224 | {
|
---|
225 | errno_t rc;
|
---|
226 |
|
---|
227 | rc = arm64virt_add_fun(dev, "intctl", "arm/gicv2",
|
---|
228 | &arm64virt_ic_fun_proto);
|
---|
229 | if (rc != EOK)
|
---|
230 | return rc;
|
---|
231 |
|
---|
232 | rc = arm64virt_add_fun(dev, "uart", "arm/pl011",
|
---|
233 | &arm64virt_uart_fun_proto);
|
---|
234 | if (rc != EOK)
|
---|
235 | return rc;
|
---|
236 |
|
---|
237 | return EOK;
|
---|
238 | }
|
---|
239 |
|
---|
240 | /** Add device. */
|
---|
241 | static errno_t arm64virt_dev_add(ddf_dev_t *dev)
|
---|
242 | {
|
---|
243 | ddf_msg(LVL_NOTE, "arm64virt_dev_add(), device=%s.",
|
---|
244 | ddf_dev_get_name(dev));
|
---|
245 |
|
---|
246 | /* Register functions. */
|
---|
247 | if (arm64virt_add_functions(dev))
|
---|
248 | ddf_msg(LVL_ERROR, "Failed to add functions for ARM64 QEMU "
|
---|
249 | "virt platform.");
|
---|
250 |
|
---|
251 | return EOK;
|
---|
252 | }
|
---|
253 |
|
---|
254 | int main(int argc, char *argv[])
|
---|
255 | {
|
---|
256 | errno_t rc;
|
---|
257 |
|
---|
258 | printf(NAME ": HelenOS ARM64 QEMU virt platform driver\n");
|
---|
259 |
|
---|
260 | rc = ddf_log_init(NAME);
|
---|
261 | if (rc != EOK) {
|
---|
262 | printf(NAME ": Failed connecting logging service.");
|
---|
263 | return 1;
|
---|
264 | }
|
---|
265 |
|
---|
266 | return ddf_driver_main(&arm64virt_driver);
|
---|
267 | }
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * @}
|
---|
271 | */
|
---|