sunxi_h616_dtb.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2024, ARM Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. * Amend the device tree to adjust the L2 cache size, which is different
  7. * between the revisions of the H616 chips: earlier versions have 256 KB of L2,
  8. * later versions 1 MB.
  9. * Read the cache ID registers and adjust the size and number of sets entries
  10. * in the L2 cache DT node.
  11. */
  12. #include <common/fdt_wrappers.h>
  13. #include <lib/utils_def.h>
  14. #include <libfdt.h>
  15. #define CACHE_L1D 0x0
  16. #define CACHE_L1I 0x1
  17. #define CACHE_L2U 0x2
  18. #define CCSIDR_SETS_SHIFT 13
  19. #define CCSIDR_SETS_MASK GENMASK(14, 0)
  20. #define CCSIDR_ASSOC_SHIFT 3
  21. #define CCSIDR_ASSOC_MASK GENMASK(9, 0)
  22. #define CCSIDR_LSIZE_SHIFT 0
  23. #define CCSIDR_LSIZE_MASK GENMASK(2, 0)
  24. static uint32_t armv8_get_ccsidr(unsigned int sel)
  25. {
  26. uint32_t reg;
  27. __asm__ volatile ("msr CSSELR_EL1, %0\n" :: "r" (sel));
  28. __asm__ volatile ("mrs %0, CCSIDR_EL1\n" : "=r" (reg));
  29. return reg;
  30. }
  31. void sunxi_soc_fdt_fixup(void *dtb)
  32. {
  33. int node = fdt_path_offset(dtb, "/cpus/cpu@0");
  34. uint32_t phandle, ccsidr, cell;
  35. int sets, line_size, assoc;
  36. int ret;
  37. if (node < 0) {
  38. return;
  39. }
  40. ret = fdt_read_uint32(dtb, node, "next-level-cache", &phandle);
  41. if (ret != 0) {
  42. return;
  43. }
  44. node = fdt_node_offset_by_phandle(dtb, phandle);
  45. if (node < 0) {
  46. return;
  47. }
  48. ccsidr = armv8_get_ccsidr(CACHE_L2U);
  49. sets = ((ccsidr >> CCSIDR_SETS_SHIFT) & CCSIDR_SETS_MASK) + 1;
  50. line_size = 16U << ((ccsidr >> CCSIDR_LSIZE_SHIFT) & CCSIDR_LSIZE_MASK);
  51. assoc = ((ccsidr >> CCSIDR_ASSOC_SHIFT) & CCSIDR_ASSOC_MASK) + 1;
  52. cell = cpu_to_fdt32(sets);
  53. fdt_setprop(dtb, node, "cache-sets", &cell, sizeof(cell));
  54. cell = cpu_to_fdt32(line_size);
  55. fdt_setprop(dtb, node, "cache-line-size", &cell, sizeof(cell));
  56. cell = cpu_to_fdt32(sets * assoc * line_size);
  57. fdt_setprop(dtb, node, "cache-size", &cell, sizeof(cell));
  58. }