blob: b693098f2160b40e2caeb131174ff204e9ebd6da [file] [log] [blame]
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01001/*
2 * ACPI support for Intel Lynxpoint LPSS.
3 *
4 * Copyright (C) 2013, Intel Corporation
5 * 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)
63
Mika Westerbergf6272172013-05-13 12:42:44 +000064struct lpss_shared_clock {
65 const char *name;
66 unsigned long rate;
67 struct clk *clk;
68};
69
Heikki Krogerus06d86412013-06-17 13:25:46 +030070struct lpss_private_data;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010071
72struct lpss_device_desc {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +030073 unsigned int flags;
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010074 unsigned int prv_offset;
Mika Westerberg958c4eb2013-06-18 16:51:35 +030075 size_t prv_size_override;
Mika Westerbergf6272172013-05-13 12:42:44 +000076 struct lpss_shared_clock *shared_clock;
Heikki Krogerus06d86412013-06-17 13:25:46 +030077 void (*setup)(struct lpss_private_data *pdata);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010078};
79
Rafael J. Wysockib59cc202013-05-08 11:55:49 +030080static struct lpss_device_desc lpss_dma_desc = {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +030081 .flags = LPSS_CLK,
Rafael J. Wysockib59cc202013-05-08 11:55:49 +030082};
83
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010084struct lpss_private_data {
85 void __iomem *mmio_base;
86 resource_size_t mmio_size;
Heikki Krogerus03f09f72014-09-02 10:55:09 +030087 unsigned int fixed_clk_rate;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010088 struct clk *clk;
89 const struct lpss_device_desc *dev_desc;
Heikki Krogerusc78b0832014-05-23 16:15:09 +030090 u32 prv_reg_ctx[LPSS_PRV_REG_COUNT];
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010091};
92
Heikki Krogerus06d86412013-06-17 13:25:46 +030093static void lpss_uart_setup(struct lpss_private_data *pdata)
94{
Heikki Krogerus088f1fd2013-10-09 09:49:20 +030095 unsigned int offset;
Heikki Krogerus06d86412013-06-17 13:25:46 +030096 u32 reg;
97
Heikki Krogerus088f1fd2013-10-09 09:49:20 +030098 offset = pdata->dev_desc->prv_offset + LPSS_TX_INT;
99 reg = readl(pdata->mmio_base + offset);
100 writel(reg | LPSS_TX_INT_MASK, pdata->mmio_base + offset);
101
102 offset = pdata->dev_desc->prv_offset + LPSS_GENERAL;
103 reg = readl(pdata->mmio_base + offset);
104 writel(reg | LPSS_GENERAL_UART_RTS_OVRD, pdata->mmio_base + offset);
Heikki Krogerus06d86412013-06-17 13:25:46 +0300105}
106
Heikki Krogerus03f09f72014-09-02 10:55:09 +0300107static void byt_i2c_setup(struct lpss_private_data *pdata)
Mika Westerberg765bdd42014-06-17 14:33:39 +0300108{
109 unsigned int offset;
110 u32 val;
111
112 offset = pdata->dev_desc->prv_offset + LPSS_RESETS;
113 val = readl(pdata->mmio_base + offset);
114 val |= LPSS_RESETS_RESET_APB | LPSS_RESETS_RESET_FUNC;
115 writel(val, pdata->mmio_base + offset);
Heikki Krogerus03f09f72014-09-02 10:55:09 +0300116
117 if (readl(pdata->mmio_base + pdata->dev_desc->prv_offset))
118 pdata->fixed_clk_rate = 133000000;
Mika Westerberg765bdd42014-06-17 14:33:39 +0300119}
120
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100121static struct lpss_device_desc lpt_dev_desc = {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300122 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_LTR,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100123 .prv_offset = 0x800,
Heikki Krogerused3a8722014-05-19 14:42:07 +0300124};
125
126static struct lpss_device_desc lpt_i2c_dev_desc = {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300127 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_LTR,
Heikki Krogerused3a8722014-05-19 14:42:07 +0300128 .prv_offset = 0x800,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100129};
130
Heikki Krogerus06d86412013-06-17 13:25:46 +0300131static struct lpss_device_desc lpt_uart_dev_desc = {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300132 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_LTR,
Heikki Krogerus06d86412013-06-17 13:25:46 +0300133 .prv_offset = 0x800,
Heikki Krogerus06d86412013-06-17 13:25:46 +0300134 .setup = lpss_uart_setup,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100135};
136
137static struct lpss_device_desc lpt_sdio_dev_desc = {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300138 .flags = LPSS_LTR,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100139 .prv_offset = 0x1000,
Mika Westerberg958c4eb2013-06-18 16:51:35 +0300140 .prv_size_override = 0x1018,
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100141};
142
Chew, Chiau Eee1c74812014-02-19 02:24:29 +0800143static struct lpss_shared_clock pwm_clock = {
144 .name = "pwm_clk",
145 .rate = 25000000,
146};
147
148static struct lpss_device_desc byt_pwm_dev_desc = {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300149 .flags = LPSS_CLK | LPSS_SAVE_CTX,
Chew, Chiau Eee1c74812014-02-19 02:24:29 +0800150 .shared_clock = &pwm_clock,
151};
152
Mika Westerbergf6272172013-05-13 12:42:44 +0000153static struct lpss_device_desc byt_uart_dev_desc = {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300154 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_SAVE_CTX,
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 = {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300160 .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_SAVE_CTX,
Mika Westerbergf6272172013-05-13 12:42:44 +0000161 .prv_offset = 0x400,
Mika Westerbergf6272172013-05-13 12:42:44 +0000162};
163
164static struct lpss_device_desc byt_sdio_dev_desc = {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300165 .flags = LPSS_CLK,
Mika Westerbergf6272172013-05-13 12:42:44 +0000166};
167
Mika Westerbergf6272172013-05-13 12:42:44 +0000168static struct lpss_device_desc byt_i2c_dev_desc = {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300169 .flags = LPSS_CLK | LPSS_SAVE_CTX,
Mika Westerbergf6272172013-05-13 12:42:44 +0000170 .prv_offset = 0x800,
Heikki Krogerus03f09f72014-09-02 10:55:09 +0300171 .setup = byt_i2c_setup,
Mika Westerbergf6272172013-05-13 12:42:44 +0000172};
173
Alan Cox1bfbd8e2014-08-19 15:55:22 +0300174static struct lpss_shared_clock bsw_pwm_clock = {
175 .name = "pwm_clk",
176 .rate = 19200000,
177};
178
179static struct lpss_device_desc bsw_pwm_dev_desc = {
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300180 .flags = LPSS_CLK | LPSS_SAVE_CTX,
Alan Cox1bfbd8e2014-08-19 15:55:22 +0300181 .shared_clock = &bsw_pwm_clock,
182};
183
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +0200184#else
185
186#define LPSS_ADDR(desc) (0UL)
187
188#endif /* CONFIG_X86_INTEL_LPSS */
189
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100190static const struct acpi_device_id acpi_lpss_device_ids[] = {
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300191 /* Generic LPSS devices */
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +0200192 { "INTL9C60", LPSS_ADDR(lpss_dma_desc) },
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300193
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100194 /* Lynxpoint LPSS devices */
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +0200195 { "INT33C0", LPSS_ADDR(lpt_dev_desc) },
196 { "INT33C1", LPSS_ADDR(lpt_dev_desc) },
197 { "INT33C2", LPSS_ADDR(lpt_i2c_dev_desc) },
198 { "INT33C3", LPSS_ADDR(lpt_i2c_dev_desc) },
199 { "INT33C4", LPSS_ADDR(lpt_uart_dev_desc) },
200 { "INT33C5", LPSS_ADDR(lpt_uart_dev_desc) },
201 { "INT33C6", LPSS_ADDR(lpt_sdio_dev_desc) },
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100202 { "INT33C7", },
203
Mika Westerbergf6272172013-05-13 12:42:44 +0000204 /* BayTrail LPSS devices */
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +0200205 { "80860F09", LPSS_ADDR(byt_pwm_dev_desc) },
206 { "80860F0A", LPSS_ADDR(byt_uart_dev_desc) },
207 { "80860F0E", LPSS_ADDR(byt_spi_dev_desc) },
208 { "80860F14", LPSS_ADDR(byt_sdio_dev_desc) },
209 { "80860F41", LPSS_ADDR(byt_i2c_dev_desc) },
Mika Westerbergf6272172013-05-13 12:42:44 +0000210 { "INT33B2", },
Jin Yao20482d32014-05-15 18:28:46 +0300211 { "INT33FC", },
Mika Westerbergf6272172013-05-13 12:42:44 +0000212
Alan Cox1bfbd8e2014-08-19 15:55:22 +0300213 /* Braswell LPSS devices */
214 { "80862288", LPSS_ADDR(bsw_pwm_dev_desc) },
215 { "8086228A", LPSS_ADDR(byt_uart_dev_desc) },
216 { "8086228E", LPSS_ADDR(byt_spi_dev_desc) },
217 { "808622C1", LPSS_ADDR(byt_i2c_dev_desc) },
218
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +0200219 { "INT3430", LPSS_ADDR(lpt_dev_desc) },
220 { "INT3431", LPSS_ADDR(lpt_dev_desc) },
221 { "INT3432", LPSS_ADDR(lpt_i2c_dev_desc) },
222 { "INT3433", LPSS_ADDR(lpt_i2c_dev_desc) },
223 { "INT3434", LPSS_ADDR(lpt_uart_dev_desc) },
224 { "INT3435", LPSS_ADDR(lpt_uart_dev_desc) },
225 { "INT3436", LPSS_ADDR(lpt_sdio_dev_desc) },
Mika Westerberga4d97532013-11-12 11:48:19 +0200226 { "INT3437", },
227
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300228 /* Wildcat Point LPSS devices */
229 { "INT3438", LPSS_ADDR(lpt_dev_desc) },
Jie Yang43218a12014-08-01 09:06:35 +0800230
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100231 { }
232};
233
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +0200234#ifdef CONFIG_X86_INTEL_LPSS
235
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100236static int is_memory(struct acpi_resource *res, void *not_used)
237{
238 struct resource r;
239 return !acpi_dev_resource_memory(res, &r);
240}
241
242/* LPSS main clock device. */
243static struct platform_device *lpss_clk_dev;
244
245static inline void lpt_register_clock_device(void)
246{
247 lpss_clk_dev = platform_device_register_simple("clk-lpt", -1, NULL, 0);
248}
249
250static int register_device_clock(struct acpi_device *adev,
251 struct lpss_private_data *pdata)
252{
253 const struct lpss_device_desc *dev_desc = pdata->dev_desc;
Mika Westerbergf6272172013-05-13 12:42:44 +0000254 struct lpss_shared_clock *shared_clock = dev_desc->shared_clock;
Heikki Krogerused3a8722014-05-19 14:42:07 +0300255 const char *devname = dev_name(&adev->dev);
Mika Westerbergf6272172013-05-13 12:42:44 +0000256 struct clk *clk = ERR_PTR(-ENODEV);
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300257 struct lpss_clk_data *clk_data;
Heikki Krogerused3a8722014-05-19 14:42:07 +0300258 const char *parent, *clk_name;
259 void __iomem *prv_base;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100260
261 if (!lpss_clk_dev)
262 lpt_register_clock_device();
263
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300264 clk_data = platform_get_drvdata(lpss_clk_dev);
265 if (!clk_data)
266 return -ENODEV;
Heikki Krogerusb0d00f82014-09-02 10:55:08 +0300267 clk = clk_data->clk;
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300268
269 if (!pdata->mmio_base
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100270 || pdata->mmio_size < dev_desc->prv_offset + LPSS_CLK_SIZE)
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100271 return -ENODATA;
272
Mika Westerbergf6272172013-05-13 12:42:44 +0000273 parent = clk_data->name;
Heikki Krogerused3a8722014-05-19 14:42:07 +0300274 prv_base = pdata->mmio_base + dev_desc->prv_offset;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100275
Mika Westerbergf6272172013-05-13 12:42:44 +0000276 if (shared_clock) {
277 clk = shared_clock->clk;
278 if (!clk) {
279 clk = clk_register_fixed_rate(NULL, shared_clock->name,
280 "lpss_clk", 0,
281 shared_clock->rate);
282 shared_clock->clk = clk;
283 }
284 parent = shared_clock->name;
285 }
286
Heikki Krogerus03f09f72014-09-02 10:55:09 +0300287 if (pdata->fixed_clk_rate) {
288 clk = clk_register_fixed_rate(NULL, devname, parent, 0,
289 pdata->fixed_clk_rate);
290 goto out;
291 }
292
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300293 if (dev_desc->flags & LPSS_CLK_GATE) {
Heikki Krogerused3a8722014-05-19 14:42:07 +0300294 clk = clk_register_gate(NULL, devname, parent, 0,
295 prv_base, 0, 0, NULL);
296 parent = devname;
297 }
298
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300299 if (dev_desc->flags & LPSS_CLK_DIVIDER) {
Heikki Krogerused3a8722014-05-19 14:42:07 +0300300 /* Prevent division by zero */
301 if (!readl(prv_base))
302 writel(LPSS_CLK_DIVIDER_DEF_MASK, prv_base);
303
304 clk_name = kasprintf(GFP_KERNEL, "%s-div", devname);
305 if (!clk_name)
306 return -ENOMEM;
307 clk = clk_register_fractional_divider(NULL, clk_name, parent,
308 0, prv_base,
309 1, 15, 16, 15, 0, NULL);
310 parent = clk_name;
311
312 clk_name = kasprintf(GFP_KERNEL, "%s-update", devname);
313 if (!clk_name) {
314 kfree(parent);
315 return -ENOMEM;
316 }
317 clk = clk_register_gate(NULL, clk_name, parent,
318 CLK_SET_RATE_PARENT | CLK_SET_RATE_GATE,
319 prv_base, 31, 0, NULL);
320 kfree(parent);
321 kfree(clk_name);
Mika Westerbergf6272172013-05-13 12:42:44 +0000322 }
Heikki Krogerus03f09f72014-09-02 10:55:09 +0300323out:
Mika Westerbergf6272172013-05-13 12:42:44 +0000324 if (IS_ERR(clk))
325 return PTR_ERR(clk);
326
Heikki Krogerused3a8722014-05-19 14:42:07 +0300327 pdata->clk = clk;
328 clk_register_clkdev(clk, NULL, devname);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100329 return 0;
330}
331
332static int acpi_lpss_create_device(struct acpi_device *adev,
333 const struct acpi_device_id *id)
334{
335 struct lpss_device_desc *dev_desc;
336 struct lpss_private_data *pdata;
337 struct resource_list_entry *rentry;
338 struct list_head resource_list;
Rafael J. Wysocki8ce62f82014-05-25 14:38:52 +0200339 struct platform_device *pdev;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100340 int ret;
341
342 dev_desc = (struct lpss_device_desc *)id->driver_data;
Rafael J. Wysocki8ce62f82014-05-25 14:38:52 +0200343 if (!dev_desc) {
344 pdev = acpi_create_platform_device(adev);
345 return IS_ERR_OR_NULL(pdev) ? PTR_ERR(pdev) : 1;
346 }
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100347 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
348 if (!pdata)
349 return -ENOMEM;
350
351 INIT_LIST_HEAD(&resource_list);
352 ret = acpi_dev_get_resources(adev, &resource_list, is_memory, NULL);
353 if (ret < 0)
354 goto err_out;
355
356 list_for_each_entry(rentry, &resource_list, node)
357 if (resource_type(&rentry->res) == IORESOURCE_MEM) {
Mika Westerberg958c4eb2013-06-18 16:51:35 +0300358 if (dev_desc->prv_size_override)
359 pdata->mmio_size = dev_desc->prv_size_override;
360 else
361 pdata->mmio_size = resource_size(&rentry->res);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100362 pdata->mmio_base = ioremap(rentry->res.start,
363 pdata->mmio_size);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100364 break;
365 }
366
367 acpi_dev_free_resource_list(&resource_list);
368
Mika Westerbergaf65cfe2013-09-02 13:30:25 +0300369 pdata->dev_desc = dev_desc;
370
Heikki Krogerus03f09f72014-09-02 10:55:09 +0300371 if (dev_desc->setup)
372 dev_desc->setup(pdata);
373
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300374 if (dev_desc->flags & LPSS_CLK) {
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100375 ret = register_device_clock(adev, pdata);
376 if (ret) {
Rafael J. Wysockib9e95fc2013-06-19 00:45:34 +0200377 /* Skip the device, but continue the namespace scan. */
378 ret = 0;
379 goto err_out;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100380 }
381 }
382
Rafael J. Wysockib9e95fc2013-06-19 00:45:34 +0200383 /*
384 * This works around a known issue in ACPI tables where LPSS devices
385 * have _PS0 and _PS3 without _PSC (and no power resources), so
386 * acpi_bus_init_power() will assume that the BIOS has put them into D0.
387 */
388 ret = acpi_device_fix_up_power(adev);
389 if (ret) {
390 /* Skip the device, but continue the namespace scan. */
391 ret = 0;
392 goto err_out;
393 }
394
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100395 adev->driver_data = pdata;
Rafael J. Wysocki8ce62f82014-05-25 14:38:52 +0200396 pdev = acpi_create_platform_device(adev);
397 if (!IS_ERR_OR_NULL(pdev)) {
398 device_enable_async_suspend(&pdev->dev);
399 return 1;
400 }
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100401
Rafael J. Wysocki8ce62f82014-05-25 14:38:52 +0200402 ret = PTR_ERR(pdev);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100403 adev->driver_data = NULL;
404
405 err_out:
406 kfree(pdata);
407 return ret;
408}
409
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100410static u32 __lpss_reg_read(struct lpss_private_data *pdata, unsigned int reg)
411{
412 return readl(pdata->mmio_base + pdata->dev_desc->prv_offset + reg);
413}
414
415static void __lpss_reg_write(u32 val, struct lpss_private_data *pdata,
416 unsigned int reg)
417{
418 writel(val, pdata->mmio_base + pdata->dev_desc->prv_offset + reg);
419}
420
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100421static int lpss_reg_read(struct device *dev, unsigned int reg, u32 *val)
422{
423 struct acpi_device *adev;
424 struct lpss_private_data *pdata;
425 unsigned long flags;
426 int ret;
427
428 ret = acpi_bus_get_device(ACPI_HANDLE(dev), &adev);
429 if (WARN_ON(ret))
430 return ret;
431
432 spin_lock_irqsave(&dev->power.lock, flags);
433 if (pm_runtime_suspended(dev)) {
434 ret = -EAGAIN;
435 goto out;
436 }
437 pdata = acpi_driver_data(adev);
438 if (WARN_ON(!pdata || !pdata->mmio_base)) {
439 ret = -ENODEV;
440 goto out;
441 }
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100442 *val = __lpss_reg_read(pdata, reg);
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100443
444 out:
445 spin_unlock_irqrestore(&dev->power.lock, flags);
446 return ret;
447}
448
449static ssize_t lpss_ltr_show(struct device *dev, struct device_attribute *attr,
450 char *buf)
451{
452 u32 ltr_value = 0;
453 unsigned int reg;
454 int ret;
455
456 reg = strcmp(attr->attr.name, "auto_ltr") ? LPSS_SW_LTR : LPSS_AUTO_LTR;
457 ret = lpss_reg_read(dev, reg, &ltr_value);
458 if (ret)
459 return ret;
460
461 return snprintf(buf, PAGE_SIZE, "%08x\n", ltr_value);
462}
463
464static ssize_t lpss_ltr_mode_show(struct device *dev,
465 struct device_attribute *attr, char *buf)
466{
467 u32 ltr_mode = 0;
468 char *outstr;
469 int ret;
470
471 ret = lpss_reg_read(dev, LPSS_GENERAL, &ltr_mode);
472 if (ret)
473 return ret;
474
475 outstr = (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) ? "sw" : "auto";
476 return sprintf(buf, "%s\n", outstr);
477}
478
479static DEVICE_ATTR(auto_ltr, S_IRUSR, lpss_ltr_show, NULL);
480static DEVICE_ATTR(sw_ltr, S_IRUSR, lpss_ltr_show, NULL);
481static DEVICE_ATTR(ltr_mode, S_IRUSR, lpss_ltr_mode_show, NULL);
482
483static struct attribute *lpss_attrs[] = {
484 &dev_attr_auto_ltr.attr,
485 &dev_attr_sw_ltr.attr,
486 &dev_attr_ltr_mode.attr,
487 NULL,
488};
489
490static struct attribute_group lpss_attr_group = {
491 .attrs = lpss_attrs,
492 .name = "lpss_ltr",
493};
494
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100495static void acpi_lpss_set_ltr(struct device *dev, s32 val)
496{
497 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
498 u32 ltr_mode, ltr_val;
499
500 ltr_mode = __lpss_reg_read(pdata, LPSS_GENERAL);
501 if (val < 0) {
502 if (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) {
503 ltr_mode &= ~LPSS_GENERAL_LTR_MODE_SW;
504 __lpss_reg_write(ltr_mode, pdata, LPSS_GENERAL);
505 }
506 return;
507 }
508 ltr_val = __lpss_reg_read(pdata, LPSS_SW_LTR) & ~LPSS_LTR_SNOOP_MASK;
509 if (val >= LPSS_LTR_SNOOP_LAT_CUTOFF) {
510 ltr_val |= LPSS_LTR_SNOOP_LAT_32US;
511 val = LPSS_LTR_MAX_VAL;
512 } else if (val > LPSS_LTR_MAX_VAL) {
513 ltr_val |= LPSS_LTR_SNOOP_LAT_32US | LPSS_LTR_SNOOP_REQ;
514 val >>= LPSS_LTR_SNOOP_LAT_SHIFT;
515 } else {
516 ltr_val |= LPSS_LTR_SNOOP_LAT_1US | LPSS_LTR_SNOOP_REQ;
517 }
518 ltr_val |= val;
519 __lpss_reg_write(ltr_val, pdata, LPSS_SW_LTR);
520 if (!(ltr_mode & LPSS_GENERAL_LTR_MODE_SW)) {
521 ltr_mode |= LPSS_GENERAL_LTR_MODE_SW;
522 __lpss_reg_write(ltr_mode, pdata, LPSS_GENERAL);
523 }
524}
525
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300526#ifdef CONFIG_PM
527/**
528 * acpi_lpss_save_ctx() - Save the private registers of LPSS device
529 * @dev: LPSS device
530 *
531 * Most LPSS devices have private registers which may loose their context when
532 * the device is powered down. acpi_lpss_save_ctx() saves those registers into
533 * prv_reg_ctx array.
534 */
535static void acpi_lpss_save_ctx(struct device *dev)
536{
537 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
538 unsigned int i;
539
540 for (i = 0; i < LPSS_PRV_REG_COUNT; i++) {
541 unsigned long offset = i * sizeof(u32);
542
543 pdata->prv_reg_ctx[i] = __lpss_reg_read(pdata, offset);
544 dev_dbg(dev, "saving 0x%08x from LPSS reg at offset 0x%02lx\n",
545 pdata->prv_reg_ctx[i], offset);
546 }
547}
548
549/**
550 * acpi_lpss_restore_ctx() - Restore the private registers of LPSS device
551 * @dev: LPSS device
552 *
553 * Restores the registers that were previously stored with acpi_lpss_save_ctx().
554 */
555static void acpi_lpss_restore_ctx(struct device *dev)
556{
557 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
558 unsigned int i;
559
560 /*
561 * The following delay is needed or the subsequent write operations may
562 * fail. The LPSS devices are actually PCI devices and the PCI spec
563 * expects 10ms delay before the device can be accessed after D3 to D0
564 * transition.
565 */
566 msleep(10);
567
568 for (i = 0; i < LPSS_PRV_REG_COUNT; i++) {
569 unsigned long offset = i * sizeof(u32);
570
571 __lpss_reg_write(pdata->prv_reg_ctx[i], pdata, offset);
572 dev_dbg(dev, "restoring 0x%08x to LPSS reg at offset 0x%02lx\n",
573 pdata->prv_reg_ctx[i], offset);
574 }
575}
576
577#ifdef CONFIG_PM_SLEEP
578static int acpi_lpss_suspend_late(struct device *dev)
579{
580 int ret = pm_generic_suspend_late(dev);
581
582 if (ret)
583 return ret;
584
585 acpi_lpss_save_ctx(dev);
586 return acpi_dev_suspend_late(dev);
587}
588
589static int acpi_lpss_restore_early(struct device *dev)
590{
591 int ret = acpi_dev_resume_early(dev);
592
593 if (ret)
594 return ret;
595
596 acpi_lpss_restore_ctx(dev);
597 return pm_generic_resume_early(dev);
598}
599#endif /* CONFIG_PM_SLEEP */
600
601#ifdef CONFIG_PM_RUNTIME
602static int acpi_lpss_runtime_suspend(struct device *dev)
603{
604 int ret = pm_generic_runtime_suspend(dev);
605
606 if (ret)
607 return ret;
608
609 acpi_lpss_save_ctx(dev);
610 return acpi_dev_runtime_suspend(dev);
611}
612
613static int acpi_lpss_runtime_resume(struct device *dev)
614{
615 int ret = acpi_dev_runtime_resume(dev);
616
617 if (ret)
618 return ret;
619
620 acpi_lpss_restore_ctx(dev);
621 return pm_generic_runtime_resume(dev);
622}
623#endif /* CONFIG_PM_RUNTIME */
624#endif /* CONFIG_PM */
625
626static struct dev_pm_domain acpi_lpss_pm_domain = {
627 .ops = {
628#ifdef CONFIG_PM_SLEEP
629 .suspend_late = acpi_lpss_suspend_late,
630 .restore_early = acpi_lpss_restore_early,
631 .prepare = acpi_subsys_prepare,
632 .complete = acpi_subsys_complete,
633 .suspend = acpi_subsys_suspend,
634 .resume_early = acpi_subsys_resume_early,
635 .freeze = acpi_subsys_freeze,
636 .poweroff = acpi_subsys_suspend,
637 .poweroff_late = acpi_subsys_suspend_late,
638#endif
639#ifdef CONFIG_PM_RUNTIME
640 .runtime_suspend = acpi_lpss_runtime_suspend,
641 .runtime_resume = acpi_lpss_runtime_resume,
642#endif
643 },
644};
645
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100646static int acpi_lpss_platform_notify(struct notifier_block *nb,
647 unsigned long action, void *data)
648{
649 struct platform_device *pdev = to_platform_device(data);
650 struct lpss_private_data *pdata;
651 struct acpi_device *adev;
652 const struct acpi_device_id *id;
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100653
654 id = acpi_match_device(acpi_lpss_device_ids, &pdev->dev);
655 if (!id || !id->driver_data)
656 return 0;
657
658 if (acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
659 return 0;
660
661 pdata = acpi_driver_data(adev);
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300662 if (!pdata || !pdata->mmio_base)
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100663 return 0;
664
665 if (pdata->mmio_size < pdata->dev_desc->prv_offset + LPSS_LTR_SIZE) {
666 dev_err(&pdev->dev, "MMIO size insufficient to access LTR\n");
667 return 0;
668 }
669
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300670 switch (action) {
671 case BUS_NOTIFY_BOUND_DRIVER:
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300672 if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300673 pdev->dev.pm_domain = &acpi_lpss_pm_domain;
674 break;
675 case BUS_NOTIFY_UNBOUND_DRIVER:
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300676 if (pdata->dev_desc->flags & LPSS_SAVE_CTX)
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300677 pdev->dev.pm_domain = NULL;
678 break;
679 case BUS_NOTIFY_ADD_DEVICE:
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300680 if (pdata->dev_desc->flags & LPSS_LTR)
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300681 return sysfs_create_group(&pdev->dev.kobj,
682 &lpss_attr_group);
683 case BUS_NOTIFY_DEL_DEVICE:
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300684 if (pdata->dev_desc->flags & LPSS_LTR)
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300685 sysfs_remove_group(&pdev->dev.kobj, &lpss_attr_group);
686 default:
687 break;
688 }
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100689
Heikki Krogerusc78b0832014-05-23 16:15:09 +0300690 return 0;
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100691}
692
693static struct notifier_block acpi_lpss_nb = {
694 .notifier_call = acpi_lpss_platform_notify,
695};
696
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100697static void acpi_lpss_bind(struct device *dev)
698{
699 struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev));
700
Heikki Krogerusff8c1af2014-09-02 10:55:07 +0300701 if (!pdata || !pdata->mmio_base || !(pdata->dev_desc->flags & LPSS_LTR))
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100702 return;
703
704 if (pdata->mmio_size >= pdata->dev_desc->prv_offset + LPSS_LTR_SIZE)
705 dev->power.set_latency_tolerance = acpi_lpss_set_ltr;
706 else
707 dev_err(dev, "MMIO size insufficient to access LTR\n");
708}
709
710static void acpi_lpss_unbind(struct device *dev)
711{
712 dev->power.set_latency_tolerance = NULL;
713}
714
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100715static struct acpi_scan_handler lpss_handler = {
716 .ids = acpi_lpss_device_ids,
717 .attach = acpi_lpss_create_device,
Rafael J. Wysocki1a8f8352014-02-11 00:35:53 +0100718 .bind = acpi_lpss_bind,
719 .unbind = acpi_lpss_unbind,
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100720};
721
722void __init acpi_lpss_init(void)
723{
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100724 if (!lpt_clk_init()) {
725 bus_register_notifier(&platform_bus_type, &acpi_lpss_nb);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100726 acpi_scan_add_handler(&lpss_handler);
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100727 }
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100728}
Rafael J. Wysockid6ddaaa2014-05-30 14:34:05 +0200729
730#else
731
732static struct acpi_scan_handler lpss_handler = {
733 .ids = acpi_lpss_device_ids,
734};
735
736void __init acpi_lpss_init(void)
737{
738 acpi_scan_add_handler(&lpss_handler);
739}
740
741#endif /* CONFIG_X86_INTEL_LPSS */