blob: 4f3febf8a58954b2ca8ced8c057106529ab1f30b [file] [log] [blame]
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01001/*
2 * ACPI support for Intel Lynxpoint LPSS.
3 *
Andy Shevchenko6c17ee42014-11-05 18:34:47 +02004 * Copyright (C) 2013, 2014, Intel Corporation
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01005 * Authors: Mika Westerberg <mika.westerberg@linux.intel.com>
6 * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/acpi.h>
14#include <linux/clk.h>
15#include <linux/clkdev.h>
16#include <linux/clk-provider.h>
17#include <linux/err.h>
18#include <linux/io.h>
19#include <linux/platform_device.h>
20#include <linux/platform_data/clk-lpss.h>
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010021#include <linux/pm_runtime.h>
Heikki Krogerusc78b0832014-05-23 16:15:09 +030022#include <linux/delay.h>
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010023
24#include "internal.h"
25
26ACPI_MODULE_NAME("acpi_lpss");
27
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +020028#ifdef CONFIG_X86_INTEL_LPSS
29
30#define LPSS_ADDR(desc) ((unsigned long)&desc)
31
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010032#define LPSS_CLK_SIZE 0x04
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010033#define LPSS_LTR_SIZE 0x18
34
35/* Offsets relative to LPSS_PRIVATE_OFFSET */
Heikki Krogerused3a8722014-05-19 14:42:07 +030036#define LPSS_CLK_DIVIDER_DEF_MASK (BIT(1) | BIT(16))
Mika Westerberg765bdd42014-06-17 14:33:39 +030037#define LPSS_RESETS 0x04
38#define LPSS_RESETS_RESET_FUNC BIT(0)
39#define LPSS_RESETS_RESET_APB BIT(1)
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010040#define LPSS_GENERAL 0x08
41#define LPSS_GENERAL_LTR_MODE_SW BIT(2)
Heikki Krogerus088f1fd2013-10-09 09:49:20 +030042#define LPSS_GENERAL_UART_RTS_OVRD BIT(3)
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010043#define LPSS_SW_LTR 0x10
44#define LPSS_AUTO_LTR 0x14
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +010045#define LPSS_LTR_SNOOP_REQ BIT(15)
46#define LPSS_LTR_SNOOP_MASK 0x0000FFFF
47#define LPSS_LTR_SNOOP_LAT_1US 0x800
48#define LPSS_LTR_SNOOP_LAT_32US 0xC00
49#define LPSS_LTR_SNOOP_LAT_SHIFT 5
50#define LPSS_LTR_SNOOP_LAT_CUTOFF 3000
51#define LPSS_LTR_MAX_VAL 0x3FF
Heikki Krogerus06d86412013-06-17 13:25:46 +030052#define LPSS_TX_INT 0x20
53#define LPSS_TX_INT_MASK BIT(1)
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010054
Heikki Krogerusc78b0832014-05-23 16:15:09 +030055#define LPSS_PRV_REG_COUNT 9
56
Heikki Krogerusff8c1af2014-09-02 10:55:07 +030057/* LPSS Flags */
58#define LPSS_CLK BIT(0)
59#define LPSS_CLK_GATE BIT(1)
60#define LPSS_CLK_DIVIDER BIT(2)
61#define LPSS_LTR BIT(3)
62#define LPSS_SAVE_CTX BIT(4)
Andy Shevchenko6c17ee42014-11-05 18:34:47 +020063#define LPSS_DEV_PROXY BIT(5)
64#define LPSS_PROXY_REQ BIT(6)
Mika Westerbergf6272172013-05-13 12:42:44 +000065
Heikki Krogerus06d86412013-06-17 13:25:46 +030066struct lpss_private_data;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010067
68struct lpss_device_desc {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +030069 unsigned int flags;
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010070 unsigned int prv_offset;
Mika Westerberg958c4eb2013-06-18 16:51:35 +030071 size_t prv_size_override;
Heikki Krogerus06d86412013-06-17 13:25:46 +030072 void (*setup)(struct lpss_private_data *pdata);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010073};
74
Andy Shevchenko6c17ee42014-11-05 18:34:47 +020075static struct device *proxy_device;
76
Rafael J. Wysockib59cc202013-05-08 11:55:49 +030077static struct lpss_device_desc lpss_dma_desc = {
Andy Shevchenko6c17ee42014-11-05 18:34:47 +020078 .flags = LPSS_CLK | LPSS_PROXY_REQ,
Rafael J. Wysockib59cc202013-05-08 11:55:49 +030079};
80
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010081struct lpss_private_data {
82 void __iomem *mmio_base;
83 resource_size_t mmio_size;
Heikki Krogerus03f09f72014-09-02 10:55:09 +030084 unsigned int fixed_clk_rate;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010085 struct clk *clk;
86 const struct lpss_device_desc *dev_desc;
Heikki Krogerusc78b0832014-05-23 16:15:09 +030087 u32 prv_reg_ctx[LPSS_PRV_REG_COUNT];
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010088};
89
Heikki Krogerus1f47a772014-09-11 15:19:33 +030090/* UART Component Parameter Register */
91#define LPSS_UART_CPR 0xF4
92#define LPSS_UART_CPR_AFCE BIT(4)
93
Heikki Krogerus06d86412013-06-17 13:25:46 +030094static void lpss_uart_setup(struct lpss_private_data *pdata)
95{
Heikki Krogerus088f1fd2013-10-09 09:49:20 +030096 unsigned int offset;
Heikki Krogerus1f47a772014-09-11 15:19:33 +030097 u32 val;
Heikki Krogerus06d86412013-06-17 13:25:46 +030098
Heikki Krogerus088f1fd2013-10-09 09:49:20 +030099 offset = pdata->dev_desc->prv_offset + LPSS_TX_INT;
Heikki Krogerus1f47a772014-09-11 15:19:33 +0300100 val = readl(pdata->mmio_base + offset);
101 writel(val | LPSS_TX_INT_MASK, pdata->mmio_base + offset);
Heikki Krogerus088f1fd2013-10-09 09:49:20 +0300102
Heikki Krogerus1f47a772014-09-11 15:19:33 +0300103 val = readl(pdata->mmio_base + LPSS_UART_CPR);
104 if (!(val & LPSS_UART_CPR_AFCE)) {
105 offset = pdata->dev_desc->prv_offset + LPSS_GENERAL;
106 val = readl(pdata->mmio_base + offset);
107 val |= LPSS_GENERAL_UART_RTS_OVRD;
108 writel(val, pdata->mmio_base + offset);
109 }
Heikki Krogerus06d86412013-06-17 13:25:46 +0300110}
111
Heikki Krogerus03f09f72014-09-02 10:55:09 +0300112static void byt_i2c_setup(struct lpss_private_data *pdata)
Mika Westerberg765bdd42014-06-17 14:33:39 +0300113{
114 unsigned int offset;
115 u32 val;
116
117 offset = pdata->dev_desc->prv_offset + LPSS_RESETS;
118 val = readl(pdata->mmio_base + offset);
119 val |= LPSS_RESETS_RESET_APB | LPSS_RESETS_RESET_FUNC;
120 writel(val, pdata->mmio_base + offset);
Heikki Krogerus03f09f72014-09-02 10:55:09 +0300121
122 if (readl(pdata->mmio_base + pdata->dev_desc->prv_offset))
123 pdata->fixed_clk_rate = 133000000;
Mika Westerberg765bdd42014-06-17 14:33:39 +0300124}
125
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100126static struct lpss_device_desc lpt_dev_desc = {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300127 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_LTR,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100128 .prv_offset = 0x800,
Heikki Krogerused3a8722014-05-19 14:42:07 +0300129};
130
131static struct lpss_device_desc lpt_i2c_dev_desc = {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300132 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_LTR,
Heikki Krogerused3a8722014-05-19 14:42:07 +0300133 .prv_offset = 0x800,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100134};
135
Heikki Krogerus06d86412013-06-17 13:25:46 +0300136static struct lpss_device_desc lpt_uart_dev_desc = {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300137 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_LTR,
Heikki Krogerus06d86412013-06-17 13:25:46 +0300138 .prv_offset = 0x800,
Heikki Krogerus06d86412013-06-17 13:25:46 +0300139 .setup = lpss_uart_setup,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100140};
141
142static struct lpss_device_desc lpt_sdio_dev_desc = {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300143 .flags = LPSS_LTR,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100144 .prv_offset = 0x1000,
Mika Westerberg958c4eb2013-06-18 16:51:35 +0300145 .prv_size_override = 0x1018,
Chew, Chiau Eee1c74812014-02-19 02:24:29 +0800146};
147
148static struct lpss_device_desc byt_pwm_dev_desc = {
Heikki Krogerus3f56bf32014-09-02 10:55:10 +0300149 .flags = LPSS_SAVE_CTX,
Chew, Chiau Eee1c74812014-02-19 02:24:29 +0800150};
151
Mika Westerbergf6272172013-05-13 12:42:44 +0000152static struct lpss_device_desc byt_uart_dev_desc = {
Andy Shevchenko6c17ee42014-11-05 18:34:47 +0200153 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_SAVE_CTX |
154 LPSS_DEV_PROXY,
Mika Westerbergf6272172013-05-13 12:42:44 +0000155 .prv_offset = 0x800,
Heikki Krogerus06d86412013-06-17 13:25:46 +0300156 .setup = lpss_uart_setup,
Mika Westerbergf6272172013-05-13 12:42:44 +0000157};
158
Mika Westerbergf6272172013-05-13 12:42:44 +0000159static struct lpss_device_desc byt_spi_dev_desc = {
Andy Shevchenko6c17ee42014-11-05 18:34:47 +0200160 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_SAVE_CTX |
161 LPSS_DEV_PROXY,
Mika Westerbergf6272172013-05-13 12:42:44 +0000162 .prv_offset = 0x400,
Mika Westerbergf6272172013-05-13 12:42:44 +0000163};
164
165static struct lpss_device_desc byt_sdio_dev_desc = {
Andy Shevchenko6c17ee42014-11-05 18:34:47 +0200166 .flags = LPSS_CLK | LPSS_DEV_PROXY,
Mika Westerbergf6272172013-05-13 12:42:44 +0000167};
168
169static struct lpss_device_desc byt_i2c_dev_desc = {
Andy Shevchenko6c17ee42014-11-05 18:34:47 +0200170 .flags = LPSS_CLK | LPSS_SAVE_CTX | LPSS_DEV_PROXY,
Mika Westerbergf6272172013-05-13 12:42:44 +0000171 .prv_offset = 0x800,
Heikki Krogerus03f09f72014-09-02 10:55:09 +0300172 .setup = byt_i2c_setup,
Alan Cox1bfbd8e2014-08-19 15:55:22 +0300173};
174
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +0200175#else
176
177#define LPSS_ADDR(desc) (0UL)
178
179#endif /* CONFIG_X86_INTEL_LPSS */
180
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100181static const struct acpi_device_id acpi_lpss_device_ids[] = {
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300182 /* Generic LPSS devices */
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +0200183 { "INTL9C60", LPSS_ADDR(lpss_dma_desc) },
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300184
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100185 /* Lynxpoint LPSS devices */
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +0200186 { "INT33C0", LPSS_ADDR(lpt_dev_desc) },
187 { "INT33C1", LPSS_ADDR(lpt_dev_desc) },
188 { "INT33C2", LPSS_ADDR(lpt_i2c_dev_desc) },
189 { "INT33C3", LPSS_ADDR(lpt_i2c_dev_desc) },
190 { "INT33C4", LPSS_ADDR(lpt_uart_dev_desc) },
191 { "INT33C5", LPSS_ADDR(lpt_uart_dev_desc) },
192 { "INT33C6", LPSS_ADDR(lpt_sdio_dev_desc) },
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100193 { "INT33C7", },
194
Mika Westerbergf6272172013-05-13 12:42:44 +0000195 /* BayTrail LPSS devices */
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +0200196 { "80860F09", LPSS_ADDR(byt_pwm_dev_desc) },
197 { "80860F0A", LPSS_ADDR(byt_uart_dev_desc) },
198 { "80860F0E", LPSS_ADDR(byt_spi_dev_desc) },
199 { "80860F14", LPSS_ADDR(byt_sdio_dev_desc) },
200 { "80860F41", LPSS_ADDR(byt_i2c_dev_desc) },
Mika Westerbergf6272172013-05-13 12:42:44 +0000201 { "INT33B2", },
Jin Yao20482d32014-05-15 18:28:46 +0300202 { "INT33FC", },
Mika Westerbergf6272172013-05-13 12:42:44 +0000203
Alan Cox1bfbd8e2014-08-19 15:55:22 +0300204 /* Braswell LPSS devices */
Heikki Krogerus3f56bf32014-09-02 10:55:10 +0300205 { "80862288", LPSS_ADDR(byt_pwm_dev_desc) },
Alan Cox1bfbd8e2014-08-19 15:55:22 +0300206 { "8086228A", LPSS_ADDR(byt_uart_dev_desc) },
207 { "8086228E", LPSS_ADDR(byt_spi_dev_desc) },
208 { "808622C1", LPSS_ADDR(byt_i2c_dev_desc) },
209
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +0200210 { "INT3430", LPSS_ADDR(lpt_dev_desc) },
211 { "INT3431", LPSS_ADDR(lpt_dev_desc) },
212 { "INT3432", LPSS_ADDR(lpt_i2c_dev_desc) },
213 { "INT3433", LPSS_ADDR(lpt_i2c_dev_desc) },
214 { "INT3434", LPSS_ADDR(lpt_uart_dev_desc) },
215 { "INT3435", LPSS_ADDR(lpt_uart_dev_desc) },
216 { "INT3436", LPSS_ADDR(lpt_sdio_dev_desc) },
Mika Westerberga4d97532013-11-12 11:48:19 +0200217 { "INT3437", },
218
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300219 /* Wildcat Point LPSS devices */
220 { "INT3438", LPSS_ADDR(lpt_dev_desc) },
Jie Yang43218a12014-08-01 09:06:35 +0800221
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100222 { }
223};
224
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +0200225#ifdef CONFIG_X86_INTEL_LPSS
226
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100227static int is_memory(struct acpi_resource *res, void *not_used)
228{
229 struct resource r;
230 return !acpi_dev_resource_memory(res, &r);
231}
232
233/* LPSS main clock device. */
234static struct platform_device *lpss_clk_dev;
235
236static inline void lpt_register_clock_device(void)
237{
238 lpss_clk_dev = platform_device_register_simple("clk-lpt", -1, NULL, 0);
239}
240
241static int register_device_clock(struct acpi_device *adev,
242 struct lpss_private_data *pdata)
243{
244 const struct lpss_device_desc *dev_desc = pdata->dev_desc;
Heikki Krogerused3a8722014-05-19 14:42:07 +0300245 const char *devname = dev_name(&adev->dev);
Mika Westerbergf6272172013-05-13 12:42:44 +0000246 struct clk *clk = ERR_PTR(-ENODEV);
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300247 struct lpss_clk_data *clk_data;
Heikki Krogerused3a8722014-05-19 14:42:07 +0300248 const char *parent, *clk_name;
249 void __iomem *prv_base;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100250
251 if (!lpss_clk_dev)
252 lpt_register_clock_device();
253
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300254 clk_data = platform_get_drvdata(lpss_clk_dev);
255 if (!clk_data)
256 return -ENODEV;
Heikki Krogerusb0d00f82014-09-02 10:55:08 +0300257 clk = clk_data->clk;
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300258
259 if (!pdata->mmio_base
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100260 || pdata->mmio_size < dev_desc->prv_offset + LPSS_CLK_SIZE)
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100261 return -ENODATA;
262
Mika Westerbergf6272172013-05-13 12:42:44 +0000263 parent = clk_data->name;
Heikki Krogerused3a8722014-05-19 14:42:07 +0300264 prv_base = pdata->mmio_base + dev_desc->prv_offset;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100265
Heikki Krogerus03f09f72014-09-02 10:55:09 +0300266 if (pdata->fixed_clk_rate) {
267 clk = clk_register_fixed_rate(NULL, devname, parent, 0,
268 pdata->fixed_clk_rate);
269 goto out;
Mika Westerbergf6272172013-05-13 12:42:44 +0000270 }
271
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300272 if (dev_desc->flags & LPSS_CLK_GATE) {
Heikki Krogerused3a8722014-05-19 14:42:07 +0300273 clk = clk_register_gate(NULL, devname, parent, 0,
274 prv_base, 0, 0, NULL);
275 parent = devname;
276 }
277
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300278 if (dev_desc->flags & LPSS_CLK_DIVIDER) {
Heikki Krogerused3a8722014-05-19 14:42:07 +0300279 /* Prevent division by zero */
280 if (!readl(prv_base))
281 writel(LPSS_CLK_DIVIDER_DEF_MASK, prv_base);
282
283 clk_name = kasprintf(GFP_KERNEL, "%s-div", devname);
284 if (!clk_name)
285 return -ENOMEM;
286 clk = clk_register_fractional_divider(NULL, clk_name, parent,
287 0, prv_base,
288 1, 15, 16, 15, 0, NULL);
289 parent = clk_name;
290
291 clk_name = kasprintf(GFP_KERNEL, "%s-update", devname);
292 if (!clk_name) {
293 kfree(parent);
294 return -ENOMEM;
295 }
296 clk = clk_register_gate(NULL, clk_name, parent,
297 CLK_SET_RATE_PARENT | CLK_SET_RATE_GATE,
298 prv_base, 31, 0, NULL);
299 kfree(parent);
300 kfree(clk_name);
Mika Westerbergf6272172013-05-13 12:42:44 +0000301 }
Heikki Krogerus03f09f72014-09-02 10:55:09 +0300302out:
Mika Westerbergf6272172013-05-13 12:42:44 +0000303 if (IS_ERR(clk))
304 return PTR_ERR(clk);
305
Heikki Krogerused3a8722014-05-19 14:42:07 +0300306 pdata->clk = clk;
307 clk_register_clkdev(clk, NULL, devname);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100308 return 0;
309}
310
311static int acpi_lpss_create_device(struct acpi_device *adev,
312 const struct acpi_device_id *id)
313{
314 struct lpss_device_desc *dev_desc;
315 struct lpss_private_data *pdata;
316 struct resource_list_entry *rentry;
317 struct list_head resource_list;
Rafael J. Wysocki8ce62f82014-05-25 14:38:52 +0200318 struct platform_device *pdev;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100319 int ret;
320
321 dev_desc = (struct lpss_device_desc *)id->driver_data;
Rafael J. Wysocki8ce62f82014-05-25 14:38:52 +0200322 if (!dev_desc) {
323 pdev = acpi_create_platform_device(adev);
324 return IS_ERR_OR_NULL(pdev) ? PTR_ERR(pdev) : 1;
325 }
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100326 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
327 if (!pdata)
328 return -ENOMEM;
329
330 INIT_LIST_HEAD(&resource_list);
331 ret = acpi_dev_get_resources(adev, &resource_list, is_memory, NULL);
332 if (ret < 0)
333 goto err_out;
334
335 list_for_each_entry(rentry, &resource_list, node)
336 if (resource_type(&rentry->res) == IORESOURCE_MEM) {
Mika Westerberg958c4eb2013-06-18 16:51:35 +0300337 if (dev_desc->prv_size_override)
338 pdata->mmio_size = dev_desc->prv_size_override;
339 else
340 pdata->mmio_size = resource_size(&rentry->res);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100341 pdata->mmio_base = ioremap(rentry->res.start,
342 pdata->mmio_size);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100343 break;
344 }
345
346 acpi_dev_free_resource_list(&resource_list);
347
Mika Westerbergaf65cfe2013-09-02 13:30:25 +0300348 pdata->dev_desc = dev_desc;
349
Heikki Krogerus03f09f72014-09-02 10:55:09 +0300350 if (dev_desc->setup)
351 dev_desc->setup(pdata);
352
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300353 if (dev_desc->flags & LPSS_CLK) {
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100354 ret = register_device_clock(adev, pdata);
355 if (ret) {
Rafael J. Wysockib9e95fc2013-06-19 00:45:34 +0200356 /* Skip the device, but continue the namespace scan. */
357 ret = 0;
358 goto err_out;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100359 }
360 }
361
Rafael J. Wysockib9e95fc2013-06-19 00:45:34 +0200362 /*
363 * This works around a known issue in ACPI tables where LPSS devices
364 * have _PS0 and _PS3 without _PSC (and no power resources), so
365 * acpi_bus_init_power() will assume that the BIOS has put them into D0.
366 */
367 ret = acpi_device_fix_up_power(adev);
368 if (ret) {
369 /* Skip the device, but continue the namespace scan. */
370 ret = 0;
371 goto err_out;
372 }
373
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100374 adev->driver_data = pdata;
Rafael J. Wysocki8ce62f82014-05-25 14:38:52 +0200375 pdev = acpi_create_platform_device(adev);
376 if (!IS_ERR_OR_NULL(pdev)) {
Andy Shevchenko6c17ee42014-11-05 18:34:47 +0200377 if (!proxy_device && dev_desc->flags & LPSS_DEV_PROXY)
378 proxy_device = &pdev->dev;
Rafael J. Wysocki8ce62f82014-05-25 14:38:52 +0200379 return 1;
380 }
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100381
Rafael J. Wysocki8ce62f82014-05-25 14:38:52 +0200382 ret = PTR_ERR(pdev);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100383 adev->driver_data = NULL;
384
385 err_out:
386 kfree(pdata);
387 return ret;
388}
389
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100390static u32 __lpss_reg_read(struct lpss_private_data *pdata, unsigned int reg)
391{
392 return readl(pdata->mmio_base + pdata->dev_desc->prv_offset + reg);
393}
394
395static void __lpss_reg_write(u32 val, struct lpss_private_data *pdata,
396 unsigned int reg)
397{
398 writel(val, pdata->mmio_base + pdata->dev_desc->prv_offset + reg);
399}
400
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100401static int lpss_reg_read(struct device *dev, unsigned int reg, u32 *val)
402{
403 struct acpi_device *adev;
404 struct lpss_private_data *pdata;
405 unsigned long flags;
406 int ret;
407
408 ret = acpi_bus_get_device(ACPI_HANDLE(dev), &adev);
409 if (WARN_ON(ret))
410 return ret;
411
412 spin_lock_irqsave(&dev->power.lock, flags);
413 if (pm_runtime_suspended(dev)) {
414 ret = -EAGAIN;
415 goto out;
416 }
417 pdata = acpi_driver_data(adev);
418 if (WARN_ON(!pdata || !pdata->mmio_base)) {
419 ret = -ENODEV;
420 goto out;
421 }
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100422 *val = __lpss_reg_read(pdata, reg);
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100423
424 out:
425 spin_unlock_irqrestore(&dev->power.lock, flags);
426 return ret;
427}
428
429static ssize_t lpss_ltr_show(struct device *dev, struct device_attribute *attr,
430 char *buf)
431{
432 u32 ltr_value = 0;
433 unsigned int reg;
434 int ret;
435
436 reg = strcmp(attr->attr.name, "auto_ltr") ? LPSS_SW_LTR : LPSS_AUTO_LTR;
437 ret = lpss_reg_read(dev, reg, &ltr_value);
438 if (ret)
439 return ret;
440
441 return snprintf(buf, PAGE_SIZE, "%08x\n", ltr_value);
442}
443
444static ssize_t lpss_ltr_mode_show(struct device *dev,
445 struct device_attribute *attr, char *buf)
446{
447 u32 ltr_mode = 0;
448 char *outstr;
449 int ret;
450
451 ret = lpss_reg_read(dev, LPSS_GENERAL, &ltr_mode);
452 if (ret)
453 return ret;
454
455 outstr = (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) ? "sw" : "auto";
456 return sprintf(buf, "%s\n", outstr);
457}
458
459static DEVICE_ATTR(auto_ltr, S_IRUSR, lpss_ltr_show, NULL);
460static DEVICE_ATTR(sw_ltr, S_IRUSR, lpss_ltr_show, NULL);
461static DEVICE_ATTR(ltr_mode, S_IRUSR, lpss_ltr_mode_show, NULL);
462
463static struct attribute *lpss_attrs[] = {
464 &dev_attr_auto_ltr.attr,
465 &dev_attr_sw_ltr.attr,
466 &dev_attr_ltr_mode.attr,
467 NULL,
468};
469
470static struct attribute_group lpss_attr_group = {
471 .attrs = lpss_attrs,
472 .name = "lpss_ltr",
473};
474
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100475static void acpi_lpss_set_ltr(struct device *dev, s32 val)
476{
477 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
478 u32 ltr_mode, ltr_val;
479
480 ltr_mode = __lpss_reg_read(pdata, LPSS_GENERAL);
481 if (val < 0) {
482 if (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) {
483 ltr_mode &= ~LPSS_GENERAL_LTR_MODE_SW;
484 __lpss_reg_write(ltr_mode, pdata, LPSS_GENERAL);
485 }
486 return;
487 }
488 ltr_val = __lpss_reg_read(pdata, LPSS_SW_LTR) & ~LPSS_LTR_SNOOP_MASK;
489 if (val >= LPSS_LTR_SNOOP_LAT_CUTOFF) {
490 ltr_val |= LPSS_LTR_SNOOP_LAT_32US;
491 val = LPSS_LTR_MAX_VAL;
492 } else if (val > LPSS_LTR_MAX_VAL) {
493 ltr_val |= LPSS_LTR_SNOOP_LAT_32US | LPSS_LTR_SNOOP_REQ;
494 val >>= LPSS_LTR_SNOOP_LAT_SHIFT;
495 } else {
496 ltr_val |= LPSS_LTR_SNOOP_LAT_1US | LPSS_LTR_SNOOP_REQ;
497 }
498 ltr_val |= val;
499 __lpss_reg_write(ltr_val, pdata, LPSS_SW_LTR);
500 if (!(ltr_mode & LPSS_GENERAL_LTR_MODE_SW)) {
501 ltr_mode |= LPSS_GENERAL_LTR_MODE_SW;
502 __lpss_reg_write(ltr_mode, pdata, LPSS_GENERAL);
503 }
504}
505
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300506#ifdef CONFIG_PM
507/**
508 * acpi_lpss_save_ctx() - Save the private registers of LPSS device
509 * @dev: LPSS device
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200510 * @pdata: pointer to the private data of the LPSS device
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300511 *
512 * Most LPSS devices have private registers which may loose their context when
513 * the device is powered down. acpi_lpss_save_ctx() saves those registers into
514 * prv_reg_ctx array.
515 */
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200516static void acpi_lpss_save_ctx(struct device *dev,
517 struct lpss_private_data *pdata)
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300518{
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300519 unsigned int i;
520
521 for (i = 0; i < LPSS_PRV_REG_COUNT; i++) {
522 unsigned long offset = i * sizeof(u32);
523
524 pdata->prv_reg_ctx[i] = __lpss_reg_read(pdata, offset);
525 dev_dbg(dev, "saving 0x%08x from LPSS reg at offset 0x%02lx\n",
526 pdata->prv_reg_ctx[i], offset);
527 }
528}
529
530/**
531 * acpi_lpss_restore_ctx() - Restore the private registers of LPSS device
532 * @dev: LPSS device
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200533 * @pdata: pointer to the private data of the LPSS device
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300534 *
535 * Restores the registers that were previously stored with acpi_lpss_save_ctx().
536 */
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200537static void acpi_lpss_restore_ctx(struct device *dev,
538 struct lpss_private_data *pdata)
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300539{
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300540 unsigned int i;
541
542 /*
543 * The following delay is needed or the subsequent write operations may
544 * fail. The LPSS devices are actually PCI devices and the PCI spec
545 * expects 10ms delay before the device can be accessed after D3 to D0
546 * transition.
547 */
548 msleep(10);
549
550 for (i = 0; i < LPSS_PRV_REG_COUNT; i++) {
551 unsigned long offset = i * sizeof(u32);
552
553 __lpss_reg_write(pdata->prv_reg_ctx[i], pdata, offset);
554 dev_dbg(dev, "restoring 0x%08x to LPSS reg at offset 0x%02lx\n",
555 pdata->prv_reg_ctx[i], offset);
556 }
557}
558
559#ifdef CONFIG_PM_SLEEP
560static int acpi_lpss_suspend_late(struct device *dev)
561{
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200562 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
563 int ret;
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300564
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200565 ret = pm_generic_suspend_late(dev);
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300566 if (ret)
567 return ret;
568
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200569 if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
570 acpi_lpss_save_ctx(dev, pdata);
571
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300572 return acpi_dev_suspend_late(dev);
573}
574
Fu Zhonghuif4168b62014-09-09 16:30:06 +0200575static int acpi_lpss_resume_early(struct device *dev)
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300576{
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200577 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
578 int ret;
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300579
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200580 ret = acpi_dev_resume_early(dev);
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300581 if (ret)
582 return ret;
583
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200584 if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
585 acpi_lpss_restore_ctx(dev, pdata);
586
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300587 return pm_generic_resume_early(dev);
588}
589#endif /* CONFIG_PM_SLEEP */
590
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300591static int acpi_lpss_runtime_suspend(struct device *dev)
592{
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200593 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
594 int ret;
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300595
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200596 ret = pm_generic_runtime_suspend(dev);
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300597 if (ret)
598 return ret;
599
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200600 if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
601 acpi_lpss_save_ctx(dev, pdata);
602
Andy Shevchenko6c17ee42014-11-05 18:34:47 +0200603 ret = acpi_dev_runtime_suspend(dev);
604 if (ret)
605 return ret;
606
607 if (pdata->dev_desc->flags & LPSS_PROXY_REQ && proxy_device)
608 return pm_runtime_put_sync_suspend(proxy_device);
609
610 return 0;
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300611}
612
613static int acpi_lpss_runtime_resume(struct device *dev)
614{
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200615 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
616 int ret;
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300617
Andy Shevchenko6c17ee42014-11-05 18:34:47 +0200618 if (pdata->dev_desc->flags & LPSS_PROXY_REQ && proxy_device) {
619 ret = pm_runtime_get_sync(proxy_device);
620 if (ret)
621 return ret;
622 }
623
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200624 ret = acpi_dev_runtime_resume(dev);
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300625 if (ret)
626 return ret;
627
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200628 if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
629 acpi_lpss_restore_ctx(dev, pdata);
630
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300631 return pm_generic_runtime_resume(dev);
632}
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300633#endif /* CONFIG_PM */
634
635static struct dev_pm_domain acpi_lpss_pm_domain = {
636 .ops = {
Rafael J. Wysocki5de21bb92014-11-27 22:38:23 +0100637#ifdef CONFIG_PM
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300638#ifdef CONFIG_PM_SLEEP
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300639 .prepare = acpi_subsys_prepare,
640 .complete = acpi_subsys_complete,
641 .suspend = acpi_subsys_suspend,
Fu Zhonghuif4168b62014-09-09 16:30:06 +0200642 .suspend_late = acpi_lpss_suspend_late,
643 .resume_early = acpi_lpss_resume_early,
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300644 .freeze = acpi_subsys_freeze,
645 .poweroff = acpi_subsys_suspend,
Fu Zhonghuif4168b62014-09-09 16:30:06 +0200646 .poweroff_late = acpi_lpss_suspend_late,
647 .restore_early = acpi_lpss_resume_early,
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300648#endif
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300649 .runtime_suspend = acpi_lpss_runtime_suspend,
650 .runtime_resume = acpi_lpss_runtime_resume,
651#endif
652 },
653};
654
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100655static int acpi_lpss_platform_notify(struct notifier_block *nb,
656 unsigned long action, void *data)
657{
658 struct platform_device *pdev = to_platform_device(data);
659 struct lpss_private_data *pdata;
660 struct acpi_device *adev;
661 const struct acpi_device_id *id;
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100662
663 id = acpi_match_device(acpi_lpss_device_ids, &pdev->dev);
664 if (!id || !id->driver_data)
665 return 0;
666
667 if (acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
668 return 0;
669
670 pdata = acpi_driver_data(adev);
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200671 if (!pdata)
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100672 return 0;
673
Andy Shevchenkocb39dcd2014-11-05 18:34:45 +0200674 if (pdata->mmio_base &&
675 pdata->mmio_size < pdata->dev_desc->prv_offset + LPSS_LTR_SIZE) {
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100676 dev_err(&pdev->dev, "MMIO size insufficient to access LTR\n");
677 return 0;
678 }
679
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300680 switch (action) {
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300681 case BUS_NOTIFY_ADD_DEVICE:
Andy Shevchenko01ac1702014-11-05 18:34:46 +0200682 pdev->dev.pm_domain = &acpi_lpss_pm_domain;
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300683 if (pdata->dev_desc->flags & LPSS_LTR)
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300684 return sysfs_create_group(&pdev->dev.kobj,
685 &lpss_attr_group);
Andy Shevchenko01ac1702014-11-05 18:34:46 +0200686 break;
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300687 case BUS_NOTIFY_DEL_DEVICE:
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300688 if (pdata->dev_desc->flags & LPSS_LTR)
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300689 sysfs_remove_group(&pdev->dev.kobj, &lpss_attr_group);
Andy Shevchenko01ac1702014-11-05 18:34:46 +0200690 pdev->dev.pm_domain = NULL;
691 break;
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300692 default:
693 break;
694 }
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100695
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300696 return 0;
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100697}
698
699static struct notifier_block acpi_lpss_nb = {
700 .notifier_call = acpi_lpss_platform_notify,
701};
702
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100703static void acpi_lpss_bind(struct device *dev)
704{
705 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
706
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300707 if (!pdata || !pdata->mmio_base || !(pdata->dev_desc->flags & LPSS_LTR))
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100708 return;
709
710 if (pdata->mmio_size >= pdata->dev_desc->prv_offset + LPSS_LTR_SIZE)
711 dev->power.set_latency_tolerance = acpi_lpss_set_ltr;
712 else
713 dev_err(dev, "MMIO size insufficient to access LTR\n");
714}
715
716static void acpi_lpss_unbind(struct device *dev)
717{
718 dev->power.set_latency_tolerance = NULL;
719}
720
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100721static struct acpi_scan_handler lpss_handler = {
722 .ids = acpi_lpss_device_ids,
723 .attach = acpi_lpss_create_device,
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100724 .bind = acpi_lpss_bind,
725 .unbind = acpi_lpss_unbind,
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100726};
727
728void __init acpi_lpss_init(void)
729{
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100730 if (!lpt_clk_init()) {
731 bus_register_notifier(&platform_bus_type, &acpi_lpss_nb);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100732 acpi_scan_add_handler(&lpss_handler);
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100733 }
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100734}
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +0200735
736#else
737
738static struct acpi_scan_handler lpss_handler = {
739 .ids = acpi_lpss_device_ids,
740};
741
742void __init acpi_lpss_init(void)
743{
744 acpi_scan_add_handler(&lpss_handler);
745}
746
747#endif /* CONFIG_X86_INTEL_LPSS */