uic.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * arch/powerpc/sysdev/uic.c
  3. *
  4. * IBM PowerPC 4xx Universal Interrupt Controller
  5. *
  6. * Copyright 2007 David Gibson <dwg@au1.ibm.com>, IBM Corporation.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/errno.h>
  16. #include <linux/reboot.h>
  17. #include <linux/slab.h>
  18. #include <linux/stddef.h>
  19. #include <linux/sched.h>
  20. #include <linux/signal.h>
  21. #include <linux/device.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/irq.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/kernel_stat.h>
  26. #include <asm/irq.h>
  27. #include <asm/io.h>
  28. #include <asm/prom.h>
  29. #include <asm/dcr.h>
  30. #define NR_UIC_INTS 32
  31. #define UIC_SR 0x0
  32. #define UIC_ER 0x2
  33. #define UIC_CR 0x3
  34. #define UIC_PR 0x4
  35. #define UIC_TR 0x5
  36. #define UIC_MSR 0x6
  37. #define UIC_VR 0x7
  38. #define UIC_VCR 0x8
  39. struct uic *primary_uic;
  40. struct uic {
  41. int index;
  42. int dcrbase;
  43. raw_spinlock_t lock;
  44. /* The remapper for this UIC */
  45. struct irq_domain *irqhost;
  46. };
  47. static void uic_unmask_irq(struct irq_data *d)
  48. {
  49. struct uic *uic = irq_data_get_irq_chip_data(d);
  50. unsigned int src = irqd_to_hwirq(d);
  51. unsigned long flags;
  52. u32 er, sr;
  53. sr = 1 << (31-src);
  54. raw_spin_lock_irqsave(&uic->lock, flags);
  55. /* ack level-triggered interrupts here */
  56. if (irqd_is_level_type(d))
  57. mtdcr(uic->dcrbase + UIC_SR, sr);
  58. er = mfdcr(uic->dcrbase + UIC_ER);
  59. er |= sr;
  60. mtdcr(uic->dcrbase + UIC_ER, er);
  61. raw_spin_unlock_irqrestore(&uic->lock, flags);
  62. }
  63. static void uic_mask_irq(struct irq_data *d)
  64. {
  65. struct uic *uic = irq_data_get_irq_chip_data(d);
  66. unsigned int src = irqd_to_hwirq(d);
  67. unsigned long flags;
  68. u32 er;
  69. raw_spin_lock_irqsave(&uic->lock, flags);
  70. er = mfdcr(uic->dcrbase + UIC_ER);
  71. er &= ~(1 << (31 - src));
  72. mtdcr(uic->dcrbase + UIC_ER, er);
  73. raw_spin_unlock_irqrestore(&uic->lock, flags);
  74. }
  75. static void uic_ack_irq(struct irq_data *d)
  76. {
  77. struct uic *uic = irq_data_get_irq_chip_data(d);
  78. unsigned int src = irqd_to_hwirq(d);
  79. unsigned long flags;
  80. raw_spin_lock_irqsave(&uic->lock, flags);
  81. mtdcr(uic->dcrbase + UIC_SR, 1 << (31-src));
  82. raw_spin_unlock_irqrestore(&uic->lock, flags);
  83. }
  84. static void uic_mask_ack_irq(struct irq_data *d)
  85. {
  86. struct uic *uic = irq_data_get_irq_chip_data(d);
  87. unsigned int src = irqd_to_hwirq(d);
  88. unsigned long flags;
  89. u32 er, sr;
  90. sr = 1 << (31-src);
  91. raw_spin_lock_irqsave(&uic->lock, flags);
  92. er = mfdcr(uic->dcrbase + UIC_ER);
  93. er &= ~sr;
  94. mtdcr(uic->dcrbase + UIC_ER, er);
  95. /* On the UIC, acking (i.e. clearing the SR bit)
  96. * a level irq will have no effect if the interrupt
  97. * is still asserted by the device, even if
  98. * the interrupt is already masked. Therefore
  99. * we only ack the egde interrupts here, while
  100. * level interrupts are ack'ed after the actual
  101. * isr call in the uic_unmask_irq()
  102. */
  103. if (!irqd_is_level_type(d))
  104. mtdcr(uic->dcrbase + UIC_SR, sr);
  105. raw_spin_unlock_irqrestore(&uic->lock, flags);
  106. }
  107. static int uic_set_irq_type(struct irq_data *d, unsigned int flow_type)
  108. {
  109. struct uic *uic = irq_data_get_irq_chip_data(d);
  110. unsigned int src = irqd_to_hwirq(d);
  111. unsigned long flags;
  112. int trigger, polarity;
  113. u32 tr, pr, mask;
  114. switch (flow_type & IRQ_TYPE_SENSE_MASK) {
  115. case IRQ_TYPE_NONE:
  116. uic_mask_irq(d);
  117. return 0;
  118. case IRQ_TYPE_EDGE_RISING:
  119. trigger = 1; polarity = 1;
  120. break;
  121. case IRQ_TYPE_EDGE_FALLING:
  122. trigger = 1; polarity = 0;
  123. break;
  124. case IRQ_TYPE_LEVEL_HIGH:
  125. trigger = 0; polarity = 1;
  126. break;
  127. case IRQ_TYPE_LEVEL_LOW:
  128. trigger = 0; polarity = 0;
  129. break;
  130. default:
  131. return -EINVAL;
  132. }
  133. mask = ~(1 << (31 - src));
  134. raw_spin_lock_irqsave(&uic->lock, flags);
  135. tr = mfdcr(uic->dcrbase + UIC_TR);
  136. pr = mfdcr(uic->dcrbase + UIC_PR);
  137. tr = (tr & mask) | (trigger << (31-src));
  138. pr = (pr & mask) | (polarity << (31-src));
  139. mtdcr(uic->dcrbase + UIC_PR, pr);
  140. mtdcr(uic->dcrbase + UIC_TR, tr);
  141. raw_spin_unlock_irqrestore(&uic->lock, flags);
  142. return 0;
  143. }
  144. static struct irq_chip uic_irq_chip = {
  145. .name = "UIC",
  146. .irq_unmask = uic_unmask_irq,
  147. .irq_mask = uic_mask_irq,
  148. .irq_mask_ack = uic_mask_ack_irq,
  149. .irq_ack = uic_ack_irq,
  150. .irq_set_type = uic_set_irq_type,
  151. };
  152. static int uic_host_map(struct irq_domain *h, unsigned int virq,
  153. irq_hw_number_t hw)
  154. {
  155. struct uic *uic = h->host_data;
  156. irq_set_chip_data(virq, uic);
  157. /* Despite the name, handle_level_irq() works for both level
  158. * and edge irqs on UIC. FIXME: check this is correct */
  159. irq_set_chip_and_handler(virq, &uic_irq_chip, handle_level_irq);
  160. /* Set default irq type */
  161. irq_set_irq_type(virq, IRQ_TYPE_NONE);
  162. return 0;
  163. }
  164. static const struct irq_domain_ops uic_host_ops = {
  165. .map = uic_host_map,
  166. .xlate = irq_domain_xlate_twocell,
  167. };
  168. static void uic_irq_cascade(struct irq_desc *desc)
  169. {
  170. struct irq_chip *chip = irq_desc_get_chip(desc);
  171. struct irq_data *idata = irq_desc_get_irq_data(desc);
  172. struct uic *uic = irq_desc_get_handler_data(desc);
  173. u32 msr;
  174. int src;
  175. int subvirq;
  176. raw_spin_lock(&desc->lock);
  177. if (irqd_is_level_type(idata))
  178. chip->irq_mask(idata);
  179. else
  180. chip->irq_mask_ack(idata);
  181. raw_spin_unlock(&desc->lock);
  182. msr = mfdcr(uic->dcrbase + UIC_MSR);
  183. if (!msr) /* spurious interrupt */
  184. goto uic_irq_ret;
  185. src = 32 - ffs(msr);
  186. subvirq = irq_linear_revmap(uic->irqhost, src);
  187. generic_handle_irq(subvirq);
  188. uic_irq_ret:
  189. raw_spin_lock(&desc->lock);
  190. if (irqd_is_level_type(idata))
  191. chip->irq_ack(idata);
  192. if (!irqd_irq_disabled(idata) && chip->irq_unmask)
  193. chip->irq_unmask(idata);
  194. raw_spin_unlock(&desc->lock);
  195. }
  196. static struct uic * __init uic_init_one(struct device_node *node)
  197. {
  198. struct uic *uic;
  199. const u32 *indexp, *dcrreg;
  200. int len;
  201. BUG_ON(! of_device_is_compatible(node, "ibm,uic"));
  202. uic = kzalloc(sizeof(*uic), GFP_KERNEL);
  203. if (! uic)
  204. return NULL; /* FIXME: panic? */
  205. raw_spin_lock_init(&uic->lock);
  206. indexp = of_get_property(node, "cell-index", &len);
  207. if (!indexp || (len != sizeof(u32))) {
  208. printk(KERN_ERR "uic: Device node %s has missing or invalid "
  209. "cell-index property\n", node->full_name);
  210. return NULL;
  211. }
  212. uic->index = *indexp;
  213. dcrreg = of_get_property(node, "dcr-reg", &len);
  214. if (!dcrreg || (len != 2*sizeof(u32))) {
  215. printk(KERN_ERR "uic: Device node %s has missing or invalid "
  216. "dcr-reg property\n", node->full_name);
  217. return NULL;
  218. }
  219. uic->dcrbase = *dcrreg;
  220. uic->irqhost = irq_domain_add_linear(node, NR_UIC_INTS, &uic_host_ops,
  221. uic);
  222. if (! uic->irqhost)
  223. return NULL; /* FIXME: panic? */
  224. /* Start with all interrupts disabled, level and non-critical */
  225. mtdcr(uic->dcrbase + UIC_ER, 0);
  226. mtdcr(uic->dcrbase + UIC_CR, 0);
  227. mtdcr(uic->dcrbase + UIC_TR, 0);
  228. /* Clear any pending interrupts, in case the firmware left some */
  229. mtdcr(uic->dcrbase + UIC_SR, 0xffffffff);
  230. printk ("UIC%d (%d IRQ sources) at DCR 0x%x\n", uic->index,
  231. NR_UIC_INTS, uic->dcrbase);
  232. return uic;
  233. }
  234. void __init uic_init_tree(void)
  235. {
  236. struct device_node *np;
  237. struct uic *uic;
  238. const u32 *interrupts;
  239. /* First locate and initialize the top-level UIC */
  240. for_each_compatible_node(np, NULL, "ibm,uic") {
  241. interrupts = of_get_property(np, "interrupts", NULL);
  242. if (!interrupts)
  243. break;
  244. }
  245. BUG_ON(!np); /* uic_init_tree() assumes there's a UIC as the
  246. * top-level interrupt controller */
  247. primary_uic = uic_init_one(np);
  248. if (!primary_uic)
  249. panic("Unable to initialize primary UIC %s\n", np->full_name);
  250. irq_set_default_host(primary_uic->irqhost);
  251. of_node_put(np);
  252. /* The scan again for cascaded UICs */
  253. for_each_compatible_node(np, NULL, "ibm,uic") {
  254. interrupts = of_get_property(np, "interrupts", NULL);
  255. if (interrupts) {
  256. /* Secondary UIC */
  257. int cascade_virq;
  258. uic = uic_init_one(np);
  259. if (! uic)
  260. panic("Unable to initialize a secondary UIC %s\n",
  261. np->full_name);
  262. cascade_virq = irq_of_parse_and_map(np, 0);
  263. irq_set_handler_data(cascade_virq, uic);
  264. irq_set_chained_handler(cascade_virq, uic_irq_cascade);
  265. /* FIXME: setup critical cascade?? */
  266. }
  267. }
  268. }
  269. /* Return an interrupt vector or 0 if no interrupt is pending. */
  270. unsigned int uic_get_irq(void)
  271. {
  272. u32 msr;
  273. int src;
  274. BUG_ON(! primary_uic);
  275. msr = mfdcr(primary_uic->dcrbase + UIC_MSR);
  276. src = 32 - ffs(msr);
  277. return irq_linear_revmap(primary_uic->irqhost, src);
  278. }