uic.c 9.8 KB

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