xref: /linux/arch/powerpc/platforms/8xx/m8xx_setup.c (revision fbbf4280dae4c02d2f176a8fdac7a7d32fe76fc0)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2df34403dSVitaly Bordug /*
3df34403dSVitaly Bordug  *  Copyright (C) 1995  Linus Torvalds
4df34403dSVitaly Bordug  *  Adapted from 'alpha' version by Gary Thomas
5df34403dSVitaly Bordug  *  Modified by Cort Dougan (cort@cs.nmt.edu)
6df34403dSVitaly Bordug  *  Modified for MBX using prep/chrp/pmac functions by Dan (dmalek@jlc.net)
7df34403dSVitaly Bordug  *  Further modified for generic 8xx by Dan.
8df34403dSVitaly Bordug  */
9df34403dSVitaly Bordug 
10df34403dSVitaly Bordug /*
11df34403dSVitaly Bordug  * bootup setup stuff..
12df34403dSVitaly Bordug  */
13df34403dSVitaly Bordug 
14df34403dSVitaly Bordug #include <linux/kernel.h>
15df34403dSVitaly Bordug #include <linux/interrupt.h>
16df34403dSVitaly Bordug #include <linux/init.h>
17df34403dSVitaly Bordug #include <linux/time.h>
18df34403dSVitaly Bordug #include <linux/rtc.h>
1902753cb6SJochen Friedrich #include <linux/fsl_devices.h>
20e6f6390aSChristophe Leroy #include <linux/of.h>
21e6f6390aSChristophe Leroy #include <linux/of_irq.h>
22df34403dSVitaly Bordug 
23df34403dSVitaly Bordug #include <asm/io.h>
24df34403dSVitaly Bordug #include <asm/8xx_immap.h>
25df34403dSVitaly Bordug #include <mm/mmu_decl.h>
26df34403dSVitaly Bordug 
27de41ef6eSChristophe Leroy #include "pic.h"
2849b51545SJochen Friedrich 
2949b51545SJochen Friedrich #include "mpc8xx.h"
30df34403dSVitaly Bordug 
31df34403dSVitaly Bordug /* A place holder for time base interrupts, if they are ever enabled. */
32fb533d0cSScott Wood static irqreturn_t timebase_interrupt(int irq, void *dev)
33df34403dSVitaly Bordug {
34df34403dSVitaly Bordug 	printk ("timebase_interrupt()\n");
35df34403dSVitaly Bordug 
36df34403dSVitaly Bordug 	return IRQ_HANDLED;
37df34403dSVitaly Bordug }
38df34403dSVitaly Bordug 
39df34403dSVitaly Bordug /* per-board overridable init_internal_rtc() function. */
40df34403dSVitaly Bordug void __init __attribute__ ((weak))
41df34403dSVitaly Bordug init_internal_rtc(void)
42df34403dSVitaly Bordug {
43df34403dSVitaly Bordug 	/* Disable the RTC one second and alarm interrupts. */
44*fbbf4280SChristophe Leroy 	clrbits16(&mpc8xx_immr->im_sit.sit_rtcsc, (RTCSC_SIE | RTCSC_ALE));
45df34403dSVitaly Bordug 
46df34403dSVitaly Bordug 	/* Enable the RTC */
47*fbbf4280SChristophe Leroy 	setbits16(&mpc8xx_immr->im_sit.sit_rtcsc, (RTCSC_RTF | RTCSC_RTE));
48df34403dSVitaly Bordug }
49df34403dSVitaly Bordug 
50df34403dSVitaly Bordug static int __init get_freq(char *name, unsigned long *val)
51df34403dSVitaly Bordug {
52df34403dSVitaly Bordug 	struct device_node *cpu;
53e2eb6392SStephen Rothwell 	const unsigned int *fp;
54df34403dSVitaly Bordug 	int found = 0;
55df34403dSVitaly Bordug 
56df34403dSVitaly Bordug 	/* The cpu node should have timebase and clock frequency properties */
5738959a09SRob Herring 	cpu = of_get_cpu_node(0, NULL);
58df34403dSVitaly Bordug 
59df34403dSVitaly Bordug 	if (cpu) {
60e2eb6392SStephen Rothwell 		fp = of_get_property(cpu, name, NULL);
61df34403dSVitaly Bordug 		if (fp) {
62df34403dSVitaly Bordug 			found = 1;
63e2eb6392SStephen Rothwell 			*val = *fp;
64df34403dSVitaly Bordug 		}
65df34403dSVitaly Bordug 
66df34403dSVitaly Bordug 		of_node_put(cpu);
67df34403dSVitaly Bordug 	}
68df34403dSVitaly Bordug 
69df34403dSVitaly Bordug 	return found;
70df34403dSVitaly Bordug }
71df34403dSVitaly Bordug 
72df34403dSVitaly Bordug /* The decrementer counts at the system (internal) clock frequency divided by
73df34403dSVitaly Bordug  * sixteen, or external oscillator divided by four.  We force the processor
74df34403dSVitaly Bordug  * to use system clock divided by sixteen.
75df34403dSVitaly Bordug  */
76df34403dSVitaly Bordug void __init mpc8xx_calibrate_decr(void)
77df34403dSVitaly Bordug {
78df34403dSVitaly Bordug 	struct device_node *cpu;
79df34403dSVitaly Bordug 	int irq, virq;
80df34403dSVitaly Bordug 
81df34403dSVitaly Bordug 	/* Unlock the SCCR. */
82*fbbf4280SChristophe Leroy 	out_be32(&mpc8xx_immr->im_clkrstk.cark_sccrk, ~KAPWR_KEY);
83*fbbf4280SChristophe Leroy 	out_be32(&mpc8xx_immr->im_clkrstk.cark_sccrk, KAPWR_KEY);
84df34403dSVitaly Bordug 
85df34403dSVitaly Bordug 	/* Force all 8xx processors to use divide by 16 processor clock. */
86*fbbf4280SChristophe Leroy 	setbits32(&mpc8xx_immr->im_clkrst.car_sccr, 0x02000000);
87df34403dSVitaly Bordug 
88df34403dSVitaly Bordug 	/* Processor frequency is MHz.
89df34403dSVitaly Bordug 	 */
90df34403dSVitaly Bordug 	ppc_proc_freq = 50000000;
91df34403dSVitaly Bordug 	if (!get_freq("clock-frequency", &ppc_proc_freq))
92df34403dSVitaly Bordug 		printk(KERN_ERR "WARNING: Estimating processor frequency "
93df34403dSVitaly Bordug 		                "(not found)\n");
94df34403dSVitaly Bordug 
9550530378SAnton Vorontsov 	ppc_tb_freq = ppc_proc_freq / 16;
96df34403dSVitaly Bordug 	printk("Decrementer Frequency = 0x%lx\n", ppc_tb_freq);
97df34403dSVitaly Bordug 
98df34403dSVitaly Bordug 	/* Perform some more timer/timebase initialization.  This used
99df34403dSVitaly Bordug 	 * to be done elsewhere, but other changes caused it to get
100df34403dSVitaly Bordug 	 * called more than once....that is a bad thing.
101df34403dSVitaly Bordug 	 *
102df34403dSVitaly Bordug 	 * First, unlock all of the registers we are going to modify.
103df34403dSVitaly Bordug 	 * To protect them from corruption during power down, registers
104df34403dSVitaly Bordug 	 * that are maintained by keep alive power are "locked".  To
105df34403dSVitaly Bordug 	 * modify these registers we have to write the key value to
106df34403dSVitaly Bordug 	 * the key location associated with the register.
107df34403dSVitaly Bordug 	 * Some boards power up with these unlocked, while others
108df34403dSVitaly Bordug 	 * are locked.  Writing anything (including the unlock code?)
109df34403dSVitaly Bordug 	 * to the unlocked registers will lock them again.  So, here
110df34403dSVitaly Bordug 	 * we guarantee the registers are locked, then we unlock them
111df34403dSVitaly Bordug 	 * for our use.
112df34403dSVitaly Bordug 	 */
113*fbbf4280SChristophe Leroy 	out_be32(&mpc8xx_immr->im_sitk.sitk_tbscrk, ~KAPWR_KEY);
114*fbbf4280SChristophe Leroy 	out_be32(&mpc8xx_immr->im_sitk.sitk_rtcsck, ~KAPWR_KEY);
115*fbbf4280SChristophe Leroy 	out_be32(&mpc8xx_immr->im_sitk.sitk_tbk, ~KAPWR_KEY);
116*fbbf4280SChristophe Leroy 	out_be32(&mpc8xx_immr->im_sitk.sitk_tbscrk, KAPWR_KEY);
117*fbbf4280SChristophe Leroy 	out_be32(&mpc8xx_immr->im_sitk.sitk_rtcsck, KAPWR_KEY);
118*fbbf4280SChristophe Leroy 	out_be32(&mpc8xx_immr->im_sitk.sitk_tbk, KAPWR_KEY);
119df34403dSVitaly Bordug 
120df34403dSVitaly Bordug 	init_internal_rtc();
121df34403dSVitaly Bordug 
122df34403dSVitaly Bordug 	/* Enabling the decrementer also enables the timebase interrupts
123df34403dSVitaly Bordug 	 * (or from the other point of view, to get decrementer interrupts
124df34403dSVitaly Bordug 	 * we have to enable the timebase).  The decrementer interrupt
125df34403dSVitaly Bordug 	 * is wired into the vector table, nothing to do here for that.
126df34403dSVitaly Bordug 	 */
12738959a09SRob Herring 	cpu = of_get_cpu_node(0, NULL);
128df34403dSVitaly Bordug 	virq= irq_of_parse_and_map(cpu, 0);
12938959a09SRob Herring 	of_node_put(cpu);
130476eb491SGrant Likely 	irq = virq_to_hw(virq);
131df34403dSVitaly Bordug 
132*fbbf4280SChristophe Leroy 	out_be16(&mpc8xx_immr->im_sit.sit_tbscr,
133*fbbf4280SChristophe Leroy 		 ((1 << (7 - (irq / 2))) << 8) | (TBSCR_TBF | TBSCR_TBE));
134df34403dSVitaly Bordug 
135b4f00d5bSafzal mohammed 	if (request_irq(virq, timebase_interrupt, IRQF_NO_THREAD, "tbint",
136b4f00d5bSafzal mohammed 			NULL))
137df34403dSVitaly Bordug 		panic("Could not allocate timer IRQ!");
138df34403dSVitaly Bordug }
139df34403dSVitaly Bordug 
140df34403dSVitaly Bordug /* The RTC on the MPC8xx is an internal register.
141df34403dSVitaly Bordug  * We want to protect this during power down, so we need to unlock,
142df34403dSVitaly Bordug  * modify, and re-lock.
143df34403dSVitaly Bordug  */
144df34403dSVitaly Bordug 
145df34403dSVitaly Bordug int mpc8xx_set_rtc_time(struct rtc_time *tm)
146df34403dSVitaly Bordug {
1475235afa8SArnd Bergmann 	time64_t time;
148df34403dSVitaly Bordug 
1495235afa8SArnd Bergmann 	time = rtc_tm_to_time64(tm);
150df34403dSVitaly Bordug 
151*fbbf4280SChristophe Leroy 	out_be32(&mpc8xx_immr->im_sitk.sitk_rtck, KAPWR_KEY);
152*fbbf4280SChristophe Leroy 	out_be32(&mpc8xx_immr->im_sit.sit_rtc, (u32)time);
153*fbbf4280SChristophe Leroy 	out_be32(&mpc8xx_immr->im_sitk.sitk_rtck, ~KAPWR_KEY);
154df34403dSVitaly Bordug 
155df34403dSVitaly Bordug 	return 0;
156df34403dSVitaly Bordug }
157df34403dSVitaly Bordug 
158df34403dSVitaly Bordug void mpc8xx_get_rtc_time(struct rtc_time *tm)
159df34403dSVitaly Bordug {
160df34403dSVitaly Bordug 	unsigned long data;
161df34403dSVitaly Bordug 
162df34403dSVitaly Bordug 	/* Get time from the RTC. */
163*fbbf4280SChristophe Leroy 	data = in_be32(&mpc8xx_immr->im_sit.sit_rtc);
1645bfd6435SArnd Bergmann 	rtc_time64_to_tm(data, tm);
165df34403dSVitaly Bordug 	return;
166df34403dSVitaly Bordug }
167df34403dSVitaly Bordug 
16895ec77c0SDaniel Axtens void __noreturn mpc8xx_restart(char *cmd)
169df34403dSVitaly Bordug {
170df34403dSVitaly Bordug 	local_irq_disable();
171df34403dSVitaly Bordug 
172*fbbf4280SChristophe Leroy 	setbits32(&mpc8xx_immr->im_clkrst.car_plprcr, 0x00000080);
173df34403dSVitaly Bordug 	/* Clear the ME bit in MSR to cause checkstop on machine check
174df34403dSVitaly Bordug 	*/
175df34403dSVitaly Bordug 	mtmsr(mfmsr() & ~0x1000);
176df34403dSVitaly Bordug 
177*fbbf4280SChristophe Leroy 	in_8(&mpc8xx_immr->im_clkrst.res[0]);
178fb533d0cSScott Wood 	panic("Restart failed\n");
179df34403dSVitaly Bordug }
180