2 * Copyright 2016 Freescale Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
8 * Generic driver for Freescale MMDC(Multi Mode DDR Controller).
15 static void set_wait_for_bits_clear(void *ptr, u32 value, u32 bits)
21 while (in_be32(ptr) & bits) {
26 printf("Error: %p wait for clear timeout.\n", ptr);
29 void mmdc_init(const struct fsl_mmdc_info *priv)
31 struct mmdc_regs *mmdc = (struct mmdc_regs *)CONFIG_SYS_FSL_DDR_ADDR;
34 /* 1. set configuration request */
35 out_be32(&mmdc->mdscr, MDSCR_ENABLE_CON_REQ);
37 /* 2. configure the desired timing parameters */
38 out_be32(&mmdc->mdotc, priv->mdotc);
39 out_be32(&mmdc->mdcfg0, priv->mdcfg0);
40 out_be32(&mmdc->mdcfg1, priv->mdcfg1);
41 out_be32(&mmdc->mdcfg2, priv->mdcfg2);
43 /* 3. configure DDR type and other miscellaneous parameters */
44 out_be32(&mmdc->mdmisc, priv->mdmisc);
45 out_be32(&mmdc->mpmur0, MMDC_MPMUR0_FRC_MSR);
46 out_be32(&mmdc->mdrwd, priv->mdrwd);
47 out_be32(&mmdc->mpodtctrl, priv->mpodtctrl);
49 /* 4. configure the required delay while leaving reset */
50 out_be32(&mmdc->mdor, priv->mdor);
52 /* 5. configure DDR physical parameters */
53 /* set row/column address width, burst length, data bus width */
54 tmp = priv->mdctl & ~(MDCTL_SDE0 | MDCTL_SDE1);
55 out_be32(&mmdc->mdctl, tmp);
56 /* configure address space partition */
57 out_be32(&mmdc->mdasp, priv->mdasp);
59 /* 6. perform a ZQ calibration - not needed here, doing in #8b */
61 /* 7. enable MMDC with the desired chip select */
62 #if (CONFIG_CHIP_SELECTS_PER_CTRL == 1)
63 out_be32(&mmdc->mdctl, tmp | MDCTL_SDE0);
64 #elif (CONFIG_CHIP_SELECTS_PER_CTRL == 2)
65 out_be32(&mmdc->mdctl, tmp | MDCTL_SDE0 | MDCTL_SDE1);
68 /* 8a. dram init sequence: update MRs for ZQ, ODT, PRE, etc */
69 out_be32(&mmdc->mdscr, CMD_ADDR_LSB_MR_ADDR(8) | MDSCR_ENABLE_CON_REQ |
70 CMD_LOAD_MODE_REG | CMD_BANK_ADDR_2);
72 out_be32(&mmdc->mdscr, CMD_ADDR_LSB_MR_ADDR(0) | MDSCR_ENABLE_CON_REQ |
73 CMD_LOAD_MODE_REG | CMD_BANK_ADDR_3);
75 out_be32(&mmdc->mdscr, CMD_ADDR_LSB_MR_ADDR(4) | MDSCR_ENABLE_CON_REQ |
76 CMD_LOAD_MODE_REG | CMD_BANK_ADDR_1);
78 out_be32(&mmdc->mdscr, CMD_ADDR_MSB_MR_OP(0x19) |
79 CMD_ADDR_LSB_MR_ADDR(0x30) |
80 MDSCR_ENABLE_CON_REQ |
81 CMD_LOAD_MODE_REG | CMD_BANK_ADDR_0);
83 /* 8b. ZQ calibration */
84 out_be32(&mmdc->mdscr, CMD_ADDR_MSB_MR_OP(0x4) | MDSCR_ENABLE_CON_REQ |
85 CMD_ZQ_CALIBRATION | CMD_BANK_ADDR_0);
87 set_wait_for_bits_clear(&mmdc->mpzqhwctrl, priv->mpzqhwctrl,
88 MPZQHWCTRL_ZQ_HW_FORCE);
90 /* 9a. calibrations now, wr lvl */
91 out_be32(&mmdc->mdscr, CMD_ADDR_LSB_MR_ADDR(0x84) |
92 MDSCR_ENABLE_CON_REQ |
93 CMD_LOAD_MODE_REG | CMD_BANK_ADDR_1);
95 out_be32(&mmdc->mdscr, MDSCR_ENABLE_CON_REQ | MDSCR_WL_EN |
98 set_wait_for_bits_clear(&mmdc->mpwlgcr, MPWLGCR_HW_WL_EN,
103 out_be32(&mmdc->mdscr, CMD_ADDR_LSB_MR_ADDR(4) | MDSCR_ENABLE_CON_REQ |
104 CMD_LOAD_MODE_REG | CMD_BANK_ADDR_1);
105 out_be32(&mmdc->mdscr, MDSCR_ENABLE_CON_REQ);
109 /* 9b. read DQS gating calibration */
110 out_be32(&mmdc->mdscr, CMD_ADDR_MSB_MR_OP(4) | MDSCR_ENABLE_CON_REQ |
111 CMD_PRECHARGE_BANK_OPEN | CMD_BANK_ADDR_0);
113 out_be32(&mmdc->mdscr, CMD_ADDR_LSB_MR_ADDR(4) | MDSCR_ENABLE_CON_REQ |
114 CMD_LOAD_MODE_REG | CMD_BANK_ADDR_3);
116 out_be32(&mmdc->mppdcmpr2, MPPDCMPR2_MPR_COMPARE_EN);
118 /* set absolute read delay offset */
120 out_be32(&mmdc->mprddlctl, priv->mprddlctl);
122 out_be32(&mmdc->mprddlctl, MMDC_MPRDDLCTL_DEFAULT_DELAY);
124 set_wait_for_bits_clear(&mmdc->mpdgctrl0,
125 AUTO_RD_DQS_GATING_CALIBRATION_EN,
126 AUTO_RD_DQS_GATING_CALIBRATION_EN);
128 out_be32(&mmdc->mdscr, MDSCR_ENABLE_CON_REQ | CMD_LOAD_MODE_REG |
131 /* 9c. read calibration */
132 out_be32(&mmdc->mdscr, CMD_ADDR_MSB_MR_OP(4) | MDSCR_ENABLE_CON_REQ |
133 CMD_PRECHARGE_BANK_OPEN | CMD_BANK_ADDR_0);
134 out_be32(&mmdc->mdscr, CMD_ADDR_LSB_MR_ADDR(4) | MDSCR_ENABLE_CON_REQ |
135 CMD_LOAD_MODE_REG | CMD_BANK_ADDR_3);
136 out_be32(&mmdc->mppdcmpr2, MPPDCMPR2_MPR_COMPARE_EN);
137 set_wait_for_bits_clear(&mmdc->mprddlhwctl,
138 MPRDDLHWCTL_AUTO_RD_CALIBRATION_EN,
139 MPRDDLHWCTL_AUTO_RD_CALIBRATION_EN);
141 out_be32(&mmdc->mdscr, MDSCR_ENABLE_CON_REQ | CMD_LOAD_MODE_REG |
144 /* 10. configure power-down, self-refresh entry, exit parameters */
145 out_be32(&mmdc->mdpdc, priv->mdpdc);
146 out_be32(&mmdc->mapsr, MMDC_MAPSR_PWR_SAV_CTRL_STAT);
148 /* 11. ZQ config again? do nothing here */
150 /* 12. refresh scheme */
151 set_wait_for_bits_clear(&mmdc->mdref, priv->mdref,
152 MDREF_START_REFRESH);
154 /* 13. disable CON_REQ */
155 out_be32(&mmdc->mdscr, MDSCR_DISABLE_CFG_REQ);