dram.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Onboard memory detection for Snapdragon boards
  4. *
  5. * (C) Copyright 2018 Ramon Fried <ramon.fried@gmail.com>
  6. *
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <log.h>
  11. #include <part.h>
  12. #include <smem.h>
  13. #include <fdt_support.h>
  14. #include <asm/arch/dram.h>
  15. #define SMEM_USABLE_RAM_PARTITION_TABLE 402
  16. #define RAM_PART_NAME_LENGTH 16
  17. #define RAM_NUM_PART_ENTRIES 32
  18. #define CATEGORY_SDRAM 0x0E
  19. #define TYPE_SYSMEM 0x01
  20. struct smem_ram_ptable_hdr {
  21. u32 magic[2];
  22. u32 version;
  23. u32 reserved;
  24. u32 len;
  25. } __attribute__ ((__packed__));
  26. struct smem_ram_ptn {
  27. char name[RAM_PART_NAME_LENGTH];
  28. u64 start;
  29. u64 size;
  30. u32 attr;
  31. u32 category;
  32. u32 domain;
  33. u32 type;
  34. u32 num_partitions;
  35. u32 reserved[3];
  36. } __attribute__ ((__packed__));
  37. struct smem_ram_ptable {
  38. struct smem_ram_ptable_hdr hdr;
  39. u32 reserved; /* Added for 8 bytes alignment of header */
  40. struct smem_ram_ptn parts[RAM_NUM_PART_ENTRIES];
  41. } __attribute__ ((__packed__));
  42. #ifndef MEMORY_BANKS_MAX
  43. #define MEMORY_BANKS_MAX 4
  44. #endif
  45. int msm_fixup_memory(void *blob)
  46. {
  47. u64 bank_start[MEMORY_BANKS_MAX];
  48. u64 bank_size[MEMORY_BANKS_MAX];
  49. size_t size;
  50. int i;
  51. int count = 0;
  52. struct udevice *smem;
  53. int ret;
  54. struct smem_ram_ptable *ram_ptable;
  55. struct smem_ram_ptn *p;
  56. ret = uclass_get_device_by_name(UCLASS_SMEM, "smem", &smem);
  57. if (ret < 0) {
  58. printf("Failed to find SMEM node. Check device tree\n");
  59. return 0;
  60. }
  61. ram_ptable = smem_get(smem, -1, SMEM_USABLE_RAM_PARTITION_TABLE, &size);
  62. if (!ram_ptable) {
  63. printf("Failed to find SMEM partition.\n");
  64. return -ENODEV;
  65. }
  66. /* Check validy of RAM */
  67. for (i = 0; i < RAM_NUM_PART_ENTRIES; i++) {
  68. p = &ram_ptable->parts[i];
  69. if (p->category == CATEGORY_SDRAM && p->type == TYPE_SYSMEM) {
  70. bank_start[count] = p->start;
  71. bank_size[count] = p->size;
  72. debug("Detected memory bank %u: start: 0x%llx size: 0x%llx\n",
  73. count, p->start, p->size);
  74. count++;
  75. }
  76. }
  77. if (!count) {
  78. printf("Failed to detect any memory bank\n");
  79. return -ENODEV;
  80. }
  81. ret = fdt_fixup_memory_banks(blob, bank_start, bank_size, count);
  82. if (ret)
  83. return ret;
  84. return 0;
  85. }