hlwd-pic.c 5.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
/*
 * arch/powerpc/platforms/embedded6xx/hlwd-pic.c
 *
 * Nintendo Wii "Hollywood" interrupt controller support.
 * Copyright (C) 2009 The GameCube Linux Team
 * Copyright (C) 2009 Albert Herranz
 *
 * 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.
 *
 */
#define DRV_MODULE_NAME "hlwd-pic"
#define pr_fmt(fmt) DRV_MODULE_NAME ": " fmt

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/irq.h>
#include <linux/of.h>
#include <asm/io.h>

#include "hlwd-pic.h"

#define HLWD_NR_IRQS	32

/*
 * Each interrupt has a corresponding bit in both
 * the Interrupt Cause (ICR) and Interrupt Mask (IMR) registers.
 *
 * Enabling/disabling an interrupt line involves asserting/clearing
 * the corresponding bit in IMR. ACK'ing a request simply involves
 * asserting the corresponding bit in ICR.
 */
#define HW_BROADWAY_ICR		0x00
#define HW_BROADWAY_IMR		0x04


/*
 * IRQ chip hooks.
 *
 */

44
static void hlwd_pic_mask_and_ack(struct irq_data *d)
45
{
46
	int irq = irqd_to_hwirq(d);
47
	void __iomem *io_base = irq_data_get_irq_chip_data(d);
48 49 50 51 52 53
	u32 mask = 1 << irq;

	clrbits32(io_base + HW_BROADWAY_IMR, mask);
	out_be32(io_base + HW_BROADWAY_ICR, mask);
}

54
static void hlwd_pic_ack(struct irq_data *d)
55
{
56
	int irq = irqd_to_hwirq(d);
57
	void __iomem *io_base = irq_data_get_irq_chip_data(d);
58 59 60 61

	out_be32(io_base + HW_BROADWAY_ICR, 1 << irq);
}

62
static void hlwd_pic_mask(struct irq_data *d)
63
{
64
	int irq = irqd_to_hwirq(d);
65
	void __iomem *io_base = irq_data_get_irq_chip_data(d);
66 67 68 69

	clrbits32(io_base + HW_BROADWAY_IMR, 1 << irq);
}

70
static void hlwd_pic_unmask(struct irq_data *d)
71
{
72
	int irq = irqd_to_hwirq(d);
73
	void __iomem *io_base = irq_data_get_irq_chip_data(d);
74 75 76 77 78 79 80

	setbits32(io_base + HW_BROADWAY_IMR, 1 << irq);
}


static struct irq_chip hlwd_pic = {
	.name		= "hlwd-pic",
81 82 83 84
	.irq_ack	= hlwd_pic_ack,
	.irq_mask_ack	= hlwd_pic_mask_and_ack,
	.irq_mask	= hlwd_pic_mask,
	.irq_unmask	= hlwd_pic_unmask,
85 86 87 88 89 90 91
};

/*
 * IRQ host hooks.
 *
 */

92
static struct irq_domain *hlwd_irq_host;
93

94
static int hlwd_pic_map(struct irq_domain *h, unsigned int virq,
95 96
			   irq_hw_number_t hwirq)
{
97
	irq_set_chip_data(virq, h->host_data);
98
	irq_set_status_flags(virq, IRQ_LEVEL);
99
	irq_set_chip_and_handler(virq, &hlwd_pic, handle_level_irq);
100 101 102
	return 0;
}

103
static struct irq_domain_ops hlwd_irq_domain_ops = {
104 105 106
	.map = hlwd_pic_map,
};

107
static unsigned int __hlwd_pic_get_irq(struct irq_domain *h)
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
{
	void __iomem *io_base = h->host_data;
	int irq;
	u32 irq_status;

	irq_status = in_be32(io_base + HW_BROADWAY_ICR) &
		     in_be32(io_base + HW_BROADWAY_IMR);
	if (irq_status == 0)
		return NO_IRQ;	/* no more IRQs pending */

	irq = __ffs(irq_status);
	return irq_linear_revmap(h, irq);
}

static void hlwd_pic_irq_cascade(unsigned int cascade_virq,
				      struct irq_desc *desc)
{
125
	struct irq_chip *chip = irq_desc_get_chip(desc);
126
	struct irq_domain *irq_domain = irq_get_handler_data(cascade_virq);
127 128
	unsigned int virq;

129
	raw_spin_lock(&desc->lock);
130
	chip->irq_mask(&desc->irq_data); /* IRQ_LEVEL */
131
	raw_spin_unlock(&desc->lock);
132

133
	virq = __hlwd_pic_get_irq(irq_domain);
134 135 136 137 138
	if (virq != NO_IRQ)
		generic_handle_irq(virq);
	else
		pr_err("spurious interrupt!\n");

139
	raw_spin_lock(&desc->lock);
140
	chip->irq_ack(&desc->irq_data); /* IRQ_LEVEL */
141
	if (!irqd_irq_disabled(&desc->irq_data) && chip->irq_unmask)
142
		chip->irq_unmask(&desc->irq_data);
143
	raw_spin_unlock(&desc->lock);
144 145 146 147 148 149 150 151 152 153 154 155 156 157
}

/*
 * Platform hooks.
 *
 */

static void __hlwd_quiesce(void __iomem *io_base)
{
	/* mask and ack all IRQs */
	out_be32(io_base + HW_BROADWAY_IMR, 0);
	out_be32(io_base + HW_BROADWAY_ICR, 0xffffffff);
}

158
struct irq_domain *hlwd_pic_init(struct device_node *np)
159
{
160
	struct irq_domain *irq_domain;
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	struct resource res;
	void __iomem *io_base;
	int retval;

	retval = of_address_to_resource(np, 0, &res);
	if (retval) {
		pr_err("no io memory range found\n");
		return NULL;
	}
	io_base = ioremap(res.start, resource_size(&res));
	if (!io_base) {
		pr_err("ioremap failed\n");
		return NULL;
	}

	pr_info("controller at 0x%08x mapped to 0x%p\n", res.start, io_base);

	__hlwd_quiesce(io_base);

180 181
	irq_domain = irq_domain_add_linear(np, HLWD_NR_IRQS,
					   &hlwd_irq_domain_ops, io_base);
182 183
	if (!irq_domain) {
		pr_err("failed to allocate irq_domain\n");
184 185 186
		return NULL;
	}

187
	return irq_domain;
188 189 190 191
}

unsigned int hlwd_pic_get_irq(void)
{
192
	return __hlwd_pic_get_irq(hlwd_irq_domain);
193 194 195 196 197 198 199 200 201
}

/*
 * Probe function.
 *
 */

void hlwd_pic_probe(void)
{
202
	struct irq_domain *host;
203 204 205 206 207 208 209 210 211 212
	struct device_node *np;
	const u32 *interrupts;
	int cascade_virq;

	for_each_compatible_node(np, NULL, "nintendo,hollywood-pic") {
		interrupts = of_get_property(np, "interrupts", NULL);
		if (interrupts) {
			host = hlwd_pic_init(np);
			BUG_ON(!host);
			cascade_virq = irq_of_parse_and_map(np, 0);
213 214
			irq_set_handler_data(cascade_virq, host);
			irq_set_chained_handler(cascade_virq,
215
						hlwd_pic_irq_cascade);
216
			hlwd_irq_domain = host;
217 218 219 220 221 222 223 224 225 226 227 228 229
			break;
		}
	}
}

/**
 * hlwd_quiesce() - quiesce hollywood irq controller
 *
 * Mask and ack all interrupt sources.
 *
 */
void hlwd_quiesce(void)
{
230
	void __iomem *io_base = hlwd_irq_domain->host_data;
231 232 233 234

	__hlwd_quiesce(io_base);
}