Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 Socionext Inc. |
| 3 | * |
Wataru Okoshi | 50fda9d | 2017-01-18 16:24:38 +0900 | [diff] [blame] | 4 | * based on commit 1f6feb76e7f9753f51955444e422486521f9b3a3 of Diag |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <linux/bitops.h> |
Masahiro Yamada | e4e789d | 2017-01-21 18:05:24 +0900 | [diff] [blame^] | 11 | #include <linux/compat.h> |
| 12 | #include <linux/errno.h> |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 13 | #include <linux/io.h> |
| 14 | #include <linux/sizes.h> |
| 15 | #include <asm/processor.h> |
| 16 | |
| 17 | #include "../init.h" |
Masahiro Yamada | 5ffc49b | 2016-10-27 23:47:03 +0900 | [diff] [blame] | 18 | #include "ddruqphy-regs.h" |
Masahiro Yamada | 900117b | 2016-05-24 21:13:59 +0900 | [diff] [blame] | 19 | #include "umc64-regs.h" |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 20 | |
| 21 | #define DRAM_CH_NR 3 |
| 22 | |
| 23 | enum dram_freq { |
| 24 | DRAM_FREQ_1866M, |
| 25 | DRAM_FREQ_NR, |
| 26 | }; |
| 27 | |
| 28 | enum dram_size { |
| 29 | DRAM_SZ_256M, |
| 30 | DRAM_SZ_512M, |
| 31 | DRAM_SZ_NR, |
| 32 | }; |
| 33 | |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 34 | enum dram_board { /* board type */ |
| 35 | DRAM_BOARD_LD20_REF, /* LD20 reference */ |
| 36 | DRAM_BOARD_LD20_GLOBAL, /* LD20 TV */ |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 37 | DRAM_BOARD_LD20_C1, /* LD20 TV C1 */ |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 38 | DRAM_BOARD_LD21_REF, /* LD21 reference */ |
| 39 | DRAM_BOARD_LD21_GLOBAL, /* LD21 TV */ |
| 40 | DRAM_BOARD_NR, |
| 41 | }; |
| 42 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 43 | /* PHY */ |
| 44 | static const int ddrphy_adrctrl[DRAM_BOARD_NR][DRAM_CH_NR] = { |
| 45 | {268 - 262, 268 - 263, 268 - 378}, /* LD20 reference */ |
| 46 | {268 - 262, 268 - 263, 268 - 378}, /* LD20 TV */ |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 47 | {268 - 262, 268 - 263, 268 - 378}, /* LD20 TV C1 */ |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 48 | {268 - 212, 268 - 268, /* No CH2 */}, /* LD21 reference */ |
| 49 | {268 - 212, 268 - 268, /* No CH2 */}, /* LD21 TV */ |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 50 | }; |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 51 | |
| 52 | static const int ddrphy_dlltrimclk[DRAM_BOARD_NR][DRAM_CH_NR] = { |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 53 | {268, 268, 268}, /* LD20 reference */ |
| 54 | {268, 268, 268}, /* LD20 TV */ |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 55 | {189, 189, 189}, /* LD20 TV C1 */ |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 56 | {268, 268 + 252, /* No CH2 */}, /* LD21 reference */ |
| 57 | {268, 268 + 202, /* No CH2 */}, /* LD21 TV */ |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 58 | }; |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 59 | |
| 60 | static const int ddrphy_dllrecalib[DRAM_BOARD_NR][DRAM_CH_NR] = { |
| 61 | {268 - 378, 268 - 263, 268 - 378}, /* LD20 reference */ |
| 62 | {268 - 378, 268 - 263, 268 - 378}, /* LD20 TV */ |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 63 | {268 - 378, 268 - 263, 268 - 378}, /* LD20 TV C1 */ |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 64 | {268 - 212, 268 - 536, /* No CH2 */}, /* LD21 reference */ |
| 65 | {268 - 212, 268 - 536, /* No CH2 */}, /* LD21 TV */ |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 66 | }; |
| 67 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 68 | static const u32 ddrphy_phy_pad_ctrl[DRAM_BOARD_NR][DRAM_CH_NR] = { |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 69 | {0x50B840B1, 0x50B840B1, 0x50B840B1}, /* LD20 reference */ |
| 70 | {0x50BB40B1, 0x50BB40B1, 0x50BB40B1}, /* LD20 TV */ |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 71 | {0x50BB40B1, 0x50BB40B1, 0x50BB40B1}, /* LD20 TV C1 */ |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 72 | {0x50BB40B4, 0x50B840B1, /* No CH2 */}, /* LD21 reference */ |
| 73 | {0x50BB40B4, 0x50B840B1, /* No CH2 */}, /* LD21 TV */ |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 74 | }; |
| 75 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 76 | static const u32 ddrphy_scl_gate_timing[DRAM_CH_NR] = { |
| 77 | 0x00000140, 0x00000180, 0x00000140 |
| 78 | }; |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 79 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 80 | static const int ddrphy_op_dq_shift_val[DRAM_BOARD_NR][DRAM_CH_NR][32] = { |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 81 | { /* LD20 reference */ |
| 82 | { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 83 | 2, 1, 0, 1, 2, 1, 1, 1, |
| 84 | 2, 1, 1, 2, 1, 1, 1, 1, |
| 85 | 1, 2, 1, 1, 1, 2, 1, 1, |
| 86 | 2, 2, 0, 1, 1, 2, 2, 1, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 87 | }, |
| 88 | { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 89 | 1, 1, 0, 1, 2, 2, 1, 1, |
| 90 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 91 | 1, 1, 0, 0, 1, 1, 0, 0, |
| 92 | 0, 1, 1, 1, 2, 1, 2, 1, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 93 | }, |
| 94 | { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 95 | 2, 2, 0, 2, 1, 1, 2, 1, |
| 96 | 1, 1, 0, 1, 1, -1, 1, 1, |
| 97 | 2, 2, 2, 2, 1, 1, 1, 1, |
| 98 | 1, 1, 1, 0, 2, 2, 1, 2, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 99 | }, |
| 100 | }, |
| 101 | { /* LD20 TV */ |
| 102 | { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 103 | 2, 1, 0, 1, 2, 1, 1, 1, |
| 104 | 2, 1, 1, 2, 1, 1, 1, 1, |
| 105 | 1, 2, 1, 1, 1, 2, 1, 1, |
| 106 | 2, 2, 0, 1, 1, 2, 2, 1, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 107 | }, |
| 108 | { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 109 | 1, 1, 0, 1, 2, 2, 1, 1, |
| 110 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 111 | 1, 1, 0, 0, 1, 1, 0, 0, |
| 112 | 0, 1, 1, 1, 2, 1, 2, 1, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 113 | }, |
| 114 | { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 115 | 2, 2, 0, 2, 1, 1, 2, 1, |
| 116 | 1, 1, 0, 1, 1, -1, 1, 1, |
| 117 | 2, 2, 2, 2, 1, 1, 1, 1, |
| 118 | 1, 1, 1, 0, 2, 2, 1, 2, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 119 | }, |
| 120 | }, |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 121 | { /* LD20 TV C1 */ |
| 122 | { |
| 123 | 2, 1, 0, 1, 2, 1, 1, 1, |
| 124 | 2, 1, 1, 2, 1, 1, 1, 1, |
| 125 | 1, 2, 1, 1, 1, 2, 1, 1, |
| 126 | 2, 2, 0, 1, 1, 2, 2, 1, |
| 127 | }, |
| 128 | { |
| 129 | 1, 1, 0, 1, 2, 2, 1, 1, |
| 130 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 131 | 1, 1, 0, 0, 1, 1, 0, 0, |
| 132 | 0, 1, 1, 1, 2, 1, 2, 1, |
| 133 | }, |
| 134 | { |
| 135 | 2, 2, 0, 2, 1, 1, 2, 1, |
| 136 | 1, 1, 0, 1, 1, -1, 1, 1, |
| 137 | 2, 2, 2, 2, 1, 1, 1, 1, |
| 138 | 1, 1, 1, 0, 2, 2, 1, 2, |
| 139 | }, |
| 140 | }, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 141 | { /* LD21 reference */ |
| 142 | { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 143 | 1, 1, 0, 1, 1, 1, 1, 1, |
| 144 | 1, 0, 0, 0, 1, 1, 0, 2, |
| 145 | 1, 1, 0, 0, 1, 1, 1, 1, |
| 146 | 1, 0, 0, 0, 1, 0, 0, 1, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 147 | }, |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 148 | { 1, 0, 2, 1, 1, 1, 1, 0, |
| 149 | 1, 0, 0, 1, 0, 1, 0, 0, |
| 150 | 1, 0, 1, 0, 1, 1, 1, 0, |
| 151 | 1, 1, 1, 1, 0, 1, 0, 0, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 152 | }, |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 153 | /* No CH2 */ |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 154 | }, |
| 155 | { /* LD21 TV */ |
| 156 | { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 157 | 1, 1, 0, 1, 1, 1, 1, 1, |
| 158 | 1, 0, 0, 0, 1, 1, 0, 2, |
| 159 | 1, 1, 0, 0, 1, 1, 1, 1, |
| 160 | 1, 0, 0, 0, 1, 0, 0, 1, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 161 | }, |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 162 | { 1, 0, 2, 1, 1, 1, 1, 0, |
| 163 | 1, 0, 0, 1, 0, 1, 0, 0, |
| 164 | 1, 0, 1, 0, 1, 1, 1, 0, |
| 165 | 1, 1, 1, 1, 0, 1, 0, 0, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 166 | }, |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 167 | /* No CH2 */ |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 168 | }, |
| 169 | }; |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 170 | |
| 171 | static int ddrphy_ip_dq_shift_val[DRAM_BOARD_NR][DRAM_CH_NR][32] = { |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 172 | { /* LD20 reference */ |
| 173 | { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 174 | 3, 3, 3, 2, 3, 2, 0, 2, |
| 175 | 2, 3, 3, 1, 2, 2, 2, 2, |
| 176 | 2, 2, 2, 2, 0, 1, 1, 1, |
| 177 | 2, 2, 2, 2, 3, 0, 2, 2, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 178 | }, |
| 179 | { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 180 | 2, 2, 1, 1, -1, 1, 1, 1, |
| 181 | 2, 0, 2, 2, 2, 1, 0, 2, |
| 182 | 2, 1, 2, 1, 0, 1, 1, 1, |
| 183 | 2, 2, 2, 2, 2, 2, 2, 2, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 184 | }, |
| 185 | { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 186 | 2, 2, 3, 2, 1, 2, 2, 2, |
| 187 | 2, 3, 4, 2, 3, 4, 3, 3, |
| 188 | 2, 2, 1, 2, 1, 1, 1, 1, |
| 189 | 2, 2, 2, 2, 1, 2, 2, 1, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 190 | }, |
| 191 | }, |
| 192 | { /* LD20 TV */ |
| 193 | { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 194 | 3, 3, 3, 2, 3, 2, 0, 2, |
| 195 | 2, 3, 3, 1, 2, 2, 2, 2, |
| 196 | 2, 2, 2, 2, 0, 1, 1, 1, |
| 197 | 2, 2, 2, 2, 3, 0, 2, 2, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 198 | }, |
| 199 | { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 200 | 2, 2, 1, 1, -1, 1, 1, 1, |
| 201 | 2, 0, 2, 2, 2, 1, 0, 2, |
| 202 | 2, 1, 2, 1, 0, 1, 1, 1, |
| 203 | 2, 2, 2, 2, 2, 2, 2, 2, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 204 | }, |
| 205 | { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 206 | 2, 2, 3, 2, 1, 2, 2, 2, |
| 207 | 2, 3, 4, 2, 3, 4, 3, 3, |
| 208 | 2, 2, 1, 2, 1, 1, 1, 1, |
| 209 | 2, 2, 2, 2, 1, 2, 2, 1, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 210 | }, |
| 211 | }, |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 212 | { /* LD20 TV C1 */ |
| 213 | { |
| 214 | 3, 3, 3, 2, 3, 2, 0, 2, |
| 215 | 2, 3, 3, 1, 2, 2, 2, 2, |
| 216 | 2, 2, 2, 2, 0, 1, 1, 1, |
| 217 | 2, 2, 2, 2, 3, 0, 2, 2, |
| 218 | }, |
| 219 | { |
| 220 | 2, 2, 1, 1, -1, 1, 1, 1, |
| 221 | 2, 0, 2, 2, 2, 1, 0, 2, |
| 222 | 2, 1, 2, 1, 0, 1, 1, 1, |
| 223 | 2, 2, 2, 2, 2, 2, 2, 2, |
| 224 | }, |
| 225 | { |
| 226 | 2, 2, 3, 2, 1, 2, 2, 2, |
| 227 | 2, 3, 4, 2, 3, 4, 3, 3, |
| 228 | 2, 2, 1, 2, 1, 1, 1, 1, |
| 229 | 2, 2, 2, 2, 1, 2, 2, 1, |
| 230 | }, |
| 231 | }, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 232 | { /* LD21 reference */ |
| 233 | { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 234 | 2, 2, 2, 2, 1, 2, 2, 2, |
| 235 | 2, 3, 3, 2, 2, 2, 2, 2, |
| 236 | 2, 1, 2, 2, 1, 1, 1, 1, |
| 237 | 2, 2, 2, 3, 1, 2, 2, 2, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 238 | }, |
| 239 | { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 240 | 3, 4, 4, 1, 0, 1, 1, 1, |
| 241 | 1, 2, 1, 2, 2, 3, 3, 2, |
| 242 | 1, 0, 2, 1, 1, 0, 1, 0, |
| 243 | 0, 1, 0, 0, 1, 1, 0, 1, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 244 | }, |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 245 | /* No CH2 */ |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 246 | }, |
| 247 | { /* LD21 TV */ |
| 248 | { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 249 | 2, 2, 2, 2, 1, 2, 2, 2, |
| 250 | 2, 3, 3, 2, 2, 2, 2, 2, |
| 251 | 2, 1, 2, 2, 1, 1, 1, 1, |
| 252 | 2, 2, 2, 3, 1, 2, 2, 2, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 253 | }, |
| 254 | { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 255 | 3, 4, 4, 1, 0, 1, 1, 1, |
| 256 | 1, 2, 1, 2, 2, 3, 3, 2, |
| 257 | 1, 0, 2, 1, 1, 0, 1, 0, |
| 258 | 0, 1, 0, 0, 1, 1, 0, 1, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 259 | }, |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 260 | /* No CH2 */ |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 261 | }, |
| 262 | }; |
| 263 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 264 | /* DDR PHY */ |
| 265 | static void ddrphy_select_lane(void __iomem *phy_base, unsigned int lane, |
| 266 | unsigned int bit) |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 267 | { |
Masahiro Yamada | 5864e16 | 2016-12-05 18:31:37 +0900 | [diff] [blame] | 268 | WARN_ON(lane >= 1 << PHY_LANE_SEL_LANE_WIDTH); |
| 269 | WARN_ON(bit >= 1 << PHY_LANE_SEL_BIT_WIDTH); |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 270 | |
| 271 | writel((bit << PHY_LANE_SEL_BIT_SHIFT) | |
| 272 | (lane << PHY_LANE_SEL_LANE_SHIFT), |
| 273 | phy_base + PHY_LANE_SEL); |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 274 | } |
| 275 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 276 | static void ddrphy_init(void __iomem *phy_base, enum dram_board board, int ch) |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 277 | { |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 278 | writel(0x0C001001, phy_base + PHY_UNIQUIFY_TSMC_IO_1); |
| 279 | while (!(readl(phy_base + PHY_UNIQUIFY_TSMC_IO_1) & BIT(1))) |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 280 | cpu_relax(); |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 281 | writel(0x0C001000, phy_base + PHY_UNIQUIFY_TSMC_IO_1); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 282 | |
| 283 | writel(0x00000000, phy_base + PHY_DLL_INCR_TRIM_3); |
| 284 | writel(0x00000000, phy_base + PHY_DLL_INCR_TRIM_1); |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 285 | ddrphy_select_lane(phy_base, 0, 0); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 286 | writel(0x00000005, phy_base + PHY_DLL_TRIM_1); |
| 287 | writel(0x0000000a, phy_base + PHY_DLL_TRIM_3); |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 288 | ddrphy_select_lane(phy_base, 6, 0); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 289 | writel(0x00000005, phy_base + PHY_DLL_TRIM_1); |
| 290 | writel(0x0000000a, phy_base + PHY_DLL_TRIM_3); |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 291 | ddrphy_select_lane(phy_base, 12, 0); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 292 | writel(0x00000005, phy_base + PHY_DLL_TRIM_1); |
| 293 | writel(0x0000000a, phy_base + PHY_DLL_TRIM_3); |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 294 | ddrphy_select_lane(phy_base, 18, 0); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 295 | writel(0x00000005, phy_base + PHY_DLL_TRIM_1); |
| 296 | writel(0x0000000a, phy_base + PHY_DLL_TRIM_3); |
| 297 | writel(0x00000001, phy_base + PHY_SCL_WINDOW_TRIM); |
| 298 | writel(0x00000000, phy_base + PHY_UNQ_ANALOG_DLL_1); |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 299 | writel(ddrphy_phy_pad_ctrl[board][ch], phy_base + PHY_PAD_CTRL); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 300 | writel(0x00000070, phy_base + PHY_VREF_TRAINING); |
| 301 | writel(0x01000075, phy_base + PHY_SCL_CONFIG_1); |
| 302 | writel(0x00000501, phy_base + PHY_SCL_CONFIG_2); |
| 303 | writel(0x00000000, phy_base + PHY_SCL_CONFIG_3); |
| 304 | writel(0x000261c0, phy_base + PHY_DYNAMIC_WRITE_BIT_LVL); |
| 305 | writel(0x00000000, phy_base + PHY_SCL_CONFIG_4); |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 306 | writel(ddrphy_scl_gate_timing[ch], phy_base + PHY_SCL_GATE_TIMING); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 307 | writel(0x02a000a0, phy_base + PHY_WRLVL_DYN_ODT); |
| 308 | writel(0x00840004, phy_base + PHY_WRLVL_ON_OFF); |
| 309 | writel(0x0000020d, phy_base + PHY_DLL_ADRCTRL); |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 310 | ddrphy_select_lane(phy_base, 0, 0); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 311 | writel(0x0000008d, phy_base + PHY_DLL_TRIM_CLK); |
| 312 | writel(0xa800100d, phy_base + PHY_DLL_RECALIB); |
| 313 | writel(0x00005076, phy_base + PHY_SCL_LATENCY); |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static int ddrphy_to_dly_step(void __iomem *phy_base, unsigned int freq, |
| 317 | int delay) |
| 318 | { |
| 319 | int mdl; |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 320 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 321 | mdl = (readl(phy_base + PHY_DLL_ADRCTRL) & PHY_DLL_ADRCTRL_MDL_MASK) >> |
| 322 | PHY_DLL_ADRCTRL_MDL_SHIFT; |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 323 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 324 | return DIV_ROUND_CLOSEST((long)freq * delay * mdl, 2 * 1000000L); |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 325 | } |
| 326 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 327 | static void ddrphy_set_delay(void __iomem *phy_base, unsigned int reg, |
| 328 | u32 mask, u32 incr, int dly_step) |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 329 | { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 330 | u32 tmp; |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 331 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 332 | tmp = readl(phy_base + reg); |
| 333 | tmp &= ~mask; |
| 334 | tmp |= min_t(u32, abs(dly_step), mask); |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 335 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 336 | if (dly_step >= 0) |
| 337 | tmp |= incr; |
| 338 | else |
| 339 | tmp &= ~incr; |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 340 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 341 | writel(tmp, phy_base + reg); |
| 342 | } |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 343 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 344 | static void ddrphy_set_dll_recalib(void __iomem *phy_base, int dly_step) |
| 345 | { |
| 346 | ddrphy_set_delay(phy_base, PHY_DLL_RECALIB, |
| 347 | PHY_DLL_RECALIB_TRIM_MASK, PHY_DLL_RECALIB_INCR, |
| 348 | dly_step); |
| 349 | } |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 350 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 351 | static void ddrphy_set_dll_adrctrl(void __iomem *phy_base, int dly_step) |
| 352 | { |
| 353 | ddrphy_set_delay(phy_base, PHY_DLL_ADRCTRL, |
| 354 | PHY_DLL_ADRCTRL_TRIM_MASK, PHY_DLL_ADRCTRL_INCR, |
| 355 | dly_step); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 356 | } |
| 357 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 358 | static void ddrphy_set_dll_trim_clk(void __iomem *phy_base, int dly_step) |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 359 | { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 360 | ddrphy_select_lane(phy_base, 0, 0); |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 361 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 362 | ddrphy_set_delay(phy_base, PHY_DLL_TRIM_CLK, |
| 363 | PHY_DLL_TRIM_CLK_MASK, PHY_DLL_TRIM_CLK_INCR, |
| 364 | dly_step); |
| 365 | } |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 366 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 367 | static void ddrphy_init_tail(void __iomem *phy_base, enum dram_board board, |
| 368 | unsigned int freq, int ch) |
| 369 | { |
| 370 | int step; |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 371 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 372 | step = ddrphy_to_dly_step(phy_base, freq, ddrphy_adrctrl[board][ch]); |
| 373 | ddrphy_set_dll_adrctrl(phy_base, step); |
| 374 | |
| 375 | step = ddrphy_to_dly_step(phy_base, freq, ddrphy_dlltrimclk[board][ch]); |
| 376 | ddrphy_set_dll_trim_clk(phy_base, step); |
| 377 | |
| 378 | step = ddrphy_to_dly_step(phy_base, freq, ddrphy_dllrecalib[board][ch]); |
| 379 | ddrphy_set_dll_recalib(phy_base, step); |
| 380 | } |
| 381 | |
| 382 | static void ddrphy_shift_one_dq(void __iomem *phy_base, unsigned int reg, |
| 383 | u32 mask, u32 incr, int shift_val) |
| 384 | { |
| 385 | u32 tmp; |
| 386 | int val; |
| 387 | |
| 388 | tmp = readl(phy_base + reg); |
| 389 | |
| 390 | val = tmp & mask; |
| 391 | if (!(tmp & incr)) |
| 392 | val = -val; |
| 393 | |
| 394 | val += shift_val; |
| 395 | |
| 396 | tmp &= ~(incr | mask); |
| 397 | tmp |= min_t(u32, abs(val), mask); |
| 398 | if (val >= 0) |
| 399 | tmp |= incr; |
| 400 | |
| 401 | writel(tmp, phy_base + reg); |
| 402 | } |
| 403 | |
| 404 | static void ddrphy_shift_dq(void __iomem *phy_base, unsigned int reg, |
| 405 | u32 mask, u32 incr, u32 override, |
| 406 | const int *shift_val_array) |
| 407 | { |
| 408 | u32 tmp; |
| 409 | int dx, bit; |
| 410 | |
| 411 | tmp = readl(phy_base + reg); |
| 412 | tmp |= override; |
| 413 | writel(tmp, phy_base + reg); |
| 414 | |
| 415 | for (dx = 0; dx < 4; dx++) { |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 416 | for (bit = 0; bit < 8; bit++) { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 417 | ddrphy_select_lane(phy_base, |
| 418 | (PHY_BITLVL_DLY_WIDTH + 1) * dx, |
| 419 | bit); |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 420 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 421 | ddrphy_shift_one_dq(phy_base, reg, mask, incr, |
| 422 | shift_val_array[dx * 8 + bit]); |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 423 | } |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 424 | } |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 425 | |
| 426 | ddrphy_select_lane(phy_base, 0, 0); |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | static int ddrphy_training(void __iomem *phy_base, enum dram_board board, |
| 430 | int ch) |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 431 | { |
| 432 | writel(0x0000000f, phy_base + PHY_WRLVL_AUTOINC_TRIM); |
| 433 | writel(0x00010000, phy_base + PHY_DLL_TRIM_2); |
| 434 | writel(0x50000000, phy_base + PHY_SCL_START); |
| 435 | |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 436 | while (readl(phy_base + PHY_SCL_START) & PHY_SCL_START_GO_DONE) |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 437 | cpu_relax(); |
| 438 | |
| 439 | writel(0x00000000, phy_base + PHY_DISABLE_GATING_FOR_SCL); |
| 440 | writel(0xff00ff00, phy_base + PHY_SCL_DATA_0); |
| 441 | writel(0xff00ff00, phy_base + PHY_SCL_DATA_1); |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 442 | writel(0xFBF8FFFF, phy_base + PHY_SCL_START_ADDR); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 443 | writel(0x11000000, phy_base + PHY_SCL_START); |
| 444 | |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 445 | while (readl(phy_base + PHY_SCL_START) & PHY_SCL_START_GO_DONE) |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 446 | cpu_relax(); |
| 447 | |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 448 | writel(0xFBF0FFFF, phy_base + PHY_SCL_START_ADDR); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 449 | writel(0x30500000, phy_base + PHY_SCL_START); |
| 450 | |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 451 | while (readl(phy_base + PHY_SCL_START) & PHY_SCL_START_GO_DONE) |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 452 | cpu_relax(); |
| 453 | |
| 454 | writel(0x00000001, phy_base + PHY_DISABLE_GATING_FOR_SCL); |
| 455 | writel(0x00000010, phy_base + PHY_SCL_MAIN_CLK_DELTA); |
| 456 | writel(0x789b3de0, phy_base + PHY_SCL_DATA_0); |
| 457 | writel(0xf10e4a56, phy_base + PHY_SCL_DATA_1); |
| 458 | writel(0x11000000, phy_base + PHY_SCL_START); |
| 459 | |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 460 | while (readl(phy_base + PHY_SCL_START) & PHY_SCL_START_GO_DONE) |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 461 | cpu_relax(); |
| 462 | |
| 463 | writel(0x34000000, phy_base + PHY_SCL_START); |
| 464 | |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 465 | while (readl(phy_base + PHY_SCL_START) & PHY_SCL_START_GO_DONE) |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 466 | cpu_relax(); |
| 467 | |
| 468 | writel(0x00000003, phy_base + PHY_DISABLE_GATING_FOR_SCL); |
| 469 | |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 470 | writel(0x000261c0, phy_base + PHY_DYNAMIC_WRITE_BIT_LVL); |
| 471 | writel(0x00003270, phy_base + PHY_DYNAMIC_BIT_LVL); |
| 472 | writel(0x011BD0C4, phy_base + PHY_DSCL_CNT); |
| 473 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 474 | /* shift ip_dq trim */ |
| 475 | ddrphy_shift_dq(phy_base, |
| 476 | PHY_IP_DQ_DQS_BITWISE_TRIM, |
| 477 | PHY_IP_DQ_DQS_BITWISE_TRIM_MASK, |
| 478 | PHY_IP_DQ_DQS_BITWISE_TRIM_INC, |
| 479 | PHY_IP_DQ_DQS_BITWISE_TRIM_OVERRIDE, |
| 480 | ddrphy_ip_dq_shift_val[board][ch]); |
| 481 | |
| 482 | /* shift op_dq trim */ |
| 483 | ddrphy_shift_dq(phy_base, |
| 484 | PHY_OP_DQ_DM_DQS_BITWISE_TRIM, |
| 485 | PHY_OP_DQ_DM_DQS_BITWISE_TRIM_MASK, |
| 486 | PHY_OP_DQ_DM_DQS_BITWISE_TRIM_INC, |
| 487 | PHY_OP_DQ_DM_DQS_BITWISE_TRIM_OVERRIDE, |
| 488 | ddrphy_op_dq_shift_val[board][ch]); |
| 489 | |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 490 | return 0; |
| 491 | } |
| 492 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 493 | /* UMC */ |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 494 | static const u32 umc_initctla[DRAM_FREQ_NR] = {0x71016D11}; |
| 495 | static const u32 umc_initctlb[DRAM_FREQ_NR] = {0x07E390AC}; |
| 496 | static const u32 umc_initctlc[DRAM_FREQ_NR] = {0x00FF00FF}; |
| 497 | static const u32 umc_drmmr0[DRAM_FREQ_NR] = {0x00000114}; |
| 498 | static const u32 umc_drmmr2[DRAM_FREQ_NR] = {0x000002a0}; |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 499 | |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 500 | static const u32 umc_memconf0a[DRAM_FREQ_NR][DRAM_SZ_NR] = { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 501 | /* 256MB 512MB */ |
| 502 | {0x00000601, 0x00000801}, /* 1866 MHz */ |
| 503 | }; |
| 504 | |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 505 | static const u32 umc_memconf0b[DRAM_FREQ_NR][DRAM_SZ_NR] = { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 506 | /* 256MB 512MB */ |
| 507 | {0x00000120, 0x00000130}, /* 1866 MHz */ |
| 508 | }; |
| 509 | |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 510 | static const u32 umc_memconfch[DRAM_FREQ_NR][DRAM_SZ_NR] = { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 511 | /* 256MB 512MB */ |
| 512 | {0x00033603, 0x00033803}, /* 1866 MHz */ |
| 513 | }; |
| 514 | |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 515 | static const u32 umc_cmdctla[DRAM_FREQ_NR] = {0x060D0D20}; |
| 516 | static const u32 umc_cmdctlb[DRAM_FREQ_NR] = {0x2D211C08}; |
| 517 | static const u32 umc_cmdctlc[DRAM_FREQ_NR] = {0x00150C04}; |
| 518 | static const u32 umc_cmdctle[DRAM_FREQ_NR][DRAM_SZ_NR] = { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 519 | /* 256MB 512MB */ |
| 520 | {0x0049071D, 0x0078071D}, /* 1866 MHz */ |
| 521 | }; |
| 522 | |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 523 | static const u32 umc_rdatactl[DRAM_FREQ_NR] = {0x00000610}; |
| 524 | static const u32 umc_wdatactl[DRAM_FREQ_NR] = {0x00000204}; |
| 525 | static const u32 umc_odtctl[DRAM_FREQ_NR] = {0x02000002}; |
| 526 | static const u32 umc_dataset[DRAM_FREQ_NR] = {0x04000000}; |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 527 | |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 528 | static const u32 umc_flowctla[DRAM_FREQ_NR] = {0x0081E01E}; |
| 529 | static const u32 umc_directbusctrla[DRAM_CH_NR] = { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 530 | 0x00000000, 0x00000001, 0x00000001 |
| 531 | }; |
| 532 | |
| 533 | static void umc_poll_phy_init_complete(void __iomem *dc_base) |
| 534 | { |
| 535 | /* Wait for PHY Init Complete */ |
| 536 | while (!(readl(dc_base + UMC_DFISTCTLC) & BIT(0))) |
| 537 | cpu_relax(); |
| 538 | } |
| 539 | |
| 540 | static int umc_dc_init(void __iomem *dc_base, unsigned int freq, |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 541 | unsigned long size, int ch) |
| 542 | { |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 543 | enum dram_freq freq_e; |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 544 | enum dram_size size_e; |
| 545 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 546 | switch (freq) { |
| 547 | case 1866: |
| 548 | freq_e = DRAM_FREQ_1866M; |
| 549 | break; |
| 550 | default: |
| 551 | pr_err("unsupported DRAM frequency %ud MHz\n", freq); |
| 552 | return -EINVAL; |
| 553 | } |
| 554 | |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 555 | switch (size) { |
| 556 | case 0: |
| 557 | return 0; |
| 558 | case SZ_256M: |
| 559 | size_e = DRAM_SZ_256M; |
| 560 | break; |
| 561 | case SZ_512M: |
| 562 | size_e = DRAM_SZ_512M; |
| 563 | break; |
| 564 | default: |
| 565 | pr_err("unsupported DRAM size 0x%08lx (per 16bit) for ch%d\n", |
| 566 | size, ch); |
| 567 | return -EINVAL; |
| 568 | } |
| 569 | |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 570 | writel(0x00000001, dc_base + UMC_DFICSOVRRD); |
| 571 | writel(0x00000000, dc_base + UMC_DFITURNOFF); |
| 572 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 573 | writel(umc_initctla[freq_e], dc_base + UMC_INITCTLA); |
| 574 | writel(umc_initctlb[freq_e], dc_base + UMC_INITCTLB); |
| 575 | writel(umc_initctlc[freq_e], dc_base + UMC_INITCTLC); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 576 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 577 | writel(umc_drmmr0[freq_e], dc_base + UMC_DRMMR0); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 578 | writel(0x00000004, dc_base + UMC_DRMMR1); |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 579 | writel(umc_drmmr2[freq_e], dc_base + UMC_DRMMR2); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 580 | writel(0x00000000, dc_base + UMC_DRMMR3); |
| 581 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 582 | writel(umc_memconf0a[freq_e][size_e], dc_base + UMC_MEMCONF0A); |
| 583 | writel(umc_memconf0b[freq_e][size_e], dc_base + UMC_MEMCONF0B); |
| 584 | writel(umc_memconfch[freq_e][size_e], dc_base + UMC_MEMCONFCH); |
Wataru Okoshi | 50fda9d | 2017-01-18 16:24:38 +0900 | [diff] [blame] | 585 | writel(0x00000000, dc_base + UMC_MEMMAPSET); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 586 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 587 | writel(umc_cmdctla[freq_e], dc_base + UMC_CMDCTLA); |
| 588 | writel(umc_cmdctlb[freq_e], dc_base + UMC_CMDCTLB); |
| 589 | writel(umc_cmdctlc[freq_e], dc_base + UMC_CMDCTLC); |
| 590 | writel(umc_cmdctle[freq_e][size_e], dc_base + UMC_CMDCTLE); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 591 | |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 592 | writel(umc_rdatactl[freq_e], dc_base + UMC_RDATACTL_D0); |
| 593 | writel(umc_rdatactl[freq_e], dc_base + UMC_RDATACTL_D1); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 594 | |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 595 | writel(umc_wdatactl[freq_e], dc_base + UMC_WDATACTL_D0); |
| 596 | writel(umc_wdatactl[freq_e], dc_base + UMC_WDATACTL_D1); |
| 597 | writel(umc_odtctl[freq_e], dc_base + UMC_ODTCTL_D0); |
| 598 | writel(umc_odtctl[freq_e], dc_base + UMC_ODTCTL_D1); |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 599 | writel(umc_dataset[freq_e], dc_base + UMC_DATASET); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 600 | |
| 601 | writel(0x00400020, dc_base + UMC_DCCGCTL); |
Masahiro Yamada | fc678cb | 2016-05-24 21:14:00 +0900 | [diff] [blame] | 602 | writel(0x00000003, dc_base + UMC_ACSSETA); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 603 | writel(0x00000103, dc_base + UMC_FLOWCTLG); |
Masahiro Yamada | fc678cb | 2016-05-24 21:14:00 +0900 | [diff] [blame] | 604 | writel(0x00010200, dc_base + UMC_ACSSETB); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 605 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 606 | writel(umc_flowctla[freq_e], dc_base + UMC_FLOWCTLA); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 607 | writel(0x00004444, dc_base + UMC_FLOWCTLC); |
| 608 | writel(0x00000000, dc_base + UMC_DFICUPDCTLA); |
| 609 | |
| 610 | writel(0x00202000, dc_base + UMC_FLOWCTLB); |
| 611 | writel(0x00000000, dc_base + UMC_BSICMAPSET); |
| 612 | writel(0x00000000, dc_base + UMC_ERRMASKA); |
| 613 | writel(0x00000000, dc_base + UMC_ERRMASKB); |
| 614 | |
| 615 | writel(umc_directbusctrla[ch], dc_base + UMC_DIRECTBUSCTRLA); |
| 616 | |
| 617 | writel(0x00000001, dc_base + UMC_INITSET); |
| 618 | /* Wait for PHY Init Complete */ |
| 619 | while (readl(dc_base + UMC_INITSTAT) & BIT(0)) |
| 620 | cpu_relax(); |
| 621 | |
| 622 | writel(0x2A0A0A00, dc_base + UMC_SPCSETB); |
| 623 | writel(0x00000000, dc_base + UMC_DFICSOVRRD); |
| 624 | |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | static int umc_ch_init(void __iomem *umc_ch_base, void __iomem *phy_ch_base, |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 629 | enum dram_board board, unsigned int freq, |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 630 | unsigned long size, int ch) |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 631 | { |
| 632 | void __iomem *dc_base = umc_ch_base + 0x00011000; |
| 633 | void __iomem *phy_base = phy_ch_base; |
| 634 | int ret; |
| 635 | |
| 636 | /* PHY Update Mode (ON) */ |
| 637 | writel(0x8000003f, dc_base + UMC_DFIPUPDCTLA); |
| 638 | |
| 639 | /* deassert PHY reset signals */ |
| 640 | writel(UMC_DIOCTLA_CTL_NRST | UMC_DIOCTLA_CFG_NRST, |
| 641 | dc_base + UMC_DIOCTLA); |
| 642 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 643 | ddrphy_init(phy_base, board, ch); |
| 644 | |
| 645 | umc_poll_phy_init_complete(dc_base); |
| 646 | |
| 647 | ddrphy_init_tail(phy_base, board, freq, ch); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 648 | |
| 649 | ret = umc_dc_init(dc_base, freq, size, ch); |
| 650 | if (ret) |
| 651 | return ret; |
| 652 | |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 653 | ret = ddrphy_training(phy_base, board, ch); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 654 | if (ret) |
| 655 | return ret; |
| 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | static void um_init(void __iomem *um_base) |
| 661 | { |
| 662 | writel(0x000000ff, um_base + UMC_MBUS0); |
| 663 | writel(0x000000ff, um_base + UMC_MBUS1); |
| 664 | writel(0x000000ff, um_base + UMC_MBUS2); |
| 665 | writel(0x00000001, um_base + UMC_MBUS3); |
| 666 | writel(0x00000001, um_base + UMC_MBUS4); |
| 667 | writel(0x00000001, um_base + UMC_MBUS5); |
| 668 | writel(0x00000001, um_base + UMC_MBUS6); |
| 669 | writel(0x00000001, um_base + UMC_MBUS7); |
| 670 | writel(0x00000001, um_base + UMC_MBUS8); |
| 671 | writel(0x00000001, um_base + UMC_MBUS9); |
| 672 | writel(0x00000001, um_base + UMC_MBUS10); |
| 673 | } |
| 674 | |
| 675 | int uniphier_ld20_umc_init(const struct uniphier_board_data *bd) |
| 676 | { |
| 677 | void __iomem *um_base = (void __iomem *)0x5b600000; |
| 678 | void __iomem *umc_ch_base = (void __iomem *)0x5b800000; |
| 679 | void __iomem *phy_ch_base = (void __iomem *)0x6e200000; |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 680 | enum dram_board board; |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 681 | int ch, ret; |
| 682 | |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 683 | switch (UNIPHIER_BD_BOARD_GET_TYPE(bd->flags)) { |
| 684 | case UNIPHIER_BD_BOARD_LD20_REF: |
| 685 | board = DRAM_BOARD_LD20_REF; |
| 686 | break; |
| 687 | case UNIPHIER_BD_BOARD_LD20_GLOBAL: |
| 688 | board = DRAM_BOARD_LD20_GLOBAL; |
| 689 | break; |
Masahiro Yamada | bf4266e | 2016-10-27 23:47:02 +0900 | [diff] [blame] | 690 | case UNIPHIER_BD_BOARD_LD20_C1: |
| 691 | board = DRAM_BOARD_LD20_C1; |
| 692 | break; |
Masahiro Yamada | 40ee94e | 2016-09-17 03:33:12 +0900 | [diff] [blame] | 693 | case UNIPHIER_BD_BOARD_LD21_REF: |
| 694 | board = DRAM_BOARD_LD21_REF; |
| 695 | break; |
| 696 | case UNIPHIER_BD_BOARD_LD21_GLOBAL: |
| 697 | board = DRAM_BOARD_LD21_GLOBAL; |
| 698 | break; |
| 699 | default: |
| 700 | pr_err("unsupported board type %d\n", |
| 701 | UNIPHIER_BD_BOARD_GET_TYPE(bd->flags)); |
| 702 | return -EINVAL; |
| 703 | } |
| 704 | |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 705 | for (ch = 0; ch < bd->dram_nr_ch; ch++) { |
| 706 | unsigned long size = bd->dram_ch[ch].size; |
| 707 | unsigned int width = bd->dram_ch[ch].width; |
| 708 | |
Masahiro Yamada | b1224f3 | 2016-10-08 13:25:24 +0900 | [diff] [blame] | 709 | ret = umc_ch_init(umc_ch_base, phy_ch_base, board, |
| 710 | bd->dram_freq, size / (width / 16), ch); |
Masahiro Yamada | 063eb1e | 2016-04-21 14:43:18 +0900 | [diff] [blame] | 711 | if (ret) { |
| 712 | pr_err("failed to initialize UMC ch%d\n", ch); |
| 713 | return ret; |
| 714 | } |
| 715 | |
| 716 | umc_ch_base += 0x00200000; |
| 717 | phy_ch_base += 0x00004000; |
| 718 | } |
| 719 | |
| 720 | um_init(um_base); |
| 721 | |
| 722 | return 0; |
| 723 | } |