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