pcnet-pci.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * QEMU AMD PC-Net II (Am79C970A) PCI 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. #include "pci.h"
  29. #include "net.h"
  30. #include "loader.h"
  31. #include "qemu-timer.h"
  32. #include "pcnet.h"
  33. //#define PCNET_DEBUG
  34. //#define PCNET_DEBUG_IO
  35. //#define PCNET_DEBUG_BCR
  36. //#define PCNET_DEBUG_CSR
  37. //#define PCNET_DEBUG_RMD
  38. //#define PCNET_DEBUG_TMD
  39. //#define PCNET_DEBUG_MATCH
  40. typedef struct {
  41. PCIDevice pci_dev;
  42. PCNetState state;
  43. } PCIPCNetState;
  44. static void pcnet_aprom_writeb(void *opaque, uint32_t addr, uint32_t val)
  45. {
  46. PCNetState *s = opaque;
  47. #ifdef PCNET_DEBUG
  48. printf("pcnet_aprom_writeb addr=0x%08x val=0x%02x\n", addr, val);
  49. #endif
  50. /* Check APROMWE bit to enable write access */
  51. if (pcnet_bcr_readw(s,2) & 0x100)
  52. s->prom[addr & 15] = val;
  53. }
  54. static uint32_t pcnet_aprom_readb(void *opaque, uint32_t addr)
  55. {
  56. PCNetState *s = opaque;
  57. uint32_t val = s->prom[addr & 15];
  58. #ifdef PCNET_DEBUG
  59. printf("pcnet_aprom_readb addr=0x%08x val=0x%02x\n", addr, val);
  60. #endif
  61. return val;
  62. }
  63. static void pcnet_ioport_map(PCIDevice *pci_dev, int region_num,
  64. pcibus_t addr, pcibus_t size, int type)
  65. {
  66. PCNetState *d = &DO_UPCAST(PCIPCNetState, pci_dev, pci_dev)->state;
  67. #ifdef PCNET_DEBUG_IO
  68. printf("pcnet_ioport_map addr=0x%04"FMT_PCIBUS" size=0x%04"FMT_PCIBUS"\n",
  69. addr, size);
  70. #endif
  71. register_ioport_write(addr, 16, 1, pcnet_aprom_writeb, d);
  72. register_ioport_read(addr, 16, 1, pcnet_aprom_readb, d);
  73. register_ioport_write(addr + 0x10, 0x10, 2, pcnet_ioport_writew, d);
  74. register_ioport_read(addr + 0x10, 0x10, 2, pcnet_ioport_readw, d);
  75. register_ioport_write(addr + 0x10, 0x10, 4, pcnet_ioport_writel, d);
  76. register_ioport_read(addr + 0x10, 0x10, 4, pcnet_ioport_readl, d);
  77. }
  78. static void pcnet_mmio_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
  79. {
  80. PCNetState *d = opaque;
  81. #ifdef PCNET_DEBUG_IO
  82. printf("pcnet_mmio_writeb addr=0x" TARGET_FMT_plx" val=0x%02x\n", addr,
  83. val);
  84. #endif
  85. if (!(addr & 0x10))
  86. pcnet_aprom_writeb(d, addr & 0x0f, val);
  87. }
  88. static uint32_t pcnet_mmio_readb(void *opaque, target_phys_addr_t addr)
  89. {
  90. PCNetState *d = opaque;
  91. uint32_t val = -1;
  92. if (!(addr & 0x10))
  93. val = pcnet_aprom_readb(d, addr & 0x0f);
  94. #ifdef PCNET_DEBUG_IO
  95. printf("pcnet_mmio_readb addr=0x" TARGET_FMT_plx " val=0x%02x\n", addr,
  96. val & 0xff);
  97. #endif
  98. return val;
  99. }
  100. static void pcnet_mmio_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
  101. {
  102. PCNetState *d = opaque;
  103. #ifdef PCNET_DEBUG_IO
  104. printf("pcnet_mmio_writew addr=0x" TARGET_FMT_plx " val=0x%04x\n", addr,
  105. val);
  106. #endif
  107. if (addr & 0x10)
  108. pcnet_ioport_writew(d, addr & 0x0f, val);
  109. else {
  110. addr &= 0x0f;
  111. pcnet_aprom_writeb(d, addr, val & 0xff);
  112. pcnet_aprom_writeb(d, addr+1, (val & 0xff00) >> 8);
  113. }
  114. }
  115. static uint32_t pcnet_mmio_readw(void *opaque, target_phys_addr_t addr)
  116. {
  117. PCNetState *d = opaque;
  118. uint32_t val = -1;
  119. if (addr & 0x10)
  120. val = pcnet_ioport_readw(d, addr & 0x0f);
  121. else {
  122. addr &= 0x0f;
  123. val = pcnet_aprom_readb(d, addr+1);
  124. val <<= 8;
  125. val |= pcnet_aprom_readb(d, addr);
  126. }
  127. #ifdef PCNET_DEBUG_IO
  128. printf("pcnet_mmio_readw addr=0x" TARGET_FMT_plx" val = 0x%04x\n", addr,
  129. val & 0xffff);
  130. #endif
  131. return val;
  132. }
  133. static void pcnet_mmio_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
  134. {
  135. PCNetState *d = opaque;
  136. #ifdef PCNET_DEBUG_IO
  137. printf("pcnet_mmio_writel addr=0x" TARGET_FMT_plx" val=0x%08x\n", addr,
  138. val);
  139. #endif
  140. if (addr & 0x10)
  141. pcnet_ioport_writel(d, addr & 0x0f, val);
  142. else {
  143. addr &= 0x0f;
  144. pcnet_aprom_writeb(d, addr, val & 0xff);
  145. pcnet_aprom_writeb(d, addr+1, (val & 0xff00) >> 8);
  146. pcnet_aprom_writeb(d, addr+2, (val & 0xff0000) >> 16);
  147. pcnet_aprom_writeb(d, addr+3, (val & 0xff000000) >> 24);
  148. }
  149. }
  150. static uint32_t pcnet_mmio_readl(void *opaque, target_phys_addr_t addr)
  151. {
  152. PCNetState *d = opaque;
  153. uint32_t val;
  154. if (addr & 0x10)
  155. val = pcnet_ioport_readl(d, addr & 0x0f);
  156. else {
  157. addr &= 0x0f;
  158. val = pcnet_aprom_readb(d, addr+3);
  159. val <<= 8;
  160. val |= pcnet_aprom_readb(d, addr+2);
  161. val <<= 8;
  162. val |= pcnet_aprom_readb(d, addr+1);
  163. val <<= 8;
  164. val |= pcnet_aprom_readb(d, addr);
  165. }
  166. #ifdef PCNET_DEBUG_IO
  167. printf("pcnet_mmio_readl addr=0x" TARGET_FMT_plx " val=0x%08x\n", addr,
  168. val);
  169. #endif
  170. return val;
  171. }
  172. static const VMStateDescription vmstate_pci_pcnet = {
  173. .name = "pcnet",
  174. .version_id = 3,
  175. .minimum_version_id = 2,
  176. .minimum_version_id_old = 2,
  177. .fields = (VMStateField []) {
  178. VMSTATE_PCI_DEVICE(pci_dev, PCIPCNetState),
  179. VMSTATE_STRUCT(state, PCIPCNetState, 0, vmstate_pcnet, PCNetState),
  180. VMSTATE_END_OF_LIST()
  181. }
  182. };
  183. /* PCI interface */
  184. static CPUWriteMemoryFunc * const pcnet_mmio_write[] = {
  185. &pcnet_mmio_writeb,
  186. &pcnet_mmio_writew,
  187. &pcnet_mmio_writel
  188. };
  189. static CPUReadMemoryFunc * const pcnet_mmio_read[] = {
  190. &pcnet_mmio_readb,
  191. &pcnet_mmio_readw,
  192. &pcnet_mmio_readl
  193. };
  194. static void pci_physical_memory_write(void *dma_opaque, target_phys_addr_t addr,
  195. uint8_t *buf, int len, int do_bswap)
  196. {
  197. cpu_physical_memory_write(addr, buf, len);
  198. }
  199. static void pci_physical_memory_read(void *dma_opaque, target_phys_addr_t addr,
  200. uint8_t *buf, int len, int do_bswap)
  201. {
  202. cpu_physical_memory_read(addr, buf, len);
  203. }
  204. static void pci_pcnet_cleanup(VLANClientState *nc)
  205. {
  206. PCNetState *d = DO_UPCAST(NICState, nc, nc)->opaque;
  207. pcnet_common_cleanup(d);
  208. }
  209. static int pci_pcnet_uninit(PCIDevice *dev)
  210. {
  211. PCIPCNetState *d = DO_UPCAST(PCIPCNetState, pci_dev, dev);
  212. cpu_unregister_io_memory(d->state.mmio_index);
  213. qemu_del_timer(d->state.poll_timer);
  214. qemu_free_timer(d->state.poll_timer);
  215. qemu_del_vlan_client(&d->state.nic->nc);
  216. return 0;
  217. }
  218. static NetClientInfo net_pci_pcnet_info = {
  219. .type = NET_CLIENT_TYPE_NIC,
  220. .size = sizeof(NICState),
  221. .can_receive = pcnet_can_receive,
  222. .receive = pcnet_receive,
  223. .cleanup = pci_pcnet_cleanup,
  224. };
  225. static int pci_pcnet_init(PCIDevice *pci_dev)
  226. {
  227. PCIPCNetState *d = DO_UPCAST(PCIPCNetState, pci_dev, pci_dev);
  228. PCNetState *s = &d->state;
  229. uint8_t *pci_conf;
  230. #if 0
  231. printf("sizeof(RMD)=%d, sizeof(TMD)=%d\n",
  232. sizeof(struct pcnet_RMD), sizeof(struct pcnet_TMD));
  233. #endif
  234. pci_conf = pci_dev->config;
  235. pci_set_word(pci_conf + PCI_STATUS,
  236. PCI_STATUS_FAST_BACK | PCI_STATUS_DEVSEL_MEDIUM);
  237. pci_set_word(pci_conf + PCI_SUBSYSTEM_VENDOR_ID, 0x0);
  238. pci_set_word(pci_conf + PCI_SUBSYSTEM_ID, 0x0);
  239. pci_conf[PCI_INTERRUPT_PIN] = 1; // interrupt pin 0
  240. pci_conf[PCI_MIN_GNT] = 0x06;
  241. pci_conf[PCI_MAX_LAT] = 0xff;
  242. /* Handler for memory-mapped I/O */
  243. s->mmio_index =
  244. cpu_register_io_memory(pcnet_mmio_read, pcnet_mmio_write, &d->state,
  245. DEVICE_NATIVE_ENDIAN);
  246. pci_register_bar(pci_dev, 0, PCNET_IOPORT_SIZE,
  247. PCI_BASE_ADDRESS_SPACE_IO, pcnet_ioport_map);
  248. pci_register_bar_simple(pci_dev, 1, PCNET_PNPMMIO_SIZE, 0, s->mmio_index);
  249. s->irq = pci_dev->irq[0];
  250. s->phys_mem_read = pci_physical_memory_read;
  251. s->phys_mem_write = pci_physical_memory_write;
  252. if (!pci_dev->qdev.hotplugged) {
  253. static int loaded = 0;
  254. if (!loaded) {
  255. rom_add_option("pxe-pcnet.rom", -1);
  256. loaded = 1;
  257. }
  258. }
  259. return pcnet_common_init(&pci_dev->qdev, s, &net_pci_pcnet_info);
  260. }
  261. static void pci_reset(DeviceState *dev)
  262. {
  263. PCIPCNetState *d = DO_UPCAST(PCIPCNetState, pci_dev.qdev, dev);
  264. pcnet_h_reset(&d->state);
  265. }
  266. static PCIDeviceInfo pcnet_info = {
  267. .qdev.name = "pcnet",
  268. .qdev.size = sizeof(PCIPCNetState),
  269. .qdev.reset = pci_reset,
  270. .qdev.vmsd = &vmstate_pci_pcnet,
  271. .init = pci_pcnet_init,
  272. .exit = pci_pcnet_uninit,
  273. .vendor_id = PCI_VENDOR_ID_AMD,
  274. .device_id = PCI_DEVICE_ID_AMD_LANCE,
  275. .revision = 0x10,
  276. .class_id = PCI_CLASS_NETWORK_ETHERNET,
  277. .qdev.props = (Property[]) {
  278. DEFINE_NIC_PROPERTIES(PCIPCNetState, state.conf),
  279. DEFINE_PROP_END_OF_LIST(),
  280. }
  281. };
  282. static void pci_pcnet_register_devices(void)
  283. {
  284. pci_qdev_register(&pcnet_info);
  285. }
  286. device_init(pci_pcnet_register_devices)