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