Path: blob/master/arch/sh/boards/mach-x3proto/ilsel.c
15126 views
/*1* arch/sh/boards/mach-x3proto/ilsel.c2*3* Helper routines for SH-X3 proto board ILSEL.4*5* Copyright (C) 2007 - 2010 Paul Mundt6*7* This file is subject to the terms and conditions of the GNU General Public8* License. See the file "COPYING" in the main directory of this archive9* for more details.10*/11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt1213#include <linux/init.h>14#include <linux/kernel.h>15#include <linux/module.h>16#include <linux/bitmap.h>17#include <linux/io.h>18#include <mach/ilsel.h>1920/*21* ILSEL is split across:22*23* ILSEL0 - 0xb8100004 [ Levels 1 - 4 ]24* ILSEL1 - 0xb8100006 [ Levels 5 - 8 ]25* ILSEL2 - 0xb8100008 [ Levels 9 - 12 ]26* ILSEL3 - 0xb810000a [ Levels 13 - 15 ]27*28* With each level being relative to an ilsel_source_t.29*/30#define ILSEL_BASE 0xb810000431#define ILSEL_LEVELS 153233/*34* ILSEL level map, in descending order from the highest level down.35*36* Supported levels are 1 - 15 spread across ILSEL0 - ILSEL4, mapping37* directly to IRLs. As the IRQs are numbered in reverse order relative38* to the interrupt level, the level map is carefully managed to ensure a39* 1:1 mapping between the bit position and the IRQ number.40*41* This careful constructions allows ilsel_enable*() to be referenced42* directly for hooking up an ILSEL set and getting back an IRQ which can43* subsequently be used for internal accounting in the (optional) disable44* path.45*/46static unsigned long ilsel_level_map;4748static inline unsigned int ilsel_offset(unsigned int bit)49{50return ILSEL_LEVELS - bit - 1;51}5253static inline unsigned long mk_ilsel_addr(unsigned int bit)54{55return ILSEL_BASE + ((ilsel_offset(bit) >> 1) & ~0x1);56}5758static inline unsigned int mk_ilsel_shift(unsigned int bit)59{60return (ilsel_offset(bit) & 0x3) << 2;61}6263static void __ilsel_enable(ilsel_source_t set, unsigned int bit)64{65unsigned int tmp, shift;66unsigned long addr;6768pr_notice("enabling ILSEL set %d\n", set);6970addr = mk_ilsel_addr(bit);71shift = mk_ilsel_shift(bit);7273pr_debug("%s: bit#%d: addr - 0x%08lx (shift %d, set %d)\n",74__func__, bit, addr, shift, set);7576tmp = __raw_readw(addr);77tmp &= ~(0xf << shift);78tmp |= set << shift;79__raw_writew(tmp, addr);80}8182/**83* ilsel_enable - Enable an ILSEL set.84* @set: ILSEL source (see ilsel_source_t enum in include/asm-sh/ilsel.h).85*86* Enables a given non-aliased ILSEL source (<= ILSEL_KEY) at the highest87* available interrupt level. Callers should take care to order callsites88* noting descending interrupt levels. Aliasing FPGA and external board89* IRQs need to use ilsel_enable_fixed().90*91* The return value is an IRQ number that can later be taken down with92* ilsel_disable().93*/94int ilsel_enable(ilsel_source_t set)95{96unsigned int bit;9798if (unlikely(set > ILSEL_KEY)) {99pr_err("Aliased sources must use ilsel_enable_fixed()\n");100return -EINVAL;101}102103do {104bit = find_first_zero_bit(&ilsel_level_map, ILSEL_LEVELS);105} while (test_and_set_bit(bit, &ilsel_level_map));106107__ilsel_enable(set, bit);108109return bit;110}111EXPORT_SYMBOL_GPL(ilsel_enable);112113/**114* ilsel_enable_fixed - Enable an ILSEL set at a fixed interrupt level115* @set: ILSEL source (see ilsel_source_t enum in include/asm-sh/ilsel.h).116* @level: Interrupt level (1 - 15)117*118* Enables a given ILSEL source at a fixed interrupt level. Necessary119* both for level reservation as well as for aliased sources that only120* exist on special ILSEL#s.121*122* Returns an IRQ number (as ilsel_enable()).123*/124int ilsel_enable_fixed(ilsel_source_t set, unsigned int level)125{126unsigned int bit = ilsel_offset(level - 1);127128if (test_and_set_bit(bit, &ilsel_level_map))129return -EBUSY;130131__ilsel_enable(set, bit);132133return bit;134}135EXPORT_SYMBOL_GPL(ilsel_enable_fixed);136137/**138* ilsel_disable - Disable an ILSEL set139* @irq: Bit position for ILSEL set value (retval from enable routines)140*141* Disable a previously enabled ILSEL set.142*/143void ilsel_disable(unsigned int irq)144{145unsigned long addr;146unsigned int tmp;147148pr_notice("disabling ILSEL set %d\n", irq);149150addr = mk_ilsel_addr(irq);151152tmp = __raw_readw(addr);153tmp &= ~(0xf << mk_ilsel_shift(irq));154__raw_writew(tmp, addr);155156clear_bit(irq, &ilsel_level_map);157}158EXPORT_SYMBOL_GPL(ilsel_disable);159160161