xilinx_ethlite.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * QEMU model of the Xilinx Ethernet Lite MAC.
  3. *
  4. * Copyright (c) 2009 Edgar E. Iglesias.
  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. #include "sysbus.h"
  25. #include "hw.h"
  26. #include "net.h"
  27. #define D(x)
  28. #define R_TX_BUF0 0
  29. #define R_TX_LEN0 (0x07f4 / 4)
  30. #define R_TX_GIE0 (0x07f8 / 4)
  31. #define R_TX_CTRL0 (0x07fc / 4)
  32. #define R_TX_BUF1 (0x0800 / 4)
  33. #define R_TX_LEN1 (0x0ff4 / 4)
  34. #define R_TX_CTRL1 (0x0ffc / 4)
  35. #define R_RX_BUF0 (0x1000 / 4)
  36. #define R_RX_CTRL0 (0x17fc / 4)
  37. #define R_RX_BUF1 (0x1800 / 4)
  38. #define R_RX_CTRL1 (0x1ffc / 4)
  39. #define R_MAX (0x2000 / 4)
  40. #define GIE_GIE 0x80000000
  41. #define CTRL_I 0x8
  42. #define CTRL_P 0x2
  43. #define CTRL_S 0x1
  44. struct xlx_ethlite
  45. {
  46. SysBusDevice busdev;
  47. MemoryRegion mmio;
  48. qemu_irq irq;
  49. NICState *nic;
  50. NICConf conf;
  51. uint32_t c_tx_pingpong;
  52. uint32_t c_rx_pingpong;
  53. unsigned int txbuf;
  54. unsigned int rxbuf;
  55. uint32_t regs[R_MAX];
  56. };
  57. static inline void eth_pulse_irq(struct xlx_ethlite *s)
  58. {
  59. /* Only the first gie reg is active. */
  60. if (s->regs[R_TX_GIE0] & GIE_GIE) {
  61. qemu_irq_pulse(s->irq);
  62. }
  63. }
  64. static uint64_t
  65. eth_read(void *opaque, target_phys_addr_t addr, unsigned int size)
  66. {
  67. struct xlx_ethlite *s = opaque;
  68. uint32_t r = 0;
  69. addr >>= 2;
  70. switch (addr)
  71. {
  72. case R_TX_GIE0:
  73. case R_TX_LEN0:
  74. case R_TX_LEN1:
  75. case R_TX_CTRL1:
  76. case R_TX_CTRL0:
  77. case R_RX_CTRL1:
  78. case R_RX_CTRL0:
  79. r = s->regs[addr];
  80. D(qemu_log("%s %x=%x\n", __func__, addr * 4, r));
  81. break;
  82. default:
  83. r = tswap32(s->regs[addr]);
  84. break;
  85. }
  86. return r;
  87. }
  88. static void
  89. eth_write(void *opaque, target_phys_addr_t addr,
  90. uint64_t val64, unsigned int size)
  91. {
  92. struct xlx_ethlite *s = opaque;
  93. unsigned int base = 0;
  94. uint32_t value = val64;
  95. addr >>= 2;
  96. switch (addr)
  97. {
  98. case R_TX_CTRL0:
  99. case R_TX_CTRL1:
  100. if (addr == R_TX_CTRL1)
  101. base = 0x800 / 4;
  102. D(qemu_log("%s addr=%x val=%x\n", __func__, addr * 4, value));
  103. if ((value & (CTRL_P | CTRL_S)) == CTRL_S) {
  104. qemu_send_packet(&s->nic->nc,
  105. (void *) &s->regs[base],
  106. s->regs[base + R_TX_LEN0]);
  107. D(qemu_log("eth_tx %d\n", s->regs[base + R_TX_LEN0]));
  108. if (s->regs[base + R_TX_CTRL0] & CTRL_I)
  109. eth_pulse_irq(s);
  110. } else if ((value & (CTRL_P | CTRL_S)) == (CTRL_P | CTRL_S)) {
  111. memcpy(&s->conf.macaddr.a[0], &s->regs[base], 6);
  112. if (s->regs[base + R_TX_CTRL0] & CTRL_I)
  113. eth_pulse_irq(s);
  114. }
  115. /* We are fast and get ready pretty much immediately so
  116. we actually never flip the S nor P bits to one. */
  117. s->regs[addr] = value & ~(CTRL_P | CTRL_S);
  118. break;
  119. /* Keep these native. */
  120. case R_TX_LEN0:
  121. case R_TX_LEN1:
  122. case R_TX_GIE0:
  123. case R_RX_CTRL0:
  124. case R_RX_CTRL1:
  125. D(qemu_log("%s addr=%x val=%x\n", __func__, addr * 4, value));
  126. s->regs[addr] = value;
  127. break;
  128. default:
  129. s->regs[addr] = tswap32(value);
  130. break;
  131. }
  132. }
  133. static const MemoryRegionOps eth_ops = {
  134. .read = eth_read,
  135. .write = eth_write,
  136. .endianness = DEVICE_NATIVE_ENDIAN,
  137. .valid = {
  138. .min_access_size = 4,
  139. .max_access_size = 4
  140. }
  141. };
  142. static int eth_can_rx(VLANClientState *nc)
  143. {
  144. struct xlx_ethlite *s = DO_UPCAST(NICState, nc, nc)->opaque;
  145. int r;
  146. r = !(s->regs[R_RX_CTRL0] & CTRL_S);
  147. return r;
  148. }
  149. static ssize_t eth_rx(VLANClientState *nc, const uint8_t *buf, size_t size)
  150. {
  151. struct xlx_ethlite *s = DO_UPCAST(NICState, nc, nc)->opaque;
  152. unsigned int rxbase = s->rxbuf * (0x800 / 4);
  153. /* DA filter. */
  154. if (!(buf[0] & 0x80) && memcmp(&s->conf.macaddr.a[0], buf, 6))
  155. return size;
  156. if (s->regs[rxbase + R_RX_CTRL0] & CTRL_S) {
  157. D(qemu_log("ethlite lost packet %x\n", s->regs[R_RX_CTRL0]));
  158. return -1;
  159. }
  160. D(qemu_log("%s %d rxbase=%x\n", __func__, size, rxbase));
  161. memcpy(&s->regs[rxbase + R_RX_BUF0], buf, size);
  162. s->regs[rxbase + R_RX_CTRL0] |= CTRL_S;
  163. if (s->regs[rxbase + R_RX_CTRL0] & CTRL_I)
  164. eth_pulse_irq(s);
  165. /* If c_rx_pingpong was set flip buffers. */
  166. s->rxbuf ^= s->c_rx_pingpong;
  167. return size;
  168. }
  169. static void eth_cleanup(VLANClientState *nc)
  170. {
  171. struct xlx_ethlite *s = DO_UPCAST(NICState, nc, nc)->opaque;
  172. s->nic = NULL;
  173. }
  174. static NetClientInfo net_xilinx_ethlite_info = {
  175. .type = NET_CLIENT_TYPE_NIC,
  176. .size = sizeof(NICState),
  177. .can_receive = eth_can_rx,
  178. .receive = eth_rx,
  179. .cleanup = eth_cleanup,
  180. };
  181. static int xilinx_ethlite_init(SysBusDevice *dev)
  182. {
  183. struct xlx_ethlite *s = FROM_SYSBUS(typeof (*s), dev);
  184. sysbus_init_irq(dev, &s->irq);
  185. s->rxbuf = 0;
  186. memory_region_init_io(&s->mmio, &eth_ops, s, "xilinx-ethlite", R_MAX * 4);
  187. sysbus_init_mmio_region(dev, &s->mmio);
  188. qemu_macaddr_default_if_unset(&s->conf.macaddr);
  189. s->nic = qemu_new_nic(&net_xilinx_ethlite_info, &s->conf,
  190. dev->qdev.info->name, dev->qdev.id, s);
  191. qemu_format_nic_info_str(&s->nic->nc, s->conf.macaddr.a);
  192. return 0;
  193. }
  194. static SysBusDeviceInfo xilinx_ethlite_info = {
  195. .init = xilinx_ethlite_init,
  196. .qdev.name = "xilinx,ethlite",
  197. .qdev.size = sizeof(struct xlx_ethlite),
  198. .qdev.props = (Property[]) {
  199. DEFINE_PROP_UINT32("txpingpong", struct xlx_ethlite, c_tx_pingpong, 1),
  200. DEFINE_PROP_UINT32("rxpingpong", struct xlx_ethlite, c_rx_pingpong, 1),
  201. DEFINE_NIC_PROPERTIES(struct xlx_ethlite, conf),
  202. DEFINE_PROP_END_OF_LIST(),
  203. }
  204. };
  205. static void xilinx_ethlite_register(void)
  206. {
  207. sysbus_register_withprop(&xilinx_ethlite_info);
  208. }
  209. device_init(xilinx_ethlite_register)