blob: 9df73ae84841a1e733a38ac09fa7cf9f6fa63714 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright 2015 IBM Corp.
4 *
5 * Joel Stanley <joel@jms.id.au>
6 */
7
8#include <asm/div64.h>
9#include <linux/clk.h>
10#include <linux/gpio/driver.h>
11#include <linux/gpio/aspeed.h>
12#include <linux/hashtable.h>
13#include <linux/init.h>
14#include <linux/io.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/pinctrl/consumer.h>
18#include <linux/platform_device.h>
19#include <linux/spinlock.h>
20#include <linux/string.h>
21
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
31struct aspeed_bank_props {
32 unsigned int bank;
33 u32 input;
34 u32 output;
35};
36
37struct aspeed_gpio_config {
38 unsigned int nr_gpios;
39 const struct aspeed_bank_props *props;
40};
41
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 */
53struct aspeed_gpio {
54 struct gpio_chip chip;
55 struct irq_chip irqc;
56 raw_spinlock_t lock;
57 void __iomem *base;
58 int irq;
59 const struct aspeed_gpio_config *config;
60
61 u8 *offset_timer;
62 unsigned int timer_users[4];
63 struct clk *clk;
64
65 u32 *dcache;
66 u8 *cf_copro_bankmap;
67};
68
69struct aspeed_gpio_bank {
70 uint16_t val_regs; /* +0: Rd: read input value, Wr: set write latch
71 * +4: Rd/Wr: Direction (0=in, 1=out)
72 */
73 uint16_t rdata_reg; /* Rd: read write latch, Wr: <none> */
74 uint16_t irq_regs;
75 uint16_t debounce_regs;
76 uint16_t tolerance_regs;
77 uint16_t cmdsrc_regs;
78 const char names[4][3];
79};
80
81/*
82 * Note: The "value" register returns the input value sampled on the
83 * line even when the GPIO is configured as an output. Since
84 * that input goes through synchronizers, writing, then reading
85 * back may not return the written value right away.
86 *
87 * The "rdata" register returns the content of the write latch
88 * and thus can be used to read back what was last written
89 * reliably.
90 */
91
92static const int debounce_timers[4] = { 0x00, 0x50, 0x54, 0x58 };
93
94static const struct aspeed_gpio_copro_ops *copro_ops;
95static void *copro_data;
96
97static const struct aspeed_gpio_bank aspeed_gpio_banks[] = {
98 {
99 .val_regs = 0x0000,
100 .rdata_reg = 0x00c0,
101 .irq_regs = 0x0008,
102 .debounce_regs = 0x0040,
103 .tolerance_regs = 0x001c,
104 .cmdsrc_regs = 0x0060,
105 .names = { "A", "B", "C", "D" },
106 },
107 {
108 .val_regs = 0x0020,
109 .rdata_reg = 0x00c4,
110 .irq_regs = 0x0028,
111 .debounce_regs = 0x0048,
112 .tolerance_regs = 0x003c,
113 .cmdsrc_regs = 0x0068,
114 .names = { "E", "F", "G", "H" },
115 },
116 {
117 .val_regs = 0x0070,
118 .rdata_reg = 0x00c8,
119 .irq_regs = 0x0098,
120 .debounce_regs = 0x00b0,
121 .tolerance_regs = 0x00ac,
122 .cmdsrc_regs = 0x0090,
123 .names = { "I", "J", "K", "L" },
124 },
125 {
126 .val_regs = 0x0078,
127 .rdata_reg = 0x00cc,
128 .irq_regs = 0x00e8,
129 .debounce_regs = 0x0100,
130 .tolerance_regs = 0x00fc,
131 .cmdsrc_regs = 0x00e0,
132 .names = { "M", "N", "O", "P" },
133 },
134 {
135 .val_regs = 0x0080,
136 .rdata_reg = 0x00d0,
137 .irq_regs = 0x0118,
138 .debounce_regs = 0x0130,
139 .tolerance_regs = 0x012c,
140 .cmdsrc_regs = 0x0110,
141 .names = { "Q", "R", "S", "T" },
142 },
143 {
144 .val_regs = 0x0088,
145 .rdata_reg = 0x00d4,
146 .irq_regs = 0x0148,
147 .debounce_regs = 0x0160,
148 .tolerance_regs = 0x015c,
149 .cmdsrc_regs = 0x0140,
150 .names = { "U", "V", "W", "X" },
151 },
152 {
153 .val_regs = 0x01E0,
154 .rdata_reg = 0x00d8,
155 .irq_regs = 0x0178,
156 .debounce_regs = 0x0190,
157 .tolerance_regs = 0x018c,
158 .cmdsrc_regs = 0x0170,
159 .names = { "Y", "Z", "AA", "AB" },
160 },
161 {
162 .val_regs = 0x01e8,
163 .rdata_reg = 0x00dc,
164 .irq_regs = 0x01a8,
165 .debounce_regs = 0x01c0,
166 .tolerance_regs = 0x01bc,
167 .cmdsrc_regs = 0x01a0,
168 .names = { "AC", "", "", "" },
169 },
170};
171
172enum aspeed_gpio_reg {
173 reg_val,
174 reg_rdata,
175 reg_dir,
176 reg_irq_enable,
177 reg_irq_type0,
178 reg_irq_type1,
179 reg_irq_type2,
180 reg_irq_status,
181 reg_debounce_sel1,
182 reg_debounce_sel2,
183 reg_tolerance,
184 reg_cmdsrc0,
185 reg_cmdsrc1,
186};
187
188#define GPIO_VAL_VALUE 0x00
189#define GPIO_VAL_DIR 0x04
190
191#define GPIO_IRQ_ENABLE 0x00
192#define GPIO_IRQ_TYPE0 0x04
193#define GPIO_IRQ_TYPE1 0x08
194#define GPIO_IRQ_TYPE2 0x0c
195#define GPIO_IRQ_STATUS 0x10
196
197#define GPIO_DEBOUNCE_SEL1 0x00
198#define GPIO_DEBOUNCE_SEL2 0x04
199
200#define GPIO_CMDSRC_0 0x00
201#define GPIO_CMDSRC_1 0x04
202#define GPIO_CMDSRC_ARM 0
203#define GPIO_CMDSRC_LPC 1
204#define GPIO_CMDSRC_COLDFIRE 2
205#define GPIO_CMDSRC_RESERVED 3
206
207/* This will be resolved at compile time */
208static inline void __iomem *bank_reg(struct aspeed_gpio *gpio,
209 const struct aspeed_gpio_bank *bank,
210 const enum aspeed_gpio_reg reg)
211{
212 switch (reg) {
213 case reg_val:
214 return gpio->base + bank->val_regs + GPIO_VAL_VALUE;
215 case reg_rdata:
216 return gpio->base + bank->rdata_reg;
217 case reg_dir:
218 return gpio->base + bank->val_regs + GPIO_VAL_DIR;
219 case reg_irq_enable:
220 return gpio->base + bank->irq_regs + GPIO_IRQ_ENABLE;
221 case reg_irq_type0:
222 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE0;
223 case reg_irq_type1:
224 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE1;
225 case reg_irq_type2:
226 return gpio->base + bank->irq_regs + GPIO_IRQ_TYPE2;
227 case reg_irq_status:
228 return gpio->base + bank->irq_regs + GPIO_IRQ_STATUS;
229 case reg_debounce_sel1:
230 return gpio->base + bank->debounce_regs + GPIO_DEBOUNCE_SEL1;
231 case reg_debounce_sel2:
232 return gpio->base + bank->debounce_regs + GPIO_DEBOUNCE_SEL2;
233 case reg_tolerance:
234 return gpio->base + bank->tolerance_regs;
235 case reg_cmdsrc0:
236 return gpio->base + bank->cmdsrc_regs + GPIO_CMDSRC_0;
237 case reg_cmdsrc1:
238 return gpio->base + bank->cmdsrc_regs + GPIO_CMDSRC_1;
239 }
240 BUG();
241}
242
243#define GPIO_BANK(x) ((x) >> 5)
244#define GPIO_OFFSET(x) ((x) & 0x1f)
245#define GPIO_BIT(x) BIT(GPIO_OFFSET(x))
246
247#define _GPIO_SET_DEBOUNCE(t, o, i) ((!!((t) & BIT(i))) << GPIO_OFFSET(o))
248#define GPIO_SET_DEBOUNCE1(t, o) _GPIO_SET_DEBOUNCE(t, o, 1)
249#define GPIO_SET_DEBOUNCE2(t, o) _GPIO_SET_DEBOUNCE(t, o, 0)
250
251static const struct aspeed_gpio_bank *to_bank(unsigned int offset)
252{
253 unsigned int bank = GPIO_BANK(offset);
254
255 WARN_ON(bank >= ARRAY_SIZE(aspeed_gpio_banks));
256 return &aspeed_gpio_banks[bank];
257}
258
259static inline bool is_bank_props_sentinel(const struct aspeed_bank_props *props)
260{
261 return !(props->input || props->output);
262}
263
264static inline const struct aspeed_bank_props *find_bank_props(
265 struct aspeed_gpio *gpio, unsigned int offset)
266{
267 const struct aspeed_bank_props *props = gpio->config->props;
268
269 while (!is_bank_props_sentinel(props)) {
270 if (props->bank == GPIO_BANK(offset))
271 return props;
272 props++;
273 }
274
275 return NULL;
276}
277
278static inline bool have_gpio(struct aspeed_gpio *gpio, unsigned int offset)
279{
280 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
281 const struct aspeed_gpio_bank *bank = to_bank(offset);
282 unsigned int group = GPIO_OFFSET(offset) / 8;
283
284 return bank->names[group][0] != '\0' &&
285 (!props || ((props->input | props->output) & GPIO_BIT(offset)));
286}
287
288static inline bool have_input(struct aspeed_gpio *gpio, unsigned int offset)
289{
290 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
291
292 return !props || (props->input & GPIO_BIT(offset));
293}
294
295#define have_irq(g, o) have_input((g), (o))
296#define have_debounce(g, o) have_input((g), (o))
297
298static inline bool have_output(struct aspeed_gpio *gpio, unsigned int offset)
299{
300 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
301
302 return !props || (props->output & GPIO_BIT(offset));
303}
304
305static void aspeed_gpio_change_cmd_source(struct aspeed_gpio *gpio,
306 const struct aspeed_gpio_bank *bank,
307 int bindex, int cmdsrc)
308{
309 void __iomem *c0 = bank_reg(gpio, bank, reg_cmdsrc0);
310 void __iomem *c1 = bank_reg(gpio, bank, reg_cmdsrc1);
311 u32 bit, reg;
312
313 /*
314 * Each register controls 4 banks, so take the bottom 2
315 * bits of the bank index, and use them to select the
316 * right control bit (0, 8, 16 or 24).
317 */
318 bit = BIT((bindex & 3) << 3);
319
320 /* Source 1 first to avoid illegal 11 combination */
321 reg = ioread32(c1);
322 if (cmdsrc & 2)
323 reg |= bit;
324 else
325 reg &= ~bit;
326 iowrite32(reg, c1);
327
328 /* Then Source 0 */
329 reg = ioread32(c0);
330 if (cmdsrc & 1)
331 reg |= bit;
332 else
333 reg &= ~bit;
334 iowrite32(reg, c0);
335}
336
337static bool aspeed_gpio_copro_request(struct aspeed_gpio *gpio,
338 unsigned int offset)
339{
340 const struct aspeed_gpio_bank *bank = to_bank(offset);
341
342 if (!copro_ops || !gpio->cf_copro_bankmap)
343 return false;
344 if (!gpio->cf_copro_bankmap[offset >> 3])
345 return false;
346 if (!copro_ops->request_access)
347 return false;
348
349 /* Pause the coprocessor */
350 copro_ops->request_access(copro_data);
351
352 /* Change command source back to ARM */
353 aspeed_gpio_change_cmd_source(gpio, bank, offset >> 3, GPIO_CMDSRC_ARM);
354
355 /* Update cache */
356 gpio->dcache[GPIO_BANK(offset)] = ioread32(bank_reg(gpio, bank, reg_rdata));
357
358 return true;
359}
360
361static void aspeed_gpio_copro_release(struct aspeed_gpio *gpio,
362 unsigned int offset)
363{
364 const struct aspeed_gpio_bank *bank = to_bank(offset);
365
366 if (!copro_ops || !gpio->cf_copro_bankmap)
367 return;
368 if (!gpio->cf_copro_bankmap[offset >> 3])
369 return;
370 if (!copro_ops->release_access)
371 return;
372
373 /* Change command source back to ColdFire */
374 aspeed_gpio_change_cmd_source(gpio, bank, offset >> 3,
375 GPIO_CMDSRC_COLDFIRE);
376
377 /* Restart the coprocessor */
378 copro_ops->release_access(copro_data);
379}
380
381static int aspeed_gpio_get(struct gpio_chip *gc, unsigned int offset)
382{
383 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
384 const struct aspeed_gpio_bank *bank = to_bank(offset);
385
386 return !!(ioread32(bank_reg(gpio, bank, reg_val)) & GPIO_BIT(offset));
387}
388
389static void __aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
390 int val)
391{
392 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
393 const struct aspeed_gpio_bank *bank = to_bank(offset);
394 void __iomem *addr;
395 u32 reg;
396
397 addr = bank_reg(gpio, bank, reg_val);
398 reg = gpio->dcache[GPIO_BANK(offset)];
399
400 if (val)
401 reg |= GPIO_BIT(offset);
402 else
403 reg &= ~GPIO_BIT(offset);
404 gpio->dcache[GPIO_BANK(offset)] = reg;
405
406 iowrite32(reg, addr);
407 /* Flush write */
408 ioread32(addr);
409}
410
411static void aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
412 int val)
413{
414 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
415 unsigned long flags;
416 bool copro;
417
418 raw_spin_lock_irqsave(&gpio->lock, flags);
419 copro = aspeed_gpio_copro_request(gpio, offset);
420
421 __aspeed_gpio_set(gc, offset, val);
422
423 if (copro)
424 aspeed_gpio_copro_release(gpio, offset);
425 raw_spin_unlock_irqrestore(&gpio->lock, flags);
426}
427
428static int aspeed_gpio_dir_in(struct gpio_chip *gc, unsigned int offset)
429{
430 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
431 const struct aspeed_gpio_bank *bank = to_bank(offset);
432 void __iomem *addr = bank_reg(gpio, bank, reg_dir);
433 unsigned long flags;
434 bool copro;
435 u32 reg;
436
437 if (!have_input(gpio, offset))
438 return -ENOTSUPP;
439
440 raw_spin_lock_irqsave(&gpio->lock, flags);
441
442 reg = ioread32(addr);
443 reg &= ~GPIO_BIT(offset);
444
445 copro = aspeed_gpio_copro_request(gpio, offset);
446 iowrite32(reg, addr);
447 if (copro)
448 aspeed_gpio_copro_release(gpio, offset);
449
450 raw_spin_unlock_irqrestore(&gpio->lock, flags);
451
452 return 0;
453}
454
455static int aspeed_gpio_dir_out(struct gpio_chip *gc,
456 unsigned int offset, int val)
457{
458 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
459 const struct aspeed_gpio_bank *bank = to_bank(offset);
460 void __iomem *addr = bank_reg(gpio, bank, reg_dir);
461 unsigned long flags;
462 bool copro;
463 u32 reg;
464
465 if (!have_output(gpio, offset))
466 return -ENOTSUPP;
467
468 raw_spin_lock_irqsave(&gpio->lock, flags);
469
470 reg = ioread32(addr);
471 reg |= GPIO_BIT(offset);
472
473 copro = aspeed_gpio_copro_request(gpio, offset);
474 __aspeed_gpio_set(gc, offset, val);
475 iowrite32(reg, addr);
476
477 if (copro)
478 aspeed_gpio_copro_release(gpio, offset);
479 raw_spin_unlock_irqrestore(&gpio->lock, flags);
480
481 return 0;
482}
483
484static int aspeed_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
485{
486 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
487 const struct aspeed_gpio_bank *bank = to_bank(offset);
488 unsigned long flags;
489 u32 val;
490
491 if (!have_input(gpio, offset))
492 return 0;
493
494 if (!have_output(gpio, offset))
495 return 1;
496
497 raw_spin_lock_irqsave(&gpio->lock, flags);
498
499 val = ioread32(bank_reg(gpio, bank, reg_dir)) & GPIO_BIT(offset);
500
501 raw_spin_unlock_irqrestore(&gpio->lock, flags);
502
503 return !val;
504
505}
506
507static inline int irqd_to_aspeed_gpio_data(struct irq_data *d,
508 struct aspeed_gpio **gpio,
509 const struct aspeed_gpio_bank **bank,
510 u32 *bit, int *offset)
511{
512 struct aspeed_gpio *internal;
513
514 *offset = irqd_to_hwirq(d);
515
516 internal = irq_data_get_irq_chip_data(d);
517
518 /* This might be a bit of a questionable place to check */
519 if (!have_irq(internal, *offset))
520 return -ENOTSUPP;
521
522 *gpio = internal;
523 *bank = to_bank(*offset);
524 *bit = GPIO_BIT(*offset);
525
526 return 0;
527}
528
529static void aspeed_gpio_irq_ack(struct irq_data *d)
530{
531 const struct aspeed_gpio_bank *bank;
532 struct aspeed_gpio *gpio;
533 unsigned long flags;
534 void __iomem *status_addr;
535 int rc, offset;
536 bool copro;
537 u32 bit;
538
539 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset);
540 if (rc)
541 return;
542
543 status_addr = bank_reg(gpio, bank, reg_irq_status);
544
545 raw_spin_lock_irqsave(&gpio->lock, flags);
546 copro = aspeed_gpio_copro_request(gpio, offset);
547
548 iowrite32(bit, status_addr);
549
550 if (copro)
551 aspeed_gpio_copro_release(gpio, offset);
552 raw_spin_unlock_irqrestore(&gpio->lock, flags);
553}
554
555static void aspeed_gpio_irq_set_mask(struct irq_data *d, bool set)
556{
557 const struct aspeed_gpio_bank *bank;
558 struct aspeed_gpio *gpio;
559 unsigned long flags;
560 u32 reg, bit;
561 void __iomem *addr;
562 int rc, offset;
563 bool copro;
564
565 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset);
566 if (rc)
567 return;
568
569 addr = bank_reg(gpio, bank, reg_irq_enable);
570
571 raw_spin_lock_irqsave(&gpio->lock, flags);
572 copro = aspeed_gpio_copro_request(gpio, offset);
573
574 reg = ioread32(addr);
575 if (set)
576 reg |= bit;
577 else
578 reg &= ~bit;
579 iowrite32(reg, addr);
580
581 if (copro)
582 aspeed_gpio_copro_release(gpio, offset);
583 raw_spin_unlock_irqrestore(&gpio->lock, flags);
584}
585
586static void aspeed_gpio_irq_mask(struct irq_data *d)
587{
588 aspeed_gpio_irq_set_mask(d, false);
589}
590
591static void aspeed_gpio_irq_unmask(struct irq_data *d)
592{
593 aspeed_gpio_irq_set_mask(d, true);
594}
595
596static int aspeed_gpio_set_type(struct irq_data *d, unsigned int type)
597{
598 u32 type0 = 0;
599 u32 type1 = 0;
600 u32 type2 = 0;
601 u32 bit, reg;
602 const struct aspeed_gpio_bank *bank;
603 irq_flow_handler_t handler;
604 struct aspeed_gpio *gpio;
605 unsigned long flags;
606 void __iomem *addr;
607 int rc, offset;
608 bool copro;
609
610 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset);
611 if (rc)
612 return -EINVAL;
613
614 switch (type & IRQ_TYPE_SENSE_MASK) {
615 case IRQ_TYPE_EDGE_BOTH:
616 type2 |= bit;
617 /* fall through */
618 case IRQ_TYPE_EDGE_RISING:
619 type0 |= bit;
620 /* fall through */
621 case IRQ_TYPE_EDGE_FALLING:
622 handler = handle_edge_irq;
623 break;
624 case IRQ_TYPE_LEVEL_HIGH:
625 type0 |= bit;
626 /* fall through */
627 case IRQ_TYPE_LEVEL_LOW:
628 type1 |= bit;
629 handler = handle_level_irq;
630 break;
631 default:
632 return -EINVAL;
633 }
634
635 raw_spin_lock_irqsave(&gpio->lock, flags);
636 copro = aspeed_gpio_copro_request(gpio, offset);
637
638 addr = bank_reg(gpio, bank, reg_irq_type0);
639 reg = ioread32(addr);
640 reg = (reg & ~bit) | type0;
641 iowrite32(reg, addr);
642
643 addr = bank_reg(gpio, bank, reg_irq_type1);
644 reg = ioread32(addr);
645 reg = (reg & ~bit) | type1;
646 iowrite32(reg, addr);
647
648 addr = bank_reg(gpio, bank, reg_irq_type2);
649 reg = ioread32(addr);
650 reg = (reg & ~bit) | type2;
651 iowrite32(reg, addr);
652
653 if (copro)
654 aspeed_gpio_copro_release(gpio, offset);
655 raw_spin_unlock_irqrestore(&gpio->lock, flags);
656
657 irq_set_handler_locked(d, handler);
658
659 return 0;
660}
661
662static void aspeed_gpio_irq_handler(struct irq_desc *desc)
663{
664 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
665 struct irq_chip *ic = irq_desc_get_chip(desc);
666 struct aspeed_gpio *data = gpiochip_get_data(gc);
667 unsigned int i, p, girq, banks;
668 unsigned long reg;
669 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
670
671 chained_irq_enter(ic, desc);
672
673 banks = DIV_ROUND_UP(gpio->chip.ngpio, 32);
674 for (i = 0; i < banks; i++) {
675 const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i];
676
677 reg = ioread32(bank_reg(data, bank, reg_irq_status));
678
679 for_each_set_bit(p, &reg, 32) {
680 girq = irq_find_mapping(gc->irq.domain, i * 32 + p);
681 generic_handle_irq(girq);
682 }
683
684 }
685
686 chained_irq_exit(ic, desc);
687}
688
689static void aspeed_init_irq_valid_mask(struct gpio_chip *gc,
690 unsigned long *valid_mask,
691 unsigned int ngpios)
692{
693 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
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->chip.ngpio)
705 break;
706
707 clear_bit(i, valid_mask);
708 }
709
710 props++;
711 }
712}
713
714static 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);
718 unsigned long flags;
719 void __iomem *treg;
720 bool copro;
721 u32 val;
722
723 treg = bank_reg(gpio, to_bank(offset), reg_tolerance);
724
725 raw_spin_lock_irqsave(&gpio->lock, flags);
726 copro = aspeed_gpio_copro_request(gpio, offset);
727
728 val = readl(treg);
729
730 if (enable)
731 val |= GPIO_BIT(offset);
732 else
733 val &= ~GPIO_BIT(offset);
734
735 writel(val, treg);
736
737 if (copro)
738 aspeed_gpio_copro_release(gpio, offset);
739 raw_spin_unlock_irqrestore(&gpio->lock, flags);
740
741 return 0;
742}
743
744static int aspeed_gpio_request(struct gpio_chip *chip, unsigned int offset)
745{
746 if (!have_gpio(gpiochip_get_data(chip), offset))
747 return -ENODEV;
748
749 return pinctrl_gpio_request(chip->base + offset);
750}
751
752static void aspeed_gpio_free(struct gpio_chip *chip, unsigned int offset)
753{
754 pinctrl_gpio_free(chip->base + offset);
755}
756
757static 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 */
781static 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 */
800static 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 */
819static 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 */
826static 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
834 /* Note: Debounce timer isn't under control of the command
835 * source registers, so no need to sync with the coprocessor
836 */
837 addr = bank_reg(gpio, bank, reg_debounce_sel1);
838 val = ioread32(addr);
839 iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE1(timer, offset), addr);
840
841 addr = bank_reg(gpio, bank, reg_debounce_sel2);
842 val = ioread32(addr);
843 iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE2(timer, offset), addr);
844}
845
846static 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
855 if (!gpio->clk)
856 return -EINVAL;
857
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 raw_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
924out:
925 raw_spin_unlock_irqrestore(&gpio->lock, flags);
926
927 return rc;
928}
929
930static 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 raw_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 raw_spin_unlock_irqrestore(&gpio->lock, flags);
943
944 return rc;
945}
946
947static 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
961static 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(chip->base + offset, config);
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;
977 else if (param == PIN_CONFIG_PERSIST_STATE)
978 return aspeed_gpio_reset_tolerance(chip, offset, arg);
979
980 return -ENOTSUPP;
981}
982
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 */
989int 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}
996EXPORT_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 */
1007int 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->chip.ngpio >> 3, GFP_KERNEL);
1018 if (!gpio->cf_copro_bankmap)
1019 return -ENOMEM;
1020 if (offset < 0 || offset > gpio->chip.ngpio)
1021 return -EINVAL;
1022 bindex = offset >> 3;
1023
1024 raw_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 raw_spin_unlock_irqrestore(&gpio->lock, flags);
1046 return rc;
1047}
1048EXPORT_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 */
1054int 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->chip.ngpio)
1066 return -EINVAL;
1067 bindex = offset >> 3;
1068
1069 raw_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 raw_spin_unlock_irqrestore(&gpio->lock, flags);
1084 return rc;
1085}
1086EXPORT_SYMBOL_GPL(aspeed_gpio_copro_release_gpio);
1087
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
1095static 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
1102static 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
1106static 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
1114static 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
1118static const struct aspeed_bank_props ast2600_bank_props[] = {
1119 /* input output */
1120 {5, 0xffffffff, 0xffffff00}, /* U/V/W/X */
1121 {6, 0x0000ffff, 0x0000ffff}, /* Y/Z */
1122 { },
1123};
1124
1125static const struct aspeed_gpio_config ast2600_config =
1126 /*
1127 * ast2600 has two controllers one with 208 GPIOs and one with 36 GPIOs.
1128 * We expect ngpio being set in the device tree and this is a fallback
1129 * option.
1130 */
1131 { .nr_gpios = 208, .props = ast2600_bank_props, };
1132
1133static const struct of_device_id aspeed_gpio_of_table[] = {
1134 { .compatible = "aspeed,ast2400-gpio", .data = &ast2400_config, },
1135 { .compatible = "aspeed,ast2500-gpio", .data = &ast2500_config, },
1136 { .compatible = "aspeed,ast2600-gpio", .data = &ast2600_config, },
1137 {}
1138};
1139MODULE_DEVICE_TABLE(of, aspeed_gpio_of_table);
1140
1141static int __init aspeed_gpio_probe(struct platform_device *pdev)
1142{
1143 const struct of_device_id *gpio_id;
1144 struct aspeed_gpio *gpio;
1145 int rc, i, banks, err;
1146 u32 ngpio;
1147
1148 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
1149 if (!gpio)
1150 return -ENOMEM;
1151
1152 gpio->base = devm_platform_ioremap_resource(pdev, 0);
1153 if (IS_ERR(gpio->base))
1154 return PTR_ERR(gpio->base);
1155
1156 raw_spin_lock_init(&gpio->lock);
1157
1158 gpio_id = of_match_node(aspeed_gpio_of_table, pdev->dev.of_node);
1159 if (!gpio_id)
1160 return -EINVAL;
1161
1162 gpio->clk = devm_clk_get_enabled(&pdev->dev, NULL);
1163 if (IS_ERR(gpio->clk)) {
1164 dev_warn(&pdev->dev,
1165 "Failed to get clock from devicetree, debouncing disabled\n");
1166 gpio->clk = NULL;
1167 }
1168
1169 gpio->config = gpio_id->data;
1170
1171 gpio->chip.parent = &pdev->dev;
1172 err = of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpio);
1173 gpio->chip.ngpio = (u16) ngpio;
1174 if (err)
1175 gpio->chip.ngpio = gpio->config->nr_gpios;
1176 gpio->chip.direction_input = aspeed_gpio_dir_in;
1177 gpio->chip.direction_output = aspeed_gpio_dir_out;
1178 gpio->chip.get_direction = aspeed_gpio_get_direction;
1179 gpio->chip.request = aspeed_gpio_request;
1180 gpio->chip.free = aspeed_gpio_free;
1181 gpio->chip.get = aspeed_gpio_get;
1182 gpio->chip.set = aspeed_gpio_set;
1183 gpio->chip.set_config = aspeed_gpio_set_config;
1184 gpio->chip.label = dev_name(&pdev->dev);
1185 gpio->chip.base = -1;
1186
1187 /* Allocate a cache of the output registers */
1188 banks = DIV_ROUND_UP(gpio->chip.ngpio, 32);
1189 gpio->dcache = devm_kcalloc(&pdev->dev,
1190 banks, sizeof(u32), GFP_KERNEL);
1191 if (!gpio->dcache)
1192 return -ENOMEM;
1193
1194 /*
1195 * Populate it with initial values read from the HW and switch
1196 * all command sources to the ARM by default
1197 */
1198 for (i = 0; i < banks; i++) {
1199 const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i];
1200 void __iomem *addr = bank_reg(gpio, bank, reg_rdata);
1201 gpio->dcache[i] = ioread32(addr);
1202 aspeed_gpio_change_cmd_source(gpio, bank, 0, GPIO_CMDSRC_ARM);
1203 aspeed_gpio_change_cmd_source(gpio, bank, 1, GPIO_CMDSRC_ARM);
1204 aspeed_gpio_change_cmd_source(gpio, bank, 2, GPIO_CMDSRC_ARM);
1205 aspeed_gpio_change_cmd_source(gpio, bank, 3, GPIO_CMDSRC_ARM);
1206 }
1207
1208 /* Optionally set up an irqchip if there is an IRQ */
1209 rc = platform_get_irq(pdev, 0);
1210 if (rc > 0) {
1211 struct gpio_irq_chip *girq;
1212
1213 gpio->irq = rc;
1214 girq = &gpio->chip.irq;
1215 girq->chip = &gpio->irqc;
1216 girq->chip->name = dev_name(&pdev->dev);
1217 girq->chip->irq_ack = aspeed_gpio_irq_ack;
1218 girq->chip->irq_mask = aspeed_gpio_irq_mask;
1219 girq->chip->irq_unmask = aspeed_gpio_irq_unmask;
1220 girq->chip->irq_set_type = aspeed_gpio_set_type;
1221 girq->parent_handler = aspeed_gpio_irq_handler;
1222 girq->num_parents = 1;
1223 girq->parents = devm_kcalloc(&pdev->dev, 1,
1224 sizeof(*girq->parents),
1225 GFP_KERNEL);
1226 if (!girq->parents)
1227 return -ENOMEM;
1228 girq->parents[0] = gpio->irq;
1229 girq->default_type = IRQ_TYPE_NONE;
1230 girq->handler = handle_bad_irq;
1231 girq->init_valid_mask = aspeed_init_irq_valid_mask;
1232 }
1233
1234 gpio->offset_timer =
1235 devm_kzalloc(&pdev->dev, gpio->chip.ngpio, GFP_KERNEL);
1236 if (!gpio->offset_timer)
1237 return -ENOMEM;
1238
1239 rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
1240 if (rc < 0)
1241 return rc;
1242
1243 return 0;
1244}
1245
1246static struct platform_driver aspeed_gpio_driver = {
1247 .driver = {
1248 .name = KBUILD_MODNAME,
1249 .of_match_table = aspeed_gpio_of_table,
1250 },
1251};
1252
1253module_platform_driver_probe(aspeed_gpio_driver, aspeed_gpio_probe);
1254
1255MODULE_DESCRIPTION("Aspeed GPIO Driver");
1256MODULE_LICENSE("GPL");