1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
229676833SPaul Burton /*
329676833SPaul Burton * Copyright (C) 2016 Imagination Technologies
4fb615d61SPaul Burton * Author: Paul Burton <paul.burton@mips.com>
529676833SPaul Burton */
629676833SPaul Burton
729676833SPaul Burton #include <linux/delay.h>
829676833SPaul Burton #include <linux/io.h>
929676833SPaul Burton #include <linux/module.h>
1029676833SPaul Burton #include <linux/pci.h>
1129676833SPaul Burton #include <linux/pm.h>
1229676833SPaul Burton
1329676833SPaul Burton static struct pci_dev *pm_dev;
1429676833SPaul Burton static resource_size_t io_offset;
1529676833SPaul Burton
1629676833SPaul Burton enum piix4_pm_io_reg {
1729676833SPaul Burton PIIX4_FUNC3IO_PMSTS = 0x00,
1829676833SPaul Burton #define PIIX4_FUNC3IO_PMSTS_PWRBTN_STS BIT(8)
1929676833SPaul Burton PIIX4_FUNC3IO_PMCNTRL = 0x04,
2029676833SPaul Burton #define PIIX4_FUNC3IO_PMCNTRL_SUS_EN BIT(13)
2129676833SPaul Burton #define PIIX4_FUNC3IO_PMCNTRL_SUS_TYP_SOFF (0x0 << 10)
2229676833SPaul Burton };
2329676833SPaul Burton
2429676833SPaul Burton #define PIIX4_SUSPEND_MAGIC 0x00120002
2529676833SPaul Burton
2629676833SPaul Burton static const int piix4_pm_io_region = PCI_BRIDGE_RESOURCES;
2729676833SPaul Burton
piix4_poweroff(void)2829676833SPaul Burton static void piix4_poweroff(void)
2929676833SPaul Burton {
3029676833SPaul Burton int spec_devid;
3129676833SPaul Burton u16 sts;
3229676833SPaul Burton
3329676833SPaul Burton /* Ensure the power button status is clear */
3429676833SPaul Burton while (1) {
3529676833SPaul Burton sts = inw(io_offset + PIIX4_FUNC3IO_PMSTS);
3629676833SPaul Burton if (!(sts & PIIX4_FUNC3IO_PMSTS_PWRBTN_STS))
3729676833SPaul Burton break;
3829676833SPaul Burton outw(sts, io_offset + PIIX4_FUNC3IO_PMSTS);
3929676833SPaul Burton }
4029676833SPaul Burton
4129676833SPaul Burton /* Enable entry to suspend */
4229676833SPaul Burton outw(PIIX4_FUNC3IO_PMCNTRL_SUS_TYP_SOFF | PIIX4_FUNC3IO_PMCNTRL_SUS_EN,
4329676833SPaul Burton io_offset + PIIX4_FUNC3IO_PMCNTRL);
4429676833SPaul Burton
4529676833SPaul Burton /* If the special cycle occurs too soon this doesn't work... */
4629676833SPaul Burton mdelay(10);
4729676833SPaul Burton
4829676833SPaul Burton /*
4929676833SPaul Burton * The PIIX4 will enter the suspend state only after seeing a special
5029676833SPaul Burton * cycle with the correct magic data on the PCI bus. Generate that
5129676833SPaul Burton * cycle now.
5229676833SPaul Burton */
5329676833SPaul Burton spec_devid = PCI_DEVID(0, PCI_DEVFN(0x1f, 0x7));
5429676833SPaul Burton pci_bus_write_config_dword(pm_dev->bus, spec_devid, 0,
5529676833SPaul Burton PIIX4_SUSPEND_MAGIC);
5629676833SPaul Burton
5729676833SPaul Burton /* Give the system some time to power down, then error */
5829676833SPaul Burton mdelay(1000);
5929676833SPaul Burton pr_emerg("Unable to poweroff system\n");
6029676833SPaul Burton }
6129676833SPaul Burton
piix4_poweroff_probe(struct pci_dev * dev,const struct pci_device_id * id)6229676833SPaul Burton static int piix4_poweroff_probe(struct pci_dev *dev,
6329676833SPaul Burton const struct pci_device_id *id)
6429676833SPaul Burton {
6529676833SPaul Burton int res;
6629676833SPaul Burton
6729676833SPaul Burton if (pm_dev)
6829676833SPaul Burton return -EINVAL;
6929676833SPaul Burton
7029676833SPaul Burton /* Request access to the PIIX4 PM IO registers */
7129676833SPaul Burton res = pci_request_region(dev, piix4_pm_io_region,
7229676833SPaul Burton "PIIX4 PM IO registers");
7329676833SPaul Burton if (res) {
7429676833SPaul Burton dev_err(&dev->dev, "failed to request PM IO registers: %d\n",
7529676833SPaul Burton res);
7629676833SPaul Burton return res;
7729676833SPaul Burton }
7829676833SPaul Burton
7929676833SPaul Burton pm_dev = dev;
8029676833SPaul Burton io_offset = pci_resource_start(dev, piix4_pm_io_region);
8129676833SPaul Burton pm_power_off = piix4_poweroff;
8229676833SPaul Burton
8329676833SPaul Burton return 0;
8429676833SPaul Burton }
8529676833SPaul Burton
piix4_poweroff_remove(struct pci_dev * dev)8629676833SPaul Burton static void piix4_poweroff_remove(struct pci_dev *dev)
8729676833SPaul Burton {
8829676833SPaul Burton if (pm_power_off == piix4_poweroff)
8929676833SPaul Burton pm_power_off = NULL;
9029676833SPaul Burton
9129676833SPaul Burton pci_release_region(dev, piix4_pm_io_region);
9229676833SPaul Burton pm_dev = NULL;
9329676833SPaul Burton }
9429676833SPaul Burton
9529676833SPaul Burton static const struct pci_device_id piix4_poweroff_ids[] = {
9629676833SPaul Burton { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3) },
9729676833SPaul Burton { 0 },
9829676833SPaul Burton };
9929676833SPaul Burton
10029676833SPaul Burton static struct pci_driver piix4_poweroff_driver = {
10129676833SPaul Burton .name = "piix4-poweroff",
10229676833SPaul Burton .id_table = piix4_poweroff_ids,
10329676833SPaul Burton .probe = piix4_poweroff_probe,
10429676833SPaul Burton .remove = piix4_poweroff_remove,
10529676833SPaul Burton };
10629676833SPaul Burton
10729676833SPaul Burton module_pci_driver(piix4_poweroff_driver);
108fb615d61SPaul Burton MODULE_AUTHOR("Paul Burton <paul.burton@mips.com>");
10929676833SPaul Burton MODULE_LICENSE("GPL");
110