irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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(m, x, y) ltq_w32((x), ltq_icu_membase[m] + (y))
  50. #define ltq_icu_r32(m, x) ltq_r32(ltq_icu_membase[m] + (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[MAX_IM];
  71. static void __iomem *ltq_eiu_membase;
  72. static struct irq_domain *ltq_domain;
  73. void ltq_disable_irq(struct irq_data *d)
  74. {
  75. u32 ier = LTQ_ICU_IM0_IER;
  76. int offset = d->hwirq - MIPS_CPU_IRQ_CASCADE;
  77. int im = offset / INT_NUM_IM_OFFSET;
  78. offset %= INT_NUM_IM_OFFSET;
  79. ltq_icu_w32(im, ltq_icu_r32(im, ier) & ~BIT(offset), ier);
  80. }
  81. void ltq_mask_and_ack_irq(struct irq_data *d)
  82. {
  83. u32 ier = LTQ_ICU_IM0_IER;
  84. u32 isr = LTQ_ICU_IM0_ISR;
  85. int offset = d->hwirq - MIPS_CPU_IRQ_CASCADE;
  86. int im = offset / INT_NUM_IM_OFFSET;
  87. offset %= INT_NUM_IM_OFFSET;
  88. ltq_icu_w32(im, ltq_icu_r32(im, ier) & ~BIT(offset), ier);
  89. ltq_icu_w32(im, 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. int im = offset / INT_NUM_IM_OFFSET;
  96. offset %= INT_NUM_IM_OFFSET;
  97. ltq_icu_w32(im, 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. int im = offset / INT_NUM_IM_OFFSET;
  104. offset %= INT_NUM_IM_OFFSET;
  105. ltq_icu_w32(im, ltq_icu_r32(im, 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(module, LTQ_ICU_IM0_IOSR);
  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. #if MIPS_CPU_TIMER_IRQ == 7
  188. static void ltq_hw5_irqdispatch(void)
  189. {
  190. do_IRQ(MIPS_CPU_TIMER_IRQ);
  191. }
  192. #else
  193. DEFINE_HWx_IRQDISPATCH(5)
  194. #endif
  195. #ifdef CONFIG_MIPS_MT_SMP
  196. void __init arch_init_ipiirq(int irq, struct irqaction *action)
  197. {
  198. setup_irq(irq, action);
  199. irq_set_handler(irq, handle_percpu_irq);
  200. }
  201. static void ltq_sw0_irqdispatch(void)
  202. {
  203. do_IRQ(MIPS_CPU_IRQ_BASE + MIPS_CPU_IPI_RESCHED_IRQ);
  204. }
  205. static void ltq_sw1_irqdispatch(void)
  206. {
  207. do_IRQ(MIPS_CPU_IRQ_BASE + MIPS_CPU_IPI_CALL_IRQ);
  208. }
  209. static irqreturn_t ipi_resched_interrupt(int irq, void *dev_id)
  210. {
  211. scheduler_ipi();
  212. return IRQ_HANDLED;
  213. }
  214. static irqreturn_t ipi_call_interrupt(int irq, void *dev_id)
  215. {
  216. smp_call_function_interrupt();
  217. return IRQ_HANDLED;
  218. }
  219. static struct irqaction irq_resched = {
  220. .handler = ipi_resched_interrupt,
  221. .flags = IRQF_PERCPU,
  222. .name = "IPI_resched"
  223. };
  224. static struct irqaction irq_call = {
  225. .handler = ipi_call_interrupt,
  226. .flags = IRQF_PERCPU,
  227. .name = "IPI_call"
  228. };
  229. #endif
  230. asmlinkage void plat_irq_dispatch(void)
  231. {
  232. unsigned int pending = read_c0_status() & read_c0_cause() & ST0_IM;
  233. unsigned int i;
  234. if ((MIPS_CPU_TIMER_IRQ == 7) && (pending & CAUSEF_IP7)) {
  235. do_IRQ(MIPS_CPU_TIMER_IRQ);
  236. goto out;
  237. } else {
  238. for (i = 0; i < MAX_IM; i++) {
  239. if (pending & (CAUSEF_IP2 << i)) {
  240. ltq_hw_irqdispatch(i);
  241. goto out;
  242. }
  243. }
  244. }
  245. pr_alert("Spurious IRQ: CAUSE=0x%08x\n", read_c0_status());
  246. out:
  247. return;
  248. }
  249. static int icu_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
  250. {
  251. struct irq_chip *chip = &ltq_irq_type;
  252. int i;
  253. if (hw < MIPS_CPU_IRQ_CASCADE)
  254. return 0;
  255. for (i = 0; i < exin_avail; i++)
  256. if (hw == ltq_eiu_irq[i])
  257. chip = &ltq_eiu_type;
  258. irq_set_chip_and_handler(hw, chip, handle_level_irq);
  259. return 0;
  260. }
  261. static const struct irq_domain_ops irq_domain_ops = {
  262. .xlate = irq_domain_xlate_onetwocell,
  263. .map = icu_map,
  264. };
  265. static struct irqaction cascade = {
  266. .handler = no_action,
  267. .name = "cascade",
  268. };
  269. int __init icu_of_init(struct device_node *node, struct device_node *parent)
  270. {
  271. struct device_node *eiu_node;
  272. struct resource res;
  273. int i;
  274. for (i = 0; i < MAX_IM; i++) {
  275. if (of_address_to_resource(node, i, &res))
  276. panic("Failed to get icu memory range");
  277. if (request_mem_region(res.start, resource_size(&res),
  278. res.name) < 0)
  279. pr_err("Failed to request icu memory");
  280. ltq_icu_membase[i] = ioremap_nocache(res.start,
  281. resource_size(&res));
  282. if (!ltq_icu_membase[i])
  283. panic("Failed to remap icu memory");
  284. }
  285. /* the external interrupts are optional and xway only */
  286. eiu_node = of_find_compatible_node(NULL, NULL, "lantiq,eiu");
  287. if (eiu_node && !of_address_to_resource(eiu_node, 0, &res)) {
  288. /* find out how many external irq sources we have */
  289. const __be32 *count = of_get_property(node,
  290. "lantiq,count", NULL);
  291. if (count)
  292. exin_avail = *count;
  293. if (exin_avail > MAX_EIU)
  294. exin_avail = MAX_EIU;
  295. if (request_mem_region(res.start, resource_size(&res),
  296. res.name) < 0)
  297. pr_err("Failed to request eiu memory");
  298. ltq_eiu_membase = ioremap_nocache(res.start,
  299. resource_size(&res));
  300. if (!ltq_eiu_membase)
  301. panic("Failed to remap eiu memory");
  302. }
  303. /* turn off all irqs by default */
  304. for (i = 0; i < MAX_IM; i++) {
  305. /* make sure all irqs are turned off by default */
  306. ltq_icu_w32(i, 0, LTQ_ICU_IM0_IER);
  307. /* clear all possibly pending interrupts */
  308. ltq_icu_w32(i, ~0, LTQ_ICU_IM0_ISR);
  309. }
  310. mips_cpu_irq_init();
  311. for (i = 0; i < MAX_IM; i++)
  312. setup_irq(i + 2, &cascade);
  313. if (cpu_has_vint) {
  314. pr_info("Setting up vectored interrupts\n");
  315. set_vi_handler(2, ltq_hw0_irqdispatch);
  316. set_vi_handler(3, ltq_hw1_irqdispatch);
  317. set_vi_handler(4, ltq_hw2_irqdispatch);
  318. set_vi_handler(5, ltq_hw3_irqdispatch);
  319. set_vi_handler(6, ltq_hw4_irqdispatch);
  320. set_vi_handler(7, ltq_hw5_irqdispatch);
  321. }
  322. ltq_domain = irq_domain_add_linear(node,
  323. (MAX_IM * INT_NUM_IM_OFFSET) + MIPS_CPU_IRQ_CASCADE,
  324. &irq_domain_ops, 0);
  325. #if defined(CONFIG_MIPS_MT_SMP)
  326. if (cpu_has_vint) {
  327. pr_info("Setting up IPI vectored interrupts\n");
  328. set_vi_handler(MIPS_CPU_IPI_RESCHED_IRQ, ltq_sw0_irqdispatch);
  329. set_vi_handler(MIPS_CPU_IPI_CALL_IRQ, ltq_sw1_irqdispatch);
  330. }
  331. arch_init_ipiirq(MIPS_CPU_IRQ_BASE + MIPS_CPU_IPI_RESCHED_IRQ,
  332. &irq_resched);
  333. arch_init_ipiirq(MIPS_CPU_IRQ_BASE + MIPS_CPU_IPI_CALL_IRQ, &irq_call);
  334. #endif
  335. #if !defined(CONFIG_MIPS_MT_SMP) && !defined(CONFIG_MIPS_MT_SMTC)
  336. set_c0_status(IE_IRQ0 | IE_IRQ1 | IE_IRQ2 |
  337. IE_IRQ3 | IE_IRQ4 | IE_IRQ5);
  338. #else
  339. set_c0_status(IE_SW0 | IE_SW1 | IE_IRQ0 | IE_IRQ1 |
  340. IE_IRQ2 | IE_IRQ3 | IE_IRQ4 | IE_IRQ5);
  341. #endif
  342. /* tell oprofile which irq to use */
  343. cp0_perfcount_irq = LTQ_PERF_IRQ;
  344. /*
  345. * if the timer irq is not one of the mips irqs we need to
  346. * create a mapping
  347. */
  348. if (MIPS_CPU_TIMER_IRQ != 7)
  349. irq_create_mapping(ltq_domain, MIPS_CPU_TIMER_IRQ);
  350. return 0;
  351. }
  352. unsigned int __cpuinit get_c0_compare_int(void)
  353. {
  354. return MIPS_CPU_TIMER_IRQ;
  355. }
  356. static struct of_device_id __initdata of_irq_ids[] = {
  357. { .compatible = "lantiq,icu", .data = icu_of_init },
  358. {},
  359. };
  360. void __init arch_init_irq(void)
  361. {
  362. of_irq_init(of_irq_ids);
  363. }