18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2016 Imagination Technologies
48c2ecf20Sopenharmony_ci * Author: Paul Burton <paul.burton@mips.com>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/delay.h>
88c2ecf20Sopenharmony_ci#include <linux/io.h>
98c2ecf20Sopenharmony_ci#include <linux/module.h>
108c2ecf20Sopenharmony_ci#include <linux/pci.h>
118c2ecf20Sopenharmony_ci#include <linux/pm.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistatic struct pci_dev *pm_dev;
148c2ecf20Sopenharmony_cistatic resource_size_t io_offset;
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cienum piix4_pm_io_reg {
178c2ecf20Sopenharmony_ci	PIIX4_FUNC3IO_PMSTS			= 0x00,
188c2ecf20Sopenharmony_ci#define PIIX4_FUNC3IO_PMSTS_PWRBTN_STS		BIT(8)
198c2ecf20Sopenharmony_ci	PIIX4_FUNC3IO_PMCNTRL			= 0x04,
208c2ecf20Sopenharmony_ci#define PIIX4_FUNC3IO_PMCNTRL_SUS_EN		BIT(13)
218c2ecf20Sopenharmony_ci#define PIIX4_FUNC3IO_PMCNTRL_SUS_TYP_SOFF	(0x0 << 10)
228c2ecf20Sopenharmony_ci};
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define PIIX4_SUSPEND_MAGIC			0x00120002
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic const int piix4_pm_io_region = PCI_BRIDGE_RESOURCES;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic void piix4_poweroff(void)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	int spec_devid;
318c2ecf20Sopenharmony_ci	u16 sts;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	/* Ensure the power button status is clear */
348c2ecf20Sopenharmony_ci	while (1) {
358c2ecf20Sopenharmony_ci		sts = inw(io_offset + PIIX4_FUNC3IO_PMSTS);
368c2ecf20Sopenharmony_ci		if (!(sts & PIIX4_FUNC3IO_PMSTS_PWRBTN_STS))
378c2ecf20Sopenharmony_ci			break;
388c2ecf20Sopenharmony_ci		outw(sts, io_offset + PIIX4_FUNC3IO_PMSTS);
398c2ecf20Sopenharmony_ci	}
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	/* Enable entry to suspend */
428c2ecf20Sopenharmony_ci	outw(PIIX4_FUNC3IO_PMCNTRL_SUS_TYP_SOFF | PIIX4_FUNC3IO_PMCNTRL_SUS_EN,
438c2ecf20Sopenharmony_ci	     io_offset + PIIX4_FUNC3IO_PMCNTRL);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	/* If the special cycle occurs too soon this doesn't work... */
468c2ecf20Sopenharmony_ci	mdelay(10);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	/*
498c2ecf20Sopenharmony_ci	 * The PIIX4 will enter the suspend state only after seeing a special
508c2ecf20Sopenharmony_ci	 * cycle with the correct magic data on the PCI bus. Generate that
518c2ecf20Sopenharmony_ci	 * cycle now.
528c2ecf20Sopenharmony_ci	 */
538c2ecf20Sopenharmony_ci	spec_devid = PCI_DEVID(0, PCI_DEVFN(0x1f, 0x7));
548c2ecf20Sopenharmony_ci	pci_bus_write_config_dword(pm_dev->bus, spec_devid, 0,
558c2ecf20Sopenharmony_ci				   PIIX4_SUSPEND_MAGIC);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	/* Give the system some time to power down, then error */
588c2ecf20Sopenharmony_ci	mdelay(1000);
598c2ecf20Sopenharmony_ci	pr_emerg("Unable to poweroff system\n");
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic int piix4_poweroff_probe(struct pci_dev *dev,
638c2ecf20Sopenharmony_ci				const struct pci_device_id *id)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	int res;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	if (pm_dev)
688c2ecf20Sopenharmony_ci		return -EINVAL;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	/* Request access to the PIIX4 PM IO registers */
718c2ecf20Sopenharmony_ci	res = pci_request_region(dev, piix4_pm_io_region,
728c2ecf20Sopenharmony_ci				 "PIIX4 PM IO registers");
738c2ecf20Sopenharmony_ci	if (res) {
748c2ecf20Sopenharmony_ci		dev_err(&dev->dev, "failed to request PM IO registers: %d\n",
758c2ecf20Sopenharmony_ci			res);
768c2ecf20Sopenharmony_ci		return res;
778c2ecf20Sopenharmony_ci	}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	pm_dev = dev;
808c2ecf20Sopenharmony_ci	io_offset = pci_resource_start(dev, piix4_pm_io_region);
818c2ecf20Sopenharmony_ci	pm_power_off = piix4_poweroff;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	return 0;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic void piix4_poweroff_remove(struct pci_dev *dev)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	if (pm_power_off == piix4_poweroff)
898c2ecf20Sopenharmony_ci		pm_power_off = NULL;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	pci_release_region(dev, piix4_pm_io_region);
928c2ecf20Sopenharmony_ci	pm_dev = NULL;
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic const struct pci_device_id piix4_poweroff_ids[] = {
968c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3) },
978c2ecf20Sopenharmony_ci	{ 0 },
988c2ecf20Sopenharmony_ci};
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic struct pci_driver piix4_poweroff_driver = {
1018c2ecf20Sopenharmony_ci	.name		= "piix4-poweroff",
1028c2ecf20Sopenharmony_ci	.id_table	= piix4_poweroff_ids,
1038c2ecf20Sopenharmony_ci	.probe		= piix4_poweroff_probe,
1048c2ecf20Sopenharmony_ci	.remove		= piix4_poweroff_remove,
1058c2ecf20Sopenharmony_ci};
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cimodule_pci_driver(piix4_poweroff_driver);
1088c2ecf20Sopenharmony_ciMODULE_AUTHOR("Paul Burton <paul.burton@mips.com>");
1098c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
110