xilinx_axienet_main.c 51 KB

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