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 | 1920051 | 2014-07-23 06:55:02 -0600 | [diff] [blame] | 7 | #include <dm/root.h> |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 8 | #include <os.h> |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 9 | #include <asm/io.h> |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 10 | #include <asm/state.h> |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 11 | |
| 12 | DECLARE_GLOBAL_DATA_PTR; |
| 13 | |
Simon Glass | 70778bc | 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 | dc9f8cd | 2013-11-10 10:27:00 -0700 | [diff] [blame] | 23 | void reset_cpu(ulong ignored) |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 24 | { |
Simon Glass | 9c3b7d6 | 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 | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 27 | if (state_uninit()) |
| 28 | os_exit(2); |
| 29 | |
Simon Glass | 1920051 | 2014-07-23 06:55:02 -0600 | [diff] [blame] | 30 | if (dm_uninit()) |
| 31 | os_exit(2); |
| 32 | |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 33 | /* This is considered normal termination for now */ |
| 34 | os_exit(0); |
Simon Glass | dc9f8cd | 2013-11-10 10:27:00 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 38 | { |
| 39 | reset_cpu(0); |
| 40 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | /* delay x useconds */ |
| 45 | void __udelay(unsigned long usec) |
| 46 | { |
Matthias Weisser | 0d3dd14 | 2011-11-29 12:16:40 +0100 | [diff] [blame] | 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 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 60 | void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags) |
| 61 | { |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 62 | #ifdef CONFIG_PCI |
| 63 | unsigned long plen = len; |
| 64 | void *ptr; |
| 65 | |
| 66 | map_dev = NULL; |
| 67 | if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) { |
| 68 | if (plen != len) { |
| 69 | printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n", |
| 70 | __func__, paddr, len, plen); |
| 71 | } |
| 72 | map_len = len; |
| 73 | return ptr; |
| 74 | } |
| 75 | #endif |
| 76 | |
Simon Glass | 6437147 | 2012-12-13 20:49:11 +0000 | [diff] [blame] | 77 | return (void *)(gd->arch.ram_buf + paddr); |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 80 | void unmap_physmem(const void *vaddr, unsigned long flags) |
| 81 | { |
| 82 | #ifdef CONFIG_PCI |
| 83 | if (map_dev) { |
| 84 | pci_unmap_physmem(vaddr, map_len, map_dev); |
| 85 | map_dev = NULL; |
| 86 | } |
| 87 | #endif |
| 88 | } |
| 89 | |
| 90 | void sandbox_set_enable_pci_map(int enable) |
| 91 | { |
| 92 | enable_pci_map = enable; |
| 93 | } |
| 94 | |
Simon Glass | f7c3f6f | 2014-02-27 13:25:55 -0700 | [diff] [blame] | 95 | phys_addr_t map_to_sysmem(const void *ptr) |
Simon Glass | e23d293 | 2013-04-20 08:42:37 +0000 | [diff] [blame] | 96 | { |
| 97 | return (u8 *)ptr - gd->arch.ram_buf; |
| 98 | } |
| 99 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 100 | void flush_dcache_range(unsigned long start, unsigned long stop) |
| 101 | { |
| 102 | } |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 103 | |
| 104 | int sandbox_read_fdt_from_file(void) |
| 105 | { |
| 106 | struct sandbox_state *state = state_get_current(); |
| 107 | const char *fname = state->fdt_fname; |
| 108 | void *blob; |
| 109 | loff_t size; |
| 110 | int err; |
| 111 | int fd; |
| 112 | |
| 113 | blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0); |
| 114 | if (!state->fdt_fname) { |
| 115 | err = fdt_create_empty_tree(blob, 256); |
| 116 | if (!err) |
| 117 | goto done; |
| 118 | printf("Unable to create empty FDT: %s\n", fdt_strerror(err)); |
| 119 | return -EINVAL; |
| 120 | } |
| 121 | |
| 122 | err = os_get_filesize(fname, &size); |
| 123 | if (err < 0) { |
| 124 | printf("Failed to file FDT file '%s'\n", fname); |
| 125 | return err; |
| 126 | } |
| 127 | fd = os_open(fname, OS_O_RDONLY); |
| 128 | if (fd < 0) { |
| 129 | printf("Failed to open FDT file '%s'\n", fname); |
| 130 | return -EACCES; |
| 131 | } |
| 132 | if (os_read(fd, blob, size) != size) { |
| 133 | os_close(fd); |
| 134 | return -EIO; |
| 135 | } |
| 136 | os_close(fd); |
| 137 | |
| 138 | done: |
| 139 | gd->fdt_blob = blob; |
| 140 | |
| 141 | return 0; |
| 142 | } |