2015-09-29 23:42:22 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Intel Corporation.
|
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-09-29 23:42:22 +08:00
|
|
|
*/
|
|
|
|
|
2020-03-26 01:05:44 +08:00
|
|
|
#define DT_DRV_COMPAT shared_irq
|
|
|
|
|
2016-03-10 01:38:02 +08:00
|
|
|
#include <errno.h>
|
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/device.h>
|
|
|
|
#include <zephyr/shared_irq.h>
|
|
|
|
#include <zephyr/init.h>
|
|
|
|
#include <zephyr/sys/sys_io.h>
|
2022-10-17 16:24:11 +08:00
|
|
|
#include <zephyr/irq.h>
|
2015-09-29 23:42:22 +08:00
|
|
|
|
2015-12-11 01:52:56 +08:00
|
|
|
#ifdef CONFIG_IOAPIC
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/drivers/interrupt_controller/ioapic.h>
|
2015-12-11 01:52:56 +08:00
|
|
|
#endif
|
|
|
|
|
2021-04-11 04:18:15 +08:00
|
|
|
typedef void (*shared_irq_config_irq_t)(void);
|
|
|
|
|
|
|
|
struct shared_irq_config {
|
|
|
|
uint32_t irq_num;
|
|
|
|
shared_irq_config_irq_t config;
|
|
|
|
uint32_t client_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct shared_irq_client {
|
|
|
|
const struct device *isr_dev;
|
|
|
|
isr_t isr_func;
|
|
|
|
uint32_t enabled;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct shared_irq_runtime {
|
2021-03-29 00:07:56 +08:00
|
|
|
struct shared_irq_client *const client;
|
2021-04-11 04:18:15 +08:00
|
|
|
};
|
|
|
|
|
2015-09-29 23:42:22 +08:00
|
|
|
/**
|
|
|
|
* @brief Register a device ISR
|
|
|
|
* @param dev Pointer to device structure for SHARED_IRQ driver instance.
|
|
|
|
* @param isr_func Pointer to the ISR function for the device.
|
|
|
|
* @param isr_dev Pointer to the device that will service the interrupt.
|
|
|
|
*/
|
2020-05-01 02:33:38 +08:00
|
|
|
static int isr_register(const struct device *dev, isr_t isr_func,
|
|
|
|
const struct device *isr_dev)
|
2015-09-29 23:42:22 +08:00
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct shared_irq_runtime *clients = dev->data;
|
2020-05-29 02:44:16 +08:00
|
|
|
const struct shared_irq_config *config = dev->config;
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t i;
|
2015-09-29 23:42:22 +08:00
|
|
|
|
2018-11-30 03:12:22 +08:00
|
|
|
for (i = 0U; i < config->client_count; i++) {
|
2015-09-29 23:42:22 +08:00
|
|
|
if (!clients->client[i].isr_dev) {
|
|
|
|
clients->client[i].isr_dev = isr_dev;
|
|
|
|
clients->client[i].isr_func = isr_func;
|
2016-03-10 01:01:20 +08:00
|
|
|
return 0;
|
2015-09-29 23:42:22 +08:00
|
|
|
}
|
|
|
|
}
|
2016-03-10 01:38:02 +08:00
|
|
|
return -EIO;
|
2015-09-29 23:42:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Enable ISR for device
|
|
|
|
* @param dev Pointer to device structure for SHARED_IRQ driver instance.
|
|
|
|
* @param isr_dev Pointer to the device that will service the interrupt.
|
|
|
|
*/
|
2020-05-01 02:33:38 +08:00
|
|
|
static inline int enable(const struct device *dev,
|
|
|
|
const struct device *isr_dev)
|
2015-09-29 23:42:22 +08:00
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct shared_irq_runtime *clients = dev->data;
|
2020-05-29 02:44:16 +08:00
|
|
|
const struct shared_irq_config *config = dev->config;
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t i;
|
2015-09-29 23:42:22 +08:00
|
|
|
|
2018-11-30 03:12:22 +08:00
|
|
|
for (i = 0U; i < config->client_count; i++) {
|
2015-09-29 23:42:22 +08:00
|
|
|
if (clients->client[i].isr_dev == isr_dev) {
|
2019-03-27 09:57:45 +08:00
|
|
|
clients->client[i].enabled = 1U;
|
2015-09-29 23:42:22 +08:00
|
|
|
irq_enable(config->irq_num);
|
2016-03-10 01:01:20 +08:00
|
|
|
return 0;
|
2015-09-29 23:42:22 +08:00
|
|
|
}
|
|
|
|
}
|
2016-03-10 01:38:02 +08:00
|
|
|
return -EIO;
|
2015-09-29 23:42:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int last_enabled_isr(struct shared_irq_runtime *clients, int count)
|
|
|
|
{
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t i;
|
2015-09-29 23:42:22 +08:00
|
|
|
|
2018-11-30 03:12:22 +08:00
|
|
|
for (i = 0U; i < count; i++) {
|
2015-09-29 23:42:22 +08:00
|
|
|
if (clients->client[i].enabled) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @brief Disable ISR for device
|
|
|
|
* @param dev Pointer to device structure for SHARED_IRQ driver instance.
|
|
|
|
* @param isr_dev Pointer to the device that will service the interrupt.
|
|
|
|
*/
|
2020-05-01 02:33:38 +08:00
|
|
|
static inline int disable(const struct device *dev,
|
|
|
|
const struct device *isr_dev)
|
2015-09-29 23:42:22 +08:00
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct shared_irq_runtime *clients = dev->data;
|
2020-05-29 02:44:16 +08:00
|
|
|
const struct shared_irq_config *config = dev->config;
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t i;
|
2015-09-29 23:42:22 +08:00
|
|
|
|
2018-11-30 03:12:22 +08:00
|
|
|
for (i = 0U; i < config->client_count; i++) {
|
2015-09-29 23:42:22 +08:00
|
|
|
if (clients->client[i].isr_dev == isr_dev) {
|
2019-03-27 09:57:45 +08:00
|
|
|
clients->client[i].enabled = 0U;
|
2015-09-29 23:42:22 +08:00
|
|
|
if (last_enabled_isr(clients, config->client_count)) {
|
|
|
|
irq_disable(config->irq_num);
|
|
|
|
}
|
2016-03-10 01:01:20 +08:00
|
|
|
return 0;
|
2015-09-29 23:42:22 +08:00
|
|
|
}
|
|
|
|
}
|
2016-03-10 01:38:02 +08:00
|
|
|
return -EIO;
|
2015-09-29 23:42:22 +08:00
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
void shared_irq_isr(const struct device *dev)
|
2015-09-29 23:42:22 +08:00
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct shared_irq_runtime *clients = dev->data;
|
2020-05-29 02:44:16 +08:00
|
|
|
const struct shared_irq_config *config = dev->config;
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t i;
|
2015-09-29 23:42:22 +08:00
|
|
|
|
2018-11-30 03:12:22 +08:00
|
|
|
for (i = 0U; i < config->client_count; i++) {
|
2015-09-29 23:42:22 +08:00
|
|
|
if (clients->client[i].isr_dev) {
|
|
|
|
clients->client[i].isr_func(clients->client[i].isr_dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 15:39:02 +08:00
|
|
|
static const struct shared_irq_driver_api api_funcs = {
|
2015-09-29 23:42:22 +08:00
|
|
|
.isr_register = isr_register,
|
|
|
|
.enable = enable,
|
|
|
|
.disable = disable,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
int shared_irq_initialize(const struct device *dev)
|
2015-09-29 23:42:22 +08:00
|
|
|
{
|
2020-05-29 02:44:16 +08:00
|
|
|
const struct shared_irq_config *config = dev->config;
|
2016-01-08 16:46:14 +08:00
|
|
|
config->config();
|
2015-09-29 23:42:22 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-28 23:27:36 +08:00
|
|
|
/*
|
|
|
|
* INST_SUPPORTS_DEP_ORDS_CNT: Counts the number of "elements" in
|
|
|
|
* DT_SUPPORTS_DEP_ORDS(n). There is a comma after each ordinal(inc. the last)
|
|
|
|
* Hence FOR_EACH adds "+1" once too often which has to be subtracted in the end.
|
|
|
|
*/
|
|
|
|
#define F1(x) 1
|
|
|
|
#define INST_SUPPORTS_DEP_ORDS_CNT(n) \
|
|
|
|
(FOR_EACH(F1, (+), DT_INST_SUPPORTS_DEP_ORDS(n)) - 1)
|
|
|
|
|
2021-03-28 23:04:43 +08:00
|
|
|
#define SHARED_IRQ_CONFIG_FUNC(n) \
|
|
|
|
void shared_irq_config_func_##n(void) \
|
|
|
|
{ \
|
|
|
|
IRQ_CONNECT(DT_INST_IRQN(n), \
|
|
|
|
DT_INST_IRQ(n, priority), \
|
|
|
|
shared_irq_isr, \
|
|
|
|
DEVICE_DT_INST_GET(n), \
|
2021-03-29 00:13:37 +08:00
|
|
|
COND_CODE_1(DT_INST_IRQ_HAS_CELL(n, sense), \
|
|
|
|
(DT_INST_IRQ(n, sense)), \
|
|
|
|
(0))); \
|
2015-09-29 23:42:22 +08:00
|
|
|
}
|
|
|
|
|
2021-03-28 23:04:43 +08:00
|
|
|
#define SHARED_IRQ_INIT(n) \
|
|
|
|
SHARED_IRQ_CONFIG_FUNC(n) \
|
2021-03-29 00:07:56 +08:00
|
|
|
struct shared_irq_client clients_##n[INST_SUPPORTS_DEP_ORDS_CNT(n)]; \
|
|
|
|
struct shared_irq_runtime shared_irq_data_##n = { \
|
|
|
|
.client = clients_##n \
|
|
|
|
}; \
|
2021-03-28 23:04:43 +08:00
|
|
|
\
|
|
|
|
const struct shared_irq_config shared_irq_config_##n = { \
|
|
|
|
.irq_num = DT_INST_IRQN(n), \
|
2021-03-28 23:27:36 +08:00
|
|
|
.client_count = INST_SUPPORTS_DEP_ORDS_CNT(n), \
|
2021-03-28 23:04:43 +08:00
|
|
|
.config = shared_irq_config_func_##n \
|
|
|
|
}; \
|
|
|
|
DEVICE_DT_INST_DEFINE(n, shared_irq_initialize, \
|
|
|
|
NULL, \
|
|
|
|
&shared_irq_data_##n, \
|
|
|
|
&shared_irq_config_##n, POST_KERNEL, \
|
|
|
|
CONFIG_SHARED_IRQ_INIT_PRIORITY, \
|
|
|
|
&api_funcs);
|
|
|
|
|
|
|
|
DT_INST_FOREACH_STATUS_OKAY(SHARED_IRQ_INIT)
|