summaryrefslogtreecommitdiff
path: root/drivers/reboot-mode/reboot-mode-rtc.c
blob: 972d0cdbcb5b23ee0a088b184575732e16490747 (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
127
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (c), Vaisala Oyj
 */

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

DECLARE_GLOBAL_DATA_PTR;

static int reboot_mode_get(struct udevice *dev, u32 *buf)
{
	if (!buf)
		return -EINVAL;

	int ret;
	u8 *val = (u8 *)buf;
	struct reboot_mode_rtc_platdata *plat_data;

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

	for (int i = 0; i < plat_data->size; i++) {
		ret = rtc_read8(plat_data->rtc, plat_data->addr + i);
		if (ret < 0)
			return ret;

		val[i] = ret;
	}

	if (plat_data->is_big_endian)
		*buf = __be32_to_cpu(*buf);
	else
		*buf = __le32_to_cpu(*buf);

	return 0;
}

static int reboot_mode_set(struct udevice *dev, u32 buf)
{
	int ret;
	u8 *val;
	struct reboot_mode_rtc_platdata *plat_data;

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

	if (plat_data->is_big_endian)
		buf = __cpu_to_be32(buf);
	else
		buf = __cpu_to_le32(buf);

	val = (u8 *)&buf;

	for (int i = 0; i < plat_data->size; i++) {
		ret = rtc_write8(plat_data->rtc, (plat_data->addr + i), val[i]);
		if (ret < 0)
			return ret;
	}

	return 0;
}

#if CONFIG_IS_ENABLED(OF_CONTROL)
static int reboot_mode_ofdata_to_platdata(struct udevice *dev)
{
	struct ofnode_phandle_args phandle_args;
	struct reboot_mode_rtc_platdata *plat_data;

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

	if (dev_read_phandle_with_args(dev, "rtc", NULL, 0, 0, &phandle_args)) {
		dev_err(dev, "RTC device not specified\n");
		return -ENOENT;
	}

	if (uclass_get_device_by_ofnode(UCLASS_RTC, phandle_args.node,
					&plat_data->rtc)) {
		dev_err(dev, "could not get the RTC device\n");
		return -ENODEV;
	}

	plat_data->addr =
		dev_read_addr_size_index(dev, 0, (fdt_size_t *)&plat_data->size);
	if (plat_data->addr == FDT_ADDR_T_NONE) {
		dev_err(dev, "Invalid RTC address\n");
		return -EINVAL;
	}
	if (plat_data->size > sizeof(u32)) {
		dev_err(dev, "Invalid reg size\n");
		return -EINVAL;
	}

	plat_data->is_big_endian = ofnode_read_bool(dev_ofnode(dev), "big-endian");

	return 0;
}

static const struct udevice_id reboot_mode_ids[] = {
	{ .compatible = "reboot-mode-rtc", 0 },
	{}
};
#endif

static const struct reboot_mode_ops reboot_mode_rtc_ops = {
	.get = reboot_mode_get,
	.set = reboot_mode_set,
};

U_BOOT_DRIVER(reboot_mode_rtc) = {
	.name = "reboot-mode-rtc",
	.id = UCLASS_REBOOT_MODE,
#if CONFIG_IS_ENABLED(OF_CONTROL)
	.of_match = reboot_mode_ids,
	.of_to_plat = reboot_mode_ofdata_to_platdata,
#endif
	.plat_auto = sizeof(struct reboot_mode_rtc_platdata),
	.ops = &reboot_mode_rtc_ops,
};