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