irq.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * linux/arch/unicore32/kernel/irq.c
  3. *
  4. * Code specific to PKUnity SoC and UniCore ISA
  5. *
  6. * Copyright (C) 2001-2010 GUAN Xue-tao
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel_stat.h>
  13. #include <linux/module.h>
  14. #include <linux/signal.h>
  15. #include <linux/ioport.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #include <linux/random.h>
  19. #include <linux/smp.h>
  20. #include <linux/init.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/errno.h>
  23. #include <linux/list.h>
  24. #include <linux/kallsyms.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/syscore_ops.h>
  27. #include <linux/gpio.h>
  28. #include <mach/hardware.h>
  29. #include "setup.h"
  30. /*
  31. * PKUnity GPIO edge detection for IRQs:
  32. * IRQs are generated on Falling-Edge, Rising-Edge, or both.
  33. * Use this instead of directly setting GRER/GFER.
  34. */
  35. static int GPIO_IRQ_rising_edge;
  36. static int GPIO_IRQ_falling_edge;
  37. static int GPIO_IRQ_mask = 0;
  38. #define GPIO_MASK(irq) (1 << (irq - IRQ_GPIO0))
  39. static int puv3_gpio_type(struct irq_data *d, unsigned int type)
  40. {
  41. unsigned int mask;
  42. if (d->irq < IRQ_GPIOHIGH)
  43. mask = 1 << d->irq;
  44. else
  45. mask = GPIO_MASK(d->irq);
  46. if (type == IRQ_TYPE_PROBE) {
  47. if ((GPIO_IRQ_rising_edge | GPIO_IRQ_falling_edge) & mask)
  48. return 0;
  49. type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  50. }
  51. if (type & IRQ_TYPE_EDGE_RISING)
  52. GPIO_IRQ_rising_edge |= mask;
  53. else
  54. GPIO_IRQ_rising_edge &= ~mask;
  55. if (type & IRQ_TYPE_EDGE_FALLING)
  56. GPIO_IRQ_falling_edge |= mask;
  57. else
  58. GPIO_IRQ_falling_edge &= ~mask;
  59. writel(GPIO_IRQ_rising_edge & GPIO_IRQ_mask, GPIO_GRER);
  60. writel(GPIO_IRQ_falling_edge & GPIO_IRQ_mask, GPIO_GFER);
  61. return 0;
  62. }
  63. /*
  64. * GPIO IRQs must be acknowledged. This is for IRQs from 0 to 7.
  65. */
  66. static void puv3_low_gpio_ack(struct irq_data *d)
  67. {
  68. writel((1 << d->irq), GPIO_GEDR);
  69. }
  70. static void puv3_low_gpio_mask(struct irq_data *d)
  71. {
  72. writel(readl(INTC_ICMR) & ~(1 << d->irq), INTC_ICMR);
  73. }
  74. static void puv3_low_gpio_unmask(struct irq_data *d)
  75. {
  76. writel(readl(INTC_ICMR) | (1 << d->irq), INTC_ICMR);
  77. }
  78. static int puv3_low_gpio_wake(struct irq_data *d, unsigned int on)
  79. {
  80. if (on)
  81. writel(readl(PM_PWER) | (1 << d->irq), PM_PWER);
  82. else
  83. writel(readl(PM_PWER) & ~(1 << d->irq), PM_PWER);
  84. return 0;
  85. }
  86. static struct irq_chip puv3_low_gpio_chip = {
  87. .name = "GPIO-low",
  88. .irq_ack = puv3_low_gpio_ack,
  89. .irq_mask = puv3_low_gpio_mask,
  90. .irq_unmask = puv3_low_gpio_unmask,
  91. .irq_set_type = puv3_gpio_type,
  92. .irq_set_wake = puv3_low_gpio_wake,
  93. };
  94. /*
  95. * IRQ8 (GPIO0 through 27) handler. We enter here with the
  96. * irq_controller_lock held, and IRQs disabled. Decode the IRQ
  97. * and call the handler.
  98. */
  99. static void puv3_gpio_handler(struct irq_desc *desc)
  100. {
  101. unsigned int mask, irq;
  102. mask = readl(GPIO_GEDR);
  103. do {
  104. /*
  105. * clear down all currently active IRQ sources.
  106. * We will be processing them all.
  107. */
  108. writel(mask, GPIO_GEDR);
  109. irq = IRQ_GPIO0;
  110. do {
  111. if (mask & 1)
  112. generic_handle_irq(irq);
  113. mask >>= 1;
  114. irq++;
  115. } while (mask);
  116. mask = readl(GPIO_GEDR);
  117. } while (mask);
  118. }
  119. /*
  120. * GPIO0-27 edge IRQs need to be handled specially.
  121. * In addition, the IRQs are all collected up into one bit in the
  122. * interrupt controller registers.
  123. */
  124. static void puv3_high_gpio_ack(struct irq_data *d)
  125. {
  126. unsigned int mask = GPIO_MASK(d->irq);
  127. writel(mask, GPIO_GEDR);
  128. }
  129. static void puv3_high_gpio_mask(struct irq_data *d)
  130. {
  131. unsigned int mask = GPIO_MASK(d->irq);
  132. GPIO_IRQ_mask &= ~mask;
  133. writel(readl(GPIO_GRER) & ~mask, GPIO_GRER);
  134. writel(readl(GPIO_GFER) & ~mask, GPIO_GFER);
  135. }
  136. static void puv3_high_gpio_unmask(struct irq_data *d)
  137. {
  138. unsigned int mask = GPIO_MASK(d->irq);
  139. GPIO_IRQ_mask |= mask;
  140. writel(GPIO_IRQ_rising_edge & GPIO_IRQ_mask, GPIO_GRER);
  141. writel(GPIO_IRQ_falling_edge & GPIO_IRQ_mask, GPIO_GFER);
  142. }
  143. static int puv3_high_gpio_wake(struct irq_data *d, unsigned int on)
  144. {
  145. if (on)
  146. writel(readl(PM_PWER) | PM_PWER_GPIOHIGH, PM_PWER);
  147. else
  148. writel(readl(PM_PWER) & ~PM_PWER_GPIOHIGH, PM_PWER);
  149. return 0;
  150. }
  151. static struct irq_chip puv3_high_gpio_chip = {
  152. .name = "GPIO-high",
  153. .irq_ack = puv3_high_gpio_ack,
  154. .irq_mask = puv3_high_gpio_mask,
  155. .irq_unmask = puv3_high_gpio_unmask,
  156. .irq_set_type = puv3_gpio_type,
  157. .irq_set_wake = puv3_high_gpio_wake,
  158. };
  159. /*
  160. * We don't need to ACK IRQs on the PKUnity unless they're GPIOs
  161. * this is for internal IRQs i.e. from 8 to 31.
  162. */
  163. static void puv3_mask_irq(struct irq_data *d)
  164. {
  165. writel(readl(INTC_ICMR) & ~(1 << d->irq), INTC_ICMR);
  166. }
  167. static void puv3_unmask_irq(struct irq_data *d)
  168. {
  169. writel(readl(INTC_ICMR) | (1 << d->irq), INTC_ICMR);
  170. }
  171. /*
  172. * Apart form GPIOs, only the RTC alarm can be a wakeup event.
  173. */
  174. static int puv3_set_wake(struct irq_data *d, unsigned int on)
  175. {
  176. if (d->irq == IRQ_RTCAlarm) {
  177. if (on)
  178. writel(readl(PM_PWER) | PM_PWER_RTC, PM_PWER);
  179. else
  180. writel(readl(PM_PWER) & ~PM_PWER_RTC, PM_PWER);
  181. return 0;
  182. }
  183. return -EINVAL;
  184. }
  185. static struct irq_chip puv3_normal_chip = {
  186. .name = "PKUnity-v3",
  187. .irq_ack = puv3_mask_irq,
  188. .irq_mask = puv3_mask_irq,
  189. .irq_unmask = puv3_unmask_irq,
  190. .irq_set_wake = puv3_set_wake,
  191. };
  192. static struct resource irq_resource = {
  193. .name = "irqs",
  194. .start = io_v2p(PKUNITY_INTC_BASE),
  195. .end = io_v2p(PKUNITY_INTC_BASE) + 0xFFFFF,
  196. };
  197. static struct puv3_irq_state {
  198. unsigned int saved;
  199. unsigned int icmr;
  200. unsigned int iclr;
  201. unsigned int iccr;
  202. } puv3_irq_state;
  203. static int puv3_irq_suspend(void)
  204. {
  205. struct puv3_irq_state *st = &puv3_irq_state;
  206. st->saved = 1;
  207. st->icmr = readl(INTC_ICMR);
  208. st->iclr = readl(INTC_ICLR);
  209. st->iccr = readl(INTC_ICCR);
  210. /*
  211. * Disable all GPIO-based interrupts.
  212. */
  213. writel(readl(INTC_ICMR) & ~(0x1ff), INTC_ICMR);
  214. /*
  215. * Set the appropriate edges for wakeup.
  216. */
  217. writel(readl(PM_PWER) & GPIO_IRQ_rising_edge, GPIO_GRER);
  218. writel(readl(PM_PWER) & GPIO_IRQ_falling_edge, GPIO_GFER);
  219. /*
  220. * Clear any pending GPIO interrupts.
  221. */
  222. writel(readl(GPIO_GEDR), GPIO_GEDR);
  223. return 0;
  224. }
  225. static void puv3_irq_resume(void)
  226. {
  227. struct puv3_irq_state *st = &puv3_irq_state;
  228. if (st->saved) {
  229. writel(st->iccr, INTC_ICCR);
  230. writel(st->iclr, INTC_ICLR);
  231. writel(GPIO_IRQ_rising_edge & GPIO_IRQ_mask, GPIO_GRER);
  232. writel(GPIO_IRQ_falling_edge & GPIO_IRQ_mask, GPIO_GFER);
  233. writel(st->icmr, INTC_ICMR);
  234. }
  235. }
  236. static struct syscore_ops puv3_irq_syscore_ops = {
  237. .suspend = puv3_irq_suspend,
  238. .resume = puv3_irq_resume,
  239. };
  240. static int __init puv3_irq_init_syscore(void)
  241. {
  242. register_syscore_ops(&puv3_irq_syscore_ops);
  243. return 0;
  244. }
  245. device_initcall(puv3_irq_init_syscore);
  246. void __init init_IRQ(void)
  247. {
  248. unsigned int irq;
  249. request_resource(&iomem_resource, &irq_resource);
  250. /* disable all IRQs */
  251. writel(0, INTC_ICMR);
  252. /* all IRQs are IRQ, not REAL */
  253. writel(0, INTC_ICLR);
  254. /* clear all GPIO edge detects */
  255. writel(FMASK(8, 0) & ~FIELD(1, 1, GPI_SOFF_REQ), GPIO_GPIR);
  256. writel(0, GPIO_GFER);
  257. writel(0, GPIO_GRER);
  258. writel(0x0FFFFFFF, GPIO_GEDR);
  259. writel(1, INTC_ICCR);
  260. for (irq = 0; irq < IRQ_GPIOHIGH; irq++) {
  261. irq_set_chip(irq, &puv3_low_gpio_chip);
  262. irq_set_handler(irq, handle_edge_irq);
  263. irq_modify_status(irq,
  264. IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN,
  265. 0);
  266. }
  267. for (irq = IRQ_GPIOHIGH + 1; irq < IRQ_GPIO0; irq++) {
  268. irq_set_chip(irq, &puv3_normal_chip);
  269. irq_set_handler(irq, handle_level_irq);
  270. irq_modify_status(irq,
  271. IRQ_NOREQUEST | IRQ_NOAUTOEN,
  272. IRQ_NOPROBE);
  273. }
  274. for (irq = IRQ_GPIO0; irq <= IRQ_GPIO27; irq++) {
  275. irq_set_chip(irq, &puv3_high_gpio_chip);
  276. irq_set_handler(irq, handle_edge_irq);
  277. irq_modify_status(irq,
  278. IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN,
  279. 0);
  280. }
  281. /*
  282. * Install handler for GPIO 0-27 edge detect interrupts
  283. */
  284. irq_set_chip(IRQ_GPIOHIGH, &puv3_normal_chip);
  285. irq_set_chained_handler(IRQ_GPIOHIGH, puv3_gpio_handler);
  286. #ifdef CONFIG_PUV3_GPIO
  287. puv3_init_gpio();
  288. #endif
  289. }
  290. /*
  291. * do_IRQ handles all hardware IRQ's. Decoded IRQs should not
  292. * come via this function. Instead, they should provide their
  293. * own 'handler'
  294. */
  295. asmlinkage void asm_do_IRQ(unsigned int irq, struct pt_regs *regs)
  296. {
  297. struct pt_regs *old_regs = set_irq_regs(regs);
  298. irq_enter();
  299. /*
  300. * Some hardware gives randomly wrong interrupts. Rather
  301. * than crashing, do something sensible.
  302. */
  303. if (unlikely(irq >= nr_irqs)) {
  304. if (printk_ratelimit())
  305. printk(KERN_WARNING "Bad IRQ%u\n", irq);
  306. ack_bad_irq(irq);
  307. } else {
  308. generic_handle_irq(irq);
  309. }
  310. irq_exit();
  311. set_irq_regs(old_regs);
  312. }