/src/barebox/drivers/of/reserved-mem.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | // SPDX-FileCopyrightText: 2021 Rouven Czerwinski <r.czerwinski@pengutronix.de>, Pengutronix |
3 | | |
4 | | #define pr_fmt(fmt) "of-reserved-mem: " fmt |
5 | | |
6 | | #include <stdio.h> |
7 | | #include <of.h> |
8 | | #include <of_address.h> |
9 | | #include <memory.h> |
10 | | #include <linux/ioport.h> |
11 | | |
12 | 0 | #define MEMRESERVE_NCELLS 2 |
13 | | |
14 | | static void request_region(struct resource *r) |
15 | 0 | { |
16 | 0 | struct memory_bank *bank; |
17 | |
|
18 | 0 | for_each_memory_bank(bank) { |
19 | 0 | if (!resource_contains(bank->res, r)) |
20 | 0 | continue; |
21 | | |
22 | 0 | pr_debug("reserving %s at %pad-%pad\n", r->name, &r->start, &r->end); |
23 | |
|
24 | 0 | if (!reserve_sdram_region(r->name, r->start, resource_size(r))) |
25 | 0 | pr_warn("couldn't request reserved sdram region %pa-%pa\n", |
26 | 0 | &r->start, &r->end); |
27 | 0 | break; |
28 | 0 | } |
29 | 0 | } |
30 | | |
31 | | static int of_reserved_mem_walk(void) |
32 | 32 | { |
33 | 32 | struct device_node *node, *child; |
34 | 32 | int ncells = 0; |
35 | 32 | const __be32 *reg; |
36 | | |
37 | 32 | node = of_find_node_by_path("/reserved-memory"); |
38 | 32 | if (node) { |
39 | 0 | for_each_available_child_of_node(node, child) { |
40 | 0 | struct resource resource = {}; |
41 | | |
42 | | /* skip e.g. linux,cma */ |
43 | 0 | if (!of_get_property(child, "reg", NULL)) |
44 | 0 | continue; |
45 | | |
46 | 0 | of_address_to_resource(child, 0, &resource); |
47 | |
|
48 | 0 | resource.name = child->name; |
49 | 0 | resource.flags = IORESOURCE_MEM; |
50 | |
|
51 | 0 | request_region(&resource); |
52 | 0 | } |
53 | 0 | } |
54 | | |
55 | 32 | node = of_find_node_by_path("/$memreserve"); |
56 | 32 | reg = of_get_property(node, "reg", &ncells); |
57 | 32 | ncells /= sizeof(__be32); |
58 | 32 | if (reg) { |
59 | 0 | char name[sizeof "fdt-memreserve-4294967295"]; |
60 | 0 | int i = 0, n = 0; |
61 | |
|
62 | 0 | while (i < ncells) { |
63 | 0 | struct resource resource = {}; |
64 | 0 | u64 size; |
65 | |
|
66 | 0 | snprintf(name, sizeof(name), "fdt-memreserve-%u", n++); |
67 | 0 | resource.name = name; |
68 | 0 | resource.flags = IORESOURCE_MEM; |
69 | |
|
70 | 0 | resource.start = of_read_number(reg + i, MEMRESERVE_NCELLS); |
71 | 0 | i += MEMRESERVE_NCELLS; |
72 | |
|
73 | 0 | size = of_read_number(reg + i, MEMRESERVE_NCELLS); |
74 | 0 | i += MEMRESERVE_NCELLS; |
75 | |
|
76 | 0 | if (!size) |
77 | 0 | continue; |
78 | | |
79 | 0 | resource.end = resource.start + size - 1; |
80 | |
|
81 | 0 | request_region(&resource); |
82 | 0 | } |
83 | 0 | } |
84 | | |
85 | 32 | return 0; |
86 | 32 | } |
87 | | postmem_initcall(of_reserved_mem_walk); |