167778e0eSLoc Ho /*
267778e0eSLoc Ho  * AppliedMicro X-Gene SoC Reboot Driver
367778e0eSLoc Ho  *
467778e0eSLoc Ho  * Copyright (c) 2013, Applied Micro Circuits Corporation
567778e0eSLoc Ho  * Author: Feng Kan <fkan@apm.com>
667778e0eSLoc Ho  * Author: Loc Ho <lho@apm.com>
767778e0eSLoc Ho  *
867778e0eSLoc Ho  * This program is free software; you can redistribute it and/or
967778e0eSLoc Ho  * modify it under the terms of the GNU General Public License as
1067778e0eSLoc Ho  * published by the Free Software Foundation; either version 2 of
1167778e0eSLoc Ho  * the License, or (at your option) any later version.
1267778e0eSLoc Ho  *
1367778e0eSLoc Ho  * This program is distributed in the hope that it will be useful,
1467778e0eSLoc Ho  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1567778e0eSLoc Ho  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1667778e0eSLoc Ho  * GNU General Public License for more details.
1767778e0eSLoc Ho  *
1867778e0eSLoc Ho  * You should have received a copy of the GNU General Public License
1967778e0eSLoc Ho  * along with this program; if not, write to the Free Software
2067778e0eSLoc Ho  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
2167778e0eSLoc Ho  * MA 02111-1307 USA
2267778e0eSLoc Ho  *
2367778e0eSLoc Ho  * This driver provides system reboot functionality for APM X-Gene SoC.
2467778e0eSLoc Ho  * For system shutdown, this is board specify. If a board designer
2567778e0eSLoc Ho  * implements GPIO shutdown, use the gpio-poweroff.c driver.
2667778e0eSLoc Ho  */
2767778e0eSLoc Ho #include <linux/io.h>
2867778e0eSLoc Ho #include <linux/of_device.h>
2967778e0eSLoc Ho #include <linux/of_address.h>
3067778e0eSLoc Ho #include <linux/platform_device.h>
3167778e0eSLoc Ho #include <linux/stat.h>
3267778e0eSLoc Ho #include <linux/slab.h>
3367778e0eSLoc Ho #include <asm/system_misc.h>
3467778e0eSLoc Ho 
3567778e0eSLoc Ho struct xgene_reboot_context {
3667778e0eSLoc Ho 	struct platform_device *pdev;
3767778e0eSLoc Ho 	void *csr;
3867778e0eSLoc Ho 	u32 mask;
3967778e0eSLoc Ho };
4067778e0eSLoc Ho 
4167778e0eSLoc Ho static struct xgene_reboot_context *xgene_restart_ctx;
4267778e0eSLoc Ho 
4367778e0eSLoc Ho static void xgene_restart(char str, const char *cmd)
4467778e0eSLoc Ho {
4567778e0eSLoc Ho 	struct xgene_reboot_context *ctx = xgene_restart_ctx;
4667778e0eSLoc Ho 	unsigned long timeout;
4767778e0eSLoc Ho 
4867778e0eSLoc Ho 	/* Issue the reboot */
4967778e0eSLoc Ho 	if (ctx)
5067778e0eSLoc Ho 		writel(ctx->mask, ctx->csr);
5167778e0eSLoc Ho 
5267778e0eSLoc Ho 	timeout = jiffies + HZ;
5367778e0eSLoc Ho 	while (time_before(jiffies, timeout))
5467778e0eSLoc Ho 		cpu_relax();
5567778e0eSLoc Ho 
5667778e0eSLoc Ho 	dev_emerg(&ctx->pdev->dev, "Unable to restart system\n");
5767778e0eSLoc Ho }
5867778e0eSLoc Ho 
5967778e0eSLoc Ho static int xgene_reboot_probe(struct platform_device *pdev)
6067778e0eSLoc Ho {
6167778e0eSLoc Ho 	struct xgene_reboot_context *ctx;
6267778e0eSLoc Ho 
6367778e0eSLoc Ho 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
6467778e0eSLoc Ho 	if (!ctx) {
6567778e0eSLoc Ho 		dev_err(&pdev->dev, "out of memory for context\n");
6667778e0eSLoc Ho 		return -ENODEV;
6767778e0eSLoc Ho 	}
6867778e0eSLoc Ho 
6967778e0eSLoc Ho 	ctx->csr = of_iomap(pdev->dev.of_node, 0);
7067778e0eSLoc Ho 	if (!ctx->csr) {
7167778e0eSLoc Ho 		devm_kfree(&pdev->dev, ctx);
7267778e0eSLoc Ho 		dev_err(&pdev->dev, "can not map resource\n");
7367778e0eSLoc Ho 		return -ENODEV;
7467778e0eSLoc Ho 	}
7567778e0eSLoc Ho 
7667778e0eSLoc Ho 	if (of_property_read_u32(pdev->dev.of_node, "mask", &ctx->mask))
7767778e0eSLoc Ho 		ctx->mask = 0xFFFFFFFF;
7867778e0eSLoc Ho 
7967778e0eSLoc Ho 	ctx->pdev = pdev;
8067778e0eSLoc Ho 	arm_pm_restart = xgene_restart;
8167778e0eSLoc Ho 	xgene_restart_ctx = ctx;
8267778e0eSLoc Ho 
8367778e0eSLoc Ho 	return 0;
8467778e0eSLoc Ho }
8567778e0eSLoc Ho 
8667778e0eSLoc Ho static struct of_device_id xgene_reboot_of_match[] = {
8767778e0eSLoc Ho 	{ .compatible = "apm,xgene-reboot" },
8867778e0eSLoc Ho 	{}
8967778e0eSLoc Ho };
9067778e0eSLoc Ho 
9167778e0eSLoc Ho static struct platform_driver xgene_reboot_driver = {
9267778e0eSLoc Ho 	.probe = xgene_reboot_probe,
9367778e0eSLoc Ho 	.driver = {
9467778e0eSLoc Ho 		.name = "xgene-reboot",
9567778e0eSLoc Ho 		.of_match_table = xgene_reboot_of_match,
9667778e0eSLoc Ho 	},
9767778e0eSLoc Ho };
9867778e0eSLoc Ho 
9967778e0eSLoc Ho static int __init xgene_reboot_init(void)
10067778e0eSLoc Ho {
10167778e0eSLoc Ho 	return platform_driver_register(&xgene_reboot_driver);
10267778e0eSLoc Ho }
10367778e0eSLoc Ho device_initcall(xgene_reboot_init);
104