xilinx_axienet_main.c 51 KB

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