irq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * linux/arch/arm/mach-at91/irq.c
  3. *
  4. * Copyright (C) 2004 SAN People
  5. * Copyright (C) 2004 ATMEL
  6. * Copyright (C) Rick Bronson
  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 as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/mm.h>
  25. #include <linux/bitmap.h>
  26. #include <linux/types.h>
  27. #include <linux/irq.h>
  28. #include <linux/of.h>
  29. #include <linux/of_address.h>
  30. #include <linux/of_irq.h>
  31. #include <linux/irqdomain.h>
  32. #include <linux/err.h>
  33. #include <linux/slab.h>
  34. #include <mach/hardware.h>
  35. #include <asm/irq.h>
  36. #include <asm/setup.h>
  37. #include <asm/exception.h>
  38. #include <asm/mach/arch.h>
  39. #include <asm/mach/irq.h>
  40. #include <asm/mach/map.h>
  41. #include "at91_aic.h"
  42. void __iomem *at91_aic_base;
  43. static struct irq_domain *at91_aic_domain;
  44. static struct device_node *at91_aic_np;
  45. static unsigned int n_irqs = NR_AIC_IRQS;
  46. static unsigned long at91_aic_caps = 0;
  47. /* AIC5 introduces a Source Select Register */
  48. #define AT91_AIC_CAP_AIC5 (1 << 0)
  49. #define has_aic5() (at91_aic_caps & AT91_AIC_CAP_AIC5)
  50. #ifdef CONFIG_PM
  51. static unsigned long *wakeups;
  52. static unsigned long *backups;
  53. #define set_backup(bit) set_bit(bit, backups)
  54. #define clear_backup(bit) clear_bit(bit, backups)
  55. static int at91_aic_pm_init(void)
  56. {
  57. backups = kzalloc(BITS_TO_LONGS(n_irqs) * sizeof(*backups), GFP_KERNEL);
  58. if (!backups)
  59. return -ENOMEM;
  60. wakeups = kzalloc(BITS_TO_LONGS(n_irqs) * sizeof(*backups), GFP_KERNEL);
  61. if (!wakeups) {
  62. kfree(backups);
  63. return -ENOMEM;
  64. }
  65. return 0;
  66. }
  67. static int at91_aic_set_wake(struct irq_data *d, unsigned value)
  68. {
  69. if (unlikely(d->hwirq >= n_irqs))
  70. return -EINVAL;
  71. if (value)
  72. set_bit(d->hwirq, wakeups);
  73. else
  74. clear_bit(d->hwirq, wakeups);
  75. return 0;
  76. }
  77. void at91_irq_suspend(void)
  78. {
  79. int bit = -1;
  80. if (has_aic5()) {
  81. /* disable enabled irqs */
  82. while ((bit = find_next_bit(backups, n_irqs, bit + 1)) < n_irqs) {
  83. at91_aic_write(AT91_AIC5_SSR,
  84. bit & AT91_AIC5_INTSEL_MSK);
  85. at91_aic_write(AT91_AIC5_IDCR, 1);
  86. }
  87. /* enable wakeup irqs */
  88. bit = -1;
  89. while ((bit = find_next_bit(wakeups, n_irqs, bit + 1)) < n_irqs) {
  90. at91_aic_write(AT91_AIC5_SSR,
  91. bit & AT91_AIC5_INTSEL_MSK);
  92. at91_aic_write(AT91_AIC5_IECR, 1);
  93. }
  94. } else {
  95. at91_aic_write(AT91_AIC_IDCR, *backups);
  96. at91_aic_write(AT91_AIC_IECR, *wakeups);
  97. }
  98. }
  99. void at91_irq_resume(void)
  100. {
  101. int bit = -1;
  102. if (has_aic5()) {
  103. /* disable wakeup irqs */
  104. while ((bit = find_next_bit(wakeups, n_irqs, bit + 1)) < n_irqs) {
  105. at91_aic_write(AT91_AIC5_SSR,
  106. bit & AT91_AIC5_INTSEL_MSK);
  107. at91_aic_write(AT91_AIC5_IDCR, 1);
  108. }
  109. /* enable irqs disabled for suspend */
  110. bit = -1;
  111. while ((bit = find_next_bit(backups, n_irqs, bit + 1)) < n_irqs) {
  112. at91_aic_write(AT91_AIC5_SSR,
  113. bit & AT91_AIC5_INTSEL_MSK);
  114. at91_aic_write(AT91_AIC5_IECR, 1);
  115. }
  116. } else {
  117. at91_aic_write(AT91_AIC_IDCR, *wakeups);
  118. at91_aic_write(AT91_AIC_IECR, *backups);
  119. }
  120. }
  121. #else
  122. static inline int at91_aic_pm_init(void)
  123. {
  124. return 0;
  125. }
  126. #define set_backup(bit)
  127. #define clear_backup(bit)
  128. #define at91_aic_set_wake NULL
  129. #endif /* CONFIG_PM */
  130. asmlinkage void __exception_irq_entry
  131. at91_aic_handle_irq(struct pt_regs *regs)
  132. {
  133. u32 irqnr;
  134. u32 irqstat;
  135. irqnr = at91_aic_read(AT91_AIC_IVR);
  136. irqstat = at91_aic_read(AT91_AIC_ISR);
  137. /*
  138. * ISR value is 0 when there is no current interrupt or when there is
  139. * a spurious interrupt
  140. */
  141. if (!irqstat)
  142. at91_aic_write(AT91_AIC_EOICR, 0);
  143. else
  144. handle_IRQ(irqnr, regs);
  145. }
  146. asmlinkage void __exception_irq_entry
  147. at91_aic5_handle_irq(struct pt_regs *regs)
  148. {
  149. u32 irqnr;
  150. u32 irqstat;
  151. irqnr = at91_aic_read(AT91_AIC5_IVR);
  152. irqstat = at91_aic_read(AT91_AIC5_ISR);
  153. if (!irqstat)
  154. at91_aic_write(AT91_AIC5_EOICR, 0);
  155. else
  156. handle_IRQ(irqnr, regs);
  157. }
  158. static void at91_aic_mask_irq(struct irq_data *d)
  159. {
  160. /* Disable interrupt on AIC */
  161. at91_aic_write(AT91_AIC_IDCR, 1 << d->hwirq);
  162. /* Update ISR cache */
  163. clear_backup(d->hwirq);
  164. }
  165. static void __maybe_unused at91_aic5_mask_irq(struct irq_data *d)
  166. {
  167. /* Disable interrupt on AIC5 */
  168. at91_aic_write(AT91_AIC5_SSR, d->hwirq & AT91_AIC5_INTSEL_MSK);
  169. at91_aic_write(AT91_AIC5_IDCR, 1);
  170. /* Update ISR cache */
  171. clear_backup(d->hwirq);
  172. }
  173. static void at91_aic_unmask_irq(struct irq_data *d)
  174. {
  175. /* Enable interrupt on AIC */
  176. at91_aic_write(AT91_AIC_IECR, 1 << d->hwirq);
  177. /* Update ISR cache */
  178. set_backup(d->hwirq);
  179. }
  180. static void __maybe_unused at91_aic5_unmask_irq(struct irq_data *d)
  181. {
  182. /* Enable interrupt on AIC5 */
  183. at91_aic_write(AT91_AIC5_SSR, d->hwirq & AT91_AIC5_INTSEL_MSK);
  184. at91_aic_write(AT91_AIC5_IECR, 1);
  185. /* Update ISR cache */
  186. set_backup(d->hwirq);
  187. }
  188. static void at91_aic_eoi(struct irq_data *d)
  189. {
  190. /*
  191. * Mark end-of-interrupt on AIC, the controller doesn't care about
  192. * the value written. Moreover it's a write-only register.
  193. */
  194. at91_aic_write(AT91_AIC_EOICR, 0);
  195. }
  196. static void __maybe_unused at91_aic5_eoi(struct irq_data *d)
  197. {
  198. at91_aic_write(AT91_AIC5_EOICR, 0);
  199. }
  200. unsigned long *at91_extern_irq;
  201. #define is_extern_irq(hwirq) test_bit(hwirq, at91_extern_irq)
  202. static int at91_aic_compute_srctype(struct irq_data *d, unsigned type)
  203. {
  204. int srctype;
  205. switch (type) {
  206. case IRQ_TYPE_LEVEL_HIGH:
  207. srctype = AT91_AIC_SRCTYPE_HIGH;
  208. break;
  209. case IRQ_TYPE_EDGE_RISING:
  210. srctype = AT91_AIC_SRCTYPE_RISING;
  211. break;
  212. case IRQ_TYPE_LEVEL_LOW:
  213. if ((d->hwirq == AT91_ID_FIQ) || is_extern_irq(d->hwirq)) /* only supported on external interrupts */
  214. srctype = AT91_AIC_SRCTYPE_LOW;
  215. else
  216. srctype = -EINVAL;
  217. break;
  218. case IRQ_TYPE_EDGE_FALLING:
  219. if ((d->hwirq == AT91_ID_FIQ) || is_extern_irq(d->hwirq)) /* only supported on external interrupts */
  220. srctype = AT91_AIC_SRCTYPE_FALLING;
  221. else
  222. srctype = -EINVAL;
  223. break;
  224. default:
  225. srctype = -EINVAL;
  226. }
  227. return srctype;
  228. }
  229. static int at91_aic_set_type(struct irq_data *d, unsigned type)
  230. {
  231. unsigned int smr;
  232. int srctype;
  233. srctype = at91_aic_compute_srctype(d, type);
  234. if (srctype < 0)
  235. return srctype;
  236. if (has_aic5()) {
  237. at91_aic_write(AT91_AIC5_SSR,
  238. d->hwirq & AT91_AIC5_INTSEL_MSK);
  239. smr = at91_aic_read(AT91_AIC5_SMR) & ~AT91_AIC_SRCTYPE;
  240. at91_aic_write(AT91_AIC5_SMR, smr | srctype);
  241. } else {
  242. smr = at91_aic_read(AT91_AIC_SMR(d->hwirq))
  243. & ~AT91_AIC_SRCTYPE;
  244. at91_aic_write(AT91_AIC_SMR(d->hwirq), smr | srctype);
  245. }
  246. return 0;
  247. }
  248. static struct irq_chip at91_aic_chip = {
  249. .name = "AIC",
  250. .irq_mask = at91_aic_mask_irq,
  251. .irq_unmask = at91_aic_unmask_irq,
  252. .irq_set_type = at91_aic_set_type,
  253. .irq_set_wake = at91_aic_set_wake,
  254. .irq_eoi = at91_aic_eoi,
  255. };
  256. static void __init at91_aic_hw_init(unsigned int spu_vector)
  257. {
  258. int i;
  259. /*
  260. * Perform 8 End Of Interrupt Command to make sure AIC
  261. * will not Lock out nIRQ
  262. */
  263. for (i = 0; i < 8; i++)
  264. at91_aic_write(AT91_AIC_EOICR, 0);
  265. /*
  266. * Spurious Interrupt ID in Spurious Vector Register.
  267. * When there is no current interrupt, the IRQ Vector Register
  268. * reads the value stored in AIC_SPU
  269. */
  270. at91_aic_write(AT91_AIC_SPU, spu_vector);
  271. /* No debugging in AIC: Debug (Protect) Control Register */
  272. at91_aic_write(AT91_AIC_DCR, 0);
  273. /* Disable and clear all interrupts initially */
  274. at91_aic_write(AT91_AIC_IDCR, 0xFFFFFFFF);
  275. at91_aic_write(AT91_AIC_ICCR, 0xFFFFFFFF);
  276. }
  277. static void __init __maybe_unused at91_aic5_hw_init(unsigned int spu_vector)
  278. {
  279. int i;
  280. /*
  281. * Perform 8 End Of Interrupt Command to make sure AIC
  282. * will not Lock out nIRQ
  283. */
  284. for (i = 0; i < 8; i++)
  285. at91_aic_write(AT91_AIC5_EOICR, 0);
  286. /*
  287. * Spurious Interrupt ID in Spurious Vector Register.
  288. * When there is no current interrupt, the IRQ Vector Register
  289. * reads the value stored in AIC_SPU
  290. */
  291. at91_aic_write(AT91_AIC5_SPU, spu_vector);
  292. /* No debugging in AIC: Debug (Protect) Control Register */
  293. at91_aic_write(AT91_AIC5_DCR, 0);
  294. /* Disable and clear all interrupts initially */
  295. for (i = 0; i < n_irqs; i++) {
  296. at91_aic_write(AT91_AIC5_SSR, i & AT91_AIC5_INTSEL_MSK);
  297. at91_aic_write(AT91_AIC5_IDCR, 1);
  298. at91_aic_write(AT91_AIC5_ICCR, 1);
  299. }
  300. }
  301. #if defined(CONFIG_OF)
  302. static unsigned int *at91_aic_irq_priorities;
  303. static int at91_aic_irq_map(struct irq_domain *h, unsigned int virq,
  304. irq_hw_number_t hw)
  305. {
  306. /* Put virq number in Source Vector Register */
  307. at91_aic_write(AT91_AIC_SVR(hw), virq);
  308. /* Active Low interrupt, with priority */
  309. at91_aic_write(AT91_AIC_SMR(hw),
  310. AT91_AIC_SRCTYPE_LOW | at91_aic_irq_priorities[hw]);
  311. irq_set_chip_and_handler(virq, &at91_aic_chip, handle_fasteoi_irq);
  312. set_irq_flags(virq, IRQF_VALID | IRQF_PROBE);
  313. return 0;
  314. }
  315. static int at91_aic5_irq_map(struct irq_domain *h, unsigned int virq,
  316. irq_hw_number_t hw)
  317. {
  318. at91_aic_write(AT91_AIC5_SSR, hw & AT91_AIC5_INTSEL_MSK);
  319. /* Put virq number in Source Vector Register */
  320. at91_aic_write(AT91_AIC5_SVR, virq);
  321. /* Active Low interrupt, with priority */
  322. at91_aic_write(AT91_AIC5_SMR,
  323. AT91_AIC_SRCTYPE_LOW | at91_aic_irq_priorities[hw]);
  324. irq_set_chip_and_handler(virq, &at91_aic_chip, handle_fasteoi_irq);
  325. set_irq_flags(virq, IRQF_VALID | IRQF_PROBE);
  326. return 0;
  327. }
  328. static int at91_aic_irq_domain_xlate(struct irq_domain *d, struct device_node *ctrlr,
  329. const u32 *intspec, unsigned int intsize,
  330. irq_hw_number_t *out_hwirq, unsigned int *out_type)
  331. {
  332. if (WARN_ON(intsize < 3))
  333. return -EINVAL;
  334. if (WARN_ON(intspec[0] >= n_irqs))
  335. return -EINVAL;
  336. if (WARN_ON((intspec[2] < AT91_AIC_IRQ_MIN_PRIORITY)
  337. || (intspec[2] > AT91_AIC_IRQ_MAX_PRIORITY)))
  338. return -EINVAL;
  339. *out_hwirq = intspec[0];
  340. *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
  341. at91_aic_irq_priorities[*out_hwirq] = intspec[2];
  342. return 0;
  343. }
  344. static struct irq_domain_ops at91_aic_irq_ops = {
  345. .map = at91_aic_irq_map,
  346. .xlate = at91_aic_irq_domain_xlate,
  347. };
  348. int __init at91_aic_of_common_init(struct device_node *node,
  349. struct device_node *parent)
  350. {
  351. struct property *prop;
  352. const __be32 *p;
  353. u32 val;
  354. at91_extern_irq = kzalloc(BITS_TO_LONGS(n_irqs)
  355. * sizeof(*at91_extern_irq), GFP_KERNEL);
  356. if (!at91_extern_irq)
  357. return -ENOMEM;
  358. if (at91_aic_pm_init()) {
  359. kfree(at91_extern_irq);
  360. return -ENOMEM;
  361. }
  362. at91_aic_irq_priorities = kzalloc(n_irqs
  363. * sizeof(*at91_aic_irq_priorities),
  364. GFP_KERNEL);
  365. if (!at91_aic_irq_priorities)
  366. return -ENOMEM;
  367. at91_aic_base = of_iomap(node, 0);
  368. at91_aic_np = node;
  369. at91_aic_domain = irq_domain_add_linear(at91_aic_np, n_irqs,
  370. &at91_aic_irq_ops, NULL);
  371. if (!at91_aic_domain)
  372. panic("Unable to add AIC irq domain (DT)\n");
  373. of_property_for_each_u32(node, "atmel,external-irqs", prop, p, val) {
  374. if (val >= n_irqs)
  375. pr_warn("AIC: external irq %d >= %d skip it\n",
  376. val, n_irqs);
  377. else
  378. set_bit(val, at91_extern_irq);
  379. }
  380. irq_set_default_host(at91_aic_domain);
  381. return 0;
  382. }
  383. int __init at91_aic_of_init(struct device_node *node,
  384. struct device_node *parent)
  385. {
  386. int err;
  387. err = at91_aic_of_common_init(node, parent);
  388. if (err)
  389. return err;
  390. at91_aic_hw_init(n_irqs);
  391. return 0;
  392. }
  393. int __init at91_aic5_of_init(struct device_node *node,
  394. struct device_node *parent)
  395. {
  396. int err;
  397. at91_aic_caps |= AT91_AIC_CAP_AIC5;
  398. n_irqs = NR_AIC5_IRQS;
  399. at91_aic_chip.irq_ack = at91_aic5_mask_irq;
  400. at91_aic_chip.irq_mask = at91_aic5_mask_irq;
  401. at91_aic_chip.irq_unmask = at91_aic5_unmask_irq;
  402. at91_aic_chip.irq_eoi = at91_aic5_eoi;
  403. at91_aic_irq_ops.map = at91_aic5_irq_map;
  404. err = at91_aic_of_common_init(node, parent);
  405. if (err)
  406. return err;
  407. at91_aic5_hw_init(n_irqs);
  408. return 0;
  409. }
  410. #endif
  411. /*
  412. * Initialize the AIC interrupt controller.
  413. */
  414. void __init at91_aic_init(unsigned int *priority, unsigned int ext_irq_mask)
  415. {
  416. unsigned int i;
  417. int irq_base;
  418. at91_extern_irq = kzalloc(BITS_TO_LONGS(n_irqs)
  419. * sizeof(*at91_extern_irq), GFP_KERNEL);
  420. if (at91_aic_pm_init() || at91_extern_irq == NULL)
  421. panic("Unable to allocate bit maps\n");
  422. *at91_extern_irq = ext_irq_mask;
  423. at91_aic_base = ioremap(AT91_AIC, 512);
  424. if (!at91_aic_base)
  425. panic("Unable to ioremap AIC registers\n");
  426. /* Add irq domain for AIC */
  427. irq_base = irq_alloc_descs(-1, 0, n_irqs, 0);
  428. if (irq_base < 0) {
  429. WARN(1, "Cannot allocate irq_descs, assuming pre-allocated\n");
  430. irq_base = 0;
  431. }
  432. at91_aic_domain = irq_domain_add_legacy(at91_aic_np, n_irqs,
  433. irq_base, 0,
  434. &irq_domain_simple_ops, NULL);
  435. if (!at91_aic_domain)
  436. panic("Unable to add AIC irq domain\n");
  437. irq_set_default_host(at91_aic_domain);
  438. /*
  439. * The IVR is used by macro get_irqnr_and_base to read and verify.
  440. * The irq number is NR_AIC_IRQS when a spurious interrupt has occurred.
  441. */
  442. for (i = 0; i < n_irqs; i++) {
  443. /* Put hardware irq number in Source Vector Register: */
  444. at91_aic_write(AT91_AIC_SVR(i), NR_IRQS_LEGACY + i);
  445. /* Active Low interrupt, with the specified priority */
  446. at91_aic_write(AT91_AIC_SMR(i), AT91_AIC_SRCTYPE_LOW | priority[i]);
  447. irq_set_chip_and_handler(NR_IRQS_LEGACY + i, &at91_aic_chip, handle_fasteoi_irq);
  448. set_irq_flags(i, IRQF_VALID | IRQF_PROBE);
  449. }
  450. at91_aic_hw_init(n_irqs);
  451. }