uic.c 8.7 KB

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