blob: fb78bb9ad8f65888817fc6f7a3ecb2563b975134 [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>
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010022
23#include "internal.h"
24
25ACPI_MODULE_NAME("acpi_lpss");
26
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010027#define LPSS_CLK_SIZE 0x04
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010028#define LPSS_LTR_SIZE 0x18
29
30/* Offsets relative to LPSS_PRIVATE_OFFSET */
31#define LPSS_GENERAL 0x08
32#define LPSS_GENERAL_LTR_MODE_SW BIT(2)
33#define LPSS_SW_LTR 0x10
34#define LPSS_AUTO_LTR 0x14
Heikki Krogerus06d86412013-06-17 13:25:46 +030035#define LPSS_TX_INT 0x20
36#define LPSS_TX_INT_MASK BIT(1)
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010037
Mika Westerbergf6272172013-05-13 12:42:44 +000038struct lpss_shared_clock {
39 const char *name;
40 unsigned long rate;
41 struct clk *clk;
42};
43
Heikki Krogerus06d86412013-06-17 13:25:46 +030044struct lpss_private_data;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010045
46struct lpss_device_desc {
47 bool clk_required;
Rafael J. Wysockib59cc202013-05-08 11:55:49 +030048 const char *clkdev_name;
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010049 bool ltr_required;
50 unsigned int prv_offset;
Mika Westerberg958c4eb2013-06-18 16:51:35 +030051 size_t prv_size_override;
Mika Westerbergf6272172013-05-13 12:42:44 +000052 bool clk_gate;
53 struct lpss_shared_clock *shared_clock;
Heikki Krogerus06d86412013-06-17 13:25:46 +030054 void (*setup)(struct lpss_private_data *pdata);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010055};
56
Rafael J. Wysockib59cc202013-05-08 11:55:49 +030057static struct lpss_device_desc lpss_dma_desc = {
58 .clk_required = true,
59 .clkdev_name = "hclk",
60};
61
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010062struct lpss_private_data {
63 void __iomem *mmio_base;
64 resource_size_t mmio_size;
65 struct clk *clk;
66 const struct lpss_device_desc *dev_desc;
67};
68
Heikki Krogerus06d86412013-06-17 13:25:46 +030069static void lpss_uart_setup(struct lpss_private_data *pdata)
70{
71 unsigned int tx_int_offset = pdata->dev_desc->prv_offset + LPSS_TX_INT;
72 u32 reg;
73
74 reg = readl(pdata->mmio_base + tx_int_offset);
75 writel(reg | LPSS_TX_INT_MASK, pdata->mmio_base + tx_int_offset);
76}
77
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010078static struct lpss_device_desc lpt_dev_desc = {
79 .clk_required = true,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010080 .prv_offset = 0x800,
81 .ltr_required = true,
Mika Westerbergf6272172013-05-13 12:42:44 +000082 .clk_gate = true,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010083};
84
Heikki Krogerus06d86412013-06-17 13:25:46 +030085static struct lpss_device_desc lpt_uart_dev_desc = {
86 .clk_required = true,
87 .prv_offset = 0x800,
88 .ltr_required = true,
89 .clk_gate = true,
90 .setup = lpss_uart_setup,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010091};
92
93static struct lpss_device_desc lpt_sdio_dev_desc = {
94 .prv_offset = 0x1000,
Mika Westerberg958c4eb2013-06-18 16:51:35 +030095 .prv_size_override = 0x1018,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010096 .ltr_required = true,
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010097};
98
Mika Westerbergf6272172013-05-13 12:42:44 +000099static struct lpss_shared_clock uart_clock = {
100 .name = "uart_clk",
101 .rate = 44236800,
102};
103
104static struct lpss_device_desc byt_uart_dev_desc = {
105 .clk_required = true,
106 .prv_offset = 0x800,
107 .clk_gate = true,
108 .shared_clock = &uart_clock,
Heikki Krogerus06d86412013-06-17 13:25:46 +0300109 .setup = lpss_uart_setup,
Mika Westerbergf6272172013-05-13 12:42:44 +0000110};
111
112static struct lpss_shared_clock spi_clock = {
113 .name = "spi_clk",
114 .rate = 50000000,
115};
116
117static struct lpss_device_desc byt_spi_dev_desc = {
118 .clk_required = true,
119 .prv_offset = 0x400,
120 .clk_gate = true,
121 .shared_clock = &spi_clock,
122};
123
124static struct lpss_device_desc byt_sdio_dev_desc = {
125 .clk_required = true,
126};
127
128static struct lpss_shared_clock i2c_clock = {
129 .name = "i2c_clk",
130 .rate = 100000000,
131};
132
133static struct lpss_device_desc byt_i2c_dev_desc = {
134 .clk_required = true,
135 .prv_offset = 0x800,
136 .shared_clock = &i2c_clock,
137};
138
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100139static const struct acpi_device_id acpi_lpss_device_ids[] = {
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300140 /* Generic LPSS devices */
141 { "INTL9C60", (unsigned long)&lpss_dma_desc },
142
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100143 /* Lynxpoint LPSS devices */
144 { "INT33C0", (unsigned long)&lpt_dev_desc },
145 { "INT33C1", (unsigned long)&lpt_dev_desc },
146 { "INT33C2", (unsigned long)&lpt_dev_desc },
147 { "INT33C3", (unsigned long)&lpt_dev_desc },
Heikki Krogerus06d86412013-06-17 13:25:46 +0300148 { "INT33C4", (unsigned long)&lpt_uart_dev_desc },
149 { "INT33C5", (unsigned long)&lpt_uart_dev_desc },
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100150 { "INT33C6", (unsigned long)&lpt_sdio_dev_desc },
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100151 { "INT33C7", },
152
Mika Westerbergf6272172013-05-13 12:42:44 +0000153 /* BayTrail LPSS devices */
154 { "80860F0A", (unsigned long)&byt_uart_dev_desc },
155 { "80860F0E", (unsigned long)&byt_spi_dev_desc },
156 { "80860F14", (unsigned long)&byt_sdio_dev_desc },
157 { "80860F41", (unsigned long)&byt_i2c_dev_desc },
158 { "INT33B2", },
159
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100160 { }
161};
162
163static int is_memory(struct acpi_resource *res, void *not_used)
164{
165 struct resource r;
166 return !acpi_dev_resource_memory(res, &r);
167}
168
169/* LPSS main clock device. */
170static struct platform_device *lpss_clk_dev;
171
172static inline void lpt_register_clock_device(void)
173{
174 lpss_clk_dev = platform_device_register_simple("clk-lpt", -1, NULL, 0);
175}
176
177static int register_device_clock(struct acpi_device *adev,
178 struct lpss_private_data *pdata)
179{
180 const struct lpss_device_desc *dev_desc = pdata->dev_desc;
Mika Westerbergf6272172013-05-13 12:42:44 +0000181 struct lpss_shared_clock *shared_clock = dev_desc->shared_clock;
182 struct clk *clk = ERR_PTR(-ENODEV);
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300183 struct lpss_clk_data *clk_data;
Mika Westerbergf6272172013-05-13 12:42:44 +0000184 const char *parent;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100185
186 if (!lpss_clk_dev)
187 lpt_register_clock_device();
188
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300189 clk_data = platform_get_drvdata(lpss_clk_dev);
190 if (!clk_data)
191 return -ENODEV;
192
193 if (dev_desc->clkdev_name) {
194 clk_register_clkdev(clk_data->clk, dev_desc->clkdev_name,
195 dev_name(&adev->dev));
196 return 0;
197 }
198
199 if (!pdata->mmio_base
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100200 || pdata->mmio_size < dev_desc->prv_offset + LPSS_CLK_SIZE)
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100201 return -ENODATA;
202
Mika Westerbergf6272172013-05-13 12:42:44 +0000203 parent = clk_data->name;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100204
Mika Westerbergf6272172013-05-13 12:42:44 +0000205 if (shared_clock) {
206 clk = shared_clock->clk;
207 if (!clk) {
208 clk = clk_register_fixed_rate(NULL, shared_clock->name,
209 "lpss_clk", 0,
210 shared_clock->rate);
211 shared_clock->clk = clk;
212 }
213 parent = shared_clock->name;
214 }
215
216 if (dev_desc->clk_gate) {
217 clk = clk_register_gate(NULL, dev_name(&adev->dev), parent, 0,
218 pdata->mmio_base + dev_desc->prv_offset,
219 0, 0, NULL);
220 pdata->clk = clk;
221 }
222
223 if (IS_ERR(clk))
224 return PTR_ERR(clk);
225
226 clk_register_clkdev(clk, NULL, dev_name(&adev->dev));
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100227 return 0;
228}
229
230static int acpi_lpss_create_device(struct acpi_device *adev,
231 const struct acpi_device_id *id)
232{
233 struct lpss_device_desc *dev_desc;
234 struct lpss_private_data *pdata;
235 struct resource_list_entry *rentry;
236 struct list_head resource_list;
237 int ret;
238
239 dev_desc = (struct lpss_device_desc *)id->driver_data;
240 if (!dev_desc)
241 return acpi_create_platform_device(adev, id);
242
243 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
244 if (!pdata)
245 return -ENOMEM;
246
247 INIT_LIST_HEAD(&resource_list);
248 ret = acpi_dev_get_resources(adev, &resource_list, is_memory, NULL);
249 if (ret < 0)
250 goto err_out;
251
252 list_for_each_entry(rentry, &resource_list, node)
253 if (resource_type(&rentry->res) == IORESOURCE_MEM) {
Mika Westerberg958c4eb2013-06-18 16:51:35 +0300254 if (dev_desc->prv_size_override)
255 pdata->mmio_size = dev_desc->prv_size_override;
256 else
257 pdata->mmio_size = resource_size(&rentry->res);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100258 pdata->mmio_base = ioremap(rentry->res.start,
259 pdata->mmio_size);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100260 break;
261 }
262
263 acpi_dev_free_resource_list(&resource_list);
264
Mika Westerbergaf65cfe2013-09-02 13:30:25 +0300265 pdata->dev_desc = dev_desc;
266
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100267 if (dev_desc->clk_required) {
268 ret = register_device_clock(adev, pdata);
269 if (ret) {
Rafael J. Wysockib9e95fc2013-06-19 00:45:34 +0200270 /* Skip the device, but continue the namespace scan. */
271 ret = 0;
272 goto err_out;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100273 }
274 }
275
Rafael J. Wysockib9e95fc2013-06-19 00:45:34 +0200276 /*
277 * This works around a known issue in ACPI tables where LPSS devices
278 * have _PS0 and _PS3 without _PSC (and no power resources), so
279 * acpi_bus_init_power() will assume that the BIOS has put them into D0.
280 */
281 ret = acpi_device_fix_up_power(adev);
282 if (ret) {
283 /* Skip the device, but continue the namespace scan. */
284 ret = 0;
285 goto err_out;
286 }
287
Heikki Krogerus06d86412013-06-17 13:25:46 +0300288 if (dev_desc->setup)
289 dev_desc->setup(pdata);
290
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100291 adev->driver_data = pdata;
292 ret = acpi_create_platform_device(adev, id);
293 if (ret > 0)
294 return ret;
295
296 adev->driver_data = NULL;
297
298 err_out:
299 kfree(pdata);
300 return ret;
301}
302
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100303static int lpss_reg_read(struct device *dev, unsigned int reg, u32 *val)
304{
305 struct acpi_device *adev;
306 struct lpss_private_data *pdata;
307 unsigned long flags;
308 int ret;
309
310 ret = acpi_bus_get_device(ACPI_HANDLE(dev), &adev);
311 if (WARN_ON(ret))
312 return ret;
313
314 spin_lock_irqsave(&dev->power.lock, flags);
315 if (pm_runtime_suspended(dev)) {
316 ret = -EAGAIN;
317 goto out;
318 }
319 pdata = acpi_driver_data(adev);
320 if (WARN_ON(!pdata || !pdata->mmio_base)) {
321 ret = -ENODEV;
322 goto out;
323 }
324 *val = readl(pdata->mmio_base + pdata->dev_desc->prv_offset + reg);
325
326 out:
327 spin_unlock_irqrestore(&dev->power.lock, flags);
328 return ret;
329}
330
331static ssize_t lpss_ltr_show(struct device *dev, struct device_attribute *attr,
332 char *buf)
333{
334 u32 ltr_value = 0;
335 unsigned int reg;
336 int ret;
337
338 reg = strcmp(attr->attr.name, "auto_ltr") ? LPSS_SW_LTR : LPSS_AUTO_LTR;
339 ret = lpss_reg_read(dev, reg, &ltr_value);
340 if (ret)
341 return ret;
342
343 return snprintf(buf, PAGE_SIZE, "%08x\n", ltr_value);
344}
345
346static ssize_t lpss_ltr_mode_show(struct device *dev,
347 struct device_attribute *attr, char *buf)
348{
349 u32 ltr_mode = 0;
350 char *outstr;
351 int ret;
352
353 ret = lpss_reg_read(dev, LPSS_GENERAL, &ltr_mode);
354 if (ret)
355 return ret;
356
357 outstr = (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) ? "sw" : "auto";
358 return sprintf(buf, "%s\n", outstr);
359}
360
361static DEVICE_ATTR(auto_ltr, S_IRUSR, lpss_ltr_show, NULL);
362static DEVICE_ATTR(sw_ltr, S_IRUSR, lpss_ltr_show, NULL);
363static DEVICE_ATTR(ltr_mode, S_IRUSR, lpss_ltr_mode_show, NULL);
364
365static struct attribute *lpss_attrs[] = {
366 &dev_attr_auto_ltr.attr,
367 &dev_attr_sw_ltr.attr,
368 &dev_attr_ltr_mode.attr,
369 NULL,
370};
371
372static struct attribute_group lpss_attr_group = {
373 .attrs = lpss_attrs,
374 .name = "lpss_ltr",
375};
376
377static int acpi_lpss_platform_notify(struct notifier_block *nb,
378 unsigned long action, void *data)
379{
380 struct platform_device *pdev = to_platform_device(data);
381 struct lpss_private_data *pdata;
382 struct acpi_device *adev;
383 const struct acpi_device_id *id;
384 int ret = 0;
385
386 id = acpi_match_device(acpi_lpss_device_ids, &pdev->dev);
387 if (!id || !id->driver_data)
388 return 0;
389
390 if (acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
391 return 0;
392
393 pdata = acpi_driver_data(adev);
394 if (!pdata || !pdata->mmio_base || !pdata->dev_desc->ltr_required)
395 return 0;
396
397 if (pdata->mmio_size < pdata->dev_desc->prv_offset + LPSS_LTR_SIZE) {
398 dev_err(&pdev->dev, "MMIO size insufficient to access LTR\n");
399 return 0;
400 }
401
402 if (action == BUS_NOTIFY_ADD_DEVICE)
403 ret = sysfs_create_group(&pdev->dev.kobj, &lpss_attr_group);
404 else if (action == BUS_NOTIFY_DEL_DEVICE)
405 sysfs_remove_group(&pdev->dev.kobj, &lpss_attr_group);
406
407 return ret;
408}
409
410static struct notifier_block acpi_lpss_nb = {
411 .notifier_call = acpi_lpss_platform_notify,
412};
413
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100414static struct acpi_scan_handler lpss_handler = {
415 .ids = acpi_lpss_device_ids,
416 .attach = acpi_lpss_create_device,
417};
418
419void __init acpi_lpss_init(void)
420{
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100421 if (!lpt_clk_init()) {
422 bus_register_notifier(&platform_bus_type, &acpi_lpss_nb);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100423 acpi_scan_add_handler(&lpss_handler);
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100424 }
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100425}