irq.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * arch/arm/mach-vt8500/irq.c
  3. *
  4. * Copyright (C) 2012 Tony Prisk <linux@prisktech.co.nz>
  5. * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /*
  22. * This file is copied and modified from the original irq.c provided by
  23. * Alexey Charkov. Minor changes have been made for Device Tree Support.
  24. */
  25. #include <linux/slab.h>
  26. #include <linux/io.h>
  27. #include <linux/irq.h>
  28. #include <linux/irqdomain.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/bitops.h>
  31. #include <linux/of.h>
  32. #include <linux/of_irq.h>
  33. #include <linux/of_address.h>
  34. #include <asm/irq.h>
  35. #define VT8500_ICPC_IRQ 0x20
  36. #define VT8500_ICPC_FIQ 0x24
  37. #define VT8500_ICDC 0x40 /* Destination Control 64*u32 */
  38. #define VT8500_ICIS 0x80 /* Interrupt status, 16*u32 */
  39. /* ICPC */
  40. #define ICPC_MASK 0x3F
  41. #define ICPC_ROTATE BIT(6)
  42. /* IC_DCTR */
  43. #define ICDC_IRQ 0x00
  44. #define ICDC_FIQ 0x01
  45. #define ICDC_DSS0 0x02
  46. #define ICDC_DSS1 0x03
  47. #define ICDC_DSS2 0x04
  48. #define ICDC_DSS3 0x05
  49. #define ICDC_DSS4 0x06
  50. #define ICDC_DSS5 0x07
  51. #define VT8500_INT_DISABLE 0
  52. #define VT8500_INT_ENABLE BIT(3)
  53. #define VT8500_TRIGGER_HIGH 0
  54. #define VT8500_TRIGGER_RISING BIT(5)
  55. #define VT8500_TRIGGER_FALLING BIT(6)
  56. #define VT8500_EDGE ( VT8500_TRIGGER_RISING \
  57. | VT8500_TRIGGER_FALLING)
  58. static int irq_cnt;
  59. struct vt8500_irq_priv {
  60. void __iomem *base;
  61. };
  62. static void vt8500_irq_mask(struct irq_data *d)
  63. {
  64. struct vt8500_irq_priv *priv =
  65. (struct vt8500_irq_priv *)(d->domain->host_data);
  66. void __iomem *base = priv->base;
  67. u8 edge;
  68. edge = readb(base + VT8500_ICDC + d->hwirq) & VT8500_EDGE;
  69. if (edge) {
  70. void __iomem *stat_reg = base + VT8500_ICIS
  71. + (d->hwirq < 32 ? 0 : 4);
  72. unsigned status = readl(stat_reg);
  73. status |= (1 << (d->hwirq & 0x1f));
  74. writel(status, stat_reg);
  75. } else {
  76. u8 dctr = readb(base + VT8500_ICDC + d->hwirq);
  77. dctr &= ~VT8500_INT_ENABLE;
  78. writeb(dctr, base + VT8500_ICDC + d->hwirq);
  79. }
  80. }
  81. static void vt8500_irq_unmask(struct irq_data *d)
  82. {
  83. struct vt8500_irq_priv *priv =
  84. (struct vt8500_irq_priv *)(d->domain->host_data);
  85. void __iomem *base = priv->base;
  86. u8 dctr;
  87. dctr = readb(base + VT8500_ICDC + d->hwirq);
  88. dctr |= VT8500_INT_ENABLE;
  89. writeb(dctr, base + VT8500_ICDC + d->hwirq);
  90. }
  91. static int vt8500_irq_set_type(struct irq_data *d, unsigned int flow_type)
  92. {
  93. struct vt8500_irq_priv *priv =
  94. (struct vt8500_irq_priv *)(d->domain->host_data);
  95. void __iomem *base = priv->base;
  96. u8 dctr;
  97. dctr = readb(base + VT8500_ICDC + d->hwirq);
  98. dctr &= ~VT8500_EDGE;
  99. switch (flow_type) {
  100. case IRQF_TRIGGER_LOW:
  101. return -EINVAL;
  102. case IRQF_TRIGGER_HIGH:
  103. dctr |= VT8500_TRIGGER_HIGH;
  104. __irq_set_handler_locked(d->irq, handle_level_irq);
  105. break;
  106. case IRQF_TRIGGER_FALLING:
  107. dctr |= VT8500_TRIGGER_FALLING;
  108. __irq_set_handler_locked(d->irq, handle_edge_irq);
  109. break;
  110. case IRQF_TRIGGER_RISING:
  111. dctr |= VT8500_TRIGGER_RISING;
  112. __irq_set_handler_locked(d->irq, handle_edge_irq);
  113. break;
  114. }
  115. writeb(dctr, base + VT8500_ICDC + d->hwirq);
  116. return 0;
  117. }
  118. static struct irq_chip vt8500_irq_chip = {
  119. .name = "vt8500",
  120. .irq_ack = vt8500_irq_mask,
  121. .irq_mask = vt8500_irq_mask,
  122. .irq_unmask = vt8500_irq_unmask,
  123. .irq_set_type = vt8500_irq_set_type,
  124. };
  125. static void __init vt8500_init_irq_hw(void __iomem *base)
  126. {
  127. unsigned int i;
  128. /* Enable rotating priority for IRQ */
  129. writel(ICPC_ROTATE, base + VT8500_ICPC_IRQ);
  130. writel(0x00, base + VT8500_ICPC_FIQ);
  131. for (i = 0; i < 64; i++) {
  132. /* Disable all interrupts and route them to IRQ */
  133. writeb(VT8500_INT_DISABLE | ICDC_IRQ,
  134. base + VT8500_ICDC + i);
  135. }
  136. }
  137. static int vt8500_irq_map(struct irq_domain *h, unsigned int virq,
  138. irq_hw_number_t hw)
  139. {
  140. irq_set_chip_and_handler(virq, &vt8500_irq_chip, handle_level_irq);
  141. set_irq_flags(virq, IRQF_VALID);
  142. return 0;
  143. }
  144. static struct irq_domain_ops vt8500_irq_domain_ops = {
  145. .map = vt8500_irq_map,
  146. .xlate = irq_domain_xlate_onecell,
  147. };
  148. int __init vt8500_irq_init(struct device_node *node, struct device_node *parent)
  149. {
  150. struct irq_domain *vt8500_irq_domain;
  151. struct vt8500_irq_priv *priv;
  152. int irq, i;
  153. struct device_node *np = node;
  154. priv = kzalloc(sizeof(struct vt8500_irq_priv), GFP_KERNEL);
  155. priv->base = of_iomap(np, 0);
  156. vt8500_irq_domain = irq_domain_add_legacy(node, 64, irq_cnt, 0,
  157. &vt8500_irq_domain_ops, priv);
  158. if (!vt8500_irq_domain)
  159. pr_err("%s: Unable to add wmt irq domain!\n", __func__);
  160. irq_set_default_host(vt8500_irq_domain);
  161. vt8500_init_irq_hw(priv->base);
  162. pr_info("Added IRQ Controller @ %x [virq_base = %d]\n",
  163. (u32)(priv->base), irq_cnt);
  164. /* check if this is a slaved controller */
  165. if (of_irq_count(np) != 0) {
  166. /* check that we have the correct number of interrupts */
  167. if (of_irq_count(np) != 8) {
  168. pr_err("%s: Incorrect IRQ map for slave controller\n",
  169. __func__);
  170. return -EINVAL;
  171. }
  172. for (i = 0; i < 8; i++) {
  173. irq = irq_of_parse_and_map(np, i);
  174. enable_irq(irq);
  175. }
  176. pr_info("vt8500-irq: Enabled slave->parent interrupts\n");
  177. }
  178. irq_cnt += 64;
  179. return 0;
  180. }