Merge tag 'pm-fixes-4.15-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[linux/fpc-iii.git] / drivers / irqchip / irq-imx-gpcv2.c
blob675eda5ff2b85f6037eaea58d49a3d48a9e2c33d
1 /*
2 * Copyright (C) 2015 Freescale Semiconductor, Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
9 #include <linux/of_address.h>
10 #include <linux/of_irq.h>
11 #include <linux/slab.h>
12 #include <linux/irqchip.h>
13 #include <linux/syscore_ops.h>
15 #define IMR_NUM 4
16 #define GPC_MAX_IRQS (IMR_NUM * 32)
18 #define GPC_IMR1_CORE0 0x30
19 #define GPC_IMR1_CORE1 0x40
21 struct gpcv2_irqchip_data {
22 struct raw_spinlock rlock;
23 void __iomem *gpc_base;
24 u32 wakeup_sources[IMR_NUM];
25 u32 saved_irq_mask[IMR_NUM];
26 u32 cpu2wakeup;
29 static struct gpcv2_irqchip_data *imx_gpcv2_instance;
32 * Interface for the low level wakeup code.
34 u32 imx_gpcv2_get_wakeup_source(u32 **sources)
36 if (!imx_gpcv2_instance)
37 return 0;
39 if (sources)
40 *sources = imx_gpcv2_instance->wakeup_sources;
42 return IMR_NUM;
45 static int gpcv2_wakeup_source_save(void)
47 struct gpcv2_irqchip_data *cd;
48 void __iomem *reg;
49 int i;
51 cd = imx_gpcv2_instance;
52 if (!cd)
53 return 0;
55 for (i = 0; i < IMR_NUM; i++) {
56 reg = cd->gpc_base + cd->cpu2wakeup + i * 4;
57 cd->saved_irq_mask[i] = readl_relaxed(reg);
58 writel_relaxed(cd->wakeup_sources[i], reg);
61 return 0;
64 static void gpcv2_wakeup_source_restore(void)
66 struct gpcv2_irqchip_data *cd;
67 void __iomem *reg;
68 int i;
70 cd = imx_gpcv2_instance;
71 if (!cd)
72 return;
74 for (i = 0; i < IMR_NUM; i++) {
75 reg = cd->gpc_base + cd->cpu2wakeup + i * 4;
76 writel_relaxed(cd->saved_irq_mask[i], reg);
80 static struct syscore_ops imx_gpcv2_syscore_ops = {
81 .suspend = gpcv2_wakeup_source_save,
82 .resume = gpcv2_wakeup_source_restore,
85 static int imx_gpcv2_irq_set_wake(struct irq_data *d, unsigned int on)
87 struct gpcv2_irqchip_data *cd = d->chip_data;
88 unsigned int idx = d->hwirq / 32;
89 unsigned long flags;
90 void __iomem *reg;
91 u32 mask, val;
93 raw_spin_lock_irqsave(&cd->rlock, flags);
94 reg = cd->gpc_base + cd->cpu2wakeup + idx * 4;
95 mask = 1 << d->hwirq % 32;
96 val = cd->wakeup_sources[idx];
98 cd->wakeup_sources[idx] = on ? (val & ~mask) : (val | mask);
99 raw_spin_unlock_irqrestore(&cd->rlock, flags);
102 * Do *not* call into the parent, as the GIC doesn't have any
103 * wake-up facility...
106 return 0;
109 static void imx_gpcv2_irq_unmask(struct irq_data *d)
111 struct gpcv2_irqchip_data *cd = d->chip_data;
112 void __iomem *reg;
113 u32 val;
115 raw_spin_lock(&cd->rlock);
116 reg = cd->gpc_base + cd->cpu2wakeup + d->hwirq / 32 * 4;
117 val = readl_relaxed(reg);
118 val &= ~(1 << d->hwirq % 32);
119 writel_relaxed(val, reg);
120 raw_spin_unlock(&cd->rlock);
122 irq_chip_unmask_parent(d);
125 static void imx_gpcv2_irq_mask(struct irq_data *d)
127 struct gpcv2_irqchip_data *cd = d->chip_data;
128 void __iomem *reg;
129 u32 val;
131 raw_spin_lock(&cd->rlock);
132 reg = cd->gpc_base + cd->cpu2wakeup + d->hwirq / 32 * 4;
133 val = readl_relaxed(reg);
134 val |= 1 << (d->hwirq % 32);
135 writel_relaxed(val, reg);
136 raw_spin_unlock(&cd->rlock);
138 irq_chip_mask_parent(d);
141 static struct irq_chip gpcv2_irqchip_data_chip = {
142 .name = "GPCv2",
143 .irq_eoi = irq_chip_eoi_parent,
144 .irq_mask = imx_gpcv2_irq_mask,
145 .irq_unmask = imx_gpcv2_irq_unmask,
146 .irq_set_wake = imx_gpcv2_irq_set_wake,
147 .irq_retrigger = irq_chip_retrigger_hierarchy,
148 #ifdef CONFIG_SMP
149 .irq_set_affinity = irq_chip_set_affinity_parent,
150 #endif
153 static int imx_gpcv2_domain_translate(struct irq_domain *d,
154 struct irq_fwspec *fwspec,
155 unsigned long *hwirq,
156 unsigned int *type)
158 if (is_of_node(fwspec->fwnode)) {
159 if (fwspec->param_count != 3)
160 return -EINVAL;
162 /* No PPI should point to this domain */
163 if (fwspec->param[0] != 0)
164 return -EINVAL;
166 *hwirq = fwspec->param[1];
167 *type = fwspec->param[2];
168 return 0;
171 return -EINVAL;
174 static int imx_gpcv2_domain_alloc(struct irq_domain *domain,
175 unsigned int irq, unsigned int nr_irqs,
176 void *data)
178 struct irq_fwspec *fwspec = data;
179 struct irq_fwspec parent_fwspec;
180 irq_hw_number_t hwirq;
181 unsigned int type;
182 int err;
183 int i;
185 err = imx_gpcv2_domain_translate(domain, fwspec, &hwirq, &type);
186 if (err)
187 return err;
189 if (hwirq >= GPC_MAX_IRQS)
190 return -EINVAL;
192 for (i = 0; i < nr_irqs; i++) {
193 irq_domain_set_hwirq_and_chip(domain, irq + i, hwirq + i,
194 &gpcv2_irqchip_data_chip, domain->host_data);
197 parent_fwspec = *fwspec;
198 parent_fwspec.fwnode = domain->parent->fwnode;
199 return irq_domain_alloc_irqs_parent(domain, irq, nr_irqs,
200 &parent_fwspec);
203 static const struct irq_domain_ops gpcv2_irqchip_data_domain_ops = {
204 .translate = imx_gpcv2_domain_translate,
205 .alloc = imx_gpcv2_domain_alloc,
206 .free = irq_domain_free_irqs_common,
209 static int __init imx_gpcv2_irqchip_init(struct device_node *node,
210 struct device_node *parent)
212 struct irq_domain *parent_domain, *domain;
213 struct gpcv2_irqchip_data *cd;
214 int i;
216 if (!parent) {
217 pr_err("%pOF: no parent, giving up\n", node);
218 return -ENODEV;
221 parent_domain = irq_find_host(parent);
222 if (!parent_domain) {
223 pr_err("%pOF: unable to get parent domain\n", node);
224 return -ENXIO;
227 cd = kzalloc(sizeof(struct gpcv2_irqchip_data), GFP_KERNEL);
228 if (!cd) {
229 pr_err("kzalloc failed!\n");
230 return -ENOMEM;
233 raw_spin_lock_init(&cd->rlock);
235 cd->gpc_base = of_iomap(node, 0);
236 if (!cd->gpc_base) {
237 pr_err("fsl-gpcv2: unable to map gpc registers\n");
238 kfree(cd);
239 return -ENOMEM;
242 domain = irq_domain_add_hierarchy(parent_domain, 0, GPC_MAX_IRQS,
243 node, &gpcv2_irqchip_data_domain_ops, cd);
244 if (!domain) {
245 iounmap(cd->gpc_base);
246 kfree(cd);
247 return -ENOMEM;
249 irq_set_default_host(domain);
251 /* Initially mask all interrupts */
252 for (i = 0; i < IMR_NUM; i++) {
253 writel_relaxed(~0, cd->gpc_base + GPC_IMR1_CORE0 + i * 4);
254 writel_relaxed(~0, cd->gpc_base + GPC_IMR1_CORE1 + i * 4);
255 cd->wakeup_sources[i] = ~0;
258 /* Let CORE0 as the default CPU to wake up by GPC */
259 cd->cpu2wakeup = GPC_IMR1_CORE0;
262 * Due to hardware design failure, need to make sure GPR
263 * interrupt(#32) is unmasked during RUN mode to avoid entering
264 * DSM by mistake.
266 writel_relaxed(~0x1, cd->gpc_base + cd->cpu2wakeup);
268 imx_gpcv2_instance = cd;
269 register_syscore_ops(&imx_gpcv2_syscore_ops);
272 * Clear the OF_POPULATED flag set in of_irq_init so that
273 * later the GPC power domain driver will not be skipped.
275 of_node_clear_flag(node, OF_POPULATED);
276 return 0;
279 IRQCHIP_DECLARE(imx_gpcv2, "fsl,imx7d-gpc", imx_gpcv2_irqchip_init);