Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Vikas Manocha | 2479609 | 2017-04-10 15:02:51 -0700 | [diff] [blame] | 2 | /* |
Patrice Chotard | 789ee0e | 2017-10-23 09:53:58 +0200 | [diff] [blame] | 3 | * Copyright (C) 2017, STMicroelectronics - All Rights Reserved |
| 4 | * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics. |
Vikas Manocha | 2479609 | 2017-04-10 15:02:51 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Vikas Manocha | f9c6e6a | 2017-04-10 15:02:55 -0700 | [diff] [blame] | 8 | #include <clk.h> |
Vikas Manocha | aa88e1a | 2017-04-10 15:02:52 -0700 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame^] | 10 | #include <init.h> |
Vikas Manocha | aa88e1a | 2017-04-10 15:02:52 -0700 | [diff] [blame] | 11 | #include <ram.h> |
Vikas Manocha | 2479609 | 2017-04-10 15:02:51 -0700 | [diff] [blame] | 12 | #include <asm/io.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <dm/device_compat.h> |
Vikas Manocha | 2479609 | 2017-04-10 15:02:51 -0700 | [diff] [blame] | 14 | |
Patrice Chotard | 63e9728 | 2017-12-12 09:49:41 +0100 | [diff] [blame] | 15 | #define MEM_MODE_MASK GENMASK(2, 0) |
Radoslaw Pietrzyk | a88d314 | 2018-05-16 17:27:11 +0200 | [diff] [blame] | 16 | #define SWP_FMC_OFFSET 10 |
| 17 | #define SWP_FMC_MASK GENMASK(SWP_FMC_OFFSET+1, SWP_FMC_OFFSET) |
Patrice Chotard | 63e9728 | 2017-12-12 09:49:41 +0100 | [diff] [blame] | 18 | #define NOT_FOUND 0xff |
| 19 | |
Patrice Chotard | 7e82a69 | 2017-07-18 17:37:24 +0200 | [diff] [blame] | 20 | struct stm32_fmc_regs { |
Patrice Chotard | f2b8000 | 2017-07-18 17:37:25 +0200 | [diff] [blame] | 21 | /* 0x0 */ |
| 22 | u32 bcr1; /* NOR/PSRAM Chip select control register 1 */ |
| 23 | u32 btr1; /* SRAM/NOR-Flash Chip select timing register 1 */ |
| 24 | u32 bcr2; /* NOR/PSRAM Chip select Control register 2 */ |
| 25 | u32 btr2; /* SRAM/NOR-Flash Chip select timing register 2 */ |
| 26 | u32 bcr3; /* NOR/PSRAMChip select Control register 3 */ |
| 27 | u32 btr3; /* SRAM/NOR-Flash Chip select timing register 3 */ |
| 28 | u32 bcr4; /* NOR/PSRAM Chip select Control register 4 */ |
| 29 | u32 btr4; /* SRAM/NOR-Flash Chip select timing register 4 */ |
| 30 | u32 reserved1[24]; |
| 31 | |
| 32 | /* 0x80 */ |
| 33 | u32 pcr; /* NAND Flash control register */ |
| 34 | u32 sr; /* FIFO status and interrupt register */ |
| 35 | u32 pmem; /* Common memory space timing register */ |
| 36 | u32 patt; /* Attribute memory space timing registers */ |
| 37 | u32 reserved2[1]; |
| 38 | u32 eccr; /* ECC result registers */ |
| 39 | u32 reserved3[27]; |
Patrice Chotard | 7e82a69 | 2017-07-18 17:37:24 +0200 | [diff] [blame] | 40 | |
Patrice Chotard | f2b8000 | 2017-07-18 17:37:25 +0200 | [diff] [blame] | 41 | /* 0x104 */ |
| 42 | u32 bwtr1; /* SRAM/NOR-Flash write timing register 1 */ |
| 43 | u32 reserved4[1]; |
| 44 | u32 bwtr2; /* SRAM/NOR-Flash write timing register 2 */ |
| 45 | u32 reserved5[1]; |
| 46 | u32 bwtr3; /* SRAM/NOR-Flash write timing register 3 */ |
| 47 | u32 reserved6[1]; |
| 48 | u32 bwtr4; /* SRAM/NOR-Flash write timing register 4 */ |
| 49 | u32 reserved7[8]; |
| 50 | |
| 51 | /* 0x140 */ |
| 52 | u32 sdcr1; /* SDRAM Control register 1 */ |
| 53 | u32 sdcr2; /* SDRAM Control register 2 */ |
| 54 | u32 sdtr1; /* SDRAM Timing register 1 */ |
| 55 | u32 sdtr2; /* SDRAM Timing register 2 */ |
| 56 | u32 sdcmr; /* SDRAM Mode register */ |
| 57 | u32 sdrtr; /* SDRAM Refresh timing register */ |
| 58 | u32 sdsr; /* SDRAM Status register */ |
| 59 | }; |
Patrice Chotard | 7e82a69 | 2017-07-18 17:37:24 +0200 | [diff] [blame] | 60 | |
Patrice Chotard | 7c695ce | 2017-07-18 17:37:29 +0200 | [diff] [blame] | 61 | /* |
| 62 | * NOR/PSRAM Control register BCR1 |
| 63 | * FMC controller Enable, only availabe for H7 |
| 64 | */ |
| 65 | #define FMC_BCR1_FMCEN BIT(31) |
| 66 | |
Patrice Chotard | 7e82a69 | 2017-07-18 17:37:24 +0200 | [diff] [blame] | 67 | /* Control register SDCR */ |
| 68 | #define FMC_SDCR_RPIPE_SHIFT 13 /* RPIPE bit shift */ |
| 69 | #define FMC_SDCR_RBURST_SHIFT 12 /* RBURST bit shift */ |
| 70 | #define FMC_SDCR_SDCLK_SHIFT 10 /* SDRAM clock divisor shift */ |
| 71 | #define FMC_SDCR_WP_SHIFT 9 /* Write protection shift */ |
| 72 | #define FMC_SDCR_CAS_SHIFT 7 /* CAS latency shift */ |
| 73 | #define FMC_SDCR_NB_SHIFT 6 /* Number of banks shift */ |
| 74 | #define FMC_SDCR_MWID_SHIFT 4 /* Memory width shift */ |
| 75 | #define FMC_SDCR_NR_SHIFT 2 /* Number of row address bits shift */ |
| 76 | #define FMC_SDCR_NC_SHIFT 0 /* Number of col address bits shift */ |
| 77 | |
| 78 | /* Timings register SDTR */ |
| 79 | #define FMC_SDTR_TMRD_SHIFT 0 /* Load mode register to active */ |
| 80 | #define FMC_SDTR_TXSR_SHIFT 4 /* Exit self-refresh time */ |
| 81 | #define FMC_SDTR_TRAS_SHIFT 8 /* Self-refresh time */ |
| 82 | #define FMC_SDTR_TRC_SHIFT 12 /* Row cycle delay */ |
| 83 | #define FMC_SDTR_TWR_SHIFT 16 /* Recovery delay */ |
| 84 | #define FMC_SDTR_TRP_SHIFT 20 /* Row precharge delay */ |
| 85 | #define FMC_SDTR_TRCD_SHIFT 24 /* Row-to-column delay */ |
| 86 | |
| 87 | #define FMC_SDCMR_NRFS_SHIFT 5 |
| 88 | |
| 89 | #define FMC_SDCMR_MODE_NORMAL 0 |
| 90 | #define FMC_SDCMR_MODE_START_CLOCK 1 |
| 91 | #define FMC_SDCMR_MODE_PRECHARGE 2 |
| 92 | #define FMC_SDCMR_MODE_AUTOREFRESH 3 |
| 93 | #define FMC_SDCMR_MODE_WRITE_MODE 4 |
| 94 | #define FMC_SDCMR_MODE_SELFREFRESH 5 |
| 95 | #define FMC_SDCMR_MODE_POWERDOWN 6 |
| 96 | |
| 97 | #define FMC_SDCMR_BANK_1 BIT(4) |
| 98 | #define FMC_SDCMR_BANK_2 BIT(3) |
| 99 | |
| 100 | #define FMC_SDCMR_MODE_REGISTER_SHIFT 9 |
| 101 | |
| 102 | #define FMC_SDSR_BUSY BIT(5) |
| 103 | |
Patrice Chotard | f2b8000 | 2017-07-18 17:37:25 +0200 | [diff] [blame] | 104 | #define FMC_BUSY_WAIT(regs) do { \ |
Patrice Chotard | 7e82a69 | 2017-07-18 17:37:24 +0200 | [diff] [blame] | 105 | __asm__ __volatile__ ("dsb" : : : "memory"); \ |
Patrice Chotard | f2b8000 | 2017-07-18 17:37:25 +0200 | [diff] [blame] | 106 | while (regs->sdsr & FMC_SDSR_BUSY) \ |
Patrice Chotard | 7e82a69 | 2017-07-18 17:37:24 +0200 | [diff] [blame] | 107 | ; \ |
| 108 | } while (0) |
| 109 | |
Vikas Manocha | 4515ffe | 2017-04-10 15:02:56 -0700 | [diff] [blame] | 110 | struct stm32_sdram_control { |
| 111 | u8 no_columns; |
| 112 | u8 no_rows; |
| 113 | u8 memory_width; |
| 114 | u8 no_banks; |
| 115 | u8 cas_latency; |
Vikas Manocha | 1c6459d | 2017-04-10 15:03:03 -0700 | [diff] [blame] | 116 | u8 sdclk; |
Vikas Manocha | 4515ffe | 2017-04-10 15:02:56 -0700 | [diff] [blame] | 117 | u8 rd_burst; |
| 118 | u8 rd_pipe_delay; |
| 119 | }; |
| 120 | |
| 121 | struct stm32_sdram_timing { |
| 122 | u8 tmrd; |
| 123 | u8 txsr; |
| 124 | u8 tras; |
| 125 | u8 trc; |
| 126 | u8 trp; |
Vikas Manocha | 1c6459d | 2017-04-10 15:03:03 -0700 | [diff] [blame] | 127 | u8 twr; |
Vikas Manocha | 4515ffe | 2017-04-10 15:02:56 -0700 | [diff] [blame] | 128 | u8 trcd; |
| 129 | }; |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 130 | enum stm32_fmc_bank { |
| 131 | SDRAM_BANK1, |
| 132 | SDRAM_BANK2, |
| 133 | MAX_SDRAM_BANK, |
| 134 | }; |
| 135 | |
Patrice Chotard | 7c695ce | 2017-07-18 17:37:29 +0200 | [diff] [blame] | 136 | enum stm32_fmc_family { |
| 137 | STM32F7_FMC, |
| 138 | STM32H7_FMC, |
| 139 | }; |
| 140 | |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 141 | struct bank_params { |
Patrice Chotard | 8b37922 | 2017-07-18 17:37:26 +0200 | [diff] [blame] | 142 | struct stm32_sdram_control *sdram_control; |
| 143 | struct stm32_sdram_timing *sdram_timing; |
Vikas Manocha | 1c6459d | 2017-04-10 15:03:03 -0700 | [diff] [blame] | 144 | u32 sdram_ref_count; |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 145 | enum stm32_fmc_bank target_bank; |
Vikas Manocha | 4515ffe | 2017-04-10 15:02:56 -0700 | [diff] [blame] | 146 | }; |
Vikas Manocha | 2479609 | 2017-04-10 15:02:51 -0700 | [diff] [blame] | 147 | |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 148 | struct stm32_sdram_params { |
| 149 | struct stm32_fmc_regs *base; |
| 150 | u8 no_sdram_banks; |
| 151 | struct bank_params bank_params[MAX_SDRAM_BANK]; |
Patrice Chotard | 7c695ce | 2017-07-18 17:37:29 +0200 | [diff] [blame] | 152 | enum stm32_fmc_family family; |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 153 | }; |
| 154 | |
Vikas Manocha | 2479609 | 2017-04-10 15:02:51 -0700 | [diff] [blame] | 155 | #define SDRAM_MODE_BL_SHIFT 0 |
| 156 | #define SDRAM_MODE_CAS_SHIFT 4 |
| 157 | #define SDRAM_MODE_BL 0 |
Vikas Manocha | 4515ffe | 2017-04-10 15:02:56 -0700 | [diff] [blame] | 158 | |
| 159 | int stm32_sdram_init(struct udevice *dev) |
Vikas Manocha | 2479609 | 2017-04-10 15:02:51 -0700 | [diff] [blame] | 160 | { |
Vikas Manocha | 4515ffe | 2017-04-10 15:02:56 -0700 | [diff] [blame] | 161 | struct stm32_sdram_params *params = dev_get_platdata(dev); |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 162 | struct stm32_sdram_control *control; |
| 163 | struct stm32_sdram_timing *timing; |
Patrice Chotard | f2b8000 | 2017-07-18 17:37:25 +0200 | [diff] [blame] | 164 | struct stm32_fmc_regs *regs = params->base; |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 165 | enum stm32_fmc_bank target_bank; |
| 166 | u32 ctb; /* SDCMR register: Command Target Bank */ |
| 167 | u32 ref_count; |
| 168 | u8 i; |
Vikas Manocha | 2479609 | 2017-04-10 15:02:51 -0700 | [diff] [blame] | 169 | |
Patrice Chotard | 7c695ce | 2017-07-18 17:37:29 +0200 | [diff] [blame] | 170 | /* disable the FMC controller */ |
| 171 | if (params->family == STM32H7_FMC) |
| 172 | clrbits_le32(®s->bcr1, FMC_BCR1_FMCEN); |
| 173 | |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 174 | for (i = 0; i < params->no_sdram_banks; i++) { |
| 175 | control = params->bank_params[i].sdram_control; |
| 176 | timing = params->bank_params[i].sdram_timing; |
| 177 | target_bank = params->bank_params[i].target_bank; |
| 178 | ref_count = params->bank_params[i].sdram_ref_count; |
Vikas Manocha | 2479609 | 2017-04-10 15:02:51 -0700 | [diff] [blame] | 179 | |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 180 | writel(control->sdclk << FMC_SDCR_SDCLK_SHIFT |
| 181 | | control->cas_latency << FMC_SDCR_CAS_SHIFT |
| 182 | | control->no_banks << FMC_SDCR_NB_SHIFT |
| 183 | | control->memory_width << FMC_SDCR_MWID_SHIFT |
| 184 | | control->no_rows << FMC_SDCR_NR_SHIFT |
| 185 | | control->no_columns << FMC_SDCR_NC_SHIFT |
| 186 | | control->rd_pipe_delay << FMC_SDCR_RPIPE_SHIFT |
| 187 | | control->rd_burst << FMC_SDCR_RBURST_SHIFT, |
| 188 | ®s->sdcr1); |
Vikas Manocha | 2479609 | 2017-04-10 15:02:51 -0700 | [diff] [blame] | 189 | |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 190 | if (target_bank == SDRAM_BANK2) |
| 191 | writel(control->cas_latency << FMC_SDCR_CAS_SHIFT |
| 192 | | control->no_banks << FMC_SDCR_NB_SHIFT |
| 193 | | control->memory_width << FMC_SDCR_MWID_SHIFT |
| 194 | | control->no_rows << FMC_SDCR_NR_SHIFT |
| 195 | | control->no_columns << FMC_SDCR_NC_SHIFT, |
| 196 | ®s->sdcr2); |
Vikas Manocha | 2479609 | 2017-04-10 15:02:51 -0700 | [diff] [blame] | 197 | |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 198 | writel(timing->trcd << FMC_SDTR_TRCD_SHIFT |
| 199 | | timing->trp << FMC_SDTR_TRP_SHIFT |
| 200 | | timing->twr << FMC_SDTR_TWR_SHIFT |
| 201 | | timing->trc << FMC_SDTR_TRC_SHIFT |
| 202 | | timing->tras << FMC_SDTR_TRAS_SHIFT |
| 203 | | timing->txsr << FMC_SDTR_TXSR_SHIFT |
| 204 | | timing->tmrd << FMC_SDTR_TMRD_SHIFT, |
| 205 | ®s->sdtr1); |
Vikas Manocha | 2479609 | 2017-04-10 15:02:51 -0700 | [diff] [blame] | 206 | |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 207 | if (target_bank == SDRAM_BANK2) |
| 208 | writel(timing->trcd << FMC_SDTR_TRCD_SHIFT |
| 209 | | timing->trp << FMC_SDTR_TRP_SHIFT |
| 210 | | timing->twr << FMC_SDTR_TWR_SHIFT |
| 211 | | timing->trc << FMC_SDTR_TRC_SHIFT |
| 212 | | timing->tras << FMC_SDTR_TRAS_SHIFT |
| 213 | | timing->txsr << FMC_SDTR_TXSR_SHIFT |
| 214 | | timing->tmrd << FMC_SDTR_TMRD_SHIFT, |
| 215 | ®s->sdtr2); |
Patrice Chotard | 7c695ce | 2017-07-18 17:37:29 +0200 | [diff] [blame] | 216 | |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 217 | if (target_bank == SDRAM_BANK1) |
| 218 | ctb = FMC_SDCMR_BANK_1; |
| 219 | else |
| 220 | ctb = FMC_SDCMR_BANK_2; |
Vikas Manocha | 2479609 | 2017-04-10 15:02:51 -0700 | [diff] [blame] | 221 | |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 222 | writel(ctb | FMC_SDCMR_MODE_START_CLOCK, ®s->sdcmr); |
| 223 | udelay(200); /* 200 us delay, page 10, "Power-Up" */ |
| 224 | FMC_BUSY_WAIT(regs); |
Vikas Manocha | 2479609 | 2017-04-10 15:02:51 -0700 | [diff] [blame] | 225 | |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 226 | writel(ctb | FMC_SDCMR_MODE_PRECHARGE, ®s->sdcmr); |
| 227 | udelay(100); |
| 228 | FMC_BUSY_WAIT(regs); |
Vikas Manocha | 2479609 | 2017-04-10 15:02:51 -0700 | [diff] [blame] | 229 | |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 230 | writel((ctb | FMC_SDCMR_MODE_AUTOREFRESH | 7 << FMC_SDCMR_NRFS_SHIFT), |
| 231 | ®s->sdcmr); |
| 232 | udelay(100); |
| 233 | FMC_BUSY_WAIT(regs); |
| 234 | |
| 235 | writel(ctb | (SDRAM_MODE_BL << SDRAM_MODE_BL_SHIFT |
| 236 | | control->cas_latency << SDRAM_MODE_CAS_SHIFT) |
| 237 | << FMC_SDCMR_MODE_REGISTER_SHIFT | FMC_SDCMR_MODE_WRITE_MODE, |
| 238 | ®s->sdcmr); |
| 239 | udelay(100); |
| 240 | FMC_BUSY_WAIT(regs); |
| 241 | |
| 242 | writel(ctb | FMC_SDCMR_MODE_NORMAL, ®s->sdcmr); |
| 243 | FMC_BUSY_WAIT(regs); |
| 244 | |
| 245 | /* Refresh timer */ |
| 246 | writel(ref_count << 1, ®s->sdrtr); |
| 247 | } |
Vikas Manocha | 2479609 | 2017-04-10 15:02:51 -0700 | [diff] [blame] | 248 | |
Patrice Chotard | 7c695ce | 2017-07-18 17:37:29 +0200 | [diff] [blame] | 249 | /* enable the FMC controller */ |
| 250 | if (params->family == STM32H7_FMC) |
| 251 | setbits_le32(®s->bcr1, FMC_BCR1_FMCEN); |
| 252 | |
Vikas Manocha | 2479609 | 2017-04-10 15:02:51 -0700 | [diff] [blame] | 253 | return 0; |
| 254 | } |
Vikas Manocha | aa88e1a | 2017-04-10 15:02:52 -0700 | [diff] [blame] | 255 | |
Vikas Manocha | 4515ffe | 2017-04-10 15:02:56 -0700 | [diff] [blame] | 256 | static int stm32_fmc_ofdata_to_platdata(struct udevice *dev) |
| 257 | { |
Vikas Manocha | 4515ffe | 2017-04-10 15:02:56 -0700 | [diff] [blame] | 258 | struct stm32_sdram_params *params = dev_get_platdata(dev); |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 259 | struct bank_params *bank_params; |
Patrice Chotard | 63e9728 | 2017-12-12 09:49:41 +0100 | [diff] [blame] | 260 | struct ofnode_phandle_args args; |
| 261 | u32 *syscfg_base; |
| 262 | u32 mem_remap; |
Radoslaw Pietrzyk | a88d314 | 2018-05-16 17:27:11 +0200 | [diff] [blame] | 263 | u32 swp_fmc; |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 264 | ofnode bank_node; |
| 265 | char *bank_name; |
| 266 | u8 bank = 0; |
Patrice Chotard | 63e9728 | 2017-12-12 09:49:41 +0100 | [diff] [blame] | 267 | int ret; |
| 268 | |
Radoslaw Pietrzyk | a88d314 | 2018-05-16 17:27:11 +0200 | [diff] [blame] | 269 | ret = dev_read_phandle_with_args(dev, "st,syscfg", NULL, 0, 0, |
Patrice Chotard | 63e9728 | 2017-12-12 09:49:41 +0100 | [diff] [blame] | 270 | &args); |
Radoslaw Pietrzyk | a88d314 | 2018-05-16 17:27:11 +0200 | [diff] [blame] | 271 | if (ret) { |
| 272 | dev_dbg(dev, "%s: can't find syscon device (%d)\n", __func__, ret); |
| 273 | } else { |
Patrice Chotard | 63e9728 | 2017-12-12 09:49:41 +0100 | [diff] [blame] | 274 | syscfg_base = (u32 *)ofnode_get_addr(args.node); |
| 275 | |
Radoslaw Pietrzyk | a88d314 | 2018-05-16 17:27:11 +0200 | [diff] [blame] | 276 | mem_remap = dev_read_u32_default(dev, "st,mem_remap", NOT_FOUND); |
| 277 | if (mem_remap != NOT_FOUND) { |
| 278 | /* set memory mapping selection */ |
| 279 | clrsetbits_le32(syscfg_base, MEM_MODE_MASK, mem_remap); |
| 280 | } else { |
| 281 | dev_dbg(dev, "%s: cannot find st,mem_remap property\n", __func__); |
| 282 | } |
| 283 | |
| 284 | swp_fmc = dev_read_u32_default(dev, "st,swp_fmc", NOT_FOUND); |
| 285 | if (swp_fmc != NOT_FOUND) { |
| 286 | /* set fmc swapping selection */ |
| 287 | clrsetbits_le32(syscfg_base, SWP_FMC_MASK, swp_fmc << SWP_FMC_OFFSET); |
| 288 | } else { |
| 289 | dev_dbg(dev, "%s: cannot find st,swp_fmc property\n", __func__); |
| 290 | } |
| 291 | |
| 292 | dev_dbg(dev, "syscfg %x = %x\n", (u32)syscfg_base, *syscfg_base); |
Patrice Chotard | 63e9728 | 2017-12-12 09:49:41 +0100 | [diff] [blame] | 293 | } |
Vikas Manocha | 4515ffe | 2017-04-10 15:02:56 -0700 | [diff] [blame] | 294 | |
Patrice Chotard | 8b37922 | 2017-07-18 17:37:26 +0200 | [diff] [blame] | 295 | dev_for_each_subnode(bank_node, dev) { |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 296 | /* extract the bank index from DT */ |
| 297 | bank_name = (char *)ofnode_get_name(bank_node); |
| 298 | strsep(&bank_name, "@"); |
| 299 | if (!bank_name) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 300 | pr_err("missing sdram bank index"); |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 301 | return -EINVAL; |
| 302 | } |
| 303 | |
| 304 | bank_params = ¶ms->bank_params[bank]; |
| 305 | strict_strtoul(bank_name, 10, |
| 306 | (long unsigned int *)&bank_params->target_bank); |
Patrice Chotard | 8b37922 | 2017-07-18 17:37:26 +0200 | [diff] [blame] | 307 | |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 308 | if (bank_params->target_bank >= MAX_SDRAM_BANK) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 309 | pr_err("Found bank %d , but only bank 0 and 1 are supported", |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 310 | bank_params->target_bank); |
Patrice Chotard | 8b37922 | 2017-07-18 17:37:26 +0200 | [diff] [blame] | 311 | return -EINVAL; |
| 312 | } |
| 313 | |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 314 | debug("Find bank %s %u\n", bank_name, bank_params->target_bank); |
Patrice Chotard | 8b37922 | 2017-07-18 17:37:26 +0200 | [diff] [blame] | 315 | |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 316 | params->bank_params[bank].sdram_control = |
| 317 | (struct stm32_sdram_control *) |
| 318 | ofnode_read_u8_array_ptr(bank_node, |
| 319 | "st,sdram-control", |
| 320 | sizeof(struct stm32_sdram_control)); |
| 321 | |
| 322 | if (!params->bank_params[bank].sdram_control) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 323 | pr_err("st,sdram-control not found for %s", |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 324 | ofnode_get_name(bank_node)); |
Patrice Chotard | 8b37922 | 2017-07-18 17:37:26 +0200 | [diff] [blame] | 325 | return -EINVAL; |
| 326 | } |
Vikas Manocha | 1c6459d | 2017-04-10 15:03:03 -0700 | [diff] [blame] | 327 | |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 328 | |
| 329 | params->bank_params[bank].sdram_timing = |
| 330 | (struct stm32_sdram_timing *) |
| 331 | ofnode_read_u8_array_ptr(bank_node, |
| 332 | "st,sdram-timing", |
| 333 | sizeof(struct stm32_sdram_timing)); |
| 334 | |
| 335 | if (!params->bank_params[bank].sdram_timing) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 336 | pr_err("st,sdram-timing not found for %s", |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 337 | ofnode_get_name(bank_node)); |
| 338 | return -EINVAL; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | bank_params->sdram_ref_count = ofnode_read_u32_default(bank_node, |
Vikas Manocha | 1c6459d | 2017-04-10 15:03:03 -0700 | [diff] [blame] | 343 | "st,sdram-refcount", 8196); |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 344 | bank++; |
Vikas Manocha | 4515ffe | 2017-04-10 15:02:56 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Patrice Chotard | 7fb9603 | 2017-07-18 17:37:27 +0200 | [diff] [blame] | 347 | params->no_sdram_banks = bank; |
| 348 | debug("%s, no of banks = %d\n", __func__, params->no_sdram_banks); |
| 349 | |
Vikas Manocha | 4515ffe | 2017-04-10 15:02:56 -0700 | [diff] [blame] | 350 | return 0; |
| 351 | } |
| 352 | |
Vikas Manocha | aa88e1a | 2017-04-10 15:02:52 -0700 | [diff] [blame] | 353 | static int stm32_fmc_probe(struct udevice *dev) |
| 354 | { |
Patrice Chotard | f2b8000 | 2017-07-18 17:37:25 +0200 | [diff] [blame] | 355 | struct stm32_sdram_params *params = dev_get_platdata(dev); |
Vikas Manocha | f9c6e6a | 2017-04-10 15:02:55 -0700 | [diff] [blame] | 356 | int ret; |
Patrice Chotard | f2b8000 | 2017-07-18 17:37:25 +0200 | [diff] [blame] | 357 | fdt_addr_t addr; |
| 358 | |
| 359 | addr = dev_read_addr(dev); |
| 360 | if (addr == FDT_ADDR_T_NONE) |
| 361 | return -EINVAL; |
| 362 | |
| 363 | params->base = (struct stm32_fmc_regs *)addr; |
Patrice Chotard | 7c695ce | 2017-07-18 17:37:29 +0200 | [diff] [blame] | 364 | params->family = dev_get_driver_data(dev); |
Patrice Chotard | f2b8000 | 2017-07-18 17:37:25 +0200 | [diff] [blame] | 365 | |
Patrice Chotard | 4fafb72 | 2017-05-30 15:06:31 +0200 | [diff] [blame] | 366 | #ifdef CONFIG_CLK |
Vikas Manocha | f9c6e6a | 2017-04-10 15:02:55 -0700 | [diff] [blame] | 367 | struct clk clk; |
Vikas Manocha | 4515ffe | 2017-04-10 15:02:56 -0700 | [diff] [blame] | 368 | |
Vikas Manocha | f9c6e6a | 2017-04-10 15:02:55 -0700 | [diff] [blame] | 369 | ret = clk_get_by_index(dev, 0, &clk); |
| 370 | if (ret < 0) |
| 371 | return ret; |
| 372 | |
| 373 | ret = clk_enable(&clk); |
| 374 | |
| 375 | if (ret) { |
| 376 | dev_err(dev, "failed to enable clock\n"); |
| 377 | return ret; |
| 378 | } |
| 379 | #endif |
Vikas Manocha | 4515ffe | 2017-04-10 15:02:56 -0700 | [diff] [blame] | 380 | ret = stm32_sdram_init(dev); |
| 381 | if (ret) |
| 382 | return ret; |
| 383 | |
Vikas Manocha | aa88e1a | 2017-04-10 15:02:52 -0700 | [diff] [blame] | 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static int stm32_fmc_get_info(struct udevice *dev, struct ram_info *info) |
| 388 | { |
Vikas Manocha | aa88e1a | 2017-04-10 15:02:52 -0700 | [diff] [blame] | 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | static struct ram_ops stm32_fmc_ops = { |
| 393 | .get_info = stm32_fmc_get_info, |
| 394 | }; |
| 395 | |
| 396 | static const struct udevice_id stm32_fmc_ids[] = { |
Patrice Chotard | 7c695ce | 2017-07-18 17:37:29 +0200 | [diff] [blame] | 397 | { .compatible = "st,stm32-fmc", .data = STM32F7_FMC }, |
| 398 | { .compatible = "st,stm32h7-fmc", .data = STM32H7_FMC }, |
Vikas Manocha | aa88e1a | 2017-04-10 15:02:52 -0700 | [diff] [blame] | 399 | { } |
| 400 | }; |
| 401 | |
| 402 | U_BOOT_DRIVER(stm32_fmc) = { |
| 403 | .name = "stm32_fmc", |
| 404 | .id = UCLASS_RAM, |
| 405 | .of_match = stm32_fmc_ids, |
| 406 | .ops = &stm32_fmc_ops, |
Vikas Manocha | 4515ffe | 2017-04-10 15:02:56 -0700 | [diff] [blame] | 407 | .ofdata_to_platdata = stm32_fmc_ofdata_to_platdata, |
Vikas Manocha | aa88e1a | 2017-04-10 15:02:52 -0700 | [diff] [blame] | 408 | .probe = stm32_fmc_probe, |
Vikas Manocha | 4515ffe | 2017-04-10 15:02:56 -0700 | [diff] [blame] | 409 | .platdata_auto_alloc_size = sizeof(struct stm32_sdram_params), |
Vikas Manocha | aa88e1a | 2017-04-10 15:02:52 -0700 | [diff] [blame] | 410 | }; |