r2d.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 "qemu/osdep.h"
  26. #include "qemu/units.h"
  27. #include "qapi/error.h"
  28. #include "cpu.h"
  29. #include "hw/sysbus.h"
  30. #include "hw/sh4/sh.h"
  31. #include "sysemu/reset.h"
  32. #include "sysemu/runstate.h"
  33. #include "sysemu/sysemu.h"
  34. #include "hw/boards.h"
  35. #include "hw/pci/pci.h"
  36. #include "hw/qdev-properties.h"
  37. #include "net/net.h"
  38. #include "sh7750_regs.h"
  39. #include "hw/ide.h"
  40. #include "hw/irq.h"
  41. #include "hw/loader.h"
  42. #include "hw/usb.h"
  43. #include "hw/block/flash.h"
  44. #include "exec/address-spaces.h"
  45. #define FLASH_BASE 0x00000000
  46. #define FLASH_SIZE (16 * MiB)
  47. #define SDRAM_BASE 0x0c000000 /* Physical location of SDRAM: Area 3 */
  48. #define SDRAM_SIZE 0x04000000
  49. #define SM501_VRAM_SIZE 0x800000
  50. #define BOOT_PARAMS_OFFSET 0x0010000
  51. /* CONFIG_BOOT_LINK_OFFSET of Linux kernel */
  52. #define LINUX_LOAD_OFFSET 0x0800000
  53. #define INITRD_LOAD_OFFSET 0x1800000
  54. #define PA_IRLMSK 0x00
  55. #define PA_POWOFF 0x30
  56. #define PA_VERREG 0x32
  57. #define PA_OUTPORT 0x36
  58. typedef struct {
  59. uint16_t bcr;
  60. uint16_t irlmsk;
  61. uint16_t irlmon;
  62. uint16_t cfctl;
  63. uint16_t cfpow;
  64. uint16_t dispctl;
  65. uint16_t sdmpow;
  66. uint16_t rtcce;
  67. uint16_t pcicd;
  68. uint16_t voyagerrts;
  69. uint16_t cfrst;
  70. uint16_t admrts;
  71. uint16_t extrst;
  72. uint16_t cfcdintclr;
  73. uint16_t keyctlclr;
  74. uint16_t pad0;
  75. uint16_t pad1;
  76. uint16_t verreg;
  77. uint16_t inport;
  78. uint16_t outport;
  79. uint16_t bverreg;
  80. /* output pin */
  81. qemu_irq irl;
  82. MemoryRegion iomem;
  83. } r2d_fpga_t;
  84. enum r2d_fpga_irq {
  85. PCI_INTD, CF_IDE, CF_CD, PCI_INTC, SM501, KEY, RTC_A, RTC_T,
  86. SDCARD, PCI_INTA, PCI_INTB, EXT, TP,
  87. NR_IRQS
  88. };
  89. static const struct { short irl; uint16_t msk; } irqtab[NR_IRQS] = {
  90. [CF_IDE] = { 1, 1<<9 },
  91. [CF_CD] = { 2, 1<<8 },
  92. [PCI_INTA] = { 9, 1<<14 },
  93. [PCI_INTB] = { 10, 1<<13 },
  94. [PCI_INTC] = { 3, 1<<12 },
  95. [PCI_INTD] = { 0, 1<<11 },
  96. [SM501] = { 4, 1<<10 },
  97. [KEY] = { 5, 1<<6 },
  98. [RTC_A] = { 6, 1<<5 },
  99. [RTC_T] = { 7, 1<<4 },
  100. [SDCARD] = { 8, 1<<7 },
  101. [EXT] = { 11, 1<<0 },
  102. [TP] = { 12, 1<<15 },
  103. };
  104. static void update_irl(r2d_fpga_t *fpga)
  105. {
  106. int i, irl = 15;
  107. for (i = 0; i < NR_IRQS; i++)
  108. if (fpga->irlmon & fpga->irlmsk & irqtab[i].msk)
  109. if (irqtab[i].irl < irl)
  110. irl = irqtab[i].irl;
  111. qemu_set_irq(fpga->irl, irl ^ 15);
  112. }
  113. static void r2d_fpga_irq_set(void *opaque, int n, int level)
  114. {
  115. r2d_fpga_t *fpga = opaque;
  116. if (level)
  117. fpga->irlmon |= irqtab[n].msk;
  118. else
  119. fpga->irlmon &= ~irqtab[n].msk;
  120. update_irl(fpga);
  121. }
  122. static uint64_t r2d_fpga_read(void *opaque, hwaddr addr, unsigned int size)
  123. {
  124. r2d_fpga_t *s = opaque;
  125. switch (addr) {
  126. case PA_IRLMSK:
  127. return s->irlmsk;
  128. case PA_OUTPORT:
  129. return s->outport;
  130. case PA_POWOFF:
  131. return 0x00;
  132. case PA_VERREG:
  133. return 0x10;
  134. }
  135. return 0;
  136. }
  137. static void
  138. r2d_fpga_write(void *opaque, hwaddr addr, uint64_t value, unsigned int size)
  139. {
  140. r2d_fpga_t *s = opaque;
  141. switch (addr) {
  142. case PA_IRLMSK:
  143. s->irlmsk = value;
  144. update_irl(s);
  145. break;
  146. case PA_OUTPORT:
  147. s->outport = value;
  148. break;
  149. case PA_POWOFF:
  150. if (value & 1) {
  151. qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
  152. }
  153. break;
  154. case PA_VERREG:
  155. /* Discard writes */
  156. break;
  157. }
  158. }
  159. static const MemoryRegionOps r2d_fpga_ops = {
  160. .read = r2d_fpga_read,
  161. .write = r2d_fpga_write,
  162. .impl.min_access_size = 2,
  163. .impl.max_access_size = 2,
  164. .endianness = DEVICE_NATIVE_ENDIAN,
  165. };
  166. static qemu_irq *r2d_fpga_init(MemoryRegion *sysmem,
  167. hwaddr base, qemu_irq irl)
  168. {
  169. r2d_fpga_t *s;
  170. s = g_malloc0(sizeof(r2d_fpga_t));
  171. s->irl = irl;
  172. memory_region_init_io(&s->iomem, NULL, &r2d_fpga_ops, s, "r2d-fpga", 0x40);
  173. memory_region_add_subregion(sysmem, base, &s->iomem);
  174. return qemu_allocate_irqs(r2d_fpga_irq_set, s, NR_IRQS);
  175. }
  176. typedef struct ResetData {
  177. SuperHCPU *cpu;
  178. uint32_t vector;
  179. } ResetData;
  180. static void main_cpu_reset(void *opaque)
  181. {
  182. ResetData *s = (ResetData *)opaque;
  183. CPUSH4State *env = &s->cpu->env;
  184. cpu_reset(CPU(s->cpu));
  185. env->pc = s->vector;
  186. }
  187. static struct QEMU_PACKED
  188. {
  189. int mount_root_rdonly;
  190. int ramdisk_flags;
  191. int orig_root_dev;
  192. int loader_type;
  193. int initrd_start;
  194. int initrd_size;
  195. char pad[232];
  196. char kernel_cmdline[256] QEMU_NONSTRING;
  197. } boot_params;
  198. static void r2d_init(MachineState *machine)
  199. {
  200. const char *kernel_filename = machine->kernel_filename;
  201. const char *kernel_cmdline = machine->kernel_cmdline;
  202. const char *initrd_filename = machine->initrd_filename;
  203. SuperHCPU *cpu;
  204. CPUSH4State *env;
  205. ResetData *reset_info;
  206. struct SH7750State *s;
  207. MemoryRegion *sdram = g_new(MemoryRegion, 1);
  208. qemu_irq *irq;
  209. DriveInfo *dinfo;
  210. int i;
  211. DeviceState *dev;
  212. SysBusDevice *busdev;
  213. MemoryRegion *address_space_mem = get_system_memory();
  214. PCIBus *pci_bus;
  215. cpu = SUPERH_CPU(cpu_create(machine->cpu_type));
  216. env = &cpu->env;
  217. reset_info = g_malloc0(sizeof(ResetData));
  218. reset_info->cpu = cpu;
  219. reset_info->vector = env->pc;
  220. qemu_register_reset(main_cpu_reset, reset_info);
  221. /* Allocate memory space */
  222. memory_region_init_ram(sdram, NULL, "r2d.sdram", SDRAM_SIZE, &error_fatal);
  223. memory_region_add_subregion(address_space_mem, SDRAM_BASE, sdram);
  224. /* Register peripherals */
  225. s = sh7750_init(cpu, address_space_mem);
  226. irq = r2d_fpga_init(address_space_mem, 0x04000000, sh7750_irl(s));
  227. dev = qdev_create(NULL, "sh_pci");
  228. busdev = SYS_BUS_DEVICE(dev);
  229. qdev_init_nofail(dev);
  230. pci_bus = PCI_BUS(qdev_get_child_bus(dev, "pci"));
  231. sysbus_mmio_map(busdev, 0, P4ADDR(0x1e200000));
  232. sysbus_mmio_map(busdev, 1, A7ADDR(0x1e200000));
  233. sysbus_connect_irq(busdev, 0, irq[PCI_INTA]);
  234. sysbus_connect_irq(busdev, 1, irq[PCI_INTB]);
  235. sysbus_connect_irq(busdev, 2, irq[PCI_INTC]);
  236. sysbus_connect_irq(busdev, 3, irq[PCI_INTD]);
  237. dev = qdev_create(NULL, "sysbus-sm501");
  238. busdev = SYS_BUS_DEVICE(dev);
  239. qdev_prop_set_uint32(dev, "vram-size", SM501_VRAM_SIZE);
  240. qdev_prop_set_uint32(dev, "base", 0x10000000);
  241. qdev_prop_set_ptr(dev, "chr-state", serial_hd(2));
  242. qdev_init_nofail(dev);
  243. sysbus_mmio_map(busdev, 0, 0x10000000);
  244. sysbus_mmio_map(busdev, 1, 0x13e00000);
  245. sysbus_connect_irq(busdev, 0, irq[SM501]);
  246. /* onboard CF (True IDE mode, Master only). */
  247. dinfo = drive_get(IF_IDE, 0, 0);
  248. dev = qdev_create(NULL, "mmio-ide");
  249. busdev = SYS_BUS_DEVICE(dev);
  250. sysbus_connect_irq(busdev, 0, irq[CF_IDE]);
  251. qdev_prop_set_uint32(dev, "shift", 1);
  252. qdev_init_nofail(dev);
  253. sysbus_mmio_map(busdev, 0, 0x14001000);
  254. sysbus_mmio_map(busdev, 1, 0x1400080c);
  255. mmio_ide_init_drives(dev, dinfo, NULL);
  256. /*
  257. * Onboard flash memory
  258. * According to the old board user document in Japanese (under
  259. * NDA) what is referred to as FROM (Area0) is connected via a
  260. * 32-bit bus and CS0 to CN8. The docs mention a Cypress
  261. * S29PL127J60TFI130 chipsset. Per the 'S29PL-J 002-00615
  262. * Rev. *E' datasheet, it is a 128Mbit NOR parallel flash
  263. * addressable in words of 16bit.
  264. */
  265. dinfo = drive_get(IF_PFLASH, 0, 0);
  266. pflash_cfi02_register(0x0, "r2d.flash", FLASH_SIZE,
  267. dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
  268. 64 * KiB, 1, 2, 0x0001, 0x227e, 0x2220, 0x2200,
  269. 0x555, 0x2aa, 0);
  270. /* NIC: rtl8139 on-board, and 2 slots. */
  271. for (i = 0; i < nb_nics; i++)
  272. pci_nic_init_nofail(&nd_table[i], pci_bus,
  273. "rtl8139", i==0 ? "2" : NULL);
  274. /* USB keyboard */
  275. usb_create_simple(usb_bus_find(-1), "usb-kbd");
  276. /* Todo: register on board registers */
  277. memset(&boot_params, 0, sizeof(boot_params));
  278. if (kernel_filename) {
  279. int kernel_size;
  280. kernel_size = load_image_targphys(kernel_filename,
  281. SDRAM_BASE + LINUX_LOAD_OFFSET,
  282. INITRD_LOAD_OFFSET - LINUX_LOAD_OFFSET);
  283. if (kernel_size < 0) {
  284. fprintf(stderr, "qemu: could not load kernel '%s'\n", kernel_filename);
  285. exit(1);
  286. }
  287. /* initialization which should be done by firmware */
  288. address_space_stl(&address_space_memory, SH7750_BCR1, 1 << 3,
  289. MEMTXATTRS_UNSPECIFIED, NULL); /* cs3 SDRAM */
  290. address_space_stw(&address_space_memory, SH7750_BCR2, 3 << (3 * 2),
  291. MEMTXATTRS_UNSPECIFIED, NULL); /* cs3 32bit */
  292. reset_info->vector = (SDRAM_BASE + LINUX_LOAD_OFFSET) | 0xa0000000; /* Start from P2 area */
  293. }
  294. if (initrd_filename) {
  295. int initrd_size;
  296. initrd_size = load_image_targphys(initrd_filename,
  297. SDRAM_BASE + INITRD_LOAD_OFFSET,
  298. SDRAM_SIZE - INITRD_LOAD_OFFSET);
  299. if (initrd_size < 0) {
  300. fprintf(stderr, "qemu: could not load initrd '%s'\n", initrd_filename);
  301. exit(1);
  302. }
  303. /* initialization which should be done by firmware */
  304. boot_params.loader_type = tswap32(1);
  305. boot_params.initrd_start = tswap32(INITRD_LOAD_OFFSET);
  306. boot_params.initrd_size = tswap32(initrd_size);
  307. }
  308. if (kernel_cmdline) {
  309. /* I see no evidence that this .kernel_cmdline buffer requires
  310. NUL-termination, so using strncpy should be ok. */
  311. strncpy(boot_params.kernel_cmdline, kernel_cmdline,
  312. sizeof(boot_params.kernel_cmdline));
  313. }
  314. rom_add_blob_fixed("boot_params", &boot_params, sizeof(boot_params),
  315. SDRAM_BASE + BOOT_PARAMS_OFFSET);
  316. }
  317. static void r2d_machine_init(MachineClass *mc)
  318. {
  319. mc->desc = "r2d-plus board";
  320. mc->init = r2d_init;
  321. mc->block_default_type = IF_IDE;
  322. mc->default_cpu_type = TYPE_SH7751R_CPU;
  323. }
  324. DEFINE_MACHINE("r2d", r2d_machine_init)