1 // SPDX-License-Identifier: GPL-2.0+
3 * Texas Instruments' K3 Clas 0 Adaptive Voltage Scaling driver
5 * Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/
6 * Tero Kristo <t-kristo@ti.com>
16 #include <dm/device_compat.h>
17 #include <power/regulator.h>
19 #define AM6_VTM_DEVINFO(i) (priv->base + 0x100 + 0x20 * (i))
20 #define AM6_VTM_OPPVID_VD(i) (priv->base + 0x104 + 0x20 * (i))
22 #define AM6_VTM_AVS0_SUPPORTED BIT(12)
24 #define AM6_VTM_OPP_SHIFT(opp) (8 * (opp))
25 #define AM6_VTM_OPP_MASK 0xff
27 #define VD_FLAG_INIT_DONE BIT(0)
29 struct k3_avs_privdata {
31 struct vd_config *vd_config;
45 struct opp opps[NUM_OPPS];
46 struct udevice *supply;
51 u32 (*efuse_xlate)(struct k3_avs_privdata *priv, int idx, int opp);
54 static struct k3_avs_privdata *k3_avs_priv;
57 * am6_efuse_voltage: read efuse voltage from VTM
58 * @priv: driver private data
59 * @idx: VD to read efuse for
60 * @opp: opp id to read
62 * Reads efuse value for the specified OPP, and converts the register
63 * value to a voltage. Returns the voltage in uV, or 0 if nominal voltage
66 * Efuse val to volt conversion logic:
68 * val > 171 volt increments in 20mV steps with base 171 => 1.66V
69 * val between 115 to 11 increments in 10mV steps with base 115 => 1.1V
70 * val between 15 to 115 increments in 5mV steps with base 15 => .6V
71 * val between 1 to 15 increments in 20mv steps with base 0 => .3V
74 static u32 am6_efuse_xlate(struct k3_avs_privdata *priv, int idx, int opp)
76 u32 val = readl(AM6_VTM_OPPVID_VD(idx));
78 val >>= AM6_VTM_OPP_SHIFT(opp);
79 val &= AM6_VTM_OPP_MASK;
85 return 1660000 + 20000 * (val - 171);
88 return 1100000 + 10000 * (val - 115);
91 return 600000 + 5000 * (val - 15);
93 return 300000 + 20000 * val;
96 static int k3_avs_program_voltage(struct k3_avs_privdata *priv,
100 u32 volt = vd->opps[opp_id].volt;
107 vd->flags |= VD_FLAG_INIT_DONE;
109 /* Take care of ganged rails and pick the Max amongst them*/
110 for (vd2 = priv->vd_config->vds; vd2->id >= 0; vd2++) {
114 if (vd2->supply != vd->supply)
117 if (vd2->opps[vd2->opp].volt > volt)
118 volt = vd2->opps[vd2->opp].volt;
120 vd2->flags |= VD_FLAG_INIT_DONE;
123 return regulator_set_value(vd->supply, volt);
126 static struct vd_data *get_vd(struct k3_avs_privdata *priv, int idx)
130 for (vd = priv->vd_config->vds; vd->id >= 0 && vd->id != idx; vd++)
140 * k3_avs_set_opp: Sets the voltage for an arbitrary VD rail
142 * @vdd_id: voltage domain ID
145 * Programs the desired OPP value for the defined voltage rail. This
146 * should be called from board files if reconfiguration is desired.
147 * Returns 0 on success, negative error value on failure.
149 int k3_avs_set_opp(struct udevice *dev, int vdd_id, int opp_id)
151 struct k3_avs_privdata *priv = dev_get_priv(dev);
154 vd = get_vd(priv, vdd_id);
158 return k3_avs_program_voltage(priv, vd, opp_id);
161 static int match_opp(struct vd_data *vd, u32 freq)
166 for (opp_id = 0; opp_id < NUM_OPPS; opp_id++) {
167 opp = &vd->opps[opp_id];
168 if (opp->freq == freq)
172 printf("No matching OPP found for freq %d.\n", freq);
178 * k3_avs_notify_freq: Notify clock rate change towards AVS subsystem
179 * @dev_id: Device ID for the clock to be changed
180 * @clk_id: Clock ID for the clock to be changed
181 * @freq: New frequency for clock
183 * Checks if the provided clock is the MPU clock or not, if not, return
184 * immediately. If MPU clock is provided, maps the provided MPU frequency
185 * towards an MPU OPP, and programs the voltage to the regulator. Return 0
186 * on success, negative error value on failure.
188 int k3_avs_notify_freq(int dev_id, int clk_id, u32 freq)
191 struct k3_avs_privdata *priv = k3_avs_priv;
194 /* Driver may not be probed yet */
198 for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
199 if (vd->dev_id != dev_id || vd->clk_id != clk_id)
202 opp_id = match_opp(vd, freq);
207 return k3_avs_program_voltage(priv, vd, opp_id);
213 static int k3_avs_configure(struct udevice *dev, struct k3_avs_privdata *priv)
215 struct vd_config *conf;
220 conf = (void *)dev_get_driver_data(dev);
222 priv->vd_config = conf;
224 for (vd = conf->vds; vd->id >= 0; vd++) {
225 sprintf(pname, "vdd-supply-%d", vd->id);
226 ret = device_get_supply_regulator(dev, pname, &vd->supply);
228 dev_warn(dev, "supply not found for VD%d.\n", vd->id);
230 sprintf(pname, "ti,default-opp-%d", vd->id);
231 ret = dev_read_u32_default(dev, pname, -1);
240 * k3_avs_probe: parses VD info from VTM, and re-configures the OPP data
242 * Parses all VDs on a device calculating the AVS class-0 voltages for them,
243 * and updates the vd_data based on this. The vd_data itself shall be used
244 * to program the required OPPs later on. Returns 0 on success, negative
245 * error value on failure.
247 static int k3_avs_probe(struct udevice *dev)
252 struct k3_avs_privdata *priv;
256 priv = dev_get_priv(dev);
260 ret = k3_avs_configure(dev, priv);
264 priv->base = dev_read_addr_ptr(dev);
268 for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
269 if (!(readl(AM6_VTM_DEVINFO(vd->id)) &
270 AM6_VTM_AVS0_SUPPORTED)) {
271 dev_warn(dev, "AVS-class 0 not supported for VD%d\n",
276 for (opp_id = 0; opp_id < NUM_OPPS; opp_id++) {
277 opp = &vd->opps[opp_id];
282 volt = priv->vd_config->efuse_xlate(priv, vd->id,
289 for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
290 if (vd->flags & VD_FLAG_INIT_DONE)
293 k3_avs_program_voltage(priv, vd, vd->opp);
299 static struct vd_data am654_vd_data[] = {
302 .dev_id = 82, /* AM6_DEV_CBASS0 */
303 .clk_id = 0, /* main sysclk0 */
308 .freq = 250000000, /* CBASS0 */
314 .dev_id = 202, /* AM6_DEV_COMPUTE_CLUSTER_A53_0 */
315 .clk_id = 0, /* ARM clock */
335 .dev_id = 204, /* AM6_DEV_COMPUTE_CLUSTER_A53_2 */
336 .clk_id = 0, /* ARM clock */
355 static struct vd_data j721e_vd_data[] = {
359 .dev_id = 202, /* J721E_DEV_A72SS0_CORE0 */
360 .clk_id = 2, /* ARM clock */
363 .volt = 880000, /* TBD in DM */
371 static struct vd_config j721e_vd_config = {
372 .efuse_xlate = am6_efuse_xlate,
373 .vds = j721e_vd_data,
376 static struct vd_config am654_vd_config = {
377 .efuse_xlate = am6_efuse_xlate,
378 .vds = am654_vd_data,
381 static const struct udevice_id k3_avs_ids[] = {
382 { .compatible = "ti,am654-avs", .data = (ulong)&am654_vd_config },
383 { .compatible = "ti,j721e-avs", .data = (ulong)&j721e_vd_config },
387 U_BOOT_DRIVER(k3_avs) = {
389 .of_match = k3_avs_ids,
391 .probe = k3_avs_probe,
392 .priv_auto_alloc_size = sizeof(struct k3_avs_privdata),