|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Tokita, Hiroshi <[email protected]> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * @brief Driver for Nuclie's Extended Core Interrupt Controller |
| 9 | + */ |
| 10 | + |
| 11 | +#include <kernel.h> |
| 12 | +#include <arch/cpu.h> |
| 13 | +#include <sys/util.h> |
| 14 | +#include <init.h> |
| 15 | +#include <soc.h> |
| 16 | + |
| 17 | +#include <sw_isr_table.h> |
| 18 | + |
| 19 | +union CLICCFG { |
| 20 | + struct { |
| 21 | + uint8_t _reserved0 : 1; |
| 22 | + /** number of interrupt level bits */ |
| 23 | + uint8_t nlbits : 4; |
| 24 | + uint8_t _reserved1 : 2; |
| 25 | + uint8_t _reserved2 : 1; |
| 26 | + } b; |
| 27 | + uint8_t w; |
| 28 | +}; |
| 29 | + |
| 30 | +union CLICINFO { |
| 31 | + struct { |
| 32 | + /** number of max supported interrupts */ |
| 33 | + uint32_t numint : 13; |
| 34 | + /** architecture version */ |
| 35 | + uint32_t version : 8; |
| 36 | + /** supported bits in the clicintctl */ |
| 37 | + uint32_t intctlbits : 4; |
| 38 | + uint32_t _reserved0 : 7; |
| 39 | + } b; |
| 40 | + uint32_t qw; |
| 41 | +}; |
| 42 | + |
| 43 | +union CLICMTH { |
| 44 | + uint8_t w; |
| 45 | +}; |
| 46 | + |
| 47 | +union CLICINTIP { |
| 48 | + struct { |
| 49 | + /** Interrupt Pending */ |
| 50 | + uint8_t IP : 1; |
| 51 | + uint8_t reserved0 : 7; |
| 52 | + } b; |
| 53 | + uint8_t w; |
| 54 | +}; |
| 55 | + |
| 56 | +union CLICINTIE { |
| 57 | + struct { |
| 58 | + /** Interrupt Enabled */ |
| 59 | + uint8_t IE : 1; |
| 60 | + uint8_t reserved0 : 7; |
| 61 | + } b; |
| 62 | + uint8_t w; |
| 63 | +}; |
| 64 | + |
| 65 | +union CLICINTATTR { |
| 66 | + struct { |
| 67 | + /** 0: non-vectored 1:vectored */ |
| 68 | + uint8_t shv : 1; |
| 69 | + /** 0: level 1: rising edge 2: falling edge */ |
| 70 | + uint8_t trg : 2; |
| 71 | + uint8_t reserved0 : 3; |
| 72 | + uint8_t reserved1 : 2; |
| 73 | + } b; |
| 74 | + uint8_t w; |
| 75 | +}; |
| 76 | + |
| 77 | +struct CLICCTRL { |
| 78 | + volatile union CLICINTIP INTIP; |
| 79 | + volatile union CLICINTIE INTIE; |
| 80 | + volatile union CLICINTATTR INTATTR; |
| 81 | + volatile uint8_t INTCTRL; |
| 82 | +}; |
| 83 | + |
| 84 | +/** ECLIC Mode mask for MTVT CSR Register */ |
| 85 | +#define ECLIC_MODE_MTVEC_Msk 3U |
| 86 | + |
| 87 | +/** CLIC INTATTR: TRIG Position */ |
| 88 | +#define CLIC_INTATTR_TRIG_Pos 1U |
| 89 | +/** CLIC INTATTR: TRIG Mask */ |
| 90 | +#define CLIC_INTATTR_TRIG_Msk (0x3UL << CLIC_INTATTR_TRIG_Pos) |
| 91 | + |
| 92 | +#define ECLIC_CFG (*((volatile union CLICCFG *)(DT_REG_ADDR_BY_IDX(DT_NODELABEL(eclic), 0)))) |
| 93 | +#define ECLIC_INFO (*((volatile union CLICINFO *)(DT_REG_ADDR_BY_IDX(DT_NODELABEL(eclic), 1)))) |
| 94 | +#define ECLIC_MTH (*((volatile union CLICMTH *)(DT_REG_ADDR_BY_IDX(DT_NODELABEL(eclic), 2)))) |
| 95 | +#define ECLIC_CTRL ((volatile struct CLICCTRL *)(DT_REG_ADDR_BY_IDX(DT_NODELABEL(eclic), 3))) |
| 96 | +#define ECLIC_CTRL_SIZE (DT_REG_SIZE_BY_IDX(DT_NODELABEL(eclic), 3)) |
| 97 | + |
| 98 | +#if CONFIG_3RD_LEVEL_INTERRUPTS |
| 99 | +#define INTERRUPT_LEVEL 2 |
| 100 | +#elif CONFIG_2ND_LEVEL_INTERRUPTS |
| 101 | +#define INTERRUPT_LEVEL 1 |
| 102 | +#else |
| 103 | +#define INTERRUPT_LEVEL 0 |
| 104 | +#endif |
| 105 | + |
| 106 | +static uint8_t nlbits; |
| 107 | +static uint8_t intctlbits; |
| 108 | +static uint8_t max_prio; |
| 109 | +static uint8_t max_level; |
| 110 | +static uint8_t intctrl_mask; |
| 111 | + |
| 112 | +static inline uint8_t leftalign8(uint8_t val, uint8_t shift) |
| 113 | +{ |
| 114 | + return (val << (8U - shift)); |
| 115 | +} |
| 116 | + |
| 117 | +static inline uint8_t mask8(uint8_t len) |
| 118 | +{ |
| 119 | + return ((1 << len) - 1) & 0xFFFFU; |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * @brief Enable interrupt |
| 124 | + */ |
| 125 | +void nuclei_eclic_irq_enable(uint32_t irq) |
| 126 | +{ |
| 127 | + ECLIC_CTRL[irq].INTIE.b.IE = 1; |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * @brief Disable interrupt |
| 132 | + */ |
| 133 | +void nuclei_eclic_irq_disable(uint32_t irq) |
| 134 | +{ |
| 135 | + ECLIC_CTRL[irq].INTIE.b.IE = 0; |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * @brief Get enable status of interrupt |
| 140 | + */ |
| 141 | +int nuclei_eclic_irq_is_enabled(uint32_t irq) |
| 142 | +{ |
| 143 | + return ECLIC_CTRL[irq].INTIE.b.IE; |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * @brief Set priority and level of interrupt |
| 148 | + */ |
| 149 | +void nuclei_eclic_irq_priority_set(uint32_t irq, uint32_t pri, uint32_t flags) |
| 150 | +{ |
| 151 | + const uint8_t prio = leftalign8(MIN(pri, max_prio), intctlbits); |
| 152 | + const uint8_t level = leftalign8(MIN((irq_get_level(irq) - 1), max_level), nlbits); |
| 153 | + const uint8_t intctrl = (prio | level) | (~intctrl_mask); |
| 154 | + |
| 155 | + ECLIC_CTRL[irq].INTCTRL = intctrl; |
| 156 | + |
| 157 | + ECLIC_CTRL[irq].INTATTR.b.shv = 0; |
| 158 | + ECLIC_CTRL[irq].INTATTR.b.trg = (uint8_t)(flags & CLIC_INTATTR_TRIG_Msk); |
| 159 | +} |
| 160 | + |
| 161 | +static int nuclei_eclic_init(const struct device *dev) |
| 162 | +{ |
| 163 | + /* check hardware support required interrupt levels */ |
| 164 | + __ASSERT_NO_MSG(ECLIC_INFO.b.intctlbits >= INTERRUPT_LEVEL); |
| 165 | + |
| 166 | + ECLIC_MTH.w = 0; |
| 167 | + ECLIC_CFG.w = 0; |
| 168 | + ECLIC_CFG.b.nlbits = INTERRUPT_LEVEL; |
| 169 | + for (int i = 0; i < ECLIC_CTRL_SIZE; i++) { |
| 170 | + ECLIC_CTRL[i] = (struct CLICCTRL) { 0 }; |
| 171 | + } |
| 172 | + |
| 173 | + csr_write(mtvec, ((csr_read(mtvec) & 0xFFFFFFC0) | ECLIC_MODE_MTVEC_Msk)); |
| 174 | + |
| 175 | + nlbits = ECLIC_CFG.b.nlbits; |
| 176 | + intctlbits = ECLIC_INFO.b.intctlbits; |
| 177 | + max_prio = mask8(intctlbits - nlbits); |
| 178 | + max_level = mask8(nlbits); |
| 179 | + intctrl_mask = leftalign8(mask8(intctlbits), intctlbits); |
| 180 | + |
| 181 | + return 0; |
| 182 | +} |
| 183 | + |
| 184 | +SYS_INIT(nuclei_eclic_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT); |
0 commit comments