Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | */ |
| 6 | |
| 7 | #include <stdio.h> |
bangzheng.liu | 4d71f92 | 2022-09-29 16:12:20 +0800 | [diff] [blame] | 8 | #include "interrupt_control_eclic.h" |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 9 | #include "common.h" |
| 10 | #include "riscv_encoding.h" |
| 11 | #include "register.h" |
| 12 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 13 | // Configure PMP to make all the address space accesable and executable |
| 14 | void eclic_init(uint32_t num_irq) |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 15 | { |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 16 | typedef volatile uint32_t vuint32_t; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 17 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 18 | //clear cfg register |
| 19 | *(volatile uint8_t *)(ECLIC_ADDR_BASE + ECLIC_CFG_OFFSET) = 0; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 20 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 21 | //clear minthresh register |
| 22 | *(volatile uint8_t *)(ECLIC_ADDR_BASE + ECLIC_MTH_OFFSET) = 0; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 23 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 24 | //clear all IP/IE/ATTR/CTRL bits for all interrupt sources |
| 25 | vuint32_t *ptr; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 26 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 27 | vuint32_t *base = (vuint32_t *)(ECLIC_ADDR_BASE + ECLIC_INT_IP_OFFSET); |
| 28 | vuint32_t *upper = (vuint32_t *)(base + num_irq * 4); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 29 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 30 | for (ptr = base; ptr < upper; ptr = ptr + 4) |
| 31 | *ptr = 0; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 32 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 33 | clean_int_src(); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void print_eclic(void) |
| 37 | { |
| 38 | typedef volatile uint32_t vuint32_t; |
| 39 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 40 | vuint32_t *ptr = (vuint32_t *)(ECLIC_ADDR_BASE + ECLIC_INT_IP_OFFSET + 7 * 4); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 41 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 42 | printf("\nTIME=0x%lx\n", *ptr); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 43 | } |
| 44 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 45 | void eclic_enable_interrupt(uint32_t source) |
| 46 | { |
| 47 | *(volatile uint8_t *)(ECLIC_ADDR_BASE + ECLIC_INT_IE_OFFSET + source * 4) = 1; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 48 | } |
| 49 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 50 | void eclic_disable_interrupt(uint32_t source) |
| 51 | { |
| 52 | *(volatile uint8_t *)(ECLIC_ADDR_BASE + ECLIC_INT_IE_OFFSET + source * 4) = 0; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 53 | } |
| 54 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 55 | void eclic_set_pending(uint32_t source) |
| 56 | { |
| 57 | *(volatile uint8_t *)(ECLIC_ADDR_BASE + ECLIC_INT_IP_OFFSET + source * 4) = 1; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 58 | } |
| 59 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 60 | void eclic_clear_pending(uint32_t source) |
| 61 | { |
| 62 | *(volatile uint8_t *)(ECLIC_ADDR_BASE + ECLIC_INT_IP_OFFSET + source * 4) = 0; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 63 | } |
| 64 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 65 | void eclic_set_intctrl(uint32_t source, uint8_t intctrl) |
| 66 | { |
| 67 | *(volatile uint8_t *)(ECLIC_ADDR_BASE + ECLIC_INT_CTRL_OFFSET + source * 4) = intctrl; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 68 | } |
| 69 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 70 | uint8_t eclic_get_intctrl(uint32_t source) |
| 71 | { |
| 72 | return *(volatile uint8_t *)(ECLIC_ADDR_BASE + ECLIC_INT_CTRL_OFFSET + source * 4); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 73 | } |
| 74 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 75 | void eclic_set_intattr(uint32_t source, uint8_t intattr) |
| 76 | { |
| 77 | *(volatile uint8_t *)(ECLIC_ADDR_BASE + ECLIC_INT_ATTR_OFFSET + source * 4) = intattr; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 78 | } |
| 79 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 80 | uint8_t eclic_get_intattr(uint32_t source) |
| 81 | { |
| 82 | return *(volatile uint8_t *)(ECLIC_ADDR_BASE + ECLIC_INT_ATTR_OFFSET + source * 4); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 83 | } |
| 84 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 85 | void eclic_set_cliccfg(uint8_t cliccfg) |
| 86 | { |
| 87 | *(volatile uint8_t *)(ECLIC_ADDR_BASE + ECLIC_CFG_OFFSET) = cliccfg; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 88 | } |
| 89 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 90 | uint8_t eclic_get_cliccfg(void) |
| 91 | { |
| 92 | return *(volatile uint8_t *)(ECLIC_ADDR_BASE + ECLIC_CFG_OFFSET); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 93 | } |
| 94 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 95 | void eclic_set_mth(uint8_t mth) |
| 96 | { |
| 97 | *(volatile uint8_t *)(ECLIC_ADDR_BASE + ECLIC_MTH_OFFSET) = mth; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 98 | } |
| 99 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 100 | uint8_t eclic_get_mth(void) |
| 101 | { |
| 102 | return *(volatile uint8_t *)(ECLIC_ADDR_BASE + ECLIC_MTH_OFFSET); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | //sets nlbits |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 106 | void eclic_set_nlbits(uint8_t nlbits) |
| 107 | { |
| 108 | //shift nlbits to correct position |
| 109 | uint8_t nlbits_shifted = nlbits << ECLIC_CFG_NLBITS_LSB; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 110 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 111 | //read the current cliccfg |
| 112 | uint8_t old_cliccfg = eclic_get_cliccfg(); |
| 113 | uint8_t new_cliccfg = |
| 114 | (old_cliccfg & (~ECLIC_CFG_NLBITS_MASK)) | (ECLIC_CFG_NLBITS_MASK & nlbits_shifted); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 115 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 116 | eclic_set_cliccfg(new_cliccfg); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | //get nlbits |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 120 | uint8_t eclic_get_nlbits(void) |
| 121 | { |
| 122 | //extract nlbits |
| 123 | uint8_t nlbits = eclic_get_cliccfg(); |
| 124 | |
| 125 | nlbits = (nlbits & ECLIC_CFG_NLBITS_MASK) >> ECLIC_CFG_NLBITS_LSB; |
| 126 | return nlbits; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | //sets an interrupt level based encoding of nlbits and CLICINTCTLBITS |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 130 | void eclic_set_irq_lvl(uint32_t source, uint8_t lvl) |
| 131 | { |
| 132 | //extract nlbits |
| 133 | uint8_t nlbits = eclic_get_nlbits(); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 134 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 135 | if (nlbits > CLICINTCTLBITS) |
| 136 | nlbits = CLICINTCTLBITS; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 137 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 138 | //shift lvl right to mask off unused bits |
| 139 | lvl = lvl >> (8 - nlbits); |
| 140 | //shift lvl into correct bit position |
| 141 | lvl = lvl << (8 - nlbits); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 142 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 143 | //write to clicintctrl |
| 144 | uint8_t current_intctrl = eclic_get_intctrl(source); |
| 145 | //shift intctrl left to mask off unused bits |
| 146 | current_intctrl = current_intctrl << nlbits; |
| 147 | //shift intctrl into correct bit position |
| 148 | current_intctrl = current_intctrl >> nlbits; |
| 149 | |
| 150 | eclic_set_intctrl(source, (current_intctrl | lvl)); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | //gets an interrupt level based encoding of nlbits |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 154 | uint8_t eclic_get_irq_lvl(uint32_t source) |
| 155 | { |
| 156 | //extract nlbits |
| 157 | uint8_t nlbits = eclic_get_nlbits(); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 158 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 159 | if (nlbits > CLICINTCTLBITS) |
| 160 | nlbits = CLICINTCTLBITS; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 161 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 162 | uint8_t intctrl = eclic_get_intctrl(source); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 163 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 164 | //shift intctrl |
| 165 | intctrl = intctrl >> (8 - nlbits); |
| 166 | //shift intctrl |
| 167 | uint8_t lvl = intctrl << (8 - nlbits); |
| 168 | |
| 169 | return lvl; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 170 | } |
| 171 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 172 | void eclic_set_irq_lvl_abs(uint32_t source, uint8_t lvl_abs) |
| 173 | { |
| 174 | //extract nlbits |
| 175 | uint8_t nlbits = eclic_get_nlbits(); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 176 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 177 | if (nlbits > CLICINTCTLBITS) |
| 178 | nlbits = CLICINTCTLBITS; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 179 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 180 | //shift lvl_abs into correct bit position |
| 181 | uint8_t lvl = lvl_abs << (8 - nlbits); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 182 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 183 | //write to clicintctrl |
| 184 | uint8_t current_intctrl = eclic_get_intctrl(source); |
| 185 | //shift intctrl left to mask off unused bits |
| 186 | current_intctrl = current_intctrl << nlbits; |
| 187 | //shift intctrl into correct bit position |
| 188 | current_intctrl = current_intctrl >> nlbits; |
| 189 | |
| 190 | eclic_set_intctrl(source, (current_intctrl | lvl)); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 191 | } |
| 192 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 193 | uint8_t eclic_get_irq_lvl_abs(uint32_t source) |
| 194 | { |
| 195 | //extract nlbits |
| 196 | uint8_t nlbits = eclic_get_nlbits(); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 197 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 198 | if (nlbits > CLICINTCTLBITS) |
| 199 | nlbits = CLICINTCTLBITS; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 200 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 201 | uint8_t intctrl = eclic_get_intctrl(source); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 202 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 203 | //shift intctrl |
| 204 | intctrl = intctrl >> (8 - nlbits); |
| 205 | //shift intctrl |
| 206 | uint8_t lvl_abs = intctrl; |
| 207 | |
| 208 | return lvl_abs; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 209 | } |
| 210 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 211 | void eclic_set_irq_pri(uint32_t source, uint8_t pri) |
| 212 | { |
| 213 | //extract nlbits |
| 214 | uint8_t nlbits = eclic_get_nlbits(); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 215 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 216 | if (nlbits > CLICINTCTLBITS) |
| 217 | nlbits = CLICINTCTLBITS; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 218 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 219 | //write to clicintctrl |
| 220 | uint8_t current_intctrl = eclic_get_intctrl(source); |
| 221 | //shift intctrl left to mask off unused bits |
| 222 | current_intctrl = current_intctrl >> (8 - nlbits); |
| 223 | //shift intctrl into correct bit position |
| 224 | current_intctrl = current_intctrl << (8 - nlbits); |
| 225 | |
| 226 | eclic_set_intctrl(source, (current_intctrl | pri)); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 227 | } |
| 228 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 229 | void eclic_mode_enable(void) |
| 230 | { |
| 231 | uint32_t mtvec_value = read_csr(mtvec); |
| 232 | |
| 233 | mtvec_value = mtvec_value & 0xFFFFFFC0; |
| 234 | mtvec_value = mtvec_value | 0x00000003; |
| 235 | write_csr(mtvec, mtvec_value); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | //sets vector-mode or non-vector mode |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 239 | void eclic_set_vmode(uint32_t source) |
| 240 | { |
| 241 | //read the current attr |
| 242 | uint8_t old_intattr = eclic_get_intattr(source); |
| 243 | // Keep other bits unchanged and only set the LSB bit |
| 244 | uint8_t new_intattr = (old_intattr | 0x1); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 245 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 246 | eclic_set_intattr(source, new_intattr); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 247 | } |
| 248 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 249 | void eclic_set_nonvmode(uint32_t source) |
| 250 | { |
| 251 | //read the current attr |
| 252 | uint8_t old_intattr = eclic_get_intattr(source); |
| 253 | // Keep other bits unchanged and only clear the LSB bit |
| 254 | uint8_t new_intattr = (old_intattr & (~0x1)); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 255 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 256 | eclic_set_intattr(source, new_intattr); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | //sets interrupt as level sensitive |
| 260 | //Bit 1, trig[0], is defined as "edge-triggered" (0: level-triggered, 1: edge-triggered); |
| 261 | //Bit 2, trig[1], is defined as "negative-edge" (0: positive-edge, 1: negative-edge). |
| 262 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 263 | void eclic_set_level_trig(uint32_t source) |
| 264 | { |
| 265 | //read the current attr |
| 266 | uint8_t old_intattr = eclic_get_intattr(source); |
| 267 | // Keep other bits unchanged and only clear the bit 1 |
| 268 | uint8_t new_intattr = (old_intattr & (~0x2)); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 269 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 270 | eclic_set_intattr(source, new_intattr); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 271 | } |
| 272 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 273 | void eclic_set_posedge_trig(uint32_t source) |
| 274 | { |
| 275 | //read the current attr |
| 276 | uint8_t old_intattr = eclic_get_intattr(source); |
| 277 | // Keep other bits unchanged and only set the bit 1 |
| 278 | uint8_t new_intattr = (old_intattr | 0x2); |
| 279 | // Keep other bits unchanged and only clear the bit 2 |
| 280 | new_intattr = (new_intattr & (~0x4)); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 281 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 282 | eclic_set_intattr(source, new_intattr); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 283 | } |
| 284 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 285 | void eclic_set_negedge_trig(uint32_t source) |
| 286 | { |
| 287 | //read the current attr |
| 288 | uint8_t old_intattr = eclic_get_intattr(source); |
| 289 | // Keep other bits unchanged and only set the bit 1 |
| 290 | uint8_t new_intattr = (old_intattr | 0x2); |
| 291 | // Keep other bits unchanged and only set the bit 2 |
| 292 | new_intattr = (new_intattr | 0x4); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 293 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 294 | eclic_set_intattr(source, new_intattr); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | extern void core_wfe(void); |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 298 | void wfe(void) |
| 299 | { |
| 300 | core_wfe(); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | void clean_int_src(void) |
| 304 | { |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 305 | for (uint32_t i = 0; i < 8; i++) |
| 306 | REG32(AOCPU_IRQ_SEL0 + i * 4) = 0; |
bangzheng.liu | 4d71f92 | 2022-09-29 16:12:20 +0800 | [diff] [blame] | 307 | #ifdef AOCPU_IRQ_REG_NONCONTINUOUS |
| 308 | for (uint32_t i = 0; i < 8; i++) |
| 309 | REG32(AOCPU_IRQ_SEL8 + i * 4) = 0; |
| 310 | #endif |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | int int_src_sel(uint32_t ulIrq, uint32_t src) |
| 314 | { |
| 315 | uint32_t index; |
| 316 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 317 | if (ulIrq < ECLIC_INTERNAL_NUM_INTERRUPTS || ulIrq > ECLIC_NUM_INTERRUPTS) { |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 318 | printf("Error ulIrq!\n"); |
| 319 | return -1; |
| 320 | } |
| 321 | |
bangzheng.liu | 4d71f92 | 2022-09-29 16:12:20 +0800 | [diff] [blame] | 322 | if (src > IRQ_NUM_MAX) { |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 323 | printf("Error src!\n"); |
| 324 | return -2; |
| 325 | } |
| 326 | |
| 327 | ulIrq -= ECLIC_INTERNAL_NUM_INTERRUPTS; |
| 328 | |
bangzheng.liu | 4d71f92 | 2022-09-29 16:12:20 +0800 | [diff] [blame] | 329 | #ifdef AOCPU_IRQ_REG_NONCONTINUOUS |
| 330 | index = ulIrq / 2; |
| 331 | |
| 332 | if (ulIrq < 16) { |
| 333 | REG32(AOCPU_IRQ_SEL0 + index * 4) &= ~(0x1ff << (ulIrq % 2) * 16); |
| 334 | REG32(AOCPU_IRQ_SEL0 + index * 4) |= src << (ulIrq % 2) * 16; |
| 335 | } else { |
| 336 | REG32(AOCPU_IRQ_SEL8 + index * 4) &= ~(0x1ff << (ulIrq % 2) * 16); |
| 337 | REG32(AOCPU_IRQ_SEL8 + index * 4) |= src << (ulIrq % 2)*16; |
| 338 | } |
| 339 | #else |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 340 | index = ulIrq / 4; |
| 341 | REG32(AOCPU_IRQ_SEL0 + index * 4) &= ~(0xff << (ulIrq % 4) * 8); |
| 342 | REG32(AOCPU_IRQ_SEL0 + index * 4) |= src << (ulIrq % 4) * 8; |
bangzheng.liu | 4d71f92 | 2022-09-29 16:12:20 +0800 | [diff] [blame] | 343 | #endif |
| 344 | |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | int int_src_clean(uint32_t ulIrq) |
| 349 | { |
| 350 | uint32_t index; |
| 351 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 352 | if (ulIrq < ECLIC_INTERNAL_NUM_INTERRUPTS || ulIrq > ECLIC_NUM_INTERRUPTS) { |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 353 | printf("Error ulIrq!\n"); |
| 354 | return -1; |
| 355 | } |
| 356 | |
| 357 | ulIrq -= ECLIC_INTERNAL_NUM_INTERRUPTS; |
| 358 | |
bangzheng.liu | 4d71f92 | 2022-09-29 16:12:20 +0800 | [diff] [blame] | 359 | #ifdef AOCPU_IRQ_REG_NONCONTINUOUS |
| 360 | index = ulIrq / 2; |
| 361 | |
| 362 | if (ulIrq < 16) |
| 363 | REG32(AOCPU_IRQ_SEL0 + index * 4) &= ~(0x1ff << (ulIrq % 2) * 16); |
| 364 | else |
| 365 | REG32(AOCPU_IRQ_SEL8 + index * 4) &= ~(0x1ff << (ulIrq % 2) * 16); |
| 366 | #else |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 367 | index = ulIrq / 4; |
| 368 | REG32(AOCPU_IRQ_SEL0 + index * 4) &= ~(0xff << (ulIrq % 4) * 8); |
bangzheng.liu | 4d71f92 | 2022-09-29 16:12:20 +0800 | [diff] [blame] | 369 | #endif |
| 370 | |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | /*Just for external interrupt source. |
| 375 | *Because int_src_sel() just support external select |
| 376 | */ |
| 377 | int eclic_map_interrupt(uint32_t ulIrq, uint32_t src) |
| 378 | { |
| 379 | uint8_t val; |
| 380 | |
| 381 | if (int_src_sel(ulIrq, src)) { |
| 382 | printf("Enable %ld irq, %ld src fail!\n", ulIrq, src); |
| 383 | return -1; |
| 384 | } |
| 385 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 386 | val = eclic_get_intattr(ulIrq); |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 387 | val |= ECLIC_INT_ATTR_MACH_MODE; |
| 388 | /*Use edge trig interrupt default*/ |
| 389 | val |= ECLIC_INT_ATTR_TRIG_EDGE; |
| 390 | eclic_set_intattr(ulIrq, val); |
| 391 | //eclic_enable_interrupt(ulIrq); |
| 392 | return 0; |
| 393 | } |
| 394 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 395 | uint32_t eclic_interrupt_inner[SOC_ECLIC_NUM_INTERRUPTS] = { 0 }; |
| 396 | int RegisterIrq(uint32_t int_num, uint32_t int_priority, function_ptr_t handler) |
| 397 | { |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 398 | int irq = 0; |
| 399 | |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 400 | for (irq = ECLIC_INTERNAL_NUM_INTERRUPTS; irq <= ECLIC_NUM_INTERRUPTS; irq++) { |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 401 | if (eclic_interrupt_inner[irq - ECLIC_INTERNAL_NUM_INTERRUPTS] == 0) |
| 402 | break; |
| 403 | } |
| 404 | if (eclic_map_interrupt(irq, int_num) < 0) { |
| 405 | printf("eclic map error.\n"); |
| 406 | return -1; |
| 407 | } |
| 408 | eclic_interrupt_inner[irq - ECLIC_INTERNAL_NUM_INTERRUPTS] = int_num; |
| 409 | |
| 410 | *(&vector_base + irq) = (uint32_t)handler; |
| 411 | eclic_set_irq_pri(irq, int_priority); |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | int UnRegisterIrq(uint32_t ulIrq) |
| 417 | { |
| 418 | int irq = 0; |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 419 | |
| 420 | for (irq = ECLIC_INTERNAL_NUM_INTERRUPTS; irq <= ECLIC_NUM_INTERRUPTS; irq++) { |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 421 | if (eclic_interrupt_inner[irq - ECLIC_INTERNAL_NUM_INTERRUPTS] == ulIrq) |
| 422 | break; |
| 423 | } |
| 424 | if (irq > ECLIC_NUM_INTERRUPTS) { |
| 425 | printf("Error ulIrq!\n"); |
| 426 | return -1; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 427 | } |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 428 | |
| 429 | if (int_src_clean(irq)) { |
| 430 | printf("unregister %ld irq, %ld src fail!\n", ulIrq, irq); |
| 431 | return -1; |
| 432 | } |
| 433 | eclic_interrupt_inner[irq - ECLIC_INTERNAL_NUM_INTERRUPTS] = 0; |
| 434 | *(&vector_base + irq) = 0; |
| 435 | |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | int EnableIrq(uint32_t ulIrq) |
| 440 | { |
| 441 | int irq = 0; |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 442 | |
| 443 | for (irq = ECLIC_INTERNAL_NUM_INTERRUPTS; irq <= ECLIC_NUM_INTERRUPTS; irq++) { |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 444 | if (eclic_interrupt_inner[irq - ECLIC_INTERNAL_NUM_INTERRUPTS] == ulIrq) |
| 445 | break; |
| 446 | } |
| 447 | if (irq > ECLIC_NUM_INTERRUPTS) { |
| 448 | printf("Error ulIrq!\n"); |
| 449 | return -1; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 450 | } |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 451 | |
| 452 | eclic_enable_interrupt(irq); |
| 453 | |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | int DisableIrq(uint32_t ulIrq) |
| 458 | { |
| 459 | int irq = 0; |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 460 | |
| 461 | for (irq = ECLIC_INTERNAL_NUM_INTERRUPTS; irq <= ECLIC_NUM_INTERRUPTS; irq++) { |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 462 | if (eclic_interrupt_inner[irq - ECLIC_INTERNAL_NUM_INTERRUPTS] == ulIrq) |
| 463 | break; |
| 464 | } |
| 465 | if (irq > ECLIC_NUM_INTERRUPTS) { |
| 466 | printf("Error ulIrq!\n"); |
| 467 | return -1; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 468 | } |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 469 | |
| 470 | eclic_disable_interrupt(irq); |
| 471 | |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | int SetIrqPriority(uint32_t ulIrq, uint32_t ulProi) |
| 476 | { |
| 477 | int irq = 0; |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 478 | |
| 479 | for (irq = ECLIC_INTERNAL_NUM_INTERRUPTS; irq <= ECLIC_NUM_INTERRUPTS; irq++) { |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 480 | if (eclic_interrupt_inner[irq - ECLIC_INTERNAL_NUM_INTERRUPTS] == ulIrq) |
| 481 | break; |
| 482 | } |
| 483 | if (irq > ECLIC_NUM_INTERRUPTS) { |
| 484 | printf("Error ulIrq!\n"); |
| 485 | return -1; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 486 | } |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 487 | |
| 488 | eclic_set_irq_pri(irq, ulProi); |
| 489 | |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | int ClearPendingIrq(uint32_t ulIrq) |
| 494 | { |
| 495 | int irq = 0; |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 496 | |
| 497 | for (irq = ECLIC_INTERNAL_NUM_INTERRUPTS; irq <= ECLIC_NUM_INTERRUPTS; irq++) { |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 498 | if (eclic_interrupt_inner[irq - ECLIC_INTERNAL_NUM_INTERRUPTS] == ulIrq) |
| 499 | break; |
| 500 | } |
| 501 | if (irq > ECLIC_NUM_INTERRUPTS) { |
| 502 | printf("Error ulIrq!\n"); |
| 503 | return -1; |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 504 | } |
xiaohu.huang | 3826210 | 2022-05-06 22:21:48 +0800 | [diff] [blame] | 505 | |
| 506 | eclic_clear_pending(irq); |
| 507 | |
Xiaohu.Huang | a2c5a04 | 2022-03-12 22:41:09 +0800 | [diff] [blame] | 508 | return 0; |
| 509 | } |