blob: cde0b055a673a2f5c897d9832cb6ead33d79d9ad [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass3d750d72011-09-26 14:10:39 +00002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
Simon Glass3d750d72011-09-26 14:10:39 +00004 */
Simon Glass70778bc2015-03-05 12:25:26 -07005#define DEBUG
Simon Glass3d750d72011-09-26 14:10:39 +00006#include <common.h>
Simon Glass11c89f32017-05-17 17:18:03 -06007#include <dm.h>
Simon Glass141e5a02016-07-04 11:57:48 -06008#include <errno.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +09009#include <linux/libfdt.h>
Simon Glasscd0684f2011-10-03 19:26:44 +000010#include <os.h>
Simon Glass26b78b22015-02-27 22:06:34 -070011#include <asm/io.h>
Simon Glass5dccd152018-05-16 09:42:22 -060012#include <asm/setjmp.h>
Simon Glass9dd10bf2013-11-10 10:27:03 -070013#include <asm/state.h>
Simon Glassbe310372016-07-04 11:57:47 -060014#include <dm/root.h>
Simon Glass3d750d72011-09-26 14:10:39 +000015
16DECLARE_GLOBAL_DATA_PTR;
17
Simon Glass70778bc2015-03-05 12:25:26 -070018/* Enable access to PCI memory with map_sysmem() */
19static bool enable_pci_map;
20
21#ifdef CONFIG_PCI
22/* Last device that was mapped into memory, and length of mapping */
23static struct udevice *map_dev;
24unsigned long map_len;
25#endif
26
Simon Glassd860f222015-07-06 12:54:29 -060027void sandbox_exit(void)
Simon Glass3d750d72011-09-26 14:10:39 +000028{
Simon Glass9c3b7d62015-05-10 21:07:27 -060029 /* Do this here while it still has an effect */
30 os_fd_restore();
Simon Glass9dd10bf2013-11-10 10:27:03 -070031 if (state_uninit())
32 os_exit(2);
33
Simon Glass19200512014-07-23 06:55:02 -060034 if (dm_uninit())
35 os_exit(2);
36
Simon Glasscd0684f2011-10-03 19:26:44 +000037 /* This is considered normal termination for now */
38 os_exit(0);
Simon Glassdc9f8cd2013-11-10 10:27:00 -070039}
40
Simon Glass3d750d72011-09-26 14:10:39 +000041/* delay x useconds */
42void __udelay(unsigned long usec)
43{
Simon Glassb25fa312015-11-08 23:47:43 -070044 struct sandbox_state *state = state_get_current();
45
46 if (!state->skip_delays)
47 os_usleep(usec);
Simon Glass3d750d72011-09-26 14:10:39 +000048}
49
Simon Glass3d750d72011-09-26 14:10:39 +000050int cleanup_before_linux(void)
51{
52 return 0;
53}
54
Simon Glass770d93f2015-05-13 07:02:26 -060055int cleanup_before_linux_select(int flags)
56{
57 return 0;
58}
59
Paul Burton51369252017-09-14 15:05:13 -070060void *phys_to_virt(phys_addr_t paddr)
61{
62 return (void *)(gd->arch.ram_buf + paddr);
63}
64
65phys_addr_t virt_to_phys(void *vaddr)
66{
67 return (phys_addr_t)((uint8_t *)vaddr - gd->arch.ram_buf);
68}
69
Simon Glass3d750d72011-09-26 14:10:39 +000070void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
71{
Simon Glassf44a3a52016-07-04 11:57:49 -060072#if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD)
Simon Glass70778bc2015-03-05 12:25:26 -070073 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",
Mario Six61efece2018-02-12 08:05:57 +010080 __func__, (uint)paddr, len, plen);
Simon Glass70778bc2015-03-05 12:25:26 -070081 }
82 map_len = len;
83 return ptr;
84 }
85#endif
86
Paul Burton51369252017-09-14 15:05:13 -070087 return phys_to_virt(paddr);
Simon Glass3d750d72011-09-26 14:10:39 +000088}
89
Simon Glass70778bc2015-03-05 12:25:26 -070090void 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
100void sandbox_set_enable_pci_map(int enable)
101{
102 enable_pci_map = enable;
103}
104
Simon Glassf7c3f6f2014-02-27 13:25:55 -0700105phys_addr_t map_to_sysmem(const void *ptr)
Simon Glasse23d2932013-04-20 08:42:37 +0000106{
107 return (u8 *)ptr - gd->arch.ram_buf;
108}
109
Simon Glass3d750d72011-09-26 14:10:39 +0000110void flush_dcache_range(unsigned long start, unsigned long stop)
111{
112}
Simon Glass26b78b22015-02-27 22:06:34 -0700113
Bin Meng843c2992017-08-22 08:15:18 -0700114void invalidate_dcache_range(unsigned long start, unsigned long stop)
115{
116}
117
Simon Glass26b78b22015-02-27 22:06:34 -0700118int 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
152done:
153 gd->fdt_blob = blob;
154
155 return 0;
156}
Simon Glass38e79742017-05-22 05:05:23 -0600157
158ulong 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}
Simon Glass5dccd152018-05-16 09:42:22 -0600168
169int setjmp(jmp_buf jmp)
170{
171 return os_setjmp((ulong *)jmp, sizeof(*jmp));
172}
173
174void longjmp(jmp_buf jmp, int ret)
175{
176 os_longjmp((ulong *)jmp, ret);
177 while (1)
178 ;
179}