ARM: samsung: remove unused tick.h
[deliverable/linux.git] / arch / arm / common / vic.c
CommitLineData
fa0fe48f
RK
1/*
2 * linux/arch/arm/common/vic.c
3 *
4 * Copyright (C) 1999 - 2003 ARM Limited
5 * Copyright (C) 2000 Deep Blue Solutions Ltd
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
bb06b737 21
f9b28ccb 22#include <linux/export.h>
fa0fe48f
RK
23#include <linux/init.h>
24#include <linux/list.h>
fced80c7 25#include <linux/io.h>
f9b28ccb
JI
26#include <linux/irqdomain.h>
27#include <linux/of.h>
28#include <linux/of_address.h>
29#include <linux/of_irq.h>
328f5cc3 30#include <linux/syscore_ops.h>
59fcf48f 31#include <linux/device.h>
f17a1f06 32#include <linux/amba/bus.h>
fa0fe48f 33
1558368e 34#include <asm/exception.h>
fa0fe48f
RK
35#include <asm/mach/irq.h>
36#include <asm/hardware/vic.h>
37
cf21af54
RH
38#define VIC_IRQ_STATUS 0x00
39#define VIC_FIQ_STATUS 0x04
40#define VIC_INT_SELECT 0x0c /* 1 = FIQ, 0 = IRQ */
41#define VIC_INT_SOFT 0x18
42#define VIC_INT_SOFT_CLEAR 0x1c
43#define VIC_PROTECT 0x20
44#define VIC_PL190_VECT_ADDR 0x30 /* PL190 only */
45#define VIC_PL190_DEF_VECT_ADDR 0x34 /* PL190 only */
46
47#define VIC_VECT_ADDR0 0x100 /* 0 to 15 (0..31 PL192) */
48#define VIC_VECT_CNTL0 0x200 /* 0 to 15 (0..31 PL192) */
49#define VIC_ITCR 0x300 /* VIC test control register */
50
51#define VIC_VECT_CNTL_ENABLE (1 << 5)
52
53#define VIC_PL192_VECT_ADDR 0xF00
54
c07f87f2
BD
55/**
56 * struct vic_device - VIC PM device
c07f87f2
BD
57 * @irq: The IRQ number for the base of the VIC.
58 * @base: The register base for the VIC.
ce94df9c 59 * @valid_sources: A bitmask of valid interrupts
c07f87f2
BD
60 * @resume_sources: A bitmask of interrupts for resume.
61 * @resume_irqs: The IRQs enabled for resume.
62 * @int_select: Save for VIC_INT_SELECT.
63 * @int_enable: Save for VIC_INT_ENABLE.
64 * @soft_int: Save for VIC_INT_SOFT.
65 * @protect: Save for VIC_PROTECT.
f9b28ccb 66 * @domain: The IRQ domain for the VIC.
c07f87f2
BD
67 */
68struct vic_device {
c07f87f2
BD
69 void __iomem *base;
70 int irq;
ce94df9c 71 u32 valid_sources;
c07f87f2
BD
72 u32 resume_sources;
73 u32 resume_irqs;
74 u32 int_select;
75 u32 int_enable;
76 u32 soft_int;
77 u32 protect;
75294957 78 struct irq_domain *domain;
c07f87f2
BD
79};
80
81/* we cannot allocate memory when VICs are initially registered */
82static struct vic_device vic_devices[CONFIG_ARM_VIC_NR];
83
bb06b737 84static int vic_id;
c07f87f2 85
a0368029
RH
86static void vic_handle_irq(struct pt_regs *regs);
87
bb06b737
HS
88/**
89 * vic_init2 - common initialisation code
90 * @base: Base of the VIC.
91 *
b595076a 92 * Common initialisation code for registration
bb06b737
HS
93 * and resume.
94*/
95static void vic_init2(void __iomem *base)
96{
97 int i;
98
99 for (i = 0; i < 16; i++) {
100 void __iomem *reg = base + VIC_VECT_CNTL0 + (i * 4);
101 writel(VIC_VECT_CNTL_ENABLE | i, reg);
102 }
103
104 writel(32, base + VIC_PL190_DEF_VECT_ADDR);
105}
c07f87f2 106
328f5cc3
RW
107#ifdef CONFIG_PM
108static void resume_one_vic(struct vic_device *vic)
c07f87f2 109{
c07f87f2
BD
110 void __iomem *base = vic->base;
111
112 printk(KERN_DEBUG "%s: resuming vic at %p\n", __func__, base);
113
114 /* re-initialise static settings */
115 vic_init2(base);
116
117 writel(vic->int_select, base + VIC_INT_SELECT);
118 writel(vic->protect, base + VIC_PROTECT);
119
120 /* set the enabled ints and then clear the non-enabled */
121 writel(vic->int_enable, base + VIC_INT_ENABLE);
122 writel(~vic->int_enable, base + VIC_INT_ENABLE_CLEAR);
123
124 /* and the same for the soft-int register */
125
126 writel(vic->soft_int, base + VIC_INT_SOFT);
127 writel(~vic->soft_int, base + VIC_INT_SOFT_CLEAR);
328f5cc3 128}
c07f87f2 129
328f5cc3
RW
130static void vic_resume(void)
131{
132 int id;
133
134 for (id = vic_id - 1; id >= 0; id--)
135 resume_one_vic(vic_devices + id);
c07f87f2
BD
136}
137
328f5cc3 138static void suspend_one_vic(struct vic_device *vic)
c07f87f2 139{
c07f87f2
BD
140 void __iomem *base = vic->base;
141
142 printk(KERN_DEBUG "%s: suspending vic at %p\n", __func__, base);
143
144 vic->int_select = readl(base + VIC_INT_SELECT);
145 vic->int_enable = readl(base + VIC_INT_ENABLE);
146 vic->soft_int = readl(base + VIC_INT_SOFT);
147 vic->protect = readl(base + VIC_PROTECT);
148
149 /* set the interrupts (if any) that are used for
150 * resuming the system */
151
152 writel(vic->resume_irqs, base + VIC_INT_ENABLE);
153 writel(~vic->resume_irqs, base + VIC_INT_ENABLE_CLEAR);
328f5cc3
RW
154}
155
156static int vic_suspend(void)
157{
158 int id;
159
160 for (id = 0; id < vic_id; id++)
161 suspend_one_vic(vic_devices + id);
c07f87f2
BD
162
163 return 0;
164}
165
328f5cc3
RW
166struct syscore_ops vic_syscore_ops = {
167 .suspend = vic_suspend,
168 .resume = vic_resume,
c07f87f2
BD
169};
170
c07f87f2
BD
171/**
172 * vic_pm_init - initicall to register VIC pm
173 *
174 * This is called via late_initcall() to register
175 * the resources for the VICs due to the early
176 * nature of the VIC's registration.
177*/
178static int __init vic_pm_init(void)
179{
328f5cc3
RW
180 if (vic_id > 0)
181 register_syscore_ops(&vic_syscore_ops);
c07f87f2
BD
182
183 return 0;
184}
c07f87f2 185late_initcall(vic_pm_init);
f9b28ccb 186#endif /* CONFIG_PM */
c07f87f2 187
ce94df9c
LW
188static struct irq_chip vic_chip;
189
190static int vic_irqdomain_map(struct irq_domain *d, unsigned int irq,
191 irq_hw_number_t hwirq)
192{
193 struct vic_device *v = d->host_data;
194
195 /* Skip invalid IRQs, only register handlers for the real ones */
196 if (!(v->valid_sources & (1 << hwirq)))
197 return -ENOTSUPP;
198 irq_set_chip_and_handler(irq, &vic_chip, handle_level_irq);
199 irq_set_chip_data(irq, v->base);
200 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
201 return 0;
202}
203
a0368029
RH
204/*
205 * Handle each interrupt in a single VIC. Returns non-zero if we've
206 * handled at least one interrupt. This reads the status register
207 * before handling each interrupt, which is necessary given that
208 * handle_IRQ may briefly re-enable interrupts for soft IRQ handling.
209 */
210static int handle_one_vic(struct vic_device *vic, struct pt_regs *regs)
211{
212 u32 stat, irq;
213 int handled = 0;
214
215 while ((stat = readl_relaxed(vic->base + VIC_IRQ_STATUS))) {
216 irq = ffs(stat) - 1;
217 handle_IRQ(irq_find_mapping(vic->domain, irq), regs);
218 handled = 1;
219 }
220
221 return handled;
222}
223
224/*
225 * Keep iterating over all registered VIC's until there are no pending
226 * interrupts.
227 */
228static asmlinkage void __exception_irq_entry vic_handle_irq(struct pt_regs *regs)
229{
230 int i, handled;
231
232 do {
233 for (i = 0, handled = 0; i < vic_id; ++i)
234 handled |= handle_one_vic(&vic_devices[i], regs);
235 } while (handled);
236}
237
ce94df9c
LW
238static struct irq_domain_ops vic_irqdomain_ops = {
239 .map = vic_irqdomain_map,
240 .xlate = irq_domain_xlate_onetwocell,
241};
242
bb06b737 243/**
f9b28ccb 244 * vic_register() - Register a VIC.
bb06b737
HS
245 * @base: The base address of the VIC.
246 * @irq: The base IRQ for the VIC.
fa943bed 247 * @valid_sources: bitmask of valid interrupts
bb06b737 248 * @resume_sources: bitmask of interrupts allowed for resume sources.
f9b28ccb 249 * @node: The device tree node associated with the VIC.
bb06b737
HS
250 *
251 * Register the VIC with the system device tree so that it can be notified
252 * of suspend and resume requests and ensure that the correct actions are
253 * taken to re-instate the settings on resume.
f9b28ccb
JI
254 *
255 * This also configures the IRQ domain for the VIC.
bb06b737 256 */
f9b28ccb 257static void __init vic_register(void __iomem *base, unsigned int irq,
fa943bed
LW
258 u32 valid_sources, u32 resume_sources,
259 struct device_node *node)
bb06b737
HS
260{
261 struct vic_device *v;
5ced33bc 262 int i;
bb06b737 263
f9b28ccb 264 if (vic_id >= ARRAY_SIZE(vic_devices)) {
bb06b737 265 printk(KERN_ERR "%s: too few VICs, increase CONFIG_ARM_VIC_NR\n", __func__);
f9b28ccb 266 return;
bb06b737 267 }
f9b28ccb
JI
268
269 v = &vic_devices[vic_id];
270 v->base = base;
ce94df9c 271 v->valid_sources = valid_sources;
f9b28ccb
JI
272 v->resume_sources = resume_sources;
273 v->irq = irq;
7fb7d8ae 274 set_handle_irq(vic_handle_irq);
f9b28ccb 275 vic_id++;
07c9249f 276 v->domain = irq_domain_add_simple(node, fls(valid_sources), irq,
fa943bed 277 &vic_irqdomain_ops, v);
5ced33bc
LW
278 /* create an IRQ mapping for each valid IRQ */
279 for (i = 0; i < fls(valid_sources); i++)
280 if (valid_sources & (1 << i))
281 irq_create_mapping(v->domain, i);
bb06b737 282}
bb06b737 283
f013c98d 284static void vic_ack_irq(struct irq_data *d)
bb06b737 285{
f013c98d 286 void __iomem *base = irq_data_get_irq_chip_data(d);
f9b28ccb 287 unsigned int irq = d->hwirq;
bb06b737
HS
288 writel(1 << irq, base + VIC_INT_ENABLE_CLEAR);
289 /* moreover, clear the soft-triggered, in case it was the reason */
290 writel(1 << irq, base + VIC_INT_SOFT_CLEAR);
291}
292
f013c98d 293static void vic_mask_irq(struct irq_data *d)
bb06b737 294{
f013c98d 295 void __iomem *base = irq_data_get_irq_chip_data(d);
f9b28ccb 296 unsigned int irq = d->hwirq;
bb06b737
HS
297 writel(1 << irq, base + VIC_INT_ENABLE_CLEAR);
298}
299
f013c98d 300static void vic_unmask_irq(struct irq_data *d)
bb06b737 301{
f013c98d 302 void __iomem *base = irq_data_get_irq_chip_data(d);
f9b28ccb 303 unsigned int irq = d->hwirq;
bb06b737
HS
304 writel(1 << irq, base + VIC_INT_ENABLE);
305}
306
307#if defined(CONFIG_PM)
c07f87f2
BD
308static struct vic_device *vic_from_irq(unsigned int irq)
309{
310 struct vic_device *v = vic_devices;
311 unsigned int base_irq = irq & ~31;
312 int id;
313
314 for (id = 0; id < vic_id; id++, v++) {
315 if (v->irq == base_irq)
316 return v;
317 }
318
319 return NULL;
320}
321
f013c98d 322static int vic_set_wake(struct irq_data *d, unsigned int on)
c07f87f2 323{
f013c98d 324 struct vic_device *v = vic_from_irq(d->irq);
f9b28ccb 325 unsigned int off = d->hwirq;
3f1a567d 326 u32 bit = 1 << off;
c07f87f2
BD
327
328 if (!v)
329 return -EINVAL;
330
3f1a567d
BD
331 if (!(bit & v->resume_sources))
332 return -EINVAL;
333
c07f87f2 334 if (on)
3f1a567d 335 v->resume_irqs |= bit;
c07f87f2 336 else
3f1a567d 337 v->resume_irqs &= ~bit;
c07f87f2
BD
338
339 return 0;
340}
c07f87f2 341#else
c07f87f2
BD
342#define vic_set_wake NULL
343#endif /* CONFIG_PM */
344
38c677cb 345static struct irq_chip vic_chip = {
b0c4c898 346 .name = "VIC",
f013c98d
LB
347 .irq_ack = vic_ack_irq,
348 .irq_mask = vic_mask_irq,
349 .irq_unmask = vic_unmask_irq,
350 .irq_set_wake = vic_set_wake,
fa0fe48f
RK
351};
352
b0c4c898
HS
353static void __init vic_disable(void __iomem *base)
354{
355 writel(0, base + VIC_INT_SELECT);
356 writel(0, base + VIC_INT_ENABLE);
357 writel(~0, base + VIC_INT_ENABLE_CLEAR);
b0c4c898
HS
358 writel(0, base + VIC_ITCR);
359 writel(~0, base + VIC_INT_SOFT_CLEAR);
360}
361
362static void __init vic_clear_interrupts(void __iomem *base)
363{
364 unsigned int i;
365
366 writel(0, base + VIC_PL190_VECT_ADDR);
367 for (i = 0; i < 19; i++) {
368 unsigned int value;
369
370 value = readl(base + VIC_PL190_VECT_ADDR);
371 writel(value, base + VIC_PL190_VECT_ADDR);
372 }
373}
374
bb06b737
HS
375/*
376 * The PL190 cell from ARM has been modified by ST to handle 64 interrupts.
377 * The original cell has 32 interrupts, while the modified one has 64,
378 * replocating two blocks 0x00..0x1f in 0x20..0x3f. In that case
379 * the probe function is called twice, with base set to offset 000
380 * and 020 within the page. We call this "second block".
381 */
382static void __init vic_init_st(void __iomem *base, unsigned int irq_start,
ad622671 383 u32 vic_sources, struct device_node *node)
bb06b737
HS
384{
385 unsigned int i;
386 int vic_2nd_block = ((unsigned long)base & ~PAGE_MASK) != 0;
387
388 /* Disable all interrupts initially. */
b0c4c898 389 vic_disable(base);
bb06b737
HS
390
391 /*
392 * Make sure we clear all existing interrupts. The vector registers
393 * in this cell are after the second block of general registers,
394 * so we can address them using standard offsets, but only from
395 * the second base address, which is 0x20 in the page
396 */
397 if (vic_2nd_block) {
b0c4c898 398 vic_clear_interrupts(base);
bb06b737 399
bb06b737
HS
400 /* ST has 16 vectors as well, but we don't enable them by now */
401 for (i = 0; i < 16; i++) {
402 void __iomem *reg = base + VIC_VECT_CNTL0 + (i * 4);
403 writel(0, reg);
404 }
405
406 writel(32, base + VIC_PL190_DEF_VECT_ADDR);
407 }
408
fa943bed 409 vic_register(base, irq_start, vic_sources, 0, node);
bb06b737 410}
87e8824b 411
07c9249f 412void __init __vic_init(void __iomem *base, int irq_start,
f9b28ccb
JI
413 u32 vic_sources, u32 resume_sources,
414 struct device_node *node)
fa0fe48f
RK
415{
416 unsigned int i;
87e8824b 417 u32 cellid = 0;
f17a1f06 418 enum amba_vendor vendor;
87e8824b
AR
419
420 /* Identify which VIC cell this one is, by reading the ID */
421 for (i = 0; i < 4; i++) {
d4f3add2
AB
422 void __iomem *addr;
423 addr = (void __iomem *)((u32)base & PAGE_MASK) + 0xfe0 + (i * 4);
87e8824b
AR
424 cellid |= (readl(addr) & 0xff) << (8 * i);
425 }
426 vendor = (cellid >> 12) & 0xff;
427 printk(KERN_INFO "VIC @%p: id 0x%08x, vendor 0x%02x\n",
428 base, cellid, vendor);
429
430 switch(vendor) {
f17a1f06 431 case AMBA_VENDOR_ST:
ad622671 432 vic_init_st(base, irq_start, vic_sources, node);
87e8824b
AR
433 return;
434 default:
435 printk(KERN_WARNING "VIC: unknown vendor, continuing anyways\n");
436 /* fall through */
f17a1f06 437 case AMBA_VENDOR_ARM:
87e8824b
AR
438 break;
439 }
fa0fe48f 440
fa0fe48f 441 /* Disable all interrupts initially. */
b0c4c898 442 vic_disable(base);
fa0fe48f 443
b0c4c898
HS
444 /* Make sure we clear all existing interrupts */
445 vic_clear_interrupts(base);
fa0fe48f 446
c07f87f2 447 vic_init2(base);
fa0fe48f 448
fa943bed 449 vic_register(base, irq_start, vic_sources, resume_sources, node);
f9b28ccb
JI
450}
451
452/**
453 * vic_init() - initialise a vectored interrupt controller
454 * @base: iomem base address
455 * @irq_start: starting interrupt number, must be muliple of 32
456 * @vic_sources: bitmask of interrupt sources to allow
457 * @resume_sources: bitmask of interrupt sources to allow for resume
458 */
459void __init vic_init(void __iomem *base, unsigned int irq_start,
460 u32 vic_sources, u32 resume_sources)
461{
462 __vic_init(base, irq_start, vic_sources, resume_sources, NULL);
463}
464
465#ifdef CONFIG_OF
466int __init vic_of_init(struct device_node *node, struct device_node *parent)
467{
468 void __iomem *regs;
f9b28ccb
JI
469
470 if (WARN(parent, "non-root VICs are not supported"))
471 return -EINVAL;
472
473 regs = of_iomap(node, 0);
474 if (WARN_ON(!regs))
475 return -EIO;
476
07c9249f 477 /*
5ced33bc 478 * Passing 0 as first IRQ makes the simple domain allocate descriptors
07c9249f 479 */
5ced33bc 480 __vic_init(regs, 0, ~0, ~0, node);
f9b28ccb
JI
481
482 return 0;
fa0fe48f 483}
f9b28ccb 484#endif /* CONFIG OF */
This page took 0.703399 seconds and 5 git commands to generate.