1161592Scognet/*	$NetBSD: hpc_machdep.c,v 1.70 2003/09/16 08:18:22 agc Exp $	*/
2161592Scognet
3161592Scognet/*-
4161592Scognet * Copyright (c) 1994-1998 Mark Brinicombe.
5161592Scognet * Copyright (c) 1994 Brini.
6161592Scognet * All rights reserved.
7161592Scognet *
8161592Scognet * This code is derived from software written for Brini by Mark Brinicombe
9161592Scognet *
10161592Scognet * Redistribution and use in source and binary forms, with or without
11161592Scognet * modification, are permitted provided that the following conditions
12161592Scognet * are met:
13161592Scognet * 1. Redistributions of source code must retain the above copyright
14161592Scognet *    notice, this list of conditions and the following disclaimer.
15161592Scognet * 2. Redistributions in binary form must reproduce the above copyright
16161592Scognet *    notice, this list of conditions and the following disclaimer in the
17161592Scognet *    documentation and/or other materials provided with the distribution.
18161592Scognet * 3. All advertising materials mentioning features or use of this software
19161592Scognet *    must display the following acknowledgement:
20161592Scognet *      This product includes software developed by Brini.
21161592Scognet * 4. The name of the company nor the name of the author may be used to
22161592Scognet *    endorse or promote products derived from this software without specific
23161592Scognet *    prior written permission.
24161592Scognet *
25161592Scognet * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
26161592Scognet * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27161592Scognet * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28161592Scognet * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29161592Scognet * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30161592Scognet * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31161592Scognet * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32161592Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33161592Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34161592Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35161592Scognet * SUCH DAMAGE.
36161592Scognet *
37161592Scognet * RiscBSD kernel project
38161592Scognet *
39161592Scognet * machdep.c
40161592Scognet *
41161592Scognet * Machine dependant functions for kernel setup
42161592Scognet *
43236987Simp * This file needs a lot of work.
44161592Scognet *
45161592Scognet * Created      : 17/09/94
46161592Scognet */
47161592Scognet
48161592Scognet#include <sys/cdefs.h>
49161592Scognet__FBSDID("$FreeBSD: releng/10.3/sys/arm/xscale/i80321/ep80219_machdep.c 278727 2015-02-13 22:32:02Z ian $");
50161592Scognet
51161592Scognet#define _ARM32_BUS_DMA_PRIVATE
52161592Scognet#include <sys/param.h>
53161592Scognet#include <sys/systm.h>
54161592Scognet#include <sys/sysproto.h>
55161592Scognet#include <sys/signalvar.h>
56161592Scognet#include <sys/imgact.h>
57161592Scognet#include <sys/kernel.h>
58161592Scognet#include <sys/ktr.h>
59161592Scognet#include <sys/linker.h>
60161592Scognet#include <sys/lock.h>
61161592Scognet#include <sys/malloc.h>
62161592Scognet#include <sys/mutex.h>
63161592Scognet#include <sys/pcpu.h>
64161592Scognet#include <sys/proc.h>
65161592Scognet#include <sys/ptrace.h>
66161592Scognet#include <sys/cons.h>
67161592Scognet#include <sys/bio.h>
68161592Scognet#include <sys/bus.h>
69161592Scognet#include <sys/buf.h>
70161592Scognet#include <sys/exec.h>
71161592Scognet#include <sys/kdb.h>
72161592Scognet#include <sys/msgbuf.h>
73161592Scognet#include <machine/reg.h>
74161592Scognet#include <machine/cpu.h>
75161592Scognet
76161592Scognet#include <vm/vm.h>
77161592Scognet#include <vm/pmap.h>
78161592Scognet#include <vm/vm_object.h>
79161592Scognet#include <vm/vm_page.h>
80161592Scognet#include <vm/vm_map.h>
81259364Sian#include <machine/devmap.h>
82161592Scognet#include <machine/vmparam.h>
83161592Scognet#include <machine/pcb.h>
84161592Scognet#include <machine/undefined.h>
85161592Scognet#include <machine/machdep.h>
86161592Scognet#include <machine/metadata.h>
87161592Scognet#include <machine/armreg.h>
88161592Scognet#include <machine/bus.h>
89266194Sian#include <machine/physmem.h>
90161592Scognet#include <sys/reboot.h>
91161592Scognet
92161592Scognet#include <arm/xscale/i80321/i80321reg.h>
93161592Scognet#include <arm/xscale/i80321/i80321var.h>
94161592Scognet#include <arm/xscale/i80321/iq80321reg.h>
95161592Scognet#include <arm/xscale/i80321/obiovar.h>
96161592Scognet
97243194Sandrew#define	KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
98243194Sandrew#define	KERNEL_PT_IOPXS		1
99243194Sandrew#define	KERNEL_PT_BEFOREKERN	2
100243194Sandrew#define	KERNEL_PT_AFKERNEL	3	/* L2 table for mapping after kernel */
101243194Sandrew#define	KERNEL_PT_AFKERNEL_NUM	9
102161592Scognet
103161592Scognet/* this should be evenly divisable by PAGE_SIZE / L2_TABLE_SIZE_REAL (or 4) */
104161592Scognet#define NUM_KERNEL_PTS		(KERNEL_PT_AFKERNEL + KERNEL_PT_AFKERNEL_NUM)
105161592Scognet
106161592Scognetstruct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
107161592Scognet
108161592Scognet/* Physical and virtual addresses for some global pages */
109161592Scognet
110161592Scognetstruct pv_addr systempage;
111161592Scognetstruct pv_addr msgbufpv;
112161592Scognetstruct pv_addr irqstack;
113161592Scognetstruct pv_addr undstack;
114161592Scognetstruct pv_addr abtstack;
115161592Scognetstruct pv_addr kernelstack;
116161592Scognetstruct pv_addr minidataclean;
117161592Scognet
118161592Scognet
119161592Scognet/* #define IQ80321_OBIO_BASE 0xfe800000UL */
120161592Scognet/* #define IQ80321_OBIO_SIZE 0x00100000UL */
121161592Scognet
122161592Scognet/* Static device mappings. */
123259364Sianstatic const struct arm_devmap_entry ep80219_devmap[] = {
124236987Simp	/*
125161592Scognet	 * Map the on-board devices VA == PA so that we can access them
126161592Scognet	 * with the MMU on or off.
127161592Scognet	 */
128161592Scognet	{
129161592Scognet		IQ80321_OBIO_BASE,
130161592Scognet		IQ80321_OBIO_BASE,
131161592Scognet		IQ80321_OBIO_SIZE,
132236987Simp		VM_PROT_READ|VM_PROT_WRITE,
133266386Sian		PTE_DEVICE,
134161592Scognet	},
135161592Scognet	{
136161592Scognet		IQ80321_IOW_VBASE,
137161592Scognet		VERDE_OUT_XLATE_IO_WIN0_BASE,
138161592Scognet		VERDE_OUT_XLATE_IO_WIN_SIZE,
139161592Scognet		VM_PROT_READ|VM_PROT_WRITE,
140266386Sian		PTE_DEVICE,
141236987Simp	},
142161592Scognet	{
143161592Scognet		IQ80321_80321_VBASE,
144161592Scognet		VERDE_PMMR_BASE,
145161592Scognet		VERDE_PMMR_SIZE,
146161592Scognet		VM_PROT_READ|VM_PROT_WRITE,
147266386Sian		PTE_DEVICE,
148161592Scognet	},
149161592Scognet	{
150161592Scognet		0,
151161592Scognet		0,
152161592Scognet		0,
153161592Scognet		0,
154161592Scognet		0,
155161592Scognet	}
156161592Scognet};
157161592Scognet
158161592Scognetextern vm_offset_t xscale_cache_clean_addr;
159161592Scognet
160161592Scognetvoid *
161236524Simpinitarm(struct arm_boot_params *abp)
162161592Scognet{
163161592Scognet	struct pv_addr  kernel_l1pt;
164194784Sjeff	struct pv_addr  dpcpu;
165177883Simp	int loop, i;
166161592Scognet	u_int l1pagetable;
167161592Scognet	vm_offset_t freemempos;
168161592Scognet	vm_offset_t freemem_pt;
169161592Scognet	vm_offset_t afterkern;
170161592Scognet	vm_offset_t freemem_after;
171161592Scognet	vm_offset_t lastaddr;
172161592Scognet	uint32_t memsize, memstart;
173161592Scognet
174237040Simp	lastaddr = parse_boot_param(abp);
175266194Sian	arm_physmem_kernaddr = abp->abp_physaddr;
176161592Scognet	set_cpufuncs();
177161592Scognet	pcpu_init(pcpup, 0, sizeof(struct pcpu));
178161592Scognet	PCPU_SET(curthread, &thread0);
179161592Scognet
180243194Sandrew	/* Do basic tuning, hz etc */
181243194Sandrew	init_param1();
182243194Sandrew
183161592Scognet	freemempos = 0xa0200000;
184161592Scognet	/* Define a macro to simplify memory allocation */
185161592Scognet#define	valloc_pages(var, np)			\
186243194Sandrew	alloc_pages((var).pv_pa, (np));		\
187161592Scognet	(var).pv_va = (var).pv_pa + 0x20000000;
188161592Scognet
189161592Scognet#define alloc_pages(var, np)			\
190161592Scognet	freemempos -= (np * PAGE_SIZE);		\
191161592Scognet	(var) = freemempos;		\
192161592Scognet	memset((char *)(var), 0, ((np) * PAGE_SIZE));
193161592Scognet
194161592Scognet	while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
195161592Scognet		freemempos -= PAGE_SIZE;
196161592Scognet	valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
197161592Scognet	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
198161592Scognet		if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
199161592Scognet			valloc_pages(kernel_pt_table[loop],
200243194Sandrew			    L2_TABLE_SIZE / PAGE_SIZE);
201161592Scognet		} else {
202161592Scognet			kernel_pt_table[loop].pv_pa = freemempos +
203161592Scognet			    (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) *
204161592Scognet			    L2_TABLE_SIZE_REAL;
205236987Simp			kernel_pt_table[loop].pv_va =
206161592Scognet			    kernel_pt_table[loop].pv_pa + 0x20000000;
207161592Scognet		}
208161592Scognet	}
209161592Scognet	freemem_pt = freemempos;
210161592Scognet	freemempos = 0xa0100000;
211161592Scognet	/*
212161592Scognet	 * Allocate a page for the system page mapped to V0x00000000
213161592Scognet	 * This page will just contain the system vectors and can be
214161592Scognet	 * shared by all processes.
215161592Scognet	 */
216161592Scognet	valloc_pages(systempage, 1);
217161592Scognet
218194784Sjeff	/* Allocate dynamic per-cpu area. */
219194784Sjeff	valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
220194784Sjeff	dpcpu_init((void *)dpcpu.pv_va, 0);
221194784Sjeff
222161592Scognet	/* Allocate stacks for all modes */
223161592Scognet	valloc_pages(irqstack, IRQ_STACK_SIZE);
224161592Scognet	valloc_pages(abtstack, ABT_STACK_SIZE);
225161592Scognet	valloc_pages(undstack, UND_STACK_SIZE);
226161592Scognet	valloc_pages(kernelstack, KSTACK_PAGES);
227161592Scognet	alloc_pages(minidataclean.pv_pa, 1);
228217688Spluknet	valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
229161592Scognet	/*
230161592Scognet	 * Allocate memory for the l1 and l2 page tables. The scheme to avoid
231161592Scognet	 * wasting memory by allocating the l1pt on the first 16k memory was
232161592Scognet	 * taken from NetBSD rpc_machdep.c. NKPT should be greater than 12 for
233161592Scognet	 * this to work (which is supposed to be the case).
234161592Scognet	 */
235161592Scognet
236161592Scognet	/*
237161592Scognet	 * Now we start construction of the L1 page table
238161592Scognet	 * We start by mapping the L2 page tables into the L1.
239161592Scognet	 * This means that we can replace L1 mappings later on if necessary
240161592Scognet	 */
241161592Scognet	l1pagetable = kernel_l1pt.pv_va;
242161592Scognet
243161592Scognet	/* Map the L2 pages tables in the L1 page table */
244161592Scognet	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00100000 - 1),
245243194Sandrew	    &kernel_pt_table[KERNEL_PT_SYS]);
246161592Scognet	pmap_link_l2pt(l1pagetable, IQ80321_IOPXS_VBASE,
247243194Sandrew	    &kernel_pt_table[KERNEL_PT_IOPXS]);
248161592Scognet	pmap_link_l2pt(l1pagetable, KERNBASE,
249243194Sandrew	    &kernel_pt_table[KERNEL_PT_BEFOREKERN]);
250161592Scognet	pmap_map_chunk(l1pagetable, KERNBASE, IQ80321_SDRAM_START, 0x100000,
251243194Sandrew	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
252161592Scognet	pmap_map_chunk(l1pagetable, KERNBASE + 0x100000, IQ80321_SDRAM_START + 0x100000,
253243194Sandrew	    0x100000, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
254161592Scognet	pmap_map_chunk(l1pagetable, KERNBASE + 0x200000, IQ80321_SDRAM_START + 0x200000,
255243194Sandrew	    (((uint32_t)(lastaddr) - KERNBASE - 0x200000) + L1_S_SIZE) & ~(L1_S_SIZE - 1),
256243194Sandrew	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
257161592Scognet	freemem_after = ((int)lastaddr + PAGE_SIZE) & ~(PAGE_SIZE - 1);
258236987Simp	afterkern = round_page(((vm_offset_t)lastaddr + L1_S_SIZE) & ~(L1_S_SIZE
259243194Sandrew	    - 1));
260161592Scognet	for (i = 0; i < KERNEL_PT_AFKERNEL_NUM; i++) {
261161592Scognet		pmap_link_l2pt(l1pagetable, afterkern + i * 0x00100000,
262243194Sandrew		    &kernel_pt_table[KERNEL_PT_AFKERNEL + i]);
263161592Scognet	}
264236987Simp	pmap_map_entry(l1pagetable, afterkern, minidataclean.pv_pa,
265243194Sandrew	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
266161592Scognet
267161592Scognet
268161592Scognet	/* Map the Mini-Data cache clean area. */
269161592Scognet	xscale_setup_minidata(l1pagetable, afterkern,
270243194Sandrew	    minidataclean.pv_pa);
271161592Scognet
272161592Scognet	/* Map the vector page. */
273161592Scognet	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
274243194Sandrew	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
275259364Sian	arm_devmap_bootstrap(l1pagetable, ep80219_devmap);
276161592Scognet	/*
277161592Scognet	 * Give the XScale global cache clean code an appropriately
278161592Scognet	 * sized chunk of unmapped VA space starting at 0xff000000
279161592Scognet	 * (our device mappings end before this address).
280161592Scognet	 */
281161592Scognet	xscale_cache_clean_addr = 0xff000000U;
282161592Scognet
283161592Scognet	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
284161592Scognet	setttb(kernel_l1pt.pv_pa);
285161592Scognet	cpu_tlb_flushID();
286161592Scognet	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
287161592Scognet	/*
288161592Scognet	 * Pages were allocated during the secondary bootstrap for the
289161592Scognet	 * stacks for different CPU modes.
290161592Scognet	 * We must now set the r13 registers in the different CPU modes to
291161592Scognet	 * point to these stacks.
292161592Scognet	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
293161592Scognet	 * of the stack memory.
294161592Scognet	 */
295240802Sandrew	set_stackptrs(0);
296161592Scognet
297161592Scognet	/*
298161592Scognet	 * We must now clean the cache again....
299161592Scognet	 * Cleaning may be done by reading new data to displace any
300161592Scognet	 * dirty data in the cache. This will have happened in setttb()
301161592Scognet	 * but since we are boot strapping the addresses used for the read
302161592Scognet	 * may have just been remapped and thus the cache could be out
303161592Scognet	 * of sync. A re-clean after the switch will cure this.
304185513Sstas	 * After booting there are no gross relocations of the kernel thus
305161592Scognet	 * this problem will not occur after initarm().
306161592Scognet	 */
307161592Scognet	cpu_idcache_wbinv_all();
308259377Sian	cpu_setup("");
309259377Sian
310161592Scognet	/*
311161592Scognet	 * Fetch the SDRAM start/size from the i80321 SDRAM configration
312161592Scognet	 * registers.
313161592Scognet	 */
314161592Scognet	i80321_calibrate_delay();
315278727Sian	i80321_sdram_bounds(obio_bs_tag, IQ80321_80321_VBASE + VERDE_MCU_BASE,
316161592Scognet	    &memstart, &memsize);
317161592Scognet	physmem = memsize / PAGE_SIZE;
318161592Scognet	cninit();
319161592Scognet
320161592Scognet	undefined_init();
321161592Scognet
322236828Sandrew	init_proc0(kernelstack.pv_va);
323161592Scognet
324161592Scognet	/* Enable MMU, I-cache, D-cache, write buffer. */
325161592Scognet
326161592Scognet	arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
327247046Salc	vm_max_kernel_address = 0xd0000000;
328247046Salc	pmap_bootstrap(pmap_curmaxkvaddr, &kernel_l1pt);
329161592Scognet	msgbufp = (void*)msgbufpv.pv_va;
330217688Spluknet	msgbufinit(msgbufp, msgbufsize);
331161592Scognet	mutex_init();
332161592Scognet
333266194Sian	/*
334266194Sian	 * Add the physical ram we have available.
335266194Sian	 *
336266194Sian	 * Exclude the kernel (and all the things we allocated which immediately
337266194Sian	 * follow the kernel) from the VM allocation pool but not from crash
338266194Sian	 * dumps.  virtual_avail is a global variable which tracks the kva we've
339266194Sian	 * "allocated" while setting up pmaps.
340266194Sian	 *
341266194Sian	 * Prepare the list of physical memory available to the vm subsystem.
342266194Sian	 */
343266194Sian	arm_physmem_hardware_region(IQ80321_SDRAM_START, memsize);
344266194Sian	arm_physmem_exclude_region(abp->abp_physaddr,
345266194Sian	    virtual_avail - KERNVIRTADDR, EXFLAG_NOALLOC);
346266194Sian	arm_physmem_init_kernel_globals();
347266194Sian
348161592Scognet	init_param2(physmem);
349161592Scognet	kdb_init();
350161592Scognet	return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
351161592Scognet	    sizeof(struct pcb)));
352161592Scognet}
353161592Scognet
354161592Scognetextern int
355161592Scognetmachdep_pci_route_interrupt(device_t pcib, device_t dev, int pin)
356161592Scognet{
357161592Scognet	int bus;
358161592Scognet	int device;
359161592Scognet	int func;
360161592Scognet	uint32_t busno;
361161592Scognet	struct i80321_pci_softc *sc = device_get_softc(pcib);
362161592Scognet	bus = pci_get_bus(dev);
363161592Scognet	device = pci_get_slot(dev);
364161592Scognet	func = pci_get_function(dev);
365161592Scognet	busno = bus_space_read_4(sc->sc_st, sc->sc_atu_sh, ATU_PCIXSR);
366161592Scognet	busno = PCIXSR_BUSNO(busno);
367161592Scognet	if (busno == 0xff)
368161592Scognet		busno = 0;
369161592Scognet	if (bus != busno)
370161592Scognet		goto no_mapping;
371161592Scognet	switch (device) {
372161592Scognet		/* EP80219 PCI */
373161592Scognet	case 1: /* Ethernet i82555 10/100 */
374161592Scognet		printf("Device %d routed to irq %d\n", device, ICU_INT_XINT(0));
375161592Scognet		return (ICU_INT_XINT(0));
376161592Scognet	case 2: /* UART */
377161592Scognet		printf("Device %d routed to irq %d\n", device, ICU_INT_XINT(1));
378161592Scognet		return (ICU_INT_XINT(1));
379161592Scognet	case 3:
380161592Scognet		/*
381161592Scognet		 * The S-ATA chips are behind the bridge, and all of
382161592Scognet		 * the S-ATA interrupts are wired together.
383161592Scognet		 */
384161592Scognet		printf("Device %d routed to irq %d\n", device, ICU_INT_XINT(2));
385161592Scognet		return (ICU_INT_XINT(2));
386161592Scognet	case 4: /* MINI-PIC_INT */
387161592Scognet		printf("Device %d routed to irq %d\n", device, ICU_INT_XINT(3));
388161592Scognet		return( ICU_INT_XINT(3));
389161592Scognet	default:
390161592Scognetno_mapping:
391161592Scognet		printf("No mapping for %d/%d/%d/%c\n", bus, device, func, pin);
392161592Scognet
393161592Scognet	}
394161592Scognet	return (0);
395161592Scognet
396161592Scognet}
397