r2d.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Renesas SH7751R R2D-PLUS emulation
  3. *
  4. * Copyright (c) 2007 Magnus Damm
  5. * Copyright (c) 2008 Paul Mundt
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "sysbus.h"
  26. #include "hw.h"
  27. #include "sh.h"
  28. #include "devices.h"
  29. #include "sysemu.h"
  30. #include "boards.h"
  31. #include "pci.h"
  32. #include "net.h"
  33. #include "sh7750_regs.h"
  34. #include "ide.h"
  35. #include "loader.h"
  36. #include "usb.h"
  37. #include "flash.h"
  38. #include "blockdev.h"
  39. #include "exec-memory.h"
  40. #define FLASH_BASE 0x00000000
  41. #define FLASH_SIZE 0x02000000
  42. #define SDRAM_BASE 0x0c000000 /* Physical location of SDRAM: Area 3 */
  43. #define SDRAM_SIZE 0x04000000
  44. #define SM501_VRAM_SIZE 0x800000
  45. #define BOOT_PARAMS_OFFSET 0x0010000
  46. /* CONFIG_BOOT_LINK_OFFSET of Linux kernel */
  47. #define LINUX_LOAD_OFFSET 0x0800000
  48. #define INITRD_LOAD_OFFSET 0x1800000
  49. #define PA_IRLMSK 0x00
  50. #define PA_POWOFF 0x30
  51. #define PA_VERREG 0x32
  52. #define PA_OUTPORT 0x36
  53. typedef struct {
  54. uint16_t bcr;
  55. uint16_t irlmsk;
  56. uint16_t irlmon;
  57. uint16_t cfctl;
  58. uint16_t cfpow;
  59. uint16_t dispctl;
  60. uint16_t sdmpow;
  61. uint16_t rtcce;
  62. uint16_t pcicd;
  63. uint16_t voyagerrts;
  64. uint16_t cfrst;
  65. uint16_t admrts;
  66. uint16_t extrst;
  67. uint16_t cfcdintclr;
  68. uint16_t keyctlclr;
  69. uint16_t pad0;
  70. uint16_t pad1;
  71. uint16_t verreg;
  72. uint16_t inport;
  73. uint16_t outport;
  74. uint16_t bverreg;
  75. /* output pin */
  76. qemu_irq irl;
  77. MemoryRegion iomem;
  78. } r2d_fpga_t;
  79. enum r2d_fpga_irq {
  80. PCI_INTD, CF_IDE, CF_CD, PCI_INTC, SM501, KEY, RTC_A, RTC_T,
  81. SDCARD, PCI_INTA, PCI_INTB, EXT, TP,
  82. NR_IRQS
  83. };
  84. static const struct { short irl; uint16_t msk; } irqtab[NR_IRQS] = {
  85. [CF_IDE] = { 1, 1<<9 },
  86. [CF_CD] = { 2, 1<<8 },
  87. [PCI_INTA] = { 9, 1<<14 },
  88. [PCI_INTB] = { 10, 1<<13 },
  89. [PCI_INTC] = { 3, 1<<12 },
  90. [PCI_INTD] = { 0, 1<<11 },
  91. [SM501] = { 4, 1<<10 },
  92. [KEY] = { 5, 1<<6 },
  93. [RTC_A] = { 6, 1<<5 },
  94. [RTC_T] = { 7, 1<<4 },
  95. [SDCARD] = { 8, 1<<7 },
  96. [EXT] = { 11, 1<<0 },
  97. [TP] = { 12, 1<<15 },
  98. };
  99. static void update_irl(r2d_fpga_t *fpga)
  100. {
  101. int i, irl = 15;
  102. for (i = 0; i < NR_IRQS; i++)
  103. if (fpga->irlmon & fpga->irlmsk & irqtab[i].msk)
  104. if (irqtab[i].irl < irl)
  105. irl = irqtab[i].irl;
  106. qemu_set_irq(fpga->irl, irl ^ 15);
  107. }
  108. static void r2d_fpga_irq_set(void *opaque, int n, int level)
  109. {
  110. r2d_fpga_t *fpga = opaque;
  111. if (level)
  112. fpga->irlmon |= irqtab[n].msk;
  113. else
  114. fpga->irlmon &= ~irqtab[n].msk;
  115. update_irl(fpga);
  116. }
  117. static uint32_t r2d_fpga_read(void *opaque, target_phys_addr_t addr)
  118. {
  119. r2d_fpga_t *s = opaque;
  120. switch (addr) {
  121. case PA_IRLMSK:
  122. return s->irlmsk;
  123. case PA_OUTPORT:
  124. return s->outport;
  125. case PA_POWOFF:
  126. return 0x00;
  127. case PA_VERREG:
  128. return 0x10;
  129. }
  130. return 0;
  131. }
  132. static void
  133. r2d_fpga_write(void *opaque, target_phys_addr_t addr, uint32_t value)
  134. {
  135. r2d_fpga_t *s = opaque;
  136. switch (addr) {
  137. case PA_IRLMSK:
  138. s->irlmsk = value;
  139. update_irl(s);
  140. break;
  141. case PA_OUTPORT:
  142. s->outport = value;
  143. break;
  144. case PA_POWOFF:
  145. if (value & 1) {
  146. qemu_system_shutdown_request();
  147. }
  148. break;
  149. case PA_VERREG:
  150. /* Discard writes */
  151. break;
  152. }
  153. }
  154. static const MemoryRegionOps r2d_fpga_ops = {
  155. .old_mmio = {
  156. .read = { r2d_fpga_read, r2d_fpga_read, NULL, },
  157. .write = { r2d_fpga_write, r2d_fpga_write, NULL, },
  158. },
  159. .endianness = DEVICE_NATIVE_ENDIAN,
  160. };
  161. static qemu_irq *r2d_fpga_init(MemoryRegion *sysmem,
  162. target_phys_addr_t base, qemu_irq irl)
  163. {
  164. r2d_fpga_t *s;
  165. s = g_malloc0(sizeof(r2d_fpga_t));
  166. s->irl = irl;
  167. memory_region_init_io(&s->iomem, &r2d_fpga_ops, s, "r2d-fpga", 0x40);
  168. memory_region_add_subregion(sysmem, base, &s->iomem);
  169. return qemu_allocate_irqs(r2d_fpga_irq_set, s, NR_IRQS);
  170. }
  171. typedef struct ResetData {
  172. CPUState *env;
  173. uint32_t vector;
  174. } ResetData;
  175. static void main_cpu_reset(void *opaque)
  176. {
  177. ResetData *s = (ResetData *)opaque;
  178. CPUState *env = s->env;
  179. cpu_reset(env);
  180. env->pc = s->vector;
  181. }
  182. static struct QEMU_PACKED
  183. {
  184. int mount_root_rdonly;
  185. int ramdisk_flags;
  186. int orig_root_dev;
  187. int loader_type;
  188. int initrd_start;
  189. int initrd_size;
  190. char pad[232];
  191. char kernel_cmdline[256];
  192. } boot_params;
  193. static void r2d_init(ram_addr_t ram_size,
  194. const char *boot_device,
  195. const char *kernel_filename, const char *kernel_cmdline,
  196. const char *initrd_filename, const char *cpu_model)
  197. {
  198. CPUState *env;
  199. ResetData *reset_info;
  200. struct SH7750State *s;
  201. MemoryRegion *sdram = g_new(MemoryRegion, 1);
  202. qemu_irq *irq;
  203. DriveInfo *dinfo;
  204. int i;
  205. MemoryRegion *address_space_mem = get_system_memory();
  206. if (!cpu_model)
  207. cpu_model = "SH7751R";
  208. env = cpu_init(cpu_model);
  209. if (!env) {
  210. fprintf(stderr, "Unable to find CPU definition\n");
  211. exit(1);
  212. }
  213. reset_info = g_malloc0(sizeof(ResetData));
  214. reset_info->env = env;
  215. reset_info->vector = env->pc;
  216. qemu_register_reset(main_cpu_reset, reset_info);
  217. /* Allocate memory space */
  218. memory_region_init_ram(sdram, NULL, "r2d.sdram", SDRAM_SIZE);
  219. memory_region_add_subregion(address_space_mem, SDRAM_BASE, sdram);
  220. /* Register peripherals */
  221. s = sh7750_init(env);
  222. irq = r2d_fpga_init(address_space_mem, 0x04000000, sh7750_irl(s));
  223. sysbus_create_varargs("sh_pci", 0x1e200000, irq[PCI_INTA], irq[PCI_INTB],
  224. irq[PCI_INTC], irq[PCI_INTD], NULL);
  225. sm501_init(address_space_mem, 0x10000000, SM501_VRAM_SIZE,
  226. irq[SM501], serial_hds[2]);
  227. /* onboard CF (True IDE mode, Master only). */
  228. dinfo = drive_get(IF_IDE, 0, 0);
  229. mmio_ide_init(0x14001000, 0x1400080c, irq[CF_IDE], 1,
  230. dinfo, NULL);
  231. /* onboard flash memory */
  232. dinfo = drive_get(IF_PFLASH, 0, 0);
  233. pflash_cfi02_register(0x0, NULL, "r2d.flash", FLASH_SIZE,
  234. dinfo ? dinfo->bdrv : NULL, (16 * 1024),
  235. FLASH_SIZE >> 16,
  236. 1, 4, 0x0000, 0x0000, 0x0000, 0x0000,
  237. 0x555, 0x2aa, 0);
  238. /* NIC: rtl8139 on-board, and 2 slots. */
  239. for (i = 0; i < nb_nics; i++)
  240. pci_nic_init_nofail(&nd_table[i], "rtl8139", i==0 ? "2" : NULL);
  241. /* USB keyboard */
  242. usbdevice_create("keyboard");
  243. /* Todo: register on board registers */
  244. memset(&boot_params, 0, sizeof(boot_params));
  245. if (kernel_filename) {
  246. int kernel_size;
  247. kernel_size = load_image_targphys(kernel_filename,
  248. SDRAM_BASE + LINUX_LOAD_OFFSET,
  249. INITRD_LOAD_OFFSET - LINUX_LOAD_OFFSET);
  250. if (kernel_size < 0) {
  251. fprintf(stderr, "qemu: could not load kernel '%s'\n", kernel_filename);
  252. exit(1);
  253. }
  254. /* initialization which should be done by firmware */
  255. stl_phys(SH7750_BCR1, 1<<3); /* cs3 SDRAM */
  256. stw_phys(SH7750_BCR2, 3<<(3*2)); /* cs3 32bit */
  257. reset_info->vector = (SDRAM_BASE + LINUX_LOAD_OFFSET) | 0xa0000000; /* Start from P2 area */
  258. }
  259. if (initrd_filename) {
  260. int initrd_size;
  261. initrd_size = load_image_targphys(initrd_filename,
  262. SDRAM_BASE + INITRD_LOAD_OFFSET,
  263. SDRAM_SIZE - INITRD_LOAD_OFFSET);
  264. if (initrd_size < 0) {
  265. fprintf(stderr, "qemu: could not load initrd '%s'\n", initrd_filename);
  266. exit(1);
  267. }
  268. /* initialization which should be done by firmware */
  269. boot_params.loader_type = 1;
  270. boot_params.initrd_start = INITRD_LOAD_OFFSET;
  271. boot_params.initrd_size = initrd_size;
  272. }
  273. if (kernel_cmdline) {
  274. strncpy(boot_params.kernel_cmdline, kernel_cmdline,
  275. sizeof(boot_params.kernel_cmdline));
  276. }
  277. rom_add_blob_fixed("boot_params", &boot_params, sizeof(boot_params),
  278. SDRAM_BASE + BOOT_PARAMS_OFFSET);
  279. }
  280. static QEMUMachine r2d_machine = {
  281. .name = "r2d",
  282. .desc = "r2d-plus board",
  283. .init = r2d_init,
  284. };
  285. static void r2d_machine_init(void)
  286. {
  287. qemu_register_machine(&r2d_machine);
  288. }
  289. machine_init(r2d_machine_init);