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. mtdcr(uic->dcrbase + UIC_SR, ~mask);
  142. raw_spin_unlock_irqrestore(&uic->lock, flags);
  143. return 0;
  144. }
  145. static struct irq_chip uic_irq_chip = {
  146. .name = "UIC",
  147. .irq_unmask = uic_unmask_irq,
  148. .irq_mask = uic_mask_irq,
  149. .irq_mask_ack = uic_mask_ack_irq,
  150. .irq_ack = uic_ack_irq,
  151. .irq_set_type = uic_set_irq_type,
  152. };
  153. static int uic_host_map(struct irq_domain *h, unsigned int virq,
  154. irq_hw_number_t hw)
  155. {
  156. struct uic *uic = h->host_data;
  157. irq_set_chip_data(virq, uic);
  158. /* Despite the name, handle_level_irq() works for both level
  159. * and edge irqs on UIC. FIXME: check this is correct */
  160. irq_set_chip_and_handler(virq, &uic_irq_chip, handle_level_irq);
  161. /* Set default irq type */
  162. irq_set_irq_type(virq, IRQ_TYPE_NONE);
  163. return 0;
  164. }
  165. static const struct irq_domain_ops uic_host_ops = {
  166. .map = uic_host_map,
  167. .xlate = irq_domain_xlate_twocell,
  168. };
  169. static void uic_irq_cascade(struct irq_desc *desc)
  170. {
  171. struct irq_chip *chip = irq_desc_get_chip(desc);
  172. struct irq_data *idata = irq_desc_get_irq_data(desc);
  173. struct uic *uic = irq_desc_get_handler_data(desc);
  174. u32 msr;
  175. int src;
  176. int subvirq;
  177. raw_spin_lock(&desc->lock);
  178. if (irqd_is_level_type(idata))
  179. chip->irq_mask(idata);
  180. else
  181. chip->irq_mask_ack(idata);
  182. raw_spin_unlock(&desc->lock);
  183. msr = mfdcr(uic->dcrbase + UIC_MSR);
  184. if (!msr) /* spurious interrupt */
  185. goto uic_irq_ret;
  186. src = 32 - ffs(msr);
  187. subvirq = irq_linear_revmap(uic->irqhost, src);
  188. generic_handle_irq(subvirq);
  189. uic_irq_ret:
  190. raw_spin_lock(&desc->lock);
  191. if (irqd_is_level_type(idata))
  192. chip->irq_ack(idata);
  193. if (!irqd_irq_disabled(idata) && chip->irq_unmask)
  194. chip->irq_unmask(idata);
  195. raw_spin_unlock(&desc->lock);
  196. }
  197. static struct uic * __init uic_init_one(struct device_node *node)
  198. {
  199. struct uic *uic;
  200. const u32 *indexp, *dcrreg;
  201. int len;
  202. BUG_ON(! of_device_is_compatible(node, "ibm,uic"));
  203. uic = kzalloc(sizeof(*uic), GFP_KERNEL);
  204. if (! uic)
  205. return NULL; /* FIXME: panic? */
  206. raw_spin_lock_init(&uic->lock);
  207. indexp = of_get_property(node, "cell-index", &len);
  208. if (!indexp || (len != sizeof(u32))) {
  209. printk(KERN_ERR "uic: Device node %pOF has missing or invalid "
  210. "cell-index property\n", node);
  211. return NULL;
  212. }
  213. uic->index = *indexp;
  214. dcrreg = of_get_property(node, "dcr-reg", &len);
  215. if (!dcrreg || (len != 2*sizeof(u32))) {
  216. printk(KERN_ERR "uic: Device node %pOF has missing or invalid "
  217. "dcr-reg property\n", node);
  218. return NULL;
  219. }
  220. uic->dcrbase = *dcrreg;
  221. uic->irqhost = irq_domain_add_linear(node, NR_UIC_INTS, &uic_host_ops,
  222. uic);
  223. if (! uic->irqhost)
  224. return NULL; /* FIXME: panic? */
  225. /* Start with all interrupts disabled, level and non-critical */
  226. mtdcr(uic->dcrbase + UIC_ER, 0);
  227. mtdcr(uic->dcrbase + UIC_CR, 0);
  228. mtdcr(uic->dcrbase + UIC_TR, 0);
  229. /* Clear any pending interrupts, in case the firmware left some */
  230. mtdcr(uic->dcrbase + UIC_SR, 0xffffffff);
  231. printk ("UIC%d (%d IRQ sources) at DCR 0x%x\n", uic->index,
  232. NR_UIC_INTS, uic->dcrbase);
  233. return uic;
  234. }
  235. void __init uic_init_tree(void)
  236. {
  237. struct device_node *np;
  238. struct uic *uic;
  239. const u32 *interrupts;
  240. /* First locate and initialize the top-level UIC */
  241. for_each_compatible_node(np, NULL, "ibm,uic") {
  242. interrupts = of_get_property(np, "interrupts", NULL);
  243. if (!interrupts)
  244. break;
  245. }
  246. BUG_ON(!np); /* uic_init_tree() assumes there's a UIC as the
  247. * top-level interrupt controller */
  248. primary_uic = uic_init_one(np);
  249. if (!primary_uic)
  250. panic("Unable to initialize primary UIC %pOF\n", np);
  251. irq_set_default_host(primary_uic->irqhost);
  252. of_node_put(np);
  253. /* The scan again for cascaded UICs */
  254. for_each_compatible_node(np, NULL, "ibm,uic") {
  255. interrupts = of_get_property(np, "interrupts", NULL);
  256. if (interrupts) {
  257. /* Secondary UIC */
  258. int cascade_virq;
  259. uic = uic_init_one(np);
  260. if (! uic)
  261. panic("Unable to initialize a secondary UIC %pOF\n",
  262. np);
  263. cascade_virq = irq_of_parse_and_map(np, 0);
  264. irq_set_handler_data(cascade_virq, uic);
  265. irq_set_chained_handler(cascade_virq, uic_irq_cascade);
  266. /* FIXME: setup critical cascade?? */
  267. }
  268. }
  269. }
  270. /* Return an interrupt vector or 0 if no interrupt is pending. */
  271. unsigned int uic_get_irq(void)
  272. {
  273. u32 msr;
  274. int src;
  275. BUG_ON(! primary_uic);
  276. msr = mfdcr(primary_uic->dcrbase + UIC_MSR);
  277. src = 32 - ffs(msr);
  278. return irq_linear_revmap(primary_uic->irqhost, src);
  279. }