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