irq.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License version 2 as published
  4. * by the Free Software Foundation.
  5. *
  6. * Copyright (C) 2010 John Crispin <john@phrozen.org>
  7. * Copyright (C) 2010 Thomas Langer <thomas.langer@lantiq.com>
  8. */
  9. #include <linux/interrupt.h>
  10. #include <linux/ioport.h>
  11. #include <linux/sched.h>
  12. #include <linux/irqdomain.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_irq.h>
  16. #include <asm/bootinfo.h>
  17. #include <asm/irq_cpu.h>
  18. #include <lantiq_soc.h>
  19. #include <irq.h>
  20. /* register definitions - internal irqs */
  21. #define LTQ_ICU_IM0_ISR 0x0000
  22. #define LTQ_ICU_IM0_IER 0x0008
  23. #define LTQ_ICU_IM0_IOSR 0x0010
  24. #define LTQ_ICU_IM0_IRSR 0x0018
  25. #define LTQ_ICU_IM0_IMR 0x0020
  26. #define LTQ_ICU_IM1_ISR 0x0028
  27. #define LTQ_ICU_OFFSET (LTQ_ICU_IM1_ISR - LTQ_ICU_IM0_ISR)
  28. /* register definitions - external irqs */
  29. #define LTQ_EIU_EXIN_C 0x0000
  30. #define LTQ_EIU_EXIN_INIC 0x0004
  31. #define LTQ_EIU_EXIN_INC 0x0008
  32. #define LTQ_EIU_EXIN_INEN 0x000C
  33. /* number of external interrupts */
  34. #define MAX_EIU 6
  35. /* the performance counter */
  36. #define LTQ_PERF_IRQ (INT_NUM_IM4_IRL0 + 31)
  37. /*
  38. * irqs generated by devices attached to the EBU need to be acked in
  39. * a special manner
  40. */
  41. #define LTQ_ICU_EBU_IRQ 22
  42. #define ltq_icu_w32(m, x, y) ltq_w32((x), ltq_icu_membase[m] + (y))
  43. #define ltq_icu_r32(m, x) ltq_r32(ltq_icu_membase[m] + (x))
  44. #define ltq_eiu_w32(x, y) ltq_w32((x), ltq_eiu_membase + (y))
  45. #define ltq_eiu_r32(x) ltq_r32(ltq_eiu_membase + (x))
  46. /* our 2 ipi interrupts for VSMP */
  47. #define MIPS_CPU_IPI_RESCHED_IRQ 0
  48. #define MIPS_CPU_IPI_CALL_IRQ 1
  49. /* we have a cascade of 8 irqs */
  50. #define MIPS_CPU_IRQ_CASCADE 8
  51. static int exin_avail;
  52. static u32 ltq_eiu_irq[MAX_EIU];
  53. static void __iomem *ltq_icu_membase[MAX_IM];
  54. static void __iomem *ltq_eiu_membase;
  55. static struct irq_domain *ltq_domain;
  56. static int ltq_perfcount_irq;
  57. int ltq_eiu_get_irq(int exin)
  58. {
  59. if (exin < exin_avail)
  60. return ltq_eiu_irq[exin];
  61. return -1;
  62. }
  63. void ltq_disable_irq(struct irq_data *d)
  64. {
  65. u32 ier = LTQ_ICU_IM0_IER;
  66. int offset = d->hwirq - MIPS_CPU_IRQ_CASCADE;
  67. int im = offset / INT_NUM_IM_OFFSET;
  68. offset %= INT_NUM_IM_OFFSET;
  69. ltq_icu_w32(im, ltq_icu_r32(im, ier) & ~BIT(offset), ier);
  70. }
  71. void ltq_mask_and_ack_irq(struct irq_data *d)
  72. {
  73. u32 ier = LTQ_ICU_IM0_IER;
  74. u32 isr = LTQ_ICU_IM0_ISR;
  75. int offset = d->hwirq - MIPS_CPU_IRQ_CASCADE;
  76. int im = offset / INT_NUM_IM_OFFSET;
  77. offset %= INT_NUM_IM_OFFSET;
  78. ltq_icu_w32(im, ltq_icu_r32(im, ier) & ~BIT(offset), ier);
  79. ltq_icu_w32(im, BIT(offset), isr);
  80. }
  81. static void ltq_ack_irq(struct irq_data *d)
  82. {
  83. u32 isr = LTQ_ICU_IM0_ISR;
  84. int offset = d->hwirq - MIPS_CPU_IRQ_CASCADE;
  85. int im = offset / INT_NUM_IM_OFFSET;
  86. offset %= INT_NUM_IM_OFFSET;
  87. ltq_icu_w32(im, BIT(offset), isr);
  88. }
  89. void ltq_enable_irq(struct irq_data *d)
  90. {
  91. u32 ier = LTQ_ICU_IM0_IER;
  92. int offset = d->hwirq - MIPS_CPU_IRQ_CASCADE;
  93. int im = offset / INT_NUM_IM_OFFSET;
  94. offset %= INT_NUM_IM_OFFSET;
  95. ltq_icu_w32(im, ltq_icu_r32(im, ier) | BIT(offset), ier);
  96. }
  97. static int ltq_eiu_settype(struct irq_data *d, unsigned int type)
  98. {
  99. int i;
  100. for (i = 0; i < exin_avail; i++) {
  101. if (d->hwirq == ltq_eiu_irq[i]) {
  102. int val = 0;
  103. int edge = 0;
  104. switch (type) {
  105. case IRQF_TRIGGER_NONE:
  106. break;
  107. case IRQF_TRIGGER_RISING:
  108. val = 1;
  109. edge = 1;
  110. break;
  111. case IRQF_TRIGGER_FALLING:
  112. val = 2;
  113. edge = 1;
  114. break;
  115. case IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING:
  116. val = 3;
  117. edge = 1;
  118. break;
  119. case IRQF_TRIGGER_HIGH:
  120. val = 5;
  121. break;
  122. case IRQF_TRIGGER_LOW:
  123. val = 6;
  124. break;
  125. default:
  126. pr_err("invalid type %d for irq %ld\n",
  127. type, d->hwirq);
  128. return -EINVAL;
  129. }
  130. if (edge)
  131. irq_set_handler(d->hwirq, handle_edge_irq);
  132. ltq_eiu_w32((ltq_eiu_r32(LTQ_EIU_EXIN_C) &
  133. (~(7 << (i * 4)))) | (val << (i * 4)),
  134. LTQ_EIU_EXIN_C);
  135. }
  136. }
  137. return 0;
  138. }
  139. static unsigned int ltq_startup_eiu_irq(struct irq_data *d)
  140. {
  141. int i;
  142. ltq_enable_irq(d);
  143. for (i = 0; i < exin_avail; i++) {
  144. if (d->hwirq == ltq_eiu_irq[i]) {
  145. /* by default we are low level triggered */
  146. ltq_eiu_settype(d, IRQF_TRIGGER_LOW);
  147. /* clear all pending */
  148. ltq_eiu_w32(ltq_eiu_r32(LTQ_EIU_EXIN_INC) & ~BIT(i),
  149. LTQ_EIU_EXIN_INC);
  150. /* enable */
  151. ltq_eiu_w32(ltq_eiu_r32(LTQ_EIU_EXIN_INEN) | BIT(i),
  152. LTQ_EIU_EXIN_INEN);
  153. break;
  154. }
  155. }
  156. return 0;
  157. }
  158. static void ltq_shutdown_eiu_irq(struct irq_data *d)
  159. {
  160. int i;
  161. ltq_disable_irq(d);
  162. for (i = 0; i < exin_avail; i++) {
  163. if (d->hwirq == ltq_eiu_irq[i]) {
  164. /* disable */
  165. ltq_eiu_w32(ltq_eiu_r32(LTQ_EIU_EXIN_INEN) & ~BIT(i),
  166. LTQ_EIU_EXIN_INEN);
  167. break;
  168. }
  169. }
  170. }
  171. static struct irq_chip ltq_irq_type = {
  172. .name = "icu",
  173. .irq_enable = ltq_enable_irq,
  174. .irq_disable = ltq_disable_irq,
  175. .irq_unmask = ltq_enable_irq,
  176. .irq_ack = ltq_ack_irq,
  177. .irq_mask = ltq_disable_irq,
  178. .irq_mask_ack = ltq_mask_and_ack_irq,
  179. };
  180. static struct irq_chip ltq_eiu_type = {
  181. .name = "eiu",
  182. .irq_startup = ltq_startup_eiu_irq,
  183. .irq_shutdown = ltq_shutdown_eiu_irq,
  184. .irq_enable = ltq_enable_irq,
  185. .irq_disable = ltq_disable_irq,
  186. .irq_unmask = ltq_enable_irq,
  187. .irq_ack = ltq_ack_irq,
  188. .irq_mask = ltq_disable_irq,
  189. .irq_mask_ack = ltq_mask_and_ack_irq,
  190. .irq_set_type = ltq_eiu_settype,
  191. };
  192. static void ltq_hw_irq_handler(struct irq_desc *desc)
  193. {
  194. int module = irq_desc_get_irq(desc) - 2;
  195. u32 irq;
  196. int hwirq;
  197. irq = ltq_icu_r32(module, LTQ_ICU_IM0_IOSR);
  198. if (irq == 0)
  199. return;
  200. /*
  201. * silicon bug causes only the msb set to 1 to be valid. all
  202. * other bits might be bogus
  203. */
  204. irq = __fls(irq);
  205. hwirq = irq + MIPS_CPU_IRQ_CASCADE + (INT_NUM_IM_OFFSET * module);
  206. generic_handle_irq(irq_linear_revmap(ltq_domain, hwirq));
  207. /* if this is a EBU irq, we need to ack it or get a deadlock */
  208. if ((irq == LTQ_ICU_EBU_IRQ) && (module == 0) && LTQ_EBU_PCC_ISTAT)
  209. ltq_ebu_w32(ltq_ebu_r32(LTQ_EBU_PCC_ISTAT) | 0x10,
  210. LTQ_EBU_PCC_ISTAT);
  211. }
  212. static int icu_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
  213. {
  214. struct irq_chip *chip = &ltq_irq_type;
  215. int i;
  216. if (hw < MIPS_CPU_IRQ_CASCADE)
  217. return 0;
  218. for (i = 0; i < exin_avail; i++)
  219. if (hw == ltq_eiu_irq[i])
  220. chip = &ltq_eiu_type;
  221. irq_set_chip_and_handler(irq, chip, handle_level_irq);
  222. return 0;
  223. }
  224. static const struct irq_domain_ops irq_domain_ops = {
  225. .xlate = irq_domain_xlate_onetwocell,
  226. .map = icu_map,
  227. };
  228. int __init icu_of_init(struct device_node *node, struct device_node *parent)
  229. {
  230. struct device_node *eiu_node;
  231. struct resource res;
  232. int i, ret;
  233. for (i = 0; i < MAX_IM; i++) {
  234. if (of_address_to_resource(node, i, &res))
  235. panic("Failed to get icu memory range");
  236. if (!request_mem_region(res.start, resource_size(&res),
  237. res.name))
  238. pr_err("Failed to request icu memory");
  239. ltq_icu_membase[i] = ioremap_nocache(res.start,
  240. resource_size(&res));
  241. if (!ltq_icu_membase[i])
  242. panic("Failed to remap icu memory");
  243. }
  244. /* turn off all irqs by default */
  245. for (i = 0; i < MAX_IM; i++) {
  246. /* make sure all irqs are turned off by default */
  247. ltq_icu_w32(i, 0, LTQ_ICU_IM0_IER);
  248. /* clear all possibly pending interrupts */
  249. ltq_icu_w32(i, ~0, LTQ_ICU_IM0_ISR);
  250. }
  251. mips_cpu_irq_init();
  252. for (i = 0; i < MAX_IM; i++)
  253. irq_set_chained_handler(i + 2, ltq_hw_irq_handler);
  254. ltq_domain = irq_domain_add_linear(node,
  255. (MAX_IM * INT_NUM_IM_OFFSET) + MIPS_CPU_IRQ_CASCADE,
  256. &irq_domain_ops, 0);
  257. /* tell oprofile which irq to use */
  258. ltq_perfcount_irq = irq_create_mapping(ltq_domain, LTQ_PERF_IRQ);
  259. /*
  260. * if the timer irq is not one of the mips irqs we need to
  261. * create a mapping
  262. */
  263. if (MIPS_CPU_TIMER_IRQ != 7)
  264. irq_create_mapping(ltq_domain, MIPS_CPU_TIMER_IRQ);
  265. /* the external interrupts are optional and xway only */
  266. eiu_node = of_find_compatible_node(NULL, NULL, "lantiq,eiu-xway");
  267. if (eiu_node && !of_address_to_resource(eiu_node, 0, &res)) {
  268. /* find out how many external irq sources we have */
  269. exin_avail = of_property_count_u32_elems(eiu_node,
  270. "lantiq,eiu-irqs");
  271. if (exin_avail > MAX_EIU)
  272. exin_avail = MAX_EIU;
  273. ret = of_property_read_u32_array(eiu_node, "lantiq,eiu-irqs",
  274. ltq_eiu_irq, exin_avail);
  275. if (ret)
  276. panic("failed to load external irq resources");
  277. if (!request_mem_region(res.start, resource_size(&res),
  278. res.name))
  279. pr_err("Failed to request eiu memory");
  280. ltq_eiu_membase = ioremap_nocache(res.start,
  281. resource_size(&res));
  282. if (!ltq_eiu_membase)
  283. panic("Failed to remap eiu memory");
  284. }
  285. return 0;
  286. }
  287. int get_c0_perfcount_int(void)
  288. {
  289. return ltq_perfcount_irq;
  290. }
  291. EXPORT_SYMBOL_GPL(get_c0_perfcount_int);
  292. unsigned int get_c0_compare_int(void)
  293. {
  294. return MIPS_CPU_TIMER_IRQ;
  295. }
  296. static struct of_device_id __initdata of_irq_ids[] = {
  297. { .compatible = "lantiq,icu", .data = icu_of_init },
  298. {},
  299. };
  300. void __init arch_init_irq(void)
  301. {
  302. of_irq_init(of_irq_ids);
  303. }