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