uic.c 8.5 KB

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