Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Álvaro Fernández Rojas | e388969 | 2017-04-25 00:39:14 +0200 | [diff] [blame] | 2 | /* |
| 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 Rojas | e388969 | 2017-04-25 00:39:14 +0200 | [diff] [blame] | 8 | */ |
| 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 Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 16 | #include <linux/err.h> |
Álvaro Fernández Rojas | e388969 | 2017-04-25 00:39:14 +0200 | [diff] [blame] | 17 | |
Álvaro Fernández Rojas | e388969 | 2017-04-25 00:39:14 +0200 | [diff] [blame] | 18 | struct syscon_reboot_priv { |
| 19 | struct regmap *regmap; |
| 20 | unsigned int offset; |
| 21 | unsigned int mask; |
Bin Meng | 9cdfade | 2020-06-22 22:29:43 -0700 | [diff] [blame] | 22 | unsigned int value; |
Álvaro Fernández Rojas | e388969 | 2017-04-25 00:39:14 +0200 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | static int syscon_reboot_request(struct udevice *dev, enum sysreset_t type) |
| 26 | { |
| 27 | struct syscon_reboot_priv *priv = dev_get_priv(dev); |
Patrick Delaunay | 4869421 | 2019-05-20 10:58:39 +0200 | [diff] [blame] | 28 | ulong driver_data = dev_get_driver_data(dev); |
Álvaro Fernández Rojas | e388969 | 2017-04-25 00:39:14 +0200 | [diff] [blame] | 29 | |
Patrick Delaunay | 4869421 | 2019-05-20 10:58:39 +0200 | [diff] [blame] | 30 | if (type != driver_data) |
Patrick Delaunay | 50fd31f | 2019-04-18 17:16:21 +0200 | [diff] [blame] | 31 | return -EPROTONOSUPPORT; |
| 32 | |
Bin Meng | 9cdfade | 2020-06-22 22:29:43 -0700 | [diff] [blame] | 33 | regmap_update_bits(priv->regmap, priv->offset, priv->mask, priv->value); |
Álvaro Fernández Rojas | e388969 | 2017-04-25 00:39:14 +0200 | [diff] [blame] | 34 | |
| 35 | return -EINPROGRESS; |
| 36 | } |
| 37 | |
| 38 | static struct sysreset_ops syscon_reboot_ops = { |
| 39 | .request = syscon_reboot_request, |
| 40 | }; |
| 41 | |
Samuel Holland | 30ba45d | 2021-11-03 22:55:12 -0500 | [diff] [blame] | 42 | static int syscon_reboot_probe(struct udevice *dev) |
Álvaro Fernández Rojas | e388969 | 2017-04-25 00:39:14 +0200 | [diff] [blame] | 43 | { |
Álvaro Fernández Rojas | e388969 | 2017-04-25 00:39:14 +0200 | [diff] [blame] | 44 | struct syscon_reboot_priv *priv = dev_get_priv(dev); |
Bin Meng | 1ce8182 | 2020-06-22 22:29:42 -0700 | [diff] [blame] | 45 | int err; |
Bin Meng | 9cdfade | 2020-06-22 22:29:43 -0700 | [diff] [blame] | 46 | int mask_err, value_err; |
Álvaro Fernández Rojas | e388969 | 2017-04-25 00:39:14 +0200 | [diff] [blame] | 47 | |
Patrick Delaunay | 662a74a | 2019-03-07 09:57:14 +0100 | [diff] [blame] | 48 | priv->regmap = syscon_regmap_lookup_by_phandle(dev, "regmap"); |
| 49 | if (IS_ERR(priv->regmap)) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 50 | pr_err("unable to find regmap\n"); |
Álvaro Fernández Rojas | e388969 | 2017-04-25 00:39:14 +0200 | [diff] [blame] | 51 | return -ENODEV; |
| 52 | } |
| 53 | |
Bin Meng | 1ce8182 | 2020-06-22 22:29:42 -0700 | [diff] [blame] | 54 | 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 Meng | 9cdfade | 2020-06-22 22:29:43 -0700 | [diff] [blame] | 60 | 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 Meng | 1ce8182 | 2020-06-22 22:29:42 -0700 | [diff] [blame] | 74 | } |
Álvaro Fernández Rojas | e388969 | 2017-04-25 00:39:14 +0200 | [diff] [blame] | 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static const struct udevice_id syscon_reboot_ids[] = { |
Patrick Delaunay | 4869421 | 2019-05-20 10:58:39 +0200 | [diff] [blame] | 80 | { .compatible = "syscon-reboot", .data = SYSRESET_COLD }, |
| 81 | { .compatible = "syscon-poweroff", .data = SYSRESET_POWER_OFF }, |
Álvaro Fernández Rojas | e388969 | 2017-04-25 00:39:14 +0200 | [diff] [blame] | 82 | { /* sentinel */ } |
| 83 | }; |
| 84 | |
| 85 | U_BOOT_DRIVER(syscon_reboot) = { |
| 86 | .name = "syscon_reboot", |
| 87 | .id = UCLASS_SYSRESET, |
| 88 | .of_match = syscon_reboot_ids, |
| 89 | .probe = syscon_reboot_probe, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 90 | .priv_auto = sizeof(struct syscon_reboot_priv), |
Álvaro Fernández Rojas | e388969 | 2017-04-25 00:39:14 +0200 | [diff] [blame] | 91 | .ops = &syscon_reboot_ops, |
| 92 | }; |