1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (C) 2013 Broadcom Corporation
3
4 #include <linux/bitops.h>
5 #include <linux/device.h>
6 #include <linux/errno.h>
7 #include <linux/init.h>
8 #include <linux/io.h>
9 #include <linux/jiffies.h>
10 #include <linux/notifier.h>
11 #include <linux/of_address.h>
12 #include <linux/of_irq.h>
13 #include <linux/of_platform.h>
14 #include <linux/platform_device.h>
15 #include <linux/printk.h>
16 #include <linux/reboot.h>
17 #include <linux/regmap.h>
18 #include <linux/smp.h>
19 #include <linux/mfd/syscon.h>
20
21 #define RESET_SOURCE_ENABLE_REG 1
22 #define SW_MASTER_RESET_REG 2
23
24 static struct regmap *regmap;
25 static u32 rst_src_en;
26 static u32 sw_mstr_rst;
27
28 struct reset_reg_mask {
29 u32 rst_src_en_mask;
30 u32 sw_mstr_rst_mask;
31 };
32
33 static const struct reset_reg_mask *reset_masks;
34
brcmstb_restart_handler(struct notifier_block * this,unsigned long mode,void * cmd)35 static int brcmstb_restart_handler(struct notifier_block *this,
36 unsigned long mode, void *cmd)
37 {
38 int rc;
39 u32 tmp;
40
41 rc = regmap_write(regmap, rst_src_en, reset_masks->rst_src_en_mask);
42 if (rc) {
43 pr_err("failed to write rst_src_en (%d)\n", rc);
44 return NOTIFY_DONE;
45 }
46
47 rc = regmap_read(regmap, rst_src_en, &tmp);
48 if (rc) {
49 pr_err("failed to read rst_src_en (%d)\n", rc);
50 return NOTIFY_DONE;
51 }
52
53 rc = regmap_write(regmap, sw_mstr_rst, reset_masks->sw_mstr_rst_mask);
54 if (rc) {
55 pr_err("failed to write sw_mstr_rst (%d)\n", rc);
56 return NOTIFY_DONE;
57 }
58
59 rc = regmap_read(regmap, sw_mstr_rst, &tmp);
60 if (rc) {
61 pr_err("failed to read sw_mstr_rst (%d)\n", rc);
62 return NOTIFY_DONE;
63 }
64
65 return NOTIFY_DONE;
66 }
67
68 static struct notifier_block brcmstb_restart_nb = {
69 .notifier_call = brcmstb_restart_handler,
70 .priority = 128,
71 };
72
73 static const struct reset_reg_mask reset_bits_40nm = {
74 .rst_src_en_mask = BIT(0),
75 .sw_mstr_rst_mask = BIT(0),
76 };
77
78 static const struct reset_reg_mask reset_bits_65nm = {
79 .rst_src_en_mask = BIT(3),
80 .sw_mstr_rst_mask = BIT(31),
81 };
82
83 static const struct of_device_id of_match[] = {
84 { .compatible = "brcm,brcmstb-reboot", .data = &reset_bits_40nm },
85 { .compatible = "brcm,bcm7038-reboot", .data = &reset_bits_65nm },
86 {},
87 };
88
brcmstb_reboot_probe(struct platform_device * pdev)89 static int brcmstb_reboot_probe(struct platform_device *pdev)
90 {
91 int rc;
92 struct device_node *np = pdev->dev.of_node;
93 const struct of_device_id *of_id;
94
95 of_id = of_match_node(of_match, np);
96 if (!of_id) {
97 pr_err("failed to look up compatible string\n");
98 return -EINVAL;
99 }
100 reset_masks = of_id->data;
101
102 regmap = syscon_regmap_lookup_by_phandle(np, "syscon");
103 if (IS_ERR(regmap)) {
104 pr_err("failed to get syscon phandle\n");
105 return -EINVAL;
106 }
107
108 rc = of_property_read_u32_index(np, "syscon", RESET_SOURCE_ENABLE_REG,
109 &rst_src_en);
110 if (rc) {
111 pr_err("can't get rst_src_en offset (%d)\n", rc);
112 return -EINVAL;
113 }
114
115 rc = of_property_read_u32_index(np, "syscon", SW_MASTER_RESET_REG,
116 &sw_mstr_rst);
117 if (rc) {
118 pr_err("can't get sw_mstr_rst offset (%d)\n", rc);
119 return -EINVAL;
120 }
121
122 rc = register_restart_handler(&brcmstb_restart_nb);
123 if (rc)
124 dev_err(&pdev->dev,
125 "cannot register restart handler (err=%d)\n", rc);
126
127 return rc;
128 }
129
130 static struct platform_driver brcmstb_reboot_driver = {
131 .probe = brcmstb_reboot_probe,
132 .driver = {
133 .name = "brcmstb-reboot",
134 .of_match_table = of_match,
135 },
136 };
137
brcmstb_reboot_init(void)138 static int __init brcmstb_reboot_init(void)
139 {
140 return platform_driver_probe(&brcmstb_reboot_driver,
141 brcmstb_reboot_probe);
142 }
143 subsys_initcall(brcmstb_reboot_init);
144