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 for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
195 if (vd->dev_id != dev_id || vd->clk_id != clk_id)
198 opp_id = match_opp(vd, freq);
203 return k3_avs_program_voltage(priv, vd, opp_id);
209 static int k3_avs_configure(struct udevice *dev, struct k3_avs_privdata *priv)
211 struct vd_config *conf;
216 conf = (void *)dev_get_driver_data(dev);
218 priv->vd_config = conf;
220 for (vd = conf->vds; vd->id >= 0; vd++) {
221 sprintf(pname, "vdd-supply-%d", vd->id);
222 ret = device_get_supply_regulator(dev, pname, &vd->supply);
224 dev_warn(dev, "supply not found for VD%d.\n", vd->id);
226 sprintf(pname, "ti,default-opp-%d", vd->id);
227 ret = dev_read_u32_default(dev, pname, -1);
236 * k3_avs_probe: parses VD info from VTM, and re-configures the OPP data
238 * Parses all VDs on a device calculating the AVS class-0 voltages for them,
239 * and updates the vd_data based on this. The vd_data itself shall be used
240 * to program the required OPPs later on. Returns 0 on success, negative
241 * error value on failure.
243 static int k3_avs_probe(struct udevice *dev)
248 struct k3_avs_privdata *priv;
252 priv = dev_get_priv(dev);
256 ret = k3_avs_configure(dev, priv);
260 priv->base = dev_read_addr_ptr(dev);
264 for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
265 if (!(readl(AM6_VTM_DEVINFO(vd->id)) &
266 AM6_VTM_AVS0_SUPPORTED)) {
267 dev_warn(dev, "AVS-class 0 not supported for VD%d\n",
272 for (opp_id = 0; opp_id < NUM_OPPS; opp_id++) {
273 opp = &vd->opps[opp_id];
278 volt = priv->vd_config->efuse_xlate(priv, vd->id,
285 for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
286 if (vd->flags & VD_FLAG_INIT_DONE)
289 k3_avs_program_voltage(priv, vd, vd->opp);
295 static struct vd_data am654_vd_data[] = {
298 .dev_id = 82, /* AM6_DEV_CBASS0 */
299 .clk_id = 0, /* main sysclk0 */
304 .freq = 250000000, /* CBASS0 */
310 .dev_id = 202, /* AM6_DEV_COMPUTE_CLUSTER_A53_0 */
311 .clk_id = 0, /* ARM clock */
331 .dev_id = 204, /* AM6_DEV_COMPUTE_CLUSTER_A53_2 */
332 .clk_id = 0, /* ARM clock */
351 static struct vd_data j721e_vd_data[] = {
355 .dev_id = 202, /* J721E_DEV_A72SS0_CORE0 */
356 .clk_id = 2, /* ARM clock */
359 .volt = 880000, /* TBD in DM */
367 static struct vd_config j721e_vd_config = {
368 .efuse_xlate = am6_efuse_xlate,
369 .vds = j721e_vd_data,
372 static struct vd_config am654_vd_config = {
373 .efuse_xlate = am6_efuse_xlate,
374 .vds = am654_vd_data,
377 static const struct udevice_id k3_avs_ids[] = {
378 { .compatible = "ti,am654-avs", .data = (ulong)&am654_vd_config },
379 { .compatible = "ti,j721e-avs", .data = (ulong)&j721e_vd_config },
383 U_BOOT_DRIVER(k3_avs) = {
385 .of_match = k3_avs_ids,
387 .probe = k3_avs_probe,
388 .priv_auto_alloc_size = sizeof(struct k3_avs_privdata),