blob: c0930a624b73a4559dfb6cce000dd0ebce00e74a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dinh Nguyen5427f5a2018-04-04 17:18:20 -05002/*
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>
Dinh Nguyen5427f5a2018-04-04 17:18:20 -050013 */
14
15#include <common.h>
16#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <malloc.h>
Simon Goldschmidtfc824662019-07-15 21:47:55 +020018#include <dm/lists.h>
Dinh Nguyen5427f5a2018-04-04 17:18:20 -050019#include <dm/of_access.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060020#include <env.h>
Dinh Nguyen5427f5a2018-04-04 17:18:20 -050021#include <reset-uclass.h>
Ley Foon Tan30f17752020-01-10 13:48:37 +080022#include <wait_bit.h>
Dinh Nguyen5427f5a2018-04-04 17:18:20 -050023#include <linux/bitops.h>
24#include <linux/io.h>
25#include <linux/sizes.h>
26
27#define BANK_INCREMENT 4
28#define NR_BANKS 8
29
30struct socfpga_reset_data {
Simon Goldschmidt53a0cf52019-03-01 20:12:30 +010031 void __iomem *modrst_base;
Dinh Nguyen5427f5a2018-04-04 17:18:20 -050032};
33
Simon Goldschmidte568bb72019-03-01 20:12:32 +010034/*
35 * For compatibility with Kernels that don't support peripheral reset, this
36 * driver can keep the old behaviour of not asserting peripheral reset before
37 * starting the OS and deasserting all peripheral resets (enabling all
38 * peripherals).
39 *
40 * For that, the reset driver checks the environment variable
41 * "socfpga_legacy_reset_compat". If this variable is '1', perihperals are not
42 * reset again once taken out of reset and all peripherals in 'permodrst' are
43 * taken out of reset before booting into the OS.
44 * Note that this should be required for gen5 systems only that are running
45 * Linux kernels without proper peripheral reset support for all drivers used.
46 */
47static bool socfpga_reset_keep_enabled(void)
48{
49#if !defined(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(ENV_SUPPORT)
50 const char *env_str;
51 long val;
52
53 env_str = env_get("socfpga_legacy_reset_compat");
54 if (env_str) {
55 val = simple_strtol(env_str, NULL, 0);
56 if (val == 1)
57 return true;
58 }
59#endif
60
61 return false;
62}
63
Dinh Nguyen5427f5a2018-04-04 17:18:20 -050064static int socfpga_reset_assert(struct reset_ctl *reset_ctl)
65{
66 struct socfpga_reset_data *data = dev_get_priv(reset_ctl->dev);
67 int id = reset_ctl->id;
68 int reg_width = sizeof(u32);
69 int bank = id / (reg_width * BITS_PER_BYTE);
70 int offset = id % (reg_width * BITS_PER_BYTE);
71
Simon Goldschmidt53a0cf52019-03-01 20:12:30 +010072 setbits_le32(data->modrst_base + (bank * BANK_INCREMENT), BIT(offset));
Dinh Nguyen5427f5a2018-04-04 17:18:20 -050073 return 0;
74}
75
76static int socfpga_reset_deassert(struct reset_ctl *reset_ctl)
77{
78 struct socfpga_reset_data *data = dev_get_priv(reset_ctl->dev);
79 int id = reset_ctl->id;
80 int reg_width = sizeof(u32);
81 int bank = id / (reg_width * BITS_PER_BYTE);
82 int offset = id % (reg_width * BITS_PER_BYTE);
83
Simon Goldschmidt53a0cf52019-03-01 20:12:30 +010084 clrbits_le32(data->modrst_base + (bank * BANK_INCREMENT), BIT(offset));
Ley Foon Tan30f17752020-01-10 13:48:37 +080085
86 return wait_for_bit_le32(data->modrst_base + (bank * BANK_INCREMENT),
87 BIT(offset),
88 false, 500, false);
Dinh Nguyen5427f5a2018-04-04 17:18:20 -050089}
90
91static int socfpga_reset_request(struct reset_ctl *reset_ctl)
92{
93 debug("%s(reset_ctl=%p) (dev=%p, id=%lu)\n", __func__,
94 reset_ctl, reset_ctl->dev, reset_ctl->id);
95
96 return 0;
97}
98
99static int socfpga_reset_free(struct reset_ctl *reset_ctl)
100{
101 debug("%s(reset_ctl=%p) (dev=%p, id=%lu)\n", __func__, reset_ctl,
102 reset_ctl->dev, reset_ctl->id);
103
104 return 0;
105}
106
107static const struct reset_ops socfpga_reset_ops = {
108 .request = socfpga_reset_request,
Simon Glass1928cd42020-02-03 07:35:52 -0700109 .rfree = socfpga_reset_free,
Dinh Nguyen5427f5a2018-04-04 17:18:20 -0500110 .rst_assert = socfpga_reset_assert,
111 .rst_deassert = socfpga_reset_deassert,
112};
113
114static int socfpga_reset_probe(struct udevice *dev)
115{
116 struct socfpga_reset_data *data = dev_get_priv(dev);
Dinh Nguyen5427f5a2018-04-04 17:18:20 -0500117 u32 modrst_offset;
Simon Goldschmidt53a0cf52019-03-01 20:12:30 +0100118 void __iomem *membase;
Dinh Nguyen5427f5a2018-04-04 17:18:20 -0500119
Simon Goldschmidt53a0cf52019-03-01 20:12:30 +0100120 membase = devfdt_get_addr_ptr(dev);
Dinh Nguyen5427f5a2018-04-04 17:18:20 -0500121
Simon Goldschmidte5291ad2019-05-09 22:11:59 +0200122 modrst_offset = dev_read_u32_default(dev, "altr,modrst-offset", 0x10);
Simon Goldschmidt53a0cf52019-03-01 20:12:30 +0100123 data->modrst_base = membase + modrst_offset;
Dinh Nguyen5427f5a2018-04-04 17:18:20 -0500124
125 return 0;
126}
127
Simon Goldschmidte568bb72019-03-01 20:12:32 +0100128static int socfpga_reset_remove(struct udevice *dev)
129{
130 struct socfpga_reset_data *data = dev_get_priv(dev);
131
132 if (socfpga_reset_keep_enabled()) {
133 puts("Deasserting all peripheral resets\n");
134 writel(0, data->modrst_base + 4);
135 }
136
137 return 0;
138}
139
Simon Goldschmidtfc824662019-07-15 21:47:55 +0200140static int socfpga_reset_bind(struct udevice *dev)
141{
142 int ret;
143 struct udevice *sys_child;
144
145 /*
146 * The sysreset driver does not have a device node, so bind it here.
147 * Bind it to the node, too, so that it can get its base address.
148 */
149 ret = device_bind_driver_to_node(dev, "socfpga_sysreset", "sysreset",
150 dev->node, &sys_child);
151 if (ret)
152 debug("Warning: No sysreset driver: ret=%d\n", ret);
153
154 return 0;
155}
156
Dinh Nguyen5427f5a2018-04-04 17:18:20 -0500157static const struct udevice_id socfpga_reset_match[] = {
158 { .compatible = "altr,rst-mgr" },
159 { /* sentinel */ },
160};
161
162U_BOOT_DRIVER(socfpga_reset) = {
163 .name = "socfpga-reset",
164 .id = UCLASS_RESET,
165 .of_match = socfpga_reset_match,
Simon Goldschmidtfc824662019-07-15 21:47:55 +0200166 .bind = socfpga_reset_bind,
Dinh Nguyen5427f5a2018-04-04 17:18:20 -0500167 .probe = socfpga_reset_probe,
168 .priv_auto_alloc_size = sizeof(struct socfpga_reset_data),
169 .ops = &socfpga_reset_ops,
Simon Goldschmidte568bb72019-03-01 20:12:32 +0100170 .remove = socfpga_reset_remove,
171 .flags = DM_FLAG_OS_PREPARE,
Dinh Nguyen5427f5a2018-04-04 17:18:20 -0500172};