irq.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. *
  3. * By Dustin McIntire (dustin@sensoria.com) (c)2001
  4. *
  5. * Setup and IRQ handling code for the HD64465 companion chip.
  6. * by Greg Banks <gbanks@pocketpenguins.com>
  7. * Copyright (c) 2000 PocketPenguins Inc
  8. *
  9. * Derived from setup_hd64465.c which bore the message:
  10. * Greg Banks <gbanks@pocketpenguins.com>
  11. * Copyright (c) 2000 PocketPenguins Inc and
  12. * Copyright (C) 2000 YAEGASHI Takeshi
  13. * and setup_cqreek.c which bore message:
  14. * Copyright (C) 2000 Niibe Yutaka
  15. *
  16. * May be copied or modified under the terms of the GNU General Public
  17. * License. See linux/COPYING for more information.
  18. *
  19. * IRQ functions for a Hitachi Big Sur Evaluation Board.
  20. *
  21. */
  22. #include <linux/config.h>
  23. #include <linux/sched.h>
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/param.h>
  27. #include <linux/ioport.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/init.h>
  30. #include <linux/irq.h>
  31. #include <linux/bitops.h>
  32. #include <asm/io.h>
  33. #include <asm/irq.h>
  34. #include <asm/bigsur/io.h>
  35. #include <asm/hd64465/hd64465.h>
  36. #include <asm/bigsur/bigsur.h>
  37. //#define BIGSUR_DEBUG 3
  38. #undef BIGSUR_DEBUG
  39. #ifdef BIGSUR_DEBUG
  40. #define DPRINTK(args...) printk(args)
  41. #define DIPRINTK(n, args...) if (BIGSUR_DEBUG>(n)) printk(args)
  42. #else
  43. #define DPRINTK(args...)
  44. #define DIPRINTK(n, args...)
  45. #endif /* BIGSUR_DEBUG */
  46. #ifdef CONFIG_HD64465
  47. extern int hd64465_irq_demux(int irq);
  48. #endif /* CONFIG_HD64465 */
  49. /*===========================================================*/
  50. // Big Sur CPLD IRQ Routines
  51. /*===========================================================*/
  52. /* Level 1 IRQ routines */
  53. static void disable_bigsur_l1irq(unsigned int irq)
  54. {
  55. unsigned long flags;
  56. unsigned char mask;
  57. unsigned int mask_port = ((irq - BIGSUR_IRQ_LOW)/8) ? BIGSUR_IRLMR1 : BIGSUR_IRLMR0;
  58. unsigned char bit = (1 << ((irq - MGATE_IRQ_LOW)%8) );
  59. if(irq >= BIGSUR_IRQ_LOW && irq < BIGSUR_IRQ_HIGH) {
  60. DPRINTK("Disable L1 IRQ %d\n", irq);
  61. DIPRINTK(2,"disable_bigsur_l1irq: IMR=0x%08x mask=0x%x\n",
  62. mask_port, bit);
  63. local_irq_save(flags);
  64. /* Disable IRQ - set mask bit */
  65. mask = inb(mask_port) | bit;
  66. outb(mask, mask_port);
  67. local_irq_restore(flags);
  68. return;
  69. }
  70. DPRINTK("disable_bigsur_l1irq: Invalid IRQ %d\n", irq);
  71. }
  72. static void enable_bigsur_l1irq(unsigned int irq)
  73. {
  74. unsigned long flags;
  75. unsigned char mask;
  76. unsigned int mask_port = ((irq - BIGSUR_IRQ_LOW)/8) ? BIGSUR_IRLMR1 : BIGSUR_IRLMR0;
  77. unsigned char bit = (1 << ((irq - MGATE_IRQ_LOW)%8) );
  78. if(irq >= BIGSUR_IRQ_LOW && irq < BIGSUR_IRQ_HIGH) {
  79. DPRINTK("Enable L1 IRQ %d\n", irq);
  80. DIPRINTK(2,"enable_bigsur_l1irq: IMR=0x%08x mask=0x%x\n",
  81. mask_port, bit);
  82. local_irq_save(flags);
  83. /* Enable L1 IRQ - clear mask bit */
  84. mask = inb(mask_port) & ~bit;
  85. outb(mask, mask_port);
  86. local_irq_restore(flags);
  87. return;
  88. }
  89. DPRINTK("enable_bigsur_l1irq: Invalid IRQ %d\n", irq);
  90. }
  91. /* Level 2 irq masks and registers for L2 decoding */
  92. /* Level2 bitmasks for each level 1 IRQ */
  93. const u32 bigsur_l2irq_mask[] =
  94. {0x40,0x80,0x08,0x01,0x01,0x3C,0x3E,0xFF,0x40,0x80,0x06,0x03};
  95. /* Level2 to ISR[n] map for each level 1 IRQ */
  96. const u32 bigsur_l2irq_reg[] =
  97. { 2, 2, 3, 3, 1, 2, 1, 0, 1, 1, 3, 2};
  98. /* Level2 to Level 1 IRQ map */
  99. const u32 bigsur_l2_l1_map[] =
  100. {7,7,7,7,7,7,7,7, 4,6,6,6,6,6,8,9, 11,11,5,5,5,5,0,1, 3,10,10,2,-1,-1,-1,-1};
  101. /* IRQ inactive level (high or low) */
  102. const u32 bigsur_l2_inactv_state[] = {0x00, 0xBE, 0xFC, 0xF7};
  103. /* CPLD external status and mask registers base and offsets */
  104. static const u32 isr_base = BIGSUR_IRQ0;
  105. static const u32 isr_offset = BIGSUR_IRQ0 - BIGSUR_IRQ1;
  106. static const u32 imr_base = BIGSUR_IMR0;
  107. static const u32 imr_offset = BIGSUR_IMR0 - BIGSUR_IMR1;
  108. #define REG_NUM(irq) ((irq-BIGSUR_2NDLVL_IRQ_LOW)/8 )
  109. /* Level 2 IRQ routines */
  110. static void disable_bigsur_l2irq(unsigned int irq)
  111. {
  112. unsigned long flags;
  113. unsigned char mask;
  114. unsigned char bit = 1 << ((irq-BIGSUR_2NDLVL_IRQ_LOW)%8);
  115. unsigned int mask_port = imr_base - REG_NUM(irq)*imr_offset;
  116. if(irq >= BIGSUR_2NDLVL_IRQ_LOW && irq < BIGSUR_2NDLVL_IRQ_HIGH) {
  117. DPRINTK("Disable L2 IRQ %d\n", irq);
  118. DIPRINTK(2,"disable_bigsur_l2irq: IMR=0x%08x mask=0x%x\n",
  119. mask_port, bit);
  120. local_irq_save(flags);
  121. /* Disable L2 IRQ - set mask bit */
  122. mask = inb(mask_port) | bit;
  123. outb(mask, mask_port);
  124. local_irq_restore(flags);
  125. return;
  126. }
  127. DPRINTK("disable_bigsur_l2irq: Invalid IRQ %d\n", irq);
  128. }
  129. static void enable_bigsur_l2irq(unsigned int irq)
  130. {
  131. unsigned long flags;
  132. unsigned char mask;
  133. unsigned char bit = 1 << ((irq-BIGSUR_2NDLVL_IRQ_LOW)%8);
  134. unsigned int mask_port = imr_base - REG_NUM(irq)*imr_offset;
  135. if(irq >= BIGSUR_2NDLVL_IRQ_LOW && irq < BIGSUR_2NDLVL_IRQ_HIGH) {
  136. DPRINTK("Enable L2 IRQ %d\n", irq);
  137. DIPRINTK(2,"enable_bigsur_l2irq: IMR=0x%08x mask=0x%x\n",
  138. mask_port, bit);
  139. local_irq_save(flags);
  140. /* Enable L2 IRQ - clear mask bit */
  141. mask = inb(mask_port) & ~bit;
  142. outb(mask, mask_port);
  143. local_irq_restore(flags);
  144. return;
  145. }
  146. DPRINTK("enable_bigsur_l2irq: Invalid IRQ %d\n", irq);
  147. }
  148. static void mask_and_ack_bigsur(unsigned int irq)
  149. {
  150. DPRINTK("mask_and_ack_bigsur IRQ %d\n", irq);
  151. if(irq >= BIGSUR_IRQ_LOW && irq < BIGSUR_IRQ_HIGH)
  152. disable_bigsur_l1irq(irq);
  153. else
  154. disable_bigsur_l2irq(irq);
  155. }
  156. static void end_bigsur_irq(unsigned int irq)
  157. {
  158. DPRINTK("end_bigsur_irq IRQ %d\n", irq);
  159. if (!(irq_desc[irq].status & (IRQ_DISABLED|IRQ_INPROGRESS))) {
  160. if(irq >= BIGSUR_IRQ_LOW && irq < BIGSUR_IRQ_HIGH)
  161. enable_bigsur_l1irq(irq);
  162. else
  163. enable_bigsur_l2irq(irq);
  164. }
  165. }
  166. static unsigned int startup_bigsur_irq(unsigned int irq)
  167. {
  168. u8 mask;
  169. u32 reg;
  170. DPRINTK("startup_bigsur_irq IRQ %d\n", irq);
  171. if(irq >= BIGSUR_IRQ_LOW && irq < BIGSUR_IRQ_HIGH) {
  172. /* Enable the L1 IRQ */
  173. enable_bigsur_l1irq(irq);
  174. /* Enable all L2 IRQs in this L1 IRQ */
  175. mask = ~(bigsur_l2irq_mask[irq-BIGSUR_IRQ_LOW]);
  176. reg = imr_base - bigsur_l2irq_reg[irq-BIGSUR_IRQ_LOW] * imr_offset;
  177. mask &= inb(reg);
  178. outb(mask,reg);
  179. DIPRINTK(2,"startup_bigsur_irq: IMR=0x%08x mask=0x%x\n",reg,inb(reg));
  180. }
  181. else {
  182. /* Enable the L2 IRQ - clear mask bit */
  183. enable_bigsur_l2irq(irq);
  184. /* Enable the L1 bit masking this L2 IRQ */
  185. enable_bigsur_l1irq(bigsur_l2_l1_map[irq-BIGSUR_2NDLVL_IRQ_LOW]);
  186. DIPRINTK(2,"startup_bigsur_irq: L1=%d L2=%d\n",
  187. bigsur_l2_l1_map[irq-BIGSUR_2NDLVL_IRQ_LOW],irq);
  188. }
  189. return 0;
  190. }
  191. static void shutdown_bigsur_irq(unsigned int irq)
  192. {
  193. DPRINTK("shutdown_bigsur_irq IRQ %d\n", irq);
  194. if(irq >= BIGSUR_IRQ_LOW && irq < BIGSUR_IRQ_HIGH)
  195. disable_bigsur_l1irq(irq);
  196. else
  197. disable_bigsur_l2irq(irq);
  198. }
  199. /* Define the IRQ structures for the L1 and L2 IRQ types */
  200. static struct hw_interrupt_type bigsur_l1irq_type = {
  201. "BigSur-CPLD-Level1-IRQ",
  202. startup_bigsur_irq,
  203. shutdown_bigsur_irq,
  204. enable_bigsur_l1irq,
  205. disable_bigsur_l1irq,
  206. mask_and_ack_bigsur,
  207. end_bigsur_irq
  208. };
  209. static struct hw_interrupt_type bigsur_l2irq_type = {
  210. "BigSur-CPLD-Level2-IRQ",
  211. startup_bigsur_irq,
  212. shutdown_bigsur_irq,
  213. enable_bigsur_l2irq,
  214. disable_bigsur_l2irq,
  215. mask_and_ack_bigsur,
  216. end_bigsur_irq
  217. };
  218. static void make_bigsur_l1isr(unsigned int irq) {
  219. /* sanity check first */
  220. if(irq >= BIGSUR_IRQ_LOW && irq < BIGSUR_IRQ_HIGH) {
  221. /* save the handler in the main description table */
  222. irq_desc[irq].handler = &bigsur_l1irq_type;
  223. irq_desc[irq].status = IRQ_DISABLED;
  224. irq_desc[irq].action = 0;
  225. irq_desc[irq].depth = 1;
  226. disable_bigsur_l1irq(irq);
  227. return;
  228. }
  229. DPRINTK("make_bigsur_l1isr: bad irq, %d\n", irq);
  230. return;
  231. }
  232. static void make_bigsur_l2isr(unsigned int irq) {
  233. /* sanity check first */
  234. if(irq >= BIGSUR_2NDLVL_IRQ_LOW && irq < BIGSUR_2NDLVL_IRQ_HIGH) {
  235. /* save the handler in the main description table */
  236. irq_desc[irq].handler = &bigsur_l2irq_type;
  237. irq_desc[irq].status = IRQ_DISABLED;
  238. irq_desc[irq].action = 0;
  239. irq_desc[irq].depth = 1;
  240. disable_bigsur_l2irq(irq);
  241. return;
  242. }
  243. DPRINTK("make_bigsur_l2isr: bad irq, %d\n", irq);
  244. return;
  245. }
  246. /* The IRQ's will be decoded as follows:
  247. * If a level 2 handler exists and there is an unmasked active
  248. * IRQ, the 2nd level handler will be called.
  249. * If a level 2 handler does not exist for the active IRQ
  250. * the 1st level handler will be called.
  251. */
  252. int bigsur_irq_demux(int irq)
  253. {
  254. int dmux_irq = irq;
  255. u8 mask, actv_irqs;
  256. u32 reg_num;
  257. DIPRINTK(3,"bigsur_irq_demux, irq=%d\n", irq);
  258. /* decode the 1st level IRQ */
  259. if(irq >= BIGSUR_IRQ_LOW && irq < BIGSUR_IRQ_HIGH) {
  260. /* Get corresponding L2 ISR bitmask and ISR number */
  261. mask = bigsur_l2irq_mask[irq-BIGSUR_IRQ_LOW];
  262. reg_num = bigsur_l2irq_reg[irq-BIGSUR_IRQ_LOW];
  263. /* find the active IRQ's (XOR with inactive level)*/
  264. actv_irqs = inb(isr_base-reg_num*isr_offset) ^
  265. bigsur_l2_inactv_state[reg_num];
  266. /* decode active IRQ's */
  267. actv_irqs = actv_irqs & mask & ~(inb(imr_base-reg_num*imr_offset));
  268. /* if NEZ then we have an active L2 IRQ */
  269. if(actv_irqs) dmux_irq = ffz(~actv_irqs) + reg_num*8+BIGSUR_2NDLVL_IRQ_LOW;
  270. /* if no 2nd level IRQ action, but has 1st level, use 1st level handler */
  271. if(!irq_desc[dmux_irq].action && irq_desc[irq].action)
  272. dmux_irq = irq;
  273. DIPRINTK(1,"bigsur_irq_demux: irq=%d dmux_irq=%d mask=0x%04x reg=%d\n",
  274. irq, dmux_irq, mask, reg_num);
  275. }
  276. #ifdef CONFIG_HD64465
  277. dmux_irq = hd64465_irq_demux(dmux_irq);
  278. #endif /* CONFIG_HD64465 */
  279. DIPRINTK(3,"bigsur_irq_demux, demux_irq=%d\n", dmux_irq);
  280. return dmux_irq;
  281. }
  282. /*===========================================================*/
  283. // Big Sur Init Routines
  284. /*===========================================================*/
  285. void __init init_bigsur_IRQ(void)
  286. {
  287. int i;
  288. if (!MACH_BIGSUR) return;
  289. /* Create ISR's for Big Sur CPLD IRQ's */
  290. /*==============================================================*/
  291. for(i=BIGSUR_IRQ_LOW;i<BIGSUR_IRQ_HIGH;i++)
  292. make_bigsur_l1isr(i);
  293. printk(KERN_INFO "Big Sur CPLD L1 interrupts %d to %d.\n",
  294. BIGSUR_IRQ_LOW,BIGSUR_IRQ_HIGH);
  295. for(i=BIGSUR_2NDLVL_IRQ_LOW;i<BIGSUR_2NDLVL_IRQ_HIGH;i++)
  296. make_bigsur_l2isr(i);
  297. printk(KERN_INFO "Big Sur CPLD L2 interrupts %d to %d.\n",
  298. BIGSUR_2NDLVL_IRQ_LOW,BIGSUR_2NDLVL_IRQ_HIGH);
  299. }