diff options
Diffstat (limited to 'drivers/gpio/gpio-aspeed.c')
-rw-r--r-- | drivers/gpio/gpio-aspeed.c | 426 |
1 files changed, 368 insertions, 58 deletions
diff --git a/drivers/gpio/gpio-aspeed.c b/drivers/gpio/gpio-aspeed.c index b31ae16170e7..2342e154029b 100644 --- a/drivers/gpio/gpio-aspeed.c +++ b/drivers/gpio/gpio-aspeed.c | |||
@@ -12,6 +12,7 @@ | |||
12 | #include <asm/div64.h> | 12 | #include <asm/div64.h> |
13 | #include <linux/clk.h> | 13 | #include <linux/clk.h> |
14 | #include <linux/gpio/driver.h> | 14 | #include <linux/gpio/driver.h> |
15 | #include <linux/gpio/aspeed.h> | ||
15 | #include <linux/hashtable.h> | 16 | #include <linux/hashtable.h> |
16 | #include <linux/init.h> | 17 | #include <linux/init.h> |
17 | #include <linux/io.h> | 18 | #include <linux/io.h> |
@@ -22,6 +23,15 @@ | |||
22 | #include <linux/spinlock.h> | 23 | #include <linux/spinlock.h> |
23 | #include <linux/string.h> | 24 | #include <linux/string.h> |
24 | 25 | ||
26 | /* | ||
27 | * These two headers aren't meant to be used by GPIO drivers. We need | ||
28 | * them in order to access gpio_chip_hwgpio() which we need to implement | ||
29 | * the aspeed specific API which allows the coprocessor to request | ||
30 | * access to some GPIOs and to arbitrate between coprocessor and ARM. | ||
31 | */ | ||
32 | #include <linux/gpio/consumer.h> | ||
33 | #include "gpiolib.h" | ||
34 | |||
25 | struct aspeed_bank_props { | 35 | struct aspeed_bank_props { |
26 | unsigned int bank; | 36 | unsigned int bank; |
27 | u32 input; | 37 | u32 input; |
@@ -56,83 +66,130 @@ struct aspeed_gpio { | |||
56 | struct clk *clk; | 66 | struct clk *clk; |
57 | 67 | ||
58 | u32 *dcache; | 68 | u32 *dcache; |
69 | u8 *cf_copro_bankmap; | ||
59 | }; | 70 | }; |
60 | 71 | ||
61 | struct aspeed_gpio_bank { | 72 | struct aspeed_gpio_bank { |
62 | uint16_t val_regs; | 73 | uint16_t val_regs; /* +0: Rd: read input value, Wr: set write latch |
74 | * +4: Rd/Wr: Direction (0=in, 1=out) | ||
75 | */ | ||
76 | uint16_t rdata_reg; /* Rd: read write latch, Wr: <none> */ | ||
63 | uint16_t irq_regs; | 77 | uint16_t irq_regs; |
64 | uint16_t debounce_regs; | 78 | uint16_t debounce_regs; |
65 | uint16_t tolerance_regs; | 79 | uint16_t tolerance_regs; |
80 | uint16_t cmdsrc_regs; | ||
66 | const char names[4][3]; | 81 | const char names[4][3]; |
67 | }; | 82 | }; |
68 | 83 | ||
84 | /* | ||
85 | * Note: The "value" register returns the input value sampled on the | ||
86 | * line even when the GPIO is configured as an output. Since | ||
87 | * that input goes through synchronizers, writing, then reading | ||
88 | * back may not return the written value right away. | ||
89 | * | ||
90 | * The "rdata" register returns the content of the write latch | ||
91 | * and thus can be used to read back what was last written | ||
92 | * reliably. | ||
93 | */ | ||
94 | |||
69 | static const int debounce_timers[4] = { 0x00, 0x50, 0x54, 0x58 }; | 95 | static const int debounce_timers[4] = { 0x00, 0x50, 0x54, 0x58 }; |
70 | 96 | ||
97 | static const struct aspeed_gpio_copro_ops *copro_ops; | ||
98 | static void *copro_data; | ||
99 | |||
71 | static const struct aspeed_gpio_bank aspeed_gpio_banks[] = { | 100 | static const struct aspeed_gpio_bank aspeed_gpio_banks[] = { |
72 | { | 101 | { |
73 | .val_regs = 0x0000, | 102 | .val_regs = 0x0000, |
103 | .rdata_reg = 0x00c0, | ||
74 | .irq_regs = 0x0008, | 104 | .irq_regs = 0x0008, |
75 | .debounce_regs = 0x0040, | 105 | .debounce_regs = 0x0040, |
76 | .tolerance_regs = 0x001c, | 106 | .tolerance_regs = 0x001c, |
107 | .cmdsrc_regs = 0x0060, | ||
77 | .names = { "A", "B", "C", "D" }, | 108 | .names = { "A", "B", "C", "D" }, |
78 | }, | 109 | }, |
79 | { | 110 | { |
80 | .val_regs = 0x0020, | 111 | .val_regs = 0x0020, |
112 | .rdata_reg = 0x00c4, | ||
81 | .irq_regs = 0x0028, | 113 | .irq_regs = 0x0028, |
82 | .debounce_regs = 0x0048, | 114 | .debounce_regs = 0x0048, |
83 | .tolerance_regs = 0x003c, | 115 | .tolerance_regs = 0x003c, |
116 | .cmdsrc_regs = 0x0068, | ||
84 | .names = { "E", "F", "G", "H" }, | 117 | .names = { "E", "F", "G", "H" }, |
85 | }, | 118 | }, |
86 | { | 119 | { |
87 | .val_regs = 0x0070, | 120 | .val_regs = 0x0070, |
121 | .rdata_reg = 0x00c8, | ||
88 | .irq_regs = 0x0098, | 122 | .irq_regs = 0x0098, |
89 | .debounce_regs = 0x00b0, | 123 | .debounce_regs = 0x00b0, |
90 | .tolerance_regs = 0x00ac, | 124 | .tolerance_regs = 0x00ac, |
125 | .cmdsrc_regs = 0x0090, | ||
91 | .names = { "I", "J", "K", "L" }, | 126 | .names = { "I", "J", "K", "L" }, |
92 | }, | 127 | }, |
93 | { | 128 | { |
94 | .val_regs = 0x0078, | 129 | .val_regs = 0x0078, |
130 | .rdata_reg = 0x00cc, | ||
95 | .irq_regs = 0x00e8, | 131 | .irq_regs = 0x00e8, |
96 | .debounce_regs = 0x0100, | 132 | .debounce_regs = 0x0100, |
97 | .tolerance_regs = 0x00fc, | 133 | .tolerance_regs = 0x00fc, |
134 | .cmdsrc_regs = 0x00e0, | ||
98 | .names = { "M", "N", "O", "P" }, | 135 | .names = { "M", "N", "O", "P" }, |
99 | }, | 136 | }, |
100 | { | 137 | { |
101 | .val_regs = 0x0080, | 138 | .val_regs = 0x0080, |
139 | .rdata_reg = 0x00d0, | ||
102 | .irq_regs = 0x0118, | 140 | .irq_regs = 0x0118, |
103 | .debounce_regs = 0x0130, | 141 | .debounce_regs = 0x0130, |
104 | .tolerance_regs = 0x012c, | 142 | .tolerance_regs = 0x012c, |
143 | .cmdsrc_regs = 0x0110, | ||
105 | .names = { "Q", "R", "S", "T" }, | 144 | .names = { "Q", "R", "S", "T" }, |
106 | }, | 145 | }, |
107 | { | 146 | { |
108 | .val_regs = 0x0088, | 147 | .val_regs = 0x0088, |
148 | .rdata_reg = 0x00d4, | ||
109 | .irq_regs = 0x0148, | 149 | .irq_regs = 0x0148, |
110 | .debounce_regs = 0x0160, | 150 | .debounce_regs = 0x0160, |
111 | .tolerance_regs = 0x015c, | 151 | .tolerance_regs = 0x015c, |
152 | .cmdsrc_regs = 0x0140, | ||
112 | .names = { "U", "V", "W", "X" }, | 153 | .names = { "U", "V", "W", "X" }, |
113 | }, | 154 | }, |
114 | { | 155 | { |
115 | .val_regs = 0x01E0, | 156 | .val_regs = 0x01E0, |
157 | .rdata_reg = 0x00d8, | ||
116 | .irq_regs = 0x0178, | 158 | .irq_regs = 0x0178, |
117 | .debounce_regs = 0x0190, | 159 | .debounce_regs = 0x0190, |
118 | .tolerance_regs = 0x018c, | 160 | .tolerance_regs = 0x018c, |
161 | .cmdsrc_regs = 0x0170, | ||
119 | .names = { "Y", "Z", "AA", "AB" }, | 162 | .names = { "Y", "Z", "AA", "AB" }, |
120 | }, | 163 | }, |
121 | { | 164 | { |
122 | .val_regs = 0x01e8, | 165 | .val_regs = 0x01e8, |
166 | .rdata_reg = 0x00dc, | ||
123 | .irq_regs = 0x01a8, | 167 | .irq_regs = 0x01a8, |
124 | .debounce_regs = 0x01c0, | 168 | .debounce_regs = 0x01c0, |
125 | .tolerance_regs = 0x01bc, | 169 | .tolerance_regs = 0x01bc, |
170 | .cmdsrc_regs = 0x01a0, | ||
126 | .names = { "AC", "", "", "" }, | 171 | .names = { "AC", "", "", "" }, |
127 | }, | 172 | }, |
128 | }; | 173 | }; |
129 | 174 | ||
130 | #define GPIO_BANK(x) ((x) >> 5) | 175 | enum aspeed_gpio_reg { |
131 | #define GPIO_OFFSET(x) ((x) & 0x1f) | 176 | reg_val, |
132 | #define GPIO_BIT(x) BIT(GPIO_OFFSET(x)) | 177 | reg_rdata, |
178 | reg_dir, | ||
179 | reg_irq_enable, | ||
180 | reg_irq_type0, | ||
181 | reg_irq_type1, | ||
182 | reg_irq_type2, | ||
183 | reg_irq_status, | ||
184 | reg_debounce_sel1, | ||
185 | reg_debounce_sel2, | ||
186 | reg_tolerance, | ||
187 | reg_cmdsrc0, | ||
188 | reg_cmdsrc1, | ||
189 | }; | ||
133 | 190 | ||
134 | #define GPIO_DATA 0x00 | 191 | #define GPIO_VAL_VALUE 0x00 |
135 | #define GPIO_DIR 0x04 | 192 | #define GPIO_VAL_DIR 0x04 |
136 | 193 | ||
137 | #define GPIO_IRQ_ENABLE 0x00 | 194 | #define GPIO_IRQ_ENABLE 0x00 |
138 | #define GPIO_IRQ_TYPE0 0x04 | 195 | #define GPIO_IRQ_TYPE0 0x04 |
@@ -143,6 +200,53 @@ static const struct aspeed_gpio_bank aspeed_gpio_banks[] = { | |||
143 | #define GPIO_DEBOUNCE_SEL1 0x00 | 200 | #define GPIO_DEBOUNCE_SEL1 0x00 |
144 | #define GPIO_DEBOUNCE_SEL2 0x04 | 201 | #define GPIO_DEBOUNCE_SEL2 0x04 |
145 | 202 | ||
203 | #define GPIO_CMDSRC_0 0x00 | ||
204 | #define GPIO_CMDSRC_1 0x04 | ||
205 | #define GPIO_CMDSRC_ARM 0 | ||
206 | #define GPIO_CMDSRC_LPC 1 | ||
207 | #define GPIO_CMDSRC_COLDFIRE 2 | ||
208 | #define GPIO_CMDSRC_RESERVED 3 | ||
209 | |||
210 | /* This will be resolved at compile time */ | ||
211 | static inline void __iomem *bank_reg(struct aspeed_gpio *gpio, | ||
212 | const struct aspeed_gpio_bank *bank, | ||
213 | const enum aspeed_gpio_reg reg) | ||
214 | { | ||
215 | switch (reg) { | ||
216 | case reg_val: | ||
217 | return gpio->base + bank->val_regs + GPIO_VAL_VALUE; | ||
218 | case reg_rdata: | ||
219 | return gpio->base + bank->rdata_reg; | ||
220 | case reg_dir: | ||
221 | return gpio->base + bank->val_regs + GPIO_VAL_DIR; | ||
222 | case reg_irq_enable: | ||
223 | return gpio->base + bank->irq_regs + GPIO_IRQ_ENABLE; | ||
224 | case reg_irq_type0: | ||
225 | return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE0; | ||
226 | case reg_irq_type1: | ||
227 | return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE1; | ||
228 | case reg_irq_type2: | ||
229 | return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE2; | ||
230 | case reg_irq_status: | ||
231 | return gpio->base + bank->irq_regs + GPIO_IRQ_STATUS; | ||
232 | case reg_debounce_sel1: | ||
233 | return gpio->base + bank->debounce_regs + GPIO_DEBOUNCE_SEL1; | ||
234 | case reg_debounce_sel2: | ||
235 | return gpio->base + bank->debounce_regs + GPIO_DEBOUNCE_SEL2; | ||
236 | case reg_tolerance: | ||
237 | return gpio->base + bank->tolerance_regs; | ||
238 | case reg_cmdsrc0: | ||
239 | return gpio->base + bank->cmdsrc_regs + GPIO_CMDSRC_0; | ||
240 | case reg_cmdsrc1: | ||
241 | return gpio->base + bank->cmdsrc_regs + GPIO_CMDSRC_1; | ||
242 | } | ||
243 | BUG(); | ||
244 | } | ||
245 | |||
246 | #define GPIO_BANK(x) ((x) >> 5) | ||
247 | #define GPIO_OFFSET(x) ((x) & 0x1f) | ||
248 | #define GPIO_BIT(x) BIT(GPIO_OFFSET(x)) | ||
249 | |||
146 | #define _GPIO_SET_DEBOUNCE(t, o, i) ((!!((t) & BIT(i))) << GPIO_OFFSET(o)) | 250 | #define _GPIO_SET_DEBOUNCE(t, o, i) ((!!((t) & BIT(i))) << GPIO_OFFSET(o)) |
147 | #define GPIO_SET_DEBOUNCE1(t, o) _GPIO_SET_DEBOUNCE(t, o, 1) | 251 | #define GPIO_SET_DEBOUNCE1(t, o) _GPIO_SET_DEBOUNCE(t, o, 1) |
148 | #define GPIO_SET_DEBOUNCE2(t, o) _GPIO_SET_DEBOUNCE(t, o, 0) | 252 | #define GPIO_SET_DEBOUNCE2(t, o) _GPIO_SET_DEBOUNCE(t, o, 0) |
@@ -201,18 +305,80 @@ static inline bool have_output(struct aspeed_gpio *gpio, unsigned int offset) | |||
201 | return !props || (props->output & GPIO_BIT(offset)); | 305 | return !props || (props->output & GPIO_BIT(offset)); |
202 | } | 306 | } |
203 | 307 | ||
204 | static void __iomem *bank_val_reg(struct aspeed_gpio *gpio, | 308 | static void aspeed_gpio_change_cmd_source(struct aspeed_gpio *gpio, |
205 | const struct aspeed_gpio_bank *bank, | 309 | const struct aspeed_gpio_bank *bank, |
206 | unsigned int reg) | 310 | int bindex, int cmdsrc) |
207 | { | 311 | { |
208 | return gpio->base + bank->val_regs + reg; | 312 | void __iomem *c0 = bank_reg(gpio, bank, reg_cmdsrc0); |
313 | void __iomem *c1 = bank_reg(gpio, bank, reg_cmdsrc1); | ||
314 | u32 bit, reg; | ||
315 | |||
316 | /* | ||
317 | * Each register controls 4 banks, so take the bottom 2 | ||
318 | * bits of the bank index, and use them to select the | ||
319 | * right control bit (0, 8, 16 or 24). | ||
320 | */ | ||
321 | bit = BIT((bindex & 3) << 3); | ||
322 | |||
323 | /* Source 1 first to avoid illegal 11 combination */ | ||
324 | reg = ioread32(c1); | ||
325 | if (cmdsrc & 2) | ||
326 | reg |= bit; | ||
327 | else | ||
328 | reg &= ~bit; | ||
329 | iowrite32(reg, c1); | ||
330 | |||
331 | /* Then Source 0 */ | ||
332 | reg = ioread32(c0); | ||
333 | if (cmdsrc & 1) | ||
334 | reg |= bit; | ||
335 | else | ||
336 | reg &= ~bit; | ||
337 | iowrite32(reg, c0); | ||
209 | } | 338 | } |
210 | 339 | ||
211 | static void __iomem *bank_irq_reg(struct aspeed_gpio *gpio, | 340 | static bool aspeed_gpio_copro_request(struct aspeed_gpio *gpio, |
212 | const struct aspeed_gpio_bank *bank, | 341 | unsigned int offset) |
213 | unsigned int reg) | ||
214 | { | 342 | { |
215 | return gpio->base + bank->irq_regs + reg; | 343 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
344 | |||
345 | if (!copro_ops || !gpio->cf_copro_bankmap) | ||
346 | return false; | ||
347 | if (!gpio->cf_copro_bankmap[offset >> 3]) | ||
348 | return false; | ||
349 | if (!copro_ops->request_access) | ||
350 | return false; | ||
351 | |||
352 | /* Pause the coprocessor */ | ||
353 | copro_ops->request_access(copro_data); | ||
354 | |||
355 | /* Change command source back to ARM */ | ||
356 | aspeed_gpio_change_cmd_source(gpio, bank, offset >> 3, GPIO_CMDSRC_ARM); | ||
357 | |||
358 | /* Update cache */ | ||
359 | gpio->dcache[GPIO_BANK(offset)] = ioread32(bank_reg(gpio, bank, reg_rdata)); | ||
360 | |||
361 | return true; | ||
362 | } | ||
363 | |||
364 | static void aspeed_gpio_copro_release(struct aspeed_gpio *gpio, | ||
365 | unsigned int offset) | ||
366 | { | ||
367 | const struct aspeed_gpio_bank *bank = to_bank(offset); | ||
368 | |||
369 | if (!copro_ops || !gpio->cf_copro_bankmap) | ||
370 | return; | ||
371 | if (!gpio->cf_copro_bankmap[offset >> 3]) | ||
372 | return; | ||
373 | if (!copro_ops->release_access) | ||
374 | return; | ||
375 | |||
376 | /* Change command source back to ColdFire */ | ||
377 | aspeed_gpio_change_cmd_source(gpio, bank, offset >> 3, | ||
378 | GPIO_CMDSRC_COLDFIRE); | ||
379 | |||
380 | /* Restart the coprocessor */ | ||
381 | copro_ops->release_access(copro_data); | ||
216 | } | 382 | } |
217 | 383 | ||
218 | static int aspeed_gpio_get(struct gpio_chip *gc, unsigned int offset) | 384 | static int aspeed_gpio_get(struct gpio_chip *gc, unsigned int offset) |
@@ -220,8 +386,7 @@ static int aspeed_gpio_get(struct gpio_chip *gc, unsigned int offset) | |||
220 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); | 386 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); |
221 | const struct aspeed_gpio_bank *bank = to_bank(offset); | 387 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
222 | 388 | ||
223 | return !!(ioread32(bank_val_reg(gpio, bank, GPIO_DATA)) | 389 | return !!(ioread32(bank_reg(gpio, bank, reg_val)) & GPIO_BIT(offset)); |
224 | & GPIO_BIT(offset)); | ||
225 | } | 390 | } |
226 | 391 | ||
227 | static void __aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset, | 392 | static void __aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset, |
@@ -232,7 +397,7 @@ static void __aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset, | |||
232 | void __iomem *addr; | 397 | void __iomem *addr; |
233 | u32 reg; | 398 | u32 reg; |
234 | 399 | ||
235 | addr = bank_val_reg(gpio, bank, GPIO_DATA); | 400 | addr = bank_reg(gpio, bank, reg_val); |
236 | reg = gpio->dcache[GPIO_BANK(offset)]; | 401 | reg = gpio->dcache[GPIO_BANK(offset)]; |
237 | 402 | ||
238 | if (val) | 403 | if (val) |
@@ -249,11 +414,15 @@ static void aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset, | |||
249 | { | 414 | { |
250 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); | 415 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); |
251 | unsigned long flags; | 416 | unsigned long flags; |
417 | bool copro; | ||
252 | 418 | ||
253 | spin_lock_irqsave(&gpio->lock, flags); | 419 | spin_lock_irqsave(&gpio->lock, flags); |
420 | copro = aspeed_gpio_copro_request(gpio, offset); | ||
254 | 421 | ||
255 | __aspeed_gpio_set(gc, offset, val); | 422 | __aspeed_gpio_set(gc, offset, val); |
256 | 423 | ||
424 | if (copro) | ||
425 | aspeed_gpio_copro_release(gpio, offset); | ||
257 | spin_unlock_irqrestore(&gpio->lock, flags); | 426 | spin_unlock_irqrestore(&gpio->lock, flags); |
258 | } | 427 | } |
259 | 428 | ||
@@ -261,7 +430,9 @@ static int aspeed_gpio_dir_in(struct gpio_chip *gc, unsigned int offset) | |||
261 | { | 430 | { |
262 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); | 431 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); |
263 | const struct aspeed_gpio_bank *bank = to_bank(offset); | 432 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
433 | void __iomem *addr = bank_reg(gpio, bank, reg_dir); | ||
264 | unsigned long flags; | 434 | unsigned long flags; |
435 | bool copro; | ||
265 | u32 reg; | 436 | u32 reg; |
266 | 437 | ||
267 | if (!have_input(gpio, offset)) | 438 | if (!have_input(gpio, offset)) |
@@ -269,8 +440,13 @@ static int aspeed_gpio_dir_in(struct gpio_chip *gc, unsigned int offset) | |||
269 | 440 | ||
270 | spin_lock_irqsave(&gpio->lock, flags); | 441 | spin_lock_irqsave(&gpio->lock, flags); |
271 | 442 | ||
272 | reg = ioread32(bank_val_reg(gpio, bank, GPIO_DIR)); | 443 | reg = ioread32(addr); |
273 | iowrite32(reg & ~GPIO_BIT(offset), bank_val_reg(gpio, bank, GPIO_DIR)); | 444 | reg &= ~GPIO_BIT(offset); |
445 | |||
446 | copro = aspeed_gpio_copro_request(gpio, offset); | ||
447 | iowrite32(reg, addr); | ||
448 | if (copro) | ||
449 | aspeed_gpio_copro_release(gpio, offset); | ||
274 | 450 | ||
275 | spin_unlock_irqrestore(&gpio->lock, flags); | 451 | spin_unlock_irqrestore(&gpio->lock, flags); |
276 | 452 | ||
@@ -282,7 +458,9 @@ static int aspeed_gpio_dir_out(struct gpio_chip *gc, | |||
282 | { | 458 | { |
283 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); | 459 | struct aspeed_gpio *gpio = gpiochip_get_data(gc); |
284 | const struct aspeed_gpio_bank *bank = to_bank(offset); | 460 | const struct aspeed_gpio_bank *bank = to_bank(offset); |
461 | void __iomem *addr = bank_reg(gpio, bank, reg_dir); | ||
285 | unsigned long flags; | 462 | unsigned long flags; |
463 | bool copro; | ||
286 | u32 reg; | 464 | u32 reg; |
287 | 465 | ||
288 | if (!have_output(gpio, offset)) | 466 | if (!have_output(gpio, offset)) |
@@ -290,10 +468,15 @@ static int aspeed_gpio_dir_out(struct gpio_chip *gc, | |||
290 | 468 | ||
291 | spin_lock_irqsave(&gpio->lock, flags); | 469 | spin_lock_irqsave(&gpio->lock, flags); |
292 | 470 | ||
471 | reg = ioread32(addr); | ||
472 | reg |= GPIO_BIT(offset); | ||
473 | |||
474 | copro = aspeed_gpio_copro_request(gpio, offset); | ||
293 | __aspeed_gpio_set(gc, offset, val); | 475 | __aspeed_gpio_set(gc, offset, val); |
294 | reg = ioread32(bank_val_reg(gpio, bank, GPIO_DIR)); | 476 | iowrite32(reg, addr); |
295 | iowrite32(reg | GPIO_BIT(offset), bank_val_reg(gpio, bank, GPIO_DIR)); | ||
296 | 477 | ||
478 | if (copro) | ||
479 | aspeed_gpio_copro_release(gpio, offset); | ||
297 | spin_unlock_irqrestore(&gpio->lock, flags); | 480 | spin_unlock_irqrestore(&gpio->lock, flags); |
298 | 481 | ||
299 | return 0; | 482 | return 0; |
@@ -314,7 +497,7 @@ static int aspeed_gpio_get_direction(struct gpio_chip *gc, unsigned int offset) | |||
314 | 497 | ||
315 | spin_lock_irqsave(&gpio->lock, flags); | 498 | spin_lock_irqsave(&gpio->lock, flags); |
316 | 499 | ||
317 | val = ioread32(bank_val_reg(gpio, bank, GPIO_DIR)) & GPIO_BIT(offset); | 500 | val = ioread32(bank_reg(gpio, bank, reg_dir)) & GPIO_BIT(offset); |
318 | 501 | ||
319 | spin_unlock_irqrestore(&gpio->lock, flags); | 502 | spin_unlock_irqrestore(&gpio->lock, flags); |
320 | 503 | ||
@@ -323,24 +506,23 @@ static int aspeed_gpio_get_direction(struct gpio_chip *gc, unsigned int offset) | |||
323 | } | 506 | } |
324 | 507 | ||
325 | static inline int irqd_to_aspeed_gpio_data(struct irq_data *d, | 508 | static inline int irqd_to_aspeed_gpio_data(struct irq_data *d, |
326 | struct aspeed_gpio **gpio, | 509 | struct aspeed_gpio **gpio, |
327 | const struct aspeed_gpio_bank **bank, | 510 | const struct aspeed_gpio_bank **bank, |
328 | u32 *bit) | 511 | u32 *bit, int *offset) |
329 | { | 512 | { |
330 | int offset; | ||
331 | struct aspeed_gpio *internal; | 513 | struct aspeed_gpio *internal; |
332 | 514 | ||
333 | offset = irqd_to_hwirq(d); | 515 | *offset = irqd_to_hwirq(d); |
334 | 516 | ||
335 | internal = irq_data_get_irq_chip_data(d); | 517 | internal = irq_data_get_irq_chip_data(d); |
336 | 518 | ||
337 | /* This might be a bit of a questionable place to check */ | 519 | /* This might be a bit of a questionable place to check */ |
338 | if (!have_irq(internal, offset)) | 520 | if (!have_irq(internal, *offset)) |
339 | return -ENOTSUPP; | 521 | return -ENOTSUPP; |
340 | 522 | ||
341 | *gpio = internal; | 523 | *gpio = internal; |
342 | *bank = to_bank(offset); | 524 | *bank = to_bank(*offset); |
343 | *bit = GPIO_BIT(offset); | 525 | *bit = GPIO_BIT(*offset); |
344 | 526 | ||
345 | return 0; | 527 | return 0; |
346 | } | 528 | } |
@@ -351,17 +533,23 @@ static void aspeed_gpio_irq_ack(struct irq_data *d) | |||
351 | struct aspeed_gpio *gpio; | 533 | struct aspeed_gpio *gpio; |
352 | unsigned long flags; | 534 | unsigned long flags; |
353 | void __iomem *status_addr; | 535 | void __iomem *status_addr; |
536 | int rc, offset; | ||
537 | bool copro; | ||
354 | u32 bit; | 538 | u32 bit; |
355 | int rc; | ||
356 | 539 | ||
357 | rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit); | 540 | rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset); |
358 | if (rc) | 541 | if (rc) |
359 | return; | 542 | return; |
360 | 543 | ||
361 | status_addr = bank_irq_reg(gpio, bank, GPIO_IRQ_STATUS); | 544 | status_addr = bank_reg(gpio, bank, reg_irq_status); |
362 | 545 | ||
363 | spin_lock_irqsave(&gpio->lock, flags); | 546 | spin_lock_irqsave(&gpio->lock, flags); |
547 | copro = aspeed_gpio_copro_request(gpio, offset); | ||
548 | |||
364 | iowrite32(bit, status_addr); | 549 | iowrite32(bit, status_addr); |
550 | |||
551 | if (copro) | ||
552 | aspeed_gpio_copro_release(gpio, offset); | ||
365 | spin_unlock_irqrestore(&gpio->lock, flags); | 553 | spin_unlock_irqrestore(&gpio->lock, flags); |
366 | } | 554 | } |
367 | 555 | ||
@@ -372,15 +560,17 @@ static void aspeed_gpio_irq_set_mask(struct irq_data *d, bool set) | |||
372 | unsigned long flags; | 560 | unsigned long flags; |
373 | u32 reg, bit; | 561 | u32 reg, bit; |
374 | void __iomem *addr; | 562 | void __iomem *addr; |
375 | int rc; | 563 | int rc, offset; |
564 | bool copro; | ||
376 | 565 | ||
377 | rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit); | 566 | rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset); |
378 | if (rc) | 567 | if (rc) |
379 | return; | 568 | return; |
380 | 569 | ||
381 | addr = bank_irq_reg(gpio, bank, GPIO_IRQ_ENABLE); | 570 | addr = bank_reg(gpio, bank, reg_irq_enable); |
382 | 571 | ||
383 | spin_lock_irqsave(&gpio->lock, flags); | 572 | spin_lock_irqsave(&gpio->lock, flags); |
573 | copro = aspeed_gpio_copro_request(gpio, offset); | ||
384 | 574 | ||
385 | reg = ioread32(addr); | 575 | reg = ioread32(addr); |
386 | if (set) | 576 | if (set) |
@@ -389,6 +579,8 @@ static void aspeed_gpio_irq_set_mask(struct irq_data *d, bool set) | |||
389 | reg &= ~bit; | 579 | reg &= ~bit; |
390 | iowrite32(reg, addr); | 580 | iowrite32(reg, addr); |
391 | 581 | ||
582 | if (copro) | ||
583 | aspeed_gpio_copro_release(gpio, offset); | ||
392 | spin_unlock_irqrestore(&gpio->lock, flags); | 584 | spin_unlock_irqrestore(&gpio->lock, flags); |
393 | } | 585 | } |
394 | 586 | ||
@@ -413,9 +605,10 @@ static int aspeed_gpio_set_type(struct irq_data *d, unsigned int type) | |||
413 | struct aspeed_gpio *gpio; | 605 | struct aspeed_gpio *gpio; |
414 | unsigned long flags; | 606 | unsigned long flags; |
415 | void __iomem *addr; | 607 | void __iomem *addr; |
416 | int rc; | 608 | int rc, offset; |
609 | bool copro; | ||
417 | 610 | ||
418 | rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit); | 611 | rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset); |
419 | if (rc) | 612 | if (rc) |
420 | return -EINVAL; | 613 | return -EINVAL; |
421 | 614 | ||
@@ -441,22 +634,25 @@ static int aspeed_gpio_set_type(struct irq_data *d, unsigned int type) | |||
441 | } | 634 | } |
442 | 635 | ||
443 | spin_lock_irqsave(&gpio->lock, flags); | 636 | spin_lock_irqsave(&gpio->lock, flags); |
637 | copro = aspeed_gpio_copro_request(gpio, offset); | ||
444 | 638 | ||
445 | addr = bank_irq_reg(gpio, bank, GPIO_IRQ_TYPE0); | 639 | addr = bank_reg(gpio, bank, reg_irq_type0); |
446 | reg = ioread32(addr); | 640 | reg = ioread32(addr); |
447 | reg = (reg & ~bit) | type0; | 641 | reg = (reg & ~bit) | type0; |
448 | iowrite32(reg, addr); | 642 | iowrite32(reg, addr); |
449 | 643 | ||
450 | addr = bank_irq_reg(gpio, bank, GPIO_IRQ_TYPE1); | 644 | addr = bank_reg(gpio, bank, reg_irq_type1); |
451 | reg = ioread32(addr); | 645 | reg = ioread32(addr); |
452 | reg = (reg & ~bit) | type1; | 646 | reg = (reg & ~bit) | type1; |
453 | iowrite32(reg, addr); | 647 | iowrite32(reg, addr); |
454 | 648 | ||
455 | addr = bank_irq_reg(gpio, bank, GPIO_IRQ_TYPE2); | 649 | addr = bank_reg(gpio, bank, reg_irq_type2); |
456 | reg = ioread32(addr); | 650 | reg = ioread32(addr); |
457 | reg = (reg & ~bit) | type2; | 651 | reg = (reg & ~bit) | type2; |
458 | iowrite32(reg, addr); | 652 | iowrite32(reg, addr); |
459 | 653 | ||
654 | if (copro) | ||
655 | aspeed_gpio_copro_release(gpio, offset); | ||
460 | spin_unlock_irqrestore(&gpio->lock, flags); | 656 | spin_unlock_irqrestore(&gpio->lock, flags); |
461 | 657 | ||
462 | irq_set_handler_locked(d, handler); | 658 | irq_set_handler_locked(d, handler); |
@@ -477,7 +673,7 @@ static void aspeed_gpio_irq_handler(struct irq_desc *desc) | |||
477 | for (i = 0; i < ARRAY_SIZE(aspeed_gpio_banks); i++) { | 673 | for (i = 0; i < ARRAY_SIZE(aspeed_gpio_banks); i++) { |
478 | const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i]; | 674 | const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i]; |
479 | 675 | ||
480 | reg = ioread32(bank_irq_reg(data, bank, GPIO_IRQ_STATUS)); | 676 | reg = ioread32(bank_reg(data, bank, reg_irq_status)); |
481 | 677 | ||
482 | for_each_set_bit(p, ®, 32) { | 678 | for_each_set_bit(p, ®, 32) { |
483 | girq = irq_find_mapping(gc->irq.domain, i * 32 + p); | 679 | girq = irq_find_mapping(gc->irq.domain, i * 32 + p); |
@@ -549,21 +745,27 @@ static int aspeed_gpio_reset_tolerance(struct gpio_chip *chip, | |||
549 | unsigned int offset, bool enable) | 745 | unsigned int offset, bool enable) |
550 | { | 746 | { |
551 | struct aspeed_gpio *gpio = gpiochip_get_data(chip); | 747 | struct aspeed_gpio *gpio = gpiochip_get_data(chip); |
552 | const struct aspeed_gpio_bank *bank; | ||
553 | unsigned long flags; | 748 | unsigned long flags; |
749 | void __iomem *treg; | ||
750 | bool copro; | ||
554 | u32 val; | 751 | u32 val; |
555 | 752 | ||
556 | bank = to_bank(offset); | 753 | treg = bank_reg(gpio, to_bank(offset), reg_tolerance); |
557 | 754 | ||
558 | spin_lock_irqsave(&gpio->lock, flags); | 755 | spin_lock_irqsave(&gpio->lock, flags); |
559 | val = readl(gpio->base + bank->tolerance_regs); | 756 | copro = aspeed_gpio_copro_request(gpio, offset); |
757 | |||
758 | val = readl(treg); | ||
560 | 759 | ||
561 | if (enable) | 760 | if (enable) |
562 | val |= GPIO_BIT(offset); | 761 | val |= GPIO_BIT(offset); |
563 | else | 762 | else |
564 | val &= ~GPIO_BIT(offset); | 763 | val &= ~GPIO_BIT(offset); |
565 | 764 | ||
566 | writel(val, gpio->base + bank->tolerance_regs); | 765 | writel(val, treg); |
766 | |||
767 | if (copro) | ||
768 | aspeed_gpio_copro_release(gpio, offset); | ||
567 | spin_unlock_irqrestore(&gpio->lock, flags); | 769 | spin_unlock_irqrestore(&gpio->lock, flags); |
568 | 770 | ||
569 | return 0; | 771 | return 0; |
@@ -582,13 +784,6 @@ static void aspeed_gpio_free(struct gpio_chip *chip, unsigned int offset) | |||
582 | pinctrl_gpio_free(chip->base + offset); | 784 | pinctrl_gpio_free(chip->base + offset); |
583 | } | 785 | } |
584 | 786 | ||
585 | static inline void __iomem *bank_debounce_reg(struct aspeed_gpio *gpio, | ||
586 | const struct aspeed_gpio_bank *bank, | ||
587 | unsigned int reg) | ||
588 | { | ||
589 | return gpio->base + bank->debounce_regs + reg; | ||
590 | } | ||
591 | |||
592 | static int usecs_to_cycles(struct aspeed_gpio *gpio, unsigned long usecs, | 787 | static int usecs_to_cycles(struct aspeed_gpio *gpio, unsigned long usecs, |
593 | u32 *cycles) | 788 | u32 *cycles) |
594 | { | 789 | { |
@@ -666,11 +861,14 @@ static void configure_timer(struct aspeed_gpio *gpio, unsigned int offset, | |||
666 | void __iomem *addr; | 861 | void __iomem *addr; |
667 | u32 val; | 862 | u32 val; |
668 | 863 | ||
669 | addr = bank_debounce_reg(gpio, bank, GPIO_DEBOUNCE_SEL1); | 864 | /* Note: Debounce timer isn't under control of the command |
865 | * source registers, so no need to sync with the coprocessor | ||
866 | */ | ||
867 | addr = bank_reg(gpio, bank, reg_debounce_sel1); | ||
670 | val = ioread32(addr); | 868 | val = ioread32(addr); |
671 | iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE1(timer, offset), addr); | 869 | iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE1(timer, offset), addr); |
672 | 870 | ||
673 | addr = bank_debounce_reg(gpio, bank, GPIO_DEBOUNCE_SEL2); | 871 | addr = bank_reg(gpio, bank, reg_debounce_sel2); |
674 | val = ioread32(addr); | 872 | val = ioread32(addr); |
675 | iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE2(timer, offset), addr); | 873 | iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE2(timer, offset), addr); |
676 | } | 874 | } |
@@ -812,6 +1010,111 @@ static int aspeed_gpio_set_config(struct gpio_chip *chip, unsigned int offset, | |||
812 | return -ENOTSUPP; | 1010 | return -ENOTSUPP; |
813 | } | 1011 | } |
814 | 1012 | ||
1013 | /** | ||
1014 | * aspeed_gpio_copro_set_ops - Sets the callbacks used for handhsaking with | ||
1015 | * the coprocessor for shared GPIO banks | ||
1016 | * @ops: The callbacks | ||
1017 | * @data: Pointer passed back to the callbacks | ||
1018 | */ | ||
1019 | int aspeed_gpio_copro_set_ops(const struct aspeed_gpio_copro_ops *ops, void *data) | ||
1020 | { | ||
1021 | copro_data = data; | ||
1022 | copro_ops = ops; | ||
1023 | |||
1024 | return 0; | ||
1025 | } | ||
1026 | EXPORT_SYMBOL_GPL(aspeed_gpio_copro_set_ops); | ||
1027 | |||
1028 | /** | ||
1029 | * aspeed_gpio_copro_grab_gpio - Mark a GPIO used by the coprocessor. The entire | ||
1030 | * bank gets marked and any access from the ARM will | ||
1031 | * result in handshaking via callbacks. | ||
1032 | * @desc: The GPIO to be marked | ||
1033 | * @vreg_offset: If non-NULL, returns the value register offset in the GPIO space | ||
1034 | * @dreg_offset: If non-NULL, returns the data latch register offset in the GPIO space | ||
1035 | * @bit: If non-NULL, returns the bit number of the GPIO in the registers | ||
1036 | */ | ||
1037 | int aspeed_gpio_copro_grab_gpio(struct gpio_desc *desc, | ||
1038 | u16 *vreg_offset, u16 *dreg_offset, u8 *bit) | ||
1039 | { | ||
1040 | struct gpio_chip *chip = gpiod_to_chip(desc); | ||
1041 | struct aspeed_gpio *gpio = gpiochip_get_data(chip); | ||
1042 | int rc = 0, bindex, offset = gpio_chip_hwgpio(desc); | ||
1043 | const struct aspeed_gpio_bank *bank = to_bank(offset); | ||
1044 | unsigned long flags; | ||
1045 | |||
1046 | if (!gpio->cf_copro_bankmap) | ||
1047 | gpio->cf_copro_bankmap = kzalloc(gpio->config->nr_gpios >> 3, GFP_KERNEL); | ||
1048 | if (!gpio->cf_copro_bankmap) | ||
1049 | return -ENOMEM; | ||
1050 | if (offset < 0 || offset > gpio->config->nr_gpios) | ||
1051 | return -EINVAL; | ||
1052 | bindex = offset >> 3; | ||
1053 | |||
1054 | spin_lock_irqsave(&gpio->lock, flags); | ||
1055 | |||
1056 | /* Sanity check, this shouldn't happen */ | ||
1057 | if (gpio->cf_copro_bankmap[bindex] == 0xff) { | ||
1058 | rc = -EIO; | ||
1059 | goto bail; | ||
1060 | } | ||
1061 | gpio->cf_copro_bankmap[bindex]++; | ||
1062 | |||
1063 | /* Switch command source */ | ||
1064 | if (gpio->cf_copro_bankmap[bindex] == 1) | ||
1065 | aspeed_gpio_change_cmd_source(gpio, bank, bindex, | ||
1066 | GPIO_CMDSRC_COLDFIRE); | ||
1067 | |||
1068 | if (vreg_offset) | ||
1069 | *vreg_offset = bank->val_regs; | ||
1070 | if (dreg_offset) | ||
1071 | *dreg_offset = bank->rdata_reg; | ||
1072 | if (bit) | ||
1073 | *bit = GPIO_OFFSET(offset); | ||
1074 | bail: | ||
1075 | spin_unlock_irqrestore(&gpio->lock, flags); | ||
1076 | return rc; | ||
1077 | } | ||
1078 | EXPORT_SYMBOL_GPL(aspeed_gpio_copro_grab_gpio); | ||
1079 | |||
1080 | /** | ||
1081 | * aspeed_gpio_copro_release_gpio - Unmark a GPIO used by the coprocessor. | ||
1082 | * @desc: The GPIO to be marked | ||
1083 | */ | ||
1084 | int aspeed_gpio_copro_release_gpio(struct gpio_desc *desc) | ||
1085 | { | ||
1086 | struct gpio_chip *chip = gpiod_to_chip(desc); | ||
1087 | struct aspeed_gpio *gpio = gpiochip_get_data(chip); | ||
1088 | int rc = 0, bindex, offset = gpio_chip_hwgpio(desc); | ||
1089 | const struct aspeed_gpio_bank *bank = to_bank(offset); | ||
1090 | unsigned long flags; | ||
1091 | |||
1092 | if (!gpio->cf_copro_bankmap) | ||
1093 | return -ENXIO; | ||
1094 | |||
1095 | if (offset < 0 || offset > gpio->config->nr_gpios) | ||
1096 | return -EINVAL; | ||
1097 | bindex = offset >> 3; | ||
1098 | |||
1099 | spin_lock_irqsave(&gpio->lock, flags); | ||
1100 | |||
1101 | /* Sanity check, this shouldn't happen */ | ||
1102 | if (gpio->cf_copro_bankmap[bindex] == 0) { | ||
1103 | rc = -EIO; | ||
1104 | goto bail; | ||
1105 | } | ||
1106 | gpio->cf_copro_bankmap[bindex]--; | ||
1107 | |||
1108 | /* Switch command source */ | ||
1109 | if (gpio->cf_copro_bankmap[bindex] == 0) | ||
1110 | aspeed_gpio_change_cmd_source(gpio, bank, bindex, | ||
1111 | GPIO_CMDSRC_ARM); | ||
1112 | bail: | ||
1113 | spin_unlock_irqrestore(&gpio->lock, flags); | ||
1114 | return rc; | ||
1115 | } | ||
1116 | EXPORT_SYMBOL_GPL(aspeed_gpio_copro_release_gpio); | ||
1117 | |||
815 | /* | 1118 | /* |
816 | * Any banks not specified in a struct aspeed_bank_props array are assumed to | 1119 | * Any banks not specified in a struct aspeed_bank_props array are assumed to |
817 | * have the properties: | 1120 | * have the properties: |
@@ -902,11 +1205,18 @@ static int __init aspeed_gpio_probe(struct platform_device *pdev) | |||
902 | if (!gpio->dcache) | 1205 | if (!gpio->dcache) |
903 | return -ENOMEM; | 1206 | return -ENOMEM; |
904 | 1207 | ||
905 | /* Populate it with initial values read from the HW */ | 1208 | /* |
1209 | * Populate it with initial values read from the HW and switch | ||
1210 | * all command sources to the ARM by default | ||
1211 | */ | ||
906 | for (i = 0; i < banks; i++) { | 1212 | for (i = 0; i < banks; i++) { |
907 | const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i]; | 1213 | const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i]; |
908 | gpio->dcache[i] = ioread32(gpio->base + bank->val_regs + | 1214 | void __iomem *addr = bank_reg(gpio, bank, reg_rdata); |
909 | GPIO_DATA); | 1215 | gpio->dcache[i] = ioread32(addr); |
1216 | aspeed_gpio_change_cmd_source(gpio, bank, 0, GPIO_CMDSRC_ARM); | ||
1217 | aspeed_gpio_change_cmd_source(gpio, bank, 1, GPIO_CMDSRC_ARM); | ||
1218 | aspeed_gpio_change_cmd_source(gpio, bank, 2, GPIO_CMDSRC_ARM); | ||
1219 | aspeed_gpio_change_cmd_source(gpio, bank, 3, GPIO_CMDSRC_ARM); | ||
910 | } | 1220 | } |
911 | 1221 | ||
912 | rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio); | 1222 | rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio); |