uic.c 7.9 KB

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