irq.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * linux/arch/arm/mach-pxa/irq.c
  3. *
  4. * Generic PXA IRQ handling, GPIO IRQ demultiplexing, etc.
  5. *
  6. * Author: Nicolas Pitre
  7. * Created: Jun 15, 2001
  8. * Copyright: MontaVista Software Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/sysdev.h>
  18. #include <asm/hardware.h>
  19. #include <asm/irq.h>
  20. #include <asm/mach/irq.h>
  21. #include <asm/arch/pxa-regs.h>
  22. #include "generic.h"
  23. /*
  24. * This is for peripheral IRQs internal to the PXA chip.
  25. */
  26. static void pxa_mask_low_irq(unsigned int irq)
  27. {
  28. ICMR &= ~(1 << irq);
  29. }
  30. static void pxa_unmask_low_irq(unsigned int irq)
  31. {
  32. ICMR |= (1 << irq);
  33. }
  34. static struct irq_chip pxa_internal_chip_low = {
  35. .name = "SC",
  36. .ack = pxa_mask_low_irq,
  37. .mask = pxa_mask_low_irq,
  38. .unmask = pxa_unmask_low_irq,
  39. };
  40. void __init pxa_init_irq_low(void)
  41. {
  42. int irq;
  43. /* disable all IRQs */
  44. ICMR = 0;
  45. /* all IRQs are IRQ, not FIQ */
  46. ICLR = 0;
  47. /* only unmasked interrupts kick us out of idle */
  48. ICCR = 1;
  49. for (irq = PXA_IRQ(0); irq <= PXA_IRQ(31); irq++) {
  50. set_irq_chip(irq, &pxa_internal_chip_low);
  51. set_irq_handler(irq, handle_level_irq);
  52. set_irq_flags(irq, IRQF_VALID);
  53. }
  54. }
  55. #if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
  56. /*
  57. * This is for the second set of internal IRQs as found on the PXA27x.
  58. */
  59. static void pxa_mask_high_irq(unsigned int irq)
  60. {
  61. ICMR2 &= ~(1 << (irq - 32));
  62. }
  63. static void pxa_unmask_high_irq(unsigned int irq)
  64. {
  65. ICMR2 |= (1 << (irq - 32));
  66. }
  67. static struct irq_chip pxa_internal_chip_high = {
  68. .name = "SC-hi",
  69. .ack = pxa_mask_high_irq,
  70. .mask = pxa_mask_high_irq,
  71. .unmask = pxa_unmask_high_irq,
  72. };
  73. void __init pxa_init_irq_high(void)
  74. {
  75. int irq;
  76. ICMR2 = 0;
  77. ICLR2 = 0;
  78. for (irq = PXA_IRQ(32); irq < PXA_IRQ(64); irq++) {
  79. set_irq_chip(irq, &pxa_internal_chip_high);
  80. set_irq_handler(irq, handle_level_irq);
  81. set_irq_flags(irq, IRQF_VALID);
  82. }
  83. }
  84. #endif
  85. /*
  86. * PXA GPIO edge detection for IRQs:
  87. * IRQs are generated on Falling-Edge, Rising-Edge, or both.
  88. * Use this instead of directly setting GRER/GFER.
  89. */
  90. static long GPIO_IRQ_rising_edge[4];
  91. static long GPIO_IRQ_falling_edge[4];
  92. static long GPIO_IRQ_mask[4];
  93. static int pxa_gpio_irq_type(unsigned int irq, unsigned int type)
  94. {
  95. int gpio, idx;
  96. gpio = IRQ_TO_GPIO(irq);
  97. idx = gpio >> 5;
  98. if (type == IRQ_TYPE_PROBE) {
  99. /* Don't mess with enabled GPIOs using preconfigured edges or
  100. * GPIOs set to alternate function or to output during probe
  101. */
  102. if ((GPIO_IRQ_rising_edge[idx] |
  103. GPIO_IRQ_falling_edge[idx] |
  104. GPDR(gpio)) & GPIO_bit(gpio))
  105. return 0;
  106. if (GAFR(gpio) & (0x3 << (((gpio) & 0xf)*2)))
  107. return 0;
  108. type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  109. }
  110. pxa_gpio_mode(gpio | GPIO_IN);
  111. if (type & IRQ_TYPE_EDGE_RISING)
  112. __set_bit(gpio, GPIO_IRQ_rising_edge);
  113. else
  114. __clear_bit(gpio, GPIO_IRQ_rising_edge);
  115. if (type & IRQ_TYPE_EDGE_FALLING)
  116. __set_bit(gpio, GPIO_IRQ_falling_edge);
  117. else
  118. __clear_bit(gpio, GPIO_IRQ_falling_edge);
  119. GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
  120. GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
  121. pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, irq, gpio,
  122. ((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
  123. ((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
  124. return 0;
  125. }
  126. /*
  127. * GPIO IRQs must be acknowledged. This is for GPIO 0 and 1.
  128. */
  129. static void pxa_ack_low_gpio(unsigned int irq)
  130. {
  131. GEDR0 = (1 << (irq - IRQ_GPIO0));
  132. }
  133. static void pxa_mask_low_gpio(unsigned int irq)
  134. {
  135. ICMR &= ~(1 << (irq - PXA_IRQ(0)));
  136. }
  137. static void pxa_unmask_low_gpio(unsigned int irq)
  138. {
  139. ICMR |= 1 << (irq - PXA_IRQ(0));
  140. }
  141. static struct irq_chip pxa_low_gpio_chip = {
  142. .name = "GPIO-l",
  143. .ack = pxa_ack_low_gpio,
  144. .mask = pxa_mask_low_gpio,
  145. .unmask = pxa_unmask_low_gpio,
  146. .set_type = pxa_gpio_irq_type,
  147. };
  148. /*
  149. * Demux handler for GPIO>=2 edge detect interrupts
  150. */
  151. #define GEDR_BITS (sizeof(gedr) * BITS_PER_BYTE)
  152. static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
  153. {
  154. int loop, bit, n;
  155. unsigned long gedr[4];
  156. do {
  157. gedr[0] = GEDR0 & GPIO_IRQ_mask[0] & ~3;
  158. gedr[1] = GEDR1 & GPIO_IRQ_mask[1];
  159. gedr[2] = GEDR2 & GPIO_IRQ_mask[2];
  160. gedr[3] = GEDR3 & GPIO_IRQ_mask[3];
  161. GEDR0 = gedr[0]; GEDR1 = gedr[1];
  162. GEDR2 = gedr[2]; GEDR3 = gedr[3];
  163. loop = 0;
  164. bit = find_first_bit(gedr, GEDR_BITS);
  165. while (bit < GEDR_BITS) {
  166. loop = 1;
  167. n = PXA_GPIO_IRQ_BASE + bit;
  168. desc_handle_irq(n, irq_desc + n);
  169. bit = find_next_bit(gedr, GEDR_BITS, bit + 1);
  170. }
  171. } while (loop);
  172. }
  173. static void pxa_ack_muxed_gpio(unsigned int irq)
  174. {
  175. int gpio = irq - IRQ_GPIO(2) + 2;
  176. GEDR(gpio) = GPIO_bit(gpio);
  177. }
  178. static void pxa_mask_muxed_gpio(unsigned int irq)
  179. {
  180. int gpio = irq - IRQ_GPIO(2) + 2;
  181. __clear_bit(gpio, GPIO_IRQ_mask);
  182. GRER(gpio) &= ~GPIO_bit(gpio);
  183. GFER(gpio) &= ~GPIO_bit(gpio);
  184. }
  185. static void pxa_unmask_muxed_gpio(unsigned int irq)
  186. {
  187. int gpio = irq - IRQ_GPIO(2) + 2;
  188. int idx = gpio >> 5;
  189. __set_bit(gpio, GPIO_IRQ_mask);
  190. GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
  191. GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
  192. }
  193. static struct irq_chip pxa_muxed_gpio_chip = {
  194. .name = "GPIO",
  195. .ack = pxa_ack_muxed_gpio,
  196. .mask = pxa_mask_muxed_gpio,
  197. .unmask = pxa_unmask_muxed_gpio,
  198. .set_type = pxa_gpio_irq_type,
  199. };
  200. void __init pxa_init_irq_gpio(int gpio_nr)
  201. {
  202. int irq, i;
  203. pxa_last_gpio = gpio_nr - 1;
  204. /* clear all GPIO edge detects */
  205. for (i = 0; i < gpio_nr; i += 32) {
  206. GFER(i) = 0;
  207. GRER(i) = 0;
  208. GEDR(i) = GEDR(i);
  209. }
  210. /* GPIO 0 and 1 must have their mask bit always set */
  211. GPIO_IRQ_mask[0] = 3;
  212. for (irq = IRQ_GPIO0; irq <= IRQ_GPIO1; irq++) {
  213. set_irq_chip(irq, &pxa_low_gpio_chip);
  214. set_irq_handler(irq, handle_edge_irq);
  215. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  216. }
  217. for (irq = IRQ_GPIO(2); irq < IRQ_GPIO(gpio_nr); irq++) {
  218. set_irq_chip(irq, &pxa_muxed_gpio_chip);
  219. set_irq_handler(irq, handle_edge_irq);
  220. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  221. }
  222. /* Install handler for GPIO>=2 edge detect interrupts */
  223. set_irq_chained_handler(IRQ_GPIO_2_x, pxa_gpio_demux_handler);
  224. pxa_init_gpio(gpio_nr);
  225. }
  226. void __init pxa_init_gpio_set_wake(int (*set_wake)(unsigned int, unsigned int))
  227. {
  228. pxa_low_gpio_chip.set_wake = set_wake;
  229. pxa_muxed_gpio_chip.set_wake = set_wake;
  230. }
  231. void __init pxa_init_irq_set_wake(int (*set_wake)(unsigned int, unsigned int))
  232. {
  233. pxa_internal_chip_low.set_wake = set_wake;
  234. #ifdef CONFIG_PXA27x
  235. pxa_internal_chip_high.set_wake = set_wake;
  236. #endif
  237. pxa_init_gpio_set_wake(set_wake);
  238. }
  239. #ifdef CONFIG_PM
  240. static unsigned long saved_icmr[2];
  241. static int pxa_irq_suspend(struct sys_device *dev, pm_message_t state)
  242. {
  243. switch (dev->id) {
  244. case 0:
  245. saved_icmr[0] = ICMR;
  246. ICMR = 0;
  247. break;
  248. #if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
  249. case 1:
  250. saved_icmr[1] = ICMR2;
  251. ICMR2 = 0;
  252. break;
  253. #endif
  254. default:
  255. return -EINVAL;
  256. }
  257. return 0;
  258. }
  259. static int pxa_irq_resume(struct sys_device *dev)
  260. {
  261. switch (dev->id) {
  262. case 0:
  263. ICMR = saved_icmr[0];
  264. ICLR = 0;
  265. ICCR = 1;
  266. break;
  267. #if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
  268. case 1:
  269. ICMR2 = saved_icmr[1];
  270. ICLR2 = 0;
  271. break;
  272. #endif
  273. default:
  274. return -EINVAL;
  275. }
  276. return 0;
  277. }
  278. #else
  279. #define pxa_irq_suspend NULL
  280. #define pxa_irq_resume NULL
  281. #endif
  282. struct sysdev_class pxa_irq_sysclass = {
  283. .name = "irq",
  284. .suspend = pxa_irq_suspend,
  285. .resume = pxa_irq_resume,
  286. };
  287. static int __init pxa_irq_init(void)
  288. {
  289. return sysdev_class_register(&pxa_irq_sysclass);
  290. }
  291. core_initcall(pxa_irq_init);