irq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. /* 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 i = 0, bit;
  80. if (has_aic5()) {
  81. /* disable enabled irqs */
  82. while ((bit = find_next_bit(backups, n_irqs, i)) < n_irqs) {
  83. at91_aic_write(AT91_AIC5_SSR,
  84. bit & AT91_AIC5_INTSEL_MSK);
  85. at91_aic_write(AT91_AIC5_IDCR, 1);
  86. i = bit;
  87. }
  88. /* enable wakeup irqs */
  89. i = 0;
  90. while ((bit = find_next_bit(wakeups, n_irqs, i)) < n_irqs) {
  91. at91_aic_write(AT91_AIC5_SSR,
  92. bit & AT91_AIC5_INTSEL_MSK);
  93. at91_aic_write(AT91_AIC5_IECR, 1);
  94. i = bit;
  95. }
  96. } else {
  97. at91_aic_write(AT91_AIC_IDCR, *backups);
  98. at91_aic_write(AT91_AIC_IECR, *wakeups);
  99. }
  100. }
  101. void at91_irq_resume(void)
  102. {
  103. int i = 0, bit;
  104. if (has_aic5()) {
  105. /* disable wakeup irqs */
  106. while ((bit = find_next_bit(wakeups, n_irqs, i)) < n_irqs) {
  107. at91_aic_write(AT91_AIC5_SSR,
  108. bit & AT91_AIC5_INTSEL_MSK);
  109. at91_aic_write(AT91_AIC5_IDCR, 1);
  110. i = bit;
  111. }
  112. /* enable irqs disabled for suspend */
  113. i = 0;
  114. while ((bit = find_next_bit(backups, n_irqs, i)) < n_irqs) {
  115. at91_aic_write(AT91_AIC5_SSR,
  116. bit & AT91_AIC5_INTSEL_MSK);
  117. at91_aic_write(AT91_AIC5_IECR, 1);
  118. i = bit;
  119. }
  120. } else {
  121. at91_aic_write(AT91_AIC_IDCR, *wakeups);
  122. at91_aic_write(AT91_AIC_IECR, *backups);
  123. }
  124. }
  125. #else
  126. static inline int at91_aic_pm_init(void)
  127. {
  128. return 0;
  129. }
  130. #define set_backup(bit)
  131. #define clear_backup(bit)
  132. #define at91_aic_set_wake NULL
  133. #endif /* CONFIG_PM */
  134. asmlinkage void __exception_irq_entry
  135. at91_aic_handle_irq(struct pt_regs *regs)
  136. {
  137. u32 irqnr;
  138. u32 irqstat;
  139. irqnr = at91_aic_read(AT91_AIC_IVR);
  140. irqstat = at91_aic_read(AT91_AIC_ISR);
  141. /*
  142. * ISR value is 0 when there is no current interrupt or when there is
  143. * a spurious interrupt
  144. */
  145. if (!irqstat)
  146. at91_aic_write(AT91_AIC_EOICR, 0);
  147. else
  148. handle_IRQ(irqnr, regs);
  149. }
  150. asmlinkage void __exception_irq_entry
  151. at91_aic5_handle_irq(struct pt_regs *regs)
  152. {
  153. u32 irqnr;
  154. u32 irqstat;
  155. irqnr = at91_aic_read(AT91_AIC5_IVR);
  156. irqstat = at91_aic_read(AT91_AIC5_ISR);
  157. if (!irqstat)
  158. at91_aic_write(AT91_AIC5_EOICR, 0);
  159. else
  160. handle_IRQ(irqnr, regs);
  161. }
  162. static void at91_aic_mask_irq(struct irq_data *d)
  163. {
  164. /* Disable interrupt on AIC */
  165. at91_aic_write(AT91_AIC_IDCR, 1 << d->hwirq);
  166. /* Update ISR cache */
  167. clear_backup(d->hwirq);
  168. }
  169. static void __maybe_unused at91_aic5_mask_irq(struct irq_data *d)
  170. {
  171. /* Disable interrupt on AIC5 */
  172. at91_aic_write(AT91_AIC5_SSR, d->hwirq & AT91_AIC5_INTSEL_MSK);
  173. at91_aic_write(AT91_AIC5_IDCR, 1);
  174. /* Update ISR cache */
  175. clear_backup(d->hwirq);
  176. }
  177. static void at91_aic_unmask_irq(struct irq_data *d)
  178. {
  179. /* Enable interrupt on AIC */
  180. at91_aic_write(AT91_AIC_IECR, 1 << d->hwirq);
  181. /* Update ISR cache */
  182. set_backup(d->hwirq);
  183. }
  184. static void __maybe_unused at91_aic5_unmask_irq(struct irq_data *d)
  185. {
  186. /* Enable interrupt on AIC5 */
  187. at91_aic_write(AT91_AIC5_SSR, d->hwirq & AT91_AIC5_INTSEL_MSK);
  188. at91_aic_write(AT91_AIC5_IECR, 1);
  189. /* Update ISR cache */
  190. set_backup(d->hwirq);
  191. }
  192. static void at91_aic_eoi(struct irq_data *d)
  193. {
  194. /*
  195. * Mark end-of-interrupt on AIC, the controller doesn't care about
  196. * the value written. Moreover it's a write-only register.
  197. */
  198. at91_aic_write(AT91_AIC_EOICR, 0);
  199. }
  200. static void __maybe_unused at91_aic5_eoi(struct irq_data *d)
  201. {
  202. at91_aic_write(AT91_AIC5_EOICR, 0);
  203. }
  204. unsigned long *at91_extern_irq;
  205. #define is_extern_irq(hwirq) test_bit(hwirq, at91_extern_irq)
  206. static int at91_aic_compute_srctype(struct irq_data *d, unsigned type)
  207. {
  208. int srctype;
  209. switch (type) {
  210. case IRQ_TYPE_LEVEL_HIGH:
  211. srctype = AT91_AIC_SRCTYPE_HIGH;
  212. break;
  213. case IRQ_TYPE_EDGE_RISING:
  214. srctype = AT91_AIC_SRCTYPE_RISING;
  215. break;
  216. case IRQ_TYPE_LEVEL_LOW:
  217. if ((d->hwirq == AT91_ID_FIQ) || is_extern_irq(d->hwirq)) /* only supported on external interrupts */
  218. srctype = AT91_AIC_SRCTYPE_LOW;
  219. else
  220. srctype = -EINVAL;
  221. break;
  222. case IRQ_TYPE_EDGE_FALLING:
  223. if ((d->hwirq == AT91_ID_FIQ) || is_extern_irq(d->hwirq)) /* only supported on external interrupts */
  224. srctype = AT91_AIC_SRCTYPE_FALLING;
  225. else
  226. srctype = -EINVAL;
  227. break;
  228. default:
  229. srctype = -EINVAL;
  230. }
  231. return srctype;
  232. }
  233. static int at91_aic_set_type(struct irq_data *d, unsigned type)
  234. {
  235. unsigned int smr;
  236. int srctype;
  237. srctype = at91_aic_compute_srctype(d, type);
  238. if (srctype < 0)
  239. return srctype;
  240. if (has_aic5()) {
  241. at91_aic_write(AT91_AIC5_SSR,
  242. d->hwirq & AT91_AIC5_INTSEL_MSK);
  243. smr = at91_aic_read(AT91_AIC5_SMR) & ~AT91_AIC_SRCTYPE;
  244. at91_aic_write(AT91_AIC5_SMR, smr | srctype);
  245. } else {
  246. smr = at91_aic_read(AT91_AIC_SMR(d->hwirq))
  247. & ~AT91_AIC_SRCTYPE;
  248. at91_aic_write(AT91_AIC_SMR(d->hwirq), smr | srctype);
  249. }
  250. return 0;
  251. }
  252. static struct irq_chip at91_aic_chip = {
  253. .name = "AIC",
  254. .irq_mask = at91_aic_mask_irq,
  255. .irq_unmask = at91_aic_unmask_irq,
  256. .irq_set_type = at91_aic_set_type,
  257. .irq_set_wake = at91_aic_set_wake,
  258. .irq_eoi = at91_aic_eoi,
  259. };
  260. static void __init at91_aic_hw_init(unsigned int spu_vector)
  261. {
  262. int i;
  263. /*
  264. * Perform 8 End Of Interrupt Command to make sure AIC
  265. * will not Lock out nIRQ
  266. */
  267. for (i = 0; i < 8; i++)
  268. at91_aic_write(AT91_AIC_EOICR, 0);
  269. /*
  270. * Spurious Interrupt ID in Spurious Vector Register.
  271. * When there is no current interrupt, the IRQ Vector Register
  272. * reads the value stored in AIC_SPU
  273. */
  274. at91_aic_write(AT91_AIC_SPU, spu_vector);
  275. /* No debugging in AIC: Debug (Protect) Control Register */
  276. at91_aic_write(AT91_AIC_DCR, 0);
  277. /* Disable and clear all interrupts initially */
  278. at91_aic_write(AT91_AIC_IDCR, 0xFFFFFFFF);
  279. at91_aic_write(AT91_AIC_ICCR, 0xFFFFFFFF);
  280. }
  281. static void __init __maybe_unused at91_aic5_hw_init(unsigned int spu_vector)
  282. {
  283. int i;
  284. /*
  285. * Perform 8 End Of Interrupt Command to make sure AIC
  286. * will not Lock out nIRQ
  287. */
  288. for (i = 0; i < 8; i++)
  289. at91_aic_write(AT91_AIC5_EOICR, 0);
  290. /*
  291. * Spurious Interrupt ID in Spurious Vector Register.
  292. * When there is no current interrupt, the IRQ Vector Register
  293. * reads the value stored in AIC_SPU
  294. */
  295. at91_aic_write(AT91_AIC5_SPU, spu_vector);
  296. /* No debugging in AIC: Debug (Protect) Control Register */
  297. at91_aic_write(AT91_AIC5_DCR, 0);
  298. /* Disable and clear all interrupts initially */
  299. for (i = 0; i < n_irqs; i++) {
  300. at91_aic_write(AT91_AIC5_SSR, i & AT91_AIC5_INTSEL_MSK);
  301. at91_aic_write(AT91_AIC5_IDCR, 1);
  302. at91_aic_write(AT91_AIC5_ICCR, 1);
  303. }
  304. }
  305. #if defined(CONFIG_OF)
  306. static unsigned int *at91_aic_irq_priorities;
  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. }