megamod-pic.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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(unsigned int irq, struct irq_desc *desc)
  83. {
  84. struct megamod_cascade_data *cascade;
  85. struct megamod_pic *pic;
  86. u32 events;
  87. int n, idx;
  88. cascade = irq_desc_get_handler_data(desc);
  89. pic = cascade->pic;
  90. idx = cascade->index;
  91. while ((events = soc_readl(&pic->regs->mevtflag[idx])) != 0) {
  92. n = __ffs(events);
  93. irq = irq_linear_revmap(pic->irqhost, idx * 32 + n);
  94. soc_writel(1 << n, &pic->regs->evtclr[idx]);
  95. generic_handle_irq(irq);
  96. }
  97. }
  98. static int megamod_map(struct irq_domain *h, unsigned int virq,
  99. irq_hw_number_t hw)
  100. {
  101. struct megamod_pic *pic = h->host_data;
  102. int i;
  103. /* We shouldn't see a hwirq which is muxed to core controller */
  104. for (i = 0; i < NR_MUX_OUTPUTS; i++)
  105. if (pic->output_to_irq[i] == hw)
  106. return -1;
  107. irq_set_chip_data(virq, pic);
  108. irq_set_chip_and_handler(virq, &megamod_chip, handle_level_irq);
  109. /* Set default irq type */
  110. irq_set_irq_type(virq, IRQ_TYPE_NONE);
  111. return 0;
  112. }
  113. static const struct irq_domain_ops megamod_domain_ops = {
  114. .map = megamod_map,
  115. .xlate = irq_domain_xlate_onecell,
  116. };
  117. static void __init set_megamod_mux(struct megamod_pic *pic, int src, int output)
  118. {
  119. int index, offset;
  120. u32 val;
  121. if (src < 0 || src >= (NR_COMBINERS * 32)) {
  122. pic->output_to_irq[output] = IRQ_UNMAPPED;
  123. return;
  124. }
  125. /* four mappings per mux register */
  126. index = output / 4;
  127. offset = (output & 3) * 8;
  128. val = soc_readl(&pic->regs->intmux[index]);
  129. val &= ~(0xff << offset);
  130. val |= src << offset;
  131. soc_writel(val, &pic->regs->intmux[index]);
  132. }
  133. /*
  134. * Parse the MUX mapping, if one exists.
  135. *
  136. * The MUX map is an array of up to 12 cells; one for each usable core priority
  137. * interrupt. The value of a given cell is the megamodule interrupt source
  138. * which is to me MUXed to the output corresponding to the cell position
  139. * withing the array. The first cell in the array corresponds to priority
  140. * 4 and the last (12th) cell corresponds to priority 15. The allowed
  141. * values are 4 - ((NR_COMBINERS * 32) - 1). Note that the combined interrupt
  142. * sources (0 - 3) are not allowed to be mapped through this property. They
  143. * are handled through the "interrupts" property. This allows us to use a
  144. * value of zero as a "do not map" placeholder.
  145. */
  146. static void __init parse_priority_map(struct megamod_pic *pic,
  147. int *mapping, int size)
  148. {
  149. struct device_node *np = pic->irqhost->of_node;
  150. const __be32 *map;
  151. int i, maplen;
  152. u32 val;
  153. map = of_get_property(np, "ti,c64x+megamod-pic-mux", &maplen);
  154. if (map) {
  155. maplen /= 4;
  156. if (maplen > size)
  157. maplen = size;
  158. for (i = 0; i < maplen; i++) {
  159. val = be32_to_cpup(map);
  160. if (val && val >= 4)
  161. mapping[i] = val;
  162. ++map;
  163. }
  164. }
  165. }
  166. static struct megamod_pic * __init init_megamod_pic(struct device_node *np)
  167. {
  168. struct megamod_pic *pic;
  169. int i, irq;
  170. int mapping[NR_MUX_OUTPUTS];
  171. pr_info("Initializing C64x+ Megamodule PIC\n");
  172. pic = kzalloc(sizeof(struct megamod_pic), GFP_KERNEL);
  173. if (!pic) {
  174. pr_err("%s: Could not alloc PIC structure.\n", np->full_name);
  175. return NULL;
  176. }
  177. pic->irqhost = irq_domain_add_linear(np, NR_COMBINERS * 32,
  178. &megamod_domain_ops, pic);
  179. if (!pic->irqhost) {
  180. pr_err("%s: Could not alloc host.\n", np->full_name);
  181. goto error_free;
  182. }
  183. pic->irqhost->host_data = pic;
  184. raw_spin_lock_init(&pic->lock);
  185. pic->regs = of_iomap(np, 0);
  186. if (!pic->regs) {
  187. pr_err("%s: Could not map registers.\n", np->full_name);
  188. goto error_free;
  189. }
  190. /* Initialize MUX map */
  191. for (i = 0; i < ARRAY_SIZE(mapping); i++)
  192. mapping[i] = IRQ_UNMAPPED;
  193. parse_priority_map(pic, mapping, ARRAY_SIZE(mapping));
  194. /*
  195. * We can have up to 12 interrupts cascading to the core controller.
  196. * These cascades can be from the combined interrupt sources or for
  197. * individual interrupt sources. The "interrupts" property only
  198. * deals with the cascaded combined interrupts. The individual
  199. * interrupts muxed to the core controller use the core controller
  200. * as their interrupt parent.
  201. */
  202. for (i = 0; i < NR_COMBINERS; i++) {
  203. struct irq_data *irq_data;
  204. irq_hw_number_t hwirq;
  205. irq = irq_of_parse_and_map(np, i);
  206. if (irq == NO_IRQ)
  207. continue;
  208. irq_data = irq_get_irq_data(irq);
  209. if (!irq_data) {
  210. pr_err("%s: combiner-%d no irq_data for virq %d!\n",
  211. np->full_name, i, irq);
  212. continue;
  213. }
  214. hwirq = irq_data->hwirq;
  215. /*
  216. * Check that device tree provided something in the range
  217. * of the core priority interrupts (4 - 15).
  218. */
  219. if (hwirq < 4 || hwirq >= NR_PRIORITY_IRQS) {
  220. pr_err("%s: combiner-%d core irq %ld out of range!\n",
  221. np->full_name, i, hwirq);
  222. continue;
  223. }
  224. /* record the mapping */
  225. mapping[hwirq - 4] = i;
  226. pr_debug("%s: combiner-%d cascading to hwirq %ld\n",
  227. np->full_name, i, hwirq);
  228. cascade_data[i].pic = pic;
  229. cascade_data[i].index = i;
  230. /* mask and clear all events in combiner */
  231. soc_writel(~0, &pic->regs->evtmask[i]);
  232. soc_writel(~0, &pic->regs->evtclr[i]);
  233. irq_set_handler_data(irq, &cascade_data[i]);
  234. irq_set_chained_handler(irq, megamod_irq_cascade);
  235. }
  236. /* Finally, set up the MUX registers */
  237. for (i = 0; i < NR_MUX_OUTPUTS; i++) {
  238. if (mapping[i] != IRQ_UNMAPPED) {
  239. pr_debug("%s: setting mux %d to priority %d\n",
  240. np->full_name, mapping[i], i + 4);
  241. set_megamod_mux(pic, mapping[i], i);
  242. }
  243. }
  244. return pic;
  245. error_free:
  246. kfree(pic);
  247. return NULL;
  248. }
  249. /*
  250. * Return next active event after ACK'ing it.
  251. * Return -1 if no events active.
  252. */
  253. static int get_exception(void)
  254. {
  255. int i, bit;
  256. u32 mask;
  257. for (i = 0; i < NR_COMBINERS; i++) {
  258. mask = soc_readl(&mm_pic->regs->mexpflag[i]);
  259. if (mask) {
  260. bit = __ffs(mask);
  261. soc_writel(1 << bit, &mm_pic->regs->evtclr[i]);
  262. return (i * 32) + bit;
  263. }
  264. }
  265. return -1;
  266. }
  267. static void assert_event(unsigned int val)
  268. {
  269. soc_writel(val, &mm_pic->regs->evtasrt);
  270. }
  271. void __init megamod_pic_init(void)
  272. {
  273. struct device_node *np;
  274. np = of_find_compatible_node(NULL, NULL, "ti,c64x+megamod-pic");
  275. if (!np)
  276. return;
  277. mm_pic = init_megamod_pic(np);
  278. of_node_put(np);
  279. soc_ops.get_exception = get_exception;
  280. soc_ops.assert_event = assert_event;
  281. return;
  282. }