18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Read/Write Loongson Extension Registers
38c2ecf20Sopenharmony_ci */
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#ifndef _LOONGSON_REGS_H_
68c2ecf20Sopenharmony_ci#define _LOONGSON_REGS_H_
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/types.h>
98c2ecf20Sopenharmony_ci#include <linux/bits.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <asm/mipsregs.h>
128c2ecf20Sopenharmony_ci#include <asm/cpu.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cistatic inline bool cpu_has_cfg(void)
158c2ecf20Sopenharmony_ci{
168c2ecf20Sopenharmony_ci	return ((read_c0_prid() & PRID_IMP_MASK) == PRID_IMP_LOONGSON_64G);
178c2ecf20Sopenharmony_ci}
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic inline u32 read_cpucfg(u32 reg)
208c2ecf20Sopenharmony_ci{
218c2ecf20Sopenharmony_ci	u32 __res;
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci	__asm__ __volatile__(
248c2ecf20Sopenharmony_ci		"parse_r __res,%0\n\t"
258c2ecf20Sopenharmony_ci		"parse_r reg,%1\n\t"
268c2ecf20Sopenharmony_ci		".insn \n\t"
278c2ecf20Sopenharmony_ci		".word (0xc8080118 | (reg << 21) | (__res << 11))\n\t"
288c2ecf20Sopenharmony_ci		:"=r"(__res)
298c2ecf20Sopenharmony_ci		:"r"(reg)
308c2ecf20Sopenharmony_ci		:
318c2ecf20Sopenharmony_ci		);
328c2ecf20Sopenharmony_ci	return __res;
338c2ecf20Sopenharmony_ci}
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* Bit Domains for CFG registers */
368c2ecf20Sopenharmony_ci#define LOONGSON_CFG0	0x0
378c2ecf20Sopenharmony_ci#define LOONGSON_CFG0_PRID GENMASK(31, 0)
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define LOONGSON_CFG1 0x1
408c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_FP	BIT(0)
418c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_FPREV	GENMASK(3, 1)
428c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_MMI	BIT(4)
438c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_MSA1	BIT(5)
448c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_MSA2	BIT(6)
458c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_CGP	BIT(7)
468c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_WRP	BIT(8)
478c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_LSX1	BIT(9)
488c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_LSX2	BIT(10)
498c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_LASX	BIT(11)
508c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_R6FXP	BIT(12)
518c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_R6CRCP	BIT(13)
528c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_R6FPP	BIT(14)
538c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_CNT64	BIT(15)
548c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_LSLDR0	BIT(16)
558c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_LSPREF	BIT(17)
568c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_LSPREFX	BIT(18)
578c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_LSSYNCI	BIT(19)
588c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_LSUCA	BIT(20)
598c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_LLSYNC	BIT(21)
608c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_TGTSYNC	BIT(22)
618c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_LLEXC	BIT(23)
628c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_SCRAND	BIT(24)
638c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_MUALP	BIT(25)
648c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_KMUALEN	BIT(26)
658c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_ITLBT	BIT(27)
668c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_LSUPERF	BIT(28)
678c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_SFBP	BIT(29)
688c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_CDMAP	BIT(30)
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#define LOONGSON_CFG1_FPREV_OFFSET	1
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#define LOONGSON_CFG2 0x2
738c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LEXT1	BIT(0)
748c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LEXT2	BIT(1)
758c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LEXT3	BIT(2)
768c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LSPW	BIT(3)
778c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LBT1	BIT(4)
788c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LBT2	BIT(5)
798c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LBT3	BIT(6)
808c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LBTMMU	BIT(7)
818c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LPMP	BIT(8)
828c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LPMREV	GENMASK(11, 9)
838c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LAMO	BIT(12)
848c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LPIXU	BIT(13)
858c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LPIXNU	BIT(14)
868c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LVZP	BIT(15)
878c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LVZREV	GENMASK(18, 16)
888c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LGFTP	BIT(19)
898c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LGFTPREV	GENMASK(22, 20)
908c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LLFTP	BIT(23)
918c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LLFTPREV	GENMASK(26, 24)
928c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LCSRP	BIT(27)
938c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LDISBLIKELY	BIT(28)
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LPMREV_OFFSET	9
968c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LPM_REV1		(1 << LOONGSON_CFG2_LPMREV_OFFSET)
978c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LPM_REV2		(2 << LOONGSON_CFG2_LPMREV_OFFSET)
988c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LVZREV_OFFSET	16
998c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LVZ_REV1		(1 << LOONGSON_CFG2_LVZREV_OFFSET)
1008c2ecf20Sopenharmony_ci#define LOONGSON_CFG2_LVZ_REV2		(2 << LOONGSON_CFG2_LVZREV_OFFSET)
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci#define LOONGSON_CFG3 0x3
1038c2ecf20Sopenharmony_ci#define LOONGSON_CFG3_LCAMP	BIT(0)
1048c2ecf20Sopenharmony_ci#define LOONGSON_CFG3_LCAMREV	GENMASK(3, 1)
1058c2ecf20Sopenharmony_ci#define LOONGSON_CFG3_LCAMNUM	GENMASK(11, 4)
1068c2ecf20Sopenharmony_ci#define LOONGSON_CFG3_LCAMKW	GENMASK(19, 12)
1078c2ecf20Sopenharmony_ci#define LOONGSON_CFG3_LCAMVW	GENMASK(27, 20)
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci#define LOONGSON_CFG3_LCAMREV_OFFSET	1
1108c2ecf20Sopenharmony_ci#define LOONGSON_CFG3_LCAM_REV1		(1 << LOONGSON_CFG3_LCAMREV_OFFSET)
1118c2ecf20Sopenharmony_ci#define LOONGSON_CFG3_LCAM_REV2		(2 << LOONGSON_CFG3_LCAMREV_OFFSET)
1128c2ecf20Sopenharmony_ci#define LOONGSON_CFG3_LCAMNUM_OFFSET	4
1138c2ecf20Sopenharmony_ci#define LOONGSON_CFG3_LCAMNUM_REV1	(0x3f << LOONGSON_CFG3_LCAMNUM_OFFSET)
1148c2ecf20Sopenharmony_ci#define LOONGSON_CFG3_LCAMKW_OFFSET	12
1158c2ecf20Sopenharmony_ci#define LOONGSON_CFG3_LCAMKW_REV1	(0x27 << LOONGSON_CFG3_LCAMKW_OFFSET)
1168c2ecf20Sopenharmony_ci#define LOONGSON_CFG3_LCAMVW_OFFSET	20
1178c2ecf20Sopenharmony_ci#define LOONGSON_CFG3_LCAMVW_REV1	(0x3f << LOONGSON_CFG3_LCAMVW_OFFSET)
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci#define LOONGSON_CFG4 0x4
1208c2ecf20Sopenharmony_ci#define LOONGSON_CFG4_CCFREQ	GENMASK(31, 0)
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci#define LOONGSON_CFG5 0x5
1238c2ecf20Sopenharmony_ci#define LOONGSON_CFG5_CFM	GENMASK(15, 0)
1248c2ecf20Sopenharmony_ci#define LOONGSON_CFG5_CFD	GENMASK(31, 16)
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci#define LOONGSON_CFG6 0x6
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci#define LOONGSON_CFG7 0x7
1298c2ecf20Sopenharmony_ci#define LOONGSON_CFG7_GCCAEQRP	BIT(0)
1308c2ecf20Sopenharmony_ci#define LOONGSON_CFG7_UCAWINP	BIT(1)
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic inline bool cpu_has_csr(void)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	if (cpu_has_cfg())
1358c2ecf20Sopenharmony_ci		return (read_cpucfg(LOONGSON_CFG2) & LOONGSON_CFG2_LCSRP);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	return false;
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic inline u32 csr_readl(u32 reg)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	u32 __res;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	/* RDCSR reg, val */
1458c2ecf20Sopenharmony_ci	__asm__ __volatile__(
1468c2ecf20Sopenharmony_ci		"parse_r __res,%0\n\t"
1478c2ecf20Sopenharmony_ci		"parse_r reg,%1\n\t"
1488c2ecf20Sopenharmony_ci		".insn \n\t"
1498c2ecf20Sopenharmony_ci		".word (0xc8000118 | (reg << 21) | (__res << 11))\n\t"
1508c2ecf20Sopenharmony_ci		:"=r"(__res)
1518c2ecf20Sopenharmony_ci		:"r"(reg)
1528c2ecf20Sopenharmony_ci		:
1538c2ecf20Sopenharmony_ci		);
1548c2ecf20Sopenharmony_ci	return __res;
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_cistatic inline u64 csr_readq(u32 reg)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	u64 __res;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	/* DRDCSR reg, val */
1628c2ecf20Sopenharmony_ci	__asm__ __volatile__(
1638c2ecf20Sopenharmony_ci		"parse_r __res,%0\n\t"
1648c2ecf20Sopenharmony_ci		"parse_r reg,%1\n\t"
1658c2ecf20Sopenharmony_ci		".insn \n\t"
1668c2ecf20Sopenharmony_ci		".word (0xc8020118 | (reg << 21) | (__res << 11))\n\t"
1678c2ecf20Sopenharmony_ci		:"=r"(__res)
1688c2ecf20Sopenharmony_ci		:"r"(reg)
1698c2ecf20Sopenharmony_ci		:
1708c2ecf20Sopenharmony_ci		);
1718c2ecf20Sopenharmony_ci	return __res;
1728c2ecf20Sopenharmony_ci}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistatic inline void csr_writel(u32 val, u32 reg)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	/* WRCSR reg, val */
1778c2ecf20Sopenharmony_ci	__asm__ __volatile__(
1788c2ecf20Sopenharmony_ci		"parse_r reg,%0\n\t"
1798c2ecf20Sopenharmony_ci		"parse_r val,%1\n\t"
1808c2ecf20Sopenharmony_ci		".insn \n\t"
1818c2ecf20Sopenharmony_ci		".word (0xc8010118 | (reg << 21) | (val << 11))\n\t"
1828c2ecf20Sopenharmony_ci		:
1838c2ecf20Sopenharmony_ci		:"r"(reg),"r"(val)
1848c2ecf20Sopenharmony_ci		:
1858c2ecf20Sopenharmony_ci		);
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_cistatic inline void csr_writeq(u64 val, u32 reg)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	/* DWRCSR reg, val */
1918c2ecf20Sopenharmony_ci	__asm__ __volatile__(
1928c2ecf20Sopenharmony_ci		"parse_r reg,%0\n\t"
1938c2ecf20Sopenharmony_ci		"parse_r val,%1\n\t"
1948c2ecf20Sopenharmony_ci		".insn \n\t"
1958c2ecf20Sopenharmony_ci		".word (0xc8030118 | (reg << 21) | (val << 11))\n\t"
1968c2ecf20Sopenharmony_ci		:
1978c2ecf20Sopenharmony_ci		:"r"(reg),"r"(val)
1988c2ecf20Sopenharmony_ci		:
1998c2ecf20Sopenharmony_ci		);
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci/* Public CSR Register can also be accessed with regular addresses */
2038c2ecf20Sopenharmony_ci#define CSR_PUBLIC_MMIO_BASE 0x1fe00000
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci#define MMIO_CSR(x)		(void *)TO_UNCAC(CSR_PUBLIC_MMIO_BASE + x)
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci#define LOONGSON_CSR_FEATURES	0x8
2088c2ecf20Sopenharmony_ci#define LOONGSON_CSRF_TEMP	BIT(0)
2098c2ecf20Sopenharmony_ci#define LOONGSON_CSRF_NODECNT	BIT(1)
2108c2ecf20Sopenharmony_ci#define LOONGSON_CSRF_MSI	BIT(2)
2118c2ecf20Sopenharmony_ci#define LOONGSON_CSRF_EXTIOI	BIT(3)
2128c2ecf20Sopenharmony_ci#define LOONGSON_CSRF_IPI	BIT(4)
2138c2ecf20Sopenharmony_ci#define LOONGSON_CSRF_FREQ	BIT(5)
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci#define LOONGSON_CSR_VENDOR	0x10 /* Vendor name string, should be "Loongson" */
2168c2ecf20Sopenharmony_ci#define LOONGSON_CSR_CPUNAME	0x20 /* Processor name string */
2178c2ecf20Sopenharmony_ci#define LOONGSON_CSR_NODECNT	0x408
2188c2ecf20Sopenharmony_ci#define LOONGSON_CSR_CPUTEMP	0x428
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci/* PerCore CSR, only accessable by local cores */
2218c2ecf20Sopenharmony_ci#define LOONGSON_CSR_IPI_STATUS	0x1000
2228c2ecf20Sopenharmony_ci#define LOONGSON_CSR_IPI_EN	0x1004
2238c2ecf20Sopenharmony_ci#define LOONGSON_CSR_IPI_SET	0x1008
2248c2ecf20Sopenharmony_ci#define LOONGSON_CSR_IPI_CLEAR	0x100c
2258c2ecf20Sopenharmony_ci#define LOONGSON_CSR_IPI_SEND	0x1040
2268c2ecf20Sopenharmony_ci#define CSR_IPI_SEND_IP_SHIFT	0
2278c2ecf20Sopenharmony_ci#define CSR_IPI_SEND_CPU_SHIFT	16
2288c2ecf20Sopenharmony_ci#define CSR_IPI_SEND_BLOCK	BIT(31)
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_cistatic inline u64 drdtime(void)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	int rID = 0;
2338c2ecf20Sopenharmony_ci	u64 val = 0;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	__asm__ __volatile__(
2368c2ecf20Sopenharmony_ci		"parse_r rID,%0\n\t"
2378c2ecf20Sopenharmony_ci		"parse_r val,%1\n\t"
2388c2ecf20Sopenharmony_ci		".insn \n\t"
2398c2ecf20Sopenharmony_ci		".word (0xc8090118 | (rID << 21) | (val << 11))\n\t"
2408c2ecf20Sopenharmony_ci		:"=r"(rID),"=r"(val)
2418c2ecf20Sopenharmony_ci		:
2428c2ecf20Sopenharmony_ci		);
2438c2ecf20Sopenharmony_ci	return val;
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci#endif
247