Simon Glass | 42bf7db | 2019-12-08 17:40:19 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2019 Google LLC |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <asm/arch/iomap.h> |
| 10 | #include <asm/arch/fsp/fsp_configs.h> |
| 11 | #include <asm/arch/fsp/fsp_m_upd.h> |
| 12 | #include <asm/fsp2/fsp_internal.h> |
| 13 | #include <dm/uclass-internal.h> |
| 14 | |
| 15 | /* |
| 16 | * ODT settings: |
| 17 | * If ODT PIN to LP4 DRAM is pulled HIGH for ODT_A and HIGH for ODT_B, |
| 18 | * choose ODT_A_B_HIGH_HIGH. If ODT PIN to LP4 DRAM is pulled HIGH for ODT_A |
| 19 | * and LOW for ODT_B, choose ODT_A_B_HIGH_LOW. |
| 20 | * |
| 21 | * Note that the enum values correspond to the interpreted UPD fields |
| 22 | * within Ch[3:0]_OdtConfig parameters. |
| 23 | */ |
| 24 | enum { |
| 25 | ODT_A_B_HIGH_LOW = 0 << 1, |
| 26 | ODT_A_B_HIGH_HIGH = 1 << 1, |
| 27 | N_WR_24 = 1 << 5, |
| 28 | }; |
| 29 | |
| 30 | /* |
| 31 | * LPDDR4 helper routines for configuring the memory UPD for LPDDR4 operation. |
| 32 | * There are four physical LPDDR4 channels, each 32-bits wide. There are two |
| 33 | * logical channels using two physical channels together to form a 64-bit |
| 34 | * interface to memory for each logical channel. |
| 35 | */ |
| 36 | |
| 37 | enum { |
| 38 | LP4_PHYS_CH0A, |
| 39 | LP4_PHYS_CH0B, |
| 40 | LP4_PHYS_CH1A, |
| 41 | LP4_PHYS_CH1B, |
| 42 | |
| 43 | LP4_NUM_PHYS_CHANNELS, |
| 44 | }; |
| 45 | |
| 46 | /* |
| 47 | * The DQs within a physical channel can be bit-swizzled within each byte. |
| 48 | * Within a channel the bytes can be swapped, but the DQs need to be routed |
| 49 | * with the corresponding DQS (strobe). |
| 50 | */ |
| 51 | enum { |
| 52 | LP4_DQS0, |
| 53 | LP4_DQS1, |
| 54 | LP4_DQS2, |
| 55 | LP4_DQS3, |
| 56 | |
| 57 | LP4_NUM_BYTE_LANES, |
| 58 | DQ_BITS_PER_DQS = 8, |
| 59 | }; |
| 60 | |
| 61 | /* Provide bit swizzling per DQS and byte swapping within a channel */ |
| 62 | struct lpddr4_chan_swizzle_cfg { |
| 63 | u8 dqs[LP4_NUM_BYTE_LANES][DQ_BITS_PER_DQS]; |
| 64 | }; |
| 65 | |
| 66 | struct lpddr4_swizzle_cfg { |
| 67 | struct lpddr4_chan_swizzle_cfg phys[LP4_NUM_PHYS_CHANNELS]; |
| 68 | }; |
| 69 | |
| 70 | static void setup_sdram(struct fsp_m_config *cfg, |
| 71 | const struct lpddr4_swizzle_cfg *swizzle_cfg) |
| 72 | { |
| 73 | const struct lpddr4_chan_swizzle_cfg *sch; |
| 74 | /* Number of bytes to copy per DQS */ |
| 75 | const size_t sz = DQ_BITS_PER_DQS; |
| 76 | int chan; |
| 77 | |
| 78 | cfg->memory_down = 1; |
| 79 | cfg->scrambler_support = 1; |
| 80 | cfg->channel_hash_mask = 0x36; |
| 81 | cfg->slice_hash_mask = 9; |
| 82 | cfg->interleaved_mode = 2; |
| 83 | cfg->channels_slices_enable = 0; |
| 84 | cfg->min_ref_rate2x_enable = 0; |
| 85 | cfg->dual_rank_support_enable = 1; |
| 86 | |
| 87 | /* LPDDR4 is memory down so no SPD addresses */ |
| 88 | cfg->dimm0_spd_address = 0; |
| 89 | cfg->dimm1_spd_address = 0; |
| 90 | |
| 91 | for (chan = 0; chan < 4; chan++) { |
| 92 | struct fsp_ram_channel *ch = &cfg->chan[chan]; |
| 93 | |
| 94 | ch->rank_enable = 1; |
| 95 | ch->device_width = 1; |
| 96 | ch->dram_density = 2; |
| 97 | ch->option = 3; |
| 98 | ch->odt_config = ODT_A_B_HIGH_HIGH; |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * CH0_DQB byte lanes in the bit swizzle configuration field are |
| 103 | * not 1:1. The mapping within the swizzling field is: |
| 104 | * indices [0:7] - byte lane 1 (DQS1) DQ[8:15] |
| 105 | * indices [8:15] - byte lane 0 (DQS0) DQ[0:7] |
| 106 | * indices [16:23] - byte lane 3 (DQS3) DQ[24:31] |
| 107 | * indices [24:31] - byte lane 2 (DQS2) DQ[16:23] |
| 108 | */ |
| 109 | sch = &swizzle_cfg->phys[LP4_PHYS_CH0B]; |
| 110 | memcpy(&cfg->ch_bit_swizzling[0][0], &sch->dqs[LP4_DQS1], sz); |
| 111 | memcpy(&cfg->ch_bit_swizzling[0][8], &sch->dqs[LP4_DQS0], sz); |
| 112 | memcpy(&cfg->ch_bit_swizzling[0][16], &sch->dqs[LP4_DQS3], sz); |
| 113 | memcpy(&cfg->ch_bit_swizzling[0][24], &sch->dqs[LP4_DQS2], sz); |
| 114 | |
| 115 | /* |
| 116 | * CH0_DQA byte lanes in the bit swizzle configuration field are 1:1. |
| 117 | */ |
| 118 | sch = &swizzle_cfg->phys[LP4_PHYS_CH0A]; |
| 119 | memcpy(&cfg->ch_bit_swizzling[1][0], &sch->dqs[LP4_DQS0], sz); |
| 120 | memcpy(&cfg->ch_bit_swizzling[1][8], &sch->dqs[LP4_DQS1], sz); |
| 121 | memcpy(&cfg->ch_bit_swizzling[1][16], &sch->dqs[LP4_DQS2], sz); |
| 122 | memcpy(&cfg->ch_bit_swizzling[1][24], &sch->dqs[LP4_DQS3], sz); |
| 123 | |
| 124 | sch = &swizzle_cfg->phys[LP4_PHYS_CH1B]; |
| 125 | memcpy(&cfg->ch_bit_swizzling[2][0], &sch->dqs[LP4_DQS1], sz); |
| 126 | memcpy(&cfg->ch_bit_swizzling[2][8], &sch->dqs[LP4_DQS0], sz); |
| 127 | memcpy(&cfg->ch_bit_swizzling[2][16], &sch->dqs[LP4_DQS3], sz); |
| 128 | memcpy(&cfg->ch_bit_swizzling[2][24], &sch->dqs[LP4_DQS2], sz); |
| 129 | |
| 130 | /* |
| 131 | * CH0_DQA byte lanes in the bit swizzle configuration field are 1:1. |
| 132 | */ |
| 133 | sch = &swizzle_cfg->phys[LP4_PHYS_CH1A]; |
| 134 | memcpy(&cfg->ch_bit_swizzling[3][0], &sch->dqs[LP4_DQS0], sz); |
| 135 | memcpy(&cfg->ch_bit_swizzling[3][8], &sch->dqs[LP4_DQS1], sz); |
| 136 | memcpy(&cfg->ch_bit_swizzling[3][16], &sch->dqs[LP4_DQS2], sz); |
| 137 | memcpy(&cfg->ch_bit_swizzling[3][24], &sch->dqs[LP4_DQS3], sz); |
| 138 | } |
| 139 | |
| 140 | int fspm_update_config(struct udevice *dev, struct fspm_upd *upd) |
| 141 | { |
| 142 | struct fsp_m_config *cfg = &upd->config; |
| 143 | struct fspm_arch_upd *arch = &upd->arch; |
| 144 | |
| 145 | arch->nvs_buffer_ptr = NULL; |
| 146 | prepare_mrc_cache(upd); |
| 147 | arch->stack_base = (void *)0xfef96000; |
| 148 | arch->boot_loader_tolum_size = 0; |
| 149 | |
| 150 | arch->boot_mode = FSP_BOOT_WITH_FULL_CONFIGURATION; |
| 151 | cfg->serial_debug_port_type = 2; |
| 152 | cfg->serial_debug_port_device = 2; |
| 153 | cfg->serial_debug_port_stride_size = 2; |
| 154 | cfg->serial_debug_port_address = 0; |
| 155 | |
| 156 | cfg->package = 1; |
| 157 | /* Don't enforce a memory size limit */ |
| 158 | cfg->memory_size_limit = 0; |
| 159 | cfg->low_memory_max_value = 2048; /* 2 GB */ |
| 160 | /* No restrictions on memory above 4GiB */ |
| 161 | cfg->high_memory_max_value = 0; |
| 162 | |
| 163 | /* Always default to attempt to use saved training data */ |
| 164 | cfg->disable_fast_boot = 0; |
| 165 | |
| 166 | const u8 *swizzle_data; |
| 167 | |
| 168 | swizzle_data = dev_read_u8_array_ptr(dev, "lpddr4-swizzle", |
| 169 | LP4_NUM_BYTE_LANES * |
| 170 | DQ_BITS_PER_DQS * |
| 171 | LP4_NUM_PHYS_CHANNELS); |
| 172 | if (!swizzle_data) |
| 173 | return log_msg_ret("Cannot read swizzel data", -EINVAL); |
| 174 | |
| 175 | setup_sdram(cfg, (struct lpddr4_swizzle_cfg *)swizzle_data); |
| 176 | |
| 177 | cfg->pre_mem_gpio_table_ptr = 0; |
| 178 | |
| 179 | cfg->profile = 0xb; |
| 180 | cfg->msg_level_mask = 0; |
| 181 | |
| 182 | /* other */ |
| 183 | cfg->skip_cse_rbp = 1; |
| 184 | cfg->periodic_retraining_disable = 0; |
| 185 | cfg->enable_s3_heci2 = 0; |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * The FSP-M binary appears to break the SPI controller. It can be fixed by |
| 192 | * writing the BAR again, so do that here |
| 193 | */ |
| 194 | int fspm_done(struct udevice *dev) |
| 195 | { |
| 196 | struct udevice *spi; |
| 197 | int ret; |
| 198 | |
| 199 | /* Don't probe the device, since that reads the BAR */ |
| 200 | ret = uclass_find_first_device(UCLASS_SPI, &spi); |
| 201 | if (ret) |
| 202 | return log_msg_ret("SPI", ret); |
| 203 | if (!spi) |
| 204 | return log_msg_ret("no SPI", -ENODEV); |
| 205 | |
| 206 | dm_pci_write_config32(spi, PCI_BASE_ADDRESS_0, |
| 207 | IOMAP_SPI_BASE | PCI_BASE_ADDRESS_SPACE_MEMORY); |
| 208 | |
| 209 | return 0; |
| 210 | } |