cpu.c 2.5 KB

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