cpu.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.h>
  8. #include <errno.h>
  9. #include <libfdt.h>
  10. #include <os.h>
  11. #include <asm/io.h>
  12. #include <asm/state.h>
  13. #include <dm/root.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. /* Enable access to PCI memory with map_sysmem() */
  16. static bool enable_pci_map;
  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. void sandbox_exit(void)
  23. {
  24. /* Do this here while it still has an effect */
  25. os_fd_restore();
  26. if (state_uninit())
  27. os_exit(2);
  28. if (dm_uninit())
  29. os_exit(2);
  30. /* This is considered normal termination for now */
  31. os_exit(0);
  32. }
  33. /* delay x useconds */
  34. void __udelay(unsigned long usec)
  35. {
  36. struct sandbox_state *state = state_get_current();
  37. if (!state->skip_delays)
  38. os_usleep(usec);
  39. }
  40. int cleanup_before_linux(void)
  41. {
  42. return 0;
  43. }
  44. int cleanup_before_linux_select(int flags)
  45. {
  46. return 0;
  47. }
  48. void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
  49. {
  50. #if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD)
  51. unsigned long plen = len;
  52. void *ptr;
  53. map_dev = NULL;
  54. if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) {
  55. if (plen != len) {
  56. printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n",
  57. __func__, paddr, len, plen);
  58. }
  59. map_len = len;
  60. return ptr;
  61. }
  62. #endif
  63. return (void *)(gd->arch.ram_buf + paddr);
  64. }
  65. void unmap_physmem(const void *vaddr, unsigned long flags)
  66. {
  67. #ifdef CONFIG_PCI
  68. if (map_dev) {
  69. pci_unmap_physmem(vaddr, map_len, map_dev);
  70. map_dev = NULL;
  71. }
  72. #endif
  73. }
  74. void sandbox_set_enable_pci_map(int enable)
  75. {
  76. enable_pci_map = enable;
  77. }
  78. phys_addr_t map_to_sysmem(const void *ptr)
  79. {
  80. return (u8 *)ptr - gd->arch.ram_buf;
  81. }
  82. void flush_dcache_range(unsigned long start, unsigned long stop)
  83. {
  84. }
  85. int sandbox_read_fdt_from_file(void)
  86. {
  87. struct sandbox_state *state = state_get_current();
  88. const char *fname = state->fdt_fname;
  89. void *blob;
  90. loff_t size;
  91. int err;
  92. int fd;
  93. blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0);
  94. if (!state->fdt_fname) {
  95. err = fdt_create_empty_tree(blob, 256);
  96. if (!err)
  97. goto done;
  98. printf("Unable to create empty FDT: %s\n", fdt_strerror(err));
  99. return -EINVAL;
  100. }
  101. err = os_get_filesize(fname, &size);
  102. if (err < 0) {
  103. printf("Failed to file FDT file '%s'\n", fname);
  104. return err;
  105. }
  106. fd = os_open(fname, OS_O_RDONLY);
  107. if (fd < 0) {
  108. printf("Failed to open FDT file '%s'\n", fname);
  109. return -EACCES;
  110. }
  111. if (os_read(fd, blob, size) != size) {
  112. os_close(fd);
  113. return -EIO;
  114. }
  115. os_close(fd);
  116. done:
  117. gd->fdt_blob = blob;
  118. return 0;
  119. }
  120. ulong timer_get_boot_us(void)
  121. {
  122. static uint64_t base_count;
  123. uint64_t count = os_get_nsec();
  124. if (!base_count)
  125. base_count = count;
  126. return (count - base_count) / 1000;
  127. }