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>
19 #define GPIO_RX_DAT 0x0
20 #define GPIO_TX_SET 0x8
21 #define GPIO_TX_CLR 0x10
22 #define GPIO_CONST 0x90
23 #define GPIO_CONST_GPIOS_MASK 0xff
24 #define GPIO_BIT_CFG 0x400
25 #define GPIO_BIT_CFG_TX_OE BIT(0)
26 #define GPIO_BIT_CFG_PIN_XOR BIT(1)
27 #define GPIO_BIT_CFG_INT_EN BIT(2)
28 #define GPIO_BIT_CFG_INT_TYPE BIT(3)
29 #define GPIO_BIT_CFG_FIL_MASK GENMASK(11, 4)
30 #define GPIO_BIT_CFG_FIL_CNT_SHIFT 4
31 #define GPIO_BIT_CFG_FIL_SEL_SHIFT 8
32 #define GPIO_BIT_CFG_TX_OD BIT(12)
33 #define GPIO_BIT_CFG_PIN_SEL_MASK GENMASK(25, 16)
34 #define GPIO_INTR 0x800
35 #define GPIO_INTR_INTR BIT(0)
36 #define GPIO_INTR_INTR_W1S BIT(1)
37 #define GPIO_INTR_ENA_W1C BIT(2)
38 #define GPIO_INTR_ENA_W1S BIT(3)
39 #define GPIO_2ND_BANK 0x1400
41 #define GLITCH_FILTER_400NS ((4u << GPIO_BIT_CFG_FIL_SEL_SHIFT) | \
42 (9u << GPIO_BIT_CFG_FIL_CNT_SHIFT))
46 struct thunderx_line {
47 struct thunderx_gpio *txgpio;
49 unsigned int fil_bits;
52 struct thunderx_gpio {
53 struct gpio_chip chip;
54 u8 __iomem *register_base;
55 struct msix_entry *msix_entries; /* per line MSI-X */
56 struct thunderx_line *line_entries; /* per line irq info */
58 unsigned long invert_mask[2];
59 unsigned long od_mask[2];
63 static unsigned int bit_cfg_reg(unsigned int line)
65 return 8 * line + GPIO_BIT_CFG;
68 static unsigned int intr_reg(unsigned int line)
70 return 8 * line + GPIO_INTR;
73 static bool thunderx_gpio_is_gpio_nowarn(struct thunderx_gpio *txgpio,
76 u64 bit_cfg = readq(txgpio->register_base + bit_cfg_reg(line));
78 return (bit_cfg & GPIO_BIT_CFG_PIN_SEL_MASK) == 0;
82 * Check (and WARN) that the pin is available for GPIO. We will not
83 * allow modification of the state of non-GPIO pins from this driver.
85 static bool thunderx_gpio_is_gpio(struct thunderx_gpio *txgpio,
88 bool rv = thunderx_gpio_is_gpio_nowarn(txgpio, line);
90 WARN_RATELIMIT(!rv, "Pin %d not available for GPIO\n", line);
95 static int thunderx_gpio_request(struct gpio_chip *chip, unsigned int line)
97 struct thunderx_gpio *txgpio = gpiochip_get_data(chip);
99 return thunderx_gpio_is_gpio(txgpio, line) ? 0 : -EIO;
102 static int thunderx_gpio_dir_in(struct gpio_chip *chip, unsigned int line)
104 struct thunderx_gpio *txgpio = gpiochip_get_data(chip);
106 if (!thunderx_gpio_is_gpio(txgpio, line))
109 raw_spin_lock(&txgpio->lock);
110 clear_bit(line, txgpio->invert_mask);
111 clear_bit(line, txgpio->od_mask);
112 writeq(txgpio->line_entries[line].fil_bits,
113 txgpio->register_base + bit_cfg_reg(line));
114 raw_spin_unlock(&txgpio->lock);
118 static void thunderx_gpio_set(struct gpio_chip *chip, unsigned int line,
121 struct thunderx_gpio *txgpio = gpiochip_get_data(chip);
122 int bank = line / 64;
123 int bank_bit = line % 64;
125 void __iomem *reg = txgpio->register_base +
126 (bank * GPIO_2ND_BANK) + (value ? GPIO_TX_SET : GPIO_TX_CLR);
128 writeq(BIT_ULL(bank_bit), reg);
131 static int thunderx_gpio_dir_out(struct gpio_chip *chip, unsigned int line,
134 struct thunderx_gpio *txgpio = gpiochip_get_data(chip);
135 u64 bit_cfg = txgpio->line_entries[line].fil_bits | GPIO_BIT_CFG_TX_OE;
137 if (!thunderx_gpio_is_gpio(txgpio, line))
140 raw_spin_lock(&txgpio->lock);
142 thunderx_gpio_set(chip, line, value);
144 if (test_bit(line, txgpio->invert_mask))
145 bit_cfg |= GPIO_BIT_CFG_PIN_XOR;
147 if (test_bit(line, txgpio->od_mask))
148 bit_cfg |= GPIO_BIT_CFG_TX_OD;
150 writeq(bit_cfg, txgpio->register_base + bit_cfg_reg(line));
152 raw_spin_unlock(&txgpio->lock);
156 static int thunderx_gpio_get_direction(struct gpio_chip *chip, unsigned int line)
158 struct thunderx_gpio *txgpio = gpiochip_get_data(chip);
161 if (!thunderx_gpio_is_gpio_nowarn(txgpio, line))
163 * Say it is input for now to avoid WARNing on
164 * gpiochip_add_data(). We will WARN if someone
165 * requests it or tries to use it.
169 bit_cfg = readq(txgpio->register_base + bit_cfg_reg(line));
171 if (bit_cfg & GPIO_BIT_CFG_TX_OE)
172 return GPIO_LINE_DIRECTION_OUT;
174 return GPIO_LINE_DIRECTION_IN;
177 static int thunderx_gpio_set_config(struct gpio_chip *chip,
181 bool orig_invert, orig_od, orig_dat, new_invert, new_od;
184 int bank = line / 64;
185 int bank_bit = line % 64;
187 struct thunderx_gpio *txgpio = gpiochip_get_data(chip);
188 void __iomem *reg = txgpio->register_base + (bank * GPIO_2ND_BANK) + GPIO_TX_SET;
190 if (!thunderx_gpio_is_gpio(txgpio, line))
193 raw_spin_lock(&txgpio->lock);
194 orig_invert = test_bit(line, txgpio->invert_mask);
195 new_invert = orig_invert;
196 orig_od = test_bit(line, txgpio->od_mask);
198 orig_dat = ((readq(reg) >> bank_bit) & 1) ^ orig_invert;
199 bit_cfg = readq(txgpio->register_base + bit_cfg_reg(line));
200 switch (pinconf_to_config_param(cfg)) {
201 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
203 * Weird, setting open-drain mode causes signal
204 * inversion. Note this so we can compensate in the
207 set_bit(line, txgpio->invert_mask);
209 set_bit(line, txgpio->od_mask);
213 case PIN_CONFIG_DRIVE_PUSH_PULL:
214 clear_bit(line, txgpio->invert_mask);
216 clear_bit(line, txgpio->od_mask);
220 case PIN_CONFIG_INPUT_DEBOUNCE:
221 arg = pinconf_to_config_argument(cfg);
222 if (arg > 1228) { /* 15 * 2^15 * 2.5nS maximum */
226 arg *= 400; /* scale to 2.5nS clocks. */
230 arg++; /* always round up */
233 txgpio->line_entries[line].fil_bits =
234 (sel << GPIO_BIT_CFG_FIL_SEL_SHIFT) |
235 (arg << GPIO_BIT_CFG_FIL_CNT_SHIFT);
236 bit_cfg &= ~GPIO_BIT_CFG_FIL_MASK;
237 bit_cfg |= txgpio->line_entries[line].fil_bits;
238 writeq(bit_cfg, txgpio->register_base + bit_cfg_reg(line));
244 raw_spin_unlock(&txgpio->lock);
247 * If currently output and OPEN_DRAIN changed, install the new
250 if ((new_invert != orig_invert || new_od != orig_od) &&
251 (bit_cfg & GPIO_BIT_CFG_TX_OE))
252 ret = thunderx_gpio_dir_out(chip, line, orig_dat ^ new_invert);
257 static int thunderx_gpio_get(struct gpio_chip *chip, unsigned int line)
259 struct thunderx_gpio *txgpio = gpiochip_get_data(chip);
260 int bank = line / 64;
261 int bank_bit = line % 64;
262 u64 read_bits = readq(txgpio->register_base + (bank * GPIO_2ND_BANK) + GPIO_RX_DAT);
263 u64 masked_bits = read_bits & BIT_ULL(bank_bit);
265 if (test_bit(line, txgpio->invert_mask))
266 return masked_bits == 0;
268 return masked_bits != 0;
271 static void thunderx_gpio_set_multiple(struct gpio_chip *chip,
276 u64 set_bits, clear_bits;
277 struct thunderx_gpio *txgpio = gpiochip_get_data(chip);
279 for (bank = 0; bank <= chip->ngpio / 64; bank++) {
280 set_bits = bits[bank] & mask[bank];
281 clear_bits = ~bits[bank] & mask[bank];
282 writeq(set_bits, txgpio->register_base + (bank * GPIO_2ND_BANK) + GPIO_TX_SET);
283 writeq(clear_bits, txgpio->register_base + (bank * GPIO_2ND_BANK) + GPIO_TX_CLR);
287 static void thunderx_gpio_irq_ack(struct irq_data *d)
289 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
290 struct thunderx_gpio *txgpio = gpiochip_get_data(gc);
292 writeq(GPIO_INTR_INTR,
293 txgpio->register_base + intr_reg(irqd_to_hwirq(d)));
296 static void thunderx_gpio_irq_mask(struct irq_data *d)
298 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
299 struct thunderx_gpio *txgpio = gpiochip_get_data(gc);
301 writeq(GPIO_INTR_ENA_W1C,
302 txgpio->register_base + intr_reg(irqd_to_hwirq(d)));
305 static void thunderx_gpio_irq_mask_ack(struct irq_data *d)
307 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
308 struct thunderx_gpio *txgpio = gpiochip_get_data(gc);
310 writeq(GPIO_INTR_ENA_W1C | GPIO_INTR_INTR,
311 txgpio->register_base + intr_reg(irqd_to_hwirq(d)));
314 static void thunderx_gpio_irq_unmask(struct irq_data *d)
316 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
317 struct thunderx_gpio *txgpio = gpiochip_get_data(gc);
319 writeq(GPIO_INTR_ENA_W1S,
320 txgpio->register_base + intr_reg(irqd_to_hwirq(d)));
323 static int thunderx_gpio_irq_set_type(struct irq_data *d,
324 unsigned int flow_type)
326 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
327 struct thunderx_gpio *txgpio = gpiochip_get_data(gc);
328 struct thunderx_line *txline =
329 &txgpio->line_entries[irqd_to_hwirq(d)];
332 irqd_set_trigger_type(d, flow_type);
334 bit_cfg = txline->fil_bits | GPIO_BIT_CFG_INT_EN;
336 if (flow_type & IRQ_TYPE_EDGE_BOTH) {
337 irq_set_handler_locked(d, handle_fasteoi_ack_irq);
338 bit_cfg |= GPIO_BIT_CFG_INT_TYPE;
340 irq_set_handler_locked(d, handle_fasteoi_mask_irq);
343 raw_spin_lock(&txgpio->lock);
344 if (flow_type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_LEVEL_LOW)) {
345 bit_cfg |= GPIO_BIT_CFG_PIN_XOR;
346 set_bit(txline->line, txgpio->invert_mask);
348 clear_bit(txline->line, txgpio->invert_mask);
350 clear_bit(txline->line, txgpio->od_mask);
351 writeq(bit_cfg, txgpio->register_base + bit_cfg_reg(txline->line));
352 raw_spin_unlock(&txgpio->lock);
354 return IRQ_SET_MASK_OK;
357 static void thunderx_gpio_irq_enable(struct irq_data *d)
359 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
361 gpiochip_enable_irq(gc, irqd_to_hwirq(d));
362 irq_chip_enable_parent(d);
363 thunderx_gpio_irq_unmask(d);
366 static void thunderx_gpio_irq_disable(struct irq_data *d)
368 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
370 thunderx_gpio_irq_mask(d);
371 irq_chip_disable_parent(d);
372 gpiochip_disable_irq(gc, irqd_to_hwirq(d));
376 * Interrupts are chained from underlying MSI-X vectors. We have
377 * these irq_chip functions to be able to handle level triggering
378 * semantics and other acknowledgment tasks associated with the GPIO
381 static const struct irq_chip thunderx_gpio_irq_chip = {
383 .irq_enable = thunderx_gpio_irq_enable,
384 .irq_disable = thunderx_gpio_irq_disable,
385 .irq_ack = thunderx_gpio_irq_ack,
386 .irq_mask = thunderx_gpio_irq_mask,
387 .irq_mask_ack = thunderx_gpio_irq_mask_ack,
388 .irq_unmask = thunderx_gpio_irq_unmask,
389 .irq_eoi = irq_chip_eoi_parent,
390 .irq_set_affinity = irq_chip_set_affinity_parent,
391 .irq_set_type = thunderx_gpio_irq_set_type,
392 .flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_IMMUTABLE,
393 GPIOCHIP_IRQ_RESOURCE_HELPERS,
396 static int thunderx_gpio_child_to_parent_hwirq(struct gpio_chip *gc,
398 unsigned int child_type,
399 unsigned int *parent,
400 unsigned int *parent_type)
402 struct thunderx_gpio *txgpio = gpiochip_get_data(gc);
403 struct irq_data *irqd;
406 irq = txgpio->msix_entries[child].vector;
407 irqd = irq_domain_get_irq_data(gc->irq.parent_domain, irq);
410 *parent = irqd_to_hwirq(irqd);
411 *parent_type = IRQ_TYPE_LEVEL_HIGH;
415 static int thunderx_gpio_populate_parent_alloc_info(struct gpio_chip *chip,
416 union gpio_irq_fwspec *gfwspec,
417 unsigned int parent_hwirq,
418 unsigned int parent_type)
420 msi_alloc_info_t *info = &gfwspec->msiinfo;
422 info->hwirq = parent_hwirq;
426 static int thunderx_gpio_probe(struct pci_dev *pdev,
427 const struct pci_device_id *id)
429 void __iomem * const *tbl;
430 struct device *dev = &pdev->dev;
431 struct thunderx_gpio *txgpio;
432 struct gpio_chip *chip;
433 struct gpio_irq_chip *girq;
437 txgpio = devm_kzalloc(dev, sizeof(*txgpio), GFP_KERNEL);
441 raw_spin_lock_init(&txgpio->lock);
442 chip = &txgpio->chip;
444 pci_set_drvdata(pdev, txgpio);
446 err = pcim_enable_device(pdev);
448 dev_err(dev, "Failed to enable PCI device: err %d\n", err);
452 err = pcim_iomap_regions(pdev, 1 << 0, KBUILD_MODNAME);
454 dev_err(dev, "Failed to iomap PCI device: err %d\n", err);
458 tbl = pcim_iomap_table(pdev);
459 txgpio->register_base = tbl[0];
460 if (!txgpio->register_base) {
461 dev_err(dev, "Cannot map PCI resource\n");
466 if (pdev->subsystem_device == 0xa10a) {
467 /* CN88XX has no GPIO_CONST register*/
469 txgpio->base_msi = 48;
471 u64 c = readq(txgpio->register_base + GPIO_CONST);
473 ngpio = c & GPIO_CONST_GPIOS_MASK;
474 txgpio->base_msi = (c >> 8) & 0xff;
477 txgpio->msix_entries = devm_kcalloc(dev,
478 ngpio, sizeof(struct msix_entry),
480 if (!txgpio->msix_entries) {
485 txgpio->line_entries = devm_kcalloc(dev,
487 sizeof(struct thunderx_line),
489 if (!txgpio->line_entries) {
494 for (i = 0; i < ngpio; i++) {
495 u64 bit_cfg = readq(txgpio->register_base + bit_cfg_reg(i));
497 txgpio->msix_entries[i].entry = txgpio->base_msi + (2 * i);
498 txgpio->line_entries[i].line = i;
499 txgpio->line_entries[i].txgpio = txgpio;
501 * If something has already programmed the pin, use
502 * the existing glitch filter settings, otherwise go
505 txgpio->line_entries[i].fil_bits = bit_cfg ?
506 (bit_cfg & GPIO_BIT_CFG_FIL_MASK) : GLITCH_FILTER_400NS;
508 if ((bit_cfg & GPIO_BIT_CFG_TX_OE) && (bit_cfg & GPIO_BIT_CFG_TX_OD))
509 set_bit(i, txgpio->od_mask);
510 if (bit_cfg & GPIO_BIT_CFG_PIN_XOR)
511 set_bit(i, txgpio->invert_mask);
515 /* Enable all MSI-X for interrupts on all possible lines. */
516 err = pci_enable_msix_range(pdev, txgpio->msix_entries, ngpio, ngpio);
520 chip->label = KBUILD_MODNAME;
522 chip->owner = THIS_MODULE;
523 chip->request = thunderx_gpio_request;
524 chip->base = -1; /* System allocated */
525 chip->can_sleep = false;
527 chip->get_direction = thunderx_gpio_get_direction;
528 chip->direction_input = thunderx_gpio_dir_in;
529 chip->get = thunderx_gpio_get;
530 chip->direction_output = thunderx_gpio_dir_out;
531 chip->set = thunderx_gpio_set;
532 chip->set_multiple = thunderx_gpio_set_multiple;
533 chip->set_config = thunderx_gpio_set_config;
535 gpio_irq_chip_set_chip(girq, &thunderx_gpio_irq_chip);
536 girq->fwnode = of_node_to_fwnode(dev->of_node);
537 girq->parent_domain =
538 irq_get_irq_data(txgpio->msix_entries[0].vector)->domain;
539 girq->child_to_parent_hwirq = thunderx_gpio_child_to_parent_hwirq;
540 girq->populate_parent_alloc_arg = thunderx_gpio_populate_parent_alloc_info;
541 girq->handler = handle_bad_irq;
542 girq->default_type = IRQ_TYPE_NONE;
544 err = devm_gpiochip_add_data(dev, chip, txgpio);
548 /* Push on irq_data and the domain for each line. */
549 for (i = 0; i < ngpio; i++) {
550 struct irq_fwspec fwspec;
552 fwspec.fwnode = of_node_to_fwnode(dev->of_node);
553 fwspec.param_count = 2;
555 fwspec.param[1] = IRQ_TYPE_NONE;
556 err = irq_domain_push_irq(girq->domain,
557 txgpio->msix_entries[i].vector,
560 dev_err(dev, "irq_domain_push_irq: %d\n", err);
563 dev_info(dev, "ThunderX GPIO: %d lines with base %d.\n",
567 pci_set_drvdata(pdev, NULL);
571 static void thunderx_gpio_remove(struct pci_dev *pdev)
574 struct thunderx_gpio *txgpio = pci_get_drvdata(pdev);
576 for (i = 0; i < txgpio->chip.ngpio; i++)
577 irq_domain_pop_irq(txgpio->chip.irq.domain,
578 txgpio->msix_entries[i].vector);
580 irq_domain_remove(txgpio->chip.irq.domain);
582 pci_set_drvdata(pdev, NULL);
585 static const struct pci_device_id thunderx_gpio_id_table[] = {
586 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, 0xA00A) },
587 { 0, } /* end of table */
590 MODULE_DEVICE_TABLE(pci, thunderx_gpio_id_table);
592 static struct pci_driver thunderx_gpio_driver = {
593 .name = KBUILD_MODNAME,
594 .id_table = thunderx_gpio_id_table,
595 .probe = thunderx_gpio_probe,
596 .remove = thunderx_gpio_remove,
599 module_pci_driver(thunderx_gpio_driver);
601 MODULE_DESCRIPTION("Cavium Inc. ThunderX/OCTEON-TX GPIO Driver");
602 MODULE_LICENSE("GPL");