irq.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 == IRQT_PROBE) {
  99. /* Don't mess with enabled GPIOs using preconfigured edges or
  100. GPIOs set to alternate function or to output during probe */
  101. if ((GPIO_IRQ_rising_edge[idx] | GPIO_IRQ_falling_edge[idx] | GPDR(gpio)) &
  102. GPIO_bit(gpio))
  103. return 0;
  104. if (GAFR(gpio) & (0x3 << (((gpio) & 0xf)*2)))
  105. return 0;
  106. type = __IRQT_RISEDGE | __IRQT_FALEDGE;
  107. }
  108. /* printk(KERN_DEBUG "IRQ%d (GPIO%d): ", irq, gpio); */
  109. pxa_gpio_mode(gpio | GPIO_IN);
  110. if (type & __IRQT_RISEDGE) {
  111. /* printk("rising "); */
  112. __set_bit (gpio, GPIO_IRQ_rising_edge);
  113. } else {
  114. __clear_bit (gpio, GPIO_IRQ_rising_edge);
  115. }
  116. if (type & __IRQT_FALEDGE) {
  117. /* printk("falling "); */
  118. __set_bit (gpio, GPIO_IRQ_falling_edge);
  119. } else {
  120. __clear_bit (gpio, GPIO_IRQ_falling_edge);
  121. }
  122. /* printk("edges\n"); */
  123. GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
  124. GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
  125. return 0;
  126. }
  127. /*
  128. * GPIO IRQs must be acknowledged. This is for GPIO 0 and 1.
  129. */
  130. static void pxa_ack_low_gpio(unsigned int irq)
  131. {
  132. GEDR0 = (1 << (irq - IRQ_GPIO0));
  133. }
  134. static struct irq_chip pxa_low_gpio_chip = {
  135. .name = "GPIO-l",
  136. .ack = pxa_ack_low_gpio,
  137. .mask = pxa_mask_low_irq,
  138. .unmask = pxa_unmask_low_irq,
  139. .set_type = pxa_gpio_irq_type,
  140. };
  141. /*
  142. * Demux handler for GPIO>=2 edge detect interrupts
  143. */
  144. static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
  145. {
  146. unsigned int mask;
  147. int loop;
  148. do {
  149. loop = 0;
  150. mask = GEDR0 & GPIO_IRQ_mask[0] & ~3;
  151. if (mask) {
  152. GEDR0 = mask;
  153. irq = IRQ_GPIO(2);
  154. desc = irq_desc + irq;
  155. mask >>= 2;
  156. do {
  157. if (mask & 1)
  158. desc_handle_irq(irq, desc);
  159. irq++;
  160. desc++;
  161. mask >>= 1;
  162. } while (mask);
  163. loop = 1;
  164. }
  165. mask = GEDR1 & GPIO_IRQ_mask[1];
  166. if (mask) {
  167. GEDR1 = mask;
  168. irq = IRQ_GPIO(32);
  169. desc = irq_desc + irq;
  170. do {
  171. if (mask & 1)
  172. desc_handle_irq(irq, desc);
  173. irq++;
  174. desc++;
  175. mask >>= 1;
  176. } while (mask);
  177. loop = 1;
  178. }
  179. mask = GEDR2 & GPIO_IRQ_mask[2];
  180. if (mask) {
  181. GEDR2 = mask;
  182. irq = IRQ_GPIO(64);
  183. desc = irq_desc + irq;
  184. do {
  185. if (mask & 1)
  186. desc_handle_irq(irq, desc);
  187. irq++;
  188. desc++;
  189. mask >>= 1;
  190. } while (mask);
  191. loop = 1;
  192. }
  193. mask = GEDR3 & GPIO_IRQ_mask[3];
  194. if (mask) {
  195. GEDR3 = mask;
  196. irq = IRQ_GPIO(96);
  197. desc = irq_desc + irq;
  198. do {
  199. if (mask & 1)
  200. desc_handle_irq(irq, desc);
  201. irq++;
  202. desc++;
  203. mask >>= 1;
  204. } while (mask);
  205. loop = 1;
  206. }
  207. } while (loop);
  208. }
  209. static void pxa_ack_muxed_gpio(unsigned int irq)
  210. {
  211. int gpio = irq - IRQ_GPIO(2) + 2;
  212. GEDR(gpio) = GPIO_bit(gpio);
  213. }
  214. static void pxa_mask_muxed_gpio(unsigned int irq)
  215. {
  216. int gpio = irq - IRQ_GPIO(2) + 2;
  217. __clear_bit(gpio, GPIO_IRQ_mask);
  218. GRER(gpio) &= ~GPIO_bit(gpio);
  219. GFER(gpio) &= ~GPIO_bit(gpio);
  220. }
  221. static void pxa_unmask_muxed_gpio(unsigned int irq)
  222. {
  223. int gpio = irq - IRQ_GPIO(2) + 2;
  224. int idx = gpio >> 5;
  225. __set_bit(gpio, GPIO_IRQ_mask);
  226. GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
  227. GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
  228. }
  229. static struct irq_chip pxa_muxed_gpio_chip = {
  230. .name = "GPIO",
  231. .ack = pxa_ack_muxed_gpio,
  232. .mask = pxa_mask_muxed_gpio,
  233. .unmask = pxa_unmask_muxed_gpio,
  234. .set_type = pxa_gpio_irq_type,
  235. };
  236. void __init pxa_init_irq_gpio(int gpio_nr)
  237. {
  238. int irq, i;
  239. pxa_last_gpio = gpio_nr - 1;
  240. /* clear all GPIO edge detects */
  241. for (i = 0; i < gpio_nr; i += 32) {
  242. GFER(i) = 0;
  243. GRER(i) = 0;
  244. GEDR(i) = GEDR(i);
  245. }
  246. /* GPIO 0 and 1 must have their mask bit always set */
  247. GPIO_IRQ_mask[0] = 3;
  248. for (irq = IRQ_GPIO0; irq <= IRQ_GPIO1; irq++) {
  249. set_irq_chip(irq, &pxa_low_gpio_chip);
  250. set_irq_handler(irq, handle_edge_irq);
  251. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  252. }
  253. for (irq = IRQ_GPIO(2); irq < IRQ_GPIO(gpio_nr); irq++) {
  254. set_irq_chip(irq, &pxa_muxed_gpio_chip);
  255. set_irq_handler(irq, handle_edge_irq);
  256. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  257. }
  258. /* Install handler for GPIO>=2 edge detect interrupts */
  259. set_irq_chip(IRQ_GPIO_2_x, &pxa_internal_chip_low);
  260. set_irq_chained_handler(IRQ_GPIO_2_x, pxa_gpio_demux_handler);
  261. pxa_init_gpio(gpio_nr);
  262. }
  263. void __init pxa_init_irq_set_wake(int (*set_wake)(unsigned int, unsigned int))
  264. {
  265. pxa_internal_chip_low.set_wake = set_wake;
  266. #ifdef CONFIG_PXA27x
  267. pxa_internal_chip_high.set_wake = set_wake;
  268. #endif
  269. pxa_low_gpio_chip.set_wake = set_wake;
  270. pxa_muxed_gpio_chip.set_wake = set_wake;
  271. }
  272. #ifdef CONFIG_PM
  273. static unsigned long saved_icmr[2];
  274. static int pxa_irq_suspend(struct sys_device *dev, pm_message_t state)
  275. {
  276. switch (dev->id) {
  277. case 0:
  278. saved_icmr[0] = ICMR;
  279. ICMR = 0;
  280. break;
  281. #if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
  282. case 1:
  283. saved_icmr[1] = ICMR2;
  284. ICMR2 = 0;
  285. break;
  286. #endif
  287. default:
  288. return -EINVAL;
  289. }
  290. return 0;
  291. }
  292. static int pxa_irq_resume(struct sys_device *dev)
  293. {
  294. switch (dev->id) {
  295. case 0:
  296. ICMR = saved_icmr[0];
  297. ICLR = 0;
  298. ICCR = 1;
  299. break;
  300. #if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
  301. case 1:
  302. ICMR2 = saved_icmr[1];
  303. ICLR2 = 0;
  304. break;
  305. #endif
  306. default:
  307. return -EINVAL;
  308. }
  309. return 0;
  310. }
  311. #else
  312. #define pxa_irq_suspend NULL
  313. #define pxa_irq_resume NULL
  314. #endif
  315. struct sysdev_class pxa_irq_sysclass = {
  316. .name = "irq",
  317. .suspend = pxa_irq_suspend,
  318. .resume = pxa_irq_resume,
  319. };
  320. static int __init pxa_irq_init(void)
  321. {
  322. return sysdev_class_register(&pxa_irq_sysclass);
  323. }
  324. core_initcall(pxa_irq_init);