1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Sleep helper for Loongson-3 sleep mode.
4 *
5 * Author: Huacai Chen <[email protected]>
6 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
7 */
8
9#include <asm/asm.h>
10#include <asm/asmmacro.h>
11#include <asm/addrspace.h>
12#include <asm/loongarch.h>
13#include <asm/stackframe.h>
14
15/* preparatory stuff */
16.macro	SETUP_SLEEP
17	addi.d		sp, sp, -PT_SIZE
18	st.d		$r1, sp, PT_R1
19	st.d		$r2, sp, PT_R2
20	st.d		$r3, sp, PT_R3
21	st.d		$r4, sp, PT_R4
22	st.d		$r21, sp, PT_R21
23	st.d		$r22, sp, PT_R22
24	st.d		$r23, sp, PT_R23
25	st.d		$r24, sp, PT_R24
26	st.d		$r25, sp, PT_R25
27	st.d		$r26, sp, PT_R26
28	st.d		$r27, sp, PT_R27
29	st.d		$r28, sp, PT_R28
30	st.d		$r29, sp, PT_R29
31	st.d		$r30, sp, PT_R30
32	st.d		$r31, sp, PT_R31
33.endm
34
35.macro SETUP_WAKEUP
36	ld.d		$r1, sp, PT_R1
37	ld.d		$r2, sp, PT_R2
38	ld.d		$r3, sp, PT_R3
39	ld.d		$r4, sp, PT_R4
40	ld.d		$r21, sp, PT_R21
41	ld.d		$r22, sp, PT_R22
42	ld.d		$r23, sp, PT_R23
43	ld.d		$r24, sp, PT_R24
44	ld.d		$r25, sp, PT_R25
45	ld.d		$r26, sp, PT_R26
46	ld.d		$r27, sp, PT_R27
47	ld.d		$r28, sp, PT_R28
48	ld.d		$r29, sp, PT_R29
49	ld.d		$r30, sp, PT_R30
50	ld.d		$r31, sp, PT_R31
51	addi.d		sp, sp, PT_SIZE
52.endm
53
54	.text
55	.align 12
56
57/* Sleep/wakeup code for Loongson-3 */
58SYM_FUNC_START(loongarch_suspend_enter)
59	SETUP_SLEEP
60
61	la.pcrel	t0, acpi_saved_sp
62	st.d		sp, t0, 0
63
64	bl		__flush_cache_all
65
66	/* Pass RA and SP to BIOS */
67	addi.d		a1, sp, 0
68	la.pcrel	a0, loongarch_wakeup_start
69	la.pcrel	t0, loongarch_suspend_addr
70	ld.d		t0, t0, 0
71	jirl		ra, t0, 0 /* Call BIOS's STR sleep routine */
72
73	/*
74	 * This is where we return upon wakeup.
75	 * Reload all of the registers and return.
76	 */
77SYM_INNER_LABEL(loongarch_wakeup_start, SYM_L_GLOBAL)
78	SETUP_DMWINS	t0
79	JUMP_VIRT_ADDR	t0, t1
80
81	/* Enable PG */
82	li.w		t0, 0xb0		# PLV=0, IE=0, PG=1
83	csrwr		t0, LOONGARCH_CSR_CRMD
84
85	la.pcrel	t0, acpi_saved_sp
86	ld.d		sp, t0, 0
87
88	SETUP_WAKEUP
89	jr		ra
90SYM_FUNC_END(loongarch_suspend_enter)
91