piix4-poweroff.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2016 Imagination Technologies
  4. * Author: Paul Burton <paul.burton@mips.com>
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/io.h>
  8. #include <linux/module.h>
  9. #include <linux/pci.h>
  10. #include <linux/pm.h>
  11. static struct pci_dev *pm_dev;
  12. static resource_size_t io_offset;
  13. enum piix4_pm_io_reg {
  14. PIIX4_FUNC3IO_PMSTS = 0x00,
  15. #define PIIX4_FUNC3IO_PMSTS_PWRBTN_STS BIT(8)
  16. PIIX4_FUNC3IO_PMCNTRL = 0x04,
  17. #define PIIX4_FUNC3IO_PMCNTRL_SUS_EN BIT(13)
  18. #define PIIX4_FUNC3IO_PMCNTRL_SUS_TYP_SOFF (0x0 << 10)
  19. };
  20. #define PIIX4_SUSPEND_MAGIC 0x00120002
  21. static const int piix4_pm_io_region = PCI_BRIDGE_RESOURCES;
  22. static void piix4_poweroff(void)
  23. {
  24. int spec_devid;
  25. u16 sts;
  26. /* Ensure the power button status is clear */
  27. while (1) {
  28. sts = inw(io_offset + PIIX4_FUNC3IO_PMSTS);
  29. if (!(sts & PIIX4_FUNC3IO_PMSTS_PWRBTN_STS))
  30. break;
  31. outw(sts, io_offset + PIIX4_FUNC3IO_PMSTS);
  32. }
  33. /* Enable entry to suspend */
  34. outw(PIIX4_FUNC3IO_PMCNTRL_SUS_TYP_SOFF | PIIX4_FUNC3IO_PMCNTRL_SUS_EN,
  35. io_offset + PIIX4_FUNC3IO_PMCNTRL);
  36. /* If the special cycle occurs too soon this doesn't work... */
  37. mdelay(10);
  38. /*
  39. * The PIIX4 will enter the suspend state only after seeing a special
  40. * cycle with the correct magic data on the PCI bus. Generate that
  41. * cycle now.
  42. */
  43. spec_devid = PCI_DEVID(0, PCI_DEVFN(0x1f, 0x7));
  44. pci_bus_write_config_dword(pm_dev->bus, spec_devid, 0,
  45. PIIX4_SUSPEND_MAGIC);
  46. /* Give the system some time to power down, then error */
  47. mdelay(1000);
  48. pr_emerg("Unable to poweroff system\n");
  49. }
  50. static int piix4_poweroff_probe(struct pci_dev *dev,
  51. const struct pci_device_id *id)
  52. {
  53. int res;
  54. if (pm_dev)
  55. return -EINVAL;
  56. /* Request access to the PIIX4 PM IO registers */
  57. res = pci_request_region(dev, piix4_pm_io_region,
  58. "PIIX4 PM IO registers");
  59. if (res) {
  60. dev_err(&dev->dev, "failed to request PM IO registers: %d\n",
  61. res);
  62. return res;
  63. }
  64. pm_dev = dev;
  65. io_offset = pci_resource_start(dev, piix4_pm_io_region);
  66. pm_power_off = piix4_poweroff;
  67. return 0;
  68. }
  69. static void piix4_poweroff_remove(struct pci_dev *dev)
  70. {
  71. if (pm_power_off == piix4_poweroff)
  72. pm_power_off = NULL;
  73. pci_release_region(dev, piix4_pm_io_region);
  74. pm_dev = NULL;
  75. }
  76. static const struct pci_device_id piix4_poweroff_ids[] = {
  77. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3) },
  78. { 0 },
  79. };
  80. static struct pci_driver piix4_poweroff_driver = {
  81. .name = "piix4-poweroff",
  82. .id_table = piix4_poweroff_ids,
  83. .probe = piix4_poweroff_probe,
  84. .remove = piix4_poweroff_remove,
  85. };
  86. module_pci_driver(piix4_poweroff_driver);
  87. MODULE_AUTHOR("Paul Burton <paul.burton@mips.com>");
  88. MODULE_LICENSE("GPL");