npcm8xx_boards.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Machine definitions for boards featuring an NPCM8xx SoC.
  3. *
  4. * Copyright 2021 Google LLC
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * 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, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * for more details.
  15. */
  16. #include "qemu/osdep.h"
  17. #include "chardev/char.h"
  18. #include "hw/boards.h"
  19. #include "hw/arm/npcm8xx.h"
  20. #include "hw/core/cpu.h"
  21. #include "hw/loader.h"
  22. #include "hw/qdev-core.h"
  23. #include "hw/qdev-properties.h"
  24. #include "qapi/error.h"
  25. #include "qemu/error-report.h"
  26. #include "qemu/datadir.h"
  27. #include "qemu/units.h"
  28. #define NPCM845_EVB_POWER_ON_STRAPS 0x000017ff
  29. static const char npcm8xx_default_bootrom[] = "npcm8xx_bootrom.bin";
  30. static void npcm8xx_load_bootrom(MachineState *machine, NPCM8xxState *soc)
  31. {
  32. const char *bios_name = machine->firmware ?: npcm8xx_default_bootrom;
  33. g_autofree char *filename = NULL;
  34. int ret;
  35. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  36. if (!filename) {
  37. error_report("Could not find ROM image '%s'", bios_name);
  38. if (!machine->kernel_filename) {
  39. /* We can't boot without a bootrom or a kernel image. */
  40. exit(1);
  41. }
  42. return;
  43. }
  44. ret = load_image_mr(filename, machine->ram);
  45. if (ret < 0) {
  46. error_report("Failed to load ROM image '%s'", filename);
  47. exit(1);
  48. }
  49. }
  50. static void npcm8xx_connect_flash(NPCM7xxFIUState *fiu, int cs_no,
  51. const char *flash_type, DriveInfo *dinfo)
  52. {
  53. DeviceState *flash;
  54. qemu_irq flash_cs;
  55. flash = qdev_new(flash_type);
  56. if (dinfo) {
  57. qdev_prop_set_drive(flash, "drive", blk_by_legacy_dinfo(dinfo));
  58. }
  59. qdev_realize_and_unref(flash, BUS(fiu->spi), &error_fatal);
  60. flash_cs = qdev_get_gpio_in_named(flash, SSI_GPIO_CS, 0);
  61. qdev_connect_gpio_out_named(DEVICE(fiu), "cs", cs_no, flash_cs);
  62. }
  63. static void npcm8xx_connect_dram(NPCM8xxState *soc, MemoryRegion *dram)
  64. {
  65. memory_region_add_subregion(get_system_memory(), NPCM8XX_DRAM_BA, dram);
  66. object_property_set_link(OBJECT(soc), "dram-mr", OBJECT(dram),
  67. &error_abort);
  68. }
  69. static NPCM8xxState *npcm8xx_create_soc(MachineState *machine,
  70. uint32_t hw_straps)
  71. {
  72. NPCM8xxMachineClass *nmc = NPCM8XX_MACHINE_GET_CLASS(machine);
  73. Object *obj;
  74. obj = object_new_with_props(nmc->soc_type, OBJECT(machine), "soc",
  75. &error_abort, NULL);
  76. object_property_set_uint(obj, "power-on-straps", hw_straps, &error_abort);
  77. return NPCM8XX(obj);
  78. }
  79. static I2CBus *npcm8xx_i2c_get_bus(NPCM8xxState *soc, uint32_t num)
  80. {
  81. g_assert(num < ARRAY_SIZE(soc->smbus));
  82. return I2C_BUS(qdev_get_child_bus(DEVICE(&soc->smbus[num]), "i2c-bus"));
  83. }
  84. static void npcm8xx_init_pwm_splitter(NPCM8xxMachine *machine,
  85. NPCM8xxState *soc, const int *fan_counts)
  86. {
  87. SplitIRQ *splitters = machine->fan_splitter;
  88. /*
  89. * PWM 0~3 belong to module 0 output 0~3.
  90. * PWM 4~7 belong to module 1 output 0~3.
  91. */
  92. for (int i = 0; i < NPCM8XX_NR_PWM_MODULES; ++i) {
  93. for (int j = 0; j < NPCM7XX_PWM_PER_MODULE; ++j) {
  94. int splitter_no = i * NPCM7XX_PWM_PER_MODULE + j;
  95. DeviceState *splitter;
  96. if (fan_counts[splitter_no] < 1) {
  97. continue;
  98. }
  99. object_initialize_child(OBJECT(machine), "fan-splitter[*]",
  100. &splitters[splitter_no], TYPE_SPLIT_IRQ);
  101. splitter = DEVICE(&splitters[splitter_no]);
  102. qdev_prop_set_uint16(splitter, "num-lines",
  103. fan_counts[splitter_no]);
  104. qdev_realize(splitter, NULL, &error_abort);
  105. qdev_connect_gpio_out_named(DEVICE(&soc->pwm[i]), "duty-gpio-out",
  106. j, qdev_get_gpio_in(splitter, 0));
  107. }
  108. }
  109. }
  110. static void npcm8xx_connect_pwm_fan(NPCM8xxState *soc, SplitIRQ *splitter,
  111. int fan_no, int output_no)
  112. {
  113. DeviceState *fan;
  114. int fan_input;
  115. qemu_irq fan_duty_gpio;
  116. g_assert(fan_no >= 0 && fan_no <= NPCM7XX_MFT_MAX_FAN_INPUT);
  117. /*
  118. * Fan 0~1 belong to module 0 input 0~1.
  119. * Fan 2~3 belong to module 1 input 0~1.
  120. * ...
  121. * Fan 14~15 belong to module 7 input 0~1.
  122. * Fan 16~17 belong to module 0 input 2~3.
  123. * Fan 18~19 belong to module 1 input 2~3.
  124. */
  125. if (fan_no < 16) {
  126. fan = DEVICE(&soc->mft[fan_no / 2]);
  127. fan_input = fan_no % 2;
  128. } else {
  129. fan = DEVICE(&soc->mft[(fan_no - 16) / 2]);
  130. fan_input = fan_no % 2 + 2;
  131. }
  132. /* Connect the Fan to PWM module */
  133. fan_duty_gpio = qdev_get_gpio_in_named(fan, "duty", fan_input);
  134. qdev_connect_gpio_out(DEVICE(splitter), output_no, fan_duty_gpio);
  135. }
  136. static void npcm845_evb_i2c_init(NPCM8xxState *soc)
  137. {
  138. /* tmp100 temperature sensor on SVB, tmp105 is compatible */
  139. i2c_slave_create_simple(npcm8xx_i2c_get_bus(soc, 6), "tmp105", 0x48);
  140. }
  141. static void npcm845_evb_fan_init(NPCM8xxMachine *machine, NPCM8xxState *soc)
  142. {
  143. SplitIRQ *splitter = machine->fan_splitter;
  144. static const int fan_counts[] = {2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0};
  145. npcm8xx_init_pwm_splitter(machine, soc, fan_counts);
  146. npcm8xx_connect_pwm_fan(soc, &splitter[0], 0x00, 0);
  147. npcm8xx_connect_pwm_fan(soc, &splitter[0], 0x01, 1);
  148. npcm8xx_connect_pwm_fan(soc, &splitter[1], 0x02, 0);
  149. npcm8xx_connect_pwm_fan(soc, &splitter[1], 0x03, 1);
  150. npcm8xx_connect_pwm_fan(soc, &splitter[2], 0x04, 0);
  151. npcm8xx_connect_pwm_fan(soc, &splitter[2], 0x05, 1);
  152. npcm8xx_connect_pwm_fan(soc, &splitter[3], 0x06, 0);
  153. npcm8xx_connect_pwm_fan(soc, &splitter[3], 0x07, 1);
  154. npcm8xx_connect_pwm_fan(soc, &splitter[4], 0x08, 0);
  155. npcm8xx_connect_pwm_fan(soc, &splitter[4], 0x09, 1);
  156. npcm8xx_connect_pwm_fan(soc, &splitter[5], 0x0a, 0);
  157. npcm8xx_connect_pwm_fan(soc, &splitter[5], 0x0b, 1);
  158. npcm8xx_connect_pwm_fan(soc, &splitter[6], 0x0c, 0);
  159. npcm8xx_connect_pwm_fan(soc, &splitter[6], 0x0d, 1);
  160. npcm8xx_connect_pwm_fan(soc, &splitter[7], 0x0e, 0);
  161. npcm8xx_connect_pwm_fan(soc, &splitter[7], 0x0f, 1);
  162. }
  163. static void npcm845_evb_init(MachineState *machine)
  164. {
  165. NPCM8xxState *soc;
  166. soc = npcm8xx_create_soc(machine, NPCM845_EVB_POWER_ON_STRAPS);
  167. npcm8xx_connect_dram(soc, machine->ram);
  168. qdev_realize(DEVICE(soc), NULL, &error_fatal);
  169. npcm8xx_load_bootrom(machine, soc);
  170. npcm8xx_connect_flash(&soc->fiu[0], 0, "w25q256", drive_get(IF_MTD, 0, 0));
  171. npcm845_evb_i2c_init(soc);
  172. npcm845_evb_fan_init(NPCM8XX_MACHINE(machine), soc);
  173. npcm8xx_load_kernel(machine, soc);
  174. }
  175. static void npcm8xx_set_soc_type(NPCM8xxMachineClass *nmc, const char *type)
  176. {
  177. NPCM8xxClass *sc = NPCM8XX_CLASS(object_class_by_name(type));
  178. MachineClass *mc = MACHINE_CLASS(nmc);
  179. nmc->soc_type = type;
  180. mc->default_cpus = mc->min_cpus = mc->max_cpus = sc->num_cpus;
  181. }
  182. static void npcm8xx_machine_class_init(ObjectClass *oc, void *data)
  183. {
  184. MachineClass *mc = MACHINE_CLASS(oc);
  185. static const char * const valid_cpu_types[] = {
  186. ARM_CPU_TYPE_NAME("cortex-a35"),
  187. NULL
  188. };
  189. mc->no_floppy = 1;
  190. mc->no_cdrom = 1;
  191. mc->no_parallel = 1;
  192. mc->default_ram_id = "ram";
  193. mc->valid_cpu_types = valid_cpu_types;
  194. }
  195. static void npcm845_evb_machine_class_init(ObjectClass *oc, void *data)
  196. {
  197. NPCM8xxMachineClass *nmc = NPCM8XX_MACHINE_CLASS(oc);
  198. MachineClass *mc = MACHINE_CLASS(oc);
  199. npcm8xx_set_soc_type(nmc, TYPE_NPCM8XX);
  200. mc->desc = "Nuvoton NPCM845 Evaluation Board (Cortex-A35)";
  201. mc->init = npcm845_evb_init;
  202. mc->default_ram_size = 1 * GiB;
  203. };
  204. static const TypeInfo npcm8xx_machine_types[] = {
  205. {
  206. .name = TYPE_NPCM8XX_MACHINE,
  207. .parent = TYPE_MACHINE,
  208. .instance_size = sizeof(NPCM8xxMachine),
  209. .class_size = sizeof(NPCM8xxMachineClass),
  210. .class_init = npcm8xx_machine_class_init,
  211. .abstract = true,
  212. }, {
  213. .name = MACHINE_TYPE_NAME("npcm845-evb"),
  214. .parent = TYPE_NPCM8XX_MACHINE,
  215. .class_init = npcm845_evb_machine_class_init,
  216. },
  217. };
  218. DEFINE_TYPES(npcm8xx_machine_types)