blob: 525faf2f89e26c610331a8537569941aee0e0c69 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Álvaro Fernández Rojase3889692017-04-25 00:39:14 +02002/*
3 * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
4 *
5 * Derived from linux/drivers/power/reset/syscon-reboot.c:
6 * Copyright (C) 2013, Applied Micro Circuits Corporation
7 * Author: Feng Kan <fkan@apm.com>
Álvaro Fernández Rojase3889692017-04-25 00:39:14 +02008 */
9
10#include <common.h>
11#include <dm.h>
12#include <errno.h>
13#include <regmap.h>
14#include <sysreset.h>
15#include <syscon.h>
Simon Glass61b29b82020-02-03 07:36:15 -070016#include <linux/err.h>
Álvaro Fernández Rojase3889692017-04-25 00:39:14 +020017
Álvaro Fernández Rojase3889692017-04-25 00:39:14 +020018struct syscon_reboot_priv {
19 struct regmap *regmap;
20 unsigned int offset;
21 unsigned int mask;
Bin Meng9cdfade2020-06-22 22:29:43 -070022 unsigned int value;
Álvaro Fernández Rojase3889692017-04-25 00:39:14 +020023};
24
25static int syscon_reboot_request(struct udevice *dev, enum sysreset_t type)
26{
27 struct syscon_reboot_priv *priv = dev_get_priv(dev);
Patrick Delaunay48694212019-05-20 10:58:39 +020028 ulong driver_data = dev_get_driver_data(dev);
Álvaro Fernández Rojase3889692017-04-25 00:39:14 +020029
Patrick Delaunay48694212019-05-20 10:58:39 +020030 if (type != driver_data)
Patrick Delaunay50fd31f2019-04-18 17:16:21 +020031 return -EPROTONOSUPPORT;
32
Bin Meng9cdfade2020-06-22 22:29:43 -070033 regmap_update_bits(priv->regmap, priv->offset, priv->mask, priv->value);
Álvaro Fernández Rojase3889692017-04-25 00:39:14 +020034
35 return -EINPROGRESS;
36}
37
38static struct sysreset_ops syscon_reboot_ops = {
39 .request = syscon_reboot_request,
40};
41
Samuel Holland30ba45d2021-11-03 22:55:12 -050042static int syscon_reboot_probe(struct udevice *dev)
Álvaro Fernández Rojase3889692017-04-25 00:39:14 +020043{
Álvaro Fernández Rojase3889692017-04-25 00:39:14 +020044 struct syscon_reboot_priv *priv = dev_get_priv(dev);
Bin Meng1ce81822020-06-22 22:29:42 -070045 int err;
Bin Meng9cdfade2020-06-22 22:29:43 -070046 int mask_err, value_err;
Álvaro Fernández Rojase3889692017-04-25 00:39:14 +020047
Patrick Delaunay662a74a2019-03-07 09:57:14 +010048 priv->regmap = syscon_regmap_lookup_by_phandle(dev, "regmap");
49 if (IS_ERR(priv->regmap)) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090050 pr_err("unable to find regmap\n");
Álvaro Fernández Rojase3889692017-04-25 00:39:14 +020051 return -ENODEV;
52 }
53
Bin Meng1ce81822020-06-22 22:29:42 -070054 err = dev_read_u32(dev, "offset", &priv->offset);
55 if (err) {
56 pr_err("unable to find offset\n");
57 return -ENOENT;
58 }
59
Bin Meng9cdfade2020-06-22 22:29:43 -070060 mask_err = dev_read_u32(dev, "mask", &priv->mask);
61 value_err = dev_read_u32(dev, "value", &priv->value);
62 if (mask_err && value_err) {
63 pr_err("unable to find mask and value\n");
64 return -EINVAL;
65 }
66
67 if (value_err) {
68 /* support old binding */
69 priv->value = priv->mask;
70 priv->mask = 0xffffffff;
71 } else if (mask_err) {
72 /* support value without mask*/
73 priv->mask = 0xffffffff;
Bin Meng1ce81822020-06-22 22:29:42 -070074 }
Álvaro Fernández Rojase3889692017-04-25 00:39:14 +020075
76 return 0;
77}
78
79static const struct udevice_id syscon_reboot_ids[] = {
Patrick Delaunay48694212019-05-20 10:58:39 +020080 { .compatible = "syscon-reboot", .data = SYSRESET_COLD },
81 { .compatible = "syscon-poweroff", .data = SYSRESET_POWER_OFF },
Álvaro Fernández Rojase3889692017-04-25 00:39:14 +020082 { /* sentinel */ }
83};
84
85U_BOOT_DRIVER(syscon_reboot) = {
86 .name = "syscon_reboot",
87 .id = UCLASS_SYSRESET,
88 .of_match = syscon_reboot_ids,
89 .probe = syscon_reboot_probe,
Simon Glass41575d82020-12-03 16:55:17 -070090 .priv_auto = sizeof(struct syscon_reboot_priv),
Álvaro Fernández Rojase3889692017-04-25 00:39:14 +020091 .ops = &syscon_reboot_ops,
92};