Simon Glass | 4b0730d | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The Chromium OS Authors. |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 3 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 4b0730d | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 4 | */ |
Simon Glass | 9569c40 | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 5 | #define DEBUG |
Simon Glass | 4b0730d | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 6 | #include <common.h> |
Simon Glass | 6133683 | 2014-07-23 06:55:02 -0600 | [diff] [blame] | 7 | #include <dm/root.h> |
Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 8 | #include <os.h> |
Simon Glass | b45122f | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 9 | #include <asm/io.h> |
Simon Glass | 5c2859c | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 10 | #include <asm/state.h> |
Simon Glass | 4b0730d | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 11 | |
| 12 | DECLARE_GLOBAL_DATA_PTR; |
| 13 | |
Simon Glass | 9569c40 | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 14 | /* Enable access to PCI memory with map_sysmem() */ |
| 15 | static bool enable_pci_map; |
| 16 | |
| 17 | #ifdef CONFIG_PCI |
| 18 | /* Last device that was mapped into memory, and length of mapping */ |
| 19 | static struct udevice *map_dev; |
| 20 | unsigned long map_len; |
| 21 | #endif |
| 22 | |
Simon Glass | 5010d98 | 2015-07-06 12:54:29 -0600 | [diff] [blame] | 23 | void sandbox_exit(void) |
Simon Glass | 4b0730d | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 24 | { |
Simon Glass | 8939df0 | 2015-05-10 21:07:27 -0600 | [diff] [blame] | 25 | /* Do this here while it still has an effect */ |
| 26 | os_fd_restore(); |
Simon Glass | 5c2859c | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 27 | if (state_uninit()) |
| 28 | os_exit(2); |
| 29 | |
Simon Glass | 6133683 | 2014-07-23 06:55:02 -0600 | [diff] [blame] | 30 | if (dm_uninit()) |
| 31 | os_exit(2); |
| 32 | |
Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 33 | /* This is considered normal termination for now */ |
| 34 | os_exit(0); |
Simon Glass | 88bd0e9 | 2013-11-10 10:27:00 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Simon Glass | 4b0730d | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 37 | /* delay x useconds */ |
| 38 | void __udelay(unsigned long usec) |
| 39 | { |
Simon Glass | 9723563 | 2015-11-08 23:47:43 -0700 | [diff] [blame] | 40 | struct sandbox_state *state = state_get_current(); |
| 41 | |
| 42 | if (!state->skip_delays) |
| 43 | os_usleep(usec); |
Simon Glass | 4b0730d | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Simon Glass | 4b0730d | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 46 | int cleanup_before_linux(void) |
| 47 | { |
| 48 | return 0; |
| 49 | } |
| 50 | |
Simon Glass | 2974851 | 2015-05-13 07:02:26 -0600 | [diff] [blame] | 51 | int cleanup_before_linux_select(int flags) |
| 52 | { |
| 53 | return 0; |
| 54 | } |
| 55 | |
Simon Glass | 4b0730d | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 56 | void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags) |
| 57 | { |
Simon Glass | 9569c40 | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 58 | #ifdef CONFIG_PCI |
| 59 | unsigned long plen = len; |
| 60 | void *ptr; |
| 61 | |
| 62 | map_dev = NULL; |
| 63 | if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) { |
| 64 | if (plen != len) { |
| 65 | printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n", |
| 66 | __func__, paddr, len, plen); |
| 67 | } |
| 68 | map_len = len; |
| 69 | return ptr; |
| 70 | } |
| 71 | #endif |
| 72 | |
Simon Glass | 8ee666a | 2012-12-13 20:49:11 +0000 | [diff] [blame] | 73 | return (void *)(gd->arch.ram_buf + paddr); |
Simon Glass | 4b0730d | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Simon Glass | 9569c40 | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 76 | void unmap_physmem(const void *vaddr, unsigned long flags) |
| 77 | { |
| 78 | #ifdef CONFIG_PCI |
| 79 | if (map_dev) { |
| 80 | pci_unmap_physmem(vaddr, map_len, map_dev); |
| 81 | map_dev = NULL; |
| 82 | } |
| 83 | #endif |
| 84 | } |
| 85 | |
| 86 | void sandbox_set_enable_pci_map(int enable) |
| 87 | { |
| 88 | enable_pci_map = enable; |
| 89 | } |
| 90 | |
Simon Glass | 66bd1cf | 2014-02-27 13:25:55 -0700 | [diff] [blame] | 91 | phys_addr_t map_to_sysmem(const void *ptr) |
Simon Glass | 781adb5 | 2013-04-20 08:42:37 +0000 | [diff] [blame] | 92 | { |
| 93 | return (u8 *)ptr - gd->arch.ram_buf; |
| 94 | } |
| 95 | |
Simon Glass | 4b0730d | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 96 | void flush_dcache_range(unsigned long start, unsigned long stop) |
| 97 | { |
| 98 | } |
Simon Glass | b45122f | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 99 | |
| 100 | int sandbox_read_fdt_from_file(void) |
| 101 | { |
| 102 | struct sandbox_state *state = state_get_current(); |
| 103 | const char *fname = state->fdt_fname; |
| 104 | void *blob; |
| 105 | loff_t size; |
| 106 | int err; |
| 107 | int fd; |
| 108 | |
| 109 | blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0); |
| 110 | if (!state->fdt_fname) { |
| 111 | err = fdt_create_empty_tree(blob, 256); |
| 112 | if (!err) |
| 113 | goto done; |
| 114 | printf("Unable to create empty FDT: %s\n", fdt_strerror(err)); |
| 115 | return -EINVAL; |
| 116 | } |
| 117 | |
| 118 | err = os_get_filesize(fname, &size); |
| 119 | if (err < 0) { |
| 120 | printf("Failed to file FDT file '%s'\n", fname); |
| 121 | return err; |
| 122 | } |
| 123 | fd = os_open(fname, OS_O_RDONLY); |
| 124 | if (fd < 0) { |
| 125 | printf("Failed to open FDT file '%s'\n", fname); |
| 126 | return -EACCES; |
| 127 | } |
| 128 | if (os_read(fd, blob, size) != size) { |
| 129 | os_close(fd); |
| 130 | return -EIO; |
| 131 | } |
| 132 | os_close(fd); |
| 133 | |
| 134 | done: |
| 135 | gd->fdt_blob = blob; |
| 136 | |
| 137 | return 0; |
| 138 | } |