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