Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The Chromium OS Authors. |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 3 | * SPDX-License-Identifier: GPL-2.0+ |
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> |
| 9 | #include <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 | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 12 | #include <asm/state.h> |
Simon Glass | be31037 | 2016-07-04 11:57:47 -0600 | [diff] [blame] | 13 | #include <dm/root.h> |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 17 | /* Enable access to PCI memory with map_sysmem() */ |
| 18 | static bool enable_pci_map; |
| 19 | |
| 20 | #ifdef CONFIG_PCI |
| 21 | /* Last device that was mapped into memory, and length of mapping */ |
| 22 | static struct udevice *map_dev; |
| 23 | unsigned long map_len; |
| 24 | #endif |
| 25 | |
Simon Glass | d860f22 | 2015-07-06 12:54:29 -0600 | [diff] [blame] | 26 | void sandbox_exit(void) |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 27 | { |
Simon Glass | 9c3b7d6 | 2015-05-10 21:07:27 -0600 | [diff] [blame] | 28 | /* Do this here while it still has an effect */ |
| 29 | os_fd_restore(); |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 30 | if (state_uninit()) |
| 31 | os_exit(2); |
| 32 | |
Simon Glass | 1920051 | 2014-07-23 06:55:02 -0600 | [diff] [blame] | 33 | if (dm_uninit()) |
| 34 | os_exit(2); |
| 35 | |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 36 | /* This is considered normal termination for now */ |
| 37 | os_exit(0); |
Simon Glass | dc9f8cd | 2013-11-10 10:27:00 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 40 | /* delay x useconds */ |
| 41 | void __udelay(unsigned long usec) |
| 42 | { |
Simon Glass | b25fa31 | 2015-11-08 23:47:43 -0700 | [diff] [blame] | 43 | struct sandbox_state *state = state_get_current(); |
| 44 | |
| 45 | if (!state->skip_delays) |
| 46 | os_usleep(usec); |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 49 | int cleanup_before_linux(void) |
| 50 | { |
| 51 | return 0; |
| 52 | } |
| 53 | |
Simon Glass | 770d93f | 2015-05-13 07:02:26 -0600 | [diff] [blame] | 54 | int cleanup_before_linux_select(int flags) |
| 55 | { |
| 56 | return 0; |
| 57 | } |
| 58 | |
Paul Burton | 5136925 | 2017-09-14 15:05:13 -0700 | [diff] [blame] | 59 | void *phys_to_virt(phys_addr_t paddr) |
| 60 | { |
| 61 | return (void *)(gd->arch.ram_buf + paddr); |
| 62 | } |
| 63 | |
| 64 | phys_addr_t virt_to_phys(void *vaddr) |
| 65 | { |
| 66 | return (phys_addr_t)((uint8_t *)vaddr - gd->arch.ram_buf); |
| 67 | } |
| 68 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 69 | void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags) |
| 70 | { |
Simon Glass | f44a3a5 | 2016-07-04 11:57:49 -0600 | [diff] [blame] | 71 | #if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD) |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 72 | unsigned long plen = len; |
| 73 | void *ptr; |
| 74 | |
| 75 | map_dev = NULL; |
| 76 | if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) { |
| 77 | if (plen != len) { |
| 78 | printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n", |
Mario Six | 61efece | 2018-02-12 08:05:57 +0100 | [diff] [blame^] | 79 | __func__, (uint)paddr, len, plen); |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 80 | } |
| 81 | map_len = len; |
| 82 | return ptr; |
| 83 | } |
| 84 | #endif |
| 85 | |
Paul Burton | 5136925 | 2017-09-14 15:05:13 -0700 | [diff] [blame] | 86 | return phys_to_virt(paddr); |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 89 | void unmap_physmem(const void *vaddr, unsigned long flags) |
| 90 | { |
| 91 | #ifdef CONFIG_PCI |
| 92 | if (map_dev) { |
| 93 | pci_unmap_physmem(vaddr, map_len, map_dev); |
| 94 | map_dev = NULL; |
| 95 | } |
| 96 | #endif |
| 97 | } |
| 98 | |
| 99 | void sandbox_set_enable_pci_map(int enable) |
| 100 | { |
| 101 | enable_pci_map = enable; |
| 102 | } |
| 103 | |
Simon Glass | f7c3f6f | 2014-02-27 13:25:55 -0700 | [diff] [blame] | 104 | phys_addr_t map_to_sysmem(const void *ptr) |
Simon Glass | e23d293 | 2013-04-20 08:42:37 +0000 | [diff] [blame] | 105 | { |
| 106 | return (u8 *)ptr - gd->arch.ram_buf; |
| 107 | } |
| 108 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 109 | void flush_dcache_range(unsigned long start, unsigned long stop) |
| 110 | { |
| 111 | } |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 112 | |
Bin Meng | 843c299 | 2017-08-22 08:15:18 -0700 | [diff] [blame] | 113 | void invalidate_dcache_range(unsigned long start, unsigned long stop) |
| 114 | { |
| 115 | } |
| 116 | |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 117 | int sandbox_read_fdt_from_file(void) |
| 118 | { |
| 119 | struct sandbox_state *state = state_get_current(); |
| 120 | const char *fname = state->fdt_fname; |
| 121 | void *blob; |
| 122 | loff_t size; |
| 123 | int err; |
| 124 | int fd; |
| 125 | |
| 126 | blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0); |
| 127 | if (!state->fdt_fname) { |
| 128 | err = fdt_create_empty_tree(blob, 256); |
| 129 | if (!err) |
| 130 | goto done; |
| 131 | printf("Unable to create empty FDT: %s\n", fdt_strerror(err)); |
| 132 | return -EINVAL; |
| 133 | } |
| 134 | |
| 135 | err = os_get_filesize(fname, &size); |
| 136 | if (err < 0) { |
| 137 | printf("Failed to file FDT file '%s'\n", fname); |
| 138 | return err; |
| 139 | } |
| 140 | fd = os_open(fname, OS_O_RDONLY); |
| 141 | if (fd < 0) { |
| 142 | printf("Failed to open FDT file '%s'\n", fname); |
| 143 | return -EACCES; |
| 144 | } |
| 145 | if (os_read(fd, blob, size) != size) { |
| 146 | os_close(fd); |
| 147 | return -EIO; |
| 148 | } |
| 149 | os_close(fd); |
| 150 | |
| 151 | done: |
| 152 | gd->fdt_blob = blob; |
| 153 | |
| 154 | return 0; |
| 155 | } |
Simon Glass | 38e7974 | 2017-05-22 05:05:23 -0600 | [diff] [blame] | 156 | |
| 157 | ulong timer_get_boot_us(void) |
| 158 | { |
| 159 | static uint64_t base_count; |
| 160 | uint64_t count = os_get_nsec(); |
| 161 | |
| 162 | if (!base_count) |
| 163 | base_count = count; |
| 164 | |
| 165 | return (count - base_count) / 1000; |
| 166 | } |