Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Bin Meng | 2229c4c | 2015-05-07 21:34:08 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com> |
Bin Meng | 2229c4c | 2015-05-07 21:34:08 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Glass | 6980b6b | 2019-11-14 12:57:45 -0700 | [diff] [blame] | 6 | #include <init.h> |
Simon Glass | 50706fd | 2025-03-15 14:25:47 +0000 | [diff] [blame] | 7 | #include <spl.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 8 | #include <asm/global_data.h> |
Simon Glass | 50706fd | 2025-03-15 14:25:47 +0000 | [diff] [blame] | 9 | #include <asm/mtrr.h> |
Bin Meng | 2229c4c | 2015-05-07 21:34:08 +0800 | [diff] [blame] | 10 | #include <asm/post.h> |
| 11 | #include <asm/arch/qemu.h> |
Simon Glass | de1669f | 2023-07-30 21:02:04 -0600 | [diff] [blame] | 12 | #include <linux/sizes.h> |
Bin Meng | 2229c4c | 2015-05-07 21:34:08 +0800 | [diff] [blame] | 13 | |
| 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | |
Bin Meng | 38502eb | 2019-08-29 02:53:04 -0700 | [diff] [blame] | 16 | u32 qemu_get_low_memory_size(void) |
Bin Meng | 2229c4c | 2015-05-07 21:34:08 +0800 | [diff] [blame] | 17 | { |
| 18 | u32 ram; |
| 19 | |
| 20 | outb(HIGH_RAM_ADDR, CMOS_ADDR_PORT); |
| 21 | ram = ((u32)inb(CMOS_DATA_PORT)) << 14; |
| 22 | outb(LOW_RAM_ADDR, CMOS_ADDR_PORT); |
| 23 | ram |= ((u32)inb(CMOS_DATA_PORT)) << 6; |
| 24 | ram += 16 * 1024; |
| 25 | |
Bin Meng | 38502eb | 2019-08-29 02:53:04 -0700 | [diff] [blame] | 26 | return ram * 1024; |
| 27 | } |
| 28 | |
Bin Meng | fc7f57f | 2019-08-29 02:53:05 -0700 | [diff] [blame] | 29 | u64 qemu_get_high_memory_size(void) |
| 30 | { |
| 31 | u64 ram; |
| 32 | |
| 33 | outb(HIGH_HIGHRAM_ADDR, CMOS_ADDR_PORT); |
| 34 | ram = ((u64)inb(CMOS_DATA_PORT)) << 22; |
| 35 | outb(MID_HIGHRAM_ADDR, CMOS_ADDR_PORT); |
| 36 | ram |= ((u64)inb(CMOS_DATA_PORT)) << 14; |
| 37 | outb(LOW_HIGHRAM_ADDR, CMOS_ADDR_PORT); |
| 38 | ram |= ((u64)inb(CMOS_DATA_PORT)) << 6; |
| 39 | |
| 40 | return ram * 1024; |
| 41 | } |
| 42 | |
Bin Meng | 38502eb | 2019-08-29 02:53:04 -0700 | [diff] [blame] | 43 | int dram_init(void) |
| 44 | { |
| 45 | gd->ram_size = qemu_get_low_memory_size(); |
Bin Meng | fc7f57f | 2019-08-29 02:53:05 -0700 | [diff] [blame] | 46 | gd->ram_size += qemu_get_high_memory_size(); |
Bin Meng | 2229c4c | 2015-05-07 21:34:08 +0800 | [diff] [blame] | 47 | post_code(POST_DRAM); |
| 48 | |
Simon Glass | 50706fd | 2025-03-15 14:25:47 +0000 | [diff] [blame] | 49 | if (xpl_phase() == PHASE_BOARD_F) { |
| 50 | u64 total = gd->ram_size; |
| 51 | int ret; |
| 52 | |
| 53 | if (total > SZ_2G + SZ_1G) |
| 54 | total += SZ_1G; |
| 55 | ret = mtrr_add_request(MTRR_TYPE_WRBACK, 0, total); |
| 56 | if (ret != -ENOSYS) { |
| 57 | if (ret) |
| 58 | return log_msg_ret("mta", ret); |
| 59 | ret = mtrr_commit(false); |
| 60 | if (ret) |
| 61 | return log_msg_ret("mtc", ret); |
| 62 | } |
| 63 | } |
| 64 | |
Bin Meng | 2229c4c | 2015-05-07 21:34:08 +0800 | [diff] [blame] | 65 | return 0; |
| 66 | } |
| 67 | |
Simon Glass | 2f949c3 | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 68 | int dram_init_banksize(void) |
Bin Meng | 2229c4c | 2015-05-07 21:34:08 +0800 | [diff] [blame] | 69 | { |
Bin Meng | fc7f57f | 2019-08-29 02:53:05 -0700 | [diff] [blame] | 70 | u64 high_mem_size; |
| 71 | |
Bin Meng | 2229c4c | 2015-05-07 21:34:08 +0800 | [diff] [blame] | 72 | gd->bd->bi_dram[0].start = 0; |
Bin Meng | fc7f57f | 2019-08-29 02:53:05 -0700 | [diff] [blame] | 73 | gd->bd->bi_dram[0].size = qemu_get_low_memory_size(); |
| 74 | |
| 75 | high_mem_size = qemu_get_high_memory_size(); |
| 76 | if (high_mem_size) { |
| 77 | gd->bd->bi_dram[1].start = SZ_4G; |
| 78 | gd->bd->bi_dram[1].size = high_mem_size; |
| 79 | } |
Simon Glass | 2f949c3 | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 80 | |
| 81 | return 0; |
Bin Meng | 2229c4c | 2015-05-07 21:34:08 +0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /* |
| 85 | * This function looks for the highest region of memory lower than 4GB which |
| 86 | * has enough space for U-Boot where U-Boot is aligned on a page boundary. |
| 87 | * It overrides the default implementation found elsewhere which simply |
| 88 | * picks the end of ram, wherever that may be. The location of the stack, |
| 89 | * the relocation address, and how far U-Boot is moved by relocation are |
| 90 | * set in the global data structure. |
| 91 | */ |
Heinrich Schuchardt | 51a9aac | 2023-08-12 20:16:58 +0200 | [diff] [blame] | 92 | phys_addr_t board_get_usable_ram_top(phys_size_t total_size) |
Bin Meng | 2229c4c | 2015-05-07 21:34:08 +0800 | [diff] [blame] | 93 | { |
Bin Meng | fc7f57f | 2019-08-29 02:53:05 -0700 | [diff] [blame] | 94 | return qemu_get_low_memory_size(); |
Bin Meng | 2229c4c | 2015-05-07 21:34:08 +0800 | [diff] [blame] | 95 | } |