2
0

002-0028-cpu-add-basic-cpu-driver-for-MediaTek-ARM-chips.patch 3.2 KB

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