blob: 99a18a109b7c12a8218315997f5b0d867bc2dad3 [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;
Manorit Chawdhry9234ab92024-10-15 16:22:20 +0530355 ofnode node;
356 struct ofnode_phandle_args phandle_args;
357 int i = 0;
Tero Kristo887dde52019-10-24 15:00:46 +0530358
359 priv = dev_get_priv(dev);
Udit Kumarbac62152023-10-19 12:57:53 +0530360 priv->dev = dev;
Tero Kristo887dde52019-10-24 15:00:46 +0530361
362 k3_avs_priv = priv;
363
364 ret = k3_avs_configure(dev, priv);
365 if (ret)
366 return ret;
367
368 priv->base = dev_read_addr_ptr(dev);
369 if (!priv->base)
370 return -ENODEV;
371
372 for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
Manorit Chawdhry9234ab92024-10-15 16:22:20 +0530373 /* Get the clock and dev id for Jacinto platforms */
374 if (vd->id == J721E_VDD_MPU) {
375 node = ofnode_by_compatible(ofnode_null(), "ti,am654-rproc");
376 if (!ofnode_valid(node))
377 return -ENODEV;
378
379 i = ofnode_stringlist_search(node, "clock-names", "core");
380 if (i < 0)
381 return -ENODEV;
382
383 ret = ofnode_parse_phandle_with_args(node, "clocks",
384 "#clock-cells",
385 0, i,
386 &phandle_args);
387 if (ret) {
388 printf("Couldn't get the clock node, ret = %d\n", ret);
389 return ret;
390 }
391
392 vd->dev_id = phandle_args.args[0];
393 vd->clk_id = phandle_args.args[1];
394
395 debug("%s: MPU dev_id: %d, clk_id: %d", __func__,
396 vd->dev_id, vd->clk_id);
397 }
398
Tero Kristo887dde52019-10-24 15:00:46 +0530399 if (!(readl(AM6_VTM_DEVINFO(vd->id)) &
400 AM6_VTM_AVS0_SUPPORTED)) {
401 dev_warn(dev, "AVS-class 0 not supported for VD%d\n",
402 vd->id);
403 continue;
404 }
405
406 for (opp_id = 0; opp_id < NUM_OPPS; opp_id++) {
407 opp = &vd->opps[opp_id];
408
409 if (!opp->freq)
410 continue;
411
412 volt = priv->vd_config->efuse_xlate(priv, vd->id,
413 opp_id);
414 if (volt)
415 opp->volt = volt;
416 }
417 }
418
419 for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
420 if (vd->flags & VD_FLAG_INIT_DONE)
421 continue;
422
Manorit Chawdhry0c60ab82024-10-15 16:22:18 +0530423 ret = k3_avs_program_voltage(priv, vd, vd->opp);
424 if (ret)
425 dev_warn(dev, "Could not program AVS voltage for VD%d, vd->opp=%d, ret=%d\n",
426 vd->id, vd->opp, ret);
Tero Kristo887dde52019-10-24 15:00:46 +0530427 }
428
Udit Kumarbac62152023-10-19 12:57:53 +0530429 if (!device_is_compatible(priv->dev, "ti,am654-avs"))
430 k3_avs_program_tshut(priv);
431
Tero Kristo887dde52019-10-24 15:00:46 +0530432 return 0;
433}
434
435static struct vd_data am654_vd_data[] = {
436 {
437 .id = AM6_VDD_CORE,
438 .dev_id = 82, /* AM6_DEV_CBASS0 */
439 .clk_id = 0, /* main sysclk0 */
440 .opp = AM6_OPP_NOM,
441 .opps = {
442 [AM6_OPP_NOM] = {
443 .volt = 1000000,
444 .freq = 250000000, /* CBASS0 */
445 },
446 },
447 },
448 {
449 .id = AM6_VDD_MPU0,
450 .dev_id = 202, /* AM6_DEV_COMPUTE_CLUSTER_A53_0 */
451 .clk_id = 0, /* ARM clock */
452 .opp = AM6_OPP_NOM,
453 .opps = {
454 [AM6_OPP_NOM] = {
Tero Kristo75774302020-02-14 09:05:10 +0200455 .volt = 1100000,
Tero Kristo887dde52019-10-24 15:00:46 +0530456 .freq = 800000000,
457 },
458 [AM6_OPP_OD] = {
Tero Kristo75774302020-02-14 09:05:10 +0200459 .volt = 1200000,
Tero Kristo887dde52019-10-24 15:00:46 +0530460 .freq = 1000000000,
461 },
462 [AM6_OPP_TURBO] = {
Tero Kristo75774302020-02-14 09:05:10 +0200463 .volt = 1240000,
Tero Kristo887dde52019-10-24 15:00:46 +0530464 .freq = 1100000000,
465 },
466 },
467 },
468 {
469 .id = AM6_VDD_MPU1,
470 .opp = AM6_OPP_NOM,
471 .dev_id = 204, /* AM6_DEV_COMPUTE_CLUSTER_A53_2 */
472 .clk_id = 0, /* ARM clock */
473 .opps = {
474 [AM6_OPP_NOM] = {
Tero Kristo75774302020-02-14 09:05:10 +0200475 .volt = 1100000,
Tero Kristo887dde52019-10-24 15:00:46 +0530476 .freq = 800000000,
477 },
478 [AM6_OPP_OD] = {
Tero Kristo75774302020-02-14 09:05:10 +0200479 .volt = 1200000,
Tero Kristo887dde52019-10-24 15:00:46 +0530480 .freq = 1000000000,
481 },
482 [AM6_OPP_TURBO] = {
Tero Kristo75774302020-02-14 09:05:10 +0200483 .volt = 1240000,
Tero Kristo887dde52019-10-24 15:00:46 +0530484 .freq = 1100000000,
485 },
486 },
487 },
488 { .id = -1 },
489};
490
Keerthyfda68e42019-10-24 15:00:49 +0530491static struct vd_data j721e_vd_data[] = {
492 {
493 .id = J721E_VDD_MPU,
494 .opp = AM6_OPP_NOM,
Manorit Chawdhry9234ab92024-10-15 16:22:20 +0530495 /*
496 * XXX: DEPRECATION WARNING: Around 2 u-boot versions
497 *
498 * These values will be picked up from DT, kept for backward
499 * compatibility
500 */
Keerthyfda68e42019-10-24 15:00:49 +0530501 .dev_id = 202, /* J721E_DEV_A72SS0_CORE0 */
502 .clk_id = 2, /* ARM clock */
503 .opps = {
504 [AM6_OPP_NOM] = {
505 .volt = 880000, /* TBD in DM */
506 .freq = 2000000000,
507 },
508 },
509 },
510 { .id = -1 },
511};
512
513static struct vd_config j721e_vd_config = {
514 .efuse_xlate = am6_efuse_xlate,
515 .vds = j721e_vd_data,
516};
517
Tero Kristo887dde52019-10-24 15:00:46 +0530518static struct vd_config am654_vd_config = {
519 .efuse_xlate = am6_efuse_xlate,
520 .vds = am654_vd_data,
521};
522
523static const struct udevice_id k3_avs_ids[] = {
524 { .compatible = "ti,am654-avs", .data = (ulong)&am654_vd_config },
Keerthyfda68e42019-10-24 15:00:49 +0530525 { .compatible = "ti,j721e-avs", .data = (ulong)&j721e_vd_config },
Reid Tonking8cc3ca32023-09-07 13:06:35 -0500526 { .compatible = "ti,j721e-vtm", .data = (ulong)&j721e_vd_config },
527 { .compatible = "ti,j7200-vtm", .data = (ulong)&j721e_vd_config },
Tero Kristo887dde52019-10-24 15:00:46 +0530528 {}
529};
530
531U_BOOT_DRIVER(k3_avs) = {
532 .name = "k3_avs",
533 .of_match = k3_avs_ids,
534 .id = UCLASS_MISC,
535 .probe = k3_avs_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700536 .priv_auto = sizeof(struct k3_avs_privdata),
Tero Kristo887dde52019-10-24 15:00:46 +0530537};