1 /*
2  * Copyright (C) 2004 Konrad Eisele (eiselekd@web.de,konrad@gaisler.com) Gaisler Research
3  * Copyright (C) 2004 Stefan Holst (mail@s-holst.de) Uni-Stuttgart
4  * Copyright (C) 2009 Daniel Hellstrom (daniel@gaisler.com) Aeroflex Gaisler AB
5  * Copyright (C) 2009 Konrad Eisele (konrad@gaisler.com) Aeroflex Gaisler AB
6  */
7 
8 #ifndef LEON_H_INCLUDE
9 #define LEON_H_INCLUDE
10 
11 #ifdef CONFIG_SPARC_LEON
12 
13 #define ASI_LEON_NOCACHE	0x01
14 
15 #define ASI_LEON_DCACHE_MISS	0x1
16 
17 #define ASI_LEON_CACHEREGS	0x02
18 #define ASI_LEON_IFLUSH		0x10
19 #define ASI_LEON_DFLUSH		0x11
20 
21 #define ASI_LEON_MMUFLUSH	0x18
22 #define ASI_LEON_MMUREGS	0x19
23 #define ASI_LEON_BYPASS		0x1c
24 #define ASI_LEON_FLUSH_PAGE	0x10
25 
26 /* mmu register access, ASI_LEON_MMUREGS */
27 #define LEON_CNR_CTRL		0x000
28 #define LEON_CNR_CTXP		0x100
29 #define LEON_CNR_CTX		0x200
30 #define LEON_CNR_F		0x300
31 #define LEON_CNR_FADDR		0x400
32 
33 #define LEON_CNR_CTX_NCTX	256	/*number of MMU ctx */
34 
35 #define LEON_CNR_CTRL_TLBDIS	0x80000000
36 
37 #define LEON_MMUTLB_ENT_MAX	64
38 
39 /*
40  * diagnostic access from mmutlb.vhd:
41  * 0: pte address
42  * 4: pte
43  * 8: additional flags
44  */
45 #define LEON_DIAGF_LVL		0x3
46 #define LEON_DIAGF_WR		0x8
47 #define LEON_DIAGF_WR_SHIFT	3
48 #define LEON_DIAGF_HIT		0x10
49 #define LEON_DIAGF_HIT_SHIFT	4
50 #define LEON_DIAGF_CTX		0x1fe0
51 #define LEON_DIAGF_CTX_SHIFT	5
52 #define LEON_DIAGF_VALID	0x2000
53 #define LEON_DIAGF_VALID_SHIFT	13
54 
55 /* irq masks */
56 #define LEON_HARD_INT(x)	(1 << (x))	/* irq 0-15 */
57 #define LEON_IRQMASK_R		0x0000fffe	/* bit 15- 1 of lregs.irqmask */
58 #define LEON_IRQPRIO_R		0xfffe0000	/* bit 31-17 of lregs.irqmask */
59 
60 /* leon uart register definitions */
61 #define LEON_OFF_UDATA	0x0
62 #define LEON_OFF_USTAT	0x4
63 #define LEON_OFF_UCTRL	0x8
64 #define LEON_OFF_USCAL	0xc
65 
66 #define LEON_UCTRL_RE	0x01
67 #define LEON_UCTRL_TE	0x02
68 #define LEON_UCTRL_RI	0x04
69 #define LEON_UCTRL_TI	0x08
70 #define LEON_UCTRL_PS	0x10
71 #define LEON_UCTRL_PE	0x20
72 #define LEON_UCTRL_FL	0x40
73 #define LEON_UCTRL_LB	0x80
74 
75 #define LEON_USTAT_DR	0x01
76 #define LEON_USTAT_TS	0x02
77 #define LEON_USTAT_TH	0x04
78 #define LEON_USTAT_BR	0x08
79 #define LEON_USTAT_OV	0x10
80 #define LEON_USTAT_PE	0x20
81 #define LEON_USTAT_FE	0x40
82 
83 #define LEON_MCFG2_SRAMDIS		0x00002000
84 #define LEON_MCFG2_SDRAMEN		0x00004000
85 #define LEON_MCFG2_SRAMBANKSZ		0x00001e00	/* [12-9] */
86 #define LEON_MCFG2_SRAMBANKSZ_SHIFT	9
87 #define LEON_MCFG2_SDRAMBANKSZ		0x03800000	/* [25-23] */
88 #define LEON_MCFG2_SDRAMBANKSZ_SHIFT	23
89 
90 #define LEON_TCNT0_MASK	0x7fffff
91 
92 #define LEON_USTAT_ERROR (LEON_USTAT_OV | LEON_USTAT_PE | LEON_USTAT_FE)
93 /* no break yet */
94 
95 #define ASI_LEON3_SYSCTRL		0x02
96 #define ASI_LEON3_SYSCTRL_ICFG		0x08
97 #define ASI_LEON3_SYSCTRL_DCFG		0x0c
98 #define ASI_LEON3_SYSCTRL_CFG_SNOOPING (1 << 27)
99 #define ASI_LEON3_SYSCTRL_CFG_SSIZE(c) (1 << ((c >> 20) & 0xf))
100 
101 #ifndef __ASSEMBLY__
102 
103 /* do a virtual address read without cache */
leon_readnobuffer_reg(unsigned long paddr)104 static inline unsigned long leon_readnobuffer_reg(unsigned long paddr)
105 {
106 	unsigned long retval;
107 	__asm__ __volatile__("lda [%1] %2, %0\n\t" :
108 			     "=r"(retval) : "r"(paddr), "i"(ASI_LEON_NOCACHE));
109 	return retval;
110 }
111 
112 /* do a physical address bypass write, i.e. for 0x80000000 */
leon_store_reg(unsigned long paddr,unsigned long value)113 static inline void leon_store_reg(unsigned long paddr, unsigned long value)
114 {
115 	__asm__ __volatile__("sta %0, [%1] %2\n\t" : : "r"(value), "r"(paddr),
116 			     "i"(ASI_LEON_BYPASS) : "memory");
117 }
118 
119 /* do a physical address bypass load, i.e. for 0x80000000 */
leon_load_reg(unsigned long paddr)120 static inline unsigned long leon_load_reg(unsigned long paddr)
121 {
122 	unsigned long retval;
123 	__asm__ __volatile__("lda [%1] %2, %0\n\t" :
124 			     "=r"(retval) : "r"(paddr), "i"(ASI_LEON_BYPASS));
125 	return retval;
126 }
127 
leon_srmmu_disabletlb(void)128 static inline void leon_srmmu_disabletlb(void)
129 {
130 	unsigned int retval;
131 	__asm__ __volatile__("lda [%%g0] %2, %0\n\t" : "=r"(retval) : "r"(0),
132 			     "i"(ASI_LEON_MMUREGS));
133 	retval |= LEON_CNR_CTRL_TLBDIS;
134 	__asm__ __volatile__("sta %0, [%%g0] %2\n\t" : : "r"(retval), "r"(0),
135 			     "i"(ASI_LEON_MMUREGS) : "memory");
136 }
137 
leon_srmmu_enabletlb(void)138 static inline void leon_srmmu_enabletlb(void)
139 {
140 	unsigned int retval;
141 	__asm__ __volatile__("lda [%%g0] %2, %0\n\t" : "=r"(retval) : "r"(0),
142 			     "i"(ASI_LEON_MMUREGS));
143 	retval = retval & ~LEON_CNR_CTRL_TLBDIS;
144 	__asm__ __volatile__("sta %0, [%%g0] %2\n\t" : : "r"(retval), "r"(0),
145 			     "i"(ASI_LEON_MMUREGS) : "memory");
146 }
147 
148 /* macro access for leon_load_reg() and leon_store_reg() */
149 #define LEON3_BYPASS_LOAD_PA(x)	    (leon_load_reg((unsigned long)(x)))
150 #define LEON3_BYPASS_STORE_PA(x, v) (leon_store_reg((unsigned long)(x), (unsigned long)(v)))
151 #define LEON3_BYPASS_ANDIN_PA(x, v) LEON3_BYPASS_STORE_PA(x, LEON3_BYPASS_LOAD_PA(x) & v)
152 #define LEON3_BYPASS_ORIN_PA(x, v)  LEON3_BYPASS_STORE_PA(x, LEON3_BYPASS_LOAD_PA(x) | v)
153 #define LEON_BYPASS_LOAD_PA(x)      leon_load_reg((unsigned long)(x))
154 #define LEON_BYPASS_STORE_PA(x, v)  leon_store_reg((unsigned long)(x), (unsigned long)(v))
155 #define LEON_REGLOAD_PA(x)          leon_load_reg((unsigned long)(x)+LEON_PREGS)
156 #define LEON_REGSTORE_PA(x, v)      leon_store_reg((unsigned long)(x)+LEON_PREGS, (unsigned long)(v))
157 #define LEON_REGSTORE_OR_PA(x, v)   LEON_REGSTORE_PA(x, LEON_REGLOAD_PA(x) | (unsigned long)(v))
158 #define LEON_REGSTORE_AND_PA(x, v)  LEON_REGSTORE_PA(x, LEON_REGLOAD_PA(x) & (unsigned long)(v))
159 
160 /* macro access for leon_readnobuffer_reg() */
161 #define LEON_BYPASSCACHE_LOAD_VA(x) leon_readnobuffer_reg((unsigned long)(x))
162 
163 extern void leon_init(void);
164 extern void leon_switch_mm(void);
165 extern void leon_init_IRQ(void);
166 
167 extern unsigned long last_valid_pfn;
168 
sparc_leon3_get_dcachecfg(void)169 static inline unsigned long sparc_leon3_get_dcachecfg(void)
170 {
171 	unsigned int retval;
172 	__asm__ __volatile__("lda [%1] %2, %0\n\t" :
173 			     "=r"(retval) :
174 			     "r"(ASI_LEON3_SYSCTRL_DCFG),
175 			     "i"(ASI_LEON3_SYSCTRL));
176 	return retval;
177 }
178 
179 /* enable snooping */
sparc_leon3_enable_snooping(void)180 static inline void sparc_leon3_enable_snooping(void)
181 {
182 	__asm__ __volatile__ ("lda [%%g0] 2, %%l1\n\t"
183 			  "set 0x800000, %%l2\n\t"
184 			  "or  %%l2, %%l1, %%l2\n\t"
185 			  "sta %%l2, [%%g0] 2\n\t" : : : "l1", "l2");
186 };
187 
sparc_leon3_snooping_enabled(void)188 static inline int sparc_leon3_snooping_enabled(void)
189 {
190 	u32 cctrl;
191 	__asm__ __volatile__("lda [%%g0] 2, %0\n\t" : "=r"(cctrl));
192         return (cctrl >> 23) & 1;
193 };
194 
sparc_leon3_disable_cache(void)195 static inline void sparc_leon3_disable_cache(void)
196 {
197 	__asm__ __volatile__ ("lda [%%g0] 2, %%l1\n\t"
198 			  "set 0x00000f, %%l2\n\t"
199 			  "andn  %%l2, %%l1, %%l2\n\t"
200 			  "sta %%l2, [%%g0] 2\n\t" : : : "l1", "l2");
201 };
202 
sparc_leon3_asr17(void)203 static inline unsigned long sparc_leon3_asr17(void)
204 {
205 	u32 asr17;
206 	__asm__ __volatile__ ("rd %%asr17, %0\n\t" : "=r"(asr17));
207 	return asr17;
208 };
209 
sparc_leon3_cpuid(void)210 static inline int sparc_leon3_cpuid(void)
211 {
212 	return sparc_leon3_asr17() >> 28;
213 }
214 
215 #endif /*!__ASSEMBLY__*/
216 
217 #ifdef CONFIG_SMP
218 # define LEON3_IRQ_IPI_DEFAULT		13
219 # define LEON3_IRQ_TICKER		(leon3_ticker_irq)
220 # define LEON3_IRQ_CROSS_CALL		15
221 #endif
222 
223 #if defined(PAGE_SIZE_LEON_8K)
224 #define LEON_PAGE_SIZE_LEON 1
225 #elif defined(PAGE_SIZE_LEON_16K)
226 #define LEON_PAGE_SIZE_LEON 2)
227 #else
228 #define LEON_PAGE_SIZE_LEON 0
229 #endif
230 
231 #if LEON_PAGE_SIZE_LEON == 0
232 /* [ 8, 6, 6 ] + 12 */
233 #define LEON_PGD_SH    24
234 #define LEON_PGD_M     0xff
235 #define LEON_PMD_SH    18
236 #define LEON_PMD_SH_V  (LEON_PGD_SH-2)
237 #define LEON_PMD_M     0x3f
238 #define LEON_PTE_SH    12
239 #define LEON_PTE_M     0x3f
240 #elif LEON_PAGE_SIZE_LEON == 1
241 /* [ 7, 6, 6 ] + 13 */
242 #define LEON_PGD_SH    25
243 #define LEON_PGD_M     0x7f
244 #define LEON_PMD_SH    19
245 #define LEON_PMD_SH_V  (LEON_PGD_SH-1)
246 #define LEON_PMD_M     0x3f
247 #define LEON_PTE_SH    13
248 #define LEON_PTE_M     0x3f
249 #elif LEON_PAGE_SIZE_LEON == 2
250 /* [ 6, 6, 6 ] + 14 */
251 #define LEON_PGD_SH    26
252 #define LEON_PGD_M     0x3f
253 #define LEON_PMD_SH    20
254 #define LEON_PMD_SH_V  (LEON_PGD_SH-0)
255 #define LEON_PMD_M     0x3f
256 #define LEON_PTE_SH    14
257 #define LEON_PTE_M     0x3f
258 #elif LEON_PAGE_SIZE_LEON == 3
259 /* [ 4, 7, 6 ] + 15 */
260 #define LEON_PGD_SH    28
261 #define LEON_PGD_M     0x0f
262 #define LEON_PMD_SH    21
263 #define LEON_PMD_SH_V  (LEON_PGD_SH-0)
264 #define LEON_PMD_M     0x7f
265 #define LEON_PTE_SH    15
266 #define LEON_PTE_M     0x3f
267 #else
268 #error cannot determine LEON_PAGE_SIZE_LEON
269 #endif
270 
271 #define PAGE_MIN_SHIFT   (12)
272 #define PAGE_MIN_SIZE    (1UL << PAGE_MIN_SHIFT)
273 
274 #define LEON3_XCCR_SETS_MASK  0x07000000UL
275 #define LEON3_XCCR_SSIZE_MASK 0x00f00000UL
276 
277 #define LEON2_CCR_DSETS_MASK 0x03000000UL
278 #define LEON2_CFG_SSIZE_MASK 0x00007000UL
279 
280 #ifndef __ASSEMBLY__
281 extern unsigned long srmmu_swprobe(unsigned long vaddr, unsigned long *paddr);
282 extern void leon_flush_icache_all(void);
283 extern void leon_flush_dcache_all(void);
284 extern void leon_flush_cache_all(void);
285 extern void leon_flush_tlb_all(void);
286 extern int leon_flush_during_switch;
287 extern int leon_flush_needed(void);
288 
289 struct vm_area_struct;
290 extern void leon_flush_icache_all(void);
291 extern void leon_flush_dcache_all(void);
292 extern void leon_flush_pcache_all(struct vm_area_struct *vma, unsigned long page);
293 extern void leon_flush_cache_all(void);
294 extern void leon_flush_tlb_all(void);
295 extern int leon_flush_during_switch;
296 extern int leon_flush_needed(void);
297 extern void leon_flush_pcache_all(struct vm_area_struct *vma, unsigned long page);
298 
299 /* struct that hold LEON3 cache configuration registers */
300 struct leon3_cacheregs {
301 	unsigned long ccr;	/* 0x00 - Cache Control Register  */
302 	unsigned long iccr;     /* 0x08 - Instruction Cache Configuration Register */
303 	unsigned long dccr;	/* 0x0c - Data Cache Configuration Register */
304 };
305 
306 /* struct that hold LEON2 cache configuration register
307  * & configuration register
308  */
309 struct leon2_cacheregs {
310 	unsigned long ccr, cfg;
311 };
312 
313 #ifdef __KERNEL__
314 
315 #include <linux/interrupt.h>
316 
317 struct device_node;
318 extern unsigned int leon_build_device_irq(unsigned int real_irq,
319 					   irq_flow_handler_t flow_handler,
320 					   const char *name, int do_ack);
321 extern void leon_update_virq_handling(unsigned int virq,
322 			      irq_flow_handler_t flow_handler,
323 			      const char *name, int do_ack);
324 extern void leon_clear_clock_irq(void);
325 extern void leon_load_profile_irq(int cpu, unsigned int limit);
326 extern void leon_init_timers(irq_handler_t counter_fn);
327 extern void leon_clear_clock_irq(void);
328 extern void leon_load_profile_irq(int cpu, unsigned int limit);
329 extern void leon_trans_init(struct device_node *dp);
330 extern void leon_node_init(struct device_node *dp, struct device_node ***nextp);
331 extern void leon_init_IRQ(void);
332 extern void leon_init(void);
333 extern unsigned long srmmu_swprobe(unsigned long vaddr, unsigned long *paddr);
334 extern void init_leon(void);
335 extern void poke_leonsparc(void);
336 extern void leon3_getCacheRegs(struct leon3_cacheregs *regs);
337 extern int leon_flush_needed(void);
338 extern void leon_switch_mm(void);
339 extern int srmmu_swprobe_trace;
340 extern int leon3_ticker_irq;
341 
342 #ifdef CONFIG_SMP
343 extern int leon_smp_nrcpus(void);
344 extern void leon_clear_profile_irq(int cpu);
345 extern void leon_smp_done(void);
346 extern void leon_boot_cpus(void);
347 extern int leon_boot_one_cpu(int i);
348 void leon_init_smp(void);
349 extern void cpu_idle(void);
350 extern void init_IRQ(void);
351 extern void cpu_panic(void);
352 extern int __leon_processor_id(void);
353 void leon_enable_irq_cpu(unsigned int irq_nr, unsigned int cpu);
354 extern irqreturn_t leon_percpu_timer_interrupt(int irq, void *unused);
355 
356 extern unsigned int real_irq_entry[];
357 extern unsigned int smpleon_ipi[];
358 extern unsigned int patchme_maybe_smp_msg[];
359 extern unsigned int t_nmi[], linux_trap_ipi15_leon[];
360 extern unsigned int linux_trap_ipi15_sun4m[];
361 extern int leon_ipi_irq;
362 
363 #endif /* CONFIG_SMP */
364 
365 #endif /* __KERNEL__ */
366 
367 #endif /* __ASSEMBLY__ */
368 
369 /* macros used in leon_mm.c */
370 #define PFN(x)           ((x) >> PAGE_SHIFT)
371 #define _pfn_valid(pfn)	 ((pfn < last_valid_pfn) && (pfn >= PFN(phys_base)))
372 #define _SRMMU_PTE_PMASK_LEON 0xffffffff
373 
374 #else /* defined(CONFIG_SPARC_LEON) */
375 
376 /* nop definitions for !LEON case */
377 #define leon_init() do {} while (0)
378 #define leon_switch_mm() do {} while (0)
379 #define leon_init_IRQ() do {} while (0)
380 #define init_leon() do {} while (0)
381 #define leon_smp_done() do {} while (0)
382 #define leon_boot_cpus() do {} while (0)
383 #define leon_boot_one_cpu(i) 1
384 #define leon_init_smp() do {} while (0)
385 
386 #endif /* !defined(CONFIG_SPARC_LEON) */
387 
388 #endif
389