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