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