xilinx_axienet_main.c 51 KB

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