mtk_cirq.c 12.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
/*
 * Copyright (c) 2020, MediaTek Inc. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <arch_helpers.h>
#include <common/debug.h>
#include <drivers/arm/gic_common.h>
#include <lib/mmio.h>

#include <mt_gic_v3.h>
13
#include <mtk_cirq.h>
14
15

static struct cirq_events cirq_all_events = {
16
	.spi_start = CIRQ_SPI_START,
17
};
18
static uint32_t already_cloned;
19
/*
20
21
22
23
 * mt_irq_mask_restore: restore all interrupts
 * @mask: pointer to struct mtk_irq_mask for storing the original mask value.
 * Return 0 for success; return negative values for failure.
 * (This is ONLY used for the idle current measurement by the factory mode.)
24
 */
25
26
27
28
29
30
31
32
33
34
35
int mt_irq_mask_restore(struct mtk_irq_mask *mask)
{
	if (mask == NULL) {
		return -1;
	}
	if (mask->header != IRQ_MASK_HEADER) {
		return -1;
	}
	if (mask->footer != IRQ_MASK_FOOTER) {
		return -1;
	}
36

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
	 mmio_write_32((BASE_GICD_BASE + GICD_ISENABLER + 0x4),
		mask->mask1);
	 mmio_write_32((BASE_GICD_BASE + GICD_ISENABLER + 0x8),
		mask->mask2);
	 mmio_write_32((BASE_GICD_BASE + GICD_ISENABLER + 0xc),
		mask->mask3);
	 mmio_write_32((BASE_GICD_BASE + GICD_ISENABLER + 0x10),
		mask->mask4);
	 mmio_write_32((BASE_GICD_BASE + GICD_ISENABLER + 0x14),
		mask->mask5);
	 mmio_write_32((BASE_GICD_BASE + GICD_ISENABLER + 0x18),
		mask->mask6);
	 mmio_write_32((BASE_GICD_BASE + GICD_ISENABLER + 0x1c),
		mask->mask7);
	 mmio_write_32((BASE_GICD_BASE + GICD_ISENABLER + 0x20),
		mask->mask8);
	 mmio_write_32((BASE_GICD_BASE + GICD_ISENABLER + 0x24),
		mask->mask9);
	 mmio_write_32((BASE_GICD_BASE + GICD_ISENABLER + 0x28),
		mask->mask10);
	 mmio_write_32((BASE_GICD_BASE + GICD_ISENABLER + 0x2c),
		mask->mask11);
	 mmio_write_32((BASE_GICD_BASE + GICD_ISENABLER + 0x30),
		mask->mask12);
	/* make sure dist changes happen */
	dsb();
63

64
65
	return 0;
}
66
67

/*
68
69
70
71
 * mt_irq_mask_all: disable all interrupts
 * @mask: pointer to struct mtk_irq_mask for storing the original mask value.
 * Return 0 for success; return negative values for failure.
 * (This is ONLY used for the idle current measurement by the factory mode.)
72
 */
73
int mt_irq_mask_all(struct mtk_irq_mask *mask)
74
{
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
	if (mask != NULL) {
		/* for SPI */
		mask->mask1 = mmio_read_32((BASE_GICD_BASE +
			GICD_ISENABLER + 0x4));
		mask->mask2 = mmio_read_32((BASE_GICD_BASE +
			GICD_ISENABLER + 0x8));
		mask->mask3 = mmio_read_32((BASE_GICD_BASE +
			GICD_ISENABLER + 0xc));
		mask->mask4 = mmio_read_32((BASE_GICD_BASE +
			GICD_ISENABLER + 0x10));
		mask->mask5 = mmio_read_32((BASE_GICD_BASE +
			GICD_ISENABLER + 0x14));
		mask->mask6 = mmio_read_32((BASE_GICD_BASE +
			GICD_ISENABLER + 0x18));
		mask->mask7 = mmio_read_32((BASE_GICD_BASE +
			GICD_ISENABLER + 0x1c));
		mask->mask8 = mmio_read_32((BASE_GICD_BASE +
			GICD_ISENABLER + 0x20));
		mask->mask9 = mmio_read_32((BASE_GICD_BASE +
			GICD_ISENABLER + 0x24));
		mask->mask10 = mmio_read_32((BASE_GICD_BASE +
			GICD_ISENABLER + 0x28));
		mask->mask11 = mmio_read_32((BASE_GICD_BASE +
			GICD_ISENABLER + 0x2c));
		mask->mask12 = mmio_read_32((BASE_GICD_BASE +
			GICD_ISENABLER + 0x30));

		/* for SPI */
		mmio_write_32((BASE_GICD_BASE + GICD_ICENABLER + 0x4),
			0xFFFFFFFF);
		mmio_write_32((BASE_GICD_BASE + GICD_ICENABLER + 0x8),
			0xFFFFFFFF);
		mmio_write_32((BASE_GICD_BASE + GICD_ICENABLER + 0xC),
			0xFFFFFFFF);
		mmio_write_32((BASE_GICD_BASE + GICD_ICENABLER + 0x10),
			0xFFFFFFFF);
		mmio_write_32((BASE_GICD_BASE + GICD_ICENABLER + 0x14),
			0xFFFFFFFF);
		mmio_write_32((BASE_GICD_BASE + GICD_ICENABLER + 0x18),
			0xFFFFFFFF);
		mmio_write_32((BASE_GICD_BASE + GICD_ICENABLER + 0x1C),
			0xFFFFFFFF);
		mmio_write_32((BASE_GICD_BASE + GICD_ICENABLER + 0x20),
			0xFFFFFFFF);
		mmio_write_32((BASE_GICD_BASE + GICD_ICENABLER + 0x24),
			0xFFFFFFFF);
		mmio_write_32((BASE_GICD_BASE + GICD_ICENABLER + 0x28),
			0xFFFFFFFF);
		mmio_write_32((BASE_GICD_BASE + GICD_ICENABLER + 0x2c),
			0xFFFFFFFF);
		mmio_write_32((BASE_GICD_BASE + GICD_ICENABLER + 0x30),
			0xFFFFFFFF);
		/* make sure distributor changes happen */
		dsb();

		mask->header = IRQ_MASK_HEADER;
		mask->footer = IRQ_MASK_FOOTER;

		return 0;
	} else {
		return -1;
136
137
138
	}
}

139
static uint32_t mt_irq_get_pol(uint32_t irq)
140
{
141
142
143
#ifdef CIRQ_WITH_POLARITY
	uint32_t reg;
	uint32_t base = INT_POL_CTL0;
144

145
146
147
	if (irq < 32U) {
		return 0;
	}
148

149
	reg = ((irq - 32U) / 32U);
150

151
152
153
154
	return  mmio_read_32(base + reg * 4U);
#else
	return 0;
#endif
155
156
}

157
unsigned int mt_irq_get_sens(unsigned int irq)
158
{
159
	unsigned int config;
160

161
162
163
164
165
166
	/*
	 * 2'b10 edge
	 * 2'b01 level
	 */
	config = mmio_read_32(MT_GIC_BASE + GICD_ICFGR + (irq / 16U) * 4U);
	config = (config >> (irq % 16U) * 2U) & 0x3;
167

168
	return config;
169
170
}

171
static void collect_all_wakeup_events(void)
172
{
173
174
175
176
177
178
179
180
181
182
183
184
185
	unsigned int i;
	uint32_t gic_irq;
	uint32_t cirq;
	uint32_t cirq_reg;
	uint32_t cirq_offset;
	uint32_t mask;
	uint32_t pol_mask;
	uint32_t irq_offset;
	uint32_t irq_mask;

	if ((cirq_all_events.wakeup_events == NULL) ||
			cirq_all_events.num_of_events == 0U) {
		return;
186
187
	}

188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
	for (i = 0U; i < cirq_all_events.num_of_events; i++) {
		if (cirq_all_events.wakeup_events[i] > 0U) {
			gic_irq = cirq_all_events.wakeup_events[i];
			cirq = gic_irq - cirq_all_events.spi_start - 32U;
			cirq_reg = cirq / 32U;
			cirq_offset = cirq % 32U;
			mask = 0x1 << cirq_offset;
			irq_offset = gic_irq % 32U;
			irq_mask = 0x1 << irq_offset;
			/*
			 * CIRQ default masks all
			 */
			cirq_all_events.table[cirq_reg].mask |= mask;
			/*
			 * CIRQ default pol is low
			 */
			pol_mask = mt_irq_get_pol(
					cirq_all_events.wakeup_events[i])
					& irq_mask;
			/*
			 * 0 means rising
			 */
			if (pol_mask == 0U) {
				cirq_all_events.table[cirq_reg].pol |= mask;
			}
			/*
			 * CIRQ could monitor edge/level trigger
			 * cirq register (0: edge, 1: level)
			 */
			if (mt_irq_get_sens(cirq_all_events.wakeup_events[i])
				== SENS_EDGE) {
				cirq_all_events.table[cirq_reg].sen |= mask;
			}
221

222
223
224
			cirq_all_events.table[cirq_reg].used = 1U;
			cirq_all_events.table[cirq_reg].reg_num = cirq_reg;
		}
225
226
227
228
	}
}

/*
229
230
231
232
233
234
 * mt_cirq_set_pol: Set the polarity for the specified SYS_CIRQ number.
 * @cirq_num: the SYS_CIRQ number to set
 * @pol: polarity to set
 * @return:
 *    0: set pol success
 *   -1: cirq num is out of range
235
 */
236
237
#ifdef CIRQ_WITH_POLARITY
static int mt_cirq_set_pol(uint32_t cirq_num, uint32_t pol)
238
{
239
240
	uint32_t base;
	uint32_t bit = 1U << (cirq_num % 32U);
241

242
243
244
245
246
247
248
249
250
251
	if (cirq_num >= CIRQ_IRQ_NUM) {
		return -1;
	}

	if (pol == MT_CIRQ_POL_NEG) {
		base = (cirq_num / 32U) * 4U + CIRQ_POL_CLR_BASE;
	} else if (pol == MT_CIRQ_POL_POS) {
		base = (cirq_num / 32U) * 4U + CIRQ_POL_SET_BASE;
	} else {
		return -1;
252
	}
253
254
255

	mmio_write_32(base, bit);
	return 0;
256
}
257
#endif
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273

/*
 * mt_cirq_mask: Mask the specified SYS_CIRQ.
 * @cirq_num: the SYS_CIRQ number to mask
 * @return:
 *    0: mask success
 *   -1: cirq num is out of range
 */
static int mt_cirq_mask(uint32_t cirq_num)
{
	uint32_t bit = 1U << (cirq_num % 32U);

	if (cirq_num >= CIRQ_IRQ_NUM) {
		return -1;
	}

274
275
	mmio_write_32((cirq_num / 32U) * 4U + CIRQ_MASK_SET_BASE, bit);

276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
	return 0;
}

/*
 * mt_cirq_unmask: Unmask the specified SYS_CIRQ.
 * @cirq_num: the SYS_CIRQ number to unmask
 * @return:
 *    0: umask success
 *   -1: cirq num is out of range
 */
static int mt_cirq_unmask(uint32_t cirq_num)
{
	uint32_t bit = 1U << (cirq_num % 32U);

	if (cirq_num >= CIRQ_IRQ_NUM) {
		return -1;
	}

294
295
	mmio_write_32((cirq_num / 32U) * 4U + CIRQ_MASK_CLR_BASE, bit);

296
297
298
	return 0;
}

299
uint32_t mt_irq_get_en(uint32_t irq)
300
{
301
	uint32_t addr, st, val;
302

303
304
	addr = BASE_GICD_BASE + GICD_ISENABLER + (irq / 32U) * 4U;
	st = mmio_read_32(addr);
305

306
	val = (st >> (irq % 32U)) & 1U;
307

308
	return val;
309
310
}

311
static void __cirq_fast_clone(void)
312
{
313
314
	struct cirq_reg *reg;
	unsigned int i;
315

316
317
	for (i = 0U; i < CIRQ_REG_NUM ; ++i) {
		uint32_t cirq_bit;
318

319
		reg = &cirq_all_events.table[i];
320

321
322
323
		if (reg->used == 0U) {
			continue;
		}
324

325
326
		mmio_write_32(CIRQ_SENS_CLR_BASE + (reg->reg_num * 4U),
				    reg->sen);
327

328
329
330
331
332
333
334
		for (cirq_bit = 0U; cirq_bit < 32U; ++cirq_bit) {
			uint32_t val, cirq_id;
			uint32_t gic_id;
#ifdef CIRQ_WITH_POLARITY
			uint32_t gic_bit, pol;
#endif
			uint32_t en;
335

336
			val = ((1U << cirq_bit) & reg->mask);
337

338
339
340
			if (val == 0U) {
				continue;
			}
341

342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
			cirq_id = (reg->reg_num << 5U) + cirq_bit;
			gic_id = CIRQ_TO_IRQ_NUM(cirq_id);
#ifdef CIRQ_WITH_POLARITY
			gic_bit = (0x1U << ((gic_id - 32U) % 32U));
			pol = mt_irq_get_pol(gic_id) & gic_bit;
			if (pol != 0U) {
				mt_cirq_set_pol(cirq_id, MT_CIRQ_POL_NEG);
			} else {
				mt_cirq_set_pol(cirq_id, MT_CIRQ_POL_POS);
			}
#endif
			en = mt_irq_get_en(gic_id);
			if (en == 1U) {
				mt_cirq_unmask(cirq_id);
			} else {
				mt_cirq_mask(cirq_id);
			}
		}
360
	}
361
}
362

363
364
365
366
367
368
369
static void cirq_fast_clone(void)
{
	if (already_cloned == 0U) {
		collect_all_wakeup_events();
		already_cloned = 1U;
	}
	__cirq_fast_clone();
370
371
}

372
373
374
375
376
void set_wakeup_sources(uint32_t *list, uint32_t num_of_events)
{
	cirq_all_events.num_of_events = num_of_events;
	cirq_all_events.wakeup_events = list;
}
377
/*
378
 * mt_cirq_clone_gic: Copy the setting from GIC to SYS_CIRQ
379
 */
380
void mt_cirq_clone_gic(void)
381
{
382
383
	cirq_fast_clone();
}
384

385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
uint32_t mt_irq_get_pending_vec(uint32_t start_irq)
{
	uint32_t base = 0U;
	uint32_t pending_vec = 0U;
	uint32_t reg = start_irq / 32U;
	uint32_t LSB_num, MSB_num;
	uint32_t LSB_vec, MSB_vec;

	base = BASE_GICD_BASE;

	/* if start_irq is not aligned 32, do some assembling */
	MSB_num = start_irq % 32U;
	if (MSB_num != 0U) {
		LSB_num = 32U - MSB_num;
		LSB_vec = mmio_read_32(base + GICD_ISPENDR +
			reg * 4U) >> MSB_num;
		MSB_vec = mmio_read_32(base + GICD_ISPENDR +
			(reg + 1U) * 4U) << LSB_num;
		pending_vec = MSB_vec | LSB_vec;
	} else {
		pending_vec = mmio_read_32(base + GICD_ISPENDR + reg * 4);
406
407
	}

408
409
410
411
412
413
	return pending_vec;
}

static int mt_cirq_get_mask_vec(unsigned int i)
{
	return mmio_read_32((i * 4U) + CIRQ_MASK_BASE);
414
415
416
}

/*
417
 * mt_cirq_ack_all: Ack all the interrupt on SYS_CIRQ
418
 */
419
void mt_cirq_ack_all(void)
420
{
421
422
	uint32_t ack_vec, pend_vec, mask_vec;
	unsigned int i;
423

424
425
426
427
428
429
430
431
432
433
434
435
436
	for (i = 0; i < CIRQ_CTRL_REG_NUM; i++) {
		/*
		 * if a irq is pending & not masked, don't ack it
		 * , since cirq start irq might not be 32 aligned with gic,
		 * need an exotic API to get proper vector of pending irq
		 */
		pend_vec = mt_irq_get_pending_vec(CIRQ_SPI_START
			+ (i + 1U) * 32U);
		mask_vec = mt_cirq_get_mask_vec(i);
		/* those should be acked are: "not (pending & not masked)",
		 */
		ack_vec = (~pend_vec) | mask_vec;
		mmio_write_32(CIRQ_ACK_BASE + (i * 4U), ack_vec);
437
438
	}

439
440
441
442
443
444
	/*
	 * make sure all cirq setting take effect
	 * before doing other things
	 */
	dsb();
}
445
/*
446
 * mt_cirq_enable: Enable SYS_CIRQ
447
 */
448
void mt_cirq_enable(void)
449
{
450
	uint32_t st;
451

452
453
	/* level only */
	mt_cirq_ack_all();
454

455
456
457
458
459
	st = mmio_read_32(CIRQ_CON);
	/*
	 * CIRQ could monitor edge/level trigger
	 */
	st |= (CIRQ_CON_EN << CIRQ_CON_EN_BITS);
460

461
	mmio_write_32(CIRQ_CON, (st & CIRQ_CON_BITS_MASK));
462
463
464
}

/*
465
 * mt_cirq_disable: Disable SYS_CIRQ
466
 */
467
void mt_cirq_disable(void)
468
{
469
	uint32_t st;
470

471
472
473
474
	st = mmio_read_32(CIRQ_CON);
	st &= ~(CIRQ_CON_EN << CIRQ_CON_EN_BITS);
	mmio_write_32(CIRQ_CON, (st & CIRQ_CON_BITS_MASK));
}
475

476
477
478
void mt_irq_unmask_for_sleep_ex(uint32_t irq)
{
	uint32_t mask;
479

480
	mask = 1U << (irq % 32U);
481

482
483
	mmio_write_32(BASE_GICD_BASE + GICD_ISENABLER +
		((irq / 32U) * 4U), mask);
484
485
}

486
void mt_cirq_mask_all(void)
487
{
488
489
490
491
492
493
	unsigned int i;

	for (i = 0U; i < CIRQ_CTRL_REG_NUM; i++) {
		mmio_write_32(CIRQ_MASK_SET_BASE + (i * 4U), 0xFFFFFFFF);
	}
	dsb();
494
495
}

496
static void cirq_fast_sw_flush(void)
497
{
498
	struct cirq_reg *reg;
499
500
	unsigned int i;

501
502
	for (i = 0U; i < CIRQ_REG_NUM ; ++i) {
		uint32_t cirq_bit;
503

504
505
506
507
		reg = &cirq_all_events.table[i];

		if (reg->used == 0U) {
			continue;
508
509
		}

510
511
512
513
514
515
516
517
518
		reg->pending = mmio_read_32(CIRQ_STA_BASE +
			(reg->reg_num << 2U));
		reg->pending &= reg->mask;

		for (cirq_bit = 0U; cirq_bit < 32U; ++cirq_bit) {
			uint32_t val, cirq_id;

			val = (1U << cirq_bit) & reg->pending;
			if (val == 0U) {
519
520
				continue;
			}
521
522
523
524
525
526

			cirq_id = (reg->reg_num << 5U) + cirq_bit;
			mt_irq_set_pending(CIRQ_TO_IRQ_NUM(cirq_id));
			if (CIRQ_TO_IRQ_NUM(cirq_id) == MD_WDT_IRQ_BIT_ID) {
				INFO("Set MD_WDT_IRQ pending in %s\n",
					__func__);
527
528
529
			}
		}
	}
530
}
531

532
533
534
535
536
537
/*
 * mt_cirq_disable: Flush interrupt from SYS_CIRQ to GIC
 */
void mt_cirq_flush(void)
{
	cirq_fast_sw_flush();
538
539
540
541
542
543
	mt_cirq_mask_all();
	mt_cirq_ack_all();
}

void mt_cirq_sw_reset(void)
{
544
#ifdef CIRQ_NEED_SW_RESET
545
546
	uint32_t st;

547
	st = mmio_read_32(CIRQ_CON);
548
	st |= (CIRQ_SW_RESET << CIRQ_CON_SW_RST_BITS);
549
550
	mmio_write_32(CIRQ_CON, st);
#endif
551
}