uic.c 8.8 KB

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