cpu.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * SPDX-License-Identifier: GPL-2.0+
  4. */
  5. #define DEBUG
  6. #include <common.h>
  7. #include <dm/root.h>
  8. #include <os.h>
  9. #include <asm/io.h>
  10. #include <asm/state.h>
  11. DECLARE_GLOBAL_DATA_PTR;
  12. /* Enable access to PCI memory with map_sysmem() */
  13. static bool enable_pci_map;
  14. #ifdef CONFIG_PCI
  15. /* Last device that was mapped into memory, and length of mapping */
  16. static struct udevice *map_dev;
  17. unsigned long map_len;
  18. #endif
  19. void reset_cpu(ulong ignored)
  20. {
  21. if (state_uninit())
  22. os_exit(2);
  23. if (dm_uninit())
  24. os_exit(2);
  25. /* This is considered normal termination for now */
  26. os_exit(0);
  27. }
  28. int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  29. {
  30. reset_cpu(0);
  31. return 0;
  32. }
  33. /* delay x useconds */
  34. void __udelay(unsigned long usec)
  35. {
  36. os_usleep(usec);
  37. }
  38. unsigned long __attribute__((no_instrument_function)) timer_get_us(void)
  39. {
  40. return os_get_nsec() / 1000;
  41. }
  42. int cleanup_before_linux(void)
  43. {
  44. return 0;
  45. }
  46. void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
  47. {
  48. #ifdef CONFIG_PCI
  49. unsigned long plen = len;
  50. void *ptr;
  51. map_dev = NULL;
  52. if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) {
  53. if (plen != len) {
  54. printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n",
  55. __func__, paddr, len, plen);
  56. }
  57. map_len = len;
  58. return ptr;
  59. }
  60. #endif
  61. return (void *)(gd->arch.ram_buf + paddr);
  62. }
  63. void unmap_physmem(const void *vaddr, unsigned long flags)
  64. {
  65. #ifdef CONFIG_PCI
  66. if (map_dev) {
  67. pci_unmap_physmem(vaddr, map_len, map_dev);
  68. map_dev = NULL;
  69. }
  70. #endif
  71. }
  72. void sandbox_set_enable_pci_map(int enable)
  73. {
  74. enable_pci_map = enable;
  75. }
  76. phys_addr_t map_to_sysmem(const void *ptr)
  77. {
  78. return (u8 *)ptr - gd->arch.ram_buf;
  79. }
  80. void flush_dcache_range(unsigned long start, unsigned long stop)
  81. {
  82. }
  83. int sandbox_read_fdt_from_file(void)
  84. {
  85. struct sandbox_state *state = state_get_current();
  86. const char *fname = state->fdt_fname;
  87. void *blob;
  88. loff_t size;
  89. int err;
  90. int fd;
  91. blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0);
  92. if (!state->fdt_fname) {
  93. err = fdt_create_empty_tree(blob, 256);
  94. if (!err)
  95. goto done;
  96. printf("Unable to create empty FDT: %s\n", fdt_strerror(err));
  97. return -EINVAL;
  98. }
  99. err = os_get_filesize(fname, &size);
  100. if (err < 0) {
  101. printf("Failed to file FDT file '%s'\n", fname);
  102. return err;
  103. }
  104. fd = os_open(fname, OS_O_RDONLY);
  105. if (fd < 0) {
  106. printf("Failed to open FDT file '%s'\n", fname);
  107. return -EACCES;
  108. }
  109. if (os_read(fd, blob, size) != size) {
  110. os_close(fd);
  111. return -EIO;
  112. }
  113. os_close(fd);
  114. done:
  115. gd->fdt_blob = blob;
  116. return 0;
  117. }