ilsel.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * arch/sh/boards/renesas/x3proto/ilsel.c
  3. *
  4. * Helper routines for SH-X3 proto board ILSEL.
  5. *
  6. * Copyright (C) 2007 Paul Mundt
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/bitmap.h>
  16. #include <linux/io.h>
  17. #include <asm/ilsel.h>
  18. /*
  19. * ILSEL is split across:
  20. *
  21. * ILSEL0 - 0xb8100004 [ Levels 1 - 4 ]
  22. * ILSEL1 - 0xb8100006 [ Levels 5 - 8 ]
  23. * ILSEL2 - 0xb8100008 [ Levels 9 - 12 ]
  24. * ILSEL3 - 0xb810000a [ Levels 13 - 15 ]
  25. *
  26. * With each level being relative to an ilsel_source_t.
  27. */
  28. #define ILSEL_BASE 0xb8100004
  29. #define ILSEL_LEVELS 15
  30. /*
  31. * ILSEL level map, in descending order from the highest level down.
  32. *
  33. * Supported levels are 1 - 15 spread across ILSEL0 - ILSEL4, mapping
  34. * directly to IRLs. As the IRQs are numbered in reverse order relative
  35. * to the interrupt level, the level map is carefully managed to ensure a
  36. * 1:1 mapping between the bit position and the IRQ number.
  37. *
  38. * This careful constructions allows ilsel_enable*() to be referenced
  39. * directly for hooking up an ILSEL set and getting back an IRQ which can
  40. * subsequently be used for internal accounting in the (optional) disable
  41. * path.
  42. */
  43. static unsigned long ilsel_level_map;
  44. static inline unsigned int ilsel_offset(unsigned int bit)
  45. {
  46. return ILSEL_LEVELS - bit - 1;
  47. }
  48. static inline unsigned long mk_ilsel_addr(unsigned int bit)
  49. {
  50. return ILSEL_BASE + ((ilsel_offset(bit) >> 1) & ~0x1);
  51. }
  52. static inline unsigned int mk_ilsel_shift(unsigned int bit)
  53. {
  54. return (ilsel_offset(bit) & 0x3) << 2;
  55. }
  56. static void __ilsel_enable(ilsel_source_t set, unsigned int bit)
  57. {
  58. unsigned int tmp, shift;
  59. unsigned long addr;
  60. addr = mk_ilsel_addr(bit);
  61. shift = mk_ilsel_shift(bit);
  62. pr_debug("%s: bit#%d: addr - 0x%08lx (shift %d, set %d)\n",
  63. __func__, bit, addr, shift, set);
  64. tmp = ctrl_inw(addr);
  65. tmp &= ~(0xf << shift);
  66. tmp |= set << shift;
  67. ctrl_outw(tmp, addr);
  68. }
  69. /**
  70. * ilsel_enable - Enable an ILSEL set.
  71. * @set: ILSEL source (see ilsel_source_t enum in include/asm-sh/ilsel.h).
  72. *
  73. * Enables a given non-aliased ILSEL source (<= ILSEL_KEY) at the highest
  74. * available interrupt level. Callers should take care to order callsites
  75. * noting descending interrupt levels. Aliasing FPGA and external board
  76. * IRQs need to use ilsel_enable_fixed().
  77. *
  78. * The return value is an IRQ number that can later be taken down with
  79. * ilsel_disable().
  80. */
  81. int ilsel_enable(ilsel_source_t set)
  82. {
  83. unsigned int bit;
  84. /* Aliased sources must use ilsel_enable_fixed() */
  85. BUG_ON(set > ILSEL_KEY);
  86. do {
  87. bit = find_first_zero_bit(&ilsel_level_map, ILSEL_LEVELS);
  88. } while (test_and_set_bit(bit, &ilsel_level_map));
  89. __ilsel_enable(set, bit);
  90. return bit;
  91. }
  92. EXPORT_SYMBOL_GPL(ilsel_enable);
  93. /**
  94. * ilsel_enable_fixed - Enable an ILSEL set at a fixed interrupt level
  95. * @set: ILSEL source (see ilsel_source_t enum in include/asm-sh/ilsel.h).
  96. * @level: Interrupt level (1 - 15)
  97. *
  98. * Enables a given ILSEL source at a fixed interrupt level. Necessary
  99. * both for level reservation as well as for aliased sources that only
  100. * exist on special ILSEL#s.
  101. *
  102. * Returns an IRQ number (as ilsel_enable()).
  103. */
  104. int ilsel_enable_fixed(ilsel_source_t set, unsigned int level)
  105. {
  106. unsigned int bit = ilsel_offset(level - 1);
  107. if (test_and_set_bit(bit, &ilsel_level_map))
  108. return -EBUSY;
  109. __ilsel_enable(set, bit);
  110. return bit;
  111. }
  112. EXPORT_SYMBOL_GPL(ilsel_enable_fixed);
  113. /**
  114. * ilsel_disable - Disable an ILSEL set
  115. * @irq: Bit position for ILSEL set value (retval from enable routines)
  116. *
  117. * Disable a previously enabled ILSEL set.
  118. */
  119. void ilsel_disable(unsigned int irq)
  120. {
  121. unsigned long addr;
  122. unsigned int tmp;
  123. addr = mk_ilsel_addr(irq);
  124. tmp = ctrl_inw(addr);
  125. tmp &= ~(0xf << mk_ilsel_shift(irq));
  126. ctrl_outw(tmp, addr);
  127. clear_bit(irq, &ilsel_level_map);
  128. }
  129. EXPORT_SYMBOL_GPL(ilsel_disable);