blob: 01991049ccf7773488328227e8c7ec0a46f5c994 [file] [log] [blame]
Simon Glass4b0730d2011-09-26 14:10:39 +00001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
Wolfgang Denk1a459662013-07-08 09:37:19 +02003 * SPDX-License-Identifier: GPL-2.0+
Simon Glass4b0730d2011-09-26 14:10:39 +00004 */
Simon Glass9569c402015-03-05 12:25:26 -07005#define DEBUG
Simon Glass4b0730d2011-09-26 14:10:39 +00006#include <common.h>
Simon Glass9d922452017-05-17 17:18:03 -06007#include <dm.h>
Simon Glassf4289cb2016-07-04 11:57:48 -06008#include <errno.h>
9#include <libfdt.h>
Simon Glass7a9219c2011-10-03 19:26:44 +000010#include <os.h>
Simon Glassb45122f2015-02-27 22:06:34 -070011#include <asm/io.h>
Simon Glass5c2859c2013-11-10 10:27:03 -070012#include <asm/state.h>
Simon Glass6e206502016-07-04 11:57:47 -060013#include <dm/root.h>
Simon Glass4b0730d2011-09-26 14:10:39 +000014
15DECLARE_GLOBAL_DATA_PTR;
16
Simon Glass9569c402015-03-05 12:25:26 -070017/* Enable access to PCI memory with map_sysmem() */
18static bool enable_pci_map;
19
20#ifdef CONFIG_PCI
21/* Last device that was mapped into memory, and length of mapping */
22static struct udevice *map_dev;
23unsigned long map_len;
24#endif
25
Simon Glass5010d982015-07-06 12:54:29 -060026void sandbox_exit(void)
Simon Glass4b0730d2011-09-26 14:10:39 +000027{
Simon Glass8939df02015-05-10 21:07:27 -060028 /* Do this here while it still has an effect */
29 os_fd_restore();
Simon Glass5c2859c2013-11-10 10:27:03 -070030 if (state_uninit())
31 os_exit(2);
32
Simon Glass61336832014-07-23 06:55:02 -060033 if (dm_uninit())
34 os_exit(2);
35
Simon Glass7a9219c2011-10-03 19:26:44 +000036 /* This is considered normal termination for now */
37 os_exit(0);
Simon Glass88bd0e92013-11-10 10:27:00 -070038}
39
Simon Glass4b0730d2011-09-26 14:10:39 +000040/* delay x useconds */
41void __udelay(unsigned long usec)
42{
Simon Glass97235632015-11-08 23:47:43 -070043 struct sandbox_state *state = state_get_current();
44
45 if (!state->skip_delays)
46 os_usleep(usec);
Simon Glass4b0730d2011-09-26 14:10:39 +000047}
48
Simon Glass4b0730d2011-09-26 14:10:39 +000049int cleanup_before_linux(void)
50{
51 return 0;
52}
53
Simon Glass29748512015-05-13 07:02:26 -060054int cleanup_before_linux_select(int flags)
55{
56 return 0;
57}
58
Simon Glass4b0730d2011-09-26 14:10:39 +000059void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
60{
Simon Glassa7d9cae2016-07-04 11:57:49 -060061#if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD)
Simon Glass9569c402015-03-05 12:25:26 -070062 unsigned long plen = len;
63 void *ptr;
64
65 map_dev = NULL;
66 if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) {
67 if (plen != len) {
68 printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n",
69 __func__, paddr, len, plen);
70 }
71 map_len = len;
72 return ptr;
73 }
74#endif
75
Simon Glass8ee666a2012-12-13 20:49:11 +000076 return (void *)(gd->arch.ram_buf + paddr);
Simon Glass4b0730d2011-09-26 14:10:39 +000077}
78
Simon Glass9569c402015-03-05 12:25:26 -070079void unmap_physmem(const void *vaddr, unsigned long flags)
80{
81#ifdef CONFIG_PCI
82 if (map_dev) {
83 pci_unmap_physmem(vaddr, map_len, map_dev);
84 map_dev = NULL;
85 }
86#endif
87}
88
89void sandbox_set_enable_pci_map(int enable)
90{
91 enable_pci_map = enable;
92}
93
Simon Glass66bd1cf2014-02-27 13:25:55 -070094phys_addr_t map_to_sysmem(const void *ptr)
Simon Glass781adb52013-04-20 08:42:37 +000095{
96 return (u8 *)ptr - gd->arch.ram_buf;
97}
98
Simon Glass4b0730d2011-09-26 14:10:39 +000099void flush_dcache_range(unsigned long start, unsigned long stop)
100{
101}
Simon Glassb45122f2015-02-27 22:06:34 -0700102
Bin Meng0d1414b2017-08-22 08:15:18 -0700103void invalidate_dcache_range(unsigned long start, unsigned long stop)
104{
105}
106
Simon Glassb45122f2015-02-27 22:06:34 -0700107int sandbox_read_fdt_from_file(void)
108{
109 struct sandbox_state *state = state_get_current();
110 const char *fname = state->fdt_fname;
111 void *blob;
112 loff_t size;
113 int err;
114 int fd;
115
116 blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0);
117 if (!state->fdt_fname) {
118 err = fdt_create_empty_tree(blob, 256);
119 if (!err)
120 goto done;
121 printf("Unable to create empty FDT: %s\n", fdt_strerror(err));
122 return -EINVAL;
123 }
124
125 err = os_get_filesize(fname, &size);
126 if (err < 0) {
127 printf("Failed to file FDT file '%s'\n", fname);
128 return err;
129 }
130 fd = os_open(fname, OS_O_RDONLY);
131 if (fd < 0) {
132 printf("Failed to open FDT file '%s'\n", fname);
133 return -EACCES;
134 }
135 if (os_read(fd, blob, size) != size) {
136 os_close(fd);
137 return -EIO;
138 }
139 os_close(fd);
140
141done:
142 gd->fdt_blob = blob;
143
144 return 0;
145}
Simon Glassc87dc382017-05-22 05:05:23 -0600146
147ulong timer_get_boot_us(void)
148{
149 static uint64_t base_count;
150 uint64_t count = os_get_nsec();
151
152 if (!base_count)
153 base_count = count;
154
155 return (count - base_count) / 1000;
156}