1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Low level PM code for TI EMIF
5 * Copyright (C) 2016-2017 Texas Instruments Incorporated - http://www.ti.com/
9 #include <linux/linkage.h>
10 #include <asm/assembler.h>
14 #include "ti-emif-asm-offsets.h"
16 #define EMIF_POWER_MGMT_WAIT_SELF_REFRESH_8192_CYCLES 0x00a0
17 #define EMIF_POWER_MGMT_SR_TIMER_MASK 0x00f0
18 #define EMIF_POWER_MGMT_SELF_REFRESH_MODE 0x0200
19 #define EMIF_POWER_MGMT_SELF_REFRESH_MODE_MASK 0x0700
21 #define EMIF_SDCFG_TYPE_DDR2 0x2 << SDRAM_TYPE_SHIFT
22 #define EMIF_SDCFG_TYPE_DDR3 0x3 << SDRAM_TYPE_SHIFT
23 #define EMIF_STATUS_READY 0x4
25 #define AM43XX_EMIF_PHY_CTRL_REG_COUNT 0x120
27 #define EMIF_AM437X_REGISTERS 0x1
36 * void ti_emif_save_context(void)
38 * Used during suspend to save the context of all required EMIF registers
39 * to local memory if the EMIF is going to lose context during the sleep
40 * transition. Operates on the VIRTUAL address of the EMIF.
42 ENTRY(ti_emif_save_context)
43 stmfd sp!, {r4 - r11, lr} @ save registers on stack
45 adr r4, ti_emif_pm_sram_data
46 ldr r0, [r4, #EMIF_PM_BASE_ADDR_VIRT_OFFSET]
47 ldr r2, [r4, #EMIF_PM_REGS_VIRT_OFFSET]
49 /* Save EMIF configuration */
50 ldr r1, [r0, #EMIF_SDRAM_CONFIG]
51 str r1, [r2, #EMIF_SDCFG_VAL_OFFSET]
53 ldr r1, [r0, #EMIF_SDRAM_REFRESH_CONTROL]
54 str r1, [r2, #EMIF_REF_CTRL_VAL_OFFSET]
56 ldr r1, [r0, #EMIF_SDRAM_TIMING_1]
57 str r1, [r2, #EMIF_TIMING1_VAL_OFFSET]
59 ldr r1, [r0, #EMIF_SDRAM_TIMING_2]
60 str r1, [r2, #EMIF_TIMING2_VAL_OFFSET]
62 ldr r1, [r0, #EMIF_SDRAM_TIMING_3]
63 str r1, [r2, #EMIF_TIMING3_VAL_OFFSET]
65 ldr r1, [r0, #EMIF_POWER_MANAGEMENT_CONTROL]
66 str r1, [r2, #EMIF_PMCR_VAL_OFFSET]
68 ldr r1, [r0, #EMIF_POWER_MANAGEMENT_CTRL_SHDW]
69 str r1, [r2, #EMIF_PMCR_SHDW_VAL_OFFSET]
71 ldr r1, [r0, #EMIF_SDRAM_OUTPUT_IMPEDANCE_CALIBRATION_CONFIG]
72 str r1, [r2, #EMIF_ZQCFG_VAL_OFFSET]
74 ldr r1, [r0, #EMIF_DDR_PHY_CTRL_1]
75 str r1, [r2, #EMIF_DDR_PHY_CTLR_1_OFFSET]
77 ldr r1, [r0, #EMIF_COS_CONFIG]
78 str r1, [r2, #EMIF_COS_CONFIG_OFFSET]
80 ldr r1, [r0, #EMIF_PRIORITY_TO_CLASS_OF_SERVICE_MAPPING]
81 str r1, [r2, #EMIF_PRIORITY_TO_COS_MAPPING_OFFSET]
83 ldr r1, [r0, #EMIF_CONNECTION_ID_TO_CLASS_OF_SERVICE_1_MAPPING]
84 str r1, [r2, #EMIF_CONNECT_ID_SERV_1_MAP_OFFSET]
86 ldr r1, [r0, #EMIF_CONNECTION_ID_TO_CLASS_OF_SERVICE_2_MAPPING]
87 str r1, [r2, #EMIF_CONNECT_ID_SERV_2_MAP_OFFSET]
89 ldr r1, [r0, #EMIF_OCP_CONFIG]
90 str r1, [r2, #EMIF_OCP_CONFIG_VAL_OFFSET]
92 ldr r5, [r4, #EMIF_PM_CONFIG_OFFSET]
93 cmp r5, #EMIF_SRAM_AM43_REG_LAYOUT
94 bne emif_skip_save_extra_regs
96 ldr r1, [r0, #EMIF_READ_WRITE_LEVELING_RAMP_CONTROL]
97 str r1, [r2, #EMIF_RD_WR_LEVEL_RAMP_CTRL_OFFSET]
99 ldr r1, [r0, #EMIF_READ_WRITE_EXECUTION_THRESHOLD]
100 str r1, [r2, #EMIF_RD_WR_EXEC_THRESH_OFFSET]
102 ldr r1, [r0, #EMIF_LPDDR2_NVM_TIMING]
103 str r1, [r2, #EMIF_LPDDR2_NVM_TIM_OFFSET]
105 ldr r1, [r0, #EMIF_LPDDR2_NVM_TIMING_SHDW]
106 str r1, [r2, #EMIF_LPDDR2_NVM_TIM_SHDW_OFFSET]
108 ldr r1, [r0, #EMIF_DLL_CALIB_CTRL]
109 str r1, [r2, #EMIF_DLL_CALIB_CTRL_VAL_OFFSET]
111 ldr r1, [r0, #EMIF_DLL_CALIB_CTRL_SHDW]
112 str r1, [r2, #EMIF_DLL_CALIB_CTRL_VAL_SHDW_OFFSET]
114 /* Loop and save entire block of emif phy regs */
116 add r4, r2, #EMIF_EXT_PHY_CTRL_VALS_OFFSET
117 add r3, r0, #EMIF_EXT_PHY_CTRL_1
122 cmp r5, #AM43XX_EMIF_PHY_CTRL_REG_COUNT
123 bne ddr_phy_ctrl_save
125 emif_skip_save_extra_regs:
126 ldmfd sp!, {r4 - r11, pc} @ restore regs and return
127 ENDPROC(ti_emif_save_context)
130 * void ti_emif_restore_context(void)
132 * Used during resume to restore the context of all required EMIF registers
133 * from local memory after the EMIF has lost context during a sleep transition.
134 * Operates on the PHYSICAL address of the EMIF.
136 ENTRY(ti_emif_restore_context)
137 adr r4, ti_emif_pm_sram_data
138 ldr r0, [r4, #EMIF_PM_BASE_ADDR_PHYS_OFFSET]
139 ldr r2, [r4, #EMIF_PM_REGS_PHYS_OFFSET]
141 /* Config EMIF Timings */
142 ldr r1, [r2, #EMIF_DDR_PHY_CTLR_1_OFFSET]
143 str r1, [r0, #EMIF_DDR_PHY_CTRL_1]
144 str r1, [r0, #EMIF_DDR_PHY_CTRL_1_SHDW]
146 ldr r1, [r2, #EMIF_TIMING1_VAL_OFFSET]
147 str r1, [r0, #EMIF_SDRAM_TIMING_1]
148 str r1, [r0, #EMIF_SDRAM_TIMING_1_SHDW]
150 ldr r1, [r2, #EMIF_TIMING2_VAL_OFFSET]
151 str r1, [r0, #EMIF_SDRAM_TIMING_2]
152 str r1, [r0, #EMIF_SDRAM_TIMING_2_SHDW]
154 ldr r1, [r2, #EMIF_TIMING3_VAL_OFFSET]
155 str r1, [r0, #EMIF_SDRAM_TIMING_3]
156 str r1, [r0, #EMIF_SDRAM_TIMING_3_SHDW]
158 ldr r1, [r2, #EMIF_REF_CTRL_VAL_OFFSET]
159 str r1, [r0, #EMIF_SDRAM_REFRESH_CONTROL]
160 str r1, [r0, #EMIF_SDRAM_REFRESH_CTRL_SHDW]
162 ldr r1, [r2, #EMIF_PMCR_VAL_OFFSET]
163 str r1, [r0, #EMIF_POWER_MANAGEMENT_CONTROL]
165 ldr r1, [r2, #EMIF_PMCR_SHDW_VAL_OFFSET]
166 str r1, [r0, #EMIF_POWER_MANAGEMENT_CTRL_SHDW]
168 ldr r1, [r2, #EMIF_COS_CONFIG_OFFSET]
169 str r1, [r0, #EMIF_COS_CONFIG]
171 ldr r1, [r2, #EMIF_PRIORITY_TO_COS_MAPPING_OFFSET]
172 str r1, [r0, #EMIF_PRIORITY_TO_CLASS_OF_SERVICE_MAPPING]
174 ldr r1, [r2, #EMIF_CONNECT_ID_SERV_1_MAP_OFFSET]
175 str r1, [r0, #EMIF_CONNECTION_ID_TO_CLASS_OF_SERVICE_1_MAPPING]
177 ldr r1, [r2, #EMIF_CONNECT_ID_SERV_2_MAP_OFFSET]
178 str r1, [r0, #EMIF_CONNECTION_ID_TO_CLASS_OF_SERVICE_2_MAPPING]
180 ldr r1, [r2, #EMIF_OCP_CONFIG_VAL_OFFSET]
181 str r1, [r0, #EMIF_OCP_CONFIG]
183 ldr r5, [r4, #EMIF_PM_CONFIG_OFFSET]
184 cmp r5, #EMIF_SRAM_AM43_REG_LAYOUT
185 bne emif_skip_restore_extra_regs
187 ldr r1, [r2, #EMIF_RD_WR_LEVEL_RAMP_CTRL_OFFSET]
188 str r1, [r0, #EMIF_READ_WRITE_LEVELING_RAMP_CONTROL]
190 ldr r1, [r2, #EMIF_RD_WR_EXEC_THRESH_OFFSET]
191 str r1, [r0, #EMIF_READ_WRITE_EXECUTION_THRESHOLD]
193 ldr r1, [r2, #EMIF_LPDDR2_NVM_TIM_OFFSET]
194 str r1, [r0, #EMIF_LPDDR2_NVM_TIMING]
196 ldr r1, [r2, #EMIF_LPDDR2_NVM_TIM_SHDW_OFFSET]
197 str r1, [r0, #EMIF_LPDDR2_NVM_TIMING_SHDW]
199 ldr r1, [r2, #EMIF_DLL_CALIB_CTRL_VAL_OFFSET]
200 str r1, [r0, #EMIF_DLL_CALIB_CTRL]
202 ldr r1, [r2, #EMIF_DLL_CALIB_CTRL_VAL_SHDW_OFFSET]
203 str r1, [r0, #EMIF_DLL_CALIB_CTRL_SHDW]
205 ldr r1, [r2, #EMIF_ZQCFG_VAL_OFFSET]
206 str r1, [r0, #EMIF_SDRAM_OUTPUT_IMPEDANCE_CALIBRATION_CONFIG]
208 /* Loop and restore entire block of emif phy regs */
210 /* Load ti_emif_regs_amx3 + EMIF_EXT_PHY_CTRL_VALS_OFFSET for address
211 * to phy register save space
213 add r3, r2, #EMIF_EXT_PHY_CTRL_VALS_OFFSET
214 add r4, r0, #EMIF_EXT_PHY_CTRL_1
215 ddr_phy_ctrl_restore:
219 cmp r5, #AM43XX_EMIF_PHY_CTRL_REG_COUNT
220 bne ddr_phy_ctrl_restore
222 emif_skip_restore_extra_regs:
224 * Output impedence calib needed only for DDR3
225 * but since the initial state of this will be
226 * disabled for DDR2 no harm in restoring the
229 ldr r1, [r2, #EMIF_ZQCFG_VAL_OFFSET]
230 str r1, [r0, #EMIF_SDRAM_OUTPUT_IMPEDANCE_CALIBRATION_CONFIG]
232 /* Write to sdcfg last for DDR2 only */
233 ldr r1, [r2, #EMIF_SDCFG_VAL_OFFSET]
234 and r2, r1, #SDRAM_TYPE_MASK
235 cmp r2, #EMIF_SDCFG_TYPE_DDR2
236 streq r1, [r0, #EMIF_SDRAM_CONFIG]
239 ENDPROC(ti_emif_restore_context)
242 * void ti_emif_run_hw_leveling(void)
244 * Used during resume to run hardware leveling again and restore the
245 * configuration of the EMIF PHY, only for DDR3.
247 ENTRY(ti_emif_run_hw_leveling)
248 adr r4, ti_emif_pm_sram_data
249 ldr r0, [r4, #EMIF_PM_BASE_ADDR_PHYS_OFFSET]
251 ldr r3, [r0, #EMIF_READ_WRITE_LEVELING_CONTROL]
252 orr r3, r3, #RDWRLVLFULL_START
253 ldr r2, [r0, #EMIF_SDRAM_CONFIG]
254 and r2, r2, #SDRAM_TYPE_MASK
255 cmp r2, #EMIF_SDCFG_TYPE_DDR3
258 str r3, [r0, #EMIF_READ_WRITE_LEVELING_CONTROL]
261 * If EMIF registers are touched during initial stage of HW
262 * leveling sequence there will be an L3 NOC timeout error issued
263 * as the EMIF will not respond, which is not fatal, but it is
264 * avoidable. This small wait loop is enough time for this condition
265 * to clear, even at worst case of CPU running at max speed of 1Ghz.
272 /* Bit clears when operation is complete */
273 2: ldr r1, [r0, #EMIF_READ_WRITE_LEVELING_CONTROL]
274 tst r1, #RDWRLVLFULL_START
279 ENDPROC(ti_emif_run_hw_leveling)
282 * void ti_emif_enter_sr(void)
284 * Programs the EMIF to tell the SDRAM to enter into self-refresh
285 * mode during a sleep transition. Operates on the VIRTUAL address
288 ENTRY(ti_emif_enter_sr)
289 stmfd sp!, {r4 - r11, lr} @ save registers on stack
291 adr r4, ti_emif_pm_sram_data
292 ldr r0, [r4, #EMIF_PM_BASE_ADDR_VIRT_OFFSET]
293 ldr r2, [r4, #EMIF_PM_REGS_VIRT_OFFSET]
295 ldr r1, [r0, #EMIF_POWER_MANAGEMENT_CONTROL]
296 bic r1, r1, #EMIF_POWER_MGMT_SELF_REFRESH_MODE_MASK
297 orr r1, r1, #EMIF_POWER_MGMT_SELF_REFRESH_MODE
298 str r1, [r0, #EMIF_POWER_MANAGEMENT_CONTROL]
300 ldmfd sp!, {r4 - r11, pc} @ restore regs and return
301 ENDPROC(ti_emif_enter_sr)
304 * void ti_emif_exit_sr(void)
306 * Programs the EMIF to tell the SDRAM to exit self-refresh mode
307 * after a sleep transition. Operates on the PHYSICAL address of
310 ENTRY(ti_emif_exit_sr)
311 adr r4, ti_emif_pm_sram_data
312 ldr r0, [r4, #EMIF_PM_BASE_ADDR_PHYS_OFFSET]
313 ldr r2, [r4, #EMIF_PM_REGS_PHYS_OFFSET]
316 * Toggle EMIF to exit refresh mode:
317 * if EMIF lost context, PWR_MGT_CTRL is currently 0, writing disable
318 * (0x0), wont do diddly squat! so do a toggle from SR(0x2) to disable
320 * *If* EMIF did not lose context, nothing broken as we write the same
321 * value(0x2) to reg before we write a disable (0x0).
323 ldr r1, [r2, #EMIF_PMCR_VAL_OFFSET]
324 bic r1, r1, #EMIF_POWER_MGMT_SELF_REFRESH_MODE_MASK
325 orr r1, r1, #EMIF_POWER_MGMT_SELF_REFRESH_MODE
326 str r1, [r0, #EMIF_POWER_MANAGEMENT_CONTROL]
327 bic r1, r1, #EMIF_POWER_MGMT_SELF_REFRESH_MODE_MASK
328 str r1, [r0, #EMIF_POWER_MANAGEMENT_CONTROL]
330 /* Wait for EMIF to become ready */
331 1: ldr r1, [r0, #EMIF_STATUS]
332 tst r1, #EMIF_STATUS_READY
336 ENDPROC(ti_emif_exit_sr)
339 * void ti_emif_abort_sr(void)
341 * Disables self-refresh after a failed transition to a low-power
342 * state so the kernel can jump back to DDR and follow abort path.
343 * Operates on the VIRTUAL address of the EMIF.
345 ENTRY(ti_emif_abort_sr)
346 stmfd sp!, {r4 - r11, lr} @ save registers on stack
348 adr r4, ti_emif_pm_sram_data
349 ldr r0, [r4, #EMIF_PM_BASE_ADDR_VIRT_OFFSET]
350 ldr r2, [r4, #EMIF_PM_REGS_VIRT_OFFSET]
352 ldr r1, [r2, #EMIF_PMCR_VAL_OFFSET]
353 bic r1, r1, #EMIF_POWER_MGMT_SELF_REFRESH_MODE_MASK
354 str r1, [r0, #EMIF_POWER_MANAGEMENT_CONTROL]
356 /* Wait for EMIF to become ready */
357 1: ldr r1, [r0, #EMIF_STATUS]
358 tst r1, #EMIF_STATUS_READY
361 ldmfd sp!, {r4 - r11, pc} @ restore regs and return
362 ENDPROC(ti_emif_abort_sr)
365 ENTRY(ti_emif_pm_sram_data)
366 .space EMIF_PM_DATA_SIZE
367 ENTRY(ti_emif_sram_sz)
368 .word . - ti_emif_save_context