mpc52xx_pic.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. *
  3. * Programmable Interrupt Controller functions for the Freescale MPC52xx.
  4. *
  5. * Copyright (C) 2008 Secret Lab Technologies Ltd.
  6. * Copyright (C) 2006 bplan GmbH
  7. * Copyright (C) 2004 Sylvain Munaut <tnt@246tNt.com>
  8. * Copyright (C) 2003 Montavista Software, Inc
  9. *
  10. * Based on the code from the 2.4 kernel by
  11. * Dale Farnsworth <dfarnsworth@mvista.com> and Kent Borg.
  12. *
  13. * This file is licensed under the terms of the GNU General Public License
  14. * version 2. This program is licensed "as is" without any warranty of any
  15. * kind, whether express or implied.
  16. *
  17. */
  18. /*
  19. * This is the device driver for the MPC5200 interrupt controller.
  20. *
  21. * hardware overview
  22. * -----------------
  23. * The MPC5200 interrupt controller groups the all interrupt sources into
  24. * three groups called 'critical', 'main', and 'peripheral'. The critical
  25. * group has 3 irqs, External IRQ0, slice timer 0 irq, and wake from deep
  26. * sleep. Main group include the other 3 external IRQs, slice timer 1, RTC,
  27. * gpios, and the general purpose timers. Peripheral group contains the
  28. * remaining irq sources from all of the on-chip peripherals (PSCs, Ethernet,
  29. * USB, DMA, etc).
  30. *
  31. * virqs
  32. * -----
  33. * The Linux IRQ subsystem requires that each irq source be assigned a
  34. * system wide unique IRQ number starting at 1 (0 means no irq). Since
  35. * systems can have multiple interrupt controllers, the virtual IRQ (virq)
  36. * infrastructure lets each interrupt controller to define a local set
  37. * of IRQ numbers and the virq infrastructure maps those numbers into
  38. * a unique range of the global IRQ# space.
  39. *
  40. * To define a range of virq numbers for this controller, this driver first
  41. * assigns a number to each of the irq groups (called the level 1 or L1
  42. * value). Within each group individual irq sources are also assigned a
  43. * number, as defined by the MPC5200 user guide, and refers to it as the
  44. * level 2 or L2 value. The virq number is determined by shifting up the
  45. * L1 value by MPC52xx_IRQ_L1_OFFSET and ORing it with the L2 value.
  46. *
  47. * For example, the TMR0 interrupt is irq 9 in the main group. The
  48. * virq for TMR0 is calculated by ((1 << MPC52xx_IRQ_L1_OFFSET) | 9).
  49. *
  50. * The observant reader will also notice that this driver defines a 4th
  51. * interrupt group called 'bestcomm'. The bestcomm group isn't physically
  52. * part of the MPC5200 interrupt controller, but it is used here to assign
  53. * a separate virq number for each bestcomm task (since any of the 16
  54. * bestcomm tasks can cause the bestcomm interrupt to be raised). When a
  55. * bestcomm interrupt occurs (peripheral group, irq 0) this driver determines
  56. * which task needs servicing and returns the irq number for that task. This
  57. * allows drivers which use bestcomm to define their own interrupt handlers.
  58. *
  59. * irq_chip structures
  60. * -------------------
  61. * For actually manipulating IRQs (masking, enabling, clearing, etc) this
  62. * driver defines four separate 'irq_chip' structures, one for the main
  63. * group, one for the peripherals group, one for the bestcomm group and one
  64. * for external interrupts. The irq_chip structures provide the hooks needed
  65. * to manipulate each IRQ source, and since each group is has a separate set
  66. * of registers for controlling the irq, it makes sense to divide up the
  67. * hooks along those lines.
  68. *
  69. * You'll notice that there is not an irq_chip for the critical group and
  70. * you'll also notice that there is an irq_chip defined for external
  71. * interrupts even though there is no external interrupt group. The reason
  72. * for this is that the four external interrupts are all managed with the same
  73. * register even though one of the external IRQs is in the critical group and
  74. * the other three are in the main group. For this reason it makes sense for
  75. * the 4 external irqs to be managed using a separate set of hooks. The
  76. * reason there is no crit irq_chip is that of the 3 irqs in the critical
  77. * group, only external interrupt is actually support at this time by this
  78. * driver and since external interrupt is the only one used, it can just
  79. * be directed to make use of the external irq irq_chip.
  80. *
  81. * device tree bindings
  82. * --------------------
  83. * The device tree bindings for this controller reflect the two level
  84. * organization of irqs in the device. #interrupt-cells = <3> where the
  85. * first cell is the group number [0..3], the second cell is the irq
  86. * number in the group, and the third cell is the sense type (level/edge).
  87. * For reference, the following is a list of the interrupt property values
  88. * associated with external interrupt sources on the MPC5200 (just because
  89. * it is non-obvious to determine what the interrupts property should be
  90. * when reading the mpc5200 manual and it is a frequently asked question).
  91. *
  92. * External interrupts:
  93. * <0 0 n> external irq0, n is sense (n=0: level high,
  94. * <1 1 n> external irq1, n is sense n=1: edge rising,
  95. * <1 2 n> external irq2, n is sense n=2: edge falling,
  96. * <1 3 n> external irq3, n is sense n=3: level low)
  97. */
  98. #undef DEBUG
  99. #include <linux/interrupt.h>
  100. #include <linux/irq.h>
  101. #include <linux/of.h>
  102. #include <asm/io.h>
  103. #include <asm/prom.h>
  104. #include <asm/mpc52xx.h>
  105. /* HW IRQ mapping */
  106. #define MPC52xx_IRQ_L1_CRIT (0)
  107. #define MPC52xx_IRQ_L1_MAIN (1)
  108. #define MPC52xx_IRQ_L1_PERP (2)
  109. #define MPC52xx_IRQ_L1_SDMA (3)
  110. #define MPC52xx_IRQ_L1_OFFSET (6)
  111. #define MPC52xx_IRQ_L1_MASK (0x00c0)
  112. #define MPC52xx_IRQ_L2_MASK (0x003f)
  113. #define MPC52xx_IRQ_HIGHTESTHWIRQ (0xd0)
  114. /* MPC5200 device tree match tables */
  115. static const struct of_device_id mpc52xx_pic_ids[] __initconst = {
  116. { .compatible = "fsl,mpc5200-pic", },
  117. { .compatible = "mpc5200-pic", },
  118. {}
  119. };
  120. static const struct of_device_id mpc52xx_sdma_ids[] __initconst = {
  121. { .compatible = "fsl,mpc5200-bestcomm", },
  122. { .compatible = "mpc5200-bestcomm", },
  123. {}
  124. };
  125. static struct mpc52xx_intr __iomem *intr;
  126. static struct mpc52xx_sdma __iomem *sdma;
  127. static struct irq_domain *mpc52xx_irqhost = NULL;
  128. static unsigned char mpc52xx_map_senses[4] = {
  129. IRQ_TYPE_LEVEL_HIGH,
  130. IRQ_TYPE_EDGE_RISING,
  131. IRQ_TYPE_EDGE_FALLING,
  132. IRQ_TYPE_LEVEL_LOW,
  133. };
  134. /* Utility functions */
  135. static inline void io_be_setbit(u32 __iomem *addr, int bitno)
  136. {
  137. out_be32(addr, in_be32(addr) | (1 << bitno));
  138. }
  139. static inline void io_be_clrbit(u32 __iomem *addr, int bitno)
  140. {
  141. out_be32(addr, in_be32(addr) & ~(1 << bitno));
  142. }
  143. /*
  144. * IRQ[0-3] interrupt irq_chip
  145. */
  146. static void mpc52xx_extirq_mask(struct irq_data *d)
  147. {
  148. int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
  149. io_be_clrbit(&intr->ctrl, 11 - l2irq);
  150. }
  151. static void mpc52xx_extirq_unmask(struct irq_data *d)
  152. {
  153. int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
  154. io_be_setbit(&intr->ctrl, 11 - l2irq);
  155. }
  156. static void mpc52xx_extirq_ack(struct irq_data *d)
  157. {
  158. int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
  159. io_be_setbit(&intr->ctrl, 27-l2irq);
  160. }
  161. static int mpc52xx_extirq_set_type(struct irq_data *d, unsigned int flow_type)
  162. {
  163. u32 ctrl_reg, type;
  164. int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
  165. void *handler = handle_level_irq;
  166. pr_debug("%s: irq=%x. l2=%d flow_type=%d\n", __func__,
  167. (int) irqd_to_hwirq(d), l2irq, flow_type);
  168. switch (flow_type) {
  169. case IRQF_TRIGGER_HIGH: type = 0; break;
  170. case IRQF_TRIGGER_RISING: type = 1; handler = handle_edge_irq; break;
  171. case IRQF_TRIGGER_FALLING: type = 2; handler = handle_edge_irq; break;
  172. case IRQF_TRIGGER_LOW: type = 3; break;
  173. default:
  174. type = 0;
  175. }
  176. ctrl_reg = in_be32(&intr->ctrl);
  177. ctrl_reg &= ~(0x3 << (22 - (l2irq * 2)));
  178. ctrl_reg |= (type << (22 - (l2irq * 2)));
  179. out_be32(&intr->ctrl, ctrl_reg);
  180. irq_set_handler_locked(d, handler);
  181. return 0;
  182. }
  183. static struct irq_chip mpc52xx_extirq_irqchip = {
  184. .name = "MPC52xx External",
  185. .irq_mask = mpc52xx_extirq_mask,
  186. .irq_unmask = mpc52xx_extirq_unmask,
  187. .irq_ack = mpc52xx_extirq_ack,
  188. .irq_set_type = mpc52xx_extirq_set_type,
  189. };
  190. /*
  191. * Main interrupt irq_chip
  192. */
  193. static int mpc52xx_null_set_type(struct irq_data *d, unsigned int flow_type)
  194. {
  195. return 0; /* Do nothing so that the sense mask will get updated */
  196. }
  197. static void mpc52xx_main_mask(struct irq_data *d)
  198. {
  199. int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
  200. io_be_setbit(&intr->main_mask, 16 - l2irq);
  201. }
  202. static void mpc52xx_main_unmask(struct irq_data *d)
  203. {
  204. int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
  205. io_be_clrbit(&intr->main_mask, 16 - l2irq);
  206. }
  207. static struct irq_chip mpc52xx_main_irqchip = {
  208. .name = "MPC52xx Main",
  209. .irq_mask = mpc52xx_main_mask,
  210. .irq_mask_ack = mpc52xx_main_mask,
  211. .irq_unmask = mpc52xx_main_unmask,
  212. .irq_set_type = mpc52xx_null_set_type,
  213. };
  214. /*
  215. * Peripherals interrupt irq_chip
  216. */
  217. static void mpc52xx_periph_mask(struct irq_data *d)
  218. {
  219. int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
  220. io_be_setbit(&intr->per_mask, 31 - l2irq);
  221. }
  222. static void mpc52xx_periph_unmask(struct irq_data *d)
  223. {
  224. int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
  225. io_be_clrbit(&intr->per_mask, 31 - l2irq);
  226. }
  227. static struct irq_chip mpc52xx_periph_irqchip = {
  228. .name = "MPC52xx Peripherals",
  229. .irq_mask = mpc52xx_periph_mask,
  230. .irq_mask_ack = mpc52xx_periph_mask,
  231. .irq_unmask = mpc52xx_periph_unmask,
  232. .irq_set_type = mpc52xx_null_set_type,
  233. };
  234. /*
  235. * SDMA interrupt irq_chip
  236. */
  237. static void mpc52xx_sdma_mask(struct irq_data *d)
  238. {
  239. int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
  240. io_be_setbit(&sdma->IntMask, l2irq);
  241. }
  242. static void mpc52xx_sdma_unmask(struct irq_data *d)
  243. {
  244. int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
  245. io_be_clrbit(&sdma->IntMask, l2irq);
  246. }
  247. static void mpc52xx_sdma_ack(struct irq_data *d)
  248. {
  249. int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
  250. out_be32(&sdma->IntPend, 1 << l2irq);
  251. }
  252. static struct irq_chip mpc52xx_sdma_irqchip = {
  253. .name = "MPC52xx SDMA",
  254. .irq_mask = mpc52xx_sdma_mask,
  255. .irq_unmask = mpc52xx_sdma_unmask,
  256. .irq_ack = mpc52xx_sdma_ack,
  257. .irq_set_type = mpc52xx_null_set_type,
  258. };
  259. /**
  260. * mpc52xx_is_extirq - Returns true if hwirq number is for an external IRQ
  261. */
  262. static int mpc52xx_is_extirq(int l1, int l2)
  263. {
  264. return ((l1 == 0) && (l2 == 0)) ||
  265. ((l1 == 1) && (l2 >= 1) && (l2 <= 3));
  266. }
  267. /**
  268. * mpc52xx_irqhost_xlate - translate virq# from device tree interrupts property
  269. */
  270. static int mpc52xx_irqhost_xlate(struct irq_domain *h, struct device_node *ct,
  271. const u32 *intspec, unsigned int intsize,
  272. irq_hw_number_t *out_hwirq,
  273. unsigned int *out_flags)
  274. {
  275. int intrvect_l1;
  276. int intrvect_l2;
  277. int intrvect_type;
  278. int intrvect_linux;
  279. if (intsize != 3)
  280. return -1;
  281. intrvect_l1 = (int)intspec[0];
  282. intrvect_l2 = (int)intspec[1];
  283. intrvect_type = (int)intspec[2] & 0x3;
  284. intrvect_linux = (intrvect_l1 << MPC52xx_IRQ_L1_OFFSET) &
  285. MPC52xx_IRQ_L1_MASK;
  286. intrvect_linux |= intrvect_l2 & MPC52xx_IRQ_L2_MASK;
  287. *out_hwirq = intrvect_linux;
  288. *out_flags = IRQ_TYPE_LEVEL_LOW;
  289. if (mpc52xx_is_extirq(intrvect_l1, intrvect_l2))
  290. *out_flags = mpc52xx_map_senses[intrvect_type];
  291. pr_debug("return %x, l1=%d, l2=%d\n", intrvect_linux, intrvect_l1,
  292. intrvect_l2);
  293. return 0;
  294. }
  295. /**
  296. * mpc52xx_irqhost_map - Hook to map from virq to an irq_chip structure
  297. */
  298. static int mpc52xx_irqhost_map(struct irq_domain *h, unsigned int virq,
  299. irq_hw_number_t irq)
  300. {
  301. int l1irq;
  302. int l2irq;
  303. struct irq_chip *uninitialized_var(irqchip);
  304. void *hndlr;
  305. int type;
  306. u32 reg;
  307. l1irq = (irq & MPC52xx_IRQ_L1_MASK) >> MPC52xx_IRQ_L1_OFFSET;
  308. l2irq = irq & MPC52xx_IRQ_L2_MASK;
  309. /*
  310. * External IRQs are handled differently by the hardware so they are
  311. * handled by a dedicated irq_chip structure.
  312. */
  313. if (mpc52xx_is_extirq(l1irq, l2irq)) {
  314. reg = in_be32(&intr->ctrl);
  315. type = mpc52xx_map_senses[(reg >> (22 - l2irq * 2)) & 0x3];
  316. if ((type == IRQ_TYPE_EDGE_FALLING) ||
  317. (type == IRQ_TYPE_EDGE_RISING))
  318. hndlr = handle_edge_irq;
  319. else
  320. hndlr = handle_level_irq;
  321. irq_set_chip_and_handler(virq, &mpc52xx_extirq_irqchip, hndlr);
  322. pr_debug("%s: External IRQ%i virq=%x, hw=%x. type=%x\n",
  323. __func__, l2irq, virq, (int)irq, type);
  324. return 0;
  325. }
  326. /* It is an internal SOC irq. Choose the correct irq_chip */
  327. switch (l1irq) {
  328. case MPC52xx_IRQ_L1_MAIN: irqchip = &mpc52xx_main_irqchip; break;
  329. case MPC52xx_IRQ_L1_PERP: irqchip = &mpc52xx_periph_irqchip; break;
  330. case MPC52xx_IRQ_L1_SDMA: irqchip = &mpc52xx_sdma_irqchip; break;
  331. case MPC52xx_IRQ_L1_CRIT:
  332. pr_warn("%s: Critical IRQ #%d is unsupported! Nopping it.\n",
  333. __func__, l2irq);
  334. irq_set_chip(virq, &no_irq_chip);
  335. return 0;
  336. }
  337. irq_set_chip_and_handler(virq, irqchip, handle_level_irq);
  338. pr_debug("%s: virq=%x, l1=%i, l2=%i\n", __func__, virq, l1irq, l2irq);
  339. return 0;
  340. }
  341. static const struct irq_domain_ops mpc52xx_irqhost_ops = {
  342. .xlate = mpc52xx_irqhost_xlate,
  343. .map = mpc52xx_irqhost_map,
  344. };
  345. /**
  346. * mpc52xx_init_irq - Initialize and register with the virq subsystem
  347. *
  348. * Hook for setting up IRQs on an mpc5200 system. A pointer to this function
  349. * is to be put into the machine definition structure.
  350. *
  351. * This function searches the device tree for an MPC5200 interrupt controller,
  352. * initializes it, and registers it with the virq subsystem.
  353. */
  354. void __init mpc52xx_init_irq(void)
  355. {
  356. u32 intr_ctrl;
  357. struct device_node *picnode;
  358. struct device_node *np;
  359. /* Remap the necessary zones */
  360. picnode = of_find_matching_node(NULL, mpc52xx_pic_ids);
  361. intr = of_iomap(picnode, 0);
  362. if (!intr)
  363. panic(__FILE__ ": find_and_map failed on 'mpc5200-pic'. "
  364. "Check node !");
  365. np = of_find_matching_node(NULL, mpc52xx_sdma_ids);
  366. sdma = of_iomap(np, 0);
  367. of_node_put(np);
  368. if (!sdma)
  369. panic(__FILE__ ": find_and_map failed on 'mpc5200-bestcomm'. "
  370. "Check node !");
  371. pr_debug("MPC5200 IRQ controller mapped to 0x%p\n", intr);
  372. /* Disable all interrupt sources. */
  373. out_be32(&sdma->IntPend, 0xffffffff); /* 1 means clear pending */
  374. out_be32(&sdma->IntMask, 0xffffffff); /* 1 means disabled */
  375. out_be32(&intr->per_mask, 0x7ffffc00); /* 1 means disabled */
  376. out_be32(&intr->main_mask, 0x00010fff); /* 1 means disabled */
  377. intr_ctrl = in_be32(&intr->ctrl);
  378. intr_ctrl &= 0x00ff0000; /* Keeps IRQ[0-3] config */
  379. intr_ctrl |= 0x0f000000 | /* clear IRQ 0-3 */
  380. 0x00001000 | /* MEE master external enable */
  381. 0x00000000 | /* 0 means disable IRQ 0-3 */
  382. 0x00000001; /* CEb route critical normally */
  383. out_be32(&intr->ctrl, intr_ctrl);
  384. /* Zero a bunch of the priority settings. */
  385. out_be32(&intr->per_pri1, 0);
  386. out_be32(&intr->per_pri2, 0);
  387. out_be32(&intr->per_pri3, 0);
  388. out_be32(&intr->main_pri1, 0);
  389. out_be32(&intr->main_pri2, 0);
  390. /*
  391. * As last step, add an irq host to translate the real
  392. * hw irq information provided by the ofw to linux virq
  393. */
  394. mpc52xx_irqhost = irq_domain_add_linear(picnode,
  395. MPC52xx_IRQ_HIGHTESTHWIRQ,
  396. &mpc52xx_irqhost_ops, NULL);
  397. if (!mpc52xx_irqhost)
  398. panic(__FILE__ ": Cannot allocate the IRQ host\n");
  399. irq_set_default_host(mpc52xx_irqhost);
  400. pr_info("MPC52xx PIC is up and running!\n");
  401. }
  402. /**
  403. * mpc52xx_get_irq - Get pending interrupt number hook function
  404. *
  405. * Called by the interrupt handler to determine what IRQ handler needs to be
  406. * executed.
  407. *
  408. * Status of pending interrupts is determined by reading the encoded status
  409. * register. The encoded status register has three fields; one for each of the
  410. * types of interrupts defined by the controller - 'critical', 'main' and
  411. * 'peripheral'. This function reads the status register and returns the IRQ
  412. * number associated with the highest priority pending interrupt. 'Critical'
  413. * interrupts have the highest priority, followed by 'main' interrupts, and
  414. * then 'peripheral'.
  415. *
  416. * The mpc5200 interrupt controller can be configured to boost the priority
  417. * of individual 'peripheral' interrupts. If this is the case then a special
  418. * value will appear in either the crit or main fields indicating a high
  419. * or medium priority peripheral irq has occurred.
  420. *
  421. * This function checks each of the 3 irq request fields and returns the
  422. * first pending interrupt that it finds.
  423. *
  424. * This function also identifies a 4th type of interrupt; 'bestcomm'. Each
  425. * bestcomm DMA task can raise the bestcomm peripheral interrupt. When this
  426. * occurs at task-specific IRQ# is decoded so that each task can have its
  427. * own IRQ handler.
  428. */
  429. unsigned int mpc52xx_get_irq(void)
  430. {
  431. u32 status;
  432. int irq;
  433. status = in_be32(&intr->enc_status);
  434. if (status & 0x00000400) { /* critical */
  435. irq = (status >> 8) & 0x3;
  436. if (irq == 2) /* high priority peripheral */
  437. goto peripheral;
  438. irq |= (MPC52xx_IRQ_L1_CRIT << MPC52xx_IRQ_L1_OFFSET);
  439. } else if (status & 0x00200000) { /* main */
  440. irq = (status >> 16) & 0x1f;
  441. if (irq == 4) /* low priority peripheral */
  442. goto peripheral;
  443. irq |= (MPC52xx_IRQ_L1_MAIN << MPC52xx_IRQ_L1_OFFSET);
  444. } else if (status & 0x20000000) { /* peripheral */
  445. peripheral:
  446. irq = (status >> 24) & 0x1f;
  447. if (irq == 0) { /* bestcomm */
  448. status = in_be32(&sdma->IntPend);
  449. irq = ffs(status) - 1;
  450. irq |= (MPC52xx_IRQ_L1_SDMA << MPC52xx_IRQ_L1_OFFSET);
  451. } else {
  452. irq |= (MPC52xx_IRQ_L1_PERP << MPC52xx_IRQ_L1_OFFSET);
  453. }
  454. } else {
  455. return NO_IRQ;
  456. }
  457. return irq_linear_revmap(mpc52xx_irqhost, irq);
  458. }