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