xilinx_axienet_main.c 50 KB

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