2 * Intel MID Power Management Unit (PWRMU) device driver
4 * Copyright (C) 2016, Intel Corporation
6 * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
12 * Intel MID Power Management Unit device driver handles the South Complex PCI
13 * devices such as GPDMA, SPI, I2C, PWM, and so on. By default PCI core
14 * modifies bits in PMCSR register in the PCI configuration space. This is not
15 * enough on some SoCs like Intel Tangier. In such case PCI core sets a new
16 * power state of the device in question through a PM hook registered in struct
17 * pci_platform_pm_ops (see drivers/pci/pci-mid.c).
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/delay.h>
23 #include <linux/errno.h>
24 #include <linux/interrupt.h>
25 #include <linux/kernel.h>
26 #include <linux/export.h>
27 #include <linux/mutex.h>
28 #include <linux/pci.h>
30 #include <asm/intel-mid.h>
36 #define PM_WKC(x) (0x10 + (x) * 4)
37 #define PM_WKS(x) (0x18 + (x) * 4)
38 #define PM_SSC(x) (0x20 + (x) * 4)
39 #define PM_SSS(x) (0x30 + (x) * 4)
42 #define PM_STS_BUSY (1 << 8)
45 #define PM_CMD_CMD(x) ((x) << 0)
46 #define PM_CMD_IOC (1 << 8)
47 #define PM_CMD_CM_NOP (0 << 9)
48 #define PM_CMD_CM_IMMEDIATE (1 << 9)
49 #define PM_CMD_CM_DELAY (2 << 9)
50 #define PM_CMD_CM_TRIGGER (3 << 9)
51 #define PM_CMD_D3cold (1 << 21)
53 /* List of commands */
54 #define CMD_SET_CFG 0x01
57 #define PM_ICS_INT_STATUS(x) ((x) & 0xff)
58 #define PM_ICS_IE (1 << 8)
59 #define PM_ICS_IP (1 << 9)
60 #define PM_ICS_SW_INT_STS (1 << 10)
62 /* List of interrupts */
64 #define INT_CMD_COMPLETE 1
66 #define INT_WAKE_EVENT 3
67 #define INT_LSS_POWER_ERR 4
68 #define INT_S0iX_MSG_ERR 5
70 #define INT_TRIGGER_ERR 7
71 #define INT_INACTIVITY 8
73 /* South Complex devices */
74 #define LSS_MAX_SHARED_DEVS 4
75 #define LSS_MAX_DEVS 64
77 #define LSS_WS_BITS 1 /* wake state width */
78 #define LSS_PWS_BITS 2 /* power state width */
80 /* Supported device IDs */
81 #define PCI_DEVICE_ID_PENWELL 0x0828
82 #define PCI_DEVICE_ID_TANGIER 0x11a1
96 struct mid_pwr_dev lss[LSS_MAX_DEVS][LSS_MAX_SHARED_DEVS];
99 static struct mid_pwr *midpwr;
101 static u32 mid_pwr_get_state(struct mid_pwr *pwr, int reg)
103 return readl(pwr->regs + PM_SSS(reg));
106 static void mid_pwr_set_state(struct mid_pwr *pwr, int reg, u32 value)
108 writel(value, pwr->regs + PM_SSC(reg));
111 static void mid_pwr_set_wake(struct mid_pwr *pwr, int reg, u32 value)
113 writel(value, pwr->regs + PM_WKC(reg));
116 static void mid_pwr_interrupt_disable(struct mid_pwr *pwr)
118 writel(~PM_ICS_IE, pwr->regs + PM_ICS);
121 static bool mid_pwr_is_busy(struct mid_pwr *pwr)
123 return !!(readl(pwr->regs + PM_STS) & PM_STS_BUSY);
126 /* Wait 500ms that the latest PWRMU command finished */
127 static int mid_pwr_wait(struct mid_pwr *pwr)
129 unsigned int count = 500000;
133 busy = mid_pwr_is_busy(pwr);
142 static int mid_pwr_wait_for_cmd(struct mid_pwr *pwr, u8 cmd)
144 writel(PM_CMD_CMD(cmd) | PM_CMD_CM_IMMEDIATE, pwr->regs + PM_CMD);
145 return mid_pwr_wait(pwr);
148 static int __update_power_state(struct mid_pwr *pwr, int reg, int bit, int new)
154 /* Check if the device is already in desired state */
155 power = mid_pwr_get_state(pwr, reg);
156 curstate = (power >> bit) & 3;
160 /* Update the power state */
161 mid_pwr_set_state(pwr, reg, (power & ~(3 << bit)) | (new << bit));
163 /* Send command to SCU */
164 ret = mid_pwr_wait_for_cmd(pwr, CMD_SET_CFG);
168 /* Check if the device is already in desired state */
169 power = mid_pwr_get_state(pwr, reg);
170 curstate = (power >> bit) & 3;
177 static pci_power_t __find_weakest_power_state(struct mid_pwr_dev *lss,
178 struct pci_dev *pdev,
181 pci_power_t weakest = PCI_D3hot;
184 /* Find device in cache or first free cell */
185 for (j = 0; j < LSS_MAX_SHARED_DEVS; j++) {
186 if (lss[j].pdev == pdev || !lss[j].pdev)
190 /* Store the desired state in cache */
191 if (j < LSS_MAX_SHARED_DEVS) {
193 lss[j].state = state;
195 dev_WARN(&pdev->dev, "No room for device in PWRMU LSS cache\n");
199 /* Find the power state we may use */
200 for (j = 0; j < LSS_MAX_SHARED_DEVS; j++) {
201 if (lss[j].state < weakest)
202 weakest = lss[j].state;
208 static int __set_power_state(struct mid_pwr *pwr, struct pci_dev *pdev,
209 pci_power_t state, int id, int reg, int bit)
214 state = __find_weakest_power_state(pwr->lss[id], pdev, state);
215 name = pci_power_name(state);
217 ret = __update_power_state(pwr, reg, bit, (__force int)state);
219 dev_warn(&pdev->dev, "Can't set power state %s: %d\n", name, ret);
223 dev_vdbg(&pdev->dev, "Set power state %s\n", name);
227 static int mid_pwr_set_power_state(struct mid_pwr *pwr, struct pci_dev *pdev,
233 id = intel_mid_pwr_get_lss_id(pdev);
237 reg = (id * LSS_PWS_BITS) / 32;
238 bit = (id * LSS_PWS_BITS) % 32;
240 /* We support states between PCI_D0 and PCI_D3hot */
243 if (state > PCI_D3hot)
246 mutex_lock(&pwr->lock);
247 ret = __set_power_state(pwr, pdev, state, id, reg, bit);
248 mutex_unlock(&pwr->lock);
252 int intel_mid_pci_set_power_state(struct pci_dev *pdev, pci_power_t state)
254 struct mid_pwr *pwr = midpwr;
259 if (pwr && pwr->available)
260 ret = mid_pwr_set_power_state(pwr, pdev, state);
261 dev_vdbg(&pdev->dev, "set_power_state() returns %d\n", ret);
265 EXPORT_SYMBOL_GPL(intel_mid_pci_set_power_state);
267 int intel_mid_pwr_get_lss_id(struct pci_dev *pdev)
273 * Mapping to PWRMU index is kept in the Logical SubSystem ID byte of
276 vndr = pci_find_capability(pdev, PCI_CAP_ID_VNDR);
280 /* Read the Logical SubSystem ID byte */
281 pci_read_config_byte(pdev, vndr + INTEL_MID_PWR_LSS_OFFSET, &id);
282 if (!(id & INTEL_MID_PWR_LSS_TYPE))
285 id &= ~INTEL_MID_PWR_LSS_TYPE;
286 if (id >= LSS_MAX_DEVS)
292 static irqreturn_t mid_pwr_irq_handler(int irq, void *dev_id)
294 struct mid_pwr *pwr = dev_id;
297 ics = readl(pwr->regs + PM_ICS);
298 if (!(ics & PM_ICS_IP))
301 writel(ics | PM_ICS_IP, pwr->regs + PM_ICS);
303 dev_warn(pwr->dev, "Unexpected IRQ: %#x\n", PM_ICS_INT_STATUS(ics));
307 struct mid_pwr_device_info {
308 int (*set_initial_state)(struct mid_pwr *pwr);
311 static int mid_pwr_probe(struct pci_dev *pdev, const struct pci_device_id *id)
313 struct mid_pwr_device_info *info = (void *)id->driver_data;
314 struct device *dev = &pdev->dev;
318 ret = pcim_enable_device(pdev);
320 dev_err(&pdev->dev, "error: could not enable device\n");
324 ret = pcim_iomap_regions(pdev, 1 << 0, pci_name(pdev));
326 dev_err(&pdev->dev, "I/O memory remapping failed\n");
330 pwr = devm_kzalloc(dev, sizeof(*pwr), GFP_KERNEL);
335 pwr->regs = pcim_iomap_table(pdev)[0];
336 pwr->irq = pdev->irq;
338 mutex_init(&pwr->lock);
340 /* Disable interrupts */
341 mid_pwr_interrupt_disable(pwr);
343 if (info && info->set_initial_state) {
344 ret = info->set_initial_state(pwr);
346 dev_warn(dev, "Can't set initial state: %d\n", ret);
349 ret = devm_request_irq(dev, pdev->irq, mid_pwr_irq_handler,
350 IRQF_NO_SUSPEND, pci_name(pdev), pwr);
354 pwr->available = true;
357 pci_set_drvdata(pdev, pwr);
361 static int mid_set_initial_state(struct mid_pwr *pwr)
367 * Enable wake events.
369 * PWRMU supports up to 32 sources for wake up the system. Ungate them
372 mid_pwr_set_wake(pwr, 0, 0xffffffff);
373 mid_pwr_set_wake(pwr, 1, 0xffffffff);
376 * Power off South Complex devices.
378 * There is a map (see a note below) of 64 devices with 2 bits per each
379 * on 32-bit HW registers. The following calls set all devices to one
380 * known initial state, i.e. PCI_D3hot. This is done in conjunction
381 * with PMCSR setting in arch/x86/pci/intel_mid_pci.c.
383 * NOTE: The actual device mapping is provided by a platform at run
384 * time using vendor capability of PCI configuration space.
386 mid_pwr_set_state(pwr, 0, 0xffffffff);
387 mid_pwr_set_state(pwr, 1, 0xffffffff);
388 mid_pwr_set_state(pwr, 2, 0xffffffff);
389 mid_pwr_set_state(pwr, 3, 0xffffffff);
391 /* Send command to SCU */
392 ret = mid_pwr_wait_for_cmd(pwr, CMD_SET_CFG);
396 for (i = 0; i < LSS_MAX_DEVS; i++) {
397 for (j = 0; j < LSS_MAX_SHARED_DEVS; j++)
398 pwr->lss[i][j].state = PCI_D3hot;
404 static const struct mid_pwr_device_info mid_info = {
405 .set_initial_state = mid_set_initial_state,
408 static const struct pci_device_id mid_pwr_pci_ids[] = {
409 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_PENWELL), (kernel_ulong_t)&mid_info },
410 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_TANGIER), (kernel_ulong_t)&mid_info },
414 static struct pci_driver mid_pwr_pci_driver = {
415 .name = "intel_mid_pwr",
416 .probe = mid_pwr_probe,
417 .id_table = mid_pwr_pci_ids,
420 builtin_pci_driver(mid_pwr_pci_driver);