xilinx_axienet_main.c 49 KB

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