Loading...
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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | /* * Copyright (c) 2015 Intel Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @file Driver for the MMIO-based GPIO driver. */ #include <errno.h> #include <nanokernel.h> #include <gpio.h> #include "gpio_mmio.h" #if defined(CONFIG_GPIO_MMIO_0_ACCESS_MM) \ || defined(CONFIG_GPIO_MMIO_1_ACCESS_MM) static uint32_t _mm_set_bit(uint32_t addr, uint32_t bit, uint32_t value) { if (!value) { sys_clear_bit(addr, bit); } else { sys_set_bit(addr, bit); } return 0; } static uint32_t _mm_read(uint32_t addr, uint32_t bit, uint32_t value) { ARG_UNUSED(bit); ARG_UNUSED(value); return sys_read32(addr); } static uint32_t _mm_write(uint32_t addr, uint32_t bit, uint32_t value) { ARG_UNUSED(bit); sys_write32(value, addr); return 0; } #endif /* if direct memory access is needed */ #if defined(CONFIG_GPIO_MMIO_0_ACCESS_IO) \ || defined(CONFIG_GPIO_MMIO_1_ACCESS_IO) static uint32_t _io_set_bit(uint32_t addr, uint32_t bit, uint32_t value) { if (!value) { sys_io_clear_bit(addr, bit); } else { sys_io_set_bit(addr, bit); } return 0; } static uint32_t _io_read(uint32_t addr, uint32_t bit, uint32_t value) { ARG_UNUSED(bit); ARG_UNUSED(value); return sys_in32(addr); } static uint32_t _io_write(uint32_t addr, uint32_t bit, uint32_t value) { ARG_UNUSED(bit); sys_out32(value, addr); return 0; } #endif /* if io port access is needed */ /** * @brief Configurate pin or port * * @param dev Device struct * @param access_op Access operation (pin or port) * @param pin The pin number * @param flags Flags of pin or port * * @return 0 if successful, failed otherwise */ static int gpio_mmio_config(struct device *dev, int access_op, uint32_t pin, int flags) { const struct gpio_mmio_config * const cfg = dev->config->config_info; uint32_t value = 0; /* Setup direction register */ if (!cfg->reg.dir) { return -EINVAL; } if (cfg->cfg_flags & GPIO_MMIO_CFG_DIR_MASK) { /* Direction register is inverse * INV: 0 - pin is input, 1 - pin is output */ if ((flags & GPIO_DIR_MASK) == GPIO_DIR_IN) { value = 0x0; } else { value = 0xFFFFFFFF; } } else { /* Direction register is normal. * NORMAL: 0 - pin is output, 1 - pin is input */ if ((flags & GPIO_DIR_MASK) == GPIO_DIR_IN) { value = 0xFFFFFFFF; } else { value = 0x0; } } switch (access_op) { case GPIO_ACCESS_BY_PIN: cfg->access.set_bit(cfg->reg.dir, pin, value); break; case GPIO_ACCESS_BY_PORT: cfg->access.write(cfg->reg.dir, 0, value); break; default: return -ENOTSUP; } /* * Enable the GPIO pin(s), since the direction is * also being setup. This indicates pin(s) is being used. * * This is not really necessary, so don't fail if * register is not defined. */ if (!cfg->reg.en) { return 0; } if (cfg->cfg_flags & GPIO_MMIO_CFG_EN_MASK) { /* INV: 0 - enable, 1 - disable */ value = 0x0; } else { /* NORMAL: 0 - disable, 1 - enable */ value = 0xFFFFFFFF; } switch (access_op) { case GPIO_ACCESS_BY_PIN: cfg->access.set_bit(cfg->reg.en, pin, value); break; case GPIO_ACCESS_BY_PORT: cfg->access.write(cfg->reg.en, 0, value); break; default: return -ENOTSUP; } return 0; } /** * @brief Set the pin or port output * * @param dev Device struct * @param access_op Access operation (pin or port) * @param pin The pin number * @param value Value to set (0 or 1) * * @return 0 if successful, failed otherwise */ static int gpio_mmio_write(struct device *dev, int access_op, uint32_t pin, uint32_t value) { const struct gpio_mmio_config * const cfg = dev->config->config_info; if (!cfg->reg.output) { return -EINVAL; } switch (access_op) { case GPIO_ACCESS_BY_PIN: cfg->access.set_bit(cfg->reg.output, pin, value); break; case GPIO_ACCESS_BY_PORT: cfg->access.write(cfg->reg.output, 0, value); break; default: return -ENOTSUP; } return 0; } /** * @brief Read the pin or port status * * @param dev Device struct * @param access_op Access operation (pin or port) * @param pin The pin number * @param value Value of input pin(s) * * @return 0 if successful, failed otherwise */ static int gpio_mmio_read(struct device *dev, int access_op, uint32_t pin, uint32_t *value) { const struct gpio_mmio_config * const cfg = dev->config->config_info; if (!cfg->reg.input) { return -EINVAL; } switch (access_op) { case GPIO_ACCESS_BY_PIN: *value = cfg->access.read(cfg->reg.input, 0, 0); *value &= BIT(pin) >> pin; break; case GPIO_ACCESS_BY_PORT: *value = cfg->access.read(cfg->reg.input, 0, 0); break; default: return -ENOTSUP; } return 0; } static int gpio_mmio_manage_callback(struct device *dev, struct gpio_callback *callback, bool set) { ARG_UNUSED(dev); ARG_UNUSED(callback); ARG_UNUSED(set); return -ENOTSUP; } static int gpio_mmio_enable_callback(struct device *dev, int access_op, uint32_t pin) { ARG_UNUSED(dev); ARG_UNUSED(access_op); ARG_UNUSED(pin); return -ENOTSUP; } static int gpio_mmio_disable_callback(struct device *dev, int access_op, uint32_t pin) { ARG_UNUSED(dev); ARG_UNUSED(access_op); ARG_UNUSED(pin); return -ENOTSUP; } static struct gpio_driver_api gpio_mmio_drv_api_funcs = { .config = gpio_mmio_config, .write = gpio_mmio_write, .read = gpio_mmio_read, .manage_callback = gpio_mmio_manage_callback, .enable_callback = gpio_mmio_enable_callback, .disable_callback = gpio_mmio_disable_callback, }; /** * @brief Initialization function of MMIO * * @param dev Device struct * @return 0 if successful, failed otherwise. */ int gpio_mmio_init(struct device *dev) { dev->driver_api = &gpio_mmio_drv_api_funcs; return 0; } /* Initialization for MMIO_0 */ #ifdef CONFIG_GPIO_MMIO_0 #include <device.h> #include <init.h> static struct gpio_mmio_config gpio_mmio_0_cfg = { .cfg_flags = CONFIG_GPIO_MMIO_0_CFG, .reg.en = CONFIG_GPIO_MMIO_0_EN, .reg.dir = CONFIG_GPIO_MMIO_0_DIR, .reg.input = CONFIG_GPIO_MMIO_0_INPUT, .reg.output = CONFIG_GPIO_MMIO_0_OUTPUT, #ifdef CONFIG_GPIO_MMIO_0_ACCESS_IO .access.set_bit = _io_set_bit, .access.read = _io_read, .access.write = _io_write, #else /* below is for CONFIG_GPIO_MMIO_0_ACCESS_MM=y or else */ .access.set_bit = _mm_set_bit, .access.read = _mm_read, .access.write = _mm_write, #endif }; DEVICE_INIT(gpio_mmio_0, CONFIG_GPIO_MMIO_0_DEV_NAME, gpio_mmio_init, NULL, &gpio_mmio_0_cfg, SECONDARY, CONFIG_GPIO_MMIO_INIT_PRIORITY); #endif /* CONFIG_GPIO_MMIO_0 */ /* Initialization for MMIO_1 */ #ifdef CONFIG_GPIO_MMIO_1 #include <device.h> #include <init.h> static struct gpio_mmio_config gpio_mmio_1_cfg = { .cfg_flags = CONFIG_GPIO_MMIO_0_CFG, .reg.en = CONFIG_GPIO_MMIO_1_EN, .reg.dir = CONFIG_GPIO_MMIO_1_DIR, .reg.input = CONFIG_GPIO_MMIO_1_INPUT, .reg.output = CONFIG_GPIO_MMIO_1_OUTPUT, #ifdef CONFIG_GPIO_MMIO_1_ACCESS_IO .access.set_bit = _io_set_bit, .access.read = _io_read, .access.write = _io_write, #else /* below is for CONFIG_GPIO_MMIO_1_ACCESS_MM=y or else */ .access.set_bit = _mm_set_bit, .access.read = _mm_read, .access.write = _mm_write, #endif }; DEVICE_INIT(gpio_mmio_1, CONFIG_GPIO_MMIO_1_DEV_NAME, gpio_mmio_init, NULL, &gpio_mmio_1_cfg, SECONDARY, CONFIG_GPIO_MMIO_INIT_PRIORITY); #endif /* CONFIG_GPIO_MMIO_1 */ |