Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 1 | /* |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 2 | * Copyright (C) 2011-2014 Panasonic Corporation |
| 3 | * Copyright (C) 2015-2016 Socionext Inc. |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Masahiro Yamada | 5f36921 | 2015-12-16 10:50:26 +0900 | [diff] [blame] | 9 | #include <linux/err.h> |
Masahiro Yamada | 663a23f | 2015-05-29 17:30:00 +0900 | [diff] [blame] | 10 | #include <linux/io.h> |
Masahiro Yamada | efdf340 | 2016-01-09 01:51:13 +0900 | [diff] [blame] | 11 | |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 12 | #include "ddrphy-init.h" |
Masahiro Yamada | efdf340 | 2016-01-09 01:51:13 +0900 | [diff] [blame] | 13 | #include "ddrphy-regs.h" |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 14 | |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 15 | void ddrphy_prepare_training(void __iomem *phy_base, int rank) |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 16 | { |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 17 | void __iomem *dx_base = phy_base + PHY_DX_BASE; |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 18 | int dx; |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 19 | u32 tmp; |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 20 | |
| 21 | for (dx = 0; dx < NR_DATX8_PER_DDRPHY; dx++) { |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 22 | tmp = readl(dx_base + PHY_DX_GCR); |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 23 | /* Specify the rank that should be write leveled */ |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 24 | tmp &= ~PHY_DX_GCR_WLRKEN_MASK; |
| 25 | tmp |= (1 << (PHY_DX_GCR_WLRKEN_SHIFT + rank)) & |
| 26 | PHY_DX_GCR_WLRKEN_MASK; |
| 27 | writel(tmp, dx_base + PHY_DX_GCR); |
| 28 | dx_base += PHY_DX_STRIDE; |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 29 | } |
| 30 | |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 31 | tmp = readl(phy_base + PHY_DTCR); |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 32 | /* Specify the rank used during data bit deskew and eye centering */ |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 33 | tmp &= ~PHY_DTCR_DTRANK_MASK; |
| 34 | tmp |= (rank << PHY_DTCR_DTRANK_SHIFT) & PHY_DTCR_DTRANK_MASK; |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 35 | /* Use Multi-Purpose Register for DQS gate training */ |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 36 | tmp |= PHY_DTCR_DTMPR; |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 37 | /* Specify the rank enabled for data-training */ |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 38 | tmp &= ~PHY_DTCR_RANKEN_MASK; |
| 39 | tmp |= (1 << (PHY_DTCR_RANKEN_SHIFT + rank)) & PHY_DTCR_RANKEN_MASK; |
| 40 | writel(tmp, phy_base + PHY_DTCR); |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | struct ddrphy_init_sequence { |
| 44 | char *description; |
| 45 | u32 init_flag; |
| 46 | u32 done_flag; |
| 47 | u32 err_flag; |
| 48 | }; |
| 49 | |
Masahiro Yamada | be5b847 | 2015-12-16 10:36:13 +0900 | [diff] [blame] | 50 | static const struct ddrphy_init_sequence init_sequence[] = { |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 51 | { |
| 52 | "DRAM Initialization", |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 53 | PHY_PIR_DRAMRST | PHY_PIR_DRAMINIT, |
| 54 | PHY_PGSR0_DIDONE, |
| 55 | PHY_PGSR0_DIERR |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 56 | }, |
| 57 | { |
| 58 | "Write Leveling", |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 59 | PHY_PIR_WL, |
| 60 | PHY_PGSR0_WLDONE, |
| 61 | PHY_PGSR0_WLERR |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 62 | }, |
| 63 | { |
| 64 | "Read DQS Gate Training", |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 65 | PHY_PIR_QSGATE, |
| 66 | PHY_PGSR0_QSGDONE, |
| 67 | PHY_PGSR0_QSGERR |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 68 | }, |
| 69 | { |
| 70 | "Write Leveling Adjustment", |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 71 | PHY_PIR_WLADJ, |
| 72 | PHY_PGSR0_WLADONE, |
| 73 | PHY_PGSR0_WLAERR |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 74 | }, |
| 75 | { |
| 76 | "Read Bit Deskew", |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 77 | PHY_PIR_RDDSKW, |
| 78 | PHY_PGSR0_RDDONE, |
| 79 | PHY_PGSR0_RDERR |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 80 | }, |
| 81 | { |
| 82 | "Write Bit Deskew", |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 83 | PHY_PIR_WRDSKW, |
| 84 | PHY_PGSR0_WDDONE, |
| 85 | PHY_PGSR0_WDERR |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 86 | }, |
| 87 | { |
| 88 | "Read Eye Training", |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 89 | PHY_PIR_RDEYE, |
| 90 | PHY_PGSR0_REDONE, |
| 91 | PHY_PGSR0_REERR |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 92 | }, |
| 93 | { |
| 94 | "Write Eye Training", |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 95 | PHY_PIR_WREYE, |
| 96 | PHY_PGSR0_WEDONE, |
| 97 | PHY_PGSR0_WEERR |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 98 | } |
| 99 | }; |
| 100 | |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 101 | int ddrphy_training(void __iomem *phy_base) |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 102 | { |
| 103 | int i; |
| 104 | u32 pgsr0; |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 105 | u32 init_flag = PHY_PIR_INIT; |
| 106 | u32 done_flag = PHY_PGSR0_IDONE; |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 107 | int timeout = 50000; /* 50 msec is long enough */ |
| 108 | #ifdef DISPLAY_ELAPSED_TIME |
| 109 | ulong start = get_timer(0); |
| 110 | #endif |
| 111 | |
| 112 | for (i = 0; i < ARRAY_SIZE(init_sequence); i++) { |
| 113 | init_flag |= init_sequence[i].init_flag; |
| 114 | done_flag |= init_sequence[i].done_flag; |
| 115 | } |
| 116 | |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 117 | writel(init_flag, phy_base + PHY_PIR); |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 118 | |
| 119 | do { |
| 120 | if (--timeout < 0) { |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 121 | printf("%s: error: timeout during DDR training\n", |
| 122 | __func__); |
Masahiro Yamada | 5f36921 | 2015-12-16 10:50:26 +0900 | [diff] [blame] | 123 | return -ETIMEDOUT; |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 124 | } |
| 125 | udelay(1); |
Masahiro Yamada | b464ff9 | 2016-10-27 23:47:07 +0900 | [diff] [blame^] | 126 | pgsr0 = readl(phy_base + PHY_PGSR0); |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 127 | } while ((pgsr0 & done_flag) != done_flag); |
| 128 | |
| 129 | for (i = 0; i < ARRAY_SIZE(init_sequence); i++) { |
| 130 | if (pgsr0 & init_sequence[i].err_flag) { |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 131 | printf("%s: error: %s failed\n", __func__, |
| 132 | init_sequence[i].description); |
Masahiro Yamada | 5f36921 | 2015-12-16 10:50:26 +0900 | [diff] [blame] | 133 | return -EIO; |
Masahiro Yamada | 04191e5 | 2014-12-19 20:20:52 +0900 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | #ifdef DISPLAY_ELAPSED_TIME |
| 138 | printf("%s: info: elapsed time %ld msec\n", get_timer(start)); |
| 139 | #endif |
| 140 | |
| 141 | return 0; |
| 142 | } |