2
0

lance.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "qemu/osdep.h"
  36. #include "qemu/module.h"
  37. #include "qemu/timer.h"
  38. #include "hw/sparc/sparc32_dma.h"
  39. #include "migration/vmstate.h"
  40. #include "hw/net/lance.h"
  41. #include "hw/qdev-properties.h"
  42. #include "trace.h"
  43. #include "sysemu/sysemu.h"
  44. static void parent_lance_reset(void *opaque, int irq, int level)
  45. {
  46. SysBusPCNetState *d = opaque;
  47. if (level)
  48. pcnet_h_reset(&d->state);
  49. }
  50. static void lance_mem_write(void *opaque, hwaddr addr,
  51. uint64_t val, unsigned size)
  52. {
  53. SysBusPCNetState *d = opaque;
  54. trace_lance_mem_writew(addr, val & 0xffff);
  55. pcnet_ioport_writew(&d->state, addr, val & 0xffff);
  56. }
  57. static uint64_t lance_mem_read(void *opaque, hwaddr addr,
  58. unsigned size)
  59. {
  60. SysBusPCNetState *d = opaque;
  61. uint32_t val;
  62. val = pcnet_ioport_readw(&d->state, addr);
  63. trace_lance_mem_readw(addr, val & 0xffff);
  64. return val & 0xffff;
  65. }
  66. static const MemoryRegionOps lance_mem_ops = {
  67. .read = lance_mem_read,
  68. .write = lance_mem_write,
  69. .endianness = DEVICE_NATIVE_ENDIAN,
  70. .valid = {
  71. .min_access_size = 2,
  72. .max_access_size = 2,
  73. },
  74. };
  75. static NetClientInfo net_lance_info = {
  76. .type = NET_CLIENT_DRIVER_NIC,
  77. .size = sizeof(NICState),
  78. .receive = pcnet_receive,
  79. .link_status_changed = pcnet_set_link_status,
  80. };
  81. static const VMStateDescription vmstate_lance = {
  82. .name = "pcnet",
  83. .version_id = 3,
  84. .minimum_version_id = 2,
  85. .fields = (VMStateField[]) {
  86. VMSTATE_STRUCT(state, SysBusPCNetState, 0, vmstate_pcnet, PCNetState),
  87. VMSTATE_END_OF_LIST()
  88. }
  89. };
  90. static void lance_realize(DeviceState *dev, Error **errp)
  91. {
  92. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  93. SysBusPCNetState *d = SYSBUS_PCNET(dev);
  94. PCNetState *s = &d->state;
  95. memory_region_init_io(&s->mmio, OBJECT(d), &lance_mem_ops, d,
  96. "lance-mmio", 4);
  97. qdev_init_gpio_in(dev, parent_lance_reset, 1);
  98. sysbus_init_mmio(sbd, &s->mmio);
  99. sysbus_init_irq(sbd, &s->irq);
  100. s->phys_mem_read = ledma_memory_read;
  101. s->phys_mem_write = ledma_memory_write;
  102. pcnet_common_init(dev, s, &net_lance_info);
  103. }
  104. static void lance_reset(DeviceState *dev)
  105. {
  106. SysBusPCNetState *d = SYSBUS_PCNET(dev);
  107. pcnet_h_reset(&d->state);
  108. }
  109. static void lance_instance_init(Object *obj)
  110. {
  111. SysBusPCNetState *d = SYSBUS_PCNET(obj);
  112. PCNetState *s = &d->state;
  113. device_add_bootindex_property(obj, &s->conf.bootindex,
  114. "bootindex", "/ethernet-phy@0",
  115. DEVICE(obj));
  116. }
  117. static Property lance_properties[] = {
  118. DEFINE_PROP_LINK("dma", SysBusPCNetState, state.dma_opaque,
  119. TYPE_DEVICE, DeviceState *),
  120. DEFINE_NIC_PROPERTIES(SysBusPCNetState, state.conf),
  121. DEFINE_PROP_END_OF_LIST(),
  122. };
  123. static void lance_class_init(ObjectClass *klass, void *data)
  124. {
  125. DeviceClass *dc = DEVICE_CLASS(klass);
  126. dc->realize = lance_realize;
  127. set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
  128. dc->fw_name = "ethernet";
  129. dc->reset = lance_reset;
  130. dc->vmsd = &vmstate_lance;
  131. device_class_set_props(dc, lance_properties);
  132. }
  133. static const TypeInfo lance_info = {
  134. .name = TYPE_LANCE,
  135. .parent = TYPE_SYS_BUS_DEVICE,
  136. .instance_size = sizeof(SysBusPCNetState),
  137. .class_init = lance_class_init,
  138. .instance_init = lance_instance_init,
  139. };
  140. static void lance_register_types(void)
  141. {
  142. type_register_static(&lance_info);
  143. }
  144. type_init(lance_register_types)