tegra186-cpufreq.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. policy->freq_table = cluster->table;
  70. break;
  71. }
  72. policy->cpuinfo.transition_latency = 300 * 1000;
  73. return 0;
  74. }
  75. static int tegra186_cpufreq_set_target(struct cpufreq_policy *policy,
  76. unsigned int index)
  77. {
  78. struct cpufreq_frequency_table *tbl = policy->freq_table + index;
  79. void __iomem *edvd_reg = policy->driver_data;
  80. u32 edvd_val = tbl->driver_data;
  81. writel(edvd_val, edvd_reg);
  82. return 0;
  83. }
  84. static struct cpufreq_driver tegra186_cpufreq_driver = {
  85. .name = "tegra186",
  86. .flags = CPUFREQ_STICKY | CPUFREQ_HAVE_GOVERNOR_PER_POLICY,
  87. .verify = cpufreq_generic_frequency_table_verify,
  88. .target_index = tegra186_cpufreq_set_target,
  89. .init = tegra186_cpufreq_init,
  90. .attr = cpufreq_generic_attr,
  91. };
  92. static struct cpufreq_frequency_table *init_vhint_table(
  93. struct platform_device *pdev, struct tegra_bpmp *bpmp,
  94. unsigned int cluster_id)
  95. {
  96. struct cpufreq_frequency_table *table;
  97. struct mrq_cpu_vhint_request req;
  98. struct tegra_bpmp_message msg;
  99. struct cpu_vhint_data *data;
  100. int err, i, j, num_rates = 0;
  101. dma_addr_t phys;
  102. void *virt;
  103. virt = dma_alloc_coherent(bpmp->dev, sizeof(*data), &phys,
  104. GFP_KERNEL | GFP_DMA32);
  105. if (!virt)
  106. return ERR_PTR(-ENOMEM);
  107. data = (struct cpu_vhint_data *)virt;
  108. memset(&req, 0, sizeof(req));
  109. req.addr = phys;
  110. req.cluster_id = cluster_id;
  111. memset(&msg, 0, sizeof(msg));
  112. msg.mrq = MRQ_CPU_VHINT;
  113. msg.tx.data = &req;
  114. msg.tx.size = sizeof(req);
  115. err = tegra_bpmp_transfer(bpmp, &msg);
  116. if (err) {
  117. table = ERR_PTR(err);
  118. goto free;
  119. }
  120. for (i = data->vfloor; i <= data->vceil; i++) {
  121. u16 ndiv = data->ndiv[i];
  122. if (ndiv < data->ndiv_min || ndiv > data->ndiv_max)
  123. continue;
  124. /* Only store lowest voltage index for each rate */
  125. if (i > 0 && ndiv == data->ndiv[i - 1])
  126. continue;
  127. num_rates++;
  128. }
  129. table = devm_kcalloc(&pdev->dev, num_rates + 1, sizeof(*table),
  130. GFP_KERNEL);
  131. if (!table) {
  132. table = ERR_PTR(-ENOMEM);
  133. goto free;
  134. }
  135. for (i = data->vfloor, j = 0; i <= data->vceil; i++) {
  136. struct cpufreq_frequency_table *point;
  137. u16 ndiv = data->ndiv[i];
  138. u32 edvd_val = 0;
  139. if (ndiv < data->ndiv_min || ndiv > data->ndiv_max)
  140. continue;
  141. /* Only store lowest voltage index for each rate */
  142. if (i > 0 && ndiv == data->ndiv[i - 1])
  143. continue;
  144. edvd_val |= i << EDVD_CORE_VOLT_FREQ_V_SHIFT;
  145. edvd_val |= ndiv << EDVD_CORE_VOLT_FREQ_F_SHIFT;
  146. point = &table[j++];
  147. point->driver_data = edvd_val;
  148. point->frequency = data->ref_clk_hz * ndiv / data->pdiv /
  149. data->mdiv / 1000;
  150. }
  151. table[j].frequency = CPUFREQ_TABLE_END;
  152. free:
  153. dma_free_coherent(bpmp->dev, sizeof(*data), virt, phys);
  154. return table;
  155. }
  156. static int tegra186_cpufreq_probe(struct platform_device *pdev)
  157. {
  158. struct tegra186_cpufreq_data *data;
  159. struct tegra_bpmp *bpmp;
  160. struct resource *res;
  161. unsigned int i = 0, err;
  162. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  163. if (!data)
  164. return -ENOMEM;
  165. data->clusters = devm_kcalloc(&pdev->dev, ARRAY_SIZE(tegra186_clusters),
  166. sizeof(*data->clusters), GFP_KERNEL);
  167. if (!data->clusters)
  168. return -ENOMEM;
  169. data->num_clusters = ARRAY_SIZE(tegra186_clusters);
  170. bpmp = tegra_bpmp_get(&pdev->dev);
  171. if (IS_ERR(bpmp))
  172. return PTR_ERR(bpmp);
  173. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  174. data->regs = devm_ioremap_resource(&pdev->dev, res);
  175. if (IS_ERR(data->regs)) {
  176. err = PTR_ERR(data->regs);
  177. goto put_bpmp;
  178. }
  179. for (i = 0; i < data->num_clusters; i++) {
  180. struct tegra186_cpufreq_cluster *cluster = &data->clusters[i];
  181. cluster->info = &tegra186_clusters[i];
  182. cluster->table = init_vhint_table(
  183. pdev, bpmp, cluster->info->bpmp_cluster_id);
  184. if (IS_ERR(cluster->table)) {
  185. err = PTR_ERR(cluster->table);
  186. goto put_bpmp;
  187. }
  188. }
  189. tegra_bpmp_put(bpmp);
  190. tegra186_cpufreq_driver.driver_data = data;
  191. err = cpufreq_register_driver(&tegra186_cpufreq_driver);
  192. if (err)
  193. return err;
  194. return 0;
  195. put_bpmp:
  196. tegra_bpmp_put(bpmp);
  197. return err;
  198. }
  199. static int tegra186_cpufreq_remove(struct platform_device *pdev)
  200. {
  201. cpufreq_unregister_driver(&tegra186_cpufreq_driver);
  202. return 0;
  203. }
  204. static const struct of_device_id tegra186_cpufreq_of_match[] = {
  205. { .compatible = "nvidia,tegra186-ccplex-cluster", },
  206. { }
  207. };
  208. MODULE_DEVICE_TABLE(of, tegra186_cpufreq_of_match);
  209. static struct platform_driver tegra186_cpufreq_platform_driver = {
  210. .driver = {
  211. .name = "tegra186-cpufreq",
  212. .of_match_table = tegra186_cpufreq_of_match,
  213. },
  214. .probe = tegra186_cpufreq_probe,
  215. .remove = tegra186_cpufreq_remove,
  216. };
  217. module_platform_driver(tegra186_cpufreq_platform_driver);
  218. MODULE_AUTHOR("Mikko Perttunen <mperttunen@nvidia.com>");
  219. MODULE_DESCRIPTION("NVIDIA Tegra186 cpufreq driver");
  220. MODULE_LICENSE("GPL v2");