162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Read/Write Loongson Extension Registers
362306a36Sopenharmony_ci */
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#ifndef _LOONGSON_REGS_H_
662306a36Sopenharmony_ci#define _LOONGSON_REGS_H_
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/types.h>
962306a36Sopenharmony_ci#include <linux/bits.h>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <asm/mipsregs.h>
1262306a36Sopenharmony_ci#include <asm/cpu.h>
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_cistatic inline bool cpu_has_cfg(void)
1562306a36Sopenharmony_ci{
1662306a36Sopenharmony_ci	return ((read_c0_prid() & PRID_IMP_MASK) == PRID_IMP_LOONGSON_64G);
1762306a36Sopenharmony_ci}
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_cistatic inline u32 read_cpucfg(u32 reg)
2062306a36Sopenharmony_ci{
2162306a36Sopenharmony_ci	u32 __res;
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci	__asm__ __volatile__(
2462306a36Sopenharmony_ci		_ASM_SET_PARSE_R
2562306a36Sopenharmony_ci		"parse_r __res,%0\n\t"
2662306a36Sopenharmony_ci		"parse_r reg,%1\n\t"
2762306a36Sopenharmony_ci		_ASM_UNSET_PARSE_R
2862306a36Sopenharmony_ci		".insn \n\t"
2962306a36Sopenharmony_ci		".word (0xc8080118 | (reg << 21) | (__res << 11))\n\t"
3062306a36Sopenharmony_ci		:"=r"(__res)
3162306a36Sopenharmony_ci		:"r"(reg)
3262306a36Sopenharmony_ci		:
3362306a36Sopenharmony_ci		);
3462306a36Sopenharmony_ci	return __res;
3562306a36Sopenharmony_ci}
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci/* Bit Domains for CFG registers */
3862306a36Sopenharmony_ci#define LOONGSON_CFG0	0x0
3962306a36Sopenharmony_ci#define LOONGSON_CFG0_PRID GENMASK(31, 0)
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci#define LOONGSON_CFG1 0x1
4262306a36Sopenharmony_ci#define LOONGSON_CFG1_FP	BIT(0)
4362306a36Sopenharmony_ci#define LOONGSON_CFG1_FPREV	GENMASK(3, 1)
4462306a36Sopenharmony_ci#define LOONGSON_CFG1_MMI	BIT(4)
4562306a36Sopenharmony_ci#define LOONGSON_CFG1_MSA1	BIT(5)
4662306a36Sopenharmony_ci#define LOONGSON_CFG1_MSA2	BIT(6)
4762306a36Sopenharmony_ci#define LOONGSON_CFG1_CGP	BIT(7)
4862306a36Sopenharmony_ci#define LOONGSON_CFG1_WRP	BIT(8)
4962306a36Sopenharmony_ci#define LOONGSON_CFG1_LSX1	BIT(9)
5062306a36Sopenharmony_ci#define LOONGSON_CFG1_LSX2	BIT(10)
5162306a36Sopenharmony_ci#define LOONGSON_CFG1_LASX	BIT(11)
5262306a36Sopenharmony_ci#define LOONGSON_CFG1_R6FXP	BIT(12)
5362306a36Sopenharmony_ci#define LOONGSON_CFG1_R6CRCP	BIT(13)
5462306a36Sopenharmony_ci#define LOONGSON_CFG1_R6FPP	BIT(14)
5562306a36Sopenharmony_ci#define LOONGSON_CFG1_CNT64	BIT(15)
5662306a36Sopenharmony_ci#define LOONGSON_CFG1_LSLDR0	BIT(16)
5762306a36Sopenharmony_ci#define LOONGSON_CFG1_LSPREF	BIT(17)
5862306a36Sopenharmony_ci#define LOONGSON_CFG1_LSPREFX	BIT(18)
5962306a36Sopenharmony_ci#define LOONGSON_CFG1_LSSYNCI	BIT(19)
6062306a36Sopenharmony_ci#define LOONGSON_CFG1_LSUCA	BIT(20)
6162306a36Sopenharmony_ci#define LOONGSON_CFG1_LLSYNC	BIT(21)
6262306a36Sopenharmony_ci#define LOONGSON_CFG1_TGTSYNC	BIT(22)
6362306a36Sopenharmony_ci#define LOONGSON_CFG1_LLEXC	BIT(23)
6462306a36Sopenharmony_ci#define LOONGSON_CFG1_SCRAND	BIT(24)
6562306a36Sopenharmony_ci#define LOONGSON_CFG1_MUALP	BIT(25)
6662306a36Sopenharmony_ci#define LOONGSON_CFG1_KMUALEN	BIT(26)
6762306a36Sopenharmony_ci#define LOONGSON_CFG1_ITLBT	BIT(27)
6862306a36Sopenharmony_ci#define LOONGSON_CFG1_LSUPERF	BIT(28)
6962306a36Sopenharmony_ci#define LOONGSON_CFG1_SFBP	BIT(29)
7062306a36Sopenharmony_ci#define LOONGSON_CFG1_CDMAP	BIT(30)
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci#define LOONGSON_CFG1_FPREV_OFFSET	1
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci#define LOONGSON_CFG2 0x2
7562306a36Sopenharmony_ci#define LOONGSON_CFG2_LEXT1	BIT(0)
7662306a36Sopenharmony_ci#define LOONGSON_CFG2_LEXT2	BIT(1)
7762306a36Sopenharmony_ci#define LOONGSON_CFG2_LEXT3	BIT(2)
7862306a36Sopenharmony_ci#define LOONGSON_CFG2_LSPW	BIT(3)
7962306a36Sopenharmony_ci#define LOONGSON_CFG2_LBT1	BIT(4)
8062306a36Sopenharmony_ci#define LOONGSON_CFG2_LBT2	BIT(5)
8162306a36Sopenharmony_ci#define LOONGSON_CFG2_LBT3	BIT(6)
8262306a36Sopenharmony_ci#define LOONGSON_CFG2_LBTMMU	BIT(7)
8362306a36Sopenharmony_ci#define LOONGSON_CFG2_LPMP	BIT(8)
8462306a36Sopenharmony_ci#define LOONGSON_CFG2_LPMREV	GENMASK(11, 9)
8562306a36Sopenharmony_ci#define LOONGSON_CFG2_LAMO	BIT(12)
8662306a36Sopenharmony_ci#define LOONGSON_CFG2_LPIXU	BIT(13)
8762306a36Sopenharmony_ci#define LOONGSON_CFG2_LPIXNU	BIT(14)
8862306a36Sopenharmony_ci#define LOONGSON_CFG2_LVZP	BIT(15)
8962306a36Sopenharmony_ci#define LOONGSON_CFG2_LVZREV	GENMASK(18, 16)
9062306a36Sopenharmony_ci#define LOONGSON_CFG2_LGFTP	BIT(19)
9162306a36Sopenharmony_ci#define LOONGSON_CFG2_LGFTPREV	GENMASK(22, 20)
9262306a36Sopenharmony_ci#define LOONGSON_CFG2_LLFTP	BIT(23)
9362306a36Sopenharmony_ci#define LOONGSON_CFG2_LLFTPREV	GENMASK(26, 24)
9462306a36Sopenharmony_ci#define LOONGSON_CFG2_LCSRP	BIT(27)
9562306a36Sopenharmony_ci#define LOONGSON_CFG2_LDISBLIKELY	BIT(28)
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci#define LOONGSON_CFG2_LPMREV_OFFSET	9
9862306a36Sopenharmony_ci#define LOONGSON_CFG2_LPM_REV1		(1 << LOONGSON_CFG2_LPMREV_OFFSET)
9962306a36Sopenharmony_ci#define LOONGSON_CFG2_LPM_REV2		(2 << LOONGSON_CFG2_LPMREV_OFFSET)
10062306a36Sopenharmony_ci#define LOONGSON_CFG2_LVZREV_OFFSET	16
10162306a36Sopenharmony_ci#define LOONGSON_CFG2_LVZ_REV1		(1 << LOONGSON_CFG2_LVZREV_OFFSET)
10262306a36Sopenharmony_ci#define LOONGSON_CFG2_LVZ_REV2		(2 << LOONGSON_CFG2_LVZREV_OFFSET)
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci#define LOONGSON_CFG3 0x3
10562306a36Sopenharmony_ci#define LOONGSON_CFG3_LCAMP	BIT(0)
10662306a36Sopenharmony_ci#define LOONGSON_CFG3_LCAMREV	GENMASK(3, 1)
10762306a36Sopenharmony_ci#define LOONGSON_CFG3_LCAMNUM	GENMASK(11, 4)
10862306a36Sopenharmony_ci#define LOONGSON_CFG3_LCAMKW	GENMASK(19, 12)
10962306a36Sopenharmony_ci#define LOONGSON_CFG3_LCAMVW	GENMASK(27, 20)
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci#define LOONGSON_CFG3_LCAMREV_OFFSET	1
11262306a36Sopenharmony_ci#define LOONGSON_CFG3_LCAM_REV1		(1 << LOONGSON_CFG3_LCAMREV_OFFSET)
11362306a36Sopenharmony_ci#define LOONGSON_CFG3_LCAM_REV2		(2 << LOONGSON_CFG3_LCAMREV_OFFSET)
11462306a36Sopenharmony_ci#define LOONGSON_CFG3_LCAMNUM_OFFSET	4
11562306a36Sopenharmony_ci#define LOONGSON_CFG3_LCAMNUM_REV1	(0x3f << LOONGSON_CFG3_LCAMNUM_OFFSET)
11662306a36Sopenharmony_ci#define LOONGSON_CFG3_LCAMKW_OFFSET	12
11762306a36Sopenharmony_ci#define LOONGSON_CFG3_LCAMKW_REV1	(0x27 << LOONGSON_CFG3_LCAMKW_OFFSET)
11862306a36Sopenharmony_ci#define LOONGSON_CFG3_LCAMVW_OFFSET	20
11962306a36Sopenharmony_ci#define LOONGSON_CFG3_LCAMVW_REV1	(0x3f << LOONGSON_CFG3_LCAMVW_OFFSET)
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci#define LOONGSON_CFG4 0x4
12262306a36Sopenharmony_ci#define LOONGSON_CFG4_CCFREQ	GENMASK(31, 0)
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci#define LOONGSON_CFG5 0x5
12562306a36Sopenharmony_ci#define LOONGSON_CFG5_CFM	GENMASK(15, 0)
12662306a36Sopenharmony_ci#define LOONGSON_CFG5_CFD	GENMASK(31, 16)
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci#define LOONGSON_CFG6 0x6
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci#define LOONGSON_CFG7 0x7
13162306a36Sopenharmony_ci#define LOONGSON_CFG7_GCCAEQRP	BIT(0)
13262306a36Sopenharmony_ci#define LOONGSON_CFG7_UCAWINP	BIT(1)
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_cistatic inline bool cpu_has_csr(void)
13562306a36Sopenharmony_ci{
13662306a36Sopenharmony_ci	if (cpu_has_cfg())
13762306a36Sopenharmony_ci		return (read_cpucfg(LOONGSON_CFG2) & LOONGSON_CFG2_LCSRP);
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	return false;
14062306a36Sopenharmony_ci}
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_cistatic inline u32 csr_readl(u32 reg)
14362306a36Sopenharmony_ci{
14462306a36Sopenharmony_ci	u32 __res;
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci	/* RDCSR reg, val */
14762306a36Sopenharmony_ci	__asm__ __volatile__(
14862306a36Sopenharmony_ci		_ASM_SET_PARSE_R
14962306a36Sopenharmony_ci		"parse_r __res,%0\n\t"
15062306a36Sopenharmony_ci		"parse_r reg,%1\n\t"
15162306a36Sopenharmony_ci		_ASM_UNSET_PARSE_R
15262306a36Sopenharmony_ci		".insn \n\t"
15362306a36Sopenharmony_ci		".word (0xc8000118 | (reg << 21) | (__res << 11))\n\t"
15462306a36Sopenharmony_ci		:"=r"(__res)
15562306a36Sopenharmony_ci		:"r"(reg)
15662306a36Sopenharmony_ci		:
15762306a36Sopenharmony_ci		);
15862306a36Sopenharmony_ci	return __res;
15962306a36Sopenharmony_ci}
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_cistatic inline u64 csr_readq(u32 reg)
16262306a36Sopenharmony_ci{
16362306a36Sopenharmony_ci	u64 __res;
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci	/* DRDCSR reg, val */
16662306a36Sopenharmony_ci	__asm__ __volatile__(
16762306a36Sopenharmony_ci		_ASM_SET_PARSE_R
16862306a36Sopenharmony_ci		"parse_r __res,%0\n\t"
16962306a36Sopenharmony_ci		"parse_r reg,%1\n\t"
17062306a36Sopenharmony_ci		_ASM_UNSET_PARSE_R
17162306a36Sopenharmony_ci		".insn \n\t"
17262306a36Sopenharmony_ci		".word (0xc8020118 | (reg << 21) | (__res << 11))\n\t"
17362306a36Sopenharmony_ci		:"=r"(__res)
17462306a36Sopenharmony_ci		:"r"(reg)
17562306a36Sopenharmony_ci		:
17662306a36Sopenharmony_ci		);
17762306a36Sopenharmony_ci	return __res;
17862306a36Sopenharmony_ci}
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_cistatic inline void csr_writel(u32 val, u32 reg)
18162306a36Sopenharmony_ci{
18262306a36Sopenharmony_ci	/* WRCSR reg, val */
18362306a36Sopenharmony_ci	__asm__ __volatile__(
18462306a36Sopenharmony_ci		_ASM_SET_PARSE_R
18562306a36Sopenharmony_ci		"parse_r reg,%0\n\t"
18662306a36Sopenharmony_ci		"parse_r val,%1\n\t"
18762306a36Sopenharmony_ci		_ASM_UNSET_PARSE_R
18862306a36Sopenharmony_ci		".insn \n\t"
18962306a36Sopenharmony_ci		".word (0xc8010118 | (reg << 21) | (val << 11))\n\t"
19062306a36Sopenharmony_ci		:
19162306a36Sopenharmony_ci		:"r"(reg),"r"(val)
19262306a36Sopenharmony_ci		:
19362306a36Sopenharmony_ci		);
19462306a36Sopenharmony_ci}
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_cistatic inline void csr_writeq(u64 val, u32 reg)
19762306a36Sopenharmony_ci{
19862306a36Sopenharmony_ci	/* DWRCSR reg, val */
19962306a36Sopenharmony_ci	__asm__ __volatile__(
20062306a36Sopenharmony_ci		_ASM_SET_PARSE_R
20162306a36Sopenharmony_ci		"parse_r reg,%0\n\t"
20262306a36Sopenharmony_ci		"parse_r val,%1\n\t"
20362306a36Sopenharmony_ci		_ASM_UNSET_PARSE_R
20462306a36Sopenharmony_ci		".insn \n\t"
20562306a36Sopenharmony_ci		".word (0xc8030118 | (reg << 21) | (val << 11))\n\t"
20662306a36Sopenharmony_ci		:
20762306a36Sopenharmony_ci		:"r"(reg),"r"(val)
20862306a36Sopenharmony_ci		:
20962306a36Sopenharmony_ci		);
21062306a36Sopenharmony_ci}
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci/* Public CSR Register can also be accessed with regular addresses */
21362306a36Sopenharmony_ci#define CSR_PUBLIC_MMIO_BASE 0x1fe00000
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci#define MMIO_CSR(x)		(void *)TO_UNCAC(CSR_PUBLIC_MMIO_BASE + x)
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci#define LOONGSON_CSR_FEATURES	0x8
21862306a36Sopenharmony_ci#define LOONGSON_CSRF_TEMP	BIT(0)
21962306a36Sopenharmony_ci#define LOONGSON_CSRF_NODECNT	BIT(1)
22062306a36Sopenharmony_ci#define LOONGSON_CSRF_MSI	BIT(2)
22162306a36Sopenharmony_ci#define LOONGSON_CSRF_EXTIOI	BIT(3)
22262306a36Sopenharmony_ci#define LOONGSON_CSRF_IPI	BIT(4)
22362306a36Sopenharmony_ci#define LOONGSON_CSRF_FREQ	BIT(5)
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci#define LOONGSON_CSR_VENDOR	0x10 /* Vendor name string, should be "Loongson" */
22662306a36Sopenharmony_ci#define LOONGSON_CSR_CPUNAME	0x20 /* Processor name string */
22762306a36Sopenharmony_ci#define LOONGSON_CSR_NODECNT	0x408
22862306a36Sopenharmony_ci#define LOONGSON_CSR_CPUTEMP	0x428
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci/* PerCore CSR, only accessable by local cores */
23162306a36Sopenharmony_ci#define LOONGSON_CSR_IPI_STATUS	0x1000
23262306a36Sopenharmony_ci#define LOONGSON_CSR_IPI_EN	0x1004
23362306a36Sopenharmony_ci#define LOONGSON_CSR_IPI_SET	0x1008
23462306a36Sopenharmony_ci#define LOONGSON_CSR_IPI_CLEAR	0x100c
23562306a36Sopenharmony_ci#define LOONGSON_CSR_IPI_SEND	0x1040
23662306a36Sopenharmony_ci#define CSR_IPI_SEND_IP_SHIFT	0
23762306a36Sopenharmony_ci#define CSR_IPI_SEND_CPU_SHIFT	16
23862306a36Sopenharmony_ci#define CSR_IPI_SEND_BLOCK	BIT(31)
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci#define LOONGSON_CSR_MAIL_BUF0		0x1020
24162306a36Sopenharmony_ci#define LOONGSON_CSR_MAIL_SEND		0x1048
24262306a36Sopenharmony_ci#define CSR_MAIL_SEND_BLOCK		BIT_ULL(31)
24362306a36Sopenharmony_ci#define CSR_MAIL_SEND_BOX_LOW(box)	(box << 1)
24462306a36Sopenharmony_ci#define CSR_MAIL_SEND_BOX_HIGH(box)	((box << 1) + 1)
24562306a36Sopenharmony_ci#define CSR_MAIL_SEND_BOX_SHIFT		2
24662306a36Sopenharmony_ci#define CSR_MAIL_SEND_CPU_SHIFT		16
24762306a36Sopenharmony_ci#define CSR_MAIL_SEND_BUF_SHIFT		32
24862306a36Sopenharmony_ci#define CSR_MAIL_SEND_H32_MASK		0xFFFFFFFF00000000ULL
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_cistatic inline u64 drdtime(void)
25162306a36Sopenharmony_ci{
25262306a36Sopenharmony_ci	int rID = 0;
25362306a36Sopenharmony_ci	u64 val = 0;
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci	__asm__ __volatile__(
25662306a36Sopenharmony_ci		_ASM_SET_PARSE_R
25762306a36Sopenharmony_ci		"parse_r rID,%0\n\t"
25862306a36Sopenharmony_ci		"parse_r val,%1\n\t"
25962306a36Sopenharmony_ci		_ASM_UNSET_PARSE_R
26062306a36Sopenharmony_ci		".insn \n\t"
26162306a36Sopenharmony_ci		".word (0xc8090118 | (rID << 21) | (val << 11))\n\t"
26262306a36Sopenharmony_ci		:"=r"(rID),"=r"(val)
26362306a36Sopenharmony_ci		:
26462306a36Sopenharmony_ci		);
26562306a36Sopenharmony_ci	return val;
26662306a36Sopenharmony_ci}
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci#endif
269