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/array_size.h>
14 #include <linux/bits.h>
15 #include <linux/clkdev.h>
16 #include <linux/clk.h>
17 #include <linux/clk-provider.h>
18 #include <linux/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/err.h>
21 #include <linux/gfp_types.h>
22 #include <linux/idr.h>
24 #include <linux/ioport.h>
25 #include <linux/mfd/core.h>
26 #include <linux/module.h>
28 #include <linux/pm_qos.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/sprintf.h>
31 #include <linux/types.h>
33 #include <linux/io-64-nonatomic-lo-hi.h>
35 #include <linux/dma/idma64.h>
37 #include "intel-lpss.h"
41 #define LPSS_DEV_OFFSET 0x000
42 #define LPSS_DEV_SIZE 0x200
43 #define LPSS_PRIV_OFFSET 0x200
44 #define LPSS_PRIV_SIZE 0x100
45 #define LPSS_PRIV_REG_COUNT (LPSS_PRIV_SIZE / 4)
46 #define LPSS_IDMA64_OFFSET 0x800
47 #define LPSS_IDMA64_SIZE 0x800
49 /* Offsets from lpss->priv */
50 #define LPSS_PRIV_RESETS 0x04
51 #define LPSS_PRIV_RESETS_IDMA BIT(2)
52 #define LPSS_PRIV_RESETS_FUNC 0x3
54 #define LPSS_PRIV_ACTIVELTR 0x10
55 #define LPSS_PRIV_IDLELTR 0x14
57 #define LPSS_PRIV_LTR_REQ BIT(15)
58 #define LPSS_PRIV_LTR_SCALE_MASK GENMASK(11, 10)
59 #define LPSS_PRIV_LTR_SCALE_1US (2 << 10)
60 #define LPSS_PRIV_LTR_SCALE_32US (3 << 10)
61 #define LPSS_PRIV_LTR_VALUE_MASK GENMASK(9, 0)
63 #define LPSS_PRIV_SSP_REG 0x20
64 #define LPSS_PRIV_SSP_REG_DIS_DMA_FIN BIT(0)
66 #define LPSS_PRIV_REMAP_ADDR 0x40
68 #define LPSS_PRIV_CAPS 0xfc
69 #define LPSS_PRIV_CAPS_NO_IDMA BIT(8)
70 #define LPSS_PRIV_CAPS_TYPE_MASK GENMASK(7, 4)
71 #define LPSS_PRIV_CAPS_TYPE_SHIFT 4
73 /* This matches the type field in CAPS register */
74 enum intel_lpss_dev_type
{
81 const struct intel_lpss_platform_info
*info
;
82 enum intel_lpss_dev_type type
;
84 struct clk_lookup
*clock
;
85 struct mfd_cell
*cell
;
88 u32 priv_ctx
[LPSS_PRIV_REG_COUNT
];
93 struct dentry
*debugfs
;
96 static const struct resource intel_lpss_dev_resources
[] = {
97 DEFINE_RES_MEM_NAMED(LPSS_DEV_OFFSET
, LPSS_DEV_SIZE
, "lpss_dev"),
98 DEFINE_RES_MEM_NAMED(LPSS_PRIV_OFFSET
, LPSS_PRIV_SIZE
, "lpss_priv"),
102 static const struct resource intel_lpss_idma64_resources
[] = {
103 DEFINE_RES_MEM(LPSS_IDMA64_OFFSET
, LPSS_IDMA64_SIZE
),
108 * Cells needs to be ordered so that the iDMA is created first. This is
109 * because we need to be sure the DMA is available when the host controller
112 static const struct mfd_cell intel_lpss_idma64_cell
= {
113 .name
= LPSS_IDMA64_DRIVER_NAME
,
114 .num_resources
= ARRAY_SIZE(intel_lpss_idma64_resources
),
115 .resources
= intel_lpss_idma64_resources
,
118 static const struct mfd_cell intel_lpss_i2c_cell
= {
119 .name
= "i2c_designware",
120 .num_resources
= ARRAY_SIZE(intel_lpss_dev_resources
),
121 .resources
= intel_lpss_dev_resources
,
124 static const struct mfd_cell intel_lpss_uart_cell
= {
125 .name
= "dw-apb-uart",
126 .num_resources
= ARRAY_SIZE(intel_lpss_dev_resources
),
127 .resources
= intel_lpss_dev_resources
,
130 static const struct mfd_cell intel_lpss_spi_cell
= {
131 .name
= "pxa2xx-spi",
132 .num_resources
= ARRAY_SIZE(intel_lpss_dev_resources
),
133 .resources
= intel_lpss_dev_resources
,
136 static DEFINE_IDA(intel_lpss_devid_ida
);
137 static struct dentry
*intel_lpss_debugfs
;
139 static void intel_lpss_cache_ltr(struct intel_lpss
*lpss
)
141 lpss
->active_ltr
= readl(lpss
->priv
+ LPSS_PRIV_ACTIVELTR
);
142 lpss
->idle_ltr
= readl(lpss
->priv
+ LPSS_PRIV_IDLELTR
);
145 static int intel_lpss_debugfs_add(struct intel_lpss
*lpss
)
149 dir
= debugfs_create_dir(dev_name(lpss
->dev
), intel_lpss_debugfs
);
153 /* Cache the values into lpss structure */
154 intel_lpss_cache_ltr(lpss
);
156 debugfs_create_x32("capabilities", S_IRUGO
, dir
, &lpss
->caps
);
157 debugfs_create_x32("active_ltr", S_IRUGO
, dir
, &lpss
->active_ltr
);
158 debugfs_create_x32("idle_ltr", S_IRUGO
, dir
, &lpss
->idle_ltr
);
164 static void intel_lpss_debugfs_remove(struct intel_lpss
*lpss
)
166 debugfs_remove_recursive(lpss
->debugfs
);
169 static void intel_lpss_ltr_set(struct device
*dev
, s32 val
)
171 struct intel_lpss
*lpss
= dev_get_drvdata(dev
);
175 * Program latency tolerance (LTR) accordingly what has been asked
176 * by the PM QoS layer or disable it in case we were passed
177 * negative value or PM_QOS_LATENCY_ANY.
179 ltr
= readl(lpss
->priv
+ LPSS_PRIV_ACTIVELTR
);
181 if (val
== PM_QOS_LATENCY_ANY
|| val
< 0) {
182 ltr
&= ~LPSS_PRIV_LTR_REQ
;
184 ltr
|= LPSS_PRIV_LTR_REQ
;
185 ltr
&= ~LPSS_PRIV_LTR_SCALE_MASK
;
186 ltr
&= ~LPSS_PRIV_LTR_VALUE_MASK
;
188 if (val
> LPSS_PRIV_LTR_VALUE_MASK
)
189 ltr
|= LPSS_PRIV_LTR_SCALE_32US
| val
>> 5;
191 ltr
|= LPSS_PRIV_LTR_SCALE_1US
| val
;
194 if (ltr
== lpss
->active_ltr
)
197 writel(ltr
, lpss
->priv
+ LPSS_PRIV_ACTIVELTR
);
198 writel(ltr
, lpss
->priv
+ LPSS_PRIV_IDLELTR
);
200 /* Cache the values into lpss structure */
201 intel_lpss_cache_ltr(lpss
);
204 static void intel_lpss_ltr_expose(struct intel_lpss
*lpss
)
206 lpss
->dev
->power
.set_latency_tolerance
= intel_lpss_ltr_set
;
207 dev_pm_qos_expose_latency_tolerance(lpss
->dev
);
210 static void intel_lpss_ltr_hide(struct intel_lpss
*lpss
)
212 dev_pm_qos_hide_latency_tolerance(lpss
->dev
);
213 lpss
->dev
->power
.set_latency_tolerance
= NULL
;
216 static int intel_lpss_assign_devs(struct intel_lpss
*lpss
)
218 const struct mfd_cell
*cell
;
221 type
= lpss
->caps
& LPSS_PRIV_CAPS_TYPE_MASK
;
222 type
>>= LPSS_PRIV_CAPS_TYPE_SHIFT
;
226 cell
= &intel_lpss_i2c_cell
;
229 cell
= &intel_lpss_uart_cell
;
232 cell
= &intel_lpss_spi_cell
;
238 lpss
->cell
= devm_kmemdup(lpss
->dev
, cell
, sizeof(*cell
), GFP_KERNEL
);
247 static bool intel_lpss_has_idma(const struct intel_lpss
*lpss
)
249 return (lpss
->caps
& LPSS_PRIV_CAPS_NO_IDMA
) == 0;
252 static void intel_lpss_set_remap_addr(const struct intel_lpss
*lpss
)
254 resource_size_t addr
= lpss
->info
->mem
->start
;
256 lo_hi_writeq(addr
, lpss
->priv
+ LPSS_PRIV_REMAP_ADDR
);
259 static void intel_lpss_deassert_reset(const struct intel_lpss
*lpss
)
261 u32 value
= LPSS_PRIV_RESETS_FUNC
| LPSS_PRIV_RESETS_IDMA
;
263 /* Bring out the device from reset */
264 writel(value
, lpss
->priv
+ LPSS_PRIV_RESETS
);
267 static void intel_lpss_init_dev(const struct intel_lpss
*lpss
)
269 u32 value
= LPSS_PRIV_SSP_REG_DIS_DMA_FIN
;
271 /* Set the device in reset state */
272 writel(0, lpss
->priv
+ LPSS_PRIV_RESETS
);
274 intel_lpss_deassert_reset(lpss
);
276 intel_lpss_set_remap_addr(lpss
);
278 if (!intel_lpss_has_idma(lpss
))
281 /* Make sure that SPI multiblock DMA transfers are re-enabled */
282 if (lpss
->type
== LPSS_DEV_SPI
)
283 writel(value
, lpss
->priv
+ LPSS_PRIV_SSP_REG
);
286 static void intel_lpss_unregister_clock_tree(struct clk
*clk
)
291 parent
= clk_get_parent(clk
);
297 static int intel_lpss_register_clock_divider(struct intel_lpss
*lpss
,
302 struct clk
*tmp
= *clk
;
305 snprintf(name
, sizeof(name
), "%s-enable", devname
);
306 tmp
= clk_register_gate(NULL
, name
, __clk_get_name(tmp
), 0,
307 lpss
->priv
, 0, 0, NULL
);
311 snprintf(name
, sizeof(name
), "%s-div", devname
);
312 tmp
= clk_register_fractional_divider(NULL
, name
, __clk_get_name(tmp
),
313 0, lpss
->priv
, 1, 15, 16, 15,
314 CLK_FRAC_DIVIDER_POWER_OF_TWO_PS
,
320 if (lpss
->info
->quirks
& QUIRK_CLOCK_DIVIDER_UNITY
) {
321 ret
= clk_set_rate(tmp
, lpss
->info
->clk_rate
);
326 snprintf(name
, sizeof(name
), "%s-update", devname
);
327 tmp
= clk_register_gate(NULL
, name
, __clk_get_name(tmp
),
328 CLK_SET_RATE_PARENT
, lpss
->priv
, 31, 0, NULL
);
336 static int intel_lpss_register_clock(struct intel_lpss
*lpss
)
338 const struct mfd_cell
*cell
= lpss
->cell
;
343 if (!lpss
->info
->clk_rate
)
347 clk
= clk_register_fixed_rate(NULL
, dev_name(lpss
->dev
), NULL
, 0,
348 lpss
->info
->clk_rate
);
352 snprintf(devname
, sizeof(devname
), "%s.%d", cell
->name
, lpss
->devid
);
355 * Support for clock divider only if it has some preset value.
356 * Otherwise we assume that the divider is not used.
358 if (lpss
->type
!= LPSS_DEV_I2C
) {
359 ret
= intel_lpss_register_clock_divider(lpss
, devname
, &clk
);
361 goto err_clk_register
;
366 /* Clock for the host controller */
367 lpss
->clock
= clkdev_create(clk
, lpss
->info
->clk_con_id
, "%s", devname
);
369 goto err_clk_register
;
376 intel_lpss_unregister_clock_tree(clk
);
381 static void intel_lpss_unregister_clock(struct intel_lpss
*lpss
)
383 if (IS_ERR_OR_NULL(lpss
->clk
))
386 clkdev_drop(lpss
->clock
);
387 intel_lpss_unregister_clock_tree(lpss
->clk
);
390 int intel_lpss_probe(struct device
*dev
,
391 const struct intel_lpss_platform_info
*info
)
393 struct intel_lpss
*lpss
;
396 if (!info
|| !info
->mem
)
402 lpss
= devm_kzalloc(dev
, sizeof(*lpss
), GFP_KERNEL
);
406 lpss
->priv
= devm_ioremap_uc(dev
, info
->mem
->start
+ LPSS_PRIV_OFFSET
,
413 lpss
->caps
= readl(lpss
->priv
+ LPSS_PRIV_CAPS
);
415 dev_set_drvdata(dev
, lpss
);
417 ret
= intel_lpss_assign_devs(lpss
);
421 lpss
->cell
->swnode
= info
->swnode
;
422 lpss
->cell
->ignore_resource_conflicts
= info
->quirks
& QUIRK_IGNORE_RESOURCE_CONFLICTS
;
424 intel_lpss_init_dev(lpss
);
426 lpss
->devid
= ida_alloc(&intel_lpss_devid_ida
, GFP_KERNEL
);
430 ret
= intel_lpss_register_clock(lpss
);
432 goto err_clk_register
;
434 intel_lpss_ltr_expose(lpss
);
436 ret
= intel_lpss_debugfs_add(lpss
);
438 dev_warn(dev
, "Failed to create debugfs entries\n");
440 if (intel_lpss_has_idma(lpss
)) {
441 ret
= mfd_add_devices(dev
, lpss
->devid
, &intel_lpss_idma64_cell
,
442 1, info
->mem
, info
->irq
, NULL
);
444 dev_warn(dev
, "Failed to add %s, fallback to PIO\n",
445 LPSS_IDMA64_DRIVER_NAME
);
448 ret
= mfd_add_devices(dev
, lpss
->devid
, lpss
->cell
,
449 1, info
->mem
, info
->irq
, NULL
);
453 dev_pm_set_driver_flags(dev
, DPM_FLAG_SMART_SUSPEND
);
458 intel_lpss_debugfs_remove(lpss
);
459 intel_lpss_ltr_hide(lpss
);
460 intel_lpss_unregister_clock(lpss
);
463 ida_free(&intel_lpss_devid_ida
, lpss
->devid
);
467 EXPORT_SYMBOL_NS_GPL(intel_lpss_probe
, "INTEL_LPSS");
469 void intel_lpss_remove(struct device
*dev
)
471 struct intel_lpss
*lpss
= dev_get_drvdata(dev
);
473 mfd_remove_devices(dev
);
474 intel_lpss_debugfs_remove(lpss
);
475 intel_lpss_ltr_hide(lpss
);
476 intel_lpss_unregister_clock(lpss
);
477 ida_free(&intel_lpss_devid_ida
, lpss
->devid
);
479 EXPORT_SYMBOL_NS_GPL(intel_lpss_remove
, "INTEL_LPSS");
481 static int resume_lpss_device(struct device
*dev
, void *data
)
483 if (!dev_pm_test_driver_flags(dev
, DPM_FLAG_SMART_SUSPEND
))
484 pm_runtime_resume(dev
);
489 static int intel_lpss_prepare(struct device
*dev
)
492 * Resume both child devices before entering system sleep. This
493 * ensures that they are in proper state before they get suspended.
495 device_for_each_child_reverse(dev
, NULL
, resume_lpss_device
);
499 static int intel_lpss_suspend(struct device
*dev
)
501 struct intel_lpss
*lpss
= dev_get_drvdata(dev
);
504 /* Save device context */
505 for (i
= 0; i
< LPSS_PRIV_REG_COUNT
; i
++)
506 lpss
->priv_ctx
[i
] = readl(lpss
->priv
+ i
* 4);
509 * If the device type is not UART, then put the controller into
510 * reset. UART cannot be put into reset since S3/S0ix fail when
511 * no_console_suspend flag is enabled.
513 if (lpss
->type
!= LPSS_DEV_UART
)
514 writel(0, lpss
->priv
+ LPSS_PRIV_RESETS
);
519 static int intel_lpss_resume(struct device
*dev
)
521 struct intel_lpss
*lpss
= dev_get_drvdata(dev
);
524 intel_lpss_deassert_reset(lpss
);
526 /* Restore device context */
527 for (i
= 0; i
< LPSS_PRIV_REG_COUNT
; i
++)
528 writel(lpss
->priv_ctx
[i
], lpss
->priv
+ i
* 4);
533 EXPORT_NS_GPL_DEV_PM_OPS(intel_lpss_pm_ops
, INTEL_LPSS
) = {
534 .prepare
= pm_sleep_ptr(&intel_lpss_prepare
),
535 LATE_SYSTEM_SLEEP_PM_OPS(intel_lpss_suspend
, intel_lpss_resume
)
536 RUNTIME_PM_OPS(intel_lpss_suspend
, intel_lpss_resume
, NULL
)
539 static int __init
intel_lpss_init(void)
541 intel_lpss_debugfs
= debugfs_create_dir("intel_lpss", NULL
);
544 module_init(intel_lpss_init
);
546 static void __exit
intel_lpss_exit(void)
548 ida_destroy(&intel_lpss_devid_ida
);
549 debugfs_remove(intel_lpss_debugfs
);
551 module_exit(intel_lpss_exit
);
553 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
554 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
555 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
556 MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@linux.intel.com>");
557 MODULE_DESCRIPTION("Intel LPSS core driver");
558 MODULE_LICENSE("GPL v2");
560 * Ensure the DMA driver is loaded before the host controller device appears,
561 * so that the host controller driver can request its DMA channels as early
564 * If the DMA module is not there that's OK as well.
566 MODULE_SOFTDEP("pre: platform:" LPSS_IDMA64_DRIVER_NAME
);