Blame view

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