k3_avs.c 8.4 KB

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