summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/irq.c
blob: b07f01c41b6d6c00be1f5290cf3ed0922584dc03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * arch/arm/mach-tegra/irq.c
 *
 * IRQ chip driver for Tegra SoCs
 *
 * Copyright (c) 2009, NVIDIA Corporation.
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/irq.h>
#include <linux/smp.h>
#include <linux/io.h>
#include <linux/interrupt.h>

#include <asm/irq.h>
#include <asm/bitops.h>
#include <mach/platform.h>

#ifdef CONFIG_ARCH_TEGRA_2x_SOC
#define INT_PPI_ADDRESS(_inst) (0x60004000ul + 0x100*(_inst))
#define INT_APBDMA_ADDRESS 0x6000a000ul
#else
#error "interrupt controller addresses not defined"
#endif

#ifdef CONFIG_ARM_GIC
#ifdef CONFIG_ARCH_TEGRA_2x_SOC
#define ARM_PERIF_BASE 0x50040000ul
#define GIC_PROC_IF_BASE (ARM_PERIF_BASE + 0x100ul)
#define GIC_DIST_BASE (ARM_PERIF_BASE + 0x1000ul)
#else
#error "interrupt distributor address not defined"
#endif
#endif

#define ICTLR_CPU_IER_0       0x20
#define ICTLR_CPU_IER_SET_0   0x24
#define ICTLR_CPU_IER_CLR_0   0x28
#define ICTLR_CPU_IEP_CLASS_0 0x2c
#define ICTLR_COP_IER_0       0x30
#define ICTLR_COP_IER_SET_0   0x34
#define ICTLR_COP_IER_CLR_0   0x38
#define ICTLR_COP_IEP_CLASS_0 0x3c

#define APBDMA_IRQ_STA_CPU_0  0x14
#define APBDMA_IRQ_MASK_SET_0 0x20
#define APBDMA_IRQ_MASK_CLR_0 0x24

#ifdef CONFIG_ARM_GIC
extern void gic_mask_irq(unsigned int);
extern void gic_unmask_irq(unsigned int);
extern void gic_ack_irq(unsigned int);
extern void gic_set_cpu(unsigned int, const struct cpumask*);
extern void gic_dist_init(unsigned int, void __iomem *, unsigned int);
extern void gic_cpu_init(unsigned int, void __iomem *);
#else
#define gic_mask_irq(i) do { } while (0)
#define gic_unmask_irq(i) do { } while (0)
#define gic_ack_irq(i) do { } while (0)
#define gic_set_cpu NULL
#define gic_dist_init(i, p, s) do { } while (0)
#define gic_cpu_init(i, p) do { } while (0)
#endif

struct tegra_irq_chip {
	unsigned int irq_start;
	void __iomem *mmio;
	/* context save/restore data for interrupts */
#ifdef CONFIG_PM
	u32 cpu_ier;
	u32 cop_ier;
#endif
};

static struct tegra_irq_chip tegra_chip[(INT_SYS_NR+INT_SYS_SZ-1)/INT_SYS_SZ];

static void tegra_mask(unsigned int irq)
{
	struct tegra_irq_chip *chip;
	gic_mask_irq(irq);
	irq -= INT_PRI_BASE;
	chip = &tegra_chip[irq/INT_SYS_SZ];
	writel(1<<(irq&31), chip->mmio + ICTLR_CPU_IER_CLR_0);
}

static void tegra_unmask(unsigned int irq)
{
	struct tegra_irq_chip *chip;
	gic_unmask_irq(irq);
	irq -= INT_PRI_BASE;
	chip = &tegra_chip[irq/INT_SYS_SZ];
	writel(1<<(irq&31), chip->mmio + ICTLR_CPU_IER_SET_0);
}

static void tegra_ack(unsigned int irq)
{
	gic_ack_irq(irq);
}

#ifdef CONFIG_PM

static int tegra_set_wake(unsigned int irq, unsigned int on)
{
	return 0;
}

void tegra_irq_resume(void)
{
	unsigned long flags;
	int i;

        local_irq_save(flags);
	for (i=0; i<ARRAY_SIZE(tegra_chip); i++) {
		struct tegra_irq_chip *chip = &tegra_chip[i];
		writel(chip->cpu_ier, chip->mmio + ICTLR_CPU_IER_SET_0);
		writel(0, chip->mmio + ICTLR_CPU_IEP_CLASS_0);
		writel(chip->cop_ier, chip->mmio + ICTLR_COP_IER_SET_0);
		writel(0, chip->mmio + ICTLR_COP_IEP_CLASS_0);
	}
	local_irq_restore(flags);

	for (i=INT_PRI_BASE; i<INT_GPIO_BASE; i++) {
		struct irq_desc *desc = irq_to_desc(i);
		if (!desc || (desc->status & IRQ_WAKEUP)) continue;
		enable_irq(i);
	}

	for (i=INT_APBDMA_BASE; i<INT_APBDMA_BASE+INT_APBDMA_NR; i++)
		enable_irq(i);
}

void tegra_irq_suspend(void)
{
	unsigned long flags;
	int i;

	for (i=INT_APBDMA_BASE; i<INT_APBDMA_BASE+INT_APBDMA_NR; i++)
		disable_irq(i);

	for (i=INT_PRI_BASE; i<INT_GPIO_BASE; i++) {
		struct irq_desc *desc= irq_to_desc(i);
		if (!desc) continue;
		if (desc->status & IRQ_WAKEUP) {
			pr_debug("irq %d is wakeup\n", i);
			continue;
		}
		disable_irq(i);
	}

	local_irq_save(flags);
	for (i=0; i<ARRAY_SIZE(tegra_chip); i++) {
		struct tegra_irq_chip *chip = &tegra_chip[i];
		chip->cpu_ier = readl(chip->mmio + ICTLR_CPU_IER_0);
		chip->cop_ier = readl(chip->mmio + ICTLR_COP_IER_0);
		writel(~0ul, chip->mmio + ICTLR_COP_IER_CLR_0);
	}
	local_irq_restore(flags);
}

#endif


#ifdef CONFIG_TEGRA_SYSTEM_DMA
struct apbdma_irq_chip {
	unsigned int irq_start;
	void __iomem *mmio;
	spinlock_t lock;
};

static struct apbdma_irq_chip apbdma_chip;

static void apbdma_ack(unsigned int irq) { }

static void apbdma_mask(unsigned int irq)
{
	struct apbdma_irq_chip *chip = get_irq_chip_data(irq);
	irq -= chip->irq_start;
	writel(1<<irq, chip->mmio + APBDMA_IRQ_MASK_CLR_0);
}

static void apbdma_unmask(unsigned int irq)
{
	struct apbdma_irq_chip *chip = get_irq_chip_data(irq);
	irq -= chip->irq_start;
	writel(1<<irq, chip->mmio + APBDMA_IRQ_MASK_SET_0);
}

static void apbdma_cascade(unsigned int irq, struct irq_desc *desc)
{
	struct irq_chip *pri = get_irq_chip(irq);
	struct apbdma_irq_chip *chip = get_irq_data(irq);
	u32 reg, ch=0;

	pri->ack(irq);
	spin_lock(&chip->lock);
	reg = readl(chip->mmio + APBDMA_IRQ_STA_CPU_0);
	if (reg) {
		reg = __fls(reg);
		writel(1<<reg, chip->mmio + APBDMA_IRQ_STA_CPU_0);
		ch = chip->irq_start + reg;
	}
	spin_unlock(&chip->lock);
	if (ch)	generic_handle_irq(ch);
	pri->unmask(irq);
}

static struct irq_chip apbdma_irq = {
	.name	= "APBDMA",
	.ack	= apbdma_ack,
	.mask	= apbdma_mask,
	.unmask	= apbdma_unmask,
};
#endif

static struct irq_chip tegra_irq = {
	.name		= "PPI",
	.mask		= tegra_mask,
	.unmask		= tegra_unmask,
	.ack		= tegra_ack,
#ifdef CONFIG_PM
	.set_wake	= tegra_set_wake,
#endif
#ifdef CONFIG_SMP
	.set_affinity	= gic_set_cpu,
#endif

};

void __init tegra_init_irq(void)
{
	unsigned int i;

	for (i=0; i<ARRAY_SIZE(tegra_chip); i++) {
		tegra_chip[i].irq_start = INT_PRI_BASE + INT_SYS_SZ*i;
		tegra_chip[i].mmio = IO_ADDRESS(INT_PPI_ADDRESS(i));
		writel(~0ul, tegra_chip[i].mmio + ICTLR_CPU_IER_CLR_0);
		writel(0, tegra_chip[i].mmio + ICTLR_CPU_IEP_CLASS_0);
	}

	gic_dist_init(0, IO_ADDRESS(GIC_DIST_BASE), 29);
	gic_cpu_init(0, IO_ADDRESS(GIC_PROC_IF_BASE));

	for (i=INT_PRI_BASE; i<INT_GPIO_BASE; i++) {
		set_irq_chip(i, &tegra_irq);
		set_irq_handler(i, handle_level_irq);
		set_irq_flags(i, IRQF_VALID);
	}

#ifdef CONFIG_TEGRA_SYSTEM_DMA
	apbdma_chip.mmio = IO_ADDRESS(INT_APBDMA_ADDRESS);
	spin_lock_init(&apbdma_chip.lock);
	apbdma_chip.irq_start = INT_APBDMA_BASE;

	for (i=INT_APBDMA_BASE; i<INT_APBDMA_NR+INT_APBDMA_BASE; i++) {
		set_irq_chip(i, &apbdma_irq);
		set_irq_chip_data(i, &apbdma_chip);
		set_irq_handler(i, handle_level_irq);
		set_irq_flags(i, IRQF_VALID);
	}
	if (set_irq_data(INT_APB_DMA, &apbdma_chip))
		BUG();
	set_irq_chained_handler(INT_APB_DMA, apbdma_cascade);
#endif
}