1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Socfpga Reset Controller Driver
4  *
5  * Copyright 2014 Steffen Trumtrar <s.trumtrar@pengutronix.de>
6  *
7  * based on
8  * Allwinner SoCs Reset Controller driver
9  *
10  * Copyright 2013 Maxime Ripard
11  *
12  * Maxime Ripard <maxime.ripard@free-electrons.com>
13  */
14 
15 #include <dm.h>
16 #include <log.h>
17 #include <malloc.h>
18 #include <dm/lists.h>
19 #include <dm/of_access.h>
20 #include <env.h>
21 #include <reset-uclass.h>
22 #include <wait_bit.h>
23 #include <linux/bitops.h>
24 #include <linux/io.h>
25 #include <linux/sizes.h>
26 #include <linux/kconfig.h>
27 
28 #define BANK_INCREMENT		4
29 #define NR_BANKS		8
30 
31 struct socfpga_reset_data {
32 	void __iomem *modrst_base;
33 };
34 
35 /*
36  * For compatibility with Kernels that don't support peripheral reset, this
37  * driver can keep the old behaviour of not asserting peripheral reset before
38  * starting the OS and deasserting all peripheral resets (enabling all
39  * peripherals).
40  *
41  * For that, the reset driver checks the environment variable
42  * "socfpga_legacy_reset_compat". If this variable is '1', perihperals are not
43  * reset again once taken out of reset and all peripherals in 'permodrst' are
44  * taken out of reset before booting into the OS.
45  * Note that this should be required for gen5 systems only that are running
46  * Linux kernels without proper peripheral reset support for all drivers used.
47  */
socfpga_reset_keep_enabled(void)48 static bool socfpga_reset_keep_enabled(void)
49 {
50 #if !defined(CONFIG_XPL_BUILD) || CONFIG_IS_ENABLED(ENV_SUPPORT)
51 	const char *env_str;
52 	long val;
53 
54 	env_str = env_get("socfpga_legacy_reset_compat");
55 	if (env_str) {
56 		val = simple_strtol(env_str, NULL, 0);
57 		if (val == 1)
58 			return true;
59 	}
60 #endif
61 
62 	return false;
63 }
64 
socfpga_reset_assert(struct reset_ctl * reset_ctl)65 static int socfpga_reset_assert(struct reset_ctl *reset_ctl)
66 {
67 	struct socfpga_reset_data *data = dev_get_priv(reset_ctl->dev);
68 	int id = reset_ctl->id;
69 	int reg_width = sizeof(u32);
70 	int bank = id / (reg_width * BITS_PER_BYTE);
71 	int offset = id % (reg_width * BITS_PER_BYTE);
72 
73 	setbits_le32(data->modrst_base + (bank * BANK_INCREMENT), BIT(offset));
74 	return 0;
75 }
76 
socfpga_reset_deassert(struct reset_ctl * reset_ctl)77 static int socfpga_reset_deassert(struct reset_ctl *reset_ctl)
78 {
79 	struct socfpga_reset_data *data = dev_get_priv(reset_ctl->dev);
80 	int id = reset_ctl->id;
81 	int reg_width = sizeof(u32);
82 	int bank = id / (reg_width * BITS_PER_BYTE);
83 	int offset = id % (reg_width * BITS_PER_BYTE);
84 
85 	clrbits_le32(data->modrst_base + (bank * BANK_INCREMENT), BIT(offset));
86 
87 	return wait_for_bit_le32(data->modrst_base + (bank * BANK_INCREMENT),
88 				 BIT(offset),
89 				 false, 500, false);
90 }
91 
92 static const struct reset_ops socfpga_reset_ops = {
93 	.rst_assert = socfpga_reset_assert,
94 	.rst_deassert = socfpga_reset_deassert,
95 };
96 
socfpga_reset_probe(struct udevice * dev)97 static int socfpga_reset_probe(struct udevice *dev)
98 {
99 	struct socfpga_reset_data *data = dev_get_priv(dev);
100 	u32 modrst_offset;
101 	void __iomem *membase;
102 
103 	membase = dev_read_addr_ptr(dev);
104 
105 	modrst_offset = dev_read_u32_default(dev, "altr,modrst-offset", 0x10);
106 	data->modrst_base = membase + modrst_offset;
107 
108 	return 0;
109 }
110 
socfpga_reset_remove(struct udevice * dev)111 static int socfpga_reset_remove(struct udevice *dev)
112 {
113 	struct socfpga_reset_data *data = dev_get_priv(dev);
114 
115 	if (socfpga_reset_keep_enabled()) {
116 		puts("Deasserting all peripheral resets\n");
117 		writel(0, data->modrst_base + 4);
118 		if (IS_ENABLED(CONFIG_TARGET_SOCFPGA_ARRIA10))
119 			writel(0, data->modrst_base + 8);
120 	}
121 
122 	return 0;
123 }
124 
socfpga_reset_bind(struct udevice * dev)125 static int socfpga_reset_bind(struct udevice *dev)
126 {
127 	int ret;
128 	struct udevice *sys_child;
129 
130 	/*
131 	 * The sysreset driver does not have a device node, so bind it here.
132 	 * Bind it to the node, too, so that it can get its base address.
133 	 */
134 	ret = device_bind_driver_to_node(dev, "socfpga_sysreset", "sysreset",
135 					 dev_ofnode(dev), &sys_child);
136 	if (ret)
137 		debug("Warning: No sysreset driver: ret=%d\n", ret);
138 
139 	return 0;
140 }
141 
142 static const struct udevice_id socfpga_reset_match[] = {
143 	{ .compatible = "altr,rst-mgr" },
144 	{ /* sentinel */ },
145 };
146 
147 U_BOOT_DRIVER(socfpga_reset) = {
148 	.name = "socfpga-reset",
149 	.id = UCLASS_RESET,
150 	.of_match = socfpga_reset_match,
151 	.bind = socfpga_reset_bind,
152 	.probe = socfpga_reset_probe,
153 	.priv_auto	= sizeof(struct socfpga_reset_data),
154 	.ops = &socfpga_reset_ops,
155 	.remove = socfpga_reset_remove,
156 	.flags	= DM_FLAG_OS_PREPARE,
157 };
158