xilinx_axienet_main.c 50 KB

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