forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared_irq.c
202 lines (171 loc) · 5.3 KB
/
shared_irq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright (c) 2015 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <kernel.h>
#include <device.h>
#include <shared_irq.h>
#include <init.h>
#include <board.h>
#include <sys_io.h>
#ifdef CONFIG_IOAPIC
#include <drivers/ioapic.h>
#endif
/**
* @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.
*/
static int isr_register(struct device *dev, isr_t isr_func,
struct device *isr_dev)
{
struct shared_irq_runtime *clients = dev->driver_data;
const struct shared_irq_config *config = dev->config->config_info;
u32_t i;
for (i = 0; i < config->client_count; i++) {
if (!clients->client[i].isr_dev) {
clients->client[i].isr_dev = isr_dev;
clients->client[i].isr_func = isr_func;
return 0;
}
}
return -EIO;
}
/**
* @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.
*/
static inline int enable(struct device *dev, struct device *isr_dev)
{
struct shared_irq_runtime *clients = dev->driver_data;
const struct shared_irq_config *config = dev->config->config_info;
u32_t i;
for (i = 0; i < config->client_count; i++) {
if (clients->client[i].isr_dev == isr_dev) {
clients->client[i].enabled = 1;
irq_enable(config->irq_num);
return 0;
}
}
return -EIO;
}
static int last_enabled_isr(struct shared_irq_runtime *clients, int count)
{
u32_t i;
for (i = 0; i < count; i++) {
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.
*/
static inline int disable(struct device *dev, struct device *isr_dev)
{
struct shared_irq_runtime *clients = dev->driver_data;
const struct shared_irq_config *config = dev->config->config_info;
u32_t i;
for (i = 0; i < config->client_count; i++) {
if (clients->client[i].isr_dev == isr_dev) {
clients->client[i].enabled = 0;
if (last_enabled_isr(clients, config->client_count)) {
irq_disable(config->irq_num);
}
return 0;
}
}
return -EIO;
}
void shared_irq_isr(struct device *dev)
{
struct shared_irq_runtime *clients = dev->driver_data;
const struct shared_irq_config *config = dev->config->config_info;
u32_t i;
for (i = 0; i < config->client_count; i++) {
if (clients->client[i].isr_dev) {
clients->client[i].isr_func(clients->client[i].isr_dev);
}
}
}
static const struct shared_irq_driver_api api_funcs = {
.isr_register = isr_register,
.enable = enable,
.disable = disable,
};
int shared_irq_initialize(struct device *dev)
{
const struct shared_irq_config *config = dev->config->config_info;
dev->driver_api = &api_funcs;
config->config();
return 0;
}
#if CONFIG_SHARED_IRQ_0
void shared_irq_config_0_irq(void);
const struct shared_irq_config shared_irq_config_0 = {
.irq_num = CONFIG_SHARED_IRQ_0_IRQ,
.client_count = CONFIG_SHARED_IRQ_NUM_CLIENTS,
.config = shared_irq_config_0_irq
};
struct shared_irq_runtime shared_irq_0_runtime;
DEVICE_INIT(shared_irq_0, CONFIG_SHARED_IRQ_0_NAME, shared_irq_initialize,
&shared_irq_0_runtime, &shared_irq_config_0,
POST_KERNEL, CONFIG_SHARED_IRQ_INIT_PRIORITY);
#if defined(CONFIG_IOAPIC)
#if defined(CONFIG_SHARED_IRQ_0_FALLING_EDGE)
#define SHARED_IRQ_0_FLAGS (IOAPIC_EDGE | IOAPIC_LOW)
#elif defined(CONFIG_SHARED_IRQ_0_RISING_EDGE)
#define SHARED_IRQ_0_FLAGS (IOAPIC_EDGE | IOAPIC_HIGH)
#elif defined(CONFIG_SHARED_IRQ_0_LEVEL_HIGH)
#define SHARED_IRQ_0_FLAGS (IOAPIC_LEVEL | IOAPIC_HIGH)
#elif defined(CONFIG_SHARED_IRQ_0_LEVEL_LOW)
#define SHARED_IRQ_0_FLAGS (IOAPIC_LEVEL | IOAPIC_LOW)
#endif
#else
#define SHARED_IRQ_0_FLAGS 0
#endif /* CONFIG_IOAPIC */
void shared_irq_config_0_irq(void)
{
IRQ_CONNECT(CONFIG_SHARED_IRQ_0_IRQ, CONFIG_SHARED_IRQ_0_PRI,
shared_irq_isr, DEVICE_GET(shared_irq_0),
SHARED_IRQ_0_FLAGS);
}
#endif /* CONFIG_SHARED_IRQ_0 */
#if CONFIG_SHARED_IRQ_1
void shared_irq_config_1_irq(void);
const struct shared_irq_config shared_irq_config_1 = {
.irq_num = CONFIG_SHARED_IRQ_1_IRQ,
.client_count = CONFIG_SHARED_IRQ_NUM_CLIENTS,
.config = shared_irq_config_1_irq
};
struct shared_irq_runtime shared_irq_1_runtime;
DEVICE_INIT(shared_irq_1, CONFIG_SHARED_IRQ_1_NAME, shared_irq_initialize,
&shared_irq_1_runtime, &shared_irq_config_1,
POST_KERNEL, CONFIG_SHARED_IRQ_INIT_PRIORITY);
#if defined(CONFIG_IOAPIC)
#if defined(CONFIG_SHARED_IRQ_1_FALLING_EDGE)
#define SHARED_IRQ_1_FLAGS (IOAPIC_EDGE | IOAPIC_LOW)
#elif defined(CONFIG_SHARED_IRQ_1_RISING_EDGE)
#define SHARED_IRQ_1_FLAGS (IOAPIC_EDGE | IOAPIC_HIGH)
#elif defined(CONFIG_SHARED_IRQ_1_LEVEL_HIGH)
#define SHARED_IRQ_1_FLAGS (IOAPIC_LEVEL | IOAPIC_HIGH)
#elif defined(CONFIG_SHARED_IRQ_1_LEVEL_LOW)
#define SHARED_IRQ_1_FLAGS (IOAPIC_LEVEL | IOAPIC_LOW)
#endif
#else
#define SHARED_IRQ_1_FLAGS 0
#endif /* CONFIG_IOAPIC */
void shared_irq_config_1_irq(void)
{
IRQ_CONNECT(CONFIG_SHARED_IRQ_1_IRQ, CONFIG_SHARED_IRQ_1_PRI,
shared_irq_isr, DEVICE_GET(shared_irq_1),
SHARED_IRQ_1_FLAGS);
}
#endif /* CONFIG_SHARED_IRQ_1 */