olpc-xo1-pm.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/pm.h>
  17. #include <linux/mfd/core.h>
  18. #include <linux/suspend.h>
  19. #include <asm/io.h>
  20. #include <asm/olpc.h>
  21. #define DRV_NAME "olpc-xo1-pm"
  22. static unsigned long acpi_base;
  23. static unsigned long pms_base;
  24. static u16 wakeup_mask = CS5536_PM_PWRBTN;
  25. static struct {
  26. unsigned long address;
  27. unsigned short segment;
  28. } ofw_bios_entry = { 0xF0000 + PAGE_OFFSET, __KERNEL_CS };
  29. /* Set bits in the wakeup mask */
  30. void olpc_xo1_pm_wakeup_set(u16 value)
  31. {
  32. wakeup_mask |= value;
  33. }
  34. EXPORT_SYMBOL_GPL(olpc_xo1_pm_wakeup_set);
  35. /* Clear bits in the wakeup mask */
  36. void olpc_xo1_pm_wakeup_clear(u16 value)
  37. {
  38. wakeup_mask &= ~value;
  39. }
  40. EXPORT_SYMBOL_GPL(olpc_xo1_pm_wakeup_clear);
  41. static int xo1_power_state_enter(suspend_state_t pm_state)
  42. {
  43. unsigned long saved_sci_mask;
  44. int r;
  45. /* Only STR is supported */
  46. if (pm_state != PM_SUSPEND_MEM)
  47. return -EINVAL;
  48. r = olpc_ec_cmd(EC_SET_SCI_INHIBIT, NULL, 0, NULL, 0);
  49. if (r)
  50. return r;
  51. /*
  52. * Save SCI mask (this gets lost since PM1_EN is used as a mask for
  53. * wakeup events, which is not necessarily the same event set)
  54. */
  55. saved_sci_mask = inl(acpi_base + CS5536_PM1_STS);
  56. saved_sci_mask &= 0xffff0000;
  57. /* Save CPU state */
  58. do_olpc_suspend_lowlevel();
  59. /* Resume path starts here */
  60. /* Restore SCI mask (using dword access to CS5536_PM1_EN) */
  61. outl(saved_sci_mask, acpi_base + CS5536_PM1_STS);
  62. /* Tell the EC to stop inhibiting SCIs */
  63. olpc_ec_cmd(EC_SET_SCI_INHIBIT_RELEASE, NULL, 0, NULL, 0);
  64. /*
  65. * Tell the wireless module to restart USB communication.
  66. * Must be done twice.
  67. */
  68. olpc_ec_cmd(EC_WAKE_UP_WLAN, NULL, 0, NULL, 0);
  69. olpc_ec_cmd(EC_WAKE_UP_WLAN, NULL, 0, NULL, 0);
  70. return 0;
  71. }
  72. asmlinkage int xo1_do_sleep(u8 sleep_state)
  73. {
  74. void *pgd_addr = __va(read_cr3());
  75. /* Program wakeup mask (using dword access to CS5536_PM1_EN) */
  76. outl(wakeup_mask << 16, acpi_base + CS5536_PM1_STS);
  77. __asm__("movl %0,%%eax" : : "r" (pgd_addr));
  78. __asm__("call *(%%edi); cld"
  79. : : "D" (&ofw_bios_entry));
  80. __asm__("movb $0x34, %al\n\t"
  81. "outb %al, $0x70\n\t"
  82. "movb $0x30, %al\n\t"
  83. "outb %al, $0x71\n\t");
  84. return 0;
  85. }
  86. static void xo1_power_off(void)
  87. {
  88. printk(KERN_INFO "OLPC XO-1 power off sequence...\n");
  89. /* Enable all of these controls with 0 delay */
  90. outl(0x40000000, pms_base + CS5536_PM_SCLK);
  91. outl(0x40000000, pms_base + CS5536_PM_IN_SLPCTL);
  92. outl(0x40000000, pms_base + CS5536_PM_WKXD);
  93. outl(0x40000000, pms_base + CS5536_PM_WKD);
  94. /* Clear status bits (possibly unnecessary) */
  95. outl(0x0002ffff, pms_base + CS5536_PM_SSC);
  96. outl(0xffffffff, acpi_base + CS5536_PM_GPE0_STS);
  97. /* Write SLP_EN bit to start the machinery */
  98. outl(0x00002000, acpi_base + CS5536_PM1_CNT);
  99. }
  100. static int xo1_power_state_valid(suspend_state_t pm_state)
  101. {
  102. /* suspend-to-RAM only */
  103. return pm_state == PM_SUSPEND_MEM;
  104. }
  105. static const struct platform_suspend_ops xo1_suspend_ops = {
  106. .valid = xo1_power_state_valid,
  107. .enter = xo1_power_state_enter,
  108. };
  109. static int __devinit xo1_pm_probe(struct platform_device *pdev)
  110. {
  111. struct resource *res;
  112. int err;
  113. /* don't run on non-XOs */
  114. if (!machine_is_olpc())
  115. return -ENODEV;
  116. err = mfd_cell_enable(pdev);
  117. if (err)
  118. return err;
  119. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  120. if (!res) {
  121. dev_err(&pdev->dev, "can't fetch device resource info\n");
  122. return -EIO;
  123. }
  124. if (strcmp(pdev->name, "cs5535-pms") == 0)
  125. pms_base = res->start;
  126. else if (strcmp(pdev->name, "olpc-xo1-pm-acpi") == 0)
  127. acpi_base = res->start;
  128. /* If we have both addresses, we can override the poweroff hook */
  129. if (pms_base && acpi_base) {
  130. suspend_set_ops(&xo1_suspend_ops);
  131. pm_power_off = xo1_power_off;
  132. printk(KERN_INFO "OLPC XO-1 support registered\n");
  133. }
  134. return 0;
  135. }
  136. static int __devexit xo1_pm_remove(struct platform_device *pdev)
  137. {
  138. mfd_cell_disable(pdev);
  139. if (strcmp(pdev->name, "cs5535-pms") == 0)
  140. pms_base = 0;
  141. else if (strcmp(pdev->name, "olpc-xo1-pm-acpi") == 0)
  142. acpi_base = 0;
  143. pm_power_off = NULL;
  144. return 0;
  145. }
  146. static struct platform_driver cs5535_pms_driver = {
  147. .driver = {
  148. .name = "cs5535-pms",
  149. .owner = THIS_MODULE,
  150. },
  151. .probe = xo1_pm_probe,
  152. .remove = __devexit_p(xo1_pm_remove),
  153. };
  154. static struct platform_driver cs5535_acpi_driver = {
  155. .driver = {
  156. .name = "olpc-xo1-pm-acpi",
  157. .owner = THIS_MODULE,
  158. },
  159. .probe = xo1_pm_probe,
  160. .remove = __devexit_p(xo1_pm_remove),
  161. };
  162. static int __init xo1_pm_init(void)
  163. {
  164. int r;
  165. r = platform_driver_register(&cs5535_pms_driver);
  166. if (r)
  167. return r;
  168. r = platform_driver_register(&cs5535_acpi_driver);
  169. if (r)
  170. platform_driver_unregister(&cs5535_pms_driver);
  171. return r;
  172. }
  173. arch_initcall(xo1_pm_init);