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 | 141e5a0 | 2016-07-04 11:57:48 -0600 | [diff] [blame] | 7 | #include <errno.h> |
| 8 | #include <libfdt.h> |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 9 | #include <os.h> |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 10 | #include <asm/io.h> |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 11 | #include <asm/state.h> |
Simon Glass | be31037 | 2016-07-04 11:57:47 -0600 | [diff] [blame] | 12 | #include <dm/root.h> |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 13 | |
| 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 16 | /* Enable access to PCI memory with map_sysmem() */ |
| 17 | static bool enable_pci_map; |
| 18 | |
| 19 | #ifdef CONFIG_PCI |
| 20 | /* Last device that was mapped into memory, and length of mapping */ |
| 21 | static struct udevice *map_dev; |
| 22 | unsigned long map_len; |
| 23 | #endif |
| 24 | |
Simon Glass | d860f22 | 2015-07-06 12:54:29 -0600 | [diff] [blame] | 25 | void sandbox_exit(void) |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 26 | { |
Simon Glass | 9c3b7d6 | 2015-05-10 21:07:27 -0600 | [diff] [blame] | 27 | /* Do this here while it still has an effect */ |
| 28 | os_fd_restore(); |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 29 | if (state_uninit()) |
| 30 | os_exit(2); |
| 31 | |
Simon Glass | 1920051 | 2014-07-23 06:55:02 -0600 | [diff] [blame] | 32 | if (dm_uninit()) |
| 33 | os_exit(2); |
| 34 | |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 35 | /* This is considered normal termination for now */ |
| 36 | os_exit(0); |
Simon Glass | dc9f8cd | 2013-11-10 10:27:00 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 39 | /* delay x useconds */ |
| 40 | void __udelay(unsigned long usec) |
| 41 | { |
Simon Glass | b25fa31 | 2015-11-08 23:47:43 -0700 | [diff] [blame] | 42 | struct sandbox_state *state = state_get_current(); |
| 43 | |
| 44 | if (!state->skip_delays) |
| 45 | os_usleep(usec); |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 48 | int cleanup_before_linux(void) |
| 49 | { |
| 50 | return 0; |
| 51 | } |
| 52 | |
Simon Glass | 770d93f | 2015-05-13 07:02:26 -0600 | [diff] [blame] | 53 | int cleanup_before_linux_select(int flags) |
| 54 | { |
| 55 | return 0; |
| 56 | } |
| 57 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 58 | void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags) |
| 59 | { |
Simon Glass | f44a3a5 | 2016-07-04 11:57:49 -0600 | [diff] [blame] | 60 | #if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD) |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 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 Glass | 6437147 | 2012-12-13 20:49:11 +0000 | [diff] [blame] | 75 | return (void *)(gd->arch.ram_buf + paddr); |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 78 | void 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 | |
| 88 | void sandbox_set_enable_pci_map(int enable) |
| 89 | { |
| 90 | enable_pci_map = enable; |
| 91 | } |
| 92 | |
Simon Glass | f7c3f6f | 2014-02-27 13:25:55 -0700 | [diff] [blame] | 93 | phys_addr_t map_to_sysmem(const void *ptr) |
Simon Glass | e23d293 | 2013-04-20 08:42:37 +0000 | [diff] [blame] | 94 | { |
| 95 | return (u8 *)ptr - gd->arch.ram_buf; |
| 96 | } |
| 97 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 98 | void flush_dcache_range(unsigned long start, unsigned long stop) |
| 99 | { |
| 100 | } |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 101 | |
| 102 | int 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 | |
| 136 | done: |
| 137 | gd->fdt_blob = blob; |
| 138 | |
| 139 | return 0; |
| 140 | } |