blob: 62fbba0df9710de0dc4763509e35e899e5e6778f [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Feng Kan09fb07b2014-09-30 16:25:03 -07002/*
3 * Generic Syscon Reboot Driver
4 *
5 * Copyright (c) 2013, Applied Micro Circuits Corporation
6 * Author: Feng Kan <fkan@apm.com>
Feng Kan09fb07b2014-09-30 16:25:03 -07007 */
Feng Kanafaebbd2014-10-02 11:24:15 -07008#include <linux/delay.h>
Feng Kan09fb07b2014-09-30 16:25:03 -07009#include <linux/io.h>
Feng Kan09fb07b2014-09-30 16:25:03 -070010#include <linux/notifier.h>
11#include <linux/mfd/syscon.h>
Feng Kanafaebbd2014-10-02 11:24:15 -070012#include <linux/of_address.h>
13#include <linux/of_device.h>
14#include <linux/platform_device.h>
Feng Kan09fb07b2014-09-30 16:25:03 -070015#include <linux/reboot.h>
Feng Kanafaebbd2014-10-02 11:24:15 -070016#include <linux/regmap.h>
Feng Kan09fb07b2014-09-30 16:25:03 -070017
18struct syscon_reboot_context {
19 struct regmap *map;
20 u32 offset;
Martin Schiller077d9952019-03-08 08:49:30 +010021 u32 value;
Feng Kan09fb07b2014-09-30 16:25:03 -070022 u32 mask;
23 struct notifier_block restart_handler;
24};
25
Feng Kan09fb07b2014-09-30 16:25:03 -070026static int syscon_restart_handle(struct notifier_block *this,
27 unsigned long mode, void *cmd)
28{
Feng Kanafaebbd2014-10-02 11:24:15 -070029 struct syscon_reboot_context *ctx =
30 container_of(this, struct syscon_reboot_context,
31 restart_handler);
Feng Kan09fb07b2014-09-30 16:25:03 -070032
33 /* Issue the reboot */
Martin Schiller077d9952019-03-08 08:49:30 +010034 regmap_update_bits(ctx->map, ctx->offset, ctx->mask, ctx->value);
Feng Kan09fb07b2014-09-30 16:25:03 -070035
Feng Kanafaebbd2014-10-02 11:24:15 -070036 mdelay(1000);
Feng Kan09fb07b2014-09-30 16:25:03 -070037
38 pr_emerg("Unable to restart system\n");
39 return NOTIFY_DONE;
40}
41
42static int syscon_reboot_probe(struct platform_device *pdev)
43{
44 struct syscon_reboot_context *ctx;
45 struct device *dev = &pdev->dev;
Martin Schiller077d9952019-03-08 08:49:30 +010046 int mask_err, value_err;
Feng Kan09fb07b2014-09-30 16:25:03 -070047 int err;
48
49 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
50 if (!ctx)
51 return -ENOMEM;
52
53 ctx->map = syscon_regmap_lookup_by_phandle(dev->of_node, "regmap");
54 if (IS_ERR(ctx->map))
55 return PTR_ERR(ctx->map);
56
57 if (of_property_read_u32(pdev->dev.of_node, "offset", &ctx->offset))
58 return -EINVAL;
59
Martin Schiller077d9952019-03-08 08:49:30 +010060 value_err = of_property_read_u32(pdev->dev.of_node, "value", &ctx->value);
61 mask_err = of_property_read_u32(pdev->dev.of_node, "mask", &ctx->mask);
62 if (value_err && mask_err) {
63 dev_err(dev, "unable to read 'value' and 'mask'");
Feng Kan09fb07b2014-09-30 16:25:03 -070064 return -EINVAL;
Martin Schiller077d9952019-03-08 08:49:30 +010065 }
66
67 if (value_err) {
68 /* support old binding */
69 ctx->value = ctx->mask;
70 ctx->mask = 0xFFFFFFFF;
71 } else if (mask_err) {
72 /* support value without mask*/
73 ctx->mask = 0xFFFFFFFF;
74 }
Feng Kan09fb07b2014-09-30 16:25:03 -070075
76 ctx->restart_handler.notifier_call = syscon_restart_handle;
Stefan Agnerb81180b2014-12-02 18:11:58 +010077 ctx->restart_handler.priority = 192;
Feng Kan09fb07b2014-09-30 16:25:03 -070078 err = register_restart_handler(&ctx->restart_handler);
79 if (err)
80 dev_err(dev, "can't register restart notifier (err=%d)\n", err);
81
Feng Kanafaebbd2014-10-02 11:24:15 -070082 return err;
Feng Kan09fb07b2014-09-30 16:25:03 -070083}
84
Fabian Frederick8fb08852015-03-16 20:17:12 +010085static const struct of_device_id syscon_reboot_of_match[] = {
Feng Kan09fb07b2014-09-30 16:25:03 -070086 { .compatible = "syscon-reboot" },
87 {}
88};
89
90static struct platform_driver syscon_reboot_driver = {
91 .probe = syscon_reboot_probe,
92 .driver = {
93 .name = "syscon-reboot",
94 .of_match_table = syscon_reboot_of_match,
95 },
96};
Paul Gortmakere35415e2015-05-01 20:10:58 -040097builtin_platform_driver(syscon_reboot_driver);