mpc52xx_pic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. *
  3. * Programmable Interrupt Controller functions for the Freescale MPC52xx.
  4. *
  5. * Copyright (C) 2006 bplan GmbH
  6. *
  7. * Based on the code from the 2.4 kernel by
  8. * Dale Farnsworth <dfarnsworth@mvista.com> and Kent Borg.
  9. *
  10. * Copyright (C) 2004 Sylvain Munaut <tnt@246tNt.com>
  11. * Copyright (C) 2003 Montavista Software, Inc
  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. #undef DEBUG
  19. #include <linux/interrupt.h>
  20. #include <linux/irq.h>
  21. #include <linux/of.h>
  22. #include <asm/io.h>
  23. #include <asm/prom.h>
  24. #include <asm/mpc52xx.h>
  25. #include "mpc52xx_pic.h"
  26. /*
  27. *
  28. */
  29. /* MPC5200 device tree match tables */
  30. static struct of_device_id mpc52xx_pic_ids[] __initdata = {
  31. { .compatible = "fsl,mpc5200-pic", },
  32. { .compatible = "mpc5200-pic", },
  33. {}
  34. };
  35. static struct of_device_id mpc52xx_sdma_ids[] __initdata = {
  36. { .compatible = "fsl,mpc5200-bestcomm", },
  37. { .compatible = "mpc5200-bestcomm", },
  38. {}
  39. };
  40. static struct mpc52xx_intr __iomem *intr;
  41. static struct mpc52xx_sdma __iomem *sdma;
  42. static struct irq_host *mpc52xx_irqhost = NULL;
  43. static unsigned char mpc52xx_map_senses[4] = {
  44. IRQ_TYPE_LEVEL_HIGH,
  45. IRQ_TYPE_EDGE_RISING,
  46. IRQ_TYPE_EDGE_FALLING,
  47. IRQ_TYPE_LEVEL_LOW,
  48. };
  49. /*
  50. *
  51. */
  52. static inline void io_be_setbit(u32 __iomem *addr, int bitno)
  53. {
  54. out_be32(addr, in_be32(addr) | (1 << bitno));
  55. }
  56. static inline void io_be_clrbit(u32 __iomem *addr, int bitno)
  57. {
  58. out_be32(addr, in_be32(addr) & ~(1 << bitno));
  59. }
  60. /*
  61. * IRQ[0-3] interrupt irq_chip
  62. */
  63. static void mpc52xx_extirq_mask(unsigned int virq)
  64. {
  65. int irq;
  66. int l2irq;
  67. irq = irq_map[virq].hwirq;
  68. l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
  69. pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
  70. io_be_clrbit(&intr->ctrl, 11 - l2irq);
  71. }
  72. static void mpc52xx_extirq_unmask(unsigned int virq)
  73. {
  74. int irq;
  75. int l2irq;
  76. irq = irq_map[virq].hwirq;
  77. l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
  78. pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
  79. io_be_setbit(&intr->ctrl, 11 - l2irq);
  80. }
  81. static void mpc52xx_extirq_ack(unsigned int virq)
  82. {
  83. int irq;
  84. int l2irq;
  85. irq = irq_map[virq].hwirq;
  86. l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
  87. pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
  88. io_be_setbit(&intr->ctrl, 27-l2irq);
  89. }
  90. static int mpc52xx_extirq_set_type(unsigned int virq, unsigned int flow_type)
  91. {
  92. u32 ctrl_reg, type;
  93. int irq;
  94. int l2irq;
  95. irq = irq_map[virq].hwirq;
  96. l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
  97. pr_debug("%s: irq=%x. l2=%d flow_type=%d\n", __func__, irq, l2irq, flow_type);
  98. switch (flow_type) {
  99. case IRQF_TRIGGER_HIGH:
  100. type = 0;
  101. break;
  102. case IRQF_TRIGGER_RISING:
  103. type = 1;
  104. break;
  105. case IRQF_TRIGGER_FALLING:
  106. type = 2;
  107. break;
  108. case IRQF_TRIGGER_LOW:
  109. type = 3;
  110. break;
  111. default:
  112. type = 0;
  113. }
  114. ctrl_reg = in_be32(&intr->ctrl);
  115. ctrl_reg &= ~(0x3 << (22 - (l2irq * 2)));
  116. ctrl_reg |= (type << (22 - (l2irq * 2)));
  117. out_be32(&intr->ctrl, ctrl_reg);
  118. return 0;
  119. }
  120. static struct irq_chip mpc52xx_extirq_irqchip = {
  121. .typename = " MPC52xx IRQ[0-3] ",
  122. .mask = mpc52xx_extirq_mask,
  123. .unmask = mpc52xx_extirq_unmask,
  124. .ack = mpc52xx_extirq_ack,
  125. .set_type = mpc52xx_extirq_set_type,
  126. };
  127. /*
  128. * Main interrupt irq_chip
  129. */
  130. static void mpc52xx_main_mask(unsigned int virq)
  131. {
  132. int irq;
  133. int l2irq;
  134. irq = irq_map[virq].hwirq;
  135. l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
  136. pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
  137. io_be_setbit(&intr->main_mask, 16 - l2irq);
  138. }
  139. static void mpc52xx_main_unmask(unsigned int virq)
  140. {
  141. int irq;
  142. int l2irq;
  143. irq = irq_map[virq].hwirq;
  144. l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
  145. pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
  146. io_be_clrbit(&intr->main_mask, 16 - l2irq);
  147. }
  148. static struct irq_chip mpc52xx_main_irqchip = {
  149. .typename = "MPC52xx Main",
  150. .mask = mpc52xx_main_mask,
  151. .mask_ack = mpc52xx_main_mask,
  152. .unmask = mpc52xx_main_unmask,
  153. };
  154. /*
  155. * Peripherals interrupt irq_chip
  156. */
  157. static void mpc52xx_periph_mask(unsigned int virq)
  158. {
  159. int irq;
  160. int l2irq;
  161. irq = irq_map[virq].hwirq;
  162. l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
  163. pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
  164. io_be_setbit(&intr->per_mask, 31 - l2irq);
  165. }
  166. static void mpc52xx_periph_unmask(unsigned int virq)
  167. {
  168. int irq;
  169. int l2irq;
  170. irq = irq_map[virq].hwirq;
  171. l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
  172. pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
  173. io_be_clrbit(&intr->per_mask, 31 - l2irq);
  174. }
  175. static struct irq_chip mpc52xx_periph_irqchip = {
  176. .typename = "MPC52xx Peripherals",
  177. .mask = mpc52xx_periph_mask,
  178. .mask_ack = mpc52xx_periph_mask,
  179. .unmask = mpc52xx_periph_unmask,
  180. };
  181. /*
  182. * SDMA interrupt irq_chip
  183. */
  184. static void mpc52xx_sdma_mask(unsigned int virq)
  185. {
  186. int irq;
  187. int l2irq;
  188. irq = irq_map[virq].hwirq;
  189. l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
  190. pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
  191. io_be_setbit(&sdma->IntMask, l2irq);
  192. }
  193. static void mpc52xx_sdma_unmask(unsigned int virq)
  194. {
  195. int irq;
  196. int l2irq;
  197. irq = irq_map[virq].hwirq;
  198. l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
  199. pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
  200. io_be_clrbit(&sdma->IntMask, l2irq);
  201. }
  202. static void mpc52xx_sdma_ack(unsigned int virq)
  203. {
  204. int irq;
  205. int l2irq;
  206. irq = irq_map[virq].hwirq;
  207. l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
  208. pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
  209. out_be32(&sdma->IntPend, 1 << l2irq);
  210. }
  211. static struct irq_chip mpc52xx_sdma_irqchip = {
  212. .typename = "MPC52xx SDMA",
  213. .mask = mpc52xx_sdma_mask,
  214. .unmask = mpc52xx_sdma_unmask,
  215. .ack = mpc52xx_sdma_ack,
  216. };
  217. /*
  218. * irq_host
  219. */
  220. static int mpc52xx_irqhost_xlate(struct irq_host *h, struct device_node *ct,
  221. u32 * intspec, unsigned int intsize,
  222. irq_hw_number_t * out_hwirq,
  223. unsigned int *out_flags)
  224. {
  225. int intrvect_l1;
  226. int intrvect_l2;
  227. int intrvect_type;
  228. int intrvect_linux;
  229. if (intsize != 3)
  230. return -1;
  231. intrvect_l1 = (int)intspec[0];
  232. intrvect_l2 = (int)intspec[1];
  233. intrvect_type = (int)intspec[2];
  234. intrvect_linux =
  235. (intrvect_l1 << MPC52xx_IRQ_L1_OFFSET) & MPC52xx_IRQ_L1_MASK;
  236. intrvect_linux |=
  237. (intrvect_l2 << MPC52xx_IRQ_L2_OFFSET) & MPC52xx_IRQ_L2_MASK;
  238. pr_debug("return %x, l1=%d, l2=%d\n", intrvect_linux, intrvect_l1,
  239. intrvect_l2);
  240. *out_hwirq = intrvect_linux;
  241. *out_flags = mpc52xx_map_senses[intrvect_type];
  242. return 0;
  243. }
  244. /*
  245. * this function retrieves the correct IRQ type out
  246. * of the MPC regs
  247. * Only externals IRQs needs this
  248. */
  249. static int mpc52xx_irqx_gettype(int irq)
  250. {
  251. int type;
  252. u32 ctrl_reg;
  253. ctrl_reg = in_be32(&intr->ctrl);
  254. type = (ctrl_reg >> (22 - irq * 2)) & 0x3;
  255. return mpc52xx_map_senses[type];
  256. }
  257. static int mpc52xx_irqhost_map(struct irq_host *h, unsigned int virq,
  258. irq_hw_number_t irq)
  259. {
  260. int l1irq;
  261. int l2irq;
  262. struct irq_chip *good_irqchip;
  263. void *good_handle;
  264. int type;
  265. l1irq = (irq & MPC52xx_IRQ_L1_MASK) >> MPC52xx_IRQ_L1_OFFSET;
  266. l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
  267. /*
  268. * Most of ours IRQs will be level low
  269. * Only external IRQs on some platform may be others
  270. */
  271. type = IRQ_TYPE_LEVEL_LOW;
  272. switch (l1irq) {
  273. case MPC52xx_IRQ_L1_CRIT:
  274. pr_debug("%s: Critical. l2=%x\n", __func__, l2irq);
  275. BUG_ON(l2irq != 0);
  276. type = mpc52xx_irqx_gettype(l2irq);
  277. good_irqchip = &mpc52xx_extirq_irqchip;
  278. break;
  279. case MPC52xx_IRQ_L1_MAIN:
  280. pr_debug("%s: Main IRQ[1-3] l2=%x\n", __func__, l2irq);
  281. if ((l2irq >= 1) && (l2irq <= 3)) {
  282. type = mpc52xx_irqx_gettype(l2irq);
  283. good_irqchip = &mpc52xx_extirq_irqchip;
  284. } else {
  285. good_irqchip = &mpc52xx_main_irqchip;
  286. }
  287. break;
  288. case MPC52xx_IRQ_L1_PERP:
  289. pr_debug("%s: Peripherals. l2=%x\n", __func__, l2irq);
  290. good_irqchip = &mpc52xx_periph_irqchip;
  291. break;
  292. case MPC52xx_IRQ_L1_SDMA:
  293. pr_debug("%s: SDMA. l2=%x\n", __func__, l2irq);
  294. good_irqchip = &mpc52xx_sdma_irqchip;
  295. break;
  296. default:
  297. pr_debug("%s: Error, unknown L1 IRQ (0x%x)\n", __func__, l1irq);
  298. printk(KERN_ERR "Unknow IRQ!\n");
  299. return -EINVAL;
  300. }
  301. switch (type) {
  302. case IRQ_TYPE_EDGE_FALLING:
  303. case IRQ_TYPE_EDGE_RISING:
  304. good_handle = handle_edge_irq;
  305. break;
  306. default:
  307. good_handle = handle_level_irq;
  308. }
  309. set_irq_chip_and_handler(virq, good_irqchip, good_handle);
  310. pr_debug("%s: virq=%x, hw=%x. type=%x\n", __func__, virq,
  311. (int)irq, type);
  312. return 0;
  313. }
  314. static struct irq_host_ops mpc52xx_irqhost_ops = {
  315. .xlate = mpc52xx_irqhost_xlate,
  316. .map = mpc52xx_irqhost_map,
  317. };
  318. /*
  319. * init (public)
  320. */
  321. void __init mpc52xx_init_irq(void)
  322. {
  323. u32 intr_ctrl;
  324. struct device_node *picnode;
  325. struct device_node *np;
  326. /* Remap the necessary zones */
  327. picnode = of_find_matching_node(NULL, mpc52xx_pic_ids);
  328. intr = of_iomap(picnode, 0);
  329. if (!intr)
  330. panic(__FILE__ ": find_and_map failed on 'mpc5200-pic'. "
  331. "Check node !");
  332. np = of_find_matching_node(NULL, mpc52xx_sdma_ids);
  333. sdma = of_iomap(np, 0);
  334. of_node_put(np);
  335. if (!sdma)
  336. panic(__FILE__ ": find_and_map failed on 'mpc5200-bestcomm'. "
  337. "Check node !");
  338. /* Disable all interrupt sources. */
  339. out_be32(&sdma->IntPend, 0xffffffff); /* 1 means clear pending */
  340. out_be32(&sdma->IntMask, 0xffffffff); /* 1 means disabled */
  341. out_be32(&intr->per_mask, 0x7ffffc00); /* 1 means disabled */
  342. out_be32(&intr->main_mask, 0x00010fff); /* 1 means disabled */
  343. intr_ctrl = in_be32(&intr->ctrl);
  344. intr_ctrl &= 0x00ff0000; /* Keeps IRQ[0-3] config */
  345. intr_ctrl |= 0x0f000000 | /* clear IRQ 0-3 */
  346. 0x00001000 | /* MEE master external enable */
  347. 0x00000000 | /* 0 means disable IRQ 0-3 */
  348. 0x00000001; /* CEb route critical normally */
  349. out_be32(&intr->ctrl, intr_ctrl);
  350. /* Zero a bunch of the priority settings. */
  351. out_be32(&intr->per_pri1, 0);
  352. out_be32(&intr->per_pri2, 0);
  353. out_be32(&intr->per_pri3, 0);
  354. out_be32(&intr->main_pri1, 0);
  355. out_be32(&intr->main_pri2, 0);
  356. /*
  357. * As last step, add an irq host to translate the real
  358. * hw irq information provided by the ofw to linux virq
  359. */
  360. mpc52xx_irqhost = irq_alloc_host(picnode, IRQ_HOST_MAP_LINEAR,
  361. MPC52xx_IRQ_HIGHTESTHWIRQ,
  362. &mpc52xx_irqhost_ops, -1);
  363. if (!mpc52xx_irqhost)
  364. panic(__FILE__ ": Cannot allocate the IRQ host\n");
  365. printk(KERN_INFO "MPC52xx PIC is up and running!\n");
  366. }
  367. /*
  368. * get_irq (public)
  369. */
  370. unsigned int mpc52xx_get_irq(void)
  371. {
  372. u32 status;
  373. int irq = NO_IRQ_IGNORE;
  374. status = in_be32(&intr->enc_status);
  375. if (status & 0x00000400) { /* critical */
  376. irq = (status >> 8) & 0x3;
  377. if (irq == 2) /* high priority peripheral */
  378. goto peripheral;
  379. irq |= (MPC52xx_IRQ_L1_CRIT << MPC52xx_IRQ_L1_OFFSET) &
  380. MPC52xx_IRQ_L1_MASK;
  381. } else if (status & 0x00200000) { /* main */
  382. irq = (status >> 16) & 0x1f;
  383. if (irq == 4) /* low priority peripheral */
  384. goto peripheral;
  385. irq |= (MPC52xx_IRQ_L1_MAIN << MPC52xx_IRQ_L1_OFFSET) &
  386. MPC52xx_IRQ_L1_MASK;
  387. } else if (status & 0x20000000) { /* peripheral */
  388. peripheral:
  389. irq = (status >> 24) & 0x1f;
  390. if (irq == 0) { /* bestcomm */
  391. status = in_be32(&sdma->IntPend);
  392. irq = ffs(status) - 1;
  393. irq |= (MPC52xx_IRQ_L1_SDMA << MPC52xx_IRQ_L1_OFFSET) &
  394. MPC52xx_IRQ_L1_MASK;
  395. } else {
  396. irq |= (MPC52xx_IRQ_L1_PERP << MPC52xx_IRQ_L1_OFFSET) &
  397. MPC52xx_IRQ_L1_MASK;
  398. }
  399. }
  400. pr_debug("%s: irq=%x. virq=%d\n", __func__, irq,
  401. irq_linear_revmap(mpc52xx_irqhost, irq));
  402. return irq_linear_revmap(mpc52xx_irqhost, irq);
  403. }