summaryrefslogtreecommitdiff
path: root/drivers/cpu/at91_cpu.c
blob: 34a3f61c7e95709afb42a4dd2a35e05ed075714f (plain)
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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2020 Microchip Technology Inc. and its subsidiaries
 *
 * Author: Claudiu Beznea <claudiu.beznea@microchip.com>
 */

#include <common.h>
#include <cpu.h>
#include <dm.h>
#include <div64.h>
#include <linux/clk-provider.h>

struct at91_cpu_plat {
	const char *name;
	ulong cpufreq_mhz;
	ulong mckfreq_mhz;
	ulong xtalfreq_mhz;
};

extern char *get_cpu_name(void);

const char *at91_cpu_get_name(void)
{
	return get_cpu_name();
}

int at91_cpu_get_desc(const struct udevice *dev, char *buf, int size)
{
	struct at91_cpu_plat *plat = dev_get_plat(dev);

	snprintf(buf, size, "%s\n"
		 "Crystal frequency: %8lu MHz\n"
		 "CPU clock        : %8lu MHz\n"
		 "Master clock     : %8lu MHz\n",
		 plat->name, plat->xtalfreq_mhz, plat->cpufreq_mhz,
		 plat->mckfreq_mhz);

	return 0;
}

static int at91_cpu_get_info(const struct udevice *dev, struct cpu_info *info)
{
	struct at91_cpu_plat *plat = dev_get_plat(dev);

	info->cpu_freq = plat->cpufreq_mhz * 1000000;
	info->features = BIT(CPU_FEAT_L1_CACHE);

	return 0;
}

static int at91_cpu_get_count(const struct udevice *dev)
{
	return 1;
}

static int at91_cpu_get_vendor(const struct udevice *dev,  char *buf, int size)
{
	snprintf(buf, size, "Microchip Technology Inc.");

	return 0;
}

static const struct cpu_ops at91_cpu_ops = {
	.get_desc	= at91_cpu_get_desc,
	.get_info	= at91_cpu_get_info,
	.get_count	= at91_cpu_get_count,
	.get_vendor	= at91_cpu_get_vendor,
};

static const struct udevice_id at91_cpu_ids[] = {
	{ .compatible = "arm,cortex-a7" },
	{ .compatible = "arm,arm926ej-s" },
	{ /* Sentinel. */ }
};

static int at91_cpu_probe(struct udevice *dev)
{
	struct at91_cpu_plat *plat = dev_get_plat(dev);
	struct clk clk;
	ulong rate;
	int ret;

	ret = clk_get_by_index(dev, 0, &clk);
	if (ret)
		return ret;

	rate  = clk_get_rate(&clk);
	if (!rate)
		return -ENOTSUPP;
	plat->cpufreq_mhz = DIV_ROUND_CLOSEST_ULL(rate, 1000000);

	ret = clk_get_by_index(dev, 1, &clk);
	if (ret)
		return ret;

	rate = clk_get_rate(&clk);
	if (!rate)
		return -ENOTSUPP;
	plat->mckfreq_mhz = DIV_ROUND_CLOSEST_ULL(rate, 1000000);

	ret = clk_get_by_index(dev, 2, &clk);
	if (ret)
		return ret;

	rate = clk_get_rate(&clk);
	if (!rate)
		return -ENOTSUPP;
	plat->xtalfreq_mhz = DIV_ROUND_CLOSEST_ULL(rate, 1000000);

	plat->name = get_cpu_name();

	return 0;
}

U_BOOT_DRIVER(cpu_at91_drv) = {
	.name		= "at91-cpu",
	.id		= UCLASS_CPU,
	.of_match	= at91_cpu_ids,
	.ops		= &at91_cpu_ops,
	.probe		= at91_cpu_probe,
	.plat_auto	= sizeof(struct at91_cpu_plat),
	.flags		= DM_FLAG_PRE_RELOC,
};