Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Bin Meng | 828d9af | 2015-02-02 22:35:27 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com> |
Bin Meng | 828d9af | 2015-02-02 22:35:27 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 1eb69ae | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 7 | #include <cpu_func.h> |
Bin Meng | 20c3411 | 2015-02-05 23:42:28 +0800 | [diff] [blame] | 8 | #include <errno.h> |
| 9 | #include <fdtdec.h> |
Simon Glass | 67c4e9f | 2019-11-14 12:57:45 -0700 | [diff] [blame] | 10 | #include <init.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Bin Meng | 2fc2b83 | 2015-10-12 01:30:42 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 13 | #include <asm/cache.h> |
Bin Meng | 2fc2b83 | 2015-10-12 01:30:42 -0700 | [diff] [blame] | 14 | #include <asm/mrccache.h> |
Bin Meng | c6d4705 | 2015-09-14 00:07:41 -0700 | [diff] [blame] | 15 | #include <asm/mtrr.h> |
Bin Meng | 828d9af | 2015-02-02 22:35:27 +0800 | [diff] [blame] | 16 | #include <asm/post.h> |
Bin Meng | 20c3411 | 2015-02-05 23:42:28 +0800 | [diff] [blame] | 17 | #include <asm/arch/mrc.h> |
Bin Meng | c6d4705 | 2015-09-14 00:07:41 -0700 | [diff] [blame] | 18 | #include <asm/arch/msg_port.h> |
Bin Meng | 828d9af | 2015-02-02 22:35:27 +0800 | [diff] [blame] | 19 | #include <asm/arch/quark.h> |
| 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Bin Meng | 2fc2b83 | 2015-10-12 01:30:42 -0700 | [diff] [blame] | 23 | static __maybe_unused int prepare_mrc_cache(struct mrc_params *mrc_params) |
| 24 | { |
| 25 | struct mrc_data_container *cache; |
| 26 | struct mrc_region entry; |
| 27 | int ret; |
| 28 | |
Simon Glass | 515e817 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 29 | ret = mrccache_get_region(MRC_TYPE_NORMAL, NULL, &entry); |
Bin Meng | 2fc2b83 | 2015-10-12 01:30:42 -0700 | [diff] [blame] | 30 | if (ret) |
| 31 | return ret; |
| 32 | |
| 33 | cache = mrccache_find_current(&entry); |
| 34 | if (!cache) |
| 35 | return -ENOENT; |
| 36 | |
| 37 | debug("%s: mrc cache at %p, size %x checksum %04x\n", __func__, |
| 38 | cache->data, cache->data_size, cache->checksum); |
| 39 | |
| 40 | /* copy mrc cache to the mrc_params */ |
| 41 | memcpy(&mrc_params->timings, cache->data, cache->data_size); |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
Bin Meng | 20c3411 | 2015-02-05 23:42:28 +0800 | [diff] [blame] | 46 | static int mrc_configure_params(struct mrc_params *mrc_params) |
| 47 | { |
| 48 | const void *blob = gd->fdt_blob; |
| 49 | int node; |
| 50 | int mrc_flags; |
| 51 | |
| 52 | node = fdtdec_next_compatible(blob, 0, COMPAT_INTEL_QRK_MRC); |
| 53 | if (node < 0) { |
| 54 | debug("%s: Cannot find MRC node\n", __func__); |
| 55 | return -EINVAL; |
| 56 | } |
| 57 | |
Bin Meng | 2fc2b83 | 2015-10-12 01:30:42 -0700 | [diff] [blame] | 58 | #ifdef CONFIG_ENABLE_MRC_CACHE |
| 59 | mrc_params->boot_mode = prepare_mrc_cache(mrc_params); |
| 60 | if (mrc_params->boot_mode) |
| 61 | mrc_params->boot_mode = BM_COLD; |
| 62 | else |
| 63 | mrc_params->boot_mode = BM_FAST; |
| 64 | #else |
Bin Meng | 20c3411 | 2015-02-05 23:42:28 +0800 | [diff] [blame] | 65 | mrc_params->boot_mode = BM_COLD; |
Bin Meng | 2fc2b83 | 2015-10-12 01:30:42 -0700 | [diff] [blame] | 66 | #endif |
Bin Meng | 20c3411 | 2015-02-05 23:42:28 +0800 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | * TODO: |
| 70 | * |
| 71 | * We need determine ECC by pin strap state |
| 72 | * |
| 73 | * Disable ECC by default for now |
| 74 | */ |
| 75 | mrc_params->ecc_enables = 0; |
| 76 | |
| 77 | mrc_flags = fdtdec_get_int(blob, node, "flags", 0); |
| 78 | if (mrc_flags & MRC_FLAG_SCRAMBLE_EN) |
| 79 | mrc_params->scrambling_enables = 1; |
| 80 | else |
| 81 | mrc_params->scrambling_enables = 0; |
| 82 | |
| 83 | mrc_params->dram_width = fdtdec_get_int(blob, node, "dram-width", 0); |
| 84 | mrc_params->ddr_speed = fdtdec_get_int(blob, node, "dram-speed", 0); |
| 85 | mrc_params->ddr_type = fdtdec_get_int(blob, node, "dram-type", 0); |
| 86 | |
| 87 | mrc_params->rank_enables = fdtdec_get_int(blob, node, "rank-mask", 0); |
| 88 | mrc_params->channel_enables = fdtdec_get_int(blob, node, |
| 89 | "chan-mask", 0); |
| 90 | mrc_params->channel_width = fdtdec_get_int(blob, node, |
| 91 | "chan-width", 0); |
| 92 | mrc_params->address_mode = fdtdec_get_int(blob, node, "addr-mode", 0); |
| 93 | |
| 94 | mrc_params->refresh_rate = fdtdec_get_int(blob, node, |
| 95 | "refresh-rate", 0); |
| 96 | mrc_params->sr_temp_range = fdtdec_get_int(blob, node, |
| 97 | "sr-temp-range", 0); |
| 98 | mrc_params->ron_value = fdtdec_get_int(blob, node, |
| 99 | "ron-value", 0); |
| 100 | mrc_params->rtt_nom_value = fdtdec_get_int(blob, node, |
| 101 | "rtt-nom-value", 0); |
| 102 | mrc_params->rd_odt_value = fdtdec_get_int(blob, node, |
| 103 | "rd-odt-value", 0); |
| 104 | |
| 105 | mrc_params->params.density = fdtdec_get_int(blob, node, |
| 106 | "dram-density", 0); |
| 107 | mrc_params->params.cl = fdtdec_get_int(blob, node, "dram-cl", 0); |
| 108 | mrc_params->params.ras = fdtdec_get_int(blob, node, "dram-ras", 0); |
| 109 | mrc_params->params.wtr = fdtdec_get_int(blob, node, "dram-wtr", 0); |
| 110 | mrc_params->params.rrd = fdtdec_get_int(blob, node, "dram-rrd", 0); |
| 111 | mrc_params->params.faw = fdtdec_get_int(blob, node, "dram-faw", 0); |
| 112 | |
| 113 | debug("MRC dram_width %d\n", mrc_params->dram_width); |
| 114 | debug("MRC rank_enables %d\n", mrc_params->rank_enables); |
| 115 | debug("MRC ddr_speed %d\n", mrc_params->ddr_speed); |
| 116 | debug("MRC flags: %s\n", |
| 117 | (mrc_params->scrambling_enables) ? "SCRAMBLE_EN" : ""); |
| 118 | |
| 119 | debug("MRC density=%d tCL=%d tRAS=%d tWTR=%d tRRD=%d tFAW=%d\n", |
| 120 | mrc_params->params.density, mrc_params->params.cl, |
| 121 | mrc_params->params.ras, mrc_params->params.wtr, |
| 122 | mrc_params->params.rrd, mrc_params->params.faw); |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
Bin Meng | 828d9af | 2015-02-02 22:35:27 +0800 | [diff] [blame] | 127 | int dram_init(void) |
| 128 | { |
Bin Meng | 20c3411 | 2015-02-05 23:42:28 +0800 | [diff] [blame] | 129 | struct mrc_params mrc_params; |
Bin Meng | 2fc2b83 | 2015-10-12 01:30:42 -0700 | [diff] [blame] | 130 | #ifdef CONFIG_ENABLE_MRC_CACHE |
| 131 | char *cache; |
| 132 | #endif |
Bin Meng | 20c3411 | 2015-02-05 23:42:28 +0800 | [diff] [blame] | 133 | int ret; |
| 134 | |
| 135 | memset(&mrc_params, 0, sizeof(struct mrc_params)); |
| 136 | ret = mrc_configure_params(&mrc_params); |
| 137 | if (ret) |
| 138 | return ret; |
| 139 | |
| 140 | /* Set up the DRAM by calling the memory reference code */ |
| 141 | mrc_init(&mrc_params); |
| 142 | if (mrc_params.status) |
| 143 | return -EIO; |
| 144 | |
| 145 | gd->ram_size = mrc_params.mem_size; |
Bin Meng | 828d9af | 2015-02-02 22:35:27 +0800 | [diff] [blame] | 146 | post_code(POST_DRAM); |
| 147 | |
Bin Meng | c6d4705 | 2015-09-14 00:07:41 -0700 | [diff] [blame] | 148 | /* variable range MTRR#2: RAM area */ |
| 149 | disable_caches(); |
| 150 | msg_port_write(MSG_PORT_HOST_BRIDGE, MTRR_VAR_PHYBASE(MTRR_VAR_RAM), |
| 151 | 0 | MTRR_TYPE_WRBACK); |
| 152 | msg_port_write(MSG_PORT_HOST_BRIDGE, MTRR_VAR_PHYMASK(MTRR_VAR_RAM), |
| 153 | (~(gd->ram_size - 1)) | MTRR_PHYS_MASK_VALID); |
| 154 | enable_caches(); |
| 155 | |
Bin Meng | 2fc2b83 | 2015-10-12 01:30:42 -0700 | [diff] [blame] | 156 | #ifdef CONFIG_ENABLE_MRC_CACHE |
| 157 | cache = malloc(sizeof(struct mrc_timings)); |
| 158 | if (cache) { |
Simon Glass | 515e817 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 159 | struct mrc_output *mrc = &gd->arch.mrc[MRC_TYPE_NORMAL]; |
| 160 | |
Bin Meng | 2fc2b83 | 2015-10-12 01:30:42 -0700 | [diff] [blame] | 161 | memcpy(cache, &mrc_params.timings, sizeof(struct mrc_timings)); |
Simon Glass | 515e817 | 2019-12-06 21:42:07 -0700 | [diff] [blame] | 162 | mrc->buf = cache; |
| 163 | mrc->len = sizeof(struct mrc_timings); |
Bin Meng | 2fc2b83 | 2015-10-12 01:30:42 -0700 | [diff] [blame] | 164 | } |
| 165 | #endif |
| 166 | |
Bin Meng | 828d9af | 2015-02-02 22:35:27 +0800 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
Simon Glass | 76b00ac | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 170 | int dram_init_banksize(void) |
Bin Meng | 828d9af | 2015-02-02 22:35:27 +0800 | [diff] [blame] | 171 | { |
| 172 | gd->bd->bi_dram[0].start = 0; |
| 173 | gd->bd->bi_dram[0].size = gd->ram_size; |
Simon Glass | 76b00ac | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 174 | |
| 175 | return 0; |
Bin Meng | 828d9af | 2015-02-02 22:35:27 +0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /* |
| 179 | * This function looks for the highest region of memory lower than 4GB which |
| 180 | * has enough space for U-Boot where U-Boot is aligned on a page boundary. |
| 181 | * It overrides the default implementation found elsewhere which simply |
| 182 | * picks the end of ram, wherever that may be. The location of the stack, |
| 183 | * the relocation address, and how far U-Boot is moved by relocation are |
| 184 | * set in the global data structure. |
| 185 | */ |
| 186 | ulong board_get_usable_ram_top(ulong total_size) |
| 187 | { |
| 188 | return gd->ram_size; |
| 189 | } |