2
0

r2d.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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/sysemu.h"
  30. #include "boards.h"
  31. #include "pci/pci.h"
  32. #include "net/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 "sysemu/blockdev.h"
  39. #include "exec/address-spaces.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, hwaddr 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, hwaddr 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. hwaddr 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. SuperHCPU *cpu;
  173. uint32_t vector;
  174. } ResetData;
  175. static void main_cpu_reset(void *opaque)
  176. {
  177. ResetData *s = (ResetData *)opaque;
  178. CPUSH4State *env = &s->cpu->env;
  179. cpu_reset(CPU(s->cpu));
  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(QEMUMachineInitArgs *args)
  194. {
  195. const char *cpu_model = args->cpu_model;
  196. const char *kernel_filename = args->kernel_filename;
  197. const char *kernel_cmdline = args->kernel_cmdline;
  198. const char *initrd_filename = args->initrd_filename;
  199. SuperHCPU *cpu;
  200. CPUSH4State *env;
  201. ResetData *reset_info;
  202. struct SH7750State *s;
  203. MemoryRegion *sdram = g_new(MemoryRegion, 1);
  204. qemu_irq *irq;
  205. DriveInfo *dinfo;
  206. int i;
  207. DeviceState *dev;
  208. SysBusDevice *busdev;
  209. MemoryRegion *address_space_mem = get_system_memory();
  210. if (cpu_model == NULL) {
  211. cpu_model = "SH7751R";
  212. }
  213. cpu = cpu_sh4_init(cpu_model);
  214. if (cpu == NULL) {
  215. fprintf(stderr, "Unable to find CPU definition\n");
  216. exit(1);
  217. }
  218. env = &cpu->env;
  219. reset_info = g_malloc0(sizeof(ResetData));
  220. reset_info->cpu = cpu;
  221. reset_info->vector = env->pc;
  222. qemu_register_reset(main_cpu_reset, reset_info);
  223. /* Allocate memory space */
  224. memory_region_init_ram(sdram, "r2d.sdram", SDRAM_SIZE);
  225. vmstate_register_ram_global(sdram);
  226. memory_region_add_subregion(address_space_mem, SDRAM_BASE, sdram);
  227. /* Register peripherals */
  228. s = sh7750_init(env, address_space_mem);
  229. irq = r2d_fpga_init(address_space_mem, 0x04000000, sh7750_irl(s));
  230. dev = qdev_create(NULL, "sh_pci");
  231. busdev = SYS_BUS_DEVICE(dev);
  232. qdev_init_nofail(dev);
  233. sysbus_mmio_map(busdev, 0, P4ADDR(0x1e200000));
  234. sysbus_mmio_map(busdev, 1, A7ADDR(0x1e200000));
  235. sysbus_connect_irq(busdev, 0, irq[PCI_INTA]);
  236. sysbus_connect_irq(busdev, 1, irq[PCI_INTB]);
  237. sysbus_connect_irq(busdev, 2, irq[PCI_INTC]);
  238. sysbus_connect_irq(busdev, 3, irq[PCI_INTD]);
  239. sm501_init(address_space_mem, 0x10000000, SM501_VRAM_SIZE,
  240. irq[SM501], serial_hds[2]);
  241. /* onboard CF (True IDE mode, Master only). */
  242. dinfo = drive_get(IF_IDE, 0, 0);
  243. dev = qdev_create(NULL, "mmio-ide");
  244. busdev = SYS_BUS_DEVICE(dev);
  245. sysbus_connect_irq(busdev, 0, irq[CF_IDE]);
  246. qdev_prop_set_uint32(dev, "shift", 1);
  247. qdev_init_nofail(dev);
  248. sysbus_mmio_map(busdev, 0, 0x14001000);
  249. sysbus_mmio_map(busdev, 1, 0x1400080c);
  250. mmio_ide_init_drives(dev, dinfo, NULL);
  251. /* onboard flash memory */
  252. dinfo = drive_get(IF_PFLASH, 0, 0);
  253. pflash_cfi02_register(0x0, NULL, "r2d.flash", FLASH_SIZE,
  254. dinfo ? dinfo->bdrv : NULL, (16 * 1024),
  255. FLASH_SIZE >> 16,
  256. 1, 4, 0x0000, 0x0000, 0x0000, 0x0000,
  257. 0x555, 0x2aa, 0);
  258. /* NIC: rtl8139 on-board, and 2 slots. */
  259. for (i = 0; i < nb_nics; i++)
  260. pci_nic_init_nofail(&nd_table[i], "rtl8139", i==0 ? "2" : NULL);
  261. /* USB keyboard */
  262. usbdevice_create("keyboard");
  263. /* Todo: register on board registers */
  264. memset(&boot_params, 0, sizeof(boot_params));
  265. if (kernel_filename) {
  266. int kernel_size;
  267. kernel_size = load_image_targphys(kernel_filename,
  268. SDRAM_BASE + LINUX_LOAD_OFFSET,
  269. INITRD_LOAD_OFFSET - LINUX_LOAD_OFFSET);
  270. if (kernel_size < 0) {
  271. fprintf(stderr, "qemu: could not load kernel '%s'\n", kernel_filename);
  272. exit(1);
  273. }
  274. /* initialization which should be done by firmware */
  275. stl_phys(SH7750_BCR1, 1<<3); /* cs3 SDRAM */
  276. stw_phys(SH7750_BCR2, 3<<(3*2)); /* cs3 32bit */
  277. reset_info->vector = (SDRAM_BASE + LINUX_LOAD_OFFSET) | 0xa0000000; /* Start from P2 area */
  278. }
  279. if (initrd_filename) {
  280. int initrd_size;
  281. initrd_size = load_image_targphys(initrd_filename,
  282. SDRAM_BASE + INITRD_LOAD_OFFSET,
  283. SDRAM_SIZE - INITRD_LOAD_OFFSET);
  284. if (initrd_size < 0) {
  285. fprintf(stderr, "qemu: could not load initrd '%s'\n", initrd_filename);
  286. exit(1);
  287. }
  288. /* initialization which should be done by firmware */
  289. boot_params.loader_type = 1;
  290. boot_params.initrd_start = INITRD_LOAD_OFFSET;
  291. boot_params.initrd_size = initrd_size;
  292. }
  293. if (kernel_cmdline) {
  294. /* I see no evidence that this .kernel_cmdline buffer requires
  295. NUL-termination, so using strncpy should be ok. */
  296. strncpy(boot_params.kernel_cmdline, kernel_cmdline,
  297. sizeof(boot_params.kernel_cmdline));
  298. }
  299. rom_add_blob_fixed("boot_params", &boot_params, sizeof(boot_params),
  300. SDRAM_BASE + BOOT_PARAMS_OFFSET);
  301. }
  302. static QEMUMachine r2d_machine = {
  303. .name = "r2d",
  304. .desc = "r2d-plus board",
  305. .init = r2d_init,
  306. DEFAULT_MACHINE_OPTIONS,
  307. };
  308. static void r2d_machine_init(void)
  309. {
  310. qemu_register_machine(&r2d_machine);
  311. }
  312. machine_init(r2d_machine_init);