mpc52xx_pic.c 10 KB

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