1 // SPDX-License-Identifier: GPL-2.0
3 // rcpm.c - Freescale QorIQ RCPM driver
5 // Copyright 2019-2020 NXP
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/of_address.h>
13 #include <linux/slab.h>
14 #include <linux/suspend.h>
15 #include <linux/kernel.h>
16 #include <linux/acpi.h>
18 #define RCPM_WAKEUP_CELL_MAX_SIZE 7
21 unsigned int wakeup_cells;
22 void __iomem *ippdexpcr_base;
26 #define SCFG_SPARECR8 0x051c
28 static void copy_ippdexpcr1_setting(u32 val)
30 struct device_node *np;
34 np = of_find_compatible_node(NULL, NULL, "fsl,ls1021a-scfg");
38 regs = of_iomap(np, 0);
43 reg_val = ioread32be(regs + SCFG_SPARECR8);
44 iowrite32be(val | reg_val, regs + SCFG_SPARECR8);
50 * rcpm_pm_prepare - performs device-level tasks associated with power
51 * management, such as programming related to the wakeup source control.
52 * @dev: Device to handle.
55 static int rcpm_pm_prepare(struct device *dev)
59 struct wakeup_source *ws;
61 struct device_node *np = dev->of_node;
62 u32 value[RCPM_WAKEUP_CELL_MAX_SIZE + 1];
63 u32 setting[RCPM_WAKEUP_CELL_MAX_SIZE] = {0};
65 rcpm = dev_get_drvdata(dev);
69 base = rcpm->ippdexpcr_base;
70 idx = wakeup_sources_read_lock();
72 /* Begin with first registered wakeup source */
73 for_each_wakeup_source(ws) {
75 /* skip object which is not attached to device */
76 if (!ws->dev || !ws->dev->parent)
79 ret = device_property_read_u32_array(ws->dev->parent,
80 "fsl,rcpm-wakeup", value,
81 rcpm->wakeup_cells + 1);
87 * For DT mode, would handle devices with "fsl,rcpm-wakeup"
88 * pointing to the current RCPM node.
90 * For ACPI mode, currently we assume there is only one
91 * RCPM controller existing.
93 if (is_of_node(dev->fwnode))
94 if (np->phandle != value[0])
97 /* Property "#fsl,rcpm-wakeup-cells" of rcpm node defines the
98 * number of IPPDEXPCR register cells, and "fsl,rcpm-wakeup"
99 * of wakeup source IP contains an integer array: <phandle to
100 * RCPM node, IPPDEXPCR0 setting, IPPDEXPCR1 setting,
101 * IPPDEXPCR2 setting, etc>.
103 * So we will go thought them to collect setting data.
105 for (i = 0; i < rcpm->wakeup_cells; i++)
106 setting[i] |= value[i + 1];
109 wakeup_sources_read_unlock(idx);
111 /* Program all IPPDEXPCRn once */
112 for (i = 0; i < rcpm->wakeup_cells; i++) {
113 u32 tmp = setting[i];
114 void __iomem *address = base + i * 4;
119 /* We can only OR related bits */
120 if (rcpm->little_endian) {
121 tmp |= ioread32(address);
122 iowrite32(tmp, address);
124 tmp |= ioread32be(address);
125 iowrite32be(tmp, address);
128 * Workaround of errata A-008646 on SoC LS1021A:
129 * There is a bug of register ippdexpcr1.
130 * Reading configuration register RCPM_IPPDEXPCR1
131 * always return zero. So save ippdexpcr1's value
132 * to register SCFG_SPARECR8.And the value of
133 * ippdexpcr1 will be read from SCFG_SPARECR8.
135 if (dev_of_node(dev) && (i == 1))
136 if (of_device_is_compatible(np, "fsl,ls1021a-rcpm"))
137 copy_ippdexpcr1_setting(tmp);
143 static const struct dev_pm_ops rcpm_pm_ops = {
144 .prepare = rcpm_pm_prepare,
147 static int rcpm_probe(struct platform_device *pdev)
149 struct device *dev = &pdev->dev;
153 rcpm = devm_kzalloc(dev, sizeof(*rcpm), GFP_KERNEL);
157 rcpm->ippdexpcr_base = devm_platform_ioremap_resource(pdev, 0);
158 if (IS_ERR(rcpm->ippdexpcr_base)) {
159 ret = PTR_ERR(rcpm->ippdexpcr_base);
163 rcpm->little_endian = device_property_read_bool(
164 &pdev->dev, "little-endian");
166 ret = device_property_read_u32(&pdev->dev,
167 "#fsl,rcpm-wakeup-cells", &rcpm->wakeup_cells);
171 dev_set_drvdata(&pdev->dev, rcpm);
176 static const struct of_device_id rcpm_of_match[] = {
177 { .compatible = "fsl,qoriq-rcpm-2.1+", },
180 MODULE_DEVICE_TABLE(of, rcpm_of_match);
183 static const struct acpi_device_id rcpm_acpi_ids[] = {
187 MODULE_DEVICE_TABLE(acpi, rcpm_acpi_ids);
190 static struct platform_driver rcpm_driver = {
193 .of_match_table = rcpm_of_match,
194 .acpi_match_table = ACPI_PTR(rcpm_acpi_ids),
200 module_platform_driver(rcpm_driver);