irq.c 8.3 KB

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