1 // SPDX-License-Identifier: GPL-2.0-only
3 * DDR Self-Refresh Power Down (SRPD) support for Broadcom STB SoCs
7 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/of_device.h>
12 #include <linux/platform_device.h>
14 #define REG_MEMC_CNTRLR_CONFIG 0x00
15 #define CNTRLR_CONFIG_LPDDR4_SHIFT 5
16 #define CNTRLR_CONFIG_MASK 0xf
17 #define REG_MEMC_SRPD_CFG_21 0x20
18 #define REG_MEMC_SRPD_CFG_20 0x34
19 #define REG_MEMC_SRPD_CFG_1x 0x3c
20 #define INACT_COUNT_SHIFT 0
21 #define INACT_COUNT_MASK 0xffff
22 #define SRPD_EN_SHIFT 16
24 struct brcmstb_memc_data {
30 void __iomem *ddr_ctrl;
31 unsigned int timeout_cycles;
36 static int brcmstb_memc_uses_lpddr4(struct brcmstb_memc *memc)
38 void __iomem *config = memc->ddr_ctrl + REG_MEMC_CNTRLR_CONFIG;
41 reg = readl_relaxed(config) & CNTRLR_CONFIG_MASK;
43 return reg == CNTRLR_CONFIG_LPDDR4_SHIFT;
46 static int brcmstb_memc_srpd_config(struct brcmstb_memc *memc,
49 void __iomem *cfg = memc->ddr_ctrl + memc->srpd_offset;
52 /* Max timeout supported in HW */
53 if (cycles > INACT_COUNT_MASK)
56 memc->timeout_cycles = cycles;
58 val = (cycles << INACT_COUNT_SHIFT) & INACT_COUNT_MASK;
60 val |= BIT(SRPD_EN_SHIFT);
62 writel_relaxed(val, cfg);
63 /* Ensure the write is committed to the controller */
64 (void)readl_relaxed(cfg);
69 static ssize_t frequency_show(struct device *dev,
70 struct device_attribute *attr, char *buf)
72 struct brcmstb_memc *memc = dev_get_drvdata(dev);
74 return sprintf(buf, "%d\n", memc->frequency);
77 static ssize_t srpd_show(struct device *dev,
78 struct device_attribute *attr, char *buf)
80 struct brcmstb_memc *memc = dev_get_drvdata(dev);
82 return sprintf(buf, "%d\n", memc->timeout_cycles);
85 static ssize_t srpd_store(struct device *dev, struct device_attribute *attr,
86 const char *buf, size_t count)
88 struct brcmstb_memc *memc = dev_get_drvdata(dev);
93 * Cannot change the inactivity timeout on LPDDR4 chips because the
94 * dynamic tuning process will also get affected by the inactivity
95 * timeout, thus making it non functional.
97 if (brcmstb_memc_uses_lpddr4(memc))
100 ret = kstrtouint(buf, 10, &val);
104 ret = brcmstb_memc_srpd_config(memc, val);
111 static DEVICE_ATTR_RO(frequency);
112 static DEVICE_ATTR_RW(srpd);
114 static struct attribute *dev_attrs[] = {
115 &dev_attr_frequency.attr,
120 static struct attribute_group dev_attr_group = {
124 static const struct of_device_id brcmstb_memc_of_match[];
126 static int brcmstb_memc_probe(struct platform_device *pdev)
128 const struct brcmstb_memc_data *memc_data;
129 const struct of_device_id *of_id;
130 struct device *dev = &pdev->dev;
131 struct brcmstb_memc *memc;
134 memc = devm_kzalloc(dev, sizeof(*memc), GFP_KERNEL);
138 dev_set_drvdata(dev, memc);
140 of_id = of_match_device(brcmstb_memc_of_match, dev);
141 memc_data = of_id->data;
142 memc->srpd_offset = memc_data->srpd_offset;
144 memc->ddr_ctrl = devm_platform_ioremap_resource(pdev, 0);
145 if (IS_ERR(memc->ddr_ctrl))
146 return PTR_ERR(memc->ddr_ctrl);
148 of_property_read_u32(pdev->dev.of_node, "clock-frequency",
151 ret = sysfs_create_group(&dev->kobj, &dev_attr_group);
158 static int brcmstb_memc_remove(struct platform_device *pdev)
160 struct device *dev = &pdev->dev;
162 sysfs_remove_group(&dev->kobj, &dev_attr_group);
167 enum brcmstb_memc_hwtype {
173 static const struct brcmstb_memc_data brcmstb_memc_versions[] = {
174 { .srpd_offset = REG_MEMC_SRPD_CFG_21 },
175 { .srpd_offset = REG_MEMC_SRPD_CFG_20 },
176 { .srpd_offset = REG_MEMC_SRPD_CFG_1x },
179 static const struct of_device_id brcmstb_memc_of_match[] = {
181 .compatible = "brcm,brcmstb-memc-ddr-rev-b.1.x",
182 .data = &brcmstb_memc_versions[BRCMSTB_MEMC_V1X]
185 .compatible = "brcm,brcmstb-memc-ddr-rev-b.2.0",
186 .data = &brcmstb_memc_versions[BRCMSTB_MEMC_V20]
189 .compatible = "brcm,brcmstb-memc-ddr-rev-b.2.1",
190 .data = &brcmstb_memc_versions[BRCMSTB_MEMC_V21]
193 .compatible = "brcm,brcmstb-memc-ddr-rev-b.2.2",
194 .data = &brcmstb_memc_versions[BRCMSTB_MEMC_V21]
197 .compatible = "brcm,brcmstb-memc-ddr-rev-b.2.3",
198 .data = &brcmstb_memc_versions[BRCMSTB_MEMC_V21]
201 .compatible = "brcm,brcmstb-memc-ddr-rev-b.2.5",
202 .data = &brcmstb_memc_versions[BRCMSTB_MEMC_V21]
205 .compatible = "brcm,brcmstb-memc-ddr-rev-b.2.6",
206 .data = &brcmstb_memc_versions[BRCMSTB_MEMC_V21]
209 .compatible = "brcm,brcmstb-memc-ddr-rev-b.2.7",
210 .data = &brcmstb_memc_versions[BRCMSTB_MEMC_V21]
213 .compatible = "brcm,brcmstb-memc-ddr-rev-b.2.8",
214 .data = &brcmstb_memc_versions[BRCMSTB_MEMC_V21]
217 .compatible = "brcm,brcmstb-memc-ddr-rev-b.3.0",
218 .data = &brcmstb_memc_versions[BRCMSTB_MEMC_V21]
221 .compatible = "brcm,brcmstb-memc-ddr-rev-b.3.1",
222 .data = &brcmstb_memc_versions[BRCMSTB_MEMC_V21]
225 .compatible = "brcm,brcmstb-memc-ddr-rev-c.1.0",
226 .data = &brcmstb_memc_versions[BRCMSTB_MEMC_V21]
229 .compatible = "brcm,brcmstb-memc-ddr-rev-c.1.1",
230 .data = &brcmstb_memc_versions[BRCMSTB_MEMC_V21]
233 .compatible = "brcm,brcmstb-memc-ddr-rev-c.1.2",
234 .data = &brcmstb_memc_versions[BRCMSTB_MEMC_V21]
237 .compatible = "brcm,brcmstb-memc-ddr-rev-c.1.3",
238 .data = &brcmstb_memc_versions[BRCMSTB_MEMC_V21]
241 .compatible = "brcm,brcmstb-memc-ddr-rev-c.1.4",
242 .data = &brcmstb_memc_versions[BRCMSTB_MEMC_V21]
244 /* default to the original offset */
246 .compatible = "brcm,brcmstb-memc-ddr",
247 .data = &brcmstb_memc_versions[BRCMSTB_MEMC_V1X]
252 static int brcmstb_memc_suspend(struct device *dev)
254 struct brcmstb_memc *memc = dev_get_drvdata(dev);
255 void __iomem *cfg = memc->ddr_ctrl + memc->srpd_offset;
258 if (memc->timeout_cycles == 0)
262 * Disable SRPD prior to suspending the system since that can
263 * cause issues with other memory clients managed by the ARM
264 * trusted firmware to access memory.
266 val = readl_relaxed(cfg);
267 val &= ~BIT(SRPD_EN_SHIFT);
268 writel_relaxed(val, cfg);
269 /* Ensure the write is committed to the controller */
270 (void)readl_relaxed(cfg);
275 static int brcmstb_memc_resume(struct device *dev)
277 struct brcmstb_memc *memc = dev_get_drvdata(dev);
279 if (memc->timeout_cycles == 0)
282 return brcmstb_memc_srpd_config(memc, memc->timeout_cycles);
285 static DEFINE_SIMPLE_DEV_PM_OPS(brcmstb_memc_pm_ops, brcmstb_memc_suspend,
286 brcmstb_memc_resume);
288 static struct platform_driver brcmstb_memc_driver = {
289 .probe = brcmstb_memc_probe,
290 .remove = brcmstb_memc_remove,
292 .name = "brcmstb_memc",
293 .of_match_table = brcmstb_memc_of_match,
294 .pm = pm_ptr(&brcmstb_memc_pm_ops),
297 module_platform_driver(brcmstb_memc_driver);
299 MODULE_LICENSE("GPL");
300 MODULE_AUTHOR("Broadcom");
301 MODULE_DESCRIPTION("DDR SRPD driver for Broadcom STB chips");