reset.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * You should have received a copy of the GNU General Public License along
  10. * with this program; if not, write to the Free Software Foundation, Inc.,
  11. * 675 Mass Ave, Cambridge, MA 02139, USA.
  12. *
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/pm.h>
  18. #include <asm/reboot.h>
  19. #include <asm/mach-jz4740/base.h>
  20. #include <asm/mach-jz4740/timer.h>
  21. #include "reset.h"
  22. #include "clock.h"
  23. static void jz4740_halt(void)
  24. {
  25. while (1) {
  26. __asm__(".set push;\n"
  27. ".set mips3;\n"
  28. "wait;\n"
  29. ".set pop;\n"
  30. );
  31. }
  32. }
  33. #define JZ_REG_WDT_DATA 0x00
  34. #define JZ_REG_WDT_COUNTER_ENABLE 0x04
  35. #define JZ_REG_WDT_COUNTER 0x08
  36. #define JZ_REG_WDT_CTRL 0x0c
  37. static void jz4740_restart(char *command)
  38. {
  39. void __iomem *wdt_base = ioremap(JZ4740_WDT_BASE_ADDR, 0x0f);
  40. jz4740_timer_enable_watchdog();
  41. writeb(0, wdt_base + JZ_REG_WDT_COUNTER_ENABLE);
  42. writew(0, wdt_base + JZ_REG_WDT_COUNTER);
  43. writew(0, wdt_base + JZ_REG_WDT_DATA);
  44. writew(BIT(2), wdt_base + JZ_REG_WDT_CTRL);
  45. writeb(1, wdt_base + JZ_REG_WDT_COUNTER_ENABLE);
  46. jz4740_halt();
  47. }
  48. #define JZ_REG_RTC_CTRL 0x00
  49. #define JZ_REG_RTC_HIBERNATE 0x20
  50. #define JZ_REG_RTC_WAKEUP_FILTER 0x24
  51. #define JZ_REG_RTC_RESET_COUNTER 0x28
  52. #define JZ_RTC_CTRL_WRDY BIT(7)
  53. #define JZ_RTC_WAKEUP_FILTER_MASK 0x0000FFE0
  54. #define JZ_RTC_RESET_COUNTER_MASK 0x00000FE0
  55. static inline void jz4740_rtc_wait_ready(void __iomem *rtc_base)
  56. {
  57. uint32_t ctrl;
  58. do {
  59. ctrl = readl(rtc_base + JZ_REG_RTC_CTRL);
  60. } while (!(ctrl & JZ_RTC_CTRL_WRDY));
  61. }
  62. static void jz4740_power_off(void)
  63. {
  64. void __iomem *rtc_base = ioremap(JZ4740_RTC_BASE_ADDR, 0x38);
  65. unsigned long wakeup_filter_ticks;
  66. unsigned long reset_counter_ticks;
  67. struct clk *rtc_clk;
  68. unsigned long rtc_rate;
  69. rtc_clk = clk_get(NULL, "rtc");
  70. if (IS_ERR(rtc_clk))
  71. panic("unable to get RTC clock");
  72. rtc_rate = clk_get_rate(rtc_clk);
  73. clk_put(rtc_clk);
  74. /*
  75. * Set minimum wakeup pin assertion time: 100 ms.
  76. * Range is 0 to 2 sec if RTC is clocked at 32 kHz.
  77. */
  78. wakeup_filter_ticks = (100 * rtc_rate) / 1000;
  79. if (wakeup_filter_ticks < JZ_RTC_WAKEUP_FILTER_MASK)
  80. wakeup_filter_ticks &= JZ_RTC_WAKEUP_FILTER_MASK;
  81. else
  82. wakeup_filter_ticks = JZ_RTC_WAKEUP_FILTER_MASK;
  83. jz4740_rtc_wait_ready(rtc_base);
  84. writel(wakeup_filter_ticks, rtc_base + JZ_REG_RTC_WAKEUP_FILTER);
  85. /*
  86. * Set reset pin low-level assertion time after wakeup: 60 ms.
  87. * Range is 0 to 125 ms if RTC is clocked at 32 kHz.
  88. */
  89. reset_counter_ticks = (60 * rtc_rate) / 1000;
  90. if (reset_counter_ticks < JZ_RTC_RESET_COUNTER_MASK)
  91. reset_counter_ticks &= JZ_RTC_RESET_COUNTER_MASK;
  92. else
  93. reset_counter_ticks = JZ_RTC_RESET_COUNTER_MASK;
  94. jz4740_rtc_wait_ready(rtc_base);
  95. writel(reset_counter_ticks, rtc_base + JZ_REG_RTC_RESET_COUNTER);
  96. jz4740_rtc_wait_ready(rtc_base);
  97. writel(1, rtc_base + JZ_REG_RTC_HIBERNATE);
  98. jz4740_halt();
  99. }
  100. void jz4740_reset_init(void)
  101. {
  102. _machine_restart = jz4740_restart;
  103. _machine_halt = jz4740_halt;
  104. pm_power_off = jz4740_power_off;
  105. }