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