xref: /linux/arch/powerpc/platforms/8xx/m8xx_setup.c (revision 476eb4912601a8c01e6702b9a029f476b4b131d2)
1df34403dSVitaly Bordug /*
2df34403dSVitaly Bordug  *  Copyright (C) 1995  Linus Torvalds
3df34403dSVitaly Bordug  *  Adapted from 'alpha' version by Gary Thomas
4df34403dSVitaly Bordug  *  Modified by Cort Dougan (cort@cs.nmt.edu)
5df34403dSVitaly Bordug  *  Modified for MBX using prep/chrp/pmac functions by Dan (dmalek@jlc.net)
6df34403dSVitaly Bordug  *  Further modified for generic 8xx by Dan.
7df34403dSVitaly Bordug  */
8df34403dSVitaly Bordug 
9df34403dSVitaly Bordug /*
10df34403dSVitaly Bordug  * bootup setup stuff..
11df34403dSVitaly Bordug  */
12df34403dSVitaly Bordug 
13df34403dSVitaly Bordug #include <linux/kernel.h>
14df34403dSVitaly Bordug #include <linux/interrupt.h>
15df34403dSVitaly Bordug #include <linux/init.h>
16df34403dSVitaly Bordug #include <linux/time.h>
17df34403dSVitaly Bordug #include <linux/rtc.h>
1802753cb6SJochen Friedrich #include <linux/fsl_devices.h>
19df34403dSVitaly Bordug 
20df34403dSVitaly Bordug #include <asm/io.h>
21df34403dSVitaly Bordug #include <asm/mpc8xx.h>
22df34403dSVitaly Bordug #include <asm/8xx_immap.h>
23df34403dSVitaly Bordug #include <asm/prom.h>
24df34403dSVitaly Bordug #include <asm/fs_pd.h>
25df34403dSVitaly Bordug #include <mm/mmu_decl.h>
26df34403dSVitaly Bordug 
27fb533d0cSScott Wood #include <sysdev/mpc8xx_pic.h>
2849b51545SJochen Friedrich 
2949b51545SJochen Friedrich #include "mpc8xx.h"
30df34403dSVitaly Bordug 
3180128ff7SVitaly Bordug struct mpc8xx_pcmcia_ops m8xx_pcmcia_ops;
3280128ff7SVitaly Bordug 
33df34403dSVitaly Bordug extern int cpm_pic_init(void);
34df34403dSVitaly Bordug extern int cpm_get_irq(void);
35df34403dSVitaly Bordug 
36df34403dSVitaly Bordug /* A place holder for time base interrupts, if they are ever enabled. */
37fb533d0cSScott Wood static irqreturn_t timebase_interrupt(int irq, void *dev)
38df34403dSVitaly Bordug {
39df34403dSVitaly Bordug 	printk ("timebase_interrupt()\n");
40df34403dSVitaly Bordug 
41df34403dSVitaly Bordug 	return IRQ_HANDLED;
42df34403dSVitaly Bordug }
43df34403dSVitaly Bordug 
44df34403dSVitaly Bordug static struct irqaction tbint_irqaction = {
45df34403dSVitaly Bordug 	.handler = timebase_interrupt,
46df34403dSVitaly Bordug 	.name = "tbint",
47df34403dSVitaly Bordug };
48df34403dSVitaly Bordug 
49df34403dSVitaly Bordug /* per-board overridable init_internal_rtc() function. */
50df34403dSVitaly Bordug void __init __attribute__ ((weak))
51df34403dSVitaly Bordug init_internal_rtc(void)
52df34403dSVitaly Bordug {
53fb533d0cSScott Wood 	sit8xx_t __iomem *sys_tmr = immr_map(im_sit);
54df34403dSVitaly Bordug 
55df34403dSVitaly Bordug 	/* Disable the RTC one second and alarm interrupts. */
56df34403dSVitaly Bordug 	clrbits16(&sys_tmr->sit_rtcsc, (RTCSC_SIE | RTCSC_ALE));
57df34403dSVitaly Bordug 
58df34403dSVitaly Bordug 	/* Enable the RTC */
59df34403dSVitaly Bordug 	setbits16(&sys_tmr->sit_rtcsc, (RTCSC_RTF | RTCSC_RTE));
60df34403dSVitaly Bordug 	immr_unmap(sys_tmr);
61df34403dSVitaly Bordug }
62df34403dSVitaly Bordug 
63df34403dSVitaly Bordug static int __init get_freq(char *name, unsigned long *val)
64df34403dSVitaly Bordug {
65df34403dSVitaly Bordug 	struct device_node *cpu;
66e2eb6392SStephen Rothwell 	const unsigned int *fp;
67df34403dSVitaly Bordug 	int found = 0;
68df34403dSVitaly Bordug 
69df34403dSVitaly Bordug 	/* The cpu node should have timebase and clock frequency properties */
70df34403dSVitaly Bordug 	cpu = of_find_node_by_type(NULL, "cpu");
71df34403dSVitaly Bordug 
72df34403dSVitaly Bordug 	if (cpu) {
73e2eb6392SStephen Rothwell 		fp = of_get_property(cpu, name, NULL);
74df34403dSVitaly Bordug 		if (fp) {
75df34403dSVitaly Bordug 			found = 1;
76e2eb6392SStephen Rothwell 			*val = *fp;
77df34403dSVitaly Bordug 		}
78df34403dSVitaly Bordug 
79df34403dSVitaly Bordug 		of_node_put(cpu);
80df34403dSVitaly Bordug 	}
81df34403dSVitaly Bordug 
82df34403dSVitaly Bordug 	return found;
83df34403dSVitaly Bordug }
84df34403dSVitaly Bordug 
85df34403dSVitaly Bordug /* The decrementer counts at the system (internal) clock frequency divided by
86df34403dSVitaly Bordug  * sixteen, or external oscillator divided by four.  We force the processor
87df34403dSVitaly Bordug  * to use system clock divided by sixteen.
88df34403dSVitaly Bordug  */
89df34403dSVitaly Bordug void __init mpc8xx_calibrate_decr(void)
90df34403dSVitaly Bordug {
91df34403dSVitaly Bordug 	struct device_node *cpu;
92fb533d0cSScott Wood 	cark8xx_t __iomem *clk_r1;
93fb533d0cSScott Wood 	car8xx_t __iomem *clk_r2;
94fb533d0cSScott Wood 	sitk8xx_t __iomem *sys_tmr1;
95fb533d0cSScott Wood 	sit8xx_t __iomem *sys_tmr2;
96df34403dSVitaly Bordug 	int irq, virq;
97df34403dSVitaly Bordug 
98fb533d0cSScott Wood 	clk_r1 = immr_map(im_clkrstk);
99df34403dSVitaly Bordug 
100df34403dSVitaly Bordug 	/* Unlock the SCCR. */
101df34403dSVitaly Bordug 	out_be32(&clk_r1->cark_sccrk, ~KAPWR_KEY);
102df34403dSVitaly Bordug 	out_be32(&clk_r1->cark_sccrk, KAPWR_KEY);
103df34403dSVitaly Bordug 	immr_unmap(clk_r1);
104df34403dSVitaly Bordug 
105df34403dSVitaly Bordug 	/* Force all 8xx processors to use divide by 16 processor clock. */
106fb533d0cSScott Wood 	clk_r2 = immr_map(im_clkrst);
107df34403dSVitaly Bordug 	setbits32(&clk_r2->car_sccr, 0x02000000);
108df34403dSVitaly Bordug 	immr_unmap(clk_r2);
109df34403dSVitaly Bordug 
110df34403dSVitaly Bordug 	/* Processor frequency is MHz.
111df34403dSVitaly Bordug 	 */
112df34403dSVitaly Bordug 	ppc_proc_freq = 50000000;
113df34403dSVitaly Bordug 	if (!get_freq("clock-frequency", &ppc_proc_freq))
114df34403dSVitaly Bordug 		printk(KERN_ERR "WARNING: Estimating processor frequency "
115df34403dSVitaly Bordug 		                "(not found)\n");
116df34403dSVitaly Bordug 
11750530378SAnton Vorontsov 	ppc_tb_freq = ppc_proc_freq / 16;
118df34403dSVitaly Bordug 	printk("Decrementer Frequency = 0x%lx\n", ppc_tb_freq);
119df34403dSVitaly Bordug 
120df34403dSVitaly Bordug 	/* Perform some more timer/timebase initialization.  This used
121df34403dSVitaly Bordug 	 * to be done elsewhere, but other changes caused it to get
122df34403dSVitaly Bordug 	 * called more than once....that is a bad thing.
123df34403dSVitaly Bordug 	 *
124df34403dSVitaly Bordug 	 * First, unlock all of the registers we are going to modify.
125df34403dSVitaly Bordug 	 * To protect them from corruption during power down, registers
126df34403dSVitaly Bordug 	 * that are maintained by keep alive power are "locked".  To
127df34403dSVitaly Bordug 	 * modify these registers we have to write the key value to
128df34403dSVitaly Bordug 	 * the key location associated with the register.
129df34403dSVitaly Bordug 	 * Some boards power up with these unlocked, while others
130df34403dSVitaly Bordug 	 * are locked.  Writing anything (including the unlock code?)
131df34403dSVitaly Bordug 	 * to the unlocked registers will lock them again.  So, here
132df34403dSVitaly Bordug 	 * we guarantee the registers are locked, then we unlock them
133df34403dSVitaly Bordug 	 * for our use.
134df34403dSVitaly Bordug 	 */
135fb533d0cSScott Wood 	sys_tmr1 = immr_map(im_sitk);
136df34403dSVitaly Bordug 	out_be32(&sys_tmr1->sitk_tbscrk, ~KAPWR_KEY);
137df34403dSVitaly Bordug 	out_be32(&sys_tmr1->sitk_rtcsck, ~KAPWR_KEY);
138df34403dSVitaly Bordug 	out_be32(&sys_tmr1->sitk_tbk, ~KAPWR_KEY);
139df34403dSVitaly Bordug 	out_be32(&sys_tmr1->sitk_tbscrk, KAPWR_KEY);
140df34403dSVitaly Bordug 	out_be32(&sys_tmr1->sitk_rtcsck, KAPWR_KEY);
141df34403dSVitaly Bordug 	out_be32(&sys_tmr1->sitk_tbk, KAPWR_KEY);
142df34403dSVitaly Bordug 	immr_unmap(sys_tmr1);
143df34403dSVitaly Bordug 
144df34403dSVitaly Bordug 	init_internal_rtc();
145df34403dSVitaly Bordug 
146df34403dSVitaly Bordug 	/* Enabling the decrementer also enables the timebase interrupts
147df34403dSVitaly Bordug 	 * (or from the other point of view, to get decrementer interrupts
148df34403dSVitaly Bordug 	 * we have to enable the timebase).  The decrementer interrupt
149df34403dSVitaly Bordug 	 * is wired into the vector table, nothing to do here for that.
150df34403dSVitaly Bordug 	 */
151df34403dSVitaly Bordug 	cpu = of_find_node_by_type(NULL, "cpu");
152df34403dSVitaly Bordug 	virq= irq_of_parse_and_map(cpu, 0);
153*476eb491SGrant Likely 	irq = virq_to_hw(virq);
154df34403dSVitaly Bordug 
155fb533d0cSScott Wood 	sys_tmr2 = immr_map(im_sit);
156df34403dSVitaly Bordug 	out_be16(&sys_tmr2->sit_tbscr, ((1 << (7 - (irq/2))) << 8) |
157df34403dSVitaly Bordug 					(TBSCR_TBF | TBSCR_TBE));
158df34403dSVitaly Bordug 	immr_unmap(sys_tmr2);
159df34403dSVitaly Bordug 
160df34403dSVitaly Bordug 	if (setup_irq(virq, &tbint_irqaction))
161df34403dSVitaly Bordug 		panic("Could not allocate timer IRQ!");
162df34403dSVitaly Bordug }
163df34403dSVitaly Bordug 
164df34403dSVitaly Bordug /* The RTC on the MPC8xx is an internal register.
165df34403dSVitaly Bordug  * We want to protect this during power down, so we need to unlock,
166df34403dSVitaly Bordug  * modify, and re-lock.
167df34403dSVitaly Bordug  */
168df34403dSVitaly Bordug 
169df34403dSVitaly Bordug int mpc8xx_set_rtc_time(struct rtc_time *tm)
170df34403dSVitaly Bordug {
171fb533d0cSScott Wood 	sitk8xx_t __iomem *sys_tmr1;
172fb533d0cSScott Wood 	sit8xx_t __iomem *sys_tmr2;
173df34403dSVitaly Bordug 	int time;
174df34403dSVitaly Bordug 
175fb533d0cSScott Wood 	sys_tmr1 = immr_map(im_sitk);
176fb533d0cSScott Wood 	sys_tmr2 = immr_map(im_sit);
177df34403dSVitaly Bordug 	time = mktime(tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
178df34403dSVitaly Bordug 	              tm->tm_hour, tm->tm_min, tm->tm_sec);
179df34403dSVitaly Bordug 
180df34403dSVitaly Bordug 	out_be32(&sys_tmr1->sitk_rtck, KAPWR_KEY);
181df34403dSVitaly Bordug 	out_be32(&sys_tmr2->sit_rtc, time);
182df34403dSVitaly Bordug 	out_be32(&sys_tmr1->sitk_rtck, ~KAPWR_KEY);
183df34403dSVitaly Bordug 
184df34403dSVitaly Bordug 	immr_unmap(sys_tmr2);
185df34403dSVitaly Bordug 	immr_unmap(sys_tmr1);
186df34403dSVitaly Bordug 	return 0;
187df34403dSVitaly Bordug }
188df34403dSVitaly Bordug 
189df34403dSVitaly Bordug void mpc8xx_get_rtc_time(struct rtc_time *tm)
190df34403dSVitaly Bordug {
191df34403dSVitaly Bordug 	unsigned long data;
192fb533d0cSScott Wood 	sit8xx_t __iomem *sys_tmr = immr_map(im_sit);
193df34403dSVitaly Bordug 
194df34403dSVitaly Bordug 	/* Get time from the RTC. */
195df34403dSVitaly Bordug 	data = in_be32(&sys_tmr->sit_rtc);
196df34403dSVitaly Bordug 	to_tm(data, tm);
197df34403dSVitaly Bordug 	tm->tm_year -= 1900;
198df34403dSVitaly Bordug 	tm->tm_mon -= 1;
199df34403dSVitaly Bordug 	immr_unmap(sys_tmr);
200df34403dSVitaly Bordug 	return;
201df34403dSVitaly Bordug }
202df34403dSVitaly Bordug 
203df34403dSVitaly Bordug void mpc8xx_restart(char *cmd)
204df34403dSVitaly Bordug {
205fb533d0cSScott Wood 	car8xx_t __iomem *clk_r = immr_map(im_clkrst);
206df34403dSVitaly Bordug 
207df34403dSVitaly Bordug 
208df34403dSVitaly Bordug 	local_irq_disable();
209df34403dSVitaly Bordug 
210df34403dSVitaly Bordug 	setbits32(&clk_r->car_plprcr, 0x00000080);
211df34403dSVitaly Bordug 	/* Clear the ME bit in MSR to cause checkstop on machine check
212df34403dSVitaly Bordug 	*/
213df34403dSVitaly Bordug 	mtmsr(mfmsr() & ~0x1000);
214df34403dSVitaly Bordug 
215fb533d0cSScott Wood 	in_8(&clk_r->res[0]);
216fb533d0cSScott Wood 	panic("Restart failed\n");
217df34403dSVitaly Bordug }
218df34403dSVitaly Bordug 
219df34403dSVitaly Bordug static void cpm_cascade(unsigned int irq, struct irq_desc *desc)
220df34403dSVitaly Bordug {
221cfe4a109SLennert Buytenhek 	struct irq_chip *chip;
222df34403dSVitaly Bordug 	int cascade_irq;
223df34403dSVitaly Bordug 
224df34403dSVitaly Bordug 	if ((cascade_irq = cpm_get_irq()) >= 0) {
225588e0508SBenjamin Herrenschmidt 		struct irq_desc *cdesc = irq_to_desc(cascade_irq);
226df34403dSVitaly Bordug 
227df34403dSVitaly Bordug 		generic_handle_irq(cascade_irq);
228cfe4a109SLennert Buytenhek 
229ec775d0eSThomas Gleixner 		chip = irq_desc_get_chip(cdesc);
230cfe4a109SLennert Buytenhek 		chip->irq_eoi(&cdesc->irq_data);
231df34403dSVitaly Bordug 	}
232cfe4a109SLennert Buytenhek 
233ec775d0eSThomas Gleixner 	chip = irq_desc_get_chip(desc);
234cfe4a109SLennert Buytenhek 	chip->irq_eoi(&desc->irq_data);
235df34403dSVitaly Bordug }
236df34403dSVitaly Bordug 
237d0a02a06SJochen Friedrich /* Initialize the internal interrupt controllers.  The number of
238df34403dSVitaly Bordug  * interrupts supported can vary with the processor type, and the
239df34403dSVitaly Bordug  * 82xx family can have up to 64.
240df34403dSVitaly Bordug  * External interrupts can be either edge or level triggered, and
241df34403dSVitaly Bordug  * need to be initialized by the appropriate driver.
242df34403dSVitaly Bordug  */
243d0a02a06SJochen Friedrich void __init mpc8xx_pics_init(void)
244df34403dSVitaly Bordug {
245df34403dSVitaly Bordug 	int irq;
246df34403dSVitaly Bordug 
247df34403dSVitaly Bordug 	if (mpc8xx_pic_init()) {
248df34403dSVitaly Bordug 		printk(KERN_ERR "Failed interrupt 8xx controller  initialization\n");
249df34403dSVitaly Bordug 		return;
250df34403dSVitaly Bordug 	}
251df34403dSVitaly Bordug 
252df34403dSVitaly Bordug 	irq = cpm_pic_init();
253df34403dSVitaly Bordug 	if (irq != NO_IRQ)
254ec775d0eSThomas Gleixner 		irq_set_chained_handler(irq, cpm_cascade);
255df34403dSVitaly Bordug }
256