cpu.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 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. os_usleep(usec);
  34. }
  35. int cleanup_before_linux(void)
  36. {
  37. return 0;
  38. }
  39. int cleanup_before_linux_select(int flags)
  40. {
  41. return 0;
  42. }
  43. void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
  44. {
  45. #ifdef CONFIG_PCI
  46. unsigned long plen = len;
  47. void *ptr;
  48. map_dev = NULL;
  49. if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) {
  50. if (plen != len) {
  51. printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n",
  52. __func__, paddr, len, plen);
  53. }
  54. map_len = len;
  55. return ptr;
  56. }
  57. #endif
  58. return (void *)(gd->arch.ram_buf + paddr);
  59. }
  60. void unmap_physmem(const void *vaddr, unsigned long flags)
  61. {
  62. #ifdef CONFIG_PCI
  63. if (map_dev) {
  64. pci_unmap_physmem(vaddr, map_len, map_dev);
  65. map_dev = NULL;
  66. }
  67. #endif
  68. }
  69. void sandbox_set_enable_pci_map(int enable)
  70. {
  71. enable_pci_map = enable;
  72. }
  73. phys_addr_t map_to_sysmem(const void *ptr)
  74. {
  75. return (u8 *)ptr - gd->arch.ram_buf;
  76. }
  77. void flush_dcache_range(unsigned long start, unsigned long stop)
  78. {
  79. }
  80. int sandbox_read_fdt_from_file(void)
  81. {
  82. struct sandbox_state *state = state_get_current();
  83. const char *fname = state->fdt_fname;
  84. void *blob;
  85. loff_t size;
  86. int err;
  87. int fd;
  88. blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0);
  89. if (!state->fdt_fname) {
  90. err = fdt_create_empty_tree(blob, 256);
  91. if (!err)
  92. goto done;
  93. printf("Unable to create empty FDT: %s\n", fdt_strerror(err));
  94. return -EINVAL;
  95. }
  96. err = os_get_filesize(fname, &size);
  97. if (err < 0) {
  98. printf("Failed to file FDT file '%s'\n", fname);
  99. return err;
  100. }
  101. fd = os_open(fname, OS_O_RDONLY);
  102. if (fd < 0) {
  103. printf("Failed to open FDT file '%s'\n", fname);
  104. return -EACCES;
  105. }
  106. if (os_read(fd, blob, size) != size) {
  107. os_close(fd);
  108. return -EIO;
  109. }
  110. os_close(fd);
  111. done:
  112. gd->fdt_blob = blob;
  113. return 0;
  114. }