at91-poweroff.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Atmel AT91 SAM9 SoCs reset code
  3. *
  4. * Copyright (C) 2007 Atmel Corporation.
  5. * Copyright (C) 2011 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
  6. * Copyright (C) 2014 Free Electrons
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/printk.h>
  18. #define AT91_SHDW_CR 0x00 /* Shut Down Control Register */
  19. #define AT91_SHDW_SHDW BIT(0) /* Shut Down command */
  20. #define AT91_SHDW_KEY (0xa5 << 24) /* KEY Password */
  21. #define AT91_SHDW_MR 0x04 /* Shut Down Mode Register */
  22. #define AT91_SHDW_WKMODE0 GENMASK(2, 0) /* Wake-up 0 Mode Selection */
  23. #define AT91_SHDW_CPTWK0_MAX 0xf /* Maximum Counter On Wake Up 0 */
  24. #define AT91_SHDW_CPTWK0 (AT91_SHDW_CPTWK0_MAX << 4) /* Counter On Wake Up 0 */
  25. #define AT91_SHDW_CPTWK0_(x) ((x) << 4)
  26. #define AT91_SHDW_RTTWKEN BIT(16) /* Real Time Timer Wake-up Enable */
  27. #define AT91_SHDW_RTCWKEN BIT(17) /* Real Time Clock Wake-up Enable */
  28. #define AT91_SHDW_SR 0x08 /* Shut Down Status Register */
  29. #define AT91_SHDW_WAKEUP0 BIT(0) /* Wake-up 0 Status */
  30. #define AT91_SHDW_RTTWK BIT(16) /* Real-time Timer Wake-up */
  31. #define AT91_SHDW_RTCWK BIT(17) /* Real-time Clock Wake-up [SAM9RL] */
  32. enum wakeup_type {
  33. AT91_SHDW_WKMODE0_NONE = 0,
  34. AT91_SHDW_WKMODE0_HIGH = 1,
  35. AT91_SHDW_WKMODE0_LOW = 2,
  36. AT91_SHDW_WKMODE0_ANYLEVEL = 3,
  37. };
  38. static const char *shdwc_wakeup_modes[] = {
  39. [AT91_SHDW_WKMODE0_NONE] = "none",
  40. [AT91_SHDW_WKMODE0_HIGH] = "high",
  41. [AT91_SHDW_WKMODE0_LOW] = "low",
  42. [AT91_SHDW_WKMODE0_ANYLEVEL] = "any",
  43. };
  44. static void __iomem *at91_shdwc_base;
  45. static struct clk *sclk;
  46. static void __init at91_wakeup_status(void)
  47. {
  48. u32 reg = readl(at91_shdwc_base + AT91_SHDW_SR);
  49. char *reason = "unknown";
  50. /* Simple power-on, just bail out */
  51. if (!reg)
  52. return;
  53. if (reg & AT91_SHDW_RTTWK)
  54. reason = "RTT";
  55. else if (reg & AT91_SHDW_RTCWK)
  56. reason = "RTC";
  57. pr_info("AT91: Wake-Up source: %s\n", reason);
  58. }
  59. static void at91_poweroff(void)
  60. {
  61. writel(AT91_SHDW_KEY | AT91_SHDW_SHDW, at91_shdwc_base + AT91_SHDW_CR);
  62. }
  63. static int at91_poweroff_get_wakeup_mode(struct device_node *np)
  64. {
  65. const char *pm;
  66. unsigned int i;
  67. int err;
  68. err = of_property_read_string(np, "atmel,wakeup-mode", &pm);
  69. if (err < 0)
  70. return AT91_SHDW_WKMODE0_ANYLEVEL;
  71. for (i = 0; i < ARRAY_SIZE(shdwc_wakeup_modes); i++)
  72. if (!strcasecmp(pm, shdwc_wakeup_modes[i]))
  73. return i;
  74. return -ENODEV;
  75. }
  76. static void at91_poweroff_dt_set_wakeup_mode(struct platform_device *pdev)
  77. {
  78. struct device_node *np = pdev->dev.of_node;
  79. int wakeup_mode;
  80. u32 mode = 0, tmp;
  81. wakeup_mode = at91_poweroff_get_wakeup_mode(np);
  82. if (wakeup_mode < 0) {
  83. dev_warn(&pdev->dev, "shdwc unknown wakeup mode\n");
  84. return;
  85. }
  86. if (!of_property_read_u32(np, "atmel,wakeup-counter", &tmp)) {
  87. if (tmp > AT91_SHDW_CPTWK0_MAX) {
  88. dev_warn(&pdev->dev,
  89. "shdwc wakeup counter 0x%x > 0x%x reduce it to 0x%x\n",
  90. tmp, AT91_SHDW_CPTWK0_MAX, AT91_SHDW_CPTWK0_MAX);
  91. tmp = AT91_SHDW_CPTWK0_MAX;
  92. }
  93. mode |= AT91_SHDW_CPTWK0_(tmp);
  94. }
  95. if (of_property_read_bool(np, "atmel,wakeup-rtc-timer"))
  96. mode |= AT91_SHDW_RTCWKEN;
  97. if (of_property_read_bool(np, "atmel,wakeup-rtt-timer"))
  98. mode |= AT91_SHDW_RTTWKEN;
  99. writel(wakeup_mode | mode, at91_shdwc_base + AT91_SHDW_MR);
  100. }
  101. static int __init at91_poweroff_probe(struct platform_device *pdev)
  102. {
  103. struct resource *res;
  104. int ret;
  105. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  106. at91_shdwc_base = devm_ioremap_resource(&pdev->dev, res);
  107. if (IS_ERR(at91_shdwc_base)) {
  108. dev_err(&pdev->dev, "Could not map reset controller address\n");
  109. return PTR_ERR(at91_shdwc_base);
  110. }
  111. sclk = devm_clk_get(&pdev->dev, NULL);
  112. if (IS_ERR(sclk))
  113. return PTR_ERR(sclk);
  114. ret = clk_prepare_enable(sclk);
  115. if (ret) {
  116. dev_err(&pdev->dev, "Could not enable slow clock\n");
  117. return ret;
  118. }
  119. at91_wakeup_status();
  120. if (pdev->dev.of_node)
  121. at91_poweroff_dt_set_wakeup_mode(pdev);
  122. pm_power_off = at91_poweroff;
  123. return 0;
  124. }
  125. static int __exit at91_poweroff_remove(struct platform_device *pdev)
  126. {
  127. if (pm_power_off == at91_poweroff)
  128. pm_power_off = NULL;
  129. clk_disable_unprepare(sclk);
  130. return 0;
  131. }
  132. static const struct of_device_id at91_poweroff_of_match[] = {
  133. { .compatible = "atmel,at91sam9260-shdwc", },
  134. { .compatible = "atmel,at91sam9rl-shdwc", },
  135. { .compatible = "atmel,at91sam9x5-shdwc", },
  136. { /*sentinel*/ }
  137. };
  138. MODULE_DEVICE_TABLE(of, at91_poweroff_of_match);
  139. static struct platform_driver at91_poweroff_driver = {
  140. .remove = __exit_p(at91_poweroff_remove),
  141. .driver = {
  142. .name = "at91-poweroff",
  143. .of_match_table = at91_poweroff_of_match,
  144. },
  145. };
  146. module_platform_driver_probe(at91_poweroff_driver, at91_poweroff_probe);
  147. MODULE_AUTHOR("Atmel Corporation");
  148. MODULE_DESCRIPTION("Shutdown driver for Atmel SoCs");
  149. MODULE_LICENSE("GPL v2");