ilsel.c 4.1 KB

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