^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2018-2019, Intel Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2012 Freescale Semiconductor, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2012 Linaro Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Based on syscon driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/arm-smccc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mfd/altera-sysmgr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/mfd/syscon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/of_address.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/of_platform.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * struct altr_sysmgr - Altera SOCFPGA System Manager
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * @regmap: the regmap used for System Manager accesses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct altr_sysmgr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static struct platform_driver altr_sysmgr_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * s10_protected_reg_write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * Write to a protected SMC register.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * @base: Base address of System Manager
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * @reg: Address offset of register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * @val: Value to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * Return: INTEL_SIP_SMC_STATUS_OK (0) on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * INTEL_SIP_SMC_REG_ERROR on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * INTEL_SIP_SMC_RETURN_UNKNOWN_FUNCTION if not supported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static int s10_protected_reg_write(void *base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) unsigned int reg, unsigned int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct arm_smccc_res result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned long sysmgr_base = (unsigned long)base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) arm_smccc_smc(INTEL_SIP_SMC_REG_WRITE, sysmgr_base + reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) val, 0, 0, 0, 0, 0, &result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return (int)result.a0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * s10_protected_reg_read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * Read the status of a protected SMC register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * @base: Base address of System Manager.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * @reg: Address of register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * @val: Value read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * Return: INTEL_SIP_SMC_STATUS_OK (0) on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * INTEL_SIP_SMC_REG_ERROR on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * INTEL_SIP_SMC_RETURN_UNKNOWN_FUNCTION if not supported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static int s10_protected_reg_read(void *base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) unsigned int reg, unsigned int *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct arm_smccc_res result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) unsigned long sysmgr_base = (unsigned long)base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) arm_smccc_smc(INTEL_SIP_SMC_REG_READ, sysmgr_base + reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) 0, 0, 0, 0, 0, 0, &result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) *val = (unsigned int)result.a1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return (int)result.a0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static struct regmap_config altr_sysmgr_regmap_cfg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) .name = "altr_sysmgr",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) .reg_bits = 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .reg_stride = 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .val_bits = 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .fast_io = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) .use_single_read = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .use_single_write = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * altr_sysmgr_regmap_lookup_by_phandle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * Find the sysmgr previous configured in probe() and return regmap property.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * Return: regmap if found or error if not found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * @np: Pointer to device's Device Tree node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * @property: Device Tree property name which references the sysmgr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct regmap *altr_sysmgr_regmap_lookup_by_phandle(struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) const char *property)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct altr_sysmgr *sysmgr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct device_node *sysmgr_np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (property)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) sysmgr_np = of_parse_phandle(np, property, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) sysmgr_np = np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (!sysmgr_np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) dev = driver_find_device_by_of_node(&altr_sysmgr_driver.driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) (void *)sysmgr_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) of_node_put(sysmgr_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return ERR_PTR(-EPROBE_DEFER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) sysmgr = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return sysmgr->regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) EXPORT_SYMBOL_GPL(altr_sysmgr_regmap_lookup_by_phandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static int sysmgr_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct altr_sysmgr *sysmgr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct regmap_config sysmgr_config = altr_sysmgr_regmap_cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) void __iomem *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) sysmgr = devm_kzalloc(dev, sizeof(*sysmgr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (!sysmgr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) sysmgr_config.max_register = resource_size(res) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) sysmgr_config.reg_stride;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (of_device_is_compatible(np, "altr,sys-mgr-s10")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) sysmgr_config.reg_read = s10_protected_reg_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) sysmgr_config.reg_write = s10_protected_reg_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /* Need physical address for SMCC call */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) regmap = devm_regmap_init(dev, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) (void *)(uintptr_t)res->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) &sysmgr_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) base = devm_ioremap(dev, res->start, resource_size(res));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (!base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) sysmgr_config.max_register = res->end - res->start - 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) regmap = devm_regmap_init_mmio(dev, base, &sysmgr_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (IS_ERR(regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) pr_err("regmap init failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return PTR_ERR(regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) sysmgr->regmap = regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) platform_set_drvdata(pdev, sysmgr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static const struct of_device_id altr_sysmgr_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) { .compatible = "altr,sys-mgr" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) { .compatible = "altr,sys-mgr-s10" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) MODULE_DEVICE_TABLE(of, altr_sysmgr_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static struct platform_driver altr_sysmgr_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) .probe = sysmgr_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) .name = "altr,system_manager",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) .of_match_table = altr_sysmgr_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static int __init altr_sysmgr_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return platform_driver_register(&altr_sysmgr_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) core_initcall(altr_sysmgr_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static void __exit altr_sysmgr_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) platform_driver_unregister(&altr_sysmgr_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) module_exit(altr_sysmgr_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) MODULE_AUTHOR("Thor Thayer <>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) MODULE_DESCRIPTION("SOCFPGA System Manager driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) MODULE_LICENSE("GPL v2");