xilinx_axienet_main.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  1. /*
  2. * Xilinx Axi Ethernet device driver
  3. *
  4. * Copyright (c) 2008 Nissin Systems Co., Ltd., Yoshio Kashiwagi
  5. * Copyright (c) 2005-2008 DLA Systems, David H. Lynch Jr. <dhlii@dlasys.net>
  6. * Copyright (c) 2008-2009 Secret Lab Technologies Ltd.
  7. * Copyright (c) 2010 - 2011 Michal Simek <monstr@monstr.eu>
  8. * Copyright (c) 2010 - 2011 PetaLogix
  9. * Copyright (c) 2010 - 2012 Xilinx, Inc. All rights reserved.
  10. *
  11. * This is a driver for the Xilinx Axi Ethernet which is used in the Virtex6
  12. * and Spartan6.
  13. *
  14. * TODO:
  15. * - Add Axi Fifo support.
  16. * - Factor out Axi DMA code into separate driver.
  17. * - Test and fix basic multicast filtering.
  18. * - Add support for extended multicast filtering.
  19. * - Test basic VLAN support.
  20. * - Add support for extended VLAN support.
  21. */
  22. #include <linux/delay.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/module.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/of_mdio.h>
  27. #include <linux/of_net.h>
  28. #include <linux/of_platform.h>
  29. #include <linux/of_irq.h>
  30. #include <linux/of_address.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/phy.h>
  34. #include <linux/mii.h>
  35. #include <linux/ethtool.h>
  36. #include "xilinx_axienet.h"
  37. /* Descriptors defines for Tx and Rx DMA - 2^n for the best performance */
  38. #define TX_BD_NUM 64
  39. #define RX_BD_NUM 128
  40. /* Must be shorter than length of ethtool_drvinfo.driver field to fit */
  41. #define DRIVER_NAME "xaxienet"
  42. #define DRIVER_DESCRIPTION "Xilinx Axi Ethernet driver"
  43. #define DRIVER_VERSION "1.00a"
  44. #define AXIENET_REGS_N 32
  45. /* Match table for of_platform binding */
  46. static const struct of_device_id axienet_of_match[] = {
  47. { .compatible = "xlnx,axi-ethernet-1.00.a", },
  48. { .compatible = "xlnx,axi-ethernet-1.01.a", },
  49. { .compatible = "xlnx,axi-ethernet-2.01.a", },
  50. {},
  51. };
  52. MODULE_DEVICE_TABLE(of, axienet_of_match);
  53. /* Option table for setting up Axi Ethernet hardware options */
  54. static struct axienet_option axienet_options[] = {
  55. /* Turn on jumbo packet support for both Rx and Tx */
  56. {
  57. .opt = XAE_OPTION_JUMBO,
  58. .reg = XAE_TC_OFFSET,
  59. .m_or = XAE_TC_JUM_MASK,
  60. }, {
  61. .opt = XAE_OPTION_JUMBO,
  62. .reg = XAE_RCW1_OFFSET,
  63. .m_or = XAE_RCW1_JUM_MASK,
  64. }, { /* Turn on VLAN packet support for both Rx and Tx */
  65. .opt = XAE_OPTION_VLAN,
  66. .reg = XAE_TC_OFFSET,
  67. .m_or = XAE_TC_VLAN_MASK,
  68. }, {
  69. .opt = XAE_OPTION_VLAN,
  70. .reg = XAE_RCW1_OFFSET,
  71. .m_or = XAE_RCW1_VLAN_MASK,
  72. }, { /* Turn on FCS stripping on receive packets */
  73. .opt = XAE_OPTION_FCS_STRIP,
  74. .reg = XAE_RCW1_OFFSET,
  75. .m_or = XAE_RCW1_FCS_MASK,
  76. }, { /* Turn on FCS insertion on transmit packets */
  77. .opt = XAE_OPTION_FCS_INSERT,
  78. .reg = XAE_TC_OFFSET,
  79. .m_or = XAE_TC_FCS_MASK,
  80. }, { /* Turn off length/type field checking on receive packets */
  81. .opt = XAE_OPTION_LENTYPE_ERR,
  82. .reg = XAE_RCW1_OFFSET,
  83. .m_or = XAE_RCW1_LT_DIS_MASK,
  84. }, { /* Turn on Rx flow control */
  85. .opt = XAE_OPTION_FLOW_CONTROL,
  86. .reg = XAE_FCC_OFFSET,
  87. .m_or = XAE_FCC_FCRX_MASK,
  88. }, { /* Turn on Tx flow control */
  89. .opt = XAE_OPTION_FLOW_CONTROL,
  90. .reg = XAE_FCC_OFFSET,
  91. .m_or = XAE_FCC_FCTX_MASK,
  92. }, { /* Turn on promiscuous frame filtering */
  93. .opt = XAE_OPTION_PROMISC,
  94. .reg = XAE_FMI_OFFSET,
  95. .m_or = XAE_FMI_PM_MASK,
  96. }, { /* Enable transmitter */
  97. .opt = XAE_OPTION_TXEN,
  98. .reg = XAE_TC_OFFSET,
  99. .m_or = XAE_TC_TX_MASK,
  100. }, { /* Enable receiver */
  101. .opt = XAE_OPTION_RXEN,
  102. .reg = XAE_RCW1_OFFSET,
  103. .m_or = XAE_RCW1_RX_MASK,
  104. },
  105. {}
  106. };
  107. /**
  108. * axienet_dma_in32 - Memory mapped Axi DMA register read
  109. * @lp: Pointer to axienet local structure
  110. * @reg: Address offset from the base address of the Axi DMA core
  111. *
  112. * Return: The contents of the Axi DMA register
  113. *
  114. * This function returns the contents of the corresponding Axi DMA register.
  115. */
  116. static inline u32 axienet_dma_in32(struct axienet_local *lp, off_t reg)
  117. {
  118. return in_be32(lp->dma_regs + reg);
  119. }
  120. /**
  121. * axienet_dma_out32 - Memory mapped Axi DMA register write.
  122. * @lp: Pointer to axienet local structure
  123. * @reg: Address offset from the base address of the Axi DMA core
  124. * @value: Value to be written into the Axi DMA register
  125. *
  126. * This function writes the desired value into the corresponding Axi DMA
  127. * register.
  128. */
  129. static inline void axienet_dma_out32(struct axienet_local *lp,
  130. off_t reg, u32 value)
  131. {
  132. out_be32((lp->dma_regs + reg), value);
  133. }
  134. /**
  135. * axienet_dma_bd_release - Release buffer descriptor rings
  136. * @ndev: Pointer to the net_device structure
  137. *
  138. * This function is used to release the descriptors allocated in
  139. * axienet_dma_bd_init. axienet_dma_bd_release is called when Axi Ethernet
  140. * driver stop api is called.
  141. */
  142. static void axienet_dma_bd_release(struct net_device *ndev)
  143. {
  144. int i;
  145. struct axienet_local *lp = netdev_priv(ndev);
  146. for (i = 0; i < RX_BD_NUM; i++) {
  147. dma_unmap_single(ndev->dev.parent, lp->rx_bd_v[i].phys,
  148. lp->max_frm_size, DMA_FROM_DEVICE);
  149. dev_kfree_skb((struct sk_buff *)
  150. (lp->rx_bd_v[i].sw_id_offset));
  151. }
  152. if (lp->rx_bd_v) {
  153. dma_free_coherent(ndev->dev.parent,
  154. sizeof(*lp->rx_bd_v) * RX_BD_NUM,
  155. lp->rx_bd_v,
  156. lp->rx_bd_p);
  157. }
  158. if (lp->tx_bd_v) {
  159. dma_free_coherent(ndev->dev.parent,
  160. sizeof(*lp->tx_bd_v) * TX_BD_NUM,
  161. lp->tx_bd_v,
  162. lp->tx_bd_p);
  163. }
  164. }
  165. /**
  166. * axienet_dma_bd_init - Setup buffer descriptor rings for Axi DMA
  167. * @ndev: Pointer to the net_device structure
  168. *
  169. * Return: 0, on success -ENOMEM, on failure
  170. *
  171. * This function is called to initialize the Rx and Tx DMA descriptor
  172. * rings. This initializes the descriptors with required default values
  173. * and is called when Axi Ethernet driver reset is called.
  174. */
  175. static int axienet_dma_bd_init(struct net_device *ndev)
  176. {
  177. u32 cr;
  178. int i;
  179. struct sk_buff *skb;
  180. struct axienet_local *lp = netdev_priv(ndev);
  181. /* Reset the indexes which are used for accessing the BDs */
  182. lp->tx_bd_ci = 0;
  183. lp->tx_bd_tail = 0;
  184. lp->rx_bd_ci = 0;
  185. /* Allocate the Tx and Rx buffer descriptors. */
  186. lp->tx_bd_v = dma_zalloc_coherent(ndev->dev.parent,
  187. sizeof(*lp->tx_bd_v) * TX_BD_NUM,
  188. &lp->tx_bd_p, GFP_KERNEL);
  189. if (!lp->tx_bd_v)
  190. goto out;
  191. lp->rx_bd_v = dma_zalloc_coherent(ndev->dev.parent,
  192. sizeof(*lp->rx_bd_v) * RX_BD_NUM,
  193. &lp->rx_bd_p, GFP_KERNEL);
  194. if (!lp->rx_bd_v)
  195. goto out;
  196. for (i = 0; i < TX_BD_NUM; i++) {
  197. lp->tx_bd_v[i].next = lp->tx_bd_p +
  198. sizeof(*lp->tx_bd_v) *
  199. ((i + 1) % TX_BD_NUM);
  200. }
  201. for (i = 0; i < RX_BD_NUM; i++) {
  202. lp->rx_bd_v[i].next = lp->rx_bd_p +
  203. sizeof(*lp->rx_bd_v) *
  204. ((i + 1) % RX_BD_NUM);
  205. skb = netdev_alloc_skb_ip_align(ndev, lp->max_frm_size);
  206. if (!skb)
  207. goto out;
  208. lp->rx_bd_v[i].sw_id_offset = (u32) skb;
  209. lp->rx_bd_v[i].phys = dma_map_single(ndev->dev.parent,
  210. skb->data,
  211. lp->max_frm_size,
  212. DMA_FROM_DEVICE);
  213. lp->rx_bd_v[i].cntrl = lp->max_frm_size;
  214. }
  215. /* Start updating the Rx channel control register */
  216. cr = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
  217. /* Update the interrupt coalesce count */
  218. cr = ((cr & ~XAXIDMA_COALESCE_MASK) |
  219. ((lp->coalesce_count_rx) << XAXIDMA_COALESCE_SHIFT));
  220. /* Update the delay timer count */
  221. cr = ((cr & ~XAXIDMA_DELAY_MASK) |
  222. (XAXIDMA_DFT_RX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
  223. /* Enable coalesce, delay timer and error interrupts */
  224. cr |= XAXIDMA_IRQ_ALL_MASK;
  225. /* Write to the Rx channel control register */
  226. axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET, cr);
  227. /* Start updating the Tx channel control register */
  228. cr = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
  229. /* Update the interrupt coalesce count */
  230. cr = (((cr & ~XAXIDMA_COALESCE_MASK)) |
  231. ((lp->coalesce_count_tx) << XAXIDMA_COALESCE_SHIFT));
  232. /* Update the delay timer count */
  233. cr = (((cr & ~XAXIDMA_DELAY_MASK)) |
  234. (XAXIDMA_DFT_TX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
  235. /* Enable coalesce, delay timer and error interrupts */
  236. cr |= XAXIDMA_IRQ_ALL_MASK;
  237. /* Write to the Tx channel control register */
  238. axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, cr);
  239. /* Populate the tail pointer and bring the Rx Axi DMA engine out of
  240. * halted state. This will make the Rx side ready for reception.
  241. */
  242. axienet_dma_out32(lp, XAXIDMA_RX_CDESC_OFFSET, lp->rx_bd_p);
  243. cr = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
  244. axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET,
  245. cr | XAXIDMA_CR_RUNSTOP_MASK);
  246. axienet_dma_out32(lp, XAXIDMA_RX_TDESC_OFFSET, lp->rx_bd_p +
  247. (sizeof(*lp->rx_bd_v) * (RX_BD_NUM - 1)));
  248. /* Write to the RS (Run-stop) bit in the Tx channel control register.
  249. * Tx channel is now ready to run. But only after we write to the
  250. * tail pointer register that the Tx channel will start transmitting.
  251. */
  252. axienet_dma_out32(lp, XAXIDMA_TX_CDESC_OFFSET, lp->tx_bd_p);
  253. cr = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
  254. axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET,
  255. cr | XAXIDMA_CR_RUNSTOP_MASK);
  256. return 0;
  257. out:
  258. axienet_dma_bd_release(ndev);
  259. return -ENOMEM;
  260. }
  261. /**
  262. * axienet_set_mac_address - Write the MAC address
  263. * @ndev: Pointer to the net_device structure
  264. * @address: 6 byte Address to be written as MAC address
  265. *
  266. * This function is called to initialize the MAC address of the Axi Ethernet
  267. * core. It writes to the UAW0 and UAW1 registers of the core.
  268. */
  269. static void axienet_set_mac_address(struct net_device *ndev,
  270. const void *address)
  271. {
  272. struct axienet_local *lp = netdev_priv(ndev);
  273. if (address)
  274. memcpy(ndev->dev_addr, address, ETH_ALEN);
  275. if (!is_valid_ether_addr(ndev->dev_addr))
  276. eth_hw_addr_random(ndev);
  277. /* Set up unicast MAC address filter set its mac address */
  278. axienet_iow(lp, XAE_UAW0_OFFSET,
  279. (ndev->dev_addr[0]) |
  280. (ndev->dev_addr[1] << 8) |
  281. (ndev->dev_addr[2] << 16) |
  282. (ndev->dev_addr[3] << 24));
  283. axienet_iow(lp, XAE_UAW1_OFFSET,
  284. (((axienet_ior(lp, XAE_UAW1_OFFSET)) &
  285. ~XAE_UAW1_UNICASTADDR_MASK) |
  286. (ndev->dev_addr[4] |
  287. (ndev->dev_addr[5] << 8))));
  288. }
  289. /**
  290. * netdev_set_mac_address - Write the MAC address (from outside the driver)
  291. * @ndev: Pointer to the net_device structure
  292. * @p: 6 byte Address to be written as MAC address
  293. *
  294. * Return: 0 for all conditions. Presently, there is no failure case.
  295. *
  296. * This function is called to initialize the MAC address of the Axi Ethernet
  297. * core. It calls the core specific axienet_set_mac_address. This is the
  298. * function that goes into net_device_ops structure entry ndo_set_mac_address.
  299. */
  300. static int netdev_set_mac_address(struct net_device *ndev, void *p)
  301. {
  302. struct sockaddr *addr = p;
  303. axienet_set_mac_address(ndev, addr->sa_data);
  304. return 0;
  305. }
  306. /**
  307. * axienet_set_multicast_list - Prepare the multicast table
  308. * @ndev: Pointer to the net_device structure
  309. *
  310. * This function is called to initialize the multicast table during
  311. * initialization. The Axi Ethernet basic multicast support has a four-entry
  312. * multicast table which is initialized here. Additionally this function
  313. * goes into the net_device_ops structure entry ndo_set_multicast_list. This
  314. * means whenever the multicast table entries need to be updated this
  315. * function gets called.
  316. */
  317. static void axienet_set_multicast_list(struct net_device *ndev)
  318. {
  319. int i;
  320. u32 reg, af0reg, af1reg;
  321. struct axienet_local *lp = netdev_priv(ndev);
  322. if (ndev->flags & (IFF_ALLMULTI | IFF_PROMISC) ||
  323. netdev_mc_count(ndev) > XAE_MULTICAST_CAM_TABLE_NUM) {
  324. /* We must make the kernel realize we had to move into
  325. * promiscuous mode. If it was a promiscuous mode request
  326. * the flag is already set. If not we set it.
  327. */
  328. ndev->flags |= IFF_PROMISC;
  329. reg = axienet_ior(lp, XAE_FMI_OFFSET);
  330. reg |= XAE_FMI_PM_MASK;
  331. axienet_iow(lp, XAE_FMI_OFFSET, reg);
  332. dev_info(&ndev->dev, "Promiscuous mode enabled.\n");
  333. } else if (!netdev_mc_empty(ndev)) {
  334. struct netdev_hw_addr *ha;
  335. i = 0;
  336. netdev_for_each_mc_addr(ha, ndev) {
  337. if (i >= XAE_MULTICAST_CAM_TABLE_NUM)
  338. break;
  339. af0reg = (ha->addr[0]);
  340. af0reg |= (ha->addr[1] << 8);
  341. af0reg |= (ha->addr[2] << 16);
  342. af0reg |= (ha->addr[3] << 24);
  343. af1reg = (ha->addr[4]);
  344. af1reg |= (ha->addr[5] << 8);
  345. reg = axienet_ior(lp, XAE_FMI_OFFSET) & 0xFFFFFF00;
  346. reg |= i;
  347. axienet_iow(lp, XAE_FMI_OFFSET, reg);
  348. axienet_iow(lp, XAE_AF0_OFFSET, af0reg);
  349. axienet_iow(lp, XAE_AF1_OFFSET, af1reg);
  350. i++;
  351. }
  352. } else {
  353. reg = axienet_ior(lp, XAE_FMI_OFFSET);
  354. reg &= ~XAE_FMI_PM_MASK;
  355. axienet_iow(lp, XAE_FMI_OFFSET, reg);
  356. for (i = 0; i < XAE_MULTICAST_CAM_TABLE_NUM; i++) {
  357. reg = axienet_ior(lp, XAE_FMI_OFFSET) & 0xFFFFFF00;
  358. reg |= i;
  359. axienet_iow(lp, XAE_FMI_OFFSET, reg);
  360. axienet_iow(lp, XAE_AF0_OFFSET, 0);
  361. axienet_iow(lp, XAE_AF1_OFFSET, 0);
  362. }
  363. dev_info(&ndev->dev, "Promiscuous mode disabled.\n");
  364. }
  365. }
  366. /**
  367. * axienet_setoptions - Set an Axi Ethernet option
  368. * @ndev: Pointer to the net_device structure
  369. * @options: Option to be enabled/disabled
  370. *
  371. * The Axi Ethernet core has multiple features which can be selectively turned
  372. * on or off. The typical options could be jumbo frame option, basic VLAN
  373. * option, promiscuous mode option etc. This function is used to set or clear
  374. * these options in the Axi Ethernet hardware. This is done through
  375. * axienet_option structure .
  376. */
  377. static void axienet_setoptions(struct net_device *ndev, u32 options)
  378. {
  379. int reg;
  380. struct axienet_local *lp = netdev_priv(ndev);
  381. struct axienet_option *tp = &axienet_options[0];
  382. while (tp->opt) {
  383. reg = ((axienet_ior(lp, tp->reg)) & ~(tp->m_or));
  384. if (options & tp->opt)
  385. reg |= tp->m_or;
  386. axienet_iow(lp, tp->reg, reg);
  387. tp++;
  388. }
  389. lp->options |= options;
  390. }
  391. static void __axienet_device_reset(struct axienet_local *lp, off_t offset)
  392. {
  393. u32 timeout;
  394. /* Reset Axi DMA. This would reset Axi Ethernet core as well. The reset
  395. * process of Axi DMA takes a while to complete as all pending
  396. * commands/transfers will be flushed or completed during this
  397. * reset process.
  398. */
  399. axienet_dma_out32(lp, offset, XAXIDMA_CR_RESET_MASK);
  400. timeout = DELAY_OF_ONE_MILLISEC;
  401. while (axienet_dma_in32(lp, offset) & XAXIDMA_CR_RESET_MASK) {
  402. udelay(1);
  403. if (--timeout == 0) {
  404. netdev_err(lp->ndev, "%s: DMA reset timeout!\n",
  405. __func__);
  406. break;
  407. }
  408. }
  409. }
  410. /**
  411. * axienet_device_reset - Reset and initialize the Axi Ethernet hardware.
  412. * @ndev: Pointer to the net_device structure
  413. *
  414. * This function is called to reset and initialize the Axi Ethernet core. This
  415. * is typically called during initialization. It does a reset of the Axi DMA
  416. * Rx/Tx channels and initializes the Axi DMA BDs. Since Axi DMA reset lines
  417. * areconnected to Axi Ethernet reset lines, this in turn resets the Axi
  418. * Ethernet core. No separate hardware reset is done for the Axi Ethernet
  419. * core.
  420. */
  421. static void axienet_device_reset(struct net_device *ndev)
  422. {
  423. u32 axienet_status;
  424. struct axienet_local *lp = netdev_priv(ndev);
  425. __axienet_device_reset(lp, XAXIDMA_TX_CR_OFFSET);
  426. __axienet_device_reset(lp, XAXIDMA_RX_CR_OFFSET);
  427. lp->max_frm_size = XAE_MAX_VLAN_FRAME_SIZE;
  428. lp->options |= XAE_OPTION_VLAN;
  429. lp->options &= (~XAE_OPTION_JUMBO);
  430. if ((ndev->mtu > XAE_MTU) &&
  431. (ndev->mtu <= XAE_JUMBO_MTU)) {
  432. lp->max_frm_size = ndev->mtu + VLAN_ETH_HLEN +
  433. XAE_TRL_SIZE;
  434. if (lp->max_frm_size <= lp->rxmem)
  435. lp->options |= XAE_OPTION_JUMBO;
  436. }
  437. if (axienet_dma_bd_init(ndev)) {
  438. netdev_err(ndev, "%s: descriptor allocation failed\n",
  439. __func__);
  440. }
  441. axienet_status = axienet_ior(lp, XAE_RCW1_OFFSET);
  442. axienet_status &= ~XAE_RCW1_RX_MASK;
  443. axienet_iow(lp, XAE_RCW1_OFFSET, axienet_status);
  444. axienet_status = axienet_ior(lp, XAE_IP_OFFSET);
  445. if (axienet_status & XAE_INT_RXRJECT_MASK)
  446. axienet_iow(lp, XAE_IS_OFFSET, XAE_INT_RXRJECT_MASK);
  447. axienet_iow(lp, XAE_FCC_OFFSET, XAE_FCC_FCRX_MASK);
  448. /* Sync default options with HW but leave receiver and
  449. * transmitter disabled.
  450. */
  451. axienet_setoptions(ndev, lp->options &
  452. ~(XAE_OPTION_TXEN | XAE_OPTION_RXEN));
  453. axienet_set_mac_address(ndev, NULL);
  454. axienet_set_multicast_list(ndev);
  455. axienet_setoptions(ndev, lp->options);
  456. netif_trans_update(ndev);
  457. }
  458. /**
  459. * axienet_adjust_link - Adjust the PHY link speed/duplex.
  460. * @ndev: Pointer to the net_device structure
  461. *
  462. * This function is called to change the speed and duplex setting after
  463. * auto negotiation is done by the PHY. This is the function that gets
  464. * registered with the PHY interface through the "of_phy_connect" call.
  465. */
  466. static void axienet_adjust_link(struct net_device *ndev)
  467. {
  468. u32 emmc_reg;
  469. u32 link_state;
  470. u32 setspeed = 1;
  471. struct axienet_local *lp = netdev_priv(ndev);
  472. struct phy_device *phy = ndev->phydev;
  473. link_state = phy->speed | (phy->duplex << 1) | phy->link;
  474. if (lp->last_link != link_state) {
  475. if ((phy->speed == SPEED_10) || (phy->speed == SPEED_100)) {
  476. if (lp->phy_mode == PHY_INTERFACE_MODE_1000BASEX)
  477. setspeed = 0;
  478. } else {
  479. if ((phy->speed == SPEED_1000) &&
  480. (lp->phy_mode == PHY_INTERFACE_MODE_MII))
  481. setspeed = 0;
  482. }
  483. if (setspeed == 1) {
  484. emmc_reg = axienet_ior(lp, XAE_EMMC_OFFSET);
  485. emmc_reg &= ~XAE_EMMC_LINKSPEED_MASK;
  486. switch (phy->speed) {
  487. case SPEED_1000:
  488. emmc_reg |= XAE_EMMC_LINKSPD_1000;
  489. break;
  490. case SPEED_100:
  491. emmc_reg |= XAE_EMMC_LINKSPD_100;
  492. break;
  493. case SPEED_10:
  494. emmc_reg |= XAE_EMMC_LINKSPD_10;
  495. break;
  496. default:
  497. dev_err(&ndev->dev, "Speed other than 10, 100 "
  498. "or 1Gbps is not supported\n");
  499. break;
  500. }
  501. axienet_iow(lp, XAE_EMMC_OFFSET, emmc_reg);
  502. lp->last_link = link_state;
  503. phy_print_status(phy);
  504. } else {
  505. netdev_err(ndev,
  506. "Error setting Axi Ethernet mac speed\n");
  507. }
  508. }
  509. }
  510. /**
  511. * axienet_start_xmit_done - Invoked once a transmit is completed by the
  512. * Axi DMA Tx channel.
  513. * @ndev: Pointer to the net_device structure
  514. *
  515. * This function is invoked from the Axi DMA Tx isr to notify the completion
  516. * of transmit operation. It clears fields in the corresponding Tx BDs and
  517. * unmaps the corresponding buffer so that CPU can regain ownership of the
  518. * buffer. It finally invokes "netif_wake_queue" to restart transmission if
  519. * required.
  520. */
  521. static void axienet_start_xmit_done(struct net_device *ndev)
  522. {
  523. u32 size = 0;
  524. u32 packets = 0;
  525. struct axienet_local *lp = netdev_priv(ndev);
  526. struct axidma_bd *cur_p;
  527. unsigned int status = 0;
  528. cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
  529. status = cur_p->status;
  530. while (status & XAXIDMA_BD_STS_COMPLETE_MASK) {
  531. dma_unmap_single(ndev->dev.parent, cur_p->phys,
  532. (cur_p->cntrl & XAXIDMA_BD_CTRL_LENGTH_MASK),
  533. DMA_TO_DEVICE);
  534. if (cur_p->app4)
  535. dev_kfree_skb_irq((struct sk_buff *)cur_p->app4);
  536. /*cur_p->phys = 0;*/
  537. cur_p->app0 = 0;
  538. cur_p->app1 = 0;
  539. cur_p->app2 = 0;
  540. cur_p->app4 = 0;
  541. cur_p->status = 0;
  542. size += status & XAXIDMA_BD_STS_ACTUAL_LEN_MASK;
  543. packets++;
  544. ++lp->tx_bd_ci;
  545. lp->tx_bd_ci %= TX_BD_NUM;
  546. cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
  547. status = cur_p->status;
  548. }
  549. ndev->stats.tx_packets += packets;
  550. ndev->stats.tx_bytes += size;
  551. netif_wake_queue(ndev);
  552. }
  553. /**
  554. * axienet_check_tx_bd_space - Checks if a BD/group of BDs are currently busy
  555. * @lp: Pointer to the axienet_local structure
  556. * @num_frag: The number of BDs to check for
  557. *
  558. * Return: 0, on success
  559. * NETDEV_TX_BUSY, if any of the descriptors are not free
  560. *
  561. * This function is invoked before BDs are allocated and transmission starts.
  562. * This function returns 0 if a BD or group of BDs can be allocated for
  563. * transmission. If the BD or any of the BDs are not free the function
  564. * returns a busy status. This is invoked from axienet_start_xmit.
  565. */
  566. static inline int axienet_check_tx_bd_space(struct axienet_local *lp,
  567. int num_frag)
  568. {
  569. struct axidma_bd *cur_p;
  570. cur_p = &lp->tx_bd_v[(lp->tx_bd_tail + num_frag) % TX_BD_NUM];
  571. if (cur_p->status & XAXIDMA_BD_STS_ALL_MASK)
  572. return NETDEV_TX_BUSY;
  573. return 0;
  574. }
  575. /**
  576. * axienet_start_xmit - Starts the transmission.
  577. * @skb: sk_buff pointer that contains data to be Txed.
  578. * @ndev: Pointer to net_device structure.
  579. *
  580. * Return: NETDEV_TX_OK, on success
  581. * NETDEV_TX_BUSY, if any of the descriptors are not free
  582. *
  583. * This function is invoked from upper layers to initiate transmission. The
  584. * function uses the next available free BDs and populates their fields to
  585. * start the transmission. Additionally if checksum offloading is supported,
  586. * it populates AXI Stream Control fields with appropriate values.
  587. */
  588. static int axienet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  589. {
  590. u32 ii;
  591. u32 num_frag;
  592. u32 csum_start_off;
  593. u32 csum_index_off;
  594. skb_frag_t *frag;
  595. dma_addr_t tail_p;
  596. struct axienet_local *lp = netdev_priv(ndev);
  597. struct axidma_bd *cur_p;
  598. num_frag = skb_shinfo(skb)->nr_frags;
  599. cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
  600. if (axienet_check_tx_bd_space(lp, num_frag)) {
  601. if (!netif_queue_stopped(ndev))
  602. netif_stop_queue(ndev);
  603. return NETDEV_TX_BUSY;
  604. }
  605. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  606. if (lp->features & XAE_FEATURE_FULL_TX_CSUM) {
  607. /* Tx Full Checksum Offload Enabled */
  608. cur_p->app0 |= 2;
  609. } else if (lp->features & XAE_FEATURE_PARTIAL_RX_CSUM) {
  610. csum_start_off = skb_transport_offset(skb);
  611. csum_index_off = csum_start_off + skb->csum_offset;
  612. /* Tx Partial Checksum Offload Enabled */
  613. cur_p->app0 |= 1;
  614. cur_p->app1 = (csum_start_off << 16) | csum_index_off;
  615. }
  616. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  617. cur_p->app0 |= 2; /* Tx Full Checksum Offload Enabled */
  618. }
  619. cur_p->cntrl = skb_headlen(skb) | XAXIDMA_BD_CTRL_TXSOF_MASK;
  620. cur_p->phys = dma_map_single(ndev->dev.parent, skb->data,
  621. skb_headlen(skb), DMA_TO_DEVICE);
  622. for (ii = 0; ii < num_frag; ii++) {
  623. ++lp->tx_bd_tail;
  624. lp->tx_bd_tail %= TX_BD_NUM;
  625. cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
  626. frag = &skb_shinfo(skb)->frags[ii];
  627. cur_p->phys = dma_map_single(ndev->dev.parent,
  628. skb_frag_address(frag),
  629. skb_frag_size(frag),
  630. DMA_TO_DEVICE);
  631. cur_p->cntrl = skb_frag_size(frag);
  632. }
  633. cur_p->cntrl |= XAXIDMA_BD_CTRL_TXEOF_MASK;
  634. cur_p->app4 = (unsigned long)skb;
  635. tail_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
  636. /* Start the transfer */
  637. axienet_dma_out32(lp, XAXIDMA_TX_TDESC_OFFSET, tail_p);
  638. ++lp->tx_bd_tail;
  639. lp->tx_bd_tail %= TX_BD_NUM;
  640. return NETDEV_TX_OK;
  641. }
  642. /**
  643. * axienet_recv - Is called from Axi DMA Rx Isr to complete the received
  644. * BD processing.
  645. * @ndev: Pointer to net_device structure.
  646. *
  647. * This function is invoked from the Axi DMA Rx isr to process the Rx BDs. It
  648. * does minimal processing and invokes "netif_rx" to complete further
  649. * processing.
  650. */
  651. static void axienet_recv(struct net_device *ndev)
  652. {
  653. u32 length;
  654. u32 csumstatus;
  655. u32 size = 0;
  656. u32 packets = 0;
  657. dma_addr_t tail_p = 0;
  658. struct axienet_local *lp = netdev_priv(ndev);
  659. struct sk_buff *skb, *new_skb;
  660. struct axidma_bd *cur_p;
  661. cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
  662. while ((cur_p->status & XAXIDMA_BD_STS_COMPLETE_MASK)) {
  663. tail_p = lp->rx_bd_p + sizeof(*lp->rx_bd_v) * lp->rx_bd_ci;
  664. skb = (struct sk_buff *) (cur_p->sw_id_offset);
  665. length = cur_p->app4 & 0x0000FFFF;
  666. dma_unmap_single(ndev->dev.parent, cur_p->phys,
  667. lp->max_frm_size,
  668. DMA_FROM_DEVICE);
  669. skb_put(skb, length);
  670. skb->protocol = eth_type_trans(skb, ndev);
  671. /*skb_checksum_none_assert(skb);*/
  672. skb->ip_summed = CHECKSUM_NONE;
  673. /* if we're doing Rx csum offload, set it up */
  674. if (lp->features & XAE_FEATURE_FULL_RX_CSUM) {
  675. csumstatus = (cur_p->app2 &
  676. XAE_FULL_CSUM_STATUS_MASK) >> 3;
  677. if ((csumstatus == XAE_IP_TCP_CSUM_VALIDATED) ||
  678. (csumstatus == XAE_IP_UDP_CSUM_VALIDATED)) {
  679. skb->ip_summed = CHECKSUM_UNNECESSARY;
  680. }
  681. } else if ((lp->features & XAE_FEATURE_PARTIAL_RX_CSUM) != 0 &&
  682. skb->protocol == htons(ETH_P_IP) &&
  683. skb->len > 64) {
  684. skb->csum = be32_to_cpu(cur_p->app3 & 0xFFFF);
  685. skb->ip_summed = CHECKSUM_COMPLETE;
  686. }
  687. netif_rx(skb);
  688. size += length;
  689. packets++;
  690. new_skb = netdev_alloc_skb_ip_align(ndev, lp->max_frm_size);
  691. if (!new_skb)
  692. return;
  693. cur_p->phys = dma_map_single(ndev->dev.parent, new_skb->data,
  694. lp->max_frm_size,
  695. DMA_FROM_DEVICE);
  696. cur_p->cntrl = lp->max_frm_size;
  697. cur_p->status = 0;
  698. cur_p->sw_id_offset = (u32) new_skb;
  699. ++lp->rx_bd_ci;
  700. lp->rx_bd_ci %= RX_BD_NUM;
  701. cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
  702. }
  703. ndev->stats.rx_packets += packets;
  704. ndev->stats.rx_bytes += size;
  705. if (tail_p)
  706. axienet_dma_out32(lp, XAXIDMA_RX_TDESC_OFFSET, tail_p);
  707. }
  708. /**
  709. * axienet_tx_irq - Tx Done Isr.
  710. * @irq: irq number
  711. * @_ndev: net_device pointer
  712. *
  713. * Return: IRQ_HANDLED for all cases.
  714. *
  715. * This is the Axi DMA Tx done Isr. It invokes "axienet_start_xmit_done"
  716. * to complete the BD processing.
  717. */
  718. static irqreturn_t axienet_tx_irq(int irq, void *_ndev)
  719. {
  720. u32 cr;
  721. unsigned int status;
  722. struct net_device *ndev = _ndev;
  723. struct axienet_local *lp = netdev_priv(ndev);
  724. status = axienet_dma_in32(lp, XAXIDMA_TX_SR_OFFSET);
  725. if (status & (XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK)) {
  726. axienet_dma_out32(lp, XAXIDMA_TX_SR_OFFSET, status);
  727. axienet_start_xmit_done(lp->ndev);
  728. goto out;
  729. }
  730. if (!(status & XAXIDMA_IRQ_ALL_MASK))
  731. dev_err(&ndev->dev, "No interrupts asserted in Tx path\n");
  732. if (status & XAXIDMA_IRQ_ERROR_MASK) {
  733. dev_err(&ndev->dev, "DMA Tx error 0x%x\n", status);
  734. dev_err(&ndev->dev, "Current BD is at: 0x%x\n",
  735. (lp->tx_bd_v[lp->tx_bd_ci]).phys);
  736. cr = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
  737. /* Disable coalesce, delay timer and error interrupts */
  738. cr &= (~XAXIDMA_IRQ_ALL_MASK);
  739. /* Write to the Tx channel control register */
  740. axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, cr);
  741. cr = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
  742. /* Disable coalesce, delay timer and error interrupts */
  743. cr &= (~XAXIDMA_IRQ_ALL_MASK);
  744. /* Write to the Rx channel control register */
  745. axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET, cr);
  746. tasklet_schedule(&lp->dma_err_tasklet);
  747. axienet_dma_out32(lp, XAXIDMA_TX_SR_OFFSET, status);
  748. }
  749. out:
  750. return IRQ_HANDLED;
  751. }
  752. /**
  753. * axienet_rx_irq - Rx Isr.
  754. * @irq: irq number
  755. * @_ndev: net_device pointer
  756. *
  757. * Return: IRQ_HANDLED for all cases.
  758. *
  759. * This is the Axi DMA Rx Isr. It invokes "axienet_recv" to complete the BD
  760. * processing.
  761. */
  762. static irqreturn_t axienet_rx_irq(int irq, void *_ndev)
  763. {
  764. u32 cr;
  765. unsigned int status;
  766. struct net_device *ndev = _ndev;
  767. struct axienet_local *lp = netdev_priv(ndev);
  768. status = axienet_dma_in32(lp, XAXIDMA_RX_SR_OFFSET);
  769. if (status & (XAXIDMA_IRQ_IOC_MASK | XAXIDMA_IRQ_DELAY_MASK)) {
  770. axienet_dma_out32(lp, XAXIDMA_RX_SR_OFFSET, status);
  771. axienet_recv(lp->ndev);
  772. goto out;
  773. }
  774. if (!(status & XAXIDMA_IRQ_ALL_MASK))
  775. dev_err(&ndev->dev, "No interrupts asserted in Rx path\n");
  776. if (status & XAXIDMA_IRQ_ERROR_MASK) {
  777. dev_err(&ndev->dev, "DMA Rx error 0x%x\n", status);
  778. dev_err(&ndev->dev, "Current BD is at: 0x%x\n",
  779. (lp->rx_bd_v[lp->rx_bd_ci]).phys);
  780. cr = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
  781. /* Disable coalesce, delay timer and error interrupts */
  782. cr &= (~XAXIDMA_IRQ_ALL_MASK);
  783. /* Finally write to the Tx channel control register */
  784. axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, cr);
  785. cr = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
  786. /* Disable coalesce, delay timer and error interrupts */
  787. cr &= (~XAXIDMA_IRQ_ALL_MASK);
  788. /* write to the Rx channel control register */
  789. axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET, cr);
  790. tasklet_schedule(&lp->dma_err_tasklet);
  791. axienet_dma_out32(lp, XAXIDMA_RX_SR_OFFSET, status);
  792. }
  793. out:
  794. return IRQ_HANDLED;
  795. }
  796. static void axienet_dma_err_handler(unsigned long data);
  797. /**
  798. * axienet_open - Driver open routine.
  799. * @ndev: Pointer to net_device structure
  800. *
  801. * Return: 0, on success.
  802. * non-zero error value on failure
  803. *
  804. * This is the driver open routine. It calls phy_start to start the PHY device.
  805. * It also allocates interrupt service routines, enables the interrupt lines
  806. * and ISR handling. Axi Ethernet core is reset through Axi DMA core. Buffer
  807. * descriptors are initialized.
  808. */
  809. static int axienet_open(struct net_device *ndev)
  810. {
  811. int ret, mdio_mcreg;
  812. struct axienet_local *lp = netdev_priv(ndev);
  813. struct phy_device *phydev = NULL;
  814. dev_dbg(&ndev->dev, "axienet_open()\n");
  815. mdio_mcreg = axienet_ior(lp, XAE_MDIO_MC_OFFSET);
  816. ret = axienet_mdio_wait_until_ready(lp);
  817. if (ret < 0)
  818. return ret;
  819. /* Disable the MDIO interface till Axi Ethernet Reset is completed.
  820. * When we do an Axi Ethernet reset, it resets the complete core
  821. * including the MDIO. If MDIO is not disabled when the reset
  822. * process is started, MDIO will be broken afterwards.
  823. */
  824. axienet_iow(lp, XAE_MDIO_MC_OFFSET,
  825. (mdio_mcreg & (~XAE_MDIO_MC_MDIOEN_MASK)));
  826. axienet_device_reset(ndev);
  827. /* Enable the MDIO */
  828. axienet_iow(lp, XAE_MDIO_MC_OFFSET, mdio_mcreg);
  829. ret = axienet_mdio_wait_until_ready(lp);
  830. if (ret < 0)
  831. return ret;
  832. if (lp->phy_node) {
  833. phydev = of_phy_connect(lp->ndev, lp->phy_node,
  834. axienet_adjust_link, 0, lp->phy_mode);
  835. if (!phydev)
  836. dev_err(lp->dev, "of_phy_connect() failed\n");
  837. else
  838. phy_start(phydev);
  839. }
  840. /* Enable tasklets for Axi DMA error handling */
  841. tasklet_init(&lp->dma_err_tasklet, axienet_dma_err_handler,
  842. (unsigned long) lp);
  843. /* Enable interrupts for Axi DMA Tx */
  844. ret = request_irq(lp->tx_irq, axienet_tx_irq, 0, ndev->name, ndev);
  845. if (ret)
  846. goto err_tx_irq;
  847. /* Enable interrupts for Axi DMA Rx */
  848. ret = request_irq(lp->rx_irq, axienet_rx_irq, 0, ndev->name, ndev);
  849. if (ret)
  850. goto err_rx_irq;
  851. return 0;
  852. err_rx_irq:
  853. free_irq(lp->tx_irq, ndev);
  854. err_tx_irq:
  855. if (phydev)
  856. phy_disconnect(phydev);
  857. tasklet_kill(&lp->dma_err_tasklet);
  858. dev_err(lp->dev, "request_irq() failed\n");
  859. return ret;
  860. }
  861. /**
  862. * axienet_stop - Driver stop routine.
  863. * @ndev: Pointer to net_device structure
  864. *
  865. * Return: 0, on success.
  866. *
  867. * This is the driver stop routine. It calls phy_disconnect to stop the PHY
  868. * device. It also removes the interrupt handlers and disables the interrupts.
  869. * The Axi DMA Tx/Rx BDs are released.
  870. */
  871. static int axienet_stop(struct net_device *ndev)
  872. {
  873. u32 cr;
  874. struct axienet_local *lp = netdev_priv(ndev);
  875. dev_dbg(&ndev->dev, "axienet_close()\n");
  876. cr = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
  877. axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET,
  878. cr & (~XAXIDMA_CR_RUNSTOP_MASK));
  879. cr = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
  880. axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET,
  881. cr & (~XAXIDMA_CR_RUNSTOP_MASK));
  882. axienet_setoptions(ndev, lp->options &
  883. ~(XAE_OPTION_TXEN | XAE_OPTION_RXEN));
  884. tasklet_kill(&lp->dma_err_tasklet);
  885. free_irq(lp->tx_irq, ndev);
  886. free_irq(lp->rx_irq, ndev);
  887. if (ndev->phydev)
  888. phy_disconnect(ndev->phydev);
  889. axienet_dma_bd_release(ndev);
  890. return 0;
  891. }
  892. /**
  893. * axienet_change_mtu - Driver change mtu routine.
  894. * @ndev: Pointer to net_device structure
  895. * @new_mtu: New mtu value to be applied
  896. *
  897. * Return: Always returns 0 (success).
  898. *
  899. * This is the change mtu driver routine. It checks if the Axi Ethernet
  900. * hardware supports jumbo frames before changing the mtu. This can be
  901. * called only when the device is not up.
  902. */
  903. static int axienet_change_mtu(struct net_device *ndev, int new_mtu)
  904. {
  905. struct axienet_local *lp = netdev_priv(ndev);
  906. if (netif_running(ndev))
  907. return -EBUSY;
  908. if ((new_mtu + VLAN_ETH_HLEN +
  909. XAE_TRL_SIZE) > lp->rxmem)
  910. return -EINVAL;
  911. ndev->mtu = new_mtu;
  912. return 0;
  913. }
  914. #ifdef CONFIG_NET_POLL_CONTROLLER
  915. /**
  916. * axienet_poll_controller - Axi Ethernet poll mechanism.
  917. * @ndev: Pointer to net_device structure
  918. *
  919. * This implements Rx/Tx ISR poll mechanisms. The interrupts are disabled prior
  920. * to polling the ISRs and are enabled back after the polling is done.
  921. */
  922. static void axienet_poll_controller(struct net_device *ndev)
  923. {
  924. struct axienet_local *lp = netdev_priv(ndev);
  925. disable_irq(lp->tx_irq);
  926. disable_irq(lp->rx_irq);
  927. axienet_rx_irq(lp->tx_irq, ndev);
  928. axienet_tx_irq(lp->rx_irq, ndev);
  929. enable_irq(lp->tx_irq);
  930. enable_irq(lp->rx_irq);
  931. }
  932. #endif
  933. static const struct net_device_ops axienet_netdev_ops = {
  934. .ndo_open = axienet_open,
  935. .ndo_stop = axienet_stop,
  936. .ndo_start_xmit = axienet_start_xmit,
  937. .ndo_change_mtu = axienet_change_mtu,
  938. .ndo_set_mac_address = netdev_set_mac_address,
  939. .ndo_validate_addr = eth_validate_addr,
  940. .ndo_set_rx_mode = axienet_set_multicast_list,
  941. #ifdef CONFIG_NET_POLL_CONTROLLER
  942. .ndo_poll_controller = axienet_poll_controller,
  943. #endif
  944. };
  945. /**
  946. * axienet_ethtools_get_drvinfo - Get various Axi Ethernet driver information.
  947. * @ndev: Pointer to net_device structure
  948. * @ed: Pointer to ethtool_drvinfo structure
  949. *
  950. * This implements ethtool command for getting the driver information.
  951. * Issue "ethtool -i ethX" under linux prompt to execute this function.
  952. */
  953. static void axienet_ethtools_get_drvinfo(struct net_device *ndev,
  954. struct ethtool_drvinfo *ed)
  955. {
  956. strlcpy(ed->driver, DRIVER_NAME, sizeof(ed->driver));
  957. strlcpy(ed->version, DRIVER_VERSION, sizeof(ed->version));
  958. }
  959. /**
  960. * axienet_ethtools_get_regs_len - Get the total regs length present in the
  961. * AxiEthernet core.
  962. * @ndev: Pointer to net_device structure
  963. *
  964. * This implements ethtool command for getting the total register length
  965. * information.
  966. *
  967. * Return: the total regs length
  968. */
  969. static int axienet_ethtools_get_regs_len(struct net_device *ndev)
  970. {
  971. return sizeof(u32) * AXIENET_REGS_N;
  972. }
  973. /**
  974. * axienet_ethtools_get_regs - Dump the contents of all registers present
  975. * in AxiEthernet core.
  976. * @ndev: Pointer to net_device structure
  977. * @regs: Pointer to ethtool_regs structure
  978. * @ret: Void pointer used to return the contents of the registers.
  979. *
  980. * This implements ethtool command for getting the Axi Ethernet register dump.
  981. * Issue "ethtool -d ethX" to execute this function.
  982. */
  983. static void axienet_ethtools_get_regs(struct net_device *ndev,
  984. struct ethtool_regs *regs, void *ret)
  985. {
  986. u32 *data = (u32 *) ret;
  987. size_t len = sizeof(u32) * AXIENET_REGS_N;
  988. struct axienet_local *lp = netdev_priv(ndev);
  989. regs->version = 0;
  990. regs->len = len;
  991. memset(data, 0, len);
  992. data[0] = axienet_ior(lp, XAE_RAF_OFFSET);
  993. data[1] = axienet_ior(lp, XAE_TPF_OFFSET);
  994. data[2] = axienet_ior(lp, XAE_IFGP_OFFSET);
  995. data[3] = axienet_ior(lp, XAE_IS_OFFSET);
  996. data[4] = axienet_ior(lp, XAE_IP_OFFSET);
  997. data[5] = axienet_ior(lp, XAE_IE_OFFSET);
  998. data[6] = axienet_ior(lp, XAE_TTAG_OFFSET);
  999. data[7] = axienet_ior(lp, XAE_RTAG_OFFSET);
  1000. data[8] = axienet_ior(lp, XAE_UAWL_OFFSET);
  1001. data[9] = axienet_ior(lp, XAE_UAWU_OFFSET);
  1002. data[10] = axienet_ior(lp, XAE_TPID0_OFFSET);
  1003. data[11] = axienet_ior(lp, XAE_TPID1_OFFSET);
  1004. data[12] = axienet_ior(lp, XAE_PPST_OFFSET);
  1005. data[13] = axienet_ior(lp, XAE_RCW0_OFFSET);
  1006. data[14] = axienet_ior(lp, XAE_RCW1_OFFSET);
  1007. data[15] = axienet_ior(lp, XAE_TC_OFFSET);
  1008. data[16] = axienet_ior(lp, XAE_FCC_OFFSET);
  1009. data[17] = axienet_ior(lp, XAE_EMMC_OFFSET);
  1010. data[18] = axienet_ior(lp, XAE_PHYC_OFFSET);
  1011. data[19] = axienet_ior(lp, XAE_MDIO_MC_OFFSET);
  1012. data[20] = axienet_ior(lp, XAE_MDIO_MCR_OFFSET);
  1013. data[21] = axienet_ior(lp, XAE_MDIO_MWD_OFFSET);
  1014. data[22] = axienet_ior(lp, XAE_MDIO_MRD_OFFSET);
  1015. data[23] = axienet_ior(lp, XAE_MDIO_MIS_OFFSET);
  1016. data[24] = axienet_ior(lp, XAE_MDIO_MIP_OFFSET);
  1017. data[25] = axienet_ior(lp, XAE_MDIO_MIE_OFFSET);
  1018. data[26] = axienet_ior(lp, XAE_MDIO_MIC_OFFSET);
  1019. data[27] = axienet_ior(lp, XAE_UAW0_OFFSET);
  1020. data[28] = axienet_ior(lp, XAE_UAW1_OFFSET);
  1021. data[29] = axienet_ior(lp, XAE_FMI_OFFSET);
  1022. data[30] = axienet_ior(lp, XAE_AF0_OFFSET);
  1023. data[31] = axienet_ior(lp, XAE_AF1_OFFSET);
  1024. }
  1025. /**
  1026. * axienet_ethtools_get_pauseparam - Get the pause parameter setting for
  1027. * Tx and Rx paths.
  1028. * @ndev: Pointer to net_device structure
  1029. * @epauseparm: Pointer to ethtool_pauseparam structure.
  1030. *
  1031. * This implements ethtool command for getting axi ethernet pause frame
  1032. * setting. Issue "ethtool -a ethX" to execute this function.
  1033. */
  1034. static void
  1035. axienet_ethtools_get_pauseparam(struct net_device *ndev,
  1036. struct ethtool_pauseparam *epauseparm)
  1037. {
  1038. u32 regval;
  1039. struct axienet_local *lp = netdev_priv(ndev);
  1040. epauseparm->autoneg = 0;
  1041. regval = axienet_ior(lp, XAE_FCC_OFFSET);
  1042. epauseparm->tx_pause = regval & XAE_FCC_FCTX_MASK;
  1043. epauseparm->rx_pause = regval & XAE_FCC_FCRX_MASK;
  1044. }
  1045. /**
  1046. * axienet_ethtools_set_pauseparam - Set device pause parameter(flow control)
  1047. * settings.
  1048. * @ndev: Pointer to net_device structure
  1049. * @epauseparm:Pointer to ethtool_pauseparam structure
  1050. *
  1051. * This implements ethtool command for enabling flow control on Rx and Tx
  1052. * paths. Issue "ethtool -A ethX tx on|off" under linux prompt to execute this
  1053. * function.
  1054. *
  1055. * Return: 0 on success, -EFAULT if device is running
  1056. */
  1057. static int
  1058. axienet_ethtools_set_pauseparam(struct net_device *ndev,
  1059. struct ethtool_pauseparam *epauseparm)
  1060. {
  1061. u32 regval = 0;
  1062. struct axienet_local *lp = netdev_priv(ndev);
  1063. if (netif_running(ndev)) {
  1064. netdev_err(ndev,
  1065. "Please stop netif before applying configuration\n");
  1066. return -EFAULT;
  1067. }
  1068. regval = axienet_ior(lp, XAE_FCC_OFFSET);
  1069. if (epauseparm->tx_pause)
  1070. regval |= XAE_FCC_FCTX_MASK;
  1071. else
  1072. regval &= ~XAE_FCC_FCTX_MASK;
  1073. if (epauseparm->rx_pause)
  1074. regval |= XAE_FCC_FCRX_MASK;
  1075. else
  1076. regval &= ~XAE_FCC_FCRX_MASK;
  1077. axienet_iow(lp, XAE_FCC_OFFSET, regval);
  1078. return 0;
  1079. }
  1080. /**
  1081. * axienet_ethtools_get_coalesce - Get DMA interrupt coalescing count.
  1082. * @ndev: Pointer to net_device structure
  1083. * @ecoalesce: Pointer to ethtool_coalesce structure
  1084. *
  1085. * This implements ethtool command for getting the DMA interrupt coalescing
  1086. * count on Tx and Rx paths. Issue "ethtool -c ethX" under linux prompt to
  1087. * execute this function.
  1088. *
  1089. * Return: 0 always
  1090. */
  1091. static int axienet_ethtools_get_coalesce(struct net_device *ndev,
  1092. struct ethtool_coalesce *ecoalesce)
  1093. {
  1094. u32 regval = 0;
  1095. struct axienet_local *lp = netdev_priv(ndev);
  1096. regval = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
  1097. ecoalesce->rx_max_coalesced_frames = (regval & XAXIDMA_COALESCE_MASK)
  1098. >> XAXIDMA_COALESCE_SHIFT;
  1099. regval = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
  1100. ecoalesce->tx_max_coalesced_frames = (regval & XAXIDMA_COALESCE_MASK)
  1101. >> XAXIDMA_COALESCE_SHIFT;
  1102. return 0;
  1103. }
  1104. /**
  1105. * axienet_ethtools_set_coalesce - Set DMA interrupt coalescing count.
  1106. * @ndev: Pointer to net_device structure
  1107. * @ecoalesce: Pointer to ethtool_coalesce structure
  1108. *
  1109. * This implements ethtool command for setting the DMA interrupt coalescing
  1110. * count on Tx and Rx paths. Issue "ethtool -C ethX rx-frames 5" under linux
  1111. * prompt to execute this function.
  1112. *
  1113. * Return: 0, on success, Non-zero error value on failure.
  1114. */
  1115. static int axienet_ethtools_set_coalesce(struct net_device *ndev,
  1116. struct ethtool_coalesce *ecoalesce)
  1117. {
  1118. struct axienet_local *lp = netdev_priv(ndev);
  1119. if (netif_running(ndev)) {
  1120. netdev_err(ndev,
  1121. "Please stop netif before applying configuration\n");
  1122. return -EFAULT;
  1123. }
  1124. if ((ecoalesce->rx_coalesce_usecs) ||
  1125. (ecoalesce->rx_coalesce_usecs_irq) ||
  1126. (ecoalesce->rx_max_coalesced_frames_irq) ||
  1127. (ecoalesce->tx_coalesce_usecs) ||
  1128. (ecoalesce->tx_coalesce_usecs_irq) ||
  1129. (ecoalesce->tx_max_coalesced_frames_irq) ||
  1130. (ecoalesce->stats_block_coalesce_usecs) ||
  1131. (ecoalesce->use_adaptive_rx_coalesce) ||
  1132. (ecoalesce->use_adaptive_tx_coalesce) ||
  1133. (ecoalesce->pkt_rate_low) ||
  1134. (ecoalesce->rx_coalesce_usecs_low) ||
  1135. (ecoalesce->rx_max_coalesced_frames_low) ||
  1136. (ecoalesce->tx_coalesce_usecs_low) ||
  1137. (ecoalesce->tx_max_coalesced_frames_low) ||
  1138. (ecoalesce->pkt_rate_high) ||
  1139. (ecoalesce->rx_coalesce_usecs_high) ||
  1140. (ecoalesce->rx_max_coalesced_frames_high) ||
  1141. (ecoalesce->tx_coalesce_usecs_high) ||
  1142. (ecoalesce->tx_max_coalesced_frames_high) ||
  1143. (ecoalesce->rate_sample_interval))
  1144. return -EOPNOTSUPP;
  1145. if (ecoalesce->rx_max_coalesced_frames)
  1146. lp->coalesce_count_rx = ecoalesce->rx_max_coalesced_frames;
  1147. if (ecoalesce->tx_max_coalesced_frames)
  1148. lp->coalesce_count_tx = ecoalesce->tx_max_coalesced_frames;
  1149. return 0;
  1150. }
  1151. static const struct ethtool_ops axienet_ethtool_ops = {
  1152. .get_drvinfo = axienet_ethtools_get_drvinfo,
  1153. .get_regs_len = axienet_ethtools_get_regs_len,
  1154. .get_regs = axienet_ethtools_get_regs,
  1155. .get_link = ethtool_op_get_link,
  1156. .get_pauseparam = axienet_ethtools_get_pauseparam,
  1157. .set_pauseparam = axienet_ethtools_set_pauseparam,
  1158. .get_coalesce = axienet_ethtools_get_coalesce,
  1159. .set_coalesce = axienet_ethtools_set_coalesce,
  1160. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  1161. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  1162. };
  1163. /**
  1164. * axienet_dma_err_handler - Tasklet handler for Axi DMA Error
  1165. * @data: Data passed
  1166. *
  1167. * Resets the Axi DMA and Axi Ethernet devices, and reconfigures the
  1168. * Tx/Rx BDs.
  1169. */
  1170. static void axienet_dma_err_handler(unsigned long data)
  1171. {
  1172. u32 axienet_status;
  1173. u32 cr, i;
  1174. int mdio_mcreg;
  1175. struct axienet_local *lp = (struct axienet_local *) data;
  1176. struct net_device *ndev = lp->ndev;
  1177. struct axidma_bd *cur_p;
  1178. axienet_setoptions(ndev, lp->options &
  1179. ~(XAE_OPTION_TXEN | XAE_OPTION_RXEN));
  1180. mdio_mcreg = axienet_ior(lp, XAE_MDIO_MC_OFFSET);
  1181. axienet_mdio_wait_until_ready(lp);
  1182. /* Disable the MDIO interface till Axi Ethernet Reset is completed.
  1183. * When we do an Axi Ethernet reset, it resets the complete core
  1184. * including the MDIO. So if MDIO is not disabled when the reset
  1185. * process is started, MDIO will be broken afterwards.
  1186. */
  1187. axienet_iow(lp, XAE_MDIO_MC_OFFSET, (mdio_mcreg &
  1188. ~XAE_MDIO_MC_MDIOEN_MASK));
  1189. __axienet_device_reset(lp, XAXIDMA_TX_CR_OFFSET);
  1190. __axienet_device_reset(lp, XAXIDMA_RX_CR_OFFSET);
  1191. axienet_iow(lp, XAE_MDIO_MC_OFFSET, mdio_mcreg);
  1192. axienet_mdio_wait_until_ready(lp);
  1193. for (i = 0; i < TX_BD_NUM; i++) {
  1194. cur_p = &lp->tx_bd_v[i];
  1195. if (cur_p->phys)
  1196. dma_unmap_single(ndev->dev.parent, cur_p->phys,
  1197. (cur_p->cntrl &
  1198. XAXIDMA_BD_CTRL_LENGTH_MASK),
  1199. DMA_TO_DEVICE);
  1200. if (cur_p->app4)
  1201. dev_kfree_skb_irq((struct sk_buff *) cur_p->app4);
  1202. cur_p->phys = 0;
  1203. cur_p->cntrl = 0;
  1204. cur_p->status = 0;
  1205. cur_p->app0 = 0;
  1206. cur_p->app1 = 0;
  1207. cur_p->app2 = 0;
  1208. cur_p->app3 = 0;
  1209. cur_p->app4 = 0;
  1210. cur_p->sw_id_offset = 0;
  1211. }
  1212. for (i = 0; i < RX_BD_NUM; i++) {
  1213. cur_p = &lp->rx_bd_v[i];
  1214. cur_p->status = 0;
  1215. cur_p->app0 = 0;
  1216. cur_p->app1 = 0;
  1217. cur_p->app2 = 0;
  1218. cur_p->app3 = 0;
  1219. cur_p->app4 = 0;
  1220. }
  1221. lp->tx_bd_ci = 0;
  1222. lp->tx_bd_tail = 0;
  1223. lp->rx_bd_ci = 0;
  1224. /* Start updating the Rx channel control register */
  1225. cr = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
  1226. /* Update the interrupt coalesce count */
  1227. cr = ((cr & ~XAXIDMA_COALESCE_MASK) |
  1228. (XAXIDMA_DFT_RX_THRESHOLD << XAXIDMA_COALESCE_SHIFT));
  1229. /* Update the delay timer count */
  1230. cr = ((cr & ~XAXIDMA_DELAY_MASK) |
  1231. (XAXIDMA_DFT_RX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
  1232. /* Enable coalesce, delay timer and error interrupts */
  1233. cr |= XAXIDMA_IRQ_ALL_MASK;
  1234. /* Finally write to the Rx channel control register */
  1235. axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET, cr);
  1236. /* Start updating the Tx channel control register */
  1237. cr = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
  1238. /* Update the interrupt coalesce count */
  1239. cr = (((cr & ~XAXIDMA_COALESCE_MASK)) |
  1240. (XAXIDMA_DFT_TX_THRESHOLD << XAXIDMA_COALESCE_SHIFT));
  1241. /* Update the delay timer count */
  1242. cr = (((cr & ~XAXIDMA_DELAY_MASK)) |
  1243. (XAXIDMA_DFT_TX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
  1244. /* Enable coalesce, delay timer and error interrupts */
  1245. cr |= XAXIDMA_IRQ_ALL_MASK;
  1246. /* Finally write to the Tx channel control register */
  1247. axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, cr);
  1248. /* Populate the tail pointer and bring the Rx Axi DMA engine out of
  1249. * halted state. This will make the Rx side ready for reception.
  1250. */
  1251. axienet_dma_out32(lp, XAXIDMA_RX_CDESC_OFFSET, lp->rx_bd_p);
  1252. cr = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
  1253. axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET,
  1254. cr | XAXIDMA_CR_RUNSTOP_MASK);
  1255. axienet_dma_out32(lp, XAXIDMA_RX_TDESC_OFFSET, lp->rx_bd_p +
  1256. (sizeof(*lp->rx_bd_v) * (RX_BD_NUM - 1)));
  1257. /* Write to the RS (Run-stop) bit in the Tx channel control register.
  1258. * Tx channel is now ready to run. But only after we write to the
  1259. * tail pointer register that the Tx channel will start transmitting
  1260. */
  1261. axienet_dma_out32(lp, XAXIDMA_TX_CDESC_OFFSET, lp->tx_bd_p);
  1262. cr = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
  1263. axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET,
  1264. cr | XAXIDMA_CR_RUNSTOP_MASK);
  1265. axienet_status = axienet_ior(lp, XAE_RCW1_OFFSET);
  1266. axienet_status &= ~XAE_RCW1_RX_MASK;
  1267. axienet_iow(lp, XAE_RCW1_OFFSET, axienet_status);
  1268. axienet_status = axienet_ior(lp, XAE_IP_OFFSET);
  1269. if (axienet_status & XAE_INT_RXRJECT_MASK)
  1270. axienet_iow(lp, XAE_IS_OFFSET, XAE_INT_RXRJECT_MASK);
  1271. axienet_iow(lp, XAE_FCC_OFFSET, XAE_FCC_FCRX_MASK);
  1272. /* Sync default options with HW but leave receiver and
  1273. * transmitter disabled.
  1274. */
  1275. axienet_setoptions(ndev, lp->options &
  1276. ~(XAE_OPTION_TXEN | XAE_OPTION_RXEN));
  1277. axienet_set_mac_address(ndev, NULL);
  1278. axienet_set_multicast_list(ndev);
  1279. axienet_setoptions(ndev, lp->options);
  1280. }
  1281. /**
  1282. * axienet_probe - Axi Ethernet probe function.
  1283. * @pdev: Pointer to platform device structure.
  1284. *
  1285. * Return: 0, on success
  1286. * Non-zero error value on failure.
  1287. *
  1288. * This is the probe routine for Axi Ethernet driver. This is called before
  1289. * any other driver routines are invoked. It allocates and sets up the Ethernet
  1290. * device. Parses through device tree and populates fields of
  1291. * axienet_local. It registers the Ethernet device.
  1292. */
  1293. static int axienet_probe(struct platform_device *pdev)
  1294. {
  1295. int ret;
  1296. struct device_node *np;
  1297. struct axienet_local *lp;
  1298. struct net_device *ndev;
  1299. const void *mac_addr;
  1300. struct resource *ethres, dmares;
  1301. u32 value;
  1302. ndev = alloc_etherdev(sizeof(*lp));
  1303. if (!ndev)
  1304. return -ENOMEM;
  1305. platform_set_drvdata(pdev, ndev);
  1306. SET_NETDEV_DEV(ndev, &pdev->dev);
  1307. ndev->flags &= ~IFF_MULTICAST; /* clear multicast */
  1308. ndev->features = NETIF_F_SG;
  1309. ndev->netdev_ops = &axienet_netdev_ops;
  1310. ndev->ethtool_ops = &axienet_ethtool_ops;
  1311. /* MTU range: 64 - 9000 */
  1312. ndev->min_mtu = 64;
  1313. ndev->max_mtu = XAE_JUMBO_MTU;
  1314. lp = netdev_priv(ndev);
  1315. lp->ndev = ndev;
  1316. lp->dev = &pdev->dev;
  1317. lp->options = XAE_OPTION_DEFAULTS;
  1318. /* Map device registers */
  1319. ethres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1320. lp->regs = devm_ioremap_resource(&pdev->dev, ethres);
  1321. if (IS_ERR(lp->regs)) {
  1322. dev_err(&pdev->dev, "could not map Axi Ethernet regs.\n");
  1323. ret = PTR_ERR(lp->regs);
  1324. goto free_netdev;
  1325. }
  1326. /* Setup checksum offload, but default to off if not specified */
  1327. lp->features = 0;
  1328. ret = of_property_read_u32(pdev->dev.of_node, "xlnx,txcsum", &value);
  1329. if (!ret) {
  1330. switch (value) {
  1331. case 1:
  1332. lp->csum_offload_on_tx_path =
  1333. XAE_FEATURE_PARTIAL_TX_CSUM;
  1334. lp->features |= XAE_FEATURE_PARTIAL_TX_CSUM;
  1335. /* Can checksum TCP/UDP over IPv4. */
  1336. ndev->features |= NETIF_F_IP_CSUM;
  1337. break;
  1338. case 2:
  1339. lp->csum_offload_on_tx_path =
  1340. XAE_FEATURE_FULL_TX_CSUM;
  1341. lp->features |= XAE_FEATURE_FULL_TX_CSUM;
  1342. /* Can checksum TCP/UDP over IPv4. */
  1343. ndev->features |= NETIF_F_IP_CSUM;
  1344. break;
  1345. default:
  1346. lp->csum_offload_on_tx_path = XAE_NO_CSUM_OFFLOAD;
  1347. }
  1348. }
  1349. ret = of_property_read_u32(pdev->dev.of_node, "xlnx,rxcsum", &value);
  1350. if (!ret) {
  1351. switch (value) {
  1352. case 1:
  1353. lp->csum_offload_on_rx_path =
  1354. XAE_FEATURE_PARTIAL_RX_CSUM;
  1355. lp->features |= XAE_FEATURE_PARTIAL_RX_CSUM;
  1356. break;
  1357. case 2:
  1358. lp->csum_offload_on_rx_path =
  1359. XAE_FEATURE_FULL_RX_CSUM;
  1360. lp->features |= XAE_FEATURE_FULL_RX_CSUM;
  1361. break;
  1362. default:
  1363. lp->csum_offload_on_rx_path = XAE_NO_CSUM_OFFLOAD;
  1364. }
  1365. }
  1366. /* For supporting jumbo frames, the Axi Ethernet hardware must have
  1367. * a larger Rx/Tx Memory. Typically, the size must be large so that
  1368. * we can enable jumbo option and start supporting jumbo frames.
  1369. * Here we check for memory allocated for Rx/Tx in the hardware from
  1370. * the device-tree and accordingly set flags.
  1371. */
  1372. of_property_read_u32(pdev->dev.of_node, "xlnx,rxmem", &lp->rxmem);
  1373. /* Start with the proprietary, and broken phy_type */
  1374. ret = of_property_read_u32(pdev->dev.of_node, "xlnx,phy-type", &value);
  1375. if (!ret) {
  1376. netdev_warn(ndev, "Please upgrade your device tree binary blob to use phy-mode");
  1377. switch (value) {
  1378. case XAE_PHY_TYPE_MII:
  1379. lp->phy_mode = PHY_INTERFACE_MODE_MII;
  1380. break;
  1381. case XAE_PHY_TYPE_GMII:
  1382. lp->phy_mode = PHY_INTERFACE_MODE_GMII;
  1383. break;
  1384. case XAE_PHY_TYPE_RGMII_2_0:
  1385. lp->phy_mode = PHY_INTERFACE_MODE_RGMII_ID;
  1386. break;
  1387. case XAE_PHY_TYPE_SGMII:
  1388. lp->phy_mode = PHY_INTERFACE_MODE_SGMII;
  1389. break;
  1390. case XAE_PHY_TYPE_1000BASE_X:
  1391. lp->phy_mode = PHY_INTERFACE_MODE_1000BASEX;
  1392. break;
  1393. default:
  1394. ret = -EINVAL;
  1395. goto free_netdev;
  1396. }
  1397. } else {
  1398. lp->phy_mode = of_get_phy_mode(pdev->dev.of_node);
  1399. if (lp->phy_mode < 0) {
  1400. ret = -EINVAL;
  1401. goto free_netdev;
  1402. }
  1403. }
  1404. /* Find the DMA node, map the DMA registers, and decode the DMA IRQs */
  1405. np = of_parse_phandle(pdev->dev.of_node, "axistream-connected", 0);
  1406. if (!np) {
  1407. dev_err(&pdev->dev, "could not find DMA node\n");
  1408. ret = -ENODEV;
  1409. goto free_netdev;
  1410. }
  1411. ret = of_address_to_resource(np, 0, &dmares);
  1412. if (ret) {
  1413. dev_err(&pdev->dev, "unable to get DMA resource\n");
  1414. goto free_netdev;
  1415. }
  1416. lp->dma_regs = devm_ioremap_resource(&pdev->dev, &dmares);
  1417. if (IS_ERR(lp->dma_regs)) {
  1418. dev_err(&pdev->dev, "could not map DMA regs\n");
  1419. ret = PTR_ERR(lp->dma_regs);
  1420. goto free_netdev;
  1421. }
  1422. lp->rx_irq = irq_of_parse_and_map(np, 1);
  1423. lp->tx_irq = irq_of_parse_and_map(np, 0);
  1424. of_node_put(np);
  1425. if ((lp->rx_irq <= 0) || (lp->tx_irq <= 0)) {
  1426. dev_err(&pdev->dev, "could not determine irqs\n");
  1427. ret = -ENOMEM;
  1428. goto free_netdev;
  1429. }
  1430. /* Retrieve the MAC address */
  1431. mac_addr = of_get_mac_address(pdev->dev.of_node);
  1432. if (!mac_addr) {
  1433. dev_err(&pdev->dev, "could not find MAC address\n");
  1434. goto free_netdev;
  1435. }
  1436. axienet_set_mac_address(ndev, mac_addr);
  1437. lp->coalesce_count_rx = XAXIDMA_DFT_RX_THRESHOLD;
  1438. lp->coalesce_count_tx = XAXIDMA_DFT_TX_THRESHOLD;
  1439. lp->phy_node = of_parse_phandle(pdev->dev.of_node, "phy-handle", 0);
  1440. if (lp->phy_node) {
  1441. ret = axienet_mdio_setup(lp, pdev->dev.of_node);
  1442. if (ret)
  1443. dev_warn(&pdev->dev, "error registering MDIO bus\n");
  1444. }
  1445. ret = register_netdev(lp->ndev);
  1446. if (ret) {
  1447. dev_err(lp->dev, "register_netdev() error (%i)\n", ret);
  1448. goto free_netdev;
  1449. }
  1450. return 0;
  1451. free_netdev:
  1452. free_netdev(ndev);
  1453. return ret;
  1454. }
  1455. static int axienet_remove(struct platform_device *pdev)
  1456. {
  1457. struct net_device *ndev = platform_get_drvdata(pdev);
  1458. struct axienet_local *lp = netdev_priv(ndev);
  1459. axienet_mdio_teardown(lp);
  1460. unregister_netdev(ndev);
  1461. of_node_put(lp->phy_node);
  1462. lp->phy_node = NULL;
  1463. free_netdev(ndev);
  1464. return 0;
  1465. }
  1466. static struct platform_driver axienet_driver = {
  1467. .probe = axienet_probe,
  1468. .remove = axienet_remove,
  1469. .driver = {
  1470. .name = "xilinx_axienet",
  1471. .of_match_table = axienet_of_match,
  1472. },
  1473. };
  1474. module_platform_driver(axienet_driver);
  1475. MODULE_DESCRIPTION("Xilinx Axi Ethernet driver");
  1476. MODULE_AUTHOR("Xilinx");
  1477. MODULE_LICENSE("GPL");