tegra186-cpufreq.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. */
  13. #include <linux/cpufreq.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <soc/tegra/bpmp.h>
  19. #include <soc/tegra/bpmp-abi.h>
  20. #define EDVD_CORE_VOLT_FREQ(core) (0x20 + (core) * 0x4)
  21. #define EDVD_CORE_VOLT_FREQ_F_SHIFT 0
  22. #define EDVD_CORE_VOLT_FREQ_V_SHIFT 16
  23. struct tegra186_cpufreq_cluster_info {
  24. unsigned long offset;
  25. int cpus[4];
  26. unsigned int bpmp_cluster_id;
  27. };
  28. #define NO_CPU -1
  29. static const struct tegra186_cpufreq_cluster_info tegra186_clusters[] = {
  30. /* Denver cluster */
  31. {
  32. .offset = SZ_64K * 7,
  33. .cpus = { 1, 2, NO_CPU, NO_CPU },
  34. .bpmp_cluster_id = 0,
  35. },
  36. /* A57 cluster */
  37. {
  38. .offset = SZ_64K * 6,
  39. .cpus = { 0, 3, 4, 5 },
  40. .bpmp_cluster_id = 1,
  41. },
  42. };
  43. struct tegra186_cpufreq_cluster {
  44. const struct tegra186_cpufreq_cluster_info *info;
  45. struct cpufreq_frequency_table *table;
  46. };
  47. struct tegra186_cpufreq_data {
  48. void __iomem *regs;
  49. size_t num_clusters;
  50. struct tegra186_cpufreq_cluster *clusters;
  51. };
  52. static int tegra186_cpufreq_init(struct cpufreq_policy *policy)
  53. {
  54. struct tegra186_cpufreq_data *data = cpufreq_get_driver_data();
  55. unsigned int i;
  56. for (i = 0; i < data->num_clusters; i++) {
  57. struct tegra186_cpufreq_cluster *cluster = &data->clusters[i];
  58. const struct tegra186_cpufreq_cluster_info *info =
  59. cluster->info;
  60. int core;
  61. for (core = 0; core < ARRAY_SIZE(info->cpus); core++) {
  62. if (info->cpus[core] == policy->cpu)
  63. break;
  64. }
  65. if (core == ARRAY_SIZE(info->cpus))
  66. continue;
  67. policy->driver_data =
  68. data->regs + info->offset + EDVD_CORE_VOLT_FREQ(core);
  69. cpufreq_table_validate_and_show(policy, cluster->table);
  70. }
  71. policy->cpuinfo.transition_latency = 300 * 1000;
  72. return 0;
  73. }
  74. static int tegra186_cpufreq_set_target(struct cpufreq_policy *policy,
  75. unsigned int index)
  76. {
  77. struct cpufreq_frequency_table *tbl = policy->freq_table + index;
  78. void __iomem *edvd_reg = policy->driver_data;
  79. u32 edvd_val = tbl->driver_data;
  80. writel(edvd_val, edvd_reg);
  81. return 0;
  82. }
  83. static struct cpufreq_driver tegra186_cpufreq_driver = {
  84. .name = "tegra186",
  85. .flags = CPUFREQ_STICKY | CPUFREQ_HAVE_GOVERNOR_PER_POLICY,
  86. .verify = cpufreq_generic_frequency_table_verify,
  87. .target_index = tegra186_cpufreq_set_target,
  88. .init = tegra186_cpufreq_init,
  89. .attr = cpufreq_generic_attr,
  90. };
  91. static struct cpufreq_frequency_table *init_vhint_table(
  92. struct platform_device *pdev, struct tegra_bpmp *bpmp,
  93. unsigned int cluster_id)
  94. {
  95. struct cpufreq_frequency_table *table;
  96. struct mrq_cpu_vhint_request req;
  97. struct tegra_bpmp_message msg;
  98. struct cpu_vhint_data *data;
  99. int err, i, j, num_rates = 0;
  100. dma_addr_t phys;
  101. void *virt;
  102. virt = dma_alloc_coherent(bpmp->dev, sizeof(*data), &phys,
  103. GFP_KERNEL | GFP_DMA32);
  104. if (!virt)
  105. return ERR_PTR(-ENOMEM);
  106. data = (struct cpu_vhint_data *)virt;
  107. memset(&req, 0, sizeof(req));
  108. req.addr = phys;
  109. req.cluster_id = cluster_id;
  110. memset(&msg, 0, sizeof(msg));
  111. msg.mrq = MRQ_CPU_VHINT;
  112. msg.tx.data = &req;
  113. msg.tx.size = sizeof(req);
  114. err = tegra_bpmp_transfer(bpmp, &msg);
  115. if (err) {
  116. table = ERR_PTR(err);
  117. goto free;
  118. }
  119. for (i = data->vfloor; i <= data->vceil; i++) {
  120. u16 ndiv = data->ndiv[i];
  121. if (ndiv < data->ndiv_min || ndiv > data->ndiv_max)
  122. continue;
  123. /* Only store lowest voltage index for each rate */
  124. if (i > 0 && ndiv == data->ndiv[i - 1])
  125. continue;
  126. num_rates++;
  127. }
  128. table = devm_kcalloc(&pdev->dev, num_rates + 1, sizeof(*table),
  129. GFP_KERNEL);
  130. if (!table) {
  131. table = ERR_PTR(-ENOMEM);
  132. goto free;
  133. }
  134. for (i = data->vfloor, j = 0; i <= data->vceil; i++) {
  135. struct cpufreq_frequency_table *point;
  136. u16 ndiv = data->ndiv[i];
  137. u32 edvd_val = 0;
  138. if (ndiv < data->ndiv_min || ndiv > data->ndiv_max)
  139. continue;
  140. /* Only store lowest voltage index for each rate */
  141. if (i > 0 && ndiv == data->ndiv[i - 1])
  142. continue;
  143. edvd_val |= i << EDVD_CORE_VOLT_FREQ_V_SHIFT;
  144. edvd_val |= ndiv << EDVD_CORE_VOLT_FREQ_F_SHIFT;
  145. point = &table[j++];
  146. point->driver_data = edvd_val;
  147. point->frequency = data->ref_clk_hz * ndiv / data->pdiv /
  148. data->mdiv / 1000;
  149. }
  150. table[j].frequency = CPUFREQ_TABLE_END;
  151. free:
  152. dma_free_coherent(bpmp->dev, sizeof(*data), virt, phys);
  153. return table;
  154. }
  155. static int tegra186_cpufreq_probe(struct platform_device *pdev)
  156. {
  157. struct tegra186_cpufreq_data *data;
  158. struct tegra_bpmp *bpmp;
  159. struct resource *res;
  160. unsigned int i = 0, err;
  161. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  162. if (!data)
  163. return -ENOMEM;
  164. data->clusters = devm_kcalloc(&pdev->dev, ARRAY_SIZE(tegra186_clusters),
  165. sizeof(*data->clusters), GFP_KERNEL);
  166. if (!data->clusters)
  167. return -ENOMEM;
  168. data->num_clusters = ARRAY_SIZE(tegra186_clusters);
  169. bpmp = tegra_bpmp_get(&pdev->dev);
  170. if (IS_ERR(bpmp))
  171. return PTR_ERR(bpmp);
  172. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  173. data->regs = devm_ioremap_resource(&pdev->dev, res);
  174. if (IS_ERR(data->regs)) {
  175. err = PTR_ERR(data->regs);
  176. goto put_bpmp;
  177. }
  178. for (i = 0; i < data->num_clusters; i++) {
  179. struct tegra186_cpufreq_cluster *cluster = &data->clusters[i];
  180. cluster->info = &tegra186_clusters[i];
  181. cluster->table = init_vhint_table(
  182. pdev, bpmp, cluster->info->bpmp_cluster_id);
  183. if (IS_ERR(cluster->table)) {
  184. err = PTR_ERR(cluster->table);
  185. goto put_bpmp;
  186. }
  187. }
  188. tegra_bpmp_put(bpmp);
  189. tegra186_cpufreq_driver.driver_data = data;
  190. err = cpufreq_register_driver(&tegra186_cpufreq_driver);
  191. if (err)
  192. return err;
  193. return 0;
  194. put_bpmp:
  195. tegra_bpmp_put(bpmp);
  196. return err;
  197. }
  198. static int tegra186_cpufreq_remove(struct platform_device *pdev)
  199. {
  200. cpufreq_unregister_driver(&tegra186_cpufreq_driver);
  201. return 0;
  202. }
  203. static const struct of_device_id tegra186_cpufreq_of_match[] = {
  204. { .compatible = "nvidia,tegra186-ccplex-cluster", },
  205. { }
  206. };
  207. MODULE_DEVICE_TABLE(of, tegra186_cpufreq_of_match);
  208. static struct platform_driver tegra186_cpufreq_platform_driver = {
  209. .driver = {
  210. .name = "tegra186-cpufreq",
  211. .of_match_table = tegra186_cpufreq_of_match,
  212. },
  213. .probe = tegra186_cpufreq_probe,
  214. .remove = tegra186_cpufreq_remove,
  215. };
  216. module_platform_driver(tegra186_cpufreq_platform_driver);
  217. MODULE_AUTHOR("Mikko Perttunen <mperttunen@nvidia.com>");
  218. MODULE_DESCRIPTION("NVIDIA Tegra186 cpufreq driver");
  219. MODULE_LICENSE("GPL v2");