cadence_gem.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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.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", s->nic->nc.link_down);
  342. /* Autonegotiation status mirrors link status. */
  343. if (s->nic->nc.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 = DO_UPCAST(NICState, nc, nc)->opaque;
  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. target_phys_addr_t 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 = DO_UPCAST(NICState, nc, nc)->opaque;
  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", 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", packet_desc_addr);
  601. s->regs[GEM_RXSTATUS] |= GEM_RXSTATUS_NOBUF;
  602. /* Handle interrupt consequences */
  603. gem_update_int_status(s);
  604. return -1;
  605. }
  606. DB_PRINT("copy %d bytes to 0x%x\n", MIN(bytes_to_copy, rxbufsize),
  607. rx_desc_get_buffer(desc));
  608. /*
  609. * Let's have QEMU lend a helping hand.
  610. */
  611. if (rx_desc_get_buffer(desc) == 0) {
  612. DB_PRINT("Invalid RX buffer (NULL) for descriptor 0x%x\n",
  613. packet_desc_addr);
  614. break;
  615. }
  616. /* Copy packet data to emulated DMA buffer */
  617. cpu_physical_memory_write(rx_desc_get_buffer(desc) + rxbuf_offset,
  618. rxbuf_ptr, MIN(bytes_to_copy, rxbufsize));
  619. bytes_to_copy -= MIN(bytes_to_copy, rxbufsize);
  620. rxbuf_ptr += MIN(bytes_to_copy, rxbufsize);
  621. if (bytes_to_copy == 0) {
  622. break;
  623. }
  624. /* Next descriptor */
  625. if (rx_desc_get_wrap(desc)) {
  626. packet_desc_addr = s->regs[GEM_RXQBASE];
  627. } else {
  628. packet_desc_addr += 8;
  629. }
  630. }
  631. DB_PRINT("set length: %ld, EOF on descriptor 0x%x\n", size,
  632. (unsigned)packet_desc_addr);
  633. /* Update last descriptor with EOF and total length */
  634. rx_desc_set_eof(desc);
  635. rx_desc_set_length(desc, size);
  636. cpu_physical_memory_write(packet_desc_addr,
  637. (uint8_t *)&desc[0], sizeof(desc));
  638. /* Advance RX packet descriptor Q */
  639. last_desc_addr = packet_desc_addr;
  640. packet_desc_addr = s->rx_desc_addr;
  641. s->rx_desc_addr = last_desc_addr;
  642. if (rx_desc_get_wrap(desc)) {
  643. s->rx_desc_addr = s->regs[GEM_RXQBASE];
  644. } else {
  645. s->rx_desc_addr += 8;
  646. }
  647. DB_PRINT("set SOF, OWN on descriptor 0x%08x\n", packet_desc_addr);
  648. /* Count it */
  649. gem_receive_updatestats(s, buf, size);
  650. /* Update first descriptor (which could also be the last) */
  651. /* read descriptor */
  652. cpu_physical_memory_read(packet_desc_addr,
  653. (uint8_t *)&desc[0], sizeof(desc));
  654. rx_desc_set_sof(desc);
  655. rx_desc_set_ownership(desc);
  656. cpu_physical_memory_write(packet_desc_addr,
  657. (uint8_t *)&desc[0], sizeof(desc));
  658. s->regs[GEM_RXSTATUS] |= GEM_RXSTATUS_FRMRCVD;
  659. /* Handle interrupt consequences */
  660. gem_update_int_status(s);
  661. return size;
  662. }
  663. /*
  664. * gem_transmit_updatestats:
  665. * Increment transmit statistics.
  666. */
  667. static void gem_transmit_updatestats(GemState *s, const uint8_t *packet,
  668. unsigned bytes)
  669. {
  670. uint64_t octets;
  671. /* Total octets (bytes) transmitted */
  672. octets = ((uint64_t)(s->regs[GEM_OCTTXLO]) << 32) |
  673. s->regs[GEM_OCTTXHI];
  674. octets += bytes;
  675. s->regs[GEM_OCTTXLO] = octets >> 32;
  676. s->regs[GEM_OCTTXHI] = octets;
  677. /* Error-free Frames transmitted */
  678. s->regs[GEM_TXCNT]++;
  679. /* Error-free Broadcast Frames counter */
  680. if (!memcmp(packet, broadcast_addr, 6)) {
  681. s->regs[GEM_TXBCNT]++;
  682. }
  683. /* Error-free Multicast Frames counter */
  684. if (packet[0] == 0x01) {
  685. s->regs[GEM_TXMCNT]++;
  686. }
  687. if (bytes <= 64) {
  688. s->regs[GEM_TX64CNT]++;
  689. } else if (bytes <= 127) {
  690. s->regs[GEM_TX65CNT]++;
  691. } else if (bytes <= 255) {
  692. s->regs[GEM_TX128CNT]++;
  693. } else if (bytes <= 511) {
  694. s->regs[GEM_TX256CNT]++;
  695. } else if (bytes <= 1023) {
  696. s->regs[GEM_TX512CNT]++;
  697. } else if (bytes <= 1518) {
  698. s->regs[GEM_TX1024CNT]++;
  699. } else {
  700. s->regs[GEM_TX1519CNT]++;
  701. }
  702. }
  703. /*
  704. * gem_transmit:
  705. * Fish packets out of the descriptor ring and feed them to QEMU
  706. */
  707. static void gem_transmit(GemState *s)
  708. {
  709. unsigned desc[2];
  710. target_phys_addr_t packet_desc_addr;
  711. uint8_t tx_packet[2048];
  712. uint8_t *p;
  713. unsigned total_bytes;
  714. /* Do nothing if transmit is not enabled. */
  715. if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_TXENA)) {
  716. return;
  717. }
  718. DB_PRINT("\n");
  719. /* The packet we will hand off to qemu.
  720. * Packets scattered across multiple descriptors are gathered to this
  721. * one contiguous buffer first.
  722. */
  723. p = tx_packet;
  724. total_bytes = 0;
  725. /* read current descriptor */
  726. packet_desc_addr = s->tx_desc_addr;
  727. cpu_physical_memory_read(packet_desc_addr,
  728. (uint8_t *)&desc[0], sizeof(desc));
  729. /* Handle all descriptors owned by hardware */
  730. while (tx_desc_get_used(desc) == 0) {
  731. /* Do nothing if transmit is not enabled. */
  732. if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_TXENA)) {
  733. return;
  734. }
  735. print_gem_tx_desc(desc);
  736. /* The real hardware would eat this (and possibly crash).
  737. * For QEMU let's lend a helping hand.
  738. */
  739. if ((tx_desc_get_buffer(desc) == 0) ||
  740. (tx_desc_get_length(desc) == 0)) {
  741. DB_PRINT("Invalid TX descriptor @ 0x%x\n", packet_desc_addr);
  742. break;
  743. }
  744. /* Gather this fragment of the packet from "dma memory" to our contig.
  745. * buffer.
  746. */
  747. cpu_physical_memory_read(tx_desc_get_buffer(desc), p,
  748. tx_desc_get_length(desc));
  749. p += tx_desc_get_length(desc);
  750. total_bytes += tx_desc_get_length(desc);
  751. /* Last descriptor for this packet; hand the whole thing off */
  752. if (tx_desc_get_last(desc)) {
  753. /* Modify the 1st descriptor of this packet to be owned by
  754. * the processor.
  755. */
  756. cpu_physical_memory_read(s->tx_desc_addr,
  757. (uint8_t *)&desc[0], sizeof(desc));
  758. tx_desc_set_used(desc);
  759. cpu_physical_memory_write(s->tx_desc_addr,
  760. (uint8_t *)&desc[0], sizeof(desc));
  761. /* Advance the hardare current descriptor past this packet */
  762. if (tx_desc_get_wrap(desc)) {
  763. s->tx_desc_addr = s->regs[GEM_TXQBASE];
  764. } else {
  765. s->tx_desc_addr = packet_desc_addr + 8;
  766. }
  767. DB_PRINT("TX descriptor next: 0x%08x\n", s->tx_desc_addr);
  768. s->regs[GEM_TXSTATUS] |= GEM_TXSTATUS_TXCMPL;
  769. /* Handle interrupt consequences */
  770. gem_update_int_status(s);
  771. /* Is checksum offload enabled? */
  772. if (s->regs[GEM_DMACFG] & GEM_DMACFG_TXCSUM_OFFL) {
  773. net_checksum_calculate(tx_packet, total_bytes);
  774. }
  775. /* Update MAC statistics */
  776. gem_transmit_updatestats(s, tx_packet, total_bytes);
  777. /* Send the packet somewhere */
  778. if (s->phy_loop) {
  779. gem_receive(&s->nic->nc, tx_packet, total_bytes);
  780. } else {
  781. qemu_send_packet(&s->nic->nc, tx_packet, total_bytes);
  782. }
  783. /* Prepare for next packet */
  784. p = tx_packet;
  785. total_bytes = 0;
  786. }
  787. /* read next descriptor */
  788. if (tx_desc_get_wrap(desc)) {
  789. packet_desc_addr = s->regs[GEM_TXQBASE];
  790. } else {
  791. packet_desc_addr += 8;
  792. }
  793. cpu_physical_memory_read(packet_desc_addr,
  794. (uint8_t *)&desc[0], sizeof(desc));
  795. }
  796. if (tx_desc_get_used(desc)) {
  797. s->regs[GEM_TXSTATUS] |= GEM_TXSTATUS_USED;
  798. gem_update_int_status(s);
  799. }
  800. }
  801. static void gem_phy_reset(GemState *s)
  802. {
  803. memset(&s->phy_regs[0], 0, sizeof(s->phy_regs));
  804. s->phy_regs[PHY_REG_CONTROL] = 0x1140;
  805. s->phy_regs[PHY_REG_STATUS] = 0x7969;
  806. s->phy_regs[PHY_REG_PHYID1] = 0x0141;
  807. s->phy_regs[PHY_REG_PHYID2] = 0x0CC2;
  808. s->phy_regs[PHY_REG_ANEGADV] = 0x01E1;
  809. s->phy_regs[PHY_REG_LINKPABIL] = 0xCDE1;
  810. s->phy_regs[PHY_REG_ANEGEXP] = 0x000F;
  811. s->phy_regs[PHY_REG_NEXTP] = 0x2001;
  812. s->phy_regs[PHY_REG_LINKPNEXTP] = 0x40E6;
  813. s->phy_regs[PHY_REG_100BTCTRL] = 0x0300;
  814. s->phy_regs[PHY_REG_1000BTSTAT] = 0x7C00;
  815. s->phy_regs[PHY_REG_EXTSTAT] = 0x3000;
  816. s->phy_regs[PHY_REG_PHYSPCFC_CTL] = 0x0078;
  817. s->phy_regs[PHY_REG_PHYSPCFC_ST] = 0xBC00;
  818. s->phy_regs[PHY_REG_EXT_PHYSPCFC_CTL] = 0x0C60;
  819. s->phy_regs[PHY_REG_LED] = 0x4100;
  820. s->phy_regs[PHY_REG_EXT_PHYSPCFC_CTL2] = 0x000A;
  821. s->phy_regs[PHY_REG_EXT_PHYSPCFC_ST] = 0x848B;
  822. phy_update_link(s);
  823. }
  824. static void gem_reset(DeviceState *d)
  825. {
  826. GemState *s = FROM_SYSBUS(GemState, sysbus_from_qdev(d));
  827. DB_PRINT("\n");
  828. /* Set post reset register values */
  829. memset(&s->regs[0], 0, sizeof(s->regs));
  830. s->regs[GEM_NWCFG] = 0x00080000;
  831. s->regs[GEM_NWSTATUS] = 0x00000006;
  832. s->regs[GEM_DMACFG] = 0x00020784;
  833. s->regs[GEM_IMR] = 0x07ffffff;
  834. s->regs[GEM_TXPAUSE] = 0x0000ffff;
  835. s->regs[GEM_TXPARTIALSF] = 0x000003ff;
  836. s->regs[GEM_RXPARTIALSF] = 0x000003ff;
  837. s->regs[GEM_MODID] = 0x00020118;
  838. s->regs[GEM_DESCONF] = 0x02500111;
  839. s->regs[GEM_DESCONF2] = 0x2ab13fff;
  840. s->regs[GEM_DESCONF5] = 0x002f2145;
  841. s->regs[GEM_DESCONF6] = 0x00000200;
  842. gem_phy_reset(s);
  843. gem_update_int_status(s);
  844. }
  845. static uint16_t gem_phy_read(GemState *s, unsigned reg_num)
  846. {
  847. DB_PRINT("reg: %d value: 0x%04x\n", reg_num, s->phy_regs[reg_num]);
  848. return s->phy_regs[reg_num];
  849. }
  850. static void gem_phy_write(GemState *s, unsigned reg_num, uint16_t val)
  851. {
  852. DB_PRINT("reg: %d value: 0x%04x\n", reg_num, val);
  853. switch (reg_num) {
  854. case PHY_REG_CONTROL:
  855. if (val & PHY_REG_CONTROL_RST) {
  856. /* Phy reset */
  857. gem_phy_reset(s);
  858. val &= ~(PHY_REG_CONTROL_RST | PHY_REG_CONTROL_LOOP);
  859. s->phy_loop = 0;
  860. }
  861. if (val & PHY_REG_CONTROL_ANEG) {
  862. /* Complete autonegotiation immediately */
  863. val &= ~PHY_REG_CONTROL_ANEG;
  864. s->phy_regs[PHY_REG_STATUS] |= PHY_REG_STATUS_ANEGCMPL;
  865. }
  866. if (val & PHY_REG_CONTROL_LOOP) {
  867. DB_PRINT("PHY placed in loopback\n");
  868. s->phy_loop = 1;
  869. } else {
  870. s->phy_loop = 0;
  871. }
  872. break;
  873. }
  874. s->phy_regs[reg_num] = val;
  875. }
  876. /*
  877. * gem_read32:
  878. * Read a GEM register.
  879. */
  880. static uint64_t gem_read(void *opaque, target_phys_addr_t offset, unsigned size)
  881. {
  882. GemState *s;
  883. uint32_t retval;
  884. s = (GemState *)opaque;
  885. offset >>= 2;
  886. retval = s->regs[offset];
  887. DB_PRINT("offset: 0x%04x read: 0x%08x\n", offset*4, retval);
  888. switch (offset) {
  889. case GEM_ISR:
  890. qemu_set_irq(s->irq, 0);
  891. break;
  892. case GEM_PHYMNTNC:
  893. if (retval & GEM_PHYMNTNC_OP_R) {
  894. uint32_t phy_addr, reg_num;
  895. phy_addr = (retval & GEM_PHYMNTNC_ADDR) >> GEM_PHYMNTNC_ADDR_SHFT;
  896. if (phy_addr == BOARD_PHY_ADDRESS) {
  897. reg_num = (retval & GEM_PHYMNTNC_REG) >> GEM_PHYMNTNC_REG_SHIFT;
  898. retval &= 0xFFFF0000;
  899. retval |= gem_phy_read(s, reg_num);
  900. } else {
  901. retval |= 0xFFFF; /* No device at this address */
  902. }
  903. }
  904. break;
  905. }
  906. /* Squash read to clear bits */
  907. s->regs[offset] &= ~(s->regs_rtc[offset]);
  908. /* Do not provide write only bits */
  909. retval &= ~(s->regs_wo[offset]);
  910. DB_PRINT("0x%08x\n", retval);
  911. return retval;
  912. }
  913. /*
  914. * gem_write32:
  915. * Write a GEM register.
  916. */
  917. static void gem_write(void *opaque, target_phys_addr_t offset, uint64_t val,
  918. unsigned size)
  919. {
  920. GemState *s = (GemState *)opaque;
  921. uint32_t readonly;
  922. DB_PRINT("offset: 0x%04x write: 0x%08x ", offset, (unsigned)val);
  923. offset >>= 2;
  924. /* Squash bits which are read only in write value */
  925. val &= ~(s->regs_ro[offset]);
  926. /* Preserve (only) bits which are read only in register */
  927. readonly = s->regs[offset];
  928. readonly &= s->regs_ro[offset];
  929. /* Squash bits which are write 1 to clear */
  930. val &= ~(s->regs_w1c[offset] & val);
  931. /* Copy register write to backing store */
  932. s->regs[offset] = val | readonly;
  933. /* Handle register write side effects */
  934. switch (offset) {
  935. case GEM_NWCTRL:
  936. if (val & GEM_NWCTRL_TXSTART) {
  937. gem_transmit(s);
  938. }
  939. if (!(val & GEM_NWCTRL_TXENA)) {
  940. /* Reset to start of Q when transmit disabled. */
  941. s->tx_desc_addr = s->regs[GEM_TXQBASE];
  942. }
  943. if (!(val & GEM_NWCTRL_RXENA)) {
  944. /* Reset to start of Q when receive disabled. */
  945. s->rx_desc_addr = s->regs[GEM_RXQBASE];
  946. }
  947. break;
  948. case GEM_TXSTATUS:
  949. gem_update_int_status(s);
  950. break;
  951. case GEM_RXQBASE:
  952. s->rx_desc_addr = val;
  953. break;
  954. case GEM_TXQBASE:
  955. s->tx_desc_addr = val;
  956. break;
  957. case GEM_RXSTATUS:
  958. gem_update_int_status(s);
  959. break;
  960. case GEM_IER:
  961. s->regs[GEM_IMR] &= ~val;
  962. gem_update_int_status(s);
  963. break;
  964. case GEM_IDR:
  965. s->regs[GEM_IMR] |= val;
  966. gem_update_int_status(s);
  967. break;
  968. case GEM_PHYMNTNC:
  969. if (val & GEM_PHYMNTNC_OP_W) {
  970. uint32_t phy_addr, reg_num;
  971. phy_addr = (val & GEM_PHYMNTNC_ADDR) >> GEM_PHYMNTNC_ADDR_SHFT;
  972. if (phy_addr == BOARD_PHY_ADDRESS) {
  973. reg_num = (val & GEM_PHYMNTNC_REG) >> GEM_PHYMNTNC_REG_SHIFT;
  974. gem_phy_write(s, reg_num, val);
  975. }
  976. }
  977. break;
  978. }
  979. DB_PRINT("newval: 0x%08x\n", s->regs[offset]);
  980. }
  981. static const MemoryRegionOps gem_ops = {
  982. .read = gem_read,
  983. .write = gem_write,
  984. .endianness = DEVICE_LITTLE_ENDIAN,
  985. };
  986. static void gem_cleanup(NetClientState *nc)
  987. {
  988. GemState *s = DO_UPCAST(NICState, nc, nc)->opaque;
  989. DB_PRINT("\n");
  990. s->nic = NULL;
  991. }
  992. static void gem_set_link(NetClientState *nc)
  993. {
  994. DB_PRINT("\n");
  995. phy_update_link(DO_UPCAST(NICState, nc, nc)->opaque);
  996. }
  997. static NetClientInfo net_gem_info = {
  998. .type = NET_CLIENT_OPTIONS_KIND_NIC,
  999. .size = sizeof(NICState),
  1000. .can_receive = gem_can_receive,
  1001. .receive = gem_receive,
  1002. .cleanup = gem_cleanup,
  1003. .link_status_changed = gem_set_link,
  1004. };
  1005. static int gem_init(SysBusDevice *dev)
  1006. {
  1007. GemState *s;
  1008. DB_PRINT("\n");
  1009. s = FROM_SYSBUS(GemState, dev);
  1010. gem_init_register_masks(s);
  1011. memory_region_init_io(&s->iomem, &gem_ops, s, "enet", sizeof(s->regs));
  1012. sysbus_init_mmio(dev, &s->iomem);
  1013. sysbus_init_irq(dev, &s->irq);
  1014. qemu_macaddr_default_if_unset(&s->conf.macaddr);
  1015. s->nic = qemu_new_nic(&net_gem_info, &s->conf,
  1016. object_get_typename(OBJECT(dev)), dev->qdev.id, s);
  1017. return 0;
  1018. }
  1019. static const VMStateDescription vmstate_cadence_gem = {
  1020. .name = "cadence_gem",
  1021. .version_id = 1,
  1022. .minimum_version_id = 1,
  1023. .minimum_version_id_old = 1,
  1024. .fields = (VMStateField[]) {
  1025. VMSTATE_UINT32_ARRAY(regs, GemState, GEM_MAXREG),
  1026. VMSTATE_UINT16_ARRAY(phy_regs, GemState, 32),
  1027. VMSTATE_UINT8(phy_loop, GemState),
  1028. VMSTATE_UINT32(rx_desc_addr, GemState),
  1029. VMSTATE_UINT32(tx_desc_addr, GemState),
  1030. }
  1031. };
  1032. static Property gem_properties[] = {
  1033. DEFINE_NIC_PROPERTIES(GemState, conf),
  1034. DEFINE_PROP_END_OF_LIST(),
  1035. };
  1036. static void gem_class_init(ObjectClass *klass, void *data)
  1037. {
  1038. DeviceClass *dc = DEVICE_CLASS(klass);
  1039. SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
  1040. sdc->init = gem_init;
  1041. dc->props = gem_properties;
  1042. dc->vmsd = &vmstate_cadence_gem;
  1043. dc->reset = gem_reset;
  1044. }
  1045. static TypeInfo gem_info = {
  1046. .class_init = gem_class_init,
  1047. .name = "cadence_gem",
  1048. .parent = TYPE_SYS_BUS_DEVICE,
  1049. .instance_size = sizeof(GemState),
  1050. };
  1051. static void gem_register_types(void)
  1052. {
  1053. type_register_static(&gem_info);
  1054. }
  1055. type_init(gem_register_types)