ti-cpufreq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TI CPUFreq/OPP hw-supported driver
  4. *
  5. * Copyright (C) 2016-2017 Texas Instruments, Inc.
  6. * Dave Gerlach <[email protected]>
  7. */
  8. #include <linux/cpu.h>
  9. #include <linux/io.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/of.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/pm_opp.h>
  16. #include <linux/regmap.h>
  17. #include <linux/slab.h>
  18. #define REVISION_MASK 0xF
  19. #define REVISION_SHIFT 28
  20. #define AM33XX_800M_ARM_MPU_MAX_FREQ 0x1E2F
  21. #define AM43XX_600M_ARM_MPU_MAX_FREQ 0xFFA
  22. #define DRA7_EFUSE_HAS_OD_MPU_OPP 11
  23. #define DRA7_EFUSE_HAS_HIGH_MPU_OPP 15
  24. #define DRA76_EFUSE_HAS_PLUS_MPU_OPP 18
  25. #define DRA7_EFUSE_HAS_ALL_MPU_OPP 23
  26. #define DRA76_EFUSE_HAS_ALL_MPU_OPP 24
  27. #define DRA7_EFUSE_NOM_MPU_OPP BIT(0)
  28. #define DRA7_EFUSE_OD_MPU_OPP BIT(1)
  29. #define DRA7_EFUSE_HIGH_MPU_OPP BIT(2)
  30. #define DRA76_EFUSE_PLUS_MPU_OPP BIT(3)
  31. #define OMAP3_CONTROL_DEVICE_STATUS 0x4800244C
  32. #define OMAP3_CONTROL_IDCODE 0x4830A204
  33. #define OMAP34xx_ProdID_SKUID 0x4830A20C
  34. #define OMAP3_SYSCON_BASE (0x48000000 + 0x2000 + 0x270)
  35. #define VERSION_COUNT 2
  36. struct ti_cpufreq_data;
  37. struct ti_cpufreq_soc_data {
  38. const char * const *reg_names;
  39. unsigned long (*efuse_xlate)(struct ti_cpufreq_data *opp_data,
  40. unsigned long efuse);
  41. unsigned long efuse_fallback;
  42. unsigned long efuse_offset;
  43. unsigned long efuse_mask;
  44. unsigned long efuse_shift;
  45. unsigned long rev_offset;
  46. bool multi_regulator;
  47. };
  48. struct ti_cpufreq_data {
  49. struct device *cpu_dev;
  50. struct device_node *opp_node;
  51. struct regmap *syscon;
  52. const struct ti_cpufreq_soc_data *soc_data;
  53. };
  54. static unsigned long amx3_efuse_xlate(struct ti_cpufreq_data *opp_data,
  55. unsigned long efuse)
  56. {
  57. if (!efuse)
  58. efuse = opp_data->soc_data->efuse_fallback;
  59. /* AM335x and AM437x use "OPP disable" bits, so invert */
  60. return ~efuse;
  61. }
  62. static unsigned long dra7_efuse_xlate(struct ti_cpufreq_data *opp_data,
  63. unsigned long efuse)
  64. {
  65. unsigned long calculated_efuse = DRA7_EFUSE_NOM_MPU_OPP;
  66. /*
  67. * The efuse on dra7 and am57 parts contains a specific
  68. * value indicating the highest available OPP.
  69. */
  70. switch (efuse) {
  71. case DRA76_EFUSE_HAS_PLUS_MPU_OPP:
  72. case DRA76_EFUSE_HAS_ALL_MPU_OPP:
  73. calculated_efuse |= DRA76_EFUSE_PLUS_MPU_OPP;
  74. fallthrough;
  75. case DRA7_EFUSE_HAS_ALL_MPU_OPP:
  76. case DRA7_EFUSE_HAS_HIGH_MPU_OPP:
  77. calculated_efuse |= DRA7_EFUSE_HIGH_MPU_OPP;
  78. fallthrough;
  79. case DRA7_EFUSE_HAS_OD_MPU_OPP:
  80. calculated_efuse |= DRA7_EFUSE_OD_MPU_OPP;
  81. }
  82. return calculated_efuse;
  83. }
  84. static unsigned long omap3_efuse_xlate(struct ti_cpufreq_data *opp_data,
  85. unsigned long efuse)
  86. {
  87. /* OPP enable bit ("Speed Binned") */
  88. return BIT(efuse);
  89. }
  90. static struct ti_cpufreq_soc_data am3x_soc_data = {
  91. .efuse_xlate = amx3_efuse_xlate,
  92. .efuse_fallback = AM33XX_800M_ARM_MPU_MAX_FREQ,
  93. .efuse_offset = 0x07fc,
  94. .efuse_mask = 0x1fff,
  95. .rev_offset = 0x600,
  96. .multi_regulator = false,
  97. };
  98. static struct ti_cpufreq_soc_data am4x_soc_data = {
  99. .efuse_xlate = amx3_efuse_xlate,
  100. .efuse_fallback = AM43XX_600M_ARM_MPU_MAX_FREQ,
  101. .efuse_offset = 0x0610,
  102. .efuse_mask = 0x3f,
  103. .rev_offset = 0x600,
  104. .multi_regulator = false,
  105. };
  106. static struct ti_cpufreq_soc_data dra7_soc_data = {
  107. .efuse_xlate = dra7_efuse_xlate,
  108. .efuse_offset = 0x020c,
  109. .efuse_mask = 0xf80000,
  110. .efuse_shift = 19,
  111. .rev_offset = 0x204,
  112. .multi_regulator = true,
  113. };
  114. /*
  115. * OMAP35x TRM (SPRUF98K):
  116. * CONTROL_IDCODE (0x4830 A204) describes Silicon revisions.
  117. * Control OMAP Status Register 15:0 (Address 0x4800 244C)
  118. * to separate between omap3503, omap3515, omap3525, omap3530
  119. * and feature presence.
  120. * There are encodings for versions limited to 400/266MHz
  121. * but we ignore.
  122. * Not clear if this also holds for omap34xx.
  123. * some eFuse values e.g. CONTROL_FUSE_OPP1_VDD1
  124. * are stored in the SYSCON register range
  125. * Register 0x4830A20C [ProdID.SKUID] [0:3]
  126. * 0x0 for normal 600/430MHz device.
  127. * 0x8 for 720/520MHz device.
  128. * Not clear what omap34xx value is.
  129. */
  130. static struct ti_cpufreq_soc_data omap34xx_soc_data = {
  131. .efuse_xlate = omap3_efuse_xlate,
  132. .efuse_offset = OMAP34xx_ProdID_SKUID - OMAP3_SYSCON_BASE,
  133. .efuse_shift = 3,
  134. .efuse_mask = BIT(3),
  135. .rev_offset = OMAP3_CONTROL_IDCODE - OMAP3_SYSCON_BASE,
  136. .multi_regulator = false,
  137. };
  138. /*
  139. * AM/DM37x TRM (SPRUGN4M)
  140. * CONTROL_IDCODE (0x4830 A204) describes Silicon revisions.
  141. * Control Device Status Register 15:0 (Address 0x4800 244C)
  142. * to separate between am3703, am3715, dm3725, dm3730
  143. * and feature presence.
  144. * Speed Binned = Bit 9
  145. * 0 800/600 MHz
  146. * 1 1000/800 MHz
  147. * some eFuse values e.g. CONTROL_FUSE_OPP 1G_VDD1
  148. * are stored in the SYSCON register range.
  149. * There is no 0x4830A20C [ProdID.SKUID] register (exists but
  150. * seems to always read as 0).
  151. */
  152. static const char * const omap3_reg_names[] = {"cpu0", "vbb", NULL};
  153. static struct ti_cpufreq_soc_data omap36xx_soc_data = {
  154. .reg_names = omap3_reg_names,
  155. .efuse_xlate = omap3_efuse_xlate,
  156. .efuse_offset = OMAP3_CONTROL_DEVICE_STATUS - OMAP3_SYSCON_BASE,
  157. .efuse_shift = 9,
  158. .efuse_mask = BIT(9),
  159. .rev_offset = OMAP3_CONTROL_IDCODE - OMAP3_SYSCON_BASE,
  160. .multi_regulator = true,
  161. };
  162. /*
  163. * AM3517 is quite similar to AM/DM37x except that it has no
  164. * high speed grade eFuse and no abb ldo
  165. */
  166. static struct ti_cpufreq_soc_data am3517_soc_data = {
  167. .efuse_xlate = omap3_efuse_xlate,
  168. .efuse_offset = OMAP3_CONTROL_DEVICE_STATUS - OMAP3_SYSCON_BASE,
  169. .efuse_shift = 0,
  170. .efuse_mask = 0,
  171. .rev_offset = OMAP3_CONTROL_IDCODE - OMAP3_SYSCON_BASE,
  172. .multi_regulator = false,
  173. };
  174. /**
  175. * ti_cpufreq_get_efuse() - Parse and return efuse value present on SoC
  176. * @opp_data: pointer to ti_cpufreq_data context
  177. * @efuse_value: Set to the value parsed from efuse
  178. *
  179. * Returns error code if efuse not read properly.
  180. */
  181. static int ti_cpufreq_get_efuse(struct ti_cpufreq_data *opp_data,
  182. u32 *efuse_value)
  183. {
  184. struct device *dev = opp_data->cpu_dev;
  185. u32 efuse;
  186. int ret;
  187. ret = regmap_read(opp_data->syscon, opp_data->soc_data->efuse_offset,
  188. &efuse);
  189. if (ret == -EIO) {
  190. /* not a syscon register! */
  191. void __iomem *regs = ioremap(OMAP3_SYSCON_BASE +
  192. opp_data->soc_data->efuse_offset, 4);
  193. if (!regs)
  194. return -ENOMEM;
  195. efuse = readl(regs);
  196. iounmap(regs);
  197. }
  198. else if (ret) {
  199. dev_err(dev,
  200. "Failed to read the efuse value from syscon: %d\n",
  201. ret);
  202. return ret;
  203. }
  204. efuse = (efuse & opp_data->soc_data->efuse_mask);
  205. efuse >>= opp_data->soc_data->efuse_shift;
  206. *efuse_value = opp_data->soc_data->efuse_xlate(opp_data, efuse);
  207. return 0;
  208. }
  209. /**
  210. * ti_cpufreq_get_rev() - Parse and return rev value present on SoC
  211. * @opp_data: pointer to ti_cpufreq_data context
  212. * @revision_value: Set to the value parsed from revision register
  213. *
  214. * Returns error code if revision not read properly.
  215. */
  216. static int ti_cpufreq_get_rev(struct ti_cpufreq_data *opp_data,
  217. u32 *revision_value)
  218. {
  219. struct device *dev = opp_data->cpu_dev;
  220. u32 revision;
  221. int ret;
  222. ret = regmap_read(opp_data->syscon, opp_data->soc_data->rev_offset,
  223. &revision);
  224. if (ret == -EIO) {
  225. /* not a syscon register! */
  226. void __iomem *regs = ioremap(OMAP3_SYSCON_BASE +
  227. opp_data->soc_data->rev_offset, 4);
  228. if (!regs)
  229. return -ENOMEM;
  230. revision = readl(regs);
  231. iounmap(regs);
  232. }
  233. else if (ret) {
  234. dev_err(dev,
  235. "Failed to read the revision number from syscon: %d\n",
  236. ret);
  237. return ret;
  238. }
  239. *revision_value = BIT((revision >> REVISION_SHIFT) & REVISION_MASK);
  240. return 0;
  241. }
  242. static int ti_cpufreq_setup_syscon_register(struct ti_cpufreq_data *opp_data)
  243. {
  244. struct device *dev = opp_data->cpu_dev;
  245. struct device_node *np = opp_data->opp_node;
  246. opp_data->syscon = syscon_regmap_lookup_by_phandle(np,
  247. "syscon");
  248. if (IS_ERR(opp_data->syscon)) {
  249. dev_err(dev,
  250. "\"syscon\" is missing, cannot use OPPv2 table.\n");
  251. return PTR_ERR(opp_data->syscon);
  252. }
  253. return 0;
  254. }
  255. static const struct of_device_id ti_cpufreq_of_match[] = {
  256. { .compatible = "ti,am33xx", .data = &am3x_soc_data, },
  257. { .compatible = "ti,am3517", .data = &am3517_soc_data, },
  258. { .compatible = "ti,am43", .data = &am4x_soc_data, },
  259. { .compatible = "ti,dra7", .data = &dra7_soc_data },
  260. { .compatible = "ti,omap34xx", .data = &omap34xx_soc_data, },
  261. { .compatible = "ti,omap36xx", .data = &omap36xx_soc_data, },
  262. /* legacy */
  263. { .compatible = "ti,omap3430", .data = &omap34xx_soc_data, },
  264. { .compatible = "ti,omap3630", .data = &omap36xx_soc_data, },
  265. {},
  266. };
  267. static const struct of_device_id *ti_cpufreq_match_node(void)
  268. {
  269. struct device_node *np;
  270. const struct of_device_id *match;
  271. np = of_find_node_by_path("/");
  272. match = of_match_node(ti_cpufreq_of_match, np);
  273. of_node_put(np);
  274. return match;
  275. }
  276. static int ti_cpufreq_probe(struct platform_device *pdev)
  277. {
  278. u32 version[VERSION_COUNT];
  279. const struct of_device_id *match;
  280. struct ti_cpufreq_data *opp_data;
  281. const char * const default_reg_names[] = {"vdd", "vbb", NULL};
  282. int ret;
  283. struct dev_pm_opp_config config = {
  284. .supported_hw = version,
  285. .supported_hw_count = ARRAY_SIZE(version),
  286. };
  287. match = dev_get_platdata(&pdev->dev);
  288. if (!match)
  289. return -ENODEV;
  290. opp_data = devm_kzalloc(&pdev->dev, sizeof(*opp_data), GFP_KERNEL);
  291. if (!opp_data)
  292. return -ENOMEM;
  293. opp_data->soc_data = match->data;
  294. opp_data->cpu_dev = get_cpu_device(0);
  295. if (!opp_data->cpu_dev) {
  296. pr_err("%s: Failed to get device for CPU0\n", __func__);
  297. return -ENODEV;
  298. }
  299. opp_data->opp_node = dev_pm_opp_of_get_opp_desc_node(opp_data->cpu_dev);
  300. if (!opp_data->opp_node) {
  301. dev_info(opp_data->cpu_dev,
  302. "OPP-v2 not supported, cpufreq-dt will attempt to use legacy tables.\n");
  303. goto register_cpufreq_dt;
  304. }
  305. ret = ti_cpufreq_setup_syscon_register(opp_data);
  306. if (ret)
  307. goto fail_put_node;
  308. /*
  309. * OPPs determine whether or not they are supported based on
  310. * two metrics:
  311. * 0 - SoC Revision
  312. * 1 - eFuse value
  313. */
  314. ret = ti_cpufreq_get_rev(opp_data, &version[0]);
  315. if (ret)
  316. goto fail_put_node;
  317. ret = ti_cpufreq_get_efuse(opp_data, &version[1]);
  318. if (ret)
  319. goto fail_put_node;
  320. if (opp_data->soc_data->multi_regulator) {
  321. if (opp_data->soc_data->reg_names)
  322. config.regulator_names = opp_data->soc_data->reg_names;
  323. else
  324. config.regulator_names = default_reg_names;
  325. }
  326. ret = dev_pm_opp_set_config(opp_data->cpu_dev, &config);
  327. if (ret < 0) {
  328. dev_err(opp_data->cpu_dev, "Failed to set OPP config\n");
  329. goto fail_put_node;
  330. }
  331. of_node_put(opp_data->opp_node);
  332. register_cpufreq_dt:
  333. platform_device_register_simple("cpufreq-dt", -1, NULL, 0);
  334. return 0;
  335. fail_put_node:
  336. of_node_put(opp_data->opp_node);
  337. return ret;
  338. }
  339. static int __init ti_cpufreq_init(void)
  340. {
  341. const struct of_device_id *match;
  342. /* Check to ensure we are on a compatible platform */
  343. match = ti_cpufreq_match_node();
  344. if (match)
  345. platform_device_register_data(NULL, "ti-cpufreq", -1, match,
  346. sizeof(*match));
  347. return 0;
  348. }
  349. module_init(ti_cpufreq_init);
  350. static struct platform_driver ti_cpufreq_driver = {
  351. .probe = ti_cpufreq_probe,
  352. .driver = {
  353. .name = "ti-cpufreq",
  354. },
  355. };
  356. builtin_platform_driver(ti_cpufreq_driver);
  357. MODULE_DESCRIPTION("TI CPUFreq/OPP hw-supported driver");
  358. MODULE_AUTHOR("Dave Gerlach <[email protected]>");
  359. MODULE_LICENSE("GPL v2");