1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2907208c4SChristophe Leroy /*
3907208c4SChristophe Leroy * (C) Copyright 2000-2002
4907208c4SChristophe Leroy * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5907208c4SChristophe Leroy */
6907208c4SChristophe Leroy
7907208c4SChristophe Leroy #include <common.h>
8907208c4SChristophe Leroy #include <mpc8xx.h>
9907208c4SChristophe Leroy #include <mpc8xx_irq.h>
1018f8d4c6SChristophe Leroy #include <asm/cpm_8xx.h>
11907208c4SChristophe Leroy #include <asm/processor.h>
12ba3da734SChristophe Leroy #include <asm/io.h>
13907208c4SChristophe Leroy
14907208c4SChristophe Leroy /************************************************************************/
15907208c4SChristophe Leroy
16907208c4SChristophe Leroy /*
17907208c4SChristophe Leroy * CPM interrupt vector functions.
18907208c4SChristophe Leroy */
19907208c4SChristophe Leroy struct interrupt_action {
20907208c4SChristophe Leroy interrupt_handler_t *handler;
21907208c4SChristophe Leroy void *arg;
22907208c4SChristophe Leroy };
23907208c4SChristophe Leroy
24907208c4SChristophe Leroy static struct interrupt_action cpm_vecs[CPMVEC_NR];
25907208c4SChristophe Leroy static struct interrupt_action irq_vecs[NR_IRQS];
26907208c4SChristophe Leroy
27907208c4SChristophe Leroy static void cpm_interrupt_init(void);
28907208c4SChristophe Leroy static void cpm_interrupt(void *regs);
29907208c4SChristophe Leroy
30907208c4SChristophe Leroy /************************************************************************/
31907208c4SChristophe Leroy
interrupt_init_cpu(unsigned * decrementer_count)32deff9b1dSTom Rini void interrupt_init_cpu(unsigned *decrementer_count)
33907208c4SChristophe Leroy {
34ba3da734SChristophe Leroy immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
35907208c4SChristophe Leroy
36907208c4SChristophe Leroy *decrementer_count = get_tbclk() / CONFIG_SYS_HZ;
37907208c4SChristophe Leroy
38907208c4SChristophe Leroy /* disable all interrupts */
39ba3da734SChristophe Leroy out_be32(&immr->im_siu_conf.sc_simask, 0);
40907208c4SChristophe Leroy
41907208c4SChristophe Leroy /* Configure CPM interrupts */
42907208c4SChristophe Leroy cpm_interrupt_init();
43907208c4SChristophe Leroy }
44907208c4SChristophe Leroy
45907208c4SChristophe Leroy /************************************************************************/
46907208c4SChristophe Leroy
47907208c4SChristophe Leroy /*
48907208c4SChristophe Leroy * Handle external interrupts
49907208c4SChristophe Leroy */
external_interrupt(struct pt_regs * regs)50907208c4SChristophe Leroy void external_interrupt(struct pt_regs *regs)
51907208c4SChristophe Leroy {
52ba3da734SChristophe Leroy immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
53907208c4SChristophe Leroy int irq;
54ba3da734SChristophe Leroy ulong simask;
55907208c4SChristophe Leroy ulong vec, v_bit;
56907208c4SChristophe Leroy
57907208c4SChristophe Leroy /*
58907208c4SChristophe Leroy * read the SIVEC register and shift the bits down
59907208c4SChristophe Leroy * to get the irq number
60907208c4SChristophe Leroy */
61ba3da734SChristophe Leroy vec = in_be32(&immr->im_siu_conf.sc_sivec);
62907208c4SChristophe Leroy irq = vec >> 26;
63907208c4SChristophe Leroy v_bit = 0x80000000UL >> irq;
64907208c4SChristophe Leroy
65907208c4SChristophe Leroy /*
66907208c4SChristophe Leroy * Read Interrupt Mask Register and Mask Interrupts
67907208c4SChristophe Leroy */
68ba3da734SChristophe Leroy simask = in_be32(&immr->im_siu_conf.sc_simask);
69ba3da734SChristophe Leroy clrbits_be32(&immr->im_siu_conf.sc_simask, 0xFFFF0000 >> irq);
70907208c4SChristophe Leroy
71907208c4SChristophe Leroy if (!(irq & 0x1)) { /* External Interrupt ? */
72907208c4SChristophe Leroy ulong siel;
73907208c4SChristophe Leroy
74907208c4SChristophe Leroy /*
75907208c4SChristophe Leroy * Read Interrupt Edge/Level Register
76907208c4SChristophe Leroy */
77ba3da734SChristophe Leroy siel = in_be32(&immr->im_siu_conf.sc_siel);
78907208c4SChristophe Leroy
79907208c4SChristophe Leroy if (siel & v_bit) { /* edge triggered interrupt ? */
80907208c4SChristophe Leroy /*
81907208c4SChristophe Leroy * Rewrite SIPEND Register to clear interrupt
82907208c4SChristophe Leroy */
83ba3da734SChristophe Leroy out_be32(&immr->im_siu_conf.sc_sipend, v_bit);
84907208c4SChristophe Leroy }
85907208c4SChristophe Leroy }
86907208c4SChristophe Leroy
87907208c4SChristophe Leroy if (irq_vecs[irq].handler != NULL) {
88907208c4SChristophe Leroy irq_vecs[irq].handler(irq_vecs[irq].arg);
89907208c4SChristophe Leroy } else {
90907208c4SChristophe Leroy printf("\nBogus External Interrupt IRQ %d Vector %ld\n",
91907208c4SChristophe Leroy irq, vec);
92907208c4SChristophe Leroy /* turn off the bogus interrupt to avoid it from now */
93907208c4SChristophe Leroy simask &= ~v_bit;
94907208c4SChristophe Leroy }
95907208c4SChristophe Leroy /*
96907208c4SChristophe Leroy * Re-Enable old Interrupt Mask
97907208c4SChristophe Leroy */
98ba3da734SChristophe Leroy out_be32(&immr->im_siu_conf.sc_simask, simask);
99907208c4SChristophe Leroy }
100907208c4SChristophe Leroy
101907208c4SChristophe Leroy /************************************************************************/
102907208c4SChristophe Leroy
103907208c4SChristophe Leroy /*
104907208c4SChristophe Leroy * CPM interrupt handler
105907208c4SChristophe Leroy */
cpm_interrupt(void * regs)106907208c4SChristophe Leroy static void cpm_interrupt(void *regs)
107907208c4SChristophe Leroy {
108ba3da734SChristophe Leroy immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
109907208c4SChristophe Leroy uint vec;
110907208c4SChristophe Leroy
111907208c4SChristophe Leroy /*
112907208c4SChristophe Leroy * Get the vector by setting the ACK bit
113907208c4SChristophe Leroy * and then reading the register.
114907208c4SChristophe Leroy */
115ba3da734SChristophe Leroy out_be16(&immr->im_cpic.cpic_civr, 1);
116ba3da734SChristophe Leroy vec = in_be16(&immr->im_cpic.cpic_civr);
117907208c4SChristophe Leroy vec >>= 11;
118907208c4SChristophe Leroy
119907208c4SChristophe Leroy if (cpm_vecs[vec].handler != NULL) {
120907208c4SChristophe Leroy (*cpm_vecs[vec].handler) (cpm_vecs[vec].arg);
121907208c4SChristophe Leroy } else {
122ba3da734SChristophe Leroy clrbits_be32(&immr->im_cpic.cpic_cimr, 1 << vec);
123907208c4SChristophe Leroy printf("Masking bogus CPM interrupt vector 0x%x\n", vec);
124907208c4SChristophe Leroy }
125907208c4SChristophe Leroy /*
126907208c4SChristophe Leroy * After servicing the interrupt,
127907208c4SChristophe Leroy * we have to remove the status indicator.
128907208c4SChristophe Leroy */
129ba3da734SChristophe Leroy setbits_be32(&immr->im_cpic.cpic_cisr, 1 << vec);
130907208c4SChristophe Leroy }
131907208c4SChristophe Leroy
132907208c4SChristophe Leroy /*
133907208c4SChristophe Leroy * The CPM can generate the error interrupt when there is a race
134907208c4SChristophe Leroy * condition between generating and masking interrupts. All we have
135907208c4SChristophe Leroy * to do is ACK it and return. This is a no-op function so we don't
136907208c4SChristophe Leroy * need any special tests in the interrupt handler.
137907208c4SChristophe Leroy */
cpm_error_interrupt(void * dummy)138907208c4SChristophe Leroy static void cpm_error_interrupt(void *dummy)
139907208c4SChristophe Leroy {
140907208c4SChristophe Leroy }
141907208c4SChristophe Leroy
142907208c4SChristophe Leroy /************************************************************************/
143907208c4SChristophe Leroy /*
144907208c4SChristophe Leroy * Install and free an interrupt handler
145907208c4SChristophe Leroy */
irq_install_handler(int vec,interrupt_handler_t * handler,void * arg)14670fd0710SChristophe Leroy void irq_install_handler(int vec, interrupt_handler_t *handler, void *arg)
147907208c4SChristophe Leroy {
148ba3da734SChristophe Leroy immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
149907208c4SChristophe Leroy
150907208c4SChristophe Leroy if ((vec & CPMVEC_OFFSET) != 0) {
151907208c4SChristophe Leroy /* CPM interrupt */
152907208c4SChristophe Leroy vec &= 0xffff;
15370fd0710SChristophe Leroy if (cpm_vecs[vec].handler != NULL)
154907208c4SChristophe Leroy printf("CPM interrupt 0x%x replacing 0x%x\n",
15570fd0710SChristophe Leroy (uint)handler, (uint)cpm_vecs[vec].handler);
156907208c4SChristophe Leroy cpm_vecs[vec].handler = handler;
157907208c4SChristophe Leroy cpm_vecs[vec].arg = arg;
158ba3da734SChristophe Leroy setbits_be32(&immr->im_cpic.cpic_cimr, 1 << vec);
159907208c4SChristophe Leroy } else {
160907208c4SChristophe Leroy /* SIU interrupt */
16170fd0710SChristophe Leroy if (irq_vecs[vec].handler != NULL)
162907208c4SChristophe Leroy printf("SIU interrupt %d 0x%x replacing 0x%x\n",
16370fd0710SChristophe Leroy vec, (uint)handler, (uint)cpm_vecs[vec].handler);
164907208c4SChristophe Leroy irq_vecs[vec].handler = handler;
165907208c4SChristophe Leroy irq_vecs[vec].arg = arg;
166ba3da734SChristophe Leroy setbits_be32(&immr->im_siu_conf.sc_simask, 1 << (31 - vec));
167907208c4SChristophe Leroy }
168907208c4SChristophe Leroy }
169907208c4SChristophe Leroy
irq_free_handler(int vec)170907208c4SChristophe Leroy void irq_free_handler(int vec)
171907208c4SChristophe Leroy {
172ba3da734SChristophe Leroy immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
173907208c4SChristophe Leroy
174907208c4SChristophe Leroy if ((vec & CPMVEC_OFFSET) != 0) {
175907208c4SChristophe Leroy /* CPM interrupt */
176907208c4SChristophe Leroy vec &= 0xffff;
177ba3da734SChristophe Leroy clrbits_be32(&immr->im_cpic.cpic_cimr, 1 << vec);
178907208c4SChristophe Leroy cpm_vecs[vec].handler = NULL;
179907208c4SChristophe Leroy cpm_vecs[vec].arg = NULL;
180907208c4SChristophe Leroy } else {
181907208c4SChristophe Leroy /* SIU interrupt */
182ba3da734SChristophe Leroy clrbits_be32(&immr->im_siu_conf.sc_simask, 1 << (31 - vec));
183907208c4SChristophe Leroy irq_vecs[vec].handler = NULL;
184907208c4SChristophe Leroy irq_vecs[vec].arg = NULL;
185907208c4SChristophe Leroy }
186907208c4SChristophe Leroy }
187907208c4SChristophe Leroy
188907208c4SChristophe Leroy /************************************************************************/
189907208c4SChristophe Leroy
cpm_interrupt_init(void)190907208c4SChristophe Leroy static void cpm_interrupt_init(void)
191907208c4SChristophe Leroy {
192ba3da734SChristophe Leroy immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
193ba3da734SChristophe Leroy uint cicr;
194907208c4SChristophe Leroy
195907208c4SChristophe Leroy /*
196907208c4SChristophe Leroy * Initialize the CPM interrupt controller.
197907208c4SChristophe Leroy */
198907208c4SChristophe Leroy
199ba3da734SChristophe Leroy cicr = CICR_SCD_SCC4 | CICR_SCC_SCC3 | CICR_SCB_SCC2 | CICR_SCA_SCC1 |
200ba3da734SChristophe Leroy ((CPM_INTERRUPT / 2) << 13) | CICR_HP_MASK;
201907208c4SChristophe Leroy
202ba3da734SChristophe Leroy out_be32(&immr->im_cpic.cpic_cicr, cicr);
203ba3da734SChristophe Leroy out_be32(&immr->im_cpic.cpic_cimr, 0);
204907208c4SChristophe Leroy
205907208c4SChristophe Leroy /*
206907208c4SChristophe Leroy * Install the error handler.
207907208c4SChristophe Leroy */
208907208c4SChristophe Leroy irq_install_handler(CPMVEC_ERROR, cpm_error_interrupt, NULL);
209907208c4SChristophe Leroy
210ba3da734SChristophe Leroy setbits_be32(&immr->im_cpic.cpic_cicr, CICR_IEN);
211907208c4SChristophe Leroy
212907208c4SChristophe Leroy /*
213907208c4SChristophe Leroy * Install the cpm interrupt handler
214907208c4SChristophe Leroy */
215907208c4SChristophe Leroy irq_install_handler(CPM_INTERRUPT, cpm_interrupt, NULL);
216907208c4SChristophe Leroy }
217907208c4SChristophe Leroy
218907208c4SChristophe Leroy /************************************************************************/
219907208c4SChristophe Leroy
220907208c4SChristophe Leroy /*
221907208c4SChristophe Leroy * timer_interrupt - gets called when the decrementer overflows,
222907208c4SChristophe Leroy * with interrupts disabled.
223907208c4SChristophe Leroy * Trivial implementation - no need to be really accurate.
224907208c4SChristophe Leroy */
timer_interrupt_cpu(struct pt_regs * regs)225907208c4SChristophe Leroy void timer_interrupt_cpu(struct pt_regs *regs)
226907208c4SChristophe Leroy {
227ba3da734SChristophe Leroy immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
228907208c4SChristophe Leroy
229907208c4SChristophe Leroy /* Reset Timer Expired and Timers Interrupt Status */
230ba3da734SChristophe Leroy out_be32(&immr->im_clkrstk.cark_plprcrk, KAPWR_KEY);
231907208c4SChristophe Leroy __asm__ ("nop");
232907208c4SChristophe Leroy /*
233907208c4SChristophe Leroy Clear TEXPS (and TMIST on older chips). SPLSS (on older
234907208c4SChristophe Leroy chips) is cleared too.
235907208c4SChristophe Leroy
236907208c4SChristophe Leroy Bitwise OR is a read-modify-write operation so ALL bits
237907208c4SChristophe Leroy which are cleared by writing `1' would be cleared by
238907208c4SChristophe Leroy operations like
239907208c4SChristophe Leroy
240907208c4SChristophe Leroy immr->im_clkrst.car_plprcr |= PLPRCR_TEXPS;
241907208c4SChristophe Leroy
242907208c4SChristophe Leroy The same can be achieved by simple writing of the PLPRCR
243907208c4SChristophe Leroy to itself. If a bit value should be preserved, read the
244907208c4SChristophe Leroy register, ZERO the bit and write, not OR, the result back.
245907208c4SChristophe Leroy */
246ba3da734SChristophe Leroy setbits_be32(&immr->im_clkrst.car_plprcr, 0);
247907208c4SChristophe Leroy }
248907208c4SChristophe Leroy
249907208c4SChristophe Leroy /************************************************************************/
250