irq.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 <blogic@openwrt.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_INEN 0x000C
  32. /* irq numbers used by the external interrupt unit (EIU) */
  33. #define LTQ_EIU_IR0 (INT_NUM_IM4_IRL0 + 30)
  34. #define LTQ_EIU_IR1 (INT_NUM_IM3_IRL0 + 31)
  35. #define LTQ_EIU_IR2 (INT_NUM_IM1_IRL0 + 26)
  36. #define LTQ_EIU_IR3 INT_NUM_IM1_IRL0
  37. #define LTQ_EIU_IR4 (INT_NUM_IM1_IRL0 + 1)
  38. #define LTQ_EIU_IR5 (INT_NUM_IM1_IRL0 + 2)
  39. #define LTQ_EIU_IR6 (INT_NUM_IM2_IRL0 + 30)
  40. #define XWAY_EXIN_COUNT 3
  41. #define MAX_EIU 6
  42. /* the performance counter */
  43. #define LTQ_PERF_IRQ (INT_NUM_IM4_IRL0 + 31)
  44. /*
  45. * irqs generated by devices attached to the EBU need to be acked in
  46. * a special manner
  47. */
  48. #define LTQ_ICU_EBU_IRQ 22
  49. #define ltq_icu_w32(x, y) ltq_w32((x), ltq_icu_membase + (y))
  50. #define ltq_icu_r32(x) ltq_r32(ltq_icu_membase + (x))
  51. #define ltq_eiu_w32(x, y) ltq_w32((x), ltq_eiu_membase + (y))
  52. #define ltq_eiu_r32(x) ltq_r32(ltq_eiu_membase + (x))
  53. /* our 2 ipi interrupts for VSMP */
  54. #define MIPS_CPU_IPI_RESCHED_IRQ 0
  55. #define MIPS_CPU_IPI_CALL_IRQ 1
  56. /* we have a cascade of 8 irqs */
  57. #define MIPS_CPU_IRQ_CASCADE 8
  58. #if defined(CONFIG_MIPS_MT_SMP) || defined(CONFIG_MIPS_MT_SMTC)
  59. int gic_present;
  60. #endif
  61. static unsigned short ltq_eiu_irq[MAX_EIU] = {
  62. LTQ_EIU_IR0,
  63. LTQ_EIU_IR1,
  64. LTQ_EIU_IR2,
  65. LTQ_EIU_IR3,
  66. LTQ_EIU_IR4,
  67. LTQ_EIU_IR5,
  68. };
  69. static int exin_avail;
  70. static void __iomem *ltq_icu_membase;
  71. static void __iomem *ltq_eiu_membase;
  72. void ltq_disable_irq(struct irq_data *d)
  73. {
  74. u32 ier = LTQ_ICU_IM0_IER;
  75. int offset = d->hwirq - MIPS_CPU_IRQ_CASCADE;
  76. ier += LTQ_ICU_OFFSET * (offset / INT_NUM_IM_OFFSET);
  77. offset %= INT_NUM_IM_OFFSET;
  78. ltq_icu_w32(ltq_icu_r32(ier) & ~BIT(offset), ier);
  79. }
  80. void ltq_mask_and_ack_irq(struct irq_data *d)
  81. {
  82. u32 ier = LTQ_ICU_IM0_IER;
  83. u32 isr = LTQ_ICU_IM0_ISR;
  84. int offset = d->hwirq - MIPS_CPU_IRQ_CASCADE;
  85. ier += LTQ_ICU_OFFSET * (offset / INT_NUM_IM_OFFSET);
  86. isr += LTQ_ICU_OFFSET * (offset / INT_NUM_IM_OFFSET);
  87. offset %= INT_NUM_IM_OFFSET;
  88. ltq_icu_w32(ltq_icu_r32(ier) & ~BIT(offset), ier);
  89. ltq_icu_w32(BIT(offset), isr);
  90. }
  91. static void ltq_ack_irq(struct irq_data *d)
  92. {
  93. u32 isr = LTQ_ICU_IM0_ISR;
  94. int offset = d->hwirq - MIPS_CPU_IRQ_CASCADE;
  95. isr += LTQ_ICU_OFFSET * (offset / INT_NUM_IM_OFFSET);
  96. offset %= INT_NUM_IM_OFFSET;
  97. ltq_icu_w32(BIT(offset), isr);
  98. }
  99. void ltq_enable_irq(struct irq_data *d)
  100. {
  101. u32 ier = LTQ_ICU_IM0_IER;
  102. int offset = d->hwirq - MIPS_CPU_IRQ_CASCADE;
  103. ier += LTQ_ICU_OFFSET * (offset / INT_NUM_IM_OFFSET);
  104. offset %= INT_NUM_IM_OFFSET;
  105. ltq_icu_w32(ltq_icu_r32(ier) | BIT(offset), ier);
  106. }
  107. static unsigned int ltq_startup_eiu_irq(struct irq_data *d)
  108. {
  109. int i;
  110. ltq_enable_irq(d);
  111. for (i = 0; i < MAX_EIU; i++) {
  112. if (d->hwirq == ltq_eiu_irq[i]) {
  113. /* low level - we should really handle set_type */
  114. ltq_eiu_w32(ltq_eiu_r32(LTQ_EIU_EXIN_C) |
  115. (0x6 << (i * 4)), LTQ_EIU_EXIN_C);
  116. /* clear all pending */
  117. ltq_eiu_w32(ltq_eiu_r32(LTQ_EIU_EXIN_INIC) & ~BIT(i),
  118. LTQ_EIU_EXIN_INIC);
  119. /* enable */
  120. ltq_eiu_w32(ltq_eiu_r32(LTQ_EIU_EXIN_INEN) | BIT(i),
  121. LTQ_EIU_EXIN_INEN);
  122. break;
  123. }
  124. }
  125. return 0;
  126. }
  127. static void ltq_shutdown_eiu_irq(struct irq_data *d)
  128. {
  129. int i;
  130. ltq_disable_irq(d);
  131. for (i = 0; i < MAX_EIU; i++) {
  132. if (d->hwirq == ltq_eiu_irq[i]) {
  133. /* disable */
  134. ltq_eiu_w32(ltq_eiu_r32(LTQ_EIU_EXIN_INEN) & ~BIT(i),
  135. LTQ_EIU_EXIN_INEN);
  136. break;
  137. }
  138. }
  139. }
  140. static struct irq_chip ltq_irq_type = {
  141. "icu",
  142. .irq_enable = ltq_enable_irq,
  143. .irq_disable = ltq_disable_irq,
  144. .irq_unmask = ltq_enable_irq,
  145. .irq_ack = ltq_ack_irq,
  146. .irq_mask = ltq_disable_irq,
  147. .irq_mask_ack = ltq_mask_and_ack_irq,
  148. };
  149. static struct irq_chip ltq_eiu_type = {
  150. "eiu",
  151. .irq_startup = ltq_startup_eiu_irq,
  152. .irq_shutdown = ltq_shutdown_eiu_irq,
  153. .irq_enable = ltq_enable_irq,
  154. .irq_disable = ltq_disable_irq,
  155. .irq_unmask = ltq_enable_irq,
  156. .irq_ack = ltq_ack_irq,
  157. .irq_mask = ltq_disable_irq,
  158. .irq_mask_ack = ltq_mask_and_ack_irq,
  159. };
  160. static void ltq_hw_irqdispatch(int module)
  161. {
  162. u32 irq;
  163. irq = ltq_icu_r32(LTQ_ICU_IM0_IOSR + (module * LTQ_ICU_OFFSET));
  164. if (irq == 0)
  165. return;
  166. /*
  167. * silicon bug causes only the msb set to 1 to be valid. all
  168. * other bits might be bogus
  169. */
  170. irq = __fls(irq);
  171. do_IRQ((int)irq + MIPS_CPU_IRQ_CASCADE + (INT_NUM_IM_OFFSET * module));
  172. /* if this is a EBU irq, we need to ack it or get a deadlock */
  173. if ((irq == LTQ_ICU_EBU_IRQ) && (module == 0) && LTQ_EBU_PCC_ISTAT)
  174. ltq_ebu_w32(ltq_ebu_r32(LTQ_EBU_PCC_ISTAT) | 0x10,
  175. LTQ_EBU_PCC_ISTAT);
  176. }
  177. #define DEFINE_HWx_IRQDISPATCH(x) \
  178. static void ltq_hw ## x ## _irqdispatch(void) \
  179. { \
  180. ltq_hw_irqdispatch(x); \
  181. }
  182. DEFINE_HWx_IRQDISPATCH(0)
  183. DEFINE_HWx_IRQDISPATCH(1)
  184. DEFINE_HWx_IRQDISPATCH(2)
  185. DEFINE_HWx_IRQDISPATCH(3)
  186. DEFINE_HWx_IRQDISPATCH(4)
  187. static void ltq_hw5_irqdispatch(void)
  188. {
  189. do_IRQ(MIPS_CPU_TIMER_IRQ);
  190. }
  191. #ifdef CONFIG_MIPS_MT_SMP
  192. void __init arch_init_ipiirq(int irq, struct irqaction *action)
  193. {
  194. setup_irq(irq, action);
  195. irq_set_handler(irq, handle_percpu_irq);
  196. }
  197. static void ltq_sw0_irqdispatch(void)
  198. {
  199. do_IRQ(MIPS_CPU_IRQ_BASE + MIPS_CPU_IPI_RESCHED_IRQ);
  200. }
  201. static void ltq_sw1_irqdispatch(void)
  202. {
  203. do_IRQ(MIPS_CPU_IRQ_BASE + MIPS_CPU_IPI_CALL_IRQ);
  204. }
  205. static irqreturn_t ipi_resched_interrupt(int irq, void *dev_id)
  206. {
  207. scheduler_ipi();
  208. return IRQ_HANDLED;
  209. }
  210. static irqreturn_t ipi_call_interrupt(int irq, void *dev_id)
  211. {
  212. smp_call_function_interrupt();
  213. return IRQ_HANDLED;
  214. }
  215. static struct irqaction irq_resched = {
  216. .handler = ipi_resched_interrupt,
  217. .flags = IRQF_PERCPU,
  218. .name = "IPI_resched"
  219. };
  220. static struct irqaction irq_call = {
  221. .handler = ipi_call_interrupt,
  222. .flags = IRQF_PERCPU,
  223. .name = "IPI_call"
  224. };
  225. #endif
  226. asmlinkage void plat_irq_dispatch(void)
  227. {
  228. unsigned int pending = read_c0_status() & read_c0_cause() & ST0_IM;
  229. unsigned int i;
  230. if (pending & CAUSEF_IP7) {
  231. do_IRQ(MIPS_CPU_TIMER_IRQ);
  232. goto out;
  233. } else {
  234. for (i = 0; i < 5; i++) {
  235. if (pending & (CAUSEF_IP2 << i)) {
  236. ltq_hw_irqdispatch(i);
  237. goto out;
  238. }
  239. }
  240. }
  241. pr_alert("Spurious IRQ: CAUSE=0x%08x\n", read_c0_status());
  242. out:
  243. return;
  244. }
  245. static int icu_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
  246. {
  247. struct irq_chip *chip = &ltq_irq_type;
  248. int i;
  249. for (i = 0; i < exin_avail; i++)
  250. if (hw == ltq_eiu_irq[i])
  251. chip = &ltq_eiu_type;
  252. irq_set_chip_and_handler(hw, chip, handle_level_irq);
  253. return 0;
  254. }
  255. static const struct irq_domain_ops irq_domain_ops = {
  256. .xlate = irq_domain_xlate_onetwocell,
  257. .map = icu_map,
  258. };
  259. static struct irqaction cascade = {
  260. .handler = no_action,
  261. .name = "cascade",
  262. };
  263. int __init icu_of_init(struct device_node *node, struct device_node *parent)
  264. {
  265. struct device_node *eiu_node;
  266. struct resource res;
  267. int i;
  268. if (of_address_to_resource(node, 0, &res))
  269. panic("Failed to get icu memory range");
  270. if (request_mem_region(res.start, resource_size(&res), res.name) < 0)
  271. pr_err("Failed to request icu memory");
  272. ltq_icu_membase = ioremap_nocache(res.start, resource_size(&res));
  273. if (!ltq_icu_membase)
  274. panic("Failed to remap icu memory");
  275. /* the external interrupts are optional and xway only */
  276. eiu_node = of_find_compatible_node(NULL, NULL, "lantiq,eiu");
  277. if (eiu_node && of_address_to_resource(eiu_node, 0, &res)) {
  278. /* find out how many external irq sources we have */
  279. const __be32 *count = of_get_property(node,
  280. "lantiq,count", NULL);
  281. if (count)
  282. exin_avail = *count;
  283. if (exin_avail > MAX_EIU)
  284. exin_avail = MAX_EIU;
  285. if (request_mem_region(res.start, resource_size(&res),
  286. res.name) < 0)
  287. pr_err("Failed to request eiu memory");
  288. ltq_eiu_membase = ioremap_nocache(res.start,
  289. resource_size(&res));
  290. if (!ltq_eiu_membase)
  291. panic("Failed to remap eiu memory");
  292. }
  293. /* turn off all irqs by default */
  294. for (i = 0; i < 5; i++) {
  295. /* make sure all irqs are turned off by default */
  296. ltq_icu_w32(0, LTQ_ICU_IM0_IER + (i * LTQ_ICU_OFFSET));
  297. /* clear all possibly pending interrupts */
  298. ltq_icu_w32(~0, LTQ_ICU_IM0_ISR + (i * LTQ_ICU_OFFSET));
  299. }
  300. mips_cpu_irq_init();
  301. for (i = 2; i <= 6; i++)
  302. setup_irq(i, &cascade);
  303. if (cpu_has_vint) {
  304. pr_info("Setting up vectored interrupts\n");
  305. set_vi_handler(2, ltq_hw0_irqdispatch);
  306. set_vi_handler(3, ltq_hw1_irqdispatch);
  307. set_vi_handler(4, ltq_hw2_irqdispatch);
  308. set_vi_handler(5, ltq_hw3_irqdispatch);
  309. set_vi_handler(6, ltq_hw4_irqdispatch);
  310. set_vi_handler(7, ltq_hw5_irqdispatch);
  311. }
  312. irq_domain_add_linear(node, 6 * INT_NUM_IM_OFFSET,
  313. &irq_domain_ops, 0);
  314. #if defined(CONFIG_MIPS_MT_SMP)
  315. if (cpu_has_vint) {
  316. pr_info("Setting up IPI vectored interrupts\n");
  317. set_vi_handler(MIPS_CPU_IPI_RESCHED_IRQ, ltq_sw0_irqdispatch);
  318. set_vi_handler(MIPS_CPU_IPI_CALL_IRQ, ltq_sw1_irqdispatch);
  319. }
  320. arch_init_ipiirq(MIPS_CPU_IRQ_BASE + MIPS_CPU_IPI_RESCHED_IRQ,
  321. &irq_resched);
  322. arch_init_ipiirq(MIPS_CPU_IRQ_BASE + MIPS_CPU_IPI_CALL_IRQ, &irq_call);
  323. #endif
  324. #if !defined(CONFIG_MIPS_MT_SMP) && !defined(CONFIG_MIPS_MT_SMTC)
  325. set_c0_status(IE_IRQ0 | IE_IRQ1 | IE_IRQ2 |
  326. IE_IRQ3 | IE_IRQ4 | IE_IRQ5);
  327. #else
  328. set_c0_status(IE_SW0 | IE_SW1 | IE_IRQ0 | IE_IRQ1 |
  329. IE_IRQ2 | IE_IRQ3 | IE_IRQ4 | IE_IRQ5);
  330. #endif
  331. /* tell oprofile which irq to use */
  332. cp0_perfcount_irq = LTQ_PERF_IRQ;
  333. return 0;
  334. }
  335. unsigned int __cpuinit get_c0_compare_int(void)
  336. {
  337. return CP0_LEGACY_COMPARE_IRQ;
  338. }
  339. static struct of_device_id __initdata of_irq_ids[] = {
  340. { .compatible = "lantiq,icu", .data = icu_of_init },
  341. {},
  342. };
  343. void __init arch_init_irq(void)
  344. {
  345. of_irq_init(of_irq_ids);
  346. }