blob: cbea880878f92df90845b572d2e941e78c49aa24 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
2 *(C) Copyright 2018 ASR Microelectronics (Shanghai) Co., Ltd.
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <asm/arch/cpu.h>
9#include <pxa_amp.h>
10
11DECLARE_GLOBAL_DATA_PTR;
12
13#ifdef CONFIG_ASR1901
14#define DMCU_ADDR_BASE 0xC0000000
15#else
16#define DMCU_ADDR_BASE 0xC0100000
17#endif
18
19#define DMCU_CH0_MMAP0 (DMCU_ADDR_BASE + 0x200)
20#define DMCU_CH0_MMAP1 (DMCU_ADDR_BASE + 0x208)
21
22#define DMCU_CH0_PMAP0 (DMCU_ADDR_BASE + 0x220)
23#define DMCU_CH0_PMAP1 (DMCU_ADDR_BASE + 0x224)
24#define DMCU_CH0_DRAM_CFG_0 (DMCU_ADDR_BASE + 0x44)
25
26extern int get_ramdump_flag_f(void);
27
28int DMCU_sdram_base(int cs, ulong *base)
29{
30 u32 mmap;
31
32 switch (cs) {
33 case 0:
34 mmap = readl(DMCU_CH0_MMAP0);
35 break;
36 case 1:
37 mmap = readl(DMCU_CH0_MMAP1);
38 break;
39 default:
40 return -1;
41 }
42
43 if (!(0x1 & mmap))
44 return -1;
45 else
46 *base = 0xFF800000 & mmap;
47 return 0;
48}
49
50int DMCU_sdram_size(int cs, ulong *size)
51{
52 u32 mmap, _size, pmap, dram_cfg_0;
53 u32 bank_bits, row_bits, col_bits, width_bits;
54
55 switch (cs) {
56 case 0:
57 mmap = readl(DMCU_CH0_MMAP0);
58 pmap = readl(DMCU_CH0_PMAP0);
59 break;
60 case 1:
61 mmap = readl(DMCU_CH0_MMAP1);
62 pmap = readl(DMCU_CH0_PMAP1);
63 break;
64 default:
65 return -1;
66 }
67
68 if (!(0x1 & mmap))
69 return -1;
70
71 dram_cfg_0 = readl(DMCU_CH0_DRAM_CFG_0);
72
73 _size = (mmap >> 16) & 0x1F;
74 if (_size < 0x7) {
75 printf("Unknown dram size!\n");
76 return -1;
77 } else {
78 width_bits = ((dram_cfg_0 & (0x7 << 8)) >> 8) - 1;
79 bank_bits = ((pmap & 0x3) >> 0) + 1;
80 row_bits = ((pmap & (0xf << 8)) >> 8) + 10;
81 col_bits = ((pmap & (0xf << 4)) >> 4) + 7;
82
83 *size = (0x1 << (width_bits + bank_bits + row_bits + col_bits));
84 }
85
86 return 0;
87}
88
89#ifndef CONFIG_SYS_BOARD_DRAM_INIT
90/* dram init when bd not avaliable */
91int dram_init(void)
92{
93 int i;
94 unsigned int reloc_end;
95 ulong start, size, ddr_st_offset = 0x0;
96
97 gd->ram_size = 0;
98
99 /* find ddr map start address */
100 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
101 if (!DMCU_sdram_base(i, &ddr_st_offset))
102 break;
103 }
104
105 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
106 if (DMCU_sdram_base(i, &start))
107 continue;
108 if (start != (gd->ram_size + ddr_st_offset)) {
109 printf("error: unspported not-continous ram\n");
110 hang();
111 }
112 if (DMCU_sdram_size(i, &size)) {
113 printf("Read dram size error for chip %d.\n", i);
114 hang();
115 }
116 gd->ram_size += size;
117 }
118
119 /* extend 64MB ddr space to 128MB in ap ramdump mode */
120 if (get_ramdump_flag_f() && (gd->ram_size == (64 * 1024 * 1024)))
121 gd->ram_size = (128 * 1024 * 1024);
122
123 /*
124 * limit the uboot ddr usage between CONFIG_SYS_TEXT_BASE and
125 * CONFIG_SYS_RELOC_END, which reside in ion range
126 * and won't impact emmddump.
127 */
128 debug("gd->ram_size = 0x%lx\n\n",gd->ram_size);
129 reloc_end = pxa_amp_reloc_end();
130 if ((gd->ram_size + ddr_st_offset) < reloc_end)
131 hang();
132 else
133 gd->ram_size = (reloc_end - ddr_st_offset);
134
135 return 0;
136}
137
138/*
139 * If this function is not defined here,
140 * board.c alters dram bank zero configuration defined above.
141 * read from mck register again and skip error as already
142 * checked before
143 */
144void dram_init_banksize(void)
145{
146 int i, b = 0;
147
148 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
149 if (!DMCU_sdram_base(i, &gd->bd->bi_dram[b].start))
150 DMCU_sdram_size(i, &gd->bd->bi_dram[b++].size);
151 }
152
153 /*
154 * If there are banks that are not valid, we need to set
155 * their start address and size to 0. Otherwise other
156 * u-boot functions and Linux kernel gets wrong values which
157 * could result in crash.
158 */
159 for (; b < CONFIG_NR_DRAM_BANKS; b++) {
160 gd->bd->bi_dram[b].start = 0;
161 gd->bd->bi_dram[b].size = 0;
162 }
163
164 if (gd->bd->bi_dram[0].size < CONFIG_TZ_HYPERVISOR_SIZE) {
165 printf("Cannot meet requirement for TrustZone!\n");
166 } else {
167 gd->bd->bi_dram[0].start += CONFIG_TZ_HYPERVISOR_SIZE;
168 gd->bd->bi_dram[0].size -= CONFIG_TZ_HYPERVISOR_SIZE;
169 gd->ram_size -= CONFIG_TZ_HYPERVISOR_SIZE;
170 }
171}
172#endif /* CONFIG_SYS_BOARD_DRAM_INIT */