Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * This file is subject to the terms and conditions of the GNU General Public
0003  * License.  See the file "COPYING" in the main directory of this archive
0004  * for more details.
0005  *
0006  * Copyright (C) 2001 Keith M Wesolowski
0007  * Copyright (C) 2001 Paul Mundt
0008  * Copyright (C) 2003 Guido Guenther <agx@sigxcpu.org>
0009  */
0010 
0011 #include <linux/compiler.h>
0012 #include <linux/init.h>
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 #include <linux/panic_notifier.h>
0016 #include <linux/sched.h>
0017 #include <linux/sched/signal.h>
0018 #include <linux/notifier.h>
0019 #include <linux/delay.h>
0020 #include <linux/rtc/ds1685.h>
0021 #include <linux/interrupt.h>
0022 #include <linux/pm.h>
0023 
0024 #include <asm/addrspace.h>
0025 #include <asm/irq.h>
0026 #include <asm/reboot.h>
0027 #include <asm/wbflush.h>
0028 #include <asm/ip32/mace.h>
0029 #include <asm/ip32/crime.h>
0030 #include <asm/ip32/ip32_ints.h>
0031 
0032 #define POWERDOWN_TIMEOUT   120
0033 /*
0034  * Blink frequency during reboot grace period and when panicked.
0035  */
0036 #define POWERDOWN_FREQ      (HZ / 4)
0037 #define PANIC_FREQ      (HZ / 8)
0038 
0039 extern struct platform_device ip32_rtc_device;
0040 
0041 static struct timer_list power_timer, blink_timer;
0042 static unsigned long blink_timer_timeout;
0043 static int has_panicked, shutting_down;
0044 
0045 static __noreturn void ip32_poweroff(void *data)
0046 {
0047     void (*poweroff_func)(struct platform_device *) =
0048         symbol_get(ds1685_rtc_poweroff);
0049 
0050 #ifdef CONFIG_MODULES
0051     /* If the first __symbol_get failed, our module wasn't loaded. */
0052     if (!poweroff_func) {
0053         request_module("rtc-ds1685");
0054         poweroff_func = symbol_get(ds1685_rtc_poweroff);
0055     }
0056 #endif
0057 
0058     if (!poweroff_func)
0059         pr_emerg("RTC not available for power-off.  Spinning forever ...\n");
0060     else {
0061         (*poweroff_func)((struct platform_device *)data);
0062         symbol_put(ds1685_rtc_poweroff);
0063     }
0064 
0065     unreachable();
0066 }
0067 
0068 static void ip32_machine_restart(char *cmd) __noreturn;
0069 static void ip32_machine_restart(char *cmd)
0070 {
0071     msleep(20);
0072     crime->control = CRIME_CONTROL_HARD_RESET;
0073     unreachable();
0074 }
0075 
0076 static void blink_timeout(struct timer_list *unused)
0077 {
0078     unsigned long led = mace->perif.ctrl.misc ^ MACEISA_LED_RED;
0079     mace->perif.ctrl.misc = led;
0080     mod_timer(&blink_timer, jiffies + blink_timer_timeout);
0081 }
0082 
0083 static void ip32_machine_halt(void)
0084 {
0085     ip32_poweroff(&ip32_rtc_device);
0086 }
0087 
0088 static void power_timeout(struct timer_list *unused)
0089 {
0090     ip32_poweroff(&ip32_rtc_device);
0091 }
0092 
0093 void ip32_prepare_poweroff(void)
0094 {
0095     if (has_panicked)
0096         return;
0097 
0098     if (shutting_down || kill_cad_pid(SIGINT, 1)) {
0099         /* No init process or button pressed twice.  */
0100         ip32_poweroff(&ip32_rtc_device);
0101     }
0102 
0103     shutting_down = 1;
0104     blink_timer_timeout = POWERDOWN_FREQ;
0105     blink_timeout(&blink_timer);
0106 
0107     timer_setup(&power_timer, power_timeout, 0);
0108     power_timer.expires = jiffies + POWERDOWN_TIMEOUT * HZ;
0109     add_timer(&power_timer);
0110 }
0111 
0112 static int panic_event(struct notifier_block *this, unsigned long event,
0113                void *ptr)
0114 {
0115     unsigned long led;
0116 
0117     if (has_panicked)
0118         return NOTIFY_DONE;
0119     has_panicked = 1;
0120 
0121     /* turn off the green LED */
0122     led = mace->perif.ctrl.misc | MACEISA_LED_GREEN;
0123     mace->perif.ctrl.misc = led;
0124 
0125     blink_timer_timeout = PANIC_FREQ;
0126     blink_timeout(&blink_timer);
0127 
0128     return NOTIFY_DONE;
0129 }
0130 
0131 static struct notifier_block panic_block = {
0132     .notifier_call = panic_event,
0133 };
0134 
0135 static __init int ip32_reboot_setup(void)
0136 {
0137     /* turn on the green led only */
0138     unsigned long led = mace->perif.ctrl.misc;
0139     led |= MACEISA_LED_RED;
0140     led &= ~MACEISA_LED_GREEN;
0141     mace->perif.ctrl.misc = led;
0142 
0143     _machine_restart = ip32_machine_restart;
0144     _machine_halt = ip32_machine_halt;
0145     pm_power_off = ip32_machine_halt;
0146 
0147     timer_setup(&blink_timer, blink_timeout, 0);
0148     atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
0149 
0150     return 0;
0151 }
0152 
0153 subsys_initcall(ip32_reboot_setup);