pc87312.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * QEMU National Semiconductor PC87312 (Super I/O)
  3. *
  4. * Copyright (c) 2010-2012 Herve Poussineau
  5. * Copyright (c) 2011-2012 Andreas Färber
  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 "pc87312.h"
  26. #include "sysemu/blockdev.h"
  27. #include "sysemu/sysemu.h"
  28. #include "char/char.h"
  29. #include "trace.h"
  30. #define REG_FER 0
  31. #define REG_FAR 1
  32. #define REG_PTR 2
  33. #define FER_PARALLEL_EN 0x01
  34. #define FER_UART1_EN 0x02
  35. #define FER_UART2_EN 0x04
  36. #define FER_FDC_EN 0x08
  37. #define FER_FDC_4 0x10
  38. #define FER_FDC_ADDR 0x20
  39. #define FER_IDE_EN 0x40
  40. #define FER_IDE_ADDR 0x80
  41. #define FAR_PARALLEL_ADDR 0x03
  42. #define FAR_UART1_ADDR 0x0C
  43. #define FAR_UART2_ADDR 0x30
  44. #define FAR_UART_3_4 0xC0
  45. #define PTR_POWER_DOWN 0x01
  46. #define PTR_CLOCK_DOWN 0x02
  47. #define PTR_PWDN 0x04
  48. #define PTR_IRQ_5_7 0x08
  49. #define PTR_UART1_TEST 0x10
  50. #define PTR_UART2_TEST 0x20
  51. #define PTR_LOCK_CONF 0x40
  52. #define PTR_EPP_MODE 0x80
  53. /* Parallel port */
  54. static inline bool is_parallel_enabled(PC87312State *s)
  55. {
  56. return s->regs[REG_FER] & FER_PARALLEL_EN;
  57. }
  58. static const uint32_t parallel_base[] = { 0x378, 0x3bc, 0x278, 0x00 };
  59. static inline uint32_t get_parallel_iobase(PC87312State *s)
  60. {
  61. return parallel_base[s->regs[REG_FAR] & FAR_PARALLEL_ADDR];
  62. }
  63. static const uint32_t parallel_irq[] = { 5, 7, 5, 0 };
  64. static inline uint32_t get_parallel_irq(PC87312State *s)
  65. {
  66. int idx;
  67. idx = (s->regs[REG_FAR] & FAR_PARALLEL_ADDR);
  68. if (idx == 0) {
  69. return (s->regs[REG_PTR] & PTR_IRQ_5_7) ? 7 : 5;
  70. } else {
  71. return parallel_irq[idx];
  72. }
  73. }
  74. static inline bool is_parallel_epp(PC87312State *s)
  75. {
  76. return s->regs[REG_PTR] & PTR_EPP_MODE;
  77. }
  78. /* UARTs */
  79. static const uint32_t uart_base[2][4] = {
  80. { 0x3e8, 0x338, 0x2e8, 0x220 },
  81. { 0x2e8, 0x238, 0x2e0, 0x228 }
  82. };
  83. static inline uint32_t get_uart_iobase(PC87312State *s, int i)
  84. {
  85. int idx;
  86. idx = (s->regs[REG_FAR] >> (2 * i + 2)) & 0x3;
  87. if (idx == 0) {
  88. return 0x3f8;
  89. } else if (idx == 1) {
  90. return 0x2f8;
  91. } else {
  92. return uart_base[idx & 1][(s->regs[REG_FAR] & FAR_UART_3_4) >> 6];
  93. }
  94. }
  95. static inline uint32_t get_uart_irq(PC87312State *s, int i)
  96. {
  97. int idx;
  98. idx = (s->regs[REG_FAR] >> (2 * i + 2)) & 0x3;
  99. return (idx & 1) ? 3 : 4;
  100. }
  101. static inline bool is_uart_enabled(PC87312State *s, int i)
  102. {
  103. return s->regs[REG_FER] & (FER_UART1_EN << i);
  104. }
  105. /* Floppy controller */
  106. static inline bool is_fdc_enabled(PC87312State *s)
  107. {
  108. return s->regs[REG_FER] & FER_FDC_EN;
  109. }
  110. static inline uint32_t get_fdc_iobase(PC87312State *s)
  111. {
  112. return (s->regs[REG_FER] & FER_FDC_ADDR) ? 0x370 : 0x3f0;
  113. }
  114. /* IDE controller */
  115. static inline bool is_ide_enabled(PC87312State *s)
  116. {
  117. return s->regs[REG_FER] & FER_IDE_EN;
  118. }
  119. static inline uint32_t get_ide_iobase(PC87312State *s)
  120. {
  121. return (s->regs[REG_FER] & FER_IDE_ADDR) ? 0x170 : 0x1f0;
  122. }
  123. static void reconfigure_devices(PC87312State *s)
  124. {
  125. error_report("pc87312: unsupported device reconfiguration (%02x %02x %02x)",
  126. s->regs[REG_FER], s->regs[REG_FAR], s->regs[REG_PTR]);
  127. }
  128. static void pc87312_soft_reset(PC87312State *s)
  129. {
  130. static const uint8_t fer_init[] = {
  131. 0x4f, 0x4f, 0x4f, 0x4f, 0x4f, 0x4f, 0x4b, 0x4b,
  132. 0x4b, 0x4b, 0x4b, 0x4b, 0x0f, 0x0f, 0x0f, 0x0f,
  133. 0x49, 0x49, 0x49, 0x49, 0x07, 0x07, 0x07, 0x07,
  134. 0x47, 0x47, 0x47, 0x47, 0x47, 0x47, 0x08, 0x00,
  135. };
  136. static const uint8_t far_init[] = {
  137. 0x10, 0x11, 0x11, 0x39, 0x24, 0x38, 0x00, 0x01,
  138. 0x01, 0x09, 0x08, 0x08, 0x10, 0x11, 0x39, 0x24,
  139. 0x00, 0x01, 0x01, 0x00, 0x10, 0x11, 0x39, 0x24,
  140. 0x10, 0x11, 0x11, 0x39, 0x24, 0x38, 0x10, 0x10,
  141. };
  142. static const uint8_t ptr_init[] = {
  143. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  144. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  145. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  146. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
  147. };
  148. s->read_id_step = 0;
  149. s->selected_index = REG_FER;
  150. s->regs[REG_FER] = fer_init[s->config & 0x1f];
  151. s->regs[REG_FAR] = far_init[s->config & 0x1f];
  152. s->regs[REG_PTR] = ptr_init[s->config & 0x1f];
  153. }
  154. static void pc87312_hard_reset(PC87312State *s)
  155. {
  156. pc87312_soft_reset(s);
  157. }
  158. static void pc87312_io_write(void *opaque, hwaddr addr, uint64_t val,
  159. unsigned int size)
  160. {
  161. PC87312State *s = opaque;
  162. trace_pc87312_io_write(addr, val);
  163. if ((addr & 1) == 0) {
  164. /* Index register */
  165. s->read_id_step = 2;
  166. s->selected_index = val;
  167. } else {
  168. /* Data register */
  169. if (s->selected_index < 3) {
  170. s->regs[s->selected_index] = val;
  171. reconfigure_devices(s);
  172. }
  173. }
  174. }
  175. static uint64_t pc87312_io_read(void *opaque, hwaddr addr, unsigned int size)
  176. {
  177. PC87312State *s = opaque;
  178. uint32_t val;
  179. if ((addr & 1) == 0) {
  180. /* Index register */
  181. if (s->read_id_step++ == 0) {
  182. val = 0x88;
  183. } else if (s->read_id_step++ == 1) {
  184. val = 0;
  185. } else {
  186. val = s->selected_index;
  187. }
  188. } else {
  189. /* Data register */
  190. if (s->selected_index < 3) {
  191. val = s->regs[s->selected_index];
  192. } else {
  193. /* Invalid selected index */
  194. val = 0;
  195. }
  196. }
  197. trace_pc87312_io_read(addr, val);
  198. return val;
  199. }
  200. static const MemoryRegionOps pc87312_io_ops = {
  201. .read = pc87312_io_read,
  202. .write = pc87312_io_write,
  203. .endianness = DEVICE_LITTLE_ENDIAN,
  204. .valid = {
  205. .min_access_size = 1,
  206. .max_access_size = 1,
  207. },
  208. };
  209. static int pc87312_post_load(void *opaque, int version_id)
  210. {
  211. PC87312State *s = opaque;
  212. reconfigure_devices(s);
  213. return 0;
  214. }
  215. static void pc87312_reset(DeviceState *d)
  216. {
  217. PC87312State *s = PC87312(d);
  218. pc87312_soft_reset(s);
  219. }
  220. static int pc87312_init(ISADevice *dev)
  221. {
  222. PC87312State *s;
  223. DeviceState *d;
  224. ISADevice *isa;
  225. ISABus *bus;
  226. CharDriverState *chr;
  227. DriveInfo *drive;
  228. char name[5];
  229. int i;
  230. s = PC87312(dev);
  231. bus = isa_bus_from_device(dev);
  232. pc87312_hard_reset(s);
  233. isa_register_ioport(dev, &s->io, s->iobase);
  234. if (is_parallel_enabled(s)) {
  235. chr = parallel_hds[0];
  236. if (chr == NULL) {
  237. chr = qemu_chr_new("par0", "null", NULL);
  238. }
  239. isa = isa_create(bus, "isa-parallel");
  240. d = DEVICE(isa);
  241. qdev_prop_set_uint32(d, "index", 0);
  242. qdev_prop_set_uint32(d, "iobase", get_parallel_iobase(s));
  243. qdev_prop_set_uint32(d, "irq", get_parallel_irq(s));
  244. qdev_prop_set_chr(d, "chardev", chr);
  245. qdev_init_nofail(d);
  246. s->parallel.dev = isa;
  247. trace_pc87312_info_parallel(get_parallel_iobase(s),
  248. get_parallel_irq(s));
  249. }
  250. for (i = 0; i < 2; i++) {
  251. if (is_uart_enabled(s, i)) {
  252. chr = serial_hds[i];
  253. if (chr == NULL) {
  254. snprintf(name, sizeof(name), "ser%d", i);
  255. chr = qemu_chr_new(name, "null", NULL);
  256. }
  257. isa = isa_create(bus, "isa-serial");
  258. d = DEVICE(isa);
  259. qdev_prop_set_uint32(d, "index", i);
  260. qdev_prop_set_uint32(d, "iobase", get_uart_iobase(s, i));
  261. qdev_prop_set_uint32(d, "irq", get_uart_irq(s, i));
  262. qdev_prop_set_chr(d, "chardev", chr);
  263. qdev_init_nofail(d);
  264. s->uart[i].dev = isa;
  265. trace_pc87312_info_serial(i, get_uart_iobase(s, i),
  266. get_uart_irq(s, i));
  267. }
  268. }
  269. if (is_fdc_enabled(s)) {
  270. isa = isa_create(bus, "isa-fdc");
  271. d = DEVICE(isa);
  272. qdev_prop_set_uint32(d, "iobase", get_fdc_iobase(s));
  273. qdev_prop_set_uint32(d, "irq", 6);
  274. drive = drive_get(IF_FLOPPY, 0, 0);
  275. if (drive != NULL) {
  276. qdev_prop_set_drive_nofail(d, "driveA", drive->bdrv);
  277. }
  278. drive = drive_get(IF_FLOPPY, 0, 1);
  279. if (drive != NULL) {
  280. qdev_prop_set_drive_nofail(d, "driveB", drive->bdrv);
  281. }
  282. qdev_init_nofail(d);
  283. s->fdc.dev = isa;
  284. trace_pc87312_info_floppy(get_fdc_iobase(s));
  285. }
  286. if (is_ide_enabled(s)) {
  287. isa = isa_create(bus, "isa-ide");
  288. d = DEVICE(isa);
  289. qdev_prop_set_uint32(d, "iobase", get_ide_iobase(s));
  290. qdev_prop_set_uint32(d, "iobase2", get_ide_iobase(s) + 0x206);
  291. qdev_prop_set_uint32(d, "irq", 14);
  292. qdev_init_nofail(d);
  293. s->ide.dev = isa;
  294. trace_pc87312_info_ide(get_ide_iobase(s));
  295. }
  296. return 0;
  297. }
  298. static void pc87312_initfn(Object *obj)
  299. {
  300. PC87312State *s = PC87312(obj);
  301. memory_region_init_io(&s->io, &pc87312_io_ops, s, "pc87312", 2);
  302. }
  303. static const VMStateDescription vmstate_pc87312 = {
  304. .name = "pc87312",
  305. .version_id = 1,
  306. .minimum_version_id = 1,
  307. .post_load = pc87312_post_load,
  308. .fields = (VMStateField[]) {
  309. VMSTATE_UINT8(read_id_step, PC87312State),
  310. VMSTATE_UINT8(selected_index, PC87312State),
  311. VMSTATE_UINT8_ARRAY(regs, PC87312State, 3),
  312. VMSTATE_END_OF_LIST()
  313. }
  314. };
  315. static Property pc87312_properties[] = {
  316. DEFINE_PROP_HEX32("iobase", PC87312State, iobase, 0x398),
  317. DEFINE_PROP_UINT8("config", PC87312State, config, 1),
  318. DEFINE_PROP_END_OF_LIST()
  319. };
  320. static void pc87312_class_init(ObjectClass *klass, void *data)
  321. {
  322. DeviceClass *dc = DEVICE_CLASS(klass);
  323. ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
  324. ic->init = pc87312_init;
  325. dc->reset = pc87312_reset;
  326. dc->vmsd = &vmstate_pc87312;
  327. dc->props = pc87312_properties;
  328. }
  329. static const TypeInfo pc87312_type_info = {
  330. .name = TYPE_PC87312,
  331. .parent = TYPE_ISA_DEVICE,
  332. .instance_size = sizeof(PC87312State),
  333. .instance_init = pc87312_initfn,
  334. .class_init = pc87312_class_init,
  335. };
  336. static void pc87312_register_types(void)
  337. {
  338. type_register_static(&pc87312_type_info);
  339. }
  340. type_init(pc87312_register_types)