blob: 87471cc3b16adb39f485ca29121be24a4f7edc4b [file] [log] [blame]
Tero Kristo887dde52019-10-24 15:00:46 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Texas Instruments' K3 Clas 0 Adaptive Voltage Scaling driver
4 *
Nishanth Menoneaa39c62023-11-01 15:56:03 -05005 * Copyright (C) 2019 Texas Instruments Incorporated - https://www.ti.com/
Tero Kristo887dde52019-10-24 15:00:46 +05306 * Tero Kristo <t-kristo@ti.com>
7 *
8 */
9
Tero Kristo887dde52019-10-24 15:00:46 +053010#include <dm.h>
11#include <errno.h>
12#include <asm/io.h>
13#include <i2c.h>
14#include <k3-avs.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060016#include <linux/bitops.h>
Udit Kumarbac62152023-10-19 12:57:53 +053017#include <linux/delay.h>
Tero Kristo887dde52019-10-24 15:00:46 +053018#include <power/regulator.h>
19
20#define AM6_VTM_DEVINFO(i) (priv->base + 0x100 + 0x20 * (i))
21#define AM6_VTM_OPPVID_VD(i) (priv->base + 0x104 + 0x20 * (i))
22
23#define AM6_VTM_AVS0_SUPPORTED BIT(12)
24
25#define AM6_VTM_OPP_SHIFT(opp) (8 * (opp))
26#define AM6_VTM_OPP_MASK 0xff
27
Udit Kumarbac62152023-10-19 12:57:53 +053028#define K3_VTM_DEVINFO_PWR0_OFFSET 0x4
29#define K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK 0xf0
30#define K3_VTM_TMPSENS0_CTRL_OFFSET 0x300
31#define K3_VTM_TMPSENS_STAT_OFFSET 0x8
32#define K3_VTM_ANYMAXT_OUTRG_ALERT_EN 0x1
33#define K3_VTM_LOW_TEMP_OFFSET 0x10
34#define K3_VTM_MISC_CTRL2_OFFSET 0x10
35#define K3_VTM_MISC_CTRL1_OFFSET 0xc
36#define K3_VTM_TMPSENS_CTRL1_SOC BIT(5)
37#define K3_VTM_TMPSENS_CTRL_CLRZ BIT(6)
38#define K3_VTM_TMPSENS_CTRL_MAXT_OUTRG_EN BIT(11)
39#define K3_VTM_ADC_COUNT_FOR_123C 0x2f8
40#define K3_VTM_ADC_COUNT_FOR_105C 0x288
41#define K3_VTM_ADC_WA_VALUE 0x2c
42#define K3_VTM_FUSE_MASK 0xc0000000
43
Tero Kristo887dde52019-10-24 15:00:46 +053044#define VD_FLAG_INIT_DONE BIT(0)
45
46struct k3_avs_privdata {
47 void *base;
48 struct vd_config *vd_config;
Udit Kumarbac62152023-10-19 12:57:53 +053049 struct udevice *dev;
Tero Kristo887dde52019-10-24 15:00:46 +053050};
51
52struct opp {
53 u32 freq;
54 u32 volt;
55};
56
57struct vd_data {
58 int id;
59 u8 opp;
60 u8 flags;
61 int dev_id;
62 int clk_id;
63 struct opp opps[NUM_OPPS];
64 struct udevice *supply;
65};
66
67struct vd_config {
68 struct vd_data *vds;
69 u32 (*efuse_xlate)(struct k3_avs_privdata *priv, int idx, int opp);
70};
71
72static struct k3_avs_privdata *k3_avs_priv;
73
74/**
75 * am6_efuse_voltage: read efuse voltage from VTM
76 * @priv: driver private data
77 * @idx: VD to read efuse for
78 * @opp: opp id to read
79 *
80 * Reads efuse value for the specified OPP, and converts the register
81 * value to a voltage. Returns the voltage in uV, or 0 if nominal voltage
82 * should be used.
83 *
84 * Efuse val to volt conversion logic:
85 *
86 * val > 171 volt increments in 20mV steps with base 171 => 1.66V
87 * val between 115 to 11 increments in 10mV steps with base 115 => 1.1V
88 * val between 15 to 115 increments in 5mV steps with base 15 => .6V
89 * val between 1 to 15 increments in 20mv steps with base 0 => .3V
90 * val 0 is invalid
91 */
92static u32 am6_efuse_xlate(struct k3_avs_privdata *priv, int idx, int opp)
93{
94 u32 val = readl(AM6_VTM_OPPVID_VD(idx));
95
96 val >>= AM6_VTM_OPP_SHIFT(opp);
97 val &= AM6_VTM_OPP_MASK;
98
99 if (!val)
100 return 0;
101
102 if (val > 171)
103 return 1660000 + 20000 * (val - 171);
104
105 if (val > 115)
106 return 1100000 + 10000 * (val - 115);
107
108 if (val > 15)
109 return 600000 + 5000 * (val - 15);
110
111 return 300000 + 20000 * val;
112}
113
114static int k3_avs_program_voltage(struct k3_avs_privdata *priv,
115 struct vd_data *vd,
116 int opp_id)
117{
118 u32 volt = vd->opps[opp_id].volt;
119 struct vd_data *vd2;
120
121 if (!vd->supply)
122 return -ENODEV;
123
124 vd->opp = opp_id;
125 vd->flags |= VD_FLAG_INIT_DONE;
126
127 /* Take care of ganged rails and pick the Max amongst them*/
128 for (vd2 = priv->vd_config->vds; vd2->id >= 0; vd2++) {
129 if (vd == vd2)
130 continue;
131
132 if (vd2->supply != vd->supply)
133 continue;
134
135 if (vd2->opps[vd2->opp].volt > volt)
136 volt = vd2->opps[vd2->opp].volt;
137
138 vd2->flags |= VD_FLAG_INIT_DONE;
139 }
140
141 return regulator_set_value(vd->supply, volt);
142}
143
144static struct vd_data *get_vd(struct k3_avs_privdata *priv, int idx)
145{
146 struct vd_data *vd;
147
148 for (vd = priv->vd_config->vds; vd->id >= 0 && vd->id != idx; vd++)
149 ;
150
151 if (vd->id < 0)
152 return NULL;
153
154 return vd;
155}
156
157/**
158 * k3_avs_set_opp: Sets the voltage for an arbitrary VD rail
159 * @dev: AVS device
160 * @vdd_id: voltage domain ID
161 * @opp_id: OPP ID
162 *
163 * Programs the desired OPP value for the defined voltage rail. This
164 * should be called from board files if reconfiguration is desired.
165 * Returns 0 on success, negative error value on failure.
166 */
167int k3_avs_set_opp(struct udevice *dev, int vdd_id, int opp_id)
168{
169 struct k3_avs_privdata *priv = dev_get_priv(dev);
170 struct vd_data *vd;
171
172 vd = get_vd(priv, vdd_id);
173 if (!vd)
174 return -EINVAL;
175
176 return k3_avs_program_voltage(priv, vd, opp_id);
177}
178
179static int match_opp(struct vd_data *vd, u32 freq)
180{
181 struct opp *opp;
182 int opp_id;
183
184 for (opp_id = 0; opp_id < NUM_OPPS; opp_id++) {
185 opp = &vd->opps[opp_id];
186 if (opp->freq == freq)
187 return opp_id;
188 }
189
190 printf("No matching OPP found for freq %d.\n", freq);
191
192 return -EINVAL;
193}
194
195/**
196 * k3_avs_notify_freq: Notify clock rate change towards AVS subsystem
197 * @dev_id: Device ID for the clock to be changed
198 * @clk_id: Clock ID for the clock to be changed
199 * @freq: New frequency for clock
200 *
201 * Checks if the provided clock is the MPU clock or not, if not, return
202 * immediately. If MPU clock is provided, maps the provided MPU frequency
203 * towards an MPU OPP, and programs the voltage to the regulator. Return 0
204 * on success, negative error value on failure.
205 */
206int k3_avs_notify_freq(int dev_id, int clk_id, u32 freq)
207{
208 int opp_id;
209 struct k3_avs_privdata *priv = k3_avs_priv;
210 struct vd_data *vd;
211
Vignesh Raghavendra0b6ce802020-02-14 17:52:17 +0530212 /* Driver may not be probed yet */
213 if (!priv)
214 return -EINVAL;
215
Tero Kristo887dde52019-10-24 15:00:46 +0530216 for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
217 if (vd->dev_id != dev_id || vd->clk_id != clk_id)
218 continue;
219
220 opp_id = match_opp(vd, freq);
221 if (opp_id < 0)
222 return opp_id;
223
224 vd->opp = opp_id;
225 return k3_avs_program_voltage(priv, vd, opp_id);
226 }
227
228 return -EINVAL;
229}
230
231static int k3_avs_configure(struct udevice *dev, struct k3_avs_privdata *priv)
232{
233 struct vd_config *conf;
234 int ret;
235 char pname[20];
236 struct vd_data *vd;
237
238 conf = (void *)dev_get_driver_data(dev);
239
240 priv->vd_config = conf;
241
242 for (vd = conf->vds; vd->id >= 0; vd++) {
243 sprintf(pname, "vdd-supply-%d", vd->id);
244 ret = device_get_supply_regulator(dev, pname, &vd->supply);
245 if (ret)
246 dev_warn(dev, "supply not found for VD%d.\n", vd->id);
247
248 sprintf(pname, "ti,default-opp-%d", vd->id);
249 ret = dev_read_u32_default(dev, pname, -1);
250 if (ret != -1)
251 vd->opp = ret;
252 }
253
254 return 0;
255}
256
Udit Kumarbac62152023-10-19 12:57:53 +0530257/* k3_avs_program_tshut : Program thermal shutdown value for SOC
258 * set the values corresponding to thresholds to ~123C and 105C
259 * This is optional feature, Few times OS driver takes care of
260 * tshut programing.
261 */
262
263static void k3_avs_program_tshut(struct k3_avs_privdata *priv)
264{
265 int cnt, id, val;
266 int workaround_needed = 0;
267 u32 ctrl_offset;
268 void __iomem *cfg2_base;
269 void __iomem *fuse_base;
270
271 cfg2_base = (void __iomem *)devfdt_get_addr_index(priv->dev, 1);
272 if (IS_ERR(cfg2_base)) {
273 dev_err(priv->dev, "cfg base is not defined\n");
274 return;
275 }
276
277 /*
278 * Some of TI's J721E SoCs require a software trimming procedure
279 * for the temperature monitors to function properly. To determine
280 * if this particular SoC is NOT affected, both bits in the
281 * WKUP_SPARE_FUSE0[31:30] will be set (0xC0000000) indicating
282 * when software trimming should NOT be applied.
283 *
284 * https://www.ti.com/lit/er/sprz455c/sprz455c.pdf
285 * This routine checks if workaround_needed to be applied or not
286 * based upon workaround_needed, adjust fixed value of tshut high and low
287 */
288
289 if (device_is_compatible(priv->dev, "ti,j721e-vtm")) {
290 fuse_base = (void __iomem *)devfdt_get_addr_index(priv->dev, 2);
291 if (IS_ERR(fuse_base)) {
292 dev_err(priv->dev, "fuse-base is not defined for J721E Soc\n");
293 return;
294 }
295
296 if (!((readl(fuse_base) & K3_VTM_FUSE_MASK) == K3_VTM_FUSE_MASK))
297 workaround_needed = 1;
298 }
299
300 dev_dbg(priv->dev, "Work around %sneeded\n", workaround_needed ? "" : "not ");
301
302 /* Get the sensor count in the VTM */
303 val = readl(priv->base + K3_VTM_DEVINFO_PWR0_OFFSET);
304 cnt = val & K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK;
305 cnt >>= __ffs(K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK);
306
307 /* Program the thermal sensors */
308 for (id = 0; id < cnt; id++) {
309 ctrl_offset = K3_VTM_TMPSENS0_CTRL_OFFSET + id * 0x20;
310
311 val = readl(cfg2_base + ctrl_offset);
312 val |= (K3_VTM_TMPSENS_CTRL_MAXT_OUTRG_EN |
313 K3_VTM_TMPSENS_CTRL1_SOC |
314 K3_VTM_TMPSENS_CTRL_CLRZ | BIT(4));
315 writel(val, cfg2_base + ctrl_offset);
316 }
317
318 /*
319 * Program TSHUT thresholds
320 * Step 1: set the thresholds to ~123C and 105C WKUP_VTM_MISC_CTRL2
321 * Step 2: WKUP_VTM_TMPSENS_CTRL_j set the MAXT_OUTRG_EN bit
322 * This is already taken care as per of init
323 * Step 3: WKUP_VTM_MISC_CTRL set the ANYMAXT_OUTRG_ALERT_EN bit
324 */
325
326 /* Low thresholds for tshut*/
327 val = (K3_VTM_ADC_COUNT_FOR_105C - workaround_needed * K3_VTM_ADC_WA_VALUE)
328 << K3_VTM_LOW_TEMP_OFFSET;
329 /* high thresholds */
330 val |= K3_VTM_ADC_COUNT_FOR_123C - workaround_needed * K3_VTM_ADC_WA_VALUE;
331
332 writel(val, cfg2_base + K3_VTM_MISC_CTRL2_OFFSET);
333 /* ramp-up delay from Linux code */
334 mdelay(100);
335 val = readl(cfg2_base + K3_VTM_MISC_CTRL1_OFFSET) | K3_VTM_ANYMAXT_OUTRG_ALERT_EN;
336 writel(val, cfg2_base + K3_VTM_MISC_CTRL1_OFFSET);
337}
338
Tero Kristo887dde52019-10-24 15:00:46 +0530339/**
340 * k3_avs_probe: parses VD info from VTM, and re-configures the OPP data
341 *
342 * Parses all VDs on a device calculating the AVS class-0 voltages for them,
343 * and updates the vd_data based on this. The vd_data itself shall be used
344 * to program the required OPPs later on. Returns 0 on success, negative
345 * error value on failure.
346 */
347static int k3_avs_probe(struct udevice *dev)
348{
349 int opp_id;
350 u32 volt;
351 struct opp *opp;
352 struct k3_avs_privdata *priv;
353 struct vd_data *vd;
354 int ret;
355
356 priv = dev_get_priv(dev);
Udit Kumarbac62152023-10-19 12:57:53 +0530357 priv->dev = dev;
Tero Kristo887dde52019-10-24 15:00:46 +0530358
359 k3_avs_priv = priv;
360
361 ret = k3_avs_configure(dev, priv);
362 if (ret)
363 return ret;
364
365 priv->base = dev_read_addr_ptr(dev);
366 if (!priv->base)
367 return -ENODEV;
368
369 for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
370 if (!(readl(AM6_VTM_DEVINFO(vd->id)) &
371 AM6_VTM_AVS0_SUPPORTED)) {
372 dev_warn(dev, "AVS-class 0 not supported for VD%d\n",
373 vd->id);
374 continue;
375 }
376
377 for (opp_id = 0; opp_id < NUM_OPPS; opp_id++) {
378 opp = &vd->opps[opp_id];
379
380 if (!opp->freq)
381 continue;
382
383 volt = priv->vd_config->efuse_xlate(priv, vd->id,
384 opp_id);
385 if (volt)
386 opp->volt = volt;
387 }
388 }
389
390 for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
391 if (vd->flags & VD_FLAG_INIT_DONE)
392 continue;
393
394 k3_avs_program_voltage(priv, vd, vd->opp);
395 }
396
Udit Kumarbac62152023-10-19 12:57:53 +0530397 if (!device_is_compatible(priv->dev, "ti,am654-avs"))
398 k3_avs_program_tshut(priv);
399
Tero Kristo887dde52019-10-24 15:00:46 +0530400 return 0;
401}
402
403static struct vd_data am654_vd_data[] = {
404 {
405 .id = AM6_VDD_CORE,
406 .dev_id = 82, /* AM6_DEV_CBASS0 */
407 .clk_id = 0, /* main sysclk0 */
408 .opp = AM6_OPP_NOM,
409 .opps = {
410 [AM6_OPP_NOM] = {
411 .volt = 1000000,
412 .freq = 250000000, /* CBASS0 */
413 },
414 },
415 },
416 {
417 .id = AM6_VDD_MPU0,
418 .dev_id = 202, /* AM6_DEV_COMPUTE_CLUSTER_A53_0 */
419 .clk_id = 0, /* ARM clock */
420 .opp = AM6_OPP_NOM,
421 .opps = {
422 [AM6_OPP_NOM] = {
Tero Kristo75774302020-02-14 09:05:10 +0200423 .volt = 1100000,
Tero Kristo887dde52019-10-24 15:00:46 +0530424 .freq = 800000000,
425 },
426 [AM6_OPP_OD] = {
Tero Kristo75774302020-02-14 09:05:10 +0200427 .volt = 1200000,
Tero Kristo887dde52019-10-24 15:00:46 +0530428 .freq = 1000000000,
429 },
430 [AM6_OPP_TURBO] = {
Tero Kristo75774302020-02-14 09:05:10 +0200431 .volt = 1240000,
Tero Kristo887dde52019-10-24 15:00:46 +0530432 .freq = 1100000000,
433 },
434 },
435 },
436 {
437 .id = AM6_VDD_MPU1,
438 .opp = AM6_OPP_NOM,
439 .dev_id = 204, /* AM6_DEV_COMPUTE_CLUSTER_A53_2 */
440 .clk_id = 0, /* ARM clock */
441 .opps = {
442 [AM6_OPP_NOM] = {
Tero Kristo75774302020-02-14 09:05:10 +0200443 .volt = 1100000,
Tero Kristo887dde52019-10-24 15:00:46 +0530444 .freq = 800000000,
445 },
446 [AM6_OPP_OD] = {
Tero Kristo75774302020-02-14 09:05:10 +0200447 .volt = 1200000,
Tero Kristo887dde52019-10-24 15:00:46 +0530448 .freq = 1000000000,
449 },
450 [AM6_OPP_TURBO] = {
Tero Kristo75774302020-02-14 09:05:10 +0200451 .volt = 1240000,
Tero Kristo887dde52019-10-24 15:00:46 +0530452 .freq = 1100000000,
453 },
454 },
455 },
456 { .id = -1 },
457};
458
Keerthyfda68e42019-10-24 15:00:49 +0530459static struct vd_data j721e_vd_data[] = {
460 {
461 .id = J721E_VDD_MPU,
462 .opp = AM6_OPP_NOM,
463 .dev_id = 202, /* J721E_DEV_A72SS0_CORE0 */
464 .clk_id = 2, /* ARM clock */
465 .opps = {
466 [AM6_OPP_NOM] = {
467 .volt = 880000, /* TBD in DM */
468 .freq = 2000000000,
469 },
470 },
471 },
472 { .id = -1 },
473};
474
475static struct vd_config j721e_vd_config = {
476 .efuse_xlate = am6_efuse_xlate,
477 .vds = j721e_vd_data,
478};
479
Tero Kristo887dde52019-10-24 15:00:46 +0530480static struct vd_config am654_vd_config = {
481 .efuse_xlate = am6_efuse_xlate,
482 .vds = am654_vd_data,
483};
484
485static const struct udevice_id k3_avs_ids[] = {
486 { .compatible = "ti,am654-avs", .data = (ulong)&am654_vd_config },
Keerthyfda68e42019-10-24 15:00:49 +0530487 { .compatible = "ti,j721e-avs", .data = (ulong)&j721e_vd_config },
Reid Tonking8cc3ca32023-09-07 13:06:35 -0500488 { .compatible = "ti,j721e-vtm", .data = (ulong)&j721e_vd_config },
489 { .compatible = "ti,j7200-vtm", .data = (ulong)&j721e_vd_config },
Tero Kristo887dde52019-10-24 15:00:46 +0530490 {}
491};
492
493U_BOOT_DRIVER(k3_avs) = {
494 .name = "k3_avs",
495 .of_match = k3_avs_ids,
496 .id = UCLASS_MISC,
497 .probe = k3_avs_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700498 .priv_auto = sizeof(struct k3_avs_privdata),
Tero Kristo887dde52019-10-24 15:00:46 +0530499};