lance.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "hw/sysbus.h"
  36. #include "net/net.h"
  37. #include "qemu/timer.h"
  38. #include "qemu/sockets.h"
  39. #include "hw/sparc/sun4m.h"
  40. #include "pcnet.h"
  41. #include "trace.h"
  42. #define TYPE_LANCE "lance"
  43. #define SYSBUS_PCNET(obj) \
  44. OBJECT_CHECK(SysBusPCNetState, (obj), TYPE_LANCE)
  45. typedef struct {
  46. SysBusDevice parent_obj;
  47. PCNetState state;
  48. } SysBusPCNetState;
  49. static void parent_lance_reset(void *opaque, int irq, int level)
  50. {
  51. SysBusPCNetState *d = opaque;
  52. if (level)
  53. pcnet_h_reset(&d->state);
  54. }
  55. static void lance_mem_write(void *opaque, hwaddr addr,
  56. uint64_t val, unsigned size)
  57. {
  58. SysBusPCNetState *d = opaque;
  59. trace_lance_mem_writew(addr, val & 0xffff);
  60. pcnet_ioport_writew(&d->state, addr, val & 0xffff);
  61. }
  62. static uint64_t lance_mem_read(void *opaque, hwaddr addr,
  63. unsigned size)
  64. {
  65. SysBusPCNetState *d = opaque;
  66. uint32_t val;
  67. val = pcnet_ioport_readw(&d->state, addr);
  68. trace_lance_mem_readw(addr, val & 0xffff);
  69. return val & 0xffff;
  70. }
  71. static const MemoryRegionOps lance_mem_ops = {
  72. .read = lance_mem_read,
  73. .write = lance_mem_write,
  74. .endianness = DEVICE_NATIVE_ENDIAN,
  75. .valid = {
  76. .min_access_size = 2,
  77. .max_access_size = 2,
  78. },
  79. };
  80. static void lance_cleanup(NetClientState *nc)
  81. {
  82. PCNetState *d = qemu_get_nic_opaque(nc);
  83. pcnet_common_cleanup(d);
  84. }
  85. static NetClientInfo net_lance_info = {
  86. .type = NET_CLIENT_OPTIONS_KIND_NIC,
  87. .size = sizeof(NICState),
  88. .can_receive = pcnet_can_receive,
  89. .receive = pcnet_receive,
  90. .link_status_changed = pcnet_set_link_status,
  91. .cleanup = lance_cleanup,
  92. };
  93. static const VMStateDescription vmstate_lance = {
  94. .name = "pcnet",
  95. .version_id = 3,
  96. .minimum_version_id = 2,
  97. .minimum_version_id_old = 2,
  98. .fields = (VMStateField []) {
  99. VMSTATE_STRUCT(state, SysBusPCNetState, 0, vmstate_pcnet, PCNetState),
  100. VMSTATE_END_OF_LIST()
  101. }
  102. };
  103. static int lance_init(SysBusDevice *sbd)
  104. {
  105. DeviceState *dev = DEVICE(sbd);
  106. SysBusPCNetState *d = SYSBUS_PCNET(dev);
  107. PCNetState *s = &d->state;
  108. memory_region_init_io(&s->mmio, OBJECT(d), &lance_mem_ops, d,
  109. "lance-mmio", 4);
  110. qdev_init_gpio_in(dev, parent_lance_reset, 1);
  111. sysbus_init_mmio(sbd, &s->mmio);
  112. sysbus_init_irq(sbd, &s->irq);
  113. s->phys_mem_read = ledma_memory_read;
  114. s->phys_mem_write = ledma_memory_write;
  115. return pcnet_common_init(dev, s, &net_lance_info);
  116. }
  117. static void lance_reset(DeviceState *dev)
  118. {
  119. SysBusPCNetState *d = SYSBUS_PCNET(dev);
  120. pcnet_h_reset(&d->state);
  121. }
  122. static Property lance_properties[] = {
  123. DEFINE_PROP_PTR("dma", SysBusPCNetState, state.dma_opaque),
  124. DEFINE_NIC_PROPERTIES(SysBusPCNetState, state.conf),
  125. DEFINE_PROP_END_OF_LIST(),
  126. };
  127. static void lance_class_init(ObjectClass *klass, void *data)
  128. {
  129. DeviceClass *dc = DEVICE_CLASS(klass);
  130. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  131. k->init = lance_init;
  132. set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
  133. dc->fw_name = "ethernet";
  134. dc->reset = lance_reset;
  135. dc->vmsd = &vmstate_lance;
  136. dc->props = lance_properties;
  137. /* Reason: pointer property "dma" */
  138. dc->cannot_instantiate_with_device_add_yet = true;
  139. }
  140. static const TypeInfo lance_info = {
  141. .name = TYPE_LANCE,
  142. .parent = TYPE_SYS_BUS_DEVICE,
  143. .instance_size = sizeof(SysBusPCNetState),
  144. .class_init = lance_class_init,
  145. };
  146. static void lance_register_types(void)
  147. {
  148. type_register_static(&lance_info);
  149. }
  150. type_init(lance_register_types)