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