18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Support for hardware-managed IRQ auto-distribution.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2010  Paul Mundt
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
78c2ecf20Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
88c2ecf20Sopenharmony_ci * for more details.
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci#include "internals.h"
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistatic unsigned long dist_handle[INTC_NR_IRQS];
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_civoid intc_balancing_enable(unsigned int irq)
158c2ecf20Sopenharmony_ci{
168c2ecf20Sopenharmony_ci	struct intc_desc_int *d = get_intc_desc(irq);
178c2ecf20Sopenharmony_ci	unsigned long handle = dist_handle[irq];
188c2ecf20Sopenharmony_ci	unsigned long addr;
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci	if (irq_balancing_disabled(irq) || !handle)
218c2ecf20Sopenharmony_ci		return;
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci	addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
248c2ecf20Sopenharmony_ci	intc_reg_fns[_INTC_FN(handle)](addr, handle, 1);
258c2ecf20Sopenharmony_ci}
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_civoid intc_balancing_disable(unsigned int irq)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	struct intc_desc_int *d = get_intc_desc(irq);
308c2ecf20Sopenharmony_ci	unsigned long handle = dist_handle[irq];
318c2ecf20Sopenharmony_ci	unsigned long addr;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	if (irq_balancing_disabled(irq) || !handle)
348c2ecf20Sopenharmony_ci		return;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
378c2ecf20Sopenharmony_ci	intc_reg_fns[_INTC_FN(handle)](addr, handle, 0);
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic unsigned int intc_dist_data(struct intc_desc *desc,
418c2ecf20Sopenharmony_ci				   struct intc_desc_int *d,
428c2ecf20Sopenharmony_ci				   intc_enum enum_id)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	struct intc_mask_reg *mr = desc->hw.mask_regs;
458c2ecf20Sopenharmony_ci	unsigned int i, j, fn, mode;
468c2ecf20Sopenharmony_ci	unsigned long reg_e, reg_d;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	for (i = 0; mr && enum_id && i < desc->hw.nr_mask_regs; i++) {
498c2ecf20Sopenharmony_ci		mr = desc->hw.mask_regs + i;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci		/*
528c2ecf20Sopenharmony_ci		 * Skip this entry if there's no auto-distribution
538c2ecf20Sopenharmony_ci		 * register associated with it.
548c2ecf20Sopenharmony_ci		 */
558c2ecf20Sopenharmony_ci		if (!mr->dist_reg)
568c2ecf20Sopenharmony_ci			continue;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci		for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
598c2ecf20Sopenharmony_ci			if (mr->enum_ids[j] != enum_id)
608c2ecf20Sopenharmony_ci				continue;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci			fn = REG_FN_MODIFY_BASE;
638c2ecf20Sopenharmony_ci			mode = MODE_ENABLE_REG;
648c2ecf20Sopenharmony_ci			reg_e = mr->dist_reg;
658c2ecf20Sopenharmony_ci			reg_d = mr->dist_reg;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci			fn += (mr->reg_width >> 3) - 1;
688c2ecf20Sopenharmony_ci			return _INTC_MK(fn, mode,
698c2ecf20Sopenharmony_ci					intc_get_reg(d, reg_e),
708c2ecf20Sopenharmony_ci					intc_get_reg(d, reg_d),
718c2ecf20Sopenharmony_ci					1,
728c2ecf20Sopenharmony_ci					(mr->reg_width - 1) - j);
738c2ecf20Sopenharmony_ci		}
748c2ecf20Sopenharmony_ci	}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	/*
778c2ecf20Sopenharmony_ci	 * It's possible we've gotten here with no distribution options
788c2ecf20Sopenharmony_ci	 * available for the IRQ in question, so we just skip over those.
798c2ecf20Sopenharmony_ci	 */
808c2ecf20Sopenharmony_ci	return 0;
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_civoid intc_set_dist_handle(unsigned int irq, struct intc_desc *desc,
848c2ecf20Sopenharmony_ci			  struct intc_desc_int *d, intc_enum id)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	unsigned long flags;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	/*
898c2ecf20Sopenharmony_ci	 * Nothing to do for this IRQ.
908c2ecf20Sopenharmony_ci	 */
918c2ecf20Sopenharmony_ci	if (!desc->hw.mask_regs)
928c2ecf20Sopenharmony_ci		return;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&intc_big_lock, flags);
958c2ecf20Sopenharmony_ci	dist_handle[irq] = intc_dist_data(desc, d, id);
968c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&intc_big_lock, flags);
978c2ecf20Sopenharmony_ci}
98