1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel Sunrisepoint LPSS core support.
5 * Copyright (C) 2015, Intel Corporation
7 * Authors: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
8 * Mika Westerberg <mika.westerberg@linux.intel.com>
9 * Heikki Krogerus <heikki.krogerus@linux.intel.com>
10 * Jarkko Nikula <jarkko.nikula@linux.intel.com>
13 #include <linux/clk.h>
14 #include <linux/clkdev.h>
15 #include <linux/clk-provider.h>
16 #include <linux/debugfs.h>
17 #include <linux/idr.h>
19 #include <linux/ioport.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/mfd/core.h>
23 #include <linux/pm_qos.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/property.h>
26 #include <linux/seq_file.h>
27 #include <linux/io-64-nonatomic-lo-hi.h>
29 #include <linux/dma/idma64.h>
31 #include "intel-lpss.h"
33 #define LPSS_DEV_OFFSET 0x000
34 #define LPSS_DEV_SIZE 0x200
35 #define LPSS_PRIV_OFFSET 0x200
36 #define LPSS_PRIV_SIZE 0x100
37 #define LPSS_PRIV_REG_COUNT (LPSS_PRIV_SIZE / 4)
38 #define LPSS_IDMA64_OFFSET 0x800
39 #define LPSS_IDMA64_SIZE 0x800
41 /* Offsets from lpss->priv */
42 #define LPSS_PRIV_RESETS 0x04
43 #define LPSS_PRIV_RESETS_IDMA BIT(2)
44 #define LPSS_PRIV_RESETS_FUNC 0x3
46 #define LPSS_PRIV_ACTIVELTR 0x10
47 #define LPSS_PRIV_IDLELTR 0x14
49 #define LPSS_PRIV_LTR_REQ BIT(15)
50 #define LPSS_PRIV_LTR_SCALE_MASK GENMASK(11, 10)
51 #define LPSS_PRIV_LTR_SCALE_1US (2 << 10)
52 #define LPSS_PRIV_LTR_SCALE_32US (3 << 10)
53 #define LPSS_PRIV_LTR_VALUE_MASK GENMASK(9, 0)
55 #define LPSS_PRIV_SSP_REG 0x20
56 #define LPSS_PRIV_SSP_REG_DIS_DMA_FIN BIT(0)
58 #define LPSS_PRIV_REMAP_ADDR 0x40
60 #define LPSS_PRIV_CAPS 0xfc
61 #define LPSS_PRIV_CAPS_NO_IDMA BIT(8)
62 #define LPSS_PRIV_CAPS_TYPE_MASK GENMASK(7, 4)
63 #define LPSS_PRIV_CAPS_TYPE_SHIFT 4
65 /* This matches the type field in CAPS register */
66 enum intel_lpss_dev_type
{
73 const struct intel_lpss_platform_info
*info
;
74 enum intel_lpss_dev_type type
;
76 struct clk_lookup
*clock
;
77 struct mfd_cell
*cell
;
80 u32 priv_ctx
[LPSS_PRIV_REG_COUNT
];
85 struct dentry
*debugfs
;
88 static 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"),
94 static const struct resource intel_lpss_idma64_resources
[] = {
95 DEFINE_RES_MEM(LPSS_IDMA64_OFFSET
, LPSS_IDMA64_SIZE
),
100 * Cells needs to be ordered so that the iDMA is created first. This is
101 * because we need to be sure the DMA is available when the host controller
104 static const struct mfd_cell intel_lpss_idma64_cell
= {
105 .name
= LPSS_IDMA64_DRIVER_NAME
,
106 .num_resources
= ARRAY_SIZE(intel_lpss_idma64_resources
),
107 .resources
= intel_lpss_idma64_resources
,
110 static const struct mfd_cell intel_lpss_i2c_cell
= {
111 .name
= "i2c_designware",
112 .num_resources
= ARRAY_SIZE(intel_lpss_dev_resources
),
113 .resources
= intel_lpss_dev_resources
,
116 static const struct mfd_cell intel_lpss_uart_cell
= {
117 .name
= "dw-apb-uart",
118 .num_resources
= ARRAY_SIZE(intel_lpss_dev_resources
),
119 .resources
= intel_lpss_dev_resources
,
122 static const struct mfd_cell intel_lpss_spi_cell
= {
123 .name
= "pxa2xx-spi",
124 .num_resources
= ARRAY_SIZE(intel_lpss_dev_resources
),
125 .resources
= intel_lpss_dev_resources
,
128 static DEFINE_IDA(intel_lpss_devid_ida
);
129 static struct dentry
*intel_lpss_debugfs
;
131 static void intel_lpss_cache_ltr(struct intel_lpss
*lpss
)
133 lpss
->active_ltr
= readl(lpss
->priv
+ LPSS_PRIV_ACTIVELTR
);
134 lpss
->idle_ltr
= readl(lpss
->priv
+ LPSS_PRIV_IDLELTR
);
137 static int intel_lpss_debugfs_add(struct intel_lpss
*lpss
)
141 dir
= debugfs_create_dir(dev_name(lpss
->dev
), intel_lpss_debugfs
);
145 /* Cache the values into lpss structure */
146 intel_lpss_cache_ltr(lpss
);
148 debugfs_create_x32("capabilities", S_IRUGO
, dir
, &lpss
->caps
);
149 debugfs_create_x32("active_ltr", S_IRUGO
, dir
, &lpss
->active_ltr
);
150 debugfs_create_x32("idle_ltr", S_IRUGO
, dir
, &lpss
->idle_ltr
);
156 static void intel_lpss_debugfs_remove(struct intel_lpss
*lpss
)
158 debugfs_remove_recursive(lpss
->debugfs
);
161 static void intel_lpss_ltr_set(struct device
*dev
, s32 val
)
163 struct intel_lpss
*lpss
= dev_get_drvdata(dev
);
167 * Program latency tolerance (LTR) accordingly what has been asked
168 * by the PM QoS layer or disable it in case we were passed
169 * negative value or PM_QOS_LATENCY_ANY.
171 ltr
= readl(lpss
->priv
+ LPSS_PRIV_ACTIVELTR
);
173 if (val
== PM_QOS_LATENCY_ANY
|| val
< 0) {
174 ltr
&= ~LPSS_PRIV_LTR_REQ
;
176 ltr
|= LPSS_PRIV_LTR_REQ
;
177 ltr
&= ~LPSS_PRIV_LTR_SCALE_MASK
;
178 ltr
&= ~LPSS_PRIV_LTR_VALUE_MASK
;
180 if (val
> LPSS_PRIV_LTR_VALUE_MASK
)
181 ltr
|= LPSS_PRIV_LTR_SCALE_32US
| val
>> 5;
183 ltr
|= LPSS_PRIV_LTR_SCALE_1US
| val
;
186 if (ltr
== lpss
->active_ltr
)
189 writel(ltr
, lpss
->priv
+ LPSS_PRIV_ACTIVELTR
);
190 writel(ltr
, lpss
->priv
+ LPSS_PRIV_IDLELTR
);
192 /* Cache the values into lpss structure */
193 intel_lpss_cache_ltr(lpss
);
196 static void intel_lpss_ltr_expose(struct intel_lpss
*lpss
)
198 lpss
->dev
->power
.set_latency_tolerance
= intel_lpss_ltr_set
;
199 dev_pm_qos_expose_latency_tolerance(lpss
->dev
);
202 static void intel_lpss_ltr_hide(struct intel_lpss
*lpss
)
204 dev_pm_qos_hide_latency_tolerance(lpss
->dev
);
205 lpss
->dev
->power
.set_latency_tolerance
= NULL
;
208 static int intel_lpss_assign_devs(struct intel_lpss
*lpss
)
210 const struct mfd_cell
*cell
;
213 type
= lpss
->caps
& LPSS_PRIV_CAPS_TYPE_MASK
;
214 type
>>= LPSS_PRIV_CAPS_TYPE_SHIFT
;
218 cell
= &intel_lpss_i2c_cell
;
221 cell
= &intel_lpss_uart_cell
;
224 cell
= &intel_lpss_spi_cell
;
230 lpss
->cell
= devm_kmemdup(lpss
->dev
, cell
, sizeof(*cell
), GFP_KERNEL
);
239 static bool intel_lpss_has_idma(const struct intel_lpss
*lpss
)
241 return (lpss
->caps
& LPSS_PRIV_CAPS_NO_IDMA
) == 0;
244 static void intel_lpss_set_remap_addr(const struct intel_lpss
*lpss
)
246 resource_size_t addr
= lpss
->info
->mem
->start
;
248 lo_hi_writeq(addr
, lpss
->priv
+ LPSS_PRIV_REMAP_ADDR
);
251 static void intel_lpss_deassert_reset(const struct intel_lpss
*lpss
)
253 u32 value
= LPSS_PRIV_RESETS_FUNC
| LPSS_PRIV_RESETS_IDMA
;
255 /* Bring out the device from reset */
256 writel(value
, lpss
->priv
+ LPSS_PRIV_RESETS
);
259 static void intel_lpss_init_dev(const struct intel_lpss
*lpss
)
261 u32 value
= LPSS_PRIV_SSP_REG_DIS_DMA_FIN
;
263 /* Set the device in reset state */
264 writel(0, lpss
->priv
+ LPSS_PRIV_RESETS
);
266 intel_lpss_deassert_reset(lpss
);
268 intel_lpss_set_remap_addr(lpss
);
270 if (!intel_lpss_has_idma(lpss
))
273 /* Make sure that SPI multiblock DMA transfers are re-enabled */
274 if (lpss
->type
== LPSS_DEV_SPI
)
275 writel(value
, lpss
->priv
+ LPSS_PRIV_SSP_REG
);
278 static void intel_lpss_unregister_clock_tree(struct clk
*clk
)
283 parent
= clk_get_parent(clk
);
289 static int intel_lpss_register_clock_divider(struct intel_lpss
*lpss
,
294 struct clk
*tmp
= *clk
;
296 snprintf(name
, sizeof(name
), "%s-enable", devname
);
297 tmp
= clk_register_gate(NULL
, name
, __clk_get_name(tmp
), 0,
298 lpss
->priv
, 0, 0, NULL
);
302 snprintf(name
, sizeof(name
), "%s-div", devname
);
303 tmp
= clk_register_fractional_divider(NULL
, name
, __clk_get_name(tmp
),
304 0, lpss
->priv
, 1, 15, 16, 15, 0,
310 snprintf(name
, sizeof(name
), "%s-update", devname
);
311 tmp
= clk_register_gate(NULL
, name
, __clk_get_name(tmp
),
312 CLK_SET_RATE_PARENT
, lpss
->priv
, 31, 0, NULL
);
320 static int intel_lpss_register_clock(struct intel_lpss
*lpss
)
322 const struct mfd_cell
*cell
= lpss
->cell
;
327 if (!lpss
->info
->clk_rate
)
331 clk
= clk_register_fixed_rate(NULL
, dev_name(lpss
->dev
), NULL
, 0,
332 lpss
->info
->clk_rate
);
336 snprintf(devname
, sizeof(devname
), "%s.%d", cell
->name
, lpss
->devid
);
339 * Support for clock divider only if it has some preset value.
340 * Otherwise we assume that the divider is not used.
342 if (lpss
->type
!= LPSS_DEV_I2C
) {
343 ret
= intel_lpss_register_clock_divider(lpss
, devname
, &clk
);
345 goto err_clk_register
;
350 /* Clock for the host controller */
351 lpss
->clock
= clkdev_create(clk
, lpss
->info
->clk_con_id
, "%s", devname
);
353 goto err_clk_register
;
360 intel_lpss_unregister_clock_tree(clk
);
365 static void intel_lpss_unregister_clock(struct intel_lpss
*lpss
)
367 if (IS_ERR_OR_NULL(lpss
->clk
))
370 clkdev_drop(lpss
->clock
);
371 intel_lpss_unregister_clock_tree(lpss
->clk
);
374 int intel_lpss_probe(struct device
*dev
,
375 const struct intel_lpss_platform_info
*info
)
377 struct intel_lpss
*lpss
;
380 if (!info
|| !info
->mem
|| info
->irq
<= 0)
383 lpss
= devm_kzalloc(dev
, sizeof(*lpss
), GFP_KERNEL
);
387 lpss
->priv
= devm_ioremap_uc(dev
, info
->mem
->start
+ LPSS_PRIV_OFFSET
,
394 lpss
->caps
= readl(lpss
->priv
+ LPSS_PRIV_CAPS
);
396 dev_set_drvdata(dev
, lpss
);
398 ret
= intel_lpss_assign_devs(lpss
);
402 lpss
->cell
->properties
= info
->properties
;
404 intel_lpss_init_dev(lpss
);
406 lpss
->devid
= ida_simple_get(&intel_lpss_devid_ida
, 0, 0, GFP_KERNEL
);
410 ret
= intel_lpss_register_clock(lpss
);
412 goto err_clk_register
;
414 intel_lpss_ltr_expose(lpss
);
416 ret
= intel_lpss_debugfs_add(lpss
);
418 dev_warn(dev
, "Failed to create debugfs entries\n");
420 if (intel_lpss_has_idma(lpss
)) {
421 ret
= mfd_add_devices(dev
, lpss
->devid
, &intel_lpss_idma64_cell
,
422 1, info
->mem
, info
->irq
, NULL
);
424 dev_warn(dev
, "Failed to add %s, fallback to PIO\n",
425 LPSS_IDMA64_DRIVER_NAME
);
428 ret
= mfd_add_devices(dev
, lpss
->devid
, lpss
->cell
,
429 1, info
->mem
, info
->irq
, NULL
);
433 dev_pm_set_driver_flags(dev
, DPM_FLAG_SMART_SUSPEND
);
438 intel_lpss_debugfs_remove(lpss
);
439 intel_lpss_ltr_hide(lpss
);
440 intel_lpss_unregister_clock(lpss
);
443 ida_simple_remove(&intel_lpss_devid_ida
, lpss
->devid
);
447 EXPORT_SYMBOL_GPL(intel_lpss_probe
);
449 void intel_lpss_remove(struct device
*dev
)
451 struct intel_lpss
*lpss
= dev_get_drvdata(dev
);
453 mfd_remove_devices(dev
);
454 intel_lpss_debugfs_remove(lpss
);
455 intel_lpss_ltr_hide(lpss
);
456 intel_lpss_unregister_clock(lpss
);
457 ida_simple_remove(&intel_lpss_devid_ida
, lpss
->devid
);
459 EXPORT_SYMBOL_GPL(intel_lpss_remove
);
461 static int resume_lpss_device(struct device
*dev
, void *data
)
463 if (!dev_pm_test_driver_flags(dev
, DPM_FLAG_SMART_SUSPEND
))
464 pm_runtime_resume(dev
);
469 int intel_lpss_prepare(struct device
*dev
)
472 * Resume both child devices before entering system sleep. This
473 * ensures that they are in proper state before they get suspended.
475 device_for_each_child_reverse(dev
, NULL
, resume_lpss_device
);
478 EXPORT_SYMBOL_GPL(intel_lpss_prepare
);
480 int intel_lpss_suspend(struct device
*dev
)
482 struct intel_lpss
*lpss
= dev_get_drvdata(dev
);
485 /* Save device context */
486 for (i
= 0; i
< LPSS_PRIV_REG_COUNT
; i
++)
487 lpss
->priv_ctx
[i
] = readl(lpss
->priv
+ i
* 4);
490 * If the device type is not UART, then put the controller into
491 * reset. UART cannot be put into reset since S3/S0ix fail when
492 * no_console_suspend flag is enabled.
494 if (lpss
->type
!= LPSS_DEV_UART
)
495 writel(0, lpss
->priv
+ LPSS_PRIV_RESETS
);
499 EXPORT_SYMBOL_GPL(intel_lpss_suspend
);
501 int intel_lpss_resume(struct device
*dev
)
503 struct intel_lpss
*lpss
= dev_get_drvdata(dev
);
506 intel_lpss_deassert_reset(lpss
);
508 /* Restore device context */
509 for (i
= 0; i
< LPSS_PRIV_REG_COUNT
; i
++)
510 writel(lpss
->priv_ctx
[i
], lpss
->priv
+ i
* 4);
514 EXPORT_SYMBOL_GPL(intel_lpss_resume
);
516 static int __init
intel_lpss_init(void)
518 intel_lpss_debugfs
= debugfs_create_dir("intel_lpss", NULL
);
521 module_init(intel_lpss_init
);
523 static void __exit
intel_lpss_exit(void)
525 ida_destroy(&intel_lpss_devid_ida
);
526 debugfs_remove(intel_lpss_debugfs
);
528 module_exit(intel_lpss_exit
);
530 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
531 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
532 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
533 MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@linux.intel.com>");
534 MODULE_DESCRIPTION("Intel LPSS core driver");
535 MODULE_LICENSE("GPL v2");
537 * Ensure the DMA driver is loaded before the host controller device appears,
538 * so that the host controller driver can request its DMA channels as early
541 * If the DMA module is not there that's OK as well.
543 MODULE_SOFTDEP("pre: platform:" LPSS_IDMA64_DRIVER_NAME
);