162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2004 Konrad Eisele (eiselekd@web.de,konrad@gaisler.com) Gaisler Research
462306a36Sopenharmony_ci * Copyright (C) 2004 Stefan Holst (mail@s-holst.de) Uni-Stuttgart
562306a36Sopenharmony_ci * Copyright (C) 2009 Daniel Hellstrom (daniel@gaisler.com) Aeroflex Gaisler AB
662306a36Sopenharmony_ci * Copyright (C) 2009 Konrad Eisele (konrad@gaisler.com) Aeroflex Gaisler AB
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#ifndef LEON_H_INCLUDE
1062306a36Sopenharmony_ci#define LEON_H_INCLUDE
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci/* mmu register access, ASI_LEON_MMUREGS */
1362306a36Sopenharmony_ci#define LEON_CNR_CTRL		0x000
1462306a36Sopenharmony_ci#define LEON_CNR_CTXP		0x100
1562306a36Sopenharmony_ci#define LEON_CNR_CTX		0x200
1662306a36Sopenharmony_ci#define LEON_CNR_F		0x300
1762306a36Sopenharmony_ci#define LEON_CNR_FADDR		0x400
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#define LEON_CNR_CTX_NCTX	256	/*number of MMU ctx */
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci#define LEON_CNR_CTRL_TLBDIS	0x80000000
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#define LEON_MMUTLB_ENT_MAX	64
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci/*
2662306a36Sopenharmony_ci * diagnostic access from mmutlb.vhd:
2762306a36Sopenharmony_ci * 0: pte address
2862306a36Sopenharmony_ci * 4: pte
2962306a36Sopenharmony_ci * 8: additional flags
3062306a36Sopenharmony_ci */
3162306a36Sopenharmony_ci#define LEON_DIAGF_LVL		0x3
3262306a36Sopenharmony_ci#define LEON_DIAGF_WR		0x8
3362306a36Sopenharmony_ci#define LEON_DIAGF_WR_SHIFT	3
3462306a36Sopenharmony_ci#define LEON_DIAGF_HIT		0x10
3562306a36Sopenharmony_ci#define LEON_DIAGF_HIT_SHIFT	4
3662306a36Sopenharmony_ci#define LEON_DIAGF_CTX		0x1fe0
3762306a36Sopenharmony_ci#define LEON_DIAGF_CTX_SHIFT	5
3862306a36Sopenharmony_ci#define LEON_DIAGF_VALID	0x2000
3962306a36Sopenharmony_ci#define LEON_DIAGF_VALID_SHIFT	13
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci/* irq masks */
4262306a36Sopenharmony_ci#define LEON_HARD_INT(x)	(1 << (x))	/* irq 0-15 */
4362306a36Sopenharmony_ci#define LEON_IRQMASK_R		0x0000fffe	/* bit 15- 1 of lregs.irqmask */
4462306a36Sopenharmony_ci#define LEON_IRQPRIO_R		0xfffe0000	/* bit 31-17 of lregs.irqmask */
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci#define LEON_MCFG2_SRAMDIS		0x00002000
4762306a36Sopenharmony_ci#define LEON_MCFG2_SDRAMEN		0x00004000
4862306a36Sopenharmony_ci#define LEON_MCFG2_SRAMBANKSZ		0x00001e00	/* [12-9] */
4962306a36Sopenharmony_ci#define LEON_MCFG2_SRAMBANKSZ_SHIFT	9
5062306a36Sopenharmony_ci#define LEON_MCFG2_SDRAMBANKSZ		0x03800000	/* [25-23] */
5162306a36Sopenharmony_ci#define LEON_MCFG2_SDRAMBANKSZ_SHIFT	23
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci#define LEON_TCNT0_MASK	0x7fffff
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci#define ASI_LEON3_SYSCTRL		0x02
5762306a36Sopenharmony_ci#define ASI_LEON3_SYSCTRL_ICFG		0x08
5862306a36Sopenharmony_ci#define ASI_LEON3_SYSCTRL_DCFG		0x0c
5962306a36Sopenharmony_ci#define ASI_LEON3_SYSCTRL_CFG_SNOOPING (1 << 27)
6062306a36Sopenharmony_ci#define ASI_LEON3_SYSCTRL_CFG_SSIZE(c) (1 << ((c >> 20) & 0xf))
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci#ifndef __ASSEMBLY__
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci/* do a physical address bypass write, i.e. for 0x80000000 */
6562306a36Sopenharmony_cistatic inline void leon_store_reg(unsigned long paddr, unsigned long value)
6662306a36Sopenharmony_ci{
6762306a36Sopenharmony_ci	__asm__ __volatile__("sta %0, [%1] %2\n\t" : : "r"(value), "r"(paddr),
6862306a36Sopenharmony_ci			     "i"(ASI_LEON_BYPASS) : "memory");
6962306a36Sopenharmony_ci}
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci/* do a physical address bypass load, i.e. for 0x80000000 */
7262306a36Sopenharmony_cistatic inline unsigned long leon_load_reg(unsigned long paddr)
7362306a36Sopenharmony_ci{
7462306a36Sopenharmony_ci	unsigned long retval;
7562306a36Sopenharmony_ci	__asm__ __volatile__("lda [%1] %2, %0\n\t" :
7662306a36Sopenharmony_ci			     "=r"(retval) : "r"(paddr), "i"(ASI_LEON_BYPASS));
7762306a36Sopenharmony_ci	return retval;
7862306a36Sopenharmony_ci}
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci/* macro access for leon_load_reg() and leon_store_reg() */
8162306a36Sopenharmony_ci#define LEON3_BYPASS_LOAD_PA(x)	    (leon_load_reg((unsigned long)(x)))
8262306a36Sopenharmony_ci#define LEON3_BYPASS_STORE_PA(x, v) (leon_store_reg((unsigned long)(x), (unsigned long)(v)))
8362306a36Sopenharmony_ci#define LEON_BYPASS_LOAD_PA(x)      leon_load_reg((unsigned long)(x))
8462306a36Sopenharmony_ci#define LEON_BYPASS_STORE_PA(x, v)  leon_store_reg((unsigned long)(x), (unsigned long)(v))
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_civoid leon_switch_mm(void);
8762306a36Sopenharmony_civoid leon_init_IRQ(void);
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_cistatic inline unsigned long sparc_leon3_get_dcachecfg(void)
9062306a36Sopenharmony_ci{
9162306a36Sopenharmony_ci	unsigned int retval;
9262306a36Sopenharmony_ci	__asm__ __volatile__("lda [%1] %2, %0\n\t" :
9362306a36Sopenharmony_ci			     "=r"(retval) :
9462306a36Sopenharmony_ci			     "r"(ASI_LEON3_SYSCTRL_DCFG),
9562306a36Sopenharmony_ci			     "i"(ASI_LEON3_SYSCTRL));
9662306a36Sopenharmony_ci	return retval;
9762306a36Sopenharmony_ci}
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci/* enable snooping */
10062306a36Sopenharmony_cistatic inline void sparc_leon3_enable_snooping(void)
10162306a36Sopenharmony_ci{
10262306a36Sopenharmony_ci	__asm__ __volatile__ ("lda [%%g0] 2, %%l1\n\t"
10362306a36Sopenharmony_ci			  "set 0x800000, %%l2\n\t"
10462306a36Sopenharmony_ci			  "or  %%l2, %%l1, %%l2\n\t"
10562306a36Sopenharmony_ci			  "sta %%l2, [%%g0] 2\n\t" : : : "l1", "l2");
10662306a36Sopenharmony_ci};
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_cistatic inline int sparc_leon3_snooping_enabled(void)
10962306a36Sopenharmony_ci{
11062306a36Sopenharmony_ci	u32 cctrl;
11162306a36Sopenharmony_ci	__asm__ __volatile__("lda [%%g0] 2, %0\n\t" : "=r"(cctrl));
11262306a36Sopenharmony_ci	return ((cctrl >> 23) & 1) && ((cctrl >> 17) & 1);
11362306a36Sopenharmony_ci};
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_cistatic inline void sparc_leon3_disable_cache(void)
11662306a36Sopenharmony_ci{
11762306a36Sopenharmony_ci	__asm__ __volatile__ ("lda [%%g0] 2, %%l1\n\t"
11862306a36Sopenharmony_ci			  "set 0x00000f, %%l2\n\t"
11962306a36Sopenharmony_ci			  "andn  %%l2, %%l1, %%l2\n\t"
12062306a36Sopenharmony_ci			  "sta %%l2, [%%g0] 2\n\t" : : : "l1", "l2");
12162306a36Sopenharmony_ci};
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_cistatic inline unsigned long sparc_leon3_asr17(void)
12462306a36Sopenharmony_ci{
12562306a36Sopenharmony_ci	u32 asr17;
12662306a36Sopenharmony_ci	__asm__ __volatile__ ("rd %%asr17, %0\n\t" : "=r"(asr17));
12762306a36Sopenharmony_ci	return asr17;
12862306a36Sopenharmony_ci};
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_cistatic inline int sparc_leon3_cpuid(void)
13162306a36Sopenharmony_ci{
13262306a36Sopenharmony_ci	return sparc_leon3_asr17() >> 28;
13362306a36Sopenharmony_ci}
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci#endif /*!__ASSEMBLY__*/
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci#ifdef CONFIG_SMP
13862306a36Sopenharmony_ci# define LEON3_IRQ_IPI_DEFAULT		13
13962306a36Sopenharmony_ci# define LEON3_IRQ_TICKER		(leon3_gptimer_irq)
14062306a36Sopenharmony_ci# define LEON3_IRQ_CROSS_CALL		15
14162306a36Sopenharmony_ci#endif
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci#if defined(PAGE_SIZE_LEON_8K)
14462306a36Sopenharmony_ci#define LEON_PAGE_SIZE_LEON 1
14562306a36Sopenharmony_ci#elif defined(PAGE_SIZE_LEON_16K)
14662306a36Sopenharmony_ci#define LEON_PAGE_SIZE_LEON 2)
14762306a36Sopenharmony_ci#else
14862306a36Sopenharmony_ci#define LEON_PAGE_SIZE_LEON 0
14962306a36Sopenharmony_ci#endif
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci#if LEON_PAGE_SIZE_LEON == 0
15262306a36Sopenharmony_ci/* [ 8, 6, 6 ] + 12 */
15362306a36Sopenharmony_ci#define LEON_PGD_SH    24
15462306a36Sopenharmony_ci#define LEON_PGD_M     0xff
15562306a36Sopenharmony_ci#define LEON_PMD_SH    18
15662306a36Sopenharmony_ci#define LEON_PMD_SH_V  (LEON_PGD_SH-2)
15762306a36Sopenharmony_ci#define LEON_PMD_M     0x3f
15862306a36Sopenharmony_ci#define LEON_PTE_SH    12
15962306a36Sopenharmony_ci#define LEON_PTE_M     0x3f
16062306a36Sopenharmony_ci#elif LEON_PAGE_SIZE_LEON == 1
16162306a36Sopenharmony_ci/* [ 7, 6, 6 ] + 13 */
16262306a36Sopenharmony_ci#define LEON_PGD_SH    25
16362306a36Sopenharmony_ci#define LEON_PGD_M     0x7f
16462306a36Sopenharmony_ci#define LEON_PMD_SH    19
16562306a36Sopenharmony_ci#define LEON_PMD_SH_V  (LEON_PGD_SH-1)
16662306a36Sopenharmony_ci#define LEON_PMD_M     0x3f
16762306a36Sopenharmony_ci#define LEON_PTE_SH    13
16862306a36Sopenharmony_ci#define LEON_PTE_M     0x3f
16962306a36Sopenharmony_ci#elif LEON_PAGE_SIZE_LEON == 2
17062306a36Sopenharmony_ci/* [ 6, 6, 6 ] + 14 */
17162306a36Sopenharmony_ci#define LEON_PGD_SH    26
17262306a36Sopenharmony_ci#define LEON_PGD_M     0x3f
17362306a36Sopenharmony_ci#define LEON_PMD_SH    20
17462306a36Sopenharmony_ci#define LEON_PMD_SH_V  (LEON_PGD_SH-0)
17562306a36Sopenharmony_ci#define LEON_PMD_M     0x3f
17662306a36Sopenharmony_ci#define LEON_PTE_SH    14
17762306a36Sopenharmony_ci#define LEON_PTE_M     0x3f
17862306a36Sopenharmony_ci#elif LEON_PAGE_SIZE_LEON == 3
17962306a36Sopenharmony_ci/* [ 4, 7, 6 ] + 15 */
18062306a36Sopenharmony_ci#define LEON_PGD_SH    28
18162306a36Sopenharmony_ci#define LEON_PGD_M     0x0f
18262306a36Sopenharmony_ci#define LEON_PMD_SH    21
18362306a36Sopenharmony_ci#define LEON_PMD_SH_V  (LEON_PGD_SH-0)
18462306a36Sopenharmony_ci#define LEON_PMD_M     0x7f
18562306a36Sopenharmony_ci#define LEON_PTE_SH    15
18662306a36Sopenharmony_ci#define LEON_PTE_M     0x3f
18762306a36Sopenharmony_ci#else
18862306a36Sopenharmony_ci#error cannot determine LEON_PAGE_SIZE_LEON
18962306a36Sopenharmony_ci#endif
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci#define LEON3_XCCR_SETS_MASK  0x07000000UL
19262306a36Sopenharmony_ci#define LEON3_XCCR_SSIZE_MASK 0x00f00000UL
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci#define LEON2_CCR_DSETS_MASK 0x03000000UL
19562306a36Sopenharmony_ci#define LEON2_CFG_SSIZE_MASK 0x00007000UL
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci#ifndef __ASSEMBLY__
19862306a36Sopenharmony_cistruct vm_area_struct;
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ciunsigned long leon_swprobe(unsigned long vaddr, unsigned long *paddr);
20162306a36Sopenharmony_civoid leon_flush_icache_all(void);
20262306a36Sopenharmony_civoid leon_flush_dcache_all(void);
20362306a36Sopenharmony_civoid leon_flush_cache_all(void);
20462306a36Sopenharmony_civoid leon_flush_tlb_all(void);
20562306a36Sopenharmony_ciextern int leon_flush_during_switch;
20662306a36Sopenharmony_ciint leon_flush_needed(void);
20762306a36Sopenharmony_civoid leon_flush_pcache_all(struct vm_area_struct *vma, unsigned long page);
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci/* struct that hold LEON3 cache configuration registers */
21062306a36Sopenharmony_cistruct leon3_cacheregs {
21162306a36Sopenharmony_ci	unsigned long ccr;	/* 0x00 - Cache Control Register  */
21262306a36Sopenharmony_ci	unsigned long iccr;     /* 0x08 - Instruction Cache Configuration Register */
21362306a36Sopenharmony_ci	unsigned long dccr;	/* 0x0c - Data Cache Configuration Register */
21462306a36Sopenharmony_ci};
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci#include <linux/irq.h>
21762306a36Sopenharmony_ci#include <linux/interrupt.h>
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_cistruct device_node;
22062306a36Sopenharmony_cistruct task_struct;
22162306a36Sopenharmony_ciunsigned int leon_build_device_irq(unsigned int real_irq,
22262306a36Sopenharmony_ci				   irq_flow_handler_t flow_handler,
22362306a36Sopenharmony_ci				   const char *name, int do_ack);
22462306a36Sopenharmony_civoid leon_update_virq_handling(unsigned int virq,
22562306a36Sopenharmony_ci			       irq_flow_handler_t flow_handler,
22662306a36Sopenharmony_ci			       const char *name, int do_ack);
22762306a36Sopenharmony_civoid leon_init_timers(void);
22862306a36Sopenharmony_civoid leon_node_init(struct device_node *dp, struct device_node ***nextp);
22962306a36Sopenharmony_civoid init_leon(void);
23062306a36Sopenharmony_civoid poke_leonsparc(void);
23162306a36Sopenharmony_civoid leon3_getCacheRegs(struct leon3_cacheregs *regs);
23262306a36Sopenharmony_ciextern int leon3_ticker_irq;
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci#ifdef CONFIG_SMP
23562306a36Sopenharmony_ciint leon_smp_nrcpus(void);
23662306a36Sopenharmony_civoid leon_clear_profile_irq(int cpu);
23762306a36Sopenharmony_civoid leon_smp_done(void);
23862306a36Sopenharmony_civoid leon_boot_cpus(void);
23962306a36Sopenharmony_ciint leon_boot_one_cpu(int i, struct task_struct *);
24062306a36Sopenharmony_civoid leon_init_smp(void);
24162306a36Sopenharmony_civoid leon_enable_irq_cpu(unsigned int irq_nr, unsigned int cpu);
24262306a36Sopenharmony_ciirqreturn_t leon_percpu_timer_interrupt(int irq, void *unused);
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ciextern unsigned int smpleon_ipi[];
24562306a36Sopenharmony_ciextern unsigned int linux_trap_ipi15_leon[];
24662306a36Sopenharmony_ciextern int leon_ipi_irq;
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci#endif /* CONFIG_SMP */
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci#endif /* __ASSEMBLY__ */
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci/* macros used in leon_mm.c */
25362306a36Sopenharmony_ci#define PFN(x)           ((x) >> PAGE_SHIFT)
25462306a36Sopenharmony_ci#define _pfn_valid(pfn)	 ((pfn < last_valid_pfn) && (pfn >= PFN(phys_base)))
25562306a36Sopenharmony_ci#define _SRMMU_PTE_PMASK_LEON 0xffffffff
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ci/*
25862306a36Sopenharmony_ci * On LEON PCI Memory space is mapped 1:1 with physical address space.
25962306a36Sopenharmony_ci *
26062306a36Sopenharmony_ci * I/O space is located at low 64Kbytes in PCI I/O space. The I/O addresses
26162306a36Sopenharmony_ci * are converted into CPU addresses to virtual addresses that are mapped with
26262306a36Sopenharmony_ci * MMU to the PCI Host PCI I/O space window which are translated to the low
26362306a36Sopenharmony_ci * 64Kbytes by the Host controller.
26462306a36Sopenharmony_ci */
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci#endif
267