blob: 3a7f5a004b0fa341dfec557f20fa087d41674327 [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 Glass61336832014-07-23 06:55:02 -06007#include <dm/root.h>
Simon Glass7a9219c2011-10-03 19:26:44 +00008#include <os.h>
Simon Glassb45122f2015-02-27 22:06:34 -07009#include <asm/io.h>
Simon Glass5c2859c2013-11-10 10:27:03 -070010#include <asm/state.h>
Simon Glass4b0730d2011-09-26 14:10:39 +000011
12DECLARE_GLOBAL_DATA_PTR;
13
Simon Glass9569c402015-03-05 12:25:26 -070014/* Enable access to PCI memory with map_sysmem() */
15static bool enable_pci_map;
16
17#ifdef CONFIG_PCI
18/* Last device that was mapped into memory, and length of mapping */
19static struct udevice *map_dev;
20unsigned long map_len;
21#endif
22
Simon Glass5010d982015-07-06 12:54:29 -060023void sandbox_exit(void)
Simon Glass4b0730d2011-09-26 14:10:39 +000024{
Simon Glass8939df02015-05-10 21:07:27 -060025 /* Do this here while it still has an effect */
26 os_fd_restore();
Simon Glass5c2859c2013-11-10 10:27:03 -070027 if (state_uninit())
28 os_exit(2);
29
Simon Glass61336832014-07-23 06:55:02 -060030 if (dm_uninit())
31 os_exit(2);
32
Simon Glass7a9219c2011-10-03 19:26:44 +000033 /* This is considered normal termination for now */
34 os_exit(0);
Simon Glass88bd0e92013-11-10 10:27:00 -070035}
36
Simon Glass4b0730d2011-09-26 14:10:39 +000037/* delay x useconds */
38void __udelay(unsigned long usec)
39{
Matthias Weisserd99a6872011-11-29 12:16:40 +010040 os_usleep(usec);
Simon Glass4b0730d2011-09-26 14:10:39 +000041}
42
Simon Glass4b0730d2011-09-26 14:10:39 +000043int cleanup_before_linux(void)
44{
45 return 0;
46}
47
Simon Glass29748512015-05-13 07:02:26 -060048int cleanup_before_linux_select(int flags)
49{
50 return 0;
51}
52
Simon Glass4b0730d2011-09-26 14:10:39 +000053void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
54{
Simon Glass9569c402015-03-05 12:25:26 -070055#ifdef CONFIG_PCI
56 unsigned long plen = len;
57 void *ptr;
58
59 map_dev = NULL;
60 if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) {
61 if (plen != len) {
62 printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n",
63 __func__, paddr, len, plen);
64 }
65 map_len = len;
66 return ptr;
67 }
68#endif
69
Simon Glass8ee666a2012-12-13 20:49:11 +000070 return (void *)(gd->arch.ram_buf + paddr);
Simon Glass4b0730d2011-09-26 14:10:39 +000071}
72
Simon Glass9569c402015-03-05 12:25:26 -070073void unmap_physmem(const void *vaddr, unsigned long flags)
74{
75#ifdef CONFIG_PCI
76 if (map_dev) {
77 pci_unmap_physmem(vaddr, map_len, map_dev);
78 map_dev = NULL;
79 }
80#endif
81}
82
83void sandbox_set_enable_pci_map(int enable)
84{
85 enable_pci_map = enable;
86}
87
Simon Glass66bd1cf2014-02-27 13:25:55 -070088phys_addr_t map_to_sysmem(const void *ptr)
Simon Glass781adb52013-04-20 08:42:37 +000089{
90 return (u8 *)ptr - gd->arch.ram_buf;
91}
92
Simon Glass4b0730d2011-09-26 14:10:39 +000093void flush_dcache_range(unsigned long start, unsigned long stop)
94{
95}
Simon Glassb45122f2015-02-27 22:06:34 -070096
97int sandbox_read_fdt_from_file(void)
98{
99 struct sandbox_state *state = state_get_current();
100 const char *fname = state->fdt_fname;
101 void *blob;
102 loff_t size;
103 int err;
104 int fd;
105
106 blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0);
107 if (!state->fdt_fname) {
108 err = fdt_create_empty_tree(blob, 256);
109 if (!err)
110 goto done;
111 printf("Unable to create empty FDT: %s\n", fdt_strerror(err));
112 return -EINVAL;
113 }
114
115 err = os_get_filesize(fname, &size);
116 if (err < 0) {
117 printf("Failed to file FDT file '%s'\n", fname);
118 return err;
119 }
120 fd = os_open(fname, OS_O_RDONLY);
121 if (fd < 0) {
122 printf("Failed to open FDT file '%s'\n", fname);
123 return -EACCES;
124 }
125 if (os_read(fd, blob, size) != size) {
126 os_close(fd);
127 return -EIO;
128 }
129 os_close(fd);
130
131done:
132 gd->fdt_blob = blob;
133
134 return 0;
135}