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 <linux/bitops.h>
18 #include <power/regulator.h>
20 #define AM6_VTM_DEVINFO(i) (priv->base + 0x100 + 0x20 * (i))
21 #define AM6_VTM_OPPVID_VD(i) (priv->base + 0x104 + 0x20 * (i))
23 #define AM6_VTM_AVS0_SUPPORTED BIT(12)
25 #define AM6_VTM_OPP_SHIFT(opp) (8 * (opp))
26 #define AM6_VTM_OPP_MASK 0xff
28 #define VD_FLAG_INIT_DONE BIT(0)
30 struct k3_avs_privdata {
32 struct vd_config *vd_config;
46 struct opp opps[NUM_OPPS];
47 struct udevice *supply;
52 u32 (*efuse_xlate)(struct k3_avs_privdata *priv, int idx, int opp);
55 static struct k3_avs_privdata *k3_avs_priv;
58 * am6_efuse_voltage: read efuse voltage from VTM
59 * @priv: driver private data
60 * @idx: VD to read efuse for
61 * @opp: opp id to read
63 * Reads efuse value for the specified OPP, and converts the register
64 * value to a voltage. Returns the voltage in uV, or 0 if nominal voltage
67 * Efuse val to volt conversion logic:
69 * val > 171 volt increments in 20mV steps with base 171 => 1.66V
70 * val between 115 to 11 increments in 10mV steps with base 115 => 1.1V
71 * val between 15 to 115 increments in 5mV steps with base 15 => .6V
72 * val between 1 to 15 increments in 20mv steps with base 0 => .3V
75 static u32 am6_efuse_xlate(struct k3_avs_privdata *priv, int idx, int opp)
77 u32 val = readl(AM6_VTM_OPPVID_VD(idx));
79 val >>= AM6_VTM_OPP_SHIFT(opp);
80 val &= AM6_VTM_OPP_MASK;
86 return 1660000 + 20000 * (val - 171);
89 return 1100000 + 10000 * (val - 115);
92 return 600000 + 5000 * (val - 15);
94 return 300000 + 20000 * val;
97 static int k3_avs_program_voltage(struct k3_avs_privdata *priv,
101 u32 volt = vd->opps[opp_id].volt;
108 vd->flags |= VD_FLAG_INIT_DONE;
110 /* Take care of ganged rails and pick the Max amongst them*/
111 for (vd2 = priv->vd_config->vds; vd2->id >= 0; vd2++) {
115 if (vd2->supply != vd->supply)
118 if (vd2->opps[vd2->opp].volt > volt)
119 volt = vd2->opps[vd2->opp].volt;
121 vd2->flags |= VD_FLAG_INIT_DONE;
124 return regulator_set_value(vd->supply, volt);
127 static struct vd_data *get_vd(struct k3_avs_privdata *priv, int idx)
131 for (vd = priv->vd_config->vds; vd->id >= 0 && vd->id != idx; vd++)
141 * k3_avs_set_opp: Sets the voltage for an arbitrary VD rail
143 * @vdd_id: voltage domain ID
146 * Programs the desired OPP value for the defined voltage rail. This
147 * should be called from board files if reconfiguration is desired.
148 * Returns 0 on success, negative error value on failure.
150 int k3_avs_set_opp(struct udevice *dev, int vdd_id, int opp_id)
152 struct k3_avs_privdata *priv = dev_get_priv(dev);
155 vd = get_vd(priv, vdd_id);
159 return k3_avs_program_voltage(priv, vd, opp_id);
162 static int match_opp(struct vd_data *vd, u32 freq)
167 for (opp_id = 0; opp_id < NUM_OPPS; opp_id++) {
168 opp = &vd->opps[opp_id];
169 if (opp->freq == freq)
173 printf("No matching OPP found for freq %d.\n", freq);
179 * k3_avs_notify_freq: Notify clock rate change towards AVS subsystem
180 * @dev_id: Device ID for the clock to be changed
181 * @clk_id: Clock ID for the clock to be changed
182 * @freq: New frequency for clock
184 * Checks if the provided clock is the MPU clock or not, if not, return
185 * immediately. If MPU clock is provided, maps the provided MPU frequency
186 * towards an MPU OPP, and programs the voltage to the regulator. Return 0
187 * on success, negative error value on failure.
189 int k3_avs_notify_freq(int dev_id, int clk_id, u32 freq)
192 struct k3_avs_privdata *priv = k3_avs_priv;
195 /* Driver may not be probed yet */
199 for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
200 if (vd->dev_id != dev_id || vd->clk_id != clk_id)
203 opp_id = match_opp(vd, freq);
208 return k3_avs_program_voltage(priv, vd, opp_id);
214 static int k3_avs_configure(struct udevice *dev, struct k3_avs_privdata *priv)
216 struct vd_config *conf;
221 conf = (void *)dev_get_driver_data(dev);
223 priv->vd_config = conf;
225 for (vd = conf->vds; vd->id >= 0; vd++) {
226 sprintf(pname, "vdd-supply-%d", vd->id);
227 ret = device_get_supply_regulator(dev, pname, &vd->supply);
229 dev_warn(dev, "supply not found for VD%d.\n", vd->id);
231 sprintf(pname, "ti,default-opp-%d", vd->id);
232 ret = dev_read_u32_default(dev, pname, -1);
241 * k3_avs_probe: parses VD info from VTM, and re-configures the OPP data
243 * Parses all VDs on a device calculating the AVS class-0 voltages for them,
244 * and updates the vd_data based on this. The vd_data itself shall be used
245 * to program the required OPPs later on. Returns 0 on success, negative
246 * error value on failure.
248 static int k3_avs_probe(struct udevice *dev)
253 struct k3_avs_privdata *priv;
257 priv = dev_get_priv(dev);
261 ret = k3_avs_configure(dev, priv);
265 priv->base = dev_read_addr_ptr(dev);
269 for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
270 if (!(readl(AM6_VTM_DEVINFO(vd->id)) &
271 AM6_VTM_AVS0_SUPPORTED)) {
272 dev_warn(dev, "AVS-class 0 not supported for VD%d\n",
277 for (opp_id = 0; opp_id < NUM_OPPS; opp_id++) {
278 opp = &vd->opps[opp_id];
283 volt = priv->vd_config->efuse_xlate(priv, vd->id,
290 for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
291 if (vd->flags & VD_FLAG_INIT_DONE)
294 k3_avs_program_voltage(priv, vd, vd->opp);
300 static struct vd_data am654_vd_data[] = {
303 .dev_id = 82, /* AM6_DEV_CBASS0 */
304 .clk_id = 0, /* main sysclk0 */
309 .freq = 250000000, /* CBASS0 */
315 .dev_id = 202, /* AM6_DEV_COMPUTE_CLUSTER_A53_0 */
316 .clk_id = 0, /* ARM clock */
336 .dev_id = 204, /* AM6_DEV_COMPUTE_CLUSTER_A53_2 */
337 .clk_id = 0, /* ARM clock */
356 static struct vd_data j721e_vd_data[] = {
360 .dev_id = 202, /* J721E_DEV_A72SS0_CORE0 */
361 .clk_id = 2, /* ARM clock */
364 .volt = 880000, /* TBD in DM */
372 static struct vd_config j721e_vd_config = {
373 .efuse_xlate = am6_efuse_xlate,
374 .vds = j721e_vd_data,
377 static struct vd_config am654_vd_config = {
378 .efuse_xlate = am6_efuse_xlate,
379 .vds = am654_vd_data,
382 static const struct udevice_id k3_avs_ids[] = {
383 { .compatible = "ti,am654-avs", .data = (ulong)&am654_vd_config },
384 { .compatible = "ti,j721e-avs", .data = (ulong)&j721e_vd_config },
388 U_BOOT_DRIVER(k3_avs) = {
390 .of_match = k3_avs_ids,
392 .probe = k3_avs_probe,
393 .priv_auto = sizeof(struct k3_avs_privdata),