irq.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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
  100. puv3_gpio_handler(unsigned int irq, struct irq_desc *desc)
  101. {
  102. unsigned int mask;
  103. mask = readl(GPIO_GEDR);
  104. do {
  105. /*
  106. * clear down all currently active IRQ sources.
  107. * We will be processing them all.
  108. */
  109. writel(mask, GPIO_GEDR);
  110. irq = IRQ_GPIO0;
  111. do {
  112. if (mask & 1)
  113. generic_handle_irq(irq);
  114. mask >>= 1;
  115. irq++;
  116. } while (mask);
  117. mask = readl(GPIO_GEDR);
  118. } while (mask);
  119. }
  120. /*
  121. * GPIO0-27 edge IRQs need to be handled specially.
  122. * In addition, the IRQs are all collected up into one bit in the
  123. * interrupt controller registers.
  124. */
  125. static void puv3_high_gpio_ack(struct irq_data *d)
  126. {
  127. unsigned int mask = GPIO_MASK(d->irq);
  128. writel(mask, GPIO_GEDR);
  129. }
  130. static void puv3_high_gpio_mask(struct irq_data *d)
  131. {
  132. unsigned int mask = GPIO_MASK(d->irq);
  133. GPIO_IRQ_mask &= ~mask;
  134. writel(readl(GPIO_GRER) & ~mask, GPIO_GRER);
  135. writel(readl(GPIO_GFER) & ~mask, GPIO_GFER);
  136. }
  137. static void puv3_high_gpio_unmask(struct irq_data *d)
  138. {
  139. unsigned int mask = GPIO_MASK(d->irq);
  140. GPIO_IRQ_mask |= mask;
  141. writel(GPIO_IRQ_rising_edge & GPIO_IRQ_mask, GPIO_GRER);
  142. writel(GPIO_IRQ_falling_edge & GPIO_IRQ_mask, GPIO_GFER);
  143. }
  144. static int puv3_high_gpio_wake(struct irq_data *d, unsigned int on)
  145. {
  146. if (on)
  147. writel(readl(PM_PWER) | PM_PWER_GPIOHIGH, PM_PWER);
  148. else
  149. writel(readl(PM_PWER) & ~PM_PWER_GPIOHIGH, PM_PWER);
  150. return 0;
  151. }
  152. static struct irq_chip puv3_high_gpio_chip = {
  153. .name = "GPIO-high",
  154. .irq_ack = puv3_high_gpio_ack,
  155. .irq_mask = puv3_high_gpio_mask,
  156. .irq_unmask = puv3_high_gpio_unmask,
  157. .irq_set_type = puv3_gpio_type,
  158. .irq_set_wake = puv3_high_gpio_wake,
  159. };
  160. /*
  161. * We don't need to ACK IRQs on the PKUnity unless they're GPIOs
  162. * this is for internal IRQs i.e. from 8 to 31.
  163. */
  164. static void puv3_mask_irq(struct irq_data *d)
  165. {
  166. writel(readl(INTC_ICMR) & ~(1 << d->irq), INTC_ICMR);
  167. }
  168. static void puv3_unmask_irq(struct irq_data *d)
  169. {
  170. writel(readl(INTC_ICMR) | (1 << d->irq), INTC_ICMR);
  171. }
  172. /*
  173. * Apart form GPIOs, only the RTC alarm can be a wakeup event.
  174. */
  175. static int puv3_set_wake(struct irq_data *d, unsigned int on)
  176. {
  177. if (d->irq == IRQ_RTCAlarm) {
  178. if (on)
  179. writel(readl(PM_PWER) | PM_PWER_RTC, PM_PWER);
  180. else
  181. writel(readl(PM_PWER) & ~PM_PWER_RTC, PM_PWER);
  182. return 0;
  183. }
  184. return -EINVAL;
  185. }
  186. static struct irq_chip puv3_normal_chip = {
  187. .name = "PKUnity-v3",
  188. .irq_ack = puv3_mask_irq,
  189. .irq_mask = puv3_mask_irq,
  190. .irq_unmask = puv3_unmask_irq,
  191. .irq_set_wake = puv3_set_wake,
  192. };
  193. static struct resource irq_resource = {
  194. .name = "irqs",
  195. .start = io_v2p(PKUNITY_INTC_BASE),
  196. .end = io_v2p(PKUNITY_INTC_BASE) + 0xFFFFF,
  197. };
  198. static struct puv3_irq_state {
  199. unsigned int saved;
  200. unsigned int icmr;
  201. unsigned int iclr;
  202. unsigned int iccr;
  203. } puv3_irq_state;
  204. static int puv3_irq_suspend(void)
  205. {
  206. struct puv3_irq_state *st = &puv3_irq_state;
  207. st->saved = 1;
  208. st->icmr = readl(INTC_ICMR);
  209. st->iclr = readl(INTC_ICLR);
  210. st->iccr = readl(INTC_ICCR);
  211. /*
  212. * Disable all GPIO-based interrupts.
  213. */
  214. writel(readl(INTC_ICMR) & ~(0x1ff), INTC_ICMR);
  215. /*
  216. * Set the appropriate edges for wakeup.
  217. */
  218. writel(readl(PM_PWER) & GPIO_IRQ_rising_edge, GPIO_GRER);
  219. writel(readl(PM_PWER) & GPIO_IRQ_falling_edge, GPIO_GFER);
  220. /*
  221. * Clear any pending GPIO interrupts.
  222. */
  223. writel(readl(GPIO_GEDR), GPIO_GEDR);
  224. return 0;
  225. }
  226. static void puv3_irq_resume(void)
  227. {
  228. struct puv3_irq_state *st = &puv3_irq_state;
  229. if (st->saved) {
  230. writel(st->iccr, INTC_ICCR);
  231. writel(st->iclr, INTC_ICLR);
  232. writel(GPIO_IRQ_rising_edge & GPIO_IRQ_mask, GPIO_GRER);
  233. writel(GPIO_IRQ_falling_edge & GPIO_IRQ_mask, GPIO_GFER);
  234. writel(st->icmr, INTC_ICMR);
  235. }
  236. }
  237. static struct syscore_ops puv3_irq_syscore_ops = {
  238. .suspend = puv3_irq_suspend,
  239. .resume = puv3_irq_resume,
  240. };
  241. static int __init puv3_irq_init_syscore(void)
  242. {
  243. register_syscore_ops(&puv3_irq_syscore_ops);
  244. return 0;
  245. }
  246. device_initcall(puv3_irq_init_syscore);
  247. void __init init_IRQ(void)
  248. {
  249. unsigned int irq;
  250. request_resource(&iomem_resource, &irq_resource);
  251. /* disable all IRQs */
  252. writel(0, INTC_ICMR);
  253. /* all IRQs are IRQ, not REAL */
  254. writel(0, INTC_ICLR);
  255. /* clear all GPIO edge detects */
  256. writel(FMASK(8, 0) & ~FIELD(1, 1, GPI_SOFF_REQ), GPIO_GPIR);
  257. writel(0, GPIO_GFER);
  258. writel(0, GPIO_GRER);
  259. writel(0x0FFFFFFF, GPIO_GEDR);
  260. writel(1, INTC_ICCR);
  261. for (irq = 0; irq < IRQ_GPIOHIGH; irq++) {
  262. irq_set_chip(irq, &puv3_low_gpio_chip);
  263. irq_set_handler(irq, handle_edge_irq);
  264. irq_modify_status(irq,
  265. IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN,
  266. 0);
  267. }
  268. for (irq = IRQ_GPIOHIGH + 1; irq < IRQ_GPIO0; irq++) {
  269. irq_set_chip(irq, &puv3_normal_chip);
  270. irq_set_handler(irq, handle_level_irq);
  271. irq_modify_status(irq,
  272. IRQ_NOREQUEST | IRQ_NOAUTOEN,
  273. IRQ_NOPROBE);
  274. }
  275. for (irq = IRQ_GPIO0; irq <= IRQ_GPIO27; irq++) {
  276. irq_set_chip(irq, &puv3_high_gpio_chip);
  277. irq_set_handler(irq, handle_edge_irq);
  278. irq_modify_status(irq,
  279. IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN,
  280. 0);
  281. }
  282. /*
  283. * Install handler for GPIO 0-27 edge detect interrupts
  284. */
  285. irq_set_chip(IRQ_GPIOHIGH, &puv3_normal_chip);
  286. irq_set_chained_handler(IRQ_GPIOHIGH, puv3_gpio_handler);
  287. #ifdef CONFIG_PUV3_GPIO
  288. puv3_init_gpio();
  289. #endif
  290. }
  291. /*
  292. * do_IRQ handles all hardware IRQ's. Decoded IRQs should not
  293. * come via this function. Instead, they should provide their
  294. * own 'handler'
  295. */
  296. asmlinkage void asm_do_IRQ(unsigned int irq, struct pt_regs *regs)
  297. {
  298. struct pt_regs *old_regs = set_irq_regs(regs);
  299. irq_enter();
  300. /*
  301. * Some hardware gives randomly wrong interrupts. Rather
  302. * than crashing, do something sensible.
  303. */
  304. if (unlikely(irq >= nr_irqs)) {
  305. if (printk_ratelimit())
  306. printk(KERN_WARNING "Bad IRQ%u\n", irq);
  307. ack_bad_irq(irq);
  308. } else {
  309. generic_handle_irq(irq);
  310. }
  311. irq_exit();
  312. set_irq_regs(old_regs);
  313. }