• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Texas Instruments Keystone IRQ controller IP driver
3  *
4  * Copyright (C) 2014 Texas Instruments, Inc.
5  * Author: Sajesh Kumar Saran <sajesh@ti.com>
6  *	   Grygorii Strashko <grygorii.strashko@ti.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation version 2.
11  *
12  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
13  * kind, whether express or implied; without even the implied warranty
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17 
18 #include <linux/irq.h>
19 #include <linux/bitops.h>
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/interrupt.h>
23 #include <linux/irqdomain.h>
24 #include <linux/irqchip.h>
25 #include <linux/of.h>
26 #include <linux/of_platform.h>
27 #include <linux/mfd/syscon.h>
28 #include <linux/regmap.h>
29 
30 /* The source ID bits start from 4 to 31 (total 28 bits)*/
31 #define BIT_OFS			4
32 #define KEYSTONE_N_IRQ		(32 - BIT_OFS)
33 
34 struct keystone_irq_device {
35 	struct device		*dev;
36 	struct irq_chip		 chip;
37 	u32			 mask;
38 	int			 irq;
39 	struct irq_domain	*irqd;
40 	struct regmap		*devctrl_regs;
41 	u32			devctrl_offset;
42 	raw_spinlock_t		wa_lock;
43 };
44 
keystone_irq_readl(struct keystone_irq_device * kirq)45 static inline u32 keystone_irq_readl(struct keystone_irq_device *kirq)
46 {
47 	int ret;
48 	u32 val = 0;
49 
50 	ret = regmap_read(kirq->devctrl_regs, kirq->devctrl_offset, &val);
51 	if (ret < 0)
52 		dev_dbg(kirq->dev, "irq read failed ret(%d)\n", ret);
53 	return val;
54 }
55 
56 static inline void
keystone_irq_writel(struct keystone_irq_device * kirq,u32 value)57 keystone_irq_writel(struct keystone_irq_device *kirq, u32 value)
58 {
59 	int ret;
60 
61 	ret = regmap_write(kirq->devctrl_regs, kirq->devctrl_offset, value);
62 	if (ret < 0)
63 		dev_dbg(kirq->dev, "irq write failed ret(%d)\n", ret);
64 }
65 
keystone_irq_setmask(struct irq_data * d)66 static void keystone_irq_setmask(struct irq_data *d)
67 {
68 	struct keystone_irq_device *kirq = irq_data_get_irq_chip_data(d);
69 
70 	kirq->mask |= BIT(d->hwirq);
71 	dev_dbg(kirq->dev, "mask %lu [%x]\n", d->hwirq, kirq->mask);
72 }
73 
keystone_irq_unmask(struct irq_data * d)74 static void keystone_irq_unmask(struct irq_data *d)
75 {
76 	struct keystone_irq_device *kirq = irq_data_get_irq_chip_data(d);
77 
78 	kirq->mask &= ~BIT(d->hwirq);
79 	dev_dbg(kirq->dev, "unmask %lu [%x]\n", d->hwirq, kirq->mask);
80 }
81 
keystone_irq_ack(struct irq_data * d)82 static void keystone_irq_ack(struct irq_data *d)
83 {
84 	/* nothing to do here */
85 }
86 
keystone_irq_handler(int irq,void * keystone_irq)87 static irqreturn_t keystone_irq_handler(int irq, void *keystone_irq)
88 {
89 	struct keystone_irq_device *kirq = keystone_irq;
90 	unsigned long wa_lock_flags;
91 	unsigned long pending;
92 	int src, virq;
93 
94 	dev_dbg(kirq->dev, "start irq %d\n", irq);
95 
96 	pending = keystone_irq_readl(kirq);
97 	keystone_irq_writel(kirq, pending);
98 
99 	dev_dbg(kirq->dev, "pending 0x%lx, mask 0x%x\n", pending, kirq->mask);
100 
101 	pending = (pending >> BIT_OFS) & ~kirq->mask;
102 
103 	dev_dbg(kirq->dev, "pending after mask 0x%lx\n", pending);
104 
105 	for (src = 0; src < KEYSTONE_N_IRQ; src++) {
106 		if (BIT(src) & pending) {
107 			virq = irq_find_mapping(kirq->irqd, src);
108 			dev_dbg(kirq->dev, "dispatch bit %d, virq %d\n",
109 				src, virq);
110 			if (!virq)
111 				dev_warn(kirq->dev, "spurious irq detected hwirq %d, virq %d\n",
112 					 src, virq);
113 			raw_spin_lock_irqsave(&kirq->wa_lock, wa_lock_flags);
114 			generic_handle_irq(virq);
115 			raw_spin_unlock_irqrestore(&kirq->wa_lock,
116 						   wa_lock_flags);
117 		}
118 	}
119 
120 	dev_dbg(kirq->dev, "end irq %d\n", irq);
121 	return IRQ_HANDLED;
122 }
123 
keystone_irq_map(struct irq_domain * h,unsigned int virq,irq_hw_number_t hw)124 static int keystone_irq_map(struct irq_domain *h, unsigned int virq,
125 				irq_hw_number_t hw)
126 {
127 	struct keystone_irq_device *kirq = h->host_data;
128 
129 	irq_set_chip_data(virq, kirq);
130 	irq_set_chip_and_handler(virq, &kirq->chip, handle_level_irq);
131 	irq_set_probe(virq);
132 	return 0;
133 }
134 
135 static const struct irq_domain_ops keystone_irq_ops = {
136 	.map	= keystone_irq_map,
137 	.xlate	= irq_domain_xlate_onecell,
138 };
139 
keystone_irq_probe(struct platform_device * pdev)140 static int keystone_irq_probe(struct platform_device *pdev)
141 {
142 	struct device *dev = &pdev->dev;
143 	struct device_node *np = dev->of_node;
144 	struct keystone_irq_device *kirq;
145 	int ret;
146 
147 	if (np == NULL)
148 		return -EINVAL;
149 
150 	kirq = devm_kzalloc(dev, sizeof(*kirq), GFP_KERNEL);
151 	if (!kirq)
152 		return -ENOMEM;
153 
154 	kirq->devctrl_regs =
155 		syscon_regmap_lookup_by_phandle(np, "ti,syscon-dev");
156 	if (IS_ERR(kirq->devctrl_regs))
157 		return PTR_ERR(kirq->devctrl_regs);
158 
159 	ret = of_property_read_u32_index(np, "ti,syscon-dev", 1,
160 					 &kirq->devctrl_offset);
161 	if (ret) {
162 		dev_err(dev, "couldn't read the devctrl_offset offset!\n");
163 		return ret;
164 	}
165 
166 	kirq->irq = platform_get_irq(pdev, 0);
167 	if (kirq->irq < 0) {
168 		dev_err(dev, "no irq resource %d\n", kirq->irq);
169 		return kirq->irq;
170 	}
171 
172 	kirq->dev = dev;
173 	kirq->mask = ~0x0;
174 	kirq->chip.name		= "keystone-irq";
175 	kirq->chip.irq_ack	= keystone_irq_ack;
176 	kirq->chip.irq_mask	= keystone_irq_setmask;
177 	kirq->chip.irq_unmask	= keystone_irq_unmask;
178 
179 	kirq->irqd = irq_domain_add_linear(np, KEYSTONE_N_IRQ,
180 					   &keystone_irq_ops, kirq);
181 	if (!kirq->irqd) {
182 		dev_err(dev, "IRQ domain registration failed\n");
183 		return -ENODEV;
184 	}
185 
186 	raw_spin_lock_init(&kirq->wa_lock);
187 
188 	platform_set_drvdata(pdev, kirq);
189 
190 	ret = request_irq(kirq->irq, keystone_irq_handler,
191 			  0, dev_name(dev), kirq);
192 	if (ret) {
193 		irq_domain_remove(kirq->irqd);
194 		return ret;
195 	}
196 
197 	/* clear all source bits */
198 	keystone_irq_writel(kirq, ~0x0);
199 
200 	dev_info(dev, "irqchip registered, nr_irqs %u\n", KEYSTONE_N_IRQ);
201 
202 	return 0;
203 }
204 
keystone_irq_remove(struct platform_device * pdev)205 static int keystone_irq_remove(struct platform_device *pdev)
206 {
207 	struct keystone_irq_device *kirq = platform_get_drvdata(pdev);
208 	int hwirq;
209 
210 	free_irq(kirq->irq, kirq);
211 
212 	for (hwirq = 0; hwirq < KEYSTONE_N_IRQ; hwirq++)
213 		irq_dispose_mapping(irq_find_mapping(kirq->irqd, hwirq));
214 
215 	irq_domain_remove(kirq->irqd);
216 	return 0;
217 }
218 
219 static const struct of_device_id keystone_irq_dt_ids[] = {
220 	{ .compatible = "ti,keystone-irq", },
221 	{},
222 };
223 MODULE_DEVICE_TABLE(of, keystone_irq_dt_ids);
224 
225 static struct platform_driver keystone_irq_device_driver = {
226 	.probe		= keystone_irq_probe,
227 	.remove		= keystone_irq_remove,
228 	.driver		= {
229 		.name	= "keystone_irq",
230 		.of_match_table	= of_match_ptr(keystone_irq_dt_ids),
231 	}
232 };
233 
234 module_platform_driver(keystone_irq_device_driver);
235 
236 MODULE_AUTHOR("Texas Instruments");
237 MODULE_AUTHOR("Sajesh Kumar Saran");
238 MODULE_AUTHOR("Grygorii Strashko");
239 MODULE_DESCRIPTION("Keystone IRQ chip");
240 MODULE_LICENSE("GPL v2");
241