2019-03-28 14:44:12 +01:00
|
|
|
/*
|
2021-03-29 15:38:25 +02:00
|
|
|
* Copyright (c) 2018 - 2021 Antmicro <www.antmicro.com>
|
2019-03-28 14:44:12 +01:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-03-24 15:58:31 -05:00
|
|
|
#define DT_DRV_COMPAT litex_eth0
|
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/arch/cpu.h>
|
|
|
|
#include <zephyr/init.h>
|
|
|
|
#include <zephyr/irq.h>
|
|
|
|
#include <zephyr/device.h>
|
2019-03-28 14:44:12 +01:00
|
|
|
#include <zephyr/types.h>
|
|
|
|
|
2020-03-24 15:58:31 -05:00
|
|
|
#define IRQ_MASK DT_REG_ADDR_BY_NAME(DT_INST(0, vexriscv_intc0), irq_mask)
|
|
|
|
#define IRQ_PENDING DT_REG_ADDR_BY_NAME(DT_INST(0, vexriscv_intc0), irq_pending)
|
2019-03-28 14:44:12 +01:00
|
|
|
|
2020-03-24 15:58:31 -05:00
|
|
|
#define TIMER0_IRQ DT_IRQN(DT_INST(0, litex_timer0))
|
|
|
|
#define UART0_IRQ DT_IRQN(DT_INST(0, litex_uart0))
|
2019-07-10 07:57:52 +02:00
|
|
|
|
2020-03-24 15:58:31 -05:00
|
|
|
#define ETH0_IRQ DT_IRQN(DT_INST(0, litex_eth0))
|
2019-07-10 07:57:52 +02:00
|
|
|
|
2020-03-12 17:54:08 +01:00
|
|
|
#define I2S_RX_IRQ DT_IRQN(DT_NODELABEL(i2s_rx))
|
|
|
|
#define I2S_TX_IRQ DT_IRQN(DT_NODELABEL(i2s_tx))
|
2021-03-29 15:38:25 +02:00
|
|
|
|
|
|
|
#define GPIO_IRQ DT_IRQN(DT_NODELABEL(gpio_in))
|
|
|
|
|
2020-05-27 11:26:57 -05:00
|
|
|
static inline void vexriscv_litex_irq_setmask(uint32_t mask)
|
2019-03-28 14:44:12 +01:00
|
|
|
{
|
|
|
|
__asm__ volatile ("csrw %0, %1" :: "i"(IRQ_MASK), "r"(mask));
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:26:57 -05:00
|
|
|
static inline uint32_t vexriscv_litex_irq_getmask(void)
|
2019-03-28 14:44:12 +01:00
|
|
|
{
|
2020-05-27 11:26:57 -05:00
|
|
|
uint32_t mask;
|
2019-03-28 14:44:12 +01:00
|
|
|
|
|
|
|
__asm__ volatile ("csrr %0, %1" : "=r"(mask) : "i"(IRQ_MASK));
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:26:57 -05:00
|
|
|
static inline uint32_t vexriscv_litex_irq_pending(void)
|
2019-03-28 14:44:12 +01:00
|
|
|
{
|
2020-05-27 11:26:57 -05:00
|
|
|
uint32_t pending;
|
2019-03-28 14:44:12 +01:00
|
|
|
|
|
|
|
__asm__ volatile ("csrr %0, %1" : "=r"(pending) : "i"(IRQ_PENDING));
|
|
|
|
return pending;
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:26:57 -05:00
|
|
|
static inline void vexriscv_litex_irq_setie(uint32_t ie)
|
2019-03-28 14:44:12 +01:00
|
|
|
{
|
|
|
|
if (ie) {
|
|
|
|
__asm__ volatile ("csrrs x0, mstatus, %0"
|
2020-01-03 18:18:24 -08:00
|
|
|
:: "r"(MSTATUS_IEN));
|
2019-03-28 14:44:12 +01:00
|
|
|
} else {
|
|
|
|
__asm__ volatile ("csrrc x0, mstatus, %0"
|
2020-01-03 18:18:24 -08:00
|
|
|
:: "r"(MSTATUS_IEN));
|
2019-03-28 14:44:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
isr: Normalize usage of device instance through ISR
The goal of this patch is to replace the 'void *' parameter by 'struct
device *' if they use such variable or just 'const void *' on all
relevant ISRs
This will avoid not-so-nice const qualifier tweaks when device instances
will be constant.
Note that only the ISR passed to IRQ_CONNECT are of interest here.
In order to do so, the script fix_isr.py below is necessary:
from pathlib import Path
import subprocess
import pickle
import mmap
import sys
import re
import os
cocci_template = """
@r_fix_isr_0
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
(
const struct device *D = (const struct device *)P;
|
const struct device *D = P;
)
...
}
@r_fix_isr_1
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
const struct device *D;
...
(
D = (const struct device *)P;
|
D = P;
)
...
}
@r_fix_isr_2
@
type ret_type;
identifier A;
@@
-ret_type <!fn!>(void *A)
+ret_type <!fn!>(const void *A)
{
...
}
@r_fix_isr_3
@
const struct device *D;
@@
-<!fn!>((void *)D);
+<!fn!>(D);
@r_fix_isr_4
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
(
-const struct device *D = (const struct device *)P;
|
-const struct device *D = P;
)
...
}
@r_fix_isr_5
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
-const struct device *D;
...
(
-D = (const struct device *)P;
|
-D = P;
)
...
}
"""
def find_isr(fn):
db = []
data = None
start = 0
try:
with open(fn, 'r+') as f:
data = str(mmap.mmap(f.fileno(), 0).read())
except Exception as e:
return db
while True:
isr = ""
irq = data.find('IRQ_CONNECT', start)
while irq > -1:
p = 1
arg = 1
p_o = data.find('(', irq)
if p_o < 0:
irq = -1
break;
pos = p_o + 1
while p > 0:
if data[pos] == ')':
p -= 1
elif data[pos] == '(':
p += 1
elif data[pos] == ',' and p == 1:
arg += 1
if arg == 3:
isr += data[pos]
pos += 1
isr = isr.strip(',\\n\\t ')
if isr not in db and len(isr) > 0:
db.append(isr)
start = pos
break
if irq < 0:
break
return db
def patch_isr(fn, isr_list):
if len(isr_list) <= 0:
return
for isr in isr_list:
tmplt = cocci_template.replace('<!fn!>', isr)
with open('/tmp/isr_fix.cocci', 'w') as f:
f.write(tmplt)
cmd = ['spatch', '--sp-file', '/tmp/isr_fix.cocci', '--in-place', fn]
subprocess.run(cmd)
def process_files(path):
if path.is_file() and path.suffix in ['.h', '.c']:
p = str(path.parent) + '/' + path.name
isr_list = find_isr(p)
patch_isr(p, isr_list)
elif path.is_dir():
for p in path.iterdir():
process_files(p)
if len(sys.argv) < 2:
print("You need to provide a dir/file path")
sys.exit(1)
process_files(Path(sys.argv[1]))
And is run: ./fix_isr.py <zephyr root directory>
Finally, some files needed manual fixes such.
Fixes #27399
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-06-17 14:58:56 +02:00
|
|
|
static void vexriscv_litex_irq_handler(const void *device)
|
2019-03-28 14:44:12 +01:00
|
|
|
{
|
|
|
|
struct _isr_table_entry *ite;
|
2020-05-27 11:26:57 -05:00
|
|
|
uint32_t pending, mask, irqs;
|
2019-03-28 14:44:12 +01:00
|
|
|
|
|
|
|
pending = vexriscv_litex_irq_pending();
|
|
|
|
mask = vexriscv_litex_irq_getmask();
|
|
|
|
irqs = pending & mask;
|
|
|
|
|
|
|
|
#ifdef CONFIG_LITEX_TIMER
|
|
|
|
if (irqs & (1 << TIMER0_IRQ)) {
|
|
|
|
ite = &_sw_isr_table[TIMER0_IRQ];
|
|
|
|
ite->isr(ite->arg);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
|
|
|
|
if (irqs & (1 << UART0_IRQ)) {
|
|
|
|
ite = &_sw_isr_table[UART0_IRQ];
|
|
|
|
ite->isr(ite->arg);
|
|
|
|
}
|
|
|
|
#endif
|
2019-07-10 07:57:52 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_ETH_LITEETH
|
|
|
|
if (irqs & (1 << ETH0_IRQ)) {
|
2020-07-07 16:44:57 +02:00
|
|
|
ite = &_sw_isr_table[ETH0_IRQ];
|
2019-07-10 07:57:52 +02:00
|
|
|
ite->isr(ite->arg);
|
|
|
|
}
|
|
|
|
#endif
|
2020-03-12 17:54:08 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_I2S
|
|
|
|
if (irqs & (1 << I2S_RX_IRQ)) {
|
|
|
|
ite = &_sw_isr_table[I2S_RX_IRQ];
|
|
|
|
ite->isr(ite->arg);
|
|
|
|
}
|
|
|
|
if (irqs & (1 << I2S_TX_IRQ)) {
|
|
|
|
ite = &_sw_isr_table[I2S_TX_IRQ];
|
|
|
|
ite->isr(ite->arg);
|
|
|
|
}
|
|
|
|
#endif
|
2021-03-29 15:38:25 +02:00
|
|
|
|
|
|
|
if (irqs & (1 << GPIO_IRQ)) {
|
|
|
|
ite = &_sw_isr_table[GPIO_IRQ];
|
|
|
|
ite->isr(ite->arg);
|
|
|
|
}
|
2019-03-28 14:44:12 +01:00
|
|
|
}
|
|
|
|
|
2019-11-07 12:43:29 -08:00
|
|
|
void arch_irq_enable(unsigned int irq)
|
2019-03-28 14:44:12 +01:00
|
|
|
{
|
|
|
|
vexriscv_litex_irq_setmask(vexriscv_litex_irq_getmask() | (1 << irq));
|
|
|
|
}
|
|
|
|
|
2019-11-07 12:43:29 -08:00
|
|
|
void arch_irq_disable(unsigned int irq)
|
2019-03-28 14:44:12 +01:00
|
|
|
{
|
|
|
|
vexriscv_litex_irq_setmask(vexriscv_litex_irq_getmask() & ~(1 << irq));
|
|
|
|
}
|
|
|
|
|
2019-11-07 12:43:29 -08:00
|
|
|
int arch_irq_is_enabled(unsigned int irq)
|
2019-03-28 14:44:12 +01:00
|
|
|
{
|
|
|
|
return vexriscv_litex_irq_getmask() & (1 << irq);
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int vexriscv_litex_irq_init(const struct device *dev)
|
2019-03-28 14:44:12 +01:00
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
__asm__ volatile ("csrrs x0, mie, %0"
|
|
|
|
:: "r"((1 << RISCV_MACHINE_TIMER_IRQ)
|
|
|
|
| (1 << RISCV_MACHINE_EXT_IRQ)));
|
|
|
|
vexriscv_litex_irq_setie(1);
|
|
|
|
IRQ_CONNECT(RISCV_MACHINE_EXT_IRQ, 0, vexriscv_litex_irq_handler,
|
|
|
|
NULL, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SYS_INIT(vexriscv_litex_irq_init, PRE_KERNEL_2,
|
2022-03-11 16:25:41 -06:00
|
|
|
CONFIG_INTC_INIT_PRIORITY);
|