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  */
27745e1976SGuenter Roeck #include <linux/delay.h>
2867778e0eSLoc Ho #include <linux/io.h>
2967778e0eSLoc Ho #include <linux/of_device.h>
3067778e0eSLoc Ho #include <linux/of_address.h>
3167778e0eSLoc Ho #include <linux/platform_device.h>
3267778e0eSLoc Ho #include <linux/stat.h>
3367778e0eSLoc Ho #include <linux/slab.h>
3467778e0eSLoc Ho #include <asm/system_misc.h>
3567778e0eSLoc Ho 
3667778e0eSLoc Ho struct xgene_reboot_context {
3743160718SGuenter Roeck 	struct device *dev;
3867778e0eSLoc Ho 	void *csr;
3967778e0eSLoc Ho 	u32 mask;
4067778e0eSLoc Ho };
4167778e0eSLoc Ho 
4267778e0eSLoc Ho static struct xgene_reboot_context *xgene_restart_ctx;
4367778e0eSLoc Ho 
44d3ed534cSMark Brown static void xgene_restart(enum reboot_mode mode, const char *cmd)
4567778e0eSLoc Ho {
4667778e0eSLoc Ho 	struct xgene_reboot_context *ctx = xgene_restart_ctx;
4767778e0eSLoc Ho 
4867778e0eSLoc Ho 	/* Issue the reboot */
4967778e0eSLoc Ho 	if (ctx)
5067778e0eSLoc Ho 		writel(ctx->mask, ctx->csr);
5167778e0eSLoc Ho 
52745e1976SGuenter Roeck 	mdelay(1000);
5367778e0eSLoc Ho 
5443160718SGuenter Roeck 	dev_emerg(ctx->dev, "Unable to restart system\n");
5567778e0eSLoc Ho }
5667778e0eSLoc Ho 
5767778e0eSLoc Ho static int xgene_reboot_probe(struct platform_device *pdev)
5867778e0eSLoc Ho {
5967778e0eSLoc Ho 	struct xgene_reboot_context *ctx;
6043160718SGuenter Roeck 	struct device *dev = &pdev->dev;
6167778e0eSLoc Ho 
6243160718SGuenter Roeck 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
63ef288f9fSGuenter Roeck 	if (!ctx)
64ef288f9fSGuenter Roeck 		return -ENOMEM;
6567778e0eSLoc Ho 
6643160718SGuenter Roeck 	ctx->csr = of_iomap(dev->of_node, 0);
6767778e0eSLoc Ho 	if (!ctx->csr) {
6843160718SGuenter Roeck 		dev_err(dev, "can not map resource\n");
6967778e0eSLoc Ho 		return -ENODEV;
7067778e0eSLoc Ho 	}
7167778e0eSLoc Ho 
7243160718SGuenter Roeck 	if (of_property_read_u32(dev->of_node, "mask", &ctx->mask))
7367778e0eSLoc Ho 		ctx->mask = 0xFFFFFFFF;
7467778e0eSLoc Ho 
7543160718SGuenter Roeck 	ctx->dev = dev;
7667778e0eSLoc Ho 	arm_pm_restart = xgene_restart;
7767778e0eSLoc Ho 	xgene_restart_ctx = ctx;
7867778e0eSLoc Ho 
7967778e0eSLoc Ho 	return 0;
8067778e0eSLoc Ho }
8167778e0eSLoc Ho 
8267778e0eSLoc Ho static struct of_device_id xgene_reboot_of_match[] = {
8367778e0eSLoc Ho 	{ .compatible = "apm,xgene-reboot" },
8467778e0eSLoc Ho 	{}
8567778e0eSLoc Ho };
8667778e0eSLoc Ho 
8767778e0eSLoc Ho static struct platform_driver xgene_reboot_driver = {
8867778e0eSLoc Ho 	.probe = xgene_reboot_probe,
8967778e0eSLoc Ho 	.driver = {
9067778e0eSLoc Ho 		.name = "xgene-reboot",
9167778e0eSLoc Ho 		.of_match_table = xgene_reboot_of_match,
9267778e0eSLoc Ho 	},
9367778e0eSLoc Ho };
9467778e0eSLoc Ho 
9567778e0eSLoc Ho static int __init xgene_reboot_init(void)
9667778e0eSLoc Ho {
9767778e0eSLoc Ho 	return platform_driver_register(&xgene_reboot_driver);
9867778e0eSLoc Ho }
9967778e0eSLoc Ho device_initcall(xgene_reboot_init);
100