Dirk Brandewie | 002c53b | 2015-09-29 08:42:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Intel Corporation. |
| 3 | * |
Javier B Perez Hernandez | f7fffae | 2015-10-06 11:00:37 -0500 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Dirk Brandewie | 002c53b | 2015-09-29 08:42:22 -0700 | [diff] [blame] | 7 | * |
Javier B Perez Hernandez | f7fffae | 2015-10-06 11:00:37 -0500 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Dirk Brandewie | 002c53b | 2015-09-29 08:42:22 -0700 | [diff] [blame] | 9 | * |
Javier B Perez Hernandez | f7fffae | 2015-10-06 11:00:37 -0500 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Dirk Brandewie | 002c53b | 2015-09-29 08:42:22 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <nanokernel.h> |
| 18 | #include <device.h> |
| 19 | #include <shared_irq.h> |
| 20 | #include <init.h> |
| 21 | #include <board.h> |
| 22 | #include <sys_io.h> |
| 23 | |
Daniel Leung | 8effb4f | 2015-12-10 09:52:56 -0800 | [diff] [blame^] | 24 | #ifdef CONFIG_IOAPIC |
| 25 | #include <drivers/ioapic.h> |
| 26 | #endif |
| 27 | |
Dirk Brandewie | 002c53b | 2015-09-29 08:42:22 -0700 | [diff] [blame] | 28 | /** |
| 29 | * @brief Register a device ISR |
| 30 | * @param dev Pointer to device structure for SHARED_IRQ driver instance. |
| 31 | * @param isr_func Pointer to the ISR function for the device. |
| 32 | * @param isr_dev Pointer to the device that will service the interrupt. |
| 33 | */ |
| 34 | static int isr_register(struct device *dev, isr_t isr_func, |
| 35 | struct device *isr_dev) |
| 36 | { |
| 37 | struct shared_irq_runtime *clients = dev->driver_data; |
| 38 | struct shared_irq_config *config = dev->config->config_info; |
| 39 | uint32_t i; |
| 40 | |
| 41 | for (i = 0; i < config->client_count; i++) { |
| 42 | if (!clients->client[i].isr_dev) { |
| 43 | clients->client[i].isr_dev = isr_dev; |
| 44 | clients->client[i].isr_func = isr_func; |
| 45 | return DEV_OK; |
| 46 | } |
| 47 | } |
| 48 | return DEV_FAIL; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @brief Enable ISR for device |
| 53 | * @param dev Pointer to device structure for SHARED_IRQ driver instance. |
| 54 | * @param isr_dev Pointer to the device that will service the interrupt. |
| 55 | */ |
| 56 | static inline int enable(struct device *dev, struct device *isr_dev) |
| 57 | { |
| 58 | struct shared_irq_runtime *clients = dev->driver_data; |
| 59 | struct shared_irq_config *config = dev->config->config_info; |
| 60 | uint32_t i; |
| 61 | |
| 62 | for (i = 0; i < config->client_count; i++) { |
| 63 | if (clients->client[i].isr_dev == isr_dev) { |
| 64 | clients->client[i].enabled = 1; |
| 65 | irq_enable(config->irq_num); |
| 66 | return DEV_OK; |
| 67 | } |
| 68 | } |
| 69 | return DEV_FAIL; |
| 70 | } |
| 71 | |
| 72 | static int last_enabled_isr(struct shared_irq_runtime *clients, int count) |
| 73 | { |
| 74 | uint32_t i; |
| 75 | |
| 76 | for (i = 0; i < count; i++) { |
| 77 | if (clients->client[i].enabled) { |
| 78 | return 0; |
| 79 | } |
| 80 | } |
| 81 | return 1; |
| 82 | } |
| 83 | /** |
| 84 | * @brief Disable ISR for device |
| 85 | * @param dev Pointer to device structure for SHARED_IRQ driver instance. |
| 86 | * @param isr_dev Pointer to the device that will service the interrupt. |
| 87 | */ |
| 88 | static inline int disable(struct device *dev, struct device *isr_dev) |
| 89 | { |
| 90 | struct shared_irq_runtime *clients = dev->driver_data; |
| 91 | struct shared_irq_config *config = dev->config->config_info; |
| 92 | uint32_t i; |
| 93 | |
| 94 | for (i = 0; i < config->client_count; i++) { |
| 95 | if (clients->client[i].isr_dev == isr_dev) { |
| 96 | clients->client[i].enabled = 0; |
| 97 | if (last_enabled_isr(clients, config->client_count)) { |
| 98 | irq_disable(config->irq_num); |
| 99 | } |
| 100 | return DEV_OK; |
| 101 | } |
| 102 | } |
| 103 | return DEV_FAIL; |
| 104 | } |
| 105 | |
| 106 | void shared_irq_isr(struct device *dev) |
| 107 | { |
| 108 | struct shared_irq_runtime *clients = dev->driver_data; |
| 109 | struct shared_irq_config *config = dev->config->config_info; |
| 110 | uint32_t i; |
| 111 | |
| 112 | for (i = 0; i < config->client_count; i++) { |
| 113 | if (clients->client[i].isr_dev) { |
| 114 | clients->client[i].isr_func(clients->client[i].isr_dev); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | static struct shared_irq_driver_api api_funcs = { |
| 120 | .isr_register = isr_register, |
| 121 | .enable = enable, |
| 122 | .disable = disable, |
| 123 | }; |
| 124 | |
| 125 | |
| 126 | int shared_irq_initialize(struct device *dev) |
| 127 | { |
| 128 | struct shared_irq_config *config = dev->config->config_info; |
| 129 | |
| 130 | dev->driver_api = &api_funcs; |
| 131 | config->config(dev); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | #if CONFIG_SHARED_IRQ_0 |
| 137 | void shared_irq_config_0_irq(struct device *port); |
| 138 | |
| 139 | struct shared_irq_config shared_irq_config_0 = { |
| 140 | .irq_num = CONFIG_SHARED_IRQ_0_IRQ, |
| 141 | .client_count = CONFIG_SHARED_IRQ_NUM_CLIENTS, |
| 142 | .config = shared_irq_config_0_irq |
| 143 | }; |
| 144 | |
| 145 | struct shared_irq_runtime shared_irq_0_runtime; |
| 146 | |
| 147 | DECLARE_DEVICE_INIT_CONFIG(shared_irq_0, CONFIG_SHARED_IRQ_0_NAME, |
| 148 | shared_irq_initialize, &shared_irq_config_0); |
Dmitriy Korovkin | 57f2741 | 2015-10-26 15:56:02 -0400 | [diff] [blame] | 149 | SYS_DEFINE_DEVICE(shared_irq_0, &shared_irq_0_runtime, SECONDARY, |
| 150 | CONFIG_SHARED_IRQ_INIT_PRIORITY); |
Dirk Brandewie | 002c53b | 2015-09-29 08:42:22 -0700 | [diff] [blame] | 151 | |
Daniel Leung | 8effb4f | 2015-12-10 09:52:56 -0800 | [diff] [blame^] | 152 | #if defined(CONFIG_IOAPIC) |
| 153 | #if defined(CONFIG_SHARED_IRQ_0) |
| 154 | #if defined(CONFIG_SHARED_IRQ_0_FALLING_EDGE) |
| 155 | #define SHARED_IRQ_0_FLAGS (IOAPIC_EDGE | IOAPIC_LOW) |
| 156 | #elif defined(CONFIG_SHARED_IRQ_0_RISING_EDGE) |
| 157 | #define SHARED_IRQ_0_FLAGS (IOAPIC_EDGE | IOAPIC_HIGH) |
| 158 | #elif defined(CONFIG_SHARED_IRQ_0_LEVEL_HIGH) |
| 159 | #define SHARED_IRQ_0_FLAGS (IOAPIC_LEVEL | IOAPIC_HIGH) |
| 160 | #elif defined(CONFIG_SHARED_IRQ_0_LEVEL_LOW) |
| 161 | #define SHARED_IRQ_0_FLAGS (IOAPIC_LEVEL | IOAPIC_LOW) |
| 162 | #endif |
| 163 | #endif /* CONFIG_SHARED_IRQ_0 */ |
| 164 | #else |
| 165 | #define SHARED_IRQ_0_FLAGS 0 |
| 166 | #endif /* CONFIG_IOAPIC */ |
| 167 | |
Dirk Brandewie | 002c53b | 2015-09-29 08:42:22 -0700 | [diff] [blame] | 168 | IRQ_CONNECT_STATIC(shared_irq_0, CONFIG_SHARED_IRQ_0_IRQ, |
Dmitriy Korovkin | f142051 | 2015-11-02 18:06:08 -0500 | [diff] [blame] | 169 | CONFIG_SHARED_IRQ_0_PRI, shared_irq_isr_0, 0, |
| 170 | SHARED_IRQ_0_FLAGS); |
Dirk Brandewie | 002c53b | 2015-09-29 08:42:22 -0700 | [diff] [blame] | 171 | |
| 172 | void shared_irq_config_0_irq(struct device *port) |
| 173 | { |
| 174 | struct shared_irq_config *config = port->config->config_info; |
| 175 | |
Juan Manuel Cruz | bc1a79c | 2015-11-30 11:21:13 -0600 | [diff] [blame] | 176 | IRQ_CONFIG(shared_irq_0, config->irq_num); |
Dirk Brandewie | 002c53b | 2015-09-29 08:42:22 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void shared_irq_isr_0(void *unused) |
| 180 | { |
Allan Stephens | 7b00606 | 2015-10-14 09:17:38 -0400 | [diff] [blame] | 181 | shared_irq_isr(&__initconfig_shared_irq_0); |
Dirk Brandewie | 002c53b | 2015-09-29 08:42:22 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | #endif /* CONFIG_SHARED_IRQ_0 */ |
| 185 | |
| 186 | #if CONFIG_SHARED_IRQ_1 |
| 187 | void shared_irq_config_1_irq(struct device *port); |
| 188 | |
| 189 | struct shared_irq_config shared_irq_config_1 = { |
| 190 | .irq_num = CONFIG_SHARED_IRQ_1_IRQ, |
| 191 | .client_count = CONFIG_SHARED_IRQ_NUM_CLIENTS, |
| 192 | .config = shared_irq_config_1_irq |
| 193 | }; |
| 194 | |
| 195 | struct shared_irq_runtime shared_irq_1_runtime; |
| 196 | |
| 197 | DECLARE_DEVICE_INIT_CONFIG(shared_irq_1, CONFIG_SHARED_IRQ_1_NAME, |
| 198 | shared_irq_initialize, &shared_irq_config_1); |
Dmitriy Korovkin | 57f2741 | 2015-10-26 15:56:02 -0400 | [diff] [blame] | 199 | SYS_DEFINE_DEVICE(shared_irq_1, &shared_irq_1_runtime, SECONDARY, |
| 200 | CONFIG_SHARED_IRQ_INIT_PRIORITY); |
Dirk Brandewie | 002c53b | 2015-09-29 08:42:22 -0700 | [diff] [blame] | 201 | |
Daniel Leung | 8effb4f | 2015-12-10 09:52:56 -0800 | [diff] [blame^] | 202 | #if defined(CONFIG_IOAPIC) |
| 203 | #if defined(CONFIG_SHARED_IRQ_1) |
| 204 | #if defined(CONFIG_SHARED_IRQ_1_FALLING_EDGE) |
| 205 | #define SHARED_IRQ_1_FLAGS (IOAPIC_EDGE | IOAPIC_LOW) |
| 206 | #elif defined(CONFIG_SHARED_IRQ_1_RISING_EDGE) |
| 207 | #define SHARED_IRQ_1_FLAGS (IOAPIC_EDGE | IOAPIC_HIGH) |
| 208 | #elif defined(CONFIG_SHARED_IRQ_1_LEVEL_HIGH) |
| 209 | #define SHARED_IRQ_1_FLAGS (IOAPIC_LEVEL | IOAPIC_HIGH) |
| 210 | #elif defined(CONFIG_SHARED_IRQ_1_LEVEL_LOW) |
| 211 | #define SHARED_IRQ_1_FLAGS (IOAPIC_LEVEL | IOAPIC_LOW) |
| 212 | #endif |
| 213 | #endif /* CONFIG_SHARED_IRQ_1 */ |
| 214 | #else |
| 215 | #define SHARED_IRQ_1_FLAGS 0 |
| 216 | #endif /* CONFIG_IOAPIC */ |
| 217 | |
Dirk Brandewie | 002c53b | 2015-09-29 08:42:22 -0700 | [diff] [blame] | 218 | IRQ_CONNECT_STATIC(shared_irq_1, CONFIG_SHARED_IRQ_1_IRQ, |
Dmitriy Korovkin | f142051 | 2015-11-02 18:06:08 -0500 | [diff] [blame] | 219 | CONFIG_SHARED_IRQ_1_PRI, shared_irq_isr_1, 0, |
| 220 | SHARED_IRQ_1_FLAGS); |
Dirk Brandewie | 002c53b | 2015-09-29 08:42:22 -0700 | [diff] [blame] | 221 | |
| 222 | void shared_irq_config_1_irq(struct device *port) |
| 223 | { |
| 224 | struct shared_irq_config *config = port->config->config_info; |
| 225 | |
Juan Manuel Cruz | bc1a79c | 2015-11-30 11:21:13 -0600 | [diff] [blame] | 226 | IRQ_CONFIG(shared_irq_1, config->irq_num); |
Dirk Brandewie | 002c53b | 2015-09-29 08:42:22 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void shared_irq_isr_1(void *unused) |
| 230 | { |
Allan Stephens | 7b00606 | 2015-10-14 09:17:38 -0400 | [diff] [blame] | 231 | shared_irq_isr(&__initconfig_shared_irq_1); |
Dirk Brandewie | 002c53b | 2015-09-29 08:42:22 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | #endif /* CONFIG_SHARED_IRQ_1 */ |
| 235 | |