xilinx_axienet_main.c 50 KB

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