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> |
Scott McNutt | c2ced00 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 30 | #include <asm/io.h> |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 31 | #include <asm/ptrace.h> |
| 32 | #include <common.h> |
| 33 | #include <command.h> |
| 34 | #include <watchdog.h> |
| 35 | #ifdef CONFIG_STATUS_LED |
| 36 | #include <status_led.h> |
| 37 | #endif |
| 38 | |
| 39 | #if defined(CFG_NIOS_TMRBASE) && !defined(CFG_NIOS_TMRIRQ) |
| 40 | #error CFG_NIOS_TMRIRQ not defined (see documentation) |
| 41 | #endif |
| 42 | |
| 43 | /****************************************************************************/ |
| 44 | |
| 45 | struct irq_action { |
| 46 | interrupt_handler_t *handler; |
| 47 | void *arg; |
| 48 | int count; |
| 49 | }; |
| 50 | |
| 51 | static struct irq_action vecs[32]; |
| 52 | |
| 53 | /*************************************************************************/ |
| 54 | volatile ulong timestamp = 0; |
| 55 | |
| 56 | void reset_timer (void) |
| 57 | { |
| 58 | timestamp = 0; |
| 59 | } |
| 60 | |
| 61 | ulong get_timer (ulong base) |
| 62 | { |
| 63 | WATCHDOG_RESET (); |
| 64 | return (timestamp - base); |
| 65 | } |
| 66 | |
| 67 | void set_timer (ulong t) |
| 68 | { |
| 69 | timestamp = t; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /* The board must handle this interrupt if a timer is not |
| 74 | * provided. |
| 75 | */ |
| 76 | #if defined(CFG_NIOS_TMRBASE) |
| 77 | void tmr_isr (void *arg) |
| 78 | { |
| 79 | nios_timer_t *tmr = (nios_timer_t *)arg; |
| 80 | /* Interrupt is cleared by writing anything to the |
| 81 | * status register. |
| 82 | */ |
Scott McNutt | c2ced00 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 83 | writel (&tmr->status, 0); |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 84 | timestamp += CFG_NIOS_TMRMS; |
| 85 | #ifdef CONFIG_STATUS_LED |
| 86 | status_led_tick(timestamp); |
| 87 | #endif |
| 88 | } |
| 89 | |
| 90 | static void tmr_init (void) |
| 91 | { |
Scott McNutt | c2ced00 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 92 | nios_timer_t *tmr =(nios_timer_t *)CFG_NIOS_TMRBASE; |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 93 | |
Scott McNutt | c2ced00 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 94 | writel (&tmr->status, 0); |
| 95 | writel (&tmr->control, 0); |
| 96 | writel (&tmr->control, NIOS_TIMER_STOP); |
| 97 | |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 98 | #if defined(CFG_NIOS_TMRCNT) |
Scott McNutt | c2ced00 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 99 | writel (&tmr->periodl, CFG_NIOS_TMRCNT & 0xffff); |
| 100 | writel (&tmr->periodh, (CFG_NIOS_TMRCNT >> 16) & 0xffff); |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 101 | #endif |
Scott McNutt | c2ced00 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 102 | writel (&tmr->control, NIOS_TIMER_ITO | NIOS_TIMER_CONT | |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 103 | NIOS_TIMER_START ); |
| 104 | irq_install_handler (CFG_NIOS_TMRIRQ, tmr_isr, (void *)tmr); |
| 105 | } |
| 106 | |
| 107 | #endif /* CFG_NIOS_TMRBASE */ |
| 108 | |
| 109 | /*************************************************************************/ |
| 110 | int disable_interrupts (void) |
| 111 | { |
| 112 | int val = rdctl (CTL_STATUS); |
| 113 | wrctl (CTL_STATUS, val & ~STATUS_IE); |
| 114 | return (val & STATUS_IE); |
| 115 | } |
| 116 | |
| 117 | void enable_interrupts( void ) |
| 118 | { |
| 119 | int val = rdctl (CTL_STATUS); |
| 120 | wrctl (CTL_STATUS, val | STATUS_IE); |
| 121 | } |
| 122 | |
| 123 | void external_interrupt (struct pt_regs *regs) |
| 124 | { |
| 125 | unsigned irqs; |
| 126 | struct irq_action *act; |
| 127 | |
| 128 | /* Evaluate only irqs that are both enabled AND pending */ |
| 129 | irqs = rdctl (CTL_IENABLE) & rdctl (CTL_IPENDING); |
| 130 | act = vecs; |
| 131 | |
| 132 | /* Assume (as does the Nios2 HAL) that bit 0 is highest |
| 133 | * priority. NOTE: There is ALWAYS a handler assigned |
| 134 | * (the default if no other). |
| 135 | */ |
| 136 | while (irqs) { |
| 137 | if (irqs & 1) { |
| 138 | act->handler (act->arg); |
| 139 | act->count++; |
| 140 | } |
| 141 | irqs >>=1; |
| 142 | act++; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | static void def_hdlr (void *arg) |
| 147 | { |
| 148 | unsigned irqs = rdctl (CTL_IENABLE); |
| 149 | |
| 150 | /* Disable the individual interrupt -- with gratuitous |
| 151 | * warning. |
| 152 | */ |
| 153 | irqs &= ~(1 << (int)arg); |
| 154 | wrctl (CTL_IENABLE, irqs); |
| 155 | printf ("WARNING: Disabling unhandled interrupt: %d\n", |
| 156 | (int)arg); |
| 157 | } |
| 158 | |
| 159 | /*************************************************************************/ |
| 160 | void irq_install_handler (int irq, interrupt_handler_t *hdlr, void *arg) |
| 161 | { |
| 162 | |
| 163 | int flag; |
| 164 | struct irq_action *act; |
| 165 | unsigned ena = rdctl (CTL_IENABLE); |
| 166 | |
| 167 | if ((irq < 0) || (irq > 31)) |
| 168 | return; |
| 169 | act = &vecs[irq]; |
| 170 | |
| 171 | flag = disable_interrupts (); |
| 172 | if (hdlr) { |
| 173 | act->handler = hdlr; |
| 174 | act->arg = arg; |
| 175 | ena |= (1 << irq); /* enable */ |
| 176 | } else { |
| 177 | act->handler = def_hdlr; |
| 178 | act->arg = (void *)irq; |
| 179 | ena &= ~(1 << irq); /* disable */ |
| 180 | } |
| 181 | wrctl (CTL_IENABLE, ena); |
| 182 | if (flag) enable_interrupts (); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | int interrupt_init (void) |
| 187 | { |
| 188 | int i; |
| 189 | |
| 190 | /* Assign the default handler to all */ |
| 191 | for (i = 0; i < 32; i++) { |
| 192 | vecs[i].handler = def_hdlr; |
| 193 | vecs[i].arg = (void *)i; |
| 194 | vecs[i].count = 0; |
| 195 | } |
| 196 | |
| 197 | #if defined(CFG_NIOS_TMRBASE) |
| 198 | tmr_init (); |
| 199 | #endif |
| 200 | |
| 201 | enable_interrupts (); |
| 202 | return (0); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | /*************************************************************************/ |
| 207 | #if (CONFIG_COMMANDS & CFG_CMD_IRQ) |
| 208 | int do_irqinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 209 | { |
| 210 | int i; |
| 211 | struct irq_action *act = vecs; |
| 212 | |
| 213 | printf ("\nInterrupt-Information:\n\n"); |
| 214 | printf ("Nr Routine Arg Count\n"); |
| 215 | printf ("-----------------------------\n"); |
| 216 | |
| 217 | for (i=0; i<32; i++) { |
| 218 | if (act->handler != def_hdlr) { |
| 219 | printf ("%02d %08lx %08lx %d\n", |
| 220 | i, |
| 221 | (ulong)act->handler, |
| 222 | (ulong)act->arg, |
| 223 | act->count); |
| 224 | } |
| 225 | act++; |
| 226 | } |
| 227 | printf ("\n"); |
| 228 | |
| 229 | return (0); |
| 230 | } |
| 231 | #endif /* CONFIG_COMMANDS & CFG_CMD_IRQ */ |