Feng Kan | 09fb07b | 2014-09-30 16:25:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Generic Syscon Reboot Driver |
| 3 | * |
| 4 | * Copyright (c) 2013, Applied Micro Circuits Corporation |
| 5 | * Author: Feng Kan <fkan@apm.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
Feng Kan | afaebbd | 2014-10-02 11:24:15 -0700 | [diff] [blame] | 17 | #include <linux/delay.h> |
Feng Kan | 09fb07b | 2014-09-30 16:25:03 -0700 | [diff] [blame] | 18 | #include <linux/io.h> |
Feng Kan | 09fb07b | 2014-09-30 16:25:03 -0700 | [diff] [blame] | 19 | #include <linux/notifier.h> |
| 20 | #include <linux/mfd/syscon.h> |
Feng Kan | afaebbd | 2014-10-02 11:24:15 -0700 | [diff] [blame] | 21 | #include <linux/of_address.h> |
| 22 | #include <linux/of_device.h> |
| 23 | #include <linux/platform_device.h> |
Feng Kan | 09fb07b | 2014-09-30 16:25:03 -0700 | [diff] [blame] | 24 | #include <linux/reboot.h> |
Feng Kan | afaebbd | 2014-10-02 11:24:15 -0700 | [diff] [blame] | 25 | #include <linux/regmap.h> |
Feng Kan | 09fb07b | 2014-09-30 16:25:03 -0700 | [diff] [blame] | 26 | |
| 27 | struct syscon_reboot_context { |
| 28 | struct regmap *map; |
| 29 | u32 offset; |
Martin Schiller | 077d995 | 2019-03-08 08:49:30 +0100 | [diff] [blame] | 30 | u32 value; |
Feng Kan | 09fb07b | 2014-09-30 16:25:03 -0700 | [diff] [blame] | 31 | u32 mask; |
| 32 | struct notifier_block restart_handler; |
| 33 | }; |
| 34 | |
Feng Kan | 09fb07b | 2014-09-30 16:25:03 -0700 | [diff] [blame] | 35 | static int syscon_restart_handle(struct notifier_block *this, |
| 36 | unsigned long mode, void *cmd) |
| 37 | { |
Feng Kan | afaebbd | 2014-10-02 11:24:15 -0700 | [diff] [blame] | 38 | struct syscon_reboot_context *ctx = |
| 39 | container_of(this, struct syscon_reboot_context, |
| 40 | restart_handler); |
Feng Kan | 09fb07b | 2014-09-30 16:25:03 -0700 | [diff] [blame] | 41 | |
| 42 | /* Issue the reboot */ |
Martin Schiller | 077d995 | 2019-03-08 08:49:30 +0100 | [diff] [blame] | 43 | regmap_update_bits(ctx->map, ctx->offset, ctx->mask, ctx->value); |
Feng Kan | 09fb07b | 2014-09-30 16:25:03 -0700 | [diff] [blame] | 44 | |
Feng Kan | afaebbd | 2014-10-02 11:24:15 -0700 | [diff] [blame] | 45 | mdelay(1000); |
Feng Kan | 09fb07b | 2014-09-30 16:25:03 -0700 | [diff] [blame] | 46 | |
| 47 | pr_emerg("Unable to restart system\n"); |
| 48 | return NOTIFY_DONE; |
| 49 | } |
| 50 | |
| 51 | static int syscon_reboot_probe(struct platform_device *pdev) |
| 52 | { |
| 53 | struct syscon_reboot_context *ctx; |
| 54 | struct device *dev = &pdev->dev; |
Martin Schiller | 077d995 | 2019-03-08 08:49:30 +0100 | [diff] [blame] | 55 | int mask_err, value_err; |
Feng Kan | 09fb07b | 2014-09-30 16:25:03 -0700 | [diff] [blame] | 56 | int err; |
| 57 | |
| 58 | ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL); |
| 59 | if (!ctx) |
| 60 | return -ENOMEM; |
| 61 | |
| 62 | ctx->map = syscon_regmap_lookup_by_phandle(dev->of_node, "regmap"); |
| 63 | if (IS_ERR(ctx->map)) |
| 64 | return PTR_ERR(ctx->map); |
| 65 | |
| 66 | if (of_property_read_u32(pdev->dev.of_node, "offset", &ctx->offset)) |
| 67 | return -EINVAL; |
| 68 | |
Martin Schiller | 077d995 | 2019-03-08 08:49:30 +0100 | [diff] [blame] | 69 | value_err = of_property_read_u32(pdev->dev.of_node, "value", &ctx->value); |
| 70 | mask_err = of_property_read_u32(pdev->dev.of_node, "mask", &ctx->mask); |
| 71 | if (value_err && mask_err) { |
| 72 | dev_err(dev, "unable to read 'value' and 'mask'"); |
Feng Kan | 09fb07b | 2014-09-30 16:25:03 -0700 | [diff] [blame] | 73 | return -EINVAL; |
Martin Schiller | 077d995 | 2019-03-08 08:49:30 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | if (value_err) { |
| 77 | /* support old binding */ |
| 78 | ctx->value = ctx->mask; |
| 79 | ctx->mask = 0xFFFFFFFF; |
| 80 | } else if (mask_err) { |
| 81 | /* support value without mask*/ |
| 82 | ctx->mask = 0xFFFFFFFF; |
| 83 | } |
Feng Kan | 09fb07b | 2014-09-30 16:25:03 -0700 | [diff] [blame] | 84 | |
| 85 | ctx->restart_handler.notifier_call = syscon_restart_handle; |
Stefan Agner | b81180b | 2014-12-02 18:11:58 +0100 | [diff] [blame] | 86 | ctx->restart_handler.priority = 192; |
Feng Kan | 09fb07b | 2014-09-30 16:25:03 -0700 | [diff] [blame] | 87 | err = register_restart_handler(&ctx->restart_handler); |
| 88 | if (err) |
| 89 | dev_err(dev, "can't register restart notifier (err=%d)\n", err); |
| 90 | |
Feng Kan | afaebbd | 2014-10-02 11:24:15 -0700 | [diff] [blame] | 91 | return err; |
Feng Kan | 09fb07b | 2014-09-30 16:25:03 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Fabian Frederick | 8fb0885 | 2015-03-16 20:17:12 +0100 | [diff] [blame] | 94 | static const struct of_device_id syscon_reboot_of_match[] = { |
Feng Kan | 09fb07b | 2014-09-30 16:25:03 -0700 | [diff] [blame] | 95 | { .compatible = "syscon-reboot" }, |
| 96 | {} |
| 97 | }; |
| 98 | |
| 99 | static struct platform_driver syscon_reboot_driver = { |
| 100 | .probe = syscon_reboot_probe, |
| 101 | .driver = { |
| 102 | .name = "syscon-reboot", |
| 103 | .of_match_table = syscon_reboot_of_match, |
| 104 | }, |
| 105 | }; |
Paul Gortmaker | e35415e | 2015-05-01 20:10:58 -0400 | [diff] [blame] | 106 | builtin_platform_driver(syscon_reboot_driver); |