blob: 7577afd4284280ad001e245087d908b5d5f46989 [file] [log] [blame]
Andy Shevchenko4b45efe2015-07-27 18:04:03 +03001/*
2 * Intel Sunrisepoint LPSS core support.
3 *
4 * Copyright (C) 2015, Intel Corporation
5 *
6 * Authors: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
8 * Heikki Krogerus <heikki.krogerus@linux.intel.com>
9 * Jarkko Nikula <jarkko.nikula@linux.intel.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
15
16#include <linux/clk.h>
17#include <linux/clkdev.h>
18#include <linux/clk-provider.h>
19#include <linux/debugfs.h>
20#include <linux/idr.h>
21#include <linux/ioport.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/mfd/core.h>
25#include <linux/pm_qos.h>
26#include <linux/pm_runtime.h>
Mika Westerberge15ad212015-11-30 17:11:41 +020027#include <linux/property.h>
Andy Shevchenko4b45efe2015-07-27 18:04:03 +030028#include <linux/seq_file.h>
Linus Torvalds9cf5c092015-11-06 14:22:15 -080029#include <linux/io-64-nonatomic-lo-hi.h>
Andy Shevchenko689d4452015-09-14 11:32:48 +030030
Andy Shevchenko4b45efe2015-07-27 18:04:03 +030031#include "intel-lpss.h"
32
33#define LPSS_DEV_OFFSET 0x000
34#define LPSS_DEV_SIZE 0x200
35#define LPSS_PRIV_OFFSET 0x200
36#define LPSS_PRIV_SIZE 0x100
Heikki Krogerus41a3da22016-04-18 15:14:56 +030037#define LPSS_PRIV_REG_COUNT (LPSS_PRIV_SIZE / 4)
Andy Shevchenko4b45efe2015-07-27 18:04:03 +030038#define LPSS_IDMA64_OFFSET 0x800
39#define LPSS_IDMA64_SIZE 0x800
40
41/* Offsets from lpss->priv */
42#define LPSS_PRIV_RESETS 0x04
Andy Shevchenkof3b23e5a2018-04-24 18:05:12 +030043#define LPSS_PRIV_RESETS_IDMA BIT(2)
44#define LPSS_PRIV_RESETS_FUNC 0x3
Andy Shevchenko4b45efe2015-07-27 18:04:03 +030045
46#define LPSS_PRIV_ACTIVELTR 0x10
47#define LPSS_PRIV_IDLELTR 0x14
48
49#define LPSS_PRIV_LTR_REQ BIT(15)
50#define LPSS_PRIV_LTR_SCALE_MASK 0xc00
51#define LPSS_PRIV_LTR_SCALE_1US 0x800
52#define LPSS_PRIV_LTR_SCALE_32US 0xc00
53#define LPSS_PRIV_LTR_VALUE_MASK 0x3ff
54
55#define LPSS_PRIV_SSP_REG 0x20
56#define LPSS_PRIV_SSP_REG_DIS_DMA_FIN BIT(0)
57
Andy Shevchenko689d4452015-09-14 11:32:48 +030058#define LPSS_PRIV_REMAP_ADDR 0x40
Andy Shevchenko4b45efe2015-07-27 18:04:03 +030059
60#define LPSS_PRIV_CAPS 0xfc
61#define LPSS_PRIV_CAPS_NO_IDMA BIT(8)
62#define LPSS_PRIV_CAPS_TYPE_SHIFT 4
63#define LPSS_PRIV_CAPS_TYPE_MASK (0xf << LPSS_PRIV_CAPS_TYPE_SHIFT)
64
65/* This matches the type field in CAPS register */
66enum intel_lpss_dev_type {
67 LPSS_DEV_I2C = 0,
68 LPSS_DEV_UART,
69 LPSS_DEV_SPI,
70};
71
72struct intel_lpss {
73 const struct intel_lpss_platform_info *info;
74 enum intel_lpss_dev_type type;
75 struct clk *clk;
76 struct clk_lookup *clock;
Mika Westerberge15ad212015-11-30 17:11:41 +020077 struct mfd_cell *cell;
Andy Shevchenko4b45efe2015-07-27 18:04:03 +030078 struct device *dev;
79 void __iomem *priv;
Heikki Krogerus41a3da22016-04-18 15:14:56 +030080 u32 priv_ctx[LPSS_PRIV_REG_COUNT];
Andy Shevchenko4b45efe2015-07-27 18:04:03 +030081 int devid;
82 u32 caps;
83 u32 active_ltr;
84 u32 idle_ltr;
85 struct dentry *debugfs;
86};
87
88static const struct resource intel_lpss_dev_resources[] = {
89 DEFINE_RES_MEM_NAMED(LPSS_DEV_OFFSET, LPSS_DEV_SIZE, "lpss_dev"),
90 DEFINE_RES_MEM_NAMED(LPSS_PRIV_OFFSET, LPSS_PRIV_SIZE, "lpss_priv"),
91 DEFINE_RES_IRQ(0),
92};
93
94static const struct resource intel_lpss_idma64_resources[] = {
95 DEFINE_RES_MEM(LPSS_IDMA64_OFFSET, LPSS_IDMA64_SIZE),
96 DEFINE_RES_IRQ(0),
97};
98
99#define LPSS_IDMA64_DRIVER_NAME "idma64"
100
101/*
102 * Cells needs to be ordered so that the iDMA is created first. This is
103 * because we need to be sure the DMA is available when the host controller
104 * driver is probed.
105 */
106static const struct mfd_cell intel_lpss_idma64_cell = {
107 .name = LPSS_IDMA64_DRIVER_NAME,
108 .num_resources = ARRAY_SIZE(intel_lpss_idma64_resources),
109 .resources = intel_lpss_idma64_resources,
110};
111
112static const struct mfd_cell intel_lpss_i2c_cell = {
113 .name = "i2c_designware",
114 .num_resources = ARRAY_SIZE(intel_lpss_dev_resources),
115 .resources = intel_lpss_dev_resources,
116};
117
118static const struct mfd_cell intel_lpss_uart_cell = {
119 .name = "dw-apb-uart",
120 .num_resources = ARRAY_SIZE(intel_lpss_dev_resources),
121 .resources = intel_lpss_dev_resources,
122};
123
124static const struct mfd_cell intel_lpss_spi_cell = {
125 .name = "pxa2xx-spi",
126 .num_resources = ARRAY_SIZE(intel_lpss_dev_resources),
127 .resources = intel_lpss_dev_resources,
128};
129
130static DEFINE_IDA(intel_lpss_devid_ida);
131static struct dentry *intel_lpss_debugfs;
132
133static int intel_lpss_request_dma_module(const char *name)
134{
135 static bool intel_lpss_dma_requested;
136
137 if (intel_lpss_dma_requested)
138 return 0;
139
140 intel_lpss_dma_requested = true;
141 return request_module("%s", name);
142}
143
144static void intel_lpss_cache_ltr(struct intel_lpss *lpss)
145{
146 lpss->active_ltr = readl(lpss->priv + LPSS_PRIV_ACTIVELTR);
147 lpss->idle_ltr = readl(lpss->priv + LPSS_PRIV_IDLELTR);
148}
149
150static int intel_lpss_debugfs_add(struct intel_lpss *lpss)
151{
152 struct dentry *dir;
153
154 dir = debugfs_create_dir(dev_name(lpss->dev), intel_lpss_debugfs);
155 if (IS_ERR(dir))
156 return PTR_ERR(dir);
157
158 /* Cache the values into lpss structure */
159 intel_lpss_cache_ltr(lpss);
160
161 debugfs_create_x32("capabilities", S_IRUGO, dir, &lpss->caps);
162 debugfs_create_x32("active_ltr", S_IRUGO, dir, &lpss->active_ltr);
163 debugfs_create_x32("idle_ltr", S_IRUGO, dir, &lpss->idle_ltr);
164
165 lpss->debugfs = dir;
166 return 0;
167}
168
169static void intel_lpss_debugfs_remove(struct intel_lpss *lpss)
170{
171 debugfs_remove_recursive(lpss->debugfs);
172}
173
174static void intel_lpss_ltr_set(struct device *dev, s32 val)
175{
176 struct intel_lpss *lpss = dev_get_drvdata(dev);
177 u32 ltr;
178
179 /*
180 * Program latency tolerance (LTR) accordingly what has been asked
181 * by the PM QoS layer or disable it in case we were passed
182 * negative value or PM_QOS_LATENCY_ANY.
183 */
184 ltr = readl(lpss->priv + LPSS_PRIV_ACTIVELTR);
185
186 if (val == PM_QOS_LATENCY_ANY || val < 0) {
187 ltr &= ~LPSS_PRIV_LTR_REQ;
188 } else {
189 ltr |= LPSS_PRIV_LTR_REQ;
190 ltr &= ~LPSS_PRIV_LTR_SCALE_MASK;
191 ltr &= ~LPSS_PRIV_LTR_VALUE_MASK;
192
193 if (val > LPSS_PRIV_LTR_VALUE_MASK)
194 ltr |= LPSS_PRIV_LTR_SCALE_32US | val >> 5;
195 else
196 ltr |= LPSS_PRIV_LTR_SCALE_1US | val;
197 }
198
199 if (ltr == lpss->active_ltr)
200 return;
201
202 writel(ltr, lpss->priv + LPSS_PRIV_ACTIVELTR);
203 writel(ltr, lpss->priv + LPSS_PRIV_IDLELTR);
204
205 /* Cache the values into lpss structure */
206 intel_lpss_cache_ltr(lpss);
207}
208
209static void intel_lpss_ltr_expose(struct intel_lpss *lpss)
210{
211 lpss->dev->power.set_latency_tolerance = intel_lpss_ltr_set;
212 dev_pm_qos_expose_latency_tolerance(lpss->dev);
213}
214
215static void intel_lpss_ltr_hide(struct intel_lpss *lpss)
216{
217 dev_pm_qos_hide_latency_tolerance(lpss->dev);
218 lpss->dev->power.set_latency_tolerance = NULL;
219}
220
221static int intel_lpss_assign_devs(struct intel_lpss *lpss)
222{
Mika Westerberge15ad212015-11-30 17:11:41 +0200223 const struct mfd_cell *cell;
Andy Shevchenko4b45efe2015-07-27 18:04:03 +0300224 unsigned int type;
225
226 type = lpss->caps & LPSS_PRIV_CAPS_TYPE_MASK;
227 type >>= LPSS_PRIV_CAPS_TYPE_SHIFT;
228
229 switch (type) {
230 case LPSS_DEV_I2C:
Mika Westerberge15ad212015-11-30 17:11:41 +0200231 cell = &intel_lpss_i2c_cell;
Andy Shevchenko4b45efe2015-07-27 18:04:03 +0300232 break;
233 case LPSS_DEV_UART:
Mika Westerberge15ad212015-11-30 17:11:41 +0200234 cell = &intel_lpss_uart_cell;
Andy Shevchenko4b45efe2015-07-27 18:04:03 +0300235 break;
236 case LPSS_DEV_SPI:
Mika Westerberge15ad212015-11-30 17:11:41 +0200237 cell = &intel_lpss_spi_cell;
Andy Shevchenko4b45efe2015-07-27 18:04:03 +0300238 break;
239 default:
240 return -ENODEV;
241 }
242
Mika Westerberge15ad212015-11-30 17:11:41 +0200243 lpss->cell = devm_kmemdup(lpss->dev, cell, sizeof(*cell), GFP_KERNEL);
244 if (!lpss->cell)
245 return -ENOMEM;
246
Andy Shevchenko4b45efe2015-07-27 18:04:03 +0300247 lpss->type = type;
248
249 return 0;
250}
251
252static bool intel_lpss_has_idma(const struct intel_lpss *lpss)
253{
254 return (lpss->caps & LPSS_PRIV_CAPS_NO_IDMA) == 0;
255}
256
257static void intel_lpss_set_remap_addr(const struct intel_lpss *lpss)
258{
259 resource_size_t addr = lpss->info->mem->start;
260
Andy Shevchenko689d4452015-09-14 11:32:48 +0300261 lo_hi_writeq(addr, lpss->priv + LPSS_PRIV_REMAP_ADDR);
Andy Shevchenko4b45efe2015-07-27 18:04:03 +0300262}
263
264static void intel_lpss_deassert_reset(const struct intel_lpss *lpss)
265{
266 u32 value = LPSS_PRIV_RESETS_FUNC | LPSS_PRIV_RESETS_IDMA;
267
268 /* Bring out the device from reset */
269 writel(value, lpss->priv + LPSS_PRIV_RESETS);
270}
271
272static void intel_lpss_init_dev(const struct intel_lpss *lpss)
273{
274 u32 value = LPSS_PRIV_SSP_REG_DIS_DMA_FIN;
275
Binbin Wue9a8c982019-04-08 16:09:10 +0800276 /* Set the device in reset state */
277 writel(0, lpss->priv + LPSS_PRIV_RESETS);
278
Andy Shevchenko4b45efe2015-07-27 18:04:03 +0300279 intel_lpss_deassert_reset(lpss);
280
Andy Shevchenkod28b6252018-04-24 18:00:10 +0300281 intel_lpss_set_remap_addr(lpss);
282
Andy Shevchenko4b45efe2015-07-27 18:04:03 +0300283 if (!intel_lpss_has_idma(lpss))
284 return;
285
Andy Shevchenko4b45efe2015-07-27 18:04:03 +0300286 /* Make sure that SPI multiblock DMA transfers are re-enabled */
287 if (lpss->type == LPSS_DEV_SPI)
288 writel(value, lpss->priv + LPSS_PRIV_SSP_REG);
289}
290
291static void intel_lpss_unregister_clock_tree(struct clk *clk)
292{
293 struct clk *parent;
294
295 while (clk) {
296 parent = clk_get_parent(clk);
297 clk_unregister(clk);
298 clk = parent;
299 }
300}
301
302static int intel_lpss_register_clock_divider(struct intel_lpss *lpss,
303 const char *devname,
304 struct clk **clk)
305{
306 char name[32];
307 struct clk *tmp = *clk;
308
309 snprintf(name, sizeof(name), "%s-enable", devname);
310 tmp = clk_register_gate(NULL, name, __clk_get_name(tmp), 0,
311 lpss->priv, 0, 0, NULL);
312 if (IS_ERR(tmp))
313 return PTR_ERR(tmp);
314
315 snprintf(name, sizeof(name), "%s-div", devname);
316 tmp = clk_register_fractional_divider(NULL, name, __clk_get_name(tmp),
317 0, lpss->priv, 1, 15, 16, 15, 0,
318 NULL);
319 if (IS_ERR(tmp))
320 return PTR_ERR(tmp);
321 *clk = tmp;
322
323 snprintf(name, sizeof(name), "%s-update", devname);
324 tmp = clk_register_gate(NULL, name, __clk_get_name(tmp),
325 CLK_SET_RATE_PARENT, lpss->priv, 31, 0, NULL);
326 if (IS_ERR(tmp))
327 return PTR_ERR(tmp);
328 *clk = tmp;
329
330 return 0;
331}
332
333static int intel_lpss_register_clock(struct intel_lpss *lpss)
334{
335 const struct mfd_cell *cell = lpss->cell;
336 struct clk *clk;
337 char devname[24];
338 int ret;
339
340 if (!lpss->info->clk_rate)
341 return 0;
342
343 /* Root clock */
Stephen Boyd0f7e70e2016-04-19 18:20:49 -0700344 clk = clk_register_fixed_rate(NULL, dev_name(lpss->dev), NULL, 0,
345 lpss->info->clk_rate);
Andy Shevchenko4b45efe2015-07-27 18:04:03 +0300346 if (IS_ERR(clk))
347 return PTR_ERR(clk);
348
349 snprintf(devname, sizeof(devname), "%s.%d", cell->name, lpss->devid);
350
351 /*
352 * Support for clock divider only if it has some preset value.
353 * Otherwise we assume that the divider is not used.
354 */
355 if (lpss->type != LPSS_DEV_I2C) {
356 ret = intel_lpss_register_clock_divider(lpss, devname, &clk);
357 if (ret)
358 goto err_clk_register;
359 }
360
361 ret = -ENOMEM;
362
363 /* Clock for the host controller */
364 lpss->clock = clkdev_create(clk, lpss->info->clk_con_id, "%s", devname);
365 if (!lpss->clock)
366 goto err_clk_register;
367
368 lpss->clk = clk;
369
370 return 0;
371
372err_clk_register:
373 intel_lpss_unregister_clock_tree(clk);
374
375 return ret;
376}
377
378static void intel_lpss_unregister_clock(struct intel_lpss *lpss)
379{
380 if (IS_ERR_OR_NULL(lpss->clk))
381 return;
382
383 clkdev_drop(lpss->clock);
384 intel_lpss_unregister_clock_tree(lpss->clk);
385}
386
387int intel_lpss_probe(struct device *dev,
388 const struct intel_lpss_platform_info *info)
389{
390 struct intel_lpss *lpss;
391 int ret;
392
393 if (!info || !info->mem || info->irq <= 0)
394 return -EINVAL;
395
396 lpss = devm_kzalloc(dev, sizeof(*lpss), GFP_KERNEL);
397 if (!lpss)
398 return -ENOMEM;
399
Tuowen Zhaoe06a8992019-10-16 15:06:29 -0600400 lpss->priv = devm_ioremap_uc(dev, info->mem->start + LPSS_PRIV_OFFSET,
Andy Shevchenko4b45efe2015-07-27 18:04:03 +0300401 LPSS_PRIV_SIZE);
402 if (!lpss->priv)
403 return -ENOMEM;
404
405 lpss->info = info;
406 lpss->dev = dev;
407 lpss->caps = readl(lpss->priv + LPSS_PRIV_CAPS);
408
409 dev_set_drvdata(dev, lpss);
410
411 ret = intel_lpss_assign_devs(lpss);
412 if (ret)
413 return ret;
414
Heikki Krogerusf4d05262016-03-29 14:52:23 +0300415 lpss->cell->properties = info->properties;
Mika Westerberge15ad212015-11-30 17:11:41 +0200416
Andy Shevchenko4b45efe2015-07-27 18:04:03 +0300417 intel_lpss_init_dev(lpss);
418
419 lpss->devid = ida_simple_get(&intel_lpss_devid_ida, 0, 0, GFP_KERNEL);
420 if (lpss->devid < 0)
421 return lpss->devid;
422
423 ret = intel_lpss_register_clock(lpss);
424 if (ret)
425 goto err_clk_register;
426
427 intel_lpss_ltr_expose(lpss);
428
429 ret = intel_lpss_debugfs_add(lpss);
430 if (ret)
431 dev_warn(dev, "Failed to create debugfs entries\n");
432
433 if (intel_lpss_has_idma(lpss)) {
434 /*
435 * Ensure the DMA driver is loaded before the host
436 * controller device appears, so that the host controller
437 * driver can request its DMA channels as early as
438 * possible.
439 *
440 * If the DMA module is not there that's OK as well.
441 */
442 intel_lpss_request_dma_module(LPSS_IDMA64_DRIVER_NAME);
443
444 ret = mfd_add_devices(dev, lpss->devid, &intel_lpss_idma64_cell,
445 1, info->mem, info->irq, NULL);
446 if (ret)
447 dev_warn(dev, "Failed to add %s, fallback to PIO\n",
448 LPSS_IDMA64_DRIVER_NAME);
449 }
450
451 ret = mfd_add_devices(dev, lpss->devid, lpss->cell,
452 1, info->mem, info->irq, NULL);
453 if (ret)
454 goto err_remove_ltr;
455
Rafael J. Wysocki8425ec72018-01-03 01:34:53 +0100456 dev_pm_set_driver_flags(dev, DPM_FLAG_SMART_SUSPEND);
457
Andy Shevchenko4b45efe2015-07-27 18:04:03 +0300458 return 0;
459
460err_remove_ltr:
461 intel_lpss_debugfs_remove(lpss);
462 intel_lpss_ltr_hide(lpss);
Andy Shevchenko84cb36c2016-01-22 16:48:46 +0200463 intel_lpss_unregister_clock(lpss);
Andy Shevchenko4b45efe2015-07-27 18:04:03 +0300464
465err_clk_register:
466 ida_simple_remove(&intel_lpss_devid_ida, lpss->devid);
467
468 return ret;
469}
470EXPORT_SYMBOL_GPL(intel_lpss_probe);
471
472void intel_lpss_remove(struct device *dev)
473{
474 struct intel_lpss *lpss = dev_get_drvdata(dev);
475
476 mfd_remove_devices(dev);
477 intel_lpss_debugfs_remove(lpss);
478 intel_lpss_ltr_hide(lpss);
479 intel_lpss_unregister_clock(lpss);
480 ida_simple_remove(&intel_lpss_devid_ida, lpss->devid);
481}
482EXPORT_SYMBOL_GPL(intel_lpss_remove);
483
484static int resume_lpss_device(struct device *dev, void *data)
485{
Rafael J. Wysocki8425ec72018-01-03 01:34:53 +0100486 if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND))
487 pm_runtime_resume(dev);
488
Andy Shevchenko4b45efe2015-07-27 18:04:03 +0300489 return 0;
490}
491
492int intel_lpss_prepare(struct device *dev)
493{
494 /*
495 * Resume both child devices before entering system sleep. This
496 * ensures that they are in proper state before they get suspended.
497 */
498 device_for_each_child_reverse(dev, NULL, resume_lpss_device);
499 return 0;
500}
501EXPORT_SYMBOL_GPL(intel_lpss_prepare);
502
503int intel_lpss_suspend(struct device *dev)
504{
Heikki Krogerus41a3da22016-04-18 15:14:56 +0300505 struct intel_lpss *lpss = dev_get_drvdata(dev);
506 unsigned int i;
507
508 /* Save device context */
509 for (i = 0; i < LPSS_PRIV_REG_COUNT; i++)
510 lpss->priv_ctx[i] = readl(lpss->priv + i * 4);
511
Furquan Shaikh0b471aa2017-07-23 23:02:19 -0700512 /*
513 * If the device type is not UART, then put the controller into
514 * reset. UART cannot be put into reset since S3/S0ix fail when
515 * no_console_suspend flag is enabled.
516 */
517 if (lpss->type != LPSS_DEV_UART)
518 writel(0, lpss->priv + LPSS_PRIV_RESETS);
519
Andy Shevchenko4b45efe2015-07-27 18:04:03 +0300520 return 0;
521}
522EXPORT_SYMBOL_GPL(intel_lpss_suspend);
523
524int intel_lpss_resume(struct device *dev)
525{
526 struct intel_lpss *lpss = dev_get_drvdata(dev);
Heikki Krogerus41a3da22016-04-18 15:14:56 +0300527 unsigned int i;
Andy Shevchenko4b45efe2015-07-27 18:04:03 +0300528
Heikki Krogerus41a3da22016-04-18 15:14:56 +0300529 intel_lpss_deassert_reset(lpss);
530
531 /* Restore device context */
532 for (i = 0; i < LPSS_PRIV_REG_COUNT; i++)
533 writel(lpss->priv_ctx[i], lpss->priv + i * 4);
Andy Shevchenko4b45efe2015-07-27 18:04:03 +0300534
535 return 0;
536}
537EXPORT_SYMBOL_GPL(intel_lpss_resume);
538
539static int __init intel_lpss_init(void)
540{
541 intel_lpss_debugfs = debugfs_create_dir("intel_lpss", NULL);
542 return 0;
543}
544module_init(intel_lpss_init);
545
546static void __exit intel_lpss_exit(void)
547{
Andy Shevchenkodbed2be2019-06-21 15:56:31 +0300548 ida_destroy(&intel_lpss_devid_ida);
Andy Shevchenko4b45efe2015-07-27 18:04:03 +0300549 debugfs_remove(intel_lpss_debugfs);
550}
551module_exit(intel_lpss_exit);
552
553MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
554MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
555MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
556MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@linux.intel.com>");
557MODULE_DESCRIPTION("Intel LPSS core driver");
558MODULE_LICENSE("GPL v2");