irq-bcm7038-l1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Broadcom BCM7038 style Level 1 interrupt controller driver
  4. *
  5. * Copyright (C) 2014 Broadcom Corporation
  6. * Author: Kevin Cernekee
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/bitops.h>
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/ioport.h>
  15. #include <linux/irq.h>
  16. #include <linux/irqdomain.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include <linux/smp.h>
  25. #include <linux/types.h>
  26. #include <linux/irqchip.h>
  27. #include <linux/irqchip/chained_irq.h>
  28. #include <linux/syscore_ops.h>
  29. #define IRQS_PER_WORD 32
  30. #define REG_BYTES_PER_IRQ_WORD (sizeof(u32) * 4)
  31. #define MAX_WORDS 8
  32. struct bcm7038_l1_cpu;
  33. struct bcm7038_l1_chip {
  34. raw_spinlock_t lock;
  35. unsigned int n_words;
  36. struct irq_domain *domain;
  37. struct bcm7038_l1_cpu *cpus[NR_CPUS];
  38. #ifdef CONFIG_PM_SLEEP
  39. struct list_head list;
  40. u32 wake_mask[MAX_WORDS];
  41. #endif
  42. u32 irq_fwd_mask[MAX_WORDS];
  43. u8 affinity[MAX_WORDS * IRQS_PER_WORD];
  44. };
  45. struct bcm7038_l1_cpu {
  46. void __iomem *map_base;
  47. u32 mask_cache[];
  48. };
  49. /*
  50. * STATUS/MASK_STATUS/MASK_SET/MASK_CLEAR are packed one right after another:
  51. *
  52. * 7038:
  53. * 0x1000_1400: W0_STATUS
  54. * 0x1000_1404: W1_STATUS
  55. * 0x1000_1408: W0_MASK_STATUS
  56. * 0x1000_140c: W1_MASK_STATUS
  57. * 0x1000_1410: W0_MASK_SET
  58. * 0x1000_1414: W1_MASK_SET
  59. * 0x1000_1418: W0_MASK_CLEAR
  60. * 0x1000_141c: W1_MASK_CLEAR
  61. *
  62. * 7445:
  63. * 0xf03e_1500: W0_STATUS
  64. * 0xf03e_1504: W1_STATUS
  65. * 0xf03e_1508: W2_STATUS
  66. * 0xf03e_150c: W3_STATUS
  67. * 0xf03e_1510: W4_STATUS
  68. * 0xf03e_1514: W0_MASK_STATUS
  69. * 0xf03e_1518: W1_MASK_STATUS
  70. * [...]
  71. */
  72. static inline unsigned int reg_status(struct bcm7038_l1_chip *intc,
  73. unsigned int word)
  74. {
  75. return (0 * intc->n_words + word) * sizeof(u32);
  76. }
  77. static inline unsigned int reg_mask_status(struct bcm7038_l1_chip *intc,
  78. unsigned int word)
  79. {
  80. return (1 * intc->n_words + word) * sizeof(u32);
  81. }
  82. static inline unsigned int reg_mask_set(struct bcm7038_l1_chip *intc,
  83. unsigned int word)
  84. {
  85. return (2 * intc->n_words + word) * sizeof(u32);
  86. }
  87. static inline unsigned int reg_mask_clr(struct bcm7038_l1_chip *intc,
  88. unsigned int word)
  89. {
  90. return (3 * intc->n_words + word) * sizeof(u32);
  91. }
  92. static inline u32 l1_readl(void __iomem *reg)
  93. {
  94. if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  95. return ioread32be(reg);
  96. else
  97. return readl(reg);
  98. }
  99. static inline void l1_writel(u32 val, void __iomem *reg)
  100. {
  101. if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  102. iowrite32be(val, reg);
  103. else
  104. writel(val, reg);
  105. }
  106. static void bcm7038_l1_irq_handle(struct irq_desc *desc)
  107. {
  108. struct bcm7038_l1_chip *intc = irq_desc_get_handler_data(desc);
  109. struct bcm7038_l1_cpu *cpu;
  110. struct irq_chip *chip = irq_desc_get_chip(desc);
  111. unsigned int idx;
  112. #if defined(CONFIG_SMP) && defined(CONFIG_MIPS)
  113. cpu = intc->cpus[cpu_logical_map(smp_processor_id())];
  114. #else
  115. cpu = intc->cpus[0];
  116. #endif
  117. chained_irq_enter(chip, desc);
  118. for (idx = 0; idx < intc->n_words; idx++) {
  119. int base = idx * IRQS_PER_WORD;
  120. unsigned long pending, flags;
  121. int hwirq;
  122. raw_spin_lock_irqsave(&intc->lock, flags);
  123. pending = l1_readl(cpu->map_base + reg_status(intc, idx)) &
  124. ~cpu->mask_cache[idx];
  125. raw_spin_unlock_irqrestore(&intc->lock, flags);
  126. for_each_set_bit(hwirq, &pending, IRQS_PER_WORD)
  127. generic_handle_domain_irq(intc->domain, base + hwirq);
  128. }
  129. chained_irq_exit(chip, desc);
  130. }
  131. static void __bcm7038_l1_unmask(struct irq_data *d, unsigned int cpu_idx)
  132. {
  133. struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
  134. u32 word = d->hwirq / IRQS_PER_WORD;
  135. u32 mask = BIT(d->hwirq % IRQS_PER_WORD);
  136. intc->cpus[cpu_idx]->mask_cache[word] &= ~mask;
  137. l1_writel(mask, intc->cpus[cpu_idx]->map_base +
  138. reg_mask_clr(intc, word));
  139. }
  140. static void __bcm7038_l1_mask(struct irq_data *d, unsigned int cpu_idx)
  141. {
  142. struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
  143. u32 word = d->hwirq / IRQS_PER_WORD;
  144. u32 mask = BIT(d->hwirq % IRQS_PER_WORD);
  145. intc->cpus[cpu_idx]->mask_cache[word] |= mask;
  146. l1_writel(mask, intc->cpus[cpu_idx]->map_base +
  147. reg_mask_set(intc, word));
  148. }
  149. static void bcm7038_l1_unmask(struct irq_data *d)
  150. {
  151. struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
  152. unsigned long flags;
  153. raw_spin_lock_irqsave(&intc->lock, flags);
  154. __bcm7038_l1_unmask(d, intc->affinity[d->hwirq]);
  155. raw_spin_unlock_irqrestore(&intc->lock, flags);
  156. }
  157. static void bcm7038_l1_mask(struct irq_data *d)
  158. {
  159. struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
  160. unsigned long flags;
  161. raw_spin_lock_irqsave(&intc->lock, flags);
  162. __bcm7038_l1_mask(d, intc->affinity[d->hwirq]);
  163. raw_spin_unlock_irqrestore(&intc->lock, flags);
  164. }
  165. #if defined(CONFIG_MIPS) && defined(CONFIG_SMP)
  166. static int bcm7038_l1_set_affinity(struct irq_data *d,
  167. const struct cpumask *dest,
  168. bool force)
  169. {
  170. struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
  171. unsigned long flags;
  172. irq_hw_number_t hw = d->hwirq;
  173. u32 word = hw / IRQS_PER_WORD;
  174. u32 mask = BIT(hw % IRQS_PER_WORD);
  175. unsigned int first_cpu = cpumask_any_and(dest, cpu_online_mask);
  176. bool was_disabled;
  177. raw_spin_lock_irqsave(&intc->lock, flags);
  178. was_disabled = !!(intc->cpus[intc->affinity[hw]]->mask_cache[word] &
  179. mask);
  180. __bcm7038_l1_mask(d, intc->affinity[hw]);
  181. intc->affinity[hw] = first_cpu;
  182. if (!was_disabled)
  183. __bcm7038_l1_unmask(d, first_cpu);
  184. raw_spin_unlock_irqrestore(&intc->lock, flags);
  185. irq_data_update_effective_affinity(d, cpumask_of(first_cpu));
  186. return 0;
  187. }
  188. #endif
  189. static int __init bcm7038_l1_init_one(struct device_node *dn,
  190. unsigned int idx,
  191. struct bcm7038_l1_chip *intc)
  192. {
  193. struct resource res;
  194. resource_size_t sz;
  195. struct bcm7038_l1_cpu *cpu;
  196. unsigned int i, n_words, parent_irq;
  197. int ret;
  198. if (of_address_to_resource(dn, idx, &res))
  199. return -EINVAL;
  200. sz = resource_size(&res);
  201. n_words = sz / REG_BYTES_PER_IRQ_WORD;
  202. if (n_words > MAX_WORDS)
  203. return -EINVAL;
  204. else if (!intc->n_words)
  205. intc->n_words = n_words;
  206. else if (intc->n_words != n_words)
  207. return -EINVAL;
  208. ret = of_property_read_u32_array(dn , "brcm,int-fwd-mask",
  209. intc->irq_fwd_mask, n_words);
  210. if (ret != 0 && ret != -EINVAL) {
  211. /* property exists but has the wrong number of words */
  212. pr_err("invalid brcm,int-fwd-mask property\n");
  213. return -EINVAL;
  214. }
  215. cpu = intc->cpus[idx] = kzalloc(sizeof(*cpu) + n_words * sizeof(u32),
  216. GFP_KERNEL);
  217. if (!cpu)
  218. return -ENOMEM;
  219. cpu->map_base = ioremap(res.start, sz);
  220. if (!cpu->map_base)
  221. return -ENOMEM;
  222. for (i = 0; i < n_words; i++) {
  223. l1_writel(~intc->irq_fwd_mask[i],
  224. cpu->map_base + reg_mask_set(intc, i));
  225. l1_writel(intc->irq_fwd_mask[i],
  226. cpu->map_base + reg_mask_clr(intc, i));
  227. cpu->mask_cache[i] = ~intc->irq_fwd_mask[i];
  228. }
  229. parent_irq = irq_of_parse_and_map(dn, idx);
  230. if (!parent_irq) {
  231. pr_err("failed to map parent interrupt %d\n", parent_irq);
  232. return -EINVAL;
  233. }
  234. if (of_property_read_bool(dn, "brcm,irq-can-wake"))
  235. enable_irq_wake(parent_irq);
  236. irq_set_chained_handler_and_data(parent_irq, bcm7038_l1_irq_handle,
  237. intc);
  238. return 0;
  239. }
  240. #ifdef CONFIG_PM_SLEEP
  241. /*
  242. * We keep a list of bcm7038_l1_chip used for suspend/resume. This hack is
  243. * used because the struct chip_type suspend/resume hooks are not called
  244. * unless chip_type is hooked onto a generic_chip. Since this driver does
  245. * not use generic_chip, we need to manually hook our resume/suspend to
  246. * syscore_ops.
  247. */
  248. static LIST_HEAD(bcm7038_l1_intcs_list);
  249. static DEFINE_RAW_SPINLOCK(bcm7038_l1_intcs_lock);
  250. static int bcm7038_l1_suspend(void)
  251. {
  252. struct bcm7038_l1_chip *intc;
  253. int boot_cpu, word;
  254. u32 val;
  255. /* Wakeup interrupt should only come from the boot cpu */
  256. #if defined(CONFIG_SMP) && defined(CONFIG_MIPS)
  257. boot_cpu = cpu_logical_map(0);
  258. #else
  259. boot_cpu = 0;
  260. #endif
  261. list_for_each_entry(intc, &bcm7038_l1_intcs_list, list) {
  262. for (word = 0; word < intc->n_words; word++) {
  263. val = intc->wake_mask[word] | intc->irq_fwd_mask[word];
  264. l1_writel(~val,
  265. intc->cpus[boot_cpu]->map_base + reg_mask_set(intc, word));
  266. l1_writel(val,
  267. intc->cpus[boot_cpu]->map_base + reg_mask_clr(intc, word));
  268. }
  269. }
  270. return 0;
  271. }
  272. static void bcm7038_l1_resume(void)
  273. {
  274. struct bcm7038_l1_chip *intc;
  275. int boot_cpu, word;
  276. #if defined(CONFIG_SMP) && defined(CONFIG_MIPS)
  277. boot_cpu = cpu_logical_map(0);
  278. #else
  279. boot_cpu = 0;
  280. #endif
  281. list_for_each_entry(intc, &bcm7038_l1_intcs_list, list) {
  282. for (word = 0; word < intc->n_words; word++) {
  283. l1_writel(intc->cpus[boot_cpu]->mask_cache[word],
  284. intc->cpus[boot_cpu]->map_base + reg_mask_set(intc, word));
  285. l1_writel(~intc->cpus[boot_cpu]->mask_cache[word],
  286. intc->cpus[boot_cpu]->map_base + reg_mask_clr(intc, word));
  287. }
  288. }
  289. }
  290. static struct syscore_ops bcm7038_l1_syscore_ops = {
  291. .suspend = bcm7038_l1_suspend,
  292. .resume = bcm7038_l1_resume,
  293. };
  294. static int bcm7038_l1_set_wake(struct irq_data *d, unsigned int on)
  295. {
  296. struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
  297. unsigned long flags;
  298. u32 word = d->hwirq / IRQS_PER_WORD;
  299. u32 mask = BIT(d->hwirq % IRQS_PER_WORD);
  300. raw_spin_lock_irqsave(&intc->lock, flags);
  301. if (on)
  302. intc->wake_mask[word] |= mask;
  303. else
  304. intc->wake_mask[word] &= ~mask;
  305. raw_spin_unlock_irqrestore(&intc->lock, flags);
  306. return 0;
  307. }
  308. #endif
  309. static struct irq_chip bcm7038_l1_irq_chip = {
  310. .name = "bcm7038-l1",
  311. .irq_mask = bcm7038_l1_mask,
  312. .irq_unmask = bcm7038_l1_unmask,
  313. #if defined(CONFIG_SMP) && defined(CONFIG_MIPS)
  314. .irq_set_affinity = bcm7038_l1_set_affinity,
  315. #endif
  316. #ifdef CONFIG_PM_SLEEP
  317. .irq_set_wake = bcm7038_l1_set_wake,
  318. #endif
  319. };
  320. static int bcm7038_l1_map(struct irq_domain *d, unsigned int virq,
  321. irq_hw_number_t hw_irq)
  322. {
  323. struct bcm7038_l1_chip *intc = d->host_data;
  324. u32 mask = BIT(hw_irq % IRQS_PER_WORD);
  325. u32 word = hw_irq / IRQS_PER_WORD;
  326. if (intc->irq_fwd_mask[word] & mask)
  327. return -EPERM;
  328. irq_set_chip_and_handler(virq, &bcm7038_l1_irq_chip, handle_level_irq);
  329. irq_set_chip_data(virq, d->host_data);
  330. irqd_set_single_target(irq_get_irq_data(virq));
  331. return 0;
  332. }
  333. static const struct irq_domain_ops bcm7038_l1_domain_ops = {
  334. .xlate = irq_domain_xlate_onecell,
  335. .map = bcm7038_l1_map,
  336. };
  337. static int __init bcm7038_l1_of_init(struct device_node *dn,
  338. struct device_node *parent)
  339. {
  340. struct bcm7038_l1_chip *intc;
  341. int idx, ret;
  342. intc = kzalloc(sizeof(*intc), GFP_KERNEL);
  343. if (!intc)
  344. return -ENOMEM;
  345. raw_spin_lock_init(&intc->lock);
  346. for_each_possible_cpu(idx) {
  347. ret = bcm7038_l1_init_one(dn, idx, intc);
  348. if (ret < 0) {
  349. if (idx)
  350. break;
  351. pr_err("failed to remap intc L1 registers\n");
  352. goto out_free;
  353. }
  354. }
  355. intc->domain = irq_domain_add_linear(dn, IRQS_PER_WORD * intc->n_words,
  356. &bcm7038_l1_domain_ops,
  357. intc);
  358. if (!intc->domain) {
  359. ret = -ENOMEM;
  360. goto out_unmap;
  361. }
  362. #ifdef CONFIG_PM_SLEEP
  363. /* Add bcm7038_l1_chip into a list */
  364. raw_spin_lock(&bcm7038_l1_intcs_lock);
  365. list_add_tail(&intc->list, &bcm7038_l1_intcs_list);
  366. raw_spin_unlock(&bcm7038_l1_intcs_lock);
  367. if (list_is_singular(&bcm7038_l1_intcs_list))
  368. register_syscore_ops(&bcm7038_l1_syscore_ops);
  369. #endif
  370. pr_info("registered BCM7038 L1 intc (%pOF, IRQs: %d)\n",
  371. dn, IRQS_PER_WORD * intc->n_words);
  372. return 0;
  373. out_unmap:
  374. for_each_possible_cpu(idx) {
  375. struct bcm7038_l1_cpu *cpu = intc->cpus[idx];
  376. if (cpu) {
  377. if (cpu->map_base)
  378. iounmap(cpu->map_base);
  379. kfree(cpu);
  380. }
  381. }
  382. out_free:
  383. kfree(intc);
  384. return ret;
  385. }
  386. IRQCHIP_PLATFORM_DRIVER_BEGIN(bcm7038_l1)
  387. IRQCHIP_MATCH("brcm,bcm7038-l1-intc", bcm7038_l1_of_init)
  388. IRQCHIP_PLATFORM_DRIVER_END(bcm7038_l1)
  389. MODULE_DESCRIPTION("Broadcom STB 7038-style L1/L2 interrupt controller");
  390. MODULE_LICENSE("GPL v2");