irq.c 11 KB

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