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