xref: /openbmc/linux/drivers/soc/renesas/rcar-rst.c (revision ba61bb17)
1 /*
2  * R-Car Gen1 RESET/WDT, R-Car Gen2, Gen3, and RZ/G RST Driver
3  *
4  * Copyright (C) 2016 Glider bvba
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License.  See the file "COPYING" in the main directory of this archive
8  * for more details.
9  */
10 
11 #include <linux/err.h>
12 #include <linux/io.h>
13 #include <linux/of_address.h>
14 #include <linux/soc/renesas/rcar-rst.h>
15 
16 #define WDTRSTCR_RESET		0xA55A0002
17 #define WDTRSTCR		0x0054
18 
19 static int rcar_rst_enable_wdt_reset(void __iomem *base)
20 {
21 	iowrite32(WDTRSTCR_RESET, base + WDTRSTCR);
22 	return 0;
23 }
24 
25 struct rst_config {
26 	unsigned int modemr;		/* Mode Monitoring Register Offset */
27 	int (*configure)(void *base);	/* Platform specific configuration */
28 };
29 
30 static const struct rst_config rcar_rst_gen1 __initconst = {
31 	.modemr = 0x20,
32 };
33 
34 static const struct rst_config rcar_rst_gen2 __initconst = {
35 	.modemr = 0x60,
36 	.configure = rcar_rst_enable_wdt_reset,
37 };
38 
39 static const struct rst_config rcar_rst_gen3 __initconst = {
40 	.modemr = 0x60,
41 };
42 
43 static const struct of_device_id rcar_rst_matches[] __initconst = {
44 	/* RZ/G is handled like R-Car Gen2 */
45 	{ .compatible = "renesas,r8a7743-rst", .data = &rcar_rst_gen2 },
46 	{ .compatible = "renesas,r8a7745-rst", .data = &rcar_rst_gen2 },
47 	{ .compatible = "renesas,r8a77470-rst", .data = &rcar_rst_gen2 },
48 	/* R-Car Gen1 */
49 	{ .compatible = "renesas,r8a7778-reset-wdt", .data = &rcar_rst_gen1 },
50 	{ .compatible = "renesas,r8a7779-reset-wdt", .data = &rcar_rst_gen1 },
51 	/* R-Car Gen2 */
52 	{ .compatible = "renesas,r8a7790-rst", .data = &rcar_rst_gen2 },
53 	{ .compatible = "renesas,r8a7791-rst", .data = &rcar_rst_gen2 },
54 	{ .compatible = "renesas,r8a7792-rst", .data = &rcar_rst_gen2 },
55 	{ .compatible = "renesas,r8a7793-rst", .data = &rcar_rst_gen2 },
56 	{ .compatible = "renesas,r8a7794-rst", .data = &rcar_rst_gen2 },
57 	/* R-Car Gen3 */
58 	{ .compatible = "renesas,r8a7795-rst", .data = &rcar_rst_gen3 },
59 	{ .compatible = "renesas,r8a7796-rst", .data = &rcar_rst_gen3 },
60 	{ .compatible = "renesas,r8a77965-rst", .data = &rcar_rst_gen3 },
61 	{ .compatible = "renesas,r8a77970-rst", .data = &rcar_rst_gen3 },
62 	{ .compatible = "renesas,r8a77980-rst", .data = &rcar_rst_gen3 },
63 	{ .compatible = "renesas,r8a77990-rst", .data = &rcar_rst_gen3 },
64 	{ .compatible = "renesas,r8a77995-rst", .data = &rcar_rst_gen3 },
65 	{ /* sentinel */ }
66 };
67 
68 static void __iomem *rcar_rst_base __initdata;
69 static u32 saved_mode __initdata;
70 
71 static int __init rcar_rst_init(void)
72 {
73 	const struct of_device_id *match;
74 	const struct rst_config *cfg;
75 	struct device_node *np;
76 	void __iomem *base;
77 	int error = 0;
78 
79 	np = of_find_matching_node_and_match(NULL, rcar_rst_matches, &match);
80 	if (!np)
81 		return -ENODEV;
82 
83 	base = of_iomap(np, 0);
84 	if (!base) {
85 		pr_warn("%pOF: Cannot map regs\n", np);
86 		error = -ENOMEM;
87 		goto out_put;
88 	}
89 
90 	rcar_rst_base = base;
91 	cfg = match->data;
92 	saved_mode = ioread32(base + cfg->modemr);
93 	if (cfg->configure) {
94 		error = cfg->configure(base);
95 		if (error) {
96 			pr_warn("%pOF: Cannot run SoC specific configuration\n",
97 				np);
98 			goto out_put;
99 		}
100 	}
101 
102 	pr_debug("%pOF: MODE = 0x%08x\n", np, saved_mode);
103 
104 out_put:
105 	of_node_put(np);
106 	return error;
107 }
108 
109 int __init rcar_rst_read_mode_pins(u32 *mode)
110 {
111 	int error;
112 
113 	if (!rcar_rst_base) {
114 		error = rcar_rst_init();
115 		if (error)
116 			return error;
117 	}
118 
119 	*mode = saved_mode;
120 	return 0;
121 }
122