183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2e3889691SÁlvaro Fernández Rojas /*
3e3889691SÁlvaro Fernández Rojas * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
4e3889691SÁlvaro Fernández Rojas *
5e3889691SÁlvaro Fernández Rojas * Derived from linux/drivers/power/reset/syscon-reboot.c:
6e3889691SÁlvaro Fernández Rojas * Copyright (C) 2013, Applied Micro Circuits Corporation
7e3889691SÁlvaro Fernández Rojas * Author: Feng Kan <fkan@apm.com>
8e3889691SÁlvaro Fernández Rojas */
9e3889691SÁlvaro Fernández Rojas
10e3889691SÁlvaro Fernández Rojas #include <common.h>
11e3889691SÁlvaro Fernández Rojas #include <dm.h>
12e3889691SÁlvaro Fernández Rojas #include <errno.h>
13e3889691SÁlvaro Fernández Rojas #include <regmap.h>
14e3889691SÁlvaro Fernández Rojas #include <sysreset.h>
15e3889691SÁlvaro Fernández Rojas #include <syscon.h>
16e3889691SÁlvaro Fernández Rojas
17e3889691SÁlvaro Fernández Rojas struct syscon_reboot_priv {
18e3889691SÁlvaro Fernández Rojas struct regmap *regmap;
19e3889691SÁlvaro Fernández Rojas unsigned int offset;
20e3889691SÁlvaro Fernández Rojas unsigned int mask;
21e3889691SÁlvaro Fernández Rojas };
22e3889691SÁlvaro Fernández Rojas
syscon_reboot_request(struct udevice * dev,enum sysreset_t type)23e3889691SÁlvaro Fernández Rojas static int syscon_reboot_request(struct udevice *dev, enum sysreset_t type)
24e3889691SÁlvaro Fernández Rojas {
25e3889691SÁlvaro Fernández Rojas struct syscon_reboot_priv *priv = dev_get_priv(dev);
26e3889691SÁlvaro Fernández Rojas
27e3889691SÁlvaro Fernández Rojas regmap_write(priv->regmap, priv->offset, priv->mask);
28e3889691SÁlvaro Fernández Rojas
29e3889691SÁlvaro Fernández Rojas return -EINPROGRESS;
30e3889691SÁlvaro Fernández Rojas }
31e3889691SÁlvaro Fernández Rojas
32e3889691SÁlvaro Fernández Rojas static struct sysreset_ops syscon_reboot_ops = {
33e3889691SÁlvaro Fernández Rojas .request = syscon_reboot_request,
34e3889691SÁlvaro Fernández Rojas };
35e3889691SÁlvaro Fernández Rojas
syscon_reboot_probe(struct udevice * dev)36e3889691SÁlvaro Fernández Rojas int syscon_reboot_probe(struct udevice *dev)
37e3889691SÁlvaro Fernández Rojas {
38e3889691SÁlvaro Fernández Rojas struct syscon_reboot_priv *priv = dev_get_priv(dev);
39e3889691SÁlvaro Fernández Rojas int err;
40*1f6ca3f4SPatrick Delaunay u32 phandle;
41*1f6ca3f4SPatrick Delaunay ofnode node;
42e3889691SÁlvaro Fernández Rojas
43*1f6ca3f4SPatrick Delaunay err = ofnode_read_u32(dev_ofnode(dev), "regmap", &phandle);
44*1f6ca3f4SPatrick Delaunay if (err)
45e3889691SÁlvaro Fernández Rojas return err;
46e3889691SÁlvaro Fernández Rojas
47*1f6ca3f4SPatrick Delaunay node = ofnode_get_by_phandle(phandle);
48*1f6ca3f4SPatrick Delaunay if (!ofnode_valid(node))
49*1f6ca3f4SPatrick Delaunay return -EINVAL;
50*1f6ca3f4SPatrick Delaunay
51*1f6ca3f4SPatrick Delaunay priv->regmap = syscon_node_to_regmap(node);
52e3889691SÁlvaro Fernández Rojas if (!priv->regmap) {
539b643e31SMasahiro Yamada pr_err("unable to find regmap\n");
54e3889691SÁlvaro Fernández Rojas return -ENODEV;
55e3889691SÁlvaro Fernández Rojas }
56e3889691SÁlvaro Fernández Rojas
5706069066SÁlvaro Fernández Rojas priv->offset = dev_read_u32_default(dev, "offset", 0);
5806069066SÁlvaro Fernández Rojas priv->mask = dev_read_u32_default(dev, "mask", 0);
59e3889691SÁlvaro Fernández Rojas
60e3889691SÁlvaro Fernández Rojas return 0;
61e3889691SÁlvaro Fernández Rojas }
62e3889691SÁlvaro Fernández Rojas
63e3889691SÁlvaro Fernández Rojas static const struct udevice_id syscon_reboot_ids[] = {
64e3889691SÁlvaro Fernández Rojas { .compatible = "syscon-reboot" },
65e3889691SÁlvaro Fernández Rojas { /* sentinel */ }
66e3889691SÁlvaro Fernández Rojas };
67e3889691SÁlvaro Fernández Rojas
68e3889691SÁlvaro Fernández Rojas U_BOOT_DRIVER(syscon_reboot) = {
69e3889691SÁlvaro Fernández Rojas .name = "syscon_reboot",
70e3889691SÁlvaro Fernández Rojas .id = UCLASS_SYSRESET,
71e3889691SÁlvaro Fernández Rojas .of_match = syscon_reboot_ids,
72e3889691SÁlvaro Fernández Rojas .probe = syscon_reboot_probe,
73e3889691SÁlvaro Fernández Rojas .priv_auto_alloc_size = sizeof(struct syscon_reboot_priv),
74e3889691SÁlvaro Fernández Rojas .ops = &syscon_reboot_ops,
75e3889691SÁlvaro Fernández Rojas };
76