cadence_gem.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. /*
  2. * QEMU Xilinx GEM emulation
  3. *
  4. * Copyright (c) 2011 Xilinx, Inc.
  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 <zlib.h> /* For crc32 */
  25. #include "sysbus.h"
  26. #include "net/net.h"
  27. #include "net/checksum.h"
  28. #ifdef CADENCE_GEM_ERR_DEBUG
  29. #define DB_PRINT(...) do { \
  30. fprintf(stderr, ": %s: ", __func__); \
  31. fprintf(stderr, ## __VA_ARGS__); \
  32. } while (0);
  33. #else
  34. #define DB_PRINT(...)
  35. #endif
  36. #define GEM_NWCTRL (0x00000000/4) /* Network Control reg */
  37. #define GEM_NWCFG (0x00000004/4) /* Network Config reg */
  38. #define GEM_NWSTATUS (0x00000008/4) /* Network Status reg */
  39. #define GEM_USERIO (0x0000000C/4) /* User IO reg */
  40. #define GEM_DMACFG (0x00000010/4) /* DMA Control reg */
  41. #define GEM_TXSTATUS (0x00000014/4) /* TX Status reg */
  42. #define GEM_RXQBASE (0x00000018/4) /* RX Q Base address reg */
  43. #define GEM_TXQBASE (0x0000001C/4) /* TX Q Base address reg */
  44. #define GEM_RXSTATUS (0x00000020/4) /* RX Status reg */
  45. #define GEM_ISR (0x00000024/4) /* Interrupt Status reg */
  46. #define GEM_IER (0x00000028/4) /* Interrupt Enable reg */
  47. #define GEM_IDR (0x0000002C/4) /* Interrupt Disable reg */
  48. #define GEM_IMR (0x00000030/4) /* Interrupt Mask reg */
  49. #define GEM_PHYMNTNC (0x00000034/4) /* Phy Maintaince reg */
  50. #define GEM_RXPAUSE (0x00000038/4) /* RX Pause Time reg */
  51. #define GEM_TXPAUSE (0x0000003C/4) /* TX Pause Time reg */
  52. #define GEM_TXPARTIALSF (0x00000040/4) /* TX Partial Store and Forward */
  53. #define GEM_RXPARTIALSF (0x00000044/4) /* RX Partial Store and Forward */
  54. #define GEM_HASHLO (0x00000080/4) /* Hash Low address reg */
  55. #define GEM_HASHHI (0x00000084/4) /* Hash High address reg */
  56. #define GEM_SPADDR1LO (0x00000088/4) /* Specific addr 1 low reg */
  57. #define GEM_SPADDR1HI (0x0000008C/4) /* Specific addr 1 high reg */
  58. #define GEM_SPADDR2LO (0x00000090/4) /* Specific addr 2 low reg */
  59. #define GEM_SPADDR2HI (0x00000094/4) /* Specific addr 2 high reg */
  60. #define GEM_SPADDR3LO (0x00000098/4) /* Specific addr 3 low reg */
  61. #define GEM_SPADDR3HI (0x0000009C/4) /* Specific addr 3 high reg */
  62. #define GEM_SPADDR4LO (0x000000A0/4) /* Specific addr 4 low reg */
  63. #define GEM_SPADDR4HI (0x000000A4/4) /* Specific addr 4 high reg */
  64. #define GEM_TIDMATCH1 (0x000000A8/4) /* Type ID1 Match reg */
  65. #define GEM_TIDMATCH2 (0x000000AC/4) /* Type ID2 Match reg */
  66. #define GEM_TIDMATCH3 (0x000000B0/4) /* Type ID3 Match reg */
  67. #define GEM_TIDMATCH4 (0x000000B4/4) /* Type ID4 Match reg */
  68. #define GEM_WOLAN (0x000000B8/4) /* Wake on LAN reg */
  69. #define GEM_IPGSTRETCH (0x000000BC/4) /* IPG Stretch reg */
  70. #define GEM_SVLAN (0x000000C0/4) /* Stacked VLAN reg */
  71. #define GEM_MODID (0x000000FC/4) /* Module ID reg */
  72. #define GEM_OCTTXLO (0x00000100/4) /* Octects transmitted Low reg */
  73. #define GEM_OCTTXHI (0x00000104/4) /* Octects transmitted High reg */
  74. #define GEM_TXCNT (0x00000108/4) /* Error-free Frames transmitted */
  75. #define GEM_TXBCNT (0x0000010C/4) /* Error-free Broadcast Frames */
  76. #define GEM_TXMCNT (0x00000110/4) /* Error-free Multicast Frame */
  77. #define GEM_TXPAUSECNT (0x00000114/4) /* Pause Frames Transmitted */
  78. #define GEM_TX64CNT (0x00000118/4) /* Error-free 64 TX */
  79. #define GEM_TX65CNT (0x0000011C/4) /* Error-free 65-127 TX */
  80. #define GEM_TX128CNT (0x00000120/4) /* Error-free 128-255 TX */
  81. #define GEM_TX256CNT (0x00000124/4) /* Error-free 256-511 */
  82. #define GEM_TX512CNT (0x00000128/4) /* Error-free 512-1023 TX */
  83. #define GEM_TX1024CNT (0x0000012C/4) /* Error-free 1024-1518 TX */
  84. #define GEM_TX1519CNT (0x00000130/4) /* Error-free larger than 1519 TX */
  85. #define GEM_TXURUNCNT (0x00000134/4) /* TX under run error counter */
  86. #define GEM_SINGLECOLLCNT (0x00000138/4) /* Single Collision Frames */
  87. #define GEM_MULTCOLLCNT (0x0000013C/4) /* Multiple Collision Frames */
  88. #define GEM_EXCESSCOLLCNT (0x00000140/4) /* Excessive Collision Frames */
  89. #define GEM_LATECOLLCNT (0x00000144/4) /* Late Collision Frames */
  90. #define GEM_DEFERTXCNT (0x00000148/4) /* Deferred Transmission Frames */
  91. #define GEM_CSENSECNT (0x0000014C/4) /* Carrier Sense Error Counter */
  92. #define GEM_OCTRXLO (0x00000150/4) /* Octects Received register Low */
  93. #define GEM_OCTRXHI (0x00000154/4) /* Octects Received register High */
  94. #define GEM_RXCNT (0x00000158/4) /* Error-free Frames Received */
  95. #define GEM_RXBROADCNT (0x0000015C/4) /* Error-free Broadcast Frames RX */
  96. #define GEM_RXMULTICNT (0x00000160/4) /* Error-free Multicast Frames RX */
  97. #define GEM_RXPAUSECNT (0x00000164/4) /* Pause Frames Received Counter */
  98. #define GEM_RX64CNT (0x00000168/4) /* Error-free 64 byte Frames RX */
  99. #define GEM_RX65CNT (0x0000016C/4) /* Error-free 65-127B Frames RX */
  100. #define GEM_RX128CNT (0x00000170/4) /* Error-free 128-255B Frames RX */
  101. #define GEM_RX256CNT (0x00000174/4) /* Error-free 256-512B Frames RX */
  102. #define GEM_RX512CNT (0x00000178/4) /* Error-free 512-1023B Frames RX */
  103. #define GEM_RX1024CNT (0x0000017C/4) /* Error-free 1024-1518B Frames RX */
  104. #define GEM_RX1519CNT (0x00000180/4) /* Error-free 1519-max Frames RX */
  105. #define GEM_RXUNDERCNT (0x00000184/4) /* Undersize Frames Received */
  106. #define GEM_RXOVERCNT (0x00000188/4) /* Oversize Frames Received */
  107. #define GEM_RXJABCNT (0x0000018C/4) /* Jabbers Received Counter */
  108. #define GEM_RXFCSCNT (0x00000190/4) /* Frame Check seq. Error Counter */
  109. #define GEM_RXLENERRCNT (0x00000194/4) /* Length Field Error Counter */
  110. #define GEM_RXSYMERRCNT (0x00000198/4) /* Symbol Error Counter */
  111. #define GEM_RXALIGNERRCNT (0x0000019C/4) /* Alignment Error Counter */
  112. #define GEM_RXRSCERRCNT (0x000001A0/4) /* Receive Resource Error Counter */
  113. #define GEM_RXORUNCNT (0x000001A4/4) /* Receive Overrun Counter */
  114. #define GEM_RXIPCSERRCNT (0x000001A8/4) /* IP header Checksum Error Counter */
  115. #define GEM_RXTCPCCNT (0x000001AC/4) /* TCP Checksum Error Counter */
  116. #define GEM_RXUDPCCNT (0x000001B0/4) /* UDP Checksum Error Counter */
  117. #define GEM_1588S (0x000001D0/4) /* 1588 Timer Seconds */
  118. #define GEM_1588NS (0x000001D4/4) /* 1588 Timer Nanoseconds */
  119. #define GEM_1588ADJ (0x000001D8/4) /* 1588 Timer Adjust */
  120. #define GEM_1588INC (0x000001DC/4) /* 1588 Timer Increment */
  121. #define GEM_PTPETXS (0x000001E0/4) /* PTP Event Frame Transmitted (s) */
  122. #define GEM_PTPETXNS (0x000001E4/4) /* PTP Event Frame Transmitted (ns) */
  123. #define GEM_PTPERXS (0x000001E8/4) /* PTP Event Frame Received (s) */
  124. #define GEM_PTPERXNS (0x000001EC/4) /* PTP Event Frame Received (ns) */
  125. #define GEM_PTPPTXS (0x000001E0/4) /* PTP Peer Frame Transmitted (s) */
  126. #define GEM_PTPPTXNS (0x000001E4/4) /* PTP Peer Frame Transmitted (ns) */
  127. #define GEM_PTPPRXS (0x000001E8/4) /* PTP Peer Frame Received (s) */
  128. #define GEM_PTPPRXNS (0x000001EC/4) /* PTP Peer Frame Received (ns) */
  129. /* Design Configuration Registers */
  130. #define GEM_DESCONF (0x00000280/4)
  131. #define GEM_DESCONF2 (0x00000284/4)
  132. #define GEM_DESCONF3 (0x00000288/4)
  133. #define GEM_DESCONF4 (0x0000028C/4)
  134. #define GEM_DESCONF5 (0x00000290/4)
  135. #define GEM_DESCONF6 (0x00000294/4)
  136. #define GEM_DESCONF7 (0x00000298/4)
  137. #define GEM_MAXREG (0x00000640/4) /* Last valid GEM address */
  138. /*****************************************/
  139. #define GEM_NWCTRL_TXSTART 0x00000200 /* Transmit Enable */
  140. #define GEM_NWCTRL_TXENA 0x00000008 /* Transmit Enable */
  141. #define GEM_NWCTRL_RXENA 0x00000004 /* Receive Enable */
  142. #define GEM_NWCTRL_LOCALLOOP 0x00000002 /* Local Loopback */
  143. #define GEM_NWCFG_STRIP_FCS 0x00020000 /* Strip FCS field */
  144. #define GEM_NWCFG_LERR_DISC 0x00010000 /* Discard RX frames with lenth err */
  145. #define GEM_NWCFG_BUFF_OFST_M 0x0000C000 /* Receive buffer offset mask */
  146. #define GEM_NWCFG_BUFF_OFST_S 14 /* Receive buffer offset shift */
  147. #define GEM_NWCFG_UCAST_HASH 0x00000080 /* accept unicast if hash match */
  148. #define GEM_NWCFG_MCAST_HASH 0x00000040 /* accept multicast if hash match */
  149. #define GEM_NWCFG_BCAST_REJ 0x00000020 /* Reject broadcast packets */
  150. #define GEM_NWCFG_PROMISC 0x00000010 /* Accept all packets */
  151. #define GEM_DMACFG_RBUFSZ_M 0x007F0000 /* DMA RX Buffer Size mask */
  152. #define GEM_DMACFG_RBUFSZ_S 16 /* DMA RX Buffer Size shift */
  153. #define GEM_DMACFG_RBUFSZ_MUL 64 /* DMA RX Buffer Size multiplier */
  154. #define GEM_DMACFG_TXCSUM_OFFL 0x00000800 /* Transmit checksum offload */
  155. #define GEM_TXSTATUS_TXCMPL 0x00000020 /* Transmit Complete */
  156. #define GEM_TXSTATUS_USED 0x00000001 /* sw owned descriptor encountered */
  157. #define GEM_RXSTATUS_FRMRCVD 0x00000002 /* Frame received */
  158. #define GEM_RXSTATUS_NOBUF 0x00000001 /* Buffer unavailable */
  159. /* GEM_ISR GEM_IER GEM_IDR GEM_IMR */
  160. #define GEM_INT_TXCMPL 0x00000080 /* Transmit Complete */
  161. #define GEM_INT_TXUSED 0x00000008
  162. #define GEM_INT_RXUSED 0x00000004
  163. #define GEM_INT_RXCMPL 0x00000002
  164. #define GEM_PHYMNTNC_OP_R 0x20000000 /* read operation */
  165. #define GEM_PHYMNTNC_OP_W 0x10000000 /* write operation */
  166. #define GEM_PHYMNTNC_ADDR 0x0F800000 /* Address bits */
  167. #define GEM_PHYMNTNC_ADDR_SHFT 23
  168. #define GEM_PHYMNTNC_REG 0x007C0000 /* register bits */
  169. #define GEM_PHYMNTNC_REG_SHIFT 18
  170. /* Marvell PHY definitions */
  171. #define BOARD_PHY_ADDRESS 23 /* PHY address we will emulate a device at */
  172. #define PHY_REG_CONTROL 0
  173. #define PHY_REG_STATUS 1
  174. #define PHY_REG_PHYID1 2
  175. #define PHY_REG_PHYID2 3
  176. #define PHY_REG_ANEGADV 4
  177. #define PHY_REG_LINKPABIL 5
  178. #define PHY_REG_ANEGEXP 6
  179. #define PHY_REG_NEXTP 7
  180. #define PHY_REG_LINKPNEXTP 8
  181. #define PHY_REG_100BTCTRL 9
  182. #define PHY_REG_1000BTSTAT 10
  183. #define PHY_REG_EXTSTAT 15
  184. #define PHY_REG_PHYSPCFC_CTL 16
  185. #define PHY_REG_PHYSPCFC_ST 17
  186. #define PHY_REG_INT_EN 18
  187. #define PHY_REG_INT_ST 19
  188. #define PHY_REG_EXT_PHYSPCFC_CTL 20
  189. #define PHY_REG_RXERR 21
  190. #define PHY_REG_EACD 22
  191. #define PHY_REG_LED 24
  192. #define PHY_REG_LED_OVRD 25
  193. #define PHY_REG_EXT_PHYSPCFC_CTL2 26
  194. #define PHY_REG_EXT_PHYSPCFC_ST 27
  195. #define PHY_REG_CABLE_DIAG 28
  196. #define PHY_REG_CONTROL_RST 0x8000
  197. #define PHY_REG_CONTROL_LOOP 0x4000
  198. #define PHY_REG_CONTROL_ANEG 0x1000
  199. #define PHY_REG_STATUS_LINK 0x0004
  200. #define PHY_REG_STATUS_ANEGCMPL 0x0020
  201. #define PHY_REG_INT_ST_ANEGCMPL 0x0800
  202. #define PHY_REG_INT_ST_LINKC 0x0400
  203. #define PHY_REG_INT_ST_ENERGY 0x0010
  204. /***********************************************************************/
  205. #define GEM_RX_REJECT 1
  206. #define GEM_RX_ACCEPT 0
  207. /***********************************************************************/
  208. #define DESC_1_USED 0x80000000
  209. #define DESC_1_LENGTH 0x00001FFF
  210. #define DESC_1_TX_WRAP 0x40000000
  211. #define DESC_1_TX_LAST 0x00008000
  212. #define DESC_0_RX_WRAP 0x00000002
  213. #define DESC_0_RX_OWNERSHIP 0x00000001
  214. #define DESC_1_RX_SOF 0x00004000
  215. #define DESC_1_RX_EOF 0x00008000
  216. static inline unsigned tx_desc_get_buffer(unsigned *desc)
  217. {
  218. return desc[0];
  219. }
  220. static inline unsigned tx_desc_get_used(unsigned *desc)
  221. {
  222. return (desc[1] & DESC_1_USED) ? 1 : 0;
  223. }
  224. static inline void tx_desc_set_used(unsigned *desc)
  225. {
  226. desc[1] |= DESC_1_USED;
  227. }
  228. static inline unsigned tx_desc_get_wrap(unsigned *desc)
  229. {
  230. return (desc[1] & DESC_1_TX_WRAP) ? 1 : 0;
  231. }
  232. static inline unsigned tx_desc_get_last(unsigned *desc)
  233. {
  234. return (desc[1] & DESC_1_TX_LAST) ? 1 : 0;
  235. }
  236. static inline unsigned tx_desc_get_length(unsigned *desc)
  237. {
  238. return desc[1] & DESC_1_LENGTH;
  239. }
  240. static inline void print_gem_tx_desc(unsigned *desc)
  241. {
  242. DB_PRINT("TXDESC:\n");
  243. DB_PRINT("bufaddr: 0x%08x\n", *desc);
  244. DB_PRINT("used_hw: %d\n", tx_desc_get_used(desc));
  245. DB_PRINT("wrap: %d\n", tx_desc_get_wrap(desc));
  246. DB_PRINT("last: %d\n", tx_desc_get_last(desc));
  247. DB_PRINT("length: %d\n", tx_desc_get_length(desc));
  248. }
  249. static inline unsigned rx_desc_get_buffer(unsigned *desc)
  250. {
  251. return desc[0] & ~0x3UL;
  252. }
  253. static inline unsigned rx_desc_get_wrap(unsigned *desc)
  254. {
  255. return desc[0] & DESC_0_RX_WRAP ? 1 : 0;
  256. }
  257. static inline unsigned rx_desc_get_ownership(unsigned *desc)
  258. {
  259. return desc[0] & DESC_0_RX_OWNERSHIP ? 1 : 0;
  260. }
  261. static inline void rx_desc_set_ownership(unsigned *desc)
  262. {
  263. desc[0] |= DESC_0_RX_OWNERSHIP;
  264. }
  265. static inline void rx_desc_set_sof(unsigned *desc)
  266. {
  267. desc[1] |= DESC_1_RX_SOF;
  268. }
  269. static inline void rx_desc_set_eof(unsigned *desc)
  270. {
  271. desc[1] |= DESC_1_RX_EOF;
  272. }
  273. static inline void rx_desc_set_length(unsigned *desc, unsigned len)
  274. {
  275. desc[1] &= ~DESC_1_LENGTH;
  276. desc[1] |= len;
  277. }
  278. typedef struct {
  279. SysBusDevice busdev;
  280. MemoryRegion iomem;
  281. NICState *nic;
  282. NICConf conf;
  283. qemu_irq irq;
  284. /* GEM registers backing store */
  285. uint32_t regs[GEM_MAXREG];
  286. /* Mask of register bits which are write only */
  287. uint32_t regs_wo[GEM_MAXREG];
  288. /* Mask of register bits which are read only */
  289. uint32_t regs_ro[GEM_MAXREG];
  290. /* Mask of register bits which are clear on read */
  291. uint32_t regs_rtc[GEM_MAXREG];
  292. /* Mask of register bits which are write 1 to clear */
  293. uint32_t regs_w1c[GEM_MAXREG];
  294. /* PHY registers backing store */
  295. uint16_t phy_regs[32];
  296. uint8_t phy_loop; /* Are we in phy loopback? */
  297. /* The current DMA descriptor pointers */
  298. uint32_t rx_desc_addr;
  299. uint32_t tx_desc_addr;
  300. } GemState;
  301. /* The broadcast MAC address: 0xFFFFFFFFFFFF */
  302. const uint8_t broadcast_addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  303. /*
  304. * gem_init_register_masks:
  305. * One time initialization.
  306. * Set masks to identify which register bits have magical clear properties
  307. */
  308. static void gem_init_register_masks(GemState *s)
  309. {
  310. /* Mask of register bits which are read only*/
  311. memset(&s->regs_ro[0], 0, sizeof(s->regs_ro));
  312. s->regs_ro[GEM_NWCTRL] = 0xFFF80000;
  313. s->regs_ro[GEM_NWSTATUS] = 0xFFFFFFFF;
  314. s->regs_ro[GEM_DMACFG] = 0xFE00F000;
  315. s->regs_ro[GEM_TXSTATUS] = 0xFFFFFE08;
  316. s->regs_ro[GEM_RXQBASE] = 0x00000003;
  317. s->regs_ro[GEM_TXQBASE] = 0x00000003;
  318. s->regs_ro[GEM_RXSTATUS] = 0xFFFFFFF0;
  319. s->regs_ro[GEM_ISR] = 0xFFFFFFFF;
  320. s->regs_ro[GEM_IMR] = 0xFFFFFFFF;
  321. s->regs_ro[GEM_MODID] = 0xFFFFFFFF;
  322. /* Mask of register bits which are clear on read */
  323. memset(&s->regs_rtc[0], 0, sizeof(s->regs_rtc));
  324. s->regs_rtc[GEM_ISR] = 0xFFFFFFFF;
  325. /* Mask of register bits which are write 1 to clear */
  326. memset(&s->regs_w1c[0], 0, sizeof(s->regs_w1c));
  327. s->regs_w1c[GEM_TXSTATUS] = 0x000001F7;
  328. s->regs_w1c[GEM_RXSTATUS] = 0x0000000F;
  329. /* Mask of register bits which are write only */
  330. memset(&s->regs_wo[0], 0, sizeof(s->regs_wo));
  331. s->regs_wo[GEM_NWCTRL] = 0x00073E60;
  332. s->regs_wo[GEM_IER] = 0x07FFFFFF;
  333. s->regs_wo[GEM_IDR] = 0x07FFFFFF;
  334. }
  335. /*
  336. * phy_update_link:
  337. * Make the emulated PHY link state match the QEMU "interface" state.
  338. */
  339. static void phy_update_link(GemState *s)
  340. {
  341. DB_PRINT("down %d\n", qemu_get_queue(s->nic)->link_down);
  342. /* Autonegotiation status mirrors link status. */
  343. if (qemu_get_queue(s->nic)->link_down) {
  344. s->phy_regs[PHY_REG_STATUS] &= ~(PHY_REG_STATUS_ANEGCMPL |
  345. PHY_REG_STATUS_LINK);
  346. s->phy_regs[PHY_REG_INT_ST] |= PHY_REG_INT_ST_LINKC;
  347. } else {
  348. s->phy_regs[PHY_REG_STATUS] |= (PHY_REG_STATUS_ANEGCMPL |
  349. PHY_REG_STATUS_LINK);
  350. s->phy_regs[PHY_REG_INT_ST] |= (PHY_REG_INT_ST_LINKC |
  351. PHY_REG_INT_ST_ANEGCMPL |
  352. PHY_REG_INT_ST_ENERGY);
  353. }
  354. }
  355. static int gem_can_receive(NetClientState *nc)
  356. {
  357. GemState *s;
  358. s = qemu_get_nic_opaque(nc);
  359. DB_PRINT("\n");
  360. /* Do nothing if receive is not enabled. */
  361. if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_RXENA)) {
  362. return 0;
  363. }
  364. return 1;
  365. }
  366. /*
  367. * gem_update_int_status:
  368. * Raise or lower interrupt based on current status.
  369. */
  370. static void gem_update_int_status(GemState *s)
  371. {
  372. uint32_t new_interrupts = 0;
  373. /* Packet transmitted ? */
  374. if (s->regs[GEM_TXSTATUS] & GEM_TXSTATUS_TXCMPL) {
  375. new_interrupts |= GEM_INT_TXCMPL;
  376. }
  377. /* End of TX ring ? */
  378. if (s->regs[GEM_TXSTATUS] & GEM_TXSTATUS_USED) {
  379. new_interrupts |= GEM_INT_TXUSED;
  380. }
  381. /* Frame received ? */
  382. if (s->regs[GEM_RXSTATUS] & GEM_RXSTATUS_FRMRCVD) {
  383. new_interrupts |= GEM_INT_RXCMPL;
  384. }
  385. /* RX ring full ? */
  386. if (s->regs[GEM_RXSTATUS] & GEM_RXSTATUS_NOBUF) {
  387. new_interrupts |= GEM_INT_RXUSED;
  388. }
  389. s->regs[GEM_ISR] |= new_interrupts & ~(s->regs[GEM_IMR]);
  390. if (s->regs[GEM_ISR]) {
  391. DB_PRINT("asserting int. (0x%08x)\n", s->regs[GEM_ISR]);
  392. qemu_set_irq(s->irq, 1);
  393. } else {
  394. qemu_set_irq(s->irq, 0);
  395. }
  396. }
  397. /*
  398. * gem_receive_updatestats:
  399. * Increment receive statistics.
  400. */
  401. static void gem_receive_updatestats(GemState *s, const uint8_t *packet,
  402. unsigned bytes)
  403. {
  404. uint64_t octets;
  405. /* Total octets (bytes) received */
  406. octets = ((uint64_t)(s->regs[GEM_OCTRXLO]) << 32) |
  407. s->regs[GEM_OCTRXHI];
  408. octets += bytes;
  409. s->regs[GEM_OCTRXLO] = octets >> 32;
  410. s->regs[GEM_OCTRXHI] = octets;
  411. /* Error-free Frames received */
  412. s->regs[GEM_RXCNT]++;
  413. /* Error-free Broadcast Frames counter */
  414. if (!memcmp(packet, broadcast_addr, 6)) {
  415. s->regs[GEM_RXBROADCNT]++;
  416. }
  417. /* Error-free Multicast Frames counter */
  418. if (packet[0] == 0x01) {
  419. s->regs[GEM_RXMULTICNT]++;
  420. }
  421. if (bytes <= 64) {
  422. s->regs[GEM_RX64CNT]++;
  423. } else if (bytes <= 127) {
  424. s->regs[GEM_RX65CNT]++;
  425. } else if (bytes <= 255) {
  426. s->regs[GEM_RX128CNT]++;
  427. } else if (bytes <= 511) {
  428. s->regs[GEM_RX256CNT]++;
  429. } else if (bytes <= 1023) {
  430. s->regs[GEM_RX512CNT]++;
  431. } else if (bytes <= 1518) {
  432. s->regs[GEM_RX1024CNT]++;
  433. } else {
  434. s->regs[GEM_RX1519CNT]++;
  435. }
  436. }
  437. /*
  438. * Get the MAC Address bit from the specified position
  439. */
  440. static unsigned get_bit(const uint8_t *mac, unsigned bit)
  441. {
  442. unsigned byte;
  443. byte = mac[bit / 8];
  444. byte >>= (bit & 0x7);
  445. byte &= 1;
  446. return byte;
  447. }
  448. /*
  449. * Calculate a GEM MAC Address hash index
  450. */
  451. static unsigned calc_mac_hash(const uint8_t *mac)
  452. {
  453. int index_bit, mac_bit;
  454. unsigned hash_index;
  455. hash_index = 0;
  456. mac_bit = 5;
  457. for (index_bit = 5; index_bit >= 0; index_bit--) {
  458. hash_index |= (get_bit(mac, mac_bit) ^
  459. get_bit(mac, mac_bit + 6) ^
  460. get_bit(mac, mac_bit + 12) ^
  461. get_bit(mac, mac_bit + 18) ^
  462. get_bit(mac, mac_bit + 24) ^
  463. get_bit(mac, mac_bit + 30) ^
  464. get_bit(mac, mac_bit + 36) ^
  465. get_bit(mac, mac_bit + 42)) << index_bit;
  466. mac_bit--;
  467. }
  468. return hash_index;
  469. }
  470. /*
  471. * gem_mac_address_filter:
  472. * Accept or reject this destination address?
  473. * Returns:
  474. * GEM_RX_REJECT: reject
  475. * GEM_RX_ACCEPT: accept
  476. */
  477. static int gem_mac_address_filter(GemState *s, const uint8_t *packet)
  478. {
  479. uint8_t *gem_spaddr;
  480. int i;
  481. /* Promiscuous mode? */
  482. if (s->regs[GEM_NWCFG] & GEM_NWCFG_PROMISC) {
  483. return GEM_RX_ACCEPT;
  484. }
  485. if (!memcmp(packet, broadcast_addr, 6)) {
  486. /* Reject broadcast packets? */
  487. if (s->regs[GEM_NWCFG] & GEM_NWCFG_BCAST_REJ) {
  488. return GEM_RX_REJECT;
  489. }
  490. return GEM_RX_ACCEPT;
  491. }
  492. /* Accept packets -w- hash match? */
  493. if ((packet[0] == 0x01 && (s->regs[GEM_NWCFG] & GEM_NWCFG_MCAST_HASH)) ||
  494. (packet[0] != 0x01 && (s->regs[GEM_NWCFG] & GEM_NWCFG_UCAST_HASH))) {
  495. unsigned hash_index;
  496. hash_index = calc_mac_hash(packet);
  497. if (hash_index < 32) {
  498. if (s->regs[GEM_HASHLO] & (1<<hash_index)) {
  499. return GEM_RX_ACCEPT;
  500. }
  501. } else {
  502. hash_index -= 32;
  503. if (s->regs[GEM_HASHHI] & (1<<hash_index)) {
  504. return GEM_RX_ACCEPT;
  505. }
  506. }
  507. }
  508. /* Check all 4 specific addresses */
  509. gem_spaddr = (uint8_t *)&(s->regs[GEM_SPADDR1LO]);
  510. for (i = 0; i < 4; i++) {
  511. if (!memcmp(packet, gem_spaddr, 6)) {
  512. return GEM_RX_ACCEPT;
  513. }
  514. gem_spaddr += 8;
  515. }
  516. /* No address match; reject the packet */
  517. return GEM_RX_REJECT;
  518. }
  519. /*
  520. * gem_receive:
  521. * Fit a packet handed to us by QEMU into the receive descriptor ring.
  522. */
  523. static ssize_t gem_receive(NetClientState *nc, const uint8_t *buf, size_t size)
  524. {
  525. unsigned desc[2];
  526. hwaddr packet_desc_addr, last_desc_addr;
  527. GemState *s;
  528. unsigned rxbufsize, bytes_to_copy;
  529. unsigned rxbuf_offset;
  530. uint8_t rxbuf[2048];
  531. uint8_t *rxbuf_ptr;
  532. s = qemu_get_nic_opaque(nc);
  533. /* Do nothing if receive is not enabled. */
  534. if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_RXENA)) {
  535. return -1;
  536. }
  537. /* Is this destination MAC address "for us" ? */
  538. if (gem_mac_address_filter(s, buf) == GEM_RX_REJECT) {
  539. return -1;
  540. }
  541. /* Discard packets with receive length error enabled ? */
  542. if (s->regs[GEM_NWCFG] & GEM_NWCFG_LERR_DISC) {
  543. unsigned type_len;
  544. /* Fish the ethertype / length field out of the RX packet */
  545. type_len = buf[12] << 8 | buf[13];
  546. /* It is a length field, not an ethertype */
  547. if (type_len < 0x600) {
  548. if (size < type_len) {
  549. /* discard */
  550. return -1;
  551. }
  552. }
  553. }
  554. /*
  555. * Determine configured receive buffer offset (probably 0)
  556. */
  557. rxbuf_offset = (s->regs[GEM_NWCFG] & GEM_NWCFG_BUFF_OFST_M) >>
  558. GEM_NWCFG_BUFF_OFST_S;
  559. /* The configure size of each receive buffer. Determines how many
  560. * buffers needed to hold this packet.
  561. */
  562. rxbufsize = ((s->regs[GEM_DMACFG] & GEM_DMACFG_RBUFSZ_M) >>
  563. GEM_DMACFG_RBUFSZ_S) * GEM_DMACFG_RBUFSZ_MUL;
  564. bytes_to_copy = size;
  565. /* Strip of FCS field ? (usually yes) */
  566. if (s->regs[GEM_NWCFG] & GEM_NWCFG_STRIP_FCS) {
  567. rxbuf_ptr = (void *)buf;
  568. } else {
  569. unsigned crc_val;
  570. int crc_offset;
  571. /* The application wants the FCS field, which QEMU does not provide.
  572. * We must try and caclculate one.
  573. */
  574. memcpy(rxbuf, buf, size);
  575. memset(rxbuf + size, 0, sizeof(rxbuf) - size);
  576. rxbuf_ptr = rxbuf;
  577. crc_val = cpu_to_le32(crc32(0, rxbuf, MAX(size, 60)));
  578. if (size < 60) {
  579. crc_offset = 60;
  580. } else {
  581. crc_offset = size;
  582. }
  583. memcpy(rxbuf + crc_offset, &crc_val, sizeof(crc_val));
  584. bytes_to_copy += 4;
  585. size += 4;
  586. }
  587. /* Pad to minimum length */
  588. if (size < 64) {
  589. size = 64;
  590. }
  591. DB_PRINT("config bufsize: %d packet size: %ld\n", rxbufsize, size);
  592. packet_desc_addr = s->rx_desc_addr;
  593. while (1) {
  594. DB_PRINT("read descriptor 0x%x\n", (unsigned)packet_desc_addr);
  595. /* read current descriptor */
  596. cpu_physical_memory_read(packet_desc_addr,
  597. (uint8_t *)&desc[0], sizeof(desc));
  598. /* Descriptor owned by software ? */
  599. if (rx_desc_get_ownership(desc) == 1) {
  600. DB_PRINT("descriptor 0x%x owned by sw.\n",
  601. (unsigned)packet_desc_addr);
  602. s->regs[GEM_RXSTATUS] |= GEM_RXSTATUS_NOBUF;
  603. /* Handle interrupt consequences */
  604. gem_update_int_status(s);
  605. return -1;
  606. }
  607. DB_PRINT("copy %d bytes to 0x%x\n", MIN(bytes_to_copy, rxbufsize),
  608. rx_desc_get_buffer(desc));
  609. /*
  610. * Let's have QEMU lend a helping hand.
  611. */
  612. if (rx_desc_get_buffer(desc) == 0) {
  613. DB_PRINT("Invalid RX buffer (NULL) for descriptor 0x%x\n",
  614. (unsigned)packet_desc_addr);
  615. break;
  616. }
  617. /* Copy packet data to emulated DMA buffer */
  618. cpu_physical_memory_write(rx_desc_get_buffer(desc) + rxbuf_offset,
  619. rxbuf_ptr, MIN(bytes_to_copy, rxbufsize));
  620. bytes_to_copy -= MIN(bytes_to_copy, rxbufsize);
  621. rxbuf_ptr += MIN(bytes_to_copy, rxbufsize);
  622. if (bytes_to_copy == 0) {
  623. break;
  624. }
  625. /* Next descriptor */
  626. if (rx_desc_get_wrap(desc)) {
  627. packet_desc_addr = s->regs[GEM_RXQBASE];
  628. } else {
  629. packet_desc_addr += 8;
  630. }
  631. }
  632. DB_PRINT("set length: %ld, EOF on descriptor 0x%x\n", size,
  633. (unsigned)packet_desc_addr);
  634. /* Update last descriptor with EOF and total length */
  635. rx_desc_set_eof(desc);
  636. rx_desc_set_length(desc, size);
  637. cpu_physical_memory_write(packet_desc_addr,
  638. (uint8_t *)&desc[0], sizeof(desc));
  639. /* Advance RX packet descriptor Q */
  640. last_desc_addr = packet_desc_addr;
  641. packet_desc_addr = s->rx_desc_addr;
  642. s->rx_desc_addr = last_desc_addr;
  643. if (rx_desc_get_wrap(desc)) {
  644. s->rx_desc_addr = s->regs[GEM_RXQBASE];
  645. } else {
  646. s->rx_desc_addr += 8;
  647. }
  648. DB_PRINT("set SOF, OWN on descriptor 0x%08x\n", (unsigned)packet_desc_addr);
  649. /* Count it */
  650. gem_receive_updatestats(s, buf, size);
  651. /* Update first descriptor (which could also be the last) */
  652. /* read descriptor */
  653. cpu_physical_memory_read(packet_desc_addr,
  654. (uint8_t *)&desc[0], sizeof(desc));
  655. rx_desc_set_sof(desc);
  656. rx_desc_set_ownership(desc);
  657. cpu_physical_memory_write(packet_desc_addr,
  658. (uint8_t *)&desc[0], sizeof(desc));
  659. s->regs[GEM_RXSTATUS] |= GEM_RXSTATUS_FRMRCVD;
  660. /* Handle interrupt consequences */
  661. gem_update_int_status(s);
  662. return size;
  663. }
  664. /*
  665. * gem_transmit_updatestats:
  666. * Increment transmit statistics.
  667. */
  668. static void gem_transmit_updatestats(GemState *s, const uint8_t *packet,
  669. unsigned bytes)
  670. {
  671. uint64_t octets;
  672. /* Total octets (bytes) transmitted */
  673. octets = ((uint64_t)(s->regs[GEM_OCTTXLO]) << 32) |
  674. s->regs[GEM_OCTTXHI];
  675. octets += bytes;
  676. s->regs[GEM_OCTTXLO] = octets >> 32;
  677. s->regs[GEM_OCTTXHI] = octets;
  678. /* Error-free Frames transmitted */
  679. s->regs[GEM_TXCNT]++;
  680. /* Error-free Broadcast Frames counter */
  681. if (!memcmp(packet, broadcast_addr, 6)) {
  682. s->regs[GEM_TXBCNT]++;
  683. }
  684. /* Error-free Multicast Frames counter */
  685. if (packet[0] == 0x01) {
  686. s->regs[GEM_TXMCNT]++;
  687. }
  688. if (bytes <= 64) {
  689. s->regs[GEM_TX64CNT]++;
  690. } else if (bytes <= 127) {
  691. s->regs[GEM_TX65CNT]++;
  692. } else if (bytes <= 255) {
  693. s->regs[GEM_TX128CNT]++;
  694. } else if (bytes <= 511) {
  695. s->regs[GEM_TX256CNT]++;
  696. } else if (bytes <= 1023) {
  697. s->regs[GEM_TX512CNT]++;
  698. } else if (bytes <= 1518) {
  699. s->regs[GEM_TX1024CNT]++;
  700. } else {
  701. s->regs[GEM_TX1519CNT]++;
  702. }
  703. }
  704. /*
  705. * gem_transmit:
  706. * Fish packets out of the descriptor ring and feed them to QEMU
  707. */
  708. static void gem_transmit(GemState *s)
  709. {
  710. unsigned desc[2];
  711. hwaddr packet_desc_addr;
  712. uint8_t tx_packet[2048];
  713. uint8_t *p;
  714. unsigned total_bytes;
  715. /* Do nothing if transmit is not enabled. */
  716. if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_TXENA)) {
  717. return;
  718. }
  719. DB_PRINT("\n");
  720. /* The packet we will hand off to qemu.
  721. * Packets scattered across multiple descriptors are gathered to this
  722. * one contiguous buffer first.
  723. */
  724. p = tx_packet;
  725. total_bytes = 0;
  726. /* read current descriptor */
  727. packet_desc_addr = s->tx_desc_addr;
  728. cpu_physical_memory_read(packet_desc_addr,
  729. (uint8_t *)&desc[0], sizeof(desc));
  730. /* Handle all descriptors owned by hardware */
  731. while (tx_desc_get_used(desc) == 0) {
  732. /* Do nothing if transmit is not enabled. */
  733. if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_TXENA)) {
  734. return;
  735. }
  736. print_gem_tx_desc(desc);
  737. /* The real hardware would eat this (and possibly crash).
  738. * For QEMU let's lend a helping hand.
  739. */
  740. if ((tx_desc_get_buffer(desc) == 0) ||
  741. (tx_desc_get_length(desc) == 0)) {
  742. DB_PRINT("Invalid TX descriptor @ 0x%x\n",
  743. (unsigned)packet_desc_addr);
  744. break;
  745. }
  746. /* Gather this fragment of the packet from "dma memory" to our contig.
  747. * buffer.
  748. */
  749. cpu_physical_memory_read(tx_desc_get_buffer(desc), p,
  750. tx_desc_get_length(desc));
  751. p += tx_desc_get_length(desc);
  752. total_bytes += tx_desc_get_length(desc);
  753. /* Last descriptor for this packet; hand the whole thing off */
  754. if (tx_desc_get_last(desc)) {
  755. /* Modify the 1st descriptor of this packet to be owned by
  756. * the processor.
  757. */
  758. cpu_physical_memory_read(s->tx_desc_addr,
  759. (uint8_t *)&desc[0], sizeof(desc));
  760. tx_desc_set_used(desc);
  761. cpu_physical_memory_write(s->tx_desc_addr,
  762. (uint8_t *)&desc[0], sizeof(desc));
  763. /* Advance the hardare current descriptor past this packet */
  764. if (tx_desc_get_wrap(desc)) {
  765. s->tx_desc_addr = s->regs[GEM_TXQBASE];
  766. } else {
  767. s->tx_desc_addr = packet_desc_addr + 8;
  768. }
  769. DB_PRINT("TX descriptor next: 0x%08x\n", s->tx_desc_addr);
  770. s->regs[GEM_TXSTATUS] |= GEM_TXSTATUS_TXCMPL;
  771. /* Handle interrupt consequences */
  772. gem_update_int_status(s);
  773. /* Is checksum offload enabled? */
  774. if (s->regs[GEM_DMACFG] & GEM_DMACFG_TXCSUM_OFFL) {
  775. net_checksum_calculate(tx_packet, total_bytes);
  776. }
  777. /* Update MAC statistics */
  778. gem_transmit_updatestats(s, tx_packet, total_bytes);
  779. /* Send the packet somewhere */
  780. if (s->phy_loop) {
  781. gem_receive(qemu_get_queue(s->nic), tx_packet, total_bytes);
  782. } else {
  783. qemu_send_packet(qemu_get_queue(s->nic), tx_packet,
  784. total_bytes);
  785. }
  786. /* Prepare for next packet */
  787. p = tx_packet;
  788. total_bytes = 0;
  789. }
  790. /* read next descriptor */
  791. if (tx_desc_get_wrap(desc)) {
  792. packet_desc_addr = s->regs[GEM_TXQBASE];
  793. } else {
  794. packet_desc_addr += 8;
  795. }
  796. cpu_physical_memory_read(packet_desc_addr,
  797. (uint8_t *)&desc[0], sizeof(desc));
  798. }
  799. if (tx_desc_get_used(desc)) {
  800. s->regs[GEM_TXSTATUS] |= GEM_TXSTATUS_USED;
  801. gem_update_int_status(s);
  802. }
  803. }
  804. static void gem_phy_reset(GemState *s)
  805. {
  806. memset(&s->phy_regs[0], 0, sizeof(s->phy_regs));
  807. s->phy_regs[PHY_REG_CONTROL] = 0x1140;
  808. s->phy_regs[PHY_REG_STATUS] = 0x7969;
  809. s->phy_regs[PHY_REG_PHYID1] = 0x0141;
  810. s->phy_regs[PHY_REG_PHYID2] = 0x0CC2;
  811. s->phy_regs[PHY_REG_ANEGADV] = 0x01E1;
  812. s->phy_regs[PHY_REG_LINKPABIL] = 0xCDE1;
  813. s->phy_regs[PHY_REG_ANEGEXP] = 0x000F;
  814. s->phy_regs[PHY_REG_NEXTP] = 0x2001;
  815. s->phy_regs[PHY_REG_LINKPNEXTP] = 0x40E6;
  816. s->phy_regs[PHY_REG_100BTCTRL] = 0x0300;
  817. s->phy_regs[PHY_REG_1000BTSTAT] = 0x7C00;
  818. s->phy_regs[PHY_REG_EXTSTAT] = 0x3000;
  819. s->phy_regs[PHY_REG_PHYSPCFC_CTL] = 0x0078;
  820. s->phy_regs[PHY_REG_PHYSPCFC_ST] = 0xBC00;
  821. s->phy_regs[PHY_REG_EXT_PHYSPCFC_CTL] = 0x0C60;
  822. s->phy_regs[PHY_REG_LED] = 0x4100;
  823. s->phy_regs[PHY_REG_EXT_PHYSPCFC_CTL2] = 0x000A;
  824. s->phy_regs[PHY_REG_EXT_PHYSPCFC_ST] = 0x848B;
  825. phy_update_link(s);
  826. }
  827. static void gem_reset(DeviceState *d)
  828. {
  829. GemState *s = FROM_SYSBUS(GemState, SYS_BUS_DEVICE(d));
  830. DB_PRINT("\n");
  831. /* Set post reset register values */
  832. memset(&s->regs[0], 0, sizeof(s->regs));
  833. s->regs[GEM_NWCFG] = 0x00080000;
  834. s->regs[GEM_NWSTATUS] = 0x00000006;
  835. s->regs[GEM_DMACFG] = 0x00020784;
  836. s->regs[GEM_IMR] = 0x07ffffff;
  837. s->regs[GEM_TXPAUSE] = 0x0000ffff;
  838. s->regs[GEM_TXPARTIALSF] = 0x000003ff;
  839. s->regs[GEM_RXPARTIALSF] = 0x000003ff;
  840. s->regs[GEM_MODID] = 0x00020118;
  841. s->regs[GEM_DESCONF] = 0x02500111;
  842. s->regs[GEM_DESCONF2] = 0x2ab13fff;
  843. s->regs[GEM_DESCONF5] = 0x002f2145;
  844. s->regs[GEM_DESCONF6] = 0x00000200;
  845. gem_phy_reset(s);
  846. gem_update_int_status(s);
  847. }
  848. static uint16_t gem_phy_read(GemState *s, unsigned reg_num)
  849. {
  850. DB_PRINT("reg: %d value: 0x%04x\n", reg_num, s->phy_regs[reg_num]);
  851. return s->phy_regs[reg_num];
  852. }
  853. static void gem_phy_write(GemState *s, unsigned reg_num, uint16_t val)
  854. {
  855. DB_PRINT("reg: %d value: 0x%04x\n", reg_num, val);
  856. switch (reg_num) {
  857. case PHY_REG_CONTROL:
  858. if (val & PHY_REG_CONTROL_RST) {
  859. /* Phy reset */
  860. gem_phy_reset(s);
  861. val &= ~(PHY_REG_CONTROL_RST | PHY_REG_CONTROL_LOOP);
  862. s->phy_loop = 0;
  863. }
  864. if (val & PHY_REG_CONTROL_ANEG) {
  865. /* Complete autonegotiation immediately */
  866. val &= ~PHY_REG_CONTROL_ANEG;
  867. s->phy_regs[PHY_REG_STATUS] |= PHY_REG_STATUS_ANEGCMPL;
  868. }
  869. if (val & PHY_REG_CONTROL_LOOP) {
  870. DB_PRINT("PHY placed in loopback\n");
  871. s->phy_loop = 1;
  872. } else {
  873. s->phy_loop = 0;
  874. }
  875. break;
  876. }
  877. s->phy_regs[reg_num] = val;
  878. }
  879. /*
  880. * gem_read32:
  881. * Read a GEM register.
  882. */
  883. static uint64_t gem_read(void *opaque, hwaddr offset, unsigned size)
  884. {
  885. GemState *s;
  886. uint32_t retval;
  887. s = (GemState *)opaque;
  888. offset >>= 2;
  889. retval = s->regs[offset];
  890. DB_PRINT("offset: 0x%04x read: 0x%08x\n", (unsigned)offset*4, retval);
  891. switch (offset) {
  892. case GEM_ISR:
  893. DB_PRINT("lowering irq on ISR read\n");
  894. qemu_set_irq(s->irq, 0);
  895. break;
  896. case GEM_PHYMNTNC:
  897. if (retval & GEM_PHYMNTNC_OP_R) {
  898. uint32_t phy_addr, reg_num;
  899. phy_addr = (retval & GEM_PHYMNTNC_ADDR) >> GEM_PHYMNTNC_ADDR_SHFT;
  900. if (phy_addr == BOARD_PHY_ADDRESS) {
  901. reg_num = (retval & GEM_PHYMNTNC_REG) >> GEM_PHYMNTNC_REG_SHIFT;
  902. retval &= 0xFFFF0000;
  903. retval |= gem_phy_read(s, reg_num);
  904. } else {
  905. retval |= 0xFFFF; /* No device at this address */
  906. }
  907. }
  908. break;
  909. }
  910. /* Squash read to clear bits */
  911. s->regs[offset] &= ~(s->regs_rtc[offset]);
  912. /* Do not provide write only bits */
  913. retval &= ~(s->regs_wo[offset]);
  914. DB_PRINT("0x%08x\n", retval);
  915. return retval;
  916. }
  917. /*
  918. * gem_write32:
  919. * Write a GEM register.
  920. */
  921. static void gem_write(void *opaque, hwaddr offset, uint64_t val,
  922. unsigned size)
  923. {
  924. GemState *s = (GemState *)opaque;
  925. uint32_t readonly;
  926. DB_PRINT("offset: 0x%04x write: 0x%08x ", (unsigned)offset, (unsigned)val);
  927. offset >>= 2;
  928. /* Squash bits which are read only in write value */
  929. val &= ~(s->regs_ro[offset]);
  930. /* Preserve (only) bits which are read only in register */
  931. readonly = s->regs[offset];
  932. readonly &= s->regs_ro[offset];
  933. /* Squash bits which are write 1 to clear */
  934. val &= ~(s->regs_w1c[offset] & val);
  935. /* Copy register write to backing store */
  936. s->regs[offset] = val | readonly;
  937. /* Handle register write side effects */
  938. switch (offset) {
  939. case GEM_NWCTRL:
  940. if (val & GEM_NWCTRL_TXSTART) {
  941. gem_transmit(s);
  942. }
  943. if (!(val & GEM_NWCTRL_TXENA)) {
  944. /* Reset to start of Q when transmit disabled. */
  945. s->tx_desc_addr = s->regs[GEM_TXQBASE];
  946. }
  947. if (!(val & GEM_NWCTRL_RXENA)) {
  948. /* Reset to start of Q when receive disabled. */
  949. s->rx_desc_addr = s->regs[GEM_RXQBASE];
  950. }
  951. break;
  952. case GEM_TXSTATUS:
  953. gem_update_int_status(s);
  954. break;
  955. case GEM_RXQBASE:
  956. s->rx_desc_addr = val;
  957. break;
  958. case GEM_TXQBASE:
  959. s->tx_desc_addr = val;
  960. break;
  961. case GEM_RXSTATUS:
  962. gem_update_int_status(s);
  963. break;
  964. case GEM_IER:
  965. s->regs[GEM_IMR] &= ~val;
  966. gem_update_int_status(s);
  967. break;
  968. case GEM_IDR:
  969. s->regs[GEM_IMR] |= val;
  970. gem_update_int_status(s);
  971. break;
  972. case GEM_PHYMNTNC:
  973. if (val & GEM_PHYMNTNC_OP_W) {
  974. uint32_t phy_addr, reg_num;
  975. phy_addr = (val & GEM_PHYMNTNC_ADDR) >> GEM_PHYMNTNC_ADDR_SHFT;
  976. if (phy_addr == BOARD_PHY_ADDRESS) {
  977. reg_num = (val & GEM_PHYMNTNC_REG) >> GEM_PHYMNTNC_REG_SHIFT;
  978. gem_phy_write(s, reg_num, val);
  979. }
  980. }
  981. break;
  982. }
  983. DB_PRINT("newval: 0x%08x\n", s->regs[offset]);
  984. }
  985. static const MemoryRegionOps gem_ops = {
  986. .read = gem_read,
  987. .write = gem_write,
  988. .endianness = DEVICE_LITTLE_ENDIAN,
  989. };
  990. static void gem_cleanup(NetClientState *nc)
  991. {
  992. GemState *s = qemu_get_nic_opaque(nc);
  993. DB_PRINT("\n");
  994. s->nic = NULL;
  995. }
  996. static void gem_set_link(NetClientState *nc)
  997. {
  998. DB_PRINT("\n");
  999. phy_update_link(qemu_get_nic_opaque(nc));
  1000. }
  1001. static NetClientInfo net_gem_info = {
  1002. .type = NET_CLIENT_OPTIONS_KIND_NIC,
  1003. .size = sizeof(NICState),
  1004. .can_receive = gem_can_receive,
  1005. .receive = gem_receive,
  1006. .cleanup = gem_cleanup,
  1007. .link_status_changed = gem_set_link,
  1008. };
  1009. static int gem_init(SysBusDevice *dev)
  1010. {
  1011. GemState *s;
  1012. DB_PRINT("\n");
  1013. s = FROM_SYSBUS(GemState, dev);
  1014. gem_init_register_masks(s);
  1015. memory_region_init_io(&s->iomem, &gem_ops, s, "enet", sizeof(s->regs));
  1016. sysbus_init_mmio(dev, &s->iomem);
  1017. sysbus_init_irq(dev, &s->irq);
  1018. qemu_macaddr_default_if_unset(&s->conf.macaddr);
  1019. s->nic = qemu_new_nic(&net_gem_info, &s->conf,
  1020. object_get_typename(OBJECT(dev)), dev->qdev.id, s);
  1021. return 0;
  1022. }
  1023. static const VMStateDescription vmstate_cadence_gem = {
  1024. .name = "cadence_gem",
  1025. .version_id = 1,
  1026. .minimum_version_id = 1,
  1027. .minimum_version_id_old = 1,
  1028. .fields = (VMStateField[]) {
  1029. VMSTATE_UINT32_ARRAY(regs, GemState, GEM_MAXREG),
  1030. VMSTATE_UINT16_ARRAY(phy_regs, GemState, 32),
  1031. VMSTATE_UINT8(phy_loop, GemState),
  1032. VMSTATE_UINT32(rx_desc_addr, GemState),
  1033. VMSTATE_UINT32(tx_desc_addr, GemState),
  1034. }
  1035. };
  1036. static Property gem_properties[] = {
  1037. DEFINE_NIC_PROPERTIES(GemState, conf),
  1038. DEFINE_PROP_END_OF_LIST(),
  1039. };
  1040. static void gem_class_init(ObjectClass *klass, void *data)
  1041. {
  1042. DeviceClass *dc = DEVICE_CLASS(klass);
  1043. SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
  1044. sdc->init = gem_init;
  1045. dc->props = gem_properties;
  1046. dc->vmsd = &vmstate_cadence_gem;
  1047. dc->reset = gem_reset;
  1048. }
  1049. static const TypeInfo gem_info = {
  1050. .class_init = gem_class_init,
  1051. .name = "cadence_gem",
  1052. .parent = TYPE_SYS_BUS_DEVICE,
  1053. .instance_size = sizeof(GemState),
  1054. };
  1055. static void gem_register_types(void)
  1056. {
  1057. type_register_static(&gem_info);
  1058. }
  1059. type_init(gem_register_types)