| 1 |
|
| 2 |
|
| 3 |
#include <device/mmio.h> |
| 4 |
#include <console/console.h> |
| 5 |
#include <soc/clock.h> |
| 6 |
#include <soc/addressmap.h> |
| 7 |
#include <stdint.h> |
| 8 |
|
| 9 |
|
| 10 |
#define FU540_BASE_FQY 33330 |
| 11 |
|
| 12 |
struct prci_ctlr { |
| 13 |
u32 hfxosccfg; |
| 14 |
u32 corepllcfg0; |
| 15 |
u32 reserved08; |
| 16 |
u32 ddrpllcfg0; |
| 17 |
u32 ddrpllcfg1; |
| 18 |
u32 reserved14; |
| 19 |
u32 reserved18; |
| 20 |
u32 gemgxlpllcfg0; |
| 21 |
u32 gemgxlpllcfg1; |
| 22 |
u32 coreclksel; |
| 23 |
u32 devicesresetreg; |
| 24 |
}; |
| 25 |
|
| 26 |
static struct prci_ctlr *prci = (void *)FU540_PRCI; |
| 27 |
|
| 28 |
#define PRCI_CORECLK_MASK 1 |
| 29 |
#define PRCI_CORECLK_CORE_PLL 0 |
| 30 |
#define PRCI_CORECLK_HFCLK 1 |
| 31 |
|
| 32 |
#define PRCI_PLLCFG_LOCK (1u << 31) |
| 33 |
#define PRCI_PLLCFG_DIVR_SHIFT 0 |
| 34 |
#define PRCI_PLLCFG_DIVF_SHIFT 6 |
| 35 |
#define PRCI_PLLCFG_DIVQ_SHIFT 15 |
| 36 |
#define PRCI_PLLCFG_RANGE_SHIFT 18 |
| 37 |
#define PRCI_PLLCFG_BYPASS_SHIFT 24 |
| 38 |
#define PRCI_PLLCFG_FSE_SHIFT 25 |
| 39 |
#define PRCI_PLLCFG_DIVR_MASK (0x03f << PRCI_PLLCFG_DIVR_SHIFT) |
| 40 |
#define PRCI_PLLCFG_DIVF_MASK (0x1ff << PRCI_PLLCFG_DIVF_SHIFT) |
| 41 |
#define PRCI_PLLCFG_DIVQ_MASK (0x007 << PRCI_PLLCFG_DIVQ_SHIFT) |
| 42 |
#define PRCI_PLLCFG_RANGE_MASK (0x07 << PRCI_PLLCFG_RANGE_SHIFT) |
| 43 |
#define PRCI_PLLCFG_BYPASS_MASK (0x1 << PRCI_PLLCFG_BYPASS_SHIFT) |
| 44 |
#define PRCI_PLLCFG_FSE_MASK (0x1 << PRCI_PLLCFG_FSE_SHIFT) |
| 45 |
|
| 46 |
#define PRCI_DDRPLLCFG1_MASK (1u << 31) |
| 47 |
|
| 48 |
#define PRCI_GEMGXLPPLCFG1_MASK (1u << 31) |
| 49 |
|
| 50 |
#define PRCI_CORECLKSEL_CORECLKSEL 1 |
| 51 |
|
| 52 |
#define PRCI_DEVICESRESET_DDR_CTRL_RST_N(x) (((x) & 0x1) << 0) |
| 53 |
#define PRCI_DEVICESRESET_DDR_AXI_RST_N(x) (((x) & 0x1) << 1) |
| 54 |
#define PRCI_DEVICESRESET_DDR_AHB_RST_N(x) (((x) & 0x1) << 2) |
| 55 |
#define PRCI_DEVICESRESET_DDR_PHY_RST_N(x) (((x) & 0x1) << 3) |
| 56 |
#define PRCI_DEVICESRESET_GEMGXL_RST_N(x) (((x) & 0x1) << 5) |
| 57 |
|
| 58 |
|
| 59 |
#if ENV_RAMINIT |
| 60 |
struct pll_settings { |
| 61 |
unsigned int divr:6; |
| 62 |
unsigned int divf:9; |
| 63 |
unsigned int divq:3; |
| 64 |
unsigned int range:3; |
| 65 |
unsigned int bypass:1; |
| 66 |
unsigned int fse:1; |
| 67 |
}; |
| 68 |
|
| 69 |
static void configure_pll(u32 *reg, const struct pll_settings *s) |
| 70 |
{ |
| 71 |
|
| 72 |
u32 c = read32(reg); |
| 73 |
clrsetbits32(&c, PRCI_PLLCFG_DIVR_MASK |
| 74 |
| PRCI_PLLCFG_DIVF_MASK | PRCI_PLLCFG_DIVQ_MASK |
| 75 |
| PRCI_PLLCFG_RANGE_MASK | PRCI_PLLCFG_BYPASS_MASK |
| 76 |
| PRCI_PLLCFG_FSE_MASK, |
| 77 |
(s->divr << PRCI_PLLCFG_DIVR_SHIFT) |
| 78 |
| (s->divf << PRCI_PLLCFG_DIVF_SHIFT) |
| 79 |
| (s->divq << PRCI_PLLCFG_DIVQ_SHIFT) |
| 80 |
| (s->range << PRCI_PLLCFG_RANGE_SHIFT) |
| 81 |
| (s->bypass << PRCI_PLLCFG_BYPASS_SHIFT) |
| 82 |
| (s->fse << PRCI_PLLCFG_FSE_SHIFT)); |
| 83 |
write32(reg, c); |
| 84 |
|
| 85 |
|
| 86 |
while (!(read32(reg) & PRCI_PLLCFG_LOCK)) |
| 87 |
; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
static const struct pll_settings corepll_settings = { |
| 101 |
.divr = 0, |
| 102 |
.divf = 59, |
| 103 |
.divq = 2, |
| 104 |
.range = 4, |
| 105 |
.bypass = 0, |
| 106 |
.fse = 1, |
| 107 |
}; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
static const struct pll_settings ddrpll_settings = { |
| 124 |
.divr = 0, |
| 125 |
.divf = 55, |
| 126 |
.divq = 2, |
| 127 |
.range = 4, |
| 128 |
.bypass = 0, |
| 129 |
.fse = 1, |
| 130 |
}; |
| 131 |
|
| 132 |
static const struct pll_settings gemgxlpll_settings = { |
| 133 |
.divr = 0, |
| 134 |
.divf = 59, |
| 135 |
.divq = 5, |
| 136 |
.range = 4, |
| 137 |
.bypass = 0, |
| 138 |
.fse = 1, |
| 139 |
}; |
| 140 |
|
| 141 |
static void init_coreclk(void) |
| 142 |
{ |
| 143 |
|
| 144 |
clrsetbits32(&prci->coreclksel, PRCI_CORECLK_MASK, |
| 145 |
PRCI_CORECLK_HFCLK); |
| 146 |
|
| 147 |
configure_pll(&prci->corepllcfg0, &corepll_settings); |
| 148 |
|
| 149 |
|
| 150 |
clrsetbits32(&prci->coreclksel, PRCI_CORECLK_MASK, |
| 151 |
PRCI_CORECLK_CORE_PLL); |
| 152 |
} |
| 153 |
|
| 154 |
static void init_pll_ddr(void) |
| 155 |
{ |
| 156 |
|
| 157 |
u32 cfg1 = read32(&prci->ddrpllcfg1); |
| 158 |
clrbits32(&cfg1, PRCI_DDRPLLCFG1_MASK); |
| 159 |
write32(&prci->ddrpllcfg1, cfg1); |
| 160 |
|
| 161 |
configure_pll(&prci->ddrpllcfg0, &ddrpll_settings); |
| 162 |
|
| 163 |
|
| 164 |
setbits32(&cfg1, PRCI_DDRPLLCFG1_MASK); |
| 165 |
write32(&prci->ddrpllcfg1, cfg1); |
| 166 |
} |
| 167 |
|
| 168 |
static void init_gemgxlclk(void) |
| 169 |
{ |
| 170 |
u32 cfg1 = read32(&prci->gemgxlpllcfg1); |
| 171 |
clrbits32(&cfg1, PRCI_GEMGXLPPLCFG1_MASK); |
| 172 |
write32(&prci->gemgxlpllcfg1, cfg1); |
| 173 |
|
| 174 |
configure_pll(&prci->gemgxlpllcfg0, &gemgxlpll_settings); |
| 175 |
|
| 176 |
setbits32(&cfg1, PRCI_GEMGXLPPLCFG1_MASK); |
| 177 |
write32(&prci->gemgxlpllcfg1, cfg1); |
| 178 |
} |
| 179 |
|
| 180 |
#define FU540_UART_DEVICES 2 |
| 181 |
#define FU540_UART_REG_DIV 0x18 |
| 182 |
#define FU540_UART_DIV_VAL 4 |
| 183 |
|
| 184 |
#define FU540_SPI_DIV 0x00 |
| 185 |
#define FU540_SPI_DIV_VAL 4 |
| 186 |
|
| 187 |
static void update_peripheral_clock_dividers(void) |
| 188 |
{ |
| 189 |
write32((uint32_t *)(FU540_QSPI0 + FU540_SPI_DIV), FU540_SPI_DIV_VAL); |
| 190 |
write32((uint32_t *)(FU540_QSPI1 + FU540_SPI_DIV), FU540_SPI_DIV_VAL); |
| 191 |
write32((uint32_t *)(FU540_QSPI2 + FU540_SPI_DIV), FU540_SPI_DIV_VAL); |
| 192 |
|
| 193 |
for (size_t i = 0; i < FU540_UART_DEVICES; i++) |
| 194 |
write32((uint32_t *)(FU540_UART(i) + FU540_UART_REG_DIV), FU540_UART_DIV_VAL); |
| 195 |
} |
| 196 |
|
| 197 |
void clock_init(void) |
| 198 |
{ |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
update_peripheral_clock_dividers(); |
| 204 |
|
| 205 |
init_coreclk(); |
| 206 |
|
| 207 |
|
| 208 |
write32(&prci->devicesresetreg, 0); |
| 209 |
|
| 210 |
init_pll_ddr(); |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
write32(&prci->devicesresetreg, PRCI_DEVICESRESET_DDR_CTRL_RST_N(1)); |
| 218 |
|
| 219 |
|
| 220 |
asm volatile ("fence"); |
| 221 |
|
| 222 |
|
| 223 |
write32(&prci->devicesresetreg, |
| 224 |
PRCI_DEVICESRESET_DDR_CTRL_RST_N(1) | |
| 225 |
PRCI_DEVICESRESET_DDR_AXI_RST_N(1) | |
| 226 |
PRCI_DEVICESRESET_DDR_AHB_RST_N(1) | |
| 227 |
PRCI_DEVICESRESET_DDR_PHY_RST_N(1)); |
| 228 |
|
| 229 |
|
| 230 |
asm volatile ("fence"); |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
for (int i = 0; i < 256; i++) |
| 237 |
asm volatile ("nop"); |
| 238 |
|
| 239 |
init_gemgxlclk(); |
| 240 |
|
| 241 |
write32(&prci->devicesresetreg, |
| 242 |
PRCI_DEVICESRESET_DDR_CTRL_RST_N(1) | |
| 243 |
PRCI_DEVICESRESET_DDR_AXI_RST_N(1) | |
| 244 |
PRCI_DEVICESRESET_DDR_AHB_RST_N(1) | |
| 245 |
PRCI_DEVICESRESET_DDR_PHY_RST_N(1) | |
| 246 |
PRCI_DEVICESRESET_GEMGXL_RST_N(1)); |
| 247 |
|
| 248 |
asm volatile ("fence"); |
| 249 |
} |
| 250 |
#endif |
| 251 |
|
| 252 |
|
| 253 |
int clock_get_coreclk_khz(void) |
| 254 |
{ |
| 255 |
if (read32(&prci->coreclksel) & PRCI_CORECLK_MASK) |
| 256 |
return FU540_BASE_FQY; |
| 257 |
|
| 258 |
u32 cfg = read32(&prci->corepllcfg0); |
| 259 |
u32 divr = (cfg & PRCI_PLLCFG_DIVR_MASK) |
| 260 |
>> PRCI_PLLCFG_DIVR_SHIFT; |
| 261 |
u32 divf = (cfg & PRCI_PLLCFG_DIVF_MASK) |
| 262 |
>> PRCI_PLLCFG_DIVF_SHIFT; |
| 263 |
u32 divq = (cfg & PRCI_PLLCFG_DIVQ_MASK) |
| 264 |
>> PRCI_PLLCFG_DIVQ_SHIFT; |
| 265 |
|
| 266 |
printk(BIOS_SPEW, "clk: r=%d f=%d q=%d\n", divr, divf, divq); |
| 267 |
return FU540_BASE_FQY |
| 268 |
* 2 * (divf + 1) |
| 269 |
/ (divr + 1) |
| 270 |
/ (1ul << divq); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
int clock_get_tlclk_khz(void) |
| 275 |
{ |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
return clock_get_coreclk_khz() / 2; |
| 282 |
} |