uic.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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/bootmem.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/irq.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/kernel_stat.h>
  27. #include <asm/irq.h>
  28. #include <asm/io.h>
  29. #include <asm/prom.h>
  30. #include <asm/dcr.h>
  31. #define NR_UIC_INTS 32
  32. #define UIC_SR 0x0
  33. #define UIC_ER 0x2
  34. #define UIC_CR 0x3
  35. #define UIC_PR 0x4
  36. #define UIC_TR 0x5
  37. #define UIC_MSR 0x6
  38. #define UIC_VR 0x7
  39. #define UIC_VCR 0x8
  40. struct uic *primary_uic;
  41. struct uic {
  42. int index;
  43. int dcrbase;
  44. raw_spinlock_t lock;
  45. /* The remapper for this UIC */
  46. struct irq_domain *irqhost;
  47. };
  48. static void uic_unmask_irq(struct irq_data *d)
  49. {
  50. struct uic *uic = irq_data_get_irq_chip_data(d);
  51. unsigned int src = irqd_to_hwirq(d);
  52. unsigned long flags;
  53. u32 er, sr;
  54. sr = 1 << (31-src);
  55. raw_spin_lock_irqsave(&uic->lock, flags);
  56. /* ack level-triggered interrupts here */
  57. if (irqd_is_level_type(d))
  58. mtdcr(uic->dcrbase + UIC_SR, sr);
  59. er = mfdcr(uic->dcrbase + UIC_ER);
  60. er |= sr;
  61. mtdcr(uic->dcrbase + UIC_ER, er);
  62. raw_spin_unlock_irqrestore(&uic->lock, flags);
  63. }
  64. static void uic_mask_irq(struct irq_data *d)
  65. {
  66. struct uic *uic = irq_data_get_irq_chip_data(d);
  67. unsigned int src = irqd_to_hwirq(d);
  68. unsigned long flags;
  69. u32 er;
  70. raw_spin_lock_irqsave(&uic->lock, flags);
  71. er = mfdcr(uic->dcrbase + UIC_ER);
  72. er &= ~(1 << (31 - src));
  73. mtdcr(uic->dcrbase + UIC_ER, er);
  74. raw_spin_unlock_irqrestore(&uic->lock, flags);
  75. }
  76. static void uic_ack_irq(struct irq_data *d)
  77. {
  78. struct uic *uic = irq_data_get_irq_chip_data(d);
  79. unsigned int src = irqd_to_hwirq(d);
  80. unsigned long flags;
  81. raw_spin_lock_irqsave(&uic->lock, flags);
  82. mtdcr(uic->dcrbase + UIC_SR, 1 << (31-src));
  83. raw_spin_unlock_irqrestore(&uic->lock, flags);
  84. }
  85. static void uic_mask_ack_irq(struct irq_data *d)
  86. {
  87. struct uic *uic = irq_data_get_irq_chip_data(d);
  88. unsigned int src = irqd_to_hwirq(d);
  89. unsigned long flags;
  90. u32 er, sr;
  91. sr = 1 << (31-src);
  92. raw_spin_lock_irqsave(&uic->lock, flags);
  93. er = mfdcr(uic->dcrbase + UIC_ER);
  94. er &= ~sr;
  95. mtdcr(uic->dcrbase + UIC_ER, er);
  96. /* On the UIC, acking (i.e. clearing the SR bit)
  97. * a level irq will have no effect if the interrupt
  98. * is still asserted by the device, even if
  99. * the interrupt is already masked. Therefore
  100. * we only ack the egde interrupts here, while
  101. * level interrupts are ack'ed after the actual
  102. * isr call in the uic_unmask_irq()
  103. */
  104. if (!irqd_is_level_type(d))
  105. mtdcr(uic->dcrbase + UIC_SR, sr);
  106. raw_spin_unlock_irqrestore(&uic->lock, flags);
  107. }
  108. static int uic_set_irq_type(struct irq_data *d, unsigned int flow_type)
  109. {
  110. struct uic *uic = irq_data_get_irq_chip_data(d);
  111. unsigned int src = irqd_to_hwirq(d);
  112. unsigned long flags;
  113. int trigger, polarity;
  114. u32 tr, pr, mask;
  115. switch (flow_type & IRQ_TYPE_SENSE_MASK) {
  116. case IRQ_TYPE_NONE:
  117. uic_mask_irq(d);
  118. return 0;
  119. case IRQ_TYPE_EDGE_RISING:
  120. trigger = 1; polarity = 1;
  121. break;
  122. case IRQ_TYPE_EDGE_FALLING:
  123. trigger = 1; polarity = 0;
  124. break;
  125. case IRQ_TYPE_LEVEL_HIGH:
  126. trigger = 0; polarity = 1;
  127. break;
  128. case IRQ_TYPE_LEVEL_LOW:
  129. trigger = 0; polarity = 0;
  130. break;
  131. default:
  132. return -EINVAL;
  133. }
  134. mask = ~(1 << (31 - src));
  135. raw_spin_lock_irqsave(&uic->lock, flags);
  136. tr = mfdcr(uic->dcrbase + UIC_TR);
  137. pr = mfdcr(uic->dcrbase + UIC_PR);
  138. tr = (tr & mask) | (trigger << (31-src));
  139. pr = (pr & mask) | (polarity << (31-src));
  140. mtdcr(uic->dcrbase + UIC_PR, pr);
  141. mtdcr(uic->dcrbase + UIC_TR, tr);
  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 struct irq_domain_ops uic_host_ops = {
  166. .map = uic_host_map,
  167. .xlate = irq_domain_xlate_twocell,
  168. };
  169. void uic_irq_cascade(unsigned int virq, 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_get_handler_data(virq);
  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 %s has missing or invalid "
  210. "cell-index property\n", node->full_name);
  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 %s has missing or invalid "
  217. "dcr-reg property\n", node->full_name);
  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 %s\n", np->full_name);
  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 %s\n",
  262. np->full_name);
  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 NO_IRQ 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. }