dram.c 2.1 KB

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