blob: e468dac0e90204150df7fad7317e8d098b71e49a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Álvaro Fernández Rojasaa19aaf2017-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 Rojasaa19aaf2017-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 Glassd66c5f72020-02-03 07:36:15 -070016#include <linux/err.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060017#include <linux/printk.h>
Álvaro Fernández Rojasaa19aaf2017-04-25 00:39:14 +020018
Álvaro Fernández Rojasaa19aaf2017-04-25 00:39:14 +020019struct syscon_reboot_priv {
20 struct regmap *regmap;
21 unsigned int offset;
22 unsigned int mask;
Bin Meng91331752020-06-22 22:29:43 -070023 unsigned int value;
Álvaro Fernández Rojasaa19aaf2017-04-25 00:39:14 +020024};
25
26static int syscon_reboot_request(struct udevice *dev, enum sysreset_t type)
27{
28 struct syscon_reboot_priv *priv = dev_get_priv(dev);
Patrick Delaunay0e826552019-05-20 10:58:39 +020029 ulong driver_data = dev_get_driver_data(dev);
Álvaro Fernández Rojasaa19aaf2017-04-25 00:39:14 +020030
Patrick Delaunay0e826552019-05-20 10:58:39 +020031 if (type != driver_data)
Patrick Delaunay9dd211e2019-04-18 17:16:21 +020032 return -EPROTONOSUPPORT;
33
Bin Meng91331752020-06-22 22:29:43 -070034 regmap_update_bits(priv->regmap, priv->offset, priv->mask, priv->value);
Álvaro Fernández Rojasaa19aaf2017-04-25 00:39:14 +020035
36 return -EINPROGRESS;
37}
38
39static struct sysreset_ops syscon_reboot_ops = {
40 .request = syscon_reboot_request,
41};
42
Samuel Holland83e32fa2021-11-03 22:55:12 -050043static int syscon_reboot_probe(struct udevice *dev)
Álvaro Fernández Rojasaa19aaf2017-04-25 00:39:14 +020044{
Álvaro Fernández Rojasaa19aaf2017-04-25 00:39:14 +020045 struct syscon_reboot_priv *priv = dev_get_priv(dev);
Bin Meng961ca842020-06-22 22:29:42 -070046 int err;
Bin Meng91331752020-06-22 22:29:43 -070047 int mask_err, value_err;
Álvaro Fernández Rojasaa19aaf2017-04-25 00:39:14 +020048
Patrick Delaunaya48fc802019-03-07 09:57:14 +010049 priv->regmap = syscon_regmap_lookup_by_phandle(dev, "regmap");
50 if (IS_ERR(priv->regmap)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090051 pr_err("unable to find regmap\n");
Álvaro Fernández Rojasaa19aaf2017-04-25 00:39:14 +020052 return -ENODEV;
53 }
54
Bin Meng961ca842020-06-22 22:29:42 -070055 err = dev_read_u32(dev, "offset", &priv->offset);
56 if (err) {
57 pr_err("unable to find offset\n");
58 return -ENOENT;
59 }
60
Bin Meng91331752020-06-22 22:29:43 -070061 mask_err = dev_read_u32(dev, "mask", &priv->mask);
62 value_err = dev_read_u32(dev, "value", &priv->value);
63 if (mask_err && value_err) {
64 pr_err("unable to find mask and value\n");
65 return -EINVAL;
66 }
67
68 if (value_err) {
69 /* support old binding */
70 priv->value = priv->mask;
71 priv->mask = 0xffffffff;
72 } else if (mask_err) {
73 /* support value without mask*/
74 priv->mask = 0xffffffff;
Bin Meng961ca842020-06-22 22:29:42 -070075 }
Álvaro Fernández Rojasaa19aaf2017-04-25 00:39:14 +020076
77 return 0;
78}
79
80static const struct udevice_id syscon_reboot_ids[] = {
Patrick Delaunay0e826552019-05-20 10:58:39 +020081 { .compatible = "syscon-reboot", .data = SYSRESET_COLD },
82 { .compatible = "syscon-poweroff", .data = SYSRESET_POWER_OFF },
Álvaro Fernández Rojasaa19aaf2017-04-25 00:39:14 +020083 { /* sentinel */ }
84};
85
86U_BOOT_DRIVER(syscon_reboot) = {
87 .name = "syscon_reboot",
88 .id = UCLASS_SYSRESET,
89 .of_match = syscon_reboot_ids,
90 .probe = syscon_reboot_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -070091 .priv_auto = sizeof(struct syscon_reboot_priv),
Álvaro Fernández Rojasaa19aaf2017-04-25 00:39:14 +020092 .ops = &syscon_reboot_ops,
93};