1205354Simp/*-
2205354Simp * Copyright (c) 1994-1998 Mark Brinicombe.
3205354Simp * Copyright (c) 1994 Brini.
4205354Simp * All rights reserved.
5205354Simp *
6205354Simp * This code is derived from software written for Brini by Mark Brinicombe
7205354Simp *
8205354Simp * Redistribution and use in source and binary forms, with or without
9205354Simp * modification, are permitted provided that the following conditions
10205354Simp * are met:
11205354Simp * 1. Redistributions of source code must retain the above copyright
12205354Simp *    notice, this list of conditions and the following disclaimer.
13205354Simp * 2. Redistributions in binary form must reproduce the above copyright
14205354Simp *    notice, this list of conditions and the following disclaimer in the
15205354Simp *    documentation and/or other materials provided with the distribution.
16205354Simp * 3. All advertising materials mentioning features or use of this software
17205354Simp *    must display the following acknowledgement:
18205354Simp *      This product includes software developed by Brini.
19205354Simp * 4. The name of the company nor the name of the author may be used to
20205354Simp *    endorse or promote products derived from this software without specific
21205354Simp *    prior written permission.
22205354Simp *
23205354Simp * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
24205354Simp * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25205354Simp * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26205354Simp * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27205354Simp * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28205354Simp * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29205354Simp * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30205354Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31205354Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32205354Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33205354Simp * SUCH DAMAGE.
34205354Simp *
35205354Simp * RiscBSD kernel project
36205354Simp *
37205354Simp * machdep.c
38205354Simp *
39205354Simp * Machine dependant functions for kernel setup
40205354Simp *
41236990Simp * This file needs a lot of work.
42205354Simp *
43205354Simp * Created      : 17/09/94
44205354Simp */
45205354Simp
46205354Simp#include "opt_ddb.h"
47205354Simp
48205354Simp#include <sys/cdefs.h>
49205354Simp__FBSDID("$FreeBSD$");
50205354Simp
51205354Simp#define _ARM32_BUS_DMA_PRIVATE
52205354Simp#include <sys/param.h>
53205354Simp#include <sys/systm.h>
54205354Simp#include <sys/sysproto.h>
55205354Simp#include <sys/signalvar.h>
56205354Simp#include <sys/imgact.h>
57205354Simp#include <sys/kernel.h>
58205354Simp#include <sys/ktr.h>
59205354Simp#include <sys/linker.h>
60205354Simp#include <sys/lock.h>
61205354Simp#include <sys/malloc.h>
62205354Simp#include <sys/mutex.h>
63205354Simp#include <sys/pcpu.h>
64205354Simp#include <sys/proc.h>
65205354Simp#include <sys/ptrace.h>
66205354Simp#include <sys/cons.h>
67205354Simp#include <sys/bio.h>
68205354Simp#include <sys/bus.h>
69205354Simp#include <sys/buf.h>
70205354Simp#include <sys/exec.h>
71205354Simp#include <sys/kdb.h>
72205354Simp#include <sys/msgbuf.h>
73205354Simp#include <machine/reg.h>
74205354Simp#include <machine/cpu.h>
75205354Simp
76205354Simp#include <vm/vm.h>
77205354Simp#include <vm/pmap.h>
78205354Simp#include <vm/vm_object.h>
79205354Simp#include <vm/vm_page.h>
80205354Simp#include <vm/vm_map.h>
81205354Simp#include <machine/vmparam.h>
82205354Simp#include <machine/pcb.h>
83205354Simp#include <machine/undefined.h>
84205354Simp#include <machine/machdep.h>
85205354Simp#include <machine/metadata.h>
86205354Simp#include <machine/armreg.h>
87205354Simp#include <machine/bus.h>
88205354Simp#include <sys/reboot.h>
89205354Simp
90205354Simp#include <arm/s3c2xx0/s3c24x0var.h>
91205354Simp#include <arm/s3c2xx0/s3c2410reg.h>
92205354Simp#include <arm/s3c2xx0/s3c2xx0board.h>
93205354Simp
94205354Simp/* Page table for mapping proc0 zero page */
95205354Simp#define KERNEL_PT_SYS		0
96205354Simp#define KERNEL_PT_KERN		1
97205354Simp#define KERNEL_PT_KERN_NUM	44
98205354Simp/* L2 table for mapping after kernel */
99205354Simp#define KERNEL_PT_AFKERNEL	KERNEL_PT_KERN + KERNEL_PT_KERN_NUM
100205354Simp#define	KERNEL_PT_AFKERNEL_NUM	5
101205354Simp
102205354Simp/* this should be evenly divisable by PAGE_SIZE / L2_TABLE_SIZE_REAL (or 4) */
103205354Simp#define NUM_KERNEL_PTS		(KERNEL_PT_AFKERNEL + KERNEL_PT_AFKERNEL_NUM)
104205354Simp
105205354Simpextern int s3c2410_pclk;
106205354Simp
107205354Simpextern u_int data_abort_handler_address;
108205354Simpextern u_int prefetch_abort_handler_address;
109205354Simpextern u_int undefined_handler_address;
110205354Simp
111205354Simpstruct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
112205354Simp
113205354Simp/* Physical and virtual addresses for some global pages */
114205354Simp
115205354Simpvm_paddr_t phys_avail[10];
116205354Simpvm_paddr_t dump_avail[4];
117205354Simp
118205354Simpstruct pv_addr systempage;
119205354Simpstruct pv_addr msgbufpv;
120205354Simpstruct pv_addr irqstack;
121205354Simpstruct pv_addr undstack;
122205354Simpstruct pv_addr abtstack;
123205354Simpstruct pv_addr kernelstack;
124205354Simp
125205354Simp#define	_A(a)	((a) & ~L1_S_OFFSET)
126205354Simp#define	_S(s)	(((s) + L1_S_SIZE - 1) & ~(L1_S_SIZE-1))
127205354Simp
128205354Simp/* Static device mappings. */
129205354Simpstatic const struct pmap_devmap s3c24x0_devmap[] = {
130210396Sandrew	/*
131210396Sandrew	 * Map the devices we need early on.
132210396Sandrew	 */
133205354Simp	{
134205354Simp		_A(S3C24X0_CLKMAN_BASE),
135205354Simp		_A(S3C24X0_CLKMAN_PA_BASE),
136205354Simp		_S(S3C24X0_CLKMAN_SIZE),
137236990Simp		VM_PROT_READ|VM_PROT_WRITE,
138205354Simp		PTE_NOCACHE,
139205354Simp	},
140205354Simp	{
141205354Simp		_A(S3C24X0_GPIO_BASE),
142205354Simp		_A(S3C24X0_GPIO_PA_BASE),
143205354Simp		_S(S3C2410_GPIO_SIZE),
144236990Simp		VM_PROT_READ|VM_PROT_WRITE,
145205354Simp		PTE_NOCACHE,
146205354Simp	},
147205354Simp	{
148205354Simp		_A(S3C24X0_INTCTL_BASE),
149205354Simp		_A(S3C24X0_INTCTL_PA_BASE),
150205354Simp		_S(S3C24X0_INTCTL_SIZE),
151236990Simp		VM_PROT_READ|VM_PROT_WRITE,
152205354Simp		PTE_NOCACHE,
153205354Simp	},
154205354Simp	{
155210396Sandrew		_A(S3C24X0_TIMER_BASE),
156210396Sandrew		_A(S3C24X0_TIMER_PA_BASE),
157210396Sandrew		_S(S3C24X0_TIMER_SIZE),
158236990Simp		VM_PROT_READ|VM_PROT_WRITE,
159205354Simp		PTE_NOCACHE,
160205354Simp	},
161205354Simp	{
162205354Simp		_A(S3C24X0_UART0_BASE),
163205354Simp		_A(S3C24X0_UART0_PA_BASE),
164205354Simp		_S(S3C24X0_UART_PA_BASE(3) - S3C24X0_UART0_PA_BASE),
165236990Simp		VM_PROT_READ|VM_PROT_WRITE,
166205354Simp		PTE_NOCACHE,
167205354Simp	},
168205354Simp	{
169205354Simp		_A(S3C24X0_WDT_BASE),
170205354Simp		_A(S3C24X0_WDT_PA_BASE),
171205354Simp		_S(S3C24X0_WDT_SIZE),
172236990Simp		VM_PROT_READ|VM_PROT_WRITE,
173205354Simp		PTE_NOCACHE,
174205354Simp	},
175205354Simp	{
176205354Simp		0,
177205354Simp		0,
178205354Simp		0,
179205354Simp		0,
180205354Simp		0,
181205354Simp	}
182205354Simp};
183205354Simp
184205354Simp#undef	_A
185205354Simp#undef	_S
186205354Simp
187205354Simp#define	ioreg_read32(a)  	(*(volatile uint32_t *)(a))
188205354Simp#define	ioreg_write32(a,v)	(*(volatile uint32_t *)(a)=(v))
189205354Simp
190205354Simpstruct arm32_dma_range s3c24x0_range = {
191205354Simp	.dr_sysbase = 0,
192205354Simp	.dr_busbase = 0,
193205354Simp	.dr_len = 0,
194205354Simp};
195205354Simp
196205354Simpstruct arm32_dma_range *
197205354Simpbus_dma_get_range(void)
198205354Simp{
199205354Simp
200205354Simp	if (s3c24x0_range.dr_len == 0) {
201205354Simp		s3c24x0_range.dr_sysbase = dump_avail[0];
202205354Simp		s3c24x0_range.dr_busbase = dump_avail[0];
203205354Simp		s3c24x0_range.dr_len = dump_avail[1] - dump_avail[0];
204205354Simp	}
205205354Simp	return (&s3c24x0_range);
206205354Simp}
207205354Simp
208205354Simpint
209205354Simpbus_dma_get_range_nb(void)
210205354Simp{
211205354Simp	return (1);
212205354Simp}
213205354Simp
214205354Simpvoid *
215236524Simpinitarm(struct arm_boot_params *abp)
216205354Simp{
217205354Simp	struct pv_addr	kernel_l1pt;
218205354Simp	int loop;
219205354Simp	u_int l1pagetable;
220205354Simp	vm_offset_t freemempos;
221205354Simp	vm_offset_t afterkern;
222205354Simp	vm_offset_t lastaddr;
223205354Simp
224205354Simp	int i;
225205354Simp	uint32_t memsize;
226205354Simp
227237040Simp	boothowto = 0;  /* Likely not needed */
228237040Simp	lastaddr = parse_boot_param(abp);
229205354Simp	i = 0;
230205354Simp	set_cpufuncs();
231205354Simp	cpufuncs.cf_sleep = s3c24x0_sleep;
232205354Simp
233239268Sgonzo	pcpu0_init();
234205354Simp
235218913Scognet	/* Do basic tuning, hz etc */
236218913Scognet	init_param1();
237218913Scognet
238205354Simp#define KERNEL_TEXT_BASE (KERNBASE)
239205354Simp	freemempos = (lastaddr + PAGE_MASK) & ~PAGE_MASK;
240205354Simp	/* Define a macro to simplify memory allocation */
241205354Simp#define valloc_pages(var, np)			\
242205354Simp	alloc_pages((var).pv_va, (np));		\
243205354Simp	(var).pv_pa = (var).pv_va + (KERNPHYSADDR - KERNVIRTADDR);
244205354Simp
245205354Simp#define alloc_pages(var, np)			\
246205354Simp	(var) = freemempos;			\
247205354Simp	freemempos += (np * PAGE_SIZE);		\
248205354Simp	memset((char *)(var), 0, ((np) * PAGE_SIZE));
249205354Simp
250205354Simp	while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
251205354Simp		freemempos += PAGE_SIZE;
252205354Simp	valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
253205354Simp	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
254205354Simp		if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
255205354Simp			valloc_pages(kernel_pt_table[loop],
256205354Simp			    L2_TABLE_SIZE / PAGE_SIZE);
257205354Simp		} else {
258205354Simp			kernel_pt_table[loop].pv_va = freemempos -
259205354Simp			    (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) *
260205354Simp			    L2_TABLE_SIZE_REAL;
261236990Simp			kernel_pt_table[loop].pv_pa =
262205354Simp			    kernel_pt_table[loop].pv_va - KERNVIRTADDR +
263205354Simp			    KERNPHYSADDR;
264205354Simp		}
265205354Simp	}
266205354Simp	/*
267205354Simp	 * Allocate a page for the system page mapped to V0x00000000
268205354Simp	 * This page will just contain the system vectors and can be
269205354Simp	 * shared by all processes.
270205354Simp	 */
271205354Simp	valloc_pages(systempage, 1);
272205354Simp
273205354Simp	/* Allocate stacks for all modes */
274205354Simp	valloc_pages(irqstack, IRQ_STACK_SIZE);
275205354Simp	valloc_pages(abtstack, ABT_STACK_SIZE);
276205354Simp	valloc_pages(undstack, UND_STACK_SIZE);
277205354Simp	valloc_pages(kernelstack, KSTACK_PAGES);
278217688Spluknet	valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
279205354Simp	/*
280205354Simp	 * Now we start construction of the L1 page table
281205354Simp	 * We start by mapping the L2 page tables into the L1.
282205354Simp	 * This means that we can replace L1 mappings later on if necessary
283205354Simp	 */
284205354Simp	l1pagetable = kernel_l1pt.pv_va;
285205354Simp
286205354Simp	/* Map the L2 pages tables in the L1 page table */
287205354Simp	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH,
288205354Simp	    &kernel_pt_table[KERNEL_PT_SYS]);
289205354Simp	for (i = 0; i < KERNEL_PT_KERN_NUM; i++)
290205354Simp		pmap_link_l2pt(l1pagetable, KERNBASE + i * L1_S_SIZE,
291205354Simp		    &kernel_pt_table[KERNEL_PT_KERN + i]);
292205354Simp	pmap_map_chunk(l1pagetable, KERNBASE, PHYSADDR,
293205354Simp	   (((uint32_t)(lastaddr) - KERNBASE) + PAGE_SIZE) & ~(PAGE_SIZE - 1),
294205354Simp	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
295236990Simp	afterkern = round_page((lastaddr + L1_S_SIZE) & ~(L1_S_SIZE
296205354Simp	    - 1));
297205354Simp	for (i = 0; i < KERNEL_PT_AFKERNEL_NUM; i++) {
298205354Simp		pmap_link_l2pt(l1pagetable, afterkern + i * L1_S_SIZE,
299205354Simp		    &kernel_pt_table[KERNEL_PT_AFKERNEL + i]);
300205354Simp	}
301205354Simp
302205354Simp	/* Map the vector page. */
303205354Simp	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
304205354Simp	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
305205354Simp	/* Map the stack pages */
306205354Simp	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
307205354Simp	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
308205354Simp	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
309205354Simp	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
310205354Simp	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
311205354Simp	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
312205354Simp	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
313205354Simp	    KSTACK_PAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
314205354Simp
315205354Simp	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
316205354Simp	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
317205354Simp	pmap_map_chunk(l1pagetable, msgbufpv.pv_va, msgbufpv.pv_pa,
318217688Spluknet	    msgbufsize, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
319205354Simp
320205354Simp
321205354Simp	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
322205354Simp		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
323205354Simp		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
324205354Simp		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
325205354Simp	}
326205354Simp
327205354Simp	pmap_devmap_bootstrap(l1pagetable, s3c24x0_devmap);
328205354Simp
329205354Simp	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
330205354Simp	setttb(kernel_l1pt.pv_pa);
331205354Simp	cpu_tlb_flushID();
332205354Simp	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
333205354Simp
334205354Simp	/*
335205354Simp	 * Pages were allocated during the secondary bootstrap for the
336205354Simp	 * stacks for different CPU modes.
337205354Simp	 * We must now set the r13 registers in the different CPU modes to
338205354Simp	 * point to these stacks.
339205354Simp	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
340205354Simp	 * of the stack memory.
341205354Simp	 */
342205354Simp
343205354Simp	cpu_control(CPU_CONTROL_MMU_ENABLE, CPU_CONTROL_MMU_ENABLE);
344240802Sandrew	set_stackptrs(0);
345205354Simp
346205354Simp	/*
347205354Simp	 * We must now clean the cache again....
348205354Simp	 * Cleaning may be done by reading new data to displace any
349205354Simp	 * dirty data in the cache. This will have happened in setttb()
350205354Simp	 * but since we are boot strapping the addresses used for the read
351205354Simp	 * may have just been remapped and thus the cache could be out
352205354Simp	 * of sync. A re-clean after the switch will cure this.
353205354Simp	 * After booting there are no gross reloations of the kernel thus
354205354Simp	 * this problem will not occur after initarm().
355205354Simp	 */
356205354Simp	cpu_idcache_wbinv_all();
357205354Simp
358205354Simp	/* Disable all peripheral interrupts */
359205354Simp	ioreg_write32(S3C24X0_INTCTL_BASE + INTCTL_INTMSK, ~0);
360205354Simp	memsize = board_init();
361205354Simp	/* Find pclk for uart */
362205354Simp	switch(ioreg_read32(S3C24X0_GPIO_BASE + GPIO_GSTATUS1) >> 16) {
363205354Simp	case 0x3241:
364205354Simp		s3c2410_clock_freq2(S3C24X0_CLKMAN_BASE, NULL, NULL,
365205354Simp		    &s3c2410_pclk);
366205354Simp		break;
367205354Simp	case 0x3244:
368205354Simp		s3c2440_clock_freq2(S3C24X0_CLKMAN_BASE, NULL, NULL,
369205354Simp		    &s3c2410_pclk);
370205354Simp		break;
371205354Simp	}
372205354Simp	cninit();
373205354Simp
374205354Simp	/* Set stack for exception handlers */
375205354Simp	data_abort_handler_address = (u_int)data_abort_handler;
376205354Simp	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
377205354Simp	undefined_handler_address = (u_int)undefinedinstruction_bounce;
378205354Simp	undefined_init();
379205354Simp
380236828Sandrew	init_proc0(kernelstack.pv_va);
381236828Sandrew
382205354Simp	arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
383205354Simp
384205354Simp	pmap_curmaxkvaddr = afterkern + 0x100000 * (KERNEL_PT_KERN_NUM - 1);
385237045Simp	arm_dump_avail_init(memsize, sizeof(dump_avail) / sizeof(dump_avail[0]));
386247046Salc	vm_max_kernel_address = KERNVIRTADDR + 3 * memsize;
387247046Salc	pmap_bootstrap(freemempos, &kernel_l1pt);
388205354Simp	msgbufp = (void*)msgbufpv.pv_va;
389217688Spluknet	msgbufinit(msgbufp, msgbufsize);
390205354Simp	mutex_init();
391205354Simp
392205354Simp	physmem = memsize / PAGE_SIZE;
393205354Simp
394205354Simp	phys_avail[0] = virtual_avail - KERNVIRTADDR + KERNPHYSADDR;
395205354Simp	phys_avail[1] = PHYSADDR + memsize;
396205354Simp	phys_avail[2] = 0;
397205354Simp	phys_avail[3] = 0;
398205354Simp
399205354Simp	init_param2(physmem);
400205354Simp	kdb_init();
401205354Simp
402205354Simp	return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
403205354Simp	    sizeof(struct pcb)));
404205354Simp}
405