|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 ispace, inc. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +#include <zephyr/irq.h> |
| 7 | +#include <zephyr/devicetree.h> |
| 8 | +#include <string.h> |
| 9 | + |
| 10 | +#define DT_DRV_COMPAT ti_tms570_vim |
| 11 | + |
| 12 | +#define DRV_CONTROL_REG DT_INST_REG_ADDR_BY_IDX(0, 0) |
| 13 | +#define DRV_ECC_REG DT_INST_REG_ADDR_BY_IDX(0, 1) |
| 14 | +#define DRV_VIMRAM_REG DT_INST_REG_ADDR_BY_IDX(0, 2) |
| 15 | +#define DRV_VIMRAM_REG_SIZE DT_INST_REG_SIZE_BY_IDX(0, 2) |
| 16 | + |
| 17 | +/* control registers */ |
| 18 | +#define VIM_REG_IRQINDEX (DRV_CONTROL_REG + 0x00) |
| 19 | +#define VIM_REG_FIQINDEX (DRV_CONTROL_REG + 0x04) |
| 20 | +#define VIM_REG_REQMASKSET_0 (DRV_CONTROL_REG + 0x30) /* 0,1,2,3 in sequence, each is a 4 byte register*/ |
| 21 | +#define VIM_REG_REQMASKCLR_0 (DRV_CONTROL_REG + 0x40) /* 0,1,2,3 in sequence, each is a 4 byte register*/ |
| 22 | + |
| 23 | +/* ECC related registers */ |
| 24 | +#define VIM_ECC_CTL (DRV_ECC_REG + 0xF0) |
| 25 | + |
| 26 | +#define REQUMASK_IRQ_PER_REG (32u) |
| 27 | + |
| 28 | +static inline void set_reqmask_bit(unsigned int irq, uintptr_t reg_0_addr) |
| 29 | +{ |
| 30 | + sys_write32(1 << (irq % REQUMASK_IRQ_PER_REG), |
| 31 | + reg_0_addr + (irq / REQUMASK_IRQ_PER_REG) * sizeof(uint32_t)); |
| 32 | +} |
| 33 | + |
| 34 | +static inline int get_reqmask_bit(unsigned int irq, uintptr_t reg_0_addr) |
| 35 | +{ |
| 36 | + return sys_read32(reg_0_addr + (irq / REQUMASK_IRQ_PER_REG) * sizeof(uint32_t)); |
| 37 | +} |
| 38 | + |
| 39 | +/* count of number of times the phantom interrupt happened */ |
| 40 | +unsigned int nr_phantom_isr; |
| 41 | + |
| 42 | +static void phantom_isr(void) |
| 43 | +{ |
| 44 | + /** |
| 45 | + * we don't want this to call the z_spurious_irq because we have seen |
| 46 | + * phantom irq happen even though we don't expect it to happen. |
| 47 | + */ |
| 48 | + nr_phantom_isr++; |
| 49 | +} |
| 50 | + |
| 51 | +#if defined(CONFIG_RUNTIME_NMI) |
| 52 | +static void tms570_nmi_handler(void) |
| 53 | +{ |
| 54 | + while (1) { |
| 55 | + /* forever */ |
| 56 | + } |
| 57 | +} |
| 58 | +#endif |
| 59 | + |
| 60 | +/** |
| 61 | + * @brief Get active interrupt ID (IRQ only) |
| 62 | + * |
| 63 | + * @return Returns the ID of an active interrupt |
| 64 | + */ |
| 65 | +unsigned int z_soc_irq_get_active(void) |
| 66 | +{ |
| 67 | + unsigned int irq_idx; |
| 68 | + |
| 69 | + /* a 0 means phantom ISR, channel 0 starts from index 1 */ |
| 70 | + irq_idx = sys_read32(VIM_REG_IRQINDEX); |
| 71 | + if (irq_idx > 0) { |
| 72 | + z_soc_irq_disable(irq_idx - 1); |
| 73 | + return irq_idx - 1; |
| 74 | + |
| 75 | + } else { |
| 76 | + phantom_isr(); |
| 77 | + } |
| 78 | + |
| 79 | + return (CONFIG_NUM_IRQS + 1); |
| 80 | +} |
| 81 | + |
| 82 | +void z_soc_irq_priority_set(unsigned int irq, unsigned int prio, uint32_t flags) |
| 83 | +{ |
| 84 | + /** |
| 85 | + * not supported, all IRQ sources generate IRQ, instead of FIQ, |
| 86 | + * and with the default priority. |
| 87 | + */ |
| 88 | +} |
| 89 | + |
| 90 | +void z_soc_irq_enable(unsigned int irq) |
| 91 | +{ |
| 92 | + set_reqmask_bit(irq, VIM_REG_REQMASKSET_0); |
| 93 | +} |
| 94 | + |
| 95 | +void z_soc_irq_disable(unsigned int irq) |
| 96 | +{ |
| 97 | + set_reqmask_bit(irq, VIM_REG_REQMASKCLR_0); |
| 98 | +} |
| 99 | + |
| 100 | +int z_soc_irq_is_enabled(unsigned int irq) |
| 101 | +{ |
| 102 | + return get_reqmask_bit(irq, VIM_REG_REQMASKSET_0); |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * @brief Signal end-of-interrupt |
| 107 | + * |
| 108 | + * @param irq interrupt ID |
| 109 | + */ |
| 110 | +void z_soc_irq_eoi(unsigned int irq) |
| 111 | +{ |
| 112 | + z_soc_irq_enable(irq); |
| 113 | +} |
| 114 | + |
| 115 | +void z_soc_irq_init(void) |
| 116 | +{ |
| 117 | + /** |
| 118 | + * ref. SPNA218.pdf |
| 119 | + * We are implementing what is referred to as "Legacy ARM7 Interrupts". |
| 120 | + * We do not use the VIM_RAM at all. |
| 121 | + * Sequence is like this: |
| 122 | + * 1. Interrupt request happens |
| 123 | + * 2. Exception vector 0x18 (IRQ) or 0x1C (FIQ) is taken |
| 124 | + * - in case of IRQ "ldr pc, =_isr_wrapper" |
| 125 | + * - in case of FIQ "ldr pc, =z_arm_nmi" |
| 126 | + * 3. _isr_wrapper uses z_soc_irq_get_active to get index into |
| 127 | + * _sw_isr_table for arg and ISR handler |
| 128 | + * 4. run ISR handler |
| 129 | + * |
| 130 | + * Drivers attach interrupts using IRQ_CONNECT/IRQ_DIRECT_CONNECT like: |
| 131 | + * IRQ_CONNECT(irqnum, irqnum, z_irq_spurious, NULL, 0); |
| 132 | + */ |
| 133 | + |
| 134 | + /* Errata VIM#28 Workaround: Disable Single Bit error correction */ |
| 135 | + sys_write32((0xAU << 0U) | (0x5U << 16U), VIM_ECC_CTL); |
| 136 | + |
| 137 | + /** |
| 138 | + * We do not use VIM RAM, or need to care about ECC, but if we do not set |
| 139 | + * the VIM RAM locations to a _valid_ memory address (memset to 0 doesn't work), |
| 140 | + * it keeps generating ESM NMI with ESM group 1 status = 0x80008000. |
| 141 | + * Doing the following stops it. |
| 142 | + */ |
| 143 | + uint32_t *p = (void *) DRV_VIMRAM_REG; |
| 144 | + for (int i = 0; i < DRV_VIMRAM_REG_SIZE / 4; i++) { |
| 145 | + *(p + i) = (uint32_t) &z_irq_spurious; |
| 146 | + } |
| 147 | + |
| 148 | +#if defined(CONFIG_RUNTIME_NMI) |
| 149 | + z_arm_nmi_set_handler(tms570_nmi_handler); |
| 150 | +#endif |
| 151 | + |
| 152 | + /* enable interrupt */ |
| 153 | + arch_irq_unlock(0); |
| 154 | +} |
0 commit comments