15685f66aSYouMin Chen /* SPDX-License-Identifier: GPL-2.0+ */ 25685f66aSYouMin Chen /* 35685f66aSYouMin Chen * Copyright (C) 2018 Rockchip Electronics Co., Ltd 45685f66aSYouMin Chen */ 55685f66aSYouMin Chen 65685f66aSYouMin Chen #ifndef _ASM_ARCH_SDRAM_SHARE_H 75685f66aSYouMin Chen #define _ASM_ARCH_SDRAM_SHARE_H 85685f66aSYouMin Chen 9*bbf5a1e4SYouMin Chen #ifndef MHZ 10*bbf5a1e4SYouMin Chen #define MHZ (1000 * 1000) 11*bbf5a1e4SYouMin Chen #endif 12*bbf5a1e4SYouMin Chen 135e6e8f2dSYouMin Chen #define PATTERN (0x5aa5f00f) 145685f66aSYouMin Chen 15*bbf5a1e4SYouMin Chen #define MIN(a, b) (((a) > (b)) ? (b) : (a)) 16*bbf5a1e4SYouMin Chen #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 17*bbf5a1e4SYouMin Chen 185685f66aSYouMin Chen struct sdram_cap_info { 195685f66aSYouMin Chen unsigned int rank; 205685f66aSYouMin Chen unsigned int col; 215685f66aSYouMin Chen /* 3:8bank, 2:4bank */ 225685f66aSYouMin Chen unsigned int bk; 235685f66aSYouMin Chen /* channel buswidth, 2:32bit, 1:16bit, 0:8bit */ 245685f66aSYouMin Chen unsigned int bw; 255685f66aSYouMin Chen /* die buswidth, 2:32bit, 1:16bit, 0:8bit */ 265685f66aSYouMin Chen unsigned int dbw; 275685f66aSYouMin Chen unsigned int row_3_4; 285685f66aSYouMin Chen unsigned int cs0_row; 295685f66aSYouMin Chen unsigned int cs1_row; 305685f66aSYouMin Chen unsigned int cs0_high16bit_row; 315685f66aSYouMin Chen unsigned int cs1_high16bit_row; 325685f66aSYouMin Chen unsigned int ddrconfig; 335685f66aSYouMin Chen }; 345685f66aSYouMin Chen 355685f66aSYouMin Chen struct sdram_base_params { 365685f66aSYouMin Chen unsigned int ddr_freq; 375685f66aSYouMin Chen unsigned int dramtype; 385685f66aSYouMin Chen unsigned int num_channels; 395685f66aSYouMin Chen unsigned int stride; 405685f66aSYouMin Chen unsigned int odt; 415685f66aSYouMin Chen }; 425685f66aSYouMin Chen 435685f66aSYouMin Chen /* 445685f66aSYouMin Chen * sys_reg bitfield struct 455685f66aSYouMin Chen * [31] row_3_4_ch1 465685f66aSYouMin Chen * [30] row_3_4_ch0 475685f66aSYouMin Chen * [29:28] chinfo 485685f66aSYouMin Chen * [27] rank_ch1 495685f66aSYouMin Chen * [26:25] col_ch1 505685f66aSYouMin Chen * [24] bk_ch1 515685f66aSYouMin Chen * [23:22] cs0_row_ch1 525685f66aSYouMin Chen * [21:20] cs1_row_ch1 535685f66aSYouMin Chen * [19:18] bw_ch1 545685f66aSYouMin Chen * [17:16] dbw_ch1; 555685f66aSYouMin Chen * [15:13] ddrtype 565685f66aSYouMin Chen * [12] channelnum 575685f66aSYouMin Chen * [11] rank_ch0 585685f66aSYouMin Chen * [10:9] col_ch0 595685f66aSYouMin Chen * [8] bk_ch0 605685f66aSYouMin Chen * [7:6] cs0_row_ch0 615685f66aSYouMin Chen * [5:4] cs1_row_ch0 625685f66aSYouMin Chen * [3:2] bw_ch0 635685f66aSYouMin Chen * [1:0] dbw_ch0 645685f66aSYouMin Chen */ 655685f66aSYouMin Chen 665685f66aSYouMin Chen #define DDR_SYS_REG_VERSION (0x2) 675685f66aSYouMin Chen #define SYS_REG_ENC_ROW_3_4(n, ch) ((n) << (30 + (ch))) 685685f66aSYouMin Chen #define SYS_REG_DEC_ROW_3_4(n, ch) (((n) >> (30 + (ch))) & 0x1) 695685f66aSYouMin Chen #define SYS_REG_ENC_CHINFO(ch) (1 << (28 + (ch))) 705685f66aSYouMin Chen #define SYS_REG_ENC_DDRTYPE(n) ((n) << 13) 715685f66aSYouMin Chen #define SYS_REG_DEC_DDRTYPE(n) (((n) >> 13) & 0x7) 725685f66aSYouMin Chen #define SYS_REG_ENC_NUM_CH(n) (((n) - 1) << 12) 735685f66aSYouMin Chen #define SYS_REG_DEC_NUM_CH(n) (1 + (((n) >> 12) & 0x1)) 745685f66aSYouMin Chen #define SYS_REG_ENC_RANK(n, ch) (((n) - 1) << (11 + ((ch) * 16))) 755685f66aSYouMin Chen #define SYS_REG_DEC_RANK(n, ch) (1 + (((n) >> (11 + 16 * (ch))) & 0x1)) 765685f66aSYouMin Chen #define SYS_REG_ENC_COL(n, ch) (((n) - 9) << (9 + ((ch) * 16))) 775685f66aSYouMin Chen #define SYS_REG_DEC_COL(n, ch) (9 + (((n) >> (9 + 16 * (ch))) & 0x3)) 785685f66aSYouMin Chen #define SYS_REG_ENC_BK(n, ch) (((n) == 3 ? 0 : 1) << \ 795685f66aSYouMin Chen (8 + ((ch) * 16))) 805685f66aSYouMin Chen #define SYS_REG_DEC_BK(n, ch) (3 - (((n) >> (8 + 16 * (ch))) & 0x1)) 815685f66aSYouMin Chen #define SYS_REG_ENC_BW(n, ch) ((2 >> (n)) << (2 + ((ch) * 16))) 825685f66aSYouMin Chen #define SYS_REG_DEC_BW(n, ch) (2 >> (((n) >> (2 + 16 * (ch))) & 0x3)) 835685f66aSYouMin Chen #define SYS_REG_ENC_DBW(n, ch) ((2 >> (n)) << (0 + ((ch) * 16))) 845685f66aSYouMin Chen #define SYS_REG_DEC_DBW(n, ch) (2 >> (((n) >> (0 + 16 * (ch))) & 0x3)) 855685f66aSYouMin Chen /* sys reg 3 */ 865685f66aSYouMin Chen #define SYS_REG_ENC_VERSION(n) ((n) << 28) 875685f66aSYouMin Chen #define SYS_REG_DEC_VERSION(n) (((n) >> 28) & 0xf) 885685f66aSYouMin Chen #define SYS_REG_ENC_CS0_ROW(n, os_reg2, os_reg3, ch) do { \ 895685f66aSYouMin Chen (os_reg2) |= (((n) - 13) & 0x3) << (6 + 16 * (ch)); \ 905685f66aSYouMin Chen (os_reg3) |= ((((n) - 13) & 0x4) >> 2) << \ 915685f66aSYouMin Chen (5 + 2 * (ch)); \ 925685f66aSYouMin Chen } while (0) 935685f66aSYouMin Chen 945685f66aSYouMin Chen #define SYS_REG_DEC_CS0_ROW(os_reg2, os_reg3, ch) \ 955685f66aSYouMin Chen ((((((os_reg2) >> (6 + 16 * (ch)) & 0x3) | \ 965685f66aSYouMin Chen ((((os_reg3) >> (5 + 2 * (ch))) & 0x1) << 2)) + 1) & 0x7) + 12) 975685f66aSYouMin Chen 985685f66aSYouMin Chen #define SYS_REG_ENC_CS1_ROW(n, os_reg2, os_reg3, ch) do { \ 995685f66aSYouMin Chen (os_reg2) &= (~(0x3 << (4 + 16 * (ch)))); \ 1005685f66aSYouMin Chen (os_reg3) &= (~(0x1 << (4 + 2 * (ch)))); \ 1015685f66aSYouMin Chen (os_reg2) |= (((n) - 13) & 0x3) << (4 + 16 * (ch)); \ 1025685f66aSYouMin Chen (os_reg3) |= ((((n) - 13) & 0x4) >> 2) << \ 1035685f66aSYouMin Chen (4 + 2 * (ch)); \ 1045685f66aSYouMin Chen } while (0) 1055685f66aSYouMin Chen 1065685f66aSYouMin Chen #define SYS_REG_DEC_CS1_ROW(os_reg2, os_reg3, ch) \ 1075685f66aSYouMin Chen ((((((os_reg2) >> (4 + 16 * (ch)) & 0x3) | \ 1085685f66aSYouMin Chen ((((os_reg3) >> (4 + 2 * (ch))) & 0x1) << 2)) + 1) & 0x7) + 12) 1095685f66aSYouMin Chen 1105685f66aSYouMin Chen #define SYS_REG_ENC_CS1_COL(n, ch) (((n) - 9) << (0 + 2 * (ch))) 1115685f66aSYouMin Chen #define SYS_REG_DEC_CS1_COL(n, ch) (9 + (((n) >> (0 + 2 * (ch))) & 0x3)) 1125685f66aSYouMin Chen 1135685f66aSYouMin Chen void sdram_org_config(struct sdram_cap_info *cap_info, 1145685f66aSYouMin Chen struct sdram_base_params *base, 1155685f66aSYouMin Chen u32 *p_os_reg2, u32 *p_os_reg3, u32 channel); 1165685f66aSYouMin Chen 1175685f66aSYouMin Chen int sdram_detect_bw(struct sdram_cap_info *cap_info); 1185685f66aSYouMin Chen int sdram_detect_cs(struct sdram_cap_info *cap_info); 1195685f66aSYouMin Chen int sdram_detect_col(struct sdram_cap_info *cap_info, 1205685f66aSYouMin Chen u32 coltmp); 1215685f66aSYouMin Chen int sdram_detect_bank(struct sdram_cap_info *cap_info, 1225685f66aSYouMin Chen u32 coltmp, u32 bktmp); 1235685f66aSYouMin Chen int sdram_detect_bg(struct sdram_cap_info *cap_info, 1245685f66aSYouMin Chen u32 coltmp); 1255685f66aSYouMin Chen int sdram_detect_dbw(struct sdram_cap_info *cap_info, u32 dram_type); 1265685f66aSYouMin Chen int sdram_detect_row(struct sdram_cap_info *cap_info, 1275685f66aSYouMin Chen u32 coltmp, u32 bktmp, u32 rowtmp); 1285685f66aSYouMin Chen int sdram_detect_row_3_4(struct sdram_cap_info *cap_info, 1295685f66aSYouMin Chen u32 coltmp, u32 bktmp); 1305685f66aSYouMin Chen int sdram_detect_high_row(struct sdram_cap_info *cap_info); 1315685f66aSYouMin Chen int sdram_detect_cs1_row(struct sdram_cap_info *cap_info, u32 dram_type); 1325685f66aSYouMin Chen 1335685f66aSYouMin Chen void sdram_print_dram_type(unsigned char dramtype); 1345685f66aSYouMin Chen void sdram_print_ddr_info(struct sdram_cap_info *cap_info, 1355685f66aSYouMin Chen struct sdram_base_params *base, u32 split); 1365685f66aSYouMin Chen u64 sdram_get_cs_cap(struct sdram_cap_info *cap_info, u32 cs, u32 dram_type); 1375685f66aSYouMin Chen void sdram_copy_to_reg(u32 *dest, const u32 *src, u32 n); 1385685f66aSYouMin Chen 1395685f66aSYouMin Chen #endif 140