exynos-combiner.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * Combiner irqchip for EXYNOS
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/err.h>
  12. #include <linux/export.h>
  13. #include <linux/init.h>
  14. #include <linux/io.h>
  15. #include <linux/slab.h>
  16. #include <linux/syscore_ops.h>
  17. #include <linux/irqdomain.h>
  18. #include <linux/irqchip.h>
  19. #include <linux/irqchip/chained_irq.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/of_address.h>
  22. #include <linux/of_irq.h>
  23. #define COMBINER_ENABLE_SET 0x0
  24. #define COMBINER_ENABLE_CLEAR 0x4
  25. #define COMBINER_INT_STATUS 0xC
  26. #define IRQ_IN_COMBINER 8
  27. static DEFINE_SPINLOCK(irq_controller_lock);
  28. struct combiner_chip_data {
  29. unsigned int hwirq_offset;
  30. unsigned int irq_mask;
  31. void __iomem *base;
  32. unsigned int parent_irq;
  33. #ifdef CONFIG_PM
  34. u32 pm_save;
  35. #endif
  36. };
  37. static struct combiner_chip_data *combiner_data;
  38. static struct irq_domain *combiner_irq_domain;
  39. static unsigned int max_nr = 20;
  40. static inline void __iomem *combiner_base(struct irq_data *data)
  41. {
  42. struct combiner_chip_data *combiner_data =
  43. irq_data_get_irq_chip_data(data);
  44. return combiner_data->base;
  45. }
  46. static void combiner_mask_irq(struct irq_data *data)
  47. {
  48. u32 mask = 1 << (data->hwirq % 32);
  49. __raw_writel(mask, combiner_base(data) + COMBINER_ENABLE_CLEAR);
  50. }
  51. static void combiner_unmask_irq(struct irq_data *data)
  52. {
  53. u32 mask = 1 << (data->hwirq % 32);
  54. __raw_writel(mask, combiner_base(data) + COMBINER_ENABLE_SET);
  55. }
  56. static void combiner_handle_cascade_irq(struct irq_desc *desc)
  57. {
  58. struct combiner_chip_data *chip_data = irq_desc_get_handler_data(desc);
  59. struct irq_chip *chip = irq_desc_get_chip(desc);
  60. unsigned int cascade_irq, combiner_irq;
  61. unsigned long status;
  62. chained_irq_enter(chip, desc);
  63. spin_lock(&irq_controller_lock);
  64. status = __raw_readl(chip_data->base + COMBINER_INT_STATUS);
  65. spin_unlock(&irq_controller_lock);
  66. status &= chip_data->irq_mask;
  67. if (status == 0)
  68. goto out;
  69. combiner_irq = chip_data->hwirq_offset + __ffs(status);
  70. cascade_irq = irq_find_mapping(combiner_irq_domain, combiner_irq);
  71. if (unlikely(!cascade_irq))
  72. handle_bad_irq(desc);
  73. else
  74. generic_handle_irq(cascade_irq);
  75. out:
  76. chained_irq_exit(chip, desc);
  77. }
  78. #ifdef CONFIG_SMP
  79. static int combiner_set_affinity(struct irq_data *d,
  80. const struct cpumask *mask_val, bool force)
  81. {
  82. struct combiner_chip_data *chip_data = irq_data_get_irq_chip_data(d);
  83. struct irq_chip *chip = irq_get_chip(chip_data->parent_irq);
  84. struct irq_data *data = irq_get_irq_data(chip_data->parent_irq);
  85. if (chip && chip->irq_set_affinity)
  86. return chip->irq_set_affinity(data, mask_val, force);
  87. else
  88. return -EINVAL;
  89. }
  90. #endif
  91. static struct irq_chip combiner_chip = {
  92. .name = "COMBINER",
  93. .irq_mask = combiner_mask_irq,
  94. .irq_unmask = combiner_unmask_irq,
  95. #ifdef CONFIG_SMP
  96. .irq_set_affinity = combiner_set_affinity,
  97. #endif
  98. };
  99. static void __init combiner_cascade_irq(struct combiner_chip_data *combiner_data,
  100. unsigned int irq)
  101. {
  102. irq_set_chained_handler_and_data(irq, combiner_handle_cascade_irq,
  103. combiner_data);
  104. }
  105. static void __init combiner_init_one(struct combiner_chip_data *combiner_data,
  106. unsigned int combiner_nr,
  107. void __iomem *base, unsigned int irq)
  108. {
  109. combiner_data->base = base;
  110. combiner_data->hwirq_offset = (combiner_nr & ~3) * IRQ_IN_COMBINER;
  111. combiner_data->irq_mask = 0xff << ((combiner_nr % 4) << 3);
  112. combiner_data->parent_irq = irq;
  113. /* Disable all interrupts */
  114. __raw_writel(combiner_data->irq_mask, base + COMBINER_ENABLE_CLEAR);
  115. }
  116. static int combiner_irq_domain_xlate(struct irq_domain *d,
  117. struct device_node *controller,
  118. const u32 *intspec, unsigned int intsize,
  119. unsigned long *out_hwirq,
  120. unsigned int *out_type)
  121. {
  122. if (irq_domain_get_of_node(d) != controller)
  123. return -EINVAL;
  124. if (intsize < 2)
  125. return -EINVAL;
  126. *out_hwirq = intspec[0] * IRQ_IN_COMBINER + intspec[1];
  127. *out_type = 0;
  128. return 0;
  129. }
  130. static int combiner_irq_domain_map(struct irq_domain *d, unsigned int irq,
  131. irq_hw_number_t hw)
  132. {
  133. struct combiner_chip_data *combiner_data = d->host_data;
  134. irq_set_chip_and_handler(irq, &combiner_chip, handle_level_irq);
  135. irq_set_chip_data(irq, &combiner_data[hw >> 3]);
  136. irq_set_probe(irq);
  137. return 0;
  138. }
  139. static const struct irq_domain_ops combiner_irq_domain_ops = {
  140. .xlate = combiner_irq_domain_xlate,
  141. .map = combiner_irq_domain_map,
  142. };
  143. static void __init combiner_init(void __iomem *combiner_base,
  144. struct device_node *np)
  145. {
  146. int i, irq;
  147. unsigned int nr_irq;
  148. nr_irq = max_nr * IRQ_IN_COMBINER;
  149. combiner_data = kcalloc(max_nr, sizeof (*combiner_data), GFP_KERNEL);
  150. if (!combiner_data) {
  151. pr_warn("%s: could not allocate combiner data\n", __func__);
  152. return;
  153. }
  154. combiner_irq_domain = irq_domain_add_linear(np, nr_irq,
  155. &combiner_irq_domain_ops, combiner_data);
  156. if (WARN_ON(!combiner_irq_domain)) {
  157. pr_warn("%s: irq domain init failed\n", __func__);
  158. return;
  159. }
  160. for (i = 0; i < max_nr; i++) {
  161. irq = irq_of_parse_and_map(np, i);
  162. combiner_init_one(&combiner_data[i], i,
  163. combiner_base + (i >> 2) * 0x10, irq);
  164. combiner_cascade_irq(&combiner_data[i], irq);
  165. }
  166. }
  167. #ifdef CONFIG_PM
  168. /**
  169. * combiner_suspend - save interrupt combiner state before suspend
  170. *
  171. * Save the interrupt enable set register for all combiner groups since
  172. * the state is lost when the system enters into a sleep state.
  173. *
  174. */
  175. static int combiner_suspend(void)
  176. {
  177. int i;
  178. for (i = 0; i < max_nr; i++)
  179. combiner_data[i].pm_save =
  180. __raw_readl(combiner_data[i].base + COMBINER_ENABLE_SET);
  181. return 0;
  182. }
  183. /**
  184. * combiner_resume - restore interrupt combiner state after resume
  185. *
  186. * Restore the interrupt enable set register for all combiner groups since
  187. * the state is lost when the system enters into a sleep state on suspend.
  188. *
  189. */
  190. static void combiner_resume(void)
  191. {
  192. int i;
  193. for (i = 0; i < max_nr; i++) {
  194. __raw_writel(combiner_data[i].irq_mask,
  195. combiner_data[i].base + COMBINER_ENABLE_CLEAR);
  196. __raw_writel(combiner_data[i].pm_save,
  197. combiner_data[i].base + COMBINER_ENABLE_SET);
  198. }
  199. }
  200. #else
  201. #define combiner_suspend NULL
  202. #define combiner_resume NULL
  203. #endif
  204. static struct syscore_ops combiner_syscore_ops = {
  205. .suspend = combiner_suspend,
  206. .resume = combiner_resume,
  207. };
  208. static int __init combiner_of_init(struct device_node *np,
  209. struct device_node *parent)
  210. {
  211. void __iomem *combiner_base;
  212. combiner_base = of_iomap(np, 0);
  213. if (!combiner_base) {
  214. pr_err("%s: failed to map combiner registers\n", __func__);
  215. return -ENXIO;
  216. }
  217. if (of_property_read_u32(np, "samsung,combiner-nr", &max_nr)) {
  218. pr_info("%s: number of combiners not specified, "
  219. "setting default as %d.\n",
  220. __func__, max_nr);
  221. }
  222. combiner_init(combiner_base, np);
  223. register_syscore_ops(&combiner_syscore_ops);
  224. return 0;
  225. }
  226. IRQCHIP_DECLARE(exynos4210_combiner, "samsung,exynos4210-combiner",
  227. combiner_of_init);