Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 2 | /* |
| 3 | * sun8i H3 platform dram controller init |
| 4 | * |
| 5 | * (C) Copyright 2007-2015 Allwinner Technology Co. |
| 6 | * Jerry Wang <wangflord@allwinnertech.com> |
| 7 | * (C) Copyright 2015 Vishnu Patekar <vishnupatekar0510@gmail.com> |
| 8 | * (C) Copyright 2015 Hans de Goede <hdegoede@redhat.com> |
| 9 | * (C) Copyright 2015 Jens Kuske <jenskuske@gmail.com> |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 10 | */ |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 11 | #include <init.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 13 | #include <asm/io.h> |
| 14 | #include <asm/arch/clock.h> |
| 15 | #include <asm/arch/dram.h> |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 16 | #include <asm/arch/cpu.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 17 | #include <linux/delay.h> |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 18 | |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 19 | static void mctl_phy_init(u32 val) |
| 20 | { |
| 21 | struct sunxi_mctl_ctl_reg * const mctl_ctl = |
| 22 | (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE; |
| 23 | |
| 24 | writel(val | PIR_INIT, &mctl_ctl->pir); |
| 25 | mctl_await_completion(&mctl_ctl->pgsr[0], PGSR_INIT_DONE, 0x1); |
| 26 | } |
| 27 | |
Jens Kuske | 8bbadc8 | 2017-01-02 11:48:40 +0000 | [diff] [blame] | 28 | static void mctl_set_bit_delays(struct dram_para *para) |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 29 | { |
| 30 | struct sunxi_mctl_ctl_reg * const mctl_ctl = |
| 31 | (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE; |
| 32 | int i, j; |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 33 | |
| 34 | clrbits_le32(&mctl_ctl->pgcr[0], 1 << 26); |
| 35 | |
Jens Kuske | 8bbadc8 | 2017-01-02 11:48:40 +0000 | [diff] [blame] | 36 | for (i = 0; i < NR_OF_BYTE_LANES; i++) |
| 37 | for (j = 0; j < LINES_PER_BYTE_LANE; j++) |
| 38 | writel(DXBDLR_WRITE_DELAY(para->dx_write_delays[i][j]) | |
| 39 | DXBDLR_READ_DELAY(para->dx_read_delays[i][j]), |
| 40 | &mctl_ctl->dx[i].bdlr[j]); |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 41 | |
Jens Kuske | 8bbadc8 | 2017-01-02 11:48:40 +0000 | [diff] [blame] | 42 | for (i = 0; i < 31; i++) |
| 43 | writel(ACBDLR_WRITE_DELAY(para->ac_delays[i]), |
| 44 | &mctl_ctl->acbdlr[i]); |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 45 | |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 46 | #ifdef CONFIG_MACH_SUN8I_R40 |
| 47 | /* DQSn, DMn, DQn output enable bit delay */ |
| 48 | for (i = 0; i < 4; i++) |
| 49 | writel(0x6 << 24, &mctl_ctl->dx[i].sdlr); |
| 50 | #endif |
| 51 | |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 52 | setbits_le32(&mctl_ctl->pgcr[0], 1 << 26); |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Philipp Tomsich | 3c31ba9 | 2017-01-02 11:48:38 +0000 | [diff] [blame] | 55 | enum { |
| 56 | MBUS_PORT_CPU = 0, |
| 57 | MBUS_PORT_GPU = 1, |
| 58 | MBUS_PORT_UNUSED = 2, |
| 59 | MBUS_PORT_DMA = 3, |
| 60 | MBUS_PORT_VE = 4, |
| 61 | MBUS_PORT_CSI = 5, |
| 62 | MBUS_PORT_NAND = 6, |
| 63 | MBUS_PORT_SS = 7, |
Icenowy Zheng | aff2d3a | 2020-10-16 17:33:08 +0800 | [diff] [blame] | 64 | MBUS_PORT_DE_V3S = 8, |
| 65 | MBUS_PORT_DE_CFD_V3S = 9, |
Philipp Tomsich | 3c31ba9 | 2017-01-02 11:48:38 +0000 | [diff] [blame] | 66 | MBUS_PORT_TS = 8, |
| 67 | MBUS_PORT_DI = 9, |
| 68 | MBUS_PORT_DE = 10, |
| 69 | MBUS_PORT_DE_CFD = 11, |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 70 | MBUS_PORT_UNKNOWN1 = 12, |
| 71 | MBUS_PORT_UNKNOWN2 = 13, |
| 72 | MBUS_PORT_UNKNOWN3 = 14, |
Philipp Tomsich | 3c31ba9 | 2017-01-02 11:48:38 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | enum { |
| 76 | MBUS_QOS_LOWEST = 0, |
| 77 | MBUS_QOS_LOW, |
| 78 | MBUS_QOS_HIGH, |
| 79 | MBUS_QOS_HIGHEST |
| 80 | }; |
| 81 | |
Andre Przywara | 16f6f79 | 2023-06-07 01:07:41 +0100 | [diff] [blame] | 82 | static void mbus_configure_port(u8 port, |
| 83 | bool bwlimit, |
| 84 | bool priority, |
| 85 | u8 qos, /* MBUS_QOS_LOWEST .. MBUS_QOS_HIGEST */ |
| 86 | u8 waittime, /* 0 .. 0xf */ |
| 87 | u8 acs, /* 0 .. 0xff */ |
| 88 | u16 bwl0, /* 0 .. 0xffff, bandwidth limit in MB/s */ |
| 89 | u16 bwl1, |
| 90 | u16 bwl2) |
Philipp Tomsich | 3c31ba9 | 2017-01-02 11:48:38 +0000 | [diff] [blame] | 91 | { |
| 92 | struct sunxi_mctl_com_reg * const mctl_com = |
| 93 | (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE; |
| 94 | |
| 95 | const u32 cfg0 = ( (bwlimit ? (1 << 0) : 0) |
| 96 | | (priority ? (1 << 1) : 0) |
| 97 | | ((qos & 0x3) << 2) |
| 98 | | ((waittime & 0xf) << 4) |
| 99 | | ((acs & 0xff) << 8) |
| 100 | | (bwl0 << 16) ); |
| 101 | const u32 cfg1 = ((u32)bwl2 << 16) | (bwl1 & 0xffff); |
| 102 | |
| 103 | debug("MBUS port %d cfg0 %08x cfg1 %08x\n", port, cfg0, cfg1); |
| 104 | writel(cfg0, &mctl_com->mcr[port][0]); |
| 105 | writel(cfg1, &mctl_com->mcr[port][1]); |
| 106 | } |
| 107 | |
| 108 | #define MBUS_CONF(port, bwlimit, qos, acs, bwl0, bwl1, bwl2) \ |
| 109 | mbus_configure_port(MBUS_PORT_ ## port, bwlimit, false, \ |
| 110 | MBUS_QOS_ ## qos, 0, acs, bwl0, bwl1, bwl2) |
| 111 | |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 112 | static void mctl_set_master_priority_h3(void) |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 113 | { |
| 114 | struct sunxi_mctl_com_reg * const mctl_com = |
| 115 | (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE; |
| 116 | |
| 117 | /* enable bandwidth limit windows and set windows size 1us */ |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 118 | writel((1 << 16) | (400 << 0), &mctl_com->bwcr); |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 119 | |
| 120 | /* set cpu high priority */ |
| 121 | writel(0x00000001, &mctl_com->mapr); |
| 122 | |
Philipp Tomsich | 3c31ba9 | 2017-01-02 11:48:38 +0000 | [diff] [blame] | 123 | MBUS_CONF( CPU, true, HIGHEST, 0, 512, 256, 128); |
| 124 | MBUS_CONF( GPU, true, HIGH, 0, 1536, 1024, 256); |
| 125 | MBUS_CONF(UNUSED, true, HIGHEST, 0, 512, 256, 96); |
| 126 | MBUS_CONF( DMA, true, HIGHEST, 0, 256, 128, 32); |
| 127 | MBUS_CONF( VE, true, HIGH, 0, 1792, 1600, 256); |
| 128 | MBUS_CONF( CSI, true, HIGHEST, 0, 256, 128, 32); |
| 129 | MBUS_CONF( NAND, true, HIGH, 0, 256, 128, 64); |
| 130 | MBUS_CONF( SS, true, HIGHEST, 0, 256, 128, 64); |
| 131 | MBUS_CONF( TS, true, HIGHEST, 0, 256, 128, 64); |
| 132 | MBUS_CONF( DI, true, HIGH, 0, 1024, 256, 64); |
| 133 | MBUS_CONF( DE, true, HIGHEST, 3, 8192, 6120, 1024); |
| 134 | MBUS_CONF(DE_CFD, true, HIGH, 0, 1024, 288, 64); |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Icenowy Zheng | aff2d3a | 2020-10-16 17:33:08 +0800 | [diff] [blame] | 137 | static void mctl_set_master_priority_v3s(void) |
| 138 | { |
| 139 | struct sunxi_mctl_com_reg * const mctl_com = |
| 140 | (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE; |
| 141 | |
| 142 | /* enable bandwidth limit windows and set windows size 1us */ |
| 143 | writel((1 << 16) | (400 << 0), &mctl_com->bwcr); |
| 144 | |
| 145 | /* set cpu high priority */ |
| 146 | writel(0x00000001, &mctl_com->mapr); |
| 147 | |
| 148 | MBUS_CONF( CPU, true, HIGHEST, 0, 160, 100, 80); |
| 149 | MBUS_CONF( GPU, true, HIGH, 0, 1792, 1536, 0); |
| 150 | MBUS_CONF( UNUSED, true, HIGHEST, 0, 256, 128, 80); |
| 151 | MBUS_CONF( DMA, true, HIGH, 0, 256, 100, 0); |
| 152 | MBUS_CONF( VE, true, HIGH, 0, 2048, 1600, 0); |
| 153 | MBUS_CONF( CSI, true, HIGHEST, 0, 384, 256, 0); |
| 154 | MBUS_CONF( NAND, true, HIGH, 0, 100, 50, 0); |
| 155 | MBUS_CONF( SS, true, HIGH, 0, 384, 256, 0); |
| 156 | MBUS_CONF( DE_V3S, false, HIGH, 0, 8192, 4096, 0); |
| 157 | MBUS_CONF(DE_CFD_V3S, true, HIGH, 0, 640, 256, 0); |
| 158 | } |
| 159 | |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 160 | static void mctl_set_master_priority_a64(void) |
| 161 | { |
| 162 | struct sunxi_mctl_com_reg * const mctl_com = |
| 163 | (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE; |
| 164 | |
| 165 | /* enable bandwidth limit windows and set windows size 1us */ |
| 166 | writel(399, &mctl_com->tmr); |
| 167 | writel((1 << 16), &mctl_com->bwcr); |
| 168 | |
| 169 | /* Port 2 is reserved per Allwinner's linux-3.10 source, yet they |
| 170 | * initialise it */ |
| 171 | MBUS_CONF( CPU, true, HIGHEST, 0, 160, 100, 80); |
| 172 | MBUS_CONF( GPU, false, HIGH, 0, 1536, 1400, 256); |
| 173 | MBUS_CONF(UNUSED, true, HIGHEST, 0, 512, 256, 96); |
| 174 | MBUS_CONF( DMA, true, HIGH, 0, 256, 80, 100); |
| 175 | MBUS_CONF( VE, true, HIGH, 0, 1792, 1600, 256); |
| 176 | MBUS_CONF( CSI, true, HIGH, 0, 256, 128, 0); |
| 177 | MBUS_CONF( NAND, true, HIGH, 0, 256, 128, 64); |
| 178 | MBUS_CONF( SS, true, HIGHEST, 0, 256, 128, 64); |
| 179 | MBUS_CONF( TS, true, HIGHEST, 0, 256, 128, 64); |
| 180 | MBUS_CONF( DI, true, HIGH, 0, 1024, 256, 64); |
| 181 | MBUS_CONF( DE, true, HIGH, 2, 8192, 6144, 2048); |
| 182 | MBUS_CONF(DE_CFD, true, HIGH, 0, 1280, 144, 64); |
| 183 | |
| 184 | writel(0x81000004, &mctl_com->mdfs_bwlr[2]); |
| 185 | } |
| 186 | |
Andre Przywara | 5d0d28f | 2017-02-16 01:20:26 +0000 | [diff] [blame] | 187 | static void mctl_set_master_priority_h5(void) |
| 188 | { |
| 189 | struct sunxi_mctl_com_reg * const mctl_com = |
| 190 | (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE; |
| 191 | |
| 192 | /* enable bandwidth limit windows and set windows size 1us */ |
| 193 | writel(399, &mctl_com->tmr); |
| 194 | writel((1 << 16), &mctl_com->bwcr); |
| 195 | |
| 196 | /* set cpu high priority */ |
| 197 | writel(0x00000001, &mctl_com->mapr); |
| 198 | |
| 199 | /* Port 2 is reserved per Allwinner's linux-3.10 source, yet |
| 200 | * they initialise it */ |
| 201 | MBUS_CONF( CPU, true, HIGHEST, 0, 300, 260, 150); |
| 202 | MBUS_CONF( GPU, true, HIGHEST, 0, 600, 400, 200); |
| 203 | MBUS_CONF(UNUSED, true, HIGHEST, 0, 512, 256, 96); |
| 204 | MBUS_CONF( DMA, true, HIGHEST, 0, 256, 128, 32); |
| 205 | MBUS_CONF( VE, true, HIGHEST, 0, 1900, 1500, 1000); |
| 206 | MBUS_CONF( CSI, true, HIGHEST, 0, 150, 120, 100); |
| 207 | MBUS_CONF( NAND, true, HIGH, 0, 256, 128, 64); |
| 208 | MBUS_CONF( SS, true, HIGHEST, 0, 256, 128, 64); |
| 209 | MBUS_CONF( TS, true, HIGHEST, 0, 256, 128, 64); |
| 210 | MBUS_CONF( DI, true, HIGH, 0, 1024, 256, 64); |
| 211 | MBUS_CONF( DE, true, HIGHEST, 3, 3400, 2400, 1024); |
| 212 | MBUS_CONF(DE_CFD, true, HIGHEST, 0, 600, 400, 200); |
| 213 | } |
| 214 | |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 215 | static void mctl_set_master_priority_r40(void) |
| 216 | { |
| 217 | struct sunxi_mctl_com_reg * const mctl_com = |
| 218 | (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE; |
| 219 | |
| 220 | /* enable bandwidth limit windows and set windows size 1us */ |
| 221 | writel(399, &mctl_com->tmr); |
| 222 | writel((1 << 16), &mctl_com->bwcr); |
| 223 | |
| 224 | /* set cpu high priority */ |
| 225 | writel(0x00000001, &mctl_com->mapr); |
| 226 | |
| 227 | /* Port 2 is reserved per Allwinner's linux-3.10 source, yet |
| 228 | * they initialise it */ |
| 229 | MBUS_CONF( CPU, true, HIGHEST, 0, 300, 260, 150); |
| 230 | MBUS_CONF( GPU, true, HIGHEST, 0, 600, 400, 200); |
| 231 | MBUS_CONF( UNUSED, true, HIGHEST, 0, 512, 256, 96); |
| 232 | MBUS_CONF( DMA, true, HIGHEST, 0, 256, 128, 32); |
| 233 | MBUS_CONF( VE, true, HIGHEST, 0, 1900, 1500, 1000); |
| 234 | MBUS_CONF( CSI, true, HIGHEST, 0, 150, 120, 100); |
| 235 | MBUS_CONF( NAND, true, HIGH, 0, 256, 128, 64); |
| 236 | MBUS_CONF( SS, true, HIGHEST, 0, 256, 128, 64); |
| 237 | MBUS_CONF( TS, true, HIGHEST, 0, 256, 128, 64); |
| 238 | MBUS_CONF( DI, true, HIGH, 0, 1024, 256, 64); |
| 239 | |
| 240 | /* |
| 241 | * The port names are probably wrong, but no correct sources |
| 242 | * are available. |
| 243 | */ |
| 244 | MBUS_CONF( DE, true, HIGH, 0, 128, 48, 0); |
| 245 | MBUS_CONF( DE_CFD, true, HIGH, 0, 384, 256, 0); |
| 246 | MBUS_CONF(UNKNOWN1, true, HIGHEST, 0, 512, 384, 256); |
| 247 | MBUS_CONF(UNKNOWN2, true, HIGHEST, 2, 8192, 6144, 1024); |
| 248 | MBUS_CONF(UNKNOWN3, true, HIGH, 0, 1280, 144, 64); |
| 249 | } |
| 250 | |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 251 | static void mctl_set_master_priority(uint16_t socid) |
| 252 | { |
| 253 | switch (socid) { |
| 254 | case SOCID_H3: |
| 255 | mctl_set_master_priority_h3(); |
| 256 | return; |
Icenowy Zheng | aff2d3a | 2020-10-16 17:33:08 +0800 | [diff] [blame] | 257 | case SOCID_V3S: |
| 258 | mctl_set_master_priority_v3s(); |
| 259 | return; |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 260 | case SOCID_A64: |
| 261 | mctl_set_master_priority_a64(); |
| 262 | return; |
Andre Przywara | 5d0d28f | 2017-02-16 01:20:26 +0000 | [diff] [blame] | 263 | case SOCID_H5: |
| 264 | mctl_set_master_priority_h5(); |
| 265 | return; |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 266 | case SOCID_R40: |
| 267 | mctl_set_master_priority_r40(); |
| 268 | return; |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 272 | static u32 bin_to_mgray(int val) |
| 273 | { |
| 274 | static const u8 lookup_table[32] = { |
| 275 | 0x00, 0x01, 0x02, 0x03, 0x06, 0x07, 0x04, 0x05, |
| 276 | 0x0c, 0x0d, 0x0e, 0x0f, 0x0a, 0x0b, 0x08, 0x09, |
| 277 | 0x18, 0x19, 0x1a, 0x1b, 0x1e, 0x1f, 0x1c, 0x1d, |
| 278 | 0x14, 0x15, 0x16, 0x17, 0x12, 0x13, 0x10, 0x11, |
| 279 | }; |
| 280 | |
| 281 | return lookup_table[clamp(val, 0, 31)]; |
| 282 | } |
| 283 | |
| 284 | static int mgray_to_bin(u32 val) |
| 285 | { |
| 286 | static const u8 lookup_table[32] = { |
| 287 | 0x00, 0x01, 0x02, 0x03, 0x06, 0x07, 0x04, 0x05, |
| 288 | 0x0e, 0x0f, 0x0c, 0x0d, 0x08, 0x09, 0x0a, 0x0b, |
| 289 | 0x1e, 0x1f, 0x1c, 0x1d, 0x18, 0x19, 0x1a, 0x1b, |
| 290 | 0x10, 0x11, 0x12, 0x13, 0x16, 0x17, 0x14, 0x15, |
| 291 | }; |
| 292 | |
| 293 | return lookup_table[val & 0x1f]; |
| 294 | } |
| 295 | |
| 296 | static void mctl_h3_zq_calibration_quirk(struct dram_para *para) |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 297 | { |
| 298 | struct sunxi_mctl_ctl_reg * const mctl_ctl = |
| 299 | (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE; |
Icenowy Zheng | b260751 | 2017-06-03 17:10:16 +0800 | [diff] [blame] | 300 | int zq_count; |
| 301 | |
| 302 | #if defined CONFIG_SUNXI_DRAM_DW_16BIT |
| 303 | zq_count = 4; |
| 304 | #else |
| 305 | zq_count = 6; |
| 306 | #endif |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 307 | |
Jens Kuske | d8b9593 | 2016-09-21 20:08:30 +0200 | [diff] [blame] | 308 | if ((readl(SUNXI_SRAMC_BASE + 0x24) & 0xff) == 0 && |
| 309 | (readl(SUNXI_SRAMC_BASE + 0xf0) & 0x1) == 0) { |
| 310 | u32 reg_val; |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 311 | |
Jens Kuske | d8b9593 | 2016-09-21 20:08:30 +0200 | [diff] [blame] | 312 | clrsetbits_le32(&mctl_ctl->zqcr, 0xffff, |
| 313 | CONFIG_DRAM_ZQ & 0xffff); |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 314 | |
| 315 | writel(PIR_CLRSR, &mctl_ctl->pir); |
| 316 | mctl_phy_init(PIR_ZCAL); |
| 317 | |
Jens Kuske | d8b9593 | 2016-09-21 20:08:30 +0200 | [diff] [blame] | 318 | reg_val = readl(&mctl_ctl->zqdr[0]); |
| 319 | reg_val &= (0x1f << 16) | (0x1f << 0); |
| 320 | reg_val |= reg_val << 8; |
| 321 | writel(reg_val, &mctl_ctl->zqdr[0]); |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 322 | |
Jens Kuske | d8b9593 | 2016-09-21 20:08:30 +0200 | [diff] [blame] | 323 | reg_val = readl(&mctl_ctl->zqdr[1]); |
| 324 | reg_val &= (0x1f << 16) | (0x1f << 0); |
| 325 | reg_val |= reg_val << 8; |
| 326 | writel(reg_val, &mctl_ctl->zqdr[1]); |
| 327 | writel(reg_val, &mctl_ctl->zqdr[2]); |
| 328 | } else { |
| 329 | int i; |
| 330 | u16 zq_val[6]; |
| 331 | u8 val; |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 332 | |
Jens Kuske | d8b9593 | 2016-09-21 20:08:30 +0200 | [diff] [blame] | 333 | writel(0x0a0a0a0a, &mctl_ctl->zqdr[2]); |
| 334 | |
Icenowy Zheng | b260751 | 2017-06-03 17:10:16 +0800 | [diff] [blame] | 335 | for (i = 0; i < zq_count; i++) { |
Jens Kuske | d8b9593 | 2016-09-21 20:08:30 +0200 | [diff] [blame] | 336 | u8 zq = (CONFIG_DRAM_ZQ >> (i * 4)) & 0xf; |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 337 | |
Jens Kuske | d8b9593 | 2016-09-21 20:08:30 +0200 | [diff] [blame] | 338 | writel((zq << 20) | (zq << 16) | (zq << 12) | |
| 339 | (zq << 8) | (zq << 4) | (zq << 0), |
| 340 | &mctl_ctl->zqcr); |
| 341 | |
| 342 | writel(PIR_CLRSR, &mctl_ctl->pir); |
| 343 | mctl_phy_init(PIR_ZCAL); |
| 344 | |
| 345 | zq_val[i] = readl(&mctl_ctl->zqdr[0]) & 0xff; |
| 346 | writel(REPEAT_BYTE(zq_val[i]), &mctl_ctl->zqdr[2]); |
| 347 | |
| 348 | writel(PIR_CLRSR, &mctl_ctl->pir); |
| 349 | mctl_phy_init(PIR_ZCAL); |
| 350 | |
| 351 | val = readl(&mctl_ctl->zqdr[0]) >> 24; |
| 352 | zq_val[i] |= bin_to_mgray(mgray_to_bin(val) - 1) << 8; |
| 353 | } |
| 354 | |
| 355 | writel((zq_val[1] << 16) | zq_val[0], &mctl_ctl->zqdr[0]); |
| 356 | writel((zq_val[3] << 16) | zq_val[2], &mctl_ctl->zqdr[1]); |
Icenowy Zheng | b260751 | 2017-06-03 17:10:16 +0800 | [diff] [blame] | 357 | if (zq_count > 4) |
| 358 | writel((zq_val[5] << 16) | zq_val[4], |
| 359 | &mctl_ctl->zqdr[2]); |
Jens Kuske | d8b9593 | 2016-09-21 20:08:30 +0200 | [diff] [blame] | 360 | } |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 361 | } |
| 362 | |
Icenowy Zheng | aff2d3a | 2020-10-16 17:33:08 +0800 | [diff] [blame] | 363 | static void mctl_v3s_zq_calibration_quirk(struct dram_para *para) |
| 364 | { |
| 365 | struct sunxi_mctl_ctl_reg * const mctl_ctl = |
| 366 | (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE; |
| 367 | |
| 368 | u32 reg_val; |
| 369 | |
| 370 | clrsetbits_le32(&mctl_ctl->zqcr, 0xffffff, |
| 371 | CONFIG_DRAM_ZQ & 0xffffff); |
| 372 | mctl_phy_init(PIR_ZCAL); |
| 373 | |
| 374 | reg_val = readl(&mctl_ctl->zqdr[0]); |
| 375 | reg_val &= (0x1f << 16) | (0x1f << 0); |
| 376 | reg_val |= reg_val << 8; |
| 377 | writel(reg_val, &mctl_ctl->zqdr[0]); |
| 378 | |
| 379 | reg_val = readl(&mctl_ctl->zqdr[1]); |
| 380 | reg_val &= (0x1f << 16) | (0x1f << 0); |
| 381 | reg_val |= reg_val << 8; |
| 382 | writel(reg_val, &mctl_ctl->zqdr[1]); |
| 383 | } |
| 384 | |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 385 | static void mctl_set_cr(uint16_t socid, struct dram_para *para) |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 386 | { |
| 387 | struct sunxi_mctl_com_reg * const mctl_com = |
| 388 | (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE; |
| 389 | |
Icenowy Zheng | f09b48e | 2017-06-03 17:10:18 +0800 | [diff] [blame] | 390 | writel(MCTL_CR_BL8 | MCTL_CR_INTERLEAVED | |
| 391 | #if defined CONFIG_SUNXI_DRAM_DDR3 |
| 392 | MCTL_CR_DDR3 | MCTL_CR_2T | |
Icenowy Zheng | e270a58 | 2017-06-03 17:10:20 +0800 | [diff] [blame] | 393 | #elif defined CONFIG_SUNXI_DRAM_DDR2 |
| 394 | MCTL_CR_DDR2 | MCTL_CR_2T | |
Icenowy Zheng | 3c1b9f1 | 2017-06-03 17:10:23 +0800 | [diff] [blame] | 395 | #elif defined CONFIG_SUNXI_DRAM_LPDDR3 |
| 396 | MCTL_CR_LPDDR3 | MCTL_CR_1T | |
Icenowy Zheng | f09b48e | 2017-06-03 17:10:18 +0800 | [diff] [blame] | 397 | #else |
| 398 | #error Unsupported DRAM type! |
| 399 | #endif |
Icenowy Zheng | 9e9b073 | 2021-02-26 00:13:24 +0800 | [diff] [blame] | 400 | (para->ranks[0].bank_bits == 3 ? MCTL_CR_EIGHT_BANKS : MCTL_CR_FOUR_BANKS) | |
Icenowy Zheng | 4323a8f | 2017-06-03 17:10:15 +0800 | [diff] [blame] | 401 | MCTL_CR_BUS_FULL_WIDTH(para->bus_full_width) | |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 402 | (para->dual_rank ? MCTL_CR_DUAL_RANK : MCTL_CR_SINGLE_RANK) | |
Icenowy Zheng | 9e9b073 | 2021-02-26 00:13:24 +0800 | [diff] [blame] | 403 | MCTL_CR_PAGE_SIZE(para->ranks[0].page_size) | |
| 404 | MCTL_CR_ROW_BITS(para->ranks[0].row_bits), &mctl_com->cr); |
| 405 | |
| 406 | if (para->dual_rank && (socid == SOCID_A64 || socid == SOCID_R40)) { |
| 407 | writel((para->ranks[1].bank_bits == 3 ? MCTL_CR_EIGHT_BANKS : MCTL_CR_FOUR_BANKS) | |
| 408 | MCTL_CR_BUS_FULL_WIDTH(para->bus_full_width) | |
| 409 | MCTL_CR_DUAL_RANK | |
| 410 | MCTL_CR_PAGE_SIZE(para->ranks[1].page_size) | |
| 411 | MCTL_CR_ROW_BITS(para->ranks[1].row_bits), &mctl_com->cr_r1); |
| 412 | } |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 413 | |
| 414 | if (socid == SOCID_R40) { |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 415 | /* Mux pin to A15 address line for single rank memory. */ |
Icenowy Zheng | c3391f6 | 2021-02-26 00:13:25 +0800 | [diff] [blame] | 416 | if (!para->dual_rank) |
| 417 | setbits_le32(&mctl_com->cr_r1, MCTL_CR_R1_MUX_A15); |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 418 | } |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 419 | } |
| 420 | |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 421 | static void mctl_sys_init(uint16_t socid, struct dram_para *para) |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 422 | { |
| 423 | struct sunxi_ccm_reg * const ccm = |
| 424 | (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; |
| 425 | struct sunxi_mctl_ctl_reg * const mctl_ctl = |
| 426 | (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE; |
| 427 | |
| 428 | clrbits_le32(&ccm->mbus0_clk_cfg, MBUS_CLK_GATE); |
| 429 | clrbits_le32(&ccm->mbus_reset, CCM_MBUS_RESET_RESET); |
| 430 | clrbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MCTL); |
| 431 | clrbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_RESET_OFFSET_MCTL); |
| 432 | clrbits_le32(&ccm->pll5_cfg, CCM_PLL5_CTRL_EN); |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 433 | if (socid == SOCID_A64 || socid == SOCID_R40) |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 434 | clrbits_le32(&ccm->pll11_cfg, CCM_PLL11_CTRL_EN); |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 435 | udelay(10); |
| 436 | |
| 437 | clrbits_le32(&ccm->dram_clk_cfg, CCM_DRAMCLK_CFG_RST); |
| 438 | udelay(1000); |
| 439 | |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 440 | if (socid == SOCID_A64 || socid == SOCID_R40) { |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 441 | clock_set_pll11(CONFIG_DRAM_CLK * 2 * 1000000, false); |
| 442 | clrsetbits_le32(&ccm->dram_clk_cfg, |
| 443 | CCM_DRAMCLK_CFG_DIV_MASK | |
| 444 | CCM_DRAMCLK_CFG_SRC_MASK, |
| 445 | CCM_DRAMCLK_CFG_DIV(1) | |
| 446 | CCM_DRAMCLK_CFG_SRC_PLL11 | |
| 447 | CCM_DRAMCLK_CFG_UPD); |
Icenowy Zheng | aff2d3a | 2020-10-16 17:33:08 +0800 | [diff] [blame] | 448 | } else if (socid == SOCID_H3 || socid == SOCID_H5 || socid == SOCID_V3S) { |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 449 | clock_set_pll5(CONFIG_DRAM_CLK * 2 * 1000000, false); |
| 450 | clrsetbits_le32(&ccm->dram_clk_cfg, |
| 451 | CCM_DRAMCLK_CFG_DIV_MASK | |
| 452 | CCM_DRAMCLK_CFG_SRC_MASK, |
| 453 | CCM_DRAMCLK_CFG_DIV(1) | |
| 454 | CCM_DRAMCLK_CFG_SRC_PLL5 | |
| 455 | CCM_DRAMCLK_CFG_UPD); |
| 456 | } |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 457 | mctl_await_completion(&ccm->dram_clk_cfg, CCM_DRAMCLK_CFG_UPD, 0); |
| 458 | |
| 459 | setbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_RESET_OFFSET_MCTL); |
| 460 | setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MCTL); |
| 461 | setbits_le32(&ccm->mbus_reset, CCM_MBUS_RESET_RESET); |
| 462 | setbits_le32(&ccm->mbus0_clk_cfg, MBUS_CLK_GATE); |
| 463 | |
| 464 | setbits_le32(&ccm->dram_clk_cfg, CCM_DRAMCLK_CFG_RST); |
| 465 | udelay(10); |
| 466 | |
Andre Przywara | 5d0d28f | 2017-02-16 01:20:26 +0000 | [diff] [blame] | 467 | writel(socid == SOCID_H5 ? 0x8000 : 0xc00e, &mctl_ctl->clken); |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 468 | udelay(500); |
| 469 | } |
| 470 | |
Andre Przywara | c98f5cc | 2017-01-02 11:48:43 +0000 | [diff] [blame] | 471 | /* These are more guessed based on some Allwinner code. */ |
| 472 | #define DX_GCR_ODT_DYNAMIC (0x0 << 4) |
| 473 | #define DX_GCR_ODT_ALWAYS_ON (0x1 << 4) |
| 474 | #define DX_GCR_ODT_OFF (0x2 << 4) |
| 475 | |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 476 | static int mctl_channel_init(uint16_t socid, struct dram_para *para) |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 477 | { |
| 478 | struct sunxi_mctl_com_reg * const mctl_com = |
| 479 | (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE; |
| 480 | struct sunxi_mctl_ctl_reg * const mctl_ctl = |
| 481 | (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE; |
| 482 | |
| 483 | unsigned int i; |
| 484 | |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 485 | mctl_set_cr(socid, para); |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 486 | mctl_set_timing_params(socid, para); |
| 487 | mctl_set_master_priority(socid); |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 488 | |
| 489 | /* setting VTC, default disable all VT */ |
| 490 | clrbits_le32(&mctl_ctl->pgcr[0], (1 << 30) | 0x3f); |
Andre Przywara | 5d0d28f | 2017-02-16 01:20:26 +0000 | [diff] [blame] | 491 | if (socid == SOCID_H5) |
| 492 | setbits_le32(&mctl_ctl->pgcr[1], (1 << 24) | (1 << 26)); |
| 493 | else |
| 494 | clrsetbits_le32(&mctl_ctl->pgcr[1], 1 << 24, 1 << 26); |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 495 | |
| 496 | /* increase DFI_PHY_UPD clock */ |
| 497 | writel(PROTECT_MAGIC, &mctl_com->protect); |
| 498 | udelay(100); |
| 499 | clrsetbits_le32(&mctl_ctl->upd2, 0xfff << 16, 0x50 << 16); |
| 500 | writel(0x0, &mctl_com->protect); |
| 501 | udelay(100); |
| 502 | |
| 503 | /* set dramc odt */ |
Andre Przywara | 5d0d28f | 2017-02-16 01:20:26 +0000 | [diff] [blame] | 504 | for (i = 0; i < 4; i++) { |
| 505 | u32 clearmask = (0x3 << 4) | (0x1 << 1) | (0x3 << 2) | |
| 506 | (0x3 << 12) | (0x3 << 14); |
| 507 | u32 setmask = IS_ENABLED(CONFIG_DRAM_ODT_EN) ? |
| 508 | DX_GCR_ODT_DYNAMIC : DX_GCR_ODT_OFF; |
| 509 | |
| 510 | if (socid == SOCID_H5) { |
| 511 | clearmask |= 0x2 << 8; |
| 512 | setmask |= 0x4 << 8; |
| 513 | } |
| 514 | clrsetbits_le32(&mctl_ctl->dx[i].gcr, clearmask, setmask); |
| 515 | } |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 516 | |
| 517 | /* AC PDR should always ON */ |
Andre Przywara | 5d0d28f | 2017-02-16 01:20:26 +0000 | [diff] [blame] | 518 | clrsetbits_le32(&mctl_ctl->aciocr, socid == SOCID_H5 ? (0x1 << 11) : 0, |
| 519 | 0x1 << 1); |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 520 | |
| 521 | /* set DQS auto gating PD mode */ |
| 522 | setbits_le32(&mctl_ctl->pgcr[2], 0x3 << 6); |
| 523 | |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 524 | if (socid == SOCID_H3) { |
| 525 | /* dx ddr_clk & hdr_clk dynamic mode */ |
| 526 | clrbits_le32(&mctl_ctl->pgcr[0], (0x3 << 14) | (0x3 << 12)); |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 527 | |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 528 | /* dphy & aphy phase select 270 degree */ |
| 529 | clrsetbits_le32(&mctl_ctl->pgcr[2], (0x3 << 10) | (0x3 << 8), |
| 530 | (0x1 << 10) | (0x2 << 8)); |
Icenowy Zheng | aff2d3a | 2020-10-16 17:33:08 +0800 | [diff] [blame] | 531 | } else if (socid == SOCID_V3S) { |
| 532 | /* dx ddr_clk & hdr_clk dynamic mode */ |
| 533 | clrbits_le32(&mctl_ctl->pgcr[0], (0x3 << 14) | (0x3 << 12)); |
| 534 | |
| 535 | /* dphy & aphy phase select 270 degree */ |
| 536 | clrsetbits_le32(&mctl_ctl->pgcr[2], (0x3 << 10) | (0x3 << 8), |
| 537 | (0x1 << 10) | (0x1 << 8)); |
Andre Przywara | 5d0d28f | 2017-02-16 01:20:26 +0000 | [diff] [blame] | 538 | } else if (socid == SOCID_A64 || socid == SOCID_H5) { |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 539 | /* dphy & aphy phase select ? */ |
| 540 | clrsetbits_le32(&mctl_ctl->pgcr[2], (0x3 << 10) | (0x3 << 8), |
| 541 | (0x0 << 10) | (0x3 << 8)); |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 542 | } else if (socid == SOCID_R40) { |
| 543 | /* dx ddr_clk & hdr_clk dynamic mode (tpr13[9] == 0) */ |
| 544 | clrbits_le32(&mctl_ctl->pgcr[0], (0x3 << 14) | (0x3 << 12)); |
| 545 | |
| 546 | /* dphy & aphy phase select ? */ |
| 547 | clrsetbits_le32(&mctl_ctl->pgcr[2], (0x3 << 10) | (0x3 << 8), |
| 548 | (0x0 << 10) | (0x3 << 8)); |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 549 | } |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 550 | |
| 551 | /* set half DQ */ |
Icenowy Zheng | 4323a8f | 2017-06-03 17:10:15 +0800 | [diff] [blame] | 552 | if (!para->bus_full_width) { |
Icenowy Zheng | b260751 | 2017-06-03 17:10:16 +0800 | [diff] [blame] | 553 | #if defined CONFIG_SUNXI_DRAM_DW_32BIT |
Jens Kuske | 3e79758 | 2017-01-02 11:48:39 +0000 | [diff] [blame] | 554 | writel(0x0, &mctl_ctl->dx[2].gcr); |
| 555 | writel(0x0, &mctl_ctl->dx[3].gcr); |
Icenowy Zheng | b260751 | 2017-06-03 17:10:16 +0800 | [diff] [blame] | 556 | #elif defined CONFIG_SUNXI_DRAM_DW_16BIT |
| 557 | writel(0x0, &mctl_ctl->dx[1].gcr); |
| 558 | #else |
| 559 | #error Unsupported DRAM bus width! |
| 560 | #endif |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /* data training configuration */ |
| 564 | clrsetbits_le32(&mctl_ctl->dtcr, 0xf << 24, |
| 565 | (para->dual_rank ? 0x3 : 0x1) << 24); |
| 566 | |
Jens Kuske | 8bbadc8 | 2017-01-02 11:48:40 +0000 | [diff] [blame] | 567 | mctl_set_bit_delays(para); |
| 568 | udelay(50); |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 569 | |
Icenowy Zheng | aff2d3a | 2020-10-16 17:33:08 +0800 | [diff] [blame] | 570 | if (socid == SOCID_V3S) { |
| 571 | mctl_v3s_zq_calibration_quirk(para); |
| 572 | |
| 573 | mctl_phy_init(PIR_PLLINIT | PIR_DCAL | PIR_PHYRST | |
| 574 | PIR_DRAMRST | PIR_DRAMINIT | PIR_QSGATE); |
| 575 | } else if (socid == SOCID_H3) { |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 576 | mctl_h3_zq_calibration_quirk(para); |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 577 | |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 578 | mctl_phy_init(PIR_PLLINIT | PIR_DCAL | PIR_PHYRST | |
| 579 | PIR_DRAMRST | PIR_DRAMINIT | PIR_QSGATE); |
Andre Przywara | 5d0d28f | 2017-02-16 01:20:26 +0000 | [diff] [blame] | 580 | } else if (socid == SOCID_A64 || socid == SOCID_H5) { |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 581 | clrsetbits_le32(&mctl_ctl->zqcr, 0xffffff, CONFIG_DRAM_ZQ); |
| 582 | |
| 583 | mctl_phy_init(PIR_ZCAL | PIR_PLLINIT | PIR_DCAL | PIR_PHYRST | |
| 584 | PIR_DRAMRST | PIR_DRAMINIT | PIR_QSGATE); |
Andre Przywara | 5d0d28f | 2017-02-16 01:20:26 +0000 | [diff] [blame] | 585 | /* no PIR_QSGATE for H5 ???? */ |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 586 | } else if (socid == SOCID_R40) { |
| 587 | clrsetbits_le32(&mctl_ctl->zqcr, 0xffffff, CONFIG_DRAM_ZQ); |
| 588 | |
| 589 | mctl_phy_init(PIR_ZCAL | PIR_PLLINIT | PIR_DCAL | PIR_PHYRST | |
| 590 | PIR_DRAMRST | PIR_DRAMINIT); |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 591 | } |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 592 | |
| 593 | /* detect ranks and bus width */ |
| 594 | if (readl(&mctl_ctl->pgsr[0]) & (0xfe << 20)) { |
| 595 | /* only one rank */ |
Icenowy Zheng | b260751 | 2017-06-03 17:10:16 +0800 | [diff] [blame] | 596 | if (((readl(&mctl_ctl->dx[0].gsr[0]) >> 24) & 0x2) |
| 597 | #if defined CONFIG_SUNXI_DRAM_DW_32BIT |
| 598 | || ((readl(&mctl_ctl->dx[1].gsr[0]) >> 24) & 0x2) |
| 599 | #endif |
| 600 | ) { |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 601 | clrsetbits_le32(&mctl_ctl->dtcr, 0xf << 24, 0x1 << 24); |
| 602 | para->dual_rank = 0; |
| 603 | } |
| 604 | |
| 605 | /* only half DQ width */ |
Icenowy Zheng | b260751 | 2017-06-03 17:10:16 +0800 | [diff] [blame] | 606 | #if defined CONFIG_SUNXI_DRAM_DW_32BIT |
Jens Kuske | 3e79758 | 2017-01-02 11:48:39 +0000 | [diff] [blame] | 607 | if (((readl(&mctl_ctl->dx[2].gsr[0]) >> 24) & 0x1) || |
| 608 | ((readl(&mctl_ctl->dx[3].gsr[0]) >> 24) & 0x1)) { |
| 609 | writel(0x0, &mctl_ctl->dx[2].gcr); |
| 610 | writel(0x0, &mctl_ctl->dx[3].gcr); |
Icenowy Zheng | 4323a8f | 2017-06-03 17:10:15 +0800 | [diff] [blame] | 611 | para->bus_full_width = 0; |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 612 | } |
Icenowy Zheng | b260751 | 2017-06-03 17:10:16 +0800 | [diff] [blame] | 613 | #elif defined CONFIG_SUNXI_DRAM_DW_16BIT |
| 614 | if ((readl(&mctl_ctl->dx[1].gsr[0]) >> 24) & 0x1) { |
| 615 | writel(0x0, &mctl_ctl->dx[1].gcr); |
| 616 | para->bus_full_width = 0; |
| 617 | } |
| 618 | #endif |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 619 | |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 620 | mctl_set_cr(socid, para); |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 621 | udelay(20); |
| 622 | |
| 623 | /* re-train */ |
| 624 | mctl_phy_init(PIR_QSGATE); |
| 625 | if (readl(&mctl_ctl->pgsr[0]) & (0xfe << 20)) |
| 626 | return 1; |
| 627 | } |
| 628 | |
| 629 | /* check the dramc status */ |
| 630 | mctl_await_completion(&mctl_ctl->statr, 0x1, 0x1); |
| 631 | |
| 632 | /* liuke added for refresh debug */ |
| 633 | setbits_le32(&mctl_ctl->rfshctl0, 0x1 << 31); |
| 634 | udelay(10); |
| 635 | clrbits_le32(&mctl_ctl->rfshctl0, 0x1 << 31); |
| 636 | udelay(10); |
| 637 | |
| 638 | /* set PGCR3, CKE polarity */ |
Icenowy Zheng | aff2d3a | 2020-10-16 17:33:08 +0800 | [diff] [blame] | 639 | if (socid == SOCID_H3 || socid == SOCID_V3S) |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 640 | writel(0x00aa0060, &mctl_ctl->pgcr[3]); |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 641 | else if (socid == SOCID_A64 || socid == SOCID_H5 || socid == SOCID_R40) |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 642 | writel(0xc0aa0060, &mctl_ctl->pgcr[3]); |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 643 | |
| 644 | /* power down zq calibration module for power save */ |
| 645 | setbits_le32(&mctl_ctl->zqcr, ZQCR_PWRDOWN); |
| 646 | |
| 647 | /* enable master access */ |
| 648 | writel(0xffffffff, &mctl_com->maer); |
| 649 | |
| 650 | return 0; |
| 651 | } |
| 652 | |
Icenowy Zheng | 9e9b073 | 2021-02-26 00:13:24 +0800 | [diff] [blame] | 653 | static void mctl_auto_detect_dram_size_rank(uint16_t socid, struct dram_para *para, ulong base, struct rank_para *rank) |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 654 | { |
| 655 | /* detect row address bits */ |
Icenowy Zheng | 9e9b073 | 2021-02-26 00:13:24 +0800 | [diff] [blame] | 656 | rank->page_size = 512; |
| 657 | rank->row_bits = 16; |
| 658 | rank->bank_bits = 2; |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 659 | mctl_set_cr(socid, para); |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 660 | |
Icenowy Zheng | 9e9b073 | 2021-02-26 00:13:24 +0800 | [diff] [blame] | 661 | for (rank->row_bits = 11; rank->row_bits < 16; rank->row_bits++) |
| 662 | if (mctl_mem_matches_base((1 << (rank->row_bits + rank->bank_bits)) * rank->page_size, base)) |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 663 | break; |
| 664 | |
Icenowy Zheng | 2002035 | 2017-06-03 17:10:17 +0800 | [diff] [blame] | 665 | /* detect bank address bits */ |
Icenowy Zheng | 9e9b073 | 2021-02-26 00:13:24 +0800 | [diff] [blame] | 666 | rank->bank_bits = 3; |
Icenowy Zheng | 2002035 | 2017-06-03 17:10:17 +0800 | [diff] [blame] | 667 | mctl_set_cr(socid, para); |
| 668 | |
Icenowy Zheng | 9e9b073 | 2021-02-26 00:13:24 +0800 | [diff] [blame] | 669 | for (rank->bank_bits = 2; rank->bank_bits < 3; rank->bank_bits++) |
| 670 | if (mctl_mem_matches_base((1 << rank->bank_bits) * rank->page_size, base)) |
Icenowy Zheng | 2002035 | 2017-06-03 17:10:17 +0800 | [diff] [blame] | 671 | break; |
| 672 | |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 673 | /* detect page size */ |
Icenowy Zheng | 9e9b073 | 2021-02-26 00:13:24 +0800 | [diff] [blame] | 674 | rank->page_size = 8192; |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 675 | mctl_set_cr(socid, para); |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 676 | |
Icenowy Zheng | 9e9b073 | 2021-02-26 00:13:24 +0800 | [diff] [blame] | 677 | for (rank->page_size = 512; rank->page_size < 8192; rank->page_size *= 2) |
| 678 | if (mctl_mem_matches_base(rank->page_size, base)) |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 679 | break; |
| 680 | } |
| 681 | |
Icenowy Zheng | 9e9b073 | 2021-02-26 00:13:24 +0800 | [diff] [blame] | 682 | static unsigned long mctl_calc_rank_size(struct rank_para *rank) |
| 683 | { |
| 684 | return (1UL << (rank->row_bits + rank->bank_bits)) * rank->page_size; |
| 685 | } |
| 686 | |
Icenowy Zheng | c3391f6 | 2021-02-26 00:13:25 +0800 | [diff] [blame] | 687 | /* |
| 688 | * Because we cannot do mctl_phy_init(PIR_QSGATE) on R40 now (which leads |
| 689 | * to failure), it's needed to detect the rank count of R40 in another way. |
| 690 | * |
| 691 | * The code here is modelled after time_out_detect() in BSP, which tries to |
| 692 | * access the memory and check for error code. |
| 693 | * |
| 694 | * TODO: auto detect half DQ width here |
| 695 | */ |
| 696 | static void mctl_r40_detect_rank_count(struct dram_para *para) |
| 697 | { |
Tom Rini | bb4dd96 | 2022-11-16 13:10:37 -0500 | [diff] [blame] | 698 | ulong rank1_base = (ulong) CFG_SYS_SDRAM_BASE + |
Icenowy Zheng | c3391f6 | 2021-02-26 00:13:25 +0800 | [diff] [blame] | 699 | mctl_calc_rank_size(¶->ranks[0]); |
| 700 | struct sunxi_mctl_ctl_reg * const mctl_ctl = |
| 701 | (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE; |
| 702 | |
| 703 | /* Enable read time out */ |
| 704 | setbits_le32(&mctl_ctl->pgcr[0], 0x1 << 25); |
| 705 | |
| 706 | (void) readl((void *) rank1_base); |
| 707 | udelay(10); |
| 708 | |
| 709 | if (readl(&mctl_ctl->pgsr[0]) & (0x1 << 13)) { |
| 710 | clrsetbits_le32(&mctl_ctl->dtcr, 0xf << 24, 0x1 << 24); |
| 711 | para->dual_rank = 0; |
| 712 | } |
| 713 | |
| 714 | /* Reset PHY FIFO to clear it */ |
| 715 | clrbits_le32(&mctl_ctl->pgcr[0], 0x1 << 26); |
| 716 | udelay(100); |
| 717 | setbits_le32(&mctl_ctl->pgcr[0], 0x1 << 26); |
| 718 | |
| 719 | /* Clear error status */ |
| 720 | setbits_le32(&mctl_ctl->pgcr[0], 0x1 << 24); |
| 721 | |
| 722 | /* Clear time out flag */ |
| 723 | clrbits_le32(&mctl_ctl->pgsr[0], 0x1 << 13); |
| 724 | |
| 725 | /* Disable read time out */ |
| 726 | clrbits_le32(&mctl_ctl->pgcr[0], 0x1 << 25); |
| 727 | } |
| 728 | |
Icenowy Zheng | 9e9b073 | 2021-02-26 00:13:24 +0800 | [diff] [blame] | 729 | static void mctl_auto_detect_dram_size(uint16_t socid, struct dram_para *para) |
| 730 | { |
Tom Rini | bb4dd96 | 2022-11-16 13:10:37 -0500 | [diff] [blame] | 731 | mctl_auto_detect_dram_size_rank(socid, para, (ulong)CFG_SYS_SDRAM_BASE, ¶->ranks[0]); |
Icenowy Zheng | 9e9b073 | 2021-02-26 00:13:24 +0800 | [diff] [blame] | 732 | |
| 733 | if ((socid == SOCID_A64 || socid == SOCID_R40) && para->dual_rank) { |
Tom Rini | bb4dd96 | 2022-11-16 13:10:37 -0500 | [diff] [blame] | 734 | mctl_auto_detect_dram_size_rank(socid, para, (ulong)CFG_SYS_SDRAM_BASE + mctl_calc_rank_size(¶->ranks[0]), ¶->ranks[1]); |
Icenowy Zheng | 9e9b073 | 2021-02-26 00:13:24 +0800 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
Jens Kuske | 8bbadc8 | 2017-01-02 11:48:40 +0000 | [diff] [blame] | 738 | /* |
| 739 | * The actual values used here are taken from Allwinner provided boot0 |
| 740 | * binaries, though they are probably board specific, so would likely benefit |
| 741 | * from invidual tuning for each board. Apparently a lot of boards copy from |
| 742 | * some Allwinner reference design, so we go with those generic values for now |
| 743 | * in the hope that they are reasonable for most (all?) boards. |
| 744 | */ |
| 745 | #define SUN8I_H3_DX_READ_DELAYS \ |
| 746 | {{ 18, 18, 18, 18, 18, 18, 18, 18, 18, 0, 0 }, \ |
| 747 | { 14, 14, 14, 14, 14, 14, 14, 14, 14, 0, 0 }, \ |
| 748 | { 18, 18, 18, 18, 18, 18, 18, 18, 18, 0, 0 }, \ |
| 749 | { 14, 14, 14, 14, 14, 14, 14, 14, 14, 0, 0 }} |
| 750 | #define SUN8I_H3_DX_WRITE_DELAYS \ |
| 751 | {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10 }, \ |
| 752 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10 }, \ |
| 753 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10 }, \ |
| 754 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6 }} |
| 755 | #define SUN8I_H3_AC_DELAYS \ |
| 756 | { 0, 0, 0, 0, 0, 0, 0, 0, \ |
| 757 | 0, 0, 0, 0, 0, 0, 0, 0, \ |
| 758 | 0, 0, 0, 0, 0, 0, 0, 0, \ |
| 759 | 0, 0, 0, 0, 0, 0, 0 } |
| 760 | |
Icenowy Zheng | aff2d3a | 2020-10-16 17:33:08 +0800 | [diff] [blame] | 761 | #define SUN8I_V3S_DX_READ_DELAYS \ |
| 762 | {{ 8, 8, 8, 8, 8, 8, 8, 8, 8, 0, 0 }, \ |
| 763 | { 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0 }, \ |
| 764 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \ |
| 765 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }} |
| 766 | #define SUN8I_V3S_DX_WRITE_DELAYS \ |
| 767 | {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4 }, \ |
| 768 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2 }, \ |
| 769 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \ |
| 770 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }} |
| 771 | #define SUN8I_V3S_AC_DELAYS \ |
| 772 | { 0, 0, 0, 0, 0, 0, 0, 0, \ |
| 773 | 0, 0, 0, 0, 0, 0, 0, 0, \ |
| 774 | 0, 0, 0, 0, 0, 0, 0, 0, \ |
| 775 | 0, 0, 0, 0, 0, 0, 0 } |
| 776 | |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 777 | #define SUN8I_R40_DX_READ_DELAYS \ |
| 778 | {{ 14, 14, 14, 14, 14, 14, 14, 14, 14, 0, 0 }, \ |
| 779 | { 14, 14, 14, 14, 14, 14, 14, 14, 14, 0, 0 }, \ |
| 780 | { 14, 14, 14, 14, 14, 14, 14, 14, 14, 0, 0 }, \ |
| 781 | { 14, 14, 14, 14, 14, 14, 14, 14, 14, 0, 0 } } |
| 782 | #define SUN8I_R40_DX_WRITE_DELAYS \ |
| 783 | {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0 }, \ |
| 784 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0 }, \ |
| 785 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0 }, \ |
| 786 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0 } } |
| 787 | #define SUN8I_R40_AC_DELAYS \ |
| 788 | { 0, 0, 3, 0, 0, 0, 0, 0, \ |
| 789 | 0, 0, 0, 0, 0, 0, 0, 0, \ |
| 790 | 0, 0, 0, 0, 0, 0, 0, 0, \ |
| 791 | 0, 0, 0, 0, 0, 0, 0 } |
| 792 | |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 793 | #define SUN50I_A64_DX_READ_DELAYS \ |
| 794 | {{ 16, 16, 16, 16, 17, 16, 16, 17, 16, 1, 0 }, \ |
| 795 | { 17, 17, 17, 17, 17, 17, 17, 17, 17, 1, 0 }, \ |
| 796 | { 16, 17, 17, 16, 16, 16, 16, 16, 16, 0, 0 }, \ |
| 797 | { 17, 17, 17, 17, 17, 17, 17, 17, 17, 1, 0 }} |
| 798 | #define SUN50I_A64_DX_WRITE_DELAYS \ |
| 799 | {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 15 }, \ |
| 800 | { 0, 0, 0, 0, 1, 1, 1, 1, 0, 10, 10 }, \ |
| 801 | { 1, 0, 1, 1, 1, 1, 1, 1, 0, 11, 11 }, \ |
| 802 | { 1, 0, 0, 1, 1, 1, 1, 1, 0, 12, 12 }} |
| 803 | #define SUN50I_A64_AC_DELAYS \ |
| 804 | { 5, 5, 13, 10, 2, 5, 3, 3, \ |
| 805 | 0, 3, 3, 3, 1, 0, 0, 0, \ |
| 806 | 3, 4, 0, 3, 4, 1, 4, 0, \ |
| 807 | 1, 1, 0, 1, 13, 5, 4 } |
| 808 | |
Andre Przywara | 5d0d28f | 2017-02-16 01:20:26 +0000 | [diff] [blame] | 809 | #define SUN8I_H5_DX_READ_DELAYS \ |
| 810 | {{ 14, 15, 17, 17, 17, 17, 17, 18, 17, 3, 3 }, \ |
| 811 | { 21, 21, 12, 22, 21, 21, 21, 21, 21, 3, 3 }, \ |
| 812 | { 16, 19, 19, 17, 22, 22, 21, 22, 19, 3, 3 }, \ |
| 813 | { 21, 21, 22, 22, 20, 21, 19, 19, 19, 3, 3 } } |
| 814 | #define SUN8I_H5_DX_WRITE_DELAYS \ |
| 815 | {{ 1, 2, 3, 4, 3, 4, 4, 4, 6, 6, 6 }, \ |
| 816 | { 6, 6, 6, 5, 5, 5, 5, 5, 6, 6, 6 }, \ |
| 817 | { 0, 2, 4, 2, 6, 5, 5, 5, 6, 6, 6 }, \ |
| 818 | { 3, 3, 3, 2, 2, 1, 1, 1, 4, 4, 4 } } |
| 819 | #define SUN8I_H5_AC_DELAYS \ |
| 820 | { 0, 0, 5, 5, 0, 0, 0, 0, \ |
| 821 | 0, 0, 0, 0, 3, 3, 3, 3, \ |
| 822 | 3, 3, 3, 3, 3, 3, 3, 3, \ |
| 823 | 3, 3, 3, 3, 2, 0, 0 } |
| 824 | |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 825 | unsigned long sunxi_dram_init(void) |
| 826 | { |
| 827 | struct sunxi_mctl_com_reg * const mctl_com = |
| 828 | (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE; |
| 829 | struct sunxi_mctl_ctl_reg * const mctl_ctl = |
| 830 | (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE; |
| 831 | |
Icenowy Zheng | 9e9b073 | 2021-02-26 00:13:24 +0800 | [diff] [blame] | 832 | unsigned long size; |
| 833 | |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 834 | struct dram_para para = { |
Icenowy Zheng | 8804877 | 2017-06-03 17:10:19 +0800 | [diff] [blame] | 835 | .dual_rank = 1, |
Icenowy Zheng | 4323a8f | 2017-06-03 17:10:15 +0800 | [diff] [blame] | 836 | .bus_full_width = 1, |
Icenowy Zheng | 9e9b073 | 2021-02-26 00:13:24 +0800 | [diff] [blame] | 837 | .ranks = { |
| 838 | { |
| 839 | .row_bits = 15, |
| 840 | .bank_bits = 3, |
| 841 | .page_size = 4096, |
| 842 | }, |
| 843 | { |
| 844 | .row_bits = 15, |
| 845 | .bank_bits = 3, |
| 846 | .page_size = 4096, |
| 847 | } |
| 848 | }, |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 849 | |
| 850 | #if defined(CONFIG_MACH_SUN8I_H3) |
Jens Kuske | 8bbadc8 | 2017-01-02 11:48:40 +0000 | [diff] [blame] | 851 | .dx_read_delays = SUN8I_H3_DX_READ_DELAYS, |
| 852 | .dx_write_delays = SUN8I_H3_DX_WRITE_DELAYS, |
| 853 | .ac_delays = SUN8I_H3_AC_DELAYS, |
Icenowy Zheng | aff2d3a | 2020-10-16 17:33:08 +0800 | [diff] [blame] | 854 | #elif defined(CONFIG_MACH_SUN8I_V3S) |
| 855 | .dx_read_delays = SUN8I_V3S_DX_READ_DELAYS, |
| 856 | .dx_write_delays = SUN8I_V3S_DX_WRITE_DELAYS, |
| 857 | .ac_delays = SUN8I_V3S_AC_DELAYS, |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 858 | #elif defined(CONFIG_MACH_SUN8I_R40) |
| 859 | .dx_read_delays = SUN8I_R40_DX_READ_DELAYS, |
| 860 | .dx_write_delays = SUN8I_R40_DX_WRITE_DELAYS, |
| 861 | .ac_delays = SUN8I_R40_AC_DELAYS, |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 862 | #elif defined(CONFIG_MACH_SUN50I) |
| 863 | .dx_read_delays = SUN50I_A64_DX_READ_DELAYS, |
| 864 | .dx_write_delays = SUN50I_A64_DX_WRITE_DELAYS, |
| 865 | .ac_delays = SUN50I_A64_AC_DELAYS, |
Andre Przywara | 5d0d28f | 2017-02-16 01:20:26 +0000 | [diff] [blame] | 866 | #elif defined(CONFIG_MACH_SUN50I_H5) |
| 867 | .dx_read_delays = SUN8I_H5_DX_READ_DELAYS, |
| 868 | .dx_write_delays = SUN8I_H5_DX_WRITE_DELAYS, |
| 869 | .ac_delays = SUN8I_H5_AC_DELAYS, |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 870 | #endif |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 871 | }; |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 872 | /* |
| 873 | * Let the compiler optimize alternatives away by passing this value into |
| 874 | * the static functions. This saves us #ifdefs, but still keeps the binary |
| 875 | * small. |
| 876 | */ |
| 877 | #if defined(CONFIG_MACH_SUN8I_H3) |
| 878 | uint16_t socid = SOCID_H3; |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 879 | #elif defined(CONFIG_MACH_SUN8I_R40) |
| 880 | uint16_t socid = SOCID_R40; |
Icenowy Zheng | fe05217 | 2017-06-03 17:10:21 +0800 | [diff] [blame] | 881 | #elif defined(CONFIG_MACH_SUN8I_V3S) |
Icenowy Zheng | aff2d3a | 2020-10-16 17:33:08 +0800 | [diff] [blame] | 882 | uint16_t socid = SOCID_V3S; |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 883 | #elif defined(CONFIG_MACH_SUN50I) |
| 884 | uint16_t socid = SOCID_A64; |
Andre Przywara | 5d0d28f | 2017-02-16 01:20:26 +0000 | [diff] [blame] | 885 | #elif defined(CONFIG_MACH_SUN50I_H5) |
| 886 | uint16_t socid = SOCID_H5; |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 887 | #endif |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 888 | |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 889 | mctl_sys_init(socid, ¶); |
| 890 | if (mctl_channel_init(socid, ¶)) |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 891 | return 0; |
| 892 | |
| 893 | if (para.dual_rank) |
| 894 | writel(0x00000303, &mctl_ctl->odtmap); |
| 895 | else |
| 896 | writel(0x00000201, &mctl_ctl->odtmap); |
| 897 | udelay(1); |
| 898 | |
| 899 | /* odt delay */ |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 900 | if (socid == SOCID_H3) |
| 901 | writel(0x0c000400, &mctl_ctl->odtcfg); |
| 902 | |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 903 | if (socid == SOCID_A64 || socid == SOCID_H5 || socid == SOCID_R40) { |
| 904 | /* VTF enable (tpr13[8] == 1) */ |
Andre Przywara | 5d0d28f | 2017-02-16 01:20:26 +0000 | [diff] [blame] | 905 | setbits_le32(&mctl_ctl->vtfcr, |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 906 | (socid != SOCID_A64 ? 3 : 2) << 8); |
| 907 | /* DQ hold disable (tpr13[26] == 1) */ |
Jens Kuske | f613817 | 2017-01-02 11:48:42 +0000 | [diff] [blame] | 908 | clrbits_le32(&mctl_ctl->pgcr[2], (1 << 13)); |
| 909 | } |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 910 | |
| 911 | /* clear credit value */ |
| 912 | setbits_le32(&mctl_com->cccr, 1 << 31); |
| 913 | udelay(10); |
| 914 | |
Icenowy Zheng | c3391f6 | 2021-02-26 00:13:25 +0800 | [diff] [blame] | 915 | if (socid == SOCID_R40) { |
| 916 | mctl_r40_detect_rank_count(¶); |
| 917 | mctl_set_cr(SOCID_R40, ¶); |
| 918 | } |
| 919 | |
Chen-Yu Tsai | 143ef79 | 2016-12-01 19:09:57 +0800 | [diff] [blame] | 920 | mctl_auto_detect_dram_size(socid, ¶); |
| 921 | mctl_set_cr(socid, ¶); |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 922 | |
Icenowy Zheng | 9e9b073 | 2021-02-26 00:13:24 +0800 | [diff] [blame] | 923 | size = mctl_calc_rank_size(¶.ranks[0]); |
| 924 | if (socid == SOCID_A64 || socid == SOCID_R40) { |
| 925 | if (para.dual_rank) |
| 926 | size += mctl_calc_rank_size(¶.ranks[1]); |
| 927 | } else if (para.dual_rank) { |
| 928 | size *= 2; |
| 929 | } |
| 930 | |
| 931 | return size; |
Jens Kuske | 53f018e | 2015-11-17 15:12:59 +0100 | [diff] [blame] | 932 | } |