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 | 9d92245 | 2017-05-17 17:18:03 -0600 | [diff] [blame^] | 7 | #include <dm.h> |
Simon Glass | f4289cb | 2016-07-04 11:57:48 -0600 | [diff] [blame] | 8 | #include <errno.h> |
| 9 | #include <libfdt.h> |
Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 10 | #include <os.h> |
Simon Glass | b45122f | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 11 | #include <asm/io.h> |
Simon Glass | 5c2859c | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 12 | #include <asm/state.h> |
Simon Glass | 6e20650 | 2016-07-04 11:57:47 -0600 | [diff] [blame] | 13 | #include <dm/root.h> |
Simon Glass | 4b0730d | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Simon Glass | 9569c40 | 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 | 5010d98 | 2015-07-06 12:54:29 -0600 | [diff] [blame] | 26 | void sandbox_exit(void) |
Simon Glass | 4b0730d | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 27 | { |
Simon Glass | 8939df0 | 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 | 5c2859c | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 30 | if (state_uninit()) |
| 31 | os_exit(2); |
| 32 | |
Simon Glass | 6133683 | 2014-07-23 06:55:02 -0600 | [diff] [blame] | 33 | if (dm_uninit()) |
| 34 | os_exit(2); |
| 35 | |
Simon Glass | 7a9219c | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 36 | /* This is considered normal termination for now */ |
| 37 | os_exit(0); |
Simon Glass | 88bd0e9 | 2013-11-10 10:27:00 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Simon Glass | 4b0730d | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 40 | /* delay x useconds */ |
| 41 | void __udelay(unsigned long usec) |
| 42 | { |
Simon Glass | 9723563 | 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 | 4b0730d | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Simon Glass | 4b0730d | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 49 | int cleanup_before_linux(void) |
| 50 | { |
| 51 | return 0; |
| 52 | } |
| 53 | |
Simon Glass | 2974851 | 2015-05-13 07:02:26 -0600 | [diff] [blame] | 54 | int cleanup_before_linux_select(int flags) |
| 55 | { |
| 56 | return 0; |
| 57 | } |
| 58 | |
Simon Glass | 4b0730d | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 59 | void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags) |
| 60 | { |
Simon Glass | a7d9cae | 2016-07-04 11:57:49 -0600 | [diff] [blame] | 61 | #if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD) |
Simon Glass | 9569c40 | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 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 | |
Simon Glass | 8ee666a | 2012-12-13 20:49:11 +0000 | [diff] [blame] | 76 | return (void *)(gd->arch.ram_buf + paddr); |
Simon Glass | 4b0730d | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Simon Glass | 9569c40 | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 79 | void 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 | |
| 89 | void sandbox_set_enable_pci_map(int enable) |
| 90 | { |
| 91 | enable_pci_map = enable; |
| 92 | } |
| 93 | |
Simon Glass | 66bd1cf | 2014-02-27 13:25:55 -0700 | [diff] [blame] | 94 | phys_addr_t map_to_sysmem(const void *ptr) |
Simon Glass | 781adb5 | 2013-04-20 08:42:37 +0000 | [diff] [blame] | 95 | { |
| 96 | return (u8 *)ptr - gd->arch.ram_buf; |
| 97 | } |
| 98 | |
Simon Glass | 4b0730d | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 99 | void flush_dcache_range(unsigned long start, unsigned long stop) |
| 100 | { |
| 101 | } |
Simon Glass | b45122f | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 102 | |
| 103 | int sandbox_read_fdt_from_file(void) |
| 104 | { |
| 105 | struct sandbox_state *state = state_get_current(); |
| 106 | const char *fname = state->fdt_fname; |
| 107 | void *blob; |
| 108 | loff_t size; |
| 109 | int err; |
| 110 | int fd; |
| 111 | |
| 112 | blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0); |
| 113 | if (!state->fdt_fname) { |
| 114 | err = fdt_create_empty_tree(blob, 256); |
| 115 | if (!err) |
| 116 | goto done; |
| 117 | printf("Unable to create empty FDT: %s\n", fdt_strerror(err)); |
| 118 | return -EINVAL; |
| 119 | } |
| 120 | |
| 121 | err = os_get_filesize(fname, &size); |
| 122 | if (err < 0) { |
| 123 | printf("Failed to file FDT file '%s'\n", fname); |
| 124 | return err; |
| 125 | } |
| 126 | fd = os_open(fname, OS_O_RDONLY); |
| 127 | if (fd < 0) { |
| 128 | printf("Failed to open FDT file '%s'\n", fname); |
| 129 | return -EACCES; |
| 130 | } |
| 131 | if (os_read(fd, blob, size) != size) { |
| 132 | os_close(fd); |
| 133 | return -EIO; |
| 134 | } |
| 135 | os_close(fd); |
| 136 | |
| 137 | done: |
| 138 | gd->fdt_blob = blob; |
| 139 | |
| 140 | return 0; |
| 141 | } |