uic.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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/sysdev.h>
  22. #include <linux/device.h>
  23. #include <linux/bootmem.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/irq.h>
  26. #include <linux/interrupt.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. #define uic_irq_to_hw(virq) (irq_map[virq].hwirq)
  41. struct uic *primary_uic;
  42. struct uic {
  43. int index;
  44. int dcrbase;
  45. spinlock_t lock;
  46. /* The remapper for this UIC */
  47. struct irq_host *irqhost;
  48. /* For secondary UICs, the cascade interrupt's irqaction */
  49. struct irqaction cascade;
  50. /* The device node of the interrupt controller */
  51. struct device_node *of_node;
  52. };
  53. static void uic_unmask_irq(unsigned int virq)
  54. {
  55. struct uic *uic = get_irq_chip_data(virq);
  56. unsigned int src = uic_irq_to_hw(virq);
  57. unsigned long flags;
  58. u32 er;
  59. spin_lock_irqsave(&uic->lock, flags);
  60. er = mfdcr(uic->dcrbase + UIC_ER);
  61. er |= 1 << (31 - src);
  62. mtdcr(uic->dcrbase + UIC_ER, er);
  63. spin_unlock_irqrestore(&uic->lock, flags);
  64. }
  65. static void uic_mask_irq(unsigned int virq)
  66. {
  67. struct uic *uic = get_irq_chip_data(virq);
  68. unsigned int src = uic_irq_to_hw(virq);
  69. unsigned long flags;
  70. u32 er;
  71. spin_lock_irqsave(&uic->lock, flags);
  72. er = mfdcr(uic->dcrbase + UIC_ER);
  73. er &= ~(1 << (31 - src));
  74. mtdcr(uic->dcrbase + UIC_ER, er);
  75. spin_unlock_irqrestore(&uic->lock, flags);
  76. }
  77. static void uic_ack_irq(unsigned int virq)
  78. {
  79. struct uic *uic = get_irq_chip_data(virq);
  80. unsigned int src = uic_irq_to_hw(virq);
  81. unsigned long flags;
  82. spin_lock_irqsave(&uic->lock, flags);
  83. mtdcr(uic->dcrbase + UIC_SR, 1 << (31-src));
  84. spin_unlock_irqrestore(&uic->lock, flags);
  85. }
  86. static int uic_set_irq_type(unsigned int virq, unsigned int flow_type)
  87. {
  88. struct uic *uic = get_irq_chip_data(virq);
  89. unsigned int src = uic_irq_to_hw(virq);
  90. struct irq_desc *desc = get_irq_desc(virq);
  91. unsigned long flags;
  92. int trigger, polarity;
  93. u32 tr, pr, mask;
  94. switch (flow_type & IRQ_TYPE_SENSE_MASK) {
  95. case IRQ_TYPE_NONE:
  96. uic_mask_irq(virq);
  97. return 0;
  98. case IRQ_TYPE_EDGE_RISING:
  99. trigger = 1; polarity = 1;
  100. break;
  101. case IRQ_TYPE_EDGE_FALLING:
  102. trigger = 1; polarity = 0;
  103. break;
  104. case IRQ_TYPE_LEVEL_HIGH:
  105. trigger = 0; polarity = 1;
  106. break;
  107. case IRQ_TYPE_LEVEL_LOW:
  108. trigger = 0; polarity = 0;
  109. break;
  110. default:
  111. return -EINVAL;
  112. }
  113. mask = ~(1 << (31 - src));
  114. spin_lock_irqsave(&uic->lock, flags);
  115. tr = mfdcr(uic->dcrbase + UIC_TR);
  116. pr = mfdcr(uic->dcrbase + UIC_PR);
  117. tr = (tr & mask) | (trigger << (31-src));
  118. pr = (pr & mask) | (polarity << (31-src));
  119. mtdcr(uic->dcrbase + UIC_PR, pr);
  120. mtdcr(uic->dcrbase + UIC_TR, tr);
  121. desc->status &= ~(IRQ_TYPE_SENSE_MASK | IRQ_LEVEL);
  122. desc->status |= flow_type & IRQ_TYPE_SENSE_MASK;
  123. if (trigger)
  124. desc->status |= IRQ_LEVEL;
  125. spin_unlock_irqrestore(&uic->lock, flags);
  126. return 0;
  127. }
  128. static struct irq_chip uic_irq_chip = {
  129. .typename = " UIC ",
  130. .unmask = uic_unmask_irq,
  131. .mask = uic_mask_irq,
  132. /* .mask_ack = uic_mask_irq_and_ack, */
  133. .ack = uic_ack_irq,
  134. .set_type = uic_set_irq_type,
  135. };
  136. static int uic_host_match(struct irq_host *h, struct device_node *node)
  137. {
  138. struct uic *uic = h->host_data;
  139. return uic->of_node == node;
  140. }
  141. static int uic_host_map(struct irq_host *h, unsigned int virq,
  142. irq_hw_number_t hw)
  143. {
  144. struct uic *uic = h->host_data;
  145. set_irq_chip_data(virq, uic);
  146. /* Despite the name, handle_level_irq() works for both level
  147. * and edge irqs on UIC. FIXME: check this is correct */
  148. set_irq_chip_and_handler(virq, &uic_irq_chip, handle_level_irq);
  149. /* Set default irq type */
  150. set_irq_type(virq, IRQ_TYPE_NONE);
  151. return 0;
  152. }
  153. static int uic_host_xlate(struct irq_host *h, struct device_node *ct,
  154. u32 *intspec, unsigned int intsize,
  155. irq_hw_number_t *out_hwirq, unsigned int *out_type)
  156. {
  157. /* UIC intspecs must have 2 cells */
  158. BUG_ON(intsize != 2);
  159. *out_hwirq = intspec[0];
  160. *out_type = intspec[1];
  161. return 0;
  162. }
  163. static struct irq_host_ops uic_host_ops = {
  164. .match = uic_host_match,
  165. .map = uic_host_map,
  166. .xlate = uic_host_xlate,
  167. };
  168. irqreturn_t uic_cascade(int virq, void *data)
  169. {
  170. struct uic *uic = data;
  171. u32 msr;
  172. int src;
  173. int subvirq;
  174. msr = mfdcr(uic->dcrbase + UIC_MSR);
  175. src = 32 - ffs(msr);
  176. subvirq = irq_linear_revmap(uic->irqhost, src);
  177. generic_handle_irq(subvirq);
  178. return IRQ_HANDLED;
  179. }
  180. static struct uic * __init uic_init_one(struct device_node *node)
  181. {
  182. struct uic *uic;
  183. const u32 *indexp, *dcrreg;
  184. int len;
  185. BUG_ON(! of_device_is_compatible(node, "ibm,uic"));
  186. uic = alloc_bootmem(sizeof(*uic));
  187. if (! uic)
  188. return NULL; /* FIXME: panic? */
  189. memset(uic, 0, sizeof(*uic));
  190. spin_lock_init(&uic->lock);
  191. uic->of_node = of_node_get(node);
  192. indexp = of_get_property(node, "cell-index", &len);
  193. if (!indexp || (len != sizeof(u32))) {
  194. printk(KERN_ERR "uic: Device node %s has missing or invalid "
  195. "cell-index property\n", node->full_name);
  196. return NULL;
  197. }
  198. uic->index = *indexp;
  199. dcrreg = of_get_property(node, "dcr-reg", &len);
  200. if (!dcrreg || (len != 2*sizeof(u32))) {
  201. printk(KERN_ERR "uic: Device node %s has missing or invalid "
  202. "dcr-reg property\n", node->full_name);
  203. return NULL;
  204. }
  205. uic->dcrbase = *dcrreg;
  206. uic->irqhost = irq_alloc_host(IRQ_HOST_MAP_LINEAR, NR_UIC_INTS,
  207. &uic_host_ops, -1);
  208. if (! uic->irqhost) {
  209. of_node_put(node);
  210. return NULL; /* FIXME: panic? */
  211. }
  212. uic->irqhost->host_data = uic;
  213. /* Start with all interrupts disabled, level and non-critical */
  214. mtdcr(uic->dcrbase + UIC_ER, 0);
  215. mtdcr(uic->dcrbase + UIC_CR, 0);
  216. mtdcr(uic->dcrbase + UIC_TR, 0);
  217. /* Clear any pending interrupts, in case the firmware left some */
  218. mtdcr(uic->dcrbase + UIC_SR, 0xffffffff);
  219. printk ("UIC%d (%d IRQ sources) at DCR 0x%x\n", uic->index,
  220. NR_UIC_INTS, uic->dcrbase);
  221. return uic;
  222. }
  223. void __init uic_init_tree(void)
  224. {
  225. struct device_node *np;
  226. struct uic *uic;
  227. const u32 *interrupts;
  228. /* First locate and initialize the top-level UIC */
  229. np = of_find_compatible_node(NULL, NULL, "ibm,uic");
  230. while (np) {
  231. interrupts = of_get_property(np, "interrupts", NULL);
  232. if (! interrupts)
  233. break;
  234. np = of_find_compatible_node(np, NULL, "ibm,uic");
  235. }
  236. BUG_ON(!np); /* uic_init_tree() assumes there's a UIC as the
  237. * top-level interrupt controller */
  238. primary_uic = uic_init_one(np);
  239. if (! primary_uic)
  240. panic("Unable to initialize primary UIC %s\n", np->full_name);
  241. irq_set_default_host(primary_uic->irqhost);
  242. of_node_put(np);
  243. /* The scan again for cascaded UICs */
  244. np = of_find_compatible_node(NULL, NULL, "ibm,uic");
  245. while (np) {
  246. interrupts = of_get_property(np, "interrupts", NULL);
  247. if (interrupts) {
  248. /* Secondary UIC */
  249. int cascade_virq;
  250. int ret;
  251. uic = uic_init_one(np);
  252. if (! uic)
  253. panic("Unable to initialize a secondary UIC %s\n",
  254. np->full_name);
  255. cascade_virq = irq_of_parse_and_map(np, 0);
  256. uic->cascade.handler = uic_cascade;
  257. uic->cascade.name = "UIC cascade";
  258. uic->cascade.dev_id = uic;
  259. ret = setup_irq(cascade_virq, &uic->cascade);
  260. if (ret)
  261. printk(KERN_ERR "Failed to setup_irq(%d) for "
  262. "UIC%d cascade\n", cascade_virq,
  263. uic->index);
  264. /* FIXME: setup critical cascade?? */
  265. }
  266. np = of_find_compatible_node(np, NULL, "ibm,uic");
  267. }
  268. }
  269. /* Return an interrupt vector or NO_IRQ 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. }