Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 1 | /* |
Yann Gautier | 634591d | 2021-09-07 09:07:35 +0200 | [diff] [blame] | 2 | * Copyright (C) 2018-2022, STMicroelectronics - All Rights Reserved |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
| 5 | */ |
| 6 | |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 7 | #include <errno.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 10 | #include <arch.h> |
| 11 | #include <arch_helpers.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 12 | #include <common/debug.h> |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 13 | #include <drivers/clk.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 14 | #include <drivers/delay_timer.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 15 | #include <drivers/st/stm32mp1_ddr.h> |
| 16 | #include <drivers/st/stm32mp1_ddr_regs.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 17 | #include <drivers/st/stm32mp1_pwr.h> |
| 18 | #include <drivers/st/stm32mp1_ram.h> |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 19 | #include <drivers/st/stm32mp_ddr.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 20 | #include <lib/mmio.h> |
| 21 | #include <plat/common/platform.h> |
| 22 | |
Yann Gautier | 634591d | 2021-09-07 09:07:35 +0200 | [diff] [blame] | 23 | #include <platform_def.h> |
| 24 | |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 25 | #define DDRCTL_REG(x, y) \ |
| 26 | { \ |
| 27 | .name = #x, \ |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 28 | .offset = offsetof(struct stm32mp_ddrctl, x), \ |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 29 | .par_offset = offsetof(struct y, x) \ |
| 30 | } |
| 31 | |
| 32 | #define DDRPHY_REG(x, y) \ |
| 33 | { \ |
| 34 | .name = #x, \ |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 35 | .offset = offsetof(struct stm32mp_ddrphy, x), \ |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 36 | .par_offset = offsetof(struct y, x) \ |
| 37 | } |
| 38 | |
Yann Gautier | 39d85f4 | 2019-02-25 13:44:27 +0100 | [diff] [blame] | 39 | /* |
| 40 | * PARAMETERS: value get from device tree : |
| 41 | * size / order need to be aligned with binding |
| 42 | * modification NOT ALLOWED !!! |
| 43 | */ |
| 44 | #define DDRCTL_REG_REG_SIZE 25 /* st,ctl-reg */ |
| 45 | #define DDRCTL_REG_TIMING_SIZE 12 /* st,ctl-timing */ |
| 46 | #define DDRCTL_REG_MAP_SIZE 9 /* st,ctl-map */ |
Yann Gautier | 6d8c244 | 2020-09-17 12:42:46 +0200 | [diff] [blame] | 47 | #if STM32MP_DDR_DUAL_AXI_PORT |
Yann Gautier | 39d85f4 | 2019-02-25 13:44:27 +0100 | [diff] [blame] | 48 | #define DDRCTL_REG_PERF_SIZE 17 /* st,ctl-perf */ |
Yann Gautier | 6d8c244 | 2020-09-17 12:42:46 +0200 | [diff] [blame] | 49 | #else |
| 50 | #define DDRCTL_REG_PERF_SIZE 11 /* st,ctl-perf */ |
| 51 | #endif |
Yann Gautier | 39d85f4 | 2019-02-25 13:44:27 +0100 | [diff] [blame] | 52 | |
Yann Gautier | 6d8c244 | 2020-09-17 12:42:46 +0200 | [diff] [blame] | 53 | #if STM32MP_DDR_32BIT_INTERFACE |
Yann Gautier | 39d85f4 | 2019-02-25 13:44:27 +0100 | [diff] [blame] | 54 | #define DDRPHY_REG_REG_SIZE 11 /* st,phy-reg */ |
Yann Gautier | 6d8c244 | 2020-09-17 12:42:46 +0200 | [diff] [blame] | 55 | #else |
| 56 | #define DDRPHY_REG_REG_SIZE 9 /* st,phy-reg */ |
| 57 | #endif |
Yann Gautier | 39d85f4 | 2019-02-25 13:44:27 +0100 | [diff] [blame] | 58 | #define DDRPHY_REG_TIMING_SIZE 10 /* st,phy-timing */ |
| 59 | |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 60 | #define DDRCTL_REG_REG(x) DDRCTL_REG(x, stm32mp1_ddrctrl_reg) |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 61 | static const struct stm32mp_ddr_reg_desc ddr_reg[DDRCTL_REG_REG_SIZE] = { |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 62 | DDRCTL_REG_REG(mstr), |
| 63 | DDRCTL_REG_REG(mrctrl0), |
| 64 | DDRCTL_REG_REG(mrctrl1), |
| 65 | DDRCTL_REG_REG(derateen), |
| 66 | DDRCTL_REG_REG(derateint), |
| 67 | DDRCTL_REG_REG(pwrctl), |
| 68 | DDRCTL_REG_REG(pwrtmg), |
| 69 | DDRCTL_REG_REG(hwlpctl), |
| 70 | DDRCTL_REG_REG(rfshctl0), |
| 71 | DDRCTL_REG_REG(rfshctl3), |
| 72 | DDRCTL_REG_REG(crcparctl0), |
| 73 | DDRCTL_REG_REG(zqctl0), |
| 74 | DDRCTL_REG_REG(dfitmg0), |
| 75 | DDRCTL_REG_REG(dfitmg1), |
| 76 | DDRCTL_REG_REG(dfilpcfg0), |
| 77 | DDRCTL_REG_REG(dfiupd0), |
| 78 | DDRCTL_REG_REG(dfiupd1), |
| 79 | DDRCTL_REG_REG(dfiupd2), |
| 80 | DDRCTL_REG_REG(dfiphymstr), |
| 81 | DDRCTL_REG_REG(odtmap), |
| 82 | DDRCTL_REG_REG(dbg0), |
| 83 | DDRCTL_REG_REG(dbg1), |
| 84 | DDRCTL_REG_REG(dbgcmd), |
| 85 | DDRCTL_REG_REG(poisoncfg), |
| 86 | DDRCTL_REG_REG(pccfg), |
| 87 | }; |
| 88 | |
| 89 | #define DDRCTL_REG_TIMING(x) DDRCTL_REG(x, stm32mp1_ddrctrl_timing) |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 90 | static const struct stm32mp_ddr_reg_desc ddr_timing[DDRCTL_REG_TIMING_SIZE] = { |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 91 | DDRCTL_REG_TIMING(rfshtmg), |
| 92 | DDRCTL_REG_TIMING(dramtmg0), |
| 93 | DDRCTL_REG_TIMING(dramtmg1), |
| 94 | DDRCTL_REG_TIMING(dramtmg2), |
| 95 | DDRCTL_REG_TIMING(dramtmg3), |
| 96 | DDRCTL_REG_TIMING(dramtmg4), |
| 97 | DDRCTL_REG_TIMING(dramtmg5), |
| 98 | DDRCTL_REG_TIMING(dramtmg6), |
| 99 | DDRCTL_REG_TIMING(dramtmg7), |
| 100 | DDRCTL_REG_TIMING(dramtmg8), |
| 101 | DDRCTL_REG_TIMING(dramtmg14), |
| 102 | DDRCTL_REG_TIMING(odtcfg), |
| 103 | }; |
| 104 | |
| 105 | #define DDRCTL_REG_MAP(x) DDRCTL_REG(x, stm32mp1_ddrctrl_map) |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 106 | static const struct stm32mp_ddr_reg_desc ddr_map[DDRCTL_REG_MAP_SIZE] = { |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 107 | DDRCTL_REG_MAP(addrmap1), |
| 108 | DDRCTL_REG_MAP(addrmap2), |
| 109 | DDRCTL_REG_MAP(addrmap3), |
| 110 | DDRCTL_REG_MAP(addrmap4), |
| 111 | DDRCTL_REG_MAP(addrmap5), |
| 112 | DDRCTL_REG_MAP(addrmap6), |
| 113 | DDRCTL_REG_MAP(addrmap9), |
| 114 | DDRCTL_REG_MAP(addrmap10), |
| 115 | DDRCTL_REG_MAP(addrmap11), |
| 116 | }; |
| 117 | |
| 118 | #define DDRCTL_REG_PERF(x) DDRCTL_REG(x, stm32mp1_ddrctrl_perf) |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 119 | static const struct stm32mp_ddr_reg_desc ddr_perf[DDRCTL_REG_PERF_SIZE] = { |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 120 | DDRCTL_REG_PERF(sched), |
| 121 | DDRCTL_REG_PERF(sched1), |
| 122 | DDRCTL_REG_PERF(perfhpr1), |
| 123 | DDRCTL_REG_PERF(perflpr1), |
| 124 | DDRCTL_REG_PERF(perfwr1), |
| 125 | DDRCTL_REG_PERF(pcfgr_0), |
| 126 | DDRCTL_REG_PERF(pcfgw_0), |
| 127 | DDRCTL_REG_PERF(pcfgqos0_0), |
| 128 | DDRCTL_REG_PERF(pcfgqos1_0), |
| 129 | DDRCTL_REG_PERF(pcfgwqos0_0), |
| 130 | DDRCTL_REG_PERF(pcfgwqos1_0), |
Yann Gautier | 6d8c244 | 2020-09-17 12:42:46 +0200 | [diff] [blame] | 131 | #if STM32MP_DDR_DUAL_AXI_PORT |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 132 | DDRCTL_REG_PERF(pcfgr_1), |
| 133 | DDRCTL_REG_PERF(pcfgw_1), |
| 134 | DDRCTL_REG_PERF(pcfgqos0_1), |
| 135 | DDRCTL_REG_PERF(pcfgqos1_1), |
| 136 | DDRCTL_REG_PERF(pcfgwqos0_1), |
| 137 | DDRCTL_REG_PERF(pcfgwqos1_1), |
Yann Gautier | 6d8c244 | 2020-09-17 12:42:46 +0200 | [diff] [blame] | 138 | #endif |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | #define DDRPHY_REG_REG(x) DDRPHY_REG(x, stm32mp1_ddrphy_reg) |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 142 | static const struct stm32mp_ddr_reg_desc ddrphy_reg[DDRPHY_REG_REG_SIZE] = { |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 143 | DDRPHY_REG_REG(pgcr), |
| 144 | DDRPHY_REG_REG(aciocr), |
| 145 | DDRPHY_REG_REG(dxccr), |
| 146 | DDRPHY_REG_REG(dsgcr), |
| 147 | DDRPHY_REG_REG(dcr), |
| 148 | DDRPHY_REG_REG(odtcr), |
| 149 | DDRPHY_REG_REG(zq0cr1), |
| 150 | DDRPHY_REG_REG(dx0gcr), |
| 151 | DDRPHY_REG_REG(dx1gcr), |
Yann Gautier | 6d8c244 | 2020-09-17 12:42:46 +0200 | [diff] [blame] | 152 | #if STM32MP_DDR_32BIT_INTERFACE |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 153 | DDRPHY_REG_REG(dx2gcr), |
| 154 | DDRPHY_REG_REG(dx3gcr), |
Yann Gautier | 6d8c244 | 2020-09-17 12:42:46 +0200 | [diff] [blame] | 155 | #endif |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | #define DDRPHY_REG_TIMING(x) DDRPHY_REG(x, stm32mp1_ddrphy_timing) |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 159 | static const struct stm32mp_ddr_reg_desc ddrphy_timing[DDRPHY_REG_TIMING_SIZE] = { |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 160 | DDRPHY_REG_TIMING(ptr0), |
| 161 | DDRPHY_REG_TIMING(ptr1), |
| 162 | DDRPHY_REG_TIMING(ptr2), |
| 163 | DDRPHY_REG_TIMING(dtpr0), |
| 164 | DDRPHY_REG_TIMING(dtpr1), |
| 165 | DDRPHY_REG_TIMING(dtpr2), |
| 166 | DDRPHY_REG_TIMING(mr0), |
| 167 | DDRPHY_REG_TIMING(mr1), |
| 168 | DDRPHY_REG_TIMING(mr2), |
| 169 | DDRPHY_REG_TIMING(mr3), |
| 170 | }; |
| 171 | |
Yann Gautier | 39d85f4 | 2019-02-25 13:44:27 +0100 | [diff] [blame] | 172 | /* |
| 173 | * REGISTERS ARRAY: used to parse device tree and interactive mode |
| 174 | */ |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 175 | static const struct stm32mp_ddr_reg_info ddr_registers[REG_TYPE_NB] = { |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 176 | [REG_REG] = { |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 177 | .name = "static", |
| 178 | .desc = ddr_reg, |
Yann Gautier | 39d85f4 | 2019-02-25 13:44:27 +0100 | [diff] [blame] | 179 | .size = DDRCTL_REG_REG_SIZE, |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 180 | .base = DDR_BASE |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 181 | }, |
| 182 | [REG_TIMING] = { |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 183 | .name = "timing", |
| 184 | .desc = ddr_timing, |
Yann Gautier | 39d85f4 | 2019-02-25 13:44:27 +0100 | [diff] [blame] | 185 | .size = DDRCTL_REG_TIMING_SIZE, |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 186 | .base = DDR_BASE |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 187 | }, |
| 188 | [REG_PERF] = { |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 189 | .name = "perf", |
| 190 | .desc = ddr_perf, |
Yann Gautier | 39d85f4 | 2019-02-25 13:44:27 +0100 | [diff] [blame] | 191 | .size = DDRCTL_REG_PERF_SIZE, |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 192 | .base = DDR_BASE |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 193 | }, |
| 194 | [REG_MAP] = { |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 195 | .name = "map", |
| 196 | .desc = ddr_map, |
Yann Gautier | 39d85f4 | 2019-02-25 13:44:27 +0100 | [diff] [blame] | 197 | .size = DDRCTL_REG_MAP_SIZE, |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 198 | .base = DDR_BASE |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 199 | }, |
| 200 | [REGPHY_REG] = { |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 201 | .name = "static", |
| 202 | .desc = ddrphy_reg, |
Yann Gautier | 39d85f4 | 2019-02-25 13:44:27 +0100 | [diff] [blame] | 203 | .size = DDRPHY_REG_REG_SIZE, |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 204 | .base = DDRPHY_BASE |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 205 | }, |
| 206 | [REGPHY_TIMING] = { |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 207 | .name = "timing", |
| 208 | .desc = ddrphy_timing, |
Yann Gautier | 39d85f4 | 2019-02-25 13:44:27 +0100 | [diff] [blame] | 209 | .size = DDRPHY_REG_TIMING_SIZE, |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 210 | .base = DDRPHY_BASE |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 211 | }, |
| 212 | }; |
| 213 | |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 214 | static void stm32mp1_ddrphy_idone_wait(struct stm32mp_ddrphy *phy) |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 215 | { |
| 216 | uint32_t pgsr; |
| 217 | int error = 0; |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 218 | uint64_t timeout = timeout_init_us(TIMEOUT_US_1S); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 219 | |
| 220 | do { |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 221 | pgsr = mmio_read_32((uintptr_t)&phy->pgsr); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 222 | |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 223 | VERBOSE(" > [0x%lx] pgsr = 0x%x &\n", |
| 224 | (uintptr_t)&phy->pgsr, pgsr); |
| 225 | |
| 226 | if (timeout_elapsed(timeout)) { |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 227 | panic(); |
| 228 | } |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 229 | |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 230 | if ((pgsr & DDRPHYC_PGSR_DTERR) != 0U) { |
| 231 | VERBOSE("DQS Gate Trainig Error\n"); |
| 232 | error++; |
| 233 | } |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 234 | |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 235 | if ((pgsr & DDRPHYC_PGSR_DTIERR) != 0U) { |
| 236 | VERBOSE("DQS Gate Trainig Intermittent Error\n"); |
| 237 | error++; |
| 238 | } |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 239 | |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 240 | if ((pgsr & DDRPHYC_PGSR_DFTERR) != 0U) { |
| 241 | VERBOSE("DQS Drift Error\n"); |
| 242 | error++; |
| 243 | } |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 244 | |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 245 | if ((pgsr & DDRPHYC_PGSR_RVERR) != 0U) { |
| 246 | VERBOSE("Read Valid Training Error\n"); |
| 247 | error++; |
| 248 | } |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 249 | |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 250 | if ((pgsr & DDRPHYC_PGSR_RVEIRR) != 0U) { |
| 251 | VERBOSE("Read Valid Training Intermittent Error\n"); |
| 252 | error++; |
| 253 | } |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 254 | } while (((pgsr & DDRPHYC_PGSR_IDONE) == 0U) && (error == 0)); |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 255 | VERBOSE("\n[0x%lx] pgsr = 0x%x\n", |
| 256 | (uintptr_t)&phy->pgsr, pgsr); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 257 | } |
| 258 | |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 259 | static void stm32mp1_ddrphy_init(struct stm32mp_ddrphy *phy, uint32_t pir) |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 260 | { |
| 261 | uint32_t pir_init = pir | DDRPHYC_PIR_INIT; |
| 262 | |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 263 | mmio_write_32((uintptr_t)&phy->pir, pir_init); |
| 264 | VERBOSE("[0x%lx] pir = 0x%x -> 0x%x\n", |
| 265 | (uintptr_t)&phy->pir, pir_init, |
| 266 | mmio_read_32((uintptr_t)&phy->pir)); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 267 | |
| 268 | /* Need to wait 10 configuration clock before start polling */ |
| 269 | udelay(10); |
| 270 | |
| 271 | /* Wait DRAM initialization and Gate Training Evaluation complete */ |
| 272 | stm32mp1_ddrphy_idone_wait(phy); |
| 273 | } |
| 274 | |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 275 | /* Wait quasi dynamic register update */ |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 276 | static void stm32mp1_wait_operating_mode(struct stm32mp_ddr_priv *priv, uint32_t mode) |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 277 | { |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 278 | uint64_t timeout; |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 279 | uint32_t stat; |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 280 | int break_loop = 0; |
| 281 | |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 282 | timeout = timeout_init_us(TIMEOUT_US_1S); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 283 | for ( ; ; ) { |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 284 | uint32_t operating_mode; |
| 285 | uint32_t selref_type; |
| 286 | |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 287 | stat = mmio_read_32((uintptr_t)&priv->ctl->stat); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 288 | operating_mode = stat & DDRCTRL_STAT_OPERATING_MODE_MASK; |
| 289 | selref_type = stat & DDRCTRL_STAT_SELFREF_TYPE_MASK; |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 290 | VERBOSE("[0x%lx] stat = 0x%x\n", |
| 291 | (uintptr_t)&priv->ctl->stat, stat); |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 292 | if (timeout_elapsed(timeout)) { |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 293 | panic(); |
| 294 | } |
| 295 | |
| 296 | if (mode == DDRCTRL_STAT_OPERATING_MODE_SR) { |
| 297 | /* |
| 298 | * Self-refresh due to software |
| 299 | * => checking also STAT.selfref_type. |
| 300 | */ |
| 301 | if ((operating_mode == |
| 302 | DDRCTRL_STAT_OPERATING_MODE_SR) && |
| 303 | (selref_type == DDRCTRL_STAT_SELFREF_TYPE_SR)) { |
| 304 | break_loop = 1; |
| 305 | } |
| 306 | } else if (operating_mode == mode) { |
| 307 | break_loop = 1; |
| 308 | } else if ((mode == DDRCTRL_STAT_OPERATING_MODE_NORMAL) && |
| 309 | (operating_mode == DDRCTRL_STAT_OPERATING_MODE_SR) && |
| 310 | (selref_type == DDRCTRL_STAT_SELFREF_TYPE_ASR)) { |
| 311 | /* Normal mode: handle also automatic self refresh */ |
| 312 | break_loop = 1; |
| 313 | } |
| 314 | |
| 315 | if (break_loop == 1) { |
| 316 | break; |
| 317 | } |
| 318 | } |
| 319 | |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 320 | VERBOSE("[0x%lx] stat = 0x%x\n", |
| 321 | (uintptr_t)&priv->ctl->stat, stat); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* Mode Register Writes (MRW or MRS) */ |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 325 | static void stm32mp1_mode_register_write(struct stm32mp_ddr_priv *priv, uint8_t addr, |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 326 | uint32_t data) |
| 327 | { |
| 328 | uint32_t mrctrl0; |
| 329 | |
| 330 | VERBOSE("MRS: %d = %x\n", addr, data); |
| 331 | |
| 332 | /* |
| 333 | * 1. Poll MRSTAT.mr_wr_busy until it is '0'. |
| 334 | * This checks that there is no outstanding MR transaction. |
| 335 | * No write should be performed to MRCTRL0 and MRCTRL1 |
| 336 | * if MRSTAT.mr_wr_busy = 1. |
| 337 | */ |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 338 | while ((mmio_read_32((uintptr_t)&priv->ctl->mrstat) & |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 339 | DDRCTRL_MRSTAT_MR_WR_BUSY) != 0U) { |
| 340 | ; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * 2. Write the MRCTRL0.mr_type, MRCTRL0.mr_addr, MRCTRL0.mr_rank |
| 345 | * and (for MRWs) MRCTRL1.mr_data to define the MR transaction. |
| 346 | */ |
| 347 | mrctrl0 = DDRCTRL_MRCTRL0_MR_TYPE_WRITE | |
| 348 | DDRCTRL_MRCTRL0_MR_RANK_ALL | |
| 349 | (((uint32_t)addr << DDRCTRL_MRCTRL0_MR_ADDR_SHIFT) & |
| 350 | DDRCTRL_MRCTRL0_MR_ADDR_MASK); |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 351 | mmio_write_32((uintptr_t)&priv->ctl->mrctrl0, mrctrl0); |
| 352 | VERBOSE("[0x%lx] mrctrl0 = 0x%x (0x%x)\n", |
| 353 | (uintptr_t)&priv->ctl->mrctrl0, |
| 354 | mmio_read_32((uintptr_t)&priv->ctl->mrctrl0), mrctrl0); |
| 355 | mmio_write_32((uintptr_t)&priv->ctl->mrctrl1, data); |
| 356 | VERBOSE("[0x%lx] mrctrl1 = 0x%x\n", |
| 357 | (uintptr_t)&priv->ctl->mrctrl1, |
| 358 | mmio_read_32((uintptr_t)&priv->ctl->mrctrl1)); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 359 | |
| 360 | /* |
| 361 | * 3. In a separate APB transaction, write the MRCTRL0.mr_wr to 1. This |
| 362 | * bit is self-clearing, and triggers the MR transaction. |
| 363 | * The uMCTL2 then asserts the MRSTAT.mr_wr_busy while it performs |
| 364 | * the MR transaction to SDRAM, and no further access can be |
| 365 | * initiated until it is deasserted. |
| 366 | */ |
| 367 | mrctrl0 |= DDRCTRL_MRCTRL0_MR_WR; |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 368 | mmio_write_32((uintptr_t)&priv->ctl->mrctrl0, mrctrl0); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 369 | |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 370 | while ((mmio_read_32((uintptr_t)&priv->ctl->mrstat) & |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 371 | DDRCTRL_MRSTAT_MR_WR_BUSY) != 0U) { |
| 372 | ; |
| 373 | } |
| 374 | |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 375 | VERBOSE("[0x%lx] mrctrl0 = 0x%x\n", |
| 376 | (uintptr_t)&priv->ctl->mrctrl0, mrctrl0); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* Switch DDR3 from DLL-on to DLL-off */ |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 380 | static void stm32mp1_ddr3_dll_off(struct stm32mp_ddr_priv *priv) |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 381 | { |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 382 | uint32_t mr1 = mmio_read_32((uintptr_t)&priv->phy->mr1); |
| 383 | uint32_t mr2 = mmio_read_32((uintptr_t)&priv->phy->mr2); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 384 | uint32_t dbgcam; |
| 385 | |
| 386 | VERBOSE("mr1: 0x%x\n", mr1); |
| 387 | VERBOSE("mr2: 0x%x\n", mr2); |
| 388 | |
| 389 | /* |
| 390 | * 1. Set the DBG1.dis_hif = 1. |
| 391 | * This prevents further reads/writes being received on the HIF. |
| 392 | */ |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 393 | mmio_setbits_32((uintptr_t)&priv->ctl->dbg1, DDRCTRL_DBG1_DIS_HIF); |
| 394 | VERBOSE("[0x%lx] dbg1 = 0x%x\n", |
| 395 | (uintptr_t)&priv->ctl->dbg1, |
| 396 | mmio_read_32((uintptr_t)&priv->ctl->dbg1)); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 397 | |
| 398 | /* |
| 399 | * 2. Ensure all commands have been flushed from the uMCTL2 by polling |
| 400 | * DBGCAM.wr_data_pipeline_empty = 1, |
| 401 | * DBGCAM.rd_data_pipeline_empty = 1, |
| 402 | * DBGCAM.dbg_wr_q_depth = 0 , |
| 403 | * DBGCAM.dbg_lpr_q_depth = 0, and |
| 404 | * DBGCAM.dbg_hpr_q_depth = 0. |
| 405 | */ |
| 406 | do { |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 407 | dbgcam = mmio_read_32((uintptr_t)&priv->ctl->dbgcam); |
| 408 | VERBOSE("[0x%lx] dbgcam = 0x%x\n", |
| 409 | (uintptr_t)&priv->ctl->dbgcam, dbgcam); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 410 | } while ((((dbgcam & DDRCTRL_DBGCAM_DATA_PIPELINE_EMPTY) == |
| 411 | DDRCTRL_DBGCAM_DATA_PIPELINE_EMPTY)) && |
| 412 | ((dbgcam & DDRCTRL_DBGCAM_DBG_Q_DEPTH) == 0U)); |
| 413 | |
| 414 | /* |
| 415 | * 3. Perform an MRS command (using MRCTRL0 and MRCTRL1 registers) |
| 416 | * to disable RTT_NOM: |
| 417 | * a. DDR3: Write to MR1[9], MR1[6] and MR1[2] |
| 418 | * b. DDR4: Write to MR1[10:8] |
| 419 | */ |
| 420 | mr1 &= ~(BIT(9) | BIT(6) | BIT(2)); |
| 421 | stm32mp1_mode_register_write(priv, 1, mr1); |
| 422 | |
| 423 | /* |
| 424 | * 4. For DDR4 only: Perform an MRS command |
| 425 | * (using MRCTRL0 and MRCTRL1 registers) to write to MR5[8:6] |
| 426 | * to disable RTT_PARK |
| 427 | */ |
| 428 | |
| 429 | /* |
| 430 | * 5. Perform an MRS command (using MRCTRL0 and MRCTRL1 registers) |
| 431 | * to write to MR2[10:9], to disable RTT_WR |
| 432 | * (and therefore disable dynamic ODT). |
| 433 | * This applies for both DDR3 and DDR4. |
| 434 | */ |
| 435 | mr2 &= ~GENMASK(10, 9); |
| 436 | stm32mp1_mode_register_write(priv, 2, mr2); |
| 437 | |
| 438 | /* |
| 439 | * 6. Perform an MRS command (using MRCTRL0 and MRCTRL1 registers) |
| 440 | * to disable the DLL. The timing of this MRS is automatically |
| 441 | * handled by the uMCTL2. |
| 442 | * a. DDR3: Write to MR1[0] |
| 443 | * b. DDR4: Write to MR1[0] |
| 444 | */ |
| 445 | mr1 |= BIT(0); |
| 446 | stm32mp1_mode_register_write(priv, 1, mr1); |
| 447 | |
| 448 | /* |
| 449 | * 7. Put the SDRAM into self-refresh mode by setting |
| 450 | * PWRCTL.selfref_sw = 1, and polling STAT.operating_mode to ensure |
| 451 | * the DDRC has entered self-refresh. |
| 452 | */ |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 453 | mmio_setbits_32((uintptr_t)&priv->ctl->pwrctl, |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 454 | DDRCTRL_PWRCTL_SELFREF_SW); |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 455 | VERBOSE("[0x%lx] pwrctl = 0x%x\n", |
| 456 | (uintptr_t)&priv->ctl->pwrctl, |
| 457 | mmio_read_32((uintptr_t)&priv->ctl->pwrctl)); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 458 | |
| 459 | /* |
| 460 | * 8. Wait until STAT.operating_mode[1:0]==11 indicating that the |
| 461 | * DWC_ddr_umctl2 core is in self-refresh mode. |
| 462 | * Ensure transition to self-refresh was due to software |
| 463 | * by checking that STAT.selfref_type[1:0]=2. |
| 464 | */ |
| 465 | stm32mp1_wait_operating_mode(priv, DDRCTRL_STAT_OPERATING_MODE_SR); |
| 466 | |
| 467 | /* |
| 468 | * 9. Set the MSTR.dll_off_mode = 1. |
| 469 | * warning: MSTR.dll_off_mode is a quasi-dynamic type 2 field |
| 470 | */ |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 471 | stm32mp_ddr_start_sw_done(priv->ctl); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 472 | |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 473 | mmio_setbits_32((uintptr_t)&priv->ctl->mstr, DDRCTRL_MSTR_DLL_OFF_MODE); |
| 474 | VERBOSE("[0x%lx] mstr = 0x%x\n", |
| 475 | (uintptr_t)&priv->ctl->mstr, |
| 476 | mmio_read_32((uintptr_t)&priv->ctl->mstr)); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 477 | |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 478 | stm32mp_ddr_wait_sw_done_ack(priv->ctl); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 479 | |
| 480 | /* 10. Change the clock frequency to the desired value. */ |
| 481 | |
| 482 | /* |
| 483 | * 11. Update any registers which may be required to change for the new |
| 484 | * frequency. This includes static and dynamic registers. |
| 485 | * This includes both uMCTL2 registers and PHY registers. |
| 486 | */ |
| 487 | |
| 488 | /* Change Bypass Mode Frequency Range */ |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 489 | if (clk_get_rate(DDRPHYC) < 100000000U) { |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 490 | mmio_clrbits_32((uintptr_t)&priv->phy->dllgcr, |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 491 | DDRPHYC_DLLGCR_BPS200); |
| 492 | } else { |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 493 | mmio_setbits_32((uintptr_t)&priv->phy->dllgcr, |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 494 | DDRPHYC_DLLGCR_BPS200); |
| 495 | } |
| 496 | |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 497 | mmio_setbits_32((uintptr_t)&priv->phy->acdllcr, DDRPHYC_ACDLLCR_DLLDIS); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 498 | |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 499 | mmio_setbits_32((uintptr_t)&priv->phy->dx0dllcr, |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 500 | DDRPHYC_DXNDLLCR_DLLDIS); |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 501 | mmio_setbits_32((uintptr_t)&priv->phy->dx1dllcr, |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 502 | DDRPHYC_DXNDLLCR_DLLDIS); |
Yann Gautier | 6d8c244 | 2020-09-17 12:42:46 +0200 | [diff] [blame] | 503 | #if STM32MP_DDR_32BIT_INTERFACE |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 504 | mmio_setbits_32((uintptr_t)&priv->phy->dx2dllcr, |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 505 | DDRPHYC_DXNDLLCR_DLLDIS); |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 506 | mmio_setbits_32((uintptr_t)&priv->phy->dx3dllcr, |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 507 | DDRPHYC_DXNDLLCR_DLLDIS); |
Yann Gautier | 6d8c244 | 2020-09-17 12:42:46 +0200 | [diff] [blame] | 508 | #endif |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 509 | |
| 510 | /* 12. Exit the self-refresh state by setting PWRCTL.selfref_sw = 0. */ |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 511 | mmio_clrbits_32((uintptr_t)&priv->ctl->pwrctl, |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 512 | DDRCTRL_PWRCTL_SELFREF_SW); |
| 513 | stm32mp1_wait_operating_mode(priv, DDRCTRL_STAT_OPERATING_MODE_NORMAL); |
| 514 | |
| 515 | /* |
| 516 | * 13. If ZQCTL0.dis_srx_zqcl = 0, the uMCTL2 performs a ZQCL command |
| 517 | * at this point. |
| 518 | */ |
| 519 | |
| 520 | /* |
| 521 | * 14. Perform MRS commands as required to re-program timing registers |
| 522 | * in the SDRAM for the new frequency |
| 523 | * (in particular, CL, CWL and WR may need to be changed). |
| 524 | */ |
| 525 | |
| 526 | /* 15. Write DBG1.dis_hif = 0 to re-enable reads and writes. */ |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 527 | mmio_clrbits_32((uintptr_t)&priv->ctl->dbg1, DDRCTRL_DBG1_DIS_HIF); |
| 528 | VERBOSE("[0x%lx] dbg1 = 0x%x\n", |
| 529 | (uintptr_t)&priv->ctl->dbg1, |
| 530 | mmio_read_32((uintptr_t)&priv->ctl->dbg1)); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 531 | } |
| 532 | |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 533 | static void stm32mp1_refresh_disable(struct stm32mp_ddrctl *ctl) |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 534 | { |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 535 | stm32mp_ddr_start_sw_done(ctl); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 536 | /* Quasi-dynamic register update*/ |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 537 | mmio_setbits_32((uintptr_t)&ctl->rfshctl3, |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 538 | DDRCTRL_RFSHCTL3_DIS_AUTO_REFRESH); |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 539 | mmio_clrbits_32((uintptr_t)&ctl->pwrctl, DDRCTRL_PWRCTL_POWERDOWN_EN); |
| 540 | mmio_clrbits_32((uintptr_t)&ctl->dfimisc, |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 541 | DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN); |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 542 | stm32mp_ddr_wait_sw_done_ack(ctl); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 543 | } |
| 544 | |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 545 | static void stm32mp1_refresh_restore(struct stm32mp_ddrctl *ctl, |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 546 | uint32_t rfshctl3, uint32_t pwrctl) |
| 547 | { |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 548 | stm32mp_ddr_start_sw_done(ctl); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 549 | if ((rfshctl3 & DDRCTRL_RFSHCTL3_DIS_AUTO_REFRESH) == 0U) { |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 550 | mmio_clrbits_32((uintptr_t)&ctl->rfshctl3, |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 551 | DDRCTRL_RFSHCTL3_DIS_AUTO_REFRESH); |
| 552 | } |
| 553 | if ((pwrctl & DDRCTRL_PWRCTL_POWERDOWN_EN) != 0U) { |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 554 | mmio_setbits_32((uintptr_t)&ctl->pwrctl, |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 555 | DDRCTRL_PWRCTL_POWERDOWN_EN); |
| 556 | } |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 557 | mmio_setbits_32((uintptr_t)&ctl->dfimisc, |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 558 | DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN); |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 559 | stm32mp_ddr_wait_sw_done_ack(ctl); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 560 | } |
| 561 | |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 562 | void stm32mp1_ddr_init(struct stm32mp_ddr_priv *priv, |
| 563 | struct stm32mp_ddr_config *config) |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 564 | { |
| 565 | uint32_t pir; |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 566 | int ret = -EINVAL; |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 567 | |
| 568 | if ((config->c_reg.mstr & DDRCTRL_MSTR_DDR3) != 0U) { |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 569 | ret = stm32mp_board_ddr_power_init(STM32MP_DDR3); |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 570 | } else if ((config->c_reg.mstr & DDRCTRL_MSTR_LPDDR2) != 0U) { |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 571 | ret = stm32mp_board_ddr_power_init(STM32MP_LPDDR2); |
Yann Gautier | 917a00c | 2019-04-16 16:20:58 +0200 | [diff] [blame] | 572 | } else if ((config->c_reg.mstr & DDRCTRL_MSTR_LPDDR3) != 0U) { |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 573 | ret = stm32mp_board_ddr_power_init(STM32MP_LPDDR3); |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 574 | } else { |
| 575 | ERROR("DDR type not supported\n"); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | if (ret != 0) { |
| 579 | panic(); |
| 580 | } |
| 581 | |
| 582 | VERBOSE("name = %s\n", config->info.name); |
Yann Gautier | 634591d | 2021-09-07 09:07:35 +0200 | [diff] [blame] | 583 | VERBOSE("speed = %u kHz\n", config->info.speed); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 584 | VERBOSE("size = 0x%x\n", config->info.size); |
| 585 | |
| 586 | /* DDR INIT SEQUENCE */ |
| 587 | |
| 588 | /* |
| 589 | * 1. Program the DWC_ddr_umctl2 registers |
| 590 | * nota: check DFIMISC.dfi_init_complete = 0 |
| 591 | */ |
| 592 | |
| 593 | /* 1.1 RESETS: presetn, core_ddrc_rstn, aresetn */ |
| 594 | mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAPBRST); |
| 595 | mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAXIRST); |
| 596 | mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCORERST); |
| 597 | mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYAPBRST); |
| 598 | mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYRST); |
| 599 | mmio_setbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYCTLRST); |
| 600 | |
| 601 | /* 1.2. start CLOCK */ |
| 602 | if (stm32mp1_ddr_clk_enable(priv, config->info.speed) != 0) { |
| 603 | panic(); |
| 604 | } |
| 605 | |
| 606 | /* 1.3. deassert reset */ |
| 607 | /* De-assert PHY rstn and ctl_rstn via DPHYRST and DPHYCTLRST. */ |
| 608 | mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYRST); |
| 609 | mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYCTLRST); |
| 610 | /* |
| 611 | * De-assert presetn once the clocks are active |
| 612 | * and stable via DDRCAPBRST bit. |
| 613 | */ |
| 614 | mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAPBRST); |
| 615 | |
| 616 | /* 1.4. wait 128 cycles to permit initialization of end logic */ |
| 617 | udelay(2); |
| 618 | /* For PCLK = 133MHz => 1 us is enough, 2 to allow lower frequency */ |
| 619 | |
| 620 | /* 1.5. initialize registers ddr_umctl2 */ |
| 621 | /* Stop uMCTL2 before PHY is ready */ |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 622 | mmio_clrbits_32((uintptr_t)&priv->ctl->dfimisc, |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 623 | DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN); |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 624 | VERBOSE("[0x%lx] dfimisc = 0x%x\n", |
| 625 | (uintptr_t)&priv->ctl->dfimisc, |
| 626 | mmio_read_32((uintptr_t)&priv->ctl->dfimisc)); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 627 | |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 628 | stm32mp_ddr_set_reg(priv, REG_REG, &config->c_reg, ddr_registers); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 629 | |
| 630 | /* DDR3 = don't set DLLOFF for init mode */ |
| 631 | if ((config->c_reg.mstr & |
| 632 | (DDRCTRL_MSTR_DDR3 | DDRCTRL_MSTR_DLL_OFF_MODE)) |
| 633 | == (DDRCTRL_MSTR_DDR3 | DDRCTRL_MSTR_DLL_OFF_MODE)) { |
| 634 | VERBOSE("deactivate DLL OFF in mstr\n"); |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 635 | mmio_clrbits_32((uintptr_t)&priv->ctl->mstr, |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 636 | DDRCTRL_MSTR_DLL_OFF_MODE); |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 637 | VERBOSE("[0x%lx] mstr = 0x%x\n", |
| 638 | (uintptr_t)&priv->ctl->mstr, |
| 639 | mmio_read_32((uintptr_t)&priv->ctl->mstr)); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 640 | } |
| 641 | |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 642 | stm32mp_ddr_set_reg(priv, REG_TIMING, &config->c_timing, ddr_registers); |
| 643 | stm32mp_ddr_set_reg(priv, REG_MAP, &config->c_map, ddr_registers); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 644 | |
| 645 | /* Skip CTRL init, SDRAM init is done by PHY PUBL */ |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 646 | mmio_clrsetbits_32((uintptr_t)&priv->ctl->init0, |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 647 | DDRCTRL_INIT0_SKIP_DRAM_INIT_MASK, |
| 648 | DDRCTRL_INIT0_SKIP_DRAM_INIT_NORMAL); |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 649 | VERBOSE("[0x%lx] init0 = 0x%x\n", |
| 650 | (uintptr_t)&priv->ctl->init0, |
| 651 | mmio_read_32((uintptr_t)&priv->ctl->init0)); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 652 | |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 653 | stm32mp_ddr_set_reg(priv, REG_PERF, &config->c_perf, ddr_registers); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 654 | |
| 655 | /* 2. deassert reset signal core_ddrc_rstn, aresetn and presetn */ |
| 656 | mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCORERST); |
| 657 | mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DDRCAXIRST); |
| 658 | mmio_clrbits_32(priv->rcc + RCC_DDRITFCR, RCC_DDRITFCR_DPHYAPBRST); |
| 659 | |
| 660 | /* |
| 661 | * 3. start PHY init by accessing relevant PUBL registers |
| 662 | * (DXGCR, DCR, PTR*, MR*, DTPR*) |
| 663 | */ |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 664 | stm32mp_ddr_set_reg(priv, REGPHY_REG, &config->p_reg, ddr_registers); |
| 665 | stm32mp_ddr_set_reg(priv, REGPHY_TIMING, &config->p_timing, ddr_registers); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 666 | |
| 667 | /* DDR3 = don't set DLLOFF for init mode */ |
| 668 | if ((config->c_reg.mstr & |
| 669 | (DDRCTRL_MSTR_DDR3 | DDRCTRL_MSTR_DLL_OFF_MODE)) |
| 670 | == (DDRCTRL_MSTR_DDR3 | DDRCTRL_MSTR_DLL_OFF_MODE)) { |
| 671 | VERBOSE("deactivate DLL OFF in mr1\n"); |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 672 | mmio_clrbits_32((uintptr_t)&priv->phy->mr1, BIT(0)); |
| 673 | VERBOSE("[0x%lx] mr1 = 0x%x\n", |
| 674 | (uintptr_t)&priv->phy->mr1, |
| 675 | mmio_read_32((uintptr_t)&priv->phy->mr1)); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | /* |
| 679 | * 4. Monitor PHY init status by polling PUBL register PGSR.IDONE |
| 680 | * Perform DDR PHY DRAM initialization and Gate Training Evaluation |
| 681 | */ |
| 682 | stm32mp1_ddrphy_idone_wait(priv->phy); |
| 683 | |
| 684 | /* |
| 685 | * 5. Indicate to PUBL that controller performs SDRAM initialization |
| 686 | * by setting PIR.INIT and PIR CTLDINIT and pool PGSR.IDONE |
| 687 | * DRAM init is done by PHY, init0.skip_dram.init = 1 |
| 688 | */ |
| 689 | |
| 690 | pir = DDRPHYC_PIR_DLLSRST | DDRPHYC_PIR_DLLLOCK | DDRPHYC_PIR_ZCAL | |
| 691 | DDRPHYC_PIR_ITMSRST | DDRPHYC_PIR_DRAMINIT | DDRPHYC_PIR_ICPC; |
| 692 | |
| 693 | if ((config->c_reg.mstr & DDRCTRL_MSTR_DDR3) != 0U) { |
| 694 | pir |= DDRPHYC_PIR_DRAMRST; /* Only for DDR3 */ |
| 695 | } |
| 696 | |
| 697 | stm32mp1_ddrphy_init(priv->phy, pir); |
| 698 | |
| 699 | /* |
| 700 | * 6. SET DFIMISC.dfi_init_complete_en to 1 |
| 701 | * Enable quasi-dynamic register programming. |
| 702 | */ |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 703 | stm32mp_ddr_start_sw_done(priv->ctl); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 704 | |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 705 | mmio_setbits_32((uintptr_t)&priv->ctl->dfimisc, |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 706 | DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN); |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 707 | VERBOSE("[0x%lx] dfimisc = 0x%x\n", |
| 708 | (uintptr_t)&priv->ctl->dfimisc, |
| 709 | mmio_read_32((uintptr_t)&priv->ctl->dfimisc)); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 710 | |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 711 | stm32mp_ddr_wait_sw_done_ack(priv->ctl); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 712 | |
| 713 | /* |
| 714 | * 7. Wait for DWC_ddr_umctl2 to move to normal operation mode |
| 715 | * by monitoring STAT.operating_mode signal |
| 716 | */ |
| 717 | |
| 718 | /* Wait uMCTL2 ready */ |
| 719 | stm32mp1_wait_operating_mode(priv, DDRCTRL_STAT_OPERATING_MODE_NORMAL); |
| 720 | |
| 721 | /* Switch to DLL OFF mode */ |
| 722 | if ((config->c_reg.mstr & DDRCTRL_MSTR_DLL_OFF_MODE) != 0U) { |
| 723 | stm32mp1_ddr3_dll_off(priv); |
| 724 | } |
| 725 | |
| 726 | VERBOSE("DDR DQS training : "); |
| 727 | |
| 728 | /* |
| 729 | * 8. Disable Auto refresh and power down by setting |
| 730 | * - RFSHCTL3.dis_au_refresh = 1 |
| 731 | * - PWRCTL.powerdown_en = 0 |
| 732 | * - DFIMISC.dfiinit_complete_en = 0 |
| 733 | */ |
| 734 | stm32mp1_refresh_disable(priv->ctl); |
| 735 | |
| 736 | /* |
| 737 | * 9. Program PUBL PGCR to enable refresh during training |
| 738 | * and rank to train |
| 739 | * not done => keep the programed value in PGCR |
| 740 | */ |
| 741 | |
| 742 | /* |
| 743 | * 10. configure PUBL PIR register to specify which training step |
| 744 | * to run |
Nicolas Le Bayon | e22424a | 2021-09-10 12:03:38 +0200 | [diff] [blame] | 745 | * RVTRN is executed only on LPDDR2/LPDDR3 |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 746 | */ |
Nicolas Le Bayon | e22424a | 2021-09-10 12:03:38 +0200 | [diff] [blame] | 747 | pir = DDRPHYC_PIR_QSTRN; |
| 748 | if ((config->c_reg.mstr & DDRCTRL_MSTR_DDR3) == 0U) { |
| 749 | pir |= DDRPHYC_PIR_RVTRN; |
| 750 | } |
| 751 | |
| 752 | stm32mp1_ddrphy_init(priv->phy, pir); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 753 | |
| 754 | /* 11. monitor PUB PGSR.IDONE to poll cpmpletion of training sequence */ |
| 755 | stm32mp1_ddrphy_idone_wait(priv->phy); |
| 756 | |
| 757 | /* |
Elyes Haouas | 2be03c0 | 2023-02-13 09:14:48 +0100 | [diff] [blame] | 758 | * 12. set back registers in step 8 to the original values if desidered |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 759 | */ |
| 760 | stm32mp1_refresh_restore(priv->ctl, config->c_reg.rfshctl3, |
| 761 | config->c_reg.pwrctl); |
| 762 | |
Nicolas Le Bayon | 8ce825f | 2021-05-18 10:01:30 +0200 | [diff] [blame] | 763 | stm32mp_ddr_enable_axi_port(priv->ctl); |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 764 | } |