paul@188 | 1 | /* |
paul@188 | 2 | * GPIO driver for Ingenic X1600. |
paul@188 | 3 | * (See below for additional copyright and licensing notices.) |
paul@188 | 4 | * |
paul@188 | 5 | * Copyright (C) 2017, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@188 | 6 | * |
paul@188 | 7 | * This program is free software; you can redistribute it and/or |
paul@188 | 8 | * modify it under the terms of the GNU General Public License as |
paul@188 | 9 | * published by the Free Software Foundation; either version 2 of |
paul@188 | 10 | * the License, or (at your option) any later version. |
paul@188 | 11 | * |
paul@188 | 12 | * This program is distributed in the hope that it will be useful, |
paul@188 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@188 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@188 | 15 | * GNU General Public License for more details. |
paul@188 | 16 | * |
paul@188 | 17 | * You should have received a copy of the GNU General Public License |
paul@188 | 18 | * along with this program; if not, write to the Free Software |
paul@188 | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@188 | 20 | * Boston, MA 02110-1301, USA |
paul@188 | 21 | * |
paul@188 | 22 | * |
paul@188 | 23 | * Subject to other copyrights, being derived from the bcm2835.cc and |
paul@188 | 24 | * omap.cc GPIO driver implementations. |
paul@188 | 25 | * |
paul@188 | 26 | * This file is part of TUD:OS and distributed under the terms of the |
paul@188 | 27 | * GNU General Public License 2. |
paul@188 | 28 | * Please see the COPYING-GPL-2 file for details. |
paul@188 | 29 | */ |
paul@188 | 30 | |
paul@188 | 31 | #include <l4/sys/icu.h> |
paul@188 | 32 | #include <l4/util/util.h> |
paul@188 | 33 | #include <l4/devices/hw_mmio_register_block.h> |
paul@188 | 34 | |
paul@188 | 35 | #include "gpio-x1600.h" |
paul@188 | 36 | |
paul@188 | 37 | /* |
paul@188 | 38 | GPIO register offsets (x in A..D). |
paul@188 | 39 | |
paul@188 | 40 | Register summary: |
paul@188 | 41 | |
paul@188 | 42 | PxINT 0 (function/GPIO) 1 (interrupt) |
paul@188 | 43 | PxMSK 0 (function) 1 (GPIO) 0 (IRQ enable)/1 (IRQ disable) |
paul@188 | 44 | PxPAT1 0 (function 0/1) 1 (function 2/3) 0 (output) 1 (input) 0 (level trigger) 1 (edge trigger) |
paul@188 | 45 | PxPAT0 0 (function 0) 0 (function 2) 0 (output value 0) 0 (low level) 0 (falling edge) |
paul@188 | 46 | 1 (function 1) 1 (function 3) 1 (output value 1) 1 (high level) 1 (rising edge) |
paul@188 | 47 | */ |
paul@188 | 48 | |
paul@188 | 49 | enum Regs |
paul@188 | 50 | { |
paul@188 | 51 | Pin_level = 0x000, // PxPINL (read-only) |
paul@188 | 52 | |
paul@188 | 53 | Port_int = 0x010, // PxINT |
paul@188 | 54 | Port_int_set = 0x014, // PxINTS |
paul@188 | 55 | Port_int_clear = 0x018, // PxINTC |
paul@188 | 56 | |
paul@188 | 57 | Irq_mask = 0x020, // PxMSK (for PxINT == 1) |
paul@188 | 58 | Irq_mask_set = 0x024, // PxMSKS |
paul@188 | 59 | Irq_mask_clear = 0x028, // PxMSKC |
paul@188 | 60 | Port_gpio = 0x020, // PxMSK (for PxINT == 0) |
paul@188 | 61 | Port_gpio_set = 0x024, // PxMSKS |
paul@188 | 62 | Port_gpio_clear = 0x028, // PxMSKC |
paul@188 | 63 | |
paul@188 | 64 | Port_trigger = 0x030, // PxPAT1 (for PxINT == 1) |
paul@188 | 65 | Port_trigger_set = 0x034, // PxPAT1S |
paul@188 | 66 | Port_trigger_clear = 0x038, // PxPAT1C |
paul@188 | 67 | Port_dir = 0x030, // PxPAT1 (for PxINT == 0, PxMSK == 1) |
paul@188 | 68 | Port_dir_set = 0x034, // PxPAT1S |
paul@188 | 69 | Port_dir_clear = 0x038, // PxPAT1C |
paul@188 | 70 | Port_group1 = 0x030, // PxPAT1 (for PxINT == 0, PxMSK == 0) |
paul@188 | 71 | Port_group1_set = 0x034, // PxPAT1S |
paul@188 | 72 | Port_group1_clear = 0x038, // PxPAT1C |
paul@188 | 73 | |
paul@188 | 74 | Port_level = 0x040, // PxPAT0 (for PxINT == 1) |
paul@188 | 75 | Port_level_set = 0x044, // PxPAT0S |
paul@188 | 76 | Port_level_clear = 0x048, // PxPAT0C |
paul@188 | 77 | Port_data = 0x040, // PxPAT0 (for PxINT == 0, PxMSK == 1, PxPAT1 == 0) |
paul@188 | 78 | Port_data_set = 0x044, // PxPAT0S |
paul@188 | 79 | Port_data_clear = 0x048, // PxPAT0C |
paul@188 | 80 | Port_group0 = 0x040, // PxPAT0 (for PxINT == 0, PxMSK == 0) |
paul@188 | 81 | Port_group0_set = 0x044, // PxPAT0S |
paul@188 | 82 | Port_group0_clear = 0x048, // PxPAT0C |
paul@188 | 83 | |
paul@188 | 84 | Irq_flag = 0x050, // PxFLG (read-only) |
paul@188 | 85 | Irq_flag_clear = 0x058, // PxFLGC |
paul@188 | 86 | |
paul@188 | 87 | // Only the following registers differ from the JZ4780. The dual-edge |
paul@188 | 88 | // registers being added to the X1600, with the pull-up/down registers being |
paul@189 | 89 | // relocated and their sense changed from disable to enable. |
paul@188 | 90 | |
paul@188 | 91 | Pull_edge = 0x070, // PxEDG |
paul@188 | 92 | Pull_edge_set = 0x074, // PxEDGS |
paul@188 | 93 | Pull_edge_clear = 0x078, // PxEDGC |
paul@188 | 94 | |
paul@189 | 95 | Pull_enable = 0x080, // PxPE |
paul@189 | 96 | Pull_enable_set = 0x084, // PxPES |
paul@189 | 97 | Pull_enable_clear = 0x088, // PxPEC |
paul@188 | 98 | |
paul@188 | 99 | // The shadow port Z is available at offset 0x700 and supports the INTS, INTC, |
paul@188 | 100 | // MSKS, MSKC, PAT1S, PAT1C, PAT0S, PAT0C registers, along with the following. |
paul@188 | 101 | |
paul@188 | 102 | Shadow_transfer = 0x0f0, // PzGID2LD |
paul@188 | 103 | }; |
paul@188 | 104 | |
paul@188 | 105 | |
paul@188 | 106 | |
paul@188 | 107 | // IRQ control for each GPIO pin. |
paul@188 | 108 | |
paul@270 | 109 | Gpio_x1600_irq_pin::Gpio_x1600_irq_pin(unsigned pin, Hw::Register_block<32> const ®s, |
paul@270 | 110 | Hw::Register_block<32> const &shadow_regs, |
paul@270 | 111 | uint8_t port_number) |
paul@270 | 112 | : _pin(pin), _regs(regs), _shadow_regs(shadow_regs), _port_number(port_number), |
paul@270 | 113 | _shadow(true) |
paul@270 | 114 | {} |
paul@270 | 115 | |
paul@188 | 116 | Gpio_x1600_irq_pin::Gpio_x1600_irq_pin(unsigned pin, Hw::Register_block<32> const ®s) |
paul@270 | 117 | : _pin(pin), _regs(regs), _shadow(false) |
paul@188 | 118 | {} |
paul@188 | 119 | |
paul@188 | 120 | void |
paul@188 | 121 | Gpio_x1600_irq_pin::write_reg_pin(unsigned reg) |
paul@188 | 122 | { |
paul@188 | 123 | // Write the pin bit to the register, setting or clearing the pin |
paul@188 | 124 | // depending on the register chosen. |
paul@188 | 125 | |
paul@270 | 126 | if (_shadow) |
paul@270 | 127 | _shadow_regs[reg] = _pin_bit(_pin); |
paul@270 | 128 | else |
paul@270 | 129 | _regs[reg] = _pin_bit(_pin); |
paul@188 | 130 | } |
paul@188 | 131 | |
paul@188 | 132 | void Gpio_x1600_irq_pin::do_mask() |
paul@188 | 133 | { |
paul@188 | 134 | // Set the interrupt bit in the PxIM register. |
paul@188 | 135 | |
paul@188 | 136 | write_reg_pin(Irq_mask_set); |
paul@188 | 137 | } |
paul@188 | 138 | |
paul@188 | 139 | void Gpio_x1600_irq_pin::do_unmask() |
paul@188 | 140 | { |
paul@188 | 141 | // Clear the interrupt bit in the PxIM register, first also clearing the |
paul@188 | 142 | // flag bit in the PxFLG register to allow interrupts to be delivered. |
paul@188 | 143 | |
paul@188 | 144 | write_reg_pin(Irq_flag_clear); |
paul@188 | 145 | write_reg_pin(Irq_mask_clear); |
paul@188 | 146 | } |
paul@188 | 147 | |
paul@188 | 148 | bool Gpio_x1600_irq_pin::do_set_mode(unsigned mode) |
paul@188 | 149 | { |
paul@188 | 150 | // Standard comment found for this method: |
paul@188 | 151 | // this operation touches multiple mmio registers and is thus |
paul@188 | 152 | // not atomic, that's why we first mask the IRQ and if it was |
paul@188 | 153 | // enabled we unmask it after we have changed the mode |
paul@188 | 154 | |
paul@270 | 155 | // The X1600 provides a special port Z that allows changes to be made and then |
paul@270 | 156 | // committed atomically using PzGID2LD. |
paul@188 | 157 | |
paul@270 | 158 | if (!_shadow && enabled()) |
paul@188 | 159 | do_mask(); |
paul@188 | 160 | |
paul@188 | 161 | // Do the PxINT, PxPAT1 and PxPAT0 configuration. |
paul@188 | 162 | |
paul@188 | 163 | switch(mode) |
paul@188 | 164 | { |
paul@188 | 165 | case L4_IRQ_F_LEVEL_HIGH: |
paul@188 | 166 | write_reg_pin(Port_int_set); |
paul@188 | 167 | write_reg_pin(Port_trigger_clear); |
paul@188 | 168 | write_reg_pin(Port_level_set); |
paul@188 | 169 | break; |
paul@188 | 170 | case L4_IRQ_F_LEVEL_LOW: |
paul@188 | 171 | write_reg_pin(Port_int_set); |
paul@188 | 172 | write_reg_pin(Port_trigger_clear); |
paul@188 | 173 | write_reg_pin(Port_level_clear); |
paul@188 | 174 | break; |
paul@188 | 175 | case L4_IRQ_F_POS_EDGE: |
paul@188 | 176 | write_reg_pin(Port_int_set); |
paul@188 | 177 | write_reg_pin(Port_trigger_set); |
paul@188 | 178 | write_reg_pin(Port_level_set); |
paul@188 | 179 | break; |
paul@188 | 180 | case L4_IRQ_F_NEG_EDGE: |
paul@188 | 181 | write_reg_pin(Port_int_set); |
paul@188 | 182 | write_reg_pin(Port_trigger_set); |
paul@188 | 183 | write_reg_pin(Port_level_clear); |
paul@188 | 184 | break; |
paul@188 | 185 | |
paul@188 | 186 | default: |
paul@188 | 187 | return false; |
paul@188 | 188 | } |
paul@188 | 189 | |
paul@270 | 190 | if (_shadow) |
paul@270 | 191 | _shadow_regs[Shadow_transfer] = _port_number; |
paul@270 | 192 | else if (enabled()) |
paul@188 | 193 | do_unmask(); |
paul@188 | 194 | |
paul@188 | 195 | return true; |
paul@188 | 196 | } |
paul@188 | 197 | |
paul@188 | 198 | int Gpio_x1600_irq_pin::clear() |
paul@188 | 199 | { |
paul@188 | 200 | // Obtain the flag status for the pin, clearing it if set. |
paul@188 | 201 | |
paul@188 | 202 | l4_uint32_t e = _regs[Irq_flag] & (1UL << _pin); |
paul@188 | 203 | if (e) |
paul@188 | 204 | _regs[Irq_flag_clear] = e; |
paul@188 | 205 | |
paul@188 | 206 | return (e >> _pin); |
paul@188 | 207 | } |
paul@188 | 208 | |
paul@188 | 209 | bool Gpio_x1600_irq_pin::enabled() |
paul@188 | 210 | { |
paul@188 | 211 | return true; |
paul@188 | 212 | } |
paul@188 | 213 | |
paul@188 | 214 | |
paul@188 | 215 | |
paul@188 | 216 | // Initialise the GPIO controller. |
paul@188 | 217 | |
paul@188 | 218 | Gpio_x1600_chip::Gpio_x1600_chip(l4_addr_t start, l4_addr_t end, |
paul@270 | 219 | unsigned nr_pins, |
paul@270 | 220 | l4_uint32_t pull_ups, l4_uint32_t pull_downs, |
paul@270 | 221 | l4_addr_t shadow_start, l4_addr_t shadow_end, |
paul@270 | 222 | uint8_t port_number) |
paul@188 | 223 | : _start(start), _end(end), |
paul@188 | 224 | _nr_pins(nr_pins), |
paul@270 | 225 | _pull_ups(pull_ups), _pull_downs(pull_downs), |
paul@270 | 226 | _shadow_start(shadow_start), _shadow_end(shadow_end), |
paul@270 | 227 | _port_number(port_number) |
paul@188 | 228 | { |
paul@188 | 229 | _regs = new Hw::Mmio_register_block<32>(_start); |
paul@270 | 230 | |
paul@270 | 231 | if (_shadow_start) |
paul@270 | 232 | { |
paul@270 | 233 | _shadow_regs = new Hw::Mmio_register_block<32>(_shadow_start); |
paul@270 | 234 | _shadow = true; |
paul@270 | 235 | } |
paul@270 | 236 | else |
paul@270 | 237 | _shadow = false; |
paul@270 | 238 | } |
paul@270 | 239 | |
paul@270 | 240 | void |
paul@270 | 241 | Gpio_x1600_chip::write_reg_pin(unsigned reg, unsigned pin) |
paul@270 | 242 | { |
paul@270 | 243 | // Write the pin bit to the register, setting or clearing the pin |
paul@270 | 244 | // depending on the register chosen. |
paul@270 | 245 | |
paul@270 | 246 | if (_shadow) |
paul@270 | 247 | _shadow_regs[reg] = _pin_bit(pin); |
paul@270 | 248 | else |
paul@270 | 249 | _regs[reg] = _pin_bit(pin); |
paul@188 | 250 | } |
paul@188 | 251 | |
paul@188 | 252 | // Return the value of a pin. |
paul@188 | 253 | |
paul@188 | 254 | int |
paul@188 | 255 | Gpio_x1600_chip::get(unsigned pin) |
paul@188 | 256 | { |
paul@188 | 257 | if (pin >= _nr_pins) |
paul@188 | 258 | throw -L4_EINVAL; |
paul@188 | 259 | |
paul@188 | 260 | l4_uint32_t val = _regs[Pin_level]; |
paul@188 | 261 | return (val >> _pin_shift(pin)) & 1; |
paul@188 | 262 | } |
paul@188 | 263 | |
paul@188 | 264 | // Return multiple pin values. |
paul@188 | 265 | |
paul@188 | 266 | unsigned |
paul@188 | 267 | Gpio_x1600_chip::multi_get(unsigned offset) |
paul@188 | 268 | { |
paul@188 | 269 | _reg_offset_check(offset); |
paul@188 | 270 | return _regs[Pin_level]; |
paul@188 | 271 | } |
paul@188 | 272 | |
paul@188 | 273 | // Set the value of a pin. |
paul@188 | 274 | |
paul@188 | 275 | void |
paul@188 | 276 | Gpio_x1600_chip::set(unsigned pin, int value) |
paul@188 | 277 | { |
paul@188 | 278 | if (pin >= _nr_pins) |
paul@188 | 279 | throw -L4_EINVAL; |
paul@188 | 280 | |
paul@188 | 281 | l4_uint32_t reg_set = value ? Port_data_set : Port_data_clear; |
paul@188 | 282 | _regs[reg_set] = _pin_bit(pin); |
paul@188 | 283 | } |
paul@188 | 284 | |
paul@188 | 285 | // Set multiple pin values. |
paul@188 | 286 | |
paul@188 | 287 | void |
paul@188 | 288 | Gpio_x1600_chip::multi_set(Pin_slice const &mask, unsigned data) |
paul@188 | 289 | { |
paul@188 | 290 | _reg_offset_check(mask.offset); |
paul@188 | 291 | if (mask.mask & data) |
paul@188 | 292 | _regs[Port_data_set] = (mask.mask & data); |
paul@188 | 293 | if (mask.mask & ~data) |
paul@188 | 294 | _regs[Port_data_clear] = (mask.mask & ~data); |
paul@188 | 295 | } |
paul@188 | 296 | |
paul@188 | 297 | // Set a pin up with the given mode and value (if appropriate). |
paul@188 | 298 | |
paul@188 | 299 | void |
paul@188 | 300 | Gpio_x1600_chip::setup(unsigned pin, unsigned mode, int value) |
paul@188 | 301 | { |
paul@188 | 302 | if (pin >= _nr_pins) |
paul@188 | 303 | throw -L4_EINVAL; |
paul@188 | 304 | |
paul@188 | 305 | config(pin, mode); |
paul@188 | 306 | |
paul@188 | 307 | if (mode == Output) |
paul@188 | 308 | set(pin, value); |
paul@188 | 309 | } |
paul@188 | 310 | |
paul@188 | 311 | // Configuration of a pin using the generic input/output/IRQ mode. |
paul@188 | 312 | |
paul@188 | 313 | void |
paul@188 | 314 | Gpio_x1600_chip::config(unsigned pin, unsigned mode) |
paul@188 | 315 | { |
paul@188 | 316 | switch (mode) |
paul@188 | 317 | { |
paul@188 | 318 | case Input: |
paul@270 | 319 | write_reg_pin(Port_int_clear, pin); |
paul@270 | 320 | write_reg_pin(Port_gpio_set, pin); |
paul@270 | 321 | write_reg_pin(Port_dir_set, pin); |
paul@188 | 322 | break; |
paul@188 | 323 | case Output: |
paul@270 | 324 | write_reg_pin(Port_int_clear, pin); |
paul@270 | 325 | write_reg_pin(Port_gpio_set, pin); |
paul@270 | 326 | write_reg_pin(Port_dir_clear, pin); |
paul@188 | 327 | break; |
paul@188 | 328 | case Irq: |
paul@270 | 329 | write_reg_pin(Port_int_set, pin); |
paul@188 | 330 | // Other details depend on the actual trigger mode. |
paul@188 | 331 | break; |
paul@188 | 332 | default: |
paul@188 | 333 | break; |
paul@188 | 334 | } |
paul@270 | 335 | |
paul@270 | 336 | if (_shadow) |
paul@270 | 337 | _shadow_regs[Shadow_transfer] = _port_number; |
paul@188 | 338 | } |
paul@188 | 339 | |
paul@188 | 340 | // Pull-up/down configuration for a pin. |
paul@188 | 341 | |
paul@188 | 342 | void |
paul@188 | 343 | Gpio_x1600_chip::config_pull(unsigned pin, unsigned mode) |
paul@188 | 344 | { |
paul@188 | 345 | if (pin >= _nr_pins) |
paul@188 | 346 | throw -L4_EINVAL; |
paul@188 | 347 | |
paul@188 | 348 | switch (mode) |
paul@188 | 349 | { |
paul@188 | 350 | case Pull_none: |
paul@189 | 351 | _regs[Pull_enable_clear] = _pin_bit(pin); |
paul@188 | 352 | break; |
paul@188 | 353 | case Pull_down: |
paul@188 | 354 | if (_pin_bit(pin) & _pull_downs) |
paul@189 | 355 | _regs[Pull_enable_set] = _pin_bit(pin); |
paul@188 | 356 | break; |
paul@188 | 357 | case Pull_up: |
paul@188 | 358 | if (_pin_bit(pin) & _pull_ups) |
paul@189 | 359 | _regs[Pull_enable_set] = _pin_bit(pin); |
paul@188 | 360 | break; |
paul@188 | 361 | default: |
paul@188 | 362 | // Invalid pull-up/down mode for pin. |
paul@188 | 363 | throw -L4_EINVAL; |
paul@188 | 364 | } |
paul@188 | 365 | } |
paul@188 | 366 | |
paul@188 | 367 | // Pin function configuration. |
paul@188 | 368 | |
paul@188 | 369 | void |
paul@188 | 370 | Gpio_x1600_chip::config_pad(unsigned pin, unsigned func, unsigned value) |
paul@188 | 371 | { |
paul@188 | 372 | if (pin >= _nr_pins) |
paul@188 | 373 | throw -L4_EINVAL; |
paul@188 | 374 | |
paul@188 | 375 | if (value > 3) |
paul@188 | 376 | throw -L4_EINVAL; |
paul@188 | 377 | |
paul@188 | 378 | switch (func) |
paul@188 | 379 | { |
paul@188 | 380 | // Support two different outputs. |
paul@188 | 381 | |
paul@188 | 382 | case Hw::Gpio_chip::Function_gpio: |
paul@270 | 383 | write_reg_pin(Port_int_clear, pin); |
paul@270 | 384 | write_reg_pin(Port_gpio_set, pin); |
paul@270 | 385 | write_reg_pin(value & 1 ? Port_data_set : Port_data_clear, pin); |
paul@188 | 386 | break; |
paul@188 | 387 | |
paul@188 | 388 | // Support four different device functions. |
paul@188 | 389 | |
paul@188 | 390 | case Hw::Gpio_chip::Function_alt: |
paul@270 | 391 | write_reg_pin(Port_int_clear, pin); |
paul@270 | 392 | write_reg_pin(Port_gpio_clear, pin); |
paul@270 | 393 | write_reg_pin(value & 2 ? Port_group1_set : Port_group1_clear, pin); |
paul@270 | 394 | write_reg_pin(value & 1 ? Port_group0_set : Port_group0_clear, pin); |
paul@188 | 395 | break; |
paul@188 | 396 | default: |
paul@188 | 397 | throw -L4_EINVAL; |
paul@188 | 398 | } |
paul@270 | 399 | |
paul@270 | 400 | if (_shadow) |
paul@270 | 401 | _shadow_regs[Shadow_transfer] = _port_number; |
paul@188 | 402 | } |
paul@188 | 403 | |
paul@188 | 404 | // Obtain a pin's configuration from a register in the supplied value. |
paul@188 | 405 | |
paul@188 | 406 | void |
paul@188 | 407 | Gpio_x1600_chip::config_get(unsigned pin, unsigned reg, unsigned *value) |
paul@188 | 408 | { |
paul@188 | 409 | if (pin >= _nr_pins) |
paul@188 | 410 | throw -L4_EINVAL; |
paul@188 | 411 | |
paul@188 | 412 | *value = (_regs[reg] >> _pin_shift(pin)) & 1; |
paul@188 | 413 | } |
paul@188 | 414 | |
paul@188 | 415 | // Return function and function-specific configuration for a pin. |
paul@188 | 416 | |
paul@188 | 417 | void |
paul@188 | 418 | Gpio_x1600_chip::config_pad_get(unsigned pin, unsigned *func, unsigned *value) |
paul@188 | 419 | { |
paul@188 | 420 | unsigned direction, gpio, group0, group1, interrupt, level, trigger; |
paul@188 | 421 | |
paul@188 | 422 | config_get(pin, Port_int, &interrupt); |
paul@188 | 423 | |
paul@188 | 424 | if (interrupt) |
paul@188 | 425 | { |
paul@188 | 426 | config_get(pin, Port_trigger, &trigger); |
paul@188 | 427 | config_get(pin, Port_level, &level); |
paul@188 | 428 | |
paul@188 | 429 | *func = Hw::Gpio_chip::Function_irq; |
paul@188 | 430 | *value = (trigger ? (level ? L4_IRQ_F_POS_EDGE : L4_IRQ_F_NEG_EDGE) |
paul@188 | 431 | : (level ? L4_IRQ_F_LEVEL_HIGH : L4_IRQ_F_LEVEL_LOW)); |
paul@188 | 432 | return; |
paul@188 | 433 | } |
paul@188 | 434 | |
paul@188 | 435 | config_get(pin, Port_gpio, &gpio); |
paul@188 | 436 | |
paul@188 | 437 | if (gpio) |
paul@188 | 438 | { |
paul@188 | 439 | config_get(pin, Port_dir, &direction); |
paul@188 | 440 | |
paul@188 | 441 | *func = Hw::Gpio_chip::Function_gpio; |
paul@188 | 442 | *value = direction ? Input : Output; |
paul@188 | 443 | return; |
paul@188 | 444 | } |
paul@188 | 445 | |
paul@188 | 446 | *func = Hw::Gpio_chip::Function_alt; |
paul@188 | 447 | |
paul@188 | 448 | config_get(pin, Port_group0, &group0); |
paul@188 | 449 | config_get(pin, Port_group1, &group1); |
paul@188 | 450 | |
paul@188 | 451 | *value = (group1 << 1) | group0; |
paul@188 | 452 | } |
paul@188 | 453 | |
paul@188 | 454 | // Obtain an IRQ abstraction for a pin. |
paul@188 | 455 | |
paul@188 | 456 | Hw::Gpio_irq_pin * |
paul@188 | 457 | Gpio_x1600_chip::get_irq(unsigned pin) |
paul@188 | 458 | { |
paul@188 | 459 | if (pin >= _nr_pins) |
paul@188 | 460 | throw -L4_EINVAL; |
paul@188 | 461 | |
paul@270 | 462 | if (_shadow) |
paul@270 | 463 | return new Gpio_x1600_irq_pin(pin, _regs, _shadow_regs, _port_number); |
paul@270 | 464 | else |
paul@270 | 465 | return new Gpio_x1600_irq_pin(pin, _regs); |
paul@188 | 466 | } |
paul@188 | 467 | |
paul@188 | 468 | // Pin function configuration for multiple pins. |
paul@188 | 469 | |
paul@188 | 470 | void |
paul@188 | 471 | Gpio_x1600_chip::multi_config_pad(Pin_slice const &mask, unsigned func, unsigned val) |
paul@188 | 472 | { |
paul@188 | 473 | unsigned m = mask.mask; |
paul@188 | 474 | for (unsigned pin = mask.offset; pin < _nr_pins; ++pin, m >>= 1) |
paul@188 | 475 | if (m & 1) |
paul@188 | 476 | config_pad(pin, func, val); |
paul@188 | 477 | } |
paul@188 | 478 | |
paul@188 | 479 | // Set up multiple pins with the given mode. |
paul@188 | 480 | |
paul@188 | 481 | void |
paul@188 | 482 | Gpio_x1600_chip::multi_setup(Pin_slice const &mask, unsigned mode, unsigned outvalues) |
paul@188 | 483 | { |
paul@188 | 484 | unsigned m = mask.mask; |
paul@188 | 485 | for (unsigned pin = mask.offset; pin < _nr_pins; ++pin, m >>= 1, outvalues >>= 1) |
paul@188 | 486 | if (m & 1) |
paul@188 | 487 | setup(pin, mode, outvalues & 1); |
paul@188 | 488 | } |
paul@188 | 489 | |
paul@188 | 490 | |
paul@188 | 491 | |
paul@188 | 492 | // C language interface functions. |
paul@188 | 493 | |
paul@188 | 494 | void *x1600_gpio_init(l4_addr_t start, l4_addr_t end, unsigned pins, |
paul@270 | 495 | l4_uint32_t pull_ups, l4_uint32_t pull_downs) |
paul@188 | 496 | { |
paul@188 | 497 | return (void *) new Gpio_x1600_chip(start, end, pins, pull_ups, pull_downs); |
paul@188 | 498 | } |
paul@188 | 499 | |
paul@270 | 500 | void *x1600_gpio_init_shadow(l4_addr_t start, l4_addr_t end, unsigned pins, |
paul@270 | 501 | l4_uint32_t pull_ups, l4_uint32_t pull_downs, |
paul@270 | 502 | l4_addr_t shadow_start, l4_addr_t shadow_end, |
paul@270 | 503 | uint8_t port_number) |
paul@270 | 504 | { |
paul@270 | 505 | return (void *) new Gpio_x1600_chip(start, end, pins, pull_ups, pull_downs, |
paul@270 | 506 | shadow_start, shadow_end, port_number); |
paul@270 | 507 | } |
paul@270 | 508 | |
paul@188 | 509 | void x1600_gpio_setup(void *gpio, unsigned pin, unsigned mode, int value) |
paul@188 | 510 | { |
paul@188 | 511 | static_cast<Gpio_x1600_chip *>(gpio)->setup(pin, mode, value); |
paul@188 | 512 | } |
paul@188 | 513 | |
paul@188 | 514 | void x1600_gpio_config_pull(void *gpio, unsigned pin, unsigned mode) |
paul@188 | 515 | { |
paul@188 | 516 | static_cast<Gpio_x1600_chip *>(gpio)->config_pull(pin, mode); |
paul@188 | 517 | } |
paul@188 | 518 | |
paul@188 | 519 | void x1600_gpio_config_pad(void *gpio, unsigned pin, unsigned func, unsigned value) |
paul@188 | 520 | { |
paul@188 | 521 | static_cast<Gpio_x1600_chip *>(gpio)->config_pad(pin, func, value); |
paul@188 | 522 | } |
paul@188 | 523 | |
paul@188 | 524 | void x1600_gpio_config_get(void *gpio, unsigned pin, unsigned reg, unsigned *value) |
paul@188 | 525 | { |
paul@188 | 526 | static_cast<Gpio_x1600_chip *>(gpio)->config_get(pin, reg, value); |
paul@188 | 527 | } |
paul@188 | 528 | |
paul@188 | 529 | void x1600_gpio_config_pad_get(void *gpio, unsigned pin, unsigned *func, unsigned *value) |
paul@188 | 530 | { |
paul@188 | 531 | static_cast<Gpio_x1600_chip *>(gpio)->config_pad_get(pin, func, value); |
paul@188 | 532 | } |
paul@188 | 533 | |
paul@188 | 534 | void x1600_gpio_multi_setup(void *gpio, Pin_slice const *mask, unsigned mode, unsigned outvalues) |
paul@188 | 535 | { |
paul@188 | 536 | static_cast<Gpio_x1600_chip *>(gpio)->multi_setup(*mask, mode, outvalues); |
paul@188 | 537 | } |
paul@188 | 538 | |
paul@188 | 539 | void x1600_gpio_multi_config_pad(void *gpio, Pin_slice const *mask, unsigned func, unsigned value) |
paul@188 | 540 | { |
paul@188 | 541 | static_cast<Gpio_x1600_chip *>(gpio)->multi_config_pad(*mask, func, value); |
paul@188 | 542 | } |
paul@188 | 543 | |
paul@188 | 544 | void x1600_gpio_multi_set(void *gpio, Pin_slice const *mask, unsigned data) |
paul@188 | 545 | { |
paul@188 | 546 | static_cast<Gpio_x1600_chip *>(gpio)->multi_set(*mask, data); |
paul@188 | 547 | } |
paul@188 | 548 | |
paul@188 | 549 | unsigned x1600_gpio_multi_get(void *gpio, unsigned offset) |
paul@188 | 550 | { |
paul@188 | 551 | return static_cast<Gpio_x1600_chip *>(gpio)->multi_get(offset); |
paul@188 | 552 | } |
paul@188 | 553 | |
paul@188 | 554 | int x1600_gpio_get(void *gpio, unsigned pin) |
paul@188 | 555 | { |
paul@188 | 556 | return static_cast<Gpio_x1600_chip *>(gpio)->get(pin); |
paul@188 | 557 | } |
paul@188 | 558 | |
paul@188 | 559 | void x1600_gpio_set(void *gpio, unsigned pin, int value) |
paul@188 | 560 | { |
paul@188 | 561 | static_cast<Gpio_x1600_chip *>(gpio)->set(pin, value); |
paul@188 | 562 | } |
paul@188 | 563 | |
paul@188 | 564 | void *x1600_gpio_get_irq(void *gpio, unsigned pin) |
paul@188 | 565 | { |
paul@188 | 566 | return (void *) static_cast<Gpio_x1600_chip *>(gpio)->get_irq(pin); |
paul@188 | 567 | } |
paul@188 | 568 | |
paul@188 | 569 | bool x1600_gpio_irq_set_mode(void *gpio_irq, unsigned mode) |
paul@188 | 570 | { |
paul@188 | 571 | return static_cast<Hw::Gpio_irq_pin *>(gpio_irq)->do_set_mode(mode); |
paul@188 | 572 | } |