irq.c 13 KB

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