Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2015 IBM Corp. |
| 4 | * |
| 5 | * Joel Stanley <joel@jms.id.au> |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 6 | */ |
| 7 | |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 8 | #include <asm/div64.h> |
| 9 | #include <linux/clk.h> |
| 10 | #include <linux/gpio/driver.h> |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 11 | #include <linux/gpio/aspeed.h> |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 12 | #include <linux/hashtable.h> |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 13 | #include <linux/init.h> |
| 14 | #include <linux/io.h> |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 17 | #include <linux/pinctrl/consumer.h> |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/spinlock.h> |
| 20 | #include <linux/string.h> |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 21 | |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 22 | /* |
| 23 | * These two headers aren't meant to be used by GPIO drivers. We need |
| 24 | * them in order to access gpio_chip_hwgpio() which we need to implement |
| 25 | * the aspeed specific API which allows the coprocessor to request |
| 26 | * access to some GPIOs and to arbitrate between coprocessor and ARM. |
| 27 | */ |
| 28 | #include <linux/gpio/consumer.h> |
| 29 | #include "gpiolib.h" |
| 30 | |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 31 | struct aspeed_bank_props { |
| 32 | unsigned int bank; |
| 33 | u32 input; |
| 34 | u32 output; |
| 35 | }; |
| 36 | |
| 37 | struct aspeed_gpio_config { |
| 38 | unsigned int nr_gpios; |
| 39 | const struct aspeed_bank_props *props; |
| 40 | }; |
| 41 | |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 42 | /* |
| 43 | * @offset_timer: Maps an offset to an @timer_users index, or zero if disabled |
| 44 | * @timer_users: Tracks the number of users for each timer |
| 45 | * |
| 46 | * The @timer_users has four elements but the first element is unused. This is |
| 47 | * to simplify accounting and indexing, as a zero value in @offset_timer |
| 48 | * represents disabled debouncing for the GPIO. Any other value for an element |
| 49 | * of @offset_timer is used as an index into @timer_users. This behaviour of |
| 50 | * the zero value aligns with the behaviour of zero built from the timer |
| 51 | * configuration registers (i.e. debouncing is disabled). |
| 52 | */ |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 53 | struct aspeed_gpio { |
| 54 | struct gpio_chip chip; |
Rashmica Gupta | 3d64a5a | 2019-09-06 16:26:44 +1000 | [diff] [blame] | 55 | struct irq_chip irqc; |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 56 | raw_spinlock_t lock; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 57 | void __iomem *base; |
| 58 | int irq; |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 59 | const struct aspeed_gpio_config *config; |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 60 | |
| 61 | u8 *offset_timer; |
| 62 | unsigned int timer_users[4]; |
| 63 | struct clk *clk; |
Benjamin Herrenschmidt | ed5cab4 | 2018-05-17 18:12:02 +1000 | [diff] [blame] | 64 | |
| 65 | u32 *dcache; |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 66 | u8 *cf_copro_bankmap; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | struct aspeed_gpio_bank { |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame] | 70 | uint16_t val_regs; /* +0: Rd: read input value, Wr: set write latch |
| 71 | * +4: Rd/Wr: Direction (0=in, 1=out) |
| 72 | */ |
| 73 | uint16_t rdata_reg; /* Rd: read write latch, Wr: <none> */ |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 74 | uint16_t irq_regs; |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 75 | uint16_t debounce_regs; |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 76 | uint16_t tolerance_regs; |
Benjamin Herrenschmidt | 0f1e03c | 2018-06-29 14:11:18 +1000 | [diff] [blame] | 77 | uint16_t cmdsrc_regs; |
Joel Stanley | 7153f8e | 2017-01-23 15:56:06 +1030 | [diff] [blame] | 78 | const char names[4][3]; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 79 | }; |
| 80 | |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame] | 81 | /* |
| 82 | * Note: The "value" register returns the input value sampled on the |
| 83 | * line even when the GPIO is configured as an output. Since |
| 84 | * that input goes through synchronizers, writing, then reading |
| 85 | * back may not return the written value right away. |
| 86 | * |
| 87 | * The "rdata" register returns the content of the write latch |
| 88 | * and thus can be used to read back what was last written |
| 89 | * reliably. |
| 90 | */ |
| 91 | |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 92 | static const int debounce_timers[4] = { 0x00, 0x50, 0x54, 0x58 }; |
| 93 | |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 94 | static const struct aspeed_gpio_copro_ops *copro_ops; |
| 95 | static void *copro_data; |
| 96 | |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 97 | static const struct aspeed_gpio_bank aspeed_gpio_banks[] = { |
| 98 | { |
| 99 | .val_regs = 0x0000, |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame] | 100 | .rdata_reg = 0x00c0, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 101 | .irq_regs = 0x0008, |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 102 | .debounce_regs = 0x0040, |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 103 | .tolerance_regs = 0x001c, |
Benjamin Herrenschmidt | 0f1e03c | 2018-06-29 14:11:18 +1000 | [diff] [blame] | 104 | .cmdsrc_regs = 0x0060, |
Joel Stanley | 7153f8e | 2017-01-23 15:56:06 +1030 | [diff] [blame] | 105 | .names = { "A", "B", "C", "D" }, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 106 | }, |
| 107 | { |
| 108 | .val_regs = 0x0020, |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame] | 109 | .rdata_reg = 0x00c4, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 110 | .irq_regs = 0x0028, |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 111 | .debounce_regs = 0x0048, |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 112 | .tolerance_regs = 0x003c, |
Benjamin Herrenschmidt | 0f1e03c | 2018-06-29 14:11:18 +1000 | [diff] [blame] | 113 | .cmdsrc_regs = 0x0068, |
Joel Stanley | 7153f8e | 2017-01-23 15:56:06 +1030 | [diff] [blame] | 114 | .names = { "E", "F", "G", "H" }, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 115 | }, |
| 116 | { |
| 117 | .val_regs = 0x0070, |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame] | 118 | .rdata_reg = 0x00c8, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 119 | .irq_regs = 0x0098, |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 120 | .debounce_regs = 0x00b0, |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 121 | .tolerance_regs = 0x00ac, |
Benjamin Herrenschmidt | 0f1e03c | 2018-06-29 14:11:18 +1000 | [diff] [blame] | 122 | .cmdsrc_regs = 0x0090, |
Joel Stanley | 7153f8e | 2017-01-23 15:56:06 +1030 | [diff] [blame] | 123 | .names = { "I", "J", "K", "L" }, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 124 | }, |
| 125 | { |
| 126 | .val_regs = 0x0078, |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame] | 127 | .rdata_reg = 0x00cc, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 128 | .irq_regs = 0x00e8, |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 129 | .debounce_regs = 0x0100, |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 130 | .tolerance_regs = 0x00fc, |
Benjamin Herrenschmidt | 0f1e03c | 2018-06-29 14:11:18 +1000 | [diff] [blame] | 131 | .cmdsrc_regs = 0x00e0, |
Joel Stanley | 7153f8e | 2017-01-23 15:56:06 +1030 | [diff] [blame] | 132 | .names = { "M", "N", "O", "P" }, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 133 | }, |
| 134 | { |
| 135 | .val_regs = 0x0080, |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame] | 136 | .rdata_reg = 0x00d0, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 137 | .irq_regs = 0x0118, |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 138 | .debounce_regs = 0x0130, |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 139 | .tolerance_regs = 0x012c, |
Benjamin Herrenschmidt | 0f1e03c | 2018-06-29 14:11:18 +1000 | [diff] [blame] | 140 | .cmdsrc_regs = 0x0110, |
Joel Stanley | 7153f8e | 2017-01-23 15:56:06 +1030 | [diff] [blame] | 141 | .names = { "Q", "R", "S", "T" }, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 142 | }, |
| 143 | { |
| 144 | .val_regs = 0x0088, |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame] | 145 | .rdata_reg = 0x00d4, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 146 | .irq_regs = 0x0148, |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 147 | .debounce_regs = 0x0160, |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 148 | .tolerance_regs = 0x015c, |
Benjamin Herrenschmidt | 0f1e03c | 2018-06-29 14:11:18 +1000 | [diff] [blame] | 149 | .cmdsrc_regs = 0x0140, |
Joel Stanley | 7153f8e | 2017-01-23 15:56:06 +1030 | [diff] [blame] | 150 | .names = { "U", "V", "W", "X" }, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 151 | }, |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 152 | { |
| 153 | .val_regs = 0x01E0, |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame] | 154 | .rdata_reg = 0x00d8, |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 155 | .irq_regs = 0x0178, |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 156 | .debounce_regs = 0x0190, |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 157 | .tolerance_regs = 0x018c, |
Benjamin Herrenschmidt | 0f1e03c | 2018-06-29 14:11:18 +1000 | [diff] [blame] | 158 | .cmdsrc_regs = 0x0170, |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 159 | .names = { "Y", "Z", "AA", "AB" }, |
| 160 | }, |
| 161 | { |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 162 | .val_regs = 0x01e8, |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame] | 163 | .rdata_reg = 0x00dc, |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 164 | .irq_regs = 0x01a8, |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 165 | .debounce_regs = 0x01c0, |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 166 | .tolerance_regs = 0x01bc, |
Benjamin Herrenschmidt | 0f1e03c | 2018-06-29 14:11:18 +1000 | [diff] [blame] | 167 | .cmdsrc_regs = 0x01a0, |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 168 | .names = { "AC", "", "", "" }, |
| 169 | }, |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 170 | }; |
| 171 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 172 | enum aspeed_gpio_reg { |
| 173 | reg_val, |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame] | 174 | reg_rdata, |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 175 | reg_dir, |
| 176 | reg_irq_enable, |
| 177 | reg_irq_type0, |
| 178 | reg_irq_type1, |
| 179 | reg_irq_type2, |
| 180 | reg_irq_status, |
| 181 | reg_debounce_sel1, |
| 182 | reg_debounce_sel2, |
| 183 | reg_tolerance, |
Benjamin Herrenschmidt | 0f1e03c | 2018-06-29 14:11:18 +1000 | [diff] [blame] | 184 | reg_cmdsrc0, |
| 185 | reg_cmdsrc1, |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 186 | }; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 187 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 188 | #define GPIO_VAL_VALUE 0x00 |
| 189 | #define GPIO_VAL_DIR 0x04 |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 190 | |
| 191 | #define GPIO_IRQ_ENABLE 0x00 |
| 192 | #define GPIO_IRQ_TYPE0 0x04 |
| 193 | #define GPIO_IRQ_TYPE1 0x08 |
| 194 | #define GPIO_IRQ_TYPE2 0x0c |
| 195 | #define GPIO_IRQ_STATUS 0x10 |
| 196 | |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 197 | #define GPIO_DEBOUNCE_SEL1 0x00 |
| 198 | #define GPIO_DEBOUNCE_SEL2 0x04 |
| 199 | |
Benjamin Herrenschmidt | 0f1e03c | 2018-06-29 14:11:18 +1000 | [diff] [blame] | 200 | #define GPIO_CMDSRC_0 0x00 |
| 201 | #define GPIO_CMDSRC_1 0x04 |
| 202 | #define GPIO_CMDSRC_ARM 0 |
| 203 | #define GPIO_CMDSRC_LPC 1 |
| 204 | #define GPIO_CMDSRC_COLDFIRE 2 |
| 205 | #define GPIO_CMDSRC_RESERVED 3 |
| 206 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 207 | /* This will be resolved at compile time */ |
| 208 | static inline void __iomem *bank_reg(struct aspeed_gpio *gpio, |
| 209 | const struct aspeed_gpio_bank *bank, |
| 210 | const enum aspeed_gpio_reg reg) |
| 211 | { |
| 212 | switch (reg) { |
| 213 | case reg_val: |
| 214 | return gpio->base + bank->val_regs + GPIO_VAL_VALUE; |
Benjamin Herrenschmidt | c67dda8 | 2018-06-29 14:11:17 +1000 | [diff] [blame] | 215 | case reg_rdata: |
| 216 | return gpio->base + bank->rdata_reg; |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 217 | case reg_dir: |
| 218 | return gpio->base + bank->val_regs + GPIO_VAL_DIR; |
| 219 | case reg_irq_enable: |
| 220 | return gpio->base + bank->irq_regs + GPIO_IRQ_ENABLE; |
| 221 | case reg_irq_type0: |
| 222 | return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE0; |
| 223 | case reg_irq_type1: |
| 224 | return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE1; |
| 225 | case reg_irq_type2: |
| 226 | return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE2; |
| 227 | case reg_irq_status: |
| 228 | return gpio->base + bank->irq_regs + GPIO_IRQ_STATUS; |
| 229 | case reg_debounce_sel1: |
| 230 | return gpio->base + bank->debounce_regs + GPIO_DEBOUNCE_SEL1; |
| 231 | case reg_debounce_sel2: |
| 232 | return gpio->base + bank->debounce_regs + GPIO_DEBOUNCE_SEL2; |
| 233 | case reg_tolerance: |
| 234 | return gpio->base + bank->tolerance_regs; |
Benjamin Herrenschmidt | 0f1e03c | 2018-06-29 14:11:18 +1000 | [diff] [blame] | 235 | case reg_cmdsrc0: |
| 236 | return gpio->base + bank->cmdsrc_regs + GPIO_CMDSRC_0; |
| 237 | case reg_cmdsrc1: |
| 238 | return gpio->base + bank->cmdsrc_regs + GPIO_CMDSRC_1; |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 239 | } |
Arnd Bergmann | c296773 | 2018-07-09 16:56:03 +0200 | [diff] [blame] | 240 | BUG(); |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | #define GPIO_BANK(x) ((x) >> 5) |
| 244 | #define GPIO_OFFSET(x) ((x) & 0x1f) |
| 245 | #define GPIO_BIT(x) BIT(GPIO_OFFSET(x)) |
| 246 | |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 247 | #define _GPIO_SET_DEBOUNCE(t, o, i) ((!!((t) & BIT(i))) << GPIO_OFFSET(o)) |
| 248 | #define GPIO_SET_DEBOUNCE1(t, o) _GPIO_SET_DEBOUNCE(t, o, 1) |
| 249 | #define GPIO_SET_DEBOUNCE2(t, o) _GPIO_SET_DEBOUNCE(t, o, 0) |
| 250 | |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 251 | static const struct aspeed_gpio_bank *to_bank(unsigned int offset) |
| 252 | { |
| 253 | unsigned int bank = GPIO_BANK(offset); |
| 254 | |
Vasyl Gomonovych | fe13862 | 2017-12-21 16:55:10 +0100 | [diff] [blame] | 255 | WARN_ON(bank >= ARRAY_SIZE(aspeed_gpio_banks)); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 256 | return &aspeed_gpio_banks[bank]; |
| 257 | } |
| 258 | |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 259 | static inline bool is_bank_props_sentinel(const struct aspeed_bank_props *props) |
| 260 | { |
| 261 | return !(props->input || props->output); |
| 262 | } |
| 263 | |
| 264 | static inline const struct aspeed_bank_props *find_bank_props( |
| 265 | struct aspeed_gpio *gpio, unsigned int offset) |
| 266 | { |
| 267 | const struct aspeed_bank_props *props = gpio->config->props; |
| 268 | |
| 269 | while (!is_bank_props_sentinel(props)) { |
| 270 | if (props->bank == GPIO_BANK(offset)) |
| 271 | return props; |
| 272 | props++; |
| 273 | } |
| 274 | |
| 275 | return NULL; |
| 276 | } |
| 277 | |
| 278 | static inline bool have_gpio(struct aspeed_gpio *gpio, unsigned int offset) |
| 279 | { |
| 280 | const struct aspeed_bank_props *props = find_bank_props(gpio, offset); |
| 281 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
| 282 | unsigned int group = GPIO_OFFSET(offset) / 8; |
| 283 | |
| 284 | return bank->names[group][0] != '\0' && |
| 285 | (!props || ((props->input | props->output) & GPIO_BIT(offset))); |
| 286 | } |
| 287 | |
| 288 | static inline bool have_input(struct aspeed_gpio *gpio, unsigned int offset) |
| 289 | { |
| 290 | const struct aspeed_bank_props *props = find_bank_props(gpio, offset); |
| 291 | |
| 292 | return !props || (props->input & GPIO_BIT(offset)); |
| 293 | } |
| 294 | |
| 295 | #define have_irq(g, o) have_input((g), (o)) |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 296 | #define have_debounce(g, o) have_input((g), (o)) |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 297 | |
| 298 | static inline bool have_output(struct aspeed_gpio *gpio, unsigned int offset) |
| 299 | { |
| 300 | const struct aspeed_bank_props *props = find_bank_props(gpio, offset); |
| 301 | |
| 302 | return !props || (props->output & GPIO_BIT(offset)); |
| 303 | } |
| 304 | |
Benjamin Herrenschmidt | 0f1e03c | 2018-06-29 14:11:18 +1000 | [diff] [blame] | 305 | static void aspeed_gpio_change_cmd_source(struct aspeed_gpio *gpio, |
| 306 | const struct aspeed_gpio_bank *bank, |
| 307 | int bindex, int cmdsrc) |
| 308 | { |
| 309 | void __iomem *c0 = bank_reg(gpio, bank, reg_cmdsrc0); |
| 310 | void __iomem *c1 = bank_reg(gpio, bank, reg_cmdsrc1); |
| 311 | u32 bit, reg; |
| 312 | |
| 313 | /* |
| 314 | * Each register controls 4 banks, so take the bottom 2 |
| 315 | * bits of the bank index, and use them to select the |
| 316 | * right control bit (0, 8, 16 or 24). |
| 317 | */ |
| 318 | bit = BIT((bindex & 3) << 3); |
| 319 | |
| 320 | /* Source 1 first to avoid illegal 11 combination */ |
| 321 | reg = ioread32(c1); |
| 322 | if (cmdsrc & 2) |
| 323 | reg |= bit; |
| 324 | else |
| 325 | reg &= ~bit; |
| 326 | iowrite32(reg, c1); |
| 327 | |
| 328 | /* Then Source 0 */ |
| 329 | reg = ioread32(c0); |
| 330 | if (cmdsrc & 1) |
| 331 | reg |= bit; |
| 332 | else |
| 333 | reg &= ~bit; |
| 334 | iowrite32(reg, c0); |
| 335 | } |
| 336 | |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 337 | static bool aspeed_gpio_copro_request(struct aspeed_gpio *gpio, |
| 338 | unsigned int offset) |
| 339 | { |
| 340 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
| 341 | |
| 342 | if (!copro_ops || !gpio->cf_copro_bankmap) |
| 343 | return false; |
| 344 | if (!gpio->cf_copro_bankmap[offset >> 3]) |
| 345 | return false; |
| 346 | if (!copro_ops->request_access) |
| 347 | return false; |
| 348 | |
| 349 | /* Pause the coprocessor */ |
| 350 | copro_ops->request_access(copro_data); |
| 351 | |
| 352 | /* Change command source back to ARM */ |
| 353 | aspeed_gpio_change_cmd_source(gpio, bank, offset >> 3, GPIO_CMDSRC_ARM); |
| 354 | |
| 355 | /* Update cache */ |
| 356 | gpio->dcache[GPIO_BANK(offset)] = ioread32(bank_reg(gpio, bank, reg_rdata)); |
| 357 | |
| 358 | return true; |
| 359 | } |
| 360 | |
| 361 | static void aspeed_gpio_copro_release(struct aspeed_gpio *gpio, |
| 362 | unsigned int offset) |
| 363 | { |
| 364 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
| 365 | |
| 366 | if (!copro_ops || !gpio->cf_copro_bankmap) |
| 367 | return; |
| 368 | if (!gpio->cf_copro_bankmap[offset >> 3]) |
| 369 | return; |
| 370 | if (!copro_ops->release_access) |
| 371 | return; |
| 372 | |
| 373 | /* Change command source back to ColdFire */ |
| 374 | aspeed_gpio_change_cmd_source(gpio, bank, offset >> 3, |
| 375 | GPIO_CMDSRC_COLDFIRE); |
| 376 | |
| 377 | /* Restart the coprocessor */ |
| 378 | copro_ops->release_access(copro_data); |
| 379 | } |
| 380 | |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 381 | static int aspeed_gpio_get(struct gpio_chip *gc, unsigned int offset) |
| 382 | { |
| 383 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); |
| 384 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
| 385 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 386 | return !!(ioread32(bank_reg(gpio, bank, reg_val)) & GPIO_BIT(offset)); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static void __aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset, |
| 390 | int val) |
| 391 | { |
| 392 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); |
| 393 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
| 394 | void __iomem *addr; |
| 395 | u32 reg; |
| 396 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 397 | addr = bank_reg(gpio, bank, reg_val); |
Benjamin Herrenschmidt | ed5cab4 | 2018-05-17 18:12:02 +1000 | [diff] [blame] | 398 | reg = gpio->dcache[GPIO_BANK(offset)]; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 399 | |
| 400 | if (val) |
| 401 | reg |= GPIO_BIT(offset); |
| 402 | else |
| 403 | reg &= ~GPIO_BIT(offset); |
Benjamin Herrenschmidt | ed5cab4 | 2018-05-17 18:12:02 +1000 | [diff] [blame] | 404 | gpio->dcache[GPIO_BANK(offset)] = reg; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 405 | |
| 406 | iowrite32(reg, addr); |
| 407 | } |
| 408 | |
| 409 | static void aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset, |
| 410 | int val) |
| 411 | { |
| 412 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); |
| 413 | unsigned long flags; |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 414 | bool copro; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 415 | |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 416 | raw_spin_lock_irqsave(&gpio->lock, flags); |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 417 | copro = aspeed_gpio_copro_request(gpio, offset); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 418 | |
| 419 | __aspeed_gpio_set(gc, offset, val); |
| 420 | |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 421 | if (copro) |
| 422 | aspeed_gpio_copro_release(gpio, offset); |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 423 | raw_spin_unlock_irqrestore(&gpio->lock, flags); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | static int aspeed_gpio_dir_in(struct gpio_chip *gc, unsigned int offset) |
| 427 | { |
| 428 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); |
| 429 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 430 | void __iomem *addr = bank_reg(gpio, bank, reg_dir); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 431 | unsigned long flags; |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 432 | bool copro; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 433 | u32 reg; |
| 434 | |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 435 | if (!have_input(gpio, offset)) |
| 436 | return -ENOTSUPP; |
| 437 | |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 438 | raw_spin_lock_irqsave(&gpio->lock, flags); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 439 | |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 440 | reg = ioread32(addr); |
| 441 | reg &= ~GPIO_BIT(offset); |
| 442 | |
| 443 | copro = aspeed_gpio_copro_request(gpio, offset); |
| 444 | iowrite32(reg, addr); |
| 445 | if (copro) |
| 446 | aspeed_gpio_copro_release(gpio, offset); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 447 | |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 448 | raw_spin_unlock_irqrestore(&gpio->lock, flags); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 449 | |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | static int aspeed_gpio_dir_out(struct gpio_chip *gc, |
| 454 | unsigned int offset, int val) |
| 455 | { |
| 456 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); |
| 457 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 458 | void __iomem *addr = bank_reg(gpio, bank, reg_dir); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 459 | unsigned long flags; |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 460 | bool copro; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 461 | u32 reg; |
| 462 | |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 463 | if (!have_output(gpio, offset)) |
| 464 | return -ENOTSUPP; |
| 465 | |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 466 | raw_spin_lock_irqsave(&gpio->lock, flags); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 467 | |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 468 | reg = ioread32(addr); |
| 469 | reg |= GPIO_BIT(offset); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 470 | |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 471 | copro = aspeed_gpio_copro_request(gpio, offset); |
| 472 | __aspeed_gpio_set(gc, offset, val); |
| 473 | iowrite32(reg, addr); |
| 474 | |
| 475 | if (copro) |
| 476 | aspeed_gpio_copro_release(gpio, offset); |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 477 | raw_spin_unlock_irqrestore(&gpio->lock, flags); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | static int aspeed_gpio_get_direction(struct gpio_chip *gc, unsigned int offset) |
| 483 | { |
| 484 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); |
| 485 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
| 486 | unsigned long flags; |
| 487 | u32 val; |
| 488 | |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 489 | if (!have_input(gpio, offset)) |
Matti Vaittinen | e42615e | 2019-11-06 10:54:12 +0200 | [diff] [blame] | 490 | return GPIO_LINE_DIRECTION_OUT; |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 491 | |
| 492 | if (!have_output(gpio, offset)) |
Matti Vaittinen | e42615e | 2019-11-06 10:54:12 +0200 | [diff] [blame] | 493 | return GPIO_LINE_DIRECTION_IN; |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 494 | |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 495 | raw_spin_lock_irqsave(&gpio->lock, flags); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 496 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 497 | val = ioread32(bank_reg(gpio, bank, reg_dir)) & GPIO_BIT(offset); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 498 | |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 499 | raw_spin_unlock_irqrestore(&gpio->lock, flags); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 500 | |
Matti Vaittinen | e42615e | 2019-11-06 10:54:12 +0200 | [diff] [blame] | 501 | return val ? GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | static inline int irqd_to_aspeed_gpio_data(struct irq_data *d, |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 505 | struct aspeed_gpio **gpio, |
| 506 | const struct aspeed_gpio_bank **bank, |
| 507 | u32 *bit, int *offset) |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 508 | { |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 509 | struct aspeed_gpio *internal; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 510 | |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 511 | *offset = irqd_to_hwirq(d); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 512 | |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 513 | internal = irq_data_get_irq_chip_data(d); |
| 514 | |
| 515 | /* This might be a bit of a questionable place to check */ |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 516 | if (!have_irq(internal, *offset)) |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 517 | return -ENOTSUPP; |
| 518 | |
| 519 | *gpio = internal; |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 520 | *bank = to_bank(*offset); |
| 521 | *bit = GPIO_BIT(*offset); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | static void aspeed_gpio_irq_ack(struct irq_data *d) |
| 527 | { |
| 528 | const struct aspeed_gpio_bank *bank; |
| 529 | struct aspeed_gpio *gpio; |
| 530 | unsigned long flags; |
| 531 | void __iomem *status_addr; |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 532 | int rc, offset; |
| 533 | bool copro; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 534 | u32 bit; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 535 | |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 536 | rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 537 | if (rc) |
| 538 | return; |
| 539 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 540 | status_addr = bank_reg(gpio, bank, reg_irq_status); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 541 | |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 542 | raw_spin_lock_irqsave(&gpio->lock, flags); |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 543 | copro = aspeed_gpio_copro_request(gpio, offset); |
| 544 | |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 545 | iowrite32(bit, status_addr); |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 546 | |
| 547 | if (copro) |
| 548 | aspeed_gpio_copro_release(gpio, offset); |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 549 | raw_spin_unlock_irqrestore(&gpio->lock, flags); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | static void aspeed_gpio_irq_set_mask(struct irq_data *d, bool set) |
| 553 | { |
| 554 | const struct aspeed_gpio_bank *bank; |
| 555 | struct aspeed_gpio *gpio; |
| 556 | unsigned long flags; |
| 557 | u32 reg, bit; |
| 558 | void __iomem *addr; |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 559 | int rc, offset; |
| 560 | bool copro; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 561 | |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 562 | rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 563 | if (rc) |
| 564 | return; |
| 565 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 566 | addr = bank_reg(gpio, bank, reg_irq_enable); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 567 | |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 568 | raw_spin_lock_irqsave(&gpio->lock, flags); |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 569 | copro = aspeed_gpio_copro_request(gpio, offset); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 570 | |
| 571 | reg = ioread32(addr); |
| 572 | if (set) |
| 573 | reg |= bit; |
| 574 | else |
Govert Overgaauw | f241632 | 2018-04-06 14:41:35 +0200 | [diff] [blame] | 575 | reg &= ~bit; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 576 | iowrite32(reg, addr); |
| 577 | |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 578 | if (copro) |
| 579 | aspeed_gpio_copro_release(gpio, offset); |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 580 | raw_spin_unlock_irqrestore(&gpio->lock, flags); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | static void aspeed_gpio_irq_mask(struct irq_data *d) |
| 584 | { |
| 585 | aspeed_gpio_irq_set_mask(d, false); |
| 586 | } |
| 587 | |
| 588 | static void aspeed_gpio_irq_unmask(struct irq_data *d) |
| 589 | { |
| 590 | aspeed_gpio_irq_set_mask(d, true); |
| 591 | } |
| 592 | |
| 593 | static int aspeed_gpio_set_type(struct irq_data *d, unsigned int type) |
| 594 | { |
| 595 | u32 type0 = 0; |
| 596 | u32 type1 = 0; |
| 597 | u32 type2 = 0; |
| 598 | u32 bit, reg; |
| 599 | const struct aspeed_gpio_bank *bank; |
| 600 | irq_flow_handler_t handler; |
| 601 | struct aspeed_gpio *gpio; |
| 602 | unsigned long flags; |
| 603 | void __iomem *addr; |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 604 | int rc, offset; |
| 605 | bool copro; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 606 | |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 607 | rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 608 | if (rc) |
| 609 | return -EINVAL; |
| 610 | |
| 611 | switch (type & IRQ_TYPE_SENSE_MASK) { |
| 612 | case IRQ_TYPE_EDGE_BOTH: |
| 613 | type2 |= bit; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 614 | fallthrough; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 615 | case IRQ_TYPE_EDGE_RISING: |
| 616 | type0 |= bit; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 617 | fallthrough; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 618 | case IRQ_TYPE_EDGE_FALLING: |
| 619 | handler = handle_edge_irq; |
| 620 | break; |
| 621 | case IRQ_TYPE_LEVEL_HIGH: |
| 622 | type0 |= bit; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 623 | fallthrough; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 624 | case IRQ_TYPE_LEVEL_LOW: |
| 625 | type1 |= bit; |
| 626 | handler = handle_level_irq; |
| 627 | break; |
| 628 | default: |
| 629 | return -EINVAL; |
| 630 | } |
| 631 | |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 632 | raw_spin_lock_irqsave(&gpio->lock, flags); |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 633 | copro = aspeed_gpio_copro_request(gpio, offset); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 634 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 635 | addr = bank_reg(gpio, bank, reg_irq_type0); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 636 | reg = ioread32(addr); |
| 637 | reg = (reg & ~bit) | type0; |
| 638 | iowrite32(reg, addr); |
| 639 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 640 | addr = bank_reg(gpio, bank, reg_irq_type1); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 641 | reg = ioread32(addr); |
| 642 | reg = (reg & ~bit) | type1; |
| 643 | iowrite32(reg, addr); |
| 644 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 645 | addr = bank_reg(gpio, bank, reg_irq_type2); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 646 | reg = ioread32(addr); |
| 647 | reg = (reg & ~bit) | type2; |
| 648 | iowrite32(reg, addr); |
| 649 | |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 650 | if (copro) |
| 651 | aspeed_gpio_copro_release(gpio, offset); |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 652 | raw_spin_unlock_irqrestore(&gpio->lock, flags); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 653 | |
| 654 | irq_set_handler_locked(d, handler); |
| 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | static void aspeed_gpio_irq_handler(struct irq_desc *desc) |
| 660 | { |
| 661 | struct gpio_chip *gc = irq_desc_get_handler_data(desc); |
| 662 | struct irq_chip *ic = irq_desc_get_chip(desc); |
| 663 | struct aspeed_gpio *data = gpiochip_get_data(gc); |
Marc Zyngier | dbd1c54 | 2021-05-04 17:42:18 +0100 | [diff] [blame] | 664 | unsigned int i, p, banks; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 665 | unsigned long reg; |
Rashmica Gupta | ab4a855 | 2019-09-06 16:37:37 +1000 | [diff] [blame] | 666 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 667 | |
| 668 | chained_irq_enter(ic, desc); |
| 669 | |
Rashmica Gupta | ab4a855 | 2019-09-06 16:37:37 +1000 | [diff] [blame] | 670 | banks = DIV_ROUND_UP(gpio->chip.ngpio, 32); |
| 671 | for (i = 0; i < banks; i++) { |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 672 | const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i]; |
| 673 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 674 | reg = ioread32(bank_reg(data, bank, reg_irq_status)); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 675 | |
Marc Zyngier | dbd1c54 | 2021-05-04 17:42:18 +0100 | [diff] [blame] | 676 | for_each_set_bit(p, ®, 32) |
| 677 | generic_handle_domain_irq(gc->irq.domain, i * 32 + p); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | chained_irq_exit(ic, desc); |
| 681 | } |
| 682 | |
Linus Walleij | 5fbe5b5 | 2019-09-04 16:01:04 +0200 | [diff] [blame] | 683 | static void aspeed_init_irq_valid_mask(struct gpio_chip *gc, |
| 684 | unsigned long *valid_mask, |
| 685 | unsigned int ngpios) |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 686 | { |
Linus Walleij | 5fbe5b5 | 2019-09-04 16:01:04 +0200 | [diff] [blame] | 687 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 688 | const struct aspeed_bank_props *props = gpio->config->props; |
| 689 | |
| 690 | while (!is_bank_props_sentinel(props)) { |
| 691 | unsigned int offset; |
| 692 | const unsigned long int input = props->input; |
| 693 | |
| 694 | /* Pretty crummy approach, but similar to GPIO core */ |
| 695 | for_each_clear_bit(offset, &input, 32) { |
| 696 | unsigned int i = props->bank * 32 + offset; |
| 697 | |
Rashmica Gupta | be2a7e2 | 2019-09-06 16:27:26 +1000 | [diff] [blame] | 698 | if (i >= gpio->chip.ngpio) |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 699 | break; |
| 700 | |
Linus Walleij | 5fbe5b5 | 2019-09-04 16:01:04 +0200 | [diff] [blame] | 701 | clear_bit(i, valid_mask); |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | props++; |
| 705 | } |
| 706 | } |
| 707 | |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 708 | static int aspeed_gpio_reset_tolerance(struct gpio_chip *chip, |
| 709 | unsigned int offset, bool enable) |
| 710 | { |
| 711 | struct aspeed_gpio *gpio = gpiochip_get_data(chip); |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 712 | unsigned long flags; |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 713 | void __iomem *treg; |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 714 | bool copro; |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 715 | u32 val; |
| 716 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 717 | treg = bank_reg(gpio, to_bank(offset), reg_tolerance); |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 718 | |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 719 | raw_spin_lock_irqsave(&gpio->lock, flags); |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 720 | copro = aspeed_gpio_copro_request(gpio, offset); |
| 721 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 722 | val = readl(treg); |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 723 | |
| 724 | if (enable) |
| 725 | val |= GPIO_BIT(offset); |
| 726 | else |
| 727 | val &= ~GPIO_BIT(offset); |
| 728 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 729 | writel(val, treg); |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 730 | |
| 731 | if (copro) |
| 732 | aspeed_gpio_copro_release(gpio, offset); |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 733 | raw_spin_unlock_irqrestore(&gpio->lock, flags); |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 734 | |
| 735 | return 0; |
| 736 | } |
| 737 | |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 738 | static int aspeed_gpio_request(struct gpio_chip *chip, unsigned int offset) |
| 739 | { |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 740 | if (!have_gpio(gpiochip_get_data(chip), offset)) |
| 741 | return -ENODEV; |
| 742 | |
Linus Walleij | a9a1d2a | 2017-09-22 11:02:10 +0200 | [diff] [blame] | 743 | return pinctrl_gpio_request(chip->base + offset); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | static void aspeed_gpio_free(struct gpio_chip *chip, unsigned int offset) |
| 747 | { |
Linus Walleij | a9a1d2a | 2017-09-22 11:02:10 +0200 | [diff] [blame] | 748 | pinctrl_gpio_free(chip->base + offset); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 749 | } |
| 750 | |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 751 | static int usecs_to_cycles(struct aspeed_gpio *gpio, unsigned long usecs, |
| 752 | u32 *cycles) |
| 753 | { |
| 754 | u64 rate; |
| 755 | u64 n; |
| 756 | u32 r; |
| 757 | |
| 758 | rate = clk_get_rate(gpio->clk); |
| 759 | if (!rate) |
| 760 | return -ENOTSUPP; |
| 761 | |
| 762 | n = rate * usecs; |
| 763 | r = do_div(n, 1000000); |
| 764 | |
| 765 | if (n >= U32_MAX) |
| 766 | return -ERANGE; |
| 767 | |
| 768 | /* At least as long as the requested time */ |
| 769 | *cycles = n + (!!r); |
| 770 | |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | /* Call under gpio->lock */ |
| 775 | static int register_allocated_timer(struct aspeed_gpio *gpio, |
| 776 | unsigned int offset, unsigned int timer) |
| 777 | { |
| 778 | if (WARN(gpio->offset_timer[offset] != 0, |
| 779 | "Offset %d already allocated timer %d\n", |
| 780 | offset, gpio->offset_timer[offset])) |
| 781 | return -EINVAL; |
| 782 | |
| 783 | if (WARN(gpio->timer_users[timer] == UINT_MAX, |
| 784 | "Timer user count would overflow\n")) |
| 785 | return -EPERM; |
| 786 | |
| 787 | gpio->offset_timer[offset] = timer; |
| 788 | gpio->timer_users[timer]++; |
| 789 | |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | /* Call under gpio->lock */ |
| 794 | static int unregister_allocated_timer(struct aspeed_gpio *gpio, |
| 795 | unsigned int offset) |
| 796 | { |
| 797 | if (WARN(gpio->offset_timer[offset] == 0, |
| 798 | "No timer allocated to offset %d\n", offset)) |
| 799 | return -EINVAL; |
| 800 | |
| 801 | if (WARN(gpio->timer_users[gpio->offset_timer[offset]] == 0, |
| 802 | "No users recorded for timer %d\n", |
| 803 | gpio->offset_timer[offset])) |
| 804 | return -EINVAL; |
| 805 | |
| 806 | gpio->timer_users[gpio->offset_timer[offset]]--; |
| 807 | gpio->offset_timer[offset] = 0; |
| 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | /* Call under gpio->lock */ |
| 813 | static inline bool timer_allocation_registered(struct aspeed_gpio *gpio, |
| 814 | unsigned int offset) |
| 815 | { |
| 816 | return gpio->offset_timer[offset] > 0; |
| 817 | } |
| 818 | |
| 819 | /* Call under gpio->lock */ |
| 820 | static void configure_timer(struct aspeed_gpio *gpio, unsigned int offset, |
| 821 | unsigned int timer) |
| 822 | { |
| 823 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
| 824 | const u32 mask = GPIO_BIT(offset); |
| 825 | void __iomem *addr; |
| 826 | u32 val; |
| 827 | |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 828 | /* Note: Debounce timer isn't under control of the command |
| 829 | * source registers, so no need to sync with the coprocessor |
| 830 | */ |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 831 | addr = bank_reg(gpio, bank, reg_debounce_sel1); |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 832 | val = ioread32(addr); |
| 833 | iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE1(timer, offset), addr); |
| 834 | |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 835 | addr = bank_reg(gpio, bank, reg_debounce_sel2); |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 836 | val = ioread32(addr); |
| 837 | iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE2(timer, offset), addr); |
| 838 | } |
| 839 | |
| 840 | static int enable_debounce(struct gpio_chip *chip, unsigned int offset, |
| 841 | unsigned long usecs) |
| 842 | { |
| 843 | struct aspeed_gpio *gpio = gpiochip_get_data(chip); |
| 844 | u32 requested_cycles; |
| 845 | unsigned long flags; |
| 846 | int rc; |
| 847 | int i; |
| 848 | |
Joel Stanley | df563c8 | 2017-05-02 15:38:24 +0930 | [diff] [blame] | 849 | if (!gpio->clk) |
| 850 | return -EINVAL; |
| 851 | |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 852 | rc = usecs_to_cycles(gpio, usecs, &requested_cycles); |
| 853 | if (rc < 0) { |
| 854 | dev_warn(chip->parent, "Failed to convert %luus to cycles at %luHz: %d\n", |
| 855 | usecs, clk_get_rate(gpio->clk), rc); |
| 856 | return rc; |
| 857 | } |
| 858 | |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 859 | raw_spin_lock_irqsave(&gpio->lock, flags); |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 860 | |
| 861 | if (timer_allocation_registered(gpio, offset)) { |
| 862 | rc = unregister_allocated_timer(gpio, offset); |
| 863 | if (rc < 0) |
| 864 | goto out; |
| 865 | } |
| 866 | |
| 867 | /* Try to find a timer already configured for the debounce period */ |
| 868 | for (i = 1; i < ARRAY_SIZE(debounce_timers); i++) { |
| 869 | u32 cycles; |
| 870 | |
| 871 | cycles = ioread32(gpio->base + debounce_timers[i]); |
| 872 | if (requested_cycles == cycles) |
| 873 | break; |
| 874 | } |
| 875 | |
| 876 | if (i == ARRAY_SIZE(debounce_timers)) { |
| 877 | int j; |
| 878 | |
| 879 | /* |
| 880 | * As there are no timers configured for the requested debounce |
| 881 | * period, find an unused timer instead |
| 882 | */ |
| 883 | for (j = 1; j < ARRAY_SIZE(gpio->timer_users); j++) { |
| 884 | if (gpio->timer_users[j] == 0) |
| 885 | break; |
| 886 | } |
| 887 | |
| 888 | if (j == ARRAY_SIZE(gpio->timer_users)) { |
| 889 | dev_warn(chip->parent, |
| 890 | "Debounce timers exhausted, cannot debounce for period %luus\n", |
| 891 | usecs); |
| 892 | |
| 893 | rc = -EPERM; |
| 894 | |
| 895 | /* |
| 896 | * We already adjusted the accounting to remove @offset |
| 897 | * as a user of its previous timer, so also configure |
| 898 | * the hardware so @offset has timers disabled for |
| 899 | * consistency. |
| 900 | */ |
| 901 | configure_timer(gpio, offset, 0); |
| 902 | goto out; |
| 903 | } |
| 904 | |
| 905 | i = j; |
| 906 | |
| 907 | iowrite32(requested_cycles, gpio->base + debounce_timers[i]); |
| 908 | } |
| 909 | |
| 910 | if (WARN(i == 0, "Cannot register index of disabled timer\n")) { |
| 911 | rc = -EINVAL; |
| 912 | goto out; |
| 913 | } |
| 914 | |
| 915 | register_allocated_timer(gpio, offset, i); |
| 916 | configure_timer(gpio, offset, i); |
| 917 | |
| 918 | out: |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 919 | raw_spin_unlock_irqrestore(&gpio->lock, flags); |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 920 | |
| 921 | return rc; |
| 922 | } |
| 923 | |
| 924 | static int disable_debounce(struct gpio_chip *chip, unsigned int offset) |
| 925 | { |
| 926 | struct aspeed_gpio *gpio = gpiochip_get_data(chip); |
| 927 | unsigned long flags; |
| 928 | int rc; |
| 929 | |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 930 | raw_spin_lock_irqsave(&gpio->lock, flags); |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 931 | |
| 932 | rc = unregister_allocated_timer(gpio, offset); |
| 933 | if (!rc) |
| 934 | configure_timer(gpio, offset, 0); |
| 935 | |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 936 | raw_spin_unlock_irqrestore(&gpio->lock, flags); |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 937 | |
| 938 | return rc; |
| 939 | } |
| 940 | |
| 941 | static int set_debounce(struct gpio_chip *chip, unsigned int offset, |
| 942 | unsigned long usecs) |
| 943 | { |
| 944 | struct aspeed_gpio *gpio = gpiochip_get_data(chip); |
| 945 | |
| 946 | if (!have_debounce(gpio, offset)) |
| 947 | return -ENOTSUPP; |
| 948 | |
| 949 | if (usecs) |
| 950 | return enable_debounce(chip, offset, usecs); |
| 951 | |
| 952 | return disable_debounce(chip, offset); |
| 953 | } |
| 954 | |
| 955 | static int aspeed_gpio_set_config(struct gpio_chip *chip, unsigned int offset, |
| 956 | unsigned long config) |
| 957 | { |
| 958 | unsigned long param = pinconf_to_config_param(config); |
| 959 | u32 arg = pinconf_to_config_argument(config); |
| 960 | |
| 961 | if (param == PIN_CONFIG_INPUT_DEBOUNCE) |
| 962 | return set_debounce(chip, offset, arg); |
| 963 | else if (param == PIN_CONFIG_BIAS_DISABLE || |
| 964 | param == PIN_CONFIG_BIAS_PULL_DOWN || |
| 965 | param == PIN_CONFIG_DRIVE_STRENGTH) |
| 966 | return pinctrl_gpio_set_config(offset, config); |
Andrew Jeffery | c3bafe0 | 2017-04-07 22:29:02 +0930 | [diff] [blame] | 967 | else if (param == PIN_CONFIG_DRIVE_OPEN_DRAIN || |
| 968 | param == PIN_CONFIG_DRIVE_OPEN_SOURCE) |
| 969 | /* Return -ENOTSUPP to trigger emulation, as per datasheet */ |
| 970 | return -ENOTSUPP; |
Andrew Jeffery | 1b43d26 | 2017-11-30 14:25:25 +1030 | [diff] [blame] | 971 | else if (param == PIN_CONFIG_PERSIST_STATE) |
| 972 | return aspeed_gpio_reset_tolerance(chip, offset, arg); |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 973 | |
| 974 | return -ENOTSUPP; |
| 975 | } |
| 976 | |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 977 | /** |
Sachin agarwal | 0d311d8 | 2020-01-18 16:23:19 +0530 | [diff] [blame] | 978 | * aspeed_gpio_copro_set_ops - Sets the callbacks used for handshaking with |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 979 | * the coprocessor for shared GPIO banks |
| 980 | * @ops: The callbacks |
| 981 | * @data: Pointer passed back to the callbacks |
| 982 | */ |
| 983 | int aspeed_gpio_copro_set_ops(const struct aspeed_gpio_copro_ops *ops, void *data) |
| 984 | { |
| 985 | copro_data = data; |
| 986 | copro_ops = ops; |
| 987 | |
| 988 | return 0; |
| 989 | } |
| 990 | EXPORT_SYMBOL_GPL(aspeed_gpio_copro_set_ops); |
| 991 | |
| 992 | /** |
| 993 | * aspeed_gpio_copro_grab_gpio - Mark a GPIO used by the coprocessor. The entire |
| 994 | * bank gets marked and any access from the ARM will |
| 995 | * result in handshaking via callbacks. |
| 996 | * @desc: The GPIO to be marked |
| 997 | * @vreg_offset: If non-NULL, returns the value register offset in the GPIO space |
| 998 | * @dreg_offset: If non-NULL, returns the data latch register offset in the GPIO space |
| 999 | * @bit: If non-NULL, returns the bit number of the GPIO in the registers |
| 1000 | */ |
| 1001 | int aspeed_gpio_copro_grab_gpio(struct gpio_desc *desc, |
| 1002 | u16 *vreg_offset, u16 *dreg_offset, u8 *bit) |
| 1003 | { |
| 1004 | struct gpio_chip *chip = gpiod_to_chip(desc); |
| 1005 | struct aspeed_gpio *gpio = gpiochip_get_data(chip); |
| 1006 | int rc = 0, bindex, offset = gpio_chip_hwgpio(desc); |
| 1007 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
| 1008 | unsigned long flags; |
| 1009 | |
| 1010 | if (!gpio->cf_copro_bankmap) |
Rashmica Gupta | be2a7e2 | 2019-09-06 16:27:26 +1000 | [diff] [blame] | 1011 | gpio->cf_copro_bankmap = kzalloc(gpio->chip.ngpio >> 3, GFP_KERNEL); |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 1012 | if (!gpio->cf_copro_bankmap) |
| 1013 | return -ENOMEM; |
Rashmica Gupta | be2a7e2 | 2019-09-06 16:27:26 +1000 | [diff] [blame] | 1014 | if (offset < 0 || offset > gpio->chip.ngpio) |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 1015 | return -EINVAL; |
| 1016 | bindex = offset >> 3; |
| 1017 | |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 1018 | raw_spin_lock_irqsave(&gpio->lock, flags); |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 1019 | |
| 1020 | /* Sanity check, this shouldn't happen */ |
| 1021 | if (gpio->cf_copro_bankmap[bindex] == 0xff) { |
| 1022 | rc = -EIO; |
| 1023 | goto bail; |
| 1024 | } |
| 1025 | gpio->cf_copro_bankmap[bindex]++; |
| 1026 | |
| 1027 | /* Switch command source */ |
| 1028 | if (gpio->cf_copro_bankmap[bindex] == 1) |
| 1029 | aspeed_gpio_change_cmd_source(gpio, bank, bindex, |
| 1030 | GPIO_CMDSRC_COLDFIRE); |
| 1031 | |
| 1032 | if (vreg_offset) |
| 1033 | *vreg_offset = bank->val_regs; |
| 1034 | if (dreg_offset) |
| 1035 | *dreg_offset = bank->rdata_reg; |
| 1036 | if (bit) |
| 1037 | *bit = GPIO_OFFSET(offset); |
| 1038 | bail: |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 1039 | raw_spin_unlock_irqrestore(&gpio->lock, flags); |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 1040 | return rc; |
| 1041 | } |
| 1042 | EXPORT_SYMBOL_GPL(aspeed_gpio_copro_grab_gpio); |
| 1043 | |
| 1044 | /** |
| 1045 | * aspeed_gpio_copro_release_gpio - Unmark a GPIO used by the coprocessor. |
| 1046 | * @desc: The GPIO to be marked |
| 1047 | */ |
| 1048 | int aspeed_gpio_copro_release_gpio(struct gpio_desc *desc) |
| 1049 | { |
| 1050 | struct gpio_chip *chip = gpiod_to_chip(desc); |
| 1051 | struct aspeed_gpio *gpio = gpiochip_get_data(chip); |
| 1052 | int rc = 0, bindex, offset = gpio_chip_hwgpio(desc); |
| 1053 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
| 1054 | unsigned long flags; |
| 1055 | |
| 1056 | if (!gpio->cf_copro_bankmap) |
| 1057 | return -ENXIO; |
| 1058 | |
Rashmica Gupta | be2a7e2 | 2019-09-06 16:27:26 +1000 | [diff] [blame] | 1059 | if (offset < 0 || offset > gpio->chip.ngpio) |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 1060 | return -EINVAL; |
| 1061 | bindex = offset >> 3; |
| 1062 | |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 1063 | raw_spin_lock_irqsave(&gpio->lock, flags); |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 1064 | |
| 1065 | /* Sanity check, this shouldn't happen */ |
| 1066 | if (gpio->cf_copro_bankmap[bindex] == 0) { |
| 1067 | rc = -EIO; |
| 1068 | goto bail; |
| 1069 | } |
| 1070 | gpio->cf_copro_bankmap[bindex]--; |
| 1071 | |
| 1072 | /* Switch command source */ |
| 1073 | if (gpio->cf_copro_bankmap[bindex] == 0) |
| 1074 | aspeed_gpio_change_cmd_source(gpio, bank, bindex, |
| 1075 | GPIO_CMDSRC_ARM); |
| 1076 | bail: |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 1077 | raw_spin_unlock_irqrestore(&gpio->lock, flags); |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 1078 | return rc; |
| 1079 | } |
| 1080 | EXPORT_SYMBOL_GPL(aspeed_gpio_copro_release_gpio); |
| 1081 | |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 1082 | /* |
| 1083 | * Any banks not specified in a struct aspeed_bank_props array are assumed to |
| 1084 | * have the properties: |
| 1085 | * |
| 1086 | * { .input = 0xffffffff, .output = 0xffffffff } |
| 1087 | */ |
| 1088 | |
| 1089 | static const struct aspeed_bank_props ast2400_bank_props[] = { |
| 1090 | /* input output */ |
| 1091 | { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */ |
| 1092 | { 6, 0x0000000f, 0x0fffff0f }, /* Y/Z/AA/AB, two 4-GPIO holes */ |
| 1093 | { }, |
| 1094 | }; |
| 1095 | |
| 1096 | static const struct aspeed_gpio_config ast2400_config = |
| 1097 | /* 220 for simplicity, really 216 with two 4-GPIO holes, four at end */ |
| 1098 | { .nr_gpios = 220, .props = ast2400_bank_props, }; |
| 1099 | |
| 1100 | static const struct aspeed_bank_props ast2500_bank_props[] = { |
| 1101 | /* input output */ |
| 1102 | { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */ |
| 1103 | { 6, 0x0fffffff, 0x0fffffff }, /* Y/Z/AA/AB, 4-GPIO hole */ |
| 1104 | { 7, 0x000000ff, 0x000000ff }, /* AC */ |
| 1105 | { }, |
| 1106 | }; |
| 1107 | |
| 1108 | static const struct aspeed_gpio_config ast2500_config = |
| 1109 | /* 232 for simplicity, actual number is 228 (4-GPIO hole in GPIOAB) */ |
| 1110 | { .nr_gpios = 232, .props = ast2500_bank_props, }; |
| 1111 | |
Rashmica Gupta | ab4a855 | 2019-09-06 16:37:37 +1000 | [diff] [blame] | 1112 | static const struct aspeed_bank_props ast2600_bank_props[] = { |
| 1113 | /* input output */ |
Billy Tsai | 560b6ac | 2020-10-19 12:50:26 +0800 | [diff] [blame] | 1114 | {4, 0xffffffff, 0x00ffffff}, /* Q/R/S/T */ |
Tao Ren | 3e640b1 | 2020-09-16 13:42:16 -0700 | [diff] [blame] | 1115 | {5, 0xffffffff, 0xffffff00}, /* U/V/W/X */ |
| 1116 | {6, 0x0000ffff, 0x0000ffff}, /* Y/Z */ |
Rashmica Gupta | ab4a855 | 2019-09-06 16:37:37 +1000 | [diff] [blame] | 1117 | { }, |
| 1118 | }; |
| 1119 | |
| 1120 | static const struct aspeed_gpio_config ast2600_config = |
| 1121 | /* |
| 1122 | * ast2600 has two controllers one with 208 GPIOs and one with 36 GPIOs. |
| 1123 | * We expect ngpio being set in the device tree and this is a fallback |
| 1124 | * option. |
| 1125 | */ |
| 1126 | { .nr_gpios = 208, .props = ast2600_bank_props, }; |
| 1127 | |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 1128 | static const struct of_device_id aspeed_gpio_of_table[] = { |
| 1129 | { .compatible = "aspeed,ast2400-gpio", .data = &ast2400_config, }, |
| 1130 | { .compatible = "aspeed,ast2500-gpio", .data = &ast2500_config, }, |
Rashmica Gupta | ab4a855 | 2019-09-06 16:37:37 +1000 | [diff] [blame] | 1131 | { .compatible = "aspeed,ast2600-gpio", .data = &ast2600_config, }, |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 1132 | {} |
| 1133 | }; |
| 1134 | MODULE_DEVICE_TABLE(of, aspeed_gpio_of_table); |
| 1135 | |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 1136 | static int __init aspeed_gpio_probe(struct platform_device *pdev) |
| 1137 | { |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 1138 | const struct of_device_id *gpio_id; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 1139 | struct aspeed_gpio *gpio; |
Rashmica Gupta | be2a7e2 | 2019-09-06 16:27:26 +1000 | [diff] [blame] | 1140 | int rc, i, banks, err; |
| 1141 | u32 ngpio; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 1142 | |
| 1143 | gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL); |
| 1144 | if (!gpio) |
| 1145 | return -ENOMEM; |
| 1146 | |
Enrico Weigelt, metux IT consult | aee70b7 | 2019-03-11 19:54:43 +0100 | [diff] [blame] | 1147 | gpio->base = devm_platform_ioremap_resource(pdev, 0); |
Wei Yongjun | 7f8b965 | 2016-09-15 01:30:32 +0000 | [diff] [blame] | 1148 | if (IS_ERR(gpio->base)) |
| 1149 | return PTR_ERR(gpio->base); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 1150 | |
Iwona Winiarska | 61a7904 | 2021-12-04 18:10:26 +0100 | [diff] [blame] | 1151 | raw_spin_lock_init(&gpio->lock); |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 1152 | |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 1153 | gpio_id = of_match_node(aspeed_gpio_of_table, pdev->dev.of_node); |
| 1154 | if (!gpio_id) |
| 1155 | return -EINVAL; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 1156 | |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 1157 | gpio->clk = of_clk_get(pdev->dev.of_node, 0); |
| 1158 | if (IS_ERR(gpio->clk)) { |
| 1159 | dev_warn(&pdev->dev, |
Andrew Jeffery | 754c045 | 2017-08-08 15:37:36 +0930 | [diff] [blame] | 1160 | "Failed to get clock from devicetree, debouncing disabled\n"); |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 1161 | gpio->clk = NULL; |
| 1162 | } |
| 1163 | |
Andrew Jeffery | 1736f75 | 2017-01-24 16:46:46 +1030 | [diff] [blame] | 1164 | gpio->config = gpio_id->data; |
| 1165 | |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 1166 | gpio->chip.parent = &pdev->dev; |
Rashmica Gupta | be2a7e2 | 2019-09-06 16:27:26 +1000 | [diff] [blame] | 1167 | err = of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpio); |
| 1168 | gpio->chip.ngpio = (u16) ngpio; |
| 1169 | if (err) |
| 1170 | gpio->chip.ngpio = gpio->config->nr_gpios; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 1171 | gpio->chip.direction_input = aspeed_gpio_dir_in; |
| 1172 | gpio->chip.direction_output = aspeed_gpio_dir_out; |
| 1173 | gpio->chip.get_direction = aspeed_gpio_get_direction; |
| 1174 | gpio->chip.request = aspeed_gpio_request; |
| 1175 | gpio->chip.free = aspeed_gpio_free; |
| 1176 | gpio->chip.get = aspeed_gpio_get; |
| 1177 | gpio->chip.set = aspeed_gpio_set; |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 1178 | gpio->chip.set_config = aspeed_gpio_set_config; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 1179 | gpio->chip.label = dev_name(&pdev->dev); |
| 1180 | gpio->chip.base = -1; |
| 1181 | |
Benjamin Herrenschmidt | ed5cab4 | 2018-05-17 18:12:02 +1000 | [diff] [blame] | 1182 | /* Allocate a cache of the output registers */ |
Rashmica Gupta | be2a7e2 | 2019-09-06 16:27:26 +1000 | [diff] [blame] | 1183 | banks = DIV_ROUND_UP(gpio->chip.ngpio, 32); |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 1184 | gpio->dcache = devm_kcalloc(&pdev->dev, |
| 1185 | banks, sizeof(u32), GFP_KERNEL); |
Benjamin Herrenschmidt | ed5cab4 | 2018-05-17 18:12:02 +1000 | [diff] [blame] | 1186 | if (!gpio->dcache) |
| 1187 | return -ENOMEM; |
| 1188 | |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 1189 | /* |
| 1190 | * Populate it with initial values read from the HW and switch |
| 1191 | * all command sources to the ARM by default |
| 1192 | */ |
Benjamin Herrenschmidt | ed5cab4 | 2018-05-17 18:12:02 +1000 | [diff] [blame] | 1193 | for (i = 0; i < banks; i++) { |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 1194 | const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i]; |
| 1195 | void __iomem *addr = bank_reg(gpio, bank, reg_rdata); |
Benjamin Herrenschmidt | 44ddf55 | 2018-06-29 14:11:16 +1000 | [diff] [blame] | 1196 | gpio->dcache[i] = ioread32(addr); |
Benjamin Herrenschmidt | a7ca138 | 2018-06-29 14:11:19 +1000 | [diff] [blame] | 1197 | aspeed_gpio_change_cmd_source(gpio, bank, 0, GPIO_CMDSRC_ARM); |
| 1198 | aspeed_gpio_change_cmd_source(gpio, bank, 1, GPIO_CMDSRC_ARM); |
| 1199 | aspeed_gpio_change_cmd_source(gpio, bank, 2, GPIO_CMDSRC_ARM); |
| 1200 | aspeed_gpio_change_cmd_source(gpio, bank, 3, GPIO_CMDSRC_ARM); |
Benjamin Herrenschmidt | ed5cab4 | 2018-05-17 18:12:02 +1000 | [diff] [blame] | 1201 | } |
| 1202 | |
Linus Walleij | 8512ee3 | 2019-08-09 14:55:15 +0200 | [diff] [blame] | 1203 | /* Optionally set up an irqchip if there is an IRQ */ |
| 1204 | rc = platform_get_irq(pdev, 0); |
| 1205 | if (rc > 0) { |
| 1206 | struct gpio_irq_chip *girq; |
| 1207 | |
| 1208 | gpio->irq = rc; |
| 1209 | girq = &gpio->chip.irq; |
Rashmica Gupta | 3d64a5a | 2019-09-06 16:26:44 +1000 | [diff] [blame] | 1210 | girq->chip = &gpio->irqc; |
| 1211 | girq->chip->name = dev_name(&pdev->dev); |
| 1212 | girq->chip->irq_ack = aspeed_gpio_irq_ack; |
| 1213 | girq->chip->irq_mask = aspeed_gpio_irq_mask; |
| 1214 | girq->chip->irq_unmask = aspeed_gpio_irq_unmask; |
| 1215 | girq->chip->irq_set_type = aspeed_gpio_set_type; |
Linus Walleij | 8512ee3 | 2019-08-09 14:55:15 +0200 | [diff] [blame] | 1216 | girq->parent_handler = aspeed_gpio_irq_handler; |
| 1217 | girq->num_parents = 1; |
| 1218 | girq->parents = devm_kcalloc(&pdev->dev, 1, |
| 1219 | sizeof(*girq->parents), |
| 1220 | GFP_KERNEL); |
| 1221 | if (!girq->parents) |
| 1222 | return -ENOMEM; |
| 1223 | girq->parents[0] = gpio->irq; |
| 1224 | girq->default_type = IRQ_TYPE_NONE; |
| 1225 | girq->handler = handle_bad_irq; |
Linus Walleij | 5fbe5b5 | 2019-09-04 16:01:04 +0200 | [diff] [blame] | 1226 | girq->init_valid_mask = aspeed_init_irq_valid_mask; |
Linus Walleij | 8512ee3 | 2019-08-09 14:55:15 +0200 | [diff] [blame] | 1227 | } |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 1228 | |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 1229 | gpio->offset_timer = |
| 1230 | devm_kzalloc(&pdev->dev, gpio->chip.ngpio, GFP_KERNEL); |
Kangjie Lu | 6cf4511 | 2019-03-24 18:10:02 -0500 | [diff] [blame] | 1231 | if (!gpio->offset_timer) |
| 1232 | return -ENOMEM; |
Andrew Jeffery | 5ae4cb9 | 2017-04-07 22:29:01 +0930 | [diff] [blame] | 1233 | |
Linus Walleij | 8512ee3 | 2019-08-09 14:55:15 +0200 | [diff] [blame] | 1234 | rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio); |
| 1235 | if (rc < 0) |
| 1236 | return rc; |
| 1237 | |
Linus Walleij | 8512ee3 | 2019-08-09 14:55:15 +0200 | [diff] [blame] | 1238 | return 0; |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 1239 | } |
| 1240 | |
Joel Stanley | 361b791 | 2016-08-30 17:24:27 +0930 | [diff] [blame] | 1241 | static struct platform_driver aspeed_gpio_driver = { |
| 1242 | .driver = { |
| 1243 | .name = KBUILD_MODNAME, |
| 1244 | .of_match_table = aspeed_gpio_of_table, |
| 1245 | }, |
| 1246 | }; |
| 1247 | |
| 1248 | module_platform_driver_probe(aspeed_gpio_driver, aspeed_gpio_probe); |
| 1249 | |
| 1250 | MODULE_DESCRIPTION("Aspeed GPIO Driver"); |
Linus Walleij | e50237c | 2016-09-13 13:43:34 +0200 | [diff] [blame] | 1251 | MODULE_LICENSE("GPL"); |