piix4-poweroff.c 2.8 KB

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