blob: 8e6b006518aec76cdb6ac7f17edee27b013656d6 [file] [log] [blame]
Rayagonda Kokatanur291635a2020-07-15 22:48:55 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2020 Broadcom.
4 *
5 */
6
7#include <common.h>
Rayagonda Kokatanurc8b98482020-07-15 22:49:04 +05308#include <fdt_support.h>
Rayagonda Kokatanur291635a2020-07-15 22:48:55 +05309#include <asm/io.h>
Rayagonda Kokatanurf01a4e82020-07-15 22:49:01 +053010#include <asm/gic-v3.h>
Simon Glass401d1c42020-10-30 21:38:53 -060011#include <asm/global_data.h>
Rayagonda Kokatanur291635a2020-07-15 22:48:55 +053012#include <asm/system.h>
13#include <asm/armv8/mmu.h>
Abhishek Shahef7192e2020-07-15 22:48:59 +053014#include <asm/arch-bcmns3/bl33_info.h>
Rayagonda Kokatanurc8b98482020-07-15 22:49:04 +053015#include <dt-bindings/memory/bcm-ns3-mc.h>
Rayagonda Kokatanuraf071932020-08-25 23:16:37 +053016#include <broadcom/chimp.h>
Rayagonda Kokatanur291635a2020-07-15 22:48:55 +053017
Rayagonda Kokatanurc8b98482020-07-15 22:49:04 +053018#define BANK_OFFSET(bank) ((u64)BCM_NS3_DDR_INFO_BASE + 8 + ((bank) * 16))
19
20/*
21 * ns3_dram_bank - DDR bank details
22 *
23 * @start: DDR bank start address
24 * @len: DDR bank length
25 */
26struct ns3_dram_bank {
27 u64 start[BCM_NS3_MAX_NR_BANKS];
28 u64 len[BCM_NS3_MAX_NR_BANKS];
29};
30
31/*
32 * ns3_dram_hdr - DDR header info
33 *
34 * @sig: DDR info signature
35 * @bank: DDR bank details
36 */
37struct ns3_dram_hdr {
38 u32 sig;
39 struct ns3_dram_bank bank;
40};
41
Rayagonda Kokatanur291635a2020-07-15 22:48:55 +053042static struct mm_region ns3_mem_map[] = {
43 {
44 .virt = 0x0UL,
45 .phys = 0x0UL,
46 .size = 0x80000000UL,
47 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
48 PTE_BLOCK_NON_SHARE |
49 PTE_BLOCK_PXN | PTE_BLOCK_UXN
50 }, {
Rayagonda Kokatanurc8b98482020-07-15 22:49:04 +053051 .virt = BCM_NS3_MEM_START,
52 .phys = BCM_NS3_MEM_START,
53 .size = BCM_NS3_MEM_LEN,
54 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
55 PTE_BLOCK_INNER_SHARE
56 }, {
57 .virt = BCM_NS3_BANK_1_MEM_START,
58 .phys = BCM_NS3_BANK_1_MEM_START,
59 .size = BCM_NS3_BANK_1_MEM_LEN,
Rayagonda Kokatanur291635a2020-07-15 22:48:55 +053060 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
61 PTE_BLOCK_INNER_SHARE
62 }, {
63 /* List terminator */
64 0,
65 }
66};
67
68struct mm_region *mem_map = ns3_mem_map;
69
70DECLARE_GLOBAL_DATA_PTR;
71
Abhishek Shahef7192e2020-07-15 22:48:59 +053072/*
73 * Force the bl33_info to the data-section, as .bss will not be valid
74 * when save_boot_params is invoked.
75 */
76struct bl33_info *bl33_info __section(".data");
77
Rayagonda Kokatanurc8b98482020-07-15 22:49:04 +053078/*
79 * Run modulo 256 checksum calculation and return the calculated checksum
80 */
81static u8 checksum_calc(u8 *p, unsigned int len)
82{
83 unsigned int i;
84 u8 chksum = 0;
85
86 for (i = 0; i < len; i++)
87 chksum += p[i];
88
89 return chksum;
90}
91
92/*
93 * This function parses the memory layout information from a reserved area in
94 * DDR, and then fix up the FDT before passing it to Linux.
95 *
96 * In the case of error, do nothing and the default memory layout in DT will
97 * be used
98 */
99static int mem_info_parse_fixup(void *fdt)
100{
101 struct ns3_dram_hdr hdr;
102 u32 *p32, i, nr_banks;
103 u64 *p64;
104
105 /* validate signature */
106 p32 = (u32 *)BCM_NS3_DDR_INFO_BASE;
107 hdr.sig = *p32;
108 if (hdr.sig != BCM_NS3_DDR_INFO_SIG) {
109 printf("DDR info signature 0x%x invalid\n", hdr.sig);
110 return -EINVAL;
111 }
112
113 /* run checksum test to validate data */
114 if (checksum_calc((u8 *)p32, BCM_NS3_DDR_INFO_LEN) != 0) {
115 printf("Checksum on DDR info failed\n");
116 return -EINVAL;
117 }
118
119 /* parse information for each bank */
120 nr_banks = 0;
121 for (i = 0; i < BCM_NS3_MAX_NR_BANKS; i++) {
122 /* skip banks with a length of zero */
123 p64 = (u64 *)BANK_OFFSET(i);
124 if (*(p64 + 1) == 0)
125 continue;
126
127 hdr.bank.start[i] = *p64;
128 hdr.bank.len[i] = *(p64 + 1);
129
130 printf("mem[%u] 0x%llx - 0x%llx\n", i, hdr.bank.start[i],
131 hdr.bank.start[i] + hdr.bank.len[i] - 1);
132 nr_banks++;
133 }
134
135 if (!nr_banks) {
136 printf("No DDR banks detected\n");
137 return -ENOMEM;
138 }
139
140 return fdt_fixup_memory_banks(fdt, hdr.bank.start, hdr.bank.len,
141 nr_banks);
142}
143
Rayagonda Kokatanur291635a2020-07-15 22:48:55 +0530144int board_init(void)
145{
Bharat Kumar Reddy Gooty53a3e8d2020-07-15 22:49:05 +0530146 /* Setup memory using "memory" node from DTB */
147 if (fdtdec_setup_mem_size_base() != 0)
148 return -EINVAL;
149 fdtdec_setup_memory_banksize();
150
Abhishek Shahef7192e2020-07-15 22:48:59 +0530151 if (bl33_info->version != BL33_INFO_VERSION)
152 printf("*** warning: ATF BL31 and U-Boot not in sync! ***\n");
153
Rayagonda Kokatanur291635a2020-07-15 22:48:55 +0530154 return 0;
155}
156
157int board_late_init(void)
158{
159 return 0;
160}
161
162int dram_init(void)
163{
Bharat Kumar Reddy Gooty53a3e8d2020-07-15 22:49:05 +0530164 /*
165 * Mark ram base as the last 16MB of 2GB DDR, which is 0xFF00_0000.
166 * So that relocation happens with in the last 16MB memory.
167 */
168 gd->ram_base = (phys_size_t)(BCM_NS3_MEM_END - SZ_16M);
169 gd->ram_size = (unsigned long)SZ_16M;
Rayagonda Kokatanur291635a2020-07-15 22:48:55 +0530170
171 return 0;
172}
173
174int dram_init_banksize(void)
175{
Bharat Kumar Reddy Gooty53a3e8d2020-07-15 22:49:05 +0530176 gd->bd->bi_dram[0].start = (BCM_NS3_MEM_END - SZ_16M);
177 gd->bd->bi_dram[0].size = SZ_16M;
Rayagonda Kokatanur291635a2020-07-15 22:48:55 +0530178
179 return 0;
180}
181
Bharat Kumar Reddy Gooty53a3e8d2020-07-15 22:49:05 +0530182/* Limit RAM used by U-Boot to the DDR first bank End region */
183ulong board_get_usable_ram_top(ulong total_size)
184{
185 return BCM_NS3_MEM_END;
186}
187
Rayagonda Kokatanurb5a152e2020-07-15 22:49:00 +0530188void reset_cpu(ulong level)
Rayagonda Kokatanur291635a2020-07-15 22:48:55 +0530189{
Harald Seiler10b86ef2020-12-15 16:47:50 +0100190 /* Perform a level 3 reset */
191 psci_system_reset2(3, 0);
Rayagonda Kokatanur291635a2020-07-15 22:48:55 +0530192}
Rayagonda Kokatanurf01a4e82020-07-15 22:49:01 +0530193
194#ifdef CONFIG_OF_BOARD_SETUP
195int ft_board_setup(void *fdt, struct bd_info *bd)
196{
Rayagonda Kokatanuraf071932020-08-25 23:16:37 +0530197 u32 chimp_hs = CHIMP_HANDSHAKE_WAIT_TIMEOUT;
198
Rayagonda Kokatanurf01a4e82020-07-15 22:49:01 +0530199 gic_lpi_tables_init();
200
Rayagonda Kokatanuraf071932020-08-25 23:16:37 +0530201 /*
202 * Check for chimp handshake status.
203 * Zero timeout value will actually fall to default timeout.
204 *
205 * System boot is independent of chimp handshake.
206 * chimp handshake failure is not a catastrophic error.
207 * Hence continue booting if chimp handshake fails.
208 */
209 chimp_handshake_status_optee(0, &chimp_hs);
210 if (chimp_hs == CHIMP_HANDSHAKE_SUCCESS)
211 printf("ChiMP handshake successful\n");
212 else
213 printf("ERROR: ChiMP handshake status 0x%x\n", chimp_hs);
214
Rayagonda Kokatanurc8b98482020-07-15 22:49:04 +0530215 return mem_info_parse_fixup(fdt);
Rayagonda Kokatanurf01a4e82020-07-15 22:49:01 +0530216}
217#endif /* CONFIG_OF_BOARD_SETUP */