wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * (C) Copyright 2000-2002 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * (C) Copyright 2004, Psyent Corporation <www.psyent.com> |
| 6 | * Scott McNutt <smcnutt@psyent.com> |
| 7 | * |
| 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | |
| 28 | #include <nios2.h> |
| 29 | #include <nios2-io.h> |
| 30 | #include <asm/ptrace.h> |
| 31 | #include <common.h> |
| 32 | #include <command.h> |
| 33 | #include <watchdog.h> |
| 34 | #ifdef CONFIG_STATUS_LED |
| 35 | #include <status_led.h> |
| 36 | #endif |
| 37 | |
| 38 | #if defined(CFG_NIOS_TMRBASE) && !defined(CFG_NIOS_TMRIRQ) |
| 39 | #error CFG_NIOS_TMRIRQ not defined (see documentation) |
| 40 | #endif |
| 41 | |
| 42 | /****************************************************************************/ |
| 43 | |
| 44 | struct irq_action { |
| 45 | interrupt_handler_t *handler; |
| 46 | void *arg; |
| 47 | int count; |
| 48 | }; |
| 49 | |
| 50 | static struct irq_action vecs[32]; |
| 51 | |
| 52 | /*************************************************************************/ |
| 53 | volatile ulong timestamp = 0; |
| 54 | |
| 55 | void reset_timer (void) |
| 56 | { |
| 57 | timestamp = 0; |
| 58 | } |
| 59 | |
| 60 | ulong get_timer (ulong base) |
| 61 | { |
| 62 | WATCHDOG_RESET (); |
| 63 | return (timestamp - base); |
| 64 | } |
| 65 | |
| 66 | void set_timer (ulong t) |
| 67 | { |
| 68 | timestamp = t; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /* The board must handle this interrupt if a timer is not |
| 73 | * provided. |
| 74 | */ |
| 75 | #if defined(CFG_NIOS_TMRBASE) |
| 76 | void tmr_isr (void *arg) |
| 77 | { |
| 78 | nios_timer_t *tmr = (nios_timer_t *)arg; |
| 79 | /* Interrupt is cleared by writing anything to the |
| 80 | * status register. |
| 81 | */ |
| 82 | tmr->status = 0; |
| 83 | timestamp += CFG_NIOS_TMRMS; |
| 84 | #ifdef CONFIG_STATUS_LED |
| 85 | status_led_tick(timestamp); |
| 86 | #endif |
| 87 | } |
| 88 | |
| 89 | static void tmr_init (void) |
| 90 | { |
| 91 | nios_timer_t *tmr =(nios_timer_t *)CACHE_BYPASS(CFG_NIOS_TMRBASE); |
| 92 | |
| 93 | tmr->control &= ~(NIOS_TIMER_START | NIOS_TIMER_ITO); |
| 94 | tmr->control |= NIOS_TIMER_STOP; |
| 95 | #if defined(CFG_NIOS_TMRCNT) |
| 96 | tmr->periodl = CFG_NIOS_TMRCNT & 0xffff; |
| 97 | tmr->periodh = (CFG_NIOS_TMRCNT >> 16) & 0xffff; |
| 98 | #endif |
| 99 | tmr->control |= ( NIOS_TIMER_ITO | |
| 100 | NIOS_TIMER_CONT | |
| 101 | NIOS_TIMER_START ); |
| 102 | irq_install_handler (CFG_NIOS_TMRIRQ, tmr_isr, (void *)tmr); |
| 103 | } |
| 104 | |
| 105 | #endif /* CFG_NIOS_TMRBASE */ |
| 106 | |
| 107 | /*************************************************************************/ |
| 108 | int disable_interrupts (void) |
| 109 | { |
| 110 | int val = rdctl (CTL_STATUS); |
| 111 | wrctl (CTL_STATUS, val & ~STATUS_IE); |
| 112 | return (val & STATUS_IE); |
| 113 | } |
| 114 | |
| 115 | void enable_interrupts( void ) |
| 116 | { |
| 117 | int val = rdctl (CTL_STATUS); |
| 118 | wrctl (CTL_STATUS, val | STATUS_IE); |
| 119 | } |
| 120 | |
| 121 | void external_interrupt (struct pt_regs *regs) |
| 122 | { |
| 123 | unsigned irqs; |
| 124 | struct irq_action *act; |
| 125 | |
| 126 | /* Evaluate only irqs that are both enabled AND pending */ |
| 127 | irqs = rdctl (CTL_IENABLE) & rdctl (CTL_IPENDING); |
| 128 | act = vecs; |
| 129 | |
| 130 | /* Assume (as does the Nios2 HAL) that bit 0 is highest |
| 131 | * priority. NOTE: There is ALWAYS a handler assigned |
| 132 | * (the default if no other). |
| 133 | */ |
| 134 | while (irqs) { |
| 135 | if (irqs & 1) { |
| 136 | act->handler (act->arg); |
| 137 | act->count++; |
| 138 | } |
| 139 | irqs >>=1; |
| 140 | act++; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | static void def_hdlr (void *arg) |
| 145 | { |
| 146 | unsigned irqs = rdctl (CTL_IENABLE); |
| 147 | |
| 148 | /* Disable the individual interrupt -- with gratuitous |
| 149 | * warning. |
| 150 | */ |
| 151 | irqs &= ~(1 << (int)arg); |
| 152 | wrctl (CTL_IENABLE, irqs); |
| 153 | printf ("WARNING: Disabling unhandled interrupt: %d\n", |
| 154 | (int)arg); |
| 155 | } |
| 156 | |
| 157 | /*************************************************************************/ |
| 158 | void irq_install_handler (int irq, interrupt_handler_t *hdlr, void *arg) |
| 159 | { |
| 160 | |
| 161 | int flag; |
| 162 | struct irq_action *act; |
| 163 | unsigned ena = rdctl (CTL_IENABLE); |
| 164 | |
| 165 | if ((irq < 0) || (irq > 31)) |
| 166 | return; |
| 167 | act = &vecs[irq]; |
| 168 | |
| 169 | flag = disable_interrupts (); |
| 170 | if (hdlr) { |
| 171 | act->handler = hdlr; |
| 172 | act->arg = arg; |
| 173 | ena |= (1 << irq); /* enable */ |
| 174 | } else { |
| 175 | act->handler = def_hdlr; |
| 176 | act->arg = (void *)irq; |
| 177 | ena &= ~(1 << irq); /* disable */ |
| 178 | } |
| 179 | wrctl (CTL_IENABLE, ena); |
| 180 | if (flag) enable_interrupts (); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | int interrupt_init (void) |
| 185 | { |
| 186 | int i; |
| 187 | |
| 188 | /* Assign the default handler to all */ |
| 189 | for (i = 0; i < 32; i++) { |
| 190 | vecs[i].handler = def_hdlr; |
| 191 | vecs[i].arg = (void *)i; |
| 192 | vecs[i].count = 0; |
| 193 | } |
| 194 | |
| 195 | #if defined(CFG_NIOS_TMRBASE) |
| 196 | tmr_init (); |
| 197 | #endif |
| 198 | |
| 199 | enable_interrupts (); |
| 200 | return (0); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | /*************************************************************************/ |
| 205 | #if (CONFIG_COMMANDS & CFG_CMD_IRQ) |
| 206 | int do_irqinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 207 | { |
| 208 | int i; |
| 209 | struct irq_action *act = vecs; |
| 210 | |
| 211 | printf ("\nInterrupt-Information:\n\n"); |
| 212 | printf ("Nr Routine Arg Count\n"); |
| 213 | printf ("-----------------------------\n"); |
| 214 | |
| 215 | for (i=0; i<32; i++) { |
| 216 | if (act->handler != def_hdlr) { |
| 217 | printf ("%02d %08lx %08lx %d\n", |
| 218 | i, |
| 219 | (ulong)act->handler, |
| 220 | (ulong)act->arg, |
| 221 | act->count); |
| 222 | } |
| 223 | act++; |
| 224 | } |
| 225 | printf ("\n"); |
| 226 | |
| 227 | return (0); |
| 228 | } |
| 229 | #endif /* CONFIG_COMMANDS & CFG_CMD_IRQ */ |