irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. #ifdef CONFIG_MIPS_MT_SMP
  52. int gic_present;
  53. #endif
  54. static int exin_avail;
  55. static u32 ltq_eiu_irq[MAX_EIU];
  56. static void __iomem *ltq_icu_membase[MAX_IM];
  57. static void __iomem *ltq_eiu_membase;
  58. static struct irq_domain *ltq_domain;
  59. static int ltq_perfcount_irq;
  60. int ltq_eiu_get_irq(int exin)
  61. {
  62. if (exin < exin_avail)
  63. return ltq_eiu_irq[exin];
  64. return -1;
  65. }
  66. void ltq_disable_irq(struct irq_data *d)
  67. {
  68. u32 ier = LTQ_ICU_IM0_IER;
  69. int offset = d->hwirq - MIPS_CPU_IRQ_CASCADE;
  70. int im = offset / INT_NUM_IM_OFFSET;
  71. offset %= INT_NUM_IM_OFFSET;
  72. ltq_icu_w32(im, ltq_icu_r32(im, ier) & ~BIT(offset), ier);
  73. }
  74. void ltq_mask_and_ack_irq(struct irq_data *d)
  75. {
  76. u32 ier = LTQ_ICU_IM0_IER;
  77. u32 isr = LTQ_ICU_IM0_ISR;
  78. int offset = d->hwirq - MIPS_CPU_IRQ_CASCADE;
  79. int im = offset / INT_NUM_IM_OFFSET;
  80. offset %= INT_NUM_IM_OFFSET;
  81. ltq_icu_w32(im, ltq_icu_r32(im, ier) & ~BIT(offset), ier);
  82. ltq_icu_w32(im, BIT(offset), isr);
  83. }
  84. static void ltq_ack_irq(struct irq_data *d)
  85. {
  86. u32 isr = LTQ_ICU_IM0_ISR;
  87. int offset = d->hwirq - MIPS_CPU_IRQ_CASCADE;
  88. int im = offset / INT_NUM_IM_OFFSET;
  89. offset %= INT_NUM_IM_OFFSET;
  90. ltq_icu_w32(im, BIT(offset), isr);
  91. }
  92. void ltq_enable_irq(struct irq_data *d)
  93. {
  94. u32 ier = LTQ_ICU_IM0_IER;
  95. int offset = d->hwirq - MIPS_CPU_IRQ_CASCADE;
  96. int im = offset / INT_NUM_IM_OFFSET;
  97. offset %= INT_NUM_IM_OFFSET;
  98. ltq_icu_w32(im, ltq_icu_r32(im, ier) | BIT(offset), ier);
  99. }
  100. static int ltq_eiu_settype(struct irq_data *d, unsigned int type)
  101. {
  102. int i;
  103. for (i = 0; i < exin_avail; i++) {
  104. if (d->hwirq == ltq_eiu_irq[i]) {
  105. int val = 0;
  106. int edge = 0;
  107. switch (type) {
  108. case IRQF_TRIGGER_NONE:
  109. break;
  110. case IRQF_TRIGGER_RISING:
  111. val = 1;
  112. edge = 1;
  113. break;
  114. case IRQF_TRIGGER_FALLING:
  115. val = 2;
  116. edge = 1;
  117. break;
  118. case IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING:
  119. val = 3;
  120. edge = 1;
  121. break;
  122. case IRQF_TRIGGER_HIGH:
  123. val = 5;
  124. break;
  125. case IRQF_TRIGGER_LOW:
  126. val = 6;
  127. break;
  128. default:
  129. pr_err("invalid type %d for irq %ld\n",
  130. type, d->hwirq);
  131. return -EINVAL;
  132. }
  133. if (edge)
  134. irq_set_handler(d->hwirq, handle_edge_irq);
  135. ltq_eiu_w32(ltq_eiu_r32(LTQ_EIU_EXIN_C) |
  136. (val << (i * 4)), LTQ_EIU_EXIN_C);
  137. }
  138. }
  139. return 0;
  140. }
  141. static unsigned int ltq_startup_eiu_irq(struct irq_data *d)
  142. {
  143. int i;
  144. ltq_enable_irq(d);
  145. for (i = 0; i < exin_avail; i++) {
  146. if (d->hwirq == ltq_eiu_irq[i]) {
  147. /* by default we are low level triggered */
  148. ltq_eiu_settype(d, IRQF_TRIGGER_LOW);
  149. /* clear all pending */
  150. ltq_eiu_w32(ltq_eiu_r32(LTQ_EIU_EXIN_INC) & ~BIT(i),
  151. LTQ_EIU_EXIN_INC);
  152. /* enable */
  153. ltq_eiu_w32(ltq_eiu_r32(LTQ_EIU_EXIN_INEN) | BIT(i),
  154. LTQ_EIU_EXIN_INEN);
  155. break;
  156. }
  157. }
  158. return 0;
  159. }
  160. static void ltq_shutdown_eiu_irq(struct irq_data *d)
  161. {
  162. int i;
  163. ltq_disable_irq(d);
  164. for (i = 0; i < exin_avail; i++) {
  165. if (d->hwirq == ltq_eiu_irq[i]) {
  166. /* disable */
  167. ltq_eiu_w32(ltq_eiu_r32(LTQ_EIU_EXIN_INEN) & ~BIT(i),
  168. LTQ_EIU_EXIN_INEN);
  169. break;
  170. }
  171. }
  172. }
  173. static struct irq_chip ltq_irq_type = {
  174. .name = "icu",
  175. .irq_enable = ltq_enable_irq,
  176. .irq_disable = ltq_disable_irq,
  177. .irq_unmask = ltq_enable_irq,
  178. .irq_ack = ltq_ack_irq,
  179. .irq_mask = ltq_disable_irq,
  180. .irq_mask_ack = ltq_mask_and_ack_irq,
  181. };
  182. static struct irq_chip ltq_eiu_type = {
  183. .name = "eiu",
  184. .irq_startup = ltq_startup_eiu_irq,
  185. .irq_shutdown = ltq_shutdown_eiu_irq,
  186. .irq_enable = ltq_enable_irq,
  187. .irq_disable = ltq_disable_irq,
  188. .irq_unmask = ltq_enable_irq,
  189. .irq_ack = ltq_ack_irq,
  190. .irq_mask = ltq_disable_irq,
  191. .irq_mask_ack = ltq_mask_and_ack_irq,
  192. .irq_set_type = ltq_eiu_settype,
  193. };
  194. static void ltq_hw_irqdispatch(int module)
  195. {
  196. u32 irq;
  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. do_IRQ((int)irq + MIPS_CPU_IRQ_CASCADE + (INT_NUM_IM_OFFSET * module));
  206. /* if this is a EBU irq, we need to ack it or get a deadlock */
  207. if ((irq == LTQ_ICU_EBU_IRQ) && (module == 0) && LTQ_EBU_PCC_ISTAT)
  208. ltq_ebu_w32(ltq_ebu_r32(LTQ_EBU_PCC_ISTAT) | 0x10,
  209. LTQ_EBU_PCC_ISTAT);
  210. }
  211. #define DEFINE_HWx_IRQDISPATCH(x) \
  212. static void ltq_hw ## x ## _irqdispatch(void) \
  213. { \
  214. ltq_hw_irqdispatch(x); \
  215. }
  216. DEFINE_HWx_IRQDISPATCH(0)
  217. DEFINE_HWx_IRQDISPATCH(1)
  218. DEFINE_HWx_IRQDISPATCH(2)
  219. DEFINE_HWx_IRQDISPATCH(3)
  220. DEFINE_HWx_IRQDISPATCH(4)
  221. #if MIPS_CPU_TIMER_IRQ == 7
  222. static void ltq_hw5_irqdispatch(void)
  223. {
  224. do_IRQ(MIPS_CPU_TIMER_IRQ);
  225. }
  226. #else
  227. DEFINE_HWx_IRQDISPATCH(5)
  228. #endif
  229. #ifdef CONFIG_MIPS_MT_SMP
  230. void __init arch_init_ipiirq(int irq, struct irqaction *action)
  231. {
  232. setup_irq(irq, action);
  233. irq_set_handler(irq, handle_percpu_irq);
  234. }
  235. static void ltq_sw0_irqdispatch(void)
  236. {
  237. do_IRQ(MIPS_CPU_IRQ_BASE + MIPS_CPU_IPI_RESCHED_IRQ);
  238. }
  239. static void ltq_sw1_irqdispatch(void)
  240. {
  241. do_IRQ(MIPS_CPU_IRQ_BASE + MIPS_CPU_IPI_CALL_IRQ);
  242. }
  243. static irqreturn_t ipi_resched_interrupt(int irq, void *dev_id)
  244. {
  245. scheduler_ipi();
  246. return IRQ_HANDLED;
  247. }
  248. static irqreturn_t ipi_call_interrupt(int irq, void *dev_id)
  249. {
  250. generic_smp_call_function_interrupt();
  251. return IRQ_HANDLED;
  252. }
  253. static struct irqaction irq_resched = {
  254. .handler = ipi_resched_interrupt,
  255. .flags = IRQF_PERCPU,
  256. .name = "IPI_resched"
  257. };
  258. static struct irqaction irq_call = {
  259. .handler = ipi_call_interrupt,
  260. .flags = IRQF_PERCPU,
  261. .name = "IPI_call"
  262. };
  263. #endif
  264. asmlinkage void plat_irq_dispatch(void)
  265. {
  266. unsigned int pending = read_c0_status() & read_c0_cause() & ST0_IM;
  267. unsigned int i;
  268. if ((MIPS_CPU_TIMER_IRQ == 7) && (pending & CAUSEF_IP7)) {
  269. do_IRQ(MIPS_CPU_TIMER_IRQ);
  270. goto out;
  271. } else {
  272. for (i = 0; i < MAX_IM; i++) {
  273. if (pending & (CAUSEF_IP2 << i)) {
  274. ltq_hw_irqdispatch(i);
  275. goto out;
  276. }
  277. }
  278. }
  279. pr_alert("Spurious IRQ: CAUSE=0x%08x\n", read_c0_status());
  280. out:
  281. return;
  282. }
  283. static int icu_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
  284. {
  285. struct irq_chip *chip = &ltq_irq_type;
  286. int i;
  287. if (hw < MIPS_CPU_IRQ_CASCADE)
  288. return 0;
  289. for (i = 0; i < exin_avail; i++)
  290. if (hw == ltq_eiu_irq[i])
  291. chip = &ltq_eiu_type;
  292. irq_set_chip_and_handler(irq, chip, handle_level_irq);
  293. return 0;
  294. }
  295. static const struct irq_domain_ops irq_domain_ops = {
  296. .xlate = irq_domain_xlate_onetwocell,
  297. .map = icu_map,
  298. };
  299. static struct irqaction cascade = {
  300. .handler = no_action,
  301. .name = "cascade",
  302. };
  303. int __init icu_of_init(struct device_node *node, struct device_node *parent)
  304. {
  305. struct device_node *eiu_node;
  306. struct resource res;
  307. int i, ret;
  308. for (i = 0; i < MAX_IM; i++) {
  309. if (of_address_to_resource(node, i, &res))
  310. panic("Failed to get icu memory range");
  311. if (!request_mem_region(res.start, resource_size(&res),
  312. res.name))
  313. pr_err("Failed to request icu memory");
  314. ltq_icu_membase[i] = ioremap_nocache(res.start,
  315. resource_size(&res));
  316. if (!ltq_icu_membase[i])
  317. panic("Failed to remap icu memory");
  318. }
  319. /* turn off all irqs by default */
  320. for (i = 0; i < MAX_IM; i++) {
  321. /* make sure all irqs are turned off by default */
  322. ltq_icu_w32(i, 0, LTQ_ICU_IM0_IER);
  323. /* clear all possibly pending interrupts */
  324. ltq_icu_w32(i, ~0, LTQ_ICU_IM0_ISR);
  325. }
  326. mips_cpu_irq_init();
  327. for (i = 0; i < MAX_IM; i++)
  328. setup_irq(i + 2, &cascade);
  329. if (cpu_has_vint) {
  330. pr_info("Setting up vectored interrupts\n");
  331. set_vi_handler(2, ltq_hw0_irqdispatch);
  332. set_vi_handler(3, ltq_hw1_irqdispatch);
  333. set_vi_handler(4, ltq_hw2_irqdispatch);
  334. set_vi_handler(5, ltq_hw3_irqdispatch);
  335. set_vi_handler(6, ltq_hw4_irqdispatch);
  336. set_vi_handler(7, ltq_hw5_irqdispatch);
  337. }
  338. ltq_domain = irq_domain_add_linear(node,
  339. (MAX_IM * INT_NUM_IM_OFFSET) + MIPS_CPU_IRQ_CASCADE,
  340. &irq_domain_ops, 0);
  341. #if defined(CONFIG_MIPS_MT_SMP)
  342. if (cpu_has_vint) {
  343. pr_info("Setting up IPI vectored interrupts\n");
  344. set_vi_handler(MIPS_CPU_IPI_RESCHED_IRQ, ltq_sw0_irqdispatch);
  345. set_vi_handler(MIPS_CPU_IPI_CALL_IRQ, ltq_sw1_irqdispatch);
  346. }
  347. arch_init_ipiirq(MIPS_CPU_IRQ_BASE + MIPS_CPU_IPI_RESCHED_IRQ,
  348. &irq_resched);
  349. arch_init_ipiirq(MIPS_CPU_IRQ_BASE + MIPS_CPU_IPI_CALL_IRQ, &irq_call);
  350. #endif
  351. #ifndef CONFIG_MIPS_MT_SMP
  352. set_c0_status(IE_IRQ0 | IE_IRQ1 | IE_IRQ2 |
  353. IE_IRQ3 | IE_IRQ4 | IE_IRQ5);
  354. #else
  355. set_c0_status(IE_SW0 | IE_SW1 | IE_IRQ0 | IE_IRQ1 |
  356. IE_IRQ2 | IE_IRQ3 | IE_IRQ4 | IE_IRQ5);
  357. #endif
  358. /* tell oprofile which irq to use */
  359. ltq_perfcount_irq = irq_create_mapping(ltq_domain, LTQ_PERF_IRQ);
  360. /*
  361. * if the timer irq is not one of the mips irqs we need to
  362. * create a mapping
  363. */
  364. if (MIPS_CPU_TIMER_IRQ != 7)
  365. irq_create_mapping(ltq_domain, MIPS_CPU_TIMER_IRQ);
  366. /* the external interrupts are optional and xway only */
  367. eiu_node = of_find_compatible_node(NULL, NULL, "lantiq,eiu-xway");
  368. if (eiu_node && !of_address_to_resource(eiu_node, 0, &res)) {
  369. /* find out how many external irq sources we have */
  370. exin_avail = of_property_count_u32_elems(eiu_node,
  371. "lantiq,eiu-irqs");
  372. if (exin_avail > MAX_EIU)
  373. exin_avail = MAX_EIU;
  374. ret = of_property_read_u32_array(eiu_node, "lantiq,eiu-irqs",
  375. ltq_eiu_irq, exin_avail);
  376. if (ret)
  377. panic("failed to load external irq resources");
  378. if (!request_mem_region(res.start, resource_size(&res),
  379. res.name))
  380. pr_err("Failed to request eiu memory");
  381. ltq_eiu_membase = ioremap_nocache(res.start,
  382. resource_size(&res));
  383. if (!ltq_eiu_membase)
  384. panic("Failed to remap eiu memory");
  385. }
  386. return 0;
  387. }
  388. int get_c0_perfcount_int(void)
  389. {
  390. return ltq_perfcount_irq;
  391. }
  392. EXPORT_SYMBOL_GPL(get_c0_perfcount_int);
  393. unsigned int get_c0_compare_int(void)
  394. {
  395. return MIPS_CPU_TIMER_IRQ;
  396. }
  397. static struct of_device_id __initdata of_irq_ids[] = {
  398. { .compatible = "lantiq,icu", .data = icu_of_init },
  399. {},
  400. };
  401. void __init arch_init_irq(void)
  402. {
  403. of_irq_init(of_irq_ids);
  404. }