mmio.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * QEMU IDE Emulation: mmio support (for embedded).
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. * Copyright (c) 2006 Openedhand Ltd.
  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/sysbus.h"
  27. #include "migration/vmstate.h"
  28. #include "qemu/module.h"
  29. #include "system/dma.h"
  30. #include "hw/ide/mmio.h"
  31. #include "hw/qdev-properties.h"
  32. #include "ide-internal.h"
  33. /***********************************************************/
  34. /* MMIO based ide port
  35. * This emulates IDE device connected directly to the CPU bus without
  36. * dedicated ide controller, which is often seen on embedded boards.
  37. */
  38. struct MMIOIDEState {
  39. /*< private >*/
  40. SysBusDevice parent_obj;
  41. /*< public >*/
  42. IDEBus bus;
  43. uint32_t shift;
  44. qemu_irq irq;
  45. MemoryRegion iomem1, iomem2;
  46. };
  47. static void mmio_ide_reset(DeviceState *dev)
  48. {
  49. MMIOIDEState *s = MMIO_IDE(dev);
  50. ide_bus_reset(&s->bus);
  51. }
  52. static uint64_t mmio_ide_read(void *opaque, hwaddr addr,
  53. unsigned size)
  54. {
  55. MMIOIDEState *s = opaque;
  56. addr >>= s->shift;
  57. if (addr & 7)
  58. return ide_ioport_read(&s->bus, addr);
  59. else
  60. return ide_data_readw(&s->bus, 0);
  61. }
  62. static void mmio_ide_write(void *opaque, hwaddr addr,
  63. uint64_t val, unsigned size)
  64. {
  65. MMIOIDEState *s = opaque;
  66. addr >>= s->shift;
  67. if (addr & 7)
  68. ide_ioport_write(&s->bus, addr, val);
  69. else
  70. ide_data_writew(&s->bus, 0, val);
  71. }
  72. static const MemoryRegionOps mmio_ide_ops = {
  73. .read = mmio_ide_read,
  74. .write = mmio_ide_write,
  75. .endianness = DEVICE_LITTLE_ENDIAN,
  76. };
  77. static uint64_t mmio_ide_status_read(void *opaque, hwaddr addr,
  78. unsigned size)
  79. {
  80. MMIOIDEState *s = opaque;
  81. return ide_status_read(&s->bus, 0);
  82. }
  83. static void mmio_ide_ctrl_write(void *opaque, hwaddr addr,
  84. uint64_t val, unsigned size)
  85. {
  86. MMIOIDEState *s = opaque;
  87. ide_ctrl_write(&s->bus, 0, val);
  88. }
  89. static const MemoryRegionOps mmio_ide_cs_ops = {
  90. .read = mmio_ide_status_read,
  91. .write = mmio_ide_ctrl_write,
  92. .endianness = DEVICE_LITTLE_ENDIAN,
  93. };
  94. static const VMStateDescription vmstate_ide_mmio = {
  95. .name = "mmio-ide",
  96. .version_id = 3,
  97. .minimum_version_id = 0,
  98. .fields = (const VMStateField[]) {
  99. VMSTATE_IDE_BUS(bus, MMIOIDEState),
  100. VMSTATE_IDE_DRIVES(bus.ifs, MMIOIDEState),
  101. VMSTATE_END_OF_LIST()
  102. }
  103. };
  104. static void mmio_ide_realizefn(DeviceState *dev, Error **errp)
  105. {
  106. SysBusDevice *d = SYS_BUS_DEVICE(dev);
  107. MMIOIDEState *s = MMIO_IDE(dev);
  108. ide_bus_init_output_irq(&s->bus, s->irq);
  109. memory_region_init_io(&s->iomem1, OBJECT(s), &mmio_ide_ops, s,
  110. "ide-mmio.1", 16 << s->shift);
  111. memory_region_init_io(&s->iomem2, OBJECT(s), &mmio_ide_cs_ops, s,
  112. "ide-mmio.2", 2 << s->shift);
  113. sysbus_init_mmio(d, &s->iomem1);
  114. sysbus_init_mmio(d, &s->iomem2);
  115. }
  116. static void mmio_ide_initfn(Object *obj)
  117. {
  118. SysBusDevice *d = SYS_BUS_DEVICE(obj);
  119. MMIOIDEState *s = MMIO_IDE(obj);
  120. ide_bus_init(&s->bus, sizeof(s->bus), DEVICE(obj), 0, 2);
  121. sysbus_init_irq(d, &s->irq);
  122. }
  123. static const Property mmio_ide_properties[] = {
  124. DEFINE_PROP_UINT32("shift", MMIOIDEState, shift, 0),
  125. };
  126. static void mmio_ide_class_init(ObjectClass *oc, void *data)
  127. {
  128. DeviceClass *dc = DEVICE_CLASS(oc);
  129. dc->realize = mmio_ide_realizefn;
  130. device_class_set_legacy_reset(dc, mmio_ide_reset);
  131. device_class_set_props(dc, mmio_ide_properties);
  132. dc->vmsd = &vmstate_ide_mmio;
  133. }
  134. static const TypeInfo mmio_ide_type_info = {
  135. .name = TYPE_MMIO_IDE,
  136. .parent = TYPE_SYS_BUS_DEVICE,
  137. .instance_size = sizeof(MMIOIDEState),
  138. .instance_init = mmio_ide_initfn,
  139. .class_init = mmio_ide_class_init,
  140. };
  141. static void mmio_ide_register_types(void)
  142. {
  143. type_register_static(&mmio_ide_type_info);
  144. }
  145. void mmio_ide_init_drives(DeviceState *dev, DriveInfo *hd0, DriveInfo *hd1)
  146. {
  147. MMIOIDEState *s = MMIO_IDE(dev);
  148. if (hd0 != NULL) {
  149. ide_bus_create_drive(&s->bus, 0, hd0);
  150. }
  151. if (hd1 != NULL) {
  152. ide_bus_create_drive(&s->bus, 1, hd1);
  153. }
  154. }
  155. type_init(mmio_ide_register_types)