r2d.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #define FLASH_BASE 0x00000000
  40. #define FLASH_SIZE 0x02000000
  41. #define SDRAM_BASE 0x0c000000 /* Physical location of SDRAM: Area 3 */
  42. #define SDRAM_SIZE 0x04000000
  43. #define SM501_VRAM_SIZE 0x800000
  44. #define BOOT_PARAMS_OFFSET 0x0010000
  45. /* CONFIG_BOOT_LINK_OFFSET of Linux kernel */
  46. #define LINUX_LOAD_OFFSET 0x0800000
  47. #define INITRD_LOAD_OFFSET 0x1800000
  48. #define PA_IRLMSK 0x00
  49. #define PA_POWOFF 0x30
  50. #define PA_VERREG 0x32
  51. #define PA_OUTPORT 0x36
  52. typedef struct {
  53. uint16_t bcr;
  54. uint16_t irlmsk;
  55. uint16_t irlmon;
  56. uint16_t cfctl;
  57. uint16_t cfpow;
  58. uint16_t dispctl;
  59. uint16_t sdmpow;
  60. uint16_t rtcce;
  61. uint16_t pcicd;
  62. uint16_t voyagerrts;
  63. uint16_t cfrst;
  64. uint16_t admrts;
  65. uint16_t extrst;
  66. uint16_t cfcdintclr;
  67. uint16_t keyctlclr;
  68. uint16_t pad0;
  69. uint16_t pad1;
  70. uint16_t verreg;
  71. uint16_t inport;
  72. uint16_t outport;
  73. uint16_t bverreg;
  74. /* output pin */
  75. qemu_irq irl;
  76. } r2d_fpga_t;
  77. enum r2d_fpga_irq {
  78. PCI_INTD, CF_IDE, CF_CD, PCI_INTC, SM501, KEY, RTC_A, RTC_T,
  79. SDCARD, PCI_INTA, PCI_INTB, EXT, TP,
  80. NR_IRQS
  81. };
  82. static const struct { short irl; uint16_t msk; } irqtab[NR_IRQS] = {
  83. [CF_IDE] = { 1, 1<<9 },
  84. [CF_CD] = { 2, 1<<8 },
  85. [PCI_INTA] = { 9, 1<<14 },
  86. [PCI_INTB] = { 10, 1<<13 },
  87. [PCI_INTC] = { 3, 1<<12 },
  88. [PCI_INTD] = { 0, 1<<11 },
  89. [SM501] = { 4, 1<<10 },
  90. [KEY] = { 5, 1<<6 },
  91. [RTC_A] = { 6, 1<<5 },
  92. [RTC_T] = { 7, 1<<4 },
  93. [SDCARD] = { 8, 1<<7 },
  94. [EXT] = { 11, 1<<0 },
  95. [TP] = { 12, 1<<15 },
  96. };
  97. static void update_irl(r2d_fpga_t *fpga)
  98. {
  99. int i, irl = 15;
  100. for (i = 0; i < NR_IRQS; i++)
  101. if (fpga->irlmon & fpga->irlmsk & irqtab[i].msk)
  102. if (irqtab[i].irl < irl)
  103. irl = irqtab[i].irl;
  104. qemu_set_irq(fpga->irl, irl ^ 15);
  105. }
  106. static void r2d_fpga_irq_set(void *opaque, int n, int level)
  107. {
  108. r2d_fpga_t *fpga = opaque;
  109. if (level)
  110. fpga->irlmon |= irqtab[n].msk;
  111. else
  112. fpga->irlmon &= ~irqtab[n].msk;
  113. update_irl(fpga);
  114. }
  115. static uint32_t r2d_fpga_read(void *opaque, target_phys_addr_t addr)
  116. {
  117. r2d_fpga_t *s = opaque;
  118. switch (addr) {
  119. case PA_IRLMSK:
  120. return s->irlmsk;
  121. case PA_OUTPORT:
  122. return s->outport;
  123. case PA_POWOFF:
  124. return 0x00;
  125. case PA_VERREG:
  126. return 0x10;
  127. }
  128. return 0;
  129. }
  130. static void
  131. r2d_fpga_write(void *opaque, target_phys_addr_t addr, uint32_t value)
  132. {
  133. r2d_fpga_t *s = opaque;
  134. switch (addr) {
  135. case PA_IRLMSK:
  136. s->irlmsk = value;
  137. update_irl(s);
  138. break;
  139. case PA_OUTPORT:
  140. s->outport = value;
  141. break;
  142. case PA_POWOFF:
  143. if (value & 1) {
  144. qemu_system_shutdown_request();
  145. }
  146. break;
  147. case PA_VERREG:
  148. /* Discard writes */
  149. break;
  150. }
  151. }
  152. static CPUReadMemoryFunc * const r2d_fpga_readfn[] = {
  153. r2d_fpga_read,
  154. r2d_fpga_read,
  155. NULL,
  156. };
  157. static CPUWriteMemoryFunc * const r2d_fpga_writefn[] = {
  158. r2d_fpga_write,
  159. r2d_fpga_write,
  160. NULL,
  161. };
  162. static qemu_irq *r2d_fpga_init(target_phys_addr_t base, qemu_irq irl)
  163. {
  164. int iomemtype;
  165. r2d_fpga_t *s;
  166. s = qemu_mallocz(sizeof(r2d_fpga_t));
  167. s->irl = irl;
  168. iomemtype = cpu_register_io_memory(r2d_fpga_readfn,
  169. r2d_fpga_writefn, s,
  170. DEVICE_NATIVE_ENDIAN);
  171. cpu_register_physical_memory(base, 0x40, iomemtype);
  172. return qemu_allocate_irqs(r2d_fpga_irq_set, s, NR_IRQS);
  173. }
  174. typedef struct ResetData {
  175. CPUState *env;
  176. uint32_t vector;
  177. } ResetData;
  178. static void main_cpu_reset(void *opaque)
  179. {
  180. ResetData *s = (ResetData *)opaque;
  181. CPUState *env = s->env;
  182. cpu_reset(env);
  183. env->pc = s->vector;
  184. }
  185. static struct __attribute__((__packed__))
  186. {
  187. int mount_root_rdonly;
  188. int ramdisk_flags;
  189. int orig_root_dev;
  190. int loader_type;
  191. int initrd_start;
  192. int initrd_size;
  193. char pad[232];
  194. char kernel_cmdline[256];
  195. } boot_params;
  196. static void r2d_init(ram_addr_t ram_size,
  197. const char *boot_device,
  198. const char *kernel_filename, const char *kernel_cmdline,
  199. const char *initrd_filename, const char *cpu_model)
  200. {
  201. CPUState *env;
  202. ResetData *reset_info;
  203. struct SH7750State *s;
  204. ram_addr_t sdram_addr;
  205. qemu_irq *irq;
  206. DriveInfo *dinfo;
  207. int i;
  208. if (!cpu_model)
  209. cpu_model = "SH7751R";
  210. env = cpu_init(cpu_model);
  211. if (!env) {
  212. fprintf(stderr, "Unable to find CPU definition\n");
  213. exit(1);
  214. }
  215. reset_info = qemu_mallocz(sizeof(ResetData));
  216. reset_info->env = env;
  217. reset_info->vector = env->pc;
  218. qemu_register_reset(main_cpu_reset, reset_info);
  219. /* Allocate memory space */
  220. sdram_addr = qemu_ram_alloc(NULL, "r2d.sdram", SDRAM_SIZE);
  221. cpu_register_physical_memory(SDRAM_BASE, SDRAM_SIZE, sdram_addr);
  222. /* Register peripherals */
  223. s = sh7750_init(env);
  224. irq = r2d_fpga_init(0x04000000, sh7750_irl(s));
  225. sysbus_create_varargs("sh_pci", 0x1e200000, irq[PCI_INTA], irq[PCI_INTB],
  226. irq[PCI_INTC], irq[PCI_INTD], NULL);
  227. sm501_init(0x10000000, SM501_VRAM_SIZE, irq[SM501], serial_hds[2]);
  228. /* onboard CF (True IDE mode, Master only). */
  229. dinfo = drive_get(IF_IDE, 0, 0);
  230. mmio_ide_init(0x14001000, 0x1400080c, irq[CF_IDE], 1,
  231. dinfo, NULL);
  232. /* onboard flash memory */
  233. dinfo = drive_get(IF_PFLASH, 0, 0);
  234. pflash_cfi02_register(0x0, qemu_ram_alloc(NULL, "r2d.flash", FLASH_SIZE),
  235. dinfo ? dinfo->bdrv : NULL, (16 * 1024),
  236. FLASH_SIZE >> 16,
  237. 1, 4, 0x0000, 0x0000, 0x0000, 0x0000,
  238. 0x555, 0x2aa, 0);
  239. /* NIC: rtl8139 on-board, and 2 slots. */
  240. for (i = 0; i < nb_nics; i++)
  241. pci_nic_init_nofail(&nd_table[i], "rtl8139", i==0 ? "2" : NULL);
  242. /* USB keyboard */
  243. usbdevice_create("keyboard");
  244. /* Todo: register on board registers */
  245. memset(&boot_params, 0, sizeof(boot_params));
  246. if (kernel_filename) {
  247. int kernel_size;
  248. kernel_size = load_image_targphys(kernel_filename,
  249. SDRAM_BASE + LINUX_LOAD_OFFSET,
  250. INITRD_LOAD_OFFSET - LINUX_LOAD_OFFSET);
  251. if (kernel_size < 0) {
  252. fprintf(stderr, "qemu: could not load kernel '%s'\n", kernel_filename);
  253. exit(1);
  254. }
  255. /* initialization which should be done by firmware */
  256. stl_phys(SH7750_BCR1, 1<<3); /* cs3 SDRAM */
  257. stw_phys(SH7750_BCR2, 3<<(3*2)); /* cs3 32bit */
  258. reset_info->vector = (SDRAM_BASE + LINUX_LOAD_OFFSET) | 0xa0000000; /* Start from P2 area */
  259. }
  260. if (initrd_filename) {
  261. int initrd_size;
  262. initrd_size = load_image_targphys(initrd_filename,
  263. SDRAM_BASE + INITRD_LOAD_OFFSET,
  264. SDRAM_SIZE - INITRD_LOAD_OFFSET);
  265. if (initrd_size < 0) {
  266. fprintf(stderr, "qemu: could not load initrd '%s'\n", initrd_filename);
  267. exit(1);
  268. }
  269. /* initialization which should be done by firmware */
  270. boot_params.loader_type = 1;
  271. boot_params.initrd_start = INITRD_LOAD_OFFSET;
  272. boot_params.initrd_size = initrd_size;
  273. }
  274. if (kernel_cmdline) {
  275. strncpy(boot_params.kernel_cmdline, kernel_cmdline,
  276. sizeof(boot_params.kernel_cmdline));
  277. }
  278. rom_add_blob_fixed("boot_params", &boot_params, sizeof(boot_params),
  279. SDRAM_BASE + BOOT_PARAMS_OFFSET);
  280. }
  281. static QEMUMachine r2d_machine = {
  282. .name = "r2d",
  283. .desc = "r2d-plus board",
  284. .init = r2d_init,
  285. };
  286. static void r2d_machine_init(void)
  287. {
  288. qemu_register_machine(&r2d_machine);
  289. }
  290. machine_init(r2d_machine_init);