1 /* 2 * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * 7 * Redistributions of source code must retain the above copyright notice, this 8 * list of conditions and the following disclaimer. 9 * 10 * Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 14 * Neither the name of ARM nor the names of its contributors may be used 15 * to endorse or promote products derived from this software without specific 16 * prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <m0_param.h> 32 #include "rk3399_mcu.h" 33 34 /* PMU */ 35 #define PMU_PWRDN_ST 0x18 36 #define PMU_BUS_IDLE_REQ 0x60 37 #define PMU_BUS_IDLE_ST 0x64 38 #define PMU_NOC_AUTO_ENA 0xd8 39 40 /* PMU_BUS_IDLE_REQ */ 41 #define IDLE_REQ_MSCH1 (1 << 19) 42 #define IDLE_REQ_MSCH0 (1 << 18) 43 44 /* #define PMU_BUS_IDLE_ST */ 45 #define IDLE_MSCH1 (1 << 19) 46 #define IDLE_MSCH0 (1 << 18) 47 48 #define PD_VOP_PWR_STAT (1 << 20) 49 50 /* CRU */ 51 #define CRU_DPLL_CON0 0x40 52 #define CRU_DPLL_CON1 0x44 53 #define CRU_DPLL_CON2 0x48 54 #define CRU_DPLL_CON3 0x4c 55 #define CRU_DPLL_CON4 0x50 56 #define CRU_DPLL_CON5 0x54 57 58 #define CRU_DPLL_CON2 0x48 59 #define CRU_DPLL_CON3 0x4c 60 #define CRU_CLKGATE10_CON 0x328 61 #define CRU_CLKGATE28_CON 0x370 62 63 /* CRU_CLKGATE10_CON */ 64 #define ACLK_VOP0_PRE_SRC_EN (1 << 8) 65 #define HCLK_VOP0_PRE_EN (1 << 9) 66 #define ACLK_VOP1_PRE_SRC_EN (1 << 10) 67 #define HCLK_VOP1_PRE_EN (1 << 11) 68 #define DCLK_VOP0_SRC_EN (1 << 12) 69 #define DCLK_VOP1_SRC_EN (1 << 13) 70 71 /* CRU_CLKGATE28_CON */ 72 #define HCLK_VOP0_EN (1 << 2) 73 #define ACLK_VOP0_EN (1 << 3) 74 #define HCLK_VOP1_EN (1 << 6) 75 #define ACLK_VOP1_EN (1 << 7) 76 77 /* CRU_PLL_CON3 */ 78 #define PLL_SLOW_MODE 0 79 #define PLL_NORMAL_MODE 1 80 #define PLL_MODE(n) ((0x3 << (8 + 16)) | ((n) << 8)) 81 #define PLL_POWER_DOWN(n) ((0x1 << (0 + 16)) | ((n) << 0)) 82 83 /* PMU CRU */ 84 #define PMU_CRU_GATEDIS_CON0 0x130 85 86 /* VOP */ 87 #define VOP_SYS_CTRL 0x8 88 #define VOP_SYS_CTRL1 0xc 89 #define VOP_WIN0_CTRL0 0x30 90 #define VOP_INTR_CLEAR0 0x284 91 #define VOP_INTR_RAW_STATUS0 0x28c 92 93 /* VOP_SYS_CTRL */ 94 #define VOP_DMA_STOP_EN (1 << 21) 95 #define VOP_STANDBY_EN (1 << 22) 96 97 /* VOP_WIN0_CTRL0 */ 98 #define WB_ENABLE (1 << 0) 99 100 /* VOP_INTR_CLEAR0 */ 101 #define INT_CLR_DMA_FINISH (1 << 15) 102 #define INT_CLR_LINE_FLAG1 (1 << 4) 103 #define INT_CLR_LINE_FLAG0 (1 << 3) 104 105 /* VOP_INTR_RAW_STATUS0 */ 106 #define INT_RAW_STATUS_DMA_FINISH (1 << 15) 107 #define INT_RAW_STATUS_LINE_FLAG1 (1 << 4) 108 #define INT_RAW_STATUS_LINE_FLAG0 (1 << 3) 109 110 /* CIC */ 111 #define CIC_CTRL0 0 112 #define CIC_CTRL1 0x4 113 #define CIC_STATUS0 0x10 114 115 uint32_t gatedis_con0; 116 117 static inline int check_dma_status(uint32_t vop_addr, uint32_t *clr_dma_flag) 118 { 119 if (*clr_dma_flag) { 120 mmio_write_32(vop_addr + VOP_INTR_CLEAR0, 0x80008000); 121 *clr_dma_flag = 0; 122 } 123 124 if ((mmio_read_32(vop_addr + VOP_SYS_CTRL) & 125 (VOP_STANDBY_EN | VOP_DMA_STOP_EN)) || 126 !(mmio_read_32(vop_addr + VOP_WIN0_CTRL0) & WB_ENABLE) || 127 (mmio_read_32(vop_addr + VOP_INTR_RAW_STATUS0) & 128 INT_RAW_STATUS_DMA_FINISH)) 129 return 1; 130 131 return 0; 132 } 133 134 static int wait_vop_dma_finish(void) 135 { 136 uint32_t clr_dma_flag = 1; 137 uint32_t ret = 0; 138 139 stopwatch_init_usecs_expire(60000); 140 while (((mmio_read_32(PMU_BASE + PMU_PWRDN_ST) & 141 PD_VOP_PWR_STAT) == 0)) { 142 /* 143 * VOPL case: 144 * CRU_CLKGATE10_CON(bit10): ACLK_VOP1_PRE_SRC_EN 145 * CRU_CLKGATE10_CON(bit11): HCLK_VOP1_PRE_EN 146 * CRU_CLKGATE10_CON(bit13): DCLK_VOP1_SRC_EN 147 * CRU_CLKGATE28_CON(bit7): ACLK_VOP1_EN 148 * CRU_CLKGATE28_CON(bit6): HCLK_VOP1_EN 149 * 150 * VOPB case: 151 * CRU_CLKGATE10_CON(bit8): ACLK_VOP0_PRE_SRC_EN 152 * CRU_CLKGATE10_CON(bit9): HCLK_VOP0_PRE_EN 153 * CRU_CLKGATE10_CON(bit12): DCLK_VOP0_SRC_EN 154 * CRU_CLKGATE28_CON(bit3): ACLK_VOP0_EN 155 * CRU_CLKGATE28_CON(bit2): HCLK_VOP0_EN 156 */ 157 if (((mmio_read_32(CRU_BASE_ADDR + CRU_CLKGATE10_CON) & 158 0x2c00) == 0) && 159 ((mmio_read_32(CRU_BASE_ADDR + CRU_CLKGATE28_CON) & 160 0xc0) == 0)) { 161 if (check_dma_status(VOP_LITE_BASE_ADDR, &clr_dma_flag)) 162 return; 163 } else if (((mmio_read_32(CRU_BASE_ADDR + CRU_CLKGATE10_CON) & 164 0x1300) == 0) && 165 ((mmio_read_32(CRU_BASE_ADDR + CRU_CLKGATE28_CON) & 166 0x0c) == 0)) { 167 if (check_dma_status(VOP_BIG_BASE_ADDR, &clr_dma_flag)) 168 return; 169 } else { 170 /* No VOPs are enabled, so don't wait. */ 171 return; 172 } 173 174 if (stopwatch_expired()) { 175 ret = 1; 176 goto out; 177 } 178 } 179 180 out: 181 stopwatch_reset(); 182 return ret; 183 } 184 185 static void idle_port(void) 186 { 187 gatedis_con0 = mmio_read_32(PMU_CRU_BASE_ADDR + PMU_CRU_GATEDIS_CON0); 188 mmio_write_32(PMU_CRU_BASE_ADDR + PMU_CRU_GATEDIS_CON0, 0x3fffffff); 189 mmio_setbits_32(PMU_BASE + PMU_BUS_IDLE_REQ, 190 IDLE_REQ_MSCH0 | IDLE_REQ_MSCH1); 191 while ((mmio_read_32(PMU_BASE + PMU_BUS_IDLE_ST) & 192 (IDLE_MSCH1 | IDLE_MSCH0)) != (IDLE_MSCH1 | IDLE_MSCH0)) 193 continue; 194 } 195 196 static void deidle_port(void) 197 { 198 mmio_clrbits_32(PMU_BASE + PMU_BUS_IDLE_REQ, 199 IDLE_REQ_MSCH0 | IDLE_REQ_MSCH1); 200 while (mmio_read_32(PMU_BASE + PMU_BUS_IDLE_ST) & 201 (IDLE_MSCH1 | IDLE_MSCH0)) 202 continue; 203 204 /* document is wrong, PMU_CRU_GATEDIS_CON0 do not need set MASK BIT */ 205 mmio_write_32(PMU_CRU_BASE_ADDR + PMU_CRU_GATEDIS_CON0, gatedis_con0); 206 } 207 208 static void ddr_set_pll(void) 209 { 210 mmio_write_32(CRU_BASE_ADDR + CRU_DPLL_CON3, PLL_MODE(PLL_SLOW_MODE)); 211 212 mmio_write_32(CRU_BASE_ADDR + CRU_DPLL_CON3, PLL_POWER_DOWN(1)); 213 mmio_write_32(CRU_BASE_ADDR + CRU_DPLL_CON0, 214 mmio_read_32(PARAM_ADDR + PARAM_DPLL_CON0)); 215 mmio_write_32(CRU_BASE_ADDR + CRU_DPLL_CON1, 216 mmio_read_32(PARAM_ADDR + PARAM_DPLL_CON1)); 217 mmio_write_32(CRU_BASE_ADDR + CRU_DPLL_CON3, PLL_POWER_DOWN(0)); 218 219 while ((mmio_read_32(CRU_BASE_ADDR + CRU_DPLL_CON2) & (1u << 31)) == 0) 220 continue; 221 222 mmio_write_32(CRU_BASE_ADDR + CRU_DPLL_CON3, PLL_MODE(PLL_NORMAL_MODE)); 223 } 224 225 void handle_dram(void) 226 { 227 wait_vop_dma_finish(); 228 229 idle_port(); 230 231 mmio_write_32(CIC_BASE_ADDR + CIC_CTRL0, 232 (((0x3 << 4) | (1 << 2) | 1) << 16) | 233 (1 << 2) | 1 | 234 mmio_read_32(PARAM_ADDR + PARAM_FREQ_SELECT)); 235 while ((mmio_read_32(CIC_BASE_ADDR + CIC_STATUS0) & (1 << 2)) == 0) 236 continue; 237 238 ddr_set_pll(); 239 mmio_write_32(CIC_BASE_ADDR + CIC_CTRL0, 0x20002); 240 while ((mmio_read_32(CIC_BASE_ADDR + CIC_STATUS0) & (1 << 0)) == 0) 241 continue; 242 243 deidle_port(); 244 } 245