2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2016, 2017 Cavium Inc.
9 #include <linux/bitops.h>
10 #include <linux/gpio/driver.h>
11 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/pci.h>
17 #include <linux/spinlock.h>
20 #define GPIO_RX_DAT 0x0
21 #define GPIO_TX_SET 0x8
22 #define GPIO_TX_CLR 0x10
23 #define GPIO_CONST 0x90
24 #define GPIO_CONST_GPIOS_MASK 0xff
25 #define GPIO_BIT_CFG 0x400
26 #define GPIO_BIT_CFG_TX_OE BIT(0)
27 #define GPIO_BIT_CFG_PIN_XOR BIT(1)
28 #define GPIO_BIT_CFG_INT_EN BIT(2)
29 #define GPIO_BIT_CFG_INT_TYPE BIT(3)
30 #define GPIO_BIT_CFG_FIL_MASK GENMASK(11, 4)
31 #define GPIO_BIT_CFG_FIL_CNT_SHIFT 4
32 #define GPIO_BIT_CFG_FIL_SEL_SHIFT 8
33 #define GPIO_BIT_CFG_TX_OD BIT(12)
34 #define GPIO_BIT_CFG_PIN_SEL_MASK GENMASK(25, 16)
35 #define GPIO_INTR 0x800
36 #define GPIO_INTR_INTR BIT(0)
37 #define GPIO_INTR_INTR_W1S BIT(1)
38 #define GPIO_INTR_ENA_W1C BIT(2)
39 #define GPIO_INTR_ENA_W1S BIT(3)
40 #define GPIO_2ND_BANK 0x1400
42 #define GLITCH_FILTER_400NS ((4u << GPIO_BIT_CFG_FIL_SEL_SHIFT) | \
43 (9u << GPIO_BIT_CFG_FIL_CNT_SHIFT))
47 struct thunderx_line
{
48 struct thunderx_gpio
*txgpio
;
50 unsigned int fil_bits
;
53 struct thunderx_gpio
{
54 struct gpio_chip chip
;
55 u8 __iomem
*register_base
;
56 struct msix_entry
*msix_entries
; /* per line MSI-X */
57 struct thunderx_line
*line_entries
; /* per line irq info */
59 unsigned long invert_mask
[2];
60 unsigned long od_mask
[2];
64 static unsigned int bit_cfg_reg(unsigned int line
)
66 return 8 * line
+ GPIO_BIT_CFG
;
69 static unsigned int intr_reg(unsigned int line
)
71 return 8 * line
+ GPIO_INTR
;
74 static bool thunderx_gpio_is_gpio_nowarn(struct thunderx_gpio
*txgpio
,
77 u64 bit_cfg
= readq(txgpio
->register_base
+ bit_cfg_reg(line
));
79 return (bit_cfg
& GPIO_BIT_CFG_PIN_SEL_MASK
) == 0;
83 * Check (and WARN) that the pin is available for GPIO. We will not
84 * allow modification of the state of non-GPIO pins from this driver.
86 static bool thunderx_gpio_is_gpio(struct thunderx_gpio
*txgpio
,
89 bool rv
= thunderx_gpio_is_gpio_nowarn(txgpio
, line
);
91 WARN_RATELIMIT(!rv
, "Pin %d not available for GPIO\n", line
);
96 static int thunderx_gpio_request(struct gpio_chip
*chip
, unsigned int line
)
98 struct thunderx_gpio
*txgpio
= gpiochip_get_data(chip
);
100 return thunderx_gpio_is_gpio(txgpio
, line
) ? 0 : -EIO
;
103 static int thunderx_gpio_dir_in(struct gpio_chip
*chip
, unsigned int line
)
105 struct thunderx_gpio
*txgpio
= gpiochip_get_data(chip
);
107 if (!thunderx_gpio_is_gpio(txgpio
, line
))
110 raw_spin_lock(&txgpio
->lock
);
111 clear_bit(line
, txgpio
->invert_mask
);
112 clear_bit(line
, txgpio
->od_mask
);
113 writeq(txgpio
->line_entries
[line
].fil_bits
,
114 txgpio
->register_base
+ bit_cfg_reg(line
));
115 raw_spin_unlock(&txgpio
->lock
);
119 static void thunderx_gpio_set(struct gpio_chip
*chip
, unsigned int line
,
122 struct thunderx_gpio
*txgpio
= gpiochip_get_data(chip
);
123 int bank
= line
/ 64;
124 int bank_bit
= line
% 64;
126 void __iomem
*reg
= txgpio
->register_base
+
127 (bank
* GPIO_2ND_BANK
) + (value
? GPIO_TX_SET
: GPIO_TX_CLR
);
129 writeq(BIT_ULL(bank_bit
), reg
);
132 static int thunderx_gpio_dir_out(struct gpio_chip
*chip
, unsigned int line
,
135 struct thunderx_gpio
*txgpio
= gpiochip_get_data(chip
);
136 u64 bit_cfg
= txgpio
->line_entries
[line
].fil_bits
| GPIO_BIT_CFG_TX_OE
;
138 if (!thunderx_gpio_is_gpio(txgpio
, line
))
141 raw_spin_lock(&txgpio
->lock
);
143 thunderx_gpio_set(chip
, line
, value
);
145 if (test_bit(line
, txgpio
->invert_mask
))
146 bit_cfg
|= GPIO_BIT_CFG_PIN_XOR
;
148 if (test_bit(line
, txgpio
->od_mask
))
149 bit_cfg
|= GPIO_BIT_CFG_TX_OD
;
151 writeq(bit_cfg
, txgpio
->register_base
+ bit_cfg_reg(line
));
153 raw_spin_unlock(&txgpio
->lock
);
157 static int thunderx_gpio_get_direction(struct gpio_chip
*chip
, unsigned int line
)
159 struct thunderx_gpio
*txgpio
= gpiochip_get_data(chip
);
162 if (!thunderx_gpio_is_gpio_nowarn(txgpio
, line
))
164 * Say it is input for now to avoid WARNing on
165 * gpiochip_add_data(). We will WARN if someone
166 * requests it or tries to use it.
170 bit_cfg
= readq(txgpio
->register_base
+ bit_cfg_reg(line
));
172 return !(bit_cfg
& GPIO_BIT_CFG_TX_OE
);
175 static int thunderx_gpio_set_config(struct gpio_chip
*chip
,
179 bool orig_invert
, orig_od
, orig_dat
, new_invert
, new_od
;
182 int bank
= line
/ 64;
183 int bank_bit
= line
% 64;
185 struct thunderx_gpio
*txgpio
= gpiochip_get_data(chip
);
186 void __iomem
*reg
= txgpio
->register_base
+ (bank
* GPIO_2ND_BANK
) + GPIO_TX_SET
;
188 if (!thunderx_gpio_is_gpio(txgpio
, line
))
191 raw_spin_lock(&txgpio
->lock
);
192 orig_invert
= test_bit(line
, txgpio
->invert_mask
);
193 new_invert
= orig_invert
;
194 orig_od
= test_bit(line
, txgpio
->od_mask
);
196 orig_dat
= ((readq(reg
) >> bank_bit
) & 1) ^ orig_invert
;
197 bit_cfg
= readq(txgpio
->register_base
+ bit_cfg_reg(line
));
198 switch (pinconf_to_config_param(cfg
)) {
199 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
201 * Weird, setting open-drain mode causes signal
202 * inversion. Note this so we can compensate in the
205 set_bit(line
, txgpio
->invert_mask
);
207 set_bit(line
, txgpio
->od_mask
);
211 case PIN_CONFIG_DRIVE_PUSH_PULL
:
212 clear_bit(line
, txgpio
->invert_mask
);
214 clear_bit(line
, txgpio
->od_mask
);
218 case PIN_CONFIG_INPUT_DEBOUNCE
:
219 arg
= pinconf_to_config_argument(cfg
);
220 if (arg
> 1228) { /* 15 * 2^15 * 2.5nS maximum */
224 arg
*= 400; /* scale to 2.5nS clocks. */
228 arg
++; /* always round up */
231 txgpio
->line_entries
[line
].fil_bits
=
232 (sel
<< GPIO_BIT_CFG_FIL_SEL_SHIFT
) |
233 (arg
<< GPIO_BIT_CFG_FIL_CNT_SHIFT
);
234 bit_cfg
&= ~GPIO_BIT_CFG_FIL_MASK
;
235 bit_cfg
|= txgpio
->line_entries
[line
].fil_bits
;
236 writeq(bit_cfg
, txgpio
->register_base
+ bit_cfg_reg(line
));
242 raw_spin_unlock(&txgpio
->lock
);
245 * If currently output and OPEN_DRAIN changed, install the new
248 if ((new_invert
!= orig_invert
|| new_od
!= orig_od
) &&
249 (bit_cfg
& GPIO_BIT_CFG_TX_OE
))
250 ret
= thunderx_gpio_dir_out(chip
, line
, orig_dat
^ new_invert
);
255 static int thunderx_gpio_get(struct gpio_chip
*chip
, unsigned int line
)
257 struct thunderx_gpio
*txgpio
= gpiochip_get_data(chip
);
258 int bank
= line
/ 64;
259 int bank_bit
= line
% 64;
260 u64 read_bits
= readq(txgpio
->register_base
+ (bank
* GPIO_2ND_BANK
) + GPIO_RX_DAT
);
261 u64 masked_bits
= read_bits
& BIT_ULL(bank_bit
);
263 if (test_bit(line
, txgpio
->invert_mask
))
264 return masked_bits
== 0;
266 return masked_bits
!= 0;
269 static void thunderx_gpio_set_multiple(struct gpio_chip
*chip
,
274 u64 set_bits
, clear_bits
;
275 struct thunderx_gpio
*txgpio
= gpiochip_get_data(chip
);
277 for (bank
= 0; bank
<= chip
->ngpio
/ 64; bank
++) {
278 set_bits
= bits
[bank
] & mask
[bank
];
279 clear_bits
= ~bits
[bank
] & mask
[bank
];
280 writeq(set_bits
, txgpio
->register_base
+ (bank
* GPIO_2ND_BANK
) + GPIO_TX_SET
);
281 writeq(clear_bits
, txgpio
->register_base
+ (bank
* GPIO_2ND_BANK
) + GPIO_TX_CLR
);
285 static void thunderx_gpio_irq_ack(struct irq_data
*d
)
287 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
288 struct thunderx_gpio
*txgpio
= gpiochip_get_data(gc
);
290 writeq(GPIO_INTR_INTR
,
291 txgpio
->register_base
+ intr_reg(irqd_to_hwirq(d
)));
294 static void thunderx_gpio_irq_mask(struct irq_data
*d
)
296 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
297 struct thunderx_gpio
*txgpio
= gpiochip_get_data(gc
);
299 writeq(GPIO_INTR_ENA_W1C
,
300 txgpio
->register_base
+ intr_reg(irqd_to_hwirq(d
)));
303 static void thunderx_gpio_irq_mask_ack(struct irq_data
*d
)
305 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
306 struct thunderx_gpio
*txgpio
= gpiochip_get_data(gc
);
308 writeq(GPIO_INTR_ENA_W1C
| GPIO_INTR_INTR
,
309 txgpio
->register_base
+ intr_reg(irqd_to_hwirq(d
)));
312 static void thunderx_gpio_irq_unmask(struct irq_data
*d
)
314 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
315 struct thunderx_gpio
*txgpio
= gpiochip_get_data(gc
);
317 writeq(GPIO_INTR_ENA_W1S
,
318 txgpio
->register_base
+ intr_reg(irqd_to_hwirq(d
)));
321 static int thunderx_gpio_irq_set_type(struct irq_data
*d
,
322 unsigned int flow_type
)
324 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
325 struct thunderx_gpio
*txgpio
= gpiochip_get_data(gc
);
326 struct thunderx_line
*txline
=
327 &txgpio
->line_entries
[irqd_to_hwirq(d
)];
330 irqd_set_trigger_type(d
, flow_type
);
332 bit_cfg
= txline
->fil_bits
| GPIO_BIT_CFG_INT_EN
;
334 if (flow_type
& IRQ_TYPE_EDGE_BOTH
) {
335 irq_set_handler_locked(d
, handle_fasteoi_ack_irq
);
336 bit_cfg
|= GPIO_BIT_CFG_INT_TYPE
;
338 irq_set_handler_locked(d
, handle_fasteoi_mask_irq
);
341 raw_spin_lock(&txgpio
->lock
);
342 if (flow_type
& (IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_LEVEL_LOW
)) {
343 bit_cfg
|= GPIO_BIT_CFG_PIN_XOR
;
344 set_bit(txline
->line
, txgpio
->invert_mask
);
346 clear_bit(txline
->line
, txgpio
->invert_mask
);
348 clear_bit(txline
->line
, txgpio
->od_mask
);
349 writeq(bit_cfg
, txgpio
->register_base
+ bit_cfg_reg(txline
->line
));
350 raw_spin_unlock(&txgpio
->lock
);
352 return IRQ_SET_MASK_OK
;
355 static void thunderx_gpio_irq_enable(struct irq_data
*data
)
357 irq_chip_enable_parent(data
);
358 thunderx_gpio_irq_unmask(data
);
361 static void thunderx_gpio_irq_disable(struct irq_data
*data
)
363 thunderx_gpio_irq_mask(data
);
364 irq_chip_disable_parent(data
);
368 * Interrupts are chained from underlying MSI-X vectors. We have
369 * these irq_chip functions to be able to handle level triggering
370 * semantics and other acknowledgment tasks associated with the GPIO
373 static struct irq_chip thunderx_gpio_irq_chip
= {
375 .irq_enable
= thunderx_gpio_irq_enable
,
376 .irq_disable
= thunderx_gpio_irq_disable
,
377 .irq_ack
= thunderx_gpio_irq_ack
,
378 .irq_mask
= thunderx_gpio_irq_mask
,
379 .irq_mask_ack
= thunderx_gpio_irq_mask_ack
,
380 .irq_unmask
= thunderx_gpio_irq_unmask
,
381 .irq_eoi
= irq_chip_eoi_parent
,
382 .irq_set_affinity
= irq_chip_set_affinity_parent
,
383 .irq_set_type
= thunderx_gpio_irq_set_type
,
385 .flags
= IRQCHIP_SET_TYPE_MASKED
388 static int thunderx_gpio_child_to_parent_hwirq(struct gpio_chip
*gc
,
390 unsigned int child_type
,
391 unsigned int *parent
,
392 unsigned int *parent_type
)
394 struct thunderx_gpio
*txgpio
= gpiochip_get_data(gc
);
396 *parent
= txgpio
->base_msi
+ (2 * child
);
397 *parent_type
= IRQ_TYPE_LEVEL_HIGH
;
401 static int thunderx_gpio_probe(struct pci_dev
*pdev
,
402 const struct pci_device_id
*id
)
404 void __iomem
* const *tbl
;
405 struct device
*dev
= &pdev
->dev
;
406 struct thunderx_gpio
*txgpio
;
407 struct gpio_chip
*chip
;
408 struct gpio_irq_chip
*girq
;
412 txgpio
= devm_kzalloc(dev
, sizeof(*txgpio
), GFP_KERNEL
);
416 raw_spin_lock_init(&txgpio
->lock
);
417 chip
= &txgpio
->chip
;
419 pci_set_drvdata(pdev
, txgpio
);
421 err
= pcim_enable_device(pdev
);
423 dev_err(dev
, "Failed to enable PCI device: err %d\n", err
);
427 err
= pcim_iomap_regions(pdev
, 1 << 0, KBUILD_MODNAME
);
429 dev_err(dev
, "Failed to iomap PCI device: err %d\n", err
);
433 tbl
= pcim_iomap_table(pdev
);
434 txgpio
->register_base
= tbl
[0];
435 if (!txgpio
->register_base
) {
436 dev_err(dev
, "Cannot map PCI resource\n");
441 if (pdev
->subsystem_device
== 0xa10a) {
442 /* CN88XX has no GPIO_CONST register*/
444 txgpio
->base_msi
= 48;
446 u64 c
= readq(txgpio
->register_base
+ GPIO_CONST
);
448 ngpio
= c
& GPIO_CONST_GPIOS_MASK
;
449 txgpio
->base_msi
= (c
>> 8) & 0xff;
452 txgpio
->msix_entries
= devm_kcalloc(dev
,
453 ngpio
, sizeof(struct msix_entry
),
455 if (!txgpio
->msix_entries
) {
460 txgpio
->line_entries
= devm_kcalloc(dev
,
462 sizeof(struct thunderx_line
),
464 if (!txgpio
->line_entries
) {
469 for (i
= 0; i
< ngpio
; i
++) {
470 u64 bit_cfg
= readq(txgpio
->register_base
+ bit_cfg_reg(i
));
472 txgpio
->msix_entries
[i
].entry
= txgpio
->base_msi
+ (2 * i
);
473 txgpio
->line_entries
[i
].line
= i
;
474 txgpio
->line_entries
[i
].txgpio
= txgpio
;
476 * If something has already programmed the pin, use
477 * the existing glitch filter settings, otherwise go
480 txgpio
->line_entries
[i
].fil_bits
= bit_cfg
?
481 (bit_cfg
& GPIO_BIT_CFG_FIL_MASK
) : GLITCH_FILTER_400NS
;
483 if ((bit_cfg
& GPIO_BIT_CFG_TX_OE
) && (bit_cfg
& GPIO_BIT_CFG_TX_OD
))
484 set_bit(i
, txgpio
->od_mask
);
485 if (bit_cfg
& GPIO_BIT_CFG_PIN_XOR
)
486 set_bit(i
, txgpio
->invert_mask
);
490 /* Enable all MSI-X for interrupts on all possible lines. */
491 err
= pci_enable_msix_range(pdev
, txgpio
->msix_entries
, ngpio
, ngpio
);
495 chip
->label
= KBUILD_MODNAME
;
497 chip
->owner
= THIS_MODULE
;
498 chip
->request
= thunderx_gpio_request
;
499 chip
->base
= -1; /* System allocated */
500 chip
->can_sleep
= false;
502 chip
->get_direction
= thunderx_gpio_get_direction
;
503 chip
->direction_input
= thunderx_gpio_dir_in
;
504 chip
->get
= thunderx_gpio_get
;
505 chip
->direction_output
= thunderx_gpio_dir_out
;
506 chip
->set
= thunderx_gpio_set
;
507 chip
->set_multiple
= thunderx_gpio_set_multiple
;
508 chip
->set_config
= thunderx_gpio_set_config
;
510 girq
->chip
= &thunderx_gpio_irq_chip
;
511 girq
->fwnode
= of_node_to_fwnode(dev
->of_node
);
512 girq
->parent_domain
=
513 irq_get_irq_data(txgpio
->msix_entries
[0].vector
)->domain
;
514 girq
->child_to_parent_hwirq
= thunderx_gpio_child_to_parent_hwirq
;
515 girq
->handler
= handle_bad_irq
;
516 girq
->default_type
= IRQ_TYPE_NONE
;
518 err
= devm_gpiochip_add_data(dev
, chip
, txgpio
);
522 /* Push on irq_data and the domain for each line. */
523 for (i
= 0; i
< ngpio
; i
++) {
524 err
= irq_domain_push_irq(chip
->irq
.domain
,
525 txgpio
->msix_entries
[i
].vector
,
528 dev_err(dev
, "irq_domain_push_irq: %d\n", err
);
531 dev_info(dev
, "ThunderX GPIO: %d lines with base %d.\n",
535 pci_set_drvdata(pdev
, NULL
);
539 static void thunderx_gpio_remove(struct pci_dev
*pdev
)
542 struct thunderx_gpio
*txgpio
= pci_get_drvdata(pdev
);
544 for (i
= 0; i
< txgpio
->chip
.ngpio
; i
++)
545 irq_domain_pop_irq(txgpio
->chip
.irq
.domain
,
546 txgpio
->msix_entries
[i
].vector
);
548 irq_domain_remove(txgpio
->chip
.irq
.domain
);
550 pci_set_drvdata(pdev
, NULL
);
553 static const struct pci_device_id thunderx_gpio_id_table
[] = {
554 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM
, 0xA00A) },
555 { 0, } /* end of table */
558 MODULE_DEVICE_TABLE(pci
, thunderx_gpio_id_table
);
560 static struct pci_driver thunderx_gpio_driver
= {
561 .name
= KBUILD_MODNAME
,
562 .id_table
= thunderx_gpio_id_table
,
563 .probe
= thunderx_gpio_probe
,
564 .remove
= thunderx_gpio_remove
,
567 module_pci_driver(thunderx_gpio_driver
);
569 MODULE_DESCRIPTION("Cavium Inc. ThunderX/OCTEON-TX GPIO Driver");
570 MODULE_LICENSE("GPL");