blob: 7fc060799af742fb2f2fb1a5552e33e25361e715 [file] [log] [blame]
developer28d70382019-12-19 15:58:20 +08001/*
2 * Copyright (c) 2020, MediaTek Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <arch_helpers.h>
8#include <common/debug.h>
9#include <drivers/arm/gic_common.h>
10#include <drivers/console.h>
11#include <lib/mmio.h>
12
13#include <mt_gic_v3.h>
14#include <mtk_plat_common.h>
15#include <plat_mt_cirq.h>
16#include <platform_def.h>
17
18static struct cirq_events cirq_all_events = {
19 .spi_start = CIRQ_SPI_START
20};
21
22static inline void mt_cirq_write32(uint32_t val, uint32_t addr)
23{
24 mmio_write_32(addr + SYS_CIRQ_BASE, val);
25}
26
27static inline uint32_t mt_cirq_read32(uint32_t addr)
28{
29 return mmio_read_32(addr + SYS_CIRQ_BASE);
30}
31
32/*
33 * cirq_clone_flush_check_store:
34 * set 1 if we need to enable clone/flush value's check
35 */
36static int32_t cirq_clone_flush_check_val;
37
38/*
39 * cirq_pattern_clone_flush_check_show: set 1 if we need to do pattern test.
40 */
41static int32_t cirq_pattern_clone_flush_check_val;
42
43/*
44 * cirq_pattern_clone_flush_check_show: set 1 if we need to do pattern test.
45 */
46static int32_t cirq_pattern_list;
47
48/*
49 * mt_cirq_ack_all: Ack all the interrupt on SYS_CIRQ
50 */
51void mt_cirq_ack_all(void)
52{
53 unsigned int i;
54
55 for (i = 0U; i < CIRQ_CTRL_REG_NUM; i++) {
56 mt_cirq_write32(0xFFFFFFFF, CIRQ_ACK_BASE + (i * 4U));
57 }
58 /* make sure all cirq setting take effect before doing other things */
59 dmbsy();
60}
61
62/*
63 * mt_cirq_enable: Enable SYS_CIRQ
64 */
65void mt_cirq_enable(void)
66{
67 uint32_t st;
68
69 mt_cirq_ack_all();
70
71 st = mt_cirq_read32(CIRQ_CON);
72 st |= (CIRQ_CON_EN << CIRQ_CON_EN_BITS) |
73 (CIRQ_CON_EDGE_ONLY << CIRQ_CON_EDGE_ONLY_BITS);
74
75 mt_cirq_write32((st & CIRQ_CON_BITS_MASK), CIRQ_CON);
76}
77
78/*
79 * mt_cirq_disable: Disable SYS_CIRQ
80 */
81void mt_cirq_disable(void)
82{
83 uint32_t st;
84
85 st = mt_cirq_read32(CIRQ_CON);
86 st &= ~(CIRQ_CON_EN << CIRQ_CON_EN_BITS);
87
88 mt_cirq_write32((st & CIRQ_CON_BITS_MASK), CIRQ_CON);
89}
90
91/*
92 * mt_cirq_get_mask: Get the specified SYS_CIRQ mask
93 * @cirq_num: the SYS_CIRQ number to get
94 * @return:
95 * 1: this cirq is masked
96 * 0: this cirq is umasked
97 * 2: cirq num is out of range
98 */
99__attribute__((weak)) unsigned int mt_cirq_get_mask(uint32_t cirq_num)
100{
101 uint32_t st;
102 unsigned int val;
103
104 if (cirq_num >= CIRQ_IRQ_NUM) {
105 ERROR("[CIRQ] %s: invalid cirq %u\n", __func__, cirq_num);
106 return 2;
107 }
108
109 st = mt_cirq_read32((cirq_num / 32U) * 4U + CIRQ_MASK_BASE);
110 val = (st >> (cirq_num % 32U)) & 1U;
111 return val;
112}
113
114/*
115 * mt_cirq_mask_all: Mask all interrupts on SYS_CIRQ.
116 */
117void mt_cirq_mask_all(void)
118{
119 unsigned int i;
120
121 for (i = 0U; i < CIRQ_CTRL_REG_NUM; i++) {
122 mt_cirq_write32(0xFFFFFFFF, CIRQ_MASK_SET_BASE + (i * 4U));
123 }
124 /* make sure all cirq setting take effect before doing other things */
125 dmbsy();
126}
127
128/*
129 * mt_cirq_unmask_all: Unmask all interrupts on SYS_CIRQ.
130 */
131void mt_cirq_unmask_all(void)
132{
133 unsigned int i;
134
135 for (i = 0U; i < CIRQ_CTRL_REG_NUM; i++) {
136 mt_cirq_write32(0xFFFFFFFF, CIRQ_MASK_CLR_BASE + (i * 4U));
137 }
138 /* make sure all cirq setting take effect before doing other things */
139 dmbsy();
140}
141
142/*
143 * mt_cirq_mask: Mask the specified SYS_CIRQ.
144 * @cirq_num: the SYS_CIRQ number to mask
145 * @return:
146 * 0: mask success
147 * -1: cirq num is out of range
148 */
149static int mt_cirq_mask(uint32_t cirq_num)
150{
151 uint32_t bit = 1U << (cirq_num % 32U);
152
153 if (cirq_num >= CIRQ_IRQ_NUM) {
154 ERROR("[CIRQ] %s: invalid cirq %u\n", __func__, cirq_num);
155 return -1;
156 }
157
158 mt_cirq_write32(bit, (cirq_num / 32U) * 4U + CIRQ_MASK_SET_BASE);
159 return 0;
160}
161
162/*
163 * mt_cirq_unmask: Unmask the specified SYS_CIRQ.
164 * @cirq_num: the SYS_CIRQ number to unmask
165 * @return:
166 * 0: umask success
167 * -1: cirq num is out of range
168 */
169static int mt_cirq_unmask(uint32_t cirq_num)
170{
171 uint32_t bit = 1U << (cirq_num % 32U);
172
173 if (cirq_num >= CIRQ_IRQ_NUM) {
174 ERROR("[CIRQ] %s: invalid cirq %u\n", __func__, cirq_num);
175 return -1;
176 }
177
178 mt_cirq_write32(bit, (cirq_num / 32U) * 4U + CIRQ_MASK_CLR_BASE);
179 return 0;
180}
181
182/*
183 * mt_cirq_set_sens: Set the sensitivity for the specified SYS_CIRQ number.
184 * @cirq_num: the SYS_CIRQ number to set
185 * @sens: sensitivity to set
186 * @return:
187 * 0: set sens success
188 * -1: cirq num is out of range
189 */
190static int mt_cirq_set_sens(uint32_t cirq_num, uint32_t sens)
191{
192 uint32_t base;
193 uint32_t bit = 1U << (cirq_num % 32U);
194
195 if (cirq_num >= CIRQ_IRQ_NUM) {
196 ERROR("[CIRQ] %s: invalid cirq %u\n", __func__, cirq_num);
197 return -1;
198 }
199
200 if (sens == MT_CIRQ_EDGE_SENSITIVE) {
201 base = (cirq_num / 32U) * 4U + CIRQ_SENS_CLR_BASE;
202 } else if (sens == MT_CIRQ_LEVEL_SENSITIVE) {
203 base = (cirq_num / 32U) * 4U + CIRQ_SENS_SET_BASE;
204 } else {
205 ERROR("[CIRQ] set_sens invalid sen value %u\n", sens);
206 return -1;
207 }
208
209 mt_cirq_write32(bit, base);
210 return 0;
211}
212
213/*
214 * mt_cirq_get_sens: Get the specified SYS_CIRQ sensitivity
215 * @cirq_num: the SYS_CIRQ number to get
216 * @return:
217 * 1: this cirq is MT_LEVEL_SENSITIVE
218 * 0: this cirq is MT_EDGE_SENSITIVE
219 * 2: cirq num is out of range
220 */
221__attribute__((weak)) unsigned int mt_cirq_get_sens(uint32_t cirq_num)
222{
223 uint32_t st;
224 unsigned int val;
225
226 if (cirq_num >= CIRQ_IRQ_NUM) {
227 ERROR("[CIRQ] %s: invalid cirq %u\n", __func__, cirq_num);
228 return 2;
229 }
230
231 st = mt_cirq_read32((cirq_num / 32U) * 4U + CIRQ_SENS_BASE);
232 val = (st >> (cirq_num % 32U)) & 1U;
233 return val;
234}
235
236/*
237 * mt_cirq_set_pol: Set the polarity for the specified SYS_CIRQ number.
238 * @cirq_num: the SYS_CIRQ number to set
239 * @pol: polarity to set
240 * @return:
241 * 0: set pol success
242 * -1: cirq num is out of range
243 */
244static int mt_cirq_set_pol(uint32_t cirq_num, uint32_t pol)
245{
246 uint32_t base;
247 uint32_t bit = 1U << (cirq_num % 32U);
248
249 if (cirq_num >= CIRQ_IRQ_NUM) {
250 ERROR("[CIRQ] %s: invalid cirq %u\n", __func__, cirq_num);
251 return -1;
252 }
253
254 if (pol == MT_CIRQ_POL_NEG) {
255 base = (cirq_num / 32U) * 4U + CIRQ_POL_CLR_BASE;
256 } else if (pol == MT_CIRQ_POL_POS) {
257 base = (cirq_num / 32U) * 4U + CIRQ_POL_SET_BASE;
258 } else {
259 ERROR("[CIRQ] set_pol invalid polarity value %u\n", pol);
260 return -1;
261 }
262
263 mt_cirq_write32(bit, base);
264 return 0;
265}
266
267/*
268 * mt_cirq_get_pol: Get the specified SYS_CIRQ polarity
269 * @cirq_num: the SYS_CIRQ number to get
270 * @return:
271 * 1: this cirq is MT_CIRQ_POL_POS
272 * 0: this cirq is MT_CIRQ_POL_NEG
273 * 2: cirq num is out of range
274 */
275__attribute__((weak)) unsigned int mt_cirq_get_pol(uint32_t cirq_num)
276{
277 uint32_t st;
278 unsigned int val;
279
280 if (cirq_num >= CIRQ_IRQ_NUM) {
281 ERROR("[CIRQ] %s: invalid cirq %u\n", __func__, cirq_num);
282 return 2;
283 }
284
285 st = mt_cirq_read32((cirq_num / 32U) * 4U + CIRQ_POL_BASE);
286 val = (st >> (cirq_num % 32U)) & 1U;
287 return val;
288}
289
290/*
291 * mt_cirq_get_pending: Get the specified SYS_CIRQ pending
292 * @cirq_num: the SYS_CIRQ number to get
293 * @return:
294 * 1: this cirq is pending
295 * 0: this cirq is not pending
296 * 2: cirq num is out of range
297 */
298static unsigned int mt_cirq_get_pending(uint32_t cirq_num)
299{
300 uint32_t st;
301 unsigned int val;
302
303 if (cirq_num >= CIRQ_IRQ_NUM) {
304 ERROR("[CIRQ] %s: invalid cirq %u\n", __func__, cirq_num);
305 return 2;
306 }
307
308 st = mt_cirq_read32((cirq_num / 32U) * 4U + CIRQ_STA_BASE);
309 val = (st >> (cirq_num % 32U)) & 1U;
310 return val;
311}
312
313/*
314 * mt_cirq_clone_pol: Copy the polarity setting from GIC to SYS_CIRQ
315 */
316void mt_cirq_clone_pol(void)
317{
318 uint32_t cirq_num;
319
320 for (cirq_num = 0U; cirq_num < CIRQ_IRQ_NUM; cirq_num++) {
321 mt_cirq_set_pol(cirq_num, MT_CIRQ_POL_POS);
322 }
323}
324
325/*
326 * mt_cirq_clone_sens: Copy the sensitivity setting from GIC to SYS_CIRQ
327 */
328void mt_cirq_clone_sens(void)
329{
330 uint32_t cirq_num, irq_num;
331 uint32_t st, val;
332
333 for (cirq_num = 0U; cirq_num < CIRQ_IRQ_NUM; cirq_num++) {
334 irq_num = CIRQ_TO_IRQ_NUM(cirq_num);
335
336 if ((cirq_num == 0U) || (irq_num % 16U == 0U)) {
337 st = mmio_read_32(BASE_GICD_BASE + GICD_ICFGR +
338 (irq_num / 16U * 4U));
339 }
340
341 val = (st >> ((irq_num % 16U) * 2U)) & 0x2U;
342
343 if (val) {
344 mt_cirq_set_sens(cirq_num, MT_CIRQ_EDGE_SENSITIVE);
345 } else {
346 mt_cirq_set_sens(cirq_num, MT_CIRQ_LEVEL_SENSITIVE);
347 }
348 }
349}
350
351/*
352 * mt_cirq_clone_mask: Copy the mask setting from GIC to SYS_CIRQ
353 */
354void mt_cirq_clone_mask(void)
355{
356 uint32_t cirq_num, irq_num;
357 uint32_t st, val;
358
359 for (cirq_num = 0U; cirq_num < CIRQ_IRQ_NUM; cirq_num++) {
360 irq_num = CIRQ_TO_IRQ_NUM(cirq_num);
361
362 if ((cirq_num == 0U) || (irq_num % 32U == 0U)) {
363 st = mmio_read_32(BASE_GICD_BASE +
364 GICD_ISENABLER + (irq_num / 32U * 4U));
365 }
366
367 val = (st >> (irq_num % 32)) & 1U;
368
369 if (val) {
370 mt_cirq_unmask(cirq_num);
371 } else {
372 mt_cirq_mask(cirq_num);
373 }
374 }
375}
376
377/*
378 * mt_cirq_clone_gic: Copy the setting from GIC to SYS_CIRQ
379 */
380void mt_cirq_clone_gic(void)
381{
382 mt_cirq_clone_sens();
383 mt_cirq_clone_mask();
384}
385
386/*
387 * mt_cirq_disable: Flush interrupt from SYS_CIRQ to GIC
388 */
389void mt_cirq_flush(void)
390{
391 unsigned int i;
392 unsigned char cirq_p_val = 0U;
393 unsigned char irq_p_val = 0U;
394 uint32_t irq_p = 0U;
395 unsigned char pass = 1U;
396 uint32_t first_cirq_found = 0U;
397 uint32_t first_flushed_cirq;
398 uint32_t first_irq_flushedto;
399 uint32_t last_fluashed_cirq;
400 uint32_t last_irq_flushedto;
401
402 if (cirq_pattern_clone_flush_check_val == 1U) {
403 if (cirq_pattern_list < CIRQ_IRQ_NUM) {
404 mt_cirq_unmask(cirq_pattern_list);
405 mt_cirq_set_sens(cirq_pattern_list,
406 MT_CIRQ_EDGE_SENSITIVE);
407 mt_cirq_set_pol(cirq_pattern_list, MT_CIRQ_POL_NEG);
408 mt_cirq_set_pol(cirq_pattern_list, MT_CIRQ_POL_POS);
409 mt_cirq_set_pol(cirq_pattern_list, MT_CIRQ_POL_NEG);
410 } else {
411 ERROR("[CIRQ] no pattern to test,");
412 ERROR("input pattern first\n");
413 }
414 ERROR("[CIRQ] cirq_pattern %u, cirq_p %u,",
415 cirq_pattern_list,
416 mt_cirq_get_pending(cirq_pattern_list));
417 ERROR("cirq_s %u, cirq_con 0x%x\n",
418 mt_cirq_get_sens(cirq_pattern_list),
419 mt_cirq_read32(CIRQ_CON));
420 }
421
422 mt_cirq_unmask_all();
423
424 for (i = 0U; i < CIRQ_IRQ_NUM; i++) {
425 cirq_p_val = mt_cirq_get_pending(i);
426 if (cirq_p_val) {
427 mt_irq_set_pending(CIRQ_TO_IRQ_NUM(i));
428 }
429
430 if (cirq_clone_flush_check_val == 1U) {
431 if (cirq_p_val == 0U) {
432 continue;
433 }
434 irq_p = CIRQ_TO_IRQ_NUM(i);
435 irq_p_val = mt_irq_get_pending(irq_p);
436 if (cirq_p_val != irq_p_val) {
437 ERROR("[CIRQ] CIRQ Flush Failed ");
438 ERROR("%u(cirq %d)!= %u(gic %d)\n",
439 cirq_p_val, i, irq_p_val,
440 CIRQ_TO_IRQ_NUM(i));
441 pass = 0;
442 } else {
443 ERROR("[CIRQ] CIRQ Flush Pass ");
444 ERROR("%u(cirq %d) = %u(gic %d)\n",
445 cirq_p_val, i, irq_p_val,
446 CIRQ_TO_IRQ_NUM(i));
447 }
448 if (!first_cirq_found) {
449 first_flushed_cirq = i;
450 first_irq_flushedto = irq_p;
451 first_cirq_found = 1U;
452 }
453 last_fluashed_cirq = i;
454 last_irq_flushedto = irq_p;
455 }
456 }
457
458 if (cirq_clone_flush_check_val == 1U) {
459 if (first_cirq_found) {
460 ERROR("[CIRQ] The first flush : CIRQ%u to IRQ%u\n",
461 first_flushed_cirq, first_irq_flushedto);
462 ERROR("[CIRQ] The last flush : CIRQ%u to IRQ%u\n",
463 last_fluashed_cirq, last_irq_flushedto);
464 } else {
465 ERROR("[CIRQ] There are no pending ");
466 ERROR("interrupt in CIRQ\n");
467 ERROR("[CIRQ] so no flush operation happened\n");
468 }
469 ERROR("[CIRQ] The Flush Max Range : CIRQ");
470 ERROR("%d to IRQ%d ~ CIRQ%d to IRQ%d\n", 0U,
471 CIRQ_TO_IRQ_NUM(0U), CIRQ_IRQ_NUM - 1U,
472 CIRQ_TO_IRQ_NUM(CIRQ_IRQ_NUM - 1U));
473 ERROR("[CIRQ] Flush Check %s, Confirm:SPI_START_OFFSET:%d\n",
474 pass == 1 ? "Pass" : "Failed", CIRQ_SPI_START);
475 }
476 mt_cirq_mask_all();
477 mt_cirq_ack_all();
478}
479
480void mt_cirq_sw_reset(void)
481{
482 uint32_t st;
483
484 st = mt_cirq_read32(CIRQ_CON);
485 st |= (CIRQ_SW_RESET << CIRQ_CON_SW_RST_BITS);
486
487 mt_cirq_write32(st, CIRQ_CON);
488}
489
490void set_wakeup_sources(uint32_t *list, uint32_t num_of_events)
491{
492 cirq_all_events.num_of_events = num_of_events;
493 cirq_all_events.wakeup_events = list;
494}