intc.c 11.2 KB
Newer Older
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
/*
 * 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);
}

90
static void write_set_reg_8(struct intc_desc *desc, unsigned int data)
91 92 93 94 95
{
	ctrl_outb(1 << _INTC_BIT(data),
		  _INTC_PTR(desc, mask_regs, data)->set_reg);
}

96
static void write_clr_reg_8(struct intc_desc *desc, unsigned int data)
97 98 99 100 101
{
	ctrl_outb(1 << _INTC_BIT(data),
		  _INTC_PTR(desc, mask_regs, data)->clr_reg);
}

102
static void write_set_reg_32(struct intc_desc *desc, unsigned int data)
103 104 105 106 107
{
	ctrl_outl(1 << _INTC_BIT(data),
		  _INTC_PTR(desc, mask_regs, data)->set_reg);
}

108
static void write_clr_reg_32(struct intc_desc *desc, unsigned int data)
109 110 111 112 113
{
	ctrl_outl(1 << _INTC_BIT(data),
		  _INTC_PTR(desc, mask_regs, data)->clr_reg);
}

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
static void or_set_reg_16(struct intc_desc *desc, unsigned int data)
{
	unsigned long addr = _INTC_PTR(desc, mask_regs, data)->set_reg;

	ctrl_outw(ctrl_inw(addr) | 1 << _INTC_BIT(data), addr);
}

static void and_set_reg_16(struct intc_desc *desc, unsigned int data)
{
	unsigned long addr = _INTC_PTR(desc, mask_regs, data)->set_reg;

	ctrl_outw(ctrl_inw(addr) & ~(1 << _INTC_BIT(data)), addr);
}

static void or_set_reg_32(struct intc_desc *desc, unsigned int data)
{
	unsigned long addr = _INTC_PTR(desc, mask_regs, data)->set_reg;

	ctrl_outl(ctrl_inl(addr) | 1 << _INTC_BIT(data), addr);
}

static void and_set_reg_32(struct intc_desc *desc, unsigned int data)
{
	unsigned long addr = _INTC_PTR(desc, mask_regs, data)->set_reg;

	ctrl_outl(ctrl_inl(addr) & ~(1 << _INTC_BIT(data)), addr);
}

142
enum {	REG_FN_ERROR=0,
143 144
	REG_FN_DUAL_8, REG_FN_DUAL_32,
	REG_FN_ENA_16, REG_FN_ENA_32,
145 146 147 148 149 150
	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[] = {
151 152 153 154
	[REG_FN_DUAL_8] = { write_clr_reg_8, write_set_reg_8 },
	[REG_FN_DUAL_32] = { write_clr_reg_32, write_set_reg_32 },
	[REG_FN_ENA_16] = { or_set_reg_16, and_set_reg_16 },
	[REG_FN_ENA_32] = { or_set_reg_32, and_set_reg_32 },
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
	[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;
}

252
static unsigned int __init intc_find_dual_handler(unsigned int width)
253 254 255
{
	switch (width) {
	case 8:
256
		return REG_FN_DUAL_8;
257
	case 32:
258
		return REG_FN_DUAL_32;
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
	}

	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;
}

278 279 280 281 282 283 284 285 286 287 288 289 290
static unsigned int __init intc_find_ena_handler(unsigned int width)
{
	switch (width) {
	case 16:
		return REG_FN_ENA_16;
	case 32:
		return REG_FN_ENA_32;
	}

	BUG();
	return REG_FN_ERROR;
}

M
Magnus Damm 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
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;
}

310
static unsigned int __init intc_prio_value(struct intc_desc *desc,
M
Magnus Damm 已提交
311
					   intc_enum enum_id, int do_grps)
312
{
M
Magnus Damm 已提交
313
	struct intc_prio *p = desc->priorities;
314 315
	unsigned int i;

M
Magnus Damm 已提交
316 317
	for (i = 0; p && enum_id && i < desc->nr_priorities; i++) {
		p = desc->priorities + i;
318 319 320 321 322 323 324

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

		return p->priority;
	}

M
Magnus Damm 已提交
325 326 327 328 329 330 331 332
	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;
333 334 335
}

static unsigned int __init intc_mask_data(struct intc_desc *desc,
M
Magnus Damm 已提交
336
					  intc_enum enum_id, int do_grps)
337
{
M
Magnus Damm 已提交
338
	struct intc_mask_reg *mr = desc->mask_regs;
339 340
	unsigned int i, j, fn;

M
Magnus Damm 已提交
341 342
	for (i = 0; mr && enum_id && i < desc->nr_mask_regs; i++) {
		mr = desc->mask_regs + i;
343 344 345 346 347

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

348 349 350 351 352 353 354 355
			switch (mr->clr_reg) {
			case 1: /* 1 = enabled interrupt - "enable" register */
				fn = intc_find_ena_handler(mr->reg_width);
				break;
			default:
				fn = intc_find_dual_handler(mr->reg_width);
			}

356 357 358 359 360 361 362
			if (fn == REG_FN_ERROR)
				return 0;

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

M
Magnus Damm 已提交
363 364 365
	if (do_grps)
		return intc_mask_data(desc, intc_grp_id(desc, enum_id), 0);

366 367 368 369
	return 0;
}

static unsigned int __init intc_prio_data(struct intc_desc *desc,
M
Magnus Damm 已提交
370
					  intc_enum enum_id, int do_grps)
371
{
M
Magnus Damm 已提交
372
	struct intc_prio_reg *pr = desc->prio_regs;
373 374
	unsigned int i, j, fn, bit, prio;

M
Magnus Damm 已提交
375 376
	for (i = 0; pr && enum_id && i < desc->nr_prio_regs; i++) {
		pr = desc->prio_regs + i;
377 378 379 380 381 382 383 384 385

		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;

M
Magnus Damm 已提交
386
			prio = intc_prio_value(desc, enum_id, 1);
387 388 389 390 391 392 393 394
			bit = pr->reg_width - ((j + 1) * pr->field_width);

			BUG_ON(bit < 0);

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

M
Magnus Damm 已提交
395 396 397
	if (do_grps)
		return intc_prio_data(desc, intc_grp_id(desc, enum_id), 0);

398 399 400 401 402 403
	return 0;
}

static void __init intc_register_irq(struct intc_desc *desc, intc_enum enum_id,
				     unsigned int irq)
{
M
Magnus Damm 已提交
404 405 406 407 408 409 410 411
	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)
	 */
412

M
Magnus Damm 已提交
413 414 415 416 417 418 419 420 421 422 423 424 425 426
	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 */
427 428 429 430

	disable_irq_nosync(irq);
	set_irq_chip_and_handler_name(irq, &desc->chip,
				      handle_level_irq, "level");
M
Magnus Damm 已提交
431
	set_irq_chip_data(irq, (void *)data[primary]);
432

M
Magnus Damm 已提交
433 434 435 436
	/* enable secondary masking method if present */
	if (data[!primary])
		intc_reg_fns[_INTC_FN(data[!primary])].enable(desc,
							      data[!primary]);
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456

	/* 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));
	}
}