xref: /openbmc/linux/arch/x86/platform/intel-mid/pwr.c (revision f43ea76cf310c3be95cb75ae1350cbe76a8f2380)
15823d089SAndy Shevchenko /*
25823d089SAndy Shevchenko  * Intel MID Power Management Unit (PWRMU) device driver
35823d089SAndy Shevchenko  *
45823d089SAndy Shevchenko  * Copyright (C) 2016, Intel Corporation
55823d089SAndy Shevchenko  *
65823d089SAndy Shevchenko  * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
75823d089SAndy Shevchenko  *
85823d089SAndy Shevchenko  * This program is free software; you can redistribute it and/or modify it
95823d089SAndy Shevchenko  * under the terms and conditions of the GNU General Public License,
105823d089SAndy Shevchenko  * version 2, as published by the Free Software Foundation.
115823d089SAndy Shevchenko  *
125823d089SAndy Shevchenko  * Intel MID Power Management Unit device driver handles the South Complex PCI
135823d089SAndy Shevchenko  * devices such as GPDMA, SPI, I2C, PWM, and so on. By default PCI core
145823d089SAndy Shevchenko  * modifies bits in PMCSR register in the PCI configuration space. This is not
155823d089SAndy Shevchenko  * enough on some SoCs like Intel Tangier. In such case PCI core sets a new
165823d089SAndy Shevchenko  * power state of the device in question through a PM hook registered in struct
175823d089SAndy Shevchenko  * pci_platform_pm_ops (see drivers/pci/pci-mid.c).
185823d089SAndy Shevchenko  */
195823d089SAndy Shevchenko 
205823d089SAndy Shevchenko #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
215823d089SAndy Shevchenko 
225823d089SAndy Shevchenko #include <linux/delay.h>
235823d089SAndy Shevchenko #include <linux/errno.h>
245823d089SAndy Shevchenko #include <linux/interrupt.h>
255823d089SAndy Shevchenko #include <linux/kernel.h>
26cc3ae7b0SPaul Gortmaker #include <linux/export.h>
275823d089SAndy Shevchenko #include <linux/mutex.h>
285823d089SAndy Shevchenko #include <linux/pci.h>
295823d089SAndy Shevchenko 
305823d089SAndy Shevchenko #include <asm/intel-mid.h>
315823d089SAndy Shevchenko 
325823d089SAndy Shevchenko /* Registers */
335823d089SAndy Shevchenko #define PM_STS			0x00
345823d089SAndy Shevchenko #define PM_CMD			0x04
355823d089SAndy Shevchenko #define PM_ICS			0x08
365823d089SAndy Shevchenko #define PM_WKC(x)		(0x10 + (x) * 4)
375823d089SAndy Shevchenko #define PM_WKS(x)		(0x18 + (x) * 4)
385823d089SAndy Shevchenko #define PM_SSC(x)		(0x20 + (x) * 4)
395823d089SAndy Shevchenko #define PM_SSS(x)		(0x30 + (x) * 4)
405823d089SAndy Shevchenko 
415823d089SAndy Shevchenko /* Bits in PM_STS */
425823d089SAndy Shevchenko #define PM_STS_BUSY		(1 << 8)
435823d089SAndy Shevchenko 
445823d089SAndy Shevchenko /* Bits in PM_CMD */
455823d089SAndy Shevchenko #define PM_CMD_CMD(x)		((x) << 0)
465823d089SAndy Shevchenko #define PM_CMD_IOC		(1 << 8)
4770b5b18fSAndy Shevchenko #define PM_CMD_CM_NOP		(0 << 9)
4870b5b18fSAndy Shevchenko #define PM_CMD_CM_IMMEDIATE	(1 << 9)
4970b5b18fSAndy Shevchenko #define PM_CMD_CM_DELAY		(2 << 9)
5070b5b18fSAndy Shevchenko #define PM_CMD_CM_TRIGGER	(3 << 9)
51bda7b072SAndy Shevchenko 
52bda7b072SAndy Shevchenko /* System states */
53bda7b072SAndy Shevchenko #define PM_CMD_SYS_STATE_S5	(5 << 16)
54bda7b072SAndy Shevchenko 
55bda7b072SAndy Shevchenko /* Trigger variants */
56bda7b072SAndy Shevchenko #define PM_CMD_CFG_TRIGGER_NC	(3 << 19)
57bda7b072SAndy Shevchenko 
58bda7b072SAndy Shevchenko /* Message to wait for TRIGGER_NC case */
59bda7b072SAndy Shevchenko #define TRIGGER_NC_MSG_2	(2 << 22)
605823d089SAndy Shevchenko 
615823d089SAndy Shevchenko /* List of commands */
625823d089SAndy Shevchenko #define CMD_SET_CFG		0x01
635823d089SAndy Shevchenko 
645823d089SAndy Shevchenko /* Bits in PM_ICS */
655823d089SAndy Shevchenko #define PM_ICS_INT_STATUS(x)	((x) & 0xff)
665823d089SAndy Shevchenko #define PM_ICS_IE		(1 << 8)
675823d089SAndy Shevchenko #define PM_ICS_IP		(1 << 9)
685823d089SAndy Shevchenko #define PM_ICS_SW_INT_STS	(1 << 10)
695823d089SAndy Shevchenko 
705823d089SAndy Shevchenko /* List of interrupts */
715823d089SAndy Shevchenko #define INT_INVALID		0
725823d089SAndy Shevchenko #define INT_CMD_COMPLETE	1
735823d089SAndy Shevchenko #define INT_CMD_ERR		2
745823d089SAndy Shevchenko #define INT_WAKE_EVENT		3
755823d089SAndy Shevchenko #define INT_LSS_POWER_ERR	4
765823d089SAndy Shevchenko #define INT_S0iX_MSG_ERR	5
775823d089SAndy Shevchenko #define INT_NO_C6		6
785823d089SAndy Shevchenko #define INT_TRIGGER_ERR		7
795823d089SAndy Shevchenko #define INT_INACTIVITY		8
805823d089SAndy Shevchenko 
815823d089SAndy Shevchenko /* South Complex devices */
825823d089SAndy Shevchenko #define LSS_MAX_SHARED_DEVS	4
835823d089SAndy Shevchenko #define LSS_MAX_DEVS		64
845823d089SAndy Shevchenko 
855823d089SAndy Shevchenko #define LSS_WS_BITS		1	/* wake state width */
865823d089SAndy Shevchenko #define LSS_PWS_BITS		2	/* power state width */
875823d089SAndy Shevchenko 
885823d089SAndy Shevchenko /* Supported device IDs */
89ca22312dSAndy Shevchenko #define PCI_DEVICE_ID_PENWELL	0x0828
905823d089SAndy Shevchenko #define PCI_DEVICE_ID_TANGIER	0x11a1
915823d089SAndy Shevchenko 
925823d089SAndy Shevchenko struct mid_pwr_dev {
935823d089SAndy Shevchenko 	struct pci_dev *pdev;
945823d089SAndy Shevchenko 	pci_power_t state;
955823d089SAndy Shevchenko };
965823d089SAndy Shevchenko 
975823d089SAndy Shevchenko struct mid_pwr {
985823d089SAndy Shevchenko 	struct device *dev;
995823d089SAndy Shevchenko 	void __iomem *regs;
1005823d089SAndy Shevchenko 	int irq;
1015823d089SAndy Shevchenko 	bool available;
1025823d089SAndy Shevchenko 
1035823d089SAndy Shevchenko 	struct mutex lock;
1045823d089SAndy Shevchenko 	struct mid_pwr_dev lss[LSS_MAX_DEVS][LSS_MAX_SHARED_DEVS];
1055823d089SAndy Shevchenko };
1065823d089SAndy Shevchenko 
1075823d089SAndy Shevchenko static struct mid_pwr *midpwr;
1085823d089SAndy Shevchenko 
1095823d089SAndy Shevchenko static u32 mid_pwr_get_state(struct mid_pwr *pwr, int reg)
1105823d089SAndy Shevchenko {
1115823d089SAndy Shevchenko 	return readl(pwr->regs + PM_SSS(reg));
1125823d089SAndy Shevchenko }
1135823d089SAndy Shevchenko 
1145823d089SAndy Shevchenko static void mid_pwr_set_state(struct mid_pwr *pwr, int reg, u32 value)
1155823d089SAndy Shevchenko {
1165823d089SAndy Shevchenko 	writel(value, pwr->regs + PM_SSC(reg));
1175823d089SAndy Shevchenko }
1185823d089SAndy Shevchenko 
1195823d089SAndy Shevchenko static void mid_pwr_set_wake(struct mid_pwr *pwr, int reg, u32 value)
1205823d089SAndy Shevchenko {
1215823d089SAndy Shevchenko 	writel(value, pwr->regs + PM_WKC(reg));
1225823d089SAndy Shevchenko }
1235823d089SAndy Shevchenko 
1245823d089SAndy Shevchenko static void mid_pwr_interrupt_disable(struct mid_pwr *pwr)
1255823d089SAndy Shevchenko {
1265823d089SAndy Shevchenko 	writel(~PM_ICS_IE, pwr->regs + PM_ICS);
1275823d089SAndy Shevchenko }
1285823d089SAndy Shevchenko 
1295823d089SAndy Shevchenko static bool mid_pwr_is_busy(struct mid_pwr *pwr)
1305823d089SAndy Shevchenko {
1315823d089SAndy Shevchenko 	return !!(readl(pwr->regs + PM_STS) & PM_STS_BUSY);
1325823d089SAndy Shevchenko }
1335823d089SAndy Shevchenko 
1345823d089SAndy Shevchenko /* Wait 500ms that the latest PWRMU command finished */
1355823d089SAndy Shevchenko static int mid_pwr_wait(struct mid_pwr *pwr)
1365823d089SAndy Shevchenko {
1375823d089SAndy Shevchenko 	unsigned int count = 500000;
1385823d089SAndy Shevchenko 	bool busy;
1395823d089SAndy Shevchenko 
1405823d089SAndy Shevchenko 	do {
1415823d089SAndy Shevchenko 		busy = mid_pwr_is_busy(pwr);
1425823d089SAndy Shevchenko 		if (!busy)
1435823d089SAndy Shevchenko 			return 0;
1445823d089SAndy Shevchenko 		udelay(1);
1455823d089SAndy Shevchenko 	} while (--count);
1465823d089SAndy Shevchenko 
1475823d089SAndy Shevchenko 	return -EBUSY;
1485823d089SAndy Shevchenko }
1495823d089SAndy Shevchenko 
1505823d089SAndy Shevchenko static int mid_pwr_wait_for_cmd(struct mid_pwr *pwr, u8 cmd)
1515823d089SAndy Shevchenko {
15270b5b18fSAndy Shevchenko 	writel(PM_CMD_CMD(cmd) | PM_CMD_CM_IMMEDIATE, pwr->regs + PM_CMD);
1535823d089SAndy Shevchenko 	return mid_pwr_wait(pwr);
1545823d089SAndy Shevchenko }
1555823d089SAndy Shevchenko 
1565823d089SAndy Shevchenko static int __update_power_state(struct mid_pwr *pwr, int reg, int bit, int new)
1575823d089SAndy Shevchenko {
1585823d089SAndy Shevchenko 	int curstate;
1595823d089SAndy Shevchenko 	u32 power;
1605823d089SAndy Shevchenko 	int ret;
1615823d089SAndy Shevchenko 
1625823d089SAndy Shevchenko 	/* Check if the device is already in desired state */
1635823d089SAndy Shevchenko 	power = mid_pwr_get_state(pwr, reg);
1645823d089SAndy Shevchenko 	curstate = (power >> bit) & 3;
1655823d089SAndy Shevchenko 	if (curstate == new)
1665823d089SAndy Shevchenko 		return 0;
1675823d089SAndy Shevchenko 
1685823d089SAndy Shevchenko 	/* Update the power state */
1695823d089SAndy Shevchenko 	mid_pwr_set_state(pwr, reg, (power & ~(3 << bit)) | (new << bit));
1705823d089SAndy Shevchenko 
1715823d089SAndy Shevchenko 	/* Send command to SCU */
1725823d089SAndy Shevchenko 	ret = mid_pwr_wait_for_cmd(pwr, CMD_SET_CFG);
1735823d089SAndy Shevchenko 	if (ret)
1745823d089SAndy Shevchenko 		return ret;
1755823d089SAndy Shevchenko 
1765823d089SAndy Shevchenko 	/* Check if the device is already in desired state */
1775823d089SAndy Shevchenko 	power = mid_pwr_get_state(pwr, reg);
1785823d089SAndy Shevchenko 	curstate = (power >> bit) & 3;
1795823d089SAndy Shevchenko 	if (curstate != new)
1805823d089SAndy Shevchenko 		return -EAGAIN;
1815823d089SAndy Shevchenko 
1825823d089SAndy Shevchenko 	return 0;
1835823d089SAndy Shevchenko }
1845823d089SAndy Shevchenko 
1855823d089SAndy Shevchenko static pci_power_t __find_weakest_power_state(struct mid_pwr_dev *lss,
1865823d089SAndy Shevchenko 					      struct pci_dev *pdev,
1875823d089SAndy Shevchenko 					      pci_power_t state)
1885823d089SAndy Shevchenko {
1895823d089SAndy Shevchenko 	pci_power_t weakest = PCI_D3hot;
1905823d089SAndy Shevchenko 	unsigned int j;
1915823d089SAndy Shevchenko 
1925823d089SAndy Shevchenko 	/* Find device in cache or first free cell */
1935823d089SAndy Shevchenko 	for (j = 0; j < LSS_MAX_SHARED_DEVS; j++) {
1945823d089SAndy Shevchenko 		if (lss[j].pdev == pdev || !lss[j].pdev)
1955823d089SAndy Shevchenko 			break;
1965823d089SAndy Shevchenko 	}
1975823d089SAndy Shevchenko 
1985823d089SAndy Shevchenko 	/* Store the desired state in cache */
1995823d089SAndy Shevchenko 	if (j < LSS_MAX_SHARED_DEVS) {
2005823d089SAndy Shevchenko 		lss[j].pdev = pdev;
2015823d089SAndy Shevchenko 		lss[j].state = state;
2025823d089SAndy Shevchenko 	} else {
2035823d089SAndy Shevchenko 		dev_WARN(&pdev->dev, "No room for device in PWRMU LSS cache\n");
2045823d089SAndy Shevchenko 		weakest = state;
2055823d089SAndy Shevchenko 	}
2065823d089SAndy Shevchenko 
2075823d089SAndy Shevchenko 	/* Find the power state we may use */
2085823d089SAndy Shevchenko 	for (j = 0; j < LSS_MAX_SHARED_DEVS; j++) {
2095823d089SAndy Shevchenko 		if (lss[j].state < weakest)
2105823d089SAndy Shevchenko 			weakest = lss[j].state;
2115823d089SAndy Shevchenko 	}
2125823d089SAndy Shevchenko 
2135823d089SAndy Shevchenko 	return weakest;
2145823d089SAndy Shevchenko }
2155823d089SAndy Shevchenko 
2165823d089SAndy Shevchenko static int __set_power_state(struct mid_pwr *pwr, struct pci_dev *pdev,
2175823d089SAndy Shevchenko 			     pci_power_t state, int id, int reg, int bit)
2185823d089SAndy Shevchenko {
2195823d089SAndy Shevchenko 	const char *name;
2205823d089SAndy Shevchenko 	int ret;
2215823d089SAndy Shevchenko 
2225823d089SAndy Shevchenko 	state = __find_weakest_power_state(pwr->lss[id], pdev, state);
2235823d089SAndy Shevchenko 	name = pci_power_name(state);
2245823d089SAndy Shevchenko 
2255823d089SAndy Shevchenko 	ret = __update_power_state(pwr, reg, bit, (__force int)state);
2265823d089SAndy Shevchenko 	if (ret) {
2275823d089SAndy Shevchenko 		dev_warn(&pdev->dev, "Can't set power state %s: %d\n", name, ret);
2285823d089SAndy Shevchenko 		return ret;
2295823d089SAndy Shevchenko 	}
2305823d089SAndy Shevchenko 
2315823d089SAndy Shevchenko 	dev_vdbg(&pdev->dev, "Set power state %s\n", name);
2325823d089SAndy Shevchenko 	return 0;
2335823d089SAndy Shevchenko }
2345823d089SAndy Shevchenko 
2355823d089SAndy Shevchenko static int mid_pwr_set_power_state(struct mid_pwr *pwr, struct pci_dev *pdev,
2365823d089SAndy Shevchenko 				   pci_power_t state)
2375823d089SAndy Shevchenko {
2385823d089SAndy Shevchenko 	int id, reg, bit;
2395823d089SAndy Shevchenko 	int ret;
2405823d089SAndy Shevchenko 
2415823d089SAndy Shevchenko 	id = intel_mid_pwr_get_lss_id(pdev);
2425823d089SAndy Shevchenko 	if (id < 0)
2435823d089SAndy Shevchenko 		return id;
2445823d089SAndy Shevchenko 
2455823d089SAndy Shevchenko 	reg = (id * LSS_PWS_BITS) / 32;
2465823d089SAndy Shevchenko 	bit = (id * LSS_PWS_BITS) % 32;
2475823d089SAndy Shevchenko 
2485823d089SAndy Shevchenko 	/* We support states between PCI_D0 and PCI_D3hot */
2495823d089SAndy Shevchenko 	if (state < PCI_D0)
2505823d089SAndy Shevchenko 		state = PCI_D0;
2515823d089SAndy Shevchenko 	if (state > PCI_D3hot)
2525823d089SAndy Shevchenko 		state = PCI_D3hot;
2535823d089SAndy Shevchenko 
2545823d089SAndy Shevchenko 	mutex_lock(&pwr->lock);
2555823d089SAndy Shevchenko 	ret = __set_power_state(pwr, pdev, state, id, reg, bit);
2565823d089SAndy Shevchenko 	mutex_unlock(&pwr->lock);
2575823d089SAndy Shevchenko 	return ret;
2585823d089SAndy Shevchenko }
2595823d089SAndy Shevchenko 
2605823d089SAndy Shevchenko int intel_mid_pci_set_power_state(struct pci_dev *pdev, pci_power_t state)
2615823d089SAndy Shevchenko {
2625823d089SAndy Shevchenko 	struct mid_pwr *pwr = midpwr;
2635823d089SAndy Shevchenko 	int ret = 0;
2645823d089SAndy Shevchenko 
2655823d089SAndy Shevchenko 	might_sleep();
2665823d089SAndy Shevchenko 
2675823d089SAndy Shevchenko 	if (pwr && pwr->available)
2685823d089SAndy Shevchenko 		ret = mid_pwr_set_power_state(pwr, pdev, state);
2695823d089SAndy Shevchenko 	dev_vdbg(&pdev->dev, "set_power_state() returns %d\n", ret);
2705823d089SAndy Shevchenko 
2715823d089SAndy Shevchenko 	return 0;
2725823d089SAndy Shevchenko }
2735823d089SAndy Shevchenko EXPORT_SYMBOL_GPL(intel_mid_pci_set_power_state);
2745823d089SAndy Shevchenko 
275bda7b072SAndy Shevchenko void intel_mid_pwr_power_off(void)
276bda7b072SAndy Shevchenko {
277bda7b072SAndy Shevchenko 	struct mid_pwr *pwr = midpwr;
278bda7b072SAndy Shevchenko 	u32 cmd = PM_CMD_SYS_STATE_S5 |
279bda7b072SAndy Shevchenko 		  PM_CMD_CMD(CMD_SET_CFG) |
280bda7b072SAndy Shevchenko 		  PM_CMD_CM_TRIGGER |
281bda7b072SAndy Shevchenko 		  PM_CMD_CFG_TRIGGER_NC |
282bda7b072SAndy Shevchenko 		  TRIGGER_NC_MSG_2;
283bda7b072SAndy Shevchenko 
284bda7b072SAndy Shevchenko 	/* Send command to SCU */
285bda7b072SAndy Shevchenko 	writel(cmd, pwr->regs + PM_CMD);
286bda7b072SAndy Shevchenko 	mid_pwr_wait(pwr);
287bda7b072SAndy Shevchenko }
288bda7b072SAndy Shevchenko 
2895823d089SAndy Shevchenko int intel_mid_pwr_get_lss_id(struct pci_dev *pdev)
2905823d089SAndy Shevchenko {
2915823d089SAndy Shevchenko 	int vndr;
2925823d089SAndy Shevchenko 	u8 id;
2935823d089SAndy Shevchenko 
2945823d089SAndy Shevchenko 	/*
2955823d089SAndy Shevchenko 	 * Mapping to PWRMU index is kept in the Logical SubSystem ID byte of
2965823d089SAndy Shevchenko 	 * Vendor capability.
2975823d089SAndy Shevchenko 	 */
2985823d089SAndy Shevchenko 	vndr = pci_find_capability(pdev, PCI_CAP_ID_VNDR);
2995823d089SAndy Shevchenko 	if (!vndr)
3005823d089SAndy Shevchenko 		return -EINVAL;
3015823d089SAndy Shevchenko 
3025823d089SAndy Shevchenko 	/* Read the Logical SubSystem ID byte */
3035823d089SAndy Shevchenko 	pci_read_config_byte(pdev, vndr + INTEL_MID_PWR_LSS_OFFSET, &id);
3045823d089SAndy Shevchenko 	if (!(id & INTEL_MID_PWR_LSS_TYPE))
3055823d089SAndy Shevchenko 		return -ENODEV;
3065823d089SAndy Shevchenko 
3075823d089SAndy Shevchenko 	id &= ~INTEL_MID_PWR_LSS_TYPE;
3085823d089SAndy Shevchenko 	if (id >= LSS_MAX_DEVS)
3095823d089SAndy Shevchenko 		return -ERANGE;
3105823d089SAndy Shevchenko 
3115823d089SAndy Shevchenko 	return id;
3125823d089SAndy Shevchenko }
3135823d089SAndy Shevchenko 
3145823d089SAndy Shevchenko static irqreturn_t mid_pwr_irq_handler(int irq, void *dev_id)
3155823d089SAndy Shevchenko {
3165823d089SAndy Shevchenko 	struct mid_pwr *pwr = dev_id;
3175823d089SAndy Shevchenko 	u32 ics;
3185823d089SAndy Shevchenko 
3195823d089SAndy Shevchenko 	ics = readl(pwr->regs + PM_ICS);
3205823d089SAndy Shevchenko 	if (!(ics & PM_ICS_IP))
3215823d089SAndy Shevchenko 		return IRQ_NONE;
3225823d089SAndy Shevchenko 
3235823d089SAndy Shevchenko 	writel(ics | PM_ICS_IP, pwr->regs + PM_ICS);
3245823d089SAndy Shevchenko 
3255823d089SAndy Shevchenko 	dev_warn(pwr->dev, "Unexpected IRQ: %#x\n", PM_ICS_INT_STATUS(ics));
3265823d089SAndy Shevchenko 	return IRQ_HANDLED;
3275823d089SAndy Shevchenko }
3285823d089SAndy Shevchenko 
3295823d089SAndy Shevchenko struct mid_pwr_device_info {
3305823d089SAndy Shevchenko 	int (*set_initial_state)(struct mid_pwr *pwr);
3315823d089SAndy Shevchenko };
3325823d089SAndy Shevchenko 
3335823d089SAndy Shevchenko static int mid_pwr_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3345823d089SAndy Shevchenko {
3355823d089SAndy Shevchenko 	struct mid_pwr_device_info *info = (void *)id->driver_data;
3365823d089SAndy Shevchenko 	struct device *dev = &pdev->dev;
3375823d089SAndy Shevchenko 	struct mid_pwr *pwr;
3385823d089SAndy Shevchenko 	int ret;
3395823d089SAndy Shevchenko 
3405823d089SAndy Shevchenko 	ret = pcim_enable_device(pdev);
3415823d089SAndy Shevchenko 	if (ret < 0) {
3425823d089SAndy Shevchenko 		dev_err(&pdev->dev, "error: could not enable device\n");
3435823d089SAndy Shevchenko 		return ret;
3445823d089SAndy Shevchenko 	}
3455823d089SAndy Shevchenko 
3465823d089SAndy Shevchenko 	ret = pcim_iomap_regions(pdev, 1 << 0, pci_name(pdev));
3475823d089SAndy Shevchenko 	if (ret) {
3485823d089SAndy Shevchenko 		dev_err(&pdev->dev, "I/O memory remapping failed\n");
3495823d089SAndy Shevchenko 		return ret;
3505823d089SAndy Shevchenko 	}
3515823d089SAndy Shevchenko 
3525823d089SAndy Shevchenko 	pwr = devm_kzalloc(dev, sizeof(*pwr), GFP_KERNEL);
3535823d089SAndy Shevchenko 	if (!pwr)
3545823d089SAndy Shevchenko 		return -ENOMEM;
3555823d089SAndy Shevchenko 
3565823d089SAndy Shevchenko 	pwr->dev = dev;
3575823d089SAndy Shevchenko 	pwr->regs = pcim_iomap_table(pdev)[0];
3585823d089SAndy Shevchenko 	pwr->irq = pdev->irq;
3595823d089SAndy Shevchenko 
3605823d089SAndy Shevchenko 	mutex_init(&pwr->lock);
3615823d089SAndy Shevchenko 
3625823d089SAndy Shevchenko 	/* Disable interrupts */
3635823d089SAndy Shevchenko 	mid_pwr_interrupt_disable(pwr);
3645823d089SAndy Shevchenko 
3655823d089SAndy Shevchenko 	if (info && info->set_initial_state) {
3665823d089SAndy Shevchenko 		ret = info->set_initial_state(pwr);
3675823d089SAndy Shevchenko 		if (ret)
3685823d089SAndy Shevchenko 			dev_warn(dev, "Can't set initial state: %d\n", ret);
3695823d089SAndy Shevchenko 	}
3705823d089SAndy Shevchenko 
3715823d089SAndy Shevchenko 	ret = devm_request_irq(dev, pdev->irq, mid_pwr_irq_handler,
3725823d089SAndy Shevchenko 			       IRQF_NO_SUSPEND, pci_name(pdev), pwr);
3735823d089SAndy Shevchenko 	if (ret)
3745823d089SAndy Shevchenko 		return ret;
3755823d089SAndy Shevchenko 
3765823d089SAndy Shevchenko 	pwr->available = true;
3775823d089SAndy Shevchenko 	midpwr = pwr;
3785823d089SAndy Shevchenko 
3795823d089SAndy Shevchenko 	pci_set_drvdata(pdev, pwr);
3805823d089SAndy Shevchenko 	return 0;
3815823d089SAndy Shevchenko }
3825823d089SAndy Shevchenko 
383*f43ea76cSAndy Shevchenko static int mid_set_initial_state(struct mid_pwr *pwr, const u32 *states)
3845823d089SAndy Shevchenko {
3855823d089SAndy Shevchenko 	unsigned int i, j;
3865823d089SAndy Shevchenko 	int ret;
3875823d089SAndy Shevchenko 
3885823d089SAndy Shevchenko 	/*
3895823d089SAndy Shevchenko 	 * Enable wake events.
3905823d089SAndy Shevchenko 	 *
3915823d089SAndy Shevchenko 	 * PWRMU supports up to 32 sources for wake up the system. Ungate them
3925823d089SAndy Shevchenko 	 * all here.
3935823d089SAndy Shevchenko 	 */
3945823d089SAndy Shevchenko 	mid_pwr_set_wake(pwr, 0, 0xffffffff);
3955823d089SAndy Shevchenko 	mid_pwr_set_wake(pwr, 1, 0xffffffff);
3965823d089SAndy Shevchenko 
3975823d089SAndy Shevchenko 	/*
3985823d089SAndy Shevchenko 	 * Power off South Complex devices.
3995823d089SAndy Shevchenko 	 *
4005823d089SAndy Shevchenko 	 * There is a map (see a note below) of 64 devices with 2 bits per each
4015823d089SAndy Shevchenko 	 * on 32-bit HW registers. The following calls set all devices to one
4025823d089SAndy Shevchenko 	 * known initial state, i.e. PCI_D3hot. This is done in conjunction
4035823d089SAndy Shevchenko 	 * with PMCSR setting in arch/x86/pci/intel_mid_pci.c.
4045823d089SAndy Shevchenko 	 *
4055823d089SAndy Shevchenko 	 * NOTE: The actual device mapping is provided by a platform at run
4065823d089SAndy Shevchenko 	 * time using vendor capability of PCI configuration space.
4075823d089SAndy Shevchenko 	 */
408*f43ea76cSAndy Shevchenko 	mid_pwr_set_state(pwr, 0, states[0]);
409*f43ea76cSAndy Shevchenko 	mid_pwr_set_state(pwr, 1, states[1]);
410*f43ea76cSAndy Shevchenko 	mid_pwr_set_state(pwr, 2, states[2]);
411*f43ea76cSAndy Shevchenko 	mid_pwr_set_state(pwr, 3, states[3]);
4125823d089SAndy Shevchenko 
4135823d089SAndy Shevchenko 	/* Send command to SCU */
4145823d089SAndy Shevchenko 	ret = mid_pwr_wait_for_cmd(pwr, CMD_SET_CFG);
4155823d089SAndy Shevchenko 	if (ret)
4165823d089SAndy Shevchenko 		return ret;
4175823d089SAndy Shevchenko 
4185823d089SAndy Shevchenko 	for (i = 0; i < LSS_MAX_DEVS; i++) {
4195823d089SAndy Shevchenko 		for (j = 0; j < LSS_MAX_SHARED_DEVS; j++)
4205823d089SAndy Shevchenko 			pwr->lss[i][j].state = PCI_D3hot;
4215823d089SAndy Shevchenko 	}
4225823d089SAndy Shevchenko 
4235823d089SAndy Shevchenko 	return 0;
4245823d089SAndy Shevchenko }
4255823d089SAndy Shevchenko 
426*f43ea76cSAndy Shevchenko static int pnw_set_initial_state(struct mid_pwr *pwr)
427*f43ea76cSAndy Shevchenko {
428*f43ea76cSAndy Shevchenko 	/* On Penwell SRAM must stay powered on */
429*f43ea76cSAndy Shevchenko 	const u32 states[] = {
430*f43ea76cSAndy Shevchenko 		0xf00fffff,		/* PM_SSC(0) */
431*f43ea76cSAndy Shevchenko 		0xffffffff,		/* PM_SSC(1) */
432*f43ea76cSAndy Shevchenko 		0xffffffff,		/* PM_SSC(2) */
433*f43ea76cSAndy Shevchenko 		0xffffffff,		/* PM_SSC(3) */
434*f43ea76cSAndy Shevchenko 	};
435*f43ea76cSAndy Shevchenko 	return mid_set_initial_state(pwr, states);
436*f43ea76cSAndy Shevchenko }
437*f43ea76cSAndy Shevchenko 
438*f43ea76cSAndy Shevchenko static int tng_set_initial_state(struct mid_pwr *pwr)
439*f43ea76cSAndy Shevchenko {
440*f43ea76cSAndy Shevchenko 	const u32 states[] = {
441*f43ea76cSAndy Shevchenko 		0xffffffff,		/* PM_SSC(0) */
442*f43ea76cSAndy Shevchenko 		0xffffffff,		/* PM_SSC(1) */
443*f43ea76cSAndy Shevchenko 		0xffffffff,		/* PM_SSC(2) */
444*f43ea76cSAndy Shevchenko 		0xffffffff,		/* PM_SSC(3) */
445*f43ea76cSAndy Shevchenko 	};
446*f43ea76cSAndy Shevchenko 	return mid_set_initial_state(pwr, states);
447*f43ea76cSAndy Shevchenko }
448*f43ea76cSAndy Shevchenko 
449*f43ea76cSAndy Shevchenko static const struct mid_pwr_device_info pnw_info = {
450*f43ea76cSAndy Shevchenko 	.set_initial_state = pnw_set_initial_state,
451*f43ea76cSAndy Shevchenko };
452*f43ea76cSAndy Shevchenko 
453*f43ea76cSAndy Shevchenko static const struct mid_pwr_device_info tng_info = {
454*f43ea76cSAndy Shevchenko 	.set_initial_state = tng_set_initial_state,
4555823d089SAndy Shevchenko };
4565823d089SAndy Shevchenko 
4578e522e1dSAndy Shevchenko /* This table should be in sync with the one in drivers/pci/pci-mid.c */
4585823d089SAndy Shevchenko static const struct pci_device_id mid_pwr_pci_ids[] = {
459*f43ea76cSAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_PENWELL), (kernel_ulong_t)&pnw_info },
460*f43ea76cSAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_TANGIER), (kernel_ulong_t)&tng_info },
4615823d089SAndy Shevchenko 	{}
4625823d089SAndy Shevchenko };
4635823d089SAndy Shevchenko 
4645823d089SAndy Shevchenko static struct pci_driver mid_pwr_pci_driver = {
4655823d089SAndy Shevchenko 	.name		= "intel_mid_pwr",
4665823d089SAndy Shevchenko 	.probe		= mid_pwr_probe,
4675823d089SAndy Shevchenko 	.id_table	= mid_pwr_pci_ids,
4685823d089SAndy Shevchenko };
4695823d089SAndy Shevchenko 
4705823d089SAndy Shevchenko builtin_pci_driver(mid_pwr_pci_driver);
471