summaryrefslogtreecommitdiffstats
path: root/arch/sh/kernel/cpu/irq/intc.c
blob: 9345a7130e9e5f9baddeb384671cb39271679213 (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
 * Shared interrupt handling code for IPR and INTC2 types of IRQs.
 *
 * Copyright (C) 2007 Magnus Damm
 *
 * Based on intc2.c and ipr.c
 *
 * Copyright (C) 1999  Niibe Yutaka & Takeshi Yaegashi
 * Copyright (C) 2000  Kazumoto Kojima
 * Copyright (C) 2001  David J. Mckay (david.mckay@st.com)
 * Copyright (C) 2003  Takashi Kusuda <kusuda-takashi@hitachi-ul.co.jp>
 * Copyright (C) 2005, 2006  Paul Mundt
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 */
#include <linux/init.h>
#include <linux/irq.h>
#include <linux/module.h>
#include <linux/io.h>
#include <linux/interrupt.h>

#define _INTC_MK(fn, idx, bit, value) \
	((fn) << 24 | ((value) << 16) | ((idx) << 8) | (bit))
#define _INTC_FN(h) (h >> 24)
#define _INTC_VALUE(h) ((h >> 16) & 0xff)
#define _INTC_IDX(h) ((h >> 8) & 0xff)
#define _INTC_BIT(h) (h & 0xff)

#define _INTC_PTR(desc, member, data) \
	(desc->member + _INTC_IDX(data))

static inline struct intc_desc *get_intc_desc(unsigned int irq)
{
	struct irq_chip *chip = get_irq_chip(irq);
	return (void *)((char *)chip - offsetof(struct intc_desc, chip));
}

static inline unsigned int set_field(unsigned int value,
				     unsigned int field_value,
				     unsigned int width,
				     unsigned int shift)
{
	value &= ~(((1 << width) - 1) << shift);
	value |= field_value << shift;
	return value;
}

static inline unsigned int set_prio_field(struct intc_desc *desc,
					  unsigned int value,
					  unsigned int priority,
					  unsigned int data)
{
	unsigned int width = _INTC_PTR(desc, prio_regs, data)->field_width;

	return set_field(value, priority, width, _INTC_BIT(data));
}

static void disable_prio_16(struct intc_desc *desc, unsigned int data)
{
	unsigned long addr = _INTC_PTR(desc, prio_regs, data)->reg;

	ctrl_outw(set_prio_field(desc, ctrl_inw(addr), 0, data), addr);
}

static void enable_prio_16(struct intc_desc *desc, unsigned int data)
{
	unsigned long addr = _INTC_PTR(desc, prio_regs, data)->reg;
	unsigned int prio = _INTC_VALUE(data);

	ctrl_outw(set_prio_field(desc, ctrl_inw(addr), prio, data), addr);
}

static void disable_prio_32(struct intc_desc *desc, unsigned int data)
{
	unsigned long addr = _INTC_PTR(desc, prio_regs, data)->reg;

	ctrl_outl(set_prio_field(desc, ctrl_inl(addr), 0, data), addr);
}

static void enable_prio_32(struct intc_desc *desc, unsigned int data)
{
	unsigned long addr = _INTC_PTR(desc, prio_regs, data)->reg;
	unsigned int prio = _INTC_VALUE(data);

	ctrl_outl(set_prio_field(desc, ctrl_inl(addr), prio, data), addr);
}

static void disable_mask_8(struct intc_desc *desc, unsigned int data)
{
	ctrl_outb(1 << _INTC_BIT(data),
		  _INTC_PTR(desc, mask_regs, data)->set_reg);
}

static void enable_mask_8(struct intc_desc *desc, unsigned int data)
{
	ctrl_outb(1 << _INTC_BIT(data),
		  _INTC_PTR(desc, mask_regs, data)->clr_reg);
}

static void disable_mask_32(struct intc_desc *desc, unsigned int data)
{
	ctrl_outl(1 << _INTC_BIT(data),
		  _INTC_PTR(desc, mask_regs, data)->set_reg);
}

static void enable_mask_32(struct intc_desc *desc, unsigned int data)
{
	ctrl_outl(1 << _INTC_BIT(data),
		  _INTC_PTR(desc, mask_regs, data)->clr_reg);
}

enum {	REG_FN_ERROR=0,
	REG_FN_MASK_8, REG_FN_MASK_32,
	REG_FN_PRIO_16, REG_FN_PRIO_32 };

static struct {
	void (*enable)(struct intc_desc *, unsigned int);
	void (*disable)(struct intc_desc *, unsigned int);
} intc_reg_fns[] = {
	[REG_FN_MASK_8] = { enable_mask_8, disable_mask_8 },
	[REG_FN_MASK_32] = { enable_mask_32, disable_mask_32 },
	[REG_FN_PRIO_16] = { enable_prio_16, disable_prio_16 },
	[REG_FN_PRIO_32] = { enable_prio_32, disable_prio_32 },
};

static void intc_enable(unsigned int irq)
{
	struct intc_desc *desc = get_intc_desc(irq);
	unsigned int data = (unsigned int) get_irq_chip_data(irq);

	intc_reg_fns[_INTC_FN(data)].enable(desc, data);
}

static void intc_disable(unsigned int irq)
{
	struct intc_desc *desc = get_intc_desc(irq);
	unsigned int data = (unsigned int) get_irq_chip_data(irq);

	intc_reg_fns[_INTC_FN(data)].disable(desc, data);
}

static void set_sense_16(struct intc_desc *desc, unsigned int data)
{
	unsigned long addr = _INTC_PTR(desc, sense_regs, data)->reg;
	unsigned int width = _INTC_PTR(desc, sense_regs, data)->field_width;
	unsigned int bit = _INTC_BIT(data);
	unsigned int value = _INTC_VALUE(data);

	ctrl_outw(set_field(ctrl_inw(addr), value, width, bit), addr);
}

static void set_sense_32(struct intc_desc *desc, unsigned int data)
{
	unsigned long addr = _INTC_PTR(desc, sense_regs, data)->reg;
	unsigned int width = _INTC_PTR(desc, sense_regs, data)->field_width;
	unsigned int bit = _INTC_BIT(data);
	unsigned int value = _INTC_VALUE(data);

	ctrl_outl(set_field(ctrl_inl(addr), value, width, bit), addr);
}

#define VALID(x) (x | 0x80)

static unsigned char intc_irq_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
	[IRQ_TYPE_EDGE_FALLING] = VALID(0),
	[IRQ_TYPE_EDGE_RISING] = VALID(1),
	[IRQ_TYPE_LEVEL_LOW] = VALID(2),
	[IRQ_TYPE_LEVEL_HIGH] = VALID(3),
};

static int intc_set_sense(unsigned int irq, unsigned int type)
{
	struct intc_desc *desc = get_intc_desc(irq);
	unsigned char value = intc_irq_sense_table[type & IRQ_TYPE_SENSE_MASK];
	unsigned int i, j, data, bit;
	intc_enum enum_id = 0;

	for (i = 0; i < desc->nr_vectors; i++) {
		struct intc_vect *vect = desc->vectors + i;

		if (evt2irq(vect->vect) != irq)
			continue;

		enum_id = vect->enum_id;
		break;
	}

	if (!enum_id || !value)
		return -EINVAL;

	value ^= VALID(0);

	for (i = 0; i < desc->nr_sense_regs; i++) {
		struct intc_sense_reg *sr = desc->sense_regs + i;

		for (j = 0; j < ARRAY_SIZE(sr->enum_ids); j++) {
			if (sr->enum_ids[j] != enum_id)
				continue;

			bit = sr->reg_width - ((j + 1) * sr->field_width);
			data = _INTC_MK(0, i, bit, value);

			switch(sr->reg_width) {
			case 16:
				set_sense_16(desc, data);
				break;
			case 32:
				set_sense_32(desc, data);
				break;
			}

			return 0;
		}
	}

	return -EINVAL;
}

static unsigned int __init intc_find_mask_handler(unsigned int width)
{
	switch (width) {
	case 8:
		return REG_FN_MASK_8;
	case 32:
		return REG_FN_MASK_32;
	}

	BUG();
	return REG_FN_ERROR;
}

static unsigned int __init intc_find_prio_handler(unsigned int width)
{
	switch (width) {
	case 16:
		return REG_FN_PRIO_16;
	case 32:
		return REG_FN_PRIO_32;
	}

	BUG();
	return REG_FN_ERROR;
}

static intc_enum __init intc_grp_id(struct intc_desc *desc, intc_enum enum_id)
{
	struct intc_group *g = desc->groups;
	unsigned int i, j;

	for (i = 0; g && enum_id && i < desc->nr_groups; i++) {
		g = desc->groups + i;

		for (j = 0; g->enum_ids[j]; j++) {
			if (g->enum_ids[j] != enum_id)
				continue;

			return g->enum_id;
		}
	}

	return 0;
}

static unsigned int __init intc_prio_value(struct intc_desc *desc,
					   intc_enum enum_id, int do_grps)
{
	struct intc_prio *p = desc->priorities;
	unsigned int i;

	for (i = 0; p && enum_id && i < desc->nr_priorities; i++) {
		p = desc->priorities + i;

		if (p->enum_id != enum_id)
			continue;

		return p->priority;
	}

	if (do_grps)
		return intc_prio_value(desc, intc_grp_id(desc, enum_id), 0);

	/* default to the lowest priority possible if no priority is set
	 * - this needs to be at least 2 for 5-bit priorities on 7780
	 */

	return 2;
}

static unsigned int __init intc_mask_data(struct intc_desc *desc,
					  intc_enum enum_id, int do_grps)
{
	struct intc_mask_reg *mr = desc->mask_regs;
	unsigned int i, j, fn;

	for (i = 0; mr && enum_id && i < desc->nr_mask_regs; i++) {
		mr = desc->mask_regs + i;

		for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
			if (mr->enum_ids[j] != enum_id)
				continue;

			fn = intc_find_mask_handler(mr->reg_width);
			if (fn == REG_FN_ERROR)
				return 0;

			return _INTC_MK(fn, i, (mr->reg_width - 1) - j, 0);
		}
	}

	if (do_grps)
		return intc_mask_data(desc, intc_grp_id(desc, enum_id), 0);

	return 0;
}

static unsigned int __init intc_prio_data(struct intc_desc *desc,
					  intc_enum enum_id, int do_grps)
{
	struct intc_prio_reg *pr = desc->prio_regs;
	unsigned int i, j, fn, bit, prio;

	for (i = 0; pr && enum_id && i < desc->nr_prio_regs; i++) {
		pr = desc->prio_regs + i;

		for (j = 0; j < ARRAY_SIZE(pr->enum_ids); j++) {
			if (pr->enum_ids[j] != enum_id)
				continue;

			fn = intc_find_prio_handler(pr->reg_width);
			if (fn == REG_FN_ERROR)
				return 0;

			prio = intc_prio_value(desc, enum_id, 1);
			bit = pr->reg_width - ((j + 1) * pr->field_width);

			BUG_ON(bit < 0);

			return _INTC_MK(fn, i, bit, prio);
		}
	}

	if (do_grps)
		return intc_prio_data(desc, intc_grp_id(desc, enum_id), 0);

	return 0;
}

static void __init intc_register_irq(struct intc_desc *desc, intc_enum enum_id,
				     unsigned int irq)
{
	unsigned int data[2], primary;

	/* Prefer single interrupt source bitmap over other combinations:
	 * 1. bitmap, single interrupt source
	 * 2. priority, single interrupt source
	 * 3. bitmap, multiple interrupt sources (groups)
	 * 4. priority, multiple interrupt sources (groups)
	 */

	data[0] = intc_mask_data(desc, enum_id, 0);
	data[1] = intc_prio_data(desc, enum_id, 0);

	primary = 0;
	if (!data[0] && data[1])
		primary = 1;

	data[0] = data[0] ? data[0] : intc_mask_data(desc, enum_id, 1);
	data[1] = data[1] ? data[1] : intc_prio_data(desc, enum_id, 1);

	if (!data[primary])
		primary ^= 1;

	BUG_ON(!data[primary]); /* must have primary masking method */

	disable_irq_nosync(irq);
	set_irq_chip_and_handler_name(irq, &desc->chip,
				      handle_level_irq, "level");
	set_irq_chip_data(irq, (void *)data[primary]);

	/* enable secondary masking method if present */
	if (data[!primary])
		intc_reg_fns[_INTC_FN(data[!primary])].enable(desc,
							      data[!primary]);

	/* irq should be disabled by default */
	desc->chip.mask(irq);
}

void __init register_intc_controller(struct intc_desc *desc)
{
	unsigned int i;

	desc->chip.mask = intc_disable;
	desc->chip.unmask = intc_enable;
	desc->chip.mask_ack = intc_disable;
	desc->chip.set_type = intc_set_sense;

	for (i = 0; i < desc->nr_vectors; i++) {
		struct intc_vect *vect = desc->vectors + i;

		intc_register_irq(desc, vect->enum_id, evt2irq(vect->vect));
	}
}