pc87312.c 10 KB

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