k3_avs.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Texas Instruments' K3 Clas 0 Adaptive Voltage Scaling driver
  4. *
  5. * Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/
  6. * Tero Kristo <t-kristo@ti.com>
  7. *
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <errno.h>
  12. #include <asm/io.h>
  13. #include <i2c.h>
  14. #include <k3-avs.h>
  15. #include <dm/device_compat.h>
  16. #include <power/regulator.h>
  17. #define AM6_VTM_DEVINFO(i) (priv->base + 0x100 + 0x20 * (i))
  18. #define AM6_VTM_OPPVID_VD(i) (priv->base + 0x104 + 0x20 * (i))
  19. #define AM6_VTM_AVS0_SUPPORTED BIT(12)
  20. #define AM6_VTM_OPP_SHIFT(opp) (8 * (opp))
  21. #define AM6_VTM_OPP_MASK 0xff
  22. #define VD_FLAG_INIT_DONE BIT(0)
  23. struct k3_avs_privdata {
  24. void *base;
  25. struct vd_config *vd_config;
  26. };
  27. struct opp {
  28. u32 freq;
  29. u32 volt;
  30. };
  31. struct vd_data {
  32. int id;
  33. u8 opp;
  34. u8 flags;
  35. int dev_id;
  36. int clk_id;
  37. struct opp opps[NUM_OPPS];
  38. struct udevice *supply;
  39. };
  40. struct vd_config {
  41. struct vd_data *vds;
  42. u32 (*efuse_xlate)(struct k3_avs_privdata *priv, int idx, int opp);
  43. };
  44. static struct k3_avs_privdata *k3_avs_priv;
  45. /**
  46. * am6_efuse_voltage: read efuse voltage from VTM
  47. * @priv: driver private data
  48. * @idx: VD to read efuse for
  49. * @opp: opp id to read
  50. *
  51. * Reads efuse value for the specified OPP, and converts the register
  52. * value to a voltage. Returns the voltage in uV, or 0 if nominal voltage
  53. * should be used.
  54. *
  55. * Efuse val to volt conversion logic:
  56. *
  57. * val > 171 volt increments in 20mV steps with base 171 => 1.66V
  58. * val between 115 to 11 increments in 10mV steps with base 115 => 1.1V
  59. * val between 15 to 115 increments in 5mV steps with base 15 => .6V
  60. * val between 1 to 15 increments in 20mv steps with base 0 => .3V
  61. * val 0 is invalid
  62. */
  63. static u32 am6_efuse_xlate(struct k3_avs_privdata *priv, int idx, int opp)
  64. {
  65. u32 val = readl(AM6_VTM_OPPVID_VD(idx));
  66. val >>= AM6_VTM_OPP_SHIFT(opp);
  67. val &= AM6_VTM_OPP_MASK;
  68. if (!val)
  69. return 0;
  70. if (val > 171)
  71. return 1660000 + 20000 * (val - 171);
  72. if (val > 115)
  73. return 1100000 + 10000 * (val - 115);
  74. if (val > 15)
  75. return 600000 + 5000 * (val - 15);
  76. return 300000 + 20000 * val;
  77. }
  78. static int k3_avs_program_voltage(struct k3_avs_privdata *priv,
  79. struct vd_data *vd,
  80. int opp_id)
  81. {
  82. u32 volt = vd->opps[opp_id].volt;
  83. struct vd_data *vd2;
  84. if (!vd->supply)
  85. return -ENODEV;
  86. vd->opp = opp_id;
  87. vd->flags |= VD_FLAG_INIT_DONE;
  88. /* Take care of ganged rails and pick the Max amongst them*/
  89. for (vd2 = priv->vd_config->vds; vd2->id >= 0; vd2++) {
  90. if (vd == vd2)
  91. continue;
  92. if (vd2->supply != vd->supply)
  93. continue;
  94. if (vd2->opps[vd2->opp].volt > volt)
  95. volt = vd2->opps[vd2->opp].volt;
  96. vd2->flags |= VD_FLAG_INIT_DONE;
  97. }
  98. return regulator_set_value(vd->supply, volt);
  99. }
  100. static struct vd_data *get_vd(struct k3_avs_privdata *priv, int idx)
  101. {
  102. struct vd_data *vd;
  103. for (vd = priv->vd_config->vds; vd->id >= 0 && vd->id != idx; vd++)
  104. ;
  105. if (vd->id < 0)
  106. return NULL;
  107. return vd;
  108. }
  109. /**
  110. * k3_avs_set_opp: Sets the voltage for an arbitrary VD rail
  111. * @dev: AVS device
  112. * @vdd_id: voltage domain ID
  113. * @opp_id: OPP ID
  114. *
  115. * Programs the desired OPP value for the defined voltage rail. This
  116. * should be called from board files if reconfiguration is desired.
  117. * Returns 0 on success, negative error value on failure.
  118. */
  119. int k3_avs_set_opp(struct udevice *dev, int vdd_id, int opp_id)
  120. {
  121. struct k3_avs_privdata *priv = dev_get_priv(dev);
  122. struct vd_data *vd;
  123. vd = get_vd(priv, vdd_id);
  124. if (!vd)
  125. return -EINVAL;
  126. return k3_avs_program_voltage(priv, vd, opp_id);
  127. }
  128. static int match_opp(struct vd_data *vd, u32 freq)
  129. {
  130. struct opp *opp;
  131. int opp_id;
  132. for (opp_id = 0; opp_id < NUM_OPPS; opp_id++) {
  133. opp = &vd->opps[opp_id];
  134. if (opp->freq == freq)
  135. return opp_id;
  136. }
  137. printf("No matching OPP found for freq %d.\n", freq);
  138. return -EINVAL;
  139. }
  140. /**
  141. * k3_avs_notify_freq: Notify clock rate change towards AVS subsystem
  142. * @dev_id: Device ID for the clock to be changed
  143. * @clk_id: Clock ID for the clock to be changed
  144. * @freq: New frequency for clock
  145. *
  146. * Checks if the provided clock is the MPU clock or not, if not, return
  147. * immediately. If MPU clock is provided, maps the provided MPU frequency
  148. * towards an MPU OPP, and programs the voltage to the regulator. Return 0
  149. * on success, negative error value on failure.
  150. */
  151. int k3_avs_notify_freq(int dev_id, int clk_id, u32 freq)
  152. {
  153. int opp_id;
  154. struct k3_avs_privdata *priv = k3_avs_priv;
  155. struct vd_data *vd;
  156. /* Driver may not be probed yet */
  157. if (!priv)
  158. return -EINVAL;
  159. for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
  160. if (vd->dev_id != dev_id || vd->clk_id != clk_id)
  161. continue;
  162. opp_id = match_opp(vd, freq);
  163. if (opp_id < 0)
  164. return opp_id;
  165. vd->opp = opp_id;
  166. return k3_avs_program_voltage(priv, vd, opp_id);
  167. }
  168. return -EINVAL;
  169. }
  170. static int k3_avs_configure(struct udevice *dev, struct k3_avs_privdata *priv)
  171. {
  172. struct vd_config *conf;
  173. int ret;
  174. char pname[20];
  175. struct vd_data *vd;
  176. conf = (void *)dev_get_driver_data(dev);
  177. priv->vd_config = conf;
  178. for (vd = conf->vds; vd->id >= 0; vd++) {
  179. sprintf(pname, "vdd-supply-%d", vd->id);
  180. ret = device_get_supply_regulator(dev, pname, &vd->supply);
  181. if (ret)
  182. dev_warn(dev, "supply not found for VD%d.\n", vd->id);
  183. sprintf(pname, "ti,default-opp-%d", vd->id);
  184. ret = dev_read_u32_default(dev, pname, -1);
  185. if (ret != -1)
  186. vd->opp = ret;
  187. }
  188. return 0;
  189. }
  190. /**
  191. * k3_avs_probe: parses VD info from VTM, and re-configures the OPP data
  192. *
  193. * Parses all VDs on a device calculating the AVS class-0 voltages for them,
  194. * and updates the vd_data based on this. The vd_data itself shall be used
  195. * to program the required OPPs later on. Returns 0 on success, negative
  196. * error value on failure.
  197. */
  198. static int k3_avs_probe(struct udevice *dev)
  199. {
  200. int opp_id;
  201. u32 volt;
  202. struct opp *opp;
  203. struct k3_avs_privdata *priv;
  204. struct vd_data *vd;
  205. int ret;
  206. priv = dev_get_priv(dev);
  207. k3_avs_priv = priv;
  208. ret = k3_avs_configure(dev, priv);
  209. if (ret)
  210. return ret;
  211. priv->base = dev_read_addr_ptr(dev);
  212. if (!priv->base)
  213. return -ENODEV;
  214. for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
  215. if (!(readl(AM6_VTM_DEVINFO(vd->id)) &
  216. AM6_VTM_AVS0_SUPPORTED)) {
  217. dev_warn(dev, "AVS-class 0 not supported for VD%d\n",
  218. vd->id);
  219. continue;
  220. }
  221. for (opp_id = 0; opp_id < NUM_OPPS; opp_id++) {
  222. opp = &vd->opps[opp_id];
  223. if (!opp->freq)
  224. continue;
  225. volt = priv->vd_config->efuse_xlate(priv, vd->id,
  226. opp_id);
  227. if (volt)
  228. opp->volt = volt;
  229. }
  230. }
  231. for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
  232. if (vd->flags & VD_FLAG_INIT_DONE)
  233. continue;
  234. k3_avs_program_voltage(priv, vd, vd->opp);
  235. }
  236. return 0;
  237. }
  238. static struct vd_data am654_vd_data[] = {
  239. {
  240. .id = AM6_VDD_CORE,
  241. .dev_id = 82, /* AM6_DEV_CBASS0 */
  242. .clk_id = 0, /* main sysclk0 */
  243. .opp = AM6_OPP_NOM,
  244. .opps = {
  245. [AM6_OPP_NOM] = {
  246. .volt = 1000000,
  247. .freq = 250000000, /* CBASS0 */
  248. },
  249. },
  250. },
  251. {
  252. .id = AM6_VDD_MPU0,
  253. .dev_id = 202, /* AM6_DEV_COMPUTE_CLUSTER_A53_0 */
  254. .clk_id = 0, /* ARM clock */
  255. .opp = AM6_OPP_NOM,
  256. .opps = {
  257. [AM6_OPP_NOM] = {
  258. .volt = 1100000,
  259. .freq = 800000000,
  260. },
  261. [AM6_OPP_OD] = {
  262. .volt = 1200000,
  263. .freq = 1000000000,
  264. },
  265. [AM6_OPP_TURBO] = {
  266. .volt = 1240000,
  267. .freq = 1100000000,
  268. },
  269. },
  270. },
  271. {
  272. .id = AM6_VDD_MPU1,
  273. .opp = AM6_OPP_NOM,
  274. .dev_id = 204, /* AM6_DEV_COMPUTE_CLUSTER_A53_2 */
  275. .clk_id = 0, /* ARM clock */
  276. .opps = {
  277. [AM6_OPP_NOM] = {
  278. .volt = 1100000,
  279. .freq = 800000000,
  280. },
  281. [AM6_OPP_OD] = {
  282. .volt = 1200000,
  283. .freq = 1000000000,
  284. },
  285. [AM6_OPP_TURBO] = {
  286. .volt = 1240000,
  287. .freq = 1100000000,
  288. },
  289. },
  290. },
  291. { .id = -1 },
  292. };
  293. static struct vd_data j721e_vd_data[] = {
  294. {
  295. .id = J721E_VDD_MPU,
  296. .opp = AM6_OPP_NOM,
  297. .dev_id = 202, /* J721E_DEV_A72SS0_CORE0 */
  298. .clk_id = 2, /* ARM clock */
  299. .opps = {
  300. [AM6_OPP_NOM] = {
  301. .volt = 880000, /* TBD in DM */
  302. .freq = 2000000000,
  303. },
  304. },
  305. },
  306. { .id = -1 },
  307. };
  308. static struct vd_config j721e_vd_config = {
  309. .efuse_xlate = am6_efuse_xlate,
  310. .vds = j721e_vd_data,
  311. };
  312. static struct vd_config am654_vd_config = {
  313. .efuse_xlate = am6_efuse_xlate,
  314. .vds = am654_vd_data,
  315. };
  316. static const struct udevice_id k3_avs_ids[] = {
  317. { .compatible = "ti,am654-avs", .data = (ulong)&am654_vd_config },
  318. { .compatible = "ti,j721e-avs", .data = (ulong)&j721e_vd_config },
  319. {}
  320. };
  321. U_BOOT_DRIVER(k3_avs) = {
  322. .name = "k3_avs",
  323. .of_match = k3_avs_ids,
  324. .id = UCLASS_MISC,
  325. .probe = k3_avs_probe,
  326. .priv_auto_alloc_size = sizeof(struct k3_avs_privdata),
  327. };