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>
298f57f231SGuenter Roeck #include <linux/notifier.h>
3067778e0eSLoc Ho #include <linux/of_device.h>
3167778e0eSLoc Ho #include <linux/of_address.h>
3267778e0eSLoc Ho #include <linux/platform_device.h>
338f57f231SGuenter Roeck #include <linux/reboot.h>
3467778e0eSLoc Ho #include <linux/stat.h>
3567778e0eSLoc Ho #include <linux/slab.h>
3667778e0eSLoc Ho 
3767778e0eSLoc Ho struct xgene_reboot_context {
3843160718SGuenter Roeck 	struct device *dev;
3967778e0eSLoc Ho 	void *csr;
4067778e0eSLoc Ho 	u32 mask;
418f57f231SGuenter Roeck 	struct notifier_block restart_handler;
4267778e0eSLoc Ho };
4367778e0eSLoc Ho 
448f57f231SGuenter Roeck static int xgene_restart_handler(struct notifier_block *this,
458f57f231SGuenter Roeck 				 unsigned long mode, void *cmd)
4667778e0eSLoc Ho {
478f57f231SGuenter Roeck 	struct xgene_reboot_context *ctx =
488f57f231SGuenter Roeck 		container_of(this, struct xgene_reboot_context,
498f57f231SGuenter Roeck 			     restart_handler);
5067778e0eSLoc Ho 
5167778e0eSLoc Ho 	/* Issue the reboot */
5267778e0eSLoc Ho 	writel(ctx->mask, ctx->csr);
5367778e0eSLoc Ho 
54745e1976SGuenter Roeck 	mdelay(1000);
5567778e0eSLoc Ho 
5643160718SGuenter Roeck 	dev_emerg(ctx->dev, "Unable to restart system\n");
578f57f231SGuenter Roeck 
588f57f231SGuenter Roeck 	return NOTIFY_DONE;
5967778e0eSLoc Ho }
6067778e0eSLoc Ho 
6167778e0eSLoc Ho static int xgene_reboot_probe(struct platform_device *pdev)
6267778e0eSLoc Ho {
6367778e0eSLoc Ho 	struct xgene_reboot_context *ctx;
6443160718SGuenter Roeck 	struct device *dev = &pdev->dev;
658f57f231SGuenter Roeck 	int err;
6667778e0eSLoc Ho 
6743160718SGuenter Roeck 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
68ef288f9fSGuenter Roeck 	if (!ctx)
69ef288f9fSGuenter Roeck 		return -ENOMEM;
7067778e0eSLoc Ho 
7143160718SGuenter Roeck 	ctx->csr = of_iomap(dev->of_node, 0);
7267778e0eSLoc Ho 	if (!ctx->csr) {
7343160718SGuenter Roeck 		dev_err(dev, "can not map resource\n");
7467778e0eSLoc Ho 		return -ENODEV;
7567778e0eSLoc Ho 	}
7667778e0eSLoc Ho 
7743160718SGuenter Roeck 	if (of_property_read_u32(dev->of_node, "mask", &ctx->mask))
7867778e0eSLoc Ho 		ctx->mask = 0xFFFFFFFF;
7967778e0eSLoc Ho 
8043160718SGuenter Roeck 	ctx->dev = dev;
818f57f231SGuenter Roeck 	ctx->restart_handler.notifier_call = xgene_restart_handler;
828f57f231SGuenter Roeck 	ctx->restart_handler.priority = 128;
838f57f231SGuenter Roeck 	err = register_restart_handler(&ctx->restart_handler);
84896af83eSArvind Yadav 	if (err) {
85896af83eSArvind Yadav 		iounmap(ctx->csr);
868f57f231SGuenter Roeck 		dev_err(dev, "cannot register restart handler (err=%d)\n", err);
87896af83eSArvind Yadav 	}
8867778e0eSLoc Ho 
898f57f231SGuenter Roeck 	return err;
9067778e0eSLoc Ho }
9167778e0eSLoc Ho 
928fb08855SFabian Frederick static const struct of_device_id xgene_reboot_of_match[] = {
9367778e0eSLoc Ho 	{ .compatible = "apm,xgene-reboot" },
9467778e0eSLoc Ho 	{}
9567778e0eSLoc Ho };
9667778e0eSLoc Ho 
9767778e0eSLoc Ho static struct platform_driver xgene_reboot_driver = {
9867778e0eSLoc Ho 	.probe = xgene_reboot_probe,
9967778e0eSLoc Ho 	.driver = {
10067778e0eSLoc Ho 		.name = "xgene-reboot",
10167778e0eSLoc Ho 		.of_match_table = xgene_reboot_of_match,
10267778e0eSLoc Ho 	},
10367778e0eSLoc Ho };
10467778e0eSLoc Ho 
10567778e0eSLoc Ho static int __init xgene_reboot_init(void)
10667778e0eSLoc Ho {
10767778e0eSLoc Ho 	return platform_driver_register(&xgene_reboot_driver);
10867778e0eSLoc Ho }
10967778e0eSLoc Ho device_initcall(xgene_reboot_init);
110