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 363 364 365 366 367 368 369 370 | /* * Copyright (c) 2021 Andes Technology Corporation * * SPDX-License-Identifier: Apache-2.0 */ /** * @file GPIO driver for the AndesTech ATCGPIO100 controller */ #include <errno.h> #include <stdbool.h> #include <kernel.h> #include <device.h> #include <soc.h> #include <drivers/gpio.h> #include <sys/util.h> #include <sys/sys_io.h> #include "gpio_utils.h" #define DT_DRV_COMPAT andestech_atcgpio100 /* Andes ATCGPIO100 register definition */ #define REG_IDR 0x00 /* ID and Revision reg. */ #define REG_CFG 0x10 /* Hardware configure reg. */ #define REG_DIN 0x20 /* Data In reg. */ #define REG_DOUT 0x24 /* Data Out reg. */ #define REG_DIR 0x28 /* Channel direction reg. */ #define REG_DCLR 0x2C /* Data out clear reg. */ #define REG_DSET 0x30 /* Data out set reg. */ #define REG_PUEN 0x40 /* Pull enable reg. */ #define REG_PTYP 0x44 /* Pull type reg. */ #define REG_INTE 0x50 /* Interrupt enable reg. */ #define REG_IMD0 0x54 /* Interrupt mode 0 ~ 7 reg. */ #define REG_IMD1 0x58 /* Interrupt mode 8 ~ 15 reg. */ #define REG_IMD2 0x5C /* Interrupt mode 16 ~ 23 reg. */ #define REG_IMD3 0x60 /* Interrupt mode 24 ~ 31 reg. */ #define REG_ISTA 0x64 /* Interrupt status reg. */ #define REG_DEBE 0x70 /* De-bounce enable reg. */ #define REG_DEBC 0x74 /* De-Bounce control reg. */ #define INT_NO_OPERATION 0x0 #define INT_HIGH_LEVEL 0x2 #define INT_LOW_LEVEL 0x3 #define INT_NEGATIVE_EDGE 0x5 #define INT_POSITIVE_EDGE 0x6 #define INT_DUAL_EDGE 0x7 #define PULL_CONFIGURED BIT(31) #define DEBOUNCE_CONFIGURED BIT(29) #define DF_DEBOUNCED_SETTING (0x80000003) #define DEV_CFG(dev) \ ((const struct gpio_atcgpio100_config * const)(dev)->config) #define DEV_DATA(dev) \ ((struct gpio_atcgpio100_data *)(dev)->data) #define GPIO_CFG(dev) (DEV_CFG(dev)->base + REG_CFG) #define GPIO_DIR(dev) (DEV_CFG(dev)->base + REG_DIR) #define GPIO_DIN(dev) (DEV_CFG(dev)->base + REG_DIN) #define GPIO_DOUT(dev) (DEV_CFG(dev)->base + REG_DOUT) #define GPIO_DCLR(dev) (DEV_CFG(dev)->base + REG_DCLR) #define GPIO_DSET(dev) (DEV_CFG(dev)->base + REG_DSET) #define GPIO_PUEN(dev) (DEV_CFG(dev)->base + REG_PUEN) #define GPIO_PTYP(dev) (DEV_CFG(dev)->base + REG_PTYP) #define GPIO_INTE(dev) (DEV_CFG(dev)->base + REG_INTE) #define GPIO_IMD(dev, idx) (DEV_CFG(dev)->base + REG_IMD0 + (idx * 4)) #define GPIO_ISTA(dev) (DEV_CFG(dev)->base + REG_ISTA) #define GPIO_DEBE(dev) (DEV_CFG(dev)->base + REG_DEBE) #define GPIO_DEBC(dev) (DEV_CFG(dev)->base + REG_DEBC) #define INWORD(x) sys_read32(x) #define OUTWORD(x, d) sys_write32(d, x) #define SET_GPIO_INT_MODE(cur_val, mode, ch_idx) \ do { \ cur_val &= ~(BIT_MASK(3) << (ch_idx * 4)); \ cur_val |= (mode << (ch_idx * 4)); \ } while (false) typedef void (*atcgpio100_cfg_func_t)(void); struct gpio_atcgpio100_config { /* gpio_driver_config needs to be first */ struct gpio_driver_config common; uint32_t base; uint32_t irq_num; atcgpio100_cfg_func_t cfg_func; }; struct gpio_atcgpio100_data { /* gpio_driver_data needs to be first */ struct gpio_driver_data common; /* list of callbacks */ sys_slist_t cb; struct k_spinlock lock; }; static int gpio_atcgpio100_config(const struct device *port, gpio_pin_t pin, gpio_flags_t flags) { struct gpio_atcgpio100_data * const data = DEV_DATA(port); uint32_t port_value, pin_mask, io_flags; k_spinlock_key_t key; /* Does not support disconnected pin, and * not supporting both input/output at same time. */ io_flags = flags & (GPIO_INPUT | GPIO_OUTPUT); if ((io_flags == GPIO_DISCONNECTED) || (io_flags == (GPIO_INPUT | GPIO_OUTPUT))) { return -ENOTSUP; } pin_mask = BIT(pin); if (flags & GPIO_OUTPUT) { if (flags & GPIO_OUTPUT_INIT_HIGH) { OUTWORD(GPIO_DSET(port), pin_mask); } else if (flags & GPIO_OUTPUT_INIT_LOW) { OUTWORD(GPIO_DCLR(port), pin_mask); } key = k_spin_lock(&data->lock); /* Set channel output */ port_value = INWORD(GPIO_DIR(port)); OUTWORD(GPIO_DIR(port), port_value | pin_mask); k_spin_unlock(&data->lock, key); } else if (flags & GPIO_INPUT) { if (flags & (GPIO_PULL_UP | GPIO_PULL_DOWN)) { return -ENOTSUP; } key = k_spin_lock(&data->lock); /* Set de-bounce */ if (flags & GPIO_INT_DEBOUNCE) { /* Default settings: Filter out pulses which are * less than 4 de-bounce clock period */ OUTWORD(GPIO_DEBC(port), DF_DEBOUNCED_SETTING); port_value = INWORD(GPIO_DEBE(port)); OUTWORD(GPIO_DEBE(port), port_value | pin_mask); } /* Set channel input */ port_value = INWORD(GPIO_DIR(port)); OUTWORD(GPIO_DIR(port), port_value & ~pin_mask); k_spin_unlock(&data->lock, key); } else { return -ENOTSUP; } return 0; } static int gpio_atcgpio100_port_get_raw(const struct device *port, gpio_port_value_t *value) { *value = INWORD(GPIO_DIN(port)); return 0; } static int gpio_atcgpio100_set_masked_raw(const struct device *port, gpio_port_pins_t mask, gpio_port_value_t value) { struct gpio_atcgpio100_data * const data = DEV_DATA(port); uint32_t port_value; k_spinlock_key_t key = k_spin_lock(&data->lock); port_value = INWORD(GPIO_DOUT(port)); OUTWORD(GPIO_DOUT(port), (port_value & ~mask) | (value & mask)); k_spin_unlock(&data->lock, key); return 0; } static int gpio_atcgpio100_set_bits_raw(const struct device *port, gpio_port_pins_t pins) { OUTWORD(GPIO_DSET(port), pins); return 0; } static int gpio_atcgpio100_clear_bits_raw(const struct device *port, gpio_port_pins_t pins) { OUTWORD(GPIO_DCLR(port), pins); return 0; } static int gpio_atcgpio100_toggle_bits(const struct device *port, gpio_port_pins_t pins) { struct gpio_atcgpio100_data * const data = DEV_DATA(port); uint32_t port_value; k_spinlock_key_t key = k_spin_lock(&data->lock); port_value = INWORD(GPIO_DOUT(port)); OUTWORD(GPIO_DOUT(port), port_value ^ pins); k_spin_unlock(&data->lock, key); return 0; } static int gpio_atcgpio100_pin_interrupt_configure( const struct device *port, gpio_pin_t pin, enum gpio_int_mode mode, enum gpio_int_trig trig) { struct gpio_atcgpio100_data * const data = DEV_DATA(port); uint32_t port_value, int_mode, imr_idx, ch_idx; k_spinlock_key_t key; switch (mode | trig) { case GPIO_INT_EDGE_BOTH: int_mode = INT_DUAL_EDGE; break; case GPIO_INT_EDGE_RISING: int_mode = INT_POSITIVE_EDGE; break; case GPIO_INT_EDGE_FALLING: int_mode = INT_NEGATIVE_EDGE; break; case GPIO_INT_LEVEL_LOW: int_mode = INT_LOW_LEVEL; break; case GPIO_INT_LEVEL_HIGH: int_mode = INT_HIGH_LEVEL; break; default: int_mode = INT_NO_OPERATION; break; } imr_idx = (pin / 8); ch_idx = (pin % 8); key = k_spin_lock(&data->lock); if (int_mode == INT_NO_OPERATION) { /* Disable interrupt of pin */ port_value = INWORD(GPIO_INTE(port)); OUTWORD(GPIO_INTE(port), port_value & ~BIT(pin)); /* Clear the remain pending interrupt */ port_value = INWORD(GPIO_ISTA(port)); OUTWORD(GPIO_ISTA(port), port_value); } else { /* Set interrupt mode of pin */ port_value = INWORD(GPIO_IMD(port, imr_idx)); SET_GPIO_INT_MODE(port_value, int_mode, ch_idx); OUTWORD(GPIO_IMD(port, imr_idx), port_value); /* Enable interrupt of pin */ port_value = INWORD(GPIO_INTE(port)); OUTWORD(GPIO_INTE(port), port_value | BIT(pin)); } k_spin_unlock(&data->lock, key); return 0; } static int gpio_atcgpio100_manage_callback(const struct device *port, struct gpio_callback *callback, bool set) { struct gpio_atcgpio100_data * const data = DEV_DATA(port); return gpio_manage_callback(&data->cb, callback, set); } static void gpio_atcgpio100_irq_handler(const struct device *port) { struct gpio_atcgpio100_data * const data = DEV_DATA(port); uint32_t port_value; port_value = INWORD(GPIO_ISTA(port)); OUTWORD(GPIO_ISTA(port), port_value); gpio_fire_callbacks(&data->cb, port, port_value); } static const struct gpio_driver_api gpio_atcgpio100_api = { .pin_configure = gpio_atcgpio100_config, .port_get_raw = gpio_atcgpio100_port_get_raw, .port_set_masked_raw = gpio_atcgpio100_set_masked_raw, .port_set_bits_raw = gpio_atcgpio100_set_bits_raw, .port_clear_bits_raw = gpio_atcgpio100_clear_bits_raw, .port_toggle_bits = gpio_atcgpio100_toggle_bits, .pin_interrupt_configure = gpio_atcgpio100_pin_interrupt_configure, .manage_callback = gpio_atcgpio100_manage_callback }; static int gpio_atcgpio100_init(const struct device *port) { const struct gpio_atcgpio100_config * const dev_cfg = DEV_CFG(port); /* Disable all interrupts */ OUTWORD(GPIO_INTE(port), BIT_MASK(0)); /* Write 1 to clear interrupt status */ OUTWORD(GPIO_ISTA(port), (uint32_t) BIT64_MASK(32)); /* Configure GPIO device */ dev_cfg->cfg_func(); /* Enable PLIC interrupt GPIO source */ irq_enable(dev_cfg->irq_num); return 0; } #define GPIO_ATCGPIO100_INIT(n) \ static void gpio_atcgpio100_cfg_func_##n(void); \ static struct gpio_atcgpio100_data gpio_atcgpio100_data_##n; \ \ static const struct gpio_atcgpio100_config \ gpio_atcgpio100_config_##n = { \ .common = { \ .port_pin_mask = \ GPIO_PORT_PIN_MASK_FROM_DT_INST(n), \ }, \ .base = DT_INST_REG_ADDR(n), \ .irq_num = DT_INST_IRQN(n), \ .cfg_func = gpio_atcgpio100_cfg_func_##n \ }; \ \ DEVICE_DT_INST_DEFINE(n, \ gpio_atcgpio100_init, \ NULL, \ &gpio_atcgpio100_data_##n, \ &gpio_atcgpio100_config_##n, \ POST_KERNEL, \ CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \ &gpio_atcgpio100_api); \ \ static void gpio_atcgpio100_cfg_func_##n(void) \ { \ IRQ_CONNECT(DT_INST_IRQN(n), \ DT_INST_IRQ(n, priority), \ gpio_atcgpio100_irq_handler, \ DEVICE_DT_INST_GET(n), \ 0); \ return; \ } \ DT_INST_FOREACH_STATUS_OKAY(GPIO_ATCGPIO100_INIT) |