hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/irqchip/qcom-pdc.c
....@@ -1,24 +1,30 @@
11 // SPDX-License-Identifier: GPL-2.0
22 /*
3
- * Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
3
+ * Copyright (c) 2017-2019, The Linux Foundation. All rights reserved.
44 */
55
66 #include <linux/err.h>
77 #include <linux/init.h>
8
+#include <linux/interrupt.h>
89 #include <linux/irq.h>
910 #include <linux/irqchip.h>
1011 #include <linux/irqdomain.h>
1112 #include <linux/io.h>
1213 #include <linux/kernel.h>
14
+#include <linux/module.h>
1315 #include <linux/of.h>
1416 #include <linux/of_address.h>
1517 #include <linux/of_device.h>
18
+#include <linux/of_irq.h>
19
+#include <linux/soc/qcom/irq.h>
1620 #include <linux/spinlock.h>
17
-#include <linux/platform_device.h>
1821 #include <linux/slab.h>
1922 #include <linux/types.h>
2023
21
-#define PDC_MAX_IRQS 126
24
+#include <linux/qcom_scm.h>
25
+
26
+#define PDC_MAX_IRQS 168
27
+#define PDC_MAX_GPIO_IRQS 256
2228
2329 #define CLEAR_INTR(reg, intr) (reg & ~(1 << intr))
2430 #define ENABLE_INTR(reg, intr) (reg | (1 << intr))
....@@ -26,16 +32,28 @@
2632 #define IRQ_ENABLE_BANK 0x10
2733 #define IRQ_i_CFG 0x110
2834
35
+#define PDC_NO_PARENT_IRQ ~0UL
36
+
2937 struct pdc_pin_region {
3038 u32 pin_base;
3139 u32 parent_base;
3240 u32 cnt;
3341 };
3442
43
+struct spi_cfg_regs {
44
+ union {
45
+ u64 start;
46
+ void __iomem *base;
47
+ };
48
+ resource_size_t size;
49
+ bool scm_io;
50
+};
51
+
3552 static DEFINE_RAW_SPINLOCK(pdc_lock);
3653 static void __iomem *pdc_base;
3754 static struct pdc_pin_region *pdc_region;
3855 static int pdc_region_cnt;
56
+static struct spi_cfg_regs *spi_cfg;
3957
4058 static void pdc_reg_write(int reg, u32 i, u32 val)
4159 {
....@@ -50,29 +68,81 @@
5068 static void pdc_enable_intr(struct irq_data *d, bool on)
5169 {
5270 int pin_out = d->hwirq;
71
+ unsigned long flags;
5372 u32 index, mask;
5473 u32 enable;
5574
5675 index = pin_out / 32;
5776 mask = pin_out % 32;
5877
59
- raw_spin_lock(&pdc_lock);
78
+ raw_spin_lock_irqsave(&pdc_lock, flags);
6079 enable = pdc_reg_read(IRQ_ENABLE_BANK, index);
6180 enable = on ? ENABLE_INTR(enable, mask) : CLEAR_INTR(enable, mask);
6281 pdc_reg_write(IRQ_ENABLE_BANK, index, enable);
63
- raw_spin_unlock(&pdc_lock);
82
+ raw_spin_unlock_irqrestore(&pdc_lock, flags);
6483 }
6584
66
-static void qcom_pdc_gic_mask(struct irq_data *d)
85
+static void qcom_pdc_gic_disable(struct irq_data *d)
6786 {
6887 pdc_enable_intr(d, false);
69
- irq_chip_mask_parent(d);
88
+ irq_chip_disable_parent(d);
7089 }
7190
72
-static void qcom_pdc_gic_unmask(struct irq_data *d)
91
+static void qcom_pdc_gic_enable(struct irq_data *d)
7392 {
7493 pdc_enable_intr(d, true);
75
- irq_chip_unmask_parent(d);
94
+ irq_chip_enable_parent(d);
95
+}
96
+
97
+static u32 __spi_pin_read(unsigned int pin)
98
+{
99
+ void __iomem *cfg_reg = spi_cfg->base + pin * 4;
100
+ u64 scm_cfg_reg = spi_cfg->start + pin * 4;
101
+
102
+ if (spi_cfg->scm_io) {
103
+ unsigned int val;
104
+
105
+ qcom_scm_io_readl(scm_cfg_reg, &val);
106
+ return val;
107
+ } else {
108
+ return readl(cfg_reg);
109
+ }
110
+}
111
+
112
+static void __spi_pin_write(unsigned int pin, unsigned int val)
113
+{
114
+ void __iomem *cfg_reg = spi_cfg->base + pin * 4;
115
+ u64 scm_cfg_reg = spi_cfg->start + pin * 4;
116
+
117
+ if (spi_cfg->scm_io)
118
+ qcom_scm_io_writel(scm_cfg_reg, val);
119
+ else
120
+ writel(val, cfg_reg);
121
+}
122
+
123
+static int spi_configure_type(irq_hw_number_t hwirq, unsigned int type)
124
+{
125
+ int spi = hwirq - 32;
126
+ u32 pin = spi / 32;
127
+ u32 mask = BIT(spi % 32);
128
+ u32 val;
129
+ unsigned long flags;
130
+
131
+ if (!spi_cfg)
132
+ return 0;
133
+
134
+ if (pin * 4 > spi_cfg->size)
135
+ return -EFAULT;
136
+
137
+ raw_spin_lock_irqsave(&pdc_lock, flags);
138
+ val = __spi_pin_read(pin);
139
+ val &= ~mask;
140
+ if (type & IRQ_TYPE_LEVEL_MASK)
141
+ val |= mask;
142
+ __spi_pin_write(pin, val);
143
+ raw_spin_unlock_irqrestore(&pdc_lock, flags);
144
+
145
+ return 0;
76146 }
77147
78148 /*
....@@ -111,8 +181,10 @@
111181 */
112182 static int qcom_pdc_gic_set_type(struct irq_data *d, unsigned int type)
113183 {
114
- int pin_out = d->hwirq;
184
+ int parent_hwirq = d->parent_data->hwirq;
115185 enum pdc_irq_config_bits pdc_type;
186
+ enum pdc_irq_config_bits old_pdc_type;
187
+ int ret;
116188
117189 switch (type) {
118190 case IRQ_TYPE_EDGE_RISING:
....@@ -138,21 +210,48 @@
138210 return -EINVAL;
139211 }
140212
141
- pdc_reg_write(IRQ_i_CFG, pin_out, pdc_type);
213
+ old_pdc_type = pdc_reg_read(IRQ_i_CFG, d->hwirq);
214
+ pdc_reg_write(IRQ_i_CFG, d->hwirq, pdc_type);
142215
143
- return irq_chip_set_type_parent(d, type);
216
+ /* Additionally, configure (only) the GPIO in the f/w */
217
+ ret = spi_configure_type(parent_hwirq, type);
218
+ if (ret)
219
+ return ret;
220
+
221
+ ret = irq_chip_set_type_parent(d, type);
222
+ if (ret)
223
+ return ret;
224
+
225
+ /*
226
+ * When we change types the PDC can give a phantom interrupt.
227
+ * Clear it. Specifically the phantom shows up when reconfiguring
228
+ * polarity of interrupt without changing the state of the signal
229
+ * but let's be consistent and clear it always.
230
+ *
231
+ * Doing this works because we have IRQCHIP_SET_TYPE_MASKED so the
232
+ * interrupt will be cleared before the rest of the system sees it.
233
+ */
234
+ if (old_pdc_type != pdc_type)
235
+ irq_chip_set_parent_state(d, IRQCHIP_STATE_PENDING, false);
236
+
237
+ return 0;
144238 }
145239
146240 static struct irq_chip qcom_pdc_gic_chip = {
147241 .name = "PDC",
148242 .irq_eoi = irq_chip_eoi_parent,
149
- .irq_mask = qcom_pdc_gic_mask,
150
- .irq_unmask = qcom_pdc_gic_unmask,
243
+ .irq_mask = irq_chip_mask_parent,
244
+ .irq_unmask = irq_chip_unmask_parent,
245
+ .irq_disable = qcom_pdc_gic_disable,
246
+ .irq_enable = qcom_pdc_gic_enable,
247
+ .irq_get_irqchip_state = irq_chip_get_parent_state,
248
+ .irq_set_irqchip_state = irq_chip_set_parent_state,
151249 .irq_retrigger = irq_chip_retrigger_hierarchy,
152250 .irq_set_type = qcom_pdc_gic_set_type,
153251 .flags = IRQCHIP_MASK_ON_SUSPEND |
154252 IRQCHIP_SET_TYPE_MASKED |
155
- IRQCHIP_SKIP_SET_WAKE,
253
+ IRQCHIP_SKIP_SET_WAKE |
254
+ IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND,
156255 .irq_set_vcpu_affinity = irq_chip_set_vcpu_affinity_parent,
157256 .irq_set_affinity = irq_chip_set_affinity_parent,
158257 };
....@@ -169,8 +268,7 @@
169268 return (region->parent_base + pin - region->pin_base);
170269 }
171270
172
- WARN_ON(1);
173
- return ~0UL;
271
+ return PDC_NO_PARENT_IRQ;
174272 }
175273
176274 static int qcom_pdc_translate(struct irq_domain *d, struct irq_fwspec *fwspec,
....@@ -199,16 +297,16 @@
199297
200298 ret = qcom_pdc_translate(domain, fwspec, &hwirq, &type);
201299 if (ret)
202
- return -EINVAL;
203
-
204
- parent_hwirq = get_parent_hwirq(hwirq);
205
- if (parent_hwirq == ~0UL)
206
- return -EINVAL;
300
+ return ret;
207301
208302 ret = irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
209303 &qcom_pdc_gic_chip, NULL);
210304 if (ret)
211305 return ret;
306
+
307
+ parent_hwirq = get_parent_hwirq(hwirq);
308
+ if (parent_hwirq == PDC_NO_PARENT_IRQ)
309
+ return irq_domain_disconnect_hierarchy(domain->parent, virq);
212310
213311 if (type & IRQ_TYPE_EDGE_BOTH)
214312 type = IRQ_TYPE_EDGE_RISING;
....@@ -232,9 +330,64 @@
232330 .free = irq_domain_free_irqs_common,
233331 };
234332
333
+static int qcom_pdc_gpio_alloc(struct irq_domain *domain, unsigned int virq,
334
+ unsigned int nr_irqs, void *data)
335
+{
336
+ struct irq_fwspec *fwspec = data;
337
+ struct irq_fwspec parent_fwspec;
338
+ irq_hw_number_t hwirq, parent_hwirq;
339
+ unsigned int type;
340
+ int ret;
341
+
342
+ ret = qcom_pdc_translate(domain, fwspec, &hwirq, &type);
343
+ if (ret)
344
+ return ret;
345
+
346
+ if (hwirq == GPIO_NO_WAKE_IRQ)
347
+ return irq_domain_disconnect_hierarchy(domain, virq);
348
+
349
+ ret = irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
350
+ &qcom_pdc_gic_chip, NULL);
351
+ if (ret)
352
+ return ret;
353
+
354
+ parent_hwirq = get_parent_hwirq(hwirq);
355
+ if (parent_hwirq == PDC_NO_PARENT_IRQ)
356
+ return irq_domain_disconnect_hierarchy(domain->parent, virq);
357
+
358
+ if (type & IRQ_TYPE_EDGE_BOTH)
359
+ type = IRQ_TYPE_EDGE_RISING;
360
+
361
+ if (type & IRQ_TYPE_LEVEL_MASK)
362
+ type = IRQ_TYPE_LEVEL_HIGH;
363
+
364
+ parent_fwspec.fwnode = domain->parent->fwnode;
365
+ parent_fwspec.param_count = 3;
366
+ parent_fwspec.param[0] = 0;
367
+ parent_fwspec.param[1] = parent_hwirq;
368
+ parent_fwspec.param[2] = type;
369
+
370
+ return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs,
371
+ &parent_fwspec);
372
+}
373
+
374
+static int qcom_pdc_gpio_domain_select(struct irq_domain *d,
375
+ struct irq_fwspec *fwspec,
376
+ enum irq_domain_bus_token bus_token)
377
+{
378
+ return bus_token == DOMAIN_BUS_WAKEUP;
379
+}
380
+
381
+static const struct irq_domain_ops qcom_pdc_gpio_ops = {
382
+ .select = qcom_pdc_gpio_domain_select,
383
+ .alloc = qcom_pdc_gpio_alloc,
384
+ .free = irq_domain_free_irqs_common,
385
+};
386
+
235387 static int pdc_setup_pin_mapping(struct device_node *np)
236388 {
237
- int ret, n;
389
+ int ret, n, i;
390
+ u32 irq_index, reg_index, val;
238391
239392 n = of_property_count_elems_of_size(np, "qcom,pdc-ranges", sizeof(u32));
240393 if (n <= 0 || n % 3)
....@@ -263,6 +416,14 @@
263416 &pdc_region[n].cnt);
264417 if (ret)
265418 return ret;
419
+
420
+ for (i = 0; i < pdc_region[n].cnt; i++) {
421
+ reg_index = (i + pdc_region[n].pin_base) >> 5;
422
+ irq_index = (i + pdc_region[n].pin_base) & 0x1f;
423
+ val = pdc_reg_read(IRQ_ENABLE_BANK, reg_index);
424
+ val &= ~BIT(irq_index);
425
+ pdc_reg_write(IRQ_ENABLE_BANK, reg_index, val);
426
+ }
266427 }
267428
268429 return 0;
....@@ -270,7 +431,8 @@
270431
271432 static int qcom_pdc_init(struct device_node *node, struct device_node *parent)
272433 {
273
- struct irq_domain *parent_domain, *pdc_domain;
434
+ struct irq_domain *parent_domain, *pdc_domain, *pdc_gpio_domain;
435
+ struct resource res;
274436 int ret;
275437
276438 pdc_base = of_iomap(node, 0);
....@@ -301,12 +463,73 @@
301463 goto fail;
302464 }
303465
466
+ ret = of_address_to_resource(node, 1, &res);
467
+ if (!ret) {
468
+ spi_cfg = kcalloc(1, sizeof(*spi_cfg), GFP_KERNEL);
469
+ if (!spi_cfg) {
470
+ ret = -ENOMEM;
471
+ goto remove;
472
+ }
473
+ spi_cfg->scm_io = of_find_property(node,
474
+ "qcom,scm-spi-cfg", NULL);
475
+ spi_cfg->size = resource_size(&res);
476
+ if (spi_cfg->scm_io) {
477
+ spi_cfg->start = res.start;
478
+ } else {
479
+ spi_cfg->base = ioremap(res.start, spi_cfg->size);
480
+ if (!spi_cfg->base) {
481
+ ret = -ENOMEM;
482
+ goto remove;
483
+ }
484
+ }
485
+ }
486
+
487
+ pdc_gpio_domain = irq_domain_create_hierarchy(parent_domain,
488
+ IRQ_DOMAIN_FLAG_QCOM_PDC_WAKEUP,
489
+ PDC_MAX_GPIO_IRQS,
490
+ of_fwnode_handle(node),
491
+ &qcom_pdc_gpio_ops, NULL);
492
+ if (!pdc_gpio_domain) {
493
+ pr_err("%pOF: PDC domain add failed for GPIO domain\n", node);
494
+ ret = -ENOMEM;
495
+ goto remove;
496
+ }
497
+
498
+ irq_domain_update_bus_token(pdc_gpio_domain, DOMAIN_BUS_WAKEUP);
499
+
304500 return 0;
305501
502
+remove:
503
+ irq_domain_remove(pdc_domain);
504
+ kfree(spi_cfg);
306505 fail:
307506 kfree(pdc_region);
308507 iounmap(pdc_base);
309508 return ret;
310509 }
311510
312
-IRQCHIP_DECLARE(pdc_sdm845, "qcom,sdm845-pdc", qcom_pdc_init);
511
+static int qcom_pdc_probe(struct platform_device *pdev)
512
+{
513
+ struct device_node *np = pdev->dev.of_node;
514
+ struct device_node *parent = of_irq_find_parent(np);
515
+
516
+ return qcom_pdc_init(np, parent);
517
+}
518
+
519
+static const struct of_device_id qcom_pdc_match_table[] = {
520
+ { .compatible = "qcom,pdc" },
521
+ {}
522
+};
523
+MODULE_DEVICE_TABLE(of, qcom_pdc_match_table);
524
+
525
+static struct platform_driver qcom_pdc_driver = {
526
+ .probe = qcom_pdc_probe,
527
+ .driver = {
528
+ .name = "qcom-pdc",
529
+ .of_match_table = qcom_pdc_match_table,
530
+ .suppress_bind_attrs = true,
531
+ },
532
+};
533
+module_platform_driver(qcom_pdc_driver);
534
+MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Power Domain Controller");
535
+MODULE_LICENSE("GPL v2");