blob: ab67bc02de87e4c8a3ecd1afc207843354c5f508 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Patrice Chotard584861f2017-03-22 10:54:03 +01002/*
Patrice Chotardfb48bc42017-10-23 09:53:57 +02003 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
4 * Author(s): Patrice Chotard, <patrice.chotard@st.com> for STMicroelectronics.
Patrice Chotard584861f2017-03-22 10:54:03 +01005 */
6
7#include <common.h>
8#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070010#include <malloc.h>
Patrice Chotard584861f2017-03-22 10:54:03 +010011#include <wait_bit.h>
12#include <dm.h>
13#include <reset-uclass.h>
14#include <regmap.h>
15#include <syscon.h>
16#include <dt-bindings/reset/stih407-resets.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
20struct sti_reset {
21 const struct syscfg_reset_controller_data *data;
22};
23
24/**
25 * Reset channel description for a system configuration register based
26 * reset controller.
27 *
28 * @compatible: Compatible string of the syscon containing this
29 * channel's control and ack (status) bits.
30 * @reset_offset: Reset register offset in sysconf bank.
31 * @reset_bit: Bit number in reset register.
32 * @ack_offset: Ack reset register offset in syscon bank.
33 * @ack_bit: Bit number in Ack reset register.
Patrice Chotardaef5b732017-05-18 09:58:00 +020034 * @deassert_cnt: incremented when reset is deasserted, reset can only be
35 * asserted when equal to 0
Patrice Chotard584861f2017-03-22 10:54:03 +010036 */
37
38struct syscfg_reset_channel_data {
39 const char *compatible;
40 int reset_offset;
41 int reset_bit;
42 int ack_offset;
43 int ack_bit;
Patrice Chotardaef5b732017-05-18 09:58:00 +020044 int deassert_cnt;
Patrice Chotard584861f2017-03-22 10:54:03 +010045};
46
47/**
48 * Description of a system configuration register based reset controller.
49 *
50 * @wait_for_ack: The controller will wait for reset assert and de-assert to
51 * be "ack'd" in a channel's ack field.
52 * @active_low: Are the resets in this controller active low, i.e. clearing
53 * the reset bit puts the hardware into reset.
54 * @nr_channels: The number of reset channels in this controller.
55 * @channels: An array of reset channel descriptions.
56 */
57struct syscfg_reset_controller_data {
58 bool wait_for_ack;
59 bool active_low;
60 int nr_channels;
Patrice Chotardaef5b732017-05-18 09:58:00 +020061 struct syscfg_reset_channel_data *channels;
Patrice Chotard584861f2017-03-22 10:54:03 +010062};
63
64/* STiH407 Peripheral powerdown definitions. */
65static const char stih407_core[] = "st,stih407-core-syscfg";
66static const char stih407_sbc_reg[] = "st,stih407-sbc-reg-syscfg";
67static const char stih407_lpm[] = "st,stih407-lpm-syscfg";
68
69#define _SYSCFG_RST_CH(_c, _rr, _rb, _ar, _ab) \
70 { .compatible = _c, \
71 .reset_offset = _rr, \
72 .reset_bit = _rb, \
73 .ack_offset = _ar, \
74 .ack_bit = _ab, }
75
76#define _SYSCFG_RST_CH_NO_ACK(_c, _rr, _rb) \
77 { .compatible = _c, \
78 .reset_offset = _rr, \
79 .reset_bit = _rb, }
80
81#define STIH407_SRST_CORE(_reg, _bit) \
82 _SYSCFG_RST_CH_NO_ACK(stih407_core, _reg, _bit)
83
84#define STIH407_SRST_SBC(_reg, _bit) \
85 _SYSCFG_RST_CH_NO_ACK(stih407_sbc_reg, _reg, _bit)
86
87#define STIH407_SRST_LPM(_reg, _bit) \
88 _SYSCFG_RST_CH_NO_ACK(stih407_lpm, _reg, _bit)
89
90#define STIH407_PDN_0(_bit) \
91 _SYSCFG_RST_CH(stih407_core, SYSCFG_5000, _bit, SYSSTAT_5500, _bit)
92#define STIH407_PDN_1(_bit) \
93 _SYSCFG_RST_CH(stih407_core, SYSCFG_5001, _bit, SYSSTAT_5501, _bit)
94#define STIH407_PDN_ETH(_bit, _stat) \
95 _SYSCFG_RST_CH(stih407_sbc_reg, SYSCFG_4032, _bit, SYSSTAT_4520, _stat)
96
97/* Powerdown requests control 0 */
98#define SYSCFG_5000 0x0
99#define SYSSTAT_5500 0x7d0
100/* Powerdown requests control 1 (High Speed Links) */
101#define SYSCFG_5001 0x4
102#define SYSSTAT_5501 0x7d4
103
104/* Ethernet powerdown/status/reset */
105#define SYSCFG_4032 0x80
106#define SYSSTAT_4520 0x820
107#define SYSCFG_4002 0x8
108
Patrice Chotardaef5b732017-05-18 09:58:00 +0200109static struct syscfg_reset_channel_data stih407_powerdowns[] = {
Patrice Chotard584861f2017-03-22 10:54:03 +0100110 [STIH407_EMISS_POWERDOWN] = STIH407_PDN_0(1),
111 [STIH407_NAND_POWERDOWN] = STIH407_PDN_0(0),
112 [STIH407_USB3_POWERDOWN] = STIH407_PDN_1(6),
113 [STIH407_USB2_PORT1_POWERDOWN] = STIH407_PDN_1(5),
114 [STIH407_USB2_PORT0_POWERDOWN] = STIH407_PDN_1(4),
115 [STIH407_PCIE1_POWERDOWN] = STIH407_PDN_1(3),
116 [STIH407_PCIE0_POWERDOWN] = STIH407_PDN_1(2),
117 [STIH407_SATA1_POWERDOWN] = STIH407_PDN_1(1),
118 [STIH407_SATA0_POWERDOWN] = STIH407_PDN_1(0),
119 [STIH407_ETH1_POWERDOWN] = STIH407_PDN_ETH(0, 2),
120};
121
122/* Reset Generator control 0/1 */
123#define SYSCFG_5128 0x200
124#define SYSCFG_5131 0x20c
125#define SYSCFG_5132 0x210
126
127#define LPM_SYSCFG_1 0x4 /* Softreset IRB & SBC UART */
128
Patrice Chotardaef5b732017-05-18 09:58:00 +0200129static struct syscfg_reset_channel_data stih407_softresets[] = {
Patrice Chotard584861f2017-03-22 10:54:03 +0100130 [STIH407_ETH1_SOFTRESET] = STIH407_SRST_SBC(SYSCFG_4002, 4),
131 [STIH407_MMC1_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5132, 3),
132 [STIH407_USB2_PORT0_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5132, 28),
133 [STIH407_USB2_PORT1_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5132, 29),
134 [STIH407_PICOPHY_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5132, 30),
135 [STIH407_IRB_SOFTRESET] = STIH407_SRST_LPM(LPM_SYSCFG_1, 6),
136 [STIH407_PCIE0_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5132, 6),
137 [STIH407_PCIE1_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5132, 15),
138 [STIH407_SATA0_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5132, 7),
139 [STIH407_SATA1_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5132, 16),
140 [STIH407_MIPHY0_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5132, 4),
141 [STIH407_MIPHY1_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5132, 13),
142 [STIH407_MIPHY2_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5132, 22),
143 [STIH407_SATA0_PWR_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5132, 5),
144 [STIH407_SATA1_PWR_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5132, 14),
145 [STIH407_DELTA_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5131, 3),
146 [STIH407_BLITTER_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5131, 10),
147 [STIH407_HDTVOUT_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5131, 11),
148 [STIH407_HDQVDP_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5131, 12),
149 [STIH407_VDP_AUX_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5131, 14),
150 [STIH407_COMPO_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5131, 15),
151 [STIH407_HDMI_TX_PHY_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5131, 21),
152 [STIH407_JPEG_DEC_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5131, 23),
153 [STIH407_VP8_DEC_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5131, 24),
154 [STIH407_GPU_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5131, 30),
155 [STIH407_HVA_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5132, 0),
156 [STIH407_ERAM_HVA_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5132, 1),
157 [STIH407_LPM_SOFTRESET] = STIH407_SRST_SBC(SYSCFG_4002, 2),
158 [STIH407_KEYSCAN_SOFTRESET] = STIH407_SRST_LPM(LPM_SYSCFG_1, 8),
159 [STIH407_ST231_AUD_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5131, 26),
160 [STIH407_ST231_DMU_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5131, 27),
161 [STIH407_ST231_GP0_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5131, 28),
162 [STIH407_ST231_GP1_SOFTRESET] = STIH407_SRST_CORE(SYSCFG_5128, 2),
163};
164
165/* PicoPHY reset/control */
166#define SYSCFG_5061 0x0f4
167
Patrice Chotardaef5b732017-05-18 09:58:00 +0200168static struct syscfg_reset_channel_data stih407_picophyresets[] = {
Patrice Chotard584861f2017-03-22 10:54:03 +0100169 [STIH407_PICOPHY0_RESET] = STIH407_SRST_CORE(SYSCFG_5061, 5),
170 [STIH407_PICOPHY1_RESET] = STIH407_SRST_CORE(SYSCFG_5061, 6),
171 [STIH407_PICOPHY2_RESET] = STIH407_SRST_CORE(SYSCFG_5061, 7),
172};
173
174static const struct
175syscfg_reset_controller_data stih407_powerdown_controller = {
176 .wait_for_ack = true,
177 .nr_channels = ARRAY_SIZE(stih407_powerdowns),
178 .channels = stih407_powerdowns,
179};
180
181static const struct
182syscfg_reset_controller_data stih407_softreset_controller = {
183 .wait_for_ack = false,
184 .active_low = true,
185 .nr_channels = ARRAY_SIZE(stih407_softresets),
186 .channels = stih407_softresets,
187};
188
189static const struct
190syscfg_reset_controller_data stih407_picophyreset_controller = {
191 .wait_for_ack = false,
192 .nr_channels = ARRAY_SIZE(stih407_picophyresets),
193 .channels = stih407_picophyresets,
194};
195
196phys_addr_t sti_reset_get_regmap(const char *compatible)
197{
198 struct udevice *syscon;
199 struct regmap *regmap;
200 int node, ret;
201
202 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1,
203 compatible);
204 if (node < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900205 pr_err("unable to find %s node\n", compatible);
Patrice Chotard584861f2017-03-22 10:54:03 +0100206 return node;
207 }
208
209 ret = uclass_get_device_by_of_offset(UCLASS_SYSCON, node, &syscon);
210 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900211 pr_err("%s: uclass_get_device_by_of_offset failed: %d\n",
Patrice Chotard584861f2017-03-22 10:54:03 +0100212 __func__, ret);
213 return ret;
214 }
215
216 regmap = syscon_get_regmap(syscon);
217 if (!regmap) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900218 pr_err("unable to get regmap for %s\n", syscon->name);
Patrice Chotard584861f2017-03-22 10:54:03 +0100219 return -ENODEV;
220 }
221
Masahiro Yamada8c1de5e2018-04-19 12:14:01 +0900222 return regmap->ranges[0].start;
Patrice Chotard584861f2017-03-22 10:54:03 +0100223}
224
225static int sti_reset_program_hw(struct reset_ctl *reset_ctl, int assert)
226{
227 struct udevice *dev = reset_ctl->dev;
228 struct syscfg_reset_controller_data *reset_desc =
229 (struct syscfg_reset_controller_data *)(dev->driver_data);
Patrice Chotardaef5b732017-05-18 09:58:00 +0200230 struct syscfg_reset_channel_data *ch;
Patrice Chotard584861f2017-03-22 10:54:03 +0100231 phys_addr_t base;
232 u32 ctrl_val = reset_desc->active_low ? !assert : !!assert;
233 void __iomem *reg;
234
235 /* check if reset id is inside available range */
236 if (reset_ctl->id >= reset_desc->nr_channels)
237 return -EINVAL;
238
239 /* get reset sysconf register base address */
240 base = sti_reset_get_regmap(reset_desc->channels[reset_ctl->id].compatible);
241
Patrice Chotardaef5b732017-05-18 09:58:00 +0200242 ch = &reset_desc->channels[reset_ctl->id];
243
244 /* check the deassert counter to assert reset when it reaches 0 */
245 if (!assert) {
246 ch->deassert_cnt++;
247 if (ch->deassert_cnt > 1)
248 return 0;
249 } else {
250 if (ch->deassert_cnt > 0) {
251 ch->deassert_cnt--;
252 if (ch->deassert_cnt > 0)
253 return 0;
254 } else
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900255 pr_err("Reset balancing error: reset_ctl=%p dev=%p id=%lu\n",
Patrice Chotardaef5b732017-05-18 09:58:00 +0200256 reset_ctl, reset_ctl->dev, reset_ctl->id);
257 }
258
259 reg = (void __iomem *)base + ch->reset_offset;
Patrice Chotard584861f2017-03-22 10:54:03 +0100260
261 if (ctrl_val)
Patrice Chotardaef5b732017-05-18 09:58:00 +0200262 generic_set_bit(ch->reset_bit, reg);
Patrice Chotard584861f2017-03-22 10:54:03 +0100263 else
Patrice Chotardaef5b732017-05-18 09:58:00 +0200264 generic_clear_bit(ch->reset_bit, reg);
Patrice Chotard584861f2017-03-22 10:54:03 +0100265
266 if (!reset_desc->wait_for_ack)
267 return 0;
268
Patrice Chotardaef5b732017-05-18 09:58:00 +0200269 reg = (void __iomem *)base + ch->ack_offset;
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +0100270 if (wait_for_bit_le32(reg, BIT(ch->ack_bit), ctrl_val,
271 1000, false)) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900272 pr_err("Stuck on waiting ack reset_ctl=%p dev=%p id=%lu\n",
Patrice Chotard584861f2017-03-22 10:54:03 +0100273 reset_ctl, reset_ctl->dev, reset_ctl->id);
274
275 return -ETIMEDOUT;
276 }
277
278 return 0;
279}
280
281static int sti_reset_request(struct reset_ctl *reset_ctl)
282{
283 return 0;
284}
285
286static int sti_reset_free(struct reset_ctl *reset_ctl)
287{
288 return 0;
289}
290
291static int sti_reset_assert(struct reset_ctl *reset_ctl)
292{
293 return sti_reset_program_hw(reset_ctl, true);
294}
295
296static int sti_reset_deassert(struct reset_ctl *reset_ctl)
297{
298 return sti_reset_program_hw(reset_ctl, false);
299}
300
301struct reset_ops sti_reset_ops = {
302 .request = sti_reset_request,
Simon Glass94474b22020-02-03 07:35:52 -0700303 .rfree = sti_reset_free,
Patrice Chotard584861f2017-03-22 10:54:03 +0100304 .rst_assert = sti_reset_assert,
305 .rst_deassert = sti_reset_deassert,
306};
307
308static int sti_reset_probe(struct udevice *dev)
309{
310 struct sti_reset *priv = dev_get_priv(dev);
311
312 priv->data = (void *)dev_get_driver_data(dev);
313
314 return 0;
315}
316
317static const struct udevice_id sti_reset_ids[] = {
318 {
319 .compatible = "st,stih407-picophyreset",
320 .data = (ulong)&stih407_picophyreset_controller,
321 },
322 {
323 .compatible = "st,stih407-powerdown",
324 .data = (ulong)&stih407_powerdown_controller,
325 },
326 {
327 .compatible = "st,stih407-softreset",
328 .data = (ulong)&stih407_softreset_controller,
329 },
330 { }
331};
332
333U_BOOT_DRIVER(sti_reset) = {
334 .name = "sti_reset",
335 .id = UCLASS_RESET,
336 .of_match = sti_reset_ids,
337 .probe = sti_reset_probe,
338 .priv_auto_alloc_size = sizeof(struct sti_reset),
339 .ops = &sti_reset_ops,
340};