lance.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * QEMU AMD PC-Net II (Am79C970A) emulation
  3. *
  4. * Copyright (c) 2004 Antony T Curtis
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. /* This software was written to be compatible with the specification:
  25. * AMD Am79C970A PCnet-PCI II Ethernet Controller Data-Sheet
  26. * AMD Publication# 19436 Rev:E Amendment/0 Issue Date: June 2000
  27. */
  28. /*
  29. * On Sparc32, this is the Lance (Am7990) part of chip STP2000 (Master I/O), also
  30. * produced as NCR89C100. See
  31. * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C100.txt
  32. * and
  33. * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR92C990.txt
  34. */
  35. #include "sysbus.h"
  36. #include "net.h"
  37. #include "qemu-timer.h"
  38. #include "qemu_socket.h"
  39. #include "sun4m.h"
  40. #include "pcnet.h"
  41. #include "trace.h"
  42. typedef struct {
  43. SysBusDevice busdev;
  44. PCNetState state;
  45. } SysBusPCNetState;
  46. static void parent_lance_reset(void *opaque, int irq, int level)
  47. {
  48. SysBusPCNetState *d = opaque;
  49. if (level)
  50. pcnet_h_reset(&d->state);
  51. }
  52. static void lance_mem_write(void *opaque, target_phys_addr_t addr,
  53. uint64_t val, unsigned size)
  54. {
  55. SysBusPCNetState *d = opaque;
  56. trace_lance_mem_writew(addr, val & 0xffff);
  57. pcnet_ioport_writew(&d->state, addr, val & 0xffff);
  58. }
  59. static uint64_t lance_mem_read(void *opaque, target_phys_addr_t addr,
  60. unsigned size)
  61. {
  62. SysBusPCNetState *d = opaque;
  63. uint32_t val;
  64. val = pcnet_ioport_readw(&d->state, addr);
  65. trace_lance_mem_readw(addr, val & 0xffff);
  66. return val & 0xffff;
  67. }
  68. static const MemoryRegionOps lance_mem_ops = {
  69. .read = lance_mem_read,
  70. .write = lance_mem_write,
  71. .endianness = DEVICE_NATIVE_ENDIAN,
  72. .valid = {
  73. .min_access_size = 2,
  74. .max_access_size = 2,
  75. },
  76. };
  77. static void lance_cleanup(VLANClientState *nc)
  78. {
  79. PCNetState *d = DO_UPCAST(NICState, nc, nc)->opaque;
  80. pcnet_common_cleanup(d);
  81. }
  82. static NetClientInfo net_lance_info = {
  83. .type = NET_CLIENT_TYPE_NIC,
  84. .size = sizeof(NICState),
  85. .can_receive = pcnet_can_receive,
  86. .receive = pcnet_receive,
  87. .link_status_changed = pcnet_set_link_status,
  88. .cleanup = lance_cleanup,
  89. };
  90. static const VMStateDescription vmstate_lance = {
  91. .name = "pcnet",
  92. .version_id = 3,
  93. .minimum_version_id = 2,
  94. .minimum_version_id_old = 2,
  95. .fields = (VMStateField []) {
  96. VMSTATE_STRUCT(state, SysBusPCNetState, 0, vmstate_pcnet, PCNetState),
  97. VMSTATE_END_OF_LIST()
  98. }
  99. };
  100. static int lance_init(SysBusDevice *dev)
  101. {
  102. SysBusPCNetState *d = FROM_SYSBUS(SysBusPCNetState, dev);
  103. PCNetState *s = &d->state;
  104. memory_region_init_io(&s->mmio, &lance_mem_ops, d, "lance-mmio", 4);
  105. qdev_init_gpio_in(&dev->qdev, parent_lance_reset, 1);
  106. sysbus_init_mmio_region(dev, &s->mmio);
  107. sysbus_init_irq(dev, &s->irq);
  108. s->phys_mem_read = ledma_memory_read;
  109. s->phys_mem_write = ledma_memory_write;
  110. return pcnet_common_init(&dev->qdev, s, &net_lance_info);
  111. }
  112. static void lance_reset(DeviceState *dev)
  113. {
  114. SysBusPCNetState *d = DO_UPCAST(SysBusPCNetState, busdev.qdev, dev);
  115. pcnet_h_reset(&d->state);
  116. }
  117. static SysBusDeviceInfo lance_info = {
  118. .init = lance_init,
  119. .qdev.name = "lance",
  120. .qdev.fw_name = "ethernet",
  121. .qdev.size = sizeof(SysBusPCNetState),
  122. .qdev.reset = lance_reset,
  123. .qdev.vmsd = &vmstate_lance,
  124. .qdev.props = (Property[]) {
  125. DEFINE_PROP_PTR("dma", SysBusPCNetState, state.dma_opaque),
  126. DEFINE_NIC_PROPERTIES(SysBusPCNetState, state.conf),
  127. DEFINE_PROP_END_OF_LIST(),
  128. }
  129. };
  130. static void lance_register_devices(void)
  131. {
  132. sysbus_register_withprop(&lance_info);
  133. }
  134. device_init(lance_register_devices)