uboot-envtools: update to 2023.01
[openwrt/staging/dedeckeh.git] / package / boot / uboot-mediatek / patches / 002-0028-cpu-add-basic-cpu-driver-for-MediaTek-ARM-chips.patch
1 From e3c707d23a3a5bc1ba9b8c03731a32c3714ae56a Mon Sep 17 00:00:00 2001
2 From: Weijie Gao <weijie.gao@mediatek.com>
3 Date: Wed, 31 Aug 2022 19:05:20 +0800
4 Subject: [PATCH 28/32] cpu: add basic cpu driver for MediaTek ARM chips
5
6 Add basic CPU driver used to retrieve CPU model information.
7
8 Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
9 ---
10 drivers/cpu/Makefile | 1 +
11 drivers/cpu/mtk_cpu.c | 106 ++++++++++++++++++++++++++++++++++++++++++
12 2 files changed, 107 insertions(+)
13 create mode 100644 drivers/cpu/mtk_cpu.c
14
15 --- a/drivers/cpu/Makefile
16 +++ b/drivers/cpu/Makefile
17 @@ -9,6 +9,7 @@ obj-$(CONFIG_CPU) += cpu-uclass.o
18 obj-$(CONFIG_ARCH_BMIPS) += bmips_cpu.o
19 obj-$(CONFIG_ARCH_IMX8) += imx8_cpu.o
20 obj-$(CONFIG_ARCH_AT91) += at91_cpu.o
21 +obj-$(CONFIG_ARCH_MEDIATEK) += mtk_cpu.o
22 obj-$(CONFIG_CPU_MPC83XX) += mpc83xx_cpu.o
23 obj-$(CONFIG_CPU_RISCV) += riscv_cpu.o
24 obj-$(CONFIG_CPU_MICROBLAZE) += microblaze_cpu.o
25 --- /dev/null
26 +++ b/drivers/cpu/mtk_cpu.c
27 @@ -0,0 +1,106 @@
28 +// SPDX-License-Identifier: GPL-2.0
29 +/*
30 + * Copyright (C) 2022 MediaTek Inc. All rights reserved.
31 + *
32 + * Author: Weijie Gao <weijie.gao@mediatek.com>
33 + */
34 +
35 +#include <linux/types.h>
36 +#include <cpu.h>
37 +#include <dm.h>
38 +#include <fdt_support.h>
39 +#include <mapmem.h>
40 +#include <asm/global_data.h>
41 +#include <linux/io.h>
42 +
43 +DECLARE_GLOBAL_DATA_PTR;
44 +
45 +struct mtk_cpu_plat {
46 + void __iomem *hwver_base;
47 +};
48 +
49 +static int mtk_cpu_get_desc(const struct udevice *dev, char *buf, int size)
50 +{
51 + struct mtk_cpu_plat *plat = dev_get_plat(dev);
52 +
53 + snprintf(buf, size, "MediaTek MT%04X", readl(plat->hwver_base));
54 +
55 + return 0;
56 +}
57 +
58 +static int mtk_cpu_get_count(const struct udevice *dev)
59 +{
60 + return 1;
61 +}
62 +
63 +static int mtk_cpu_get_vendor(const struct udevice *dev, char *buf, int size)
64 +{
65 + snprintf(buf, size, "MediaTek");
66 +
67 + return 0;
68 +}
69 +
70 +static int mtk_cpu_probe(struct udevice *dev)
71 +{
72 + struct mtk_cpu_plat *plat = dev_get_plat(dev);
73 + const void *fdt = gd->fdt_blob, *reg;
74 + int offset, parent, len, na, ns;
75 + u64 addr;
76 +
77 + if (!fdt)
78 + return -ENODEV;
79 +
80 + offset = fdt_path_offset(fdt, "/hwver");
81 + if (offset < 0)
82 + return -ENODEV;
83 +
84 + parent = fdt_parent_offset(fdt, offset);
85 + if (parent < 0)
86 + return -ENODEV;
87 +
88 + na = fdt_address_cells(fdt, parent);
89 + if (na < 1)
90 + return -ENODEV;
91 +
92 + ns = fdt_size_cells(gd->fdt_blob, parent);
93 + if (ns < 0)
94 + return -ENODEV;
95 +
96 + reg = fdt_getprop(gd->fdt_blob, offset, "reg", &len);
97 + if (!reg)
98 + return -ENODEV;
99 +
100 + if (ns)
101 + addr = fdt_translate_address(fdt, offset, reg);
102 + else
103 + addr = fdt_read_number(reg, na);
104 +
105 + plat->hwver_base = map_sysmem(addr, 0);
106 + if (!plat->hwver_base)
107 + return -EINVAL;
108 +
109 + return 0;
110 +}
111 +
112 +static const struct cpu_ops mtk_cpu_ops = {
113 + .get_desc = mtk_cpu_get_desc,
114 + .get_count = mtk_cpu_get_count,
115 + .get_vendor = mtk_cpu_get_vendor,
116 +};
117 +
118 +static const struct udevice_id mtk_cpu_ids[] = {
119 + { .compatible = "arm,cortex-a7" },
120 + { .compatible = "arm,cortex-a53" },
121 + { .compatible = "arm,cortex-a73" },
122 + { /* sentinel */ }
123 +};
124 +
125 +U_BOOT_DRIVER(cpu_mtk) = {
126 + .name = "mtk-cpu",
127 + .id = UCLASS_CPU,
128 + .of_match = mtk_cpu_ids,
129 + .ops = &mtk_cpu_ops,
130 + .probe = mtk_cpu_probe,
131 + .plat_auto = sizeof(struct mtk_cpu_plat),
132 + .flags = DM_FLAG_PRE_RELOC,
133 +};