blob: 613e002fc4f666c3312c57f9a28fc72add6461ca [file] [log] [blame]
Huan Zhou10ad7982025-03-11 09:38:49 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2022 Spacemit Inc.
4 * Copyright (C) 2025 Huan Zhou <pericycle.cc@gmail.com>
5 */
6
7#include <asm/io.h>
8#include <config.h>
9#include <dm.h>
10#include <dm/device_compat.h>
11#include <dm/device-internal.h>
12#include <dm/lists.h>
13#include <dt-bindings/reset/spacemit-k1-reset.h>
14#include <linux/bitops.h>
15#include <reset-uclass.h>
16
17/* APBC register offset */
18#define APBC_UART1_CLK_RST 0x0
19#define APBC_UART2_CLK_RST 0x4
20#define APBC_GPIO_CLK_RST 0x8
21#define APBC_PWM0_CLK_RST 0xc
22#define APBC_PWM1_CLK_RST 0x10
23#define APBC_PWM2_CLK_RST 0x14
24#define APBC_PWM3_CLK_RST 0x18
25#define APBC_TWSI8_CLK_RST 0x20
26#define APBC_UART3_CLK_RST 0x24
27#define APBC_RTC_CLK_RST 0x28
28#define APBC_TWSI0_CLK_RST 0x2c
29#define APBC_TWSI1_CLK_RST 0x30
30#define APBC_TIMERS1_CLK_RST 0x34
31#define APBC_TWSI2_CLK_RST 0x38
32#define APBC_AIB_CLK_RST 0x3c
33#define APBC_TWSI4_CLK_RST 0x40
34#define APBC_TIMERS2_CLK_RST 0x44
35#define APBC_ONEWIRE_CLK_RST 0x48
36#define APBC_TWSI5_CLK_RST 0x4c
37#define APBC_DRO_CLK_RST 0x58
38#define APBC_IR_CLK_RST 0x5c
39#define APBC_TWSI6_CLK_RST 0x60
40#define APBC_TWSI7_CLK_RST 0x68
41#define APBC_TSEN_CLK_RST 0x6c
42
43#define APBC_UART4_CLK_RST 0x70
44#define APBC_UART5_CLK_RST 0x74
45#define APBC_UART6_CLK_RST 0x78
46#define APBC_SSP3_CLK_RST 0x7c
47
48#define APBC_SSPA0_CLK_RST 0x80
49#define APBC_SSPA1_CLK_RST 0x84
50
51#define APBC_IPC_AP2AUD_CLK_RST 0x90
52#define APBC_UART7_CLK_RST 0x94
53#define APBC_UART8_CLK_RST 0x98
54#define APBC_UART9_CLK_RST 0x9c
55
56#define APBC_CAN0_CLK_RST 0xa0
57#define APBC_PWM4_CLK_RST 0xa8
58#define APBC_PWM5_CLK_RST 0xac
59#define APBC_PWM6_CLK_RST 0xb0
60#define APBC_PWM7_CLK_RST 0xb4
61#define APBC_PWM8_CLK_RST 0xb8
62#define APBC_PWM9_CLK_RST 0xbc
63#define APBC_PWM10_CLK_RST 0xc0
64#define APBC_PWM11_CLK_RST 0xc4
65#define APBC_PWM12_CLK_RST 0xc8
66#define APBC_PWM13_CLK_RST 0xcc
67#define APBC_PWM14_CLK_RST 0xd0
68#define APBC_PWM15_CLK_RST 0xd4
69#define APBC_PWM16_CLK_RST 0xd8
70#define APBC_PWM17_CLK_RST 0xdc
71#define APBC_PWM18_CLK_RST 0xe0
72#define APBC_PWM19_CLK_RST 0xe4
73/* end of APBC register offset */
74
75/* MPMU register offset */
76#define MPMU_WDTPCR 0x200
77/* end of MPMU register offset */
78
79/* APMU register offset */
80#define APMU_JPG_CLK_RES_CTRL 0x20
81#define APMU_CSI_CCIC2_CLK_RES_CTRL 0x24
82#define APMU_ISP_CLK_RES_CTRL 0x38
83#define APMU_LCD_CLK_RES_CTRL1 0x44
84#define APMU_LCD_SPI_CLK_RES_CTRL 0x48
85#define APMU_LCD_CLK_RES_CTRL2 0x4c
86#define APMU_CCIC_CLK_RES_CTRL 0x50
87#define APMU_SDH0_CLK_RES_CTRL 0x54
88#define APMU_SDH1_CLK_RES_CTRL 0x58
89#define APMU_USB_CLK_RES_CTRL 0x5c
90#define APMU_QSPI_CLK_RES_CTRL 0x60
91#define APMU_USB_CLK_RES_CTRL 0x5c
92#define APMU_DMA_CLK_RES_CTRL 0x64
93#define APMU_AES_CLK_RES_CTRL 0x68
94#define APMU_VPU_CLK_RES_CTRL 0xa4
95#define APMU_GPU_CLK_RES_CTRL 0xcc
96#define APMU_SDH2_CLK_RES_CTRL 0xe0
97#define APMU_PMUA_MC_CTRL 0xe8
98#define APMU_PMU_CC2_AP 0x100
99#define APMU_PMUA_EM_CLK_RES_CTRL 0x104
100
101#define APMU_AUDIO_CLK_RES_CTRL 0x14c
102#define APMU_HDMI_CLK_RES_CTRL 0x1B8
103
104#define APMU_PCIE_CLK_RES_CTRL_0 0x3cc
105#define APMU_PCIE_CLK_RES_CTRL_1 0x3d4
106#define APMU_PCIE_CLK_RES_CTRL_2 0x3dc
107
108#define APMU_EMAC0_CLK_RES_CTRL 0x3e4
109#define APMU_EMAC1_CLK_RES_CTRL 0x3ec
110/* end of APMU register offset */
111
112/* APBC2 register offset */
113#define APBC2_UART1_CLK_RST 0x00
114#define APBC2_SSP2_CLK_RST 0x04
115#define APBC2_TWSI3_CLK_RST 0x08
116#define APBC2_RTC_CLK_RST 0x0c
117#define APBC2_TIMERS0_CLK_RST 0x10
118#define APBC2_KPC_CLK_RST 0x14
119#define APBC2_GPIO_CLK_RST 0x1c
120/* end of APBC2 register offset */
121
122enum spacemit_reset_base_type {
123 RST_BASE_TYPE_MPMU = 0,
124 RST_BASE_TYPE_APMU = 1,
125 RST_BASE_TYPE_APBC = 2,
126 RST_BASE_TYPE_APBS = 3,
127 RST_BASE_TYPE_CIU = 4,
128 RST_BASE_TYPE_DCIU = 5,
129 RST_BASE_TYPE_DDRC = 6,
130 RST_BASE_TYPE_AUDC = 7,
131 RST_BASE_TYPE_APBC2 = 8,
132};
133
134struct spacemit_reset_signal {
135 u32 offset;
136 u32 mask;
137 u32 deassert_val;
138 u32 assert_val;
139 enum spacemit_reset_base_type type;
140};
141
142struct spacemit_reset_base {
143 void __iomem *mpmu_base;
144 void __iomem *apmu_base;
145 void __iomem *apbc_base;
146 void __iomem *apbs_base;
147 void __iomem *ciu_base;
148 void __iomem *dciu_base;
149 void __iomem *ddrc_base;
150 void __iomem *audio_ctrl_base;
151 void __iomem *apbc2_base;
152};
153
154struct spacemit_reset {
155 struct spacemit_reset_base io_base;
156 const struct spacemit_reset_signal *signals;
157};
158
159enum {
160 RESET_TWSI6_SPL = 0,
161 RESET_TWSI8_SPL,
162 RESET_SDH_AXI_SPL,
163 RESET_SDH0_SPL,
164 RESET_USB_AXI_SPL,
165 RESET_USBP1_AXI_SPL,
166 RESET_USB3_0_SPL,
167 RESET_QSPI_SPL,
168 RESET_QSPI_BUS_SPL,
169 RESET_AES_SPL,
170 RESET_SDH2_SPL,
171 RESET_NUMBER_SPL
172};
173
174static const struct spacemit_reset_signal
175 k1_reset_signals[RESET_NUMBER] = {
176 [RESET_UART1] = { APBC_UART1_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
177 [RESET_UART2] = { APBC_UART2_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
178 [RESET_GPIO] = { APBC_GPIO_CLK_RST, BIT(2), 0,
179 BIT(2),
180 RST_BASE_TYPE_APBC },
181 [RESET_PWM0] = { APBC_PWM0_CLK_RST,
182 BIT(2) | BIT(0),
183 BIT(0),
184 BIT(2),
185 RST_BASE_TYPE_APBC },
186 [RESET_PWM1] = { APBC_PWM1_CLK_RST,
187 BIT(2) | BIT(0),
188 BIT(0),
189 BIT(2),
190 RST_BASE_TYPE_APBC },
191 [RESET_PWM2] = { APBC_PWM2_CLK_RST,
192 BIT(2) | BIT(0),
193 BIT(0),
194 BIT(2),
195 RST_BASE_TYPE_APBC },
196 [RESET_PWM3] = { APBC_PWM3_CLK_RST,
197 BIT(2) | BIT(0),
198 BIT(0),
199 BIT(2),
200 RST_BASE_TYPE_APBC },
201 [RESET_PWM4] = { APBC_PWM4_CLK_RST,
202 BIT(2) | BIT(0),
203 BIT(0),
204 BIT(2),
205 RST_BASE_TYPE_APBC },
206 [RESET_PWM5] = { APBC_PWM5_CLK_RST,
207 BIT(2) | BIT(0),
208 BIT(0),
209 BIT(2),
210 RST_BASE_TYPE_APBC },
211 [RESET_PWM6] = { APBC_PWM6_CLK_RST,
212 BIT(2) | BIT(0),
213 BIT(0),
214 BIT(2),
215 RST_BASE_TYPE_APBC },
216 [RESET_PWM7] = { APBC_PWM7_CLK_RST,
217 BIT(2) | BIT(0),
218 BIT(0),
219 BIT(2),
220 RST_BASE_TYPE_APBC },
221 [RESET_PWM8] = { APBC_PWM8_CLK_RST,
222 BIT(2) | BIT(0),
223 BIT(0),
224 BIT(2),
225 RST_BASE_TYPE_APBC },
226 [RESET_PWM9] = { APBC_PWM9_CLK_RST,
227 BIT(2) | BIT(0),
228 BIT(0),
229 BIT(2),
230 RST_BASE_TYPE_APBC },
231 [RESET_PWM10] = { APBC_PWM10_CLK_RST,
232 BIT(2) | BIT(0),
233 BIT(0),
234 BIT(2),
235 RST_BASE_TYPE_APBC },
236 [RESET_PWM11] = { APBC_PWM11_CLK_RST,
237 BIT(2) | BIT(0),
238 BIT(0),
239 BIT(2),
240 RST_BASE_TYPE_APBC },
241 [RESET_PWM12] = { APBC_PWM12_CLK_RST,
242 BIT(2) | BIT(0),
243 BIT(0),
244 BIT(2),
245 RST_BASE_TYPE_APBC },
246 [RESET_PWM13] = { APBC_PWM13_CLK_RST,
247 BIT(2) | BIT(0),
248 BIT(0),
249 BIT(2),
250 RST_BASE_TYPE_APBC },
251 [RESET_PWM14] = { APBC_PWM14_CLK_RST,
252 BIT(2) | BIT(0),
253 BIT(0),
254 BIT(2),
255 RST_BASE_TYPE_APBC },
256 [RESET_PWM15] = { APBC_PWM15_CLK_RST,
257 BIT(2) | BIT(0),
258 BIT(0),
259 BIT(2),
260 RST_BASE_TYPE_APBC },
261 [RESET_PWM16] = { APBC_PWM16_CLK_RST,
262 BIT(2) | BIT(0),
263 BIT(0),
264 BIT(2),
265 RST_BASE_TYPE_APBC },
266 [RESET_PWM17] = { APBC_PWM17_CLK_RST,
267 BIT(2) | BIT(0),
268 BIT(0),
269 BIT(2),
270 RST_BASE_TYPE_APBC },
271 [RESET_PWM18] = { APBC_PWM18_CLK_RST,
272 BIT(2) | BIT(0),
273 BIT(0),
274 BIT(2),
275 RST_BASE_TYPE_APBC },
276 [RESET_PWM19] = { APBC_PWM19_CLK_RST,
277 BIT(2) | BIT(0),
278 BIT(0),
279 BIT(2),
280 RST_BASE_TYPE_APBC },
281 [RESET_SSP3] = { APBC_SSP3_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
282 [RESET_UART3] = { APBC_UART3_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
283 [RESET_RTC] = { APBC_RTC_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
284 [RESET_TWSI0] = { APBC_TWSI0_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
285 [RESET_TIMERS1] = { APBC_TIMERS1_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
286 [RESET_AIB] = { APBC_AIB_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
287 [RESET_TIMERS2] = { APBC_TIMERS2_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
288 [RESET_ONEWIRE] = { APBC_ONEWIRE_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
289 [RESET_SSPA0] = { APBC_SSPA0_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
290 [RESET_SSPA1] = { APBC_SSPA1_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
291 [RESET_DRO] = { APBC_DRO_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
292 [RESET_IR] = { APBC_IR_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
293 [RESET_TWSI1] = { APBC_TWSI1_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
294 [RESET_TSEN] = { APBC_TSEN_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
295 [RESET_TWSI2] = { APBC_TWSI2_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
296 [RESET_TWSI4] = { APBC_TWSI4_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
297 [RESET_TWSI5] = { APBC_TWSI5_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
298 [RESET_TWSI6] = { APBC_TWSI6_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
299 [RESET_TWSI7] = { APBC_TWSI7_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
300 [RESET_TWSI8] = { APBC_TWSI8_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
301 [RESET_IPC_AP2AUD] = { APBC_IPC_AP2AUD_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
302 [RESET_UART4] = { APBC_UART4_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
303 [RESET_UART5] = { APBC_UART5_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
304 [RESET_UART6] = { APBC_UART6_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
305 [RESET_UART7] = { APBC_UART7_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
306 [RESET_UART8] = { APBC_UART8_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
307 [RESET_UART9] = { APBC_UART9_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
308 [RESET_CAN0] = { APBC_CAN0_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC },
309 /* MPMU */
310 [RESET_WDT] = { MPMU_WDTPCR, BIT(2), 0, BIT(2), RST_BASE_TYPE_MPMU },
311 /* APMU */
312 [RESET_JPG] = { APMU_JPG_CLK_RES_CTRL, BIT(0), BIT(0), 0, RST_BASE_TYPE_APMU },
313 [RESET_CSI] = { APMU_CSI_CCIC2_CLK_RES_CTRL, BIT(1), BIT(1), 0, RST_BASE_TYPE_APMU },
314 [RESET_CCIC2_PHY] = { APMU_CSI_CCIC2_CLK_RES_CTRL,
315 BIT(2),
316 BIT(2),
317 0,
318 RST_BASE_TYPE_APMU },
319 [RESET_CCIC3_PHY] = { APMU_CSI_CCIC2_CLK_RES_CTRL,
320 BIT(29),
321 BIT(29),
322 0,
323 RST_BASE_TYPE_APMU },
324 [RESET_ISP] = { APMU_ISP_CLK_RES_CTRL, BIT(0), BIT(0), 0, RST_BASE_TYPE_APMU },
325 [RESET_ISP_AHB] = { APMU_ISP_CLK_RES_CTRL, BIT(3), BIT(3), 0, RST_BASE_TYPE_APMU },
326 [RESET_ISP_CI] = { APMU_ISP_CLK_RES_CTRL, BIT(16), BIT(16), 0, RST_BASE_TYPE_APMU },
327 [RESET_ISP_CPP] = { APMU_ISP_CLK_RES_CTRL, BIT(27), BIT(27), 0, RST_BASE_TYPE_APMU },
328 [RESET_LCD] = { APMU_LCD_CLK_RES_CTRL1, BIT(4), BIT(4), 0, RST_BASE_TYPE_APMU },
329 [RESET_DSI_ESC] = { APMU_LCD_CLK_RES_CTRL1, BIT(3), BIT(3), 0, RST_BASE_TYPE_APMU },
330 [RESET_V2D] = { APMU_LCD_CLK_RES_CTRL1, BIT(27), BIT(27), 0, RST_BASE_TYPE_APMU },
331 [RESET_MIPI] = { APMU_LCD_CLK_RES_CTRL1, BIT(15), BIT(15), 0, RST_BASE_TYPE_APMU },
332 [RESET_LCD_SPI] = { APMU_LCD_SPI_CLK_RES_CTRL, BIT(0), BIT(0), 0, RST_BASE_TYPE_APMU },
333 [RESET_LCD_SPI_BUS] = { APMU_LCD_SPI_CLK_RES_CTRL,
334 BIT(4),
335 BIT(4),
336 0,
337 RST_BASE_TYPE_APMU },
338 [RESET_LCD_SPI_HBUS] = { APMU_LCD_SPI_CLK_RES_CTRL,
339 BIT(2),
340 BIT(2),
341 0,
342 RST_BASE_TYPE_APMU },
343 [RESET_LCD_MCLK] = { APMU_LCD_CLK_RES_CTRL2, BIT(9), BIT(9), 0, RST_BASE_TYPE_APMU },
344 [RESET_CCIC_4X] = { APMU_CCIC_CLK_RES_CTRL, BIT(1), BIT(1), 0, RST_BASE_TYPE_APMU },
345 [RESET_CCIC1_PHY] = { APMU_CCIC_CLK_RES_CTRL, BIT(2), BIT(2), 0, RST_BASE_TYPE_APMU },
346 [RESET_SDH_AXI] = { APMU_SDH0_CLK_RES_CTRL, BIT(0), BIT(0), 0, RST_BASE_TYPE_APMU },
347 [RESET_SDH0] = { APMU_SDH0_CLK_RES_CTRL, BIT(1), BIT(1), 0, RST_BASE_TYPE_APMU },
348 [RESET_SDH1] = { APMU_SDH1_CLK_RES_CTRL, BIT(1), BIT(1), 0, RST_BASE_TYPE_APMU },
349 [RESET_USB_AXI] = { APMU_USB_CLK_RES_CTRL, BIT(0), BIT(0), 0, RST_BASE_TYPE_APMU },
350 [RESET_USBP1_AXI] = { APMU_USB_CLK_RES_CTRL, BIT(4), BIT(4), 0, RST_BASE_TYPE_APMU },
351 [RESET_USB3_0] = { APMU_USB_CLK_RES_CTRL,
352 BIT(9) | BIT(10) | BIT(11),
353 BIT(9) | BIT(10) | BIT(11),
354 0,
355 RST_BASE_TYPE_APMU },
356 [RESET_QSPI] = { APMU_QSPI_CLK_RES_CTRL, BIT(1), BIT(1), 0, RST_BASE_TYPE_APMU },
357 [RESET_QSPI_BUS] = { APMU_QSPI_CLK_RES_CTRL, BIT(0), BIT(0), 0, RST_BASE_TYPE_APMU },
358 [RESET_DMA] = { APMU_DMA_CLK_RES_CTRL, BIT(0), BIT(0), 0, RST_BASE_TYPE_APMU },
359 [RESET_AES] = { APMU_AES_CLK_RES_CTRL, BIT(4), BIT(4), 0, RST_BASE_TYPE_APMU },
360 [RESET_VPU] = { APMU_VPU_CLK_RES_CTRL, BIT(0), BIT(0), 0, RST_BASE_TYPE_APMU },
361 [RESET_GPU] = { APMU_GPU_CLK_RES_CTRL, BIT(1), BIT(1), 0, RST_BASE_TYPE_APMU },
362 [RESET_SDH2] = { APMU_SDH2_CLK_RES_CTRL, BIT(1), BIT(1), 0, RST_BASE_TYPE_APMU },
363 [RESET_MC] = { APMU_PMUA_MC_CTRL, BIT(0), BIT(0), 0, RST_BASE_TYPE_APMU },
364 [RESET_EM_AXI] = { APMU_PMUA_EM_CLK_RES_CTRL, BIT(0), BIT(0), 0, RST_BASE_TYPE_APMU },
365 [RESET_EM] = { APMU_PMUA_EM_CLK_RES_CTRL, BIT(1), BIT(1), 0, RST_BASE_TYPE_APMU },
366 [RESET_AUDIO_SYS] = { APMU_AUDIO_CLK_RES_CTRL,
367 BIT(0) | BIT(2) | BIT(3),
368 BIT(0) | BIT(2) | BIT(3),
369 0,
370 RST_BASE_TYPE_APMU },
371 [RESET_HDMI] = { APMU_HDMI_CLK_RES_CTRL, BIT(9), BIT(9), 0, RST_BASE_TYPE_APMU },
372 [RESET_PCIE0] = { APMU_PCIE_CLK_RES_CTRL_0,
373 BIT(3) | BIT(4) | BIT(5) | BIT(8),
374 BIT(3) | BIT(4) | BIT(5),
375 BIT(8),
376 RST_BASE_TYPE_APMU },
377 [RESET_PCIE1] = { APMU_PCIE_CLK_RES_CTRL_1,
378 BIT(3) | BIT(4) | BIT(5) | BIT(8),
379 BIT(3) | BIT(4) | BIT(5),
380 BIT(8),
381 RST_BASE_TYPE_APMU },
382 [RESET_PCIE2] = { APMU_PCIE_CLK_RES_CTRL_2, 0x138, 0x38, 0x100, RST_BASE_TYPE_APMU },
383 [RESET_EMAC0] = { APMU_EMAC0_CLK_RES_CTRL, BIT(1), BIT(1), 0, RST_BASE_TYPE_APMU },
384 [RESET_EMAC1] = { APMU_EMAC1_CLK_RES_CTRL, BIT(1), BIT(1), 0, RST_BASE_TYPE_APMU },
385 [RESET_SEC_UART1] = { APBC2_UART1_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC2 },
386 [RESET_SEC_SSP2] = { APBC2_SSP2_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC2 },
387 [RESET_SEC_TWSI3] = { APBC2_TWSI3_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC2 },
388 [RESET_SEC_RTC] = { APBC2_RTC_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC2 },
389 [RESET_SEC_TIMERS0] = { APBC2_TIMERS0_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC2 },
390 [RESET_SEC_KPC] = { APBC2_KPC_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC2 },
391 [RESET_SEC_GPIO] = { APBC2_GPIO_CLK_RST, BIT(2), 0, BIT(2), RST_BASE_TYPE_APBC2 },
392 };
393
394static u32 spacemit_reset_read(struct spacemit_reset *reset, u32 id)
395{
396 void __iomem *base;
397
398 switch (reset->signals[id].type) {
399 case RST_BASE_TYPE_APMU:
400 base = reset->io_base.apmu_base;
401 break;
402 case RST_BASE_TYPE_APBC:
403 base = reset->io_base.apbc_base;
404 break;
405 default:
406 base = reset->io_base.apbc_base;
407 break;
408 }
409
410 return readl(base + reset->signals[id].offset);
411}
412
413static void spacemit_reset_write(struct spacemit_reset *reset, u32 value, u32 id)
414{
415 void __iomem *base;
416
417 switch (reset->signals[id].type) {
418 case RST_BASE_TYPE_APMU:
419 base = reset->io_base.apmu_base;
420 break;
421 case RST_BASE_TYPE_APBC:
422 base = reset->io_base.apbc_base;
423 break;
424 default:
425 base = reset->io_base.apbc_base;
426 break;
427 }
428
429 writel(value, base + reset->signals[id].offset);
430}
431
432static void spacemit_reset_set(struct reset_ctl *rst, u32 id, bool assert)
433{
434 u32 value;
435 struct spacemit_reset *reset = dev_get_priv(rst->dev);
436
437 value = spacemit_reset_read(reset, id);
438
439 if (assert) {
440 value &= ~reset->signals[id].mask;
441 value |= reset->signals[id].assert_val;
442 } else {
443 value &= ~reset->signals[id].mask;
444 value |= reset->signals[id].deassert_val;
445 }
446
447 spacemit_reset_write(reset, value, id);
448}
449
450static int spacemit_reset_update(struct reset_ctl *rst, bool assert)
451{
452 if (rst->id < RESET_UART1 || rst->id >= RESET_NUMBER)
453 return 0;
454
455 /* can not write to twsi8 */
456 if (rst->id == RESET_TWSI8)
457 return 0;
458
459 spacemit_reset_set(rst, rst->id, assert);
460 return 0;
461}
462
463static int spacemit_reset_assert(struct reset_ctl *rst)
464{
465 return spacemit_reset_update(rst, true);
466}
467
468static int spacemit_reset_deassert(struct reset_ctl *rst)
469{
470 return spacemit_reset_update(rst, false);
471}
472
473static int spacemit_k1_reset_probe(struct udevice *dev)
474{
475 struct spacemit_reset *reset = dev_get_priv(dev);
476
477 reset->io_base.mpmu_base = (void *)dev_remap_addr_index(dev, 0);
478 if (!reset->io_base.mpmu_base) {
479 pr_err("failed to map mpmu registers\n");
480 goto out;
481 }
482
483 reset->io_base.apmu_base = (void *)dev_remap_addr_index(dev, 1);
484 if (!reset->io_base.apmu_base) {
485 pr_err("failed to map apmu registers\n");
486 goto out;
487 }
488
489 reset->io_base.apbc_base = (void *)dev_remap_addr_index(dev, 2);
490 if (!reset->io_base.apbc_base) {
491 pr_err("failed to map apbc registers\n");
492 goto out;
493 }
494
495 reset->io_base.apbs_base = (void *)dev_remap_addr_index(dev, 3);
496 if (!reset->io_base.apbs_base) {
497 pr_err("failed to map apbs registers\n");
498 goto out;
499 }
500
501 reset->io_base.ciu_base = (void *)dev_remap_addr_index(dev, 4);
502 if (!reset->io_base.ciu_base) {
503 pr_err("failed to map ciu registers\n");
504 goto out;
505 }
506
507 reset->io_base.dciu_base = (void *)dev_remap_addr_index(dev, 5);
508 if (!reset->io_base.dciu_base) {
509 pr_err("failed to map dragon ciu registers\n");
510 goto out;
511 }
512
513 reset->io_base.ddrc_base = (void *)dev_remap_addr_index(dev, 6);
514 if (!reset->io_base.ddrc_base) {
515 pr_err("failed to map ddrc registers\n");
516 goto out;
517 }
518
519 reset->io_base.apbc2_base = (void *)dev_remap_addr_index(dev, 7);
520 if (!reset->io_base.apbc2_base) {
521 pr_err("failed to map apbc2 registers\n");
522 goto out;
523 }
524
525 reset->signals = k1_reset_signals;
526
527out:
528 return 0;
529}
530
531const struct reset_ops k1_reset_ops = {
532 .rst_assert = spacemit_reset_assert,
533 .rst_deassert = spacemit_reset_deassert,
534};
535
536static const struct udevice_id k1_reset_ids[] = {
537 { .compatible = "spacemit,k1-reset", },
538 {},
539};
540
541U_BOOT_DRIVER(k1_reset) = {
542 .name = "spacemit,k1-reset",
543 .id = UCLASS_RESET,
544 .ops = &k1_reset_ops,
545 .of_match = k1_reset_ids,
546 .probe = spacemit_k1_reset_probe,
547 .priv_auto = sizeof(struct spacemit_reset),
548};