xref: /OK3568_Linux_fs/u-boot/arch/sandbox/cpu/cpu.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2011 The Chromium OS Authors.
3*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun #define DEBUG
6*4882a593Smuzhiyun #include <common.h>
7*4882a593Smuzhiyun #include <dm.h>
8*4882a593Smuzhiyun #include <errno.h>
9*4882a593Smuzhiyun #include <linux/libfdt.h>
10*4882a593Smuzhiyun #include <os.h>
11*4882a593Smuzhiyun #include <asm/io.h>
12*4882a593Smuzhiyun #include <asm/state.h>
13*4882a593Smuzhiyun #include <dm/root.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /* Enable access to PCI memory with map_sysmem() */
18*4882a593Smuzhiyun static bool enable_pci_map;
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #ifdef CONFIG_PCI
21*4882a593Smuzhiyun /* Last device that was mapped into memory, and length of mapping */
22*4882a593Smuzhiyun static struct udevice *map_dev;
23*4882a593Smuzhiyun unsigned long map_len;
24*4882a593Smuzhiyun #endif
25*4882a593Smuzhiyun 
sandbox_exit(void)26*4882a593Smuzhiyun void sandbox_exit(void)
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun 	/* Do this here while it still has an effect */
29*4882a593Smuzhiyun 	os_fd_restore();
30*4882a593Smuzhiyun 	if (state_uninit())
31*4882a593Smuzhiyun 		os_exit(2);
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	if (dm_uninit())
34*4882a593Smuzhiyun 		os_exit(2);
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun 	/* This is considered normal termination for now */
37*4882a593Smuzhiyun 	os_exit(0);
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun /* delay x useconds */
__udelay(unsigned long usec)41*4882a593Smuzhiyun void __udelay(unsigned long usec)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun 	struct sandbox_state *state = state_get_current();
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	if (!state->skip_delays)
46*4882a593Smuzhiyun 		os_usleep(usec);
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun 
cleanup_before_linux(void)49*4882a593Smuzhiyun int cleanup_before_linux(void)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun 	return 0;
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun 
cleanup_before_linux_select(int flags)54*4882a593Smuzhiyun int cleanup_before_linux_select(int flags)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun 	return 0;
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun 
map_physmem(phys_addr_t paddr,unsigned long len,unsigned long flags)59*4882a593Smuzhiyun void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun #if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD)
62*4882a593Smuzhiyun 	unsigned long plen = len;
63*4882a593Smuzhiyun 	void *ptr;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	map_dev = NULL;
66*4882a593Smuzhiyun 	if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) {
67*4882a593Smuzhiyun 		if (plen != len) {
68*4882a593Smuzhiyun 			printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n",
69*4882a593Smuzhiyun 			       __func__, paddr, len, plen);
70*4882a593Smuzhiyun 		}
71*4882a593Smuzhiyun 		map_len = len;
72*4882a593Smuzhiyun 		return ptr;
73*4882a593Smuzhiyun 	}
74*4882a593Smuzhiyun #endif
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	return (void *)(gd->arch.ram_buf + paddr);
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
unmap_physmem(const void * vaddr,unsigned long flags)79*4882a593Smuzhiyun void unmap_physmem(const void *vaddr, unsigned long flags)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun #ifdef CONFIG_PCI
82*4882a593Smuzhiyun 	if (map_dev) {
83*4882a593Smuzhiyun 		pci_unmap_physmem(vaddr, map_len, map_dev);
84*4882a593Smuzhiyun 		map_dev = NULL;
85*4882a593Smuzhiyun 	}
86*4882a593Smuzhiyun #endif
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun 
sandbox_set_enable_pci_map(int enable)89*4882a593Smuzhiyun void sandbox_set_enable_pci_map(int enable)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun 	enable_pci_map = enable;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun 
map_to_sysmem(const void * ptr)94*4882a593Smuzhiyun phys_addr_t map_to_sysmem(const void *ptr)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun 	return (u8 *)ptr - gd->arch.ram_buf;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
flush_dcache_range(unsigned long start,unsigned long stop)99*4882a593Smuzhiyun void flush_dcache_range(unsigned long start, unsigned long stop)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun 
invalidate_dcache_range(unsigned long start,unsigned long stop)103*4882a593Smuzhiyun void invalidate_dcache_range(unsigned long start, unsigned long stop)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun 
sandbox_read_fdt_from_file(void)107*4882a593Smuzhiyun int sandbox_read_fdt_from_file(void)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun 	struct sandbox_state *state = state_get_current();
110*4882a593Smuzhiyun 	const char *fname = state->fdt_fname;
111*4882a593Smuzhiyun 	void *blob;
112*4882a593Smuzhiyun 	loff_t size;
113*4882a593Smuzhiyun 	int err;
114*4882a593Smuzhiyun 	int fd;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0);
117*4882a593Smuzhiyun 	if (!state->fdt_fname) {
118*4882a593Smuzhiyun 		err = fdt_create_empty_tree(blob, 256);
119*4882a593Smuzhiyun 		if (!err)
120*4882a593Smuzhiyun 			goto done;
121*4882a593Smuzhiyun 		printf("Unable to create empty FDT: %s\n", fdt_strerror(err));
122*4882a593Smuzhiyun 		return -EINVAL;
123*4882a593Smuzhiyun 	}
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	err = os_get_filesize(fname, &size);
126*4882a593Smuzhiyun 	if (err < 0) {
127*4882a593Smuzhiyun 		printf("Failed to file FDT file '%s'\n", fname);
128*4882a593Smuzhiyun 		return err;
129*4882a593Smuzhiyun 	}
130*4882a593Smuzhiyun 	fd = os_open(fname, OS_O_RDONLY);
131*4882a593Smuzhiyun 	if (fd < 0) {
132*4882a593Smuzhiyun 		printf("Failed to open FDT file '%s'\n", fname);
133*4882a593Smuzhiyun 		return -EACCES;
134*4882a593Smuzhiyun 	}
135*4882a593Smuzhiyun 	if (os_read(fd, blob, size) != size) {
136*4882a593Smuzhiyun 		os_close(fd);
137*4882a593Smuzhiyun 		return -EIO;
138*4882a593Smuzhiyun 	}
139*4882a593Smuzhiyun 	os_close(fd);
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun done:
142*4882a593Smuzhiyun 	gd->fdt_blob = blob;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	return 0;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun 
timer_get_boot_us(void)147*4882a593Smuzhiyun ulong timer_get_boot_us(void)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	static uint64_t base_count;
150*4882a593Smuzhiyun 	uint64_t count = os_get_nsec();
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	if (!base_count)
153*4882a593Smuzhiyun 		base_count = count;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	return (count - base_count) / 1000;
156*4882a593Smuzhiyun }
157