blob: aea612eef15e4a1c8c619579a2abdf67a5639daa [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk507bbe32004-04-18 21:13:41 +00002/*
Michal Simekcfc67112007-03-11 13:48:24 +01003 * (C) Copyright 2007 Michal Simek
wdenk507bbe32004-04-18 21:13:41 +00004 * (C) Copyright 2004 Atmark Techno, Inc.
5 *
Michal Simekcfc67112007-03-11 13:48:24 +01006 * Michal SIMEK <monstr@monstr.eu>
wdenk507bbe32004-04-18 21:13:41 +00007 * Yasushi SHOJI <yashi@atmark-techno.com>
wdenk507bbe32004-04-18 21:13:41 +00008 */
9
Michal Simekcfc67112007-03-11 13:48:24 +010010#include <common.h>
11#include <command.h>
Michal Simek9aa65ca2016-02-15 12:10:32 +010012#include <fdtdec.h>
Michal Simek575a3d22012-07-10 10:31:31 +020013#include <malloc.h>
Michal Simekcfc67112007-03-11 13:48:24 +010014#include <asm/microblaze_intc.h>
Michal Simek42efed62007-05-07 17:22:25 +020015#include <asm/asm.h>
Michal Simekcfc67112007-03-11 13:48:24 +010016
Michal Simek9aa65ca2016-02-15 12:10:32 +010017DECLARE_GLOBAL_DATA_PTR;
18
Michal Simek26e6da82012-06-29 13:27:28 +020019void enable_interrupts(void)
wdenk507bbe32004-04-18 21:13:41 +000020{
Michal Simek070b8e02015-01-26 15:25:32 +010021 debug("Enable interrupts for the whole CPU\n");
Michal Simekfb05f6d2007-05-07 23:58:31 +020022 MSRSET(0x2);
wdenk507bbe32004-04-18 21:13:41 +000023}
24
Michal Simek26e6da82012-06-29 13:27:28 +020025int disable_interrupts(void)
wdenk507bbe32004-04-18 21:13:41 +000026{
Michal Simek68e99e52010-12-21 08:30:39 +010027 unsigned int msr;
28
29 MFS(msr, rmsr);
Michal Simekfb05f6d2007-05-07 23:58:31 +020030 MSRCLR(0x2);
Michal Simek68e99e52010-12-21 08:30:39 +010031 return (msr & 0x2) != 0;
wdenk507bbe32004-04-18 21:13:41 +000032}
Michal Simekcfc67112007-03-11 13:48:24 +010033
Michal Simek575a3d22012-07-10 10:31:31 +020034static struct irq_action *vecs;
35static u32 irq_no;
Michal Simekcfc67112007-03-11 13:48:24 +010036
37/* mapping structure to interrupt controller */
Michal Simek575a3d22012-07-10 10:31:31 +020038microblaze_intc_t *intc;
Michal Simekcfc67112007-03-11 13:48:24 +010039
40/* default handler */
Michal Simek575a3d22012-07-10 10:31:31 +020041static void def_hdlr(void)
Michal Simekcfc67112007-03-11 13:48:24 +010042{
Michal Simek26e6da82012-06-29 13:27:28 +020043 puts("def_hdlr\n");
Michal Simekcfc67112007-03-11 13:48:24 +010044}
45
Michal Simek575a3d22012-07-10 10:31:31 +020046static void enable_one_interrupt(int irq)
Michal Simekcfc67112007-03-11 13:48:24 +010047{
48 int mask;
49 int offset = 1;
Michal Simek26e6da82012-06-29 13:27:28 +020050
Michal Simekcfc67112007-03-11 13:48:24 +010051 offset <<= irq;
52 mask = intc->ier;
53 intc->ier = (mask | offset);
Michal Simek4c0922f2015-01-26 14:37:52 +010054
55 debug("Enable one interrupt irq %x - mask %x,ier %x\n", offset, mask,
56 intc->ier);
57 debug("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier,
58 intc->iar, intc->mer);
Michal Simekcfc67112007-03-11 13:48:24 +010059}
60
Michal Simek575a3d22012-07-10 10:31:31 +020061static void disable_one_interrupt(int irq)
Michal Simekcfc67112007-03-11 13:48:24 +010062{
63 int mask;
64 int offset = 1;
Michal Simek26e6da82012-06-29 13:27:28 +020065
Michal Simekcfc67112007-03-11 13:48:24 +010066 offset <<= irq;
67 mask = intc->ier;
68 intc->ier = (mask & ~offset);
Michal Simek4c0922f2015-01-26 14:37:52 +010069
70 debug("Disable one interrupt irq %x - mask %x,ier %x\n", irq, mask,
71 intc->ier);
72 debug("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier,
73 intc->iar, intc->mer);
Michal Simekcfc67112007-03-11 13:48:24 +010074}
75
Michal Simek87069082012-06-29 14:21:52 +020076int install_interrupt_handler(int irq, interrupt_handler_t *hdlr, void *arg)
Michal Simekcfc67112007-03-11 13:48:24 +010077{
78 struct irq_action *act;
Michal Simek26e6da82012-06-29 13:27:28 +020079
Michal Simekcfc67112007-03-11 13:48:24 +010080 /* irq out of range */
Michal Simek575a3d22012-07-10 10:31:31 +020081 if ((irq < 0) || (irq > irq_no)) {
Michal Simek26e6da82012-06-29 13:27:28 +020082 puts("IRQ out of range\n");
Michal Simek87069082012-06-29 14:21:52 +020083 return -1;
Michal Simekcfc67112007-03-11 13:48:24 +010084 }
85 act = &vecs[irq];
86 if (hdlr) { /* enable */
87 act->handler = hdlr;
88 act->arg = arg;
89 act->count = 0;
Michal Simeke217b0d2015-01-26 14:39:22 +010090 enable_one_interrupt(irq);
Michal Simek87069082012-06-29 14:21:52 +020091 return 0;
Michal Simekcfc67112007-03-11 13:48:24 +010092 }
Michal Simek87069082012-06-29 14:21:52 +020093
94 /* Disable */
Michal Simeke217b0d2015-01-26 14:39:22 +010095 act->handler = (interrupt_handler_t *)def_hdlr;
Michal Simek87069082012-06-29 14:21:52 +020096 act->arg = (void *)irq;
97 disable_one_interrupt(irq);
98 return 1;
Michal Simekcfc67112007-03-11 13:48:24 +010099}
100
101/* initialization interrupt controller - hardware */
Michal Simek575a3d22012-07-10 10:31:31 +0200102static void intc_init(void)
Michal Simekcfc67112007-03-11 13:48:24 +0100103{
104 intc->mer = 0;
105 intc->ier = 0;
106 intc->iar = 0xFFFFFFFF;
107 /* XIntc_Start - hw_interrupt enable and all interrupt enable */
108 intc->mer = 0x3;
Michal Simek4c0922f2015-01-26 14:37:52 +0100109
110 debug("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier,
111 intc->iar, intc->mer);
Michal Simekcfc67112007-03-11 13:48:24 +0100112}
113
Michal Simek2c7c32f2015-01-27 12:44:12 +0100114int interrupt_init(void)
Michal Simekcfc67112007-03-11 13:48:24 +0100115{
116 int i;
Michal Simek9aa65ca2016-02-15 12:10:32 +0100117 const void *blob = gd->fdt_blob;
118 int node = 0;
119
120 debug("INTC: Initialization\n");
121
122 node = fdt_node_offset_by_compatible(blob, node,
123 "xlnx,xps-intc-1.00.a");
124 if (node != -1) {
125 fdt_addr_t base = fdtdec_get_addr(blob, node, "reg");
126 if (base == FDT_ADDR_T_NONE)
127 return -1;
128
129 debug("INTC: Base addr %lx\n", base);
130 intc = (microblaze_intc_t *)base;
131 irq_no = fdtdec_get_int(blob, node, "xlnx,num-intr-inputs", 0);
132 debug("INTC: IRQ NO %x\n", irq_no);
133 } else {
134 return node;
135 }
Michal Simeka359eaa2016-02-15 13:44:19 +0100136
Michal Simek575a3d22012-07-10 10:31:31 +0200137 if (irq_no) {
138 vecs = calloc(1, sizeof(struct irq_action) * irq_no);
139 if (vecs == NULL) {
140 puts("Interrupt vector allocation failed\n");
141 return -1;
142 }
143
144 /* initialize irq list */
145 for (i = 0; i < irq_no; i++) {
Michal Simeke217b0d2015-01-26 14:39:22 +0100146 vecs[i].handler = (interrupt_handler_t *)def_hdlr;
Michal Simek575a3d22012-07-10 10:31:31 +0200147 vecs[i].arg = (void *)i;
148 vecs[i].count = 0;
149 }
150 /* initialize intc controller */
151 intc_init();
152 enable_interrupts();
153 } else {
154 puts("Undefined interrupt controller\n");
Michal Simekcfc67112007-03-11 13:48:24 +0100155 }
Michal Simekcfc67112007-03-11 13:48:24 +0100156 return 0;
157}
158
Michal Simek26e6da82012-06-29 13:27:28 +0200159void interrupt_handler(void)
Michal Simekcfc67112007-03-11 13:48:24 +0100160{
Michal Simek8125c982010-04-16 11:51:59 +0200161 int irqs = intc->ivr; /* find active interrupt */
162 int mask = 1;
Michal Simek42efed62007-05-07 17:22:25 +0200163 int value;
Michal Simek8125c982010-04-16 11:51:59 +0200164 struct irq_action *act = vecs + irqs;
165
Michal Simek4c0922f2015-01-26 14:37:52 +0100166 debug("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier,
167 intc->iar, intc->mer);
168#ifdef DEBUG
169 R14(value);
Michal Simekcfc67112007-03-11 13:48:24 +0100170#endif
Michal Simek4c0922f2015-01-26 14:37:52 +0100171 debug("Interrupt handler on %x line, r14 %x\n", irqs, value);
172
173 debug("Jumping to interrupt handler rutine addr %x,count %x,arg %x\n",
174 (u32)act->handler, act->count, (u32)act->arg);
Michal Simeke217b0d2015-01-26 14:39:22 +0100175 act->handler(act->arg);
Michal Simek8125c982010-04-16 11:51:59 +0200176 act->count++;
Michal Simek42efed62007-05-07 17:22:25 +0200177
Stephan Linz0f883262012-02-22 19:12:43 +0100178 intc->iar = mask << irqs;
179
Michal Simek4c0922f2015-01-26 14:37:52 +0100180 debug("Dump INTC reg, isr %x, ier %x, iar %x, mer %x\n", intc->isr,
181 intc->ier, intc->iar, intc->mer);
182#ifdef DEBUG
Michal Simek42efed62007-05-07 17:22:25 +0200183 R14(value);
Michal Simekcfc67112007-03-11 13:48:24 +0100184#endif
Michal Simek4c0922f2015-01-26 14:37:52 +0100185 debug("Interrupt handler on %x line, r14 %x\n", irqs, value);
Michal Simekcfc67112007-03-11 13:48:24 +0100186}
Michal Simekcfc67112007-03-11 13:48:24 +0100187
Jon Loeliger44312832007-07-09 19:06:00 -0500188#if defined(CONFIG_CMD_IRQ)
Michal Simek575a3d22012-07-10 10:31:31 +0200189int do_irqinfo(cmd_tbl_t *cmdtp, int flag, int argc, const char *argv[])
Michal Simekcfc67112007-03-11 13:48:24 +0100190{
191 int i;
192 struct irq_action *act = vecs;
193
Michal Simek575a3d22012-07-10 10:31:31 +0200194 if (irq_no) {
195 puts("\nInterrupt-Information:\n\n"
196 "Nr Routine Arg Count\n"
197 "-----------------------------\n");
Michal Simekcfc67112007-03-11 13:48:24 +0100198
Michal Simek575a3d22012-07-10 10:31:31 +0200199 for (i = 0; i < irq_no; i++) {
Michal Simeke217b0d2015-01-26 14:39:22 +0100200 if (act->handler != (interrupt_handler_t *)def_hdlr) {
Michal Simek575a3d22012-07-10 10:31:31 +0200201 printf("%02d %08x %08x %d\n", i,
Michal Simeke217b0d2015-01-26 14:39:22 +0100202 (int)act->handler, (int)act->arg,
203 act->count);
Michal Simek575a3d22012-07-10 10:31:31 +0200204 }
205 act++;
Michal Simekcfc67112007-03-11 13:48:24 +0100206 }
Michal Simek575a3d22012-07-10 10:31:31 +0200207 puts("\n");
208 } else {
209 puts("Undefined interrupt controller\n");
Michal Simekcfc67112007-03-11 13:48:24 +0100210 }
Michal Simek575a3d22012-07-10 10:31:31 +0200211 return 0;
Michal Simekcfc67112007-03-11 13:48:24 +0100212}
Jon Loeliger44312832007-07-09 19:06:00 -0500213#endif