2
0

lasi_i82596.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * QEMU LASI NIC i82596 emulation
  3. *
  4. * Copyright (c) 2019 Helge Deller <deller@gmx.de>
  5. * This work is licensed under the GNU GPL license version 2 or later.
  6. *
  7. *
  8. * On PA-RISC, this is the Network part of LASI chip.
  9. * See:
  10. * https://parisc.wiki.kernel.org/images-parisc/7/79/Lasi_ers.pdf
  11. */
  12. #include "qemu/osdep.h"
  13. #include "qapi/error.h"
  14. #include "qemu/timer.h"
  15. #include "hw/sysbus.h"
  16. #include "system/system.h"
  17. #include "net/eth.h"
  18. #include "hw/net/lasi_82596.h"
  19. #include "hw/net/i82596.h"
  20. #include "trace.h"
  21. #include "hw/qdev-properties.h"
  22. #include "migration/vmstate.h"
  23. #define PA_I82596_RESET 0 /* Offsets relative to LASI-LAN-Addr.*/
  24. #define PA_CPU_PORT_L_ACCESS 4
  25. #define PA_CHANNEL_ATTENTION 8
  26. #define PA_GET_MACADDR 12
  27. #define SWAP32(x) (((uint32_t)(x) << 16) | ((((uint32_t)(x))) >> 16))
  28. static void lasi_82596_mem_write(void *opaque, hwaddr addr,
  29. uint64_t val, unsigned size)
  30. {
  31. SysBusI82596State *d = opaque;
  32. trace_lasi_82596_mem_writew(addr, val);
  33. switch (addr) {
  34. case PA_I82596_RESET:
  35. i82596_h_reset(&d->state);
  36. break;
  37. case PA_CPU_PORT_L_ACCESS:
  38. d->val_index++;
  39. if (d->val_index == 0) {
  40. uint32_t v = d->last_val | (val << 16);
  41. v = v & ~0xff;
  42. i82596_ioport_writew(&d->state, d->last_val & 0xff, v);
  43. }
  44. d->last_val = val;
  45. break;
  46. case PA_CHANNEL_ATTENTION:
  47. i82596_ioport_writew(&d->state, PORT_CA, val);
  48. break;
  49. case PA_GET_MACADDR:
  50. /*
  51. * Provided for SeaBIOS only. Write MAC of Network card to addr @val.
  52. * Needed for the PDC_LAN_STATION_ID_READ PDC call.
  53. */
  54. address_space_write(&address_space_memory, val,
  55. MEMTXATTRS_UNSPECIFIED, d->state.conf.macaddr.a,
  56. ETH_ALEN);
  57. break;
  58. }
  59. }
  60. static uint64_t lasi_82596_mem_read(void *opaque, hwaddr addr,
  61. unsigned size)
  62. {
  63. SysBusI82596State *d = opaque;
  64. uint32_t val;
  65. if (addr == PA_GET_MACADDR) {
  66. val = 0xBEEFBABE;
  67. } else {
  68. val = i82596_ioport_readw(&d->state, addr);
  69. }
  70. trace_lasi_82596_mem_readw(addr, val);
  71. return val;
  72. }
  73. static const MemoryRegionOps lasi_82596_mem_ops = {
  74. .read = lasi_82596_mem_read,
  75. .write = lasi_82596_mem_write,
  76. .endianness = DEVICE_BIG_ENDIAN,
  77. .valid = {
  78. .min_access_size = 4,
  79. .max_access_size = 4,
  80. },
  81. };
  82. static NetClientInfo net_lasi_82596_info = {
  83. .type = NET_CLIENT_DRIVER_NIC,
  84. .size = sizeof(NICState),
  85. .can_receive = i82596_can_receive,
  86. .receive = i82596_receive,
  87. .link_status_changed = i82596_set_link_status,
  88. };
  89. static const VMStateDescription vmstate_lasi_82596 = {
  90. .name = "i82596",
  91. .version_id = 1,
  92. .minimum_version_id = 1,
  93. .fields = (const VMStateField[]) {
  94. VMSTATE_STRUCT(state, SysBusI82596State, 0, vmstate_i82596,
  95. I82596State),
  96. VMSTATE_END_OF_LIST()
  97. }
  98. };
  99. static void lasi_82596_realize(DeviceState *dev, Error **errp)
  100. {
  101. SysBusI82596State *d = SYSBUS_I82596(dev);
  102. I82596State *s = &d->state;
  103. memory_region_init_io(&s->mmio, OBJECT(d), &lasi_82596_mem_ops, d,
  104. "lasi_82596-mmio", PA_GET_MACADDR + 4);
  105. i82596_common_init(dev, s, &net_lasi_82596_info);
  106. }
  107. SysBusI82596State *lasi_82596_init(MemoryRegion *addr_space, hwaddr hpa,
  108. qemu_irq lan_irq, gboolean match_default)
  109. {
  110. DeviceState *dev;
  111. SysBusI82596State *s;
  112. static const MACAddr HP_MAC = {
  113. .a = { 0x08, 0x00, 0x09, 0xef, 0x34, 0xf6 } };
  114. dev = qemu_create_nic_device(TYPE_LASI_82596, match_default, "lasi");
  115. if (!dev) {
  116. return NULL;
  117. }
  118. s = SYSBUS_I82596(dev);
  119. s->state.irq = lan_irq;
  120. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  121. s->state.conf.macaddr = HP_MAC; /* set HP MAC prefix */
  122. /* LASI 82596 ports in main memory. */
  123. memory_region_add_subregion(addr_space, hpa, &s->state.mmio);
  124. return s;
  125. }
  126. static void lasi_82596_reset(DeviceState *dev)
  127. {
  128. SysBusI82596State *d = SYSBUS_I82596(dev);
  129. i82596_h_reset(&d->state);
  130. }
  131. static void lasi_82596_instance_init(Object *obj)
  132. {
  133. SysBusI82596State *d = SYSBUS_I82596(obj);
  134. I82596State *s = &d->state;
  135. device_add_bootindex_property(obj, &s->conf.bootindex,
  136. "bootindex", "/ethernet-phy@0",
  137. DEVICE(obj));
  138. }
  139. static const Property lasi_82596_properties[] = {
  140. DEFINE_NIC_PROPERTIES(SysBusI82596State, state.conf),
  141. };
  142. static void lasi_82596_class_init(ObjectClass *klass, void *data)
  143. {
  144. DeviceClass *dc = DEVICE_CLASS(klass);
  145. dc->realize = lasi_82596_realize;
  146. set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
  147. dc->fw_name = "ethernet";
  148. device_class_set_legacy_reset(dc, lasi_82596_reset);
  149. dc->vmsd = &vmstate_lasi_82596;
  150. dc->user_creatable = false;
  151. device_class_set_props(dc, lasi_82596_properties);
  152. }
  153. static const TypeInfo lasi_82596_info = {
  154. .name = TYPE_LASI_82596,
  155. .parent = TYPE_SYS_BUS_DEVICE,
  156. .instance_size = sizeof(SysBusI82596State),
  157. .class_init = lasi_82596_class_init,
  158. .instance_init = lasi_82596_instance_init,
  159. };
  160. static void lasi_82596_register_types(void)
  161. {
  162. type_register_static(&lasi_82596_info);
  163. }
  164. type_init(lasi_82596_register_types)