xilinx_axienet_main.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  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. }
  1012. /**
  1013. * axienet_ethtools_get_regs_len - Get the total regs length present in the
  1014. * AxiEthernet core.
  1015. * @ndev: Pointer to net_device structure
  1016. *
  1017. * This implements ethtool command for getting the total register length
  1018. * information.
  1019. *
  1020. * Return: the total regs length
  1021. */
  1022. static int axienet_ethtools_get_regs_len(struct net_device *ndev)
  1023. {
  1024. return sizeof(u32) * AXIENET_REGS_N;
  1025. }
  1026. /**
  1027. * axienet_ethtools_get_regs - Dump the contents of all registers present
  1028. * in AxiEthernet core.
  1029. * @ndev: Pointer to net_device structure
  1030. * @regs: Pointer to ethtool_regs structure
  1031. * @ret: Void pointer used to return the contents of the registers.
  1032. *
  1033. * This implements ethtool command for getting the Axi Ethernet register dump.
  1034. * Issue "ethtool -d ethX" to execute this function.
  1035. */
  1036. static void axienet_ethtools_get_regs(struct net_device *ndev,
  1037. struct ethtool_regs *regs, void *ret)
  1038. {
  1039. u32 *data = (u32 *) ret;
  1040. size_t len = sizeof(u32) * AXIENET_REGS_N;
  1041. struct axienet_local *lp = netdev_priv(ndev);
  1042. regs->version = 0;
  1043. regs->len = len;
  1044. memset(data, 0, len);
  1045. data[0] = axienet_ior(lp, XAE_RAF_OFFSET);
  1046. data[1] = axienet_ior(lp, XAE_TPF_OFFSET);
  1047. data[2] = axienet_ior(lp, XAE_IFGP_OFFSET);
  1048. data[3] = axienet_ior(lp, XAE_IS_OFFSET);
  1049. data[4] = axienet_ior(lp, XAE_IP_OFFSET);
  1050. data[5] = axienet_ior(lp, XAE_IE_OFFSET);
  1051. data[6] = axienet_ior(lp, XAE_TTAG_OFFSET);
  1052. data[7] = axienet_ior(lp, XAE_RTAG_OFFSET);
  1053. data[8] = axienet_ior(lp, XAE_UAWL_OFFSET);
  1054. data[9] = axienet_ior(lp, XAE_UAWU_OFFSET);
  1055. data[10] = axienet_ior(lp, XAE_TPID0_OFFSET);
  1056. data[11] = axienet_ior(lp, XAE_TPID1_OFFSET);
  1057. data[12] = axienet_ior(lp, XAE_PPST_OFFSET);
  1058. data[13] = axienet_ior(lp, XAE_RCW0_OFFSET);
  1059. data[14] = axienet_ior(lp, XAE_RCW1_OFFSET);
  1060. data[15] = axienet_ior(lp, XAE_TC_OFFSET);
  1061. data[16] = axienet_ior(lp, XAE_FCC_OFFSET);
  1062. data[17] = axienet_ior(lp, XAE_EMMC_OFFSET);
  1063. data[18] = axienet_ior(lp, XAE_PHYC_OFFSET);
  1064. data[19] = axienet_ior(lp, XAE_MDIO_MC_OFFSET);
  1065. data[20] = axienet_ior(lp, XAE_MDIO_MCR_OFFSET);
  1066. data[21] = axienet_ior(lp, XAE_MDIO_MWD_OFFSET);
  1067. data[22] = axienet_ior(lp, XAE_MDIO_MRD_OFFSET);
  1068. data[23] = axienet_ior(lp, XAE_MDIO_MIS_OFFSET);
  1069. data[24] = axienet_ior(lp, XAE_MDIO_MIP_OFFSET);
  1070. data[25] = axienet_ior(lp, XAE_MDIO_MIE_OFFSET);
  1071. data[26] = axienet_ior(lp, XAE_MDIO_MIC_OFFSET);
  1072. data[27] = axienet_ior(lp, XAE_UAW0_OFFSET);
  1073. data[28] = axienet_ior(lp, XAE_UAW1_OFFSET);
  1074. data[29] = axienet_ior(lp, XAE_FMI_OFFSET);
  1075. data[30] = axienet_ior(lp, XAE_AF0_OFFSET);
  1076. data[31] = axienet_ior(lp, XAE_AF1_OFFSET);
  1077. }
  1078. /**
  1079. * axienet_ethtools_get_pauseparam - Get the pause parameter setting for
  1080. * Tx and Rx paths.
  1081. * @ndev: Pointer to net_device structure
  1082. * @epauseparm: Pointer to ethtool_pauseparam structure.
  1083. *
  1084. * This implements ethtool command for getting axi ethernet pause frame
  1085. * setting. Issue "ethtool -a ethX" to execute this function.
  1086. */
  1087. static void
  1088. axienet_ethtools_get_pauseparam(struct net_device *ndev,
  1089. struct ethtool_pauseparam *epauseparm)
  1090. {
  1091. u32 regval;
  1092. struct axienet_local *lp = netdev_priv(ndev);
  1093. epauseparm->autoneg = 0;
  1094. regval = axienet_ior(lp, XAE_FCC_OFFSET);
  1095. epauseparm->tx_pause = regval & XAE_FCC_FCTX_MASK;
  1096. epauseparm->rx_pause = regval & XAE_FCC_FCRX_MASK;
  1097. }
  1098. /**
  1099. * axienet_ethtools_set_pauseparam - Set device pause parameter(flow control)
  1100. * settings.
  1101. * @ndev: Pointer to net_device structure
  1102. * @epauseparm:Pointer to ethtool_pauseparam structure
  1103. *
  1104. * This implements ethtool command for enabling flow control on Rx and Tx
  1105. * paths. Issue "ethtool -A ethX tx on|off" under linux prompt to execute this
  1106. * function.
  1107. *
  1108. * Return: 0 on success, -EFAULT if device is running
  1109. */
  1110. static int
  1111. axienet_ethtools_set_pauseparam(struct net_device *ndev,
  1112. struct ethtool_pauseparam *epauseparm)
  1113. {
  1114. u32 regval = 0;
  1115. struct axienet_local *lp = netdev_priv(ndev);
  1116. if (netif_running(ndev)) {
  1117. netdev_err(ndev,
  1118. "Please stop netif before applying configuration\n");
  1119. return -EFAULT;
  1120. }
  1121. regval = axienet_ior(lp, XAE_FCC_OFFSET);
  1122. if (epauseparm->tx_pause)
  1123. regval |= XAE_FCC_FCTX_MASK;
  1124. else
  1125. regval &= ~XAE_FCC_FCTX_MASK;
  1126. if (epauseparm->rx_pause)
  1127. regval |= XAE_FCC_FCRX_MASK;
  1128. else
  1129. regval &= ~XAE_FCC_FCRX_MASK;
  1130. axienet_iow(lp, XAE_FCC_OFFSET, regval);
  1131. return 0;
  1132. }
  1133. /**
  1134. * axienet_ethtools_get_coalesce - Get DMA interrupt coalescing count.
  1135. * @ndev: Pointer to net_device structure
  1136. * @ecoalesce: Pointer to ethtool_coalesce structure
  1137. *
  1138. * This implements ethtool command for getting the DMA interrupt coalescing
  1139. * count on Tx and Rx paths. Issue "ethtool -c ethX" under linux prompt to
  1140. * execute this function.
  1141. *
  1142. * Return: 0 always
  1143. */
  1144. static int axienet_ethtools_get_coalesce(struct net_device *ndev,
  1145. struct ethtool_coalesce *ecoalesce)
  1146. {
  1147. u32 regval = 0;
  1148. struct axienet_local *lp = netdev_priv(ndev);
  1149. regval = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
  1150. ecoalesce->rx_max_coalesced_frames = (regval & XAXIDMA_COALESCE_MASK)
  1151. >> XAXIDMA_COALESCE_SHIFT;
  1152. regval = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
  1153. ecoalesce->tx_max_coalesced_frames = (regval & XAXIDMA_COALESCE_MASK)
  1154. >> XAXIDMA_COALESCE_SHIFT;
  1155. return 0;
  1156. }
  1157. /**
  1158. * axienet_ethtools_set_coalesce - Set DMA interrupt coalescing count.
  1159. * @ndev: Pointer to net_device structure
  1160. * @ecoalesce: Pointer to ethtool_coalesce structure
  1161. *
  1162. * This implements ethtool command for setting the DMA interrupt coalescing
  1163. * count on Tx and Rx paths. Issue "ethtool -C ethX rx-frames 5" under linux
  1164. * prompt to execute this function.
  1165. *
  1166. * Return: 0, on success, Non-zero error value on failure.
  1167. */
  1168. static int axienet_ethtools_set_coalesce(struct net_device *ndev,
  1169. struct ethtool_coalesce *ecoalesce)
  1170. {
  1171. struct axienet_local *lp = netdev_priv(ndev);
  1172. if (netif_running(ndev)) {
  1173. netdev_err(ndev,
  1174. "Please stop netif before applying configuration\n");
  1175. return -EFAULT;
  1176. }
  1177. if ((ecoalesce->rx_coalesce_usecs) ||
  1178. (ecoalesce->rx_coalesce_usecs_irq) ||
  1179. (ecoalesce->rx_max_coalesced_frames_irq) ||
  1180. (ecoalesce->tx_coalesce_usecs) ||
  1181. (ecoalesce->tx_coalesce_usecs_irq) ||
  1182. (ecoalesce->tx_max_coalesced_frames_irq) ||
  1183. (ecoalesce->stats_block_coalesce_usecs) ||
  1184. (ecoalesce->use_adaptive_rx_coalesce) ||
  1185. (ecoalesce->use_adaptive_tx_coalesce) ||
  1186. (ecoalesce->pkt_rate_low) ||
  1187. (ecoalesce->rx_coalesce_usecs_low) ||
  1188. (ecoalesce->rx_max_coalesced_frames_low) ||
  1189. (ecoalesce->tx_coalesce_usecs_low) ||
  1190. (ecoalesce->tx_max_coalesced_frames_low) ||
  1191. (ecoalesce->pkt_rate_high) ||
  1192. (ecoalesce->rx_coalesce_usecs_high) ||
  1193. (ecoalesce->rx_max_coalesced_frames_high) ||
  1194. (ecoalesce->tx_coalesce_usecs_high) ||
  1195. (ecoalesce->tx_max_coalesced_frames_high) ||
  1196. (ecoalesce->rate_sample_interval))
  1197. return -EOPNOTSUPP;
  1198. if (ecoalesce->rx_max_coalesced_frames)
  1199. lp->coalesce_count_rx = ecoalesce->rx_max_coalesced_frames;
  1200. if (ecoalesce->tx_max_coalesced_frames)
  1201. lp->coalesce_count_tx = ecoalesce->tx_max_coalesced_frames;
  1202. return 0;
  1203. }
  1204. static struct ethtool_ops axienet_ethtool_ops = {
  1205. .get_settings = axienet_ethtools_get_settings,
  1206. .set_settings = axienet_ethtools_set_settings,
  1207. .get_drvinfo = axienet_ethtools_get_drvinfo,
  1208. .get_regs_len = axienet_ethtools_get_regs_len,
  1209. .get_regs = axienet_ethtools_get_regs,
  1210. .get_link = ethtool_op_get_link,
  1211. .get_pauseparam = axienet_ethtools_get_pauseparam,
  1212. .set_pauseparam = axienet_ethtools_set_pauseparam,
  1213. .get_coalesce = axienet_ethtools_get_coalesce,
  1214. .set_coalesce = axienet_ethtools_set_coalesce,
  1215. };
  1216. /**
  1217. * axienet_dma_err_handler - Tasklet handler for Axi DMA Error
  1218. * @data: Data passed
  1219. *
  1220. * Resets the Axi DMA and Axi Ethernet devices, and reconfigures the
  1221. * Tx/Rx BDs.
  1222. */
  1223. static void axienet_dma_err_handler(unsigned long data)
  1224. {
  1225. u32 axienet_status;
  1226. u32 cr, i;
  1227. int mdio_mcreg;
  1228. struct axienet_local *lp = (struct axienet_local *) data;
  1229. struct net_device *ndev = lp->ndev;
  1230. struct axidma_bd *cur_p;
  1231. axienet_setoptions(ndev, lp->options &
  1232. ~(XAE_OPTION_TXEN | XAE_OPTION_RXEN));
  1233. mdio_mcreg = axienet_ior(lp, XAE_MDIO_MC_OFFSET);
  1234. axienet_mdio_wait_until_ready(lp);
  1235. /* Disable the MDIO interface till Axi Ethernet Reset is completed.
  1236. * When we do an Axi Ethernet reset, it resets the complete core
  1237. * including the MDIO. So if MDIO is not disabled when the reset
  1238. * process is started, MDIO will be broken afterwards.
  1239. */
  1240. axienet_iow(lp, XAE_MDIO_MC_OFFSET, (mdio_mcreg &
  1241. ~XAE_MDIO_MC_MDIOEN_MASK));
  1242. __axienet_device_reset(lp, &ndev->dev, XAXIDMA_TX_CR_OFFSET);
  1243. __axienet_device_reset(lp, &ndev->dev, XAXIDMA_RX_CR_OFFSET);
  1244. axienet_iow(lp, XAE_MDIO_MC_OFFSET, mdio_mcreg);
  1245. axienet_mdio_wait_until_ready(lp);
  1246. for (i = 0; i < TX_BD_NUM; i++) {
  1247. cur_p = &lp->tx_bd_v[i];
  1248. if (cur_p->phys)
  1249. dma_unmap_single(ndev->dev.parent, cur_p->phys,
  1250. (cur_p->cntrl &
  1251. XAXIDMA_BD_CTRL_LENGTH_MASK),
  1252. DMA_TO_DEVICE);
  1253. if (cur_p->app4)
  1254. dev_kfree_skb_irq((struct sk_buff *) cur_p->app4);
  1255. cur_p->phys = 0;
  1256. cur_p->cntrl = 0;
  1257. cur_p->status = 0;
  1258. cur_p->app0 = 0;
  1259. cur_p->app1 = 0;
  1260. cur_p->app2 = 0;
  1261. cur_p->app3 = 0;
  1262. cur_p->app4 = 0;
  1263. cur_p->sw_id_offset = 0;
  1264. }
  1265. for (i = 0; i < RX_BD_NUM; i++) {
  1266. cur_p = &lp->rx_bd_v[i];
  1267. cur_p->status = 0;
  1268. cur_p->app0 = 0;
  1269. cur_p->app1 = 0;
  1270. cur_p->app2 = 0;
  1271. cur_p->app3 = 0;
  1272. cur_p->app4 = 0;
  1273. }
  1274. lp->tx_bd_ci = 0;
  1275. lp->tx_bd_tail = 0;
  1276. lp->rx_bd_ci = 0;
  1277. /* Start updating the Rx channel control register */
  1278. cr = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
  1279. /* Update the interrupt coalesce count */
  1280. cr = ((cr & ~XAXIDMA_COALESCE_MASK) |
  1281. (XAXIDMA_DFT_RX_THRESHOLD << XAXIDMA_COALESCE_SHIFT));
  1282. /* Update the delay timer count */
  1283. cr = ((cr & ~XAXIDMA_DELAY_MASK) |
  1284. (XAXIDMA_DFT_RX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
  1285. /* Enable coalesce, delay timer and error interrupts */
  1286. cr |= XAXIDMA_IRQ_ALL_MASK;
  1287. /* Finally write to the Rx channel control register */
  1288. axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET, cr);
  1289. /* Start updating the Tx channel control register */
  1290. cr = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
  1291. /* Update the interrupt coalesce count */
  1292. cr = (((cr & ~XAXIDMA_COALESCE_MASK)) |
  1293. (XAXIDMA_DFT_TX_THRESHOLD << XAXIDMA_COALESCE_SHIFT));
  1294. /* Update the delay timer count */
  1295. cr = (((cr & ~XAXIDMA_DELAY_MASK)) |
  1296. (XAXIDMA_DFT_TX_WAITBOUND << XAXIDMA_DELAY_SHIFT));
  1297. /* Enable coalesce, delay timer and error interrupts */
  1298. cr |= XAXIDMA_IRQ_ALL_MASK;
  1299. /* Finally write to the Tx channel control register */
  1300. axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET, cr);
  1301. /* Populate the tail pointer and bring the Rx Axi DMA engine out of
  1302. * halted state. This will make the Rx side ready for reception.
  1303. */
  1304. axienet_dma_out32(lp, XAXIDMA_RX_CDESC_OFFSET, lp->rx_bd_p);
  1305. cr = axienet_dma_in32(lp, XAXIDMA_RX_CR_OFFSET);
  1306. axienet_dma_out32(lp, XAXIDMA_RX_CR_OFFSET,
  1307. cr | XAXIDMA_CR_RUNSTOP_MASK);
  1308. axienet_dma_out32(lp, XAXIDMA_RX_TDESC_OFFSET, lp->rx_bd_p +
  1309. (sizeof(*lp->rx_bd_v) * (RX_BD_NUM - 1)));
  1310. /* Write to the RS (Run-stop) bit in the Tx channel control register.
  1311. * Tx channel is now ready to run. But only after we write to the
  1312. * tail pointer register that the Tx channel will start transmitting
  1313. */
  1314. axienet_dma_out32(lp, XAXIDMA_TX_CDESC_OFFSET, lp->tx_bd_p);
  1315. cr = axienet_dma_in32(lp, XAXIDMA_TX_CR_OFFSET);
  1316. axienet_dma_out32(lp, XAXIDMA_TX_CR_OFFSET,
  1317. cr | XAXIDMA_CR_RUNSTOP_MASK);
  1318. axienet_status = axienet_ior(lp, XAE_RCW1_OFFSET);
  1319. axienet_status &= ~XAE_RCW1_RX_MASK;
  1320. axienet_iow(lp, XAE_RCW1_OFFSET, axienet_status);
  1321. axienet_status = axienet_ior(lp, XAE_IP_OFFSET);
  1322. if (axienet_status & XAE_INT_RXRJECT_MASK)
  1323. axienet_iow(lp, XAE_IS_OFFSET, XAE_INT_RXRJECT_MASK);
  1324. axienet_iow(lp, XAE_FCC_OFFSET, XAE_FCC_FCRX_MASK);
  1325. /* Sync default options with HW but leave receiver and
  1326. * transmitter disabled.
  1327. */
  1328. axienet_setoptions(ndev, lp->options &
  1329. ~(XAE_OPTION_TXEN | XAE_OPTION_RXEN));
  1330. axienet_set_mac_address(ndev, NULL);
  1331. axienet_set_multicast_list(ndev);
  1332. axienet_setoptions(ndev, lp->options);
  1333. }
  1334. /**
  1335. * axienet_probe - Axi Ethernet probe function.
  1336. * @pdev: Pointer to platform device structure.
  1337. *
  1338. * Return: 0, on success
  1339. * Non-zero error value on failure.
  1340. *
  1341. * This is the probe routine for Axi Ethernet driver. This is called before
  1342. * any other driver routines are invoked. It allocates and sets up the Ethernet
  1343. * device. Parses through device tree and populates fields of
  1344. * axienet_local. It registers the Ethernet device.
  1345. */
  1346. static int axienet_probe(struct platform_device *pdev)
  1347. {
  1348. int ret;
  1349. struct device_node *np;
  1350. struct axienet_local *lp;
  1351. struct net_device *ndev;
  1352. u8 mac_addr[6];
  1353. struct resource *ethres, dmares;
  1354. u32 value;
  1355. ndev = alloc_etherdev(sizeof(*lp));
  1356. if (!ndev)
  1357. return -ENOMEM;
  1358. platform_set_drvdata(pdev, ndev);
  1359. SET_NETDEV_DEV(ndev, &pdev->dev);
  1360. ndev->flags &= ~IFF_MULTICAST; /* clear multicast */
  1361. ndev->features = NETIF_F_SG;
  1362. ndev->netdev_ops = &axienet_netdev_ops;
  1363. ndev->ethtool_ops = &axienet_ethtool_ops;
  1364. lp = netdev_priv(ndev);
  1365. lp->ndev = ndev;
  1366. lp->dev = &pdev->dev;
  1367. lp->options = XAE_OPTION_DEFAULTS;
  1368. /* Map device registers */
  1369. ethres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1370. lp->regs = devm_ioremap_resource(&pdev->dev, ethres);
  1371. if (IS_ERR(lp->regs)) {
  1372. dev_err(&pdev->dev, "could not map Axi Ethernet regs.\n");
  1373. ret = PTR_ERR(lp->regs);
  1374. goto free_netdev;
  1375. }
  1376. /* Setup checksum offload, but default to off if not specified */
  1377. lp->features = 0;
  1378. ret = of_property_read_u32(pdev->dev.of_node, "xlnx,txcsum", &value);
  1379. if (!ret) {
  1380. switch (value) {
  1381. case 1:
  1382. lp->csum_offload_on_tx_path =
  1383. XAE_FEATURE_PARTIAL_TX_CSUM;
  1384. lp->features |= XAE_FEATURE_PARTIAL_TX_CSUM;
  1385. /* Can checksum TCP/UDP over IPv4. */
  1386. ndev->features |= NETIF_F_IP_CSUM;
  1387. break;
  1388. case 2:
  1389. lp->csum_offload_on_tx_path =
  1390. XAE_FEATURE_FULL_TX_CSUM;
  1391. lp->features |= XAE_FEATURE_FULL_TX_CSUM;
  1392. /* Can checksum TCP/UDP over IPv4. */
  1393. ndev->features |= NETIF_F_IP_CSUM;
  1394. break;
  1395. default:
  1396. lp->csum_offload_on_tx_path = XAE_NO_CSUM_OFFLOAD;
  1397. }
  1398. }
  1399. ret = of_property_read_u32(pdev->dev.of_node, "xlnx,rxcsum", &value);
  1400. if (!ret) {
  1401. switch (value) {
  1402. case 1:
  1403. lp->csum_offload_on_rx_path =
  1404. XAE_FEATURE_PARTIAL_RX_CSUM;
  1405. lp->features |= XAE_FEATURE_PARTIAL_RX_CSUM;
  1406. break;
  1407. case 2:
  1408. lp->csum_offload_on_rx_path =
  1409. XAE_FEATURE_FULL_RX_CSUM;
  1410. lp->features |= XAE_FEATURE_FULL_RX_CSUM;
  1411. break;
  1412. default:
  1413. lp->csum_offload_on_rx_path = XAE_NO_CSUM_OFFLOAD;
  1414. }
  1415. }
  1416. /* For supporting jumbo frames, the Axi Ethernet hardware must have
  1417. * a larger Rx/Tx Memory. Typically, the size must be large so that
  1418. * we can enable jumbo option and start supporting jumbo frames.
  1419. * Here we check for memory allocated for Rx/Tx in the hardware from
  1420. * the device-tree and accordingly set flags.
  1421. */
  1422. of_property_read_u32(pdev->dev.of_node, "xlnx,rxmem", &lp->rxmem);
  1423. of_property_read_u32(pdev->dev.of_node, "xlnx,phy-type", &lp->phy_type);
  1424. /* Find the DMA node, map the DMA registers, and decode the DMA IRQs */
  1425. np = of_parse_phandle(pdev->dev.of_node, "axistream-connected", 0);
  1426. if (IS_ERR(np)) {
  1427. dev_err(&pdev->dev, "could not find DMA node\n");
  1428. ret = PTR_ERR(np);
  1429. goto free_netdev;
  1430. }
  1431. ret = of_address_to_resource(np, 0, &dmares);
  1432. if (ret) {
  1433. dev_err(&pdev->dev, "unable to get DMA resource\n");
  1434. goto free_netdev;
  1435. }
  1436. lp->dma_regs = devm_ioremap_resource(&pdev->dev, &dmares);
  1437. if (IS_ERR(lp->dma_regs)) {
  1438. dev_err(&pdev->dev, "could not map DMA regs\n");
  1439. ret = PTR_ERR(lp->dma_regs);
  1440. goto free_netdev;
  1441. }
  1442. lp->rx_irq = irq_of_parse_and_map(np, 1);
  1443. lp->tx_irq = irq_of_parse_and_map(np, 0);
  1444. of_node_put(np);
  1445. if ((lp->rx_irq <= 0) || (lp->tx_irq <= 0)) {
  1446. dev_err(&pdev->dev, "could not determine irqs\n");
  1447. ret = -ENOMEM;
  1448. goto free_netdev;
  1449. }
  1450. /* Retrieve the MAC address */
  1451. ret = of_property_read_u8_array(pdev->dev.of_node,
  1452. "local-mac-address", mac_addr, 6);
  1453. if (ret) {
  1454. dev_err(&pdev->dev, "could not find MAC address\n");
  1455. goto free_netdev;
  1456. }
  1457. axienet_set_mac_address(ndev, (void *)mac_addr);
  1458. lp->coalesce_count_rx = XAXIDMA_DFT_RX_THRESHOLD;
  1459. lp->coalesce_count_tx = XAXIDMA_DFT_TX_THRESHOLD;
  1460. lp->phy_node = of_parse_phandle(pdev->dev.of_node, "phy-handle", 0);
  1461. if (lp->phy_node) {
  1462. ret = axienet_mdio_setup(lp, pdev->dev.of_node);
  1463. if (ret)
  1464. dev_warn(&pdev->dev, "error registering MDIO bus\n");
  1465. }
  1466. ret = register_netdev(lp->ndev);
  1467. if (ret) {
  1468. dev_err(lp->dev, "register_netdev() error (%i)\n", ret);
  1469. goto free_netdev;
  1470. }
  1471. return 0;
  1472. free_netdev:
  1473. free_netdev(ndev);
  1474. return ret;
  1475. }
  1476. static int axienet_remove(struct platform_device *pdev)
  1477. {
  1478. struct net_device *ndev = platform_get_drvdata(pdev);
  1479. struct axienet_local *lp = netdev_priv(ndev);
  1480. axienet_mdio_teardown(lp);
  1481. unregister_netdev(ndev);
  1482. of_node_put(lp->phy_node);
  1483. lp->phy_node = NULL;
  1484. free_netdev(ndev);
  1485. return 0;
  1486. }
  1487. static struct platform_driver axienet_driver = {
  1488. .probe = axienet_probe,
  1489. .remove = axienet_remove,
  1490. .driver = {
  1491. .name = "xilinx_axienet",
  1492. .of_match_table = axienet_of_match,
  1493. },
  1494. };
  1495. module_platform_driver(axienet_driver);
  1496. MODULE_DESCRIPTION("Xilinx Axi Ethernet driver");
  1497. MODULE_AUTHOR("Xilinx");
  1498. MODULE_LICENSE("GPL");