Andre Przywara | 1c7a751 | 2019-07-15 02:27:06 +0100 | [diff] [blame] | 1 | /* |
| 2 | * sun50i H6 LPDDR3 timings |
| 3 | * |
| 4 | * (C) Copyright 2017 Icenowy Zheng <icenowy@aosc.io> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
Andre Przywara | 1c7a751 | 2019-07-15 02:27:06 +0100 | [diff] [blame] | 9 | #include <asm/arch/dram.h> |
| 10 | #include <asm/arch/cpu.h> |
| 11 | |
| 12 | static u32 mr_lpddr3[12] = { |
| 13 | 0x00000000, 0x00000043, 0x0000001a, 0x00000001, |
| 14 | 0x00000000, 0x00000000, 0x00000048, 0x00000000, |
| 15 | 0x00000000, 0x00000000, 0x00000000, 0x00000003, |
| 16 | }; |
| 17 | |
| 18 | /* TODO: flexible timing */ |
| 19 | void mctl_set_timing_params(struct dram_para *para) |
| 20 | { |
| 21 | struct sunxi_mctl_ctl_reg * const mctl_ctl = |
| 22 | (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE; |
| 23 | struct sunxi_mctl_phy_reg * const mctl_phy = |
| 24 | (struct sunxi_mctl_phy_reg *)SUNXI_DRAM_PHY0_BASE; |
| 25 | int i; |
| 26 | |
| 27 | u8 tccd = 2; |
| 28 | u8 tfaw = max(ns_to_t(50), 4); |
| 29 | u8 trrd = max(ns_to_t(10), 2); |
| 30 | u8 trcd = max(ns_to_t(24), 2); |
| 31 | u8 trc = ns_to_t(70); |
| 32 | u8 txp = max(ns_to_t(8), 2); |
| 33 | u8 twtr = max(ns_to_t(8), 2); |
| 34 | u8 trtp = max(ns_to_t(8), 2); |
| 35 | u8 twr = max(ns_to_t(15), 2); |
| 36 | u8 trp = ns_to_t(18); |
| 37 | u8 tras = ns_to_t(42); |
| 38 | u8 twtr_sa = ns_to_t(5); |
| 39 | u8 tcksrea = ns_to_t(11); |
| 40 | u16 trefi = ns_to_t(3900) / 32; |
| 41 | u16 trfc = ns_to_t(210); |
| 42 | u16 txsr = ns_to_t(220); |
| 43 | |
| 44 | if (CONFIG_DRAM_CLK % 400 == 0) { |
| 45 | /* Round up these parameters */ |
| 46 | twtr_sa++; |
| 47 | tcksrea++; |
| 48 | } |
| 49 | |
| 50 | u8 tmrw = 5; |
| 51 | u8 tmrd = 5; |
| 52 | u8 tmod = 12; |
| 53 | u8 tcke = 3; |
| 54 | u8 tcksrx = 5; |
| 55 | u8 tcksre = 5; |
| 56 | u8 tckesr = 5; |
| 57 | u8 trasmax = CONFIG_DRAM_CLK / 60; |
| 58 | u8 txs = 4; |
| 59 | u8 txsdll = 4; |
| 60 | u8 txsabort = 4; |
| 61 | u8 txsfast = 4; |
| 62 | |
| 63 | u8 tcl = 5; /* CL 10 */ |
| 64 | u8 tcwl = 3; /* CWL 6 */ |
| 65 | u8 t_rdata_en = twtr_sa + 8; |
| 66 | |
| 67 | u32 tdinit0 = (200 * CONFIG_DRAM_CLK) + 1; /* 200us */ |
| 68 | u32 tdinit1 = (100 * CONFIG_DRAM_CLK) / 1000 + 1; /* 100ns */ |
| 69 | u32 tdinit2 = (11 * CONFIG_DRAM_CLK) + 1; /* 11us */ |
| 70 | u32 tdinit3 = (1 * CONFIG_DRAM_CLK) + 1; /* 1us */ |
| 71 | |
| 72 | u8 twtp = tcwl + 4 + twr + 1; |
| 73 | /* |
| 74 | * The code below for twr2rd and trd2wr follows the IP core's |
| 75 | * document from ZynqMP and i.MX7. The BSP has both number |
| 76 | * substracted by 2. |
| 77 | */ |
| 78 | u8 twr2rd = tcwl + 4 + 1 + twtr; |
| 79 | u8 trd2wr = tcl + 4 + (tcksrea >> 1) - tcwl + 1; |
| 80 | |
| 81 | /* set mode registers */ |
| 82 | for (i = 0; i < ARRAY_SIZE(mr_lpddr3); i++) |
| 83 | writel(mr_lpddr3[i], &mctl_phy->mr[i]); |
| 84 | |
| 85 | /* set DRAM timing */ |
| 86 | writel((twtp << 24) | (tfaw << 16) | (trasmax << 8) | tras, |
| 87 | &mctl_ctl->dramtmg[0]); |
| 88 | writel((txp << 16) | (trtp << 8) | trc, &mctl_ctl->dramtmg[1]); |
| 89 | writel((tcwl << 24) | (tcl << 16) | (trd2wr << 8) | twr2rd, |
| 90 | &mctl_ctl->dramtmg[2]); |
| 91 | writel((tmrw << 20) | (tmrd << 12) | tmod, &mctl_ctl->dramtmg[3]); |
| 92 | writel((trcd << 24) | (tccd << 16) | (trrd << 8) | trp, |
| 93 | &mctl_ctl->dramtmg[4]); |
| 94 | writel((tcksrx << 24) | (tcksre << 16) | (tckesr << 8) | tcke, |
| 95 | &mctl_ctl->dramtmg[5]); |
| 96 | /* Value suggested by ZynqMP manual and used by libdram */ |
| 97 | writel((txp + 2) | 0x02020000, &mctl_ctl->dramtmg[6]); |
| 98 | writel((txsfast << 24) | (txsabort << 16) | (txsdll << 8) | txs, |
| 99 | &mctl_ctl->dramtmg[8]); |
| 100 | writel(txsr, &mctl_ctl->dramtmg[14]); |
| 101 | |
| 102 | clrsetbits_le32(&mctl_ctl->init[0], (3 << 30), (1 << 30)); |
| 103 | writel(0, &mctl_ctl->dfimisc); |
| 104 | clrsetbits_le32(&mctl_ctl->rankctl, 0xff0, 0x660); |
| 105 | |
| 106 | /* |
| 107 | * Set timing registers of the PHY. |
| 108 | * Note: the PHY is clocked 2x from the DRAM frequency. |
| 109 | */ |
| 110 | writel((trrd << 25) | (tras << 17) | (trp << 9) | (trtp << 1), |
| 111 | &mctl_phy->dtpr[0]); |
| 112 | writel((tfaw << 17) | 0x28000400 | (tmrd << 1), &mctl_phy->dtpr[1]); |
| 113 | writel(((txs << 6) - 1) | (tcke << 17), &mctl_phy->dtpr[2]); |
| 114 | writel(((txsdll << 22) - (0x1 << 16)) | twtr_sa | (tcksrea << 8), |
| 115 | &mctl_phy->dtpr[3]); |
| 116 | writel((txp << 1) | (trfc << 17) | 0x800, &mctl_phy->dtpr[4]); |
| 117 | writel((trc << 17) | (trcd << 9) | (twtr << 1), &mctl_phy->dtpr[5]); |
| 118 | writel(0x0505, &mctl_phy->dtpr[6]); |
| 119 | |
| 120 | /* Configure DFI timing */ |
| 121 | writel(tcl | 0x2000200 | (t_rdata_en << 16) | 0x808000, |
| 122 | &mctl_ctl->dfitmg0); |
| 123 | writel(0x040201, &mctl_ctl->dfitmg1); |
| 124 | |
| 125 | /* Configure PHY timing */ |
| 126 | writel(tdinit0 | (tdinit1 << 20), &mctl_phy->ptr[3]); |
| 127 | writel(tdinit2 | (tdinit3 << 18), &mctl_phy->ptr[4]); |
| 128 | |
| 129 | /* set refresh timing */ |
| 130 | writel((trefi << 16) | trfc, &mctl_ctl->rfshtmg); |
| 131 | } |