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 <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))
21 #define AM6_VTM_AVS0_SUPPORTED BIT(12)
23 #define AM6_VTM_OPP_SHIFT(opp) (8 * (opp))
24 #define AM6_VTM_OPP_MASK 0xff
26 #define VD_FLAG_INIT_DONE BIT(0)
28 struct k3_avs_privdata {
30 struct vd_config *vd_config;
44 struct opp opps[NUM_OPPS];
45 struct udevice *supply;
50 u32 (*efuse_xlate)(struct k3_avs_privdata *priv, int idx, int opp);
53 static struct k3_avs_privdata *k3_avs_priv;
56 * am6_efuse_voltage: read efuse voltage from VTM
57 * @priv: driver private data
58 * @idx: VD to read efuse for
59 * @opp: opp id to read
61 * Reads efuse value for the specified OPP, and converts the register
62 * value to a voltage. Returns the voltage in uV, or 0 if nominal voltage
65 * Efuse val to volt conversion logic:
67 * val > 171 volt increments in 20mV steps with base 171 => 1.66V
68 * val between 115 to 11 increments in 10mV steps with base 115 => 1.1V
69 * val between 15 to 115 increments in 5mV steps with base 15 => .6V
70 * val between 1 to 15 increments in 20mv steps with base 0 => .3V
73 static u32 am6_efuse_xlate(struct k3_avs_privdata *priv, int idx, int opp)
75 u32 val = readl(AM6_VTM_OPPVID_VD(idx));
77 val >>= AM6_VTM_OPP_SHIFT(opp);
78 val &= AM6_VTM_OPP_MASK;
84 return 1660000 + 20000 * (val - 171);
87 return 1100000 + 10000 * (val - 115);
90 return 600000 + 5000 * (val - 15);
92 return 300000 + 20000 * val;
95 static int k3_avs_program_voltage(struct k3_avs_privdata *priv,
99 u32 volt = vd->opps[opp_id].volt;
106 vd->flags |= VD_FLAG_INIT_DONE;
108 /* Take care of ganged rails and pick the Max amongst them*/
109 for (vd2 = priv->vd_config->vds; vd2->id >= 0; vd2++) {
113 if (vd2->supply != vd->supply)
116 if (vd2->opps[vd2->opp].volt > volt)
117 volt = vd2->opps[vd2->opp].volt;
119 vd2->flags |= VD_FLAG_INIT_DONE;
122 return regulator_set_value(vd->supply, volt);
125 static struct vd_data *get_vd(struct k3_avs_privdata *priv, int idx)
129 for (vd = priv->vd_config->vds; vd->id >= 0 && vd->id != idx; vd++)
139 * k3_avs_set_opp: Sets the voltage for an arbitrary VD rail
141 * @vdd_id: voltage domain ID
144 * Programs the desired OPP value for the defined voltage rail. This
145 * should be called from board files if reconfiguration is desired.
146 * Returns 0 on success, negative error value on failure.
148 int k3_avs_set_opp(struct udevice *dev, int vdd_id, int opp_id)
150 struct k3_avs_privdata *priv = dev_get_priv(dev);
153 vd = get_vd(priv, vdd_id);
157 return k3_avs_program_voltage(priv, vd, opp_id);
160 static int match_opp(struct vd_data *vd, u32 freq)
165 for (opp_id = 0; opp_id < NUM_OPPS; opp_id++) {
166 opp = &vd->opps[opp_id];
167 if (opp->freq == freq)
171 printf("No matching OPP found for freq %d.\n", freq);
177 * k3_avs_notify_freq: Notify clock rate change towards AVS subsystem
178 * @dev_id: Device ID for the clock to be changed
179 * @clk_id: Clock ID for the clock to be changed
180 * @freq: New frequency for clock
182 * Checks if the provided clock is the MPU clock or not, if not, return
183 * immediately. If MPU clock is provided, maps the provided MPU frequency
184 * towards an MPU OPP, and programs the voltage to the regulator. Return 0
185 * on success, negative error value on failure.
187 int k3_avs_notify_freq(int dev_id, int clk_id, u32 freq)
190 struct k3_avs_privdata *priv = k3_avs_priv;
193 for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
194 if (vd->dev_id != dev_id || vd->clk_id != clk_id)
197 opp_id = match_opp(vd, freq);
202 return k3_avs_program_voltage(priv, vd, opp_id);
208 static int k3_avs_configure(struct udevice *dev, struct k3_avs_privdata *priv)
210 struct vd_config *conf;
215 conf = (void *)dev_get_driver_data(dev);
217 priv->vd_config = conf;
219 for (vd = conf->vds; vd->id >= 0; vd++) {
220 sprintf(pname, "vdd-supply-%d", vd->id);
221 ret = device_get_supply_regulator(dev, pname, &vd->supply);
223 dev_warn(dev, "supply not found for VD%d.\n", vd->id);
225 sprintf(pname, "ti,default-opp-%d", vd->id);
226 ret = dev_read_u32_default(dev, pname, -1);
235 * k3_avs_probe: parses VD info from VTM, and re-configures the OPP data
237 * Parses all VDs on a device calculating the AVS class-0 voltages for them,
238 * and updates the vd_data based on this. The vd_data itself shall be used
239 * to program the required OPPs later on. Returns 0 on success, negative
240 * error value on failure.
242 static int k3_avs_probe(struct udevice *dev)
247 struct k3_avs_privdata *priv;
251 priv = dev_get_priv(dev);
255 ret = k3_avs_configure(dev, priv);
259 priv->base = dev_read_addr_ptr(dev);
263 for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
264 if (!(readl(AM6_VTM_DEVINFO(vd->id)) &
265 AM6_VTM_AVS0_SUPPORTED)) {
266 dev_warn(dev, "AVS-class 0 not supported for VD%d\n",
271 for (opp_id = 0; opp_id < NUM_OPPS; opp_id++) {
272 opp = &vd->opps[opp_id];
277 volt = priv->vd_config->efuse_xlate(priv, vd->id,
284 for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
285 if (vd->flags & VD_FLAG_INIT_DONE)
288 k3_avs_program_voltage(priv, vd, vd->opp);
294 static struct vd_data am654_vd_data[] = {
297 .dev_id = 82, /* AM6_DEV_CBASS0 */
298 .clk_id = 0, /* main sysclk0 */
303 .freq = 250000000, /* CBASS0 */
309 .dev_id = 202, /* AM6_DEV_COMPUTE_CLUSTER_A53_0 */
310 .clk_id = 0, /* ARM clock */
330 .dev_id = 204, /* AM6_DEV_COMPUTE_CLUSTER_A53_2 */
331 .clk_id = 0, /* ARM clock */
350 static struct vd_data j721e_vd_data[] = {
354 .dev_id = 202, /* J721E_DEV_A72SS0_CORE0 */
355 .clk_id = 2, /* ARM clock */
358 .volt = 880000, /* TBD in DM */
366 static struct vd_config j721e_vd_config = {
367 .efuse_xlate = am6_efuse_xlate,
368 .vds = j721e_vd_data,
371 static struct vd_config am654_vd_config = {
372 .efuse_xlate = am6_efuse_xlate,
373 .vds = am654_vd_data,
376 static const struct udevice_id k3_avs_ids[] = {
377 { .compatible = "ti,am654-avs", .data = (ulong)&am654_vd_config },
378 { .compatible = "ti,j721e-avs", .data = (ulong)&j721e_vd_config },
382 U_BOOT_DRIVER(k3_avs) = {
384 .of_match = k3_avs_ids,
386 .probe = k3_avs_probe,
387 .priv_auto_alloc_size = sizeof(struct k3_avs_privdata),