18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2018-2019, Intel Corporation. 48c2ecf20Sopenharmony_ci * Copyright (C) 2012 Freescale Semiconductor, Inc. 58c2ecf20Sopenharmony_ci * Copyright (C) 2012 Linaro Ltd. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Based on syscon driver. 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/arm-smccc.h> 118c2ecf20Sopenharmony_ci#include <linux/err.h> 128c2ecf20Sopenharmony_ci#include <linux/io.h> 138c2ecf20Sopenharmony_ci#include <linux/mfd/altera-sysmgr.h> 148c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h> 158c2ecf20Sopenharmony_ci#include <linux/module.h> 168c2ecf20Sopenharmony_ci#include <linux/of.h> 178c2ecf20Sopenharmony_ci#include <linux/of_address.h> 188c2ecf20Sopenharmony_ci#include <linux/of_platform.h> 198c2ecf20Sopenharmony_ci#include <linux/regmap.h> 208c2ecf20Sopenharmony_ci#include <linux/slab.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci/** 238c2ecf20Sopenharmony_ci * struct altr_sysmgr - Altera SOCFPGA System Manager 248c2ecf20Sopenharmony_ci * @regmap: the regmap used for System Manager accesses. 258c2ecf20Sopenharmony_ci */ 268c2ecf20Sopenharmony_cistruct altr_sysmgr { 278c2ecf20Sopenharmony_ci struct regmap *regmap; 288c2ecf20Sopenharmony_ci}; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cistatic struct platform_driver altr_sysmgr_driver; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci/** 338c2ecf20Sopenharmony_ci * s10_protected_reg_write 348c2ecf20Sopenharmony_ci * Write to a protected SMC register. 358c2ecf20Sopenharmony_ci * @base: Base address of System Manager 368c2ecf20Sopenharmony_ci * @reg: Address offset of register 378c2ecf20Sopenharmony_ci * @val: Value to write 388c2ecf20Sopenharmony_ci * Return: INTEL_SIP_SMC_STATUS_OK (0) on success 398c2ecf20Sopenharmony_ci * INTEL_SIP_SMC_REG_ERROR on error 408c2ecf20Sopenharmony_ci * INTEL_SIP_SMC_RETURN_UNKNOWN_FUNCTION if not supported 418c2ecf20Sopenharmony_ci */ 428c2ecf20Sopenharmony_cistatic int s10_protected_reg_write(void *base, 438c2ecf20Sopenharmony_ci unsigned int reg, unsigned int val) 448c2ecf20Sopenharmony_ci{ 458c2ecf20Sopenharmony_ci struct arm_smccc_res result; 468c2ecf20Sopenharmony_ci unsigned long sysmgr_base = (unsigned long)base; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci arm_smccc_smc(INTEL_SIP_SMC_REG_WRITE, sysmgr_base + reg, 498c2ecf20Sopenharmony_ci val, 0, 0, 0, 0, 0, &result); 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci return (int)result.a0; 528c2ecf20Sopenharmony_ci} 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci/** 558c2ecf20Sopenharmony_ci * s10_protected_reg_read 568c2ecf20Sopenharmony_ci * Read the status of a protected SMC register 578c2ecf20Sopenharmony_ci * @base: Base address of System Manager. 588c2ecf20Sopenharmony_ci * @reg: Address of register 598c2ecf20Sopenharmony_ci * @val: Value read. 608c2ecf20Sopenharmony_ci * Return: INTEL_SIP_SMC_STATUS_OK (0) on success 618c2ecf20Sopenharmony_ci * INTEL_SIP_SMC_REG_ERROR on error 628c2ecf20Sopenharmony_ci * INTEL_SIP_SMC_RETURN_UNKNOWN_FUNCTION if not supported 638c2ecf20Sopenharmony_ci */ 648c2ecf20Sopenharmony_cistatic int s10_protected_reg_read(void *base, 658c2ecf20Sopenharmony_ci unsigned int reg, unsigned int *val) 668c2ecf20Sopenharmony_ci{ 678c2ecf20Sopenharmony_ci struct arm_smccc_res result; 688c2ecf20Sopenharmony_ci unsigned long sysmgr_base = (unsigned long)base; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci arm_smccc_smc(INTEL_SIP_SMC_REG_READ, sysmgr_base + reg, 718c2ecf20Sopenharmony_ci 0, 0, 0, 0, 0, 0, &result); 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci *val = (unsigned int)result.a1; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci return (int)result.a0; 768c2ecf20Sopenharmony_ci} 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_cistatic struct regmap_config altr_sysmgr_regmap_cfg = { 798c2ecf20Sopenharmony_ci .name = "altr_sysmgr", 808c2ecf20Sopenharmony_ci .reg_bits = 32, 818c2ecf20Sopenharmony_ci .reg_stride = 4, 828c2ecf20Sopenharmony_ci .val_bits = 32, 838c2ecf20Sopenharmony_ci .fast_io = true, 848c2ecf20Sopenharmony_ci .use_single_read = true, 858c2ecf20Sopenharmony_ci .use_single_write = true, 868c2ecf20Sopenharmony_ci}; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci/** 898c2ecf20Sopenharmony_ci * altr_sysmgr_regmap_lookup_by_phandle 908c2ecf20Sopenharmony_ci * Find the sysmgr previous configured in probe() and return regmap property. 918c2ecf20Sopenharmony_ci * Return: regmap if found or error if not found. 928c2ecf20Sopenharmony_ci * 938c2ecf20Sopenharmony_ci * @np: Pointer to device's Device Tree node 948c2ecf20Sopenharmony_ci * @property: Device Tree property name which references the sysmgr 958c2ecf20Sopenharmony_ci */ 968c2ecf20Sopenharmony_cistruct regmap *altr_sysmgr_regmap_lookup_by_phandle(struct device_node *np, 978c2ecf20Sopenharmony_ci const char *property) 988c2ecf20Sopenharmony_ci{ 998c2ecf20Sopenharmony_ci struct device *dev; 1008c2ecf20Sopenharmony_ci struct altr_sysmgr *sysmgr; 1018c2ecf20Sopenharmony_ci struct device_node *sysmgr_np; 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci if (property) 1048c2ecf20Sopenharmony_ci sysmgr_np = of_parse_phandle(np, property, 0); 1058c2ecf20Sopenharmony_ci else 1068c2ecf20Sopenharmony_ci sysmgr_np = np; 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci if (!sysmgr_np) 1098c2ecf20Sopenharmony_ci return ERR_PTR(-ENODEV); 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci dev = driver_find_device_by_of_node(&altr_sysmgr_driver.driver, 1128c2ecf20Sopenharmony_ci (void *)sysmgr_np); 1138c2ecf20Sopenharmony_ci of_node_put(sysmgr_np); 1148c2ecf20Sopenharmony_ci if (!dev) 1158c2ecf20Sopenharmony_ci return ERR_PTR(-EPROBE_DEFER); 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci sysmgr = dev_get_drvdata(dev); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci return sysmgr->regmap; 1208c2ecf20Sopenharmony_ci} 1218c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(altr_sysmgr_regmap_lookup_by_phandle); 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_cistatic int sysmgr_probe(struct platform_device *pdev) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci struct altr_sysmgr *sysmgr; 1268c2ecf20Sopenharmony_ci struct regmap *regmap; 1278c2ecf20Sopenharmony_ci struct resource *res; 1288c2ecf20Sopenharmony_ci struct regmap_config sysmgr_config = altr_sysmgr_regmap_cfg; 1298c2ecf20Sopenharmony_ci struct device *dev = &pdev->dev; 1308c2ecf20Sopenharmony_ci struct device_node *np = dev->of_node; 1318c2ecf20Sopenharmony_ci void __iomem *base; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci sysmgr = devm_kzalloc(dev, sizeof(*sysmgr), GFP_KERNEL); 1348c2ecf20Sopenharmony_ci if (!sysmgr) 1358c2ecf20Sopenharmony_ci return -ENOMEM; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1388c2ecf20Sopenharmony_ci if (!res) 1398c2ecf20Sopenharmony_ci return -ENOENT; 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci sysmgr_config.max_register = resource_size(res) - 1428c2ecf20Sopenharmony_ci sysmgr_config.reg_stride; 1438c2ecf20Sopenharmony_ci if (of_device_is_compatible(np, "altr,sys-mgr-s10")) { 1448c2ecf20Sopenharmony_ci sysmgr_config.reg_read = s10_protected_reg_read; 1458c2ecf20Sopenharmony_ci sysmgr_config.reg_write = s10_protected_reg_write; 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci /* Need physical address for SMCC call */ 1488c2ecf20Sopenharmony_ci regmap = devm_regmap_init(dev, NULL, 1498c2ecf20Sopenharmony_ci (void *)(uintptr_t)res->start, 1508c2ecf20Sopenharmony_ci &sysmgr_config); 1518c2ecf20Sopenharmony_ci } else { 1528c2ecf20Sopenharmony_ci base = devm_ioremap(dev, res->start, resource_size(res)); 1538c2ecf20Sopenharmony_ci if (!base) 1548c2ecf20Sopenharmony_ci return -ENOMEM; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci sysmgr_config.max_register = res->end - res->start - 3; 1578c2ecf20Sopenharmony_ci regmap = devm_regmap_init_mmio(dev, base, &sysmgr_config); 1588c2ecf20Sopenharmony_ci } 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci if (IS_ERR(regmap)) { 1618c2ecf20Sopenharmony_ci pr_err("regmap init failed\n"); 1628c2ecf20Sopenharmony_ci return PTR_ERR(regmap); 1638c2ecf20Sopenharmony_ci } 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci sysmgr->regmap = regmap; 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, sysmgr); 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci return 0; 1708c2ecf20Sopenharmony_ci} 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_cistatic const struct of_device_id altr_sysmgr_of_match[] = { 1738c2ecf20Sopenharmony_ci { .compatible = "altr,sys-mgr" }, 1748c2ecf20Sopenharmony_ci { .compatible = "altr,sys-mgr-s10" }, 1758c2ecf20Sopenharmony_ci {}, 1768c2ecf20Sopenharmony_ci}; 1778c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, altr_sysmgr_of_match); 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_cistatic struct platform_driver altr_sysmgr_driver = { 1808c2ecf20Sopenharmony_ci .probe = sysmgr_probe, 1818c2ecf20Sopenharmony_ci .driver = { 1828c2ecf20Sopenharmony_ci .name = "altr,system_manager", 1838c2ecf20Sopenharmony_ci .of_match_table = altr_sysmgr_of_match, 1848c2ecf20Sopenharmony_ci }, 1858c2ecf20Sopenharmony_ci}; 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_cistatic int __init altr_sysmgr_init(void) 1888c2ecf20Sopenharmony_ci{ 1898c2ecf20Sopenharmony_ci return platform_driver_register(&altr_sysmgr_driver); 1908c2ecf20Sopenharmony_ci} 1918c2ecf20Sopenharmony_cicore_initcall(altr_sysmgr_init); 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_cistatic void __exit altr_sysmgr_exit(void) 1948c2ecf20Sopenharmony_ci{ 1958c2ecf20Sopenharmony_ci platform_driver_unregister(&altr_sysmgr_driver); 1968c2ecf20Sopenharmony_ci} 1978c2ecf20Sopenharmony_cimodule_exit(altr_sysmgr_exit); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ciMODULE_AUTHOR("Thor Thayer <>"); 2008c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("SOCFPGA System Manager driver"); 2018c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 202