Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright (C) 2016 Imagination Technologies
0004  * Author: Paul Burton <paul.burton@mips.com>
0005  */
0006 
0007 #include <linux/delay.h>
0008 #include <linux/io.h>
0009 #include <linux/module.h>
0010 #include <linux/pci.h>
0011 #include <linux/pm.h>
0012 
0013 static struct pci_dev *pm_dev;
0014 static resource_size_t io_offset;
0015 
0016 enum piix4_pm_io_reg {
0017     PIIX4_FUNC3IO_PMSTS         = 0x00,
0018 #define PIIX4_FUNC3IO_PMSTS_PWRBTN_STS      BIT(8)
0019     PIIX4_FUNC3IO_PMCNTRL           = 0x04,
0020 #define PIIX4_FUNC3IO_PMCNTRL_SUS_EN        BIT(13)
0021 #define PIIX4_FUNC3IO_PMCNTRL_SUS_TYP_SOFF  (0x0 << 10)
0022 };
0023 
0024 #define PIIX4_SUSPEND_MAGIC         0x00120002
0025 
0026 static const int piix4_pm_io_region = PCI_BRIDGE_RESOURCES;
0027 
0028 static void piix4_poweroff(void)
0029 {
0030     int spec_devid;
0031     u16 sts;
0032 
0033     /* Ensure the power button status is clear */
0034     while (1) {
0035         sts = inw(io_offset + PIIX4_FUNC3IO_PMSTS);
0036         if (!(sts & PIIX4_FUNC3IO_PMSTS_PWRBTN_STS))
0037             break;
0038         outw(sts, io_offset + PIIX4_FUNC3IO_PMSTS);
0039     }
0040 
0041     /* Enable entry to suspend */
0042     outw(PIIX4_FUNC3IO_PMCNTRL_SUS_TYP_SOFF | PIIX4_FUNC3IO_PMCNTRL_SUS_EN,
0043          io_offset + PIIX4_FUNC3IO_PMCNTRL);
0044 
0045     /* If the special cycle occurs too soon this doesn't work... */
0046     mdelay(10);
0047 
0048     /*
0049      * The PIIX4 will enter the suspend state only after seeing a special
0050      * cycle with the correct magic data on the PCI bus. Generate that
0051      * cycle now.
0052      */
0053     spec_devid = PCI_DEVID(0, PCI_DEVFN(0x1f, 0x7));
0054     pci_bus_write_config_dword(pm_dev->bus, spec_devid, 0,
0055                    PIIX4_SUSPEND_MAGIC);
0056 
0057     /* Give the system some time to power down, then error */
0058     mdelay(1000);
0059     pr_emerg("Unable to poweroff system\n");
0060 }
0061 
0062 static int piix4_poweroff_probe(struct pci_dev *dev,
0063                 const struct pci_device_id *id)
0064 {
0065     int res;
0066 
0067     if (pm_dev)
0068         return -EINVAL;
0069 
0070     /* Request access to the PIIX4 PM IO registers */
0071     res = pci_request_region(dev, piix4_pm_io_region,
0072                  "PIIX4 PM IO registers");
0073     if (res) {
0074         dev_err(&dev->dev, "failed to request PM IO registers: %d\n",
0075             res);
0076         return res;
0077     }
0078 
0079     pm_dev = dev;
0080     io_offset = pci_resource_start(dev, piix4_pm_io_region);
0081     pm_power_off = piix4_poweroff;
0082 
0083     return 0;
0084 }
0085 
0086 static void piix4_poweroff_remove(struct pci_dev *dev)
0087 {
0088     if (pm_power_off == piix4_poweroff)
0089         pm_power_off = NULL;
0090 
0091     pci_release_region(dev, piix4_pm_io_region);
0092     pm_dev = NULL;
0093 }
0094 
0095 static const struct pci_device_id piix4_poweroff_ids[] = {
0096     { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3) },
0097     { 0 },
0098 };
0099 
0100 static struct pci_driver piix4_poweroff_driver = {
0101     .name       = "piix4-poweroff",
0102     .id_table   = piix4_poweroff_ids,
0103     .probe      = piix4_poweroff_probe,
0104     .remove     = piix4_poweroff_remove,
0105 };
0106 
0107 module_pci_driver(piix4_poweroff_driver);
0108 MODULE_AUTHOR("Paul Burton <paul.burton@mips.com>");
0109 MODULE_LICENSE("GPL");