opb_pic.c 7.2 KB

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