/src/barebox/arch/sandbox/board/power.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | |
3 | | #include <common.h> |
4 | | #include <driver.h> |
5 | | #include <init.h> |
6 | | #include <of.h> |
7 | | #include <poweroff.h> |
8 | | #include <restart.h> |
9 | | #include <mach/linux.h> |
10 | | #include <asm/reset_source.h> |
11 | | #include <linux/nvmem-consumer.h> |
12 | | |
13 | | struct sandbox_power { |
14 | | struct restart_handler rst_hang, rst_reexec; |
15 | | struct poweroff_handler poweroff; |
16 | | struct nvmem_cell *reset_source_cell; |
17 | | }; |
18 | | |
19 | | static void sandbox_poweroff(struct poweroff_handler *poweroff, |
20 | | unsigned long flags) |
21 | 0 | { |
22 | 0 | struct sandbox_power *power = container_of(poweroff, struct sandbox_power, poweroff); |
23 | |
|
24 | 0 | sandbox_save_reset_source(power->reset_source_cell, RESET_POR); |
25 | |
|
26 | 0 | linux_exit(); |
27 | 0 | } |
28 | | |
29 | | static void sandbox_rst_hang(struct restart_handler *rst, |
30 | | unsigned long flags) |
31 | 0 | { |
32 | 0 | linux_hang(); |
33 | 0 | } |
34 | | |
35 | | static void sandbox_rst_reexec(struct restart_handler *rst, |
36 | | unsigned long flags) |
37 | 0 | { |
38 | 0 | struct sandbox_power *power = container_of(rst, struct sandbox_power, rst_reexec); |
39 | |
|
40 | 0 | sandbox_save_reset_source(power->reset_source_cell, RESET_RST); |
41 | |
|
42 | 0 | linux_reexec(); |
43 | 0 | } |
44 | | |
45 | | static int sandbox_power_probe(struct device *dev) |
46 | 32 | { |
47 | 32 | struct sandbox_power *power = xzalloc(sizeof(*power)); |
48 | 32 | size_t len; |
49 | 32 | u8 *rst; |
50 | | |
51 | 32 | power->poweroff = (struct poweroff_handler) { |
52 | 32 | .name = "exit", |
53 | 32 | .poweroff = sandbox_poweroff |
54 | 32 | }; |
55 | | |
56 | 32 | poweroff_handler_register(&power->poweroff); |
57 | | |
58 | 32 | power->rst_hang = (struct restart_handler) { |
59 | 32 | .name = "hang", |
60 | 32 | .restart = sandbox_rst_hang |
61 | 32 | }; |
62 | | |
63 | 32 | power->rst_reexec = (struct restart_handler) { |
64 | 32 | .name = "reexec", .priority = 200, |
65 | 32 | .restart = sandbox_rst_reexec, |
66 | 32 | }; |
67 | | |
68 | 32 | restart_handler_register(&power->rst_hang); |
69 | | |
70 | 32 | if (IS_ENABLED(CONFIG_SANDBOX_REEXEC)) |
71 | 32 | restart_handler_register(&power->rst_reexec); |
72 | | |
73 | 32 | if (!dev->of_node) |
74 | 0 | return 0; |
75 | | |
76 | 32 | power->reset_source_cell = of_nvmem_cell_get(dev->of_node, |
77 | 32 | "reset-source"); |
78 | 32 | if (IS_ERR(power->reset_source_cell)) { |
79 | 0 | if (PTR_ERR(power->reset_source_cell) != -EPROBE_DEFER) |
80 | 0 | dev_warn(dev, "No reset source info available: %pe\n", |
81 | 0 | power->reset_source_cell); |
82 | 0 | power->reset_source_cell = NULL; |
83 | 0 | return 0; |
84 | 0 | } |
85 | | |
86 | 32 | rst = nvmem_cell_read(power->reset_source_cell, &len); |
87 | 32 | if (!IS_ERR(rst)) { |
88 | 32 | reset_source_set_prinst(*rst, RESET_SOURCE_DEFAULT_PRIORITY, 0); |
89 | | |
90 | 32 | free(rst); |
91 | 32 | } |
92 | | |
93 | 32 | return 0; |
94 | 32 | } |
95 | | |
96 | | static __maybe_unused struct of_device_id sandbox_power_dt_ids[] = { |
97 | | { .compatible = "barebox,sandbox-power" }, |
98 | | { /* sentinel */ } |
99 | | }; |
100 | | MODULE_DEVICE_TABLE(of, sandbox_power_dt_ids); |
101 | | |
102 | | static struct driver sandbox_power_drv = { |
103 | | .name = "sandbox-power", |
104 | | .of_compatible = sandbox_power_dt_ids, |
105 | | .probe = sandbox_power_probe, |
106 | | }; |
107 | | coredevice_platform_driver(sandbox_power_drv); |
108 | | |
109 | | /* |
110 | | * A device tree passed with --dtb need not have a sandbox-power node. |
111 | | * Without one, poweroff and reset end in hang(), which busy-loops forever, |
112 | | * so register the device ourselves in this case. |
113 | | */ |
114 | | static int sandbox_power_fallback(void) |
115 | 32 | { |
116 | 32 | if (!of_find_compatible_node(NULL, NULL, "barebox,sandbox-power")) |
117 | 0 | add_generic_device("sandbox-power", DEVICE_ID_SINGLE, NULL, 0, |
118 | 0 | 0, 0, NULL); |
119 | | |
120 | 32 | return 0; |
121 | 32 | } |
122 | | coredevice_initcall(sandbox_power_fallback); |