xref: /openbmc/linux/drivers/acpi/acpi_lpss.c (revision 7b6d864b)
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>
21 #include <linux/pm_runtime.h>
22 
23 #include "internal.h"
24 
25 ACPI_MODULE_NAME("acpi_lpss");
26 
27 #define LPSS_CLK_SIZE	0x04
28 #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
35 #define LPSS_TX_INT			0x20
36 #define LPSS_TX_INT_MASK		BIT(1)
37 
38 struct lpss_shared_clock {
39 	const char *name;
40 	unsigned long rate;
41 	struct clk *clk;
42 };
43 
44 struct lpss_private_data;
45 
46 struct lpss_device_desc {
47 	bool clk_required;
48 	const char *clkdev_name;
49 	bool ltr_required;
50 	unsigned int prv_offset;
51 	size_t prv_size_override;
52 	bool clk_gate;
53 	struct lpss_shared_clock *shared_clock;
54 	void (*setup)(struct lpss_private_data *pdata);
55 };
56 
57 static struct lpss_device_desc lpss_dma_desc = {
58 	.clk_required = true,
59 	.clkdev_name = "hclk",
60 };
61 
62 struct 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 
69 static 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 
78 static struct lpss_device_desc lpt_dev_desc = {
79 	.clk_required = true,
80 	.prv_offset = 0x800,
81 	.ltr_required = true,
82 	.clk_gate = true,
83 };
84 
85 static 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,
91 };
92 
93 static struct lpss_device_desc lpt_sdio_dev_desc = {
94 	.prv_offset = 0x1000,
95 	.prv_size_override = 0x1018,
96 	.ltr_required = true,
97 };
98 
99 static struct lpss_shared_clock uart_clock = {
100 	.name = "uart_clk",
101 	.rate = 44236800,
102 };
103 
104 static 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,
109 	.setup = lpss_uart_setup,
110 };
111 
112 static struct lpss_shared_clock spi_clock = {
113 	.name = "spi_clk",
114 	.rate = 50000000,
115 };
116 
117 static 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 
124 static struct lpss_device_desc byt_sdio_dev_desc = {
125 	.clk_required = true,
126 };
127 
128 static struct lpss_shared_clock i2c_clock = {
129 	.name = "i2c_clk",
130 	.rate = 100000000,
131 };
132 
133 static struct lpss_device_desc byt_i2c_dev_desc = {
134 	.clk_required = true,
135 	.prv_offset = 0x800,
136 	.shared_clock = &i2c_clock,
137 };
138 
139 static const struct acpi_device_id acpi_lpss_device_ids[] = {
140 	/* Generic LPSS devices */
141 	{ "INTL9C60", (unsigned long)&lpss_dma_desc },
142 
143 	/* 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 },
148 	{ "INT33C4", (unsigned long)&lpt_uart_dev_desc },
149 	{ "INT33C5", (unsigned long)&lpt_uart_dev_desc },
150 	{ "INT33C6", (unsigned long)&lpt_sdio_dev_desc },
151 	{ "INT33C7", },
152 
153 	/* 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 
160 	{ }
161 };
162 
163 static 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. */
170 static struct platform_device *lpss_clk_dev;
171 
172 static inline void lpt_register_clock_device(void)
173 {
174 	lpss_clk_dev = platform_device_register_simple("clk-lpt", -1, NULL, 0);
175 }
176 
177 static 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;
181 	struct lpss_shared_clock *shared_clock = dev_desc->shared_clock;
182 	struct clk *clk = ERR_PTR(-ENODEV);
183 	struct lpss_clk_data *clk_data;
184 	const char *parent;
185 
186 	if (!lpss_clk_dev)
187 		lpt_register_clock_device();
188 
189 	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
200 	    || pdata->mmio_size < dev_desc->prv_offset + LPSS_CLK_SIZE)
201 		return -ENODATA;
202 
203 	parent = clk_data->name;
204 
205 	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));
227 	return 0;
228 }
229 
230 static 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) {
254 			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);
258 			pdata->mmio_base = ioremap(rentry->res.start,
259 						   pdata->mmio_size);
260 			pdata->dev_desc = dev_desc;
261 			break;
262 		}
263 
264 	acpi_dev_free_resource_list(&resource_list);
265 
266 	if (dev_desc->clk_required) {
267 		ret = register_device_clock(adev, pdata);
268 		if (ret) {
269 			/* Skip the device, but continue the namespace scan. */
270 			ret = 0;
271 			goto err_out;
272 		}
273 	}
274 
275 	/*
276 	 * This works around a known issue in ACPI tables where LPSS devices
277 	 * have _PS0 and _PS3 without _PSC (and no power resources), so
278 	 * acpi_bus_init_power() will assume that the BIOS has put them into D0.
279 	 */
280 	ret = acpi_device_fix_up_power(adev);
281 	if (ret) {
282 		/* Skip the device, but continue the namespace scan. */
283 		ret = 0;
284 		goto err_out;
285 	}
286 
287 	if (dev_desc->setup)
288 		dev_desc->setup(pdata);
289 
290 	adev->driver_data = pdata;
291 	ret = acpi_create_platform_device(adev, id);
292 	if (ret > 0)
293 		return ret;
294 
295 	adev->driver_data = NULL;
296 
297  err_out:
298 	kfree(pdata);
299 	return ret;
300 }
301 
302 static int lpss_reg_read(struct device *dev, unsigned int reg, u32 *val)
303 {
304 	struct acpi_device *adev;
305 	struct lpss_private_data *pdata;
306 	unsigned long flags;
307 	int ret;
308 
309 	ret = acpi_bus_get_device(ACPI_HANDLE(dev), &adev);
310 	if (WARN_ON(ret))
311 		return ret;
312 
313 	spin_lock_irqsave(&dev->power.lock, flags);
314 	if (pm_runtime_suspended(dev)) {
315 		ret = -EAGAIN;
316 		goto out;
317 	}
318 	pdata = acpi_driver_data(adev);
319 	if (WARN_ON(!pdata || !pdata->mmio_base)) {
320 		ret = -ENODEV;
321 		goto out;
322 	}
323 	*val = readl(pdata->mmio_base + pdata->dev_desc->prv_offset + reg);
324 
325  out:
326 	spin_unlock_irqrestore(&dev->power.lock, flags);
327 	return ret;
328 }
329 
330 static ssize_t lpss_ltr_show(struct device *dev, struct device_attribute *attr,
331 			     char *buf)
332 {
333 	u32 ltr_value = 0;
334 	unsigned int reg;
335 	int ret;
336 
337 	reg = strcmp(attr->attr.name, "auto_ltr") ? LPSS_SW_LTR : LPSS_AUTO_LTR;
338 	ret = lpss_reg_read(dev, reg, &ltr_value);
339 	if (ret)
340 		return ret;
341 
342 	return snprintf(buf, PAGE_SIZE, "%08x\n", ltr_value);
343 }
344 
345 static ssize_t lpss_ltr_mode_show(struct device *dev,
346 				  struct device_attribute *attr, char *buf)
347 {
348 	u32 ltr_mode = 0;
349 	char *outstr;
350 	int ret;
351 
352 	ret = lpss_reg_read(dev, LPSS_GENERAL, &ltr_mode);
353 	if (ret)
354 		return ret;
355 
356 	outstr = (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) ? "sw" : "auto";
357 	return sprintf(buf, "%s\n", outstr);
358 }
359 
360 static DEVICE_ATTR(auto_ltr, S_IRUSR, lpss_ltr_show, NULL);
361 static DEVICE_ATTR(sw_ltr, S_IRUSR, lpss_ltr_show, NULL);
362 static DEVICE_ATTR(ltr_mode, S_IRUSR, lpss_ltr_mode_show, NULL);
363 
364 static struct attribute *lpss_attrs[] = {
365 	&dev_attr_auto_ltr.attr,
366 	&dev_attr_sw_ltr.attr,
367 	&dev_attr_ltr_mode.attr,
368 	NULL,
369 };
370 
371 static struct attribute_group lpss_attr_group = {
372 	.attrs = lpss_attrs,
373 	.name = "lpss_ltr",
374 };
375 
376 static int acpi_lpss_platform_notify(struct notifier_block *nb,
377 				     unsigned long action, void *data)
378 {
379 	struct platform_device *pdev = to_platform_device(data);
380 	struct lpss_private_data *pdata;
381 	struct acpi_device *adev;
382 	const struct acpi_device_id *id;
383 	int ret = 0;
384 
385 	id = acpi_match_device(acpi_lpss_device_ids, &pdev->dev);
386 	if (!id || !id->driver_data)
387 		return 0;
388 
389 	if (acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
390 		return 0;
391 
392 	pdata = acpi_driver_data(adev);
393 	if (!pdata || !pdata->mmio_base || !pdata->dev_desc->ltr_required)
394 		return 0;
395 
396 	if (pdata->mmio_size < pdata->dev_desc->prv_offset + LPSS_LTR_SIZE) {
397 		dev_err(&pdev->dev, "MMIO size insufficient to access LTR\n");
398 		return 0;
399 	}
400 
401 	if (action == BUS_NOTIFY_ADD_DEVICE)
402 		ret = sysfs_create_group(&pdev->dev.kobj, &lpss_attr_group);
403 	else if (action == BUS_NOTIFY_DEL_DEVICE)
404 		sysfs_remove_group(&pdev->dev.kobj, &lpss_attr_group);
405 
406 	return ret;
407 }
408 
409 static struct notifier_block acpi_lpss_nb = {
410 	.notifier_call = acpi_lpss_platform_notify,
411 };
412 
413 static struct acpi_scan_handler lpss_handler = {
414 	.ids = acpi_lpss_device_ids,
415 	.attach = acpi_lpss_create_device,
416 };
417 
418 void __init acpi_lpss_init(void)
419 {
420 	if (!lpt_clk_init()) {
421 		bus_register_notifier(&platform_bus_type, &acpi_lpss_nb);
422 		acpi_scan_add_handler(&lpss_handler);
423 	}
424 }
425