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@270 | 5 | * Copyright (C) 2017, 2018, 2023, 2024 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 | #pragma once |
paul@188 | 32 | |
paul@188 | 33 | #include <l4/sys/err.h> |
paul@188 | 34 | #include <l4/sys/types.h> |
paul@188 | 35 | #include <stdint.h> |
paul@188 | 36 | #include "gpio.h" |
paul@188 | 37 | |
paul@188 | 38 | |
paul@188 | 39 | |
paul@188 | 40 | #ifdef __cplusplus |
paul@188 | 41 | |
paul@188 | 42 | #include <l4/devices/hw_mmio_register_block.h> |
paul@188 | 43 | |
paul@188 | 44 | // GPIO device control. |
paul@188 | 45 | |
paul@188 | 46 | class Gpio_x1600_irq_pin : public Hw::Gpio_irq_pin |
paul@188 | 47 | { |
paul@188 | 48 | unsigned _pin; |
paul@270 | 49 | |
paul@270 | 50 | // Register blocks for the conventional and optional shadow registers. |
paul@270 | 51 | |
paul@270 | 52 | Hw::Register_block<32> _regs, _shadow_regs; |
paul@270 | 53 | uint8_t _port_number; |
paul@270 | 54 | bool _shadow; |
paul@188 | 55 | |
paul@188 | 56 | // Convenience method for obtaining the bit corresponding to a pin. |
paul@188 | 57 | |
paul@188 | 58 | l4_uint32_t _pin_bit(unsigned pin) |
paul@188 | 59 | { return 1 << (pin & 31); } |
paul@188 | 60 | |
paul@188 | 61 | void write_reg_pin(unsigned reg); |
paul@188 | 62 | |
paul@188 | 63 | public: |
paul@270 | 64 | Gpio_x1600_irq_pin(unsigned pin, Hw::Register_block<32> const ®s, |
paul@270 | 65 | Hw::Register_block<32> const &shadow_regs, |
paul@270 | 66 | uint8_t port_number); |
paul@270 | 67 | |
paul@188 | 68 | Gpio_x1600_irq_pin(unsigned pin, Hw::Register_block<32> const ®s); |
paul@188 | 69 | |
paul@188 | 70 | void do_mask(); |
paul@188 | 71 | void do_unmask(); |
paul@188 | 72 | bool do_set_mode(unsigned mode); |
paul@188 | 73 | int clear(); |
paul@188 | 74 | bool enabled(); |
paul@188 | 75 | }; |
paul@188 | 76 | |
paul@188 | 77 | class Gpio_x1600_chip : public Hw::Gpio_chip |
paul@188 | 78 | { |
paul@188 | 79 | private: |
paul@270 | 80 | // Register blocks for the conventional and optional shadow registers. |
paul@270 | 81 | |
paul@270 | 82 | Hw::Register_block<32> _regs, _shadow_regs; |
paul@188 | 83 | |
paul@188 | 84 | l4_addr_t _start, _end; |
paul@188 | 85 | unsigned _nr_pins; |
paul@188 | 86 | l4_uint32_t _pull_ups, _pull_downs; |
paul@188 | 87 | |
paul@270 | 88 | // Optional shadow port configuration. |
paul@270 | 89 | |
paul@270 | 90 | l4_addr_t _shadow_start, _shadow_end; |
paul@270 | 91 | uint8_t _port_number; |
paul@270 | 92 | bool _shadow; |
paul@270 | 93 | |
paul@188 | 94 | // Convenience method for obtaining the bit corresponding to a pin. |
paul@188 | 95 | |
paul@188 | 96 | l4_uint32_t _pin_bit(unsigned pin) |
paul@188 | 97 | { return 1 << (pin & 31); } |
paul@188 | 98 | |
paul@188 | 99 | // Convenience method for obtaining the bit position of a pin. |
paul@188 | 100 | |
paul@188 | 101 | unsigned _pin_shift(unsigned pin) |
paul@188 | 102 | { return pin % 32; } |
paul@188 | 103 | |
paul@188 | 104 | // Permit only "aligned" accesses to registers. |
paul@188 | 105 | |
paul@188 | 106 | unsigned _reg_offset_check(unsigned pin_offset) const |
paul@188 | 107 | { |
paul@188 | 108 | switch (pin_offset) |
paul@188 | 109 | { |
paul@188 | 110 | case 0: |
paul@188 | 111 | return 0; |
paul@188 | 112 | |
paul@188 | 113 | default: |
paul@188 | 114 | throw -L4_EINVAL; |
paul@188 | 115 | } |
paul@188 | 116 | } |
paul@188 | 117 | |
paul@270 | 118 | void write_reg_pin(unsigned reg, unsigned pin); |
paul@270 | 119 | |
paul@188 | 120 | public: |
paul@188 | 121 | Gpio_x1600_chip(l4_addr_t start, l4_addr_t end, |
paul@270 | 122 | unsigned nr_pins, |
paul@270 | 123 | l4_uint32_t pull_ups, l4_uint32_t pull_downs, |
paul@270 | 124 | l4_addr_t shadow_start = 0, l4_addr_t shadow_end = 0, |
paul@270 | 125 | uint8_t port_number = 0); |
paul@188 | 126 | |
paul@188 | 127 | // Obtain the number of pins. |
paul@188 | 128 | |
paul@188 | 129 | unsigned nr_pins() const { return _nr_pins; } |
paul@188 | 130 | |
paul@188 | 131 | // Unnecessary operations. |
paul@188 | 132 | |
paul@188 | 133 | void request(unsigned) {} |
paul@188 | 134 | void free(unsigned) {} |
paul@188 | 135 | |
paul@188 | 136 | // Configuration methods. |
paul@188 | 137 | |
paul@188 | 138 | void setup(unsigned pin, unsigned mode, int value = 0); |
paul@188 | 139 | void config_pull(unsigned pin, unsigned mode); |
paul@188 | 140 | void config_pad(unsigned pin, unsigned func, unsigned value); |
paul@188 | 141 | void config_get(unsigned pin, unsigned reg, unsigned *value); |
paul@188 | 142 | void config_pad_get(unsigned pin, unsigned *func, unsigned *value); |
paul@188 | 143 | |
paul@188 | 144 | // Multiple pin configuration methods. |
paul@188 | 145 | |
paul@188 | 146 | void multi_setup(Pin_slice const &mask, unsigned mode, unsigned outvalues = 0); |
paul@188 | 147 | void multi_config_pad(Pin_slice const &mask, unsigned func, unsigned value = 0); |
paul@188 | 148 | void multi_set(Pin_slice const &mask, unsigned data); |
paul@188 | 149 | unsigned multi_get(unsigned offset); |
paul@188 | 150 | |
paul@188 | 151 | // IRQ pin configuration. |
paul@188 | 152 | |
paul@188 | 153 | Hw::Gpio_irq_pin *get_irq(unsigned pin); |
paul@188 | 154 | |
paul@188 | 155 | // Pin/port data methods. |
paul@188 | 156 | |
paul@188 | 157 | int get(unsigned pin); |
paul@188 | 158 | void set(unsigned pin, int value); |
paul@188 | 159 | |
paul@188 | 160 | private: |
paul@188 | 161 | void config(unsigned pin, unsigned mode); |
paul@188 | 162 | }; |
paul@188 | 163 | |
paul@188 | 164 | #endif /* __cplusplus */ |
paul@188 | 165 | |
paul@188 | 166 | |
paul@188 | 167 | |
paul@188 | 168 | /* C language interface. */ |
paul@188 | 169 | |
paul@188 | 170 | EXTERN_C_BEGIN |
paul@188 | 171 | |
paul@188 | 172 | void *x1600_gpio_init(l4_addr_t start, l4_addr_t end, unsigned pins, |
paul@270 | 173 | l4_uint32_t pull_ups, l4_uint32_t pull_downs); |
paul@270 | 174 | |
paul@270 | 175 | void *x1600_gpio_init_shadow(l4_addr_t start, l4_addr_t end, unsigned pins, |
paul@270 | 176 | l4_uint32_t pull_ups, l4_uint32_t pull_downs, |
paul@270 | 177 | l4_addr_t shadow_start, l4_addr_t shadow_end, |
paul@270 | 178 | uint8_t port_number); |
paul@188 | 179 | |
paul@188 | 180 | void x1600_gpio_setup(void *gpio, unsigned pin, unsigned mode, int value); |
paul@188 | 181 | void x1600_gpio_config_pull(void *gpio, unsigned pin, unsigned mode); |
paul@188 | 182 | void x1600_gpio_config_pad(void *gpio, unsigned pin, unsigned func, unsigned value); |
paul@188 | 183 | void x1600_gpio_config_get(void *gpio, unsigned pin, unsigned reg, unsigned *value); |
paul@188 | 184 | void x1600_gpio_config_pad_get(void *gpio, unsigned pin, unsigned *func, unsigned *value); |
paul@188 | 185 | |
paul@188 | 186 | void x1600_gpio_multi_setup(void *gpio, Pin_slice const *mask, unsigned mode, unsigned outvalues); |
paul@188 | 187 | void x1600_gpio_multi_config_pad(void *gpio, Pin_slice const *mask, unsigned func, unsigned value); |
paul@188 | 188 | void x1600_gpio_multi_set(void *gpio, Pin_slice const *mask, unsigned data); |
paul@188 | 189 | unsigned x1600_gpio_multi_get(void *gpio, unsigned offset); |
paul@188 | 190 | |
paul@188 | 191 | int x1600_gpio_get(void *gpio, unsigned pin); |
paul@188 | 192 | void x1600_gpio_set(void *gpio, unsigned pin, int value); |
paul@188 | 193 | |
paul@188 | 194 | void *x1600_gpio_get_irq(void *gpio, unsigned pin); |
paul@188 | 195 | bool x1600_gpio_irq_set_mode(void *gpio_irq, unsigned mode); |
paul@188 | 196 | |
paul@188 | 197 | EXTERN_C_END |