extint.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * External interrupt handling for AT32AP CPUs
  3. *
  4. * Copyright (C) 2006 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irq.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/random.h>
  16. #include <linux/slab.h>
  17. #include <asm/io.h>
  18. /* EIC register offsets */
  19. #define EIC_IER 0x0000
  20. #define EIC_IDR 0x0004
  21. #define EIC_IMR 0x0008
  22. #define EIC_ISR 0x000c
  23. #define EIC_ICR 0x0010
  24. #define EIC_MODE 0x0014
  25. #define EIC_EDGE 0x0018
  26. #define EIC_LEVEL 0x001c
  27. #define EIC_NMIC 0x0024
  28. /* Bitfields in NMIC */
  29. #define EIC_NMIC_ENABLE (1 << 0)
  30. /* Bit manipulation macros */
  31. #define EIC_BIT(name) \
  32. (1 << EIC_##name##_OFFSET)
  33. #define EIC_BF(name,value) \
  34. (((value) & ((1 << EIC_##name##_SIZE) - 1)) \
  35. << EIC_##name##_OFFSET)
  36. #define EIC_BFEXT(name,value) \
  37. (((value) >> EIC_##name##_OFFSET) \
  38. & ((1 << EIC_##name##_SIZE) - 1))
  39. #define EIC_BFINS(name,value,old) \
  40. (((old) & ~(((1 << EIC_##name##_SIZE) - 1) \
  41. << EIC_##name##_OFFSET)) \
  42. | EIC_BF(name,value))
  43. /* Register access macros */
  44. #define eic_readl(port,reg) \
  45. __raw_readl((port)->regs + EIC_##reg)
  46. #define eic_writel(port,reg,value) \
  47. __raw_writel((value), (port)->regs + EIC_##reg)
  48. struct eic {
  49. void __iomem *regs;
  50. struct irq_chip *chip;
  51. unsigned int first_irq;
  52. };
  53. static struct eic *nmi_eic;
  54. static bool nmi_enabled;
  55. static void eic_ack_irq(struct irq_data *d)
  56. {
  57. struct eic *eic = irq_data_get_irq_chip_data(d);
  58. eic_writel(eic, ICR, 1 << (d->irq - eic->first_irq));
  59. }
  60. static void eic_mask_irq(struct irq_data *d)
  61. {
  62. struct eic *eic = irq_data_get_irq_chip_data(d);
  63. eic_writel(eic, IDR, 1 << (d->irq - eic->first_irq));
  64. }
  65. static void eic_mask_ack_irq(struct irq_data *d)
  66. {
  67. struct eic *eic = irq_data_get_irq_chip_data(d);
  68. eic_writel(eic, ICR, 1 << (d->irq - eic->first_irq));
  69. eic_writel(eic, IDR, 1 << (d->irq - eic->first_irq));
  70. }
  71. static void eic_unmask_irq(struct irq_data *d)
  72. {
  73. struct eic *eic = irq_data_get_irq_chip_data(d);
  74. eic_writel(eic, IER, 1 << (d->irq - eic->first_irq));
  75. }
  76. static int eic_set_irq_type(struct irq_data *d, unsigned int flow_type)
  77. {
  78. struct eic *eic = irq_data_get_irq_chip_data(d);
  79. unsigned int irq = d->irq;
  80. unsigned int i = irq - eic->first_irq;
  81. u32 mode, edge, level;
  82. flow_type &= IRQ_TYPE_SENSE_MASK;
  83. if (flow_type == IRQ_TYPE_NONE)
  84. flow_type = IRQ_TYPE_LEVEL_LOW;
  85. mode = eic_readl(eic, MODE);
  86. edge = eic_readl(eic, EDGE);
  87. level = eic_readl(eic, LEVEL);
  88. switch (flow_type) {
  89. case IRQ_TYPE_LEVEL_LOW:
  90. mode |= 1 << i;
  91. level &= ~(1 << i);
  92. break;
  93. case IRQ_TYPE_LEVEL_HIGH:
  94. mode |= 1 << i;
  95. level |= 1 << i;
  96. break;
  97. case IRQ_TYPE_EDGE_RISING:
  98. mode &= ~(1 << i);
  99. edge |= 1 << i;
  100. break;
  101. case IRQ_TYPE_EDGE_FALLING:
  102. mode &= ~(1 << i);
  103. edge &= ~(1 << i);
  104. break;
  105. default:
  106. return -EINVAL;
  107. }
  108. eic_writel(eic, MODE, mode);
  109. eic_writel(eic, EDGE, edge);
  110. eic_writel(eic, LEVEL, level);
  111. irqd_set_trigger_type(d, flow_type);
  112. if (flow_type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  113. __irq_set_handler_locked(irq, handle_level_irq);
  114. else
  115. __irq_set_handler_locked(irq, handle_edge_irq);
  116. return IRQ_SET_MASK_OK_NOCOPY;
  117. }
  118. static struct irq_chip eic_chip = {
  119. .name = "eic",
  120. .irq_ack = eic_ack_irq,
  121. .irq_mask = eic_mask_irq,
  122. .irq_mask_ack = eic_mask_ack_irq,
  123. .irq_unmask = eic_unmask_irq,
  124. .irq_set_type = eic_set_irq_type,
  125. };
  126. static void demux_eic_irq(unsigned int irq, struct irq_desc *desc)
  127. {
  128. struct eic *eic = irq_desc_get_handler_data(desc);
  129. unsigned long status, pending;
  130. unsigned int i;
  131. status = eic_readl(eic, ISR);
  132. pending = status & eic_readl(eic, IMR);
  133. while (pending) {
  134. i = fls(pending) - 1;
  135. pending &= ~(1 << i);
  136. generic_handle_irq(i + eic->first_irq);
  137. }
  138. }
  139. int nmi_enable(void)
  140. {
  141. nmi_enabled = true;
  142. if (nmi_eic)
  143. eic_writel(nmi_eic, NMIC, EIC_NMIC_ENABLE);
  144. return 0;
  145. }
  146. void nmi_disable(void)
  147. {
  148. if (nmi_eic)
  149. eic_writel(nmi_eic, NMIC, 0);
  150. nmi_enabled = false;
  151. }
  152. static int __init eic_probe(struct platform_device *pdev)
  153. {
  154. struct eic *eic;
  155. struct resource *regs;
  156. unsigned int i;
  157. unsigned int nr_of_irqs;
  158. unsigned int int_irq;
  159. int ret;
  160. u32 pattern;
  161. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  162. int_irq = platform_get_irq(pdev, 0);
  163. if (!regs || (int)int_irq <= 0) {
  164. dev_dbg(&pdev->dev, "missing regs and/or irq resource\n");
  165. return -ENXIO;
  166. }
  167. ret = -ENOMEM;
  168. eic = kzalloc(sizeof(struct eic), GFP_KERNEL);
  169. if (!eic) {
  170. dev_dbg(&pdev->dev, "no memory for eic structure\n");
  171. goto err_kzalloc;
  172. }
  173. eic->first_irq = EIM_IRQ_BASE + 32 * pdev->id;
  174. eic->regs = ioremap(regs->start, resource_size(regs));
  175. if (!eic->regs) {
  176. dev_dbg(&pdev->dev, "failed to map regs\n");
  177. goto err_ioremap;
  178. }
  179. /*
  180. * Find out how many interrupt lines that are actually
  181. * implemented in hardware.
  182. */
  183. eic_writel(eic, IDR, ~0UL);
  184. eic_writel(eic, MODE, ~0UL);
  185. pattern = eic_readl(eic, MODE);
  186. nr_of_irqs = fls(pattern);
  187. /* Trigger on low level unless overridden by driver */
  188. eic_writel(eic, EDGE, 0UL);
  189. eic_writel(eic, LEVEL, 0UL);
  190. eic->chip = &eic_chip;
  191. for (i = 0; i < nr_of_irqs; i++) {
  192. irq_set_chip_and_handler(eic->first_irq + i, &eic_chip,
  193. handle_level_irq);
  194. irq_set_chip_data(eic->first_irq + i, eic);
  195. }
  196. irq_set_chained_handler_and_data(int_irq, demux_eic_irq, eic);
  197. if (pdev->id == 0) {
  198. nmi_eic = eic;
  199. if (nmi_enabled)
  200. /*
  201. * Someone tried to enable NMI before we were
  202. * ready. Do it now.
  203. */
  204. nmi_enable();
  205. }
  206. dev_info(&pdev->dev,
  207. "External Interrupt Controller at 0x%p, IRQ %u\n",
  208. eic->regs, int_irq);
  209. dev_info(&pdev->dev,
  210. "Handling %u external IRQs, starting with IRQ %u\n",
  211. nr_of_irqs, eic->first_irq);
  212. return 0;
  213. err_ioremap:
  214. kfree(eic);
  215. err_kzalloc:
  216. return ret;
  217. }
  218. static struct platform_driver eic_driver = {
  219. .driver = {
  220. .name = "at32_eic",
  221. },
  222. };
  223. static int __init eic_init(void)
  224. {
  225. return platform_driver_probe(&eic_driver, eic_probe);
  226. }
  227. arch_initcall(eic_init);