irq-crossbar.c 8.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 *  drivers/irqchip/irq-crossbar.c
 *
 *  Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
 *  Author: Sricharan R <r.sricharan@ti.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */
#include <linux/err.h>
#include <linux/io.h>
14
#include <linux/irqchip.h>
15
#include <linux/irqdomain.h>
16 17 18
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/slab.h>
19

20
#define IRQ_FREE	-1
21
#define IRQ_RESERVED	-2
22
#define IRQ_SKIP	-3
23 24
#define GIC_IRQ_START	32

25 26
/**
 * struct crossbar_device - crossbar device description
27
 * @lock: spinlock serializing access to @irq_map
28
 * @int_max: maximum number of supported interrupts
29
 * @safe_map: safe default value to initialize the crossbar
30
 * @max_crossbar_sources: Maximum number of crossbar sources
31 32 33
 * @irq_map: array of interrupts to crossbar number mapping
 * @crossbar_base: crossbar base address
 * @register_offsets: offsets for each irq number
34
 * @write: register write function pointer
35 36
 */
struct crossbar_device {
37
	raw_spinlock_t lock;
38
	uint int_max;
39
	uint safe_map;
40
	uint max_crossbar_sources;
41 42 43
	uint *irq_map;
	void __iomem *crossbar_base;
	int *register_offsets;
44
	void (*write)(int, int);
45 46 47 48
};

static struct crossbar_device *cb;

49
static void crossbar_writel(int irq_no, int cb_no)
50 51 52 53
{
	writel(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
}

54
static void crossbar_writew(int irq_no, int cb_no)
55 56 57 58
{
	writew(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
}

59
static void crossbar_writeb(int irq_no, int cb_no)
60 61 62 63
{
	writeb(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
}

64 65 66 67 68 69
static struct irq_chip crossbar_chip = {
	.name			= "CBAR",
	.irq_eoi		= irq_chip_eoi_parent,
	.irq_mask		= irq_chip_mask_parent,
	.irq_unmask		= irq_chip_unmask_parent,
	.irq_retrigger		= irq_chip_retrigger_hierarchy,
70
	.irq_set_type		= irq_chip_set_type_parent,
71 72
	.flags			= IRQCHIP_MASK_ON_SUSPEND |
				  IRQCHIP_SKIP_SET_WAKE,
73 74 75 76
#ifdef CONFIG_SMP
	.irq_set_affinity	= irq_chip_set_affinity_parent,
#endif
};
77

78 79
static int allocate_gic_irq(struct irq_domain *domain, unsigned virq,
			    irq_hw_number_t hwirq)
80
{
81
	struct irq_fwspec fwspec;
82
	int i;
83
	int err;
84

85 86 87
	if (!irq_domain_get_of_node(domain->parent))
		return -EINVAL;

88
	raw_spin_lock(&cb->lock);
89
	for (i = cb->int_max - 1; i >= 0; i--) {
90
		if (cb->irq_map[i] == IRQ_FREE) {
91 92
			cb->irq_map[i] = hwirq;
			break;
93 94
		}
	}
95
	raw_spin_unlock(&cb->lock);
96

97 98
	if (i < 0)
		return -ENODEV;
99

100 101 102 103 104
	fwspec.fwnode = domain->parent->fwnode;
	fwspec.param_count = 3;
	fwspec.param[0] = 0;	/* SPI */
	fwspec.param[1] = i;
	fwspec.param[2] = IRQ_TYPE_LEVEL_HIGH;
105

106
	err = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
107 108 109 110
	if (err)
		cb->irq_map[i] = IRQ_FREE;
	else
		cb->write(i, hwirq);
111

112
	return err;
113 114
}

115 116
static int crossbar_domain_alloc(struct irq_domain *d, unsigned int virq,
				 unsigned int nr_irqs, void *data)
117
{
118
	struct irq_fwspec *fwspec = data;
119 120 121
	irq_hw_number_t hwirq;
	int i;

122
	if (fwspec->param_count != 3)
123
		return -EINVAL;	/* Not GIC compliant */
124
	if (fwspec->param[0] != 0)
125 126
		return -EINVAL;	/* No PPI should point to this domain */

127
	hwirq = fwspec->param[1];
128 129 130 131 132 133 134 135 136 137 138 139
	if ((hwirq + nr_irqs) > cb->max_crossbar_sources)
		return -EINVAL;	/* Can't deal with this */

	for (i = 0; i < nr_irqs; i++) {
		int err = allocate_gic_irq(d, virq + i, hwirq + i);

		if (err)
			return err;

		irq_domain_set_hwirq_and_chip(d, virq + i, hwirq + i,
					      &crossbar_chip, NULL);
	}
140

141 142 143
	return 0;
}

144
/**
145 146 147 148
 * crossbar_domain_free - unmap/free a crossbar<->irq connection
 * @domain: domain of irq to unmap
 * @virq: virq number
 * @nr_irqs: number of irqs to free
149 150 151 152 153 154 155
 *
 * We do not maintain a use count of total number of map/unmap
 * calls for a particular irq to find out if a irq can be really
 * unmapped. This is because unmap is called during irq_dispose_mapping(irq),
 * after which irq is anyways unusable. So an explicit map has to be called
 * after that.
 */
156 157
static void crossbar_domain_free(struct irq_domain *domain, unsigned int virq,
				 unsigned int nr_irqs)
158
{
159
	int i;
160

161 162 163 164 165 166 167
	raw_spin_lock(&cb->lock);
	for (i = 0; i < nr_irqs; i++) {
		struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);

		irq_domain_reset_irq_data(d);
		cb->irq_map[d->hwirq] = IRQ_FREE;
		cb->write(d->hwirq, cb->safe_map);
168
	}
169
	raw_spin_unlock(&cb->lock);
170 171
}

172 173 174 175
static int crossbar_domain_translate(struct irq_domain *d,
				     struct irq_fwspec *fwspec,
				     unsigned long *hwirq,
				     unsigned int *type)
176
{
177 178 179
	if (is_of_node(fwspec->fwnode)) {
		if (fwspec->param_count != 3)
			return -EINVAL;
180

181 182 183 184 185
		/* No PPI should point to this domain */
		if (fwspec->param[0] != 0)
			return -EINVAL;

		*hwirq = fwspec->param[1];
186
		*type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
187 188 189 190
		return 0;
	}

	return -EINVAL;
191 192
}

193
static const struct irq_domain_ops crossbar_domain_ops = {
194 195 196
	.alloc		= crossbar_domain_alloc,
	.free		= crossbar_domain_free,
	.translate	= crossbar_domain_translate,
197 198 199 200
};

static int __init crossbar_of_init(struct device_node *node)
{
201 202
	int i, size, reserved = 0;
	u32 max = 0, entry;
203
	const __be32 *irqsr;
204
	int ret = -ENOMEM;
205

206
	cb = kzalloc(sizeof(*cb), GFP_KERNEL);
207 208

	if (!cb)
209
		return ret;
210 211 212

	cb->crossbar_base = of_iomap(node, 0);
	if (!cb->crossbar_base)
213
		goto err_cb;
214

215 216 217 218 219 220 221 222
	of_property_read_u32(node, "ti,max-crossbar-sources",
			     &cb->max_crossbar_sources);
	if (!cb->max_crossbar_sources) {
		pr_err("missing 'ti,max-crossbar-sources' property\n");
		ret = -EINVAL;
		goto err_base;
	}

223
	of_property_read_u32(node, "ti,max-irqs", &max);
224 225 226
	if (!max) {
		pr_err("missing 'ti,max-irqs' property\n");
		ret = -EINVAL;
227
		goto err_base;
228
	}
229
	cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL);
230
	if (!cb->irq_map)
231
		goto err_base;
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

	cb->int_max = max;

	for (i = 0; i < max; i++)
		cb->irq_map[i] = IRQ_FREE;

	/* Get and mark reserved irqs */
	irqsr = of_get_property(node, "ti,irqs-reserved", &size);
	if (irqsr) {
		size /= sizeof(__be32);

		for (i = 0; i < size; i++) {
			of_property_read_u32_index(node,
						   "ti,irqs-reserved",
						   i, &entry);
247
			if (entry >= max) {
248
				pr_err("Invalid reserved entry\n");
249
				ret = -EINVAL;
250
				goto err_irq_map;
251
			}
252
			cb->irq_map[entry] = IRQ_RESERVED;
253 254 255
		}
	}

256 257 258 259 260 261 262 263 264
	/* Skip irqs hardwired to bypass the crossbar */
	irqsr = of_get_property(node, "ti,irqs-skip", &size);
	if (irqsr) {
		size /= sizeof(__be32);

		for (i = 0; i < size; i++) {
			of_property_read_u32_index(node,
						   "ti,irqs-skip",
						   i, &entry);
265
			if (entry >= max) {
266 267
				pr_err("Invalid skip entry\n");
				ret = -EINVAL;
268
				goto err_irq_map;
269 270 271 272 273 274
			}
			cb->irq_map[entry] = IRQ_SKIP;
		}
	}


275
	cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL);
276
	if (!cb->register_offsets)
277
		goto err_irq_map;
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

	of_property_read_u32(node, "ti,reg-size", &size);

	switch (size) {
	case 1:
		cb->write = crossbar_writeb;
		break;
	case 2:
		cb->write = crossbar_writew;
		break;
	case 4:
		cb->write = crossbar_writel;
		break;
	default:
		pr_err("Invalid reg-size property\n");
293
		ret = -EINVAL;
294
		goto err_reg_offset;
295 296 297 298 299 300 301 302
		break;
	}

	/*
	 * Register offsets are not linear because of the
	 * reserved irqs. so find and store the offsets once.
	 */
	for (i = 0; i < max; i++) {
303
		if (cb->irq_map[i] == IRQ_RESERVED)
304 305 306 307 308 309
			continue;

		cb->register_offsets[i] = reserved;
		reserved += size;
	}

310 311 312 313 314 315 316 317 318 319
	of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map);
	/* Initialize the crossbar with safe map to start with */
	for (i = 0; i < max; i++) {
		if (cb->irq_map[i] == IRQ_RESERVED ||
		    cb->irq_map[i] == IRQ_SKIP)
			continue;

		cb->write(i, cb->safe_map);
	}

320 321
	raw_spin_lock_init(&cb->lock);

322 323
	return 0;

324
err_reg_offset:
325
	kfree(cb->register_offsets);
326
err_irq_map:
327
	kfree(cb->irq_map);
328
err_base:
329
	iounmap(cb->crossbar_base);
330
err_cb:
331
	kfree(cb);
332 333

	cb = NULL;
334
	return ret;
335 336
}

337 338
static int __init irqcrossbar_init(struct device_node *node,
				   struct device_node *parent)
339
{
340 341 342 343 344
	struct irq_domain *parent_domain, *domain;
	int err;

	if (!parent) {
		pr_err("%s: no parent, giving up\n", node->full_name);
345
		return -ENODEV;
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
	}

	parent_domain = irq_find_host(parent);
	if (!parent_domain) {
		pr_err("%s: unable to obtain parent domain\n", node->full_name);
		return -ENXIO;
	}

	err = crossbar_of_init(node);
	if (err)
		return err;

	domain = irq_domain_add_hierarchy(parent_domain, 0,
					  cb->max_crossbar_sources,
					  node, &crossbar_domain_ops,
					  NULL);
	if (!domain) {
		pr_err("%s: failed to allocated domain\n", node->full_name);
		return -ENOMEM;
	}
366 367 368

	return 0;
}
369 370

IRQCHIP_DECLARE(ti_irqcrossbar, "ti,irq-crossbar", irqcrossbar_init);