blob: e603905973721c415a912c04e2c7f3926fcd5634 [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)
Heikki Krogerus088f1fd2013-10-09 09:49:20 +030033#define LPSS_GENERAL_UART_RTS_OVRD BIT(3)
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010034#define LPSS_SW_LTR 0x10
35#define LPSS_AUTO_LTR 0x14
Heikki Krogerus06d86412013-06-17 13:25:46 +030036#define LPSS_TX_INT 0x20
37#define LPSS_TX_INT_MASK BIT(1)
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010038
Mika Westerbergf6272172013-05-13 12:42:44 +000039struct lpss_shared_clock {
40 const char *name;
41 unsigned long rate;
42 struct clk *clk;
43};
44
Heikki Krogerus06d86412013-06-17 13:25:46 +030045struct lpss_private_data;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010046
47struct lpss_device_desc {
48 bool clk_required;
Rafael J. Wysockib59cc202013-05-08 11:55:49 +030049 const char *clkdev_name;
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010050 bool ltr_required;
51 unsigned int prv_offset;
Mika Westerberg958c4eb2013-06-18 16:51:35 +030052 size_t prv_size_override;
Mika Westerbergf6272172013-05-13 12:42:44 +000053 bool clk_gate;
54 struct lpss_shared_clock *shared_clock;
Heikki Krogerus06d86412013-06-17 13:25:46 +030055 void (*setup)(struct lpss_private_data *pdata);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010056};
57
Rafael J. Wysockib59cc202013-05-08 11:55:49 +030058static struct lpss_device_desc lpss_dma_desc = {
59 .clk_required = true,
60 .clkdev_name = "hclk",
61};
62
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010063struct lpss_private_data {
64 void __iomem *mmio_base;
65 resource_size_t mmio_size;
66 struct clk *clk;
67 const struct lpss_device_desc *dev_desc;
68};
69
Heikki Krogerus06d86412013-06-17 13:25:46 +030070static void lpss_uart_setup(struct lpss_private_data *pdata)
71{
Heikki Krogerus088f1fd2013-10-09 09:49:20 +030072 unsigned int offset;
Heikki Krogerus06d86412013-06-17 13:25:46 +030073 u32 reg;
74
Heikki Krogerus088f1fd2013-10-09 09:49:20 +030075 offset = pdata->dev_desc->prv_offset + LPSS_TX_INT;
76 reg = readl(pdata->mmio_base + offset);
77 writel(reg | LPSS_TX_INT_MASK, pdata->mmio_base + offset);
78
79 offset = pdata->dev_desc->prv_offset + LPSS_GENERAL;
80 reg = readl(pdata->mmio_base + offset);
81 writel(reg | LPSS_GENERAL_UART_RTS_OVRD, pdata->mmio_base + offset);
Heikki Krogerus06d86412013-06-17 13:25:46 +030082}
83
Rafael J. Wysockif58b0822013-03-06 23:46:20 +010084static struct lpss_device_desc lpt_dev_desc = {
85 .clk_required = true,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010086 .prv_offset = 0x800,
87 .ltr_required = true,
Mika Westerbergf6272172013-05-13 12:42:44 +000088 .clk_gate = true,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010089};
90
Heikki Krogerus06d86412013-06-17 13:25:46 +030091static struct lpss_device_desc lpt_uart_dev_desc = {
92 .clk_required = true,
93 .prv_offset = 0x800,
94 .ltr_required = true,
95 .clk_gate = true,
96 .setup = lpss_uart_setup,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +010097};
98
99static struct lpss_device_desc lpt_sdio_dev_desc = {
100 .prv_offset = 0x1000,
Mika Westerberg958c4eb2013-06-18 16:51:35 +0300101 .prv_size_override = 0x1018,
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100102 .ltr_required = true,
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100103};
104
Mika Westerbergf6272172013-05-13 12:42:44 +0000105static struct lpss_shared_clock uart_clock = {
106 .name = "uart_clk",
107 .rate = 44236800,
108};
109
110static struct lpss_device_desc byt_uart_dev_desc = {
111 .clk_required = true,
112 .prv_offset = 0x800,
113 .clk_gate = true,
114 .shared_clock = &uart_clock,
Heikki Krogerus06d86412013-06-17 13:25:46 +0300115 .setup = lpss_uart_setup,
Mika Westerbergf6272172013-05-13 12:42:44 +0000116};
117
118static struct lpss_shared_clock spi_clock = {
119 .name = "spi_clk",
120 .rate = 50000000,
121};
122
123static struct lpss_device_desc byt_spi_dev_desc = {
124 .clk_required = true,
125 .prv_offset = 0x400,
126 .clk_gate = true,
127 .shared_clock = &spi_clock,
128};
129
130static struct lpss_device_desc byt_sdio_dev_desc = {
131 .clk_required = true,
132};
133
134static struct lpss_shared_clock i2c_clock = {
135 .name = "i2c_clk",
136 .rate = 100000000,
137};
138
139static struct lpss_device_desc byt_i2c_dev_desc = {
140 .clk_required = true,
141 .prv_offset = 0x800,
142 .shared_clock = &i2c_clock,
143};
144
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100145static const struct acpi_device_id acpi_lpss_device_ids[] = {
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300146 /* Generic LPSS devices */
147 { "INTL9C60", (unsigned long)&lpss_dma_desc },
148
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100149 /* Lynxpoint LPSS devices */
150 { "INT33C0", (unsigned long)&lpt_dev_desc },
151 { "INT33C1", (unsigned long)&lpt_dev_desc },
152 { "INT33C2", (unsigned long)&lpt_dev_desc },
153 { "INT33C3", (unsigned long)&lpt_dev_desc },
Heikki Krogerus06d86412013-06-17 13:25:46 +0300154 { "INT33C4", (unsigned long)&lpt_uart_dev_desc },
155 { "INT33C5", (unsigned long)&lpt_uart_dev_desc },
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100156 { "INT33C6", (unsigned long)&lpt_sdio_dev_desc },
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100157 { "INT33C7", },
158
Mika Westerbergf6272172013-05-13 12:42:44 +0000159 /* BayTrail LPSS devices */
160 { "80860F0A", (unsigned long)&byt_uart_dev_desc },
161 { "80860F0E", (unsigned long)&byt_spi_dev_desc },
162 { "80860F14", (unsigned long)&byt_sdio_dev_desc },
163 { "80860F41", (unsigned long)&byt_i2c_dev_desc },
164 { "INT33B2", },
Paul Drewsf6308b32013-11-25 14:15:55 -0800165 { "INT33FC", },
Mika Westerbergf6272172013-05-13 12:42:44 +0000166
Mika Westerberga4d97532013-11-12 11:48:19 +0200167 { "INT3430", (unsigned long)&lpt_dev_desc },
168 { "INT3431", (unsigned long)&lpt_dev_desc },
169 { "INT3432", (unsigned long)&lpt_dev_desc },
170 { "INT3433", (unsigned long)&lpt_dev_desc },
171 { "INT3434", (unsigned long)&lpt_uart_dev_desc },
172 { "INT3435", (unsigned long)&lpt_uart_dev_desc },
173 { "INT3436", (unsigned long)&lpt_sdio_dev_desc },
174 { "INT3437", },
175
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100176 { }
177};
178
179static int is_memory(struct acpi_resource *res, void *not_used)
180{
181 struct resource r;
182 return !acpi_dev_resource_memory(res, &r);
183}
184
185/* LPSS main clock device. */
186static struct platform_device *lpss_clk_dev;
187
188static inline void lpt_register_clock_device(void)
189{
190 lpss_clk_dev = platform_device_register_simple("clk-lpt", -1, NULL, 0);
191}
192
193static int register_device_clock(struct acpi_device *adev,
194 struct lpss_private_data *pdata)
195{
196 const struct lpss_device_desc *dev_desc = pdata->dev_desc;
Mika Westerbergf6272172013-05-13 12:42:44 +0000197 struct lpss_shared_clock *shared_clock = dev_desc->shared_clock;
198 struct clk *clk = ERR_PTR(-ENODEV);
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300199 struct lpss_clk_data *clk_data;
Mika Westerbergf6272172013-05-13 12:42:44 +0000200 const char *parent;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100201
202 if (!lpss_clk_dev)
203 lpt_register_clock_device();
204
Rafael J. Wysockib59cc202013-05-08 11:55:49 +0300205 clk_data = platform_get_drvdata(lpss_clk_dev);
206 if (!clk_data)
207 return -ENODEV;
208
209 if (dev_desc->clkdev_name) {
210 clk_register_clkdev(clk_data->clk, dev_desc->clkdev_name,
211 dev_name(&adev->dev));
212 return 0;
213 }
214
215 if (!pdata->mmio_base
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100216 || pdata->mmio_size < dev_desc->prv_offset + LPSS_CLK_SIZE)
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100217 return -ENODATA;
218
Mika Westerbergf6272172013-05-13 12:42:44 +0000219 parent = clk_data->name;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100220
Mika Westerbergf6272172013-05-13 12:42:44 +0000221 if (shared_clock) {
222 clk = shared_clock->clk;
223 if (!clk) {
224 clk = clk_register_fixed_rate(NULL, shared_clock->name,
225 "lpss_clk", 0,
226 shared_clock->rate);
227 shared_clock->clk = clk;
228 }
229 parent = shared_clock->name;
230 }
231
232 if (dev_desc->clk_gate) {
233 clk = clk_register_gate(NULL, dev_name(&adev->dev), parent, 0,
234 pdata->mmio_base + dev_desc->prv_offset,
235 0, 0, NULL);
236 pdata->clk = clk;
237 }
238
239 if (IS_ERR(clk))
240 return PTR_ERR(clk);
241
242 clk_register_clkdev(clk, NULL, dev_name(&adev->dev));
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100243 return 0;
244}
245
246static int acpi_lpss_create_device(struct acpi_device *adev,
247 const struct acpi_device_id *id)
248{
249 struct lpss_device_desc *dev_desc;
250 struct lpss_private_data *pdata;
251 struct resource_list_entry *rentry;
252 struct list_head resource_list;
253 int ret;
254
255 dev_desc = (struct lpss_device_desc *)id->driver_data;
256 if (!dev_desc)
257 return acpi_create_platform_device(adev, id);
258
259 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
260 if (!pdata)
261 return -ENOMEM;
262
263 INIT_LIST_HEAD(&resource_list);
264 ret = acpi_dev_get_resources(adev, &resource_list, is_memory, NULL);
265 if (ret < 0)
266 goto err_out;
267
268 list_for_each_entry(rentry, &resource_list, node)
269 if (resource_type(&rentry->res) == IORESOURCE_MEM) {
Mika Westerberg958c4eb2013-06-18 16:51:35 +0300270 if (dev_desc->prv_size_override)
271 pdata->mmio_size = dev_desc->prv_size_override;
272 else
273 pdata->mmio_size = resource_size(&rentry->res);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100274 pdata->mmio_base = ioremap(rentry->res.start,
275 pdata->mmio_size);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100276 break;
277 }
278
279 acpi_dev_free_resource_list(&resource_list);
280
Mika Westerbergaf65cfe2013-09-02 13:30:25 +0300281 pdata->dev_desc = dev_desc;
282
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100283 if (dev_desc->clk_required) {
284 ret = register_device_clock(adev, pdata);
285 if (ret) {
Rafael J. Wysockib9e95fc2013-06-19 00:45:34 +0200286 /* Skip the device, but continue the namespace scan. */
287 ret = 0;
288 goto err_out;
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100289 }
290 }
291
Rafael J. Wysockib9e95fc2013-06-19 00:45:34 +0200292 /*
293 * This works around a known issue in ACPI tables where LPSS devices
294 * have _PS0 and _PS3 without _PSC (and no power resources), so
295 * acpi_bus_init_power() will assume that the BIOS has put them into D0.
296 */
297 ret = acpi_device_fix_up_power(adev);
298 if (ret) {
299 /* Skip the device, but continue the namespace scan. */
300 ret = 0;
301 goto err_out;
302 }
303
Heikki Krogerus06d86412013-06-17 13:25:46 +0300304 if (dev_desc->setup)
305 dev_desc->setup(pdata);
306
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100307 adev->driver_data = pdata;
308 ret = acpi_create_platform_device(adev, id);
309 if (ret > 0)
310 return ret;
311
312 adev->driver_data = NULL;
313
314 err_out:
315 kfree(pdata);
316 return ret;
317}
318
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100319static int lpss_reg_read(struct device *dev, unsigned int reg, u32 *val)
320{
321 struct acpi_device *adev;
322 struct lpss_private_data *pdata;
323 unsigned long flags;
324 int ret;
325
326 ret = acpi_bus_get_device(ACPI_HANDLE(dev), &adev);
327 if (WARN_ON(ret))
328 return ret;
329
330 spin_lock_irqsave(&dev->power.lock, flags);
331 if (pm_runtime_suspended(dev)) {
332 ret = -EAGAIN;
333 goto out;
334 }
335 pdata = acpi_driver_data(adev);
336 if (WARN_ON(!pdata || !pdata->mmio_base)) {
337 ret = -ENODEV;
338 goto out;
339 }
340 *val = readl(pdata->mmio_base + pdata->dev_desc->prv_offset + reg);
341
342 out:
343 spin_unlock_irqrestore(&dev->power.lock, flags);
344 return ret;
345}
346
347static ssize_t lpss_ltr_show(struct device *dev, struct device_attribute *attr,
348 char *buf)
349{
350 u32 ltr_value = 0;
351 unsigned int reg;
352 int ret;
353
354 reg = strcmp(attr->attr.name, "auto_ltr") ? LPSS_SW_LTR : LPSS_AUTO_LTR;
355 ret = lpss_reg_read(dev, reg, &ltr_value);
356 if (ret)
357 return ret;
358
359 return snprintf(buf, PAGE_SIZE, "%08x\n", ltr_value);
360}
361
362static ssize_t lpss_ltr_mode_show(struct device *dev,
363 struct device_attribute *attr, char *buf)
364{
365 u32 ltr_mode = 0;
366 char *outstr;
367 int ret;
368
369 ret = lpss_reg_read(dev, LPSS_GENERAL, &ltr_mode);
370 if (ret)
371 return ret;
372
373 outstr = (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) ? "sw" : "auto";
374 return sprintf(buf, "%s\n", outstr);
375}
376
377static DEVICE_ATTR(auto_ltr, S_IRUSR, lpss_ltr_show, NULL);
378static DEVICE_ATTR(sw_ltr, S_IRUSR, lpss_ltr_show, NULL);
379static DEVICE_ATTR(ltr_mode, S_IRUSR, lpss_ltr_mode_show, NULL);
380
381static struct attribute *lpss_attrs[] = {
382 &dev_attr_auto_ltr.attr,
383 &dev_attr_sw_ltr.attr,
384 &dev_attr_ltr_mode.attr,
385 NULL,
386};
387
388static struct attribute_group lpss_attr_group = {
389 .attrs = lpss_attrs,
390 .name = "lpss_ltr",
391};
392
393static int acpi_lpss_platform_notify(struct notifier_block *nb,
394 unsigned long action, void *data)
395{
396 struct platform_device *pdev = to_platform_device(data);
397 struct lpss_private_data *pdata;
398 struct acpi_device *adev;
399 const struct acpi_device_id *id;
400 int ret = 0;
401
402 id = acpi_match_device(acpi_lpss_device_ids, &pdev->dev);
403 if (!id || !id->driver_data)
404 return 0;
405
406 if (acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
407 return 0;
408
409 pdata = acpi_driver_data(adev);
410 if (!pdata || !pdata->mmio_base || !pdata->dev_desc->ltr_required)
411 return 0;
412
413 if (pdata->mmio_size < pdata->dev_desc->prv_offset + LPSS_LTR_SIZE) {
414 dev_err(&pdev->dev, "MMIO size insufficient to access LTR\n");
415 return 0;
416 }
417
418 if (action == BUS_NOTIFY_ADD_DEVICE)
419 ret = sysfs_create_group(&pdev->dev.kobj, &lpss_attr_group);
420 else if (action == BUS_NOTIFY_DEL_DEVICE)
421 sysfs_remove_group(&pdev->dev.kobj, &lpss_attr_group);
422
423 return ret;
424}
425
426static struct notifier_block acpi_lpss_nb = {
427 .notifier_call = acpi_lpss_platform_notify,
428};
429
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100430static struct acpi_scan_handler lpss_handler = {
431 .ids = acpi_lpss_device_ids,
432 .attach = acpi_lpss_create_device,
433};
434
435void __init acpi_lpss_init(void)
436{
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100437 if (!lpt_clk_init()) {
438 bus_register_notifier(&platform_bus_type, &acpi_lpss_nb);
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100439 acpi_scan_add_handler(&lpss_handler);
Rafael J. Wysocki2e0f8822013-03-06 23:46:28 +0100440 }
Rafael J. Wysockif58b0822013-03-06 23:46:20 +0100441}