xref: /OK3568_Linux_fs/kernel/drivers/power/reset/xgene-reboot.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * AppliedMicro X-Gene SoC Reboot Driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2013, Applied Micro Circuits Corporation
6*4882a593Smuzhiyun  * Author: Feng Kan <fkan@apm.com>
7*4882a593Smuzhiyun  * Author: Loc Ho <lho@apm.com>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This driver provides system reboot functionality for APM X-Gene SoC.
10*4882a593Smuzhiyun  * For system shutdown, this is board specify. If a board designer
11*4882a593Smuzhiyun  * implements GPIO shutdown, use the gpio-poweroff.c driver.
12*4882a593Smuzhiyun  */
13*4882a593Smuzhiyun #include <linux/delay.h>
14*4882a593Smuzhiyun #include <linux/io.h>
15*4882a593Smuzhiyun #include <linux/notifier.h>
16*4882a593Smuzhiyun #include <linux/of_device.h>
17*4882a593Smuzhiyun #include <linux/of_address.h>
18*4882a593Smuzhiyun #include <linux/platform_device.h>
19*4882a593Smuzhiyun #include <linux/reboot.h>
20*4882a593Smuzhiyun #include <linux/stat.h>
21*4882a593Smuzhiyun #include <linux/slab.h>
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun struct xgene_reboot_context {
24*4882a593Smuzhiyun 	struct device *dev;
25*4882a593Smuzhiyun 	void *csr;
26*4882a593Smuzhiyun 	u32 mask;
27*4882a593Smuzhiyun 	struct notifier_block restart_handler;
28*4882a593Smuzhiyun };
29*4882a593Smuzhiyun 
xgene_restart_handler(struct notifier_block * this,unsigned long mode,void * cmd)30*4882a593Smuzhiyun static int xgene_restart_handler(struct notifier_block *this,
31*4882a593Smuzhiyun 				 unsigned long mode, void *cmd)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun 	struct xgene_reboot_context *ctx =
34*4882a593Smuzhiyun 		container_of(this, struct xgene_reboot_context,
35*4882a593Smuzhiyun 			     restart_handler);
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	/* Issue the reboot */
38*4882a593Smuzhiyun 	writel(ctx->mask, ctx->csr);
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	mdelay(1000);
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	dev_emerg(ctx->dev, "Unable to restart system\n");
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	return NOTIFY_DONE;
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun 
xgene_reboot_probe(struct platform_device * pdev)47*4882a593Smuzhiyun static int xgene_reboot_probe(struct platform_device *pdev)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun 	struct xgene_reboot_context *ctx;
50*4882a593Smuzhiyun 	struct device *dev = &pdev->dev;
51*4882a593Smuzhiyun 	int err;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
54*4882a593Smuzhiyun 	if (!ctx)
55*4882a593Smuzhiyun 		return -ENOMEM;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	ctx->csr = of_iomap(dev->of_node, 0);
58*4882a593Smuzhiyun 	if (!ctx->csr) {
59*4882a593Smuzhiyun 		dev_err(dev, "can not map resource\n");
60*4882a593Smuzhiyun 		return -ENODEV;
61*4882a593Smuzhiyun 	}
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	if (of_property_read_u32(dev->of_node, "mask", &ctx->mask))
64*4882a593Smuzhiyun 		ctx->mask = 0xFFFFFFFF;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	ctx->dev = dev;
67*4882a593Smuzhiyun 	ctx->restart_handler.notifier_call = xgene_restart_handler;
68*4882a593Smuzhiyun 	ctx->restart_handler.priority = 128;
69*4882a593Smuzhiyun 	err = register_restart_handler(&ctx->restart_handler);
70*4882a593Smuzhiyun 	if (err) {
71*4882a593Smuzhiyun 		iounmap(ctx->csr);
72*4882a593Smuzhiyun 		dev_err(dev, "cannot register restart handler (err=%d)\n", err);
73*4882a593Smuzhiyun 	}
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	return err;
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun static const struct of_device_id xgene_reboot_of_match[] = {
79*4882a593Smuzhiyun 	{ .compatible = "apm,xgene-reboot" },
80*4882a593Smuzhiyun 	{}
81*4882a593Smuzhiyun };
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun static struct platform_driver xgene_reboot_driver = {
84*4882a593Smuzhiyun 	.probe = xgene_reboot_probe,
85*4882a593Smuzhiyun 	.driver = {
86*4882a593Smuzhiyun 		.name = "xgene-reboot",
87*4882a593Smuzhiyun 		.of_match_table = xgene_reboot_of_match,
88*4882a593Smuzhiyun 	},
89*4882a593Smuzhiyun };
90*4882a593Smuzhiyun 
xgene_reboot_init(void)91*4882a593Smuzhiyun static int __init xgene_reboot_init(void)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun 	return platform_driver_register(&xgene_reboot_driver);
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun device_initcall(xgene_reboot_init);
96