irq-vt8500.c 6.5 KB

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