mv_machdep.c revision 235609
1/*-
2 * Copyright (c) 1994-1998 Mark Brinicombe.
3 * Copyright (c) 1994 Brini.
4 * All rights reserved.
5 *
6 * This code is derived from software written for Brini by Mark Brinicombe
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *      This product includes software developed by Brini.
19 * 4. The name of the company nor the name of the author may be used to
20 *    endorse or promote products derived from this software without specific
21 *    prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * from: FreeBSD: //depot/projects/arm/src/sys/arm/at91/kb920x_machdep.c, rev 45
36 */
37
38#include "opt_ddb.h"
39#include "opt_platform.h"
40
41#include <sys/cdefs.h>
42__FBSDID("$FreeBSD: head/sys/arm/mv/mv_machdep.c 235609 2012-05-18 14:41:14Z gber $");
43
44#define _ARM32_BUS_DMA_PRIVATE
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/sysproto.h>
48#include <sys/signalvar.h>
49#include <sys/imgact.h>
50#include <sys/kernel.h>
51#include <sys/ktr.h>
52#include <sys/linker.h>
53#include <sys/lock.h>
54#include <sys/malloc.h>
55#include <sys/mutex.h>
56#include <sys/pcpu.h>
57#include <sys/proc.h>
58#include <sys/ptrace.h>
59#include <sys/cons.h>
60#include <sys/bio.h>
61#include <sys/bus.h>
62#include <sys/buf.h>
63#include <sys/exec.h>
64#include <sys/kdb.h>
65#include <sys/msgbuf.h>
66#include <machine/reg.h>
67#include <machine/cpu.h>
68#include <machine/fdt.h>
69
70#include <dev/fdt/fdt_common.h>
71#include <dev/ofw/openfirm.h>
72
73#include <vm/vm.h>
74#include <vm/pmap.h>
75#include <vm/vm_object.h>
76#include <vm/vm_page.h>
77#include <vm/vm_pager.h>
78#include <vm/vm_map.h>
79#include <machine/pte.h>
80#include <machine/pmap.h>
81#include <machine/vmparam.h>
82#include <machine/pcb.h>
83#include <machine/undefined.h>
84#include <machine/machdep.h>
85#include <machine/metadata.h>
86#include <machine/armreg.h>
87#include <machine/bus.h>
88#include <sys/reboot.h>
89
90#include <arm/mv/mvreg.h>	/* XXX */
91#include <arm/mv/mvvar.h>	/* XXX eventually this should be eliminated */
92#include <arm/mv/mvwin.h>
93
94#ifdef  DEBUG
95#define debugf(fmt, args...) printf(fmt, ##args)
96#else
97#define debugf(fmt, args...)
98#endif
99
100/*
101 * This is the number of L2 page tables required for covering max
102 * (hypothetical) memsize of 4GB and all kernel mappings (vectors, msgbuf,
103 * stacks etc.), uprounded to be divisible by 4.
104 */
105#define KERNEL_PT_MAX	78
106
107/* Define various stack sizes in pages */
108#define IRQ_STACK_SIZE	1
109#define ABT_STACK_SIZE	1
110#define UND_STACK_SIZE	1
111
112extern unsigned char kernbase[];
113extern unsigned char _etext[];
114extern unsigned char _edata[];
115extern unsigned char __bss_start[];
116extern unsigned char _end[];
117
118#ifdef DDB
119extern vm_offset_t ksym_start, ksym_end;
120#endif
121
122extern u_int data_abort_handler_address;
123extern u_int prefetch_abort_handler_address;
124extern u_int undefined_handler_address;
125
126extern vm_offset_t pmap_bootstrap_lastaddr;
127extern int *end;
128
129struct pv_addr kernel_pt_table[KERNEL_PT_MAX];
130struct pcpu __pcpu;
131struct pcpu *pcpup = &__pcpu;
132
133/* Physical and virtual addresses for some global pages */
134
135vm_paddr_t phys_avail[10];
136vm_paddr_t dump_avail[4];
137vm_offset_t physical_pages;
138vm_offset_t pmap_bootstrap_lastaddr;
139
140const struct pmap_devmap *pmap_devmap_bootstrap_table;
141struct pv_addr systempage;
142struct pv_addr msgbufpv;
143struct pv_addr irqstack;
144struct pv_addr undstack;
145struct pv_addr abtstack;
146struct pv_addr kernelstack;
147
148static struct trapframe proc0_tf;
149
150static struct mem_region availmem_regions[FDT_MEM_REGIONS];
151static int availmem_regions_sz;
152
153static void print_kenv(void);
154static void print_kernel_section_addr(void);
155
156static void physmap_init(void);
157static int platform_devmap_init(void);
158static int platform_mpp_init(void);
159
160static char *
161kenv_next(char *cp)
162{
163
164	if (cp != NULL) {
165		while (*cp != 0)
166			cp++;
167		cp++;
168		if (*cp == 0)
169			cp = NULL;
170	}
171	return (cp);
172}
173
174static void
175print_kenv(void)
176{
177	int len;
178	char *cp;
179
180	debugf("loader passed (static) kenv:\n");
181	if (kern_envp == NULL) {
182		debugf(" no env, null ptr\n");
183		return;
184	}
185	debugf(" kern_envp = 0x%08x\n", (uint32_t)kern_envp);
186
187	len = 0;
188	for (cp = kern_envp; cp != NULL; cp = kenv_next(cp))
189		debugf(" %x %s\n", (uint32_t)cp, cp);
190}
191
192static void
193print_kernel_section_addr(void)
194{
195
196	debugf("kernel image addresses:\n");
197	debugf(" kernbase       = 0x%08x\n", (uint32_t)kernbase);
198	debugf(" _etext (sdata) = 0x%08x\n", (uint32_t)_etext);
199	debugf(" _edata         = 0x%08x\n", (uint32_t)_edata);
200	debugf(" __bss_start    = 0x%08x\n", (uint32_t)__bss_start);
201	debugf(" _end           = 0x%08x\n", (uint32_t)_end);
202}
203
204static void
205physmap_init(void)
206{
207	int i, j, cnt;
208	vm_offset_t phys_kernelend, kernload;
209	uint32_t s, e, sz;
210	struct mem_region *mp, *mp1;
211
212	phys_kernelend = KERNPHYSADDR + (virtual_avail - KERNVIRTADDR);
213	kernload = KERNPHYSADDR;
214
215	/*
216	 * Remove kernel physical address range from avail
217	 * regions list. Page align all regions.
218	 * Non-page aligned memory isn't very interesting to us.
219	 * Also, sort the entries for ascending addresses.
220	 */
221	sz = 0;
222	cnt = availmem_regions_sz;
223	debugf("processing avail regions:\n");
224	for (mp = availmem_regions; mp->mr_size; mp++) {
225		s = mp->mr_start;
226		e = mp->mr_start + mp->mr_size;
227		debugf(" %08x-%08x -> ", s, e);
228		/* Check whether this region holds all of the kernel. */
229		if (s < kernload && e > phys_kernelend) {
230			availmem_regions[cnt].mr_start = phys_kernelend;
231			availmem_regions[cnt++].mr_size = e - phys_kernelend;
232			e = kernload;
233		}
234		/* Look whether this regions starts within the kernel. */
235		if (s >= kernload && s < phys_kernelend) {
236			if (e <= phys_kernelend)
237				goto empty;
238			s = phys_kernelend;
239		}
240		/* Now look whether this region ends within the kernel. */
241		if (e > kernload && e <= phys_kernelend) {
242			if (s >= kernload) {
243				goto empty;
244			}
245			e = kernload;
246		}
247		/* Now page align the start and size of the region. */
248		s = round_page(s);
249		e = trunc_page(e);
250		if (e < s)
251			e = s;
252		sz = e - s;
253		debugf("%08x-%08x = %x\n", s, e, sz);
254
255		/* Check whether some memory is left here. */
256		if (sz == 0) {
257		empty:
258			printf("skipping\n");
259			bcopy(mp + 1, mp,
260			    (cnt - (mp - availmem_regions)) * sizeof(*mp));
261			cnt--;
262			mp--;
263			continue;
264		}
265
266		/* Do an insertion sort. */
267		for (mp1 = availmem_regions; mp1 < mp; mp1++)
268			if (s < mp1->mr_start)
269				break;
270		if (mp1 < mp) {
271			bcopy(mp1, mp1 + 1, (char *)mp - (char *)mp1);
272			mp1->mr_start = s;
273			mp1->mr_size = sz;
274		} else {
275			mp->mr_start = s;
276			mp->mr_size = sz;
277		}
278	}
279	availmem_regions_sz = cnt;
280
281	/* Fill in phys_avail table, based on availmem_regions */
282	debugf("fill in phys_avail:\n");
283	for (i = 0, j = 0; i < availmem_regions_sz; i++, j += 2) {
284
285		debugf(" region: 0x%08x - 0x%08x (0x%08x)\n",
286		    availmem_regions[i].mr_start,
287		    availmem_regions[i].mr_start + availmem_regions[i].mr_size,
288		    availmem_regions[i].mr_size);
289
290		/*
291		 * We should not map the page at PA 0x0000000, the VM can't
292		 * handle it, as pmap_extract() == 0 means failure.
293		 */
294		if (availmem_regions[i].mr_start > 0 ||
295		    availmem_regions[i].mr_size > PAGE_SIZE) {
296			phys_avail[j] = availmem_regions[i].mr_start;
297			if (phys_avail[j] == 0)
298				phys_avail[j] += PAGE_SIZE;
299			phys_avail[j + 1] = availmem_regions[i].mr_start +
300			    availmem_regions[i].mr_size;
301		} else
302			j -= 2;
303	}
304	phys_avail[j] = 0;
305	phys_avail[j + 1] = 0;
306}
307
308void *
309initarm(void *mdp, void *unused __unused)
310{
311	struct pv_addr kernel_l1pt;
312	struct pv_addr dpcpu;
313	vm_offset_t dtbp, freemempos, l2_start, lastaddr;
314	uint32_t memsize, l2size;
315	void *kmdp;
316	u_int l1pagetable;
317	int i = 0, j = 0, err_devmap = 0;
318
319	kmdp = NULL;
320	lastaddr = 0;
321	memsize = 0;
322	dtbp = (vm_offset_t)NULL;
323
324	set_cpufuncs();
325
326	/*
327	 * Mask metadata pointer: it is supposed to be on page boundary. If
328	 * the first argument (mdp) doesn't point to a valid address the
329	 * bootloader must have passed us something else than the metadata
330	 * ptr... In this case we want to fall back to some built-in settings.
331	 */
332	mdp = (void *)((uint32_t)mdp & ~PAGE_MASK);
333
334	/* Parse metadata and fetch parameters */
335	if (mdp != NULL) {
336		preload_metadata = mdp;
337		kmdp = preload_search_by_type("elf kernel");
338		if (kmdp != NULL) {
339			boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
340			kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
341			dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
342			lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND,
343			    vm_offset_t);
344#ifdef DDB
345			ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
346			ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
347#endif
348		}
349
350		preload_addr_relocate = KERNVIRTADDR - KERNPHYSADDR;
351	} else {
352		/* Fall back to hardcoded metadata. */
353		lastaddr = fake_preload_metadata();
354	}
355
356#if defined(FDT_DTB_STATIC)
357	/*
358	 * In case the device tree blob was not retrieved (from metadata) try
359	 * to use the statically embedded one.
360	 */
361	if (dtbp == (vm_offset_t)NULL)
362		dtbp = (vm_offset_t)&fdt_static_dtb;
363#endif
364
365	if (OF_install(OFW_FDT, 0) == FALSE)
366		while (1);
367
368	if (OF_init((void *)dtbp) != 0)
369		while (1);
370
371	/* Grab physical memory regions information from device tree. */
372	if (fdt_get_mem_regions(availmem_regions, &availmem_regions_sz,
373	    &memsize) != 0)
374		while(1);
375
376	if (fdt_immr_addr(MV_BASE) != 0)
377		while (1);
378
379	/* Platform-specific initialisation */
380	pmap_bootstrap_lastaddr = fdt_immr_va - ARM_NOCACHE_KVA_SIZE;
381
382	pcpu_init(pcpup, 0, sizeof(struct pcpu));
383	PCPU_SET(curthread, &thread0);
384
385	/* Calculate number of L2 tables needed for mapping vm_page_array */
386	l2size = (memsize / PAGE_SIZE) * sizeof(struct vm_page);
387	l2size = (l2size >> L1_S_SHIFT) + 1;
388
389	/*
390	 * Add one table for end of kernel map, one for stacks, msgbuf and
391	 * L1 and L2 tables map and one for vectors map.
392	 */
393	l2size += 3;
394
395	/* Make it divisible by 4 */
396	l2size = (l2size + 3) & ~3;
397
398#define KERNEL_TEXT_BASE (KERNBASE)
399	freemempos = (lastaddr + PAGE_MASK) & ~PAGE_MASK;
400
401	/* Define a macro to simplify memory allocation */
402#define valloc_pages(var, np)                   \
403	alloc_pages((var).pv_va, (np));         \
404	(var).pv_pa = (var).pv_va + (KERNPHYSADDR - KERNVIRTADDR);
405
406#define alloc_pages(var, np)			\
407	(var) = freemempos;		\
408	freemempos += (np * PAGE_SIZE);		\
409	memset((char *)(var), 0, ((np) * PAGE_SIZE));
410
411	while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
412		freemempos += PAGE_SIZE;
413	valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
414
415	for (i = 0; i < l2size; ++i) {
416		if (!(i % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
417			valloc_pages(kernel_pt_table[i],
418			    L2_TABLE_SIZE / PAGE_SIZE);
419			j = i;
420		} else {
421			kernel_pt_table[i].pv_va = kernel_pt_table[j].pv_va +
422			    L2_TABLE_SIZE_REAL * (i - j);
423			kernel_pt_table[i].pv_pa =
424			    kernel_pt_table[i].pv_va - KERNVIRTADDR +
425			    KERNPHYSADDR;
426
427		}
428	}
429	/*
430	 * Allocate a page for the system page mapped to 0x00000000
431	 * or 0xffff0000. This page will just contain the system vectors
432	 * and can be shared by all processes.
433	 */
434	valloc_pages(systempage, 1);
435
436	/* Allocate dynamic per-cpu area. */
437	valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
438	dpcpu_init((void *)dpcpu.pv_va, 0);
439
440	/* Allocate stacks for all modes */
441	valloc_pages(irqstack, IRQ_STACK_SIZE);
442	valloc_pages(abtstack, ABT_STACK_SIZE);
443	valloc_pages(undstack, UND_STACK_SIZE);
444	valloc_pages(kernelstack, KSTACK_PAGES);
445
446	init_param1();
447
448	valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
449
450	/*
451	 * Now we start construction of the L1 page table
452	 * We start by mapping the L2 page tables into the L1.
453	 * This means that we can replace L1 mappings later on if necessary
454	 */
455	l1pagetable = kernel_l1pt.pv_va;
456
457	/*
458	 * Try to map as much as possible of kernel text and data using
459	 * 1MB section mapping and for the rest of initial kernel address
460	 * space use L2 coarse tables.
461	 *
462	 * Link L2 tables for mapping remainder of kernel (modulo 1MB)
463	 * and kernel structures
464	 */
465	l2_start = lastaddr & ~(L1_S_OFFSET);
466	for (i = 0 ; i < l2size - 1; i++)
467		pmap_link_l2pt(l1pagetable, l2_start + i * L1_S_SIZE,
468		    &kernel_pt_table[i]);
469
470	pmap_curmaxkvaddr = l2_start + (l2size - 1) * L1_S_SIZE;
471
472	/* Map kernel code and data */
473	pmap_map_chunk(l1pagetable, KERNVIRTADDR, KERNPHYSADDR,
474	   (((uint32_t)(lastaddr) - KERNVIRTADDR) + PAGE_MASK) & ~PAGE_MASK,
475	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
476
477
478	/* Map L1 directory and allocated L2 page tables */
479	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
480	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
481
482	pmap_map_chunk(l1pagetable, kernel_pt_table[0].pv_va,
483	    kernel_pt_table[0].pv_pa,
484	    L2_TABLE_SIZE_REAL * l2size,
485	    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
486
487	/* Map allocated DPCPU, stacks and msgbuf */
488	pmap_map_chunk(l1pagetable, dpcpu.pv_va, dpcpu.pv_pa,
489	    freemempos - dpcpu.pv_va,
490	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
491
492	/* Link and map the vector page */
493	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH,
494	    &kernel_pt_table[l2size - 1]);
495	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
496	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
497
498	/* Map pmap_devmap[] entries */
499	err_devmap = platform_devmap_init();
500	pmap_devmap_bootstrap(l1pagetable, pmap_devmap_bootstrap_table);
501
502	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2)) |
503	    DOMAIN_CLIENT);
504	setttb(kernel_l1pt.pv_pa);
505	cpu_tlb_flushID();
506	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2));
507
508	/*
509	 * Only after the SOC registers block is mapped we can perform device
510	 * tree fixups, as they may attempt to read parameters from hardware.
511	 */
512	OF_interpret("perform-fixup", 0);
513
514	/*
515	 * Re-initialise MPP. It is important to call this prior to using
516	 * console as the physical connection can be routed via MPP.
517	 */
518	if (platform_mpp_init() != 0)
519		while (1);
520
521	cninit();
522
523	physmem = memsize / PAGE_SIZE;
524
525	debugf("initarm: console initialized\n");
526	debugf(" arg1 mdp = 0x%08x\n", (uint32_t)mdp);
527	debugf(" boothowto = 0x%08x\n", boothowto);
528	printf(" dtbp = 0x%08x\n", (uint32_t)dtbp);
529	print_kernel_section_addr();
530	print_kenv();
531
532	if (err_devmap != 0)
533		printf("WARNING: could not fully configure devmap, error=%d\n",
534                    err_devmap);
535
536	/*
537	 * Re-initialise decode windows
538	 */
539	if (soc_decode_win() != 0)
540		printf("WARNING: could not re-initialise decode windows! "
541		    "Running with existing settings...\n");
542
543	/*
544	 * Pages were allocated during the secondary bootstrap for the
545	 * stacks for different CPU modes.
546	 * We must now set the r13 registers in the different CPU modes to
547	 * point to these stacks.
548	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
549	 * of the stack memory.
550	 */
551	cpu_control(CPU_CONTROL_MMU_ENABLE, CPU_CONTROL_MMU_ENABLE);
552	set_stackptr(PSR_IRQ32_MODE,
553	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
554	set_stackptr(PSR_ABT32_MODE,
555	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
556	set_stackptr(PSR_UND32_MODE,
557	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
558
559	/*
560	 * We must now clean the cache again....
561	 * Cleaning may be done by reading new data to displace any
562	 * dirty data in the cache. This will have happened in setttb()
563	 * but since we are boot strapping the addresses used for the read
564	 * may have just been remapped and thus the cache could be out
565	 * of sync. A re-clean after the switch will cure this.
566	 * After booting there are no gross relocations of the kernel thus
567	 * this problem will not occur after initarm().
568	 */
569	cpu_idcache_wbinv_all();
570
571	/* Set stack for exception handlers */
572	data_abort_handler_address = (u_int)data_abort_handler;
573	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
574	undefined_handler_address = (u_int)undefinedinstruction_bounce;
575	undefined_init();
576
577	proc_linkup0(&proc0, &thread0);
578	thread0.td_kstack = kernelstack.pv_va;
579	thread0.td_kstack_pages = KSTACK_PAGES;
580	thread0.td_pcb = (struct pcb *)
581	    (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
582	thread0.td_pcb->pcb_flags = 0;
583	thread0.td_frame = &proc0_tf;
584	pcpup->pc_curpcb = thread0.td_pcb;
585
586	arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
587
588	dump_avail[0] = 0;
589	dump_avail[1] = memsize;
590	dump_avail[2] = 0;
591	dump_avail[3] = 0;
592
593	pmap_bootstrap(freemempos, pmap_bootstrap_lastaddr, &kernel_l1pt);
594	msgbufp = (void *)msgbufpv.pv_va;
595	msgbufinit(msgbufp, msgbufsize);
596	mutex_init();
597
598	/*
599	 * Prepare map of physical memory regions available to vm subsystem.
600	 */
601	physmap_init();
602
603	/* Do basic tuning, hz etc */
604	init_param2(physmem);
605	kdb_init();
606	return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
607	    sizeof(struct pcb)));
608}
609
610#define MPP_PIN_MAX		50
611#define MPP_PIN_CELLS		2
612#define MPP_PINS_PER_REG	8
613#define MPP_SEL(pin,func)	(((func) & 0xf) <<		\
614    (((pin) % MPP_PINS_PER_REG) * 4))
615
616static int
617platform_mpp_init(void)
618{
619	pcell_t pinmap[MPP_PIN_MAX * MPP_PIN_CELLS];
620	int mpp[MPP_PIN_MAX];
621	uint32_t ctrl_val, ctrl_offset;
622	pcell_t reg[4];
623	u_long start, size;
624	phandle_t node;
625	pcell_t pin_cells, *pinmap_ptr, pin_count;
626	ssize_t len;
627	int par_addr_cells, par_size_cells;
628	int tuple_size, tuples, rv, pins, i, j;
629	int mpp_pin, mpp_function;
630
631	/*
632	 * Try to access the MPP node directly i.e. through /aliases/mpp.
633	 */
634	if ((node = OF_finddevice("mpp")) != -1)
635		if (fdt_is_compatible(node, "mrvl,mpp"))
636			goto moveon;
637	/*
638	 * Find the node the long way.
639	 */
640	if ((node = OF_finddevice("/")) == -1)
641		return (ENXIO);
642
643	if ((node = fdt_find_compatible(node, "simple-bus", 0)) == 0)
644		return (ENXIO);
645
646	if ((node = fdt_find_compatible(node, "mrvl,mpp", 0)) == 0)
647		return (ENXIO);
648moveon:
649	/*
650	 * Process 'reg' prop.
651	 */
652	if ((rv = fdt_addrsize_cells(OF_parent(node), &par_addr_cells,
653	    &par_size_cells)) != 0)
654		return(ENXIO);
655
656	tuple_size = sizeof(pcell_t) * (par_addr_cells + par_size_cells);
657	len = OF_getprop(node, "reg", reg, sizeof(reg));
658	tuples = len / tuple_size;
659	if (tuple_size <= 0)
660		return (EINVAL);
661
662	/*
663	 * Get address/size. XXX we assume only the first 'reg' tuple is used.
664	 */
665	rv = fdt_data_to_res(reg, par_addr_cells, par_size_cells,
666	    &start, &size);
667	if (rv != 0)
668		return (rv);
669	start += fdt_immr_va;
670
671	/*
672	 * Process 'pin-count' and 'pin-map' props.
673	 */
674	if (OF_getprop(node, "pin-count", &pin_count, sizeof(pin_count)) <= 0)
675		return (ENXIO);
676	pin_count = fdt32_to_cpu(pin_count);
677	if (pin_count > MPP_PIN_MAX)
678		return (ERANGE);
679
680	if (OF_getprop(node, "#pin-cells", &pin_cells, sizeof(pin_cells)) <= 0)
681		pin_cells = MPP_PIN_CELLS;
682	pin_cells = fdt32_to_cpu(pin_cells);
683	if (pin_cells > MPP_PIN_CELLS)
684		return (ERANGE);
685	tuple_size = sizeof(pcell_t) * pin_cells;
686
687	bzero(pinmap, sizeof(pinmap));
688	len = OF_getprop(node, "pin-map", pinmap, sizeof(pinmap));
689	if (len <= 0)
690		return (ERANGE);
691	if (len % tuple_size)
692		return (ERANGE);
693	pins = len / tuple_size;
694	if (pins > pin_count)
695		return (ERANGE);
696	/*
697	 * Fill out a "mpp[pin] => function" table. All pins unspecified in
698	 * the 'pin-map' property are defaulted to 0 function i.e. GPIO.
699	 */
700	bzero(mpp, sizeof(mpp));
701	pinmap_ptr = pinmap;
702	for (i = 0; i < pins; i++) {
703		mpp_pin = fdt32_to_cpu(*pinmap_ptr);
704		mpp_function = fdt32_to_cpu(*(pinmap_ptr + 1));
705		mpp[mpp_pin] = mpp_function;
706		pinmap_ptr += pin_cells;
707	}
708
709	/*
710	 * Prepare and program MPP control register values.
711	 */
712	ctrl_offset = 0;
713	for (i = 0; i < pin_count;) {
714		ctrl_val = 0;
715
716		for (j = 0; j < MPP_PINS_PER_REG; j++) {
717			if (i + j == pin_count - 1)
718				break;
719			ctrl_val |= MPP_SEL(i + j, mpp[i + j]);
720		}
721		i += MPP_PINS_PER_REG;
722		bus_space_write_4(fdtbus_bs_tag, start, ctrl_offset,
723		    ctrl_val);
724
725#if defined(SOC_MV_ORION)
726		/*
727		 * Third MPP reg on Orion SoC is placed
728		 * non-linearly (with different offset).
729		 */
730		if (i ==  (2 * MPP_PINS_PER_REG))
731			ctrl_offset = 0x50;
732		else
733#endif
734			ctrl_offset += 4;
735	}
736
737	return (0);
738}
739
740#define FDT_DEVMAP_MAX	(MV_WIN_CPU_MAX + 1)
741static struct pmap_devmap fdt_devmap[FDT_DEVMAP_MAX] = {
742	{ 0, 0, 0, 0, 0, }
743};
744
745/*
746 * XXX: When device entry in devmap has pd_size smaller than section size,
747 * system will freeze during initialization
748 */
749
750/*
751 * Construct pmap_devmap[] with DT-derived config data.
752 */
753
754static int
755platform_devmap_init(void)
756{
757	phandle_t root, child;
758	pcell_t bank_count;
759	u_long base, size;
760	int i, num_mapped;
761
762	i = 0;
763	pmap_devmap_bootstrap_table = &fdt_devmap[0];
764
765	/*
766	 * IMMR range.
767	 */
768	fdt_devmap[i].pd_va = fdt_immr_va;
769	fdt_devmap[i].pd_pa = fdt_immr_pa;
770	fdt_devmap[i].pd_size = fdt_immr_size;
771	fdt_devmap[i].pd_prot = VM_PROT_READ | VM_PROT_WRITE;
772	fdt_devmap[i].pd_cache = PTE_NOCACHE;
773	i++;
774
775	/*
776	 * PCI range(s) and localbus.
777	 */
778	if ((root = OF_finddevice("/")) == -1)
779		return (ENXIO);
780
781	for (child = OF_child(root); child != 0; child = OF_peer(child)) {
782		if (fdt_is_type(child, "pci")) {
783			/*
784			 * Check space: each PCI node will consume 2 devmap
785			 * entries.
786			 */
787			if (i + 1 >= FDT_DEVMAP_MAX) {
788				return (ENOMEM);
789			}
790
791			/*
792			 * XXX this should account for PCI and multiple ranges
793			 * of a given kind.
794			 */
795			if (fdt_pci_devmap(child, &fdt_devmap[i],
796			    MV_PCIE_IO_BASE, MV_PCIE_MEM_BASE) != 0)
797				return (ENXIO);
798			i += 2;
799		}
800
801		if (fdt_is_compatible(child, "mrvl,lbc")) {
802			/* Check available space */
803			if (OF_getprop(child, "bank-count", (void *)&bank_count,
804			    sizeof(bank_count)) <= 0)
805				/* If no property, use default value */
806				bank_count = 1;
807			else
808				bank_count = fdt32_to_cpu(bank_count);
809
810			if ((i + bank_count) >= FDT_DEVMAP_MAX)
811				return (ENOMEM);
812
813			/* Add all localbus ranges to device map */
814			num_mapped = 0;
815
816			if (fdt_localbus_devmap(child, &fdt_devmap[i],
817			    (int)bank_count, &num_mapped) != 0)
818				return (ENXIO);
819
820			i += num_mapped;
821		}
822	}
823
824	/*
825	 * CESA SRAM range.
826	 */
827	if ((child = OF_finddevice("sram")) != -1)
828		if (fdt_is_compatible(child, "mrvl,cesa-sram"))
829			goto moveon;
830
831	if ((child = fdt_find_compatible(root, "mrvl,cesa-sram", 0)) == 0)
832		/* No CESA SRAM node. */
833		return (0);
834moveon:
835	if (i >= FDT_DEVMAP_MAX)
836		return (ENOMEM);
837
838	if (fdt_regsize(child, &base, &size) != 0)
839		return (EINVAL);
840
841	fdt_devmap[i].pd_va = MV_CESA_SRAM_BASE; /* XXX */
842	fdt_devmap[i].pd_pa = base;
843	fdt_devmap[i].pd_size = size;
844	fdt_devmap[i].pd_prot = VM_PROT_READ | VM_PROT_WRITE;
845	fdt_devmap[i].pd_cache = PTE_NOCACHE;
846
847	return (0);
848}
849
850struct arm32_dma_range *
851bus_dma_get_range(void)
852{
853
854	return (NULL);
855}
856
857int
858bus_dma_get_range_nb(void)
859{
860
861	return (0);
862}
863