cadence_gem.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  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 "hw/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_PROMISCUOUS_ACCEPT (-2)
  207. #define GEM_RX_BROADCAST_ACCEPT (-3)
  208. #define GEM_RX_MULTICAST_HASH_ACCEPT (-4)
  209. #define GEM_RX_UNICAST_HASH_ACCEPT (-5)
  210. #define GEM_RX_SAR_ACCEPT 0
  211. /***********************************************************************/
  212. #define DESC_1_USED 0x80000000
  213. #define DESC_1_LENGTH 0x00001FFF
  214. #define DESC_1_TX_WRAP 0x40000000
  215. #define DESC_1_TX_LAST 0x00008000
  216. #define DESC_0_RX_WRAP 0x00000002
  217. #define DESC_0_RX_OWNERSHIP 0x00000001
  218. #define R_DESC_1_RX_SAR_SHIFT 25
  219. #define R_DESC_1_RX_SAR_LENGTH 2
  220. #define R_DESC_1_RX_SAR_MATCH (1 << 27)
  221. #define R_DESC_1_RX_UNICAST_HASH (1 << 29)
  222. #define R_DESC_1_RX_MULTICAST_HASH (1 << 30)
  223. #define R_DESC_1_RX_BROADCAST (1 << 31)
  224. #define DESC_1_RX_SOF 0x00004000
  225. #define DESC_1_RX_EOF 0x00008000
  226. static inline unsigned tx_desc_get_buffer(unsigned *desc)
  227. {
  228. return desc[0];
  229. }
  230. static inline unsigned tx_desc_get_used(unsigned *desc)
  231. {
  232. return (desc[1] & DESC_1_USED) ? 1 : 0;
  233. }
  234. static inline void tx_desc_set_used(unsigned *desc)
  235. {
  236. desc[1] |= DESC_1_USED;
  237. }
  238. static inline unsigned tx_desc_get_wrap(unsigned *desc)
  239. {
  240. return (desc[1] & DESC_1_TX_WRAP) ? 1 : 0;
  241. }
  242. static inline unsigned tx_desc_get_last(unsigned *desc)
  243. {
  244. return (desc[1] & DESC_1_TX_LAST) ? 1 : 0;
  245. }
  246. static inline unsigned tx_desc_get_length(unsigned *desc)
  247. {
  248. return desc[1] & DESC_1_LENGTH;
  249. }
  250. static inline void print_gem_tx_desc(unsigned *desc)
  251. {
  252. DB_PRINT("TXDESC:\n");
  253. DB_PRINT("bufaddr: 0x%08x\n", *desc);
  254. DB_PRINT("used_hw: %d\n", tx_desc_get_used(desc));
  255. DB_PRINT("wrap: %d\n", tx_desc_get_wrap(desc));
  256. DB_PRINT("last: %d\n", tx_desc_get_last(desc));
  257. DB_PRINT("length: %d\n", tx_desc_get_length(desc));
  258. }
  259. static inline unsigned rx_desc_get_buffer(unsigned *desc)
  260. {
  261. return desc[0] & ~0x3UL;
  262. }
  263. static inline unsigned rx_desc_get_wrap(unsigned *desc)
  264. {
  265. return desc[0] & DESC_0_RX_WRAP ? 1 : 0;
  266. }
  267. static inline unsigned rx_desc_get_ownership(unsigned *desc)
  268. {
  269. return desc[0] & DESC_0_RX_OWNERSHIP ? 1 : 0;
  270. }
  271. static inline void rx_desc_set_ownership(unsigned *desc)
  272. {
  273. desc[0] |= DESC_0_RX_OWNERSHIP;
  274. }
  275. static inline void rx_desc_set_sof(unsigned *desc)
  276. {
  277. desc[1] |= DESC_1_RX_SOF;
  278. }
  279. static inline void rx_desc_set_eof(unsigned *desc)
  280. {
  281. desc[1] |= DESC_1_RX_EOF;
  282. }
  283. static inline void rx_desc_set_length(unsigned *desc, unsigned len)
  284. {
  285. desc[1] &= ~DESC_1_LENGTH;
  286. desc[1] |= len;
  287. }
  288. static inline void rx_desc_set_broadcast(unsigned *desc)
  289. {
  290. desc[1] |= R_DESC_1_RX_BROADCAST;
  291. }
  292. static inline void rx_desc_set_unicast_hash(unsigned *desc)
  293. {
  294. desc[1] |= R_DESC_1_RX_UNICAST_HASH;
  295. }
  296. static inline void rx_desc_set_multicast_hash(unsigned *desc)
  297. {
  298. desc[1] |= R_DESC_1_RX_MULTICAST_HASH;
  299. }
  300. static inline void rx_desc_set_sar(unsigned *desc, int sar_idx)
  301. {
  302. desc[1] = deposit32(desc[1], R_DESC_1_RX_SAR_SHIFT, R_DESC_1_RX_SAR_LENGTH,
  303. sar_idx);
  304. desc[1] |= R_DESC_1_RX_SAR_MATCH;
  305. }
  306. #define TYPE_CADENCE_GEM "cadence_gem"
  307. #define GEM(obj) OBJECT_CHECK(GemState, (obj), TYPE_CADENCE_GEM)
  308. typedef struct GemState {
  309. SysBusDevice parent_obj;
  310. MemoryRegion iomem;
  311. NICState *nic;
  312. NICConf conf;
  313. qemu_irq irq;
  314. /* GEM registers backing store */
  315. uint32_t regs[GEM_MAXREG];
  316. /* Mask of register bits which are write only */
  317. uint32_t regs_wo[GEM_MAXREG];
  318. /* Mask of register bits which are read only */
  319. uint32_t regs_ro[GEM_MAXREG];
  320. /* Mask of register bits which are clear on read */
  321. uint32_t regs_rtc[GEM_MAXREG];
  322. /* Mask of register bits which are write 1 to clear */
  323. uint32_t regs_w1c[GEM_MAXREG];
  324. /* PHY registers backing store */
  325. uint16_t phy_regs[32];
  326. uint8_t phy_loop; /* Are we in phy loopback? */
  327. /* The current DMA descriptor pointers */
  328. uint32_t rx_desc_addr;
  329. uint32_t tx_desc_addr;
  330. uint8_t can_rx_state; /* Debug only */
  331. unsigned rx_desc[2];
  332. bool sar_active[4];
  333. } GemState;
  334. /* The broadcast MAC address: 0xFFFFFFFFFFFF */
  335. const uint8_t broadcast_addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  336. /*
  337. * gem_init_register_masks:
  338. * One time initialization.
  339. * Set masks to identify which register bits have magical clear properties
  340. */
  341. static void gem_init_register_masks(GemState *s)
  342. {
  343. /* Mask of register bits which are read only*/
  344. memset(&s->regs_ro[0], 0, sizeof(s->regs_ro));
  345. s->regs_ro[GEM_NWCTRL] = 0xFFF80000;
  346. s->regs_ro[GEM_NWSTATUS] = 0xFFFFFFFF;
  347. s->regs_ro[GEM_DMACFG] = 0xFE00F000;
  348. s->regs_ro[GEM_TXSTATUS] = 0xFFFFFE08;
  349. s->regs_ro[GEM_RXQBASE] = 0x00000003;
  350. s->regs_ro[GEM_TXQBASE] = 0x00000003;
  351. s->regs_ro[GEM_RXSTATUS] = 0xFFFFFFF0;
  352. s->regs_ro[GEM_ISR] = 0xFFFFFFFF;
  353. s->regs_ro[GEM_IMR] = 0xFFFFFFFF;
  354. s->regs_ro[GEM_MODID] = 0xFFFFFFFF;
  355. /* Mask of register bits which are clear on read */
  356. memset(&s->regs_rtc[0], 0, sizeof(s->regs_rtc));
  357. s->regs_rtc[GEM_ISR] = 0xFFFFFFFF;
  358. /* Mask of register bits which are write 1 to clear */
  359. memset(&s->regs_w1c[0], 0, sizeof(s->regs_w1c));
  360. s->regs_w1c[GEM_TXSTATUS] = 0x000001F7;
  361. s->regs_w1c[GEM_RXSTATUS] = 0x0000000F;
  362. /* Mask of register bits which are write only */
  363. memset(&s->regs_wo[0], 0, sizeof(s->regs_wo));
  364. s->regs_wo[GEM_NWCTRL] = 0x00073E60;
  365. s->regs_wo[GEM_IER] = 0x07FFFFFF;
  366. s->regs_wo[GEM_IDR] = 0x07FFFFFF;
  367. }
  368. /*
  369. * phy_update_link:
  370. * Make the emulated PHY link state match the QEMU "interface" state.
  371. */
  372. static void phy_update_link(GemState *s)
  373. {
  374. DB_PRINT("down %d\n", qemu_get_queue(s->nic)->link_down);
  375. /* Autonegotiation status mirrors link status. */
  376. if (qemu_get_queue(s->nic)->link_down) {
  377. s->phy_regs[PHY_REG_STATUS] &= ~(PHY_REG_STATUS_ANEGCMPL |
  378. PHY_REG_STATUS_LINK);
  379. s->phy_regs[PHY_REG_INT_ST] |= PHY_REG_INT_ST_LINKC;
  380. } else {
  381. s->phy_regs[PHY_REG_STATUS] |= (PHY_REG_STATUS_ANEGCMPL |
  382. PHY_REG_STATUS_LINK);
  383. s->phy_regs[PHY_REG_INT_ST] |= (PHY_REG_INT_ST_LINKC |
  384. PHY_REG_INT_ST_ANEGCMPL |
  385. PHY_REG_INT_ST_ENERGY);
  386. }
  387. }
  388. static int gem_can_receive(NetClientState *nc)
  389. {
  390. GemState *s;
  391. s = qemu_get_nic_opaque(nc);
  392. /* Do nothing if receive is not enabled. */
  393. if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_RXENA)) {
  394. if (s->can_rx_state != 1) {
  395. s->can_rx_state = 1;
  396. DB_PRINT("can't receive - no enable\n");
  397. }
  398. return 0;
  399. }
  400. if (rx_desc_get_ownership(s->rx_desc) == 1) {
  401. if (s->can_rx_state != 2) {
  402. s->can_rx_state = 2;
  403. DB_PRINT("can't receive - busy buffer descriptor 0x%x\n",
  404. s->rx_desc_addr);
  405. }
  406. return 0;
  407. }
  408. if (s->can_rx_state != 0) {
  409. s->can_rx_state = 0;
  410. DB_PRINT("can receive 0x%x\n", s->rx_desc_addr);
  411. }
  412. return 1;
  413. }
  414. /*
  415. * gem_update_int_status:
  416. * Raise or lower interrupt based on current status.
  417. */
  418. static void gem_update_int_status(GemState *s)
  419. {
  420. if (s->regs[GEM_ISR]) {
  421. DB_PRINT("asserting int. (0x%08x)\n", s->regs[GEM_ISR]);
  422. qemu_set_irq(s->irq, 1);
  423. }
  424. }
  425. /*
  426. * gem_receive_updatestats:
  427. * Increment receive statistics.
  428. */
  429. static void gem_receive_updatestats(GemState *s, const uint8_t *packet,
  430. unsigned bytes)
  431. {
  432. uint64_t octets;
  433. /* Total octets (bytes) received */
  434. octets = ((uint64_t)(s->regs[GEM_OCTRXLO]) << 32) |
  435. s->regs[GEM_OCTRXHI];
  436. octets += bytes;
  437. s->regs[GEM_OCTRXLO] = octets >> 32;
  438. s->regs[GEM_OCTRXHI] = octets;
  439. /* Error-free Frames received */
  440. s->regs[GEM_RXCNT]++;
  441. /* Error-free Broadcast Frames counter */
  442. if (!memcmp(packet, broadcast_addr, 6)) {
  443. s->regs[GEM_RXBROADCNT]++;
  444. }
  445. /* Error-free Multicast Frames counter */
  446. if (packet[0] == 0x01) {
  447. s->regs[GEM_RXMULTICNT]++;
  448. }
  449. if (bytes <= 64) {
  450. s->regs[GEM_RX64CNT]++;
  451. } else if (bytes <= 127) {
  452. s->regs[GEM_RX65CNT]++;
  453. } else if (bytes <= 255) {
  454. s->regs[GEM_RX128CNT]++;
  455. } else if (bytes <= 511) {
  456. s->regs[GEM_RX256CNT]++;
  457. } else if (bytes <= 1023) {
  458. s->regs[GEM_RX512CNT]++;
  459. } else if (bytes <= 1518) {
  460. s->regs[GEM_RX1024CNT]++;
  461. } else {
  462. s->regs[GEM_RX1519CNT]++;
  463. }
  464. }
  465. /*
  466. * Get the MAC Address bit from the specified position
  467. */
  468. static unsigned get_bit(const uint8_t *mac, unsigned bit)
  469. {
  470. unsigned byte;
  471. byte = mac[bit / 8];
  472. byte >>= (bit & 0x7);
  473. byte &= 1;
  474. return byte;
  475. }
  476. /*
  477. * Calculate a GEM MAC Address hash index
  478. */
  479. static unsigned calc_mac_hash(const uint8_t *mac)
  480. {
  481. int index_bit, mac_bit;
  482. unsigned hash_index;
  483. hash_index = 0;
  484. mac_bit = 5;
  485. for (index_bit = 5; index_bit >= 0; index_bit--) {
  486. hash_index |= (get_bit(mac, mac_bit) ^
  487. get_bit(mac, mac_bit + 6) ^
  488. get_bit(mac, mac_bit + 12) ^
  489. get_bit(mac, mac_bit + 18) ^
  490. get_bit(mac, mac_bit + 24) ^
  491. get_bit(mac, mac_bit + 30) ^
  492. get_bit(mac, mac_bit + 36) ^
  493. get_bit(mac, mac_bit + 42)) << index_bit;
  494. mac_bit--;
  495. }
  496. return hash_index;
  497. }
  498. /*
  499. * gem_mac_address_filter:
  500. * Accept or reject this destination address?
  501. * Returns:
  502. * GEM_RX_REJECT: reject
  503. * >= 0: Specific address accept (which matched SAR is returned)
  504. * others for various other modes of accept:
  505. * GEM_RM_PROMISCUOUS_ACCEPT, GEM_RX_BROADCAST_ACCEPT,
  506. * GEM_RX_MULTICAST_HASH_ACCEPT or GEM_RX_UNICAST_HASH_ACCEPT
  507. */
  508. static int gem_mac_address_filter(GemState *s, const uint8_t *packet)
  509. {
  510. uint8_t *gem_spaddr;
  511. int i;
  512. /* Promiscuous mode? */
  513. if (s->regs[GEM_NWCFG] & GEM_NWCFG_PROMISC) {
  514. return GEM_RX_PROMISCUOUS_ACCEPT;
  515. }
  516. if (!memcmp(packet, broadcast_addr, 6)) {
  517. /* Reject broadcast packets? */
  518. if (s->regs[GEM_NWCFG] & GEM_NWCFG_BCAST_REJ) {
  519. return GEM_RX_REJECT;
  520. }
  521. return GEM_RX_BROADCAST_ACCEPT;
  522. }
  523. /* Accept packets -w- hash match? */
  524. if ((packet[0] == 0x01 && (s->regs[GEM_NWCFG] & GEM_NWCFG_MCAST_HASH)) ||
  525. (packet[0] != 0x01 && (s->regs[GEM_NWCFG] & GEM_NWCFG_UCAST_HASH))) {
  526. unsigned hash_index;
  527. hash_index = calc_mac_hash(packet);
  528. if (hash_index < 32) {
  529. if (s->regs[GEM_HASHLO] & (1<<hash_index)) {
  530. return packet[0] == 0x01 ? GEM_RX_MULTICAST_HASH_ACCEPT :
  531. GEM_RX_UNICAST_HASH_ACCEPT;
  532. }
  533. } else {
  534. hash_index -= 32;
  535. if (s->regs[GEM_HASHHI] & (1<<hash_index)) {
  536. return packet[0] == 0x01 ? GEM_RX_MULTICAST_HASH_ACCEPT :
  537. GEM_RX_UNICAST_HASH_ACCEPT;
  538. }
  539. }
  540. }
  541. /* Check all 4 specific addresses */
  542. gem_spaddr = (uint8_t *)&(s->regs[GEM_SPADDR1LO]);
  543. for (i = 3; i >= 0; i--) {
  544. if (s->sar_active[i] && !memcmp(packet, gem_spaddr + 8 * i, 6)) {
  545. return GEM_RX_SAR_ACCEPT + i;
  546. }
  547. }
  548. /* No address match; reject the packet */
  549. return GEM_RX_REJECT;
  550. }
  551. static void gem_get_rx_desc(GemState *s)
  552. {
  553. DB_PRINT("read descriptor 0x%x\n", (unsigned)s->rx_desc_addr);
  554. /* read current descriptor */
  555. cpu_physical_memory_read(s->rx_desc_addr,
  556. (uint8_t *)s->rx_desc, sizeof(s->rx_desc));
  557. /* Descriptor owned by software ? */
  558. if (rx_desc_get_ownership(s->rx_desc) == 1) {
  559. DB_PRINT("descriptor 0x%x owned by sw.\n",
  560. (unsigned)s->rx_desc_addr);
  561. s->regs[GEM_RXSTATUS] |= GEM_RXSTATUS_NOBUF;
  562. s->regs[GEM_ISR] |= GEM_INT_RXUSED & ~(s->regs[GEM_IMR]);
  563. /* Handle interrupt consequences */
  564. gem_update_int_status(s);
  565. }
  566. }
  567. /*
  568. * gem_receive:
  569. * Fit a packet handed to us by QEMU into the receive descriptor ring.
  570. */
  571. static ssize_t gem_receive(NetClientState *nc, const uint8_t *buf, size_t size)
  572. {
  573. GemState *s;
  574. unsigned rxbufsize, bytes_to_copy;
  575. unsigned rxbuf_offset;
  576. uint8_t rxbuf[2048];
  577. uint8_t *rxbuf_ptr;
  578. bool first_desc = true;
  579. int maf;
  580. s = qemu_get_nic_opaque(nc);
  581. /* Is this destination MAC address "for us" ? */
  582. maf = gem_mac_address_filter(s, buf);
  583. if (maf == GEM_RX_REJECT) {
  584. return -1;
  585. }
  586. /* Discard packets with receive length error enabled ? */
  587. if (s->regs[GEM_NWCFG] & GEM_NWCFG_LERR_DISC) {
  588. unsigned type_len;
  589. /* Fish the ethertype / length field out of the RX packet */
  590. type_len = buf[12] << 8 | buf[13];
  591. /* It is a length field, not an ethertype */
  592. if (type_len < 0x600) {
  593. if (size < type_len) {
  594. /* discard */
  595. return -1;
  596. }
  597. }
  598. }
  599. /*
  600. * Determine configured receive buffer offset (probably 0)
  601. */
  602. rxbuf_offset = (s->regs[GEM_NWCFG] & GEM_NWCFG_BUFF_OFST_M) >>
  603. GEM_NWCFG_BUFF_OFST_S;
  604. /* The configure size of each receive buffer. Determines how many
  605. * buffers needed to hold this packet.
  606. */
  607. rxbufsize = ((s->regs[GEM_DMACFG] & GEM_DMACFG_RBUFSZ_M) >>
  608. GEM_DMACFG_RBUFSZ_S) * GEM_DMACFG_RBUFSZ_MUL;
  609. bytes_to_copy = size;
  610. /* Pad to minimum length. Assume FCS field is stripped, logic
  611. * below will increment it to the real minimum of 64 when
  612. * not FCS stripping
  613. */
  614. if (size < 60) {
  615. size = 60;
  616. }
  617. /* Strip of FCS field ? (usually yes) */
  618. if (s->regs[GEM_NWCFG] & GEM_NWCFG_STRIP_FCS) {
  619. rxbuf_ptr = (void *)buf;
  620. } else {
  621. unsigned crc_val;
  622. int crc_offset;
  623. /* The application wants the FCS field, which QEMU does not provide.
  624. * We must try and caclculate one.
  625. */
  626. memcpy(rxbuf, buf, size);
  627. memset(rxbuf + size, 0, sizeof(rxbuf) - size);
  628. rxbuf_ptr = rxbuf;
  629. crc_val = cpu_to_le32(crc32(0, rxbuf, MAX(size, 60)));
  630. if (size < 60) {
  631. crc_offset = 60;
  632. } else {
  633. crc_offset = size;
  634. }
  635. memcpy(rxbuf + crc_offset, &crc_val, sizeof(crc_val));
  636. bytes_to_copy += 4;
  637. size += 4;
  638. }
  639. DB_PRINT("config bufsize: %d packet size: %ld\n", rxbufsize, size);
  640. while (bytes_to_copy) {
  641. /* Do nothing if receive is not enabled. */
  642. if (!gem_can_receive(nc)) {
  643. assert(!first_desc);
  644. return -1;
  645. }
  646. DB_PRINT("copy %d bytes to 0x%x\n", MIN(bytes_to_copy, rxbufsize),
  647. rx_desc_get_buffer(s->rx_desc));
  648. /* Copy packet data to emulated DMA buffer */
  649. cpu_physical_memory_write(rx_desc_get_buffer(s->rx_desc) + rxbuf_offset,
  650. rxbuf_ptr, MIN(bytes_to_copy, rxbufsize));
  651. rxbuf_ptr += MIN(bytes_to_copy, rxbufsize);
  652. bytes_to_copy -= MIN(bytes_to_copy, rxbufsize);
  653. /* Update the descriptor. */
  654. if (first_desc) {
  655. rx_desc_set_sof(s->rx_desc);
  656. first_desc = false;
  657. }
  658. if (bytes_to_copy == 0) {
  659. rx_desc_set_eof(s->rx_desc);
  660. rx_desc_set_length(s->rx_desc, size);
  661. }
  662. rx_desc_set_ownership(s->rx_desc);
  663. switch (maf) {
  664. case GEM_RX_PROMISCUOUS_ACCEPT:
  665. break;
  666. case GEM_RX_BROADCAST_ACCEPT:
  667. rx_desc_set_broadcast(s->rx_desc);
  668. break;
  669. case GEM_RX_UNICAST_HASH_ACCEPT:
  670. rx_desc_set_unicast_hash(s->rx_desc);
  671. break;
  672. case GEM_RX_MULTICAST_HASH_ACCEPT:
  673. rx_desc_set_multicast_hash(s->rx_desc);
  674. break;
  675. case GEM_RX_REJECT:
  676. abort();
  677. default: /* SAR */
  678. rx_desc_set_sar(s->rx_desc, maf);
  679. }
  680. /* Descriptor write-back. */
  681. cpu_physical_memory_write(s->rx_desc_addr,
  682. (uint8_t *)s->rx_desc, sizeof(s->rx_desc));
  683. /* Next descriptor */
  684. if (rx_desc_get_wrap(s->rx_desc)) {
  685. DB_PRINT("wrapping RX descriptor list\n");
  686. s->rx_desc_addr = s->regs[GEM_RXQBASE];
  687. } else {
  688. DB_PRINT("incrementing RX descriptor list\n");
  689. s->rx_desc_addr += 8;
  690. }
  691. gem_get_rx_desc(s);
  692. }
  693. /* Count it */
  694. gem_receive_updatestats(s, buf, size);
  695. s->regs[GEM_RXSTATUS] |= GEM_RXSTATUS_FRMRCVD;
  696. s->regs[GEM_ISR] |= GEM_INT_RXCMPL & ~(s->regs[GEM_IMR]);
  697. /* Handle interrupt consequences */
  698. gem_update_int_status(s);
  699. return size;
  700. }
  701. /*
  702. * gem_transmit_updatestats:
  703. * Increment transmit statistics.
  704. */
  705. static void gem_transmit_updatestats(GemState *s, const uint8_t *packet,
  706. unsigned bytes)
  707. {
  708. uint64_t octets;
  709. /* Total octets (bytes) transmitted */
  710. octets = ((uint64_t)(s->regs[GEM_OCTTXLO]) << 32) |
  711. s->regs[GEM_OCTTXHI];
  712. octets += bytes;
  713. s->regs[GEM_OCTTXLO] = octets >> 32;
  714. s->regs[GEM_OCTTXHI] = octets;
  715. /* Error-free Frames transmitted */
  716. s->regs[GEM_TXCNT]++;
  717. /* Error-free Broadcast Frames counter */
  718. if (!memcmp(packet, broadcast_addr, 6)) {
  719. s->regs[GEM_TXBCNT]++;
  720. }
  721. /* Error-free Multicast Frames counter */
  722. if (packet[0] == 0x01) {
  723. s->regs[GEM_TXMCNT]++;
  724. }
  725. if (bytes <= 64) {
  726. s->regs[GEM_TX64CNT]++;
  727. } else if (bytes <= 127) {
  728. s->regs[GEM_TX65CNT]++;
  729. } else if (bytes <= 255) {
  730. s->regs[GEM_TX128CNT]++;
  731. } else if (bytes <= 511) {
  732. s->regs[GEM_TX256CNT]++;
  733. } else if (bytes <= 1023) {
  734. s->regs[GEM_TX512CNT]++;
  735. } else if (bytes <= 1518) {
  736. s->regs[GEM_TX1024CNT]++;
  737. } else {
  738. s->regs[GEM_TX1519CNT]++;
  739. }
  740. }
  741. /*
  742. * gem_transmit:
  743. * Fish packets out of the descriptor ring and feed them to QEMU
  744. */
  745. static void gem_transmit(GemState *s)
  746. {
  747. unsigned desc[2];
  748. hwaddr packet_desc_addr;
  749. uint8_t tx_packet[2048];
  750. uint8_t *p;
  751. unsigned total_bytes;
  752. /* Do nothing if transmit is not enabled. */
  753. if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_TXENA)) {
  754. return;
  755. }
  756. DB_PRINT("\n");
  757. /* The packet we will hand off to qemu.
  758. * Packets scattered across multiple descriptors are gathered to this
  759. * one contiguous buffer first.
  760. */
  761. p = tx_packet;
  762. total_bytes = 0;
  763. /* read current descriptor */
  764. packet_desc_addr = s->tx_desc_addr;
  765. cpu_physical_memory_read(packet_desc_addr,
  766. (uint8_t *)&desc[0], sizeof(desc));
  767. /* Handle all descriptors owned by hardware */
  768. while (tx_desc_get_used(desc) == 0) {
  769. /* Do nothing if transmit is not enabled. */
  770. if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_TXENA)) {
  771. return;
  772. }
  773. print_gem_tx_desc(desc);
  774. /* The real hardware would eat this (and possibly crash).
  775. * For QEMU let's lend a helping hand.
  776. */
  777. if ((tx_desc_get_buffer(desc) == 0) ||
  778. (tx_desc_get_length(desc) == 0)) {
  779. DB_PRINT("Invalid TX descriptor @ 0x%x\n",
  780. (unsigned)packet_desc_addr);
  781. break;
  782. }
  783. /* Gather this fragment of the packet from "dma memory" to our contig.
  784. * buffer.
  785. */
  786. cpu_physical_memory_read(tx_desc_get_buffer(desc), p,
  787. tx_desc_get_length(desc));
  788. p += tx_desc_get_length(desc);
  789. total_bytes += tx_desc_get_length(desc);
  790. /* Last descriptor for this packet; hand the whole thing off */
  791. if (tx_desc_get_last(desc)) {
  792. /* Modify the 1st descriptor of this packet to be owned by
  793. * the processor.
  794. */
  795. cpu_physical_memory_read(s->tx_desc_addr,
  796. (uint8_t *)&desc[0], sizeof(desc));
  797. tx_desc_set_used(desc);
  798. cpu_physical_memory_write(s->tx_desc_addr,
  799. (uint8_t *)&desc[0], sizeof(desc));
  800. /* Advance the hardare current descriptor past this packet */
  801. if (tx_desc_get_wrap(desc)) {
  802. s->tx_desc_addr = s->regs[GEM_TXQBASE];
  803. } else {
  804. s->tx_desc_addr = packet_desc_addr + 8;
  805. }
  806. DB_PRINT("TX descriptor next: 0x%08x\n", s->tx_desc_addr);
  807. s->regs[GEM_TXSTATUS] |= GEM_TXSTATUS_TXCMPL;
  808. s->regs[GEM_ISR] |= GEM_INT_TXCMPL & ~(s->regs[GEM_IMR]);
  809. /* Handle interrupt consequences */
  810. gem_update_int_status(s);
  811. /* Is checksum offload enabled? */
  812. if (s->regs[GEM_DMACFG] & GEM_DMACFG_TXCSUM_OFFL) {
  813. net_checksum_calculate(tx_packet, total_bytes);
  814. }
  815. /* Update MAC statistics */
  816. gem_transmit_updatestats(s, tx_packet, total_bytes);
  817. /* Send the packet somewhere */
  818. if (s->phy_loop || (s->regs[GEM_NWCTRL] & GEM_NWCTRL_LOCALLOOP)) {
  819. gem_receive(qemu_get_queue(s->nic), tx_packet, total_bytes);
  820. } else {
  821. qemu_send_packet(qemu_get_queue(s->nic), tx_packet,
  822. total_bytes);
  823. }
  824. /* Prepare for next packet */
  825. p = tx_packet;
  826. total_bytes = 0;
  827. }
  828. /* read next descriptor */
  829. if (tx_desc_get_wrap(desc)) {
  830. packet_desc_addr = s->regs[GEM_TXQBASE];
  831. } else {
  832. packet_desc_addr += 8;
  833. }
  834. cpu_physical_memory_read(packet_desc_addr,
  835. (uint8_t *)&desc[0], sizeof(desc));
  836. }
  837. if (tx_desc_get_used(desc)) {
  838. s->regs[GEM_TXSTATUS] |= GEM_TXSTATUS_USED;
  839. s->regs[GEM_ISR] |= GEM_INT_TXUSED & ~(s->regs[GEM_IMR]);
  840. gem_update_int_status(s);
  841. }
  842. }
  843. static void gem_phy_reset(GemState *s)
  844. {
  845. memset(&s->phy_regs[0], 0, sizeof(s->phy_regs));
  846. s->phy_regs[PHY_REG_CONTROL] = 0x1140;
  847. s->phy_regs[PHY_REG_STATUS] = 0x7969;
  848. s->phy_regs[PHY_REG_PHYID1] = 0x0141;
  849. s->phy_regs[PHY_REG_PHYID2] = 0x0CC2;
  850. s->phy_regs[PHY_REG_ANEGADV] = 0x01E1;
  851. s->phy_regs[PHY_REG_LINKPABIL] = 0xCDE1;
  852. s->phy_regs[PHY_REG_ANEGEXP] = 0x000F;
  853. s->phy_regs[PHY_REG_NEXTP] = 0x2001;
  854. s->phy_regs[PHY_REG_LINKPNEXTP] = 0x40E6;
  855. s->phy_regs[PHY_REG_100BTCTRL] = 0x0300;
  856. s->phy_regs[PHY_REG_1000BTSTAT] = 0x7C00;
  857. s->phy_regs[PHY_REG_EXTSTAT] = 0x3000;
  858. s->phy_regs[PHY_REG_PHYSPCFC_CTL] = 0x0078;
  859. s->phy_regs[PHY_REG_PHYSPCFC_ST] = 0xBC00;
  860. s->phy_regs[PHY_REG_EXT_PHYSPCFC_CTL] = 0x0C60;
  861. s->phy_regs[PHY_REG_LED] = 0x4100;
  862. s->phy_regs[PHY_REG_EXT_PHYSPCFC_CTL2] = 0x000A;
  863. s->phy_regs[PHY_REG_EXT_PHYSPCFC_ST] = 0x848B;
  864. phy_update_link(s);
  865. }
  866. static void gem_reset(DeviceState *d)
  867. {
  868. int i;
  869. GemState *s = GEM(d);
  870. DB_PRINT("\n");
  871. /* Set post reset register values */
  872. memset(&s->regs[0], 0, sizeof(s->regs));
  873. s->regs[GEM_NWCFG] = 0x00080000;
  874. s->regs[GEM_NWSTATUS] = 0x00000006;
  875. s->regs[GEM_DMACFG] = 0x00020784;
  876. s->regs[GEM_IMR] = 0x07ffffff;
  877. s->regs[GEM_TXPAUSE] = 0x0000ffff;
  878. s->regs[GEM_TXPARTIALSF] = 0x000003ff;
  879. s->regs[GEM_RXPARTIALSF] = 0x000003ff;
  880. s->regs[GEM_MODID] = 0x00020118;
  881. s->regs[GEM_DESCONF] = 0x02500111;
  882. s->regs[GEM_DESCONF2] = 0x2ab13fff;
  883. s->regs[GEM_DESCONF5] = 0x002f2145;
  884. s->regs[GEM_DESCONF6] = 0x00000200;
  885. for (i = 0; i < 4; i++) {
  886. s->sar_active[i] = false;
  887. }
  888. gem_phy_reset(s);
  889. gem_update_int_status(s);
  890. }
  891. static uint16_t gem_phy_read(GemState *s, unsigned reg_num)
  892. {
  893. DB_PRINT("reg: %d value: 0x%04x\n", reg_num, s->phy_regs[reg_num]);
  894. return s->phy_regs[reg_num];
  895. }
  896. static void gem_phy_write(GemState *s, unsigned reg_num, uint16_t val)
  897. {
  898. DB_PRINT("reg: %d value: 0x%04x\n", reg_num, val);
  899. switch (reg_num) {
  900. case PHY_REG_CONTROL:
  901. if (val & PHY_REG_CONTROL_RST) {
  902. /* Phy reset */
  903. gem_phy_reset(s);
  904. val &= ~(PHY_REG_CONTROL_RST | PHY_REG_CONTROL_LOOP);
  905. s->phy_loop = 0;
  906. }
  907. if (val & PHY_REG_CONTROL_ANEG) {
  908. /* Complete autonegotiation immediately */
  909. val &= ~PHY_REG_CONTROL_ANEG;
  910. s->phy_regs[PHY_REG_STATUS] |= PHY_REG_STATUS_ANEGCMPL;
  911. }
  912. if (val & PHY_REG_CONTROL_LOOP) {
  913. DB_PRINT("PHY placed in loopback\n");
  914. s->phy_loop = 1;
  915. } else {
  916. s->phy_loop = 0;
  917. }
  918. break;
  919. }
  920. s->phy_regs[reg_num] = val;
  921. }
  922. /*
  923. * gem_read32:
  924. * Read a GEM register.
  925. */
  926. static uint64_t gem_read(void *opaque, hwaddr offset, unsigned size)
  927. {
  928. GemState *s;
  929. uint32_t retval;
  930. s = (GemState *)opaque;
  931. offset >>= 2;
  932. retval = s->regs[offset];
  933. DB_PRINT("offset: 0x%04x read: 0x%08x\n", (unsigned)offset*4, retval);
  934. switch (offset) {
  935. case GEM_ISR:
  936. DB_PRINT("lowering irq on ISR read\n");
  937. qemu_set_irq(s->irq, 0);
  938. break;
  939. case GEM_PHYMNTNC:
  940. if (retval & GEM_PHYMNTNC_OP_R) {
  941. uint32_t phy_addr, reg_num;
  942. phy_addr = (retval & GEM_PHYMNTNC_ADDR) >> GEM_PHYMNTNC_ADDR_SHFT;
  943. if (phy_addr == BOARD_PHY_ADDRESS) {
  944. reg_num = (retval & GEM_PHYMNTNC_REG) >> GEM_PHYMNTNC_REG_SHIFT;
  945. retval &= 0xFFFF0000;
  946. retval |= gem_phy_read(s, reg_num);
  947. } else {
  948. retval |= 0xFFFF; /* No device at this address */
  949. }
  950. }
  951. break;
  952. }
  953. /* Squash read to clear bits */
  954. s->regs[offset] &= ~(s->regs_rtc[offset]);
  955. /* Do not provide write only bits */
  956. retval &= ~(s->regs_wo[offset]);
  957. DB_PRINT("0x%08x\n", retval);
  958. return retval;
  959. }
  960. /*
  961. * gem_write32:
  962. * Write a GEM register.
  963. */
  964. static void gem_write(void *opaque, hwaddr offset, uint64_t val,
  965. unsigned size)
  966. {
  967. GemState *s = (GemState *)opaque;
  968. uint32_t readonly;
  969. DB_PRINT("offset: 0x%04x write: 0x%08x ", (unsigned)offset, (unsigned)val);
  970. offset >>= 2;
  971. /* Squash bits which are read only in write value */
  972. val &= ~(s->regs_ro[offset]);
  973. /* Preserve (only) bits which are read only and wtc in register */
  974. readonly = s->regs[offset] & (s->regs_ro[offset] | s->regs_w1c[offset]);
  975. /* Copy register write to backing store */
  976. s->regs[offset] = (val & ~s->regs_w1c[offset]) | readonly;
  977. /* do w1c */
  978. s->regs[offset] &= ~(s->regs_w1c[offset] & val);
  979. /* Handle register write side effects */
  980. switch (offset) {
  981. case GEM_NWCTRL:
  982. if (val & GEM_NWCTRL_RXENA) {
  983. gem_get_rx_desc(s);
  984. }
  985. if (val & GEM_NWCTRL_TXSTART) {
  986. gem_transmit(s);
  987. }
  988. if (!(val & GEM_NWCTRL_TXENA)) {
  989. /* Reset to start of Q when transmit disabled. */
  990. s->tx_desc_addr = s->regs[GEM_TXQBASE];
  991. }
  992. if (gem_can_receive(qemu_get_queue(s->nic))) {
  993. qemu_flush_queued_packets(qemu_get_queue(s->nic));
  994. }
  995. break;
  996. case GEM_TXSTATUS:
  997. gem_update_int_status(s);
  998. break;
  999. case GEM_RXQBASE:
  1000. s->rx_desc_addr = val;
  1001. break;
  1002. case GEM_TXQBASE:
  1003. s->tx_desc_addr = val;
  1004. break;
  1005. case GEM_RXSTATUS:
  1006. gem_update_int_status(s);
  1007. break;
  1008. case GEM_IER:
  1009. s->regs[GEM_IMR] &= ~val;
  1010. gem_update_int_status(s);
  1011. break;
  1012. case GEM_IDR:
  1013. s->regs[GEM_IMR] |= val;
  1014. gem_update_int_status(s);
  1015. break;
  1016. case GEM_SPADDR1LO:
  1017. case GEM_SPADDR2LO:
  1018. case GEM_SPADDR3LO:
  1019. case GEM_SPADDR4LO:
  1020. s->sar_active[(offset - GEM_SPADDR1LO) / 2] = false;
  1021. break;
  1022. case GEM_SPADDR1HI:
  1023. case GEM_SPADDR2HI:
  1024. case GEM_SPADDR3HI:
  1025. case GEM_SPADDR4HI:
  1026. s->sar_active[(offset - GEM_SPADDR1HI) / 2] = true;
  1027. break;
  1028. case GEM_PHYMNTNC:
  1029. if (val & GEM_PHYMNTNC_OP_W) {
  1030. uint32_t phy_addr, reg_num;
  1031. phy_addr = (val & GEM_PHYMNTNC_ADDR) >> GEM_PHYMNTNC_ADDR_SHFT;
  1032. if (phy_addr == BOARD_PHY_ADDRESS) {
  1033. reg_num = (val & GEM_PHYMNTNC_REG) >> GEM_PHYMNTNC_REG_SHIFT;
  1034. gem_phy_write(s, reg_num, val);
  1035. }
  1036. }
  1037. break;
  1038. }
  1039. DB_PRINT("newval: 0x%08x\n", s->regs[offset]);
  1040. }
  1041. static const MemoryRegionOps gem_ops = {
  1042. .read = gem_read,
  1043. .write = gem_write,
  1044. .endianness = DEVICE_LITTLE_ENDIAN,
  1045. };
  1046. static void gem_cleanup(NetClientState *nc)
  1047. {
  1048. GemState *s = qemu_get_nic_opaque(nc);
  1049. DB_PRINT("\n");
  1050. s->nic = NULL;
  1051. }
  1052. static void gem_set_link(NetClientState *nc)
  1053. {
  1054. DB_PRINT("\n");
  1055. phy_update_link(qemu_get_nic_opaque(nc));
  1056. }
  1057. static NetClientInfo net_gem_info = {
  1058. .type = NET_CLIENT_OPTIONS_KIND_NIC,
  1059. .size = sizeof(NICState),
  1060. .can_receive = gem_can_receive,
  1061. .receive = gem_receive,
  1062. .cleanup = gem_cleanup,
  1063. .link_status_changed = gem_set_link,
  1064. };
  1065. static int gem_init(SysBusDevice *sbd)
  1066. {
  1067. DeviceState *dev = DEVICE(sbd);
  1068. GemState *s = GEM(dev);
  1069. DB_PRINT("\n");
  1070. gem_init_register_masks(s);
  1071. memory_region_init_io(&s->iomem, OBJECT(s), &gem_ops, s,
  1072. "enet", sizeof(s->regs));
  1073. sysbus_init_mmio(sbd, &s->iomem);
  1074. sysbus_init_irq(sbd, &s->irq);
  1075. qemu_macaddr_default_if_unset(&s->conf.macaddr);
  1076. s->nic = qemu_new_nic(&net_gem_info, &s->conf,
  1077. object_get_typename(OBJECT(dev)), dev->id, s);
  1078. return 0;
  1079. }
  1080. static const VMStateDescription vmstate_cadence_gem = {
  1081. .name = "cadence_gem",
  1082. .version_id = 2,
  1083. .minimum_version_id = 2,
  1084. .minimum_version_id_old = 2,
  1085. .fields = (VMStateField[]) {
  1086. VMSTATE_UINT32_ARRAY(regs, GemState, GEM_MAXREG),
  1087. VMSTATE_UINT16_ARRAY(phy_regs, GemState, 32),
  1088. VMSTATE_UINT8(phy_loop, GemState),
  1089. VMSTATE_UINT32(rx_desc_addr, GemState),
  1090. VMSTATE_UINT32(tx_desc_addr, GemState),
  1091. VMSTATE_BOOL_ARRAY(sar_active, GemState, 4),
  1092. VMSTATE_END_OF_LIST(),
  1093. }
  1094. };
  1095. static Property gem_properties[] = {
  1096. DEFINE_NIC_PROPERTIES(GemState, conf),
  1097. DEFINE_PROP_END_OF_LIST(),
  1098. };
  1099. static void gem_class_init(ObjectClass *klass, void *data)
  1100. {
  1101. DeviceClass *dc = DEVICE_CLASS(klass);
  1102. SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
  1103. sdc->init = gem_init;
  1104. dc->props = gem_properties;
  1105. dc->vmsd = &vmstate_cadence_gem;
  1106. dc->reset = gem_reset;
  1107. }
  1108. static const TypeInfo gem_info = {
  1109. .name = TYPE_CADENCE_GEM,
  1110. .parent = TYPE_SYS_BUS_DEVICE,
  1111. .instance_size = sizeof(GemState),
  1112. .class_init = gem_class_init,
  1113. };
  1114. static void gem_register_types(void)
  1115. {
  1116. type_register_static(&gem_info);
  1117. }
  1118. type_init(gem_register_types)