]>
Commit | Line | Data |
---|---|---|
f58b082a RW |
1 | /* |
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> | |
2e0f8822 | 21 | #include <linux/pm_runtime.h> |
c78b0830 | 22 | #include <linux/delay.h> |
f58b082a RW |
23 | |
24 | #include "internal.h" | |
25 | ||
26 | ACPI_MODULE_NAME("acpi_lpss"); | |
27 | ||
d6ddaaac RW |
28 | #ifdef CONFIG_X86_INTEL_LPSS |
29 | ||
30 | #define LPSS_ADDR(desc) ((unsigned long)&desc) | |
31 | ||
f58b082a | 32 | #define LPSS_CLK_SIZE 0x04 |
2e0f8822 RW |
33 | #define LPSS_LTR_SIZE 0x18 |
34 | ||
35 | /* Offsets relative to LPSS_PRIVATE_OFFSET */ | |
ed3a872e | 36 | #define LPSS_CLK_DIVIDER_DEF_MASK (BIT(1) | BIT(16)) |
765bdd4e MW |
37 | #define LPSS_RESETS 0x04 |
38 | #define LPSS_RESETS_RESET_FUNC BIT(0) | |
39 | #define LPSS_RESETS_RESET_APB BIT(1) | |
2e0f8822 RW |
40 | #define LPSS_GENERAL 0x08 |
41 | #define LPSS_GENERAL_LTR_MODE_SW BIT(2) | |
088f1fd2 | 42 | #define LPSS_GENERAL_UART_RTS_OVRD BIT(3) |
2e0f8822 RW |
43 | #define LPSS_SW_LTR 0x10 |
44 | #define LPSS_AUTO_LTR 0x14 | |
1a8f8351 RW |
45 | #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 | |
06d86415 HK |
52 | #define LPSS_TX_INT 0x20 |
53 | #define LPSS_TX_INT_MASK BIT(1) | |
f58b082a | 54 | |
c78b0830 HK |
55 | #define LPSS_PRV_REG_COUNT 9 |
56 | ||
ff8c1af5 HK |
57 | /* 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 | ||
f6272170 MW |
64 | struct lpss_shared_clock { |
65 | const char *name; | |
66 | unsigned long rate; | |
67 | struct clk *clk; | |
68 | }; | |
69 | ||
06d86415 | 70 | struct lpss_private_data; |
f58b082a RW |
71 | |
72 | struct lpss_device_desc { | |
ff8c1af5 | 73 | unsigned int flags; |
2e0f8822 | 74 | unsigned int prv_offset; |
958c4eb2 | 75 | size_t prv_size_override; |
f6272170 | 76 | struct lpss_shared_clock *shared_clock; |
06d86415 | 77 | void (*setup)(struct lpss_private_data *pdata); |
f58b082a RW |
78 | }; |
79 | ||
b59cc200 | 80 | static struct lpss_device_desc lpss_dma_desc = { |
ff8c1af5 | 81 | .flags = LPSS_CLK, |
b59cc200 RW |
82 | }; |
83 | ||
f58b082a RW |
84 | struct lpss_private_data { |
85 | void __iomem *mmio_base; | |
86 | resource_size_t mmio_size; | |
03f09f73 | 87 | unsigned int fixed_clk_rate; |
f58b082a RW |
88 | struct clk *clk; |
89 | const struct lpss_device_desc *dev_desc; | |
c78b0830 | 90 | u32 prv_reg_ctx[LPSS_PRV_REG_COUNT]; |
f58b082a RW |
91 | }; |
92 | ||
06d86415 HK |
93 | static void lpss_uart_setup(struct lpss_private_data *pdata) |
94 | { | |
088f1fd2 | 95 | unsigned int offset; |
06d86415 HK |
96 | u32 reg; |
97 | ||
088f1fd2 HK |
98 | 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); | |
06d86415 HK |
105 | } |
106 | ||
03f09f73 | 107 | static void byt_i2c_setup(struct lpss_private_data *pdata) |
765bdd4e MW |
108 | { |
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); | |
03f09f73 HK |
116 | |
117 | if (readl(pdata->mmio_base + pdata->dev_desc->prv_offset)) | |
118 | pdata->fixed_clk_rate = 133000000; | |
765bdd4e MW |
119 | } |
120 | ||
f58b082a | 121 | static struct lpss_device_desc lpt_dev_desc = { |
ff8c1af5 | 122 | .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_LTR, |
ed3a872e | 123 | .prv_offset = 0x800, |
ed3a872e HK |
124 | }; |
125 | ||
126 | static struct lpss_device_desc lpt_i2c_dev_desc = { | |
ff8c1af5 | 127 | .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_LTR, |
2e0f8822 | 128 | .prv_offset = 0x800, |
2e0f8822 RW |
129 | }; |
130 | ||
06d86415 | 131 | static struct lpss_device_desc lpt_uart_dev_desc = { |
ff8c1af5 | 132 | .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_LTR, |
06d86415 | 133 | .prv_offset = 0x800, |
06d86415 | 134 | .setup = lpss_uart_setup, |
2e0f8822 RW |
135 | }; |
136 | ||
137 | static struct lpss_device_desc lpt_sdio_dev_desc = { | |
ff8c1af5 | 138 | .flags = LPSS_LTR, |
2e0f8822 | 139 | .prv_offset = 0x1000, |
958c4eb2 | 140 | .prv_size_override = 0x1018, |
f58b082a RW |
141 | }; |
142 | ||
e1c74817 CCE |
143 | static struct lpss_shared_clock pwm_clock = { |
144 | .name = "pwm_clk", | |
145 | .rate = 25000000, | |
146 | }; | |
147 | ||
148 | static struct lpss_device_desc byt_pwm_dev_desc = { | |
ff8c1af5 | 149 | .flags = LPSS_CLK | LPSS_SAVE_CTX, |
e1c74817 CCE |
150 | .shared_clock = &pwm_clock, |
151 | }; | |
152 | ||
f6272170 | 153 | static struct lpss_device_desc byt_uart_dev_desc = { |
ff8c1af5 | 154 | .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_SAVE_CTX, |
f6272170 | 155 | .prv_offset = 0x800, |
06d86415 | 156 | .setup = lpss_uart_setup, |
f6272170 MW |
157 | }; |
158 | ||
f6272170 | 159 | static struct lpss_device_desc byt_spi_dev_desc = { |
ff8c1af5 | 160 | .flags = LPSS_CLK | LPSS_CLK_GATE | LPSS_CLK_DIVIDER | LPSS_SAVE_CTX, |
f6272170 | 161 | .prv_offset = 0x400, |
f6272170 MW |
162 | }; |
163 | ||
164 | static struct lpss_device_desc byt_sdio_dev_desc = { | |
ff8c1af5 | 165 | .flags = LPSS_CLK, |
f6272170 MW |
166 | }; |
167 | ||
f6272170 | 168 | static struct lpss_device_desc byt_i2c_dev_desc = { |
ff8c1af5 | 169 | .flags = LPSS_CLK | LPSS_SAVE_CTX, |
f6272170 | 170 | .prv_offset = 0x800, |
03f09f73 | 171 | .setup = byt_i2c_setup, |
f6272170 MW |
172 | }; |
173 | ||
1bfbd8eb AC |
174 | static struct lpss_shared_clock bsw_pwm_clock = { |
175 | .name = "pwm_clk", | |
176 | .rate = 19200000, | |
177 | }; | |
178 | ||
179 | static struct lpss_device_desc bsw_pwm_dev_desc = { | |
ff8c1af5 | 180 | .flags = LPSS_CLK | LPSS_SAVE_CTX, |
1bfbd8eb AC |
181 | .shared_clock = &bsw_pwm_clock, |
182 | }; | |
183 | ||
d6ddaaac RW |
184 | #else |
185 | ||
186 | #define LPSS_ADDR(desc) (0UL) | |
187 | ||
188 | #endif /* CONFIG_X86_INTEL_LPSS */ | |
189 | ||
f58b082a | 190 | static const struct acpi_device_id acpi_lpss_device_ids[] = { |
b59cc200 | 191 | /* Generic LPSS devices */ |
d6ddaaac | 192 | { "INTL9C60", LPSS_ADDR(lpss_dma_desc) }, |
b59cc200 | 193 | |
f58b082a | 194 | /* Lynxpoint LPSS devices */ |
d6ddaaac RW |
195 | { "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) }, | |
f58b082a RW |
202 | { "INT33C7", }, |
203 | ||
f6272170 | 204 | /* BayTrail LPSS devices */ |
d6ddaaac RW |
205 | { "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) }, | |
f6272170 | 210 | { "INT33B2", }, |
20482d32 | 211 | { "INT33FC", }, |
f6272170 | 212 | |
1bfbd8eb AC |
213 | /* 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 | ||
d6ddaaac RW |
219 | { "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) }, | |
a4d97536 MW |
226 | { "INT3437", }, |
227 | ||
ff8c1af5 HK |
228 | /* Wildcat Point LPSS devices */ |
229 | { "INT3438", LPSS_ADDR(lpt_dev_desc) }, | |
43218a1b | 230 | |
f58b082a RW |
231 | { } |
232 | }; | |
233 | ||
d6ddaaac RW |
234 | #ifdef CONFIG_X86_INTEL_LPSS |
235 | ||
f58b082a RW |
236 | static 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. */ | |
243 | static struct platform_device *lpss_clk_dev; | |
244 | ||
245 | static inline void lpt_register_clock_device(void) | |
246 | { | |
247 | lpss_clk_dev = platform_device_register_simple("clk-lpt", -1, NULL, 0); | |
248 | } | |
249 | ||
250 | static 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; | |
f6272170 | 254 | struct lpss_shared_clock *shared_clock = dev_desc->shared_clock; |
ed3a872e | 255 | const char *devname = dev_name(&adev->dev); |
f6272170 | 256 | struct clk *clk = ERR_PTR(-ENODEV); |
b59cc200 | 257 | struct lpss_clk_data *clk_data; |
ed3a872e HK |
258 | const char *parent, *clk_name; |
259 | void __iomem *prv_base; | |
f58b082a RW |
260 | |
261 | if (!lpss_clk_dev) | |
262 | lpt_register_clock_device(); | |
263 | ||
b59cc200 RW |
264 | clk_data = platform_get_drvdata(lpss_clk_dev); |
265 | if (!clk_data) | |
266 | return -ENODEV; | |
b0d00f8b | 267 | clk = clk_data->clk; |
b59cc200 RW |
268 | |
269 | if (!pdata->mmio_base | |
2e0f8822 | 270 | || pdata->mmio_size < dev_desc->prv_offset + LPSS_CLK_SIZE) |
f58b082a RW |
271 | return -ENODATA; |
272 | ||
f6272170 | 273 | parent = clk_data->name; |
ed3a872e | 274 | prv_base = pdata->mmio_base + dev_desc->prv_offset; |
f6272170 MW |
275 | |
276 | 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 | ||
03f09f73 HK |
287 | 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 | ||
ff8c1af5 | 293 | if (dev_desc->flags & LPSS_CLK_GATE) { |
ed3a872e HK |
294 | clk = clk_register_gate(NULL, devname, parent, 0, |
295 | prv_base, 0, 0, NULL); | |
296 | parent = devname; | |
297 | } | |
298 | ||
ff8c1af5 | 299 | if (dev_desc->flags & LPSS_CLK_DIVIDER) { |
ed3a872e HK |
300 | /* 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); | |
f6272170 | 322 | } |
03f09f73 | 323 | out: |
f6272170 MW |
324 | if (IS_ERR(clk)) |
325 | return PTR_ERR(clk); | |
f58b082a | 326 | |
ed3a872e HK |
327 | pdata->clk = clk; |
328 | clk_register_clkdev(clk, NULL, devname); | |
f58b082a RW |
329 | return 0; |
330 | } | |
331 | ||
332 | static 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; | |
8ce62f85 | 339 | struct platform_device *pdev; |
f58b082a RW |
340 | int ret; |
341 | ||
342 | dev_desc = (struct lpss_device_desc *)id->driver_data; | |
8ce62f85 RW |
343 | if (!dev_desc) { |
344 | pdev = acpi_create_platform_device(adev); | |
345 | return IS_ERR_OR_NULL(pdev) ? PTR_ERR(pdev) : 1; | |
346 | } | |
f58b082a RW |
347 | 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) { | |
958c4eb2 MW |
358 | 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); | |
f58b082a RW |
362 | pdata->mmio_base = ioremap(rentry->res.start, |
363 | pdata->mmio_size); | |
f58b082a RW |
364 | break; |
365 | } | |
366 | ||
367 | acpi_dev_free_resource_list(&resource_list); | |
368 | ||
af65cfe9 MW |
369 | pdata->dev_desc = dev_desc; |
370 | ||
03f09f73 HK |
371 | if (dev_desc->setup) |
372 | dev_desc->setup(pdata); | |
373 | ||
ff8c1af5 | 374 | if (dev_desc->flags & LPSS_CLK) { |
f58b082a RW |
375 | ret = register_device_clock(adev, pdata); |
376 | if (ret) { | |
b9e95fc6 RW |
377 | /* Skip the device, but continue the namespace scan. */ |
378 | ret = 0; | |
379 | goto err_out; | |
f58b082a RW |
380 | } |
381 | } | |
382 | ||
b9e95fc6 RW |
383 | /* |
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 | ||
f58b082a | 395 | adev->driver_data = pdata; |
8ce62f85 RW |
396 | pdev = acpi_create_platform_device(adev); |
397 | if (!IS_ERR_OR_NULL(pdev)) { | |
398 | device_enable_async_suspend(&pdev->dev); | |
399 | return 1; | |
400 | } | |
f58b082a | 401 | |
8ce62f85 | 402 | ret = PTR_ERR(pdev); |
f58b082a RW |
403 | adev->driver_data = NULL; |
404 | ||
405 | err_out: | |
406 | kfree(pdata); | |
407 | return ret; | |
408 | } | |
409 | ||
1a8f8351 RW |
410 | static 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 | ||
415 | static 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 | ||
2e0f8822 RW |
421 | static 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 | } | |
1a8f8351 | 442 | *val = __lpss_reg_read(pdata, reg); |
2e0f8822 RW |
443 | |
444 | out: | |
445 | spin_unlock_irqrestore(&dev->power.lock, flags); | |
446 | return ret; | |
447 | } | |
448 | ||
449 | static 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, <r_value); | |
458 | if (ret) | |
459 | return ret; | |
460 | ||
461 | return snprintf(buf, PAGE_SIZE, "%08x\n", ltr_value); | |
462 | } | |
463 | ||
464 | static 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, <r_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 | ||
479 | static DEVICE_ATTR(auto_ltr, S_IRUSR, lpss_ltr_show, NULL); | |
480 | static DEVICE_ATTR(sw_ltr, S_IRUSR, lpss_ltr_show, NULL); | |
481 | static DEVICE_ATTR(ltr_mode, S_IRUSR, lpss_ltr_mode_show, NULL); | |
482 | ||
483 | static 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 | ||
490 | static struct attribute_group lpss_attr_group = { | |
491 | .attrs = lpss_attrs, | |
492 | .name = "lpss_ltr", | |
493 | }; | |
494 | ||
1a8f8351 RW |
495 | static 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 | ||
c78b0830 HK |
526 | #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 | */ | |
535 | static 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 | */ | |
555 | static 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 | |
578 | static 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 | ||
589 | static 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 | |
602 | static 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 | ||
613 | static 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 | ||
626 | static 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 | ||
2e0f8822 RW |
646 | static 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; | |
2e0f8822 RW |
653 | |
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); | |
c78b0830 | 662 | if (!pdata || !pdata->mmio_base) |
2e0f8822 RW |
663 | 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 | ||
c78b0830 HK |
670 | switch (action) { |
671 | case BUS_NOTIFY_BOUND_DRIVER: | |
ff8c1af5 | 672 | if (pdata->dev_desc->flags & LPSS_SAVE_CTX) |
c78b0830 HK |
673 | pdev->dev.pm_domain = &acpi_lpss_pm_domain; |
674 | break; | |
675 | case BUS_NOTIFY_UNBOUND_DRIVER: | |
ff8c1af5 | 676 | if (pdata->dev_desc->flags & LPSS_SAVE_CTX) |
c78b0830 HK |
677 | pdev->dev.pm_domain = NULL; |
678 | break; | |
679 | case BUS_NOTIFY_ADD_DEVICE: | |
ff8c1af5 | 680 | if (pdata->dev_desc->flags & LPSS_LTR) |
c78b0830 HK |
681 | return sysfs_create_group(&pdev->dev.kobj, |
682 | &lpss_attr_group); | |
683 | case BUS_NOTIFY_DEL_DEVICE: | |
ff8c1af5 | 684 | if (pdata->dev_desc->flags & LPSS_LTR) |
c78b0830 HK |
685 | sysfs_remove_group(&pdev->dev.kobj, &lpss_attr_group); |
686 | default: | |
687 | break; | |
688 | } | |
2e0f8822 | 689 | |
c78b0830 | 690 | return 0; |
2e0f8822 RW |
691 | } |
692 | ||
693 | static struct notifier_block acpi_lpss_nb = { | |
694 | .notifier_call = acpi_lpss_platform_notify, | |
695 | }; | |
696 | ||
1a8f8351 RW |
697 | static void acpi_lpss_bind(struct device *dev) |
698 | { | |
699 | struct lpss_private_data *pdata = acpi_driver_data(ACPI_COMPANION(dev)); | |
700 | ||
ff8c1af5 | 701 | if (!pdata || !pdata->mmio_base || !(pdata->dev_desc->flags & LPSS_LTR)) |
1a8f8351 RW |
702 | 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 | ||
710 | static void acpi_lpss_unbind(struct device *dev) | |
711 | { | |
712 | dev->power.set_latency_tolerance = NULL; | |
713 | } | |
714 | ||
f58b082a RW |
715 | static struct acpi_scan_handler lpss_handler = { |
716 | .ids = acpi_lpss_device_ids, | |
717 | .attach = acpi_lpss_create_device, | |
1a8f8351 RW |
718 | .bind = acpi_lpss_bind, |
719 | .unbind = acpi_lpss_unbind, | |
f58b082a RW |
720 | }; |
721 | ||
722 | void __init acpi_lpss_init(void) | |
723 | { | |
2e0f8822 RW |
724 | if (!lpt_clk_init()) { |
725 | bus_register_notifier(&platform_bus_type, &acpi_lpss_nb); | |
f58b082a | 726 | acpi_scan_add_handler(&lpss_handler); |
2e0f8822 | 727 | } |
f58b082a | 728 | } |
d6ddaaac RW |
729 | |
730 | #else | |
731 | ||
732 | static struct acpi_scan_handler lpss_handler = { | |
733 | .ids = acpi_lpss_device_ids, | |
734 | }; | |
735 | ||
736 | void __init acpi_lpss_init(void) | |
737 | { | |
738 | acpi_scan_add_handler(&lpss_handler); | |
739 | } | |
740 | ||
741 | #endif /* CONFIG_X86_INTEL_LPSS */ |