bananapi_m2u.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Bananapi M2U emulation
  3. *
  4. * Copyright (C) 2023 qianfan Zhao <qianfanguijin@163.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/units.h"
  21. #include "exec/address-spaces.h"
  22. #include "qapi/error.h"
  23. #include "qemu/error-report.h"
  24. #include "hw/boards.h"
  25. #include "hw/i2c/i2c.h"
  26. #include "hw/qdev-properties.h"
  27. #include "hw/arm/allwinner-r40.h"
  28. #include "hw/arm/boot.h"
  29. static struct arm_boot_info bpim2u_binfo;
  30. /*
  31. * R40 can boot from mmc0 and mmc2, and bpim2u has two mmc interface, one is
  32. * connected to sdcard and another mount an emmc media.
  33. * Attach the mmc driver and try loading bootloader.
  34. */
  35. static void mmc_attach_drive(AwR40State *s, AwSdHostState *mmc, int unit,
  36. bool load_bootroom, bool *bootroom_loaded)
  37. {
  38. DriveInfo *di = drive_get(IF_SD, 0, unit);
  39. BlockBackend *blk = di ? blk_by_legacy_dinfo(di) : NULL;
  40. BusState *bus;
  41. DeviceState *carddev;
  42. bus = qdev_get_child_bus(DEVICE(mmc), "sd-bus");
  43. if (bus == NULL) {
  44. error_report("No SD bus found in SOC object");
  45. exit(1);
  46. }
  47. carddev = qdev_new(TYPE_SD_CARD);
  48. qdev_prop_set_drive_err(carddev, "drive", blk, &error_fatal);
  49. qdev_realize_and_unref(carddev, bus, &error_fatal);
  50. if (load_bootroom && blk && blk_is_available(blk)) {
  51. /* Use Boot ROM to copy data from SD card to SRAM */
  52. *bootroom_loaded = allwinner_r40_bootrom_setup(s, blk, unit);
  53. }
  54. }
  55. static void bpim2u_init(MachineState *machine)
  56. {
  57. bool bootroom_loaded = false;
  58. AwR40State *r40;
  59. I2CBus *i2c;
  60. /* BIOS is not supported by this board */
  61. if (machine->firmware) {
  62. error_report("BIOS not supported for this machine");
  63. exit(1);
  64. }
  65. r40 = AW_R40(object_new(TYPE_AW_R40));
  66. object_property_add_child(OBJECT(machine), "soc", OBJECT(r40));
  67. object_unref(OBJECT(r40));
  68. /* Setup timer properties */
  69. object_property_set_int(OBJECT(r40), "clk0-freq", 32768, &error_abort);
  70. object_property_set_int(OBJECT(r40), "clk1-freq", 24 * 1000 * 1000,
  71. &error_abort);
  72. /* DRAMC */
  73. r40->ram_size = machine->ram_size / MiB;
  74. object_property_set_uint(OBJECT(r40), "ram-addr",
  75. r40->memmap[AW_R40_DEV_SDRAM], &error_abort);
  76. object_property_set_int(OBJECT(r40), "ram-size",
  77. r40->ram_size, &error_abort);
  78. /* GMAC PHY */
  79. object_property_set_uint(OBJECT(r40), "gmac-phy-addr", 1, &error_abort);
  80. /* Mark R40 object realized */
  81. qdev_realize(DEVICE(r40), NULL, &error_abort);
  82. /*
  83. * Plug in SD card and try load bootrom, R40 has 4 mmc controllers but can
  84. * only booting from mmc0 and mmc2.
  85. */
  86. for (int i = 0; i < AW_R40_NUM_MMCS; i++) {
  87. switch (i) {
  88. case 0:
  89. case 2:
  90. mmc_attach_drive(r40, &r40->mmc[i], i,
  91. !machine->kernel_filename && !bootroom_loaded,
  92. &bootroom_loaded);
  93. break;
  94. default:
  95. mmc_attach_drive(r40, &r40->mmc[i], i, false, NULL);
  96. break;
  97. }
  98. }
  99. /* Connect AXP221 */
  100. i2c = I2C_BUS(qdev_get_child_bus(DEVICE(&r40->i2c0), "i2c"));
  101. i2c_slave_create_simple(i2c, "axp221_pmu", 0x34);
  102. /* SDRAM */
  103. memory_region_add_subregion(get_system_memory(),
  104. r40->memmap[AW_R40_DEV_SDRAM], machine->ram);
  105. bpim2u_binfo.loader_start = r40->memmap[AW_R40_DEV_SDRAM];
  106. bpim2u_binfo.ram_size = machine->ram_size;
  107. bpim2u_binfo.psci_conduit = QEMU_PSCI_CONDUIT_SMC;
  108. arm_load_kernel(&r40->cpus[0], machine, &bpim2u_binfo);
  109. }
  110. static void bpim2u_machine_init(MachineClass *mc)
  111. {
  112. static const char * const valid_cpu_types[] = {
  113. ARM_CPU_TYPE_NAME("cortex-a7"),
  114. NULL
  115. };
  116. mc->desc = "Bananapi M2U (Cortex-A7)";
  117. mc->init = bpim2u_init;
  118. mc->min_cpus = AW_R40_NUM_CPUS;
  119. mc->max_cpus = AW_R40_NUM_CPUS;
  120. mc->default_cpus = AW_R40_NUM_CPUS;
  121. mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a7");
  122. mc->valid_cpu_types = valid_cpu_types;
  123. mc->default_ram_size = 1 * GiB;
  124. mc->default_ram_id = "bpim2u.ram";
  125. }
  126. DEFINE_MACHINE("bpim2u", bpim2u_machine_init)