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