olpc-xo1-pm.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Support for power management features of the OLPC XO-1 laptop
  4. *
  5. * Copyright (C) 2010 Andres Salomon <[email protected]>
  6. * Copyright (C) 2010 One Laptop per Child
  7. * Copyright (C) 2006 Red Hat, Inc.
  8. * Copyright (C) 2006 Advanced Micro Devices, Inc.
  9. */
  10. #include <linux/cs5535.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/export.h>
  13. #include <linux/pm.h>
  14. #include <linux/suspend.h>
  15. #include <linux/olpc-ec.h>
  16. #include <asm/io.h>
  17. #include <asm/olpc.h>
  18. #define DRV_NAME "olpc-xo1-pm"
  19. static unsigned long acpi_base;
  20. static unsigned long pms_base;
  21. static u16 wakeup_mask = CS5536_PM_PWRBTN;
  22. static struct {
  23. unsigned long address;
  24. unsigned short segment;
  25. } ofw_bios_entry = { 0xF0000 + PAGE_OFFSET, __KERNEL_CS };
  26. /* Set bits in the wakeup mask */
  27. void olpc_xo1_pm_wakeup_set(u16 value)
  28. {
  29. wakeup_mask |= value;
  30. }
  31. EXPORT_SYMBOL_GPL(olpc_xo1_pm_wakeup_set);
  32. /* Clear bits in the wakeup mask */
  33. void olpc_xo1_pm_wakeup_clear(u16 value)
  34. {
  35. wakeup_mask &= ~value;
  36. }
  37. EXPORT_SYMBOL_GPL(olpc_xo1_pm_wakeup_clear);
  38. static int xo1_power_state_enter(suspend_state_t pm_state)
  39. {
  40. unsigned long saved_sci_mask;
  41. /* Only STR is supported */
  42. if (pm_state != PM_SUSPEND_MEM)
  43. return -EINVAL;
  44. /*
  45. * Save SCI mask (this gets lost since PM1_EN is used as a mask for
  46. * wakeup events, which is not necessarily the same event set)
  47. */
  48. saved_sci_mask = inl(acpi_base + CS5536_PM1_STS);
  49. saved_sci_mask &= 0xffff0000;
  50. /* Save CPU state */
  51. do_olpc_suspend_lowlevel();
  52. /* Resume path starts here */
  53. /* Restore SCI mask (using dword access to CS5536_PM1_EN) */
  54. outl(saved_sci_mask, acpi_base + CS5536_PM1_STS);
  55. return 0;
  56. }
  57. asmlinkage __visible int xo1_do_sleep(u8 sleep_state)
  58. {
  59. void *pgd_addr = __va(read_cr3_pa());
  60. /* Program wakeup mask (using dword access to CS5536_PM1_EN) */
  61. outl(wakeup_mask << 16, acpi_base + CS5536_PM1_STS);
  62. __asm__("movl %0,%%eax" : : "r" (pgd_addr));
  63. __asm__("call *(%%edi); cld"
  64. : : "D" (&ofw_bios_entry));
  65. __asm__("movb $0x34, %al\n\t"
  66. "outb %al, $0x70\n\t"
  67. "movb $0x30, %al\n\t"
  68. "outb %al, $0x71\n\t");
  69. return 0;
  70. }
  71. static void xo1_power_off(void)
  72. {
  73. printk(KERN_INFO "OLPC XO-1 power off sequence...\n");
  74. /* Enable all of these controls with 0 delay */
  75. outl(0x40000000, pms_base + CS5536_PM_SCLK);
  76. outl(0x40000000, pms_base + CS5536_PM_IN_SLPCTL);
  77. outl(0x40000000, pms_base + CS5536_PM_WKXD);
  78. outl(0x40000000, pms_base + CS5536_PM_WKD);
  79. /* Clear status bits (possibly unnecessary) */
  80. outl(0x0002ffff, pms_base + CS5536_PM_SSC);
  81. outl(0xffffffff, acpi_base + CS5536_PM_GPE0_STS);
  82. /* Write SLP_EN bit to start the machinery */
  83. outl(0x00002000, acpi_base + CS5536_PM1_CNT);
  84. }
  85. static int xo1_power_state_valid(suspend_state_t pm_state)
  86. {
  87. /* suspend-to-RAM only */
  88. return pm_state == PM_SUSPEND_MEM;
  89. }
  90. static const struct platform_suspend_ops xo1_suspend_ops = {
  91. .valid = xo1_power_state_valid,
  92. .enter = xo1_power_state_enter,
  93. };
  94. static int xo1_pm_probe(struct platform_device *pdev)
  95. {
  96. struct resource *res;
  97. /* don't run on non-XOs */
  98. if (!machine_is_olpc())
  99. return -ENODEV;
  100. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  101. if (!res) {
  102. dev_err(&pdev->dev, "can't fetch device resource info\n");
  103. return -EIO;
  104. }
  105. if (strcmp(pdev->name, "cs5535-pms") == 0)
  106. pms_base = res->start;
  107. else if (strcmp(pdev->name, "olpc-xo1-pm-acpi") == 0)
  108. acpi_base = res->start;
  109. /* If we have both addresses, we can override the poweroff hook */
  110. if (pms_base && acpi_base) {
  111. suspend_set_ops(&xo1_suspend_ops);
  112. pm_power_off = xo1_power_off;
  113. printk(KERN_INFO "OLPC XO-1 support registered\n");
  114. }
  115. return 0;
  116. }
  117. static int xo1_pm_remove(struct platform_device *pdev)
  118. {
  119. if (strcmp(pdev->name, "cs5535-pms") == 0)
  120. pms_base = 0;
  121. else if (strcmp(pdev->name, "olpc-xo1-pm-acpi") == 0)
  122. acpi_base = 0;
  123. pm_power_off = NULL;
  124. return 0;
  125. }
  126. static struct platform_driver cs5535_pms_driver = {
  127. .driver = {
  128. .name = "cs5535-pms",
  129. },
  130. .probe = xo1_pm_probe,
  131. .remove = xo1_pm_remove,
  132. };
  133. static struct platform_driver cs5535_acpi_driver = {
  134. .driver = {
  135. .name = "olpc-xo1-pm-acpi",
  136. },
  137. .probe = xo1_pm_probe,
  138. .remove = xo1_pm_remove,
  139. };
  140. static int __init xo1_pm_init(void)
  141. {
  142. int r;
  143. r = platform_driver_register(&cs5535_pms_driver);
  144. if (r)
  145. return r;
  146. r = platform_driver_register(&cs5535_acpi_driver);
  147. if (r)
  148. platform_driver_unregister(&cs5535_pms_driver);
  149. return r;
  150. }
  151. arch_initcall(xo1_pm_init);