]> git.ipfire.org Git - people/ms/u-boot.git/blame - arch/sandbox/cpu/cpu.c
powerpc: Use asm-generic/io.h
[people/ms/u-boot.git] / arch / sandbox / cpu / cpu.c
CommitLineData
4b0730d2
SG
1/*
2 * Copyright (c) 2011 The Chromium OS Authors.
1a459660 3 * SPDX-License-Identifier: GPL-2.0+
4b0730d2 4 */
9569c406 5#define DEBUG
4b0730d2 6#include <common.h>
9d922450 7#include <dm.h>
f4289cbd
SG
8#include <errno.h>
9#include <libfdt.h>
7a9219c1 10#include <os.h>
b45122fd 11#include <asm/io.h>
5c2859cd 12#include <asm/state.h>
6e206504 13#include <dm/root.h>
4b0730d2
SG
14
15DECLARE_GLOBAL_DATA_PTR;
16
9569c406
SG
17/* 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
5010d98f 26void sandbox_exit(void)
4b0730d2 27{
8939df09
SG
28 /* Do this here while it still has an effect */
29 os_fd_restore();
5c2859cd
SG
30 if (state_uninit())
31 os_exit(2);
32
61336833
SG
33 if (dm_uninit())
34 os_exit(2);
35
7a9219c1
SG
36 /* This is considered normal termination for now */
37 os_exit(0);
88bd0e9d
SG
38}
39
4b0730d2
SG
40/* delay x useconds */
41void __udelay(unsigned long usec)
42{
9723563a
SG
43 struct sandbox_state *state = state_get_current();
44
45 if (!state->skip_delays)
46 os_usleep(usec);
4b0730d2
SG
47}
48
4b0730d2
SG
49int cleanup_before_linux(void)
50{
51 return 0;
52}
53
29748515
SG
54int cleanup_before_linux_select(int flags)
55{
56 return 0;
57}
58
4b0730d2
SG
59void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
60{
a7d9caec 61#if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD)
9569c406
SG
62 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
8ee666a7 76 return (void *)(gd->arch.ram_buf + paddr);
4b0730d2
SG
77}
78
9569c406
SG
79void 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
66bd1cff 94phys_addr_t map_to_sysmem(const void *ptr)
781adb57
SG
95{
96 return (u8 *)ptr - gd->arch.ram_buf;
97}
98
4b0730d2
SG
99void flush_dcache_range(unsigned long start, unsigned long stop)
100{
101}
b45122fd 102
0d1414bd
BM
103void invalidate_dcache_range(unsigned long start, unsigned long stop)
104{
105}
106
b45122fd
SG
107int 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}
c87dc38d
SG
146
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}