sysreset_x86.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
  4. *
  5. * Generic reset driver for x86 processor
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <efi_loader.h>
  10. #include <pch.h>
  11. #include <sysreset.h>
  12. #include <acpi/acpi_s3.h>
  13. #include <asm/io.h>
  14. #include <asm/processor.h>
  15. struct x86_sysreset_platdata {
  16. struct udevice *pch;
  17. };
  18. /*
  19. * Power down the machine by using the power management sleep control
  20. * of the chipset. This will currently only work on Intel chipsets.
  21. * However, adapting it to new chipsets is fairly simple. You will
  22. * have to find the IO address of the power management register block
  23. * in your southbridge, and look up the appropriate SLP_TYP_S5 value
  24. * from your southbridge's data sheet.
  25. *
  26. * This function never returns.
  27. */
  28. int pch_sysreset_power_off(struct udevice *dev)
  29. {
  30. struct x86_sysreset_platdata *plat = dev_get_platdata(dev);
  31. struct pch_pmbase_info pm;
  32. u32 reg32;
  33. int ret;
  34. if (!plat->pch)
  35. return -ENOENT;
  36. ret = pch_ioctl(plat->pch, PCH_REQ_PMBASE_INFO, &pm, sizeof(pm));
  37. if (ret)
  38. return ret;
  39. /*
  40. * Mask interrupts or system might stay in a coma, not executing code
  41. * anymore, but not powered off either.
  42. */
  43. asm("cli");
  44. /*
  45. * Avoid any GPI waking the system from S5* or the system might stay in
  46. * a coma
  47. */
  48. outl(0x00000000, pm.base + pm.gpio0_en_ofs);
  49. /* Clear Power Button Status */
  50. outw(PWRBTN_STS, pm.base + pm.pm1_sts_ofs);
  51. /* PMBASE + 4, Bit 10-12, Sleeping Type, * set to 111 -> S5, soft_off */
  52. reg32 = inl(pm.base + pm.pm1_cnt_ofs);
  53. /* Set Sleeping Type to S5 (poweroff) */
  54. reg32 &= ~(SLP_EN | SLP_TYP);
  55. reg32 |= SLP_TYP_S5;
  56. outl(reg32, pm.base + pm.pm1_cnt_ofs);
  57. /* Now set the Sleep Enable bit */
  58. reg32 |= SLP_EN;
  59. outl(reg32, pm.base + pm.pm1_cnt_ofs);
  60. for (;;)
  61. asm("hlt");
  62. }
  63. static int x86_sysreset_request(struct udevice *dev, enum sysreset_t type)
  64. {
  65. int value;
  66. int ret;
  67. switch (type) {
  68. case SYSRESET_WARM:
  69. value = SYS_RST | RST_CPU;
  70. break;
  71. case SYSRESET_COLD:
  72. value = SYS_RST | RST_CPU | FULL_RST;
  73. break;
  74. case SYSRESET_POWER_OFF:
  75. ret = pch_sysreset_power_off(dev);
  76. if (ret)
  77. return ret;
  78. return -EINPROGRESS;
  79. default:
  80. return -ENOSYS;
  81. }
  82. outb(value, IO_PORT_RESET);
  83. return -EINPROGRESS;
  84. }
  85. static int x86_sysreset_get_last(struct udevice *dev)
  86. {
  87. return SYSRESET_POWER;
  88. }
  89. #ifdef CONFIG_EFI_LOADER
  90. void __efi_runtime EFIAPI efi_reset_system(
  91. enum efi_reset_type reset_type,
  92. efi_status_t reset_status,
  93. unsigned long data_size, void *reset_data)
  94. {
  95. int value;
  96. /*
  97. * inline this code since we are not caused in the context of a
  98. * udevice and passing NULL to x86_sysreset_request() is too horrible.
  99. */
  100. if (reset_type == EFI_RESET_COLD ||
  101. reset_type == EFI_RESET_PLATFORM_SPECIFIC)
  102. value = SYS_RST | RST_CPU | FULL_RST;
  103. else /* assume EFI_RESET_WARM since we cannot return an error */
  104. value = SYS_RST | RST_CPU;
  105. outb(value, IO_PORT_RESET);
  106. /* TODO EFI_RESET_SHUTDOWN */
  107. while (1) { }
  108. }
  109. #endif
  110. static int x86_sysreset_probe(struct udevice *dev)
  111. {
  112. struct x86_sysreset_platdata *plat = dev_get_platdata(dev);
  113. /* Locate the PCH if there is one. It isn't essential */
  114. uclass_first_device(UCLASS_PCH, &plat->pch);
  115. return 0;
  116. }
  117. static const struct udevice_id x86_sysreset_ids[] = {
  118. { .compatible = "x86,reset" },
  119. { }
  120. };
  121. static struct sysreset_ops x86_sysreset_ops = {
  122. .request = x86_sysreset_request,
  123. .get_last = x86_sysreset_get_last,
  124. };
  125. U_BOOT_DRIVER(x86_reset) = {
  126. .name = "x86_reset",
  127. .id = UCLASS_SYSRESET,
  128. .of_match = x86_sysreset_ids,
  129. .ops = &x86_sysreset_ops,
  130. .probe = x86_sysreset_probe,
  131. .platdata_auto_alloc_size = sizeof(struct x86_sysreset_platdata),
  132. };