blob: 168f2efa33e5fe4f5eaab52503dbb724fd2f5528 [file] [log] [blame]
Simon Glass3d750d72011-09-26 14:10:39 +00001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02003 * SPDX-License-Identifier: GPL-2.0+
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 Glass19200512014-07-23 06:55:02 -06007#include <dm/root.h>
Simon Glasscd0684f2011-10-03 19:26:44 +00008#include <os.h>
Simon Glass26b78b22015-02-27 22:06:34 -07009#include <asm/io.h>
Simon Glass9dd10bf2013-11-10 10:27:03 -070010#include <asm/state.h>
Simon Glass3d750d72011-09-26 14:10:39 +000011
12DECLARE_GLOBAL_DATA_PTR;
13
Simon Glass70778bc2015-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 Glassdc9f8cd2013-11-10 10:27:00 -070023void reset_cpu(ulong ignored)
Simon Glass3d750d72011-09-26 14:10:39 +000024{
Simon Glass9dd10bf2013-11-10 10:27:03 -070025 if (state_uninit())
26 os_exit(2);
27
Simon Glass19200512014-07-23 06:55:02 -060028 if (dm_uninit())
29 os_exit(2);
30
Simon Glasscd0684f2011-10-03 19:26:44 +000031 /* This is considered normal termination for now */
32 os_exit(0);
Simon Glassdc9f8cd2013-11-10 10:27:00 -070033}
34
35int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
36{
37 reset_cpu(0);
38
Simon Glass3d750d72011-09-26 14:10:39 +000039 return 0;
40}
41
42/* delay x useconds */
43void __udelay(unsigned long usec)
44{
Matthias Weisser0d3dd142011-11-29 12:16:40 +010045 os_usleep(usec);
Simon Glass3d750d72011-09-26 14:10:39 +000046}
47
Simon Glasse7f1eb52013-06-11 11:14:44 -070048unsigned long __attribute__((no_instrument_function)) timer_get_us(void)
Simon Glass3d750d72011-09-26 14:10:39 +000049{
Matthias Weisser0d3dd142011-11-29 12:16:40 +010050 return os_get_nsec() / 1000;
Simon Glass3d750d72011-09-26 14:10:39 +000051}
52
Simon Glass3d750d72011-09-26 14:10:39 +000053int cleanup_before_linux(void)
54{
55 return 0;
56}
57
58void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
59{
Simon Glass70778bc2015-03-05 12:25:26 -070060#ifdef CONFIG_PCI
61 unsigned long plen = len;
62 void *ptr;
63
64 map_dev = NULL;
65 if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) {
66 if (plen != len) {
67 printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n",
68 __func__, paddr, len, plen);
69 }
70 map_len = len;
71 return ptr;
72 }
73#endif
74
Simon Glass64371472012-12-13 20:49:11 +000075 return (void *)(gd->arch.ram_buf + paddr);
Simon Glass3d750d72011-09-26 14:10:39 +000076}
77
Simon Glass70778bc2015-03-05 12:25:26 -070078void unmap_physmem(const void *vaddr, unsigned long flags)
79{
80#ifdef CONFIG_PCI
81 if (map_dev) {
82 pci_unmap_physmem(vaddr, map_len, map_dev);
83 map_dev = NULL;
84 }
85#endif
86}
87
88void sandbox_set_enable_pci_map(int enable)
89{
90 enable_pci_map = enable;
91}
92
Simon Glassf7c3f6f2014-02-27 13:25:55 -070093phys_addr_t map_to_sysmem(const void *ptr)
Simon Glasse23d2932013-04-20 08:42:37 +000094{
95 return (u8 *)ptr - gd->arch.ram_buf;
96}
97
Simon Glass3d750d72011-09-26 14:10:39 +000098void flush_dcache_range(unsigned long start, unsigned long stop)
99{
100}
Simon Glass26b78b22015-02-27 22:06:34 -0700101
102int sandbox_read_fdt_from_file(void)
103{
104 struct sandbox_state *state = state_get_current();
105 const char *fname = state->fdt_fname;
106 void *blob;
107 loff_t size;
108 int err;
109 int fd;
110
111 blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0);
112 if (!state->fdt_fname) {
113 err = fdt_create_empty_tree(blob, 256);
114 if (!err)
115 goto done;
116 printf("Unable to create empty FDT: %s\n", fdt_strerror(err));
117 return -EINVAL;
118 }
119
120 err = os_get_filesize(fname, &size);
121 if (err < 0) {
122 printf("Failed to file FDT file '%s'\n", fname);
123 return err;
124 }
125 fd = os_open(fname, OS_O_RDONLY);
126 if (fd < 0) {
127 printf("Failed to open FDT file '%s'\n", fname);
128 return -EACCES;
129 }
130 if (os_read(fd, blob, size) != size) {
131 os_close(fd);
132 return -EIO;
133 }
134 os_close(fd);
135
136done:
137 gd->fdt_blob = blob;
138
139 return 0;
140}