// SPDX-License-Identifier: GPL-2.01/*2* arch/sh/boards/mach-x3proto/ilsel.c3*4* Helper routines for SH-X3 proto board ILSEL.5*6* Copyright (C) 2007 - 2010 Paul Mundt7*/8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt910#include <linux/init.h>11#include <linux/kernel.h>12#include <linux/module.h>13#include <linux/bitmap.h>14#include <linux/io.h>15#include <mach/ilsel.h>1617/*18* ILSEL is split across:19*20* ILSEL0 - 0xb8100004 [ Levels 1 - 4 ]21* ILSEL1 - 0xb8100006 [ Levels 5 - 8 ]22* ILSEL2 - 0xb8100008 [ Levels 9 - 12 ]23* ILSEL3 - 0xb810000a [ Levels 13 - 15 ]24*25* With each level being relative to an ilsel_source_t.26*/27#define ILSEL_BASE 0xb810000428#define ILSEL_LEVELS 152930/*31* ILSEL level map, in descending order from the highest level down.32*33* Supported levels are 1 - 15 spread across ILSEL0 - ILSEL4, mapping34* directly to IRLs. As the IRQs are numbered in reverse order relative35* to the interrupt level, the level map is carefully managed to ensure a36* 1:1 mapping between the bit position and the IRQ number.37*38* This careful constructions allows ilsel_enable*() to be referenced39* directly for hooking up an ILSEL set and getting back an IRQ which can40* subsequently be used for internal accounting in the (optional) disable41* path.42*/43static unsigned long ilsel_level_map;4445static inline unsigned int ilsel_offset(unsigned int bit)46{47return ILSEL_LEVELS - bit - 1;48}4950static inline unsigned long mk_ilsel_addr(unsigned int bit)51{52return ILSEL_BASE + ((ilsel_offset(bit) >> 1) & ~0x1);53}5455static inline unsigned int mk_ilsel_shift(unsigned int bit)56{57return (ilsel_offset(bit) & 0x3) << 2;58}5960static void __ilsel_enable(ilsel_source_t set, unsigned int bit)61{62unsigned int tmp, shift;63unsigned long addr;6465pr_notice("enabling ILSEL set %d\n", set);6667addr = mk_ilsel_addr(bit);68shift = mk_ilsel_shift(bit);6970pr_debug("%s: bit#%d: addr - 0x%08lx (shift %d, set %d)\n",71__func__, bit, addr, shift, set);7273tmp = __raw_readw(addr);74tmp &= ~(0xf << shift);75tmp |= set << shift;76__raw_writew(tmp, addr);77}7879/**80* ilsel_enable - Enable an ILSEL set.81* @set: ILSEL source (see ilsel_source_t enum in include/asm-sh/ilsel.h).82*83* Enables a given non-aliased ILSEL source (<= ILSEL_KEY) at the highest84* available interrupt level. Callers should take care to order callsites85* noting descending interrupt levels. Aliasing FPGA and external board86* IRQs need to use ilsel_enable_fixed().87*88* The return value is an IRQ number that can later be taken down with89* ilsel_disable().90*/91int ilsel_enable(ilsel_source_t set)92{93unsigned int bit;9495if (unlikely(set > ILSEL_KEY)) {96pr_err("Aliased sources must use ilsel_enable_fixed()\n");97return -EINVAL;98}99100do {101bit = find_first_zero_bit(&ilsel_level_map, ILSEL_LEVELS);102} while (test_and_set_bit(bit, &ilsel_level_map));103104__ilsel_enable(set, bit);105106return bit;107}108EXPORT_SYMBOL_GPL(ilsel_enable);109110/**111* ilsel_enable_fixed - Enable an ILSEL set at a fixed interrupt level112* @set: ILSEL source (see ilsel_source_t enum in include/asm-sh/ilsel.h).113* @level: Interrupt level (1 - 15)114*115* Enables a given ILSEL source at a fixed interrupt level. Necessary116* both for level reservation as well as for aliased sources that only117* exist on special ILSEL#s.118*119* Returns an IRQ number (as ilsel_enable()).120*/121int ilsel_enable_fixed(ilsel_source_t set, unsigned int level)122{123unsigned int bit = ilsel_offset(level - 1);124125if (test_and_set_bit(bit, &ilsel_level_map))126return -EBUSY;127128__ilsel_enable(set, bit);129130return bit;131}132EXPORT_SYMBOL_GPL(ilsel_enable_fixed);133134/**135* ilsel_disable - Disable an ILSEL set136* @irq: Bit position for ILSEL set value (retval from enable routines)137*138* Disable a previously enabled ILSEL set.139*/140void ilsel_disable(unsigned int irq)141{142unsigned long addr;143unsigned int tmp;144145pr_notice("disabling ILSEL set %d\n", irq);146147addr = mk_ilsel_addr(irq);148149tmp = __raw_readw(addr);150tmp &= ~(0xf << mk_ilsel_shift(irq));151__raw_writew(tmp, addr);152153clear_bit(irq, &ilsel_level_map);154}155EXPORT_SYMBOL_GPL(ilsel_disable);156157158