18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright 2002 Embedded Edge, LLC
48c2ecf20Sopenharmony_ci * Author: dan@embeddededge.com
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Sleep helper for Au1xxx sleep mode.
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <asm/asm.h>
108c2ecf20Sopenharmony_ci#include <asm/mipsregs.h>
118c2ecf20Sopenharmony_ci#include <asm/regdef.h>
128c2ecf20Sopenharmony_ci#include <asm/stackframe.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci	.extern __flush_cache_all
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci	.text
178c2ecf20Sopenharmony_ci	.set noreorder
188c2ecf20Sopenharmony_ci	.set noat
198c2ecf20Sopenharmony_ci	.align	5
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/* preparatory stuff */
238c2ecf20Sopenharmony_ci.macro	SETUP_SLEEP
248c2ecf20Sopenharmony_ci	subu	sp, PT_SIZE
258c2ecf20Sopenharmony_ci	sw	$1, PT_R1(sp)
268c2ecf20Sopenharmony_ci	sw	$2, PT_R2(sp)
278c2ecf20Sopenharmony_ci	sw	$3, PT_R3(sp)
288c2ecf20Sopenharmony_ci	sw	$4, PT_R4(sp)
298c2ecf20Sopenharmony_ci	sw	$5, PT_R5(sp)
308c2ecf20Sopenharmony_ci	sw	$6, PT_R6(sp)
318c2ecf20Sopenharmony_ci	sw	$7, PT_R7(sp)
328c2ecf20Sopenharmony_ci	sw	$16, PT_R16(sp)
338c2ecf20Sopenharmony_ci	sw	$17, PT_R17(sp)
348c2ecf20Sopenharmony_ci	sw	$18, PT_R18(sp)
358c2ecf20Sopenharmony_ci	sw	$19, PT_R19(sp)
368c2ecf20Sopenharmony_ci	sw	$20, PT_R20(sp)
378c2ecf20Sopenharmony_ci	sw	$21, PT_R21(sp)
388c2ecf20Sopenharmony_ci	sw	$22, PT_R22(sp)
398c2ecf20Sopenharmony_ci	sw	$23, PT_R23(sp)
408c2ecf20Sopenharmony_ci	sw	$26, PT_R26(sp)
418c2ecf20Sopenharmony_ci	sw	$27, PT_R27(sp)
428c2ecf20Sopenharmony_ci	sw	$28, PT_R28(sp)
438c2ecf20Sopenharmony_ci	sw	$30, PT_R30(sp)
448c2ecf20Sopenharmony_ci	sw	$31, PT_R31(sp)
458c2ecf20Sopenharmony_ci	mfc0	k0, CP0_STATUS
468c2ecf20Sopenharmony_ci	sw	k0, 0x20(sp)
478c2ecf20Sopenharmony_ci	mfc0	k0, CP0_CONTEXT
488c2ecf20Sopenharmony_ci	sw	k0, 0x1c(sp)
498c2ecf20Sopenharmony_ci	mfc0	k0, CP0_PAGEMASK
508c2ecf20Sopenharmony_ci	sw	k0, 0x18(sp)
518c2ecf20Sopenharmony_ci	mfc0	k0, CP0_CONFIG
528c2ecf20Sopenharmony_ci	sw	k0, 0x14(sp)
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	/* flush caches to make sure context is in memory */
558c2ecf20Sopenharmony_ci	la	t1, __flush_cache_all
568c2ecf20Sopenharmony_ci	lw	t0, 0(t1)
578c2ecf20Sopenharmony_ci	jalr	t0
588c2ecf20Sopenharmony_ci	 nop
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	/* Now set up the scratch registers so the boot rom will
618c2ecf20Sopenharmony_ci	 * return to this point upon wakeup.
628c2ecf20Sopenharmony_ci	 * sys_scratch0 : SP
638c2ecf20Sopenharmony_ci	 * sys_scratch1 : RA
648c2ecf20Sopenharmony_ci	 */
658c2ecf20Sopenharmony_ci	lui	t3, 0xb190		/* sys_xxx */
668c2ecf20Sopenharmony_ci	sw	sp, 0x0018(t3)
678c2ecf20Sopenharmony_ci	la	k0, alchemy_sleep_wakeup	/* resume path */
688c2ecf20Sopenharmony_ci	sw	k0, 0x001c(t3)
698c2ecf20Sopenharmony_ci.endm
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci.macro	DO_SLEEP
728c2ecf20Sopenharmony_ci	/* put power supply and processor to sleep */
738c2ecf20Sopenharmony_ci	sw	zero, 0x0078(t3)	/* sys_slppwr */
748c2ecf20Sopenharmony_ci	sync
758c2ecf20Sopenharmony_ci	sw	zero, 0x007c(t3)	/* sys_sleep */
768c2ecf20Sopenharmony_ci	sync
778c2ecf20Sopenharmony_ci	nop
788c2ecf20Sopenharmony_ci	nop
798c2ecf20Sopenharmony_ci	nop
808c2ecf20Sopenharmony_ci	nop
818c2ecf20Sopenharmony_ci	nop
828c2ecf20Sopenharmony_ci	nop
838c2ecf20Sopenharmony_ci	nop
848c2ecf20Sopenharmony_ci	nop
858c2ecf20Sopenharmony_ci.endm
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/* sleep code for Au1000/Au1100/Au1500 memory controller type */
888c2ecf20Sopenharmony_ciLEAF(alchemy_sleep_au1000)
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	SETUP_SLEEP
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	/* cache following instructions, as memory gets put to sleep */
938c2ecf20Sopenharmony_ci	la	t0, 1f
948c2ecf20Sopenharmony_ci	.set	arch=r4000
958c2ecf20Sopenharmony_ci	cache	0x14, 0(t0)
968c2ecf20Sopenharmony_ci	cache	0x14, 32(t0)
978c2ecf20Sopenharmony_ci	cache	0x14, 64(t0)
988c2ecf20Sopenharmony_ci	cache	0x14, 96(t0)
998c2ecf20Sopenharmony_ci	.set	mips0
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci1:	lui	a0, 0xb400		/* mem_xxx */
1028c2ecf20Sopenharmony_ci	sw	zero, 0x001c(a0)	/* Precharge */
1038c2ecf20Sopenharmony_ci	sync
1048c2ecf20Sopenharmony_ci	sw	zero, 0x0020(a0)	/* Auto Refresh */
1058c2ecf20Sopenharmony_ci	sync
1068c2ecf20Sopenharmony_ci	sw	zero, 0x0030(a0)	/* Sleep */
1078c2ecf20Sopenharmony_ci	sync
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	DO_SLEEP
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ciEND(alchemy_sleep_au1000)
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci/* sleep code for Au1550/Au1200 memory controller type */
1148c2ecf20Sopenharmony_ciLEAF(alchemy_sleep_au1550)
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	SETUP_SLEEP
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	/* cache following instructions, as memory gets put to sleep */
1198c2ecf20Sopenharmony_ci	la	t0, 1f
1208c2ecf20Sopenharmony_ci	.set	arch=r4000
1218c2ecf20Sopenharmony_ci	cache	0x14, 0(t0)
1228c2ecf20Sopenharmony_ci	cache	0x14, 32(t0)
1238c2ecf20Sopenharmony_ci	cache	0x14, 64(t0)
1248c2ecf20Sopenharmony_ci	cache	0x14, 96(t0)
1258c2ecf20Sopenharmony_ci	.set	mips0
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci1:	lui	a0, 0xb400		/* mem_xxx */
1288c2ecf20Sopenharmony_ci	sw	zero, 0x08c0(a0)	/* Precharge */
1298c2ecf20Sopenharmony_ci	sync
1308c2ecf20Sopenharmony_ci	sw	zero, 0x08d0(a0)	/* Self Refresh */
1318c2ecf20Sopenharmony_ci	sync
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	/* wait for sdram to enter self-refresh mode */
1348c2ecf20Sopenharmony_ci	lui	t0, 0x0100
1358c2ecf20Sopenharmony_ci2:	lw	t1, 0x0850(a0)		/* mem_sdstat */
1368c2ecf20Sopenharmony_ci	and	t2, t1, t0
1378c2ecf20Sopenharmony_ci	beq	t2, zero, 2b
1388c2ecf20Sopenharmony_ci	 nop
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	/* disable SDRAM clocks */
1418c2ecf20Sopenharmony_ci	lui	t0, 0xcfff
1428c2ecf20Sopenharmony_ci	ori	t0, t0, 0xffff
1438c2ecf20Sopenharmony_ci	lw	t1, 0x0840(a0)		/* mem_sdconfiga */
1448c2ecf20Sopenharmony_ci	and	t1, t0, t1		/* clear CE[1:0] */
1458c2ecf20Sopenharmony_ci	sw	t1, 0x0840(a0)		/* mem_sdconfiga */
1468c2ecf20Sopenharmony_ci	sync
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	DO_SLEEP
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ciEND(alchemy_sleep_au1550)
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci/* sleepcode for Au1300 memory controller type */
1538c2ecf20Sopenharmony_ciLEAF(alchemy_sleep_au1300)
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	SETUP_SLEEP
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	/* cache following instructions, as memory gets put to sleep */
1588c2ecf20Sopenharmony_ci	la	t0, 2f
1598c2ecf20Sopenharmony_ci	la	t1, 4f
1608c2ecf20Sopenharmony_ci	subu	t2, t1, t0
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	.set	arch=r4000
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci1:	cache	0x14, 0(t0)
1658c2ecf20Sopenharmony_ci	subu	t2, t2, 32
1668c2ecf20Sopenharmony_ci	bgez	t2, 1b
1678c2ecf20Sopenharmony_ci	 addu	t0, t0, 32
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	.set	mips0
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci2:	lui	a0, 0xb400		/* mem_xxx */
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	/* disable all ports in mem_sdportcfga */
1748c2ecf20Sopenharmony_ci	sw	zero, 0x868(a0)		/* mem_sdportcfga */
1758c2ecf20Sopenharmony_ci	sync
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	/* disable ODT */
1788c2ecf20Sopenharmony_ci	li	t0, 0x03010000
1798c2ecf20Sopenharmony_ci	sw	t0, 0x08d8(a0)		/* mem_sdcmd0 */
1808c2ecf20Sopenharmony_ci	sw	t0, 0x08dc(a0)		/* mem_sdcmd1 */
1818c2ecf20Sopenharmony_ci	sync
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	/* precharge */
1848c2ecf20Sopenharmony_ci	li	t0, 0x23000400
1858c2ecf20Sopenharmony_ci	sw	t0, 0x08dc(a0)		/* mem_sdcmd1 */
1868c2ecf20Sopenharmony_ci	sw	t0, 0x08d8(a0)		/* mem_sdcmd0 */
1878c2ecf20Sopenharmony_ci	sync
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	/* auto refresh */
1908c2ecf20Sopenharmony_ci	sw	zero, 0x08c8(a0)	/* mem_sdautoref */
1918c2ecf20Sopenharmony_ci	sync
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	/* block access to the DDR */
1948c2ecf20Sopenharmony_ci	lw	t0, 0x0848(a0)		/* mem_sdconfigb */
1958c2ecf20Sopenharmony_ci	li	t1, (1 << 7 | 0x3F)
1968c2ecf20Sopenharmony_ci	or	t0, t0, t1
1978c2ecf20Sopenharmony_ci	sw	t0, 0x0848(a0)		/* mem_sdconfigb */
1988c2ecf20Sopenharmony_ci	sync
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	/* issue the Self Refresh command */
2018c2ecf20Sopenharmony_ci	li	t0, 0x10000000
2028c2ecf20Sopenharmony_ci	sw	t0, 0x08dc(a0)		/* mem_sdcmd1 */
2038c2ecf20Sopenharmony_ci	sw	t0, 0x08d8(a0)		/* mem_sdcmd0 */
2048c2ecf20Sopenharmony_ci	sync
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	/* wait for sdram to enter self-refresh mode */
2078c2ecf20Sopenharmony_ci	lui	t0, 0x0300
2088c2ecf20Sopenharmony_ci3:	lw	t1, 0x0850(a0)		/* mem_sdstat */
2098c2ecf20Sopenharmony_ci	and	t2, t1, t0
2108c2ecf20Sopenharmony_ci	bne	t2, t0, 3b
2118c2ecf20Sopenharmony_ci	 nop
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	/* disable SDRAM clocks */
2148c2ecf20Sopenharmony_ci	li	t0, ~(3<<28)
2158c2ecf20Sopenharmony_ci	lw	t1, 0x0840(a0)		/* mem_sdconfiga */
2168c2ecf20Sopenharmony_ci	and	t1, t1, t0		/* clear CE[1:0] */
2178c2ecf20Sopenharmony_ci	sw	t1, 0x0840(a0)		/* mem_sdconfiga */
2188c2ecf20Sopenharmony_ci	sync
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	DO_SLEEP
2218c2ecf20Sopenharmony_ci4:
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ciEND(alchemy_sleep_au1300)
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	/* This is where we return upon wakeup.
2278c2ecf20Sopenharmony_ci	 * Reload all of the registers and return.
2288c2ecf20Sopenharmony_ci	 */
2298c2ecf20Sopenharmony_ciLEAF(alchemy_sleep_wakeup)
2308c2ecf20Sopenharmony_ci	lw	k0, 0x20(sp)
2318c2ecf20Sopenharmony_ci	mtc0	k0, CP0_STATUS
2328c2ecf20Sopenharmony_ci	lw	k0, 0x1c(sp)
2338c2ecf20Sopenharmony_ci	mtc0	k0, CP0_CONTEXT
2348c2ecf20Sopenharmony_ci	lw	k0, 0x18(sp)
2358c2ecf20Sopenharmony_ci	mtc0	k0, CP0_PAGEMASK
2368c2ecf20Sopenharmony_ci	lw	k0, 0x14(sp)
2378c2ecf20Sopenharmony_ci	mtc0	k0, CP0_CONFIG
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	/* We need to catch the early Alchemy SOCs with
2408c2ecf20Sopenharmony_ci	 * the write-only Config[OD] bit and set it back to one...
2418c2ecf20Sopenharmony_ci	 */
2428c2ecf20Sopenharmony_ci	jal	au1x00_fixup_config_od
2438c2ecf20Sopenharmony_ci	 nop
2448c2ecf20Sopenharmony_ci	lw	$1, PT_R1(sp)
2458c2ecf20Sopenharmony_ci	lw	$2, PT_R2(sp)
2468c2ecf20Sopenharmony_ci	lw	$3, PT_R3(sp)
2478c2ecf20Sopenharmony_ci	lw	$4, PT_R4(sp)
2488c2ecf20Sopenharmony_ci	lw	$5, PT_R5(sp)
2498c2ecf20Sopenharmony_ci	lw	$6, PT_R6(sp)
2508c2ecf20Sopenharmony_ci	lw	$7, PT_R7(sp)
2518c2ecf20Sopenharmony_ci	lw	$16, PT_R16(sp)
2528c2ecf20Sopenharmony_ci	lw	$17, PT_R17(sp)
2538c2ecf20Sopenharmony_ci	lw	$18, PT_R18(sp)
2548c2ecf20Sopenharmony_ci	lw	$19, PT_R19(sp)
2558c2ecf20Sopenharmony_ci	lw	$20, PT_R20(sp)
2568c2ecf20Sopenharmony_ci	lw	$21, PT_R21(sp)
2578c2ecf20Sopenharmony_ci	lw	$22, PT_R22(sp)
2588c2ecf20Sopenharmony_ci	lw	$23, PT_R23(sp)
2598c2ecf20Sopenharmony_ci	lw	$26, PT_R26(sp)
2608c2ecf20Sopenharmony_ci	lw	$27, PT_R27(sp)
2618c2ecf20Sopenharmony_ci	lw	$28, PT_R28(sp)
2628c2ecf20Sopenharmony_ci	lw	$30, PT_R30(sp)
2638c2ecf20Sopenharmony_ci	lw	$31, PT_R31(sp)
2648c2ecf20Sopenharmony_ci	jr	ra
2658c2ecf20Sopenharmony_ci	 addiu	sp, PT_SIZE
2668c2ecf20Sopenharmony_ciEND(alchemy_sleep_wakeup)
267