uic.c 9.6 KB

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