^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Copyright (C) 2013 Broadcom Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * This program is free software; you can redistribute it and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * modify it under the terms of the GNU General Public License as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * published by the Free Software Foundation version 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * This program is distributed "as is" WITHOUT ANY WARRANTY of any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * kind, whether express or implied; without even the implied warranty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/of_address.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/of_irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/of_platform.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/printk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/mfd/syscon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define RESET_SOURCE_ENABLE_REG 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define SW_MASTER_RESET_REG 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static u32 rst_src_en;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static u32 sw_mstr_rst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct reset_reg_mask {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) u32 rst_src_en_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) u32 sw_mstr_rst_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static const struct reset_reg_mask *reset_masks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static int brcmstb_restart_handler(struct notifier_block *this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned long mode, void *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) u32 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) rc = regmap_write(regmap, rst_src_en, reset_masks->rst_src_en_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) pr_err("failed to write rst_src_en (%d)\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) rc = regmap_read(regmap, rst_src_en, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) pr_err("failed to read rst_src_en (%d)\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return NOTIFY_DONE;
^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) rc = regmap_write(regmap, sw_mstr_rst, reset_masks->sw_mstr_rst_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) pr_err("failed to write sw_mstr_rst (%d)\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) rc = regmap_read(regmap, sw_mstr_rst, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) pr_err("failed to read sw_mstr_rst (%d)\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return NOTIFY_DONE;
^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) while (1)
^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) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static struct notifier_block brcmstb_restart_nb = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .notifier_call = brcmstb_restart_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .priority = 128,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static const struct reset_reg_mask reset_bits_40nm = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) .rst_src_en_mask = BIT(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) .sw_mstr_rst_mask = BIT(0),
^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 const struct reset_reg_mask reset_bits_65nm = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) .rst_src_en_mask = BIT(3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) .sw_mstr_rst_mask = BIT(31),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static const struct of_device_id of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) { .compatible = "brcm,brcmstb-reboot", .data = &reset_bits_40nm },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) { .compatible = "brcm,bcm7038-reboot", .data = &reset_bits_65nm },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static int brcmstb_reboot_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct device_node *np = pdev->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) const struct of_device_id *of_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) of_id = of_match_node(of_match, np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (!of_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) pr_err("failed to look up compatible string\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) reset_masks = of_id->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) regmap = syscon_regmap_lookup_by_phandle(np, "syscon");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (IS_ERR(regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) pr_err("failed to get syscon phandle\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) rc = of_property_read_u32_index(np, "syscon", RESET_SOURCE_ENABLE_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) &rst_src_en);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) pr_err("can't get rst_src_en offset (%d)\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) rc = of_property_read_u32_index(np, "syscon", SW_MASTER_RESET_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) &sw_mstr_rst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) pr_err("can't get sw_mstr_rst offset (%d)\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) rc = register_restart_handler(&brcmstb_restart_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) "cannot register restart handler (err=%d)\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static struct platform_driver brcmstb_reboot_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .probe = brcmstb_reboot_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) .name = "brcmstb-reboot",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) .of_match_table = of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static int __init brcmstb_reboot_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return platform_driver_probe(&brcmstb_reboot_driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) brcmstb_reboot_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) subsys_initcall(brcmstb_reboot_init);