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