blob: c19c3c0646b793018a4c943abd4ddcaef8eb89c2 [file] [log] [blame]
Tero Kristo9d233b42019-10-24 15:00:46 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Texas Instruments' K3 Clas 0 Adaptive Voltage Scaling driver
4 *
5 * Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/
6 * Tero Kristo <t-kristo@ti.com>
7 *
8 */
9
10#include <common.h>
11#include <dm.h>
12#include <errno.h>
13#include <asm/io.h>
14#include <i2c.h>
15#include <k3-avs.h>
16#include <power/regulator.h>
17
18#define AM6_VTM_DEVINFO(i) (priv->base + 0x100 + 0x20 * (i))
19#define AM6_VTM_OPPVID_VD(i) (priv->base + 0x104 + 0x20 * (i))
20
21#define AM6_VTM_AVS0_SUPPORTED BIT(12)
22
23#define AM6_VTM_OPP_SHIFT(opp) (8 * (opp))
24#define AM6_VTM_OPP_MASK 0xff
25
26#define VD_FLAG_INIT_DONE BIT(0)
27
28struct k3_avs_privdata {
29 void *base;
30 struct vd_config *vd_config;
31};
32
33struct opp {
34 u32 freq;
35 u32 volt;
36};
37
38struct vd_data {
39 int id;
40 u8 opp;
41 u8 flags;
42 int dev_id;
43 int clk_id;
44 struct opp opps[NUM_OPPS];
45 struct udevice *supply;
46};
47
48struct vd_config {
49 struct vd_data *vds;
50 u32 (*efuse_xlate)(struct k3_avs_privdata *priv, int idx, int opp);
51};
52
53static struct k3_avs_privdata *k3_avs_priv;
54
55/**
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
60 *
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
63 * should be used.
64 *
65 * Efuse val to volt conversion logic:
66 *
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
71 * val 0 is invalid
72 */
73static u32 am6_efuse_xlate(struct k3_avs_privdata *priv, int idx, int opp)
74{
75 u32 val = readl(AM6_VTM_OPPVID_VD(idx));
76
77 val >>= AM6_VTM_OPP_SHIFT(opp);
78 val &= AM6_VTM_OPP_MASK;
79
80 if (!val)
81 return 0;
82
83 if (val > 171)
84 return 1660000 + 20000 * (val - 171);
85
86 if (val > 115)
87 return 1100000 + 10000 * (val - 115);
88
89 if (val > 15)
90 return 600000 + 5000 * (val - 15);
91
92 return 300000 + 20000 * val;
93}
94
95static int k3_avs_program_voltage(struct k3_avs_privdata *priv,
96 struct vd_data *vd,
97 int opp_id)
98{
99 u32 volt = vd->opps[opp_id].volt;
100 struct vd_data *vd2;
101
102 if (!vd->supply)
103 return -ENODEV;
104
105 vd->opp = opp_id;
106 vd->flags |= VD_FLAG_INIT_DONE;
107
108 /* Take care of ganged rails and pick the Max amongst them*/
109 for (vd2 = priv->vd_config->vds; vd2->id >= 0; vd2++) {
110 if (vd == vd2)
111 continue;
112
113 if (vd2->supply != vd->supply)
114 continue;
115
116 if (vd2->opps[vd2->opp].volt > volt)
117 volt = vd2->opps[vd2->opp].volt;
118
119 vd2->flags |= VD_FLAG_INIT_DONE;
120 }
121
122 return regulator_set_value(vd->supply, volt);
123}
124
125static struct vd_data *get_vd(struct k3_avs_privdata *priv, int idx)
126{
127 struct vd_data *vd;
128
129 for (vd = priv->vd_config->vds; vd->id >= 0 && vd->id != idx; vd++)
130 ;
131
132 if (vd->id < 0)
133 return NULL;
134
135 return vd;
136}
137
138/**
139 * k3_avs_set_opp: Sets the voltage for an arbitrary VD rail
140 * @dev: AVS device
141 * @vdd_id: voltage domain ID
142 * @opp_id: OPP ID
143 *
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.
147 */
148int k3_avs_set_opp(struct udevice *dev, int vdd_id, int opp_id)
149{
150 struct k3_avs_privdata *priv = dev_get_priv(dev);
151 struct vd_data *vd;
152
153 vd = get_vd(priv, vdd_id);
154 if (!vd)
155 return -EINVAL;
156
157 return k3_avs_program_voltage(priv, vd, opp_id);
158}
159
160static int match_opp(struct vd_data *vd, u32 freq)
161{
162 struct opp *opp;
163 int opp_id;
164
165 for (opp_id = 0; opp_id < NUM_OPPS; opp_id++) {
166 opp = &vd->opps[opp_id];
167 if (opp->freq == freq)
168 return opp_id;
169 }
170
171 printf("No matching OPP found for freq %d.\n", freq);
172
173 return -EINVAL;
174}
175
176/**
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
181 *
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.
186 */
187int k3_avs_notify_freq(int dev_id, int clk_id, u32 freq)
188{
189 int opp_id;
190 struct k3_avs_privdata *priv = k3_avs_priv;
191 struct vd_data *vd;
192
193 for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
194 if (vd->dev_id != dev_id || vd->clk_id != clk_id)
195 continue;
196
197 opp_id = match_opp(vd, freq);
198 if (opp_id < 0)
199 return opp_id;
200
201 vd->opp = opp_id;
202 return k3_avs_program_voltage(priv, vd, opp_id);
203 }
204
205 return -EINVAL;
206}
207
208static int k3_avs_configure(struct udevice *dev, struct k3_avs_privdata *priv)
209{
210 struct vd_config *conf;
211 int ret;
212 char pname[20];
213 struct vd_data *vd;
214
215 conf = (void *)dev_get_driver_data(dev);
216
217 priv->vd_config = conf;
218
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);
222 if (ret)
223 dev_warn(dev, "supply not found for VD%d.\n", vd->id);
224
225 sprintf(pname, "ti,default-opp-%d", vd->id);
226 ret = dev_read_u32_default(dev, pname, -1);
227 if (ret != -1)
228 vd->opp = ret;
229 }
230
231 return 0;
232}
233
234/**
235 * k3_avs_probe: parses VD info from VTM, and re-configures the OPP data
236 *
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.
241 */
242static int k3_avs_probe(struct udevice *dev)
243{
244 int opp_id;
245 u32 volt;
246 struct opp *opp;
247 struct k3_avs_privdata *priv;
248 struct vd_data *vd;
249 int ret;
250
251 priv = dev_get_priv(dev);
252
253 k3_avs_priv = priv;
254
255 ret = k3_avs_configure(dev, priv);
256 if (ret)
257 return ret;
258
259 priv->base = dev_read_addr_ptr(dev);
260 if (!priv->base)
261 return -ENODEV;
262
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",
267 vd->id);
268 continue;
269 }
270
271 for (opp_id = 0; opp_id < NUM_OPPS; opp_id++) {
272 opp = &vd->opps[opp_id];
273
274 if (!opp->freq)
275 continue;
276
277 volt = priv->vd_config->efuse_xlate(priv, vd->id,
278 opp_id);
279 if (volt)
280 opp->volt = volt;
281 }
282 }
283
284 for (vd = priv->vd_config->vds; vd->id >= 0; vd++) {
285 if (vd->flags & VD_FLAG_INIT_DONE)
286 continue;
287
288 k3_avs_program_voltage(priv, vd, vd->opp);
289 }
290
291 return 0;
292}
293
294static struct vd_data am654_vd_data[] = {
295 {
296 .id = AM6_VDD_CORE,
297 .dev_id = 82, /* AM6_DEV_CBASS0 */
298 .clk_id = 0, /* main sysclk0 */
299 .opp = AM6_OPP_NOM,
300 .opps = {
301 [AM6_OPP_NOM] = {
302 .volt = 1000000,
303 .freq = 250000000, /* CBASS0 */
304 },
305 },
306 },
307 {
308 .id = AM6_VDD_MPU0,
309 .dev_id = 202, /* AM6_DEV_COMPUTE_CLUSTER_A53_0 */
310 .clk_id = 0, /* ARM clock */
311 .opp = AM6_OPP_NOM,
312 .opps = {
313 [AM6_OPP_NOM] = {
314 .volt = 1000000,
315 .freq = 800000000,
316 },
317 [AM6_OPP_OD] = {
318 .volt = 1100000,
319 .freq = 1000000000,
320 },
321 [AM6_OPP_TURBO] = {
322 .volt = 1220000,
323 .freq = 1100000000,
324 },
325 },
326 },
327 {
328 .id = AM6_VDD_MPU1,
329 .opp = AM6_OPP_NOM,
330 .dev_id = 204, /* AM6_DEV_COMPUTE_CLUSTER_A53_2 */
331 .clk_id = 0, /* ARM clock */
332 .opps = {
333 [AM6_OPP_NOM] = {
334 .volt = 1000000,
335 .freq = 800000000,
336 },
337 [AM6_OPP_OD] = {
338 .volt = 1100000,
339 .freq = 1000000000,
340 },
341 [AM6_OPP_TURBO] = {
342 .volt = 1220000,
343 .freq = 1100000000,
344 },
345 },
346 },
347 { .id = -1 },
348};
349
Keerthy9a03e502019-10-24 15:00:49 +0530350static struct vd_data j721e_vd_data[] = {
351 {
352 .id = J721E_VDD_MPU,
353 .opp = AM6_OPP_NOM,
354 .dev_id = 202, /* J721E_DEV_A72SS0_CORE0 */
355 .clk_id = 2, /* ARM clock */
356 .opps = {
357 [AM6_OPP_NOM] = {
358 .volt = 880000, /* TBD in DM */
359 .freq = 2000000000,
360 },
361 },
362 },
363 { .id = -1 },
364};
365
366static struct vd_config j721e_vd_config = {
367 .efuse_xlate = am6_efuse_xlate,
368 .vds = j721e_vd_data,
369};
370
Tero Kristo9d233b42019-10-24 15:00:46 +0530371static struct vd_config am654_vd_config = {
372 .efuse_xlate = am6_efuse_xlate,
373 .vds = am654_vd_data,
374};
375
376static const struct udevice_id k3_avs_ids[] = {
377 { .compatible = "ti,am654-avs", .data = (ulong)&am654_vd_config },
Keerthy9a03e502019-10-24 15:00:49 +0530378 { .compatible = "ti,j721e-avs", .data = (ulong)&j721e_vd_config },
Tero Kristo9d233b42019-10-24 15:00:46 +0530379 {}
380};
381
382U_BOOT_DRIVER(k3_avs) = {
383 .name = "k3_avs",
384 .of_match = k3_avs_ids,
385 .id = UCLASS_MISC,
386 .probe = k3_avs_probe,
387 .priv_auto_alloc_size = sizeof(struct k3_avs_privdata),
388};