aboutsummaryrefslogtreecommitdiff
path: root/drivers/reboot-mode/reboot-mode-uclass.c
blob: 2b38aa26b859d72b6270151228db1f53a44672b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (c), Vaisala Oyj
 */

#include <common.h>
#include <dm.h>
#include <dm/device_compat.h>
#include <dm/devres.h>
#include <exports.h>
#include <reboot-mode/reboot-mode.h>

int dm_reboot_mode_update(struct udevice *dev)
{
	struct reboot_mode_ops *ops = reboot_mode_get_ops(dev);
	u32 rebootmode;
	int ret, i;

	assert(ops);

	if (!ops->get)
		return -ENOSYS;

	ret = ops->get(dev, &rebootmode);
	if (ret < 0) {
		dev_err(dev, "Failed to retrieve the reboot mode value\n");
		return ret;
	}

	const struct reboot_mode_uclass_platdata *plat_data =
		dev_get_uclass_plat(dev);

	for (i = 0; i < plat_data->count; i++) {
		if (plat_data->modes[i].mode_id == rebootmode) {
			ret = env_set(plat_data->env_variable,
				      plat_data->modes[i].mode_name);
			if (ret) {
				dev_err(dev, "Failed to set env: %s\n",
					plat_data->env_variable);
				return ret;
			}
		}
	}

	if (ops->set) {
		/* Clear the value */
		rebootmode = 0;
		ret = ops->set(dev, rebootmode);
		if (ret) {
			dev_err(dev, "Failed to clear the reboot mode\n");
			return ret;
		}
	}

	return 0;
}

int dm_reboot_mode_pre_probe(struct udevice *dev)
{
	struct reboot_mode_uclass_platdata *plat_data;

	plat_data = dev_get_uclass_plat(dev);
	if (!plat_data)
		return -EINVAL;

#if CONFIG_IS_ENABLED(OF_CONTROL)
	const char *mode_prefix = "mode-";
	const int mode_prefix_len = strlen(mode_prefix);
	struct ofprop property;
	const u32 *propvalue;
	const char *propname;

	plat_data->env_variable = dev_read_string(dev, "u-boot,env-variable");
	if (!plat_data->env_variable)
		plat_data->env_variable = "reboot-mode";

	plat_data->count = 0;

	dev_for_each_property(property, dev) {
		propvalue = dev_read_prop_by_prop(&property, &propname, NULL);
		if (!propvalue) {
			dev_err(dev, "Could not get the value for property %s\n",
				propname);
			return -EINVAL;
		}

		if (!strncmp(propname, mode_prefix, mode_prefix_len))
			plat_data->count++;
	}

	plat_data->modes = devm_kcalloc(dev, plat_data->count,
					sizeof(struct reboot_mode_mode), 0);

	struct reboot_mode_mode *next = plat_data->modes;

	dev_for_each_property(property, dev) {
		propvalue = dev_read_prop_by_prop(&property, &propname, NULL);
		if (!propvalue) {
			dev_err(dev, "Could not get the value for property %s\n",
				propname);
			return -EINVAL;
		}

		if (!strncmp(propname, mode_prefix, mode_prefix_len)) {
			next->mode_name = &propname[mode_prefix_len];
			next->mode_id = fdt32_to_cpu(*propvalue);

			next++;
		}
	}
#else
	if (!plat_data->env_variable)
		plat_data->env_variable = "reboot-mode";

#endif

	return 0;
}

UCLASS_DRIVER(reboot_mode) = {
	.name	= "reboot-mode",
	.id	= UCLASS_REBOOT_MODE,
	.pre_probe	= dm_reboot_mode_pre_probe,
	.per_device_plat_auto =
		sizeof(struct reboot_mode_uclass_platdata),
};