irq.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. #include <asm/exception.h>
  36. #define VT8500_ICPC_IRQ 0x20
  37. #define VT8500_ICPC_FIQ 0x24
  38. #define VT8500_ICDC 0x40 /* Destination Control 64*u32 */
  39. #define VT8500_ICIS 0x80 /* Interrupt status, 16*u32 */
  40. /* ICPC */
  41. #define ICPC_MASK 0x3F
  42. #define ICPC_ROTATE BIT(6)
  43. /* IC_DCTR */
  44. #define ICDC_IRQ 0x00
  45. #define ICDC_FIQ 0x01
  46. #define ICDC_DSS0 0x02
  47. #define ICDC_DSS1 0x03
  48. #define ICDC_DSS2 0x04
  49. #define ICDC_DSS3 0x05
  50. #define ICDC_DSS4 0x06
  51. #define ICDC_DSS5 0x07
  52. #define VT8500_INT_DISABLE 0
  53. #define VT8500_INT_ENABLE BIT(3)
  54. #define VT8500_TRIGGER_HIGH 0
  55. #define VT8500_TRIGGER_RISING BIT(5)
  56. #define VT8500_TRIGGER_FALLING BIT(6)
  57. #define VT8500_EDGE ( VT8500_TRIGGER_RISING \
  58. | VT8500_TRIGGER_FALLING)
  59. /* vt8500 has 1 intc, wm8505 and wm8650 have 2 */
  60. #define VT8500_INTC_MAX 2
  61. struct vt8500_irq_data {
  62. void __iomem *base; /* IO Memory base address */
  63. struct irq_domain *domain; /* Domain for this controller */
  64. };
  65. /* Global variable for accessing io-mem addresses */
  66. static struct vt8500_irq_data intc[VT8500_INTC_MAX];
  67. static u32 active_cnt = 0;
  68. static void vt8500_irq_mask(struct irq_data *d)
  69. {
  70. struct vt8500_irq_data *priv = d->domain->host_data;
  71. void __iomem *base = priv->base;
  72. void __iomem *stat_reg = base + VT8500_ICIS + (d->hwirq < 32 ? 0 : 4);
  73. u8 edge, dctr;
  74. u32 status;
  75. edge = readb(base + VT8500_ICDC + d->hwirq) & VT8500_EDGE;
  76. if (edge) {
  77. status = readl(stat_reg);
  78. status |= (1 << (d->hwirq & 0x1f));
  79. writel(status, stat_reg);
  80. } else {
  81. dctr = readb(base + VT8500_ICDC + d->hwirq);
  82. dctr &= ~VT8500_INT_ENABLE;
  83. writeb(dctr, base + VT8500_ICDC + d->hwirq);
  84. }
  85. }
  86. static void vt8500_irq_unmask(struct irq_data *d)
  87. {
  88. struct vt8500_irq_data *priv = d->domain->host_data;
  89. void __iomem *base = priv->base;
  90. u8 dctr;
  91. dctr = readb(base + VT8500_ICDC + d->hwirq);
  92. dctr |= VT8500_INT_ENABLE;
  93. writeb(dctr, base + VT8500_ICDC + d->hwirq);
  94. }
  95. static int vt8500_irq_set_type(struct irq_data *d, unsigned int flow_type)
  96. {
  97. struct vt8500_irq_data *priv = d->domain->host_data;
  98. void __iomem *base = priv->base;
  99. u8 dctr;
  100. dctr = readb(base + VT8500_ICDC + d->hwirq);
  101. dctr &= ~VT8500_EDGE;
  102. switch (flow_type) {
  103. case IRQF_TRIGGER_LOW:
  104. return -EINVAL;
  105. case IRQF_TRIGGER_HIGH:
  106. dctr |= VT8500_TRIGGER_HIGH;
  107. __irq_set_handler_locked(d->irq, handle_level_irq);
  108. break;
  109. case IRQF_TRIGGER_FALLING:
  110. dctr |= VT8500_TRIGGER_FALLING;
  111. __irq_set_handler_locked(d->irq, handle_edge_irq);
  112. break;
  113. case IRQF_TRIGGER_RISING:
  114. dctr |= VT8500_TRIGGER_RISING;
  115. __irq_set_handler_locked(d->irq, handle_edge_irq);
  116. break;
  117. }
  118. writeb(dctr, base + VT8500_ICDC + d->hwirq);
  119. return 0;
  120. }
  121. static struct irq_chip vt8500_irq_chip = {
  122. .name = "vt8500",
  123. .irq_ack = vt8500_irq_mask,
  124. .irq_mask = vt8500_irq_mask,
  125. .irq_unmask = vt8500_irq_unmask,
  126. .irq_set_type = vt8500_irq_set_type,
  127. };
  128. static void __init vt8500_init_irq_hw(void __iomem *base)
  129. {
  130. u32 i;
  131. /* Enable rotating priority for IRQ */
  132. writel(ICPC_ROTATE, base + VT8500_ICPC_IRQ);
  133. writel(0x00, base + VT8500_ICPC_FIQ);
  134. /* Disable all interrupts and route them to IRQ */
  135. for (i = 0; i < 64; i++)
  136. writeb(VT8500_INT_DISABLE | ICDC_IRQ, base + VT8500_ICDC + i);
  137. }
  138. static int vt8500_irq_map(struct irq_domain *h, unsigned int virq,
  139. irq_hw_number_t hw)
  140. {
  141. irq_set_chip_and_handler(virq, &vt8500_irq_chip, handle_level_irq);
  142. set_irq_flags(virq, IRQF_VALID);
  143. return 0;
  144. }
  145. static struct irq_domain_ops vt8500_irq_domain_ops = {
  146. .map = vt8500_irq_map,
  147. .xlate = irq_domain_xlate_onecell,
  148. };
  149. asmlinkage void __exception_irq_entry vt8500_handle_irq(struct pt_regs *regs)
  150. {
  151. u32 stat, i;
  152. int irqnr, virq;
  153. void __iomem *base;
  154. /* Loop through each active controller */
  155. for (i=0; i<active_cnt; i++) {
  156. base = intc[i].base;
  157. irqnr = readl_relaxed(base) & 0x3F;
  158. /*
  159. Highest Priority register default = 63, so check that this
  160. is a real interrupt by checking the status register
  161. */
  162. if (irqnr == 63) {
  163. stat = readl_relaxed(base + VT8500_ICIS + 4);
  164. if (!(stat & BIT(31)))
  165. continue;
  166. }
  167. virq = irq_find_mapping(intc[i].domain, irqnr);
  168. handle_IRQ(virq, regs);
  169. }
  170. }
  171. int __init vt8500_irq_init(struct device_node *node, struct device_node *parent)
  172. {
  173. int irq, i;
  174. struct device_node *np = node;
  175. if (active_cnt == VT8500_INTC_MAX) {
  176. pr_err("%s: Interrupt controllers > VT8500_INTC_MAX\n",
  177. __func__);
  178. goto out;
  179. }
  180. intc[active_cnt].base = of_iomap(np, 0);
  181. intc[active_cnt].domain = irq_domain_add_linear(node, 64,
  182. &vt8500_irq_domain_ops, &intc[active_cnt]);
  183. if (!intc[active_cnt].base) {
  184. pr_err("%s: Unable to map IO memory\n", __func__);
  185. goto out;
  186. }
  187. if (!intc[active_cnt].domain) {
  188. pr_err("%s: Unable to add irq domain!\n", __func__);
  189. goto out;
  190. }
  191. vt8500_init_irq_hw(intc[active_cnt].base);
  192. pr_info("vt8500-irq: Added interrupt controller\n");
  193. active_cnt++;
  194. /* check if this is a slaved controller */
  195. if (of_irq_count(np) != 0) {
  196. /* check that we have the correct number of interrupts */
  197. if (of_irq_count(np) != 8) {
  198. pr_err("%s: Incorrect IRQ map for slaved controller\n",
  199. __func__);
  200. return -EINVAL;
  201. }
  202. for (i = 0; i < 8; i++) {
  203. irq = irq_of_parse_and_map(np, i);
  204. enable_irq(irq);
  205. }
  206. pr_info("vt8500-irq: Enabled slave->parent interrupts\n");
  207. }
  208. out:
  209. return 0;
  210. }