opb_pic.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * IBM Onboard Peripheral Bus Interrupt Controller
  3. *
  4. * Copyright 2010 Jack Miller, IBM Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/irq.h>
  14. #include <linux/of.h>
  15. #include <linux/slab.h>
  16. #include <linux/time.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_irq.h>
  19. #include <asm/reg_a2.h>
  20. #include <asm/irq.h>
  21. #define OPB_NR_IRQS 32
  22. #define OPB_MLSASIER 0x04 /* MLS Accumulated Status IER */
  23. #define OPB_MLSIR 0x50 /* MLS Interrupt Register */
  24. #define OPB_MLSIER 0x54 /* MLS Interrupt Enable Register */
  25. #define OPB_MLSIPR 0x58 /* MLS Interrupt Polarity Register */
  26. #define OPB_MLSIIR 0x5c /* MLS Interrupt Inputs Register */
  27. static int opb_index = 0;
  28. struct opb_pic {
  29. struct irq_domain *host;
  30. void *regs;
  31. int index;
  32. spinlock_t lock;
  33. };
  34. static u32 opb_in(struct opb_pic *opb, int offset)
  35. {
  36. return in_be32(opb->regs + offset);
  37. }
  38. static void opb_out(struct opb_pic *opb, int offset, u32 val)
  39. {
  40. out_be32(opb->regs + offset, val);
  41. }
  42. static void opb_unmask_irq(struct irq_data *d)
  43. {
  44. struct opb_pic *opb;
  45. unsigned long flags;
  46. u32 ier, bitset;
  47. opb = d->chip_data;
  48. bitset = (1 << (31 - irqd_to_hwirq(d)));
  49. spin_lock_irqsave(&opb->lock, flags);
  50. ier = opb_in(opb, OPB_MLSIER);
  51. opb_out(opb, OPB_MLSIER, ier | bitset);
  52. ier = opb_in(opb, OPB_MLSIER);
  53. spin_unlock_irqrestore(&opb->lock, flags);
  54. }
  55. static void opb_mask_irq(struct irq_data *d)
  56. {
  57. struct opb_pic *opb;
  58. unsigned long flags;
  59. u32 ier, mask;
  60. opb = d->chip_data;
  61. mask = ~(1 << (31 - irqd_to_hwirq(d)));
  62. spin_lock_irqsave(&opb->lock, flags);
  63. ier = opb_in(opb, OPB_MLSIER);
  64. opb_out(opb, OPB_MLSIER, ier & mask);
  65. ier = opb_in(opb, OPB_MLSIER); // Flush posted writes
  66. spin_unlock_irqrestore(&opb->lock, flags);
  67. }
  68. static void opb_ack_irq(struct irq_data *d)
  69. {
  70. struct opb_pic *opb;
  71. unsigned long flags;
  72. u32 bitset;
  73. opb = d->chip_data;
  74. bitset = (1 << (31 - irqd_to_hwirq(d)));
  75. spin_lock_irqsave(&opb->lock, flags);
  76. opb_out(opb, OPB_MLSIR, bitset);
  77. opb_in(opb, OPB_MLSIR); // Flush posted writes
  78. spin_unlock_irqrestore(&opb->lock, flags);
  79. }
  80. static void opb_mask_ack_irq(struct irq_data *d)
  81. {
  82. struct opb_pic *opb;
  83. unsigned long flags;
  84. u32 bitset;
  85. u32 ier, ir;
  86. opb = d->chip_data;
  87. bitset = (1 << (31 - irqd_to_hwirq(d)));
  88. spin_lock_irqsave(&opb->lock, flags);
  89. ier = opb_in(opb, OPB_MLSIER);
  90. opb_out(opb, OPB_MLSIER, ier & ~bitset);
  91. ier = opb_in(opb, OPB_MLSIER); // Flush posted writes
  92. opb_out(opb, OPB_MLSIR, bitset);
  93. ir = opb_in(opb, OPB_MLSIR); // Flush posted writes
  94. spin_unlock_irqrestore(&opb->lock, flags);
  95. }
  96. static int opb_set_irq_type(struct irq_data *d, unsigned int flow)
  97. {
  98. struct opb_pic *opb;
  99. unsigned long flags;
  100. int invert, ipr, mask, bit;
  101. opb = d->chip_data;
  102. /* The only information we're interested in in the type is whether it's
  103. * a high or low trigger. For high triggered interrupts, the polarity
  104. * set for it in the MLS Interrupt Polarity Register is 0, for low
  105. * interrupts it's 1 so that the proper input in the MLS Interrupt Input
  106. * Register is interrupted as asserting the interrupt. */
  107. switch (flow) {
  108. case IRQ_TYPE_NONE:
  109. opb_mask_irq(d);
  110. return 0;
  111. case IRQ_TYPE_LEVEL_HIGH:
  112. invert = 0;
  113. break;
  114. case IRQ_TYPE_LEVEL_LOW:
  115. invert = 1;
  116. break;
  117. default:
  118. return -EINVAL;
  119. }
  120. bit = (1 << (31 - irqd_to_hwirq(d)));
  121. mask = ~bit;
  122. spin_lock_irqsave(&opb->lock, flags);
  123. ipr = opb_in(opb, OPB_MLSIPR);
  124. ipr = (ipr & mask) | (invert ? bit : 0);
  125. opb_out(opb, OPB_MLSIPR, ipr);
  126. ipr = opb_in(opb, OPB_MLSIPR); // Flush posted writes
  127. spin_unlock_irqrestore(&opb->lock, flags);
  128. /* Record the type in the interrupt descriptor */
  129. irqd_set_trigger_type(d, flow);
  130. return 0;
  131. }
  132. static struct irq_chip opb_irq_chip = {
  133. .name = "OPB",
  134. .irq_mask = opb_mask_irq,
  135. .irq_unmask = opb_unmask_irq,
  136. .irq_mask_ack = opb_mask_ack_irq,
  137. .irq_ack = opb_ack_irq,
  138. .irq_set_type = opb_set_irq_type
  139. };
  140. static int opb_host_map(struct irq_domain *host, unsigned int virq,
  141. irq_hw_number_t hwirq)
  142. {
  143. struct opb_pic *opb;
  144. opb = host->host_data;
  145. /* Most of the important stuff is handled by the generic host code, like
  146. * the lookup, so just attach some info to the virtual irq */
  147. irq_set_chip_data(virq, opb);
  148. irq_set_chip_and_handler(virq, &opb_irq_chip, handle_level_irq);
  149. irq_set_irq_type(virq, IRQ_TYPE_NONE);
  150. return 0;
  151. }
  152. static const struct irq_domain_ops opb_host_ops = {
  153. .map = opb_host_map,
  154. .xlate = irq_domain_xlate_twocell,
  155. };
  156. irqreturn_t opb_irq_handler(int irq, void *private)
  157. {
  158. struct opb_pic *opb;
  159. u32 ir, src, subvirq;
  160. opb = (struct opb_pic *) private;
  161. /* Read the OPB MLS Interrupt Register for
  162. * asserted interrupts */
  163. ir = opb_in(opb, OPB_MLSIR);
  164. if (!ir)
  165. return IRQ_NONE;
  166. do {
  167. /* Get 1 - 32 source, *NOT* bit */
  168. src = 32 - ffs(ir);
  169. /* Translate from the OPB's conception of interrupt number to
  170. * Linux's virtual IRQ */
  171. subvirq = irq_linear_revmap(opb->host, src);
  172. generic_handle_irq(subvirq);
  173. } while ((ir = opb_in(opb, OPB_MLSIR)));
  174. return IRQ_HANDLED;
  175. }
  176. struct opb_pic *opb_pic_init_one(struct device_node *dn)
  177. {
  178. struct opb_pic *opb;
  179. struct resource res;
  180. if (of_address_to_resource(dn, 0, &res)) {
  181. printk(KERN_ERR "opb: Couldn't translate resource\n");
  182. return NULL;
  183. }
  184. opb = kzalloc(sizeof(struct opb_pic), GFP_KERNEL);
  185. if (!opb) {
  186. printk(KERN_ERR "opb: Failed to allocate opb struct!\n");
  187. return NULL;
  188. }
  189. /* Get access to the OPB MMIO registers */
  190. opb->regs = ioremap(res.start + 0x10000, 0x1000);
  191. if (!opb->regs) {
  192. printk(KERN_ERR "opb: Failed to allocate register space!\n");
  193. goto free_opb;
  194. }
  195. /* Allocate an irq domain so that Linux knows that despite only
  196. * having one interrupt to issue, we're the controller for multiple
  197. * hardware IRQs, so later we can lookup their virtual IRQs. */
  198. opb->host = irq_domain_add_linear(dn, OPB_NR_IRQS, &opb_host_ops, opb);
  199. if (!opb->host) {
  200. printk(KERN_ERR "opb: Failed to allocate IRQ host!\n");
  201. goto free_regs;
  202. }
  203. opb->index = opb_index++;
  204. spin_lock_init(&opb->lock);
  205. /* Disable all interrupts by default */
  206. opb_out(opb, OPB_MLSASIER, 0);
  207. opb_out(opb, OPB_MLSIER, 0);
  208. /* ACK any interrupts left by FW */
  209. opb_out(opb, OPB_MLSIR, 0xFFFFFFFF);
  210. return opb;
  211. free_regs:
  212. iounmap(opb->regs);
  213. free_opb:
  214. kfree(opb);
  215. return NULL;
  216. }
  217. void __init opb_pic_init(void)
  218. {
  219. struct device_node *dn;
  220. struct opb_pic *opb;
  221. int virq;
  222. int rc;
  223. /* Call init_one for each OPB device */
  224. for_each_compatible_node(dn, NULL, "ibm,opb") {
  225. /* Fill in an OPB struct */
  226. opb = opb_pic_init_one(dn);
  227. if (!opb) {
  228. printk(KERN_WARNING "opb: Failed to init node, skipped!\n");
  229. continue;
  230. }
  231. /* Map / get opb's hardware virtual irq */
  232. virq = irq_of_parse_and_map(dn, 0);
  233. if (virq <= 0) {
  234. printk("opb: irq_op_parse_and_map failed!\n");
  235. continue;
  236. }
  237. /* Attach opb interrupt handler to new virtual IRQ */
  238. rc = request_irq(virq, opb_irq_handler, IRQF_NO_THREAD,
  239. "OPB LS Cascade", opb);
  240. if (rc) {
  241. printk("opb: request_irq failed: %d\n", rc);
  242. continue;
  243. }
  244. printk("OPB%d init with %d IRQs at %p\n", opb->index,
  245. OPB_NR_IRQS, opb->regs);
  246. }
  247. }