blob: ad3ed7374782c0b8439056dab1bde8b0655aee2a [file] [log] [blame]
Elyes HAOUASf50b6622020-07-19 14:00:43 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Nick Vaccarob1fa25f2020-01-28 00:31:26 -08002
3#include <assert.h>
4#include <console/console.h>
5#include <fsp/util.h>
Aamir Bohra555c9b62020-03-23 10:13:10 +05306#include <soc/meminit.h>
Nick Vaccarob1fa25f2020-01-28 00:31:26 -08007#include <string.h>
8
Furquan Shaikhf06d0462020-12-31 21:15:34 -08009#define LP4X_CH_WIDTH 16
10#define LP4X_CHANNELS CHANNEL_COUNT(LP4X_CH_WIDTH)
Furquan Shaikh5b1f3352020-03-26 15:36:19 -070011
Furquan Shaikhf06d0462020-12-31 21:15:34 -080012#define DDR4_CH_WIDTH 64
13#define DDR4_CHANNELS CHANNEL_COUNT(DDR4_CH_WIDTH)
Furquan Shaikh8ebbe172020-04-21 23:19:52 -070014
Furquan Shaikhf06d0462020-12-31 21:15:34 -080015static const struct soc_mem_cfg soc_mem_cfg[] = {
16 [MEM_TYPE_DDR4] = {
17 .num_phys_channels = DDR4_CHANNELS,
18 .phys_to_mrc_map = {
19 [0] = 0,
20 [1] = 4,
21 },
22 .md_phy_masks = {
23 /*
24 * Only physical channel 0 is populated in case of half-populated
25 * configuration.
26 */
27 .half_channel = BIT(0),
28 /* In mixed topologies, channel 0 is always memory-down. */
29 .mixed_topo = BIT(0),
30 },
31 },
32 [MEM_TYPE_LP4X] = {
33 .num_phys_channels = LP4X_CHANNELS,
34 .phys_to_mrc_map = {
35 [0] = 0,
36 [1] = 1,
37 [2] = 2,
38 [3] = 3,
39 [4] = 4,
40 [5] = 5,
41 [6] = 6,
42 [7] = 7,
43 },
44 .md_phy_masks = {
45 /*
46 * Physical channels 0, 1, 2 and 3 are populated in case of
47 * half-populated configurations.
48 */
49 .half_channel = BIT(0) | BIT(1) | BIT(2) | BIT(3),
50 /* LP4x does not support mixed topologies. */
51 },
52 },
Nick Vaccarob1fa25f2020-01-28 00:31:26 -080053};
54
Furquan Shaikhf06d0462020-12-31 21:15:34 -080055static void mem_init_spd_upds(FSP_M_CONFIG *mem_cfg, const struct mem_channel_data *data)
Furquan Shaikh5b1f3352020-03-26 15:36:19 -070056{
Furquan Shaikhf06d0462020-12-31 21:15:34 -080057 uint32_t *spd_upds[MRC_CHANNELS][CONFIG_DIMMS_PER_CHANNEL] = {
Felix Singer3e3c4562020-12-17 18:34:45 +000058 [0] = { &mem_cfg->MemorySpdPtr000, &mem_cfg->MemorySpdPtr001, },
59 [1] = { &mem_cfg->MemorySpdPtr010, &mem_cfg->MemorySpdPtr011, },
60 [2] = { &mem_cfg->MemorySpdPtr020, &mem_cfg->MemorySpdPtr021, },
61 [3] = { &mem_cfg->MemorySpdPtr030, &mem_cfg->MemorySpdPtr031, },
62 [4] = { &mem_cfg->MemorySpdPtr100, &mem_cfg->MemorySpdPtr101, },
63 [5] = { &mem_cfg->MemorySpdPtr110, &mem_cfg->MemorySpdPtr111, },
64 [6] = { &mem_cfg->MemorySpdPtr120, &mem_cfg->MemorySpdPtr121, },
65 [7] = { &mem_cfg->MemorySpdPtr130, &mem_cfg->MemorySpdPtr131, },
Furquan Shaikhf06d0462020-12-31 21:15:34 -080066 };
67 uint8_t *disable_dimm_upds[MRC_CHANNELS] = {
Felix Singer3e3c4562020-12-17 18:34:45 +000068 &mem_cfg->DisableDimmMc0Ch0,
69 &mem_cfg->DisableDimmMc0Ch1,
70 &mem_cfg->DisableDimmMc0Ch2,
71 &mem_cfg->DisableDimmMc0Ch3,
72 &mem_cfg->DisableDimmMc1Ch0,
73 &mem_cfg->DisableDimmMc1Ch1,
74 &mem_cfg->DisableDimmMc1Ch2,
75 &mem_cfg->DisableDimmMc1Ch3,
Furquan Shaikhf06d0462020-12-31 21:15:34 -080076 };
77 int ch, dimm;
Nick Vaccarob1fa25f2020-01-28 00:31:26 -080078
Furquan Shaikhf06d0462020-12-31 21:15:34 -080079 mem_cfg->MemorySpdDataLen = data->spd_len;
Nick Vaccarob1fa25f2020-01-28 00:31:26 -080080
Furquan Shaikhf06d0462020-12-31 21:15:34 -080081 for (ch = 0; ch < MRC_CHANNELS; ch++) {
82 uint8_t *disable_dimm_ptr = disable_dimm_upds[ch];
83 *disable_dimm_ptr = 0;
Furquan Shaikh5b1f3352020-03-26 15:36:19 -070084
Furquan Shaikhf06d0462020-12-31 21:15:34 -080085 for (dimm = 0; dimm < CONFIG_DIMMS_PER_CHANNEL; dimm++) {
86 uint32_t *spd_ptr = spd_upds[ch][dimm];
Furquan Shaikh5b1f3352020-03-26 15:36:19 -070087
Furquan Shaikhf06d0462020-12-31 21:15:34 -080088 *spd_ptr = data->spd[ch][dimm];
89 if (!*spd_ptr)
90 *disable_dimm_ptr |= BIT(dimm);
Varun Joshi97343252020-03-23 13:24:36 -070091 }
92 }
93}
Nick Vaccaro0cc63cc2020-08-05 14:45:58 -070094
Furquan Shaikhf06d0462020-12-31 21:15:34 -080095static void mem_init_dq_dqs_upds(void *upds[MRC_CHANNELS], const void *map, size_t upd_size,
96 const struct mem_channel_data *data)
Nick Vaccaro0cc63cc2020-08-05 14:45:58 -070097{
Furquan Shaikhf06d0462020-12-31 21:15:34 -080098 size_t i;
99
100 for (i = 0; i < MRC_CHANNELS; i++, map += upd_size) {
101 if (channel_is_populated(i, MRC_CHANNELS, data->ch_population_flags))
102 memcpy(upds[i], map, upd_size);
103 else
104 memset(upds[i], 0, upd_size);
105 }
106}
107
108static void mem_init_dq_upds(FSP_M_CONFIG *mem_cfg, const struct mem_channel_data *data,
109 const struct mb_cfg *mb_cfg)
110{
111 void *dq_upds[MRC_CHANNELS] = {
Felix Singer3e3c4562020-12-17 18:34:45 +0000112 &mem_cfg->DqMapCpu2DramMc0Ch0,
113 &mem_cfg->DqMapCpu2DramMc0Ch1,
114 &mem_cfg->DqMapCpu2DramMc0Ch2,
115 &mem_cfg->DqMapCpu2DramMc0Ch3,
116 &mem_cfg->DqMapCpu2DramMc1Ch0,
117 &mem_cfg->DqMapCpu2DramMc1Ch1,
118 &mem_cfg->DqMapCpu2DramMc1Ch2,
119 &mem_cfg->DqMapCpu2DramMc1Ch3,
Furquan Shaikhf06d0462020-12-31 21:15:34 -0800120 };
121
Felix Singer3e3c4562020-12-17 18:34:45 +0000122 const size_t upd_size = sizeof(mem_cfg->DqMapCpu2DramMc0Ch0);
Furquan Shaikhf06d0462020-12-31 21:15:34 -0800123
Arthur Heymans10c43d82022-03-24 01:04:25 +0100124 _Static_assert(sizeof(mem_cfg->DqMapCpu2DramMc0Ch0) == CONFIG_MRC_CHANNEL_WIDTH,
125 "Incorrect DQ UPD size!");
Furquan Shaikhf06d0462020-12-31 21:15:34 -0800126
127 mem_init_dq_dqs_upds(dq_upds, mb_cfg->dq_map, upd_size, data);
128}
129
130static void mem_init_dqs_upds(FSP_M_CONFIG *mem_cfg, const struct mem_channel_data *data,
131 const struct mb_cfg *mb_cfg)
132{
133 void *dqs_upds[MRC_CHANNELS] = {
Felix Singer3e3c4562020-12-17 18:34:45 +0000134 &mem_cfg->DqsMapCpu2DramMc0Ch0,
135 &mem_cfg->DqsMapCpu2DramMc0Ch1,
136 &mem_cfg->DqsMapCpu2DramMc0Ch2,
137 &mem_cfg->DqsMapCpu2DramMc0Ch3,
138 &mem_cfg->DqsMapCpu2DramMc1Ch0,
139 &mem_cfg->DqsMapCpu2DramMc1Ch1,
140 &mem_cfg->DqsMapCpu2DramMc1Ch2,
141 &mem_cfg->DqsMapCpu2DramMc1Ch3,
Furquan Shaikhf06d0462020-12-31 21:15:34 -0800142 };
143
Felix Singer3e3c4562020-12-17 18:34:45 +0000144 const size_t upd_size = sizeof(mem_cfg->DqsMapCpu2DramMc0Ch0);
Furquan Shaikhf06d0462020-12-31 21:15:34 -0800145
Arthur Heymans10c43d82022-03-24 01:04:25 +0100146 _Static_assert(sizeof(mem_cfg->DqsMapCpu2DramMc0Ch0) == CONFIG_MRC_CHANNEL_WIDTH / 8,
147 "Incorrect DQS UPD size!");
Furquan Shaikhf06d0462020-12-31 21:15:34 -0800148
149 mem_init_dq_dqs_upds(dqs_upds, mb_cfg->dqs_map, upd_size, data);
150}
151
Subrata Banik2eb51aa2022-03-10 17:53:14 +0530152void memcfg_init(FSPM_UPD *memupd, const struct mb_cfg *mb_cfg,
Furquan Shaikhf06d0462020-12-31 21:15:34 -0800153 const struct mem_spd *spd_info, bool half_populated)
154{
155 struct mem_channel_data data;
Subrata Banik2eb51aa2022-03-10 17:53:14 +0530156 FSP_M_CONFIG *mem_cfg = &memupd->FspmConfig;
Furquan Shaikhf06d0462020-12-31 21:15:34 -0800157
158 if (mb_cfg->type >= ARRAY_SIZE(soc_mem_cfg))
159 die("Invalid memory type(%x)!\n", mb_cfg->type);
160
Subrata Banik47b836a2022-03-10 17:21:33 +0530161 mem_populate_channel_data(memupd, &soc_mem_cfg[mb_cfg->type], spd_info, half_populated,
Subrata Banik4703edc2022-03-10 19:12:02 +0530162 &data);
Furquan Shaikhf06d0462020-12-31 21:15:34 -0800163 mem_init_spd_upds(mem_cfg, &data);
164 mem_init_dq_upds(mem_cfg, &data, mb_cfg);
165 mem_init_dqs_upds(mem_cfg, &data, mb_cfg);
166
167 mem_cfg->ECT = mb_cfg->ect;
168
169 switch (mb_cfg->type) {
170 case MEM_TYPE_DDR4:
171 mem_cfg->DqPinsInterleaved = mb_cfg->ddr4_config.dq_pins_interleaved;
Nick Vaccaro0cc63cc2020-08-05 14:45:58 -0700172 break;
Furquan Shaikhf06d0462020-12-31 21:15:34 -0800173 case MEM_TYPE_LP4X:
174 /* LPDDR4x does not allow interleaved memory */
175 mem_cfg->DqPinsInterleaved = 0;
Nick Vaccaro0cc63cc2020-08-05 14:45:58 -0700176 break;
177 default:
Furquan Shaikhf06d0462020-12-31 21:15:34 -0800178 die("Unsupported memory type(%d)\n", mb_cfg->type);
Nick Vaccaro0cc63cc2020-08-05 14:45:58 -0700179 }
Furquan Shaikhf06d0462020-12-31 21:15:34 -0800180
Nick Vaccaro0cc63cc2020-08-05 14:45:58 -0700181}