olpc-xo1-pm.c 4.7 KB

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