1ea65c980SMasahiro Yamada /* 2ea65c980SMasahiro Yamada * Copyright (C) 2011-2015 Masahiro Yamada <yamada.masahiro@socionext.com> 3ea65c980SMasahiro Yamada * 4ea65c980SMasahiro Yamada * SPDX-License-Identifier: GPL-2.0+ 5ea65c980SMasahiro Yamada */ 6ea65c980SMasahiro Yamada 7ea65c980SMasahiro Yamada #include <common.h> 8ea65c980SMasahiro Yamada #include <linux/err.h> 9ea65c980SMasahiro Yamada #include <linux/io.h> 10ea65c980SMasahiro Yamada #include <linux/sizes.h> 11ea65c980SMasahiro Yamada #include <asm/processor.h> 12ea65c980SMasahiro Yamada 13ea65c980SMasahiro Yamada #include "../init.h" 14ea65c980SMasahiro Yamada #include "ddrphy-regs.h" 15ea65c980SMasahiro Yamada #include "umc-regs.h" 16ea65c980SMasahiro Yamada 17ea65c980SMasahiro Yamada #define DRAM_CH_NR 2 18ea65c980SMasahiro Yamada 19ea65c980SMasahiro Yamada enum dram_freq { 20ea65c980SMasahiro Yamada DRAM_FREQ_1333M, 21ea65c980SMasahiro Yamada DRAM_FREQ_1600M, 22ea65c980SMasahiro Yamada DRAM_FREQ_NR, 23ea65c980SMasahiro Yamada }; 24ea65c980SMasahiro Yamada 25ea65c980SMasahiro Yamada enum dram_size { 26ea65c980SMasahiro Yamada DRAM_SZ_128M, 27ea65c980SMasahiro Yamada DRAM_SZ_256M, 28ea65c980SMasahiro Yamada DRAM_SZ_512M, 29ea65c980SMasahiro Yamada DRAM_SZ_NR, 30ea65c980SMasahiro Yamada }; 31ea65c980SMasahiro Yamada 32ea65c980SMasahiro Yamada static u32 umc_cmdctla[DRAM_FREQ_NR] = {0x55990b11, 0x66bb0f17}; 33ea65c980SMasahiro Yamada static u32 umc_cmdctla_plus[DRAM_FREQ_NR] = {0x45990b11, 0x46bb0f17}; 34ea65c980SMasahiro Yamada static u32 umc_cmdctlb[DRAM_FREQ_NR] = {0x16958944, 0x18c6ab44}; 35ea65c980SMasahiro Yamada static u32 umc_cmdctlb_plus[DRAM_FREQ_NR] = {0x16958924, 0x18c6ab24}; 36ea65c980SMasahiro Yamada static u32 umc_spcctla[DRAM_FREQ_NR][DRAM_SZ_NR] = { 37ea65c980SMasahiro Yamada {0x00240512, 0x00350512, 0x00000000}, /* no data for 1333MHz,128MB */ 38ea65c980SMasahiro Yamada {0x002b0617, 0x003f0617, 0x00670617}, 39ea65c980SMasahiro Yamada }; 40ea65c980SMasahiro Yamada static u32 umc_spcctlb[DRAM_FREQ_NR] = {0x00ff0006, 0x00ff0008}; 41ea65c980SMasahiro Yamada static u32 umc_rdatactl[DRAM_FREQ_NR] = {0x000a00ac, 0x000c00ac}; 42ea65c980SMasahiro Yamada 43ea65c980SMasahiro Yamada static int umc_get_rank(int ch) 44ea65c980SMasahiro Yamada { 45ea65c980SMasahiro Yamada return ch; /* ch0: rank0, ch1: rank1 for this SoC */ 46ea65c980SMasahiro Yamada } 47ea65c980SMasahiro Yamada 48ea65c980SMasahiro Yamada static void umc_start_ssif(void __iomem *ssif_base) 49ea65c980SMasahiro Yamada { 50ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + 0x0000b004); 51ea65c980SMasahiro Yamada writel(0xffffffff, ssif_base + 0x0000c004); 52ea65c980SMasahiro Yamada writel(0x000fffcf, ssif_base + 0x0000c008); 53ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + 0x0000b000); 54ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + 0x0000c000); 55ea65c980SMasahiro Yamada writel(0x03010101, ssif_base + UMC_MDMCHSEL); 56ea65c980SMasahiro Yamada writel(0x03010100, ssif_base + UMC_DMDCHSEL); 57ea65c980SMasahiro Yamada 58ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + UMC_CLKEN_SSIF_FETCH); 59ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + UMC_CLKEN_SSIF_COMQUE0); 60ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + UMC_CLKEN_SSIF_COMWC0); 61ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + UMC_CLKEN_SSIF_COMRC0); 62ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + UMC_CLKEN_SSIF_COMQUE1); 63ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + UMC_CLKEN_SSIF_COMWC1); 64ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + UMC_CLKEN_SSIF_COMRC1); 65ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + UMC_CLKEN_SSIF_WC); 66ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + UMC_CLKEN_SSIF_RC); 67ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + UMC_CLKEN_SSIF_DST); 68ea65c980SMasahiro Yamada 69ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_CPURST); 70ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_IDSRST); 71ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_IXMRST); 72ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_MDMRST); 73ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_MDDRST); 74ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_SIORST); 75ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_VIORST); 76ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_FRCRST); 77ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_RGLRST); 78ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_AIORST); 79ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_DMDRST); 80ea65c980SMasahiro Yamada } 81ea65c980SMasahiro Yamada 82ea65c980SMasahiro Yamada static int umc_dramcont_init(void __iomem *dc_base, void __iomem *ca_base, 83ea65c980SMasahiro Yamada int freq, unsigned long size, bool ddr3plus) 84ea65c980SMasahiro Yamada { 85ea65c980SMasahiro Yamada enum dram_freq freq_e; 86ea65c980SMasahiro Yamada enum dram_size size_e; 87ea65c980SMasahiro Yamada 88ea65c980SMasahiro Yamada switch (freq) { 89ea65c980SMasahiro Yamada case 1333: 90ea65c980SMasahiro Yamada freq_e = DRAM_FREQ_1333M; 91ea65c980SMasahiro Yamada break; 92ea65c980SMasahiro Yamada case 1600: 93ea65c980SMasahiro Yamada freq_e = DRAM_FREQ_1600M; 94ea65c980SMasahiro Yamada break; 95ea65c980SMasahiro Yamada default: 96ea65c980SMasahiro Yamada pr_err("unsupported DRAM frequency %d MHz\n", freq); 97ea65c980SMasahiro Yamada return -EINVAL; 98ea65c980SMasahiro Yamada } 99ea65c980SMasahiro Yamada 100ea65c980SMasahiro Yamada switch (size) { 101ea65c980SMasahiro Yamada case 0: 102ea65c980SMasahiro Yamada return 0; 103ea65c980SMasahiro Yamada case SZ_128M: 104ea65c980SMasahiro Yamada size_e = DRAM_SZ_128M; 105ea65c980SMasahiro Yamada break; 106ea65c980SMasahiro Yamada case SZ_256M: 107ea65c980SMasahiro Yamada size_e = DRAM_SZ_256M; 108ea65c980SMasahiro Yamada break; 109ea65c980SMasahiro Yamada case SZ_512M: 110ea65c980SMasahiro Yamada size_e = DRAM_SZ_512M; 111ea65c980SMasahiro Yamada break; 112ea65c980SMasahiro Yamada default: 113ea65c980SMasahiro Yamada pr_err("unsupported DRAM size 0x%08lx\n", size); 114ea65c980SMasahiro Yamada return -EINVAL; 115ea65c980SMasahiro Yamada } 116ea65c980SMasahiro Yamada 117ea65c980SMasahiro Yamada writel((ddr3plus ? umc_cmdctla_plus : umc_cmdctla)[freq_e], 118ea65c980SMasahiro Yamada dc_base + UMC_CMDCTLA); 119ea65c980SMasahiro Yamada writel((ddr3plus ? umc_cmdctlb_plus : umc_cmdctlb)[freq_e], 120ea65c980SMasahiro Yamada dc_base + UMC_CMDCTLB); 121ea65c980SMasahiro Yamada writel(umc_spcctla[freq_e][size_e], dc_base + UMC_SPCCTLA); 122ea65c980SMasahiro Yamada writel(umc_spcctlb[freq_e], dc_base + UMC_SPCCTLB); 123ea65c980SMasahiro Yamada writel(umc_rdatactl[freq_e], dc_base + UMC_RDATACTL_D0); 124ea65c980SMasahiro Yamada writel(0x04060806, dc_base + UMC_WDATACTL_D0); 125ea65c980SMasahiro Yamada writel(0x04a02000, dc_base + UMC_DATASET); 126ea65c980SMasahiro Yamada writel(0x00000000, ca_base + 0x2300); 127ea65c980SMasahiro Yamada writel(0x00400020, dc_base + UMC_DCCGCTL); 128ea65c980SMasahiro Yamada writel(0x00000003, dc_base + 0x7000); 129ea65c980SMasahiro Yamada writel(0x0000004f, dc_base + 0x8000); 130ea65c980SMasahiro Yamada writel(0x000000c3, dc_base + 0x8004); 131ea65c980SMasahiro Yamada writel(0x00000077, dc_base + 0x8008); 132ea65c980SMasahiro Yamada writel(0x0000003b, dc_base + UMC_DICGCTLA); 133ea65c980SMasahiro Yamada writel(0x020a0808, dc_base + UMC_DICGCTLB); 134ea65c980SMasahiro Yamada writel(0x00000004, dc_base + UMC_FLOWCTLG); 135ea65c980SMasahiro Yamada writel(0x80000201, ca_base + 0xc20); 136ea65c980SMasahiro Yamada writel(0x0801e01e, dc_base + UMC_FLOWCTLA); 137ea65c980SMasahiro Yamada writel(0x00200000, dc_base + UMC_FLOWCTLB); 138ea65c980SMasahiro Yamada writel(0x00004444, dc_base + UMC_FLOWCTLC); 139ea65c980SMasahiro Yamada writel(0x200a0a00, dc_base + UMC_SPCSETB); 140ea65c980SMasahiro Yamada writel(0x00000000, dc_base + UMC_SPCSETD); 141ea65c980SMasahiro Yamada writel(0x00000520, dc_base + UMC_DFICUPDCTLA); 142ea65c980SMasahiro Yamada 143ea65c980SMasahiro Yamada return 0; 144ea65c980SMasahiro Yamada } 145ea65c980SMasahiro Yamada 146ea65c980SMasahiro Yamada static int umc_ch_init(void __iomem *dc_base, void __iomem *ca_base, 147ea65c980SMasahiro Yamada int freq, unsigned long size, bool ddr3plus, int ch) 148ea65c980SMasahiro Yamada { 149ea65c980SMasahiro Yamada void __iomem *phy_base = dc_base + 0x00001000; 150ea65c980SMasahiro Yamada int ret; 151ea65c980SMasahiro Yamada 152ea65c980SMasahiro Yamada writel(UMC_INITSET_INIT1EN, dc_base + UMC_INITSET); 153ea65c980SMasahiro Yamada while (readl(dc_base + UMC_INITSET) & UMC_INITSTAT_INIT1ST) 154ea65c980SMasahiro Yamada cpu_relax(); 155ea65c980SMasahiro Yamada 156ea65c980SMasahiro Yamada writel(0x00000101, dc_base + UMC_DIOCTLA); 157ea65c980SMasahiro Yamada 158*5b660066SMasahiro Yamada ret = uniphier_ld4_ddrphy_init(phy_base, freq, ddr3plus); 159ea65c980SMasahiro Yamada if (ret) 160ea65c980SMasahiro Yamada return ret; 161ea65c980SMasahiro Yamada 162ea65c980SMasahiro Yamada ddrphy_prepare_training(phy_base, umc_get_rank(ch)); 163ea65c980SMasahiro Yamada ret = ddrphy_training(phy_base); 164ea65c980SMasahiro Yamada if (ret) 165ea65c980SMasahiro Yamada return ret; 166ea65c980SMasahiro Yamada 167ea65c980SMasahiro Yamada return umc_dramcont_init(dc_base, ca_base, freq, size, ddr3plus); 168ea65c980SMasahiro Yamada } 169ea65c980SMasahiro Yamada 170*5b660066SMasahiro Yamada int uniphier_sld8_umc_init(const struct uniphier_board_data *bd) 171ea65c980SMasahiro Yamada { 172ea65c980SMasahiro Yamada void __iomem *umc_base = (void __iomem *)0x5b800000; 173ea65c980SMasahiro Yamada void __iomem *ca_base = umc_base + 0x00001000; 174ea65c980SMasahiro Yamada void __iomem *dc_base = umc_base + 0x00400000; 175ea65c980SMasahiro Yamada void __iomem *ssif_base = umc_base; 176ea65c980SMasahiro Yamada int ch, ret; 177ea65c980SMasahiro Yamada 178ea65c980SMasahiro Yamada for (ch = 0; ch < DRAM_CH_NR; ch++) { 179ea65c980SMasahiro Yamada ret = umc_ch_init(dc_base, ca_base, bd->dram_freq, 180ea65c980SMasahiro Yamada bd->dram_ch[ch].size, 181ea65c980SMasahiro Yamada bd->dram_ddr3plus, ch); 182ea65c980SMasahiro Yamada if (ret) { 183ea65c980SMasahiro Yamada pr_err("failed to initialize UMC ch%d\n", ch); 184ea65c980SMasahiro Yamada return ret; 185ea65c980SMasahiro Yamada } 186ea65c980SMasahiro Yamada 187ea65c980SMasahiro Yamada ca_base += 0x00001000; 188ea65c980SMasahiro Yamada dc_base += 0x00200000; 189ea65c980SMasahiro Yamada } 190ea65c980SMasahiro Yamada 191ea65c980SMasahiro Yamada umc_start_ssif(ssif_base); 192ea65c980SMasahiro Yamada 193ea65c980SMasahiro Yamada return 0; 194ea65c980SMasahiro Yamada } 195