Advertisement
Guest User

Untitled

a guest
Mar 11th, 2025
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 37.50 KB | None | 0 0
  1. /* Originally checked out from: https://github.com/smaeul/u-boot/commits/d1-wip */
  2. /* Modified using: https://github.com/YuzukiHD/SyterKit/blob/main/src/drivers/chips/sun20iw1/sys-dram.c#L1066 */
  3.  
  4. // SPDX-License-Identifier: GPL-2.0+
  5. /*
  6.  * Allwinner D1/D1s/R528/T113-sx DRAM initialisation
  7.  *
  8.  * As usual there is no documentation for the memory controller or PHY IP
  9.  * used here. The baseline of this code was lifted from awboot[1], which
  10.  * seems to be based on some form of de-compilation of some original Allwinner
  11.  * code bits (with a GPL2 license tag from the very beginning).
  12.  * This version here is a reworked version, to match the U-Boot coding style
  13.  * and style of the other Allwinner DRAM drivers.
  14.  *
  15.  * [1] https://github.com/szemzoa/awboot.git
  16.  */
  17.  
  18. #define CONFIG_DRAM_CLK 528
  19.  
  20. #define CONFIG_DRAM_ZQ 0x007b7bf9
  21.  
  22. #define readl rv_readl
  23. #define writel rv_writel
  24. #include <asm/io.h>
  25. #undef readl
  26. #undef writel
  27.  
  28. #define readl(x)       rv_readl((const volatile void __iomem *)(u64)(x))
  29. #define writel(v, x)   rv_writel(v, (volatile void __iomem *)(u64)(x))
  30.  
  31. #include <common.h>
  32. #ifdef CONFIG_RAM
  33.   #include <dm.h>
  34.   #include <ram.h>
  35. #endif
  36. #include <linux/delay.h>
  37.  
  38. #include "dram_sun20i_d1.h"
  39.  
  40. #ifndef SUNXI_SID_BASE
  41. #define SUNXI_SID_BASE  0x3006200
  42. #endif
  43.  
  44. #ifndef SUNXI_CCM_BASE
  45. #define SUNXI_CCM_BASE  0x2001000
  46. #endif
  47.  
  48. #ifdef debug
  49. #undef debug
  50. #endif
  51. #define debug(...) printf(__VA_ARGS__)
  52.  
  53. static void sid_read_ldoB_cal(const dram_para_t *para)
  54. {
  55.     uint32_t reg;
  56.  
  57.     reg = (readl(SUNXI_SID_BASE + 0x1c) & 0xff00) >> 8;
  58.  
  59.     if (reg == 0)
  60.         return;
  61.  
  62.     switch (para->dram_type) {
  63.     case SUNXI_DRAM_TYPE_DDR2:
  64.         break;
  65.     case SUNXI_DRAM_TYPE_DDR3:
  66.         if (reg > 0x20)
  67.             reg -= 0x16;
  68.         break;
  69.     default:
  70.         reg = 0;
  71.         break;
  72.     }
  73.  
  74.     clrsetbits_le32(0x3000150, 0xff00, reg << 8);
  75. }
  76.  
  77. static void dram_voltage_set(const dram_para_t *para)
  78. {
  79.     int vol;
  80.  
  81.     switch (para->dram_type) {
  82.     case SUNXI_DRAM_TYPE_DDR2:
  83.         vol = 47;
  84.         break;
  85.     case SUNXI_DRAM_TYPE_DDR3:
  86.         vol = 25;
  87.         break;
  88.     default:
  89.         vol = 0;
  90.         break;
  91.     }
  92.  
  93.     clrsetbits_le32(0x3000150, 0x20ff00, vol << 8);
  94.  
  95.     udelay(1);
  96.  
  97.     sid_read_ldoB_cal(para);
  98. }
  99.  
  100. static void dram_enable_all_master(void)
  101. {
  102.     writel(~0, 0x3102020);
  103.     writel(0xff, 0x3102024);
  104.     writel(0xffff, 0x3102028);
  105.     udelay(10);
  106. }
  107.  
  108. static void dram_disable_all_master(void)
  109. {
  110.     writel(1, 0x3102020);
  111.     writel(0, 0x3102024);
  112.     writel(0, 0x3102028);
  113.     udelay(10);
  114. }
  115.  
  116. static void eye_delay_compensation(const dram_para_t *para)
  117. {
  118.     uint32_t delay;
  119.     unsigned long ptr;
  120.  
  121.     // DATn0IOCR, n =  0...7
  122.     delay = (para->dram_tpr11 & 0xf) << 9;
  123.     delay |= (para->dram_tpr12 & 0xf) << 1;
  124.     for (ptr = 0x3103310; ptr < 0x3103334; ptr += 4)
  125.         setbits_le32(ptr, delay);
  126.  
  127.     // DATn1IOCR, n =  0...7
  128.     delay = (para->dram_tpr11 & 0xf0) << 5;
  129.     delay |= (para->dram_tpr12 & 0xf0) >> 3;
  130.     for (ptr = 0x3103390; ptr != 0x31033b4; ptr += 4)
  131.         setbits_le32(ptr, delay);
  132.  
  133.     // PGCR0: assert AC loopback FIFO reset
  134.     clrbits_le32(0x3103100, 0x04000000);
  135.  
  136.     // ??
  137.  
  138.     delay = (para->dram_tpr11 & 0xf0000) >> 7;
  139.     delay |= (para->dram_tpr12 & 0xf0000) >> 15;
  140.     setbits_le32(0x3103334, delay);
  141.     setbits_le32(0x3103338, delay);
  142.  
  143.     delay = (para->dram_tpr11 & 0xf00000) >> 11;
  144.     delay |= (para->dram_tpr12 & 0xf00000) >> 19;
  145.     setbits_le32(0x31033b4, delay);
  146.     setbits_le32(0x31033b8, delay);
  147.  
  148.     setbits_le32(0x310333c, (para->dram_tpr11 & 0xf0000) << 9);
  149.     setbits_le32(0x31033bc, (para->dram_tpr11 & 0xf00000) << 5);
  150.  
  151.     // PGCR0: release AC loopback FIFO reset
  152.     setbits_le32(0x3103100, BIT(26));
  153.  
  154.     udelay(1);
  155.  
  156.     delay = (para->dram_tpr10 & 0xf0) << 4;
  157.     for (ptr = 0x3103240; ptr != 0x310327c; ptr += 4)
  158.         setbits_le32(ptr, delay);
  159.     for (ptr = 0x3103228; ptr != 0x3103240; ptr += 4)
  160.         setbits_le32(ptr, delay);
  161.  
  162.     setbits_le32(0x3103218, (para->dram_tpr10 & 0x0f) << 8);
  163.     setbits_le32(0x310321c, (para->dram_tpr10 & 0x0f) << 8);
  164.  
  165.     setbits_le32(0x3103280, (para->dram_tpr10 & 0xf00) >> 4);
  166. }
  167.  
  168. /*
  169.  * Main purpose of the auto_set_timing routine seems to be to calculate all
  170.  * timing settings for the specific type of sdram used. Read together with
  171.  * an sdram datasheet for context on the various variables.
  172.  */
  173. static void mctl_set_timing_params(const dram_para_t *para,
  174.                    const dram_config_t *config)
  175. {
  176.     /* DRAM_TPR0 */
  177.     u8 tccd     = 2;
  178.     u8 tfaw;
  179.     u8 trrd;
  180.     u8 trcd;
  181.     u8 trc;
  182.  
  183.     /* DRAM_TPR1 */
  184.     u8 txp;
  185.     u8 twtr;
  186.     u8 trtp     = 4;
  187.     u8 twr;
  188.     u8 trp;
  189.     u8 tras;
  190.  
  191.     /* DRAM_TPR2 */
  192.     u16 trefi;
  193.     u16 trfc;
  194.  
  195.     u8 tcksrx;
  196.     u8 tckesr;
  197.     u8 trd2wr;
  198.     u8 twr2rd;
  199.     u8 trasmax;
  200.     u8 twtp;
  201.     u8 tcke;
  202.     u8 tmod;
  203.     u8 tmrd;
  204.     u8 tmrw;
  205.  
  206.     u8 tcl;
  207.     u8 tcwl;
  208.     u8 t_rdata_en;
  209.     u8 wr_latency;
  210.  
  211.     u32 mr0;
  212.     u32 mr1;
  213.     u32 mr2;
  214.     u32 mr3;
  215.  
  216.     u32 tdinit0;
  217.     u32 tdinit1;
  218.     u32 tdinit2;
  219.     u32 tdinit3;
  220.  
  221.     switch (para->dram_type) {
  222.     case SUNXI_DRAM_TYPE_DDR2:
  223.         /* DRAM_TPR0 */
  224.         tfaw        = ns_to_t(50);
  225.         trrd        = ns_to_t(10);
  226.         trcd        = ns_to_t(20);
  227.         trc     = ns_to_t(65);
  228.  
  229.         /* DRAM_TPR1 */
  230.         txp     = 2;
  231.         twtr        = ns_to_t(8);
  232.         twr     = ns_to_t(15);
  233.         trp     = ns_to_t(15);
  234.         tras        = ns_to_t(45);
  235.  
  236.         /* DRAM_TRP2 */
  237.         trfc        = ns_to_t(328);
  238.         trefi       = ns_to_t(7800) / 32;
  239.  
  240.         trasmax     = CONFIG_DRAM_CLK / 30;
  241.         if (CONFIG_DRAM_CLK < 409) {
  242.             t_rdata_en  = 1;
  243.             tcl     = 3;
  244.             mr0     = 0x06a3;
  245.         } else {
  246.             t_rdata_en  = 2;
  247.             tcl     = 4;
  248.             mr0     = 0x0e73;
  249.         }
  250.         tmrd        = 2;
  251.         twtp        = twr + 5;
  252.         tcksrx      = 5;
  253.         tckesr      = 4;
  254.         trd2wr      = 4;
  255.         tcke        = 3;
  256.         tmod        = 12;
  257.         wr_latency  = 1;
  258.         tmrw        = 0;
  259.         twr2rd      = twtr + 5;
  260.         tcwl        = 0;
  261.  
  262.         mr1     = para->dram_mr1;
  263.         mr2     = 0;
  264.         mr3     = 0;
  265.  
  266.         tdinit0     = 200 * CONFIG_DRAM_CLK + 1;
  267.         tdinit1     = 100 * CONFIG_DRAM_CLK / 1000 + 1;
  268.         tdinit2     = 200 * CONFIG_DRAM_CLK + 1;
  269.         tdinit3     = 1 * CONFIG_DRAM_CLK + 1;
  270.  
  271.         break;
  272.     case SUNXI_DRAM_TYPE_DDR3:
  273.         trfc        = ns_to_t(350);
  274.         trefi       = ns_to_t(7800) / 32 + 1;   // XXX
  275.  
  276.         twtr        = ns_to_t(8) + 2;       // + 2 ? XXX
  277.         /* Only used by trd2wr calculation, which gets discard below */
  278. //      twr     = max(ns_to_t(15), 2);
  279.         trrd        = max(ns_to_t(10), 2);
  280.         txp     = max(ns_to_t(10), 2);
  281.  
  282.         if (CONFIG_DRAM_CLK <= 800) {
  283.             tfaw        = ns_to_t(50);
  284.             trcd        = ns_to_t(15);
  285.             trp     = ns_to_t(15);
  286.             trc     = ns_to_t(53);
  287.             tras        = ns_to_t(38);
  288.  
  289.             mr0     = 0x1c70;
  290.             mr2     = 0x18;
  291.             tcl     = 6;
  292.             wr_latency  = 2;
  293.             tcwl        = 4;
  294.             t_rdata_en  = 4;
  295.         } else {
  296.             tfaw        = ns_to_t(35);
  297.             trcd        = ns_to_t(14);
  298.             trp     = ns_to_t(14);
  299.             trc     = ns_to_t(48);
  300.             tras        = ns_to_t(34);
  301.  
  302.             mr0     = 0x1e14;
  303.             mr2     = 0x20;
  304.             tcl     = 7;
  305.             wr_latency  = 3;
  306.             tcwl        = 5;
  307.             t_rdata_en  = 5;
  308.         }
  309.  
  310.         trasmax     = CONFIG_DRAM_CLK / 30;
  311.         twtp        = tcwl + 2 + twtr;      // WL+BL/2+tWTR
  312.         /* Gets overwritten below */
  313. //      trd2wr      = tcwl + 2 + twr;       // WL+BL/2+tWR
  314.         twr2rd      = tcwl + twtr;          // WL+tWTR
  315.  
  316.         tdinit0     = 500 * CONFIG_DRAM_CLK + 1;    // 500 us
  317.         tdinit1     = 360 * CONFIG_DRAM_CLK / 1000 + 1;   // 360 ns
  318.         tdinit2     = 200 * CONFIG_DRAM_CLK + 1;    // 200 us
  319.         tdinit3     = 1 * CONFIG_DRAM_CLK + 1;  //   1 us
  320.  
  321.         mr1     = para->dram_mr1;
  322.         mr3     = 0;
  323.         tcke        = 3;
  324.         tcksrx      = 5;
  325.         tckesr      = 4;
  326.         if (((config->dram_tpr13 & 0xc) == 0x04) || CONFIG_DRAM_CLK < 912)
  327.             trd2wr     = 5;
  328.         else
  329.             trd2wr     = 6;
  330.  
  331.         tmod        = 12;
  332.         tmrd        = 4;
  333.         tmrw        = 0;
  334.  
  335.         break;
  336.     case SUNXI_DRAM_TYPE_LPDDR2:
  337.         tfaw        = max(ns_to_t(50), 4);
  338.         trrd        = max(ns_to_t(10), 1);
  339.         trcd        = max(ns_to_t(24), 2);
  340.         trc     = ns_to_t(70);
  341.         txp     = ns_to_t(8);
  342.         if (txp < 2) {
  343.             txp++;
  344.             twtr    = 2;
  345.         } else {
  346.             twtr    = txp;
  347.         }
  348.         twr     = max(ns_to_t(15), 2);
  349.         trp     = ns_to_t(17);
  350.         tras        = ns_to_t(42);
  351.         trefi       = ns_to_t(3900) / 32;
  352.         trfc        = ns_to_t(210);
  353.  
  354.         trasmax     = CONFIG_DRAM_CLK / 60;
  355.         mr3     = para->dram_mr3;
  356.         twtp        = twr + 5;
  357.         mr2     = 6;
  358.         mr1     = 5;
  359.         tcksrx      = 5;
  360.         tckesr      = 5;
  361.         trd2wr      = 10;
  362.         tcke        = 2;
  363.         tmod        = 5;
  364.         tmrd        = 5;
  365.         tmrw        = 3;
  366.         tcl     = 4;
  367.         wr_latency  = 1;
  368.         t_rdata_en  = 1;
  369.  
  370.         tdinit0     = 200 * CONFIG_DRAM_CLK + 1;
  371.         tdinit1     = 100 * CONFIG_DRAM_CLK / 1000 + 1;
  372.         tdinit2     = 11 * CONFIG_DRAM_CLK + 1;
  373.         tdinit3     = 1 * CONFIG_DRAM_CLK + 1;
  374.         twr2rd      = twtr + 5;
  375.         tcwl        = 2;
  376.         mr1     = 195;
  377.         mr0     = 0;
  378.  
  379.         break;
  380.     case SUNXI_DRAM_TYPE_LPDDR3:
  381.         tfaw        = max(ns_to_t(50), 4);
  382.         trrd        = max(ns_to_t(10), 1);
  383.         trcd        = max(ns_to_t(24), 2);
  384.         trc     = ns_to_t(70);
  385.         twtr        = max(ns_to_t(8), 2);
  386.         twr     = max(ns_to_t(15), 2);
  387.         trp     = ns_to_t(17);
  388.         tras        = ns_to_t(42);
  389.         trefi       = ns_to_t(3900) / 32;
  390.         trfc        = ns_to_t(210);
  391.         txp     = twtr;
  392.  
  393.         trasmax     = CONFIG_DRAM_CLK / 60;
  394.         if (CONFIG_DRAM_CLK < 800) {
  395.             tcwl       = 4;
  396.             wr_latency = 3;
  397.             t_rdata_en = 6;
  398.             mr2        = 12;
  399.         } else {
  400.             tcwl       = 3;
  401.             tcke       = 6;
  402.             wr_latency = 2;
  403.             t_rdata_en = 5;
  404.             mr2        = 10;
  405.         }
  406.         twtp        = tcwl + 5;
  407.         tcl     = 7;
  408.         mr3     = para->dram_mr3;
  409.         tcksrx      = 5;
  410.         tckesr      = 5;
  411.         trd2wr      = 13;
  412.         tcke        = 3;
  413.         tmod        = 12;
  414.         tdinit0     = 400 * CONFIG_DRAM_CLK + 1;
  415.         tdinit1     = 500 * CONFIG_DRAM_CLK / 1000 + 1;
  416.         tdinit2     = 11 * CONFIG_DRAM_CLK + 1;
  417.         tdinit3     = 1 * CONFIG_DRAM_CLK + 1;
  418.         tmrd        = 5;
  419.         tmrw        = 5;
  420.         twr2rd      = tcwl + twtr + 5;
  421.         mr1     = 195;
  422.         mr0     = 0;
  423.  
  424.         break;
  425.     default:
  426.         trfc        = 128;
  427.         trp     = 6;
  428.         trefi       = 98;
  429.         txp     = 10;
  430.         twr     = 8;
  431.         twtr        = 3;
  432.         tras        = 14;
  433.         tfaw        = 16;
  434.         trc     = 20;
  435.         trcd        = 6;
  436.         trrd        = 3;
  437.  
  438.         twr2rd      = 8;
  439.         tcksrx      = 4;
  440.         tckesr      = 3;
  441.         trd2wr      = 4;
  442.         trasmax     = 27;
  443.         twtp        = 12;
  444.         tcke        = 2;
  445.         tmod        = 6;
  446.         tmrd        = 2;
  447.         tmrw        = 0;
  448.         tcwl        = 3;
  449.         tcl     = 3;
  450.         wr_latency  = 1;
  451.         t_rdata_en  = 1;
  452.         mr3     = 0;
  453.         mr2     = 0;
  454.         mr1     = 0;
  455.         mr0     = 0;
  456.         tdinit3     = 0;
  457.         tdinit2     = 0;
  458.         tdinit1     = 0;
  459.         tdinit0     = 0;
  460.  
  461.         break;
  462.     }
  463.  
  464.     /* Set mode registers */
  465.     writel(mr0, 0x3103030);
  466.     writel(mr1, 0x3103034);
  467.     writel(mr2, 0x3103038);
  468.     writel(mr3, 0x310303c);
  469.     /* TODO: dram_odt_en is either 0x0 or 0x1, so right shift looks weird */
  470.     writel((para->dram_odt_en >> 4) & 0x3, 0x310302c);
  471.  
  472.     /* Set dram timing DRAMTMG0 - DRAMTMG5 */
  473.     writel((twtp << 24) | (tfaw << 16) | (trasmax << 8) | (tras << 0),
  474.         0x3103058);
  475.     writel((txp << 16) | (trtp << 8) | (trc << 0),
  476.         0x310305c);
  477.     writel((tcwl << 24) | (tcl << 16) | (trd2wr << 8) | (twr2rd << 0),
  478.         0x3103060);
  479.     writel((tmrw << 16) | (tmrd << 12) | (tmod << 0),
  480.         0x3103064);
  481.     writel((trcd << 24) | (tccd << 16) | (trrd << 8) | (trp << 0),
  482.         0x3103068);
  483.     writel((tcksrx << 24) | (tcksrx << 16) | (tckesr << 8) | (tcke << 0),
  484.         0x310306c);
  485.  
  486.     /* Set dual rank timing */
  487.     clrsetbits_le32(0x3103078, 0xf000ffff,
  488.             (CONFIG_DRAM_CLK < 800) ? 0xf0006610 : 0xf0007610);
  489.  
  490.     /* Set phy interface time PITMG0, PTR3, PTR4 */
  491.     writel((0x2 << 24) | (t_rdata_en << 16) | BIT(8) | (wr_latency << 0),
  492.         0x3103080);
  493.     writel(((tdinit0 << 0) | (tdinit1 << 20)), 0x3103050);
  494.     writel(((tdinit2 << 0) | (tdinit3 << 20)), 0x3103054);
  495.  
  496.     /* Set refresh timing and mode */
  497.     writel((trefi << 16) | (trfc << 0), 0x3103090);
  498.     writel((trefi << 15) & 0x0fff0000, 0x3103094);
  499. }
  500.  
  501. // Purpose of this routine seems to be to initialize the PLL driving
  502. // the MBUS and sdram.
  503. //
  504. static int ccu_set_pll_ddr_clk(int index, const dram_para_t *para,
  505.                    const dram_config_t *config)
  506. {
  507.     unsigned int val, clk, n;
  508.  
  509.     if (config->dram_tpr13 & BIT(6))
  510.         clk = para->dram_tpr9;
  511.     else
  512.         clk = para->dram_clk;
  513.  
  514.     // set VCO clock divider
  515.     n = (clk * 2) / 24;
  516.  
  517.     val = readl(SUNXI_CCM_BASE + 0x10);
  518.     val &= ~0x0007ff03;         // clear dividers
  519.     val |= (n - 1) << 8;            // set PLL division
  520.     val |= BIT(31) | BIT(30);       // enable PLL and LDO
  521.     writel(val | BIT(29), SUNXI_CCM_BASE + 0x10);
  522.  
  523.     // wait for PLL to lock
  524.     while ((readl(SUNXI_CCM_BASE + 0x10) & BIT(28)) == 0)
  525.         ;
  526.  
  527.     udelay(20);
  528.  
  529.     // enable PLL output
  530.     setbits_le32(SUNXI_CCM_BASE + 0x0, BIT(27));
  531.  
  532.     // turn clock gate on
  533.     val = readl(SUNXI_CCM_BASE + 0x800);
  534.     val &= ~0x03000303;     // select DDR clk source, n=1, m=1
  535.     val |= BIT(31);         // turn clock on
  536.     writel(val, SUNXI_CCM_BASE + 0x800);
  537.  
  538.     return n * 24;
  539. }
  540.  
  541. /* Set up the PLL and clock gates for the DRAM controller and MBUS clocks. */
  542. static void mctl_sys_init(const dram_para_t *para, const dram_config_t *config)
  543. {
  544.     // assert MBUS reset
  545.     clrbits_le32(SUNXI_CCM_BASE + 0x540, BIT(30));
  546.  
  547.     // turn off sdram clock gate, assert sdram reset
  548.     clrbits_le32(SUNXI_CCM_BASE + 0x80c, 0x10001);
  549.     clrsetbits_le32(SUNXI_CCM_BASE + 0x800, BIT(31) | BIT(30), BIT(27));
  550.     udelay(10);
  551.  
  552.     // set ddr pll clock
  553.     ccu_set_pll_ddr_clk(0, para, config);
  554.     udelay(100);
  555.     dram_disable_all_master();
  556.  
  557.     // release sdram reset
  558.     setbits_le32(SUNXI_CCM_BASE + 0x80c, BIT(16));
  559.  
  560.     // release MBUS reset
  561.     setbits_le32(SUNXI_CCM_BASE + 0x540, BIT(30));
  562.     setbits_le32(SUNXI_CCM_BASE + 0x800, BIT(30));
  563.  
  564.     udelay(5);
  565.  
  566.     // turn on sdram clock gate
  567.     setbits_le32(SUNXI_CCM_BASE + 0x80c, BIT(0));
  568.  
  569.     // turn dram clock gate on, trigger sdr clock update
  570.     setbits_le32(SUNXI_CCM_BASE + 0x800, BIT(31) | BIT(27));
  571.     udelay(5);
  572.  
  573.     // mCTL clock enable
  574.     writel(0x8000, 0x310300c);
  575.     udelay(10);
  576. }
  577.  
  578. // The main purpose of this routine seems to be to copy an address configuration
  579. // from the dram_para1 and dram_para2 fields to the PHY configuration registers
  580. // (0x3102000, 0x3102004).
  581. //
  582. static void mctl_com_init(const dram_para_t *para, const dram_config_t *config)
  583. {
  584.     uint32_t val, width;
  585.     unsigned long ptr;
  586.     int i;
  587.  
  588.     // purpose ??
  589.     clrsetbits_le32(0x3102008, 0x3f00, 0x2000);
  590.  
  591.     // set SDRAM type and word width
  592.     val  = readl(0x3102000) & ~0x00fff000;
  593.     val |= (para->dram_type & 0x7) << 16;       // DRAM type
  594.     val |= (~config->dram_para2 & 0x1) << 12;       // DQ width
  595.     val |= BIT(22);                 // ??
  596.     if (para->dram_type == SUNXI_DRAM_TYPE_LPDDR2 ||
  597.         para->dram_type == SUNXI_DRAM_TYPE_LPDDR3) {
  598.         val |= BIT(19);     // type 6 and 7 must use 1T
  599.     } else {
  600.         if (config->dram_tpr13 & BIT(5))
  601.             val |= BIT(19);
  602.     }
  603.     writel(val, 0x3102000);
  604.  
  605.     // init rank / bank / row for single/dual or two different ranks
  606.     if ((config->dram_para2 & BIT(8)) &&
  607.         ((config->dram_para2 & 0xf000) != 0x1000))
  608.         width = 32;
  609.     else
  610.         width = 16;
  611.  
  612.     ptr = 0x3102000;
  613.     for (i = 0; i < width; i += 16) {
  614.         val = readl(ptr) & 0xfffff000;
  615.  
  616.         val |= (config->dram_para2 >> 12) & 0x3; // rank
  617.         val |= ((config->dram_para1 >> (i + 12)) << 2) & 0x4; // bank - 2
  618.         val |= (((config->dram_para1 >> (i + 4)) - 1) << 4) & 0xff; // row - 1
  619.  
  620.         // convert from page size to column addr width - 3
  621.         switch ((config->dram_para1 >> i) & 0xf) {
  622.         case 8: val |= 0xa00; break;
  623.         case 4: val |= 0x900; break;
  624.         case 2: val |= 0x800; break;
  625.         case 1: val |= 0x700; break;
  626.         default: val |= 0x600; break;
  627.         }
  628.         writel(val, ptr);
  629.         ptr += 4;
  630.     }
  631.  
  632.     // set ODTMAP based on number of ranks in use
  633.     val = (readl(0x3102000) & 0x1) ? 0x303 : 0x201;
  634.     writel(val, 0x3103120);
  635.  
  636.     // set mctl reg 3c4 to zero when using half DQ
  637.     if (config->dram_para2 & BIT(0))
  638.         writel(0, 0x31033c4);
  639.  
  640.     // purpose ??
  641.     if (para->dram_tpr4) {
  642.                 setbits_le32(0x3102000, (para->dram_tpr4 & 0x3) << 25);
  643.                 setbits_le32(0x3102004, (para->dram_tpr4 & 0x7fc) << 10);
  644.     }
  645. }
  646.  
  647. static const uint8_t ac_remapping_tables[][22] = {
  648.     [0] = { 0 },
  649.     [1] = {  1,  9,  3,  7,  8, 18,  4, 13,  5,  6, 10,
  650.          2, 14, 12,  0,  0, 21, 17, 20, 19, 11, 22 },
  651.     [2] = {  4,  9,  3,  7,  8, 18,  1, 13,  2,  6, 10,
  652.          5, 14, 12,  0,  0, 21, 17, 20, 19, 11, 22 },
  653.     [3] = {  1,  7,  8, 12, 10, 18,  4, 13,  5,  6,  3,
  654.          2,  9,  0,  0,  0, 21, 17, 20, 19, 11, 22 },
  655.     [4] = {  4, 12, 10,  7,  8, 18,  1, 13,  2,  6,  3,
  656.          5,  9,  0,  0,  0, 21, 17, 20, 19, 11, 22 },
  657.     [5] = { 13,  2,  7,  9, 12, 19,  5,  1,  6,  3,  4,
  658.          8, 10,  0,  0,  0, 21, 22, 18, 17, 11, 20 },
  659.     [6] = {  3, 10,  7, 13,  9, 11,  1,  2,  4,  6,  8,
  660.          5, 12,  0,  0,  0, 20,  1,  0, 21, 22, 17 },
  661.     [7] = {  3,  2,  4,  7,  9,  1, 17, 12, 18, 14, 13,
  662.          8, 15,  6, 10,  5, 19, 22, 16, 21, 20, 11 },
  663. };
  664.  
  665. /*
  666.  * This routine chooses one of several remapping tables for 22 lines.
  667.  * It is unclear which lines are being remapped. It seems to pick
  668.  * table cfg7 for the Nezha board.
  669.  */
  670. static void mctl_phy_ac_remapping(const dram_para_t *para,
  671.                   const dram_config_t *config)
  672. {
  673.     const uint8_t *cfg;
  674.     uint32_t fuse, val;
  675.  
  676.     /*
  677.      * It is unclear whether the LPDDRx types don't need any remapping,
  678.      * or whether the original code just didn't provide tables.
  679.      */
  680.     if (para->dram_type != SUNXI_DRAM_TYPE_DDR2 &&
  681.         para->dram_type != SUNXI_DRAM_TYPE_DDR3)
  682.         return;
  683.  
  684.     fuse = (readl(SUNXI_SID_BASE + 0x28) & 0xf00) >> 8;
  685.     debug("DDR efuse: 0x%x\n", fuse);
  686.  
  687.     if (para->dram_type == SUNXI_DRAM_TYPE_DDR2) {
  688.         if (fuse == 15 || fuse == 10)
  689.             return;
  690.         cfg = ac_remapping_tables[6];
  691.     } else {
  692.         if (config->dram_tpr13 & 0xc0000) {
  693.             cfg = ac_remapping_tables[7];
  694.         } else {
  695.             switch (fuse) {
  696.             case 8: cfg = ac_remapping_tables[2]; break;
  697.             case 9: cfg = ac_remapping_tables[3]; break;
  698.             case 10: cfg = ac_remapping_tables[5]; break;
  699.             case 11: cfg = ac_remapping_tables[4]; break;
  700.             default:
  701.             case 12: cfg = ac_remapping_tables[1]; break;
  702.             case 13:
  703.             case 14: cfg = ac_remapping_tables[0]; break;
  704.             }
  705.         }
  706.     }
  707.  
  708.     val = (cfg[4] << 25) | (cfg[3] << 20) | (cfg[2] << 15) |
  709.           (cfg[1] << 10) | (cfg[0] << 5);
  710.     writel(val, 0x3102500);
  711.  
  712.     val = (cfg[10] << 25) | (cfg[9] << 20) | (cfg[8] << 15) |
  713.           (cfg[ 7] << 10) | (cfg[6] <<  5) | cfg[5];
  714.     writel(val, 0x3102504);
  715.  
  716.     val = (cfg[15] << 20) | (cfg[14] << 15) | (cfg[13] << 10) |
  717.           (cfg[12] <<  5) | cfg[11];
  718.     writel(val, 0x3102508);
  719.  
  720.     val = (cfg[21] << 25) | (cfg[20] << 20) | (cfg[19] << 15) |
  721.           (cfg[18] << 10) | (cfg[17] <<  5) | cfg[16];
  722.     writel(val, 0x310250c);
  723.  
  724.     val = (cfg[4] << 25) | (cfg[3] << 20) | (cfg[2] << 15) |
  725.           (cfg[1] << 10) | (cfg[0] <<  5) | 1;
  726.     writel(val, 0x3102500);
  727. }
  728.  
  729. // Init the controller channel. The key part is placing commands in the main
  730. // command register (PIR, 0x3103000) and checking command status (PGSR0, 0x3103010).
  731. //
  732. static unsigned int mctl_channel_init(unsigned int ch_index,
  733.                       const dram_para_t *para,
  734.                       const dram_config_t *config)
  735. {
  736.     unsigned int val, dqs_gating_mode;
  737.  
  738.     dqs_gating_mode = (config->dram_tpr13 & 0xc) >> 2;
  739.  
  740.     // set DDR clock to half of CPU clock
  741.     clrsetbits_le32(0x310200c, 0xfff, (para->dram_clk / 2) - 1);
  742.  
  743.     // MRCTRL0 nibble 3 undocumented
  744.     clrsetbits_le32(0x3103108, 0xf00, 0x300);
  745.  
  746.     if (para->dram_odt_en)
  747.         val = 0;
  748.     else
  749.         val = BIT(5);
  750.  
  751.     // DX0GCR0
  752.     if (para->dram_clk > 672)
  753.         clrsetbits_le32(0x3103344, 0xf63e, val);
  754.     else
  755.         clrsetbits_le32(0x3103344, 0xf03e, val);
  756.  
  757.     // DX1GCR0
  758.     if (para->dram_clk > 672) {
  759.                 setbits_le32(0x3103344, 0x400);
  760.         clrsetbits_le32(0x31033c4, 0xf63e, val);
  761.     } else {
  762.         clrsetbits_le32(0x31033c4, 0xf03e, val);
  763.     }
  764.  
  765.     // 0x3103208 undocumented
  766.     setbits_le32(0x3103208, BIT(1));
  767.  
  768.     eye_delay_compensation(para);
  769.  
  770.     // set PLL SSCG ?
  771.     val = readl(0x3103108);
  772.     if (dqs_gating_mode == 1) {
  773.         clrsetbits_le32(0x3103108, 0xc0, 0);
  774.         clrbits_le32(0x31030bc, 0x107);
  775.     } else if (dqs_gating_mode == 2) {
  776.         clrsetbits_le32(0x3103108, 0xc0, 0x80);
  777.  
  778.         clrsetbits_le32(0x31030bc, 0x107,
  779.                 (((config->dram_tpr13 >> 16) & 0x1f) - 2) | 0x100);
  780.         clrsetbits_le32(0x310311c, BIT(31), BIT(27));
  781.     } else {
  782.         clrbits_le32(0x3103108, 0x40);
  783.         udelay(10);
  784.         setbits_le32(0x3103108, 0xc0);
  785.     }
  786.  
  787.     if (para->dram_type == SUNXI_DRAM_TYPE_LPDDR2 ||
  788.         para->dram_type == SUNXI_DRAM_TYPE_LPDDR3) {
  789.         if (dqs_gating_mode == 1)
  790.             clrsetbits_le32(0x310311c, 0x080000c0, 0x80000000);
  791.         else
  792.             clrsetbits_le32(0x310311c, 0x77000000, 0x22000000);
  793.     }
  794.  
  795.     clrsetbits_le32(0x31030c0, 0x0fffffff,
  796.             (config->dram_para2 & BIT(12)) ? 0x03000001 : 0x01000007);
  797.  
  798.     if (readl(0x70005d4) & BIT(16)) {
  799.         clrbits_le32(0x7010250, 0x2);
  800.         udelay(10);
  801.     }
  802.  
  803.     // Set ZQ config
  804.     clrsetbits_le32(0x3103140, 0x3ffffff,
  805.             (para->dram_zq & 0x00ffffff) | BIT(25));
  806.  
  807.     // Initialise DRAM controller
  808.     if (dqs_gating_mode == 1) {
  809.         //writel(0x52, 0x3103000); // prep PHY reset + PLL init + z-cal
  810.         writel(0x53, 0x3103000); // Go
  811.  
  812.         while ((readl(0x3103010) & 0x1) == 0) {
  813.         } // wait for IDONE
  814.         udelay(10);
  815.  
  816.         // 0x520 = prep DQS gating + DRAM init + d-cal
  817.         if (para->dram_type == SUNXI_DRAM_TYPE_DDR3)
  818.             writel(0x5a0, 0x3103000);       // + DRAM reset
  819.         else
  820.             writel(0x520, 0x3103000);
  821.     } else {
  822.         if ((readl(0x70005d4) & (1 << 16)) == 0) {
  823.             // prep DRAM init + PHY reset + d-cal + PLL init + z-cal
  824.             if (para->dram_type == SUNXI_DRAM_TYPE_DDR3)
  825.                 writel(0x1f2, 0x3103000);   // + DRAM reset
  826.             else
  827.                 writel(0x172, 0x3103000);
  828.         } else {
  829.             // prep PHY reset + d-cal + z-cal
  830.             writel(0x62, 0x3103000);
  831.         }
  832.     }
  833.  
  834.     setbits_le32(0x3103000, 0x1);        // GO
  835.  
  836.     udelay(10);
  837.     while ((readl(0x3103010) & 0x1) == 0) {
  838.     } // wait for IDONE
  839.  
  840.     if (readl(0x70005d4) & BIT(16)) {
  841.         clrsetbits_le32(0x310310c, 0x06000000, 0x04000000);
  842.         udelay(10);
  843.  
  844.         setbits_le32(0x3103004, 0x1);
  845.  
  846.         while ((readl(0x3103018) & 0x7) != 0x3) {
  847.         }
  848.  
  849.         clrbits_le32(0x7010250, 0x1);
  850.         udelay(10);
  851.  
  852.         clrbits_le32(0x3103004, 0x1);
  853.  
  854.         while ((readl(0x3103018) & 0x7) != 0x1) {
  855.         }
  856.  
  857.         udelay(15);
  858.  
  859.         if (dqs_gating_mode == 1) {
  860.             clrbits_le32(0x3103108, 0xc0);
  861.             clrsetbits_le32(0x310310c, 0x06000000, 0x02000000);
  862.             udelay(1);
  863.             writel(0x401, 0x3103000);
  864.  
  865.             while ((readl(0x3103010) & 0x1) == 0) {
  866.             }
  867.         }
  868.     }
  869.  
  870.     // Check for training error
  871.     if (readl(0x3103010) & BIT(20)) {
  872.         printf("ZQ calibration error, check external 240 ohm resistor\n");
  873.         return 0;
  874.     }
  875.  
  876.     // STATR = Zynq STAT? Wait for status 'normal'?
  877.     while ((readl(0x3103018) & 0x1) == 0) {
  878.     }
  879.  
  880.     setbits_le32(0x310308c, BIT(31));
  881.     udelay(10);
  882.     clrbits_le32(0x310308c, BIT(31));
  883.     udelay(10);
  884.     setbits_le32(0x3102014, BIT(31));
  885.     udelay(10);
  886.  
  887.     clrbits_le32(0x310310c, 0x06000000);
  888.  
  889.     if (dqs_gating_mode == 1)
  890.         clrsetbits_le32(0x310311c, 0xc0, 0x40);
  891.  
  892.     return 1;
  893. }
  894.  
  895. static unsigned int calculate_rank_size(uint32_t regval)
  896. {
  897.     unsigned int bits;
  898.  
  899.     bits = (regval >> 8) & 0xf; /* page size - 3 */
  900.     bits += (regval >> 4) & 0xf;    /* row width - 1 */
  901.     bits += (regval >> 2) & 0x3;    /* bank count - 2 */
  902.     bits -= 14;         /* 1MB = 20 bits, minus above 6 = 14 */
  903.  
  904.     return 1U << bits;
  905. }
  906.  
  907. /*
  908.  * The below routine reads the dram config registers and extracts
  909.  * the number of address bits in each rank available. It then calculates
  910.  * total memory size in MB.
  911.  */
  912. static unsigned int DRAMC_get_dram_size(void)
  913. {
  914.     uint32_t val;
  915.     unsigned int size;
  916.  
  917.     val = readl(0x3102000);     /* MC_WORK_MODE0 */
  918.     size = calculate_rank_size(val);
  919.     if ((val & 0x3) == 0)       /* single rank? */
  920.         return size;
  921.  
  922.     val = readl(0x3102004);     /* MC_WORK_MODE1 */
  923.     if ((val & 0x3) == 0)       /* two identical ranks? */
  924.         return size * 2;
  925.  
  926.     /* add sizes of both ranks */
  927.     return size + calculate_rank_size(val);
  928. }
  929.  
  930. /*
  931.  * The below routine reads the command status register to extract
  932.  * DQ width and rank count. This follows the DQS training command in
  933.  * channel_init. If error bit 22 is reset, we have two ranks and full DQ.
  934.  * If there was an error, figure out whether it was half DQ, single rank,
  935.  * or both. Set bit 12 and 0 in dram_para2 with the results.
  936.  */
  937. static int dqs_gate_detect(dram_config_t *config)
  938. {
  939.     uint32_t dx0, dx1;
  940.  
  941.     if ((readl(0x3103010) & BIT(22)) == 0) {
  942.         config->dram_para2 = (config->dram_para2 & ~0xf) | BIT(12);
  943.         printf("dual rank and full DQ\n");
  944.  
  945.         return 1;
  946.     }
  947.  
  948.     dx0 = (readl(0x3103348) & 0x3000000) >> 24;
  949.     if (dx0 == 0) {
  950.         config->dram_para2 = (config->dram_para2 & ~0xf) | 0x1001;
  951.         printf("dual rank and half DQ\n");
  952.  
  953.         return 1;
  954.     }
  955.  
  956.     if (dx0 == 2) {
  957.         dx1 = (readl(0x31033c8) & 0x3000000) >> 24;
  958.         if (dx1 == 2) {
  959.             config->dram_para2 = config->dram_para2 & ~0xf00f;
  960.             printf("single rank and full DQ\n");
  961.         } else {
  962.             config->dram_para2 = (config->dram_para2 & ~0xf00f) | BIT(0);
  963.             printf("single rank and half DQ\n");
  964.         }
  965.  
  966.         return 1;
  967.     }
  968.  
  969.     if ((config->dram_tpr13 & BIT(29)) == 0)
  970.         return 0;
  971.  
  972.     printf("DX0 state: %d\n", dx0);
  973.     printf("DX1 state: %d\n", dx1);
  974.  
  975.     return 0;
  976. }
  977.  
  978. static int dramc_simple_wr_test(unsigned int mem_mb, int len)
  979. {
  980.     unsigned int  offs  = (mem_mb / 2) << 18; // half of memory size
  981.     unsigned int  patt1 = 0x01234567;
  982.     unsigned int  patt2 = 0xfedcba98;
  983.     unsigned int *addr, v1, v2, i;
  984.  
  985.     addr = (unsigned int *)CFG_SYS_SDRAM_BASE;
  986.     for (i = 0; i != len; i++, addr++) {
  987.         writel(patt1 + i, (unsigned long)addr);
  988.         writel(patt2 + i, (unsigned long)(addr + offs));
  989.     }
  990.  
  991.     addr = (unsigned int *)CFG_SYS_SDRAM_BASE;
  992.     for (i = 0; i != len; i++) {
  993.         v1 = readl((unsigned long)(addr + i));
  994.         v2 = patt1 + i;
  995.         if (v1 != v2) {
  996.             printf("DRAM: simple test FAIL\n");
  997.             printf("%x != %x at address %p\n", v1, v2, addr + i);
  998.             return 1;
  999.         }
  1000.         v1 = readl((unsigned long)(addr + offs + i));
  1001.         v2 = patt2 + i;
  1002.         if (v1 != v2) {
  1003.             printf("DRAM: simple test FAIL\n");
  1004.             printf("%x != %x at address %p\n", v1, v2, addr + offs + i);
  1005.             return 1;
  1006.         }
  1007.     }
  1008.  
  1009.     debug("DRAM: simple test OK\n");
  1010.     return 0;
  1011. }
  1012.  
  1013. // Set the Vref mode for the controller
  1014. //
  1015. static void mctl_vrefzq_init(const dram_para_t *para, const dram_config_t *config)
  1016. {
  1017.     if (config->dram_tpr13 & BIT(17))
  1018.         return;
  1019.  
  1020.     clrsetbits_le32(0x3103110, 0x7f7f7f7f, para->dram_tpr5);
  1021.  
  1022.     // IOCVR1
  1023.     if ((config->dram_tpr13 & BIT(16)) == 0)
  1024.         clrsetbits_le32(0x3103114, 0x7f, para->dram_tpr6 & 0x7f);
  1025. }
  1026.  
  1027. // Perform an init of the controller. This is actually done 3 times. The first
  1028. // time to establish the number of ranks and DQ width. The second time to
  1029. // establish the actual ram size. The third time is final one, with the final
  1030. // settings.
  1031. //
  1032. static int mctl_core_init(const dram_para_t *para, const dram_config_t *config)
  1033. {
  1034.     mctl_sys_init(para, config);
  1035.  
  1036.     mctl_vrefzq_init(para, config);
  1037.  
  1038.     mctl_com_init(para, config);
  1039.  
  1040.     mctl_phy_ac_remapping(para, config);
  1041.  
  1042.     mctl_set_timing_params(para, config);
  1043.  
  1044.     return mctl_channel_init(0, para, config);
  1045. }
  1046.  
  1047. /*
  1048.  * This routine sizes a DRAM device by cycling through address lines and
  1049.  * figuring out if they are connected to a real address line, or if the
  1050.  * address is a mirror.
  1051.  * First the column and bank bit allocations are set to low values (2 and 9
  1052.  * address lines). Then a maximum allocation (16 lines) is set for rows and
  1053.  * this is tested.
  1054.  * Next the BA2 line is checked. This seems to be placed above the column,
  1055.  * BA0-1 and row addresses. Finally, the column address is allocated 13 lines
  1056.  * and these are tested. The results are placed in dram_para1 and dram_para2.
  1057.  */
  1058. static int auto_scan_dram_size(const dram_para_t *para, dram_config_t *config) {
  1059.     uint32_t i = 0, j = 0, current_rank = 0;
  1060.     uint32_t rank_count = 1, addr_line = 0;
  1061.     uint32_t reg_val = 0, ret = 0, cnt = 0;
  1062.     unsigned long mc_work_mode;
  1063.     uint32_t rank1_addr = CFG_SYS_SDRAM_BASE;
  1064.  
  1065.     // init core
  1066.     if (mctl_core_init(para, config) == 0) {
  1067.         debug("DRAM initial error : 0!\n");
  1068.         return 0;
  1069.     }
  1070.  
  1071.     // Set rank_count to 2
  1072.     if ((((config->dram_para2 >> 12) & 0xf) == 0x1))
  1073.         rank_count = 2;
  1074.  
  1075.     for (current_rank = 0; current_rank < rank_count; current_rank++) {
  1076.         mc_work_mode = ((0x3102000 + 0x00) + 4 * current_rank);
  1077.  
  1078.         /* Set 16 Row 4Bank 512BPage for Rank 1 */
  1079.         if (current_rank == 1) {
  1080.             clrsetbits_le32((0x3102000 + 0x00), 0xf0c, 0x6f0);
  1081.             clrsetbits_le32((0x3102000 + 0x04), 0xf0c, 0x6f0);
  1082.             /* update Rank 1 addr */
  1083.             rank1_addr = CFG_SYS_SDRAM_BASE + (0x1 << 27);
  1084.         }
  1085.  
  1086.         /* write test pattern */
  1087.         for (i = 0; i < 64; i++) {
  1088.             writel((i % 2) ? (CFG_SYS_SDRAM_BASE + 4 * i) : (~(CFG_SYS_SDRAM_BASE + 4 * i)),
  1089.                    CFG_SYS_SDRAM_BASE + 4 * i);
  1090.         }
  1091.         /* set row mode */
  1092.         clrsetbits_le32(mc_work_mode, 0xf0c, 0x6f0);
  1093.         udelay(2);
  1094.  
  1095.         for (i = 11; i < 17; i++) {
  1096.             ret = CFG_SYS_SDRAM_BASE + (1 << (i + 2 + 9)); /* row-bank-column */
  1097.             cnt = 0;
  1098.             for (j = 0; j < 64; j++) {
  1099.                 reg_val = (j % 2) ? (rank1_addr + 4 * j) : (~(rank1_addr + 4 * j));
  1100.                 if (reg_val == readl(ret + j * 4)) {
  1101.                     cnt++;
  1102.                 } else
  1103.                     break;
  1104.             }
  1105.             if (cnt == 64) {
  1106.                 break;
  1107.             }
  1108.         }
  1109.         if (i >= 16)
  1110.             i = 16;
  1111.         addr_line += i;
  1112.  
  1113.         debug("rank %u row = %u \n", current_rank, i);
  1114.  
  1115.         /* Store rows in para 1 */
  1116.         config->dram_para1 &= ~(0xffU << (16 * current_rank + 4));
  1117.         config->dram_para1 |= (i << (16 * current_rank + 4));
  1118.  
  1119.         /* Set bank mode for current rank */
  1120.         if (current_rank == 1) { /* Set bank mode for rank0 */
  1121.             clrsetbits_le32((0x3102000 + 0x00), 0xffc, 0x6a4);
  1122.         }
  1123.  
  1124.         /* Set bank mode for current rank */
  1125.         clrsetbits_le32(mc_work_mode, 0xffc, 0x6a4);
  1126.         udelay(1);
  1127.  
  1128.         for (i = 0; i < 1; i++) {
  1129.             ret = CFG_SYS_SDRAM_BASE + (0x1U << (i + 2 + 9));
  1130.             cnt = 0;
  1131.             for (j = 0; j < 64; j++) {
  1132.                 reg_val = (j % 2) ? (rank1_addr + 4 * j) : (~(rank1_addr + 4 * j));
  1133.                 if (reg_val == readl(ret + j * 4)) {
  1134.                     cnt++;
  1135.                 } else
  1136.                     break;
  1137.             }
  1138.             if (cnt == 64) {
  1139.                 break;
  1140.             }
  1141.         }
  1142.  
  1143.         addr_line += i + 2;
  1144.         debug("rank %u bank = %u \n", current_rank, (4 + i * 4));
  1145.  
  1146.         /* Store bank in para 1 */
  1147.         config->dram_para1 &= ~(0xfU << (16 * current_rank + 12));
  1148.         config->dram_para1 |= (i << (16 * current_rank + 12));
  1149.  
  1150.         /* Set page mode for rank0 */
  1151.         if (current_rank == 1) {
  1152.             clrsetbits_le32(mc_work_mode, 0xffc, 0xaa0);
  1153.         }
  1154.  
  1155.         /* Set page mode for current rank */
  1156.         clrsetbits_le32(mc_work_mode, 0xffc, 0xaa0);
  1157.         udelay(2);
  1158.  
  1159.         /* Scan per address line, until address wraps (i.e. see shadow) */
  1160.         for (i = 9; i <= 13; i++) {
  1161.             ret = CFG_SYS_SDRAM_BASE + (0x1U << i);// column 40000000+(9~13)
  1162.             cnt = 0;
  1163.             for (j = 0; j < 64; j++) {
  1164.                 reg_val = (j % 2) ? (CFG_SYS_SDRAM_BASE + 4 * j) : (~(CFG_SYS_SDRAM_BASE + 4 * j));
  1165.                 if (reg_val == readl(ret + j * 4)) {
  1166.                     cnt++;
  1167.                 } else {
  1168.                     break;
  1169.                 }
  1170.             }
  1171.             if (cnt == 64) {
  1172.                 break;
  1173.             }
  1174.         }
  1175.  
  1176.         if (i >= 13) {
  1177.             i = 13;
  1178.         }
  1179.  
  1180.         /* add page size */
  1181.         addr_line += i;
  1182.  
  1183.         if (i == 9) {
  1184.             i = 0;
  1185.         } else {
  1186.             i = (0x1U << (i - 10));
  1187.         }
  1188.  
  1189.         debug("rank %u page size = %u KB \n", current_rank, i);
  1190.  
  1191.         /* Store page in para 1 */
  1192.         config->dram_para1 &= ~(0xfU << (16 * current_rank));
  1193.         config->dram_para1 |= (i << (16 * current_rank));
  1194.     }
  1195.  
  1196.     /* check dual rank config */
  1197.     if (rank_count == 2) {
  1198.         config->dram_para2 &= 0xfffff0ff;
  1199.         if ((config->dram_para1 & 0xffff) == (config->dram_para1 >> 16)) {
  1200.             debug("rank1 config same as rank0\n");
  1201.         } else {
  1202.             config->dram_para2 |= 0x1 << 8;
  1203.             debug("rank1 config different from rank0\n");
  1204.         }
  1205.     }
  1206.     return 1;
  1207. }
  1208.  
  1209. /*
  1210.  * This routine sets up parameters with dqs_gating_mode equal to 1 and two
  1211.  * ranks enabled. It then configures the core and tests for 1 or 2 ranks and
  1212.  * full or half DQ width. It then resets the parameters to the original values.
  1213.  * dram_para2 is updated with the rank and width findings.
  1214.  */
  1215. static int auto_scan_dram_rank_width(const dram_para_t *para,
  1216.                      dram_config_t *config)
  1217. {
  1218.     unsigned int s1 = config->dram_tpr13;
  1219.     unsigned int s2 = config->dram_para1;
  1220.  
  1221.     config->dram_para1 = 0x00b000b0;
  1222.     config->dram_para2 = (config->dram_para2 & ~0xf) | BIT(12);
  1223.  
  1224.     /* set DQS probe mode */
  1225.     config->dram_tpr13 = (config->dram_tpr13 & ~0x8) | BIT(2) | BIT(0);
  1226.  
  1227.     mctl_core_init(para, config);
  1228.  
  1229.     if (readl(0x3103010) & BIT(20)) {
  1230.         printf("ERROR: read 0x3103010 failed\n");
  1231.         return 0;
  1232.     }
  1233.  
  1234.     if (dqs_gate_detect(config) == 0) {
  1235.         printf("ERROR: dqs_gate_detect failed\n");
  1236.         return 0;
  1237.     }
  1238.  
  1239.     config->dram_tpr13 = s1;
  1240.     config->dram_para1 = s2;
  1241.  
  1242.     return 1;
  1243. }
  1244.  
  1245. /*
  1246.  * This routine determines the SDRAM topology. It first establishes the number
  1247.  * of ranks and the DQ width. Then it scans the SDRAM address lines to establish
  1248.  * the size of each rank. It then updates dram_tpr13 to reflect that the sizes
  1249.  * are now known: a re-init will not repeat the autoscan.
  1250.  */
  1251. static int auto_scan_dram_config(const dram_para_t *para,
  1252.                  dram_config_t *config)
  1253. {
  1254.     if (((config->dram_tpr13 & BIT(14)) == 0) &&
  1255.         (auto_scan_dram_rank_width(para, config) == 0)) {
  1256.         printf("ERROR: auto scan dram rank & width failed\n");
  1257.         return 0;
  1258.     }
  1259.  
  1260.     if (((config->dram_tpr13 & BIT(0)) == 0) &&
  1261.         (auto_scan_dram_size(para, config) == 0)) {
  1262.         printf("ERROR: auto scan dram size failed\n");
  1263.         return 0;
  1264.     }
  1265.  
  1266.     if ((config->dram_tpr13 & BIT(15)) == 0)
  1267.         config->dram_tpr13 |= BIT(14) | BIT(13) | BIT(1) | BIT(0);
  1268.  
  1269.     return 1;
  1270. }
  1271.  
  1272. static int init_DRAM(int type, const dram_para_t *para)
  1273. {
  1274.     dram_config_t config = {
  1275.         .dram_para1 = 0x000000d2, // was 0x000010d2
  1276.         .dram_para2 = 0,
  1277.         .dram_tpr13 = CONFIG_DRAM_SUNXI_TPR13,
  1278.     };
  1279.     u32 rc, mem_size_mb;
  1280.  
  1281.     debug("DRAM BOOT DRIVE INFO: %s\n", "V0.24");
  1282.     debug("DRAM CLK = %d MHz\n", para->dram_clk);
  1283.     debug("DRAM Type = %d (2:DDR2,3:DDR3)\n", para->dram_type);
  1284.     if ((para->dram_odt_en & 0x1) == 0)
  1285.         debug("DRAMC read ODT off\n");
  1286.     else
  1287.         debug("DRAMC ZQ value: 0x%x\n", para->dram_zq);
  1288.  
  1289.     /* Test ZQ status */
  1290.     if (config.dram_tpr13 & BIT(16)) {
  1291.         debug("DRAM only have internal ZQ\n");
  1292.         setbits_le32(0x3000160, BIT(8));
  1293.         writel(0, 0x3000168);
  1294.         udelay(10);
  1295.     } else {
  1296.         clrbits_le32(0x3000160, 0x3);
  1297.         writel(config.dram_tpr13 & BIT(16), 0x7010254);
  1298.         udelay(10);
  1299.         clrsetbits_le32(0x3000160, 0x108, BIT(1));
  1300.         udelay(10);
  1301.         setbits_le32(0x3000160, BIT(0));
  1302.         udelay(20);
  1303.         debug("ZQ value = 0x%x\n", readl(0x300016c));
  1304.     }
  1305.  
  1306.     dram_voltage_set(para);
  1307.  
  1308.     /* Set SDRAM controller auto config */
  1309.     if ((config.dram_tpr13 & BIT(0)) == 0) {
  1310.         if (auto_scan_dram_config(para, &config) == 0) {
  1311.             printf("auto_scan_dram_config() FAILED\n");
  1312.             return 0;
  1313.         }
  1314.     }
  1315.  
  1316.     /* report ODT */
  1317.     rc = para->dram_mr1;
  1318.     if ((rc & 0x44) == 0)
  1319.         debug("DRAM ODT off\n");
  1320.     else
  1321.         debug("DRAM ODT value: 0x%x\n", rc);
  1322.  
  1323.     /* Init core, final run */
  1324.     if (mctl_core_init(para, &config) == 0) {
  1325.         printf("DRAM initialisation error: 1\n");
  1326.         return 0;
  1327.     }
  1328.  
  1329.     /* Get SDRAM size */
  1330.     /* TODO: who ever puts a negative number in the top half? */
  1331.     rc = config.dram_para2;
  1332.     if (rc & BIT(31)) {
  1333.         rc = (rc >> 16) & ~BIT(15);
  1334.     } else {
  1335.         rc = DRAMC_get_dram_size();
  1336.         debug("DRAM: size = %dMB\n", rc);
  1337.         config.dram_para2 = (config.dram_para2 & 0xffffU) | rc << 16;
  1338.     }
  1339.     mem_size_mb = rc;
  1340.  
  1341.     /* Purpose ?? */
  1342.     if (config.dram_tpr13 & BIT(30)) {
  1343.         rc = para->dram_tpr8;
  1344.         if (rc == 0)
  1345.             rc = 0x10000200;
  1346.         writel(rc, 0x31030a0);
  1347.         writel(0x40a, 0x310309c);
  1348.         setbits_le32(0x3103004, BIT(0));
  1349.         debug("Enable Auto SR\n");
  1350.     } else {
  1351.         clrbits_le32(0x31030a0, 0xffff);
  1352.         clrbits_le32(0x3103004, 0x1);
  1353.     }
  1354.  
  1355.     /* Purpose ?? */
  1356.     if (config.dram_tpr13 & BIT(9)) {
  1357.         clrsetbits_le32(0x3103100, 0xf000, 0x5000);
  1358.     } else {
  1359.         if (para->dram_type != SUNXI_DRAM_TYPE_LPDDR2)
  1360.             clrbits_le32(0x3103100, 0xf000);
  1361.     }
  1362.  
  1363.     setbits_le32(0x3103140, BIT(31));
  1364.  
  1365.     /* CHECK: is that really writing to a different register? */
  1366.     if (config.dram_tpr13 & BIT(8))
  1367.         writel(readl(0x3103140) | 0x300, 0x31030b8);
  1368.  
  1369.     if (config.dram_tpr13 & BIT(16))
  1370.         clrbits_le32(0x3103108, BIT(13));
  1371.     else
  1372.         setbits_le32(0x3103108, BIT(13));
  1373.  
  1374.     /* Purpose ?? */
  1375.     if (para->dram_type == SUNXI_DRAM_TYPE_LPDDR3)
  1376.         clrsetbits_le32(0x310307c, 0xf0000, 0x1000);
  1377.  
  1378.     dram_enable_all_master();
  1379.     if (config.dram_tpr13 & BIT(28)) {
  1380.         if ((readl(0x70005d4) & BIT(16)) ||
  1381.             dramc_simple_wr_test(mem_size_mb, 4096))
  1382.             return 0;
  1383.     }
  1384.  
  1385.     return mem_size_mb;
  1386. }
  1387.  
  1388. static const dram_para_t para = {
  1389.     .dram_clk   = CONFIG_DRAM_CLK,
  1390.     .dram_type  = CONFIG_SUNXI_DRAM_TYPE,
  1391.     .dram_zq    = CONFIG_DRAM_ZQ,
  1392.     .dram_odt_en    = CONFIG_DRAM_SUNXI_ODT_EN,
  1393.     .dram_mr0   = 0xe73,    // was 0x1c70
  1394.     .dram_mr1   = 0x02,     // was 0x42
  1395.     .dram_mr2   = 0,        // was 0x18
  1396.     .dram_mr3   = 0,
  1397.     .dram_tpr0  = CONFIG_DRAM_SUNXI_TPR0, // was 0x004a2195
  1398.     .dram_tpr1  = 0x0131A10C,   // was 0x02423190
  1399.     .dram_tpr2  = 0x00057041,   // was 0x0008b061
  1400.     .dram_tpr3  = 0xb4787896,   // unused
  1401.     .dram_tpr4  = 0,
  1402.     .dram_tpr5  = 0x48484848,
  1403.     .dram_tpr6  = 0x00000048,
  1404.     .dram_tpr7  = 0x1621121e,   // unused
  1405.     .dram_tpr8  = 0,
  1406.     .dram_tpr9  = 0,        // clock?
  1407.     .dram_tpr10 = 0,
  1408.     .dram_tpr11 = CONFIG_DRAM_SUNXI_TPR11,
  1409.     .dram_tpr12 = CONFIG_DRAM_SUNXI_TPR12,
  1410. };
  1411.  
  1412. unsigned long sunxi_dram_init(void)
  1413. {
  1414.     return init_DRAM(0, &para) * 1024UL * 1024;
  1415. };
  1416.  
  1417. #ifdef CONFIG_RAM       /* using the driver model */
  1418. struct sunxi_ram_priv {
  1419.     size_t size;
  1420. };
  1421.  
  1422. static int sunxi_ram_probe(struct udevice *dev)
  1423. {
  1424.     struct sunxi_ram_priv *priv = dev_get_priv(dev);
  1425.     unsigned long dram_size;
  1426.  
  1427.     debug("%s: %s: probing\n", __func__, dev->name);
  1428.  
  1429.     dram_size = sunxi_dram_init();
  1430.     if (!dram_size) {
  1431.         printf("DRAM init failed\n");
  1432.         return -ENODEV;
  1433.     }
  1434.  
  1435.     priv->size = dram_size;
  1436.  
  1437.     return 0;
  1438. }
  1439.  
  1440. static int sunxi_ram_get_info(struct udevice *dev, struct ram_info *info)
  1441. {
  1442.     struct sunxi_ram_priv *priv = dev_get_priv(dev);
  1443.  
  1444.     debug("%s: %s: getting info\n", __func__, dev->name);
  1445.  
  1446.     info->base = CFG_SYS_SDRAM_BASE;
  1447.     info->size = priv->size;
  1448.  
  1449.     return 0;
  1450. }
  1451.  
  1452. static struct ram_ops sunxi_ram_ops = {
  1453.     .get_info = sunxi_ram_get_info,
  1454. };
  1455.  
  1456. static const struct udevice_id sunxi_ram_ids[] = {
  1457.     { .compatible = "allwinner,sun20i-d1-mbus" },
  1458.     { }
  1459. };
  1460.  
  1461. U_BOOT_DRIVER(sunxi_ram) = {
  1462.     .name = "sunxi_ram",
  1463.     .id = UCLASS_RAM,
  1464.     .of_match = sunxi_ram_ids,
  1465.     .ops = &sunxi_ram_ops,
  1466.     .probe = sunxi_ram_probe,
  1467.     .priv_auto = sizeof(struct sunxi_ram_priv),
  1468. };
  1469. #endif              /* CONFIG_RAM (using driver model) */
  1470.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement