blob: ee4cbcb02ff9ccbcafe6071fa4a3508aad061f5c [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>
17#include <dm/of_access.h>
18#include <reset-uclass.h>
19#include <linux/bitops.h>
20#include <linux/io.h>
21#include <linux/sizes.h>
22
23#define BANK_INCREMENT 4
24#define NR_BANKS 8
25
26struct socfpga_reset_data {
Simon Goldschmidt53a0cf52019-03-01 20:12:30 +010027 void __iomem *modrst_base;
Dinh Nguyen5427f5a2018-04-04 17:18:20 -050028};
29
Simon Goldschmidte568bb72019-03-01 20:12:32 +010030/*
31 * For compatibility with Kernels that don't support peripheral reset, this
32 * driver can keep the old behaviour of not asserting peripheral reset before
33 * starting the OS and deasserting all peripheral resets (enabling all
34 * peripherals).
35 *
36 * For that, the reset driver checks the environment variable
37 * "socfpga_legacy_reset_compat". If this variable is '1', perihperals are not
38 * reset again once taken out of reset and all peripherals in 'permodrst' are
39 * taken out of reset before booting into the OS.
40 * Note that this should be required for gen5 systems only that are running
41 * Linux kernels without proper peripheral reset support for all drivers used.
42 */
43static bool socfpga_reset_keep_enabled(void)
44{
45#if !defined(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(ENV_SUPPORT)
46 const char *env_str;
47 long val;
48
49 env_str = env_get("socfpga_legacy_reset_compat");
50 if (env_str) {
51 val = simple_strtol(env_str, NULL, 0);
52 if (val == 1)
53 return true;
54 }
55#endif
56
57 return false;
58}
59
Dinh Nguyen5427f5a2018-04-04 17:18:20 -050060static int socfpga_reset_assert(struct reset_ctl *reset_ctl)
61{
62 struct socfpga_reset_data *data = dev_get_priv(reset_ctl->dev);
63 int id = reset_ctl->id;
64 int reg_width = sizeof(u32);
65 int bank = id / (reg_width * BITS_PER_BYTE);
66 int offset = id % (reg_width * BITS_PER_BYTE);
67
Simon Goldschmidt53a0cf52019-03-01 20:12:30 +010068 setbits_le32(data->modrst_base + (bank * BANK_INCREMENT), BIT(offset));
Dinh Nguyen5427f5a2018-04-04 17:18:20 -050069 return 0;
70}
71
72static int socfpga_reset_deassert(struct reset_ctl *reset_ctl)
73{
74 struct socfpga_reset_data *data = dev_get_priv(reset_ctl->dev);
75 int id = reset_ctl->id;
76 int reg_width = sizeof(u32);
77 int bank = id / (reg_width * BITS_PER_BYTE);
78 int offset = id % (reg_width * BITS_PER_BYTE);
79
Simon Goldschmidt53a0cf52019-03-01 20:12:30 +010080 clrbits_le32(data->modrst_base + (bank * BANK_INCREMENT), BIT(offset));
Dinh Nguyen5427f5a2018-04-04 17:18:20 -050081 return 0;
82}
83
84static int socfpga_reset_request(struct reset_ctl *reset_ctl)
85{
86 debug("%s(reset_ctl=%p) (dev=%p, id=%lu)\n", __func__,
87 reset_ctl, reset_ctl->dev, reset_ctl->id);
88
89 return 0;
90}
91
92static int socfpga_reset_free(struct reset_ctl *reset_ctl)
93{
94 debug("%s(reset_ctl=%p) (dev=%p, id=%lu)\n", __func__, reset_ctl,
95 reset_ctl->dev, reset_ctl->id);
96
97 return 0;
98}
99
100static const struct reset_ops socfpga_reset_ops = {
101 .request = socfpga_reset_request,
102 .free = socfpga_reset_free,
103 .rst_assert = socfpga_reset_assert,
104 .rst_deassert = socfpga_reset_deassert,
105};
106
107static int socfpga_reset_probe(struct udevice *dev)
108{
109 struct socfpga_reset_data *data = dev_get_priv(dev);
Dinh Nguyen5427f5a2018-04-04 17:18:20 -0500110 u32 modrst_offset;
Simon Goldschmidt53a0cf52019-03-01 20:12:30 +0100111 void __iomem *membase;
Dinh Nguyen5427f5a2018-04-04 17:18:20 -0500112
Simon Goldschmidt53a0cf52019-03-01 20:12:30 +0100113 membase = devfdt_get_addr_ptr(dev);
Dinh Nguyen5427f5a2018-04-04 17:18:20 -0500114
Simon Goldschmidte5291ad2019-05-09 22:11:59 +0200115 modrst_offset = dev_read_u32_default(dev, "altr,modrst-offset", 0x10);
Simon Goldschmidt53a0cf52019-03-01 20:12:30 +0100116 data->modrst_base = membase + modrst_offset;
Dinh Nguyen5427f5a2018-04-04 17:18:20 -0500117
118 return 0;
119}
120
Simon Goldschmidte568bb72019-03-01 20:12:32 +0100121static int socfpga_reset_remove(struct udevice *dev)
122{
123 struct socfpga_reset_data *data = dev_get_priv(dev);
124
125 if (socfpga_reset_keep_enabled()) {
126 puts("Deasserting all peripheral resets\n");
127 writel(0, data->modrst_base + 4);
128 }
129
130 return 0;
131}
132
Dinh Nguyen5427f5a2018-04-04 17:18:20 -0500133static const struct udevice_id socfpga_reset_match[] = {
134 { .compatible = "altr,rst-mgr" },
135 { /* sentinel */ },
136};
137
138U_BOOT_DRIVER(socfpga_reset) = {
139 .name = "socfpga-reset",
140 .id = UCLASS_RESET,
141 .of_match = socfpga_reset_match,
142 .probe = socfpga_reset_probe,
143 .priv_auto_alloc_size = sizeof(struct socfpga_reset_data),
144 .ops = &socfpga_reset_ops,
Simon Goldschmidte568bb72019-03-01 20:12:32 +0100145 .remove = socfpga_reset_remove,
146 .flags = DM_FLAG_OS_PREPARE,
Dinh Nguyen5427f5a2018-04-04 17:18:20 -0500147};