xilinx_axienet_main.c 51 KB

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