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