blob: 6e3f03e2484344ccb32073ced94ca55361bfe84f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dinh Nguyen2ac71882018-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 Nguyen2ac71882018-04-04 17:18:20 -050013 */
14
15#include <common.h>
16#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060017#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070018#include <malloc.h>
Simon Goldschmidtef72ba02019-07-15 21:47:55 +020019#include <dm/lists.h>
Dinh Nguyen2ac71882018-04-04 17:18:20 -050020#include <dm/of_access.h>
Simon Glass7b51b572019-08-01 09:46:52 -060021#include <env.h>
Dinh Nguyen2ac71882018-04-04 17:18:20 -050022#include <reset-uclass.h>
Ley Foon Tan9e608212020-01-10 13:48:37 +080023#include <wait_bit.h>
Dinh Nguyen2ac71882018-04-04 17:18:20 -050024#include <linux/bitops.h>
25#include <linux/io.h>
26#include <linux/sizes.h>
27
28#define BANK_INCREMENT 4
29#define NR_BANKS 8
30
31struct socfpga_reset_data {
Simon Goldschmidt1ea97502019-03-01 20:12:30 +010032 void __iomem *modrst_base;
Dinh Nguyen2ac71882018-04-04 17:18:20 -050033};
34
Simon Goldschmidtede6e7b2019-03-01 20:12:32 +010035/*
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 */
48static bool socfpga_reset_keep_enabled(void)
49{
50#if !defined(CONFIG_SPL_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
Dinh Nguyen2ac71882018-04-04 17:18:20 -050065static 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
Simon Goldschmidt1ea97502019-03-01 20:12:30 +010073 setbits_le32(data->modrst_base + (bank * BANK_INCREMENT), BIT(offset));
Dinh Nguyen2ac71882018-04-04 17:18:20 -050074 return 0;
75}
76
77static 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
Simon Goldschmidt1ea97502019-03-01 20:12:30 +010085 clrbits_le32(data->modrst_base + (bank * BANK_INCREMENT), BIT(offset));
Ley Foon Tan9e608212020-01-10 13:48:37 +080086
87 return wait_for_bit_le32(data->modrst_base + (bank * BANK_INCREMENT),
88 BIT(offset),
89 false, 500, false);
Dinh Nguyen2ac71882018-04-04 17:18:20 -050090}
91
Dinh Nguyen2ac71882018-04-04 17:18:20 -050092static const struct reset_ops socfpga_reset_ops = {
Dinh Nguyen2ac71882018-04-04 17:18:20 -050093 .rst_assert = socfpga_reset_assert,
94 .rst_deassert = socfpga_reset_deassert,
95};
96
97static int socfpga_reset_probe(struct udevice *dev)
98{
99 struct socfpga_reset_data *data = dev_get_priv(dev);
Dinh Nguyen2ac71882018-04-04 17:18:20 -0500100 u32 modrst_offset;
Simon Goldschmidt1ea97502019-03-01 20:12:30 +0100101 void __iomem *membase;
Dinh Nguyen2ac71882018-04-04 17:18:20 -0500102
Masahiro Yamada702e57e2020-08-04 14:14:43 +0900103 membase = dev_read_addr_ptr(dev);
Dinh Nguyen2ac71882018-04-04 17:18:20 -0500104
Simon Goldschmidt6cdd0a42019-05-09 22:11:59 +0200105 modrst_offset = dev_read_u32_default(dev, "altr,modrst-offset", 0x10);
Simon Goldschmidt1ea97502019-03-01 20:12:30 +0100106 data->modrst_base = membase + modrst_offset;
Dinh Nguyen2ac71882018-04-04 17:18:20 -0500107
108 return 0;
109}
110
Simon Goldschmidtede6e7b2019-03-01 20:12:32 +0100111static 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 }
119
120 return 0;
121}
122
Simon Goldschmidtef72ba02019-07-15 21:47:55 +0200123static int socfpga_reset_bind(struct udevice *dev)
124{
125 int ret;
126 struct udevice *sys_child;
127
128 /*
129 * The sysreset driver does not have a device node, so bind it here.
130 * Bind it to the node, too, so that it can get its base address.
131 */
132 ret = device_bind_driver_to_node(dev, "socfpga_sysreset", "sysreset",
Simon Glassf10643c2020-12-19 10:40:14 -0700133 dev_ofnode(dev), &sys_child);
Simon Goldschmidtef72ba02019-07-15 21:47:55 +0200134 if (ret)
135 debug("Warning: No sysreset driver: ret=%d\n", ret);
136
137 return 0;
138}
139
Dinh Nguyen2ac71882018-04-04 17:18:20 -0500140static const struct udevice_id socfpga_reset_match[] = {
141 { .compatible = "altr,rst-mgr" },
142 { /* sentinel */ },
143};
144
145U_BOOT_DRIVER(socfpga_reset) = {
146 .name = "socfpga-reset",
147 .id = UCLASS_RESET,
148 .of_match = socfpga_reset_match,
Simon Goldschmidtef72ba02019-07-15 21:47:55 +0200149 .bind = socfpga_reset_bind,
Dinh Nguyen2ac71882018-04-04 17:18:20 -0500150 .probe = socfpga_reset_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700151 .priv_auto = sizeof(struct socfpga_reset_data),
Dinh Nguyen2ac71882018-04-04 17:18:20 -0500152 .ops = &socfpga_reset_ops,
Simon Goldschmidtede6e7b2019-03-01 20:12:32 +0100153 .remove = socfpga_reset_remove,
154 .flags = DM_FLAG_OS_PREPARE,
Dinh Nguyen2ac71882018-04-04 17:18:20 -0500155};