exynos-pmu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (c) 2011-2014 Samsung Electronics Co., Ltd.
  4. // http://www.samsung.com/
  5. //
  6. // Exynos - CPU PMU(Power Management Unit) support
  7. #include <linux/arm-smccc.h>
  8. #include <linux/of.h>
  9. #include <linux/of_address.h>
  10. #include <linux/mfd/core.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/delay.h>
  15. #include <linux/regmap.h>
  16. #include <linux/soc/samsung/exynos-regs-pmu.h>
  17. #include <linux/soc/samsung/exynos-pmu.h>
  18. #include "exynos-pmu.h"
  19. #define PMUALIVE_MASK GENMASK(13, 0)
  20. #define TENSOR_SET_BITS (BIT(15) | BIT(14))
  21. #define TENSOR_CLR_BITS BIT(15)
  22. #define TENSOR_SMC_PMU_SEC_REG 0x82000504
  23. #define TENSOR_PMUREG_READ 0
  24. #define TENSOR_PMUREG_WRITE 1
  25. #define TENSOR_PMUREG_RMW 2
  26. struct exynos_pmu_context {
  27. struct device *dev;
  28. const struct exynos_pmu_data *pmu_data;
  29. struct regmap *pmureg;
  30. };
  31. void __iomem *pmu_base_addr;
  32. static struct exynos_pmu_context *pmu_context;
  33. /* forward declaration */
  34. static struct platform_driver exynos_pmu_driver;
  35. /*
  36. * Tensor SoCs are configured so that PMU_ALIVE registers can only be written
  37. * from EL3, but are still read accessible. As Linux needs to write some of
  38. * these registers, the following functions are provided and exposed via
  39. * regmap.
  40. *
  41. * Note: This SMC interface is known to be implemented on gs101 and derivative
  42. * SoCs.
  43. */
  44. /* Write to a protected PMU register. */
  45. static int tensor_sec_reg_write(void *context, unsigned int reg,
  46. unsigned int val)
  47. {
  48. struct arm_smccc_res res;
  49. unsigned long pmu_base = (unsigned long)context;
  50. arm_smccc_smc(TENSOR_SMC_PMU_SEC_REG, pmu_base + reg,
  51. TENSOR_PMUREG_WRITE, val, 0, 0, 0, 0, &res);
  52. /* returns -EINVAL if access isn't allowed or 0 */
  53. if (res.a0)
  54. pr_warn("%s(): SMC failed: %d\n", __func__, (int)res.a0);
  55. return (int)res.a0;
  56. }
  57. /* Read/Modify/Write a protected PMU register. */
  58. static int tensor_sec_reg_rmw(void *context, unsigned int reg,
  59. unsigned int mask, unsigned int val)
  60. {
  61. struct arm_smccc_res res;
  62. unsigned long pmu_base = (unsigned long)context;
  63. arm_smccc_smc(TENSOR_SMC_PMU_SEC_REG, pmu_base + reg,
  64. TENSOR_PMUREG_RMW, mask, val, 0, 0, 0, &res);
  65. /* returns -EINVAL if access isn't allowed or 0 */
  66. if (res.a0)
  67. pr_warn("%s(): SMC failed: %d\n", __func__, (int)res.a0);
  68. return (int)res.a0;
  69. }
  70. /*
  71. * Read a protected PMU register. All PMU registers can be read by Linux.
  72. * Note: The SMC read register is not used, as only registers that can be
  73. * written are readable via SMC.
  74. */
  75. static int tensor_sec_reg_read(void *context, unsigned int reg,
  76. unsigned int *val)
  77. {
  78. *val = pmu_raw_readl(reg);
  79. return 0;
  80. }
  81. /*
  82. * For SoCs that have set/clear bit hardware this function can be used when
  83. * the PMU register will be accessed by multiple masters.
  84. *
  85. * For example, to set bits 13:8 in PMU reg offset 0x3e80
  86. * tensor_set_bits_atomic(ctx, 0x3e80, 0x3f00, 0x3f00);
  87. *
  88. * Set bit 8, and clear bits 13:9 PMU reg offset 0x3e80
  89. * tensor_set_bits_atomic(0x3e80, 0x100, 0x3f00);
  90. */
  91. static int tensor_set_bits_atomic(void *ctx, unsigned int offset, u32 val,
  92. u32 mask)
  93. {
  94. int ret;
  95. unsigned int i;
  96. for (i = 0; i < 32; i++) {
  97. if (!(mask & BIT(i)))
  98. continue;
  99. offset &= ~TENSOR_SET_BITS;
  100. if (val & BIT(i))
  101. offset |= TENSOR_SET_BITS;
  102. else
  103. offset |= TENSOR_CLR_BITS;
  104. ret = tensor_sec_reg_write(ctx, offset, i);
  105. if (ret)
  106. return ret;
  107. }
  108. return 0;
  109. }
  110. static bool tensor_is_atomic(unsigned int reg)
  111. {
  112. /*
  113. * Use atomic operations for PMU_ALIVE registers (offset 0~0x3FFF)
  114. * as the target registers can be accessed by multiple masters. SFRs
  115. * that don't support atomic are added to the switch statement below.
  116. */
  117. if (reg > PMUALIVE_MASK)
  118. return false;
  119. switch (reg) {
  120. case GS101_SYSIP_DAT0:
  121. case GS101_SYSTEM_CONFIGURATION:
  122. return false;
  123. default:
  124. return true;
  125. }
  126. }
  127. static int tensor_sec_update_bits(void *ctx, unsigned int reg,
  128. unsigned int mask, unsigned int val)
  129. {
  130. if (!tensor_is_atomic(reg))
  131. return tensor_sec_reg_rmw(ctx, reg, mask, val);
  132. return tensor_set_bits_atomic(ctx, reg, val, mask);
  133. }
  134. void pmu_raw_writel(u32 val, u32 offset)
  135. {
  136. writel_relaxed(val, pmu_base_addr + offset);
  137. }
  138. u32 pmu_raw_readl(u32 offset)
  139. {
  140. return readl_relaxed(pmu_base_addr + offset);
  141. }
  142. void exynos_sys_powerdown_conf(enum sys_powerdown mode)
  143. {
  144. unsigned int i;
  145. const struct exynos_pmu_data *pmu_data;
  146. if (!pmu_context || !pmu_context->pmu_data)
  147. return;
  148. pmu_data = pmu_context->pmu_data;
  149. if (pmu_data->powerdown_conf)
  150. pmu_data->powerdown_conf(mode);
  151. if (pmu_data->pmu_config) {
  152. for (i = 0; (pmu_data->pmu_config[i].offset != PMU_TABLE_END); i++)
  153. pmu_raw_writel(pmu_data->pmu_config[i].val[mode],
  154. pmu_data->pmu_config[i].offset);
  155. }
  156. if (pmu_data->powerdown_conf_extra)
  157. pmu_data->powerdown_conf_extra(mode);
  158. if (pmu_data->pmu_config_extra) {
  159. for (i = 0; pmu_data->pmu_config_extra[i].offset != PMU_TABLE_END; i++)
  160. pmu_raw_writel(pmu_data->pmu_config_extra[i].val[mode],
  161. pmu_data->pmu_config_extra[i].offset);
  162. }
  163. }
  164. /*
  165. * Split the data between ARM architectures because it is relatively big
  166. * and useless on other arch.
  167. */
  168. #ifdef CONFIG_EXYNOS_PMU_ARM_DRIVERS
  169. #define exynos_pmu_data_arm_ptr(data) (&data)
  170. #else
  171. #define exynos_pmu_data_arm_ptr(data) NULL
  172. #endif
  173. static const struct regmap_config regmap_smccfg = {
  174. .name = "pmu_regs",
  175. .reg_bits = 32,
  176. .reg_stride = 4,
  177. .val_bits = 32,
  178. .fast_io = true,
  179. .use_single_read = true,
  180. .use_single_write = true,
  181. .reg_read = tensor_sec_reg_read,
  182. .reg_write = tensor_sec_reg_write,
  183. .reg_update_bits = tensor_sec_update_bits,
  184. };
  185. static const struct exynos_pmu_data gs101_pmu_data = {
  186. .pmu_secure = true
  187. };
  188. /*
  189. * PMU platform driver and devicetree bindings.
  190. */
  191. static const struct of_device_id exynos_pmu_of_device_ids[] = {
  192. {
  193. .compatible = "google,gs101-pmu",
  194. .data = &gs101_pmu_data,
  195. }, {
  196. .compatible = "samsung,exynos3250-pmu",
  197. .data = exynos_pmu_data_arm_ptr(exynos3250_pmu_data),
  198. }, {
  199. .compatible = "samsung,exynos4210-pmu",
  200. .data = exynos_pmu_data_arm_ptr(exynos4210_pmu_data),
  201. }, {
  202. .compatible = "samsung,exynos4212-pmu",
  203. .data = exynos_pmu_data_arm_ptr(exynos4212_pmu_data),
  204. }, {
  205. .compatible = "samsung,exynos4412-pmu",
  206. .data = exynos_pmu_data_arm_ptr(exynos4412_pmu_data),
  207. }, {
  208. .compatible = "samsung,exynos5250-pmu",
  209. .data = exynos_pmu_data_arm_ptr(exynos5250_pmu_data),
  210. }, {
  211. .compatible = "samsung,exynos5410-pmu",
  212. }, {
  213. .compatible = "samsung,exynos5420-pmu",
  214. .data = exynos_pmu_data_arm_ptr(exynos5420_pmu_data),
  215. }, {
  216. .compatible = "samsung,exynos5433-pmu",
  217. }, {
  218. .compatible = "samsung,exynos7-pmu",
  219. }, {
  220. .compatible = "samsung,exynos850-pmu",
  221. },
  222. { /*sentinel*/ },
  223. };
  224. static const struct mfd_cell exynos_pmu_devs[] = {
  225. { .name = "exynos-clkout", },
  226. };
  227. /**
  228. * exynos_get_pmu_regmap() - Obtain pmureg regmap
  229. *
  230. * Find the pmureg regmap previously configured in probe() and return regmap
  231. * pointer.
  232. *
  233. * Return: A pointer to regmap if found or ERR_PTR error value.
  234. */
  235. struct regmap *exynos_get_pmu_regmap(void)
  236. {
  237. struct device_node *np = of_find_matching_node(NULL,
  238. exynos_pmu_of_device_ids);
  239. if (np)
  240. return exynos_get_pmu_regmap_by_phandle(np, NULL);
  241. return ERR_PTR(-ENODEV);
  242. }
  243. EXPORT_SYMBOL_GPL(exynos_get_pmu_regmap);
  244. /**
  245. * exynos_get_pmu_regmap_by_phandle() - Obtain pmureg regmap via phandle
  246. * @np: Device node holding PMU phandle property
  247. * @propname: Name of property holding phandle value
  248. *
  249. * Find the pmureg regmap previously configured in probe() and return regmap
  250. * pointer.
  251. *
  252. * Return: A pointer to regmap if found or ERR_PTR error value.
  253. */
  254. struct regmap *exynos_get_pmu_regmap_by_phandle(struct device_node *np,
  255. const char *propname)
  256. {
  257. struct device_node *pmu_np;
  258. struct device *dev;
  259. if (propname)
  260. pmu_np = of_parse_phandle(np, propname, 0);
  261. else
  262. pmu_np = np;
  263. if (!pmu_np)
  264. return ERR_PTR(-ENODEV);
  265. /*
  266. * Determine if exynos-pmu device has probed and therefore regmap
  267. * has been created and can be returned to the caller. Otherwise we
  268. * return -EPROBE_DEFER.
  269. */
  270. dev = driver_find_device_by_of_node(&exynos_pmu_driver.driver,
  271. (void *)pmu_np);
  272. if (propname)
  273. of_node_put(pmu_np);
  274. if (!dev)
  275. return ERR_PTR(-EPROBE_DEFER);
  276. return syscon_node_to_regmap(pmu_np);
  277. }
  278. EXPORT_SYMBOL_GPL(exynos_get_pmu_regmap_by_phandle);
  279. static int exynos_pmu_probe(struct platform_device *pdev)
  280. {
  281. struct device *dev = &pdev->dev;
  282. struct regmap_config pmu_regmcfg;
  283. struct regmap *regmap;
  284. struct resource *res;
  285. int ret;
  286. pmu_base_addr = devm_platform_ioremap_resource(pdev, 0);
  287. if (IS_ERR(pmu_base_addr))
  288. return PTR_ERR(pmu_base_addr);
  289. pmu_context = devm_kzalloc(&pdev->dev,
  290. sizeof(struct exynos_pmu_context),
  291. GFP_KERNEL);
  292. if (!pmu_context)
  293. return -ENOMEM;
  294. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  295. if (!res)
  296. return -ENODEV;
  297. pmu_context->pmu_data = of_device_get_match_data(dev);
  298. /* For SoCs that secure PMU register writes use custom regmap */
  299. if (pmu_context->pmu_data && pmu_context->pmu_data->pmu_secure) {
  300. pmu_regmcfg = regmap_smccfg;
  301. pmu_regmcfg.max_register = resource_size(res) -
  302. pmu_regmcfg.reg_stride;
  303. /* Need physical address for SMC call */
  304. regmap = devm_regmap_init(dev, NULL,
  305. (void *)(uintptr_t)res->start,
  306. &pmu_regmcfg);
  307. if (IS_ERR(regmap))
  308. return dev_err_probe(&pdev->dev, PTR_ERR(regmap),
  309. "regmap init failed\n");
  310. ret = of_syscon_register_regmap(dev->of_node, regmap);
  311. if (ret)
  312. return ret;
  313. } else {
  314. /* let syscon create mmio regmap */
  315. regmap = syscon_node_to_regmap(dev->of_node);
  316. if (IS_ERR(regmap))
  317. return dev_err_probe(&pdev->dev, PTR_ERR(regmap),
  318. "syscon_node_to_regmap failed\n");
  319. }
  320. pmu_context->pmureg = regmap;
  321. pmu_context->dev = dev;
  322. if (pmu_context->pmu_data && pmu_context->pmu_data->pmu_init)
  323. pmu_context->pmu_data->pmu_init();
  324. platform_set_drvdata(pdev, pmu_context);
  325. ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_NONE, exynos_pmu_devs,
  326. ARRAY_SIZE(exynos_pmu_devs), NULL, 0, NULL);
  327. if (ret)
  328. return ret;
  329. if (devm_of_platform_populate(dev))
  330. dev_err(dev, "Error populating children, reboot and poweroff might not work properly\n");
  331. dev_dbg(dev, "Exynos PMU Driver probe done\n");
  332. return 0;
  333. }
  334. static struct platform_driver exynos_pmu_driver = {
  335. .driver = {
  336. .name = "exynos-pmu",
  337. .of_match_table = exynos_pmu_of_device_ids,
  338. },
  339. .probe = exynos_pmu_probe,
  340. };
  341. static int __init exynos_pmu_init(void)
  342. {
  343. return platform_driver_register(&exynos_pmu_driver);
  344. }
  345. postcore_initcall(exynos_pmu_init);