serial-isa.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * QEMU 16550A UART emulation
  3. *
  4. * Copyright (c) 2003-2004 Fabrice Bellard
  5. * Copyright (c) 2008 Citrix Systems, Inc.
  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 "serial.h"
  26. #include "isa.h"
  27. typedef struct ISASerialState {
  28. ISADevice dev;
  29. uint32_t index;
  30. uint32_t iobase;
  31. uint32_t isairq;
  32. SerialState state;
  33. } ISASerialState;
  34. static const int isa_serial_io[MAX_SERIAL_PORTS] = {
  35. 0x3f8, 0x2f8, 0x3e8, 0x2e8
  36. };
  37. static const int isa_serial_irq[MAX_SERIAL_PORTS] = {
  38. 4, 3, 4, 3
  39. };
  40. static int serial_isa_initfn(ISADevice *dev)
  41. {
  42. static int index;
  43. ISASerialState *isa = DO_UPCAST(ISASerialState, dev, dev);
  44. SerialState *s = &isa->state;
  45. if (isa->index == -1) {
  46. isa->index = index;
  47. }
  48. if (isa->index >= MAX_SERIAL_PORTS) {
  49. return -1;
  50. }
  51. if (isa->iobase == -1) {
  52. isa->iobase = isa_serial_io[isa->index];
  53. }
  54. if (isa->isairq == -1) {
  55. isa->isairq = isa_serial_irq[isa->index];
  56. }
  57. index++;
  58. s->baudbase = 115200;
  59. isa_init_irq(dev, &s->irq, isa->isairq);
  60. serial_init_core(s);
  61. qdev_set_legacy_instance_id(&dev->qdev, isa->iobase, 3);
  62. memory_region_init_io(&s->io, &serial_io_ops, s, "serial", 8);
  63. isa_register_ioport(dev, &s->io, isa->iobase);
  64. return 0;
  65. }
  66. static const VMStateDescription vmstate_isa_serial = {
  67. .name = "serial",
  68. .version_id = 3,
  69. .minimum_version_id = 2,
  70. .fields = (VMStateField[]) {
  71. VMSTATE_STRUCT(state, ISASerialState, 0, vmstate_serial, SerialState),
  72. VMSTATE_END_OF_LIST()
  73. }
  74. };
  75. static Property serial_isa_properties[] = {
  76. DEFINE_PROP_UINT32("index", ISASerialState, index, -1),
  77. DEFINE_PROP_HEX32("iobase", ISASerialState, iobase, -1),
  78. DEFINE_PROP_UINT32("irq", ISASerialState, isairq, -1),
  79. DEFINE_PROP_CHR("chardev", ISASerialState, state.chr),
  80. DEFINE_PROP_UINT32("wakeup", ISASerialState, state.wakeup, 0),
  81. DEFINE_PROP_END_OF_LIST(),
  82. };
  83. static void serial_isa_class_initfn(ObjectClass *klass, void *data)
  84. {
  85. DeviceClass *dc = DEVICE_CLASS(klass);
  86. ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
  87. ic->init = serial_isa_initfn;
  88. dc->vmsd = &vmstate_isa_serial;
  89. dc->props = serial_isa_properties;
  90. }
  91. static const TypeInfo serial_isa_info = {
  92. .name = "isa-serial",
  93. .parent = TYPE_ISA_DEVICE,
  94. .instance_size = sizeof(ISASerialState),
  95. .class_init = serial_isa_class_initfn,
  96. };
  97. static void serial_register_types(void)
  98. {
  99. type_register_static(&serial_isa_info);
  100. }
  101. type_init(serial_register_types)
  102. bool serial_isa_init(ISABus *bus, int index, CharDriverState *chr)
  103. {
  104. ISADevice *dev;
  105. dev = isa_try_create(bus, "isa-serial");
  106. if (!dev) {
  107. return false;
  108. }
  109. qdev_prop_set_uint32(&dev->qdev, "index", index);
  110. qdev_prop_set_chr(&dev->qdev, "chardev", chr);
  111. if (qdev_init(&dev->qdev) < 0) {
  112. return false;
  113. }
  114. return true;
  115. }