megamod-pic.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Support for C64x+ Megamodule Interrupt Controller
  3. *
  4. * Copyright (C) 2010, 2011 Texas Instruments Incorporated
  5. * Contributed by: Mark Salter <msalter@redhat.com>
  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/module.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/of.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/of_address.h>
  17. #include <linux/slab.h>
  18. #include <asm/soc.h>
  19. #include <asm/megamod-pic.h>
  20. #define NR_COMBINERS 4
  21. #define NR_MUX_OUTPUTS 12
  22. #define IRQ_UNMAPPED 0xffff
  23. /*
  24. * Megamodule Interrupt Controller register layout
  25. */
  26. struct megamod_regs {
  27. u32 evtflag[8];
  28. u32 evtset[8];
  29. u32 evtclr[8];
  30. u32 reserved0[8];
  31. u32 evtmask[8];
  32. u32 mevtflag[8];
  33. u32 expmask[8];
  34. u32 mexpflag[8];
  35. u32 intmux_unused;
  36. u32 intmux[7];
  37. u32 reserved1[8];
  38. u32 aegmux[2];
  39. u32 reserved2[14];
  40. u32 intxstat;
  41. u32 intxclr;
  42. u32 intdmask;
  43. u32 reserved3[13];
  44. u32 evtasrt;
  45. };
  46. struct megamod_pic {
  47. struct irq_domain *irqhost;
  48. struct megamod_regs __iomem *regs;
  49. raw_spinlock_t lock;
  50. /* hw mux mapping */
  51. unsigned int output_to_irq[NR_MUX_OUTPUTS];
  52. };
  53. static struct megamod_pic *mm_pic;
  54. struct megamod_cascade_data {
  55. struct megamod_pic *pic;
  56. int index;
  57. };
  58. static struct megamod_cascade_data cascade_data[NR_COMBINERS];
  59. static void mask_megamod(struct irq_data *data)
  60. {
  61. struct megamod_pic *pic = irq_data_get_irq_chip_data(data);
  62. irq_hw_number_t src = irqd_to_hwirq(data);
  63. u32 __iomem *evtmask = &pic->regs->evtmask[src / 32];
  64. raw_spin_lock(&pic->lock);
  65. soc_writel(soc_readl(evtmask) | (1 << (src & 31)), evtmask);
  66. raw_spin_unlock(&pic->lock);
  67. }
  68. static void unmask_megamod(struct irq_data *data)
  69. {
  70. struct megamod_pic *pic = irq_data_get_irq_chip_data(data);
  71. irq_hw_number_t src = irqd_to_hwirq(data);
  72. u32 __iomem *evtmask = &pic->regs->evtmask[src / 32];
  73. raw_spin_lock(&pic->lock);
  74. soc_writel(soc_readl(evtmask) & ~(1 << (src & 31)), evtmask);
  75. raw_spin_unlock(&pic->lock);
  76. }
  77. static struct irq_chip megamod_chip = {
  78. .name = "megamod",
  79. .irq_mask = mask_megamod,
  80. .irq_unmask = unmask_megamod,
  81. };
  82. static void megamod_irq_cascade(struct irq_desc *desc)
  83. {
  84. struct megamod_cascade_data *cascade;
  85. struct megamod_pic *pic;
  86. unsigned int irq;
  87. u32 events;
  88. int n, idx;
  89. cascade = irq_desc_get_handler_data(desc);
  90. pic = cascade->pic;
  91. idx = cascade->index;
  92. while ((events = soc_readl(&pic->regs->mevtflag[idx])) != 0) {
  93. n = __ffs(events);
  94. irq = irq_linear_revmap(pic->irqhost, idx * 32 + n);
  95. soc_writel(1 << n, &pic->regs->evtclr[idx]);
  96. generic_handle_irq(irq);
  97. }
  98. }
  99. static int megamod_map(struct irq_domain *h, unsigned int virq,
  100. irq_hw_number_t hw)
  101. {
  102. struct megamod_pic *pic = h->host_data;
  103. int i;
  104. /* We shouldn't see a hwirq which is muxed to core controller */
  105. for (i = 0; i < NR_MUX_OUTPUTS; i++)
  106. if (pic->output_to_irq[i] == hw)
  107. return -1;
  108. irq_set_chip_data(virq, pic);
  109. irq_set_chip_and_handler(virq, &megamod_chip, handle_level_irq);
  110. /* Set default irq type */
  111. irq_set_irq_type(virq, IRQ_TYPE_NONE);
  112. return 0;
  113. }
  114. static const struct irq_domain_ops megamod_domain_ops = {
  115. .map = megamod_map,
  116. .xlate = irq_domain_xlate_onecell,
  117. };
  118. static void __init set_megamod_mux(struct megamod_pic *pic, int src, int output)
  119. {
  120. int index, offset;
  121. u32 val;
  122. if (src < 0 || src >= (NR_COMBINERS * 32)) {
  123. pic->output_to_irq[output] = IRQ_UNMAPPED;
  124. return;
  125. }
  126. /* four mappings per mux register */
  127. index = output / 4;
  128. offset = (output & 3) * 8;
  129. val = soc_readl(&pic->regs->intmux[index]);
  130. val &= ~(0xff << offset);
  131. val |= src << offset;
  132. soc_writel(val, &pic->regs->intmux[index]);
  133. }
  134. /*
  135. * Parse the MUX mapping, if one exists.
  136. *
  137. * The MUX map is an array of up to 12 cells; one for each usable core priority
  138. * interrupt. The value of a given cell is the megamodule interrupt source
  139. * which is to me MUXed to the output corresponding to the cell position
  140. * withing the array. The first cell in the array corresponds to priority
  141. * 4 and the last (12th) cell corresponds to priority 15. The allowed
  142. * values are 4 - ((NR_COMBINERS * 32) - 1). Note that the combined interrupt
  143. * sources (0 - 3) are not allowed to be mapped through this property. They
  144. * are handled through the "interrupts" property. This allows us to use a
  145. * value of zero as a "do not map" placeholder.
  146. */
  147. static void __init parse_priority_map(struct megamod_pic *pic,
  148. int *mapping, int size)
  149. {
  150. struct device_node *np = irq_domain_get_of_node(pic->irqhost);
  151. const __be32 *map;
  152. int i, maplen;
  153. u32 val;
  154. map = of_get_property(np, "ti,c64x+megamod-pic-mux", &maplen);
  155. if (map) {
  156. maplen /= 4;
  157. if (maplen > size)
  158. maplen = size;
  159. for (i = 0; i < maplen; i++) {
  160. val = be32_to_cpup(map);
  161. if (val && val >= 4)
  162. mapping[i] = val;
  163. ++map;
  164. }
  165. }
  166. }
  167. static struct megamod_pic * __init init_megamod_pic(struct device_node *np)
  168. {
  169. struct megamod_pic *pic;
  170. int i, irq;
  171. int mapping[NR_MUX_OUTPUTS];
  172. pr_info("Initializing C64x+ Megamodule PIC\n");
  173. pic = kzalloc(sizeof(struct megamod_pic), GFP_KERNEL);
  174. if (!pic) {
  175. pr_err("%pOF: Could not alloc PIC structure.\n", np);
  176. return NULL;
  177. }
  178. pic->irqhost = irq_domain_add_linear(np, NR_COMBINERS * 32,
  179. &megamod_domain_ops, pic);
  180. if (!pic->irqhost) {
  181. pr_err("%pOF: Could not alloc host.\n", np);
  182. goto error_free;
  183. }
  184. pic->irqhost->host_data = pic;
  185. raw_spin_lock_init(&pic->lock);
  186. pic->regs = of_iomap(np, 0);
  187. if (!pic->regs) {
  188. pr_err("%pOF: Could not map registers.\n", np);
  189. goto error_free;
  190. }
  191. /* Initialize MUX map */
  192. for (i = 0; i < ARRAY_SIZE(mapping); i++)
  193. mapping[i] = IRQ_UNMAPPED;
  194. parse_priority_map(pic, mapping, ARRAY_SIZE(mapping));
  195. /*
  196. * We can have up to 12 interrupts cascading to the core controller.
  197. * These cascades can be from the combined interrupt sources or for
  198. * individual interrupt sources. The "interrupts" property only
  199. * deals with the cascaded combined interrupts. The individual
  200. * interrupts muxed to the core controller use the core controller
  201. * as their interrupt parent.
  202. */
  203. for (i = 0; i < NR_COMBINERS; i++) {
  204. struct irq_data *irq_data;
  205. irq_hw_number_t hwirq;
  206. irq = irq_of_parse_and_map(np, i);
  207. if (irq == NO_IRQ)
  208. continue;
  209. irq_data = irq_get_irq_data(irq);
  210. if (!irq_data) {
  211. pr_err("%pOF: combiner-%d no irq_data for virq %d!\n",
  212. np, i, irq);
  213. continue;
  214. }
  215. hwirq = irq_data->hwirq;
  216. /*
  217. * Check that device tree provided something in the range
  218. * of the core priority interrupts (4 - 15).
  219. */
  220. if (hwirq < 4 || hwirq >= NR_PRIORITY_IRQS) {
  221. pr_err("%pOF: combiner-%d core irq %ld out of range!\n",
  222. np, i, hwirq);
  223. continue;
  224. }
  225. /* record the mapping */
  226. mapping[hwirq - 4] = i;
  227. pr_debug("%pOF: combiner-%d cascading to hwirq %ld\n",
  228. np, i, hwirq);
  229. cascade_data[i].pic = pic;
  230. cascade_data[i].index = i;
  231. /* mask and clear all events in combiner */
  232. soc_writel(~0, &pic->regs->evtmask[i]);
  233. soc_writel(~0, &pic->regs->evtclr[i]);
  234. irq_set_chained_handler_and_data(irq, megamod_irq_cascade,
  235. &cascade_data[i]);
  236. }
  237. /* Finally, set up the MUX registers */
  238. for (i = 0; i < NR_MUX_OUTPUTS; i++) {
  239. if (mapping[i] != IRQ_UNMAPPED) {
  240. pr_debug("%pOF: setting mux %d to priority %d\n",
  241. np, mapping[i], i + 4);
  242. set_megamod_mux(pic, mapping[i], i);
  243. }
  244. }
  245. return pic;
  246. error_free:
  247. kfree(pic);
  248. return NULL;
  249. }
  250. /*
  251. * Return next active event after ACK'ing it.
  252. * Return -1 if no events active.
  253. */
  254. static int get_exception(void)
  255. {
  256. int i, bit;
  257. u32 mask;
  258. for (i = 0; i < NR_COMBINERS; i++) {
  259. mask = soc_readl(&mm_pic->regs->mexpflag[i]);
  260. if (mask) {
  261. bit = __ffs(mask);
  262. soc_writel(1 << bit, &mm_pic->regs->evtclr[i]);
  263. return (i * 32) + bit;
  264. }
  265. }
  266. return -1;
  267. }
  268. static void assert_event(unsigned int val)
  269. {
  270. soc_writel(val, &mm_pic->regs->evtasrt);
  271. }
  272. void __init megamod_pic_init(void)
  273. {
  274. struct device_node *np;
  275. np = of_find_compatible_node(NULL, NULL, "ti,c64x+megamod-pic");
  276. if (!np)
  277. return;
  278. mm_pic = init_megamod_pic(np);
  279. of_node_put(np);
  280. soc_ops.get_exception = get_exception;
  281. soc_ops.assert_event = assert_event;
  282. return;
  283. }