sparc32,leon: cleaned away code from the LEON2 days
[cascardo/linux.git] / arch / sparc / kernel / leon_kernel.c
index 2969f77..5d7e41c 100644 (file)
 #include <asm/leon_amba.h>
 #include <asm/traps.h>
 #include <asm/cacheflush.h>
+#include <asm/smp.h>
 
 #include "prom.h"
 #include "irq.h"
 
 struct leon3_irqctrl_regs_map *leon3_irqctrl_regs; /* interrupt controller base address */
 struct leon3_gptimer_regs_map *leon3_gptimer_regs; /* timer controller base address */
-struct amba_apb_device leon_percpu_timer_dev[16];
 
 int leondebug_irq_disable;
 int leon_debug_irqout;
 static int dummy_master_l10_counter;
 unsigned long amba_system_id;
+static DEFINE_SPINLOCK(leon_irq_lock);
 
 unsigned long leon3_gptimer_irq; /* interrupt controller irq number */
 unsigned long leon3_gptimer_idx; /* Timer Index (0..6) within Timer Core */
+int leon3_ticker_irq; /* Timer ticker IRQ */
 unsigned int sparc_leon_eirq;
-#define LEON_IMASK ((&leon3_irqctrl_regs->mask[0]))
+#define LEON_IMASK (&leon3_irqctrl_regs->mask[0])
+#define LEON_IACK (&leon3_irqctrl_regs->iclear)
+#define LEON_DO_ACK_HW 1
 
-/* Return the IRQ of the pending IRQ on the extended IRQ controller */
-int sparc_leon_eirq_get(int eirq, int cpu)
+/* Return the last ACKed IRQ by the Extended IRQ controller. It has already
+ * been (automatically) ACKed when the CPU takes the trap.
+ */
+static inline unsigned int leon_eirq_get(int cpu)
 {
        return LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->intid[cpu]) & 0x1f;
 }
 
-irqreturn_t sparc_leon_eirq_isr(int dummy, void *dev_id)
+/* Handle one or multiple IRQs from the extended interrupt controller */
+static void leon_handle_ext_irq(unsigned int irq, struct irq_desc *desc)
 {
-       printk(KERN_ERR "sparc_leon_eirq_isr: ERROR EXTENDED IRQ\n");
-       return IRQ_HANDLED;
+       unsigned int eirq;
+       int cpu = hard_smp_processor_id();
+
+       eirq = leon_eirq_get(cpu);
+       if ((eirq & 0x10) && irq_map[eirq]->irq) /* bit4 tells if IRQ happened */
+               generic_handle_irq(irq_map[eirq]->irq);
 }
 
 /* The extended IRQ controller has been found, this function registers it */
-void sparc_leon_eirq_register(int eirq)
+void leon_eirq_setup(unsigned int eirq)
 {
-       int irq;
-
-       /* Register a "BAD" handler for this interrupt, it should never happen */
-       irq = request_irq(eirq, sparc_leon_eirq_isr,
-                         (IRQF_DISABLED | SA_STATIC_ALLOC), "extirq", NULL);
+       unsigned long mask, oldmask;
+       unsigned int veirq;
 
-       if (irq) {
-               printk(KERN_ERR
-                      "sparc_leon_eirq_register: unable to attach IRQ%d\n",
-                      eirq);
-       } else {
-               sparc_leon_eirq = eirq;
+       if (eirq < 1 || eirq > 0xf) {
+               printk(KERN_ERR "LEON EXT IRQ NUMBER BAD: %d\n", eirq);
+               return;
        }
 
+       veirq = leon_build_device_irq(eirq, leon_handle_ext_irq, "extirq", 0);
+
+       /*
+        * Unmask the Extended IRQ, the IRQs routed through the Ext-IRQ
+        * controller have a mask-bit of their own, so this is safe.
+        */
+       irq_link(veirq);
+       mask = 1 << eirq;
+       oldmask = LEON3_BYPASS_LOAD_PA(LEON_IMASK);
+       LEON3_BYPASS_STORE_PA(LEON_IMASK, (oldmask | mask));
+       sparc_leon_eirq = eirq;
 }
 
 static inline unsigned long get_irqmask(unsigned int irq)
@@ -83,35 +99,107 @@ static inline unsigned long get_irqmask(unsigned int irq)
        return mask;
 }
 
-static void leon_enable_irq(unsigned int irq_nr)
+static void leon_unmask_irq(struct irq_data *data)
 {
        unsigned long mask, flags;
-       mask = get_irqmask(irq_nr);
-       local_irq_save(flags);
+
+       mask = (unsigned long)data->chip_data;
+       spin_lock_irqsave(&leon_irq_lock, flags);
        LEON3_BYPASS_STORE_PA(LEON_IMASK,
                              (LEON3_BYPASS_LOAD_PA(LEON_IMASK) | (mask)));
-       local_irq_restore(flags);
+       spin_unlock_irqrestore(&leon_irq_lock, flags);
 }
 
-static void leon_disable_irq(unsigned int irq_nr)
+static void leon_mask_irq(struct irq_data *data)
 {
        unsigned long mask, flags;
-       mask = get_irqmask(irq_nr);
-       local_irq_save(flags);
+
+       mask = (unsigned long)data->chip_data;
+       spin_lock_irqsave(&leon_irq_lock, flags);
        LEON3_BYPASS_STORE_PA(LEON_IMASK,
                              (LEON3_BYPASS_LOAD_PA(LEON_IMASK) & ~(mask)));
-       local_irq_restore(flags);
+       spin_unlock_irqrestore(&leon_irq_lock, flags);
+}
 
+static unsigned int leon_startup_irq(struct irq_data *data)
+{
+       irq_link(data->irq);
+       leon_unmask_irq(data);
+       return 0;
+}
+
+static void leon_shutdown_irq(struct irq_data *data)
+{
+       leon_mask_irq(data);
+       irq_unlink(data->irq);
+}
+
+/* Used by external level sensitive IRQ handlers on the LEON: ACK IRQ ctrl */
+static void leon_eoi_irq(struct irq_data *data)
+{
+       unsigned long mask = (unsigned long)data->chip_data;
+
+       if (mask & LEON_DO_ACK_HW)
+               LEON3_BYPASS_STORE_PA(LEON_IACK, mask & ~LEON_DO_ACK_HW);
+}
+
+static struct irq_chip leon_irq = {
+       .name           = "leon",
+       .irq_startup    = leon_startup_irq,
+       .irq_shutdown   = leon_shutdown_irq,
+       .irq_mask       = leon_mask_irq,
+       .irq_unmask     = leon_unmask_irq,
+       .irq_eoi        = leon_eoi_irq,
+};
+
+/*
+ * Build a LEON IRQ for the edge triggered LEON IRQ controller:
+ *  Edge (normal) IRQ           - handle_simple_irq, ack=DONT-CARE, never ack
+ *  Level IRQ (PCI|Level-GPIO)  - handle_fasteoi_irq, ack=1, ack after ISR
+ *  Per-CPU Edge                - handle_percpu_irq, ack=0
+ */
+unsigned int leon_build_device_irq(unsigned int real_irq,
+                                   irq_flow_handler_t flow_handler,
+                                   const char *name, int do_ack)
+{
+       unsigned int irq;
+       unsigned long mask;
+
+       irq = 0;
+       mask = get_irqmask(real_irq);
+       if (mask == 0)
+               goto out;
+
+       irq = irq_alloc(real_irq, real_irq);
+       if (irq == 0)
+               goto out;
+
+       if (do_ack)
+               mask |= LEON_DO_ACK_HW;
+
+       irq_set_chip_and_handler_name(irq, &leon_irq,
+                                     flow_handler, name);
+       irq_set_chip_data(irq, (void *)mask);
+
+out:
+       return irq;
+}
+
+static unsigned int _leon_build_device_irq(struct platform_device *op,
+                                          unsigned int real_irq)
+{
+       return leon_build_device_irq(real_irq, handle_simple_irq, "edge", 0);
 }
 
 void __init leon_init_timers(irq_handler_t counter_fn)
 {
-       int irq;
+       int irq, eirq;
        struct device_node *rootnp, *np, *nnp;
        struct property *pp;
        int len;
        int cpu, icsel;
        int ampopts;
+       int err;
 
        leondebug_irq_disable = 0;
        leon_debug_irqout = 0;
@@ -183,9 +271,7 @@ void __init leon_init_timers(irq_handler_t counter_fn)
                        &leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl, 0);
 
 #ifdef CONFIG_SMP
-               leon_percpu_timer_dev[0].start = (int)leon3_gptimer_regs;
-               leon_percpu_timer_dev[0].irq = leon3_gptimer_irq + 1 +
-                                              leon3_gptimer_idx;
+               leon3_ticker_irq = leon3_gptimer_irq + 1 + leon3_gptimer_idx;
 
                if (!(LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->config) &
                      (1<<LEON3_GPTIMER_SEPIRQ))) {
@@ -215,41 +301,25 @@ void __init leon_init_timers(irq_handler_t counter_fn)
                icsel = LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->icsel[cpu/8]);
                icsel = (icsel >> ((7 - (cpu&0x7)) * 4)) & 0xf;
                leon3_irqctrl_regs += icsel;
+
+               /* Probe extended IRQ controller */
+               eirq = (LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->mpstatus)
+                       >> 16) & 0xf;
+               if (eirq != 0)
+                       leon_eirq_setup(eirq);
        } else {
                goto bad;
        }
 
-       irq = request_irq(leon3_gptimer_irq+leon3_gptimer_idx,
-                         counter_fn,
-                         (IRQF_DISABLED | SA_STATIC_ALLOC), "timer", NULL);
+       irq = _leon_build_device_irq(NULL, leon3_gptimer_irq+leon3_gptimer_idx);
+       err = request_irq(irq, counter_fn, IRQF_TIMER, "timer", NULL);
 
-       if (irq) {
+       if (err) {
                printk(KERN_ERR "leon_time_init: unable to attach IRQ%d\n",
-                      LEON_INTERRUPT_TIMER1);
+                      irq);
                prom_halt();
        }
 
-# ifdef CONFIG_SMP
-       {
-               unsigned long flags;
-               struct tt_entry *trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (leon_percpu_timer_dev[0].irq - 1)];
-
-               /* For SMP we use the level 14 ticker, however the bootup code
-                * has copied the firmwares level 14 vector into boot cpu's
-                * trap table, we must fix this now or we get squashed.
-                */
-               local_irq_save(flags);
-
-               patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
-
-               /* Adjust so that we jump directly to smpleon_ticker */
-               trap_table->inst_three += smpleon_ticker - real_irq_entry;
-
-               local_flush_cache_all();
-               local_irq_restore(flags);
-       }
-# endif
-
        if (leon3_gptimer_regs) {
                LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl,
                                      LEON3_GPTIMER_EN |
@@ -257,6 +327,18 @@ void __init leon_init_timers(irq_handler_t counter_fn)
                                      LEON3_GPTIMER_LD | LEON3_GPTIMER_IRQEN);
 
 #ifdef CONFIG_SMP
+               /* Install per-cpu IRQ handler for broadcasted ticker */
+               irq = leon_build_device_irq(leon3_ticker_irq,
+                                               handle_percpu_irq, "per-cpu",
+                                               0);
+               err = request_irq(irq, leon_percpu_timer_interrupt,
+                                       IRQF_PERCPU | IRQF_TIMER, "ticker",
+                                       NULL);
+               if (err) {
+                       printk(KERN_ERR "unable to attach ticker IRQ%d\n", irq);
+                       prom_halt();
+               }
+
                LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx+1].ctrl,
                                      LEON3_GPTIMER_EN |
                                      LEON3_GPTIMER_RL |
@@ -337,22 +419,18 @@ void leon_enable_irq_cpu(unsigned int irq_nr, unsigned int cpu)
 {
        unsigned long mask, flags, *addr;
        mask = get_irqmask(irq_nr);
-       local_irq_save(flags);
+       spin_lock_irqsave(&leon_irq_lock, flags);
        addr = (unsigned long *)&(leon3_irqctrl_regs->mask[cpu]);
        LEON3_BYPASS_STORE_PA(addr, (LEON3_BYPASS_LOAD_PA(addr) | (mask)));
-       local_irq_restore(flags);
+       spin_unlock_irqrestore(&leon_irq_lock, flags);
 }
 
 #endif
 
 void __init leon_init_IRQ(void)
 {
-       sparc_irq_config.init_timers = leon_init_timers;
-
-       BTFIXUPSET_CALL(enable_irq, leon_enable_irq, BTFIXUPCALL_NORM);
-       BTFIXUPSET_CALL(disable_irq, leon_disable_irq, BTFIXUPCALL_NORM);
-       BTFIXUPSET_CALL(enable_pil_irq, leon_enable_irq, BTFIXUPCALL_NORM);
-       BTFIXUPSET_CALL(disable_pil_irq, leon_disable_irq, BTFIXUPCALL_NORM);
+       sparc_irq_config.init_timers      = leon_init_timers;
+       sparc_irq_config.build_device_irq = _leon_build_device_irq;
 
        BTFIXUPSET_CALL(clear_clock_irq, leon_clear_clock_irq,
                        BTFIXUPCALL_NORM);