This repository has been archived by the owner on Jan 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
sun50i-cpufreq-nvmem.c
543 lines (465 loc) · 11.5 KB
/
sun50i-cpufreq-nvmem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
// SPDX-License-Identifier: GPL-2.0
/*
* The sun50i-cpufreq-nvmem driver reads the efuse value from the SoC to
* provide the OPP framework with required information.
*
* Copyright (C) 2020 frank@allwinnertech.com
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/nvmem-consumer.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/pm_opp.h>
#include <linux/slab.h>
#include <linux/sunxi-sid.h>
#define MAX_NAME_LEN 3
#define SUN50IW9_ICPU_MASK GENMASK(9, 0)
static struct platform_device *cpufreq_dt_pdev, *sun50i_cpufreq_pdev;
struct cpufreq_nvmem_data {
u32 nv_speed;
u32 nv_Icpu;
u32 nv_bin;
u32 nv_bin_ext;
u32 version;
char name[MAX_NAME_LEN];
};
static struct cpufreq_nvmem_data ver_data;
struct cpufreq_soc_data {
void (*nvmem_xlate)(u32 *versions, char *name);
bool has_nvmem_Icpu;
bool has_nvmem_bin;
bool has_nvmem_extend_bin;
};
static int sun50i_nvmem_get_data(char *cell_name, u32 *data)
{
struct nvmem_cell *cell_nvmem;
size_t len;
u8 *cell_value;
u32 tmp_data = 0;
u32 i;
struct device_node *np;
struct device *cpu_dev;
int ret = 0;
cpu_dev = get_cpu_device(0);
if (!cpu_dev)
return -ENODEV;
np = dev_pm_opp_of_get_opp_desc_node(cpu_dev);
if (!np)
return -ENOENT;
ret = of_device_is_compatible(np,
"allwinner,sun50i-operating-points");
if (!ret) {
of_node_put(np);
return -ENOENT;
}
cell_nvmem = of_nvmem_cell_get(np, cell_name);
of_node_put(np);
if (IS_ERR(cell_nvmem)) {
if (PTR_ERR(cell_nvmem) != -EPROBE_DEFER)
pr_err("Could not get nvmem cell: %ld\n",
PTR_ERR(cell_nvmem));
return PTR_ERR(cell_nvmem);
}
cell_value = nvmem_cell_read(cell_nvmem, &len);
nvmem_cell_put(cell_nvmem);
if (IS_ERR(cell_value))
return PTR_ERR(cell_value);
if (len > 4) {
pr_err("Invalid nvmem cell length\n");
ret = -EINVAL;
} else {
for (i = 0; i < len; i++)
tmp_data |= ((u32)cell_value[i] << (i * 8));
*data = tmp_data;
}
kfree(cell_value);
return 0;
}
static void sun50iw9_icpu_xlate(char *prop_name, char *name, u32 i_data)
{
int value = 0;
i_data &= SUN50IW9_ICPU_MASK;
if ((i_data >= 0) && (i_data < 93))
value = 0;
/* 150 is temp munber */
else if ((i_data >= 93) && (i_data < 150))
value = 1;
else if ((i_data >= 150) && (i_data < 271))
value = 2;
snprintf(name, MAX_NAME_LEN, "%s%d", prop_name, value);
}
static void sun50iw9_nvmem_xlate(u32 *versions, char *name)
{
switch (ver_data.nv_speed) {
case 0x2400:
case 0x7400:
sun50iw9_icpu_xlate("a", name, ver_data.nv_Icpu);
break;
case 0x2c00:
case 0x7c00:
sun50iw9_icpu_xlate("b", name, ver_data.nv_Icpu);
break;
case 0x5000:
case 0x5400:
case 0x6c00:
*versions = 0b0001;
break;
case 0x5c00:
default:
*versions = 0b0010;
}
}
static struct cpufreq_soc_data sun50iw9_soc_data = {
.nvmem_xlate = sun50iw9_nvmem_xlate,
.has_nvmem_Icpu = true,
};
static void sun50iw10_bin_xlate(bool high_speed, char *name, u32 bin)
{
int value = 0;
bool version_before_f;
unsigned int ver_bits = sunxi_get_soc_ver() & 0x7;
u32 bin_ext = ver_data.nv_bin_ext;
bin >>= 12;
bin_ext >>= 31;
if (ver_bits == 0 || ver_bits == 3 || ver_bits == 4)
version_before_f = true;
else
version_before_f = false;
if (high_speed) {
switch (bin) {
case 0b100:
if (version_before_f) {
/* ic version A-E */
value = 1;
} else {
/* ic version F and later version */
value = 3;
}
break;
default:
if (version_before_f) {
/* ic version A-E */
value = 0;
} else {
/* ic version F and later version */
value = 2;
}
}
snprintf(name, MAX_NAME_LEN, "b%d", value);
} else {
if (bin_ext && (!version_before_f)) {
value = 6;
} else {
switch (bin) {
case 0b100:
if (version_before_f) {
/* ic version A-E */
value = 2;
} else {
/* ic version F and later version */
value = 5;
}
break;
case 0b010:
if (version_before_f) {
/* ic version A-E */
value = 1;
} else {
/* ic version F and later version */
value = 4;
}
break;
default:
if (version_before_f) {
/* ic version A-E */
value = 0;
} else {
/* ic version F and later version */
value = 3;
}
}
}
snprintf(name, MAX_NAME_LEN, "a%d", value);
}
}
static void sun50iw10_nvmem_xlate(u32 *versions, char *name)
{
unsigned int ver_bits = sunxi_get_soc_ver() & 0x7;
switch (ver_data.nv_speed) {
case 0x0200:
case 0x0600:
case 0x0620:
case 0x0640:
case 0x0800:
case 0x1000:
case 0x1400:
case 0x2000:
case 0x4000:
if (ver_bits == 0 || ver_bits == 3 || ver_bits == 4) {
/* ic version A-E */
*versions = 0b0100;
} else {
/* ic version F and later version */
*versions = 0b0010;
}
sun50iw10_bin_xlate(true, name, ver_data.nv_bin);
break;
case 0x0400:
default:
*versions = 0b0001;
sun50iw10_bin_xlate(false, name, ver_data.nv_bin);
}
}
static struct cpufreq_soc_data sun50iw10_soc_data = {
.nvmem_xlate = sun50iw10_nvmem_xlate,
.has_nvmem_bin = true,
.has_nvmem_extend_bin = true,
};
static void sun8iw20_bin_xlate(bool bin_select, char *name, u32 nv_bin)
{
int value = 0;
u32 bin = (nv_bin >> 12) & 0xf;
if (bin_select) {
if (bin <= 1)
value = 1;
else
value = 0;
/*BGA use axx in VF table*/
snprintf(name, MAX_NAME_LEN, "a%d", value);
} else {
value = 0;
/*QFN use bxx in VF table*/
snprintf(name, MAX_NAME_LEN, "b%d", value);
}
}
static void sun8iw20_nvmem_xlate(u32 *versions, char *name)
{
switch (ver_data.nv_speed) {
case 0x6000:
sun8iw20_bin_xlate(false, name, ver_data.nv_bin);
*versions = 0b0010;
break;
case 0x6400:
case 0x7000:
case 0x7c00:
default:
sun8iw20_bin_xlate(true, name, ver_data.nv_bin);
*versions = 0b0001;
break;
}
pr_debug("sun8iw20 match vf:%s, mark:0x%x\n", name, ver_data.nv_speed);
}
static struct cpufreq_soc_data sun8iw20_soc_data = {
.nvmem_xlate = sun8iw20_nvmem_xlate,
.has_nvmem_bin = true,
};
static void sun20iw1_bin_xlate(bool bin_select, bool high_speed, char *name, u32 nv_bin)
{
int value = 0;
u32 bin = (nv_bin >> 12) & 0xf;
if (bin_select) {
if (bin <= 1)
value = 1;
else
value = 0;
/*BGA use axx in VF table*/
snprintf(name, MAX_NAME_LEN, "a%d", value);
} else {
if (high_speed)
value = 1;
else
value = 0;
/*QFN use bxx in VF table*/
snprintf(name, MAX_NAME_LEN, "b%d", value);
}
}
static void sun20iw1_nvmem_xlate(u32 *versions, char *name)
{
switch (ver_data.nv_speed) {
case 0x5e00:
sun20iw1_bin_xlate(false, false, name, ver_data.nv_bin);
*versions = 0b0010;
break;
case 0x5c00:
case 0x7400:
sun20iw1_bin_xlate(false, true, name, ver_data.nv_bin);
*versions = 0b0001;
break;
case 0x5000:
default:
sun20iw1_bin_xlate(true, true, name, ver_data.nv_bin);
*versions = 0b0001;
}
pr_debug("sun20iw1 match vf:%s, mark:0x%x\n", name, ver_data.nv_speed);
}
static struct cpufreq_soc_data sun20iw1_soc_data = {
.nvmem_xlate = sun20iw1_nvmem_xlate,
.has_nvmem_bin = true,
};
/**
* sun50i_cpufreq_get_efuse() - Determine speed grade from efuse value
* @versions: Set to the value parsed from efuse
*
* Returns 0 if success.
*/
static int sun50i_cpufreq_get_efuse(const struct cpufreq_soc_data *soc_data,
u32 *versions, char *name)
{
int ret;
ret = sun50i_nvmem_get_data("speed", &ver_data.nv_speed);
if (ret)
return ret;
if (soc_data->has_nvmem_Icpu) {
ret = sun50i_nvmem_get_data("Icpu", &ver_data.nv_Icpu);
if (ret)
return ret;
}
if (soc_data->has_nvmem_bin) {
ret = sun50i_nvmem_get_data("bin", &ver_data.nv_bin);
if (ret)
return ret;
}
if (soc_data->has_nvmem_extend_bin) {
ret = sun50i_nvmem_get_data("bin_ext", &ver_data.nv_bin_ext);
if (ret)
return ret;
}
soc_data->nvmem_xlate(versions, name);
return 0;
};
static int sun50i_cpufreq_nvmem_probe(struct platform_device *pdev)
{
const struct of_device_id *match;
struct opp_table **opp_tables;
unsigned int cpu;
int ret;
opp_tables = kcalloc(num_possible_cpus(), sizeof(*opp_tables),
GFP_KERNEL);
if (!opp_tables)
return -ENOMEM;
match = dev_get_platdata(&pdev->dev);
if (!match)
return -EINVAL;
ret = sun50i_cpufreq_get_efuse(match->data,
&ver_data.version, ver_data.name);
if (ret)
return ret;
for_each_possible_cpu(cpu) {
struct device *cpu_dev = get_cpu_device(cpu);
if (!cpu_dev) {
ret = -ENODEV;
goto free_opp;
}
if (strlen(ver_data.name)) {
opp_tables[cpu] = dev_pm_opp_set_prop_name(cpu_dev,
ver_data.name);
if (IS_ERR(opp_tables[cpu])) {
ret = PTR_ERR(opp_tables[cpu]);
pr_err("Failed to set prop name\n");
goto free_opp;
}
}
if (ver_data.version) {
opp_tables[cpu] = dev_pm_opp_set_supported_hw(cpu_dev,
&ver_data.version, 1);
if (IS_ERR(opp_tables[cpu])) {
ret = PTR_ERR(opp_tables[cpu]);
pr_err("Failed to set hw\n");
goto free_opp;
}
}
}
cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
NULL, 0);
if (!IS_ERR(cpufreq_dt_pdev)) {
platform_set_drvdata(pdev, opp_tables);
return 0;
}
ret = PTR_ERR(cpufreq_dt_pdev);
pr_err("Failed to register platform device\n");
free_opp:
for_each_possible_cpu(cpu) {
if (IS_ERR_OR_NULL(opp_tables[cpu]))
break;
if (strlen(ver_data.name))
dev_pm_opp_put_prop_name(opp_tables[cpu]);
if (ver_data.version)
dev_pm_opp_put_supported_hw(opp_tables[cpu]);
}
kfree(opp_tables);
return ret;
}
static int sun50i_cpufreq_nvmem_remove(struct platform_device *pdev)
{
struct opp_table **opp_tables = platform_get_drvdata(pdev);
unsigned int cpu;
platform_device_unregister(cpufreq_dt_pdev);
for_each_possible_cpu(cpu) {
if (IS_ERR_OR_NULL(opp_tables[cpu]))
break;
if (strlen(ver_data.name))
dev_pm_opp_put_prop_name(opp_tables[cpu]);
if (ver_data.version)
dev_pm_opp_put_supported_hw(opp_tables[cpu]);
}
kfree(opp_tables);
return 0;
}
static struct platform_driver sun50i_cpufreq_driver = {
.probe = sun50i_cpufreq_nvmem_probe,
.remove = sun50i_cpufreq_nvmem_remove,
.driver = {
.name = "sun50i-cpufreq-nvmem",
},
};
static const struct of_device_id sun50i_cpufreq_match_list[] = {
{ .compatible = "arm,sun50iw9p1", .data = &sun50iw9_soc_data, },
{ .compatible = "arm,sun50iw10p1", .data = &sun50iw10_soc_data, },
{ .compatible = "arm,sun8iw20p1", .data = &sun8iw20_soc_data, },
{ .compatible = "arm,sun20iw1p1", .data = &sun20iw1_soc_data, },
{}
};
static const struct of_device_id *sun50i_cpufreq_match_node(void)
{
const struct of_device_id *match;
struct device_node *np;
np = of_find_node_by_path("/");
match = of_match_node(sun50i_cpufreq_match_list, np);
of_node_put(np);
return match;
}
/*
* Since the driver depends on nvmem drivers, which may return EPROBE_DEFER,
* all the real activity is done in the probe, which may be defered as well.
* The init here is only registering the driver and the platform device.
*/
static int __init sun50i_cpufreq_init(void)
{
const struct of_device_id *match;
int ret;
match = sun50i_cpufreq_match_node();
if (!match)
return -ENODEV;
ret = platform_driver_register(&sun50i_cpufreq_driver);
if (unlikely(ret < 0))
return ret;
sun50i_cpufreq_pdev = platform_device_register_data(NULL,
"sun50i-cpufreq-nvmem",
-1, match,
sizeof(*match));
ret = PTR_ERR_OR_ZERO(sun50i_cpufreq_pdev);
if (ret == 0)
return 0;
platform_driver_unregister(&sun50i_cpufreq_driver);
return ret;
}
module_init(sun50i_cpufreq_init);
static void __exit sun50i_cpufreq_exit(void)
{
platform_device_unregister(sun50i_cpufreq_pdev);
platform_driver_unregister(&sun50i_cpufreq_driver);
}
module_exit(sun50i_cpufreq_exit);
MODULE_DESCRIPTION("Sun50i cpufreq driver");
MODULE_LICENSE("GPL v2");