megamod-pic.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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_host *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_host *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_host *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_host_ops megamod_host_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_alloc_host(np, IRQ_HOST_MAP_LINEAR,
  188. NR_COMBINERS * 32, &megamod_host_ops,
  189. IRQ_UNMAPPED);
  190. if (!pic->irqhost) {
  191. pr_err("%s: Could not alloc host.\n", np->full_name);
  192. goto error_free;
  193. }
  194. pic->irqhost->host_data = pic;
  195. raw_spin_lock_init(&pic->lock);
  196. pic->regs = of_iomap(np, 0);
  197. if (!pic->regs) {
  198. pr_err("%s: Could not map registers.\n", np->full_name);
  199. goto error_free;
  200. }
  201. /* Initialize MUX map */
  202. for (i = 0; i < ARRAY_SIZE(mapping); i++)
  203. mapping[i] = IRQ_UNMAPPED;
  204. parse_priority_map(pic, mapping, ARRAY_SIZE(mapping));
  205. /*
  206. * We can have up to 12 interrupts cascading to the core controller.
  207. * These cascades can be from the combined interrupt sources or for
  208. * individual interrupt sources. The "interrupts" property only
  209. * deals with the cascaded combined interrupts. The individual
  210. * interrupts muxed to the core controller use the core controller
  211. * as their interrupt parent.
  212. */
  213. for (i = 0; i < NR_COMBINERS; i++) {
  214. irq = irq_of_parse_and_map(np, i);
  215. if (irq == NO_IRQ)
  216. continue;
  217. /*
  218. * We count on the core priority interrupts (4 - 15) being
  219. * direct mapped. Check that device tree provided something
  220. * in that range.
  221. */
  222. if (irq < 4 || irq >= NR_PRIORITY_IRQS) {
  223. pr_err("%s: combiner-%d virq %d out of range!\n",
  224. np->full_name, i, irq);
  225. continue;
  226. }
  227. /* record the mapping */
  228. mapping[irq - 4] = i;
  229. pr_debug("%s: combiner-%d cascading to virq %d\n",
  230. np->full_name, i, irq);
  231. cascade_data[i].pic = pic;
  232. cascade_data[i].index = i;
  233. /* mask and clear all events in combiner */
  234. soc_writel(~0, &pic->regs->evtmask[i]);
  235. soc_writel(~0, &pic->regs->evtclr[i]);
  236. irq_set_handler_data(irq, &cascade_data[i]);
  237. irq_set_chained_handler(irq, megamod_irq_cascade);
  238. }
  239. /* Finally, set up the MUX registers */
  240. for (i = 0; i < NR_MUX_OUTPUTS; i++) {
  241. if (mapping[i] != IRQ_UNMAPPED) {
  242. pr_debug("%s: setting mux %d to priority %d\n",
  243. np->full_name, mapping[i], i + 4);
  244. set_megamod_mux(pic, mapping[i], i);
  245. }
  246. }
  247. return pic;
  248. error_free:
  249. kfree(pic);
  250. return NULL;
  251. }
  252. /*
  253. * Return next active event after ACK'ing it.
  254. * Return -1 if no events active.
  255. */
  256. static int get_exception(void)
  257. {
  258. int i, bit;
  259. u32 mask;
  260. for (i = 0; i < NR_COMBINERS; i++) {
  261. mask = soc_readl(&mm_pic->regs->mexpflag[i]);
  262. if (mask) {
  263. bit = __ffs(mask);
  264. soc_writel(1 << bit, &mm_pic->regs->evtclr[i]);
  265. return (i * 32) + bit;
  266. }
  267. }
  268. return -1;
  269. }
  270. static void assert_event(unsigned int val)
  271. {
  272. soc_writel(val, &mm_pic->regs->evtasrt);
  273. }
  274. void __init megamod_pic_init(void)
  275. {
  276. struct device_node *np;
  277. np = of_find_compatible_node(NULL, NULL, "ti,c64x+megamod-pic");
  278. if (!np)
  279. return;
  280. mm_pic = init_megamod_pic(np);
  281. of_node_put(np);
  282. soc_ops.get_exception = get_exception;
  283. soc_ops.assert_event = assert_event;
  284. return;
  285. }