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