uic.c 10.0 KB

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