xref: /linux/arch/powerpc/platforms/8xx/m8xx_setup.c (revision 80128ff79d282cf71b1819dbca9b8dd47d8ed3e8)
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/errno.h>
14df34403dSVitaly Bordug #include <linux/sched.h>
15df34403dSVitaly Bordug #include <linux/kernel.h>
16df34403dSVitaly Bordug #include <linux/mm.h>
17df34403dSVitaly Bordug #include <linux/stddef.h>
18df34403dSVitaly Bordug #include <linux/unistd.h>
19df34403dSVitaly Bordug #include <linux/ptrace.h>
20df34403dSVitaly Bordug #include <linux/slab.h>
21df34403dSVitaly Bordug #include <linux/user.h>
22df34403dSVitaly Bordug #include <linux/a.out.h>
23df34403dSVitaly Bordug #include <linux/tty.h>
24df34403dSVitaly Bordug #include <linux/major.h>
25df34403dSVitaly Bordug #include <linux/interrupt.h>
26df34403dSVitaly Bordug #include <linux/reboot.h>
27df34403dSVitaly Bordug #include <linux/init.h>
28df34403dSVitaly Bordug #include <linux/initrd.h>
29df34403dSVitaly Bordug #include <linux/ioport.h>
30df34403dSVitaly Bordug #include <linux/bootmem.h>
31df34403dSVitaly Bordug #include <linux/seq_file.h>
32df34403dSVitaly Bordug #include <linux/root_dev.h>
33df34403dSVitaly Bordug #include <linux/time.h>
34df34403dSVitaly Bordug #include <linux/rtc.h>
35*80128ff7SVitaly Bordug #include <linux/fsl_devices.h>
36df34403dSVitaly Bordug 
37df34403dSVitaly Bordug #include <asm/mmu.h>
38df34403dSVitaly Bordug #include <asm/reg.h>
39df34403dSVitaly Bordug #include <asm/residual.h>
40df34403dSVitaly Bordug #include <asm/io.h>
41df34403dSVitaly Bordug #include <asm/pgtable.h>
42df34403dSVitaly Bordug #include <asm/mpc8xx.h>
43df34403dSVitaly Bordug #include <asm/8xx_immap.h>
44df34403dSVitaly Bordug #include <asm/machdep.h>
45df34403dSVitaly Bordug #include <asm/bootinfo.h>
46df34403dSVitaly Bordug #include <asm/time.h>
47df34403dSVitaly Bordug #include <asm/prom.h>
48df34403dSVitaly Bordug #include <asm/fs_pd.h>
49df34403dSVitaly Bordug #include <mm/mmu_decl.h>
50df34403dSVitaly Bordug 
51df34403dSVitaly Bordug #include "sysdev/mpc8xx_pic.h"
52df34403dSVitaly Bordug 
53*80128ff7SVitaly Bordug #ifdef CONFIG_PCMCIA_M8XX
54*80128ff7SVitaly Bordug struct mpc8xx_pcmcia_ops m8xx_pcmcia_ops;
55*80128ff7SVitaly Bordug #endif
56*80128ff7SVitaly Bordug 
57df34403dSVitaly Bordug void m8xx_calibrate_decr(void);
58df34403dSVitaly Bordug extern void m8xx_wdt_handler_install(bd_t *bp);
59df34403dSVitaly Bordug extern int cpm_pic_init(void);
60df34403dSVitaly Bordug extern int cpm_get_irq(void);
61df34403dSVitaly Bordug 
62df34403dSVitaly Bordug /* A place holder for time base interrupts, if they are ever enabled. */
63df34403dSVitaly Bordug irqreturn_t timebase_interrupt(int irq, void * dev)
64df34403dSVitaly Bordug {
65df34403dSVitaly Bordug 	printk ("timebase_interrupt()\n");
66df34403dSVitaly Bordug 
67df34403dSVitaly Bordug 	return IRQ_HANDLED;
68df34403dSVitaly Bordug }
69df34403dSVitaly Bordug 
70df34403dSVitaly Bordug static struct irqaction tbint_irqaction = {
71df34403dSVitaly Bordug 	.handler = timebase_interrupt,
72df34403dSVitaly Bordug 	.mask = CPU_MASK_NONE,
73df34403dSVitaly Bordug 	.name = "tbint",
74df34403dSVitaly Bordug };
75df34403dSVitaly Bordug 
76df34403dSVitaly Bordug /* per-board overridable init_internal_rtc() function. */
77df34403dSVitaly Bordug void __init __attribute__ ((weak))
78df34403dSVitaly Bordug init_internal_rtc(void)
79df34403dSVitaly Bordug {
80df34403dSVitaly Bordug 	sit8xx_t *sys_tmr = (sit8xx_t *) immr_map(im_sit);
81df34403dSVitaly Bordug 
82df34403dSVitaly Bordug 	/* Disable the RTC one second and alarm interrupts. */
83df34403dSVitaly Bordug 	clrbits16(&sys_tmr->sit_rtcsc, (RTCSC_SIE | RTCSC_ALE));
84df34403dSVitaly Bordug 
85df34403dSVitaly Bordug 	/* Enable the RTC */
86df34403dSVitaly Bordug 	setbits16(&sys_tmr->sit_rtcsc, (RTCSC_RTF | RTCSC_RTE));
87df34403dSVitaly Bordug 	immr_unmap(sys_tmr);
88df34403dSVitaly Bordug }
89df34403dSVitaly Bordug 
90df34403dSVitaly Bordug static int __init get_freq(char *name, unsigned long *val)
91df34403dSVitaly Bordug {
92df34403dSVitaly Bordug         struct device_node *cpu;
93e2eb6392SStephen Rothwell         const unsigned int *fp;
94df34403dSVitaly Bordug         int found = 0;
95df34403dSVitaly Bordug 
96df34403dSVitaly Bordug         /* The cpu node should have timebase and clock frequency properties */
97df34403dSVitaly Bordug         cpu = of_find_node_by_type(NULL, "cpu");
98df34403dSVitaly Bordug 
99df34403dSVitaly Bordug         if (cpu) {
100e2eb6392SStephen Rothwell                 fp = of_get_property(cpu, name, NULL);
101df34403dSVitaly Bordug                 if (fp) {
102df34403dSVitaly Bordug                         found = 1;
103e2eb6392SStephen Rothwell                         *val = *fp;
104df34403dSVitaly Bordug                 }
105df34403dSVitaly Bordug 
106df34403dSVitaly Bordug                 of_node_put(cpu);
107df34403dSVitaly Bordug         }
108df34403dSVitaly Bordug 
109df34403dSVitaly Bordug         return found;
110df34403dSVitaly Bordug }
111df34403dSVitaly Bordug 
112df34403dSVitaly Bordug /* The decrementer counts at the system (internal) clock frequency divided by
113df34403dSVitaly Bordug  * sixteen, or external oscillator divided by four.  We force the processor
114df34403dSVitaly Bordug  * to use system clock divided by sixteen.
115df34403dSVitaly Bordug  */
116df34403dSVitaly Bordug void __init mpc8xx_calibrate_decr(void)
117df34403dSVitaly Bordug {
118df34403dSVitaly Bordug 	struct device_node *cpu;
119df34403dSVitaly Bordug 	cark8xx_t *clk_r1;
120df34403dSVitaly Bordug 	car8xx_t *clk_r2;
121df34403dSVitaly Bordug 	sitk8xx_t *sys_tmr1;
122df34403dSVitaly Bordug 	sit8xx_t *sys_tmr2;
123df34403dSVitaly Bordug 	int irq, virq;
124df34403dSVitaly Bordug 
125df34403dSVitaly Bordug         clk_r1 = (cark8xx_t *) immr_map(im_clkrstk);
126df34403dSVitaly Bordug 
127df34403dSVitaly Bordug 	/* Unlock the SCCR. */
128df34403dSVitaly Bordug 	out_be32(&clk_r1->cark_sccrk, ~KAPWR_KEY);
129df34403dSVitaly Bordug 	out_be32(&clk_r1->cark_sccrk, KAPWR_KEY);
130df34403dSVitaly Bordug 	immr_unmap(clk_r1);
131df34403dSVitaly Bordug 
132df34403dSVitaly Bordug 	/* Force all 8xx processors to use divide by 16 processor clock. */
133df34403dSVitaly Bordug         clk_r2 = (car8xx_t *) immr_map(im_clkrst);
134df34403dSVitaly Bordug 	setbits32(&clk_r2->car_sccr, 0x02000000);
135df34403dSVitaly Bordug 	immr_unmap(clk_r2);
136df34403dSVitaly Bordug 
137df34403dSVitaly Bordug 	/* Processor frequency is MHz.
138df34403dSVitaly Bordug 	 */
139df34403dSVitaly Bordug         ppc_tb_freq = 50000000;
140df34403dSVitaly Bordug         if (!get_freq("bus-frequency", &ppc_tb_freq)) {
141df34403dSVitaly Bordug                 printk(KERN_ERR "WARNING: Estimating decrementer frequency "
142df34403dSVitaly Bordug                                 "(not found)\n");
143df34403dSVitaly Bordug         }
144df34403dSVitaly Bordug         ppc_tb_freq /= 16;
145df34403dSVitaly Bordug         ppc_proc_freq = 50000000;
146df34403dSVitaly Bordug         if (!get_freq("clock-frequency", &ppc_proc_freq))
147df34403dSVitaly Bordug                 printk(KERN_ERR "WARNING: Estimating processor frequency"
148df34403dSVitaly Bordug                                 "(not found)\n");
149df34403dSVitaly Bordug 
150df34403dSVitaly Bordug         printk("Decrementer Frequency = 0x%lx\n", ppc_tb_freq);
151df34403dSVitaly Bordug 
152df34403dSVitaly Bordug 	/* Perform some more timer/timebase initialization.  This used
153df34403dSVitaly Bordug 	 * to be done elsewhere, but other changes caused it to get
154df34403dSVitaly Bordug 	 * called more than once....that is a bad thing.
155df34403dSVitaly Bordug 	 *
156df34403dSVitaly Bordug 	 * First, unlock all of the registers we are going to modify.
157df34403dSVitaly Bordug 	 * To protect them from corruption during power down, registers
158df34403dSVitaly Bordug 	 * that are maintained by keep alive power are "locked".  To
159df34403dSVitaly Bordug 	 * modify these registers we have to write the key value to
160df34403dSVitaly Bordug 	 * the key location associated with the register.
161df34403dSVitaly Bordug 	 * Some boards power up with these unlocked, while others
162df34403dSVitaly Bordug 	 * are locked.  Writing anything (including the unlock code?)
163df34403dSVitaly Bordug 	 * to the unlocked registers will lock them again.  So, here
164df34403dSVitaly Bordug 	 * we guarantee the registers are locked, then we unlock them
165df34403dSVitaly Bordug 	 * for our use.
166df34403dSVitaly Bordug 	 */
167df34403dSVitaly Bordug         sys_tmr1 = (sitk8xx_t *) immr_map(im_sitk);
168df34403dSVitaly Bordug 	out_be32(&sys_tmr1->sitk_tbscrk, ~KAPWR_KEY);
169df34403dSVitaly Bordug 	out_be32(&sys_tmr1->sitk_rtcsck, ~KAPWR_KEY);
170df34403dSVitaly Bordug 	out_be32(&sys_tmr1->sitk_tbk, ~KAPWR_KEY);
171df34403dSVitaly Bordug 	out_be32(&sys_tmr1->sitk_tbscrk, KAPWR_KEY);
172df34403dSVitaly Bordug 	out_be32(&sys_tmr1->sitk_rtcsck, KAPWR_KEY);
173df34403dSVitaly Bordug 	out_be32(&sys_tmr1->sitk_tbk, KAPWR_KEY);
174df34403dSVitaly Bordug 	immr_unmap(sys_tmr1);
175df34403dSVitaly Bordug 
176df34403dSVitaly Bordug 	init_internal_rtc();
177df34403dSVitaly Bordug 
178df34403dSVitaly Bordug 	/* Enabling the decrementer also enables the timebase interrupts
179df34403dSVitaly Bordug 	 * (or from the other point of view, to get decrementer interrupts
180df34403dSVitaly Bordug 	 * we have to enable the timebase).  The decrementer interrupt
181df34403dSVitaly Bordug 	 * is wired into the vector table, nothing to do here for that.
182df34403dSVitaly Bordug 	 */
183df34403dSVitaly Bordug         cpu = of_find_node_by_type(NULL, "cpu");
184df34403dSVitaly Bordug         virq= irq_of_parse_and_map(cpu, 0);
185df34403dSVitaly Bordug 	irq = irq_map[virq].hwirq;
186df34403dSVitaly Bordug 
187df34403dSVitaly Bordug 	sys_tmr2 = (sit8xx_t *) immr_map(im_sit);
188df34403dSVitaly Bordug 	out_be16(&sys_tmr2->sit_tbscr, ((1 << (7 - (irq/2))) << 8) |
189df34403dSVitaly Bordug 					(TBSCR_TBF | TBSCR_TBE));
190df34403dSVitaly Bordug 	immr_unmap(sys_tmr2);
191df34403dSVitaly Bordug 
192df34403dSVitaly Bordug 	if (setup_irq(virq, &tbint_irqaction))
193df34403dSVitaly Bordug 		panic("Could not allocate timer IRQ!");
194df34403dSVitaly Bordug 
195df34403dSVitaly Bordug #ifdef CONFIG_8xx_WDT
196df34403dSVitaly Bordug 	/* Install watchdog timer handler early because it might be
197df34403dSVitaly Bordug 	 * already enabled by the bootloader
198df34403dSVitaly Bordug 	 */
199df34403dSVitaly Bordug 	m8xx_wdt_handler_install(binfo);
200df34403dSVitaly Bordug #endif
201df34403dSVitaly Bordug }
202df34403dSVitaly Bordug 
203df34403dSVitaly Bordug /* The RTC on the MPC8xx is an internal register.
204df34403dSVitaly Bordug  * We want to protect this during power down, so we need to unlock,
205df34403dSVitaly Bordug  * modify, and re-lock.
206df34403dSVitaly Bordug  */
207df34403dSVitaly Bordug 
208df34403dSVitaly Bordug int mpc8xx_set_rtc_time(struct rtc_time *tm)
209df34403dSVitaly Bordug {
210df34403dSVitaly Bordug 	sitk8xx_t *sys_tmr1;
211df34403dSVitaly Bordug 	sit8xx_t *sys_tmr2;
212df34403dSVitaly Bordug 	int time;
213df34403dSVitaly Bordug 
214df34403dSVitaly Bordug         sys_tmr1 = (sitk8xx_t *) immr_map(im_sitk);
215df34403dSVitaly Bordug 	sys_tmr2 = (sit8xx_t *) immr_map(im_sit);
216df34403dSVitaly Bordug 	time = mktime(tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
217df34403dSVitaly Bordug                       tm->tm_hour, tm->tm_min, tm->tm_sec);
218df34403dSVitaly Bordug 
219df34403dSVitaly Bordug 	out_be32(&sys_tmr1->sitk_rtck, KAPWR_KEY);
220df34403dSVitaly Bordug 	out_be32(&sys_tmr2->sit_rtc, time);
221df34403dSVitaly Bordug 	out_be32(&sys_tmr1->sitk_rtck, ~KAPWR_KEY);
222df34403dSVitaly Bordug 
223df34403dSVitaly Bordug 	immr_unmap(sys_tmr2);
224df34403dSVitaly Bordug 	immr_unmap(sys_tmr1);
225df34403dSVitaly Bordug 	return 0;
226df34403dSVitaly Bordug }
227df34403dSVitaly Bordug 
228df34403dSVitaly Bordug void mpc8xx_get_rtc_time(struct rtc_time *tm)
229df34403dSVitaly Bordug {
230df34403dSVitaly Bordug 	unsigned long data;
231df34403dSVitaly Bordug 	sit8xx_t *sys_tmr = (sit8xx_t *) immr_map(im_sit);
232df34403dSVitaly Bordug 
233df34403dSVitaly Bordug 	/* Get time from the RTC. */
234df34403dSVitaly Bordug 	data = in_be32(&sys_tmr->sit_rtc);
235df34403dSVitaly Bordug 	to_tm(data, tm);
236df34403dSVitaly Bordug         tm->tm_year -= 1900;
237df34403dSVitaly Bordug         tm->tm_mon -= 1;
238df34403dSVitaly Bordug 	immr_unmap(sys_tmr);
239df34403dSVitaly Bordug 	return;
240df34403dSVitaly Bordug }
241df34403dSVitaly Bordug 
242df34403dSVitaly Bordug void mpc8xx_restart(char *cmd)
243df34403dSVitaly Bordug {
244df34403dSVitaly Bordug 	__volatile__ unsigned char dummy;
245df34403dSVitaly Bordug 	car8xx_t * clk_r = (car8xx_t *) immr_map(im_clkrst);
246df34403dSVitaly Bordug 
247df34403dSVitaly Bordug 
248df34403dSVitaly Bordug 	local_irq_disable();
249df34403dSVitaly Bordug 
250df34403dSVitaly Bordug 	setbits32(&clk_r->car_plprcr, 0x00000080);
251df34403dSVitaly Bordug 	/* Clear the ME bit in MSR to cause checkstop on machine check
252df34403dSVitaly Bordug 	*/
253df34403dSVitaly Bordug 	mtmsr(mfmsr() & ~0x1000);
254df34403dSVitaly Bordug 
255df34403dSVitaly Bordug 	dummy = in_8(&clk_r->res[0]);
256df34403dSVitaly Bordug 	printk("Restart failed\n");
257df34403dSVitaly Bordug 	while(1);
258df34403dSVitaly Bordug }
259df34403dSVitaly Bordug 
260df34403dSVitaly Bordug void mpc8xx_show_cpuinfo(struct seq_file *m)
261df34403dSVitaly Bordug {
262df34403dSVitaly Bordug 	struct device_node *root;
263df34403dSVitaly Bordug 	uint memsize = total_memory;
264df34403dSVitaly Bordug 	const char *model = "";
265df34403dSVitaly Bordug 
266df34403dSVitaly Bordug 	seq_printf(m, "Vendor\t\t: Freescale Semiconductor\n");
267df34403dSVitaly Bordug 
268df34403dSVitaly Bordug 	root = of_find_node_by_path("/");
269df34403dSVitaly Bordug 	if (root)
270e2eb6392SStephen Rothwell 		model = of_get_property(root, "model", NULL);
271df34403dSVitaly Bordug 	seq_printf(m, "Machine\t\t: %s\n", model);
272df34403dSVitaly Bordug 	of_node_put(root);
273df34403dSVitaly Bordug 
274df34403dSVitaly Bordug 	seq_printf(m, "Memory\t\t: %d MB\n", memsize / (1024 * 1024));
275df34403dSVitaly Bordug }
276df34403dSVitaly Bordug 
277df34403dSVitaly Bordug static void cpm_cascade(unsigned int irq, struct irq_desc *desc)
278df34403dSVitaly Bordug {
279df34403dSVitaly Bordug 	int cascade_irq;
280df34403dSVitaly Bordug 
281df34403dSVitaly Bordug 	if ((cascade_irq = cpm_get_irq()) >= 0) {
282df34403dSVitaly Bordug 		struct irq_desc *cdesc = irq_desc + cascade_irq;
283df34403dSVitaly Bordug 
284df34403dSVitaly Bordug 		generic_handle_irq(cascade_irq);
285df34403dSVitaly Bordug 		cdesc->chip->eoi(cascade_irq);
286df34403dSVitaly Bordug 	}
287df34403dSVitaly Bordug 	desc->chip->eoi(irq);
288df34403dSVitaly Bordug }
289df34403dSVitaly Bordug 
290df34403dSVitaly Bordug /* Initialize the internal interrupt controller.  The number of
291df34403dSVitaly Bordug  * interrupts supported can vary with the processor type, and the
292df34403dSVitaly Bordug  * 82xx family can have up to 64.
293df34403dSVitaly Bordug  * External interrupts can be either edge or level triggered, and
294df34403dSVitaly Bordug  * need to be initialized by the appropriate driver.
295df34403dSVitaly Bordug  */
296df34403dSVitaly Bordug void __init m8xx_pic_init(void)
297df34403dSVitaly Bordug {
298df34403dSVitaly Bordug 	int irq;
299df34403dSVitaly Bordug 
300df34403dSVitaly Bordug 	if (mpc8xx_pic_init()) {
301df34403dSVitaly Bordug                 printk(KERN_ERR "Failed interrupt 8xx controller  initialization\n");
302df34403dSVitaly Bordug 		return;
303df34403dSVitaly Bordug 	}
304df34403dSVitaly Bordug 
305df34403dSVitaly Bordug 	irq = cpm_pic_init();
306df34403dSVitaly Bordug 	if (irq != NO_IRQ)
307df34403dSVitaly Bordug 		set_irq_chained_handler(irq, cpm_cascade);
308df34403dSVitaly Bordug }
309