irq.c 12 KB

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