18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * arch/arm/mach-lpc32xx/suspend.S
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Original authors: Dmitry Chigirev, Vitaly Wool <source@mvista.com>
58c2ecf20Sopenharmony_ci * Modified by Kevin Wells <kevin.wells@nxp.com>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * 2005 (c) MontaVista Software, Inc. This file is licensed under
88c2ecf20Sopenharmony_ci * the terms of the GNU General Public License version 2. This program
98c2ecf20Sopenharmony_ci * is licensed "as is" without any warranty of any kind, whether express
108c2ecf20Sopenharmony_ci * or implied.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci#include <linux/linkage.h>
138c2ecf20Sopenharmony_ci#include <asm/assembler.h>
148c2ecf20Sopenharmony_ci#include "lpc32xx.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/* Using named register defines makes the code easier to follow */
178c2ecf20Sopenharmony_ci#define WORK1_REG			r0
188c2ecf20Sopenharmony_ci#define WORK2_REG			r1
198c2ecf20Sopenharmony_ci#define SAVED_HCLK_DIV_REG		r2
208c2ecf20Sopenharmony_ci#define SAVED_HCLK_PLL_REG		r3
218c2ecf20Sopenharmony_ci#define SAVED_DRAM_CLKCTRL_REG		r4
228c2ecf20Sopenharmony_ci#define SAVED_PWR_CTRL_REG		r5
238c2ecf20Sopenharmony_ci#define CLKPWRBASE_REG			r6
248c2ecf20Sopenharmony_ci#define EMCBASE_REG			r7
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define LPC32XX_EMC_STATUS_OFFS		0x04
278c2ecf20Sopenharmony_ci#define LPC32XX_EMC_STATUS_BUSY		0x1
288c2ecf20Sopenharmony_ci#define LPC32XX_EMC_STATUS_SELF_RFSH	0x4
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define LPC32XX_CLKPWR_PWR_CTRL_OFFS	0x44
318c2ecf20Sopenharmony_ci#define LPC32XX_CLKPWR_HCLK_DIV_OFFS	0x40
328c2ecf20Sopenharmony_ci#define LPC32XX_CLKPWR_HCLKPLL_CTRL_OFFS 0x58
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define CLKPWR_PCLK_DIV_MASK		0xFFFFFE7F
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	.text
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ciENTRY(lpc32xx_sys_suspend)
398c2ecf20Sopenharmony_ci	@ Save a copy of the used registers in IRAM, r0 is corrupted
408c2ecf20Sopenharmony_ci	adr	r0, tmp_stack_end
418c2ecf20Sopenharmony_ci	stmfd	r0!, {r3 - r7, sp, lr}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	@ Load a few common register addresses
448c2ecf20Sopenharmony_ci	adr	WORK1_REG, reg_bases
458c2ecf20Sopenharmony_ci	ldr	CLKPWRBASE_REG, [WORK1_REG, #0]
468c2ecf20Sopenharmony_ci	ldr	EMCBASE_REG, [WORK1_REG, #4]
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	ldr	SAVED_PWR_CTRL_REG, [CLKPWRBASE_REG,\
498c2ecf20Sopenharmony_ci		#LPC32XX_CLKPWR_PWR_CTRL_OFFS]
508c2ecf20Sopenharmony_ci	orr	WORK1_REG, SAVED_PWR_CTRL_REG, #LPC32XX_CLKPWR_SDRAM_SELF_RFSH
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	@ Wait for SDRAM busy status to go busy and then idle
538c2ecf20Sopenharmony_ci	@ This guarantees a small windows where DRAM isn't busy
548c2ecf20Sopenharmony_ci1:
558c2ecf20Sopenharmony_ci	ldr	WORK2_REG, [EMCBASE_REG, #LPC32XX_EMC_STATUS_OFFS]
568c2ecf20Sopenharmony_ci	and	WORK2_REG, WORK2_REG, #LPC32XX_EMC_STATUS_BUSY
578c2ecf20Sopenharmony_ci	cmp	WORK2_REG, #LPC32XX_EMC_STATUS_BUSY
588c2ecf20Sopenharmony_ci	bne	1b @ Branch while idle
598c2ecf20Sopenharmony_ci2:
608c2ecf20Sopenharmony_ci	ldr	WORK2_REG, [EMCBASE_REG, #LPC32XX_EMC_STATUS_OFFS]
618c2ecf20Sopenharmony_ci	and	WORK2_REG, WORK2_REG, #LPC32XX_EMC_STATUS_BUSY
628c2ecf20Sopenharmony_ci	cmp	WORK2_REG, #LPC32XX_EMC_STATUS_BUSY
638c2ecf20Sopenharmony_ci	beq	2b @ Branch until idle
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	@ Setup self-refresh with support for manual exit of
668c2ecf20Sopenharmony_ci	@ self-refresh mode
678c2ecf20Sopenharmony_ci	str	WORK1_REG, [CLKPWRBASE_REG, #LPC32XX_CLKPWR_PWR_CTRL_OFFS]
688c2ecf20Sopenharmony_ci	orr	WORK2_REG, WORK1_REG, #LPC32XX_CLKPWR_UPD_SDRAM_SELF_RFSH
698c2ecf20Sopenharmony_ci	str	WORK2_REG, [CLKPWRBASE_REG, #LPC32XX_CLKPWR_PWR_CTRL_OFFS]
708c2ecf20Sopenharmony_ci	str	WORK1_REG, [CLKPWRBASE_REG, #LPC32XX_CLKPWR_PWR_CTRL_OFFS]
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	@ Wait for self-refresh acknowledge, clocks to the DRAM device
738c2ecf20Sopenharmony_ci	@ will automatically stop on start of self-refresh
748c2ecf20Sopenharmony_ci3:
758c2ecf20Sopenharmony_ci	ldr	WORK2_REG, [EMCBASE_REG, #LPC32XX_EMC_STATUS_OFFS]
768c2ecf20Sopenharmony_ci	and	WORK2_REG, WORK2_REG, #LPC32XX_EMC_STATUS_SELF_RFSH
778c2ecf20Sopenharmony_ci	cmp	WORK2_REG, #LPC32XX_EMC_STATUS_SELF_RFSH
788c2ecf20Sopenharmony_ci	bne	3b @ Branch until self-refresh mode starts
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	@ Enter direct-run mode from run mode
818c2ecf20Sopenharmony_ci	bic	WORK1_REG, WORK1_REG, #LPC32XX_CLKPWR_SELECT_RUN_MODE
828c2ecf20Sopenharmony_ci	str	WORK1_REG, [CLKPWRBASE_REG, #LPC32XX_CLKPWR_PWR_CTRL_OFFS]
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	@ Safe disable of DRAM clock in EMC block, prevents DDR sync
858c2ecf20Sopenharmony_ci	@ issues on restart
868c2ecf20Sopenharmony_ci	ldr	SAVED_HCLK_DIV_REG, [CLKPWRBASE_REG,\
878c2ecf20Sopenharmony_ci		#LPC32XX_CLKPWR_HCLK_DIV_OFFS]
888c2ecf20Sopenharmony_ci	and	WORK2_REG, SAVED_HCLK_DIV_REG, #CLKPWR_PCLK_DIV_MASK
898c2ecf20Sopenharmony_ci	str	WORK2_REG, [CLKPWRBASE_REG, #LPC32XX_CLKPWR_HCLK_DIV_OFFS]
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	@ Save HCLK PLL state and disable HCLK PLL
928c2ecf20Sopenharmony_ci	ldr	SAVED_HCLK_PLL_REG, [CLKPWRBASE_REG,\
938c2ecf20Sopenharmony_ci		#LPC32XX_CLKPWR_HCLKPLL_CTRL_OFFS]
948c2ecf20Sopenharmony_ci	bic	WORK2_REG, SAVED_HCLK_PLL_REG, #LPC32XX_CLKPWR_HCLKPLL_POWER_UP
958c2ecf20Sopenharmony_ci	str	WORK2_REG, [CLKPWRBASE_REG, #LPC32XX_CLKPWR_HCLKPLL_CTRL_OFFS]
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	@ Enter stop mode until an enabled event occurs
988c2ecf20Sopenharmony_ci	orr	WORK1_REG, WORK1_REG, #LPC32XX_CLKPWR_STOP_MODE_CTRL
998c2ecf20Sopenharmony_ci	str	WORK1_REG, [CLKPWRBASE_REG, #LPC32XX_CLKPWR_PWR_CTRL_OFFS]
1008c2ecf20Sopenharmony_ci	.rept 9
1018c2ecf20Sopenharmony_ci	nop
1028c2ecf20Sopenharmony_ci	.endr
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	@ Clear stop status
1058c2ecf20Sopenharmony_ci	bic	WORK1_REG, WORK1_REG, #LPC32XX_CLKPWR_STOP_MODE_CTRL
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	@ Restore original HCLK PLL value and wait for PLL lock
1088c2ecf20Sopenharmony_ci	str	SAVED_HCLK_PLL_REG, [CLKPWRBASE_REG,\
1098c2ecf20Sopenharmony_ci		#LPC32XX_CLKPWR_HCLKPLL_CTRL_OFFS]
1108c2ecf20Sopenharmony_ci4:
1118c2ecf20Sopenharmony_ci	ldr	WORK2_REG, [CLKPWRBASE_REG, #LPC32XX_CLKPWR_HCLKPLL_CTRL_OFFS]
1128c2ecf20Sopenharmony_ci	and	WORK2_REG, WORK2_REG, #LPC32XX_CLKPWR_HCLKPLL_PLL_STS
1138c2ecf20Sopenharmony_ci	bne	4b
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	@ Re-enter run mode with self-refresh flag cleared, but no DRAM
1168c2ecf20Sopenharmony_ci	@ update yet. DRAM is still in self-refresh
1178c2ecf20Sopenharmony_ci	str	SAVED_PWR_CTRL_REG, [CLKPWRBASE_REG,\
1188c2ecf20Sopenharmony_ci		#LPC32XX_CLKPWR_PWR_CTRL_OFFS]
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	@ Restore original DRAM clock mode to restore DRAM clocks
1218c2ecf20Sopenharmony_ci	str	SAVED_HCLK_DIV_REG, [CLKPWRBASE_REG,\
1228c2ecf20Sopenharmony_ci		#LPC32XX_CLKPWR_HCLK_DIV_OFFS]
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	@ Clear self-refresh mode
1258c2ecf20Sopenharmony_ci	orr	WORK1_REG, SAVED_PWR_CTRL_REG,\
1268c2ecf20Sopenharmony_ci		#LPC32XX_CLKPWR_UPD_SDRAM_SELF_RFSH
1278c2ecf20Sopenharmony_ci	str	WORK1_REG, [CLKPWRBASE_REG, #LPC32XX_CLKPWR_PWR_CTRL_OFFS]
1288c2ecf20Sopenharmony_ci	str	SAVED_PWR_CTRL_REG, [CLKPWRBASE_REG,\
1298c2ecf20Sopenharmony_ci		#LPC32XX_CLKPWR_PWR_CTRL_OFFS]
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	@ Wait for EMC to clear self-refresh mode
1328c2ecf20Sopenharmony_ci5:
1338c2ecf20Sopenharmony_ci	ldr	WORK2_REG, [EMCBASE_REG, #LPC32XX_EMC_STATUS_OFFS]
1348c2ecf20Sopenharmony_ci	and	WORK2_REG, WORK2_REG, #LPC32XX_EMC_STATUS_SELF_RFSH
1358c2ecf20Sopenharmony_ci	bne	5b @ Branch until self-refresh has exited
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	@ restore regs and return
1388c2ecf20Sopenharmony_ci	adr	r0, tmp_stack
1398c2ecf20Sopenharmony_ci	ldmfd	r0!, {r3 - r7, sp, pc}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cireg_bases:
1428c2ecf20Sopenharmony_ci	.long	IO_ADDRESS(LPC32XX_CLK_PM_BASE)
1438c2ecf20Sopenharmony_ci	.long	IO_ADDRESS(LPC32XX_EMC_BASE)
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_citmp_stack:
1468c2ecf20Sopenharmony_ci	.long	0, 0, 0, 0, 0, 0, 0
1478c2ecf20Sopenharmony_citmp_stack_end:
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ciENTRY(lpc32xx_sys_suspend_sz)
1508c2ecf20Sopenharmony_ci	.word	. - lpc32xx_sys_suspend
151