ilsel.c 4.0 KB

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