Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2011 The Chromium OS Authors. |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 4 | */ |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 5 | #define DEBUG |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 6 | #include <common.h> |
Simon Glass | 11c89f3 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 7 | #include <dm.h> |
Simon Glass | 141e5a0 | 2016-07-04 11:57:48 -0600 | [diff] [blame] | 8 | #include <errno.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 9 | #include <linux/libfdt.h> |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 10 | #include <os.h> |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 11 | #include <asm/io.h> |
Simon Glass | 5dccd15 | 2018-05-16 09:42:22 -0600 | [diff] [blame^] | 12 | #include <asm/setjmp.h> |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 13 | #include <asm/state.h> |
Simon Glass | be31037 | 2016-07-04 11:57:47 -0600 | [diff] [blame] | 14 | #include <dm/root.h> |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 18 | /* Enable access to PCI memory with map_sysmem() */ |
| 19 | static bool enable_pci_map; |
| 20 | |
| 21 | #ifdef CONFIG_PCI |
| 22 | /* Last device that was mapped into memory, and length of mapping */ |
| 23 | static struct udevice *map_dev; |
| 24 | unsigned long map_len; |
| 25 | #endif |
| 26 | |
Simon Glass | d860f22 | 2015-07-06 12:54:29 -0600 | [diff] [blame] | 27 | void sandbox_exit(void) |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 28 | { |
Simon Glass | 9c3b7d6 | 2015-05-10 21:07:27 -0600 | [diff] [blame] | 29 | /* Do this here while it still has an effect */ |
| 30 | os_fd_restore(); |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 31 | if (state_uninit()) |
| 32 | os_exit(2); |
| 33 | |
Simon Glass | 1920051 | 2014-07-23 06:55:02 -0600 | [diff] [blame] | 34 | if (dm_uninit()) |
| 35 | os_exit(2); |
| 36 | |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 37 | /* This is considered normal termination for now */ |
| 38 | os_exit(0); |
Simon Glass | dc9f8cd | 2013-11-10 10:27:00 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 41 | /* delay x useconds */ |
| 42 | void __udelay(unsigned long usec) |
| 43 | { |
Simon Glass | b25fa31 | 2015-11-08 23:47:43 -0700 | [diff] [blame] | 44 | struct sandbox_state *state = state_get_current(); |
| 45 | |
| 46 | if (!state->skip_delays) |
| 47 | os_usleep(usec); |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 50 | int cleanup_before_linux(void) |
| 51 | { |
| 52 | return 0; |
| 53 | } |
| 54 | |
Simon Glass | 770d93f | 2015-05-13 07:02:26 -0600 | [diff] [blame] | 55 | int cleanup_before_linux_select(int flags) |
| 56 | { |
| 57 | return 0; |
| 58 | } |
| 59 | |
Paul Burton | 5136925 | 2017-09-14 15:05:13 -0700 | [diff] [blame] | 60 | void *phys_to_virt(phys_addr_t paddr) |
| 61 | { |
| 62 | return (void *)(gd->arch.ram_buf + paddr); |
| 63 | } |
| 64 | |
| 65 | phys_addr_t virt_to_phys(void *vaddr) |
| 66 | { |
| 67 | return (phys_addr_t)((uint8_t *)vaddr - gd->arch.ram_buf); |
| 68 | } |
| 69 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 70 | void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags) |
| 71 | { |
Simon Glass | f44a3a5 | 2016-07-04 11:57:49 -0600 | [diff] [blame] | 72 | #if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD) |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 73 | 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 Six | 61efece | 2018-02-12 08:05:57 +0100 | [diff] [blame] | 80 | __func__, (uint)paddr, len, plen); |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 81 | } |
| 82 | map_len = len; |
| 83 | return ptr; |
| 84 | } |
| 85 | #endif |
| 86 | |
Paul Burton | 5136925 | 2017-09-14 15:05:13 -0700 | [diff] [blame] | 87 | return phys_to_virt(paddr); |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 90 | void 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 | |
| 100 | void sandbox_set_enable_pci_map(int enable) |
| 101 | { |
| 102 | enable_pci_map = enable; |
| 103 | } |
| 104 | |
Simon Glass | f7c3f6f | 2014-02-27 13:25:55 -0700 | [diff] [blame] | 105 | phys_addr_t map_to_sysmem(const void *ptr) |
Simon Glass | e23d293 | 2013-04-20 08:42:37 +0000 | [diff] [blame] | 106 | { |
| 107 | return (u8 *)ptr - gd->arch.ram_buf; |
| 108 | } |
| 109 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 110 | void flush_dcache_range(unsigned long start, unsigned long stop) |
| 111 | { |
| 112 | } |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 113 | |
Bin Meng | 843c299 | 2017-08-22 08:15:18 -0700 | [diff] [blame] | 114 | void invalidate_dcache_range(unsigned long start, unsigned long stop) |
| 115 | { |
| 116 | } |
| 117 | |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 118 | int 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 | |
| 152 | done: |
| 153 | gd->fdt_blob = blob; |
| 154 | |
| 155 | return 0; |
| 156 | } |
Simon Glass | 38e7974 | 2017-05-22 05:05:23 -0600 | [diff] [blame] | 157 | |
| 158 | ulong 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 Glass | 5dccd15 | 2018-05-16 09:42:22 -0600 | [diff] [blame^] | 168 | |
| 169 | int setjmp(jmp_buf jmp) |
| 170 | { |
| 171 | return os_setjmp((ulong *)jmp, sizeof(*jmp)); |
| 172 | } |
| 173 | |
| 174 | void longjmp(jmp_buf jmp, int ret) |
| 175 | { |
| 176 | os_longjmp((ulong *)jmp, ret); |
| 177 | while (1) |
| 178 | ; |
| 179 | } |