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