ftgmac100.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Faraday FTGMAC100 Gigabit Ethernet
  4. *
  5. * (C) Copyright 2009-2011 Faraday Technology
  6. * Po-Yu Chuang <[email protected]>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/clk.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/etherdevice.h>
  12. #include <linux/ethtool.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/of.h>
  18. #include <linux/of_mdio.h>
  19. #include <linux/phy.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/property.h>
  22. #include <linux/crc32.h>
  23. #include <linux/if_vlan.h>
  24. #include <linux/of_net.h>
  25. #include <net/ip.h>
  26. #include <net/ncsi.h>
  27. #include "ftgmac100.h"
  28. #define DRV_NAME "ftgmac100"
  29. /* Arbitrary values, I am not sure the HW has limits */
  30. #define MAX_RX_QUEUE_ENTRIES 1024
  31. #define MAX_TX_QUEUE_ENTRIES 1024
  32. #define MIN_RX_QUEUE_ENTRIES 32
  33. #define MIN_TX_QUEUE_ENTRIES 32
  34. /* Defaults */
  35. #define DEF_RX_QUEUE_ENTRIES 128
  36. #define DEF_TX_QUEUE_ENTRIES 128
  37. #define MAX_PKT_SIZE 1536
  38. #define RX_BUF_SIZE MAX_PKT_SIZE /* must be smaller than 0x3fff */
  39. /* Min number of tx ring entries before stopping queue */
  40. #define TX_THRESHOLD (MAX_SKB_FRAGS + 1)
  41. #define FTGMAC_100MHZ 100000000
  42. #define FTGMAC_25MHZ 25000000
  43. struct ftgmac100 {
  44. /* Registers */
  45. struct resource *res;
  46. void __iomem *base;
  47. /* Rx ring */
  48. unsigned int rx_q_entries;
  49. struct ftgmac100_rxdes *rxdes;
  50. dma_addr_t rxdes_dma;
  51. struct sk_buff **rx_skbs;
  52. unsigned int rx_pointer;
  53. u32 rxdes0_edorr_mask;
  54. /* Tx ring */
  55. unsigned int tx_q_entries;
  56. struct ftgmac100_txdes *txdes;
  57. dma_addr_t txdes_dma;
  58. struct sk_buff **tx_skbs;
  59. unsigned int tx_clean_pointer;
  60. unsigned int tx_pointer;
  61. u32 txdes0_edotr_mask;
  62. /* Used to signal the reset task of ring change request */
  63. unsigned int new_rx_q_entries;
  64. unsigned int new_tx_q_entries;
  65. /* Scratch page to use when rx skb alloc fails */
  66. void *rx_scratch;
  67. dma_addr_t rx_scratch_dma;
  68. /* Component structures */
  69. struct net_device *netdev;
  70. struct device *dev;
  71. struct ncsi_dev *ndev;
  72. struct napi_struct napi;
  73. struct work_struct reset_task;
  74. struct mii_bus *mii_bus;
  75. struct clk *clk;
  76. /* AST2500/AST2600 RMII ref clock gate */
  77. struct clk *rclk;
  78. /* Link management */
  79. int cur_speed;
  80. int cur_duplex;
  81. bool use_ncsi;
  82. /* Multicast filter settings */
  83. u32 maht0;
  84. u32 maht1;
  85. /* Flow control settings */
  86. bool tx_pause;
  87. bool rx_pause;
  88. bool aneg_pause;
  89. /* Misc */
  90. bool need_mac_restart;
  91. bool is_aspeed;
  92. };
  93. static int ftgmac100_reset_mac(struct ftgmac100 *priv, u32 maccr)
  94. {
  95. struct net_device *netdev = priv->netdev;
  96. int i;
  97. /* NOTE: reset clears all registers */
  98. iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
  99. iowrite32(maccr | FTGMAC100_MACCR_SW_RST,
  100. priv->base + FTGMAC100_OFFSET_MACCR);
  101. for (i = 0; i < 200; i++) {
  102. unsigned int maccr;
  103. maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
  104. if (!(maccr & FTGMAC100_MACCR_SW_RST))
  105. return 0;
  106. udelay(1);
  107. }
  108. netdev_err(netdev, "Hardware reset failed\n");
  109. return -EIO;
  110. }
  111. static int ftgmac100_reset_and_config_mac(struct ftgmac100 *priv)
  112. {
  113. u32 maccr = 0;
  114. switch (priv->cur_speed) {
  115. case SPEED_10:
  116. case 0: /* no link */
  117. break;
  118. case SPEED_100:
  119. maccr |= FTGMAC100_MACCR_FAST_MODE;
  120. break;
  121. case SPEED_1000:
  122. maccr |= FTGMAC100_MACCR_GIGA_MODE;
  123. break;
  124. default:
  125. netdev_err(priv->netdev, "Unknown speed %d !\n",
  126. priv->cur_speed);
  127. break;
  128. }
  129. /* (Re)initialize the queue pointers */
  130. priv->rx_pointer = 0;
  131. priv->tx_clean_pointer = 0;
  132. priv->tx_pointer = 0;
  133. /* The doc says reset twice with 10us interval */
  134. if (ftgmac100_reset_mac(priv, maccr))
  135. return -EIO;
  136. usleep_range(10, 1000);
  137. return ftgmac100_reset_mac(priv, maccr);
  138. }
  139. static void ftgmac100_write_mac_addr(struct ftgmac100 *priv, const u8 *mac)
  140. {
  141. unsigned int maddr = mac[0] << 8 | mac[1];
  142. unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
  143. iowrite32(maddr, priv->base + FTGMAC100_OFFSET_MAC_MADR);
  144. iowrite32(laddr, priv->base + FTGMAC100_OFFSET_MAC_LADR);
  145. }
  146. static void ftgmac100_initial_mac(struct ftgmac100 *priv)
  147. {
  148. u8 mac[ETH_ALEN];
  149. unsigned int m;
  150. unsigned int l;
  151. if (!device_get_ethdev_address(priv->dev, priv->netdev)) {
  152. dev_info(priv->dev, "Read MAC address %pM from device tree\n",
  153. priv->netdev->dev_addr);
  154. return;
  155. }
  156. m = ioread32(priv->base + FTGMAC100_OFFSET_MAC_MADR);
  157. l = ioread32(priv->base + FTGMAC100_OFFSET_MAC_LADR);
  158. mac[0] = (m >> 8) & 0xff;
  159. mac[1] = m & 0xff;
  160. mac[2] = (l >> 24) & 0xff;
  161. mac[3] = (l >> 16) & 0xff;
  162. mac[4] = (l >> 8) & 0xff;
  163. mac[5] = l & 0xff;
  164. if (is_valid_ether_addr(mac)) {
  165. eth_hw_addr_set(priv->netdev, mac);
  166. dev_info(priv->dev, "Read MAC address %pM from chip\n", mac);
  167. } else {
  168. eth_hw_addr_random(priv->netdev);
  169. dev_info(priv->dev, "Generated random MAC address %pM\n",
  170. priv->netdev->dev_addr);
  171. }
  172. }
  173. static int ftgmac100_set_mac_addr(struct net_device *dev, void *p)
  174. {
  175. int ret;
  176. ret = eth_prepare_mac_addr_change(dev, p);
  177. if (ret < 0)
  178. return ret;
  179. eth_commit_mac_addr_change(dev, p);
  180. ftgmac100_write_mac_addr(netdev_priv(dev), dev->dev_addr);
  181. return 0;
  182. }
  183. static void ftgmac100_config_pause(struct ftgmac100 *priv)
  184. {
  185. u32 fcr = FTGMAC100_FCR_PAUSE_TIME(16);
  186. /* Throttle tx queue when receiving pause frames */
  187. if (priv->rx_pause)
  188. fcr |= FTGMAC100_FCR_FC_EN;
  189. /* Enables sending pause frames when the RX queue is past a
  190. * certain threshold.
  191. */
  192. if (priv->tx_pause)
  193. fcr |= FTGMAC100_FCR_FCTHR_EN;
  194. iowrite32(fcr, priv->base + FTGMAC100_OFFSET_FCR);
  195. }
  196. static void ftgmac100_init_hw(struct ftgmac100 *priv)
  197. {
  198. u32 reg, rfifo_sz, tfifo_sz;
  199. /* Clear stale interrupts */
  200. reg = ioread32(priv->base + FTGMAC100_OFFSET_ISR);
  201. iowrite32(reg, priv->base + FTGMAC100_OFFSET_ISR);
  202. /* Setup RX ring buffer base */
  203. iowrite32(priv->rxdes_dma, priv->base + FTGMAC100_OFFSET_RXR_BADR);
  204. /* Setup TX ring buffer base */
  205. iowrite32(priv->txdes_dma, priv->base + FTGMAC100_OFFSET_NPTXR_BADR);
  206. /* Configure RX buffer size */
  207. iowrite32(FTGMAC100_RBSR_SIZE(RX_BUF_SIZE),
  208. priv->base + FTGMAC100_OFFSET_RBSR);
  209. /* Set RX descriptor autopoll */
  210. iowrite32(FTGMAC100_APTC_RXPOLL_CNT(1),
  211. priv->base + FTGMAC100_OFFSET_APTC);
  212. /* Write MAC address */
  213. ftgmac100_write_mac_addr(priv, priv->netdev->dev_addr);
  214. /* Write multicast filter */
  215. iowrite32(priv->maht0, priv->base + FTGMAC100_OFFSET_MAHT0);
  216. iowrite32(priv->maht1, priv->base + FTGMAC100_OFFSET_MAHT1);
  217. /* Configure descriptor sizes and increase burst sizes according
  218. * to values in Aspeed SDK. The FIFO arbitration is enabled and
  219. * the thresholds set based on the recommended values in the
  220. * AST2400 specification.
  221. */
  222. iowrite32(FTGMAC100_DBLAC_RXDES_SIZE(2) | /* 2*8 bytes RX descs */
  223. FTGMAC100_DBLAC_TXDES_SIZE(2) | /* 2*8 bytes TX descs */
  224. FTGMAC100_DBLAC_RXBURST_SIZE(3) | /* 512 bytes max RX bursts */
  225. FTGMAC100_DBLAC_TXBURST_SIZE(3) | /* 512 bytes max TX bursts */
  226. FTGMAC100_DBLAC_RX_THR_EN | /* Enable fifo threshold arb */
  227. FTGMAC100_DBLAC_RXFIFO_HTHR(6) | /* 6/8 of FIFO high threshold */
  228. FTGMAC100_DBLAC_RXFIFO_LTHR(2), /* 2/8 of FIFO low threshold */
  229. priv->base + FTGMAC100_OFFSET_DBLAC);
  230. /* Interrupt mitigation configured for 1 interrupt/packet. HW interrupt
  231. * mitigation doesn't seem to provide any benefit with NAPI so leave
  232. * it at that.
  233. */
  234. iowrite32(FTGMAC100_ITC_RXINT_THR(1) |
  235. FTGMAC100_ITC_TXINT_THR(1),
  236. priv->base + FTGMAC100_OFFSET_ITC);
  237. /* Configure FIFO sizes in the TPAFCR register */
  238. reg = ioread32(priv->base + FTGMAC100_OFFSET_FEAR);
  239. rfifo_sz = reg & 0x00000007;
  240. tfifo_sz = (reg >> 3) & 0x00000007;
  241. reg = ioread32(priv->base + FTGMAC100_OFFSET_TPAFCR);
  242. reg &= ~0x3f000000;
  243. reg |= (tfifo_sz << 27);
  244. reg |= (rfifo_sz << 24);
  245. iowrite32(reg, priv->base + FTGMAC100_OFFSET_TPAFCR);
  246. }
  247. static void ftgmac100_start_hw(struct ftgmac100 *priv)
  248. {
  249. u32 maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
  250. /* Keep the original GMAC and FAST bits */
  251. maccr &= (FTGMAC100_MACCR_FAST_MODE | FTGMAC100_MACCR_GIGA_MODE);
  252. /* Add all the main enable bits */
  253. maccr |= FTGMAC100_MACCR_TXDMA_EN |
  254. FTGMAC100_MACCR_RXDMA_EN |
  255. FTGMAC100_MACCR_TXMAC_EN |
  256. FTGMAC100_MACCR_RXMAC_EN |
  257. FTGMAC100_MACCR_CRC_APD |
  258. FTGMAC100_MACCR_PHY_LINK_LEVEL |
  259. FTGMAC100_MACCR_RX_RUNT |
  260. FTGMAC100_MACCR_RX_BROADPKT;
  261. /* Add other bits as needed */
  262. if (priv->cur_duplex == DUPLEX_FULL)
  263. maccr |= FTGMAC100_MACCR_FULLDUP;
  264. if (priv->netdev->flags & IFF_PROMISC)
  265. maccr |= FTGMAC100_MACCR_RX_ALL;
  266. if (priv->netdev->flags & IFF_ALLMULTI)
  267. maccr |= FTGMAC100_MACCR_RX_MULTIPKT;
  268. else if (netdev_mc_count(priv->netdev))
  269. maccr |= FTGMAC100_MACCR_HT_MULTI_EN;
  270. /* Vlan filtering enabled */
  271. if (priv->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
  272. maccr |= FTGMAC100_MACCR_RM_VLAN;
  273. /* Hit the HW */
  274. iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
  275. }
  276. static void ftgmac100_stop_hw(struct ftgmac100 *priv)
  277. {
  278. iowrite32(0, priv->base + FTGMAC100_OFFSET_MACCR);
  279. }
  280. static void ftgmac100_calc_mc_hash(struct ftgmac100 *priv)
  281. {
  282. struct netdev_hw_addr *ha;
  283. priv->maht1 = 0;
  284. priv->maht0 = 0;
  285. netdev_for_each_mc_addr(ha, priv->netdev) {
  286. u32 crc_val = ether_crc_le(ETH_ALEN, ha->addr);
  287. crc_val = (~(crc_val >> 2)) & 0x3f;
  288. if (crc_val >= 32)
  289. priv->maht1 |= 1ul << (crc_val - 32);
  290. else
  291. priv->maht0 |= 1ul << (crc_val);
  292. }
  293. }
  294. static void ftgmac100_set_rx_mode(struct net_device *netdev)
  295. {
  296. struct ftgmac100 *priv = netdev_priv(netdev);
  297. /* Setup the hash filter */
  298. ftgmac100_calc_mc_hash(priv);
  299. /* Interface down ? that's all there is to do */
  300. if (!netif_running(netdev))
  301. return;
  302. /* Update the HW */
  303. iowrite32(priv->maht0, priv->base + FTGMAC100_OFFSET_MAHT0);
  304. iowrite32(priv->maht1, priv->base + FTGMAC100_OFFSET_MAHT1);
  305. /* Reconfigure MACCR */
  306. ftgmac100_start_hw(priv);
  307. }
  308. static int ftgmac100_alloc_rx_buf(struct ftgmac100 *priv, unsigned int entry,
  309. struct ftgmac100_rxdes *rxdes, gfp_t gfp)
  310. {
  311. struct net_device *netdev = priv->netdev;
  312. struct sk_buff *skb;
  313. dma_addr_t map;
  314. int err = 0;
  315. skb = netdev_alloc_skb_ip_align(netdev, RX_BUF_SIZE);
  316. if (unlikely(!skb)) {
  317. if (net_ratelimit())
  318. netdev_warn(netdev, "failed to allocate rx skb\n");
  319. err = -ENOMEM;
  320. map = priv->rx_scratch_dma;
  321. } else {
  322. map = dma_map_single(priv->dev, skb->data, RX_BUF_SIZE,
  323. DMA_FROM_DEVICE);
  324. if (unlikely(dma_mapping_error(priv->dev, map))) {
  325. if (net_ratelimit())
  326. netdev_err(netdev, "failed to map rx page\n");
  327. dev_kfree_skb_any(skb);
  328. map = priv->rx_scratch_dma;
  329. skb = NULL;
  330. err = -ENOMEM;
  331. }
  332. }
  333. /* Store skb */
  334. priv->rx_skbs[entry] = skb;
  335. /* Store DMA address into RX desc */
  336. rxdes->rxdes3 = cpu_to_le32(map);
  337. /* Ensure the above is ordered vs clearing the OWN bit */
  338. dma_wmb();
  339. /* Clean status (which resets own bit) */
  340. if (entry == (priv->rx_q_entries - 1))
  341. rxdes->rxdes0 = cpu_to_le32(priv->rxdes0_edorr_mask);
  342. else
  343. rxdes->rxdes0 = 0;
  344. return err;
  345. }
  346. static unsigned int ftgmac100_next_rx_pointer(struct ftgmac100 *priv,
  347. unsigned int pointer)
  348. {
  349. return (pointer + 1) & (priv->rx_q_entries - 1);
  350. }
  351. static void ftgmac100_rx_packet_error(struct ftgmac100 *priv, u32 status)
  352. {
  353. struct net_device *netdev = priv->netdev;
  354. if (status & FTGMAC100_RXDES0_RX_ERR)
  355. netdev->stats.rx_errors++;
  356. if (status & FTGMAC100_RXDES0_CRC_ERR)
  357. netdev->stats.rx_crc_errors++;
  358. if (status & (FTGMAC100_RXDES0_FTL |
  359. FTGMAC100_RXDES0_RUNT |
  360. FTGMAC100_RXDES0_RX_ODD_NB))
  361. netdev->stats.rx_length_errors++;
  362. }
  363. static bool ftgmac100_rx_packet(struct ftgmac100 *priv, int *processed)
  364. {
  365. struct net_device *netdev = priv->netdev;
  366. struct ftgmac100_rxdes *rxdes;
  367. struct sk_buff *skb;
  368. unsigned int pointer, size;
  369. u32 status, csum_vlan;
  370. dma_addr_t map;
  371. /* Grab next RX descriptor */
  372. pointer = priv->rx_pointer;
  373. rxdes = &priv->rxdes[pointer];
  374. /* Grab descriptor status */
  375. status = le32_to_cpu(rxdes->rxdes0);
  376. /* Do we have a packet ? */
  377. if (!(status & FTGMAC100_RXDES0_RXPKT_RDY))
  378. return false;
  379. /* Order subsequent reads with the test for the ready bit */
  380. dma_rmb();
  381. /* We don't cope with fragmented RX packets */
  382. if (unlikely(!(status & FTGMAC100_RXDES0_FRS) ||
  383. !(status & FTGMAC100_RXDES0_LRS)))
  384. goto drop;
  385. /* Grab received size and csum vlan field in the descriptor */
  386. size = status & FTGMAC100_RXDES0_VDBC;
  387. csum_vlan = le32_to_cpu(rxdes->rxdes1);
  388. /* Any error (other than csum offload) flagged ? */
  389. if (unlikely(status & RXDES0_ANY_ERROR)) {
  390. /* Correct for incorrect flagging of runt packets
  391. * with vlan tags... Just accept a runt packet that
  392. * has been flagged as vlan and whose size is at
  393. * least 60 bytes.
  394. */
  395. if ((status & FTGMAC100_RXDES0_RUNT) &&
  396. (csum_vlan & FTGMAC100_RXDES1_VLANTAG_AVAIL) &&
  397. (size >= 60))
  398. status &= ~FTGMAC100_RXDES0_RUNT;
  399. /* Any error still in there ? */
  400. if (status & RXDES0_ANY_ERROR) {
  401. ftgmac100_rx_packet_error(priv, status);
  402. goto drop;
  403. }
  404. }
  405. /* If the packet had no skb (failed to allocate earlier)
  406. * then try to allocate one and skip
  407. */
  408. skb = priv->rx_skbs[pointer];
  409. if (!unlikely(skb)) {
  410. ftgmac100_alloc_rx_buf(priv, pointer, rxdes, GFP_ATOMIC);
  411. goto drop;
  412. }
  413. if (unlikely(status & FTGMAC100_RXDES0_MULTICAST))
  414. netdev->stats.multicast++;
  415. /* If the HW found checksum errors, bounce it to software.
  416. *
  417. * If we didn't, we need to see if the packet was recognized
  418. * by HW as one of the supported checksummed protocols before
  419. * we accept the HW test results.
  420. */
  421. if (netdev->features & NETIF_F_RXCSUM) {
  422. u32 err_bits = FTGMAC100_RXDES1_TCP_CHKSUM_ERR |
  423. FTGMAC100_RXDES1_UDP_CHKSUM_ERR |
  424. FTGMAC100_RXDES1_IP_CHKSUM_ERR;
  425. if ((csum_vlan & err_bits) ||
  426. !(csum_vlan & FTGMAC100_RXDES1_PROT_MASK))
  427. skb->ip_summed = CHECKSUM_NONE;
  428. else
  429. skb->ip_summed = CHECKSUM_UNNECESSARY;
  430. }
  431. /* Transfer received size to skb */
  432. skb_put(skb, size);
  433. /* Extract vlan tag */
  434. if ((netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
  435. (csum_vlan & FTGMAC100_RXDES1_VLANTAG_AVAIL))
  436. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  437. csum_vlan & 0xffff);
  438. /* Tear down DMA mapping, do necessary cache management */
  439. map = le32_to_cpu(rxdes->rxdes3);
  440. #if defined(CONFIG_ARM) && !defined(CONFIG_ARM_DMA_USE_IOMMU)
  441. /* When we don't have an iommu, we can save cycles by not
  442. * invalidating the cache for the part of the packet that
  443. * wasn't received.
  444. */
  445. dma_unmap_single(priv->dev, map, size, DMA_FROM_DEVICE);
  446. #else
  447. dma_unmap_single(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
  448. #endif
  449. /* Resplenish rx ring */
  450. ftgmac100_alloc_rx_buf(priv, pointer, rxdes, GFP_ATOMIC);
  451. priv->rx_pointer = ftgmac100_next_rx_pointer(priv, pointer);
  452. skb->protocol = eth_type_trans(skb, netdev);
  453. netdev->stats.rx_packets++;
  454. netdev->stats.rx_bytes += size;
  455. /* push packet to protocol stack */
  456. if (skb->ip_summed == CHECKSUM_NONE)
  457. netif_receive_skb(skb);
  458. else
  459. napi_gro_receive(&priv->napi, skb);
  460. (*processed)++;
  461. return true;
  462. drop:
  463. /* Clean rxdes0 (which resets own bit) */
  464. rxdes->rxdes0 = cpu_to_le32(status & priv->rxdes0_edorr_mask);
  465. priv->rx_pointer = ftgmac100_next_rx_pointer(priv, pointer);
  466. netdev->stats.rx_dropped++;
  467. return true;
  468. }
  469. static u32 ftgmac100_base_tx_ctlstat(struct ftgmac100 *priv,
  470. unsigned int index)
  471. {
  472. if (index == (priv->tx_q_entries - 1))
  473. return priv->txdes0_edotr_mask;
  474. else
  475. return 0;
  476. }
  477. static unsigned int ftgmac100_next_tx_pointer(struct ftgmac100 *priv,
  478. unsigned int pointer)
  479. {
  480. return (pointer + 1) & (priv->tx_q_entries - 1);
  481. }
  482. static u32 ftgmac100_tx_buf_avail(struct ftgmac100 *priv)
  483. {
  484. /* Returns the number of available slots in the TX queue
  485. *
  486. * This always leaves one free slot so we don't have to
  487. * worry about empty vs. full, and this simplifies the
  488. * test for ftgmac100_tx_buf_cleanable() below
  489. */
  490. return (priv->tx_clean_pointer - priv->tx_pointer - 1) &
  491. (priv->tx_q_entries - 1);
  492. }
  493. static bool ftgmac100_tx_buf_cleanable(struct ftgmac100 *priv)
  494. {
  495. return priv->tx_pointer != priv->tx_clean_pointer;
  496. }
  497. static void ftgmac100_free_tx_packet(struct ftgmac100 *priv,
  498. unsigned int pointer,
  499. struct sk_buff *skb,
  500. struct ftgmac100_txdes *txdes,
  501. u32 ctl_stat)
  502. {
  503. dma_addr_t map = le32_to_cpu(txdes->txdes3);
  504. size_t len;
  505. if (ctl_stat & FTGMAC100_TXDES0_FTS) {
  506. len = skb_headlen(skb);
  507. dma_unmap_single(priv->dev, map, len, DMA_TO_DEVICE);
  508. } else {
  509. len = FTGMAC100_TXDES0_TXBUF_SIZE(ctl_stat);
  510. dma_unmap_page(priv->dev, map, len, DMA_TO_DEVICE);
  511. }
  512. /* Free SKB on last segment */
  513. if (ctl_stat & FTGMAC100_TXDES0_LTS)
  514. dev_kfree_skb(skb);
  515. priv->tx_skbs[pointer] = NULL;
  516. }
  517. static bool ftgmac100_tx_complete_packet(struct ftgmac100 *priv)
  518. {
  519. struct net_device *netdev = priv->netdev;
  520. struct ftgmac100_txdes *txdes;
  521. struct sk_buff *skb;
  522. unsigned int pointer;
  523. u32 ctl_stat;
  524. pointer = priv->tx_clean_pointer;
  525. txdes = &priv->txdes[pointer];
  526. ctl_stat = le32_to_cpu(txdes->txdes0);
  527. if (ctl_stat & FTGMAC100_TXDES0_TXDMA_OWN)
  528. return false;
  529. skb = priv->tx_skbs[pointer];
  530. netdev->stats.tx_packets++;
  531. netdev->stats.tx_bytes += skb->len;
  532. ftgmac100_free_tx_packet(priv, pointer, skb, txdes, ctl_stat);
  533. txdes->txdes0 = cpu_to_le32(ctl_stat & priv->txdes0_edotr_mask);
  534. priv->tx_clean_pointer = ftgmac100_next_tx_pointer(priv, pointer);
  535. return true;
  536. }
  537. static void ftgmac100_tx_complete(struct ftgmac100 *priv)
  538. {
  539. struct net_device *netdev = priv->netdev;
  540. /* Process all completed packets */
  541. while (ftgmac100_tx_buf_cleanable(priv) &&
  542. ftgmac100_tx_complete_packet(priv))
  543. ;
  544. /* Restart queue if needed */
  545. smp_mb();
  546. if (unlikely(netif_queue_stopped(netdev) &&
  547. ftgmac100_tx_buf_avail(priv) >= TX_THRESHOLD)) {
  548. struct netdev_queue *txq;
  549. txq = netdev_get_tx_queue(netdev, 0);
  550. __netif_tx_lock(txq, smp_processor_id());
  551. if (netif_queue_stopped(netdev) &&
  552. ftgmac100_tx_buf_avail(priv) >= TX_THRESHOLD)
  553. netif_wake_queue(netdev);
  554. __netif_tx_unlock(txq);
  555. }
  556. }
  557. static bool ftgmac100_prep_tx_csum(struct sk_buff *skb, u32 *csum_vlan)
  558. {
  559. if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
  560. u8 ip_proto = ip_hdr(skb)->protocol;
  561. *csum_vlan |= FTGMAC100_TXDES1_IP_CHKSUM;
  562. switch(ip_proto) {
  563. case IPPROTO_TCP:
  564. *csum_vlan |= FTGMAC100_TXDES1_TCP_CHKSUM;
  565. return true;
  566. case IPPROTO_UDP:
  567. *csum_vlan |= FTGMAC100_TXDES1_UDP_CHKSUM;
  568. return true;
  569. case IPPROTO_IP:
  570. return true;
  571. }
  572. }
  573. return skb_checksum_help(skb) == 0;
  574. }
  575. static netdev_tx_t ftgmac100_hard_start_xmit(struct sk_buff *skb,
  576. struct net_device *netdev)
  577. {
  578. struct ftgmac100 *priv = netdev_priv(netdev);
  579. struct ftgmac100_txdes *txdes, *first;
  580. unsigned int pointer, nfrags, len, i, j;
  581. u32 f_ctl_stat, ctl_stat, csum_vlan;
  582. dma_addr_t map;
  583. /* The HW doesn't pad small frames */
  584. if (eth_skb_pad(skb)) {
  585. netdev->stats.tx_dropped++;
  586. return NETDEV_TX_OK;
  587. }
  588. /* Reject oversize packets */
  589. if (unlikely(skb->len > MAX_PKT_SIZE)) {
  590. if (net_ratelimit())
  591. netdev_dbg(netdev, "tx packet too big\n");
  592. goto drop;
  593. }
  594. /* Do we have a limit on #fragments ? I yet have to get a reply
  595. * from Aspeed. If there's one I haven't hit it.
  596. */
  597. nfrags = skb_shinfo(skb)->nr_frags;
  598. /* Setup HW checksumming */
  599. csum_vlan = 0;
  600. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  601. !ftgmac100_prep_tx_csum(skb, &csum_vlan))
  602. goto drop;
  603. /* Add VLAN tag */
  604. if (skb_vlan_tag_present(skb)) {
  605. csum_vlan |= FTGMAC100_TXDES1_INS_VLANTAG;
  606. csum_vlan |= skb_vlan_tag_get(skb) & 0xffff;
  607. }
  608. /* Get header len */
  609. len = skb_headlen(skb);
  610. /* Map the packet head */
  611. map = dma_map_single(priv->dev, skb->data, len, DMA_TO_DEVICE);
  612. if (dma_mapping_error(priv->dev, map)) {
  613. if (net_ratelimit())
  614. netdev_err(netdev, "map tx packet head failed\n");
  615. goto drop;
  616. }
  617. /* Grab the next free tx descriptor */
  618. pointer = priv->tx_pointer;
  619. txdes = first = &priv->txdes[pointer];
  620. /* Setup it up with the packet head. Don't write the head to the
  621. * ring just yet
  622. */
  623. priv->tx_skbs[pointer] = skb;
  624. f_ctl_stat = ftgmac100_base_tx_ctlstat(priv, pointer);
  625. f_ctl_stat |= FTGMAC100_TXDES0_TXDMA_OWN;
  626. f_ctl_stat |= FTGMAC100_TXDES0_TXBUF_SIZE(len);
  627. f_ctl_stat |= FTGMAC100_TXDES0_FTS;
  628. if (nfrags == 0)
  629. f_ctl_stat |= FTGMAC100_TXDES0_LTS;
  630. txdes->txdes3 = cpu_to_le32(map);
  631. txdes->txdes1 = cpu_to_le32(csum_vlan);
  632. /* Next descriptor */
  633. pointer = ftgmac100_next_tx_pointer(priv, pointer);
  634. /* Add the fragments */
  635. for (i = 0; i < nfrags; i++) {
  636. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  637. len = skb_frag_size(frag);
  638. /* Map it */
  639. map = skb_frag_dma_map(priv->dev, frag, 0, len,
  640. DMA_TO_DEVICE);
  641. if (dma_mapping_error(priv->dev, map))
  642. goto dma_err;
  643. /* Setup descriptor */
  644. priv->tx_skbs[pointer] = skb;
  645. txdes = &priv->txdes[pointer];
  646. ctl_stat = ftgmac100_base_tx_ctlstat(priv, pointer);
  647. ctl_stat |= FTGMAC100_TXDES0_TXDMA_OWN;
  648. ctl_stat |= FTGMAC100_TXDES0_TXBUF_SIZE(len);
  649. if (i == (nfrags - 1))
  650. ctl_stat |= FTGMAC100_TXDES0_LTS;
  651. txdes->txdes0 = cpu_to_le32(ctl_stat);
  652. txdes->txdes1 = 0;
  653. txdes->txdes3 = cpu_to_le32(map);
  654. /* Next one */
  655. pointer = ftgmac100_next_tx_pointer(priv, pointer);
  656. }
  657. /* Order the previous packet and descriptor udpates
  658. * before setting the OWN bit on the first descriptor.
  659. */
  660. dma_wmb();
  661. first->txdes0 = cpu_to_le32(f_ctl_stat);
  662. /* Update next TX pointer */
  663. priv->tx_pointer = pointer;
  664. /* If there isn't enough room for all the fragments of a new packet
  665. * in the TX ring, stop the queue. The sequence below is race free
  666. * vs. a concurrent restart in ftgmac100_poll()
  667. */
  668. if (unlikely(ftgmac100_tx_buf_avail(priv) < TX_THRESHOLD)) {
  669. netif_stop_queue(netdev);
  670. /* Order the queue stop with the test below */
  671. smp_mb();
  672. if (ftgmac100_tx_buf_avail(priv) >= TX_THRESHOLD)
  673. netif_wake_queue(netdev);
  674. }
  675. /* Poke transmitter to read the updated TX descriptors */
  676. iowrite32(1, priv->base + FTGMAC100_OFFSET_NPTXPD);
  677. return NETDEV_TX_OK;
  678. dma_err:
  679. if (net_ratelimit())
  680. netdev_err(netdev, "map tx fragment failed\n");
  681. /* Free head */
  682. pointer = priv->tx_pointer;
  683. ftgmac100_free_tx_packet(priv, pointer, skb, first, f_ctl_stat);
  684. first->txdes0 = cpu_to_le32(f_ctl_stat & priv->txdes0_edotr_mask);
  685. /* Then all fragments */
  686. for (j = 0; j < i; j++) {
  687. pointer = ftgmac100_next_tx_pointer(priv, pointer);
  688. txdes = &priv->txdes[pointer];
  689. ctl_stat = le32_to_cpu(txdes->txdes0);
  690. ftgmac100_free_tx_packet(priv, pointer, skb, txdes, ctl_stat);
  691. txdes->txdes0 = cpu_to_le32(ctl_stat & priv->txdes0_edotr_mask);
  692. }
  693. /* This cannot be reached if we successfully mapped the
  694. * last fragment, so we know ftgmac100_free_tx_packet()
  695. * hasn't freed the skb yet.
  696. */
  697. drop:
  698. /* Drop the packet */
  699. dev_kfree_skb_any(skb);
  700. netdev->stats.tx_dropped++;
  701. return NETDEV_TX_OK;
  702. }
  703. static void ftgmac100_free_buffers(struct ftgmac100 *priv)
  704. {
  705. int i;
  706. /* Free all RX buffers */
  707. for (i = 0; i < priv->rx_q_entries; i++) {
  708. struct ftgmac100_rxdes *rxdes = &priv->rxdes[i];
  709. struct sk_buff *skb = priv->rx_skbs[i];
  710. dma_addr_t map = le32_to_cpu(rxdes->rxdes3);
  711. if (!skb)
  712. continue;
  713. priv->rx_skbs[i] = NULL;
  714. dma_unmap_single(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
  715. dev_kfree_skb_any(skb);
  716. }
  717. /* Free all TX buffers */
  718. for (i = 0; i < priv->tx_q_entries; i++) {
  719. struct ftgmac100_txdes *txdes = &priv->txdes[i];
  720. struct sk_buff *skb = priv->tx_skbs[i];
  721. if (!skb)
  722. continue;
  723. ftgmac100_free_tx_packet(priv, i, skb, txdes,
  724. le32_to_cpu(txdes->txdes0));
  725. }
  726. }
  727. static void ftgmac100_free_rings(struct ftgmac100 *priv)
  728. {
  729. /* Free skb arrays */
  730. kfree(priv->rx_skbs);
  731. kfree(priv->tx_skbs);
  732. /* Free descriptors */
  733. if (priv->rxdes)
  734. dma_free_coherent(priv->dev, MAX_RX_QUEUE_ENTRIES *
  735. sizeof(struct ftgmac100_rxdes),
  736. priv->rxdes, priv->rxdes_dma);
  737. priv->rxdes = NULL;
  738. if (priv->txdes)
  739. dma_free_coherent(priv->dev, MAX_TX_QUEUE_ENTRIES *
  740. sizeof(struct ftgmac100_txdes),
  741. priv->txdes, priv->txdes_dma);
  742. priv->txdes = NULL;
  743. /* Free scratch packet buffer */
  744. if (priv->rx_scratch)
  745. dma_free_coherent(priv->dev, RX_BUF_SIZE,
  746. priv->rx_scratch, priv->rx_scratch_dma);
  747. }
  748. static int ftgmac100_alloc_rings(struct ftgmac100 *priv)
  749. {
  750. /* Allocate skb arrays */
  751. priv->rx_skbs = kcalloc(MAX_RX_QUEUE_ENTRIES, sizeof(void *),
  752. GFP_KERNEL);
  753. if (!priv->rx_skbs)
  754. return -ENOMEM;
  755. priv->tx_skbs = kcalloc(MAX_TX_QUEUE_ENTRIES, sizeof(void *),
  756. GFP_KERNEL);
  757. if (!priv->tx_skbs)
  758. return -ENOMEM;
  759. /* Allocate descriptors */
  760. priv->rxdes = dma_alloc_coherent(priv->dev,
  761. MAX_RX_QUEUE_ENTRIES * sizeof(struct ftgmac100_rxdes),
  762. &priv->rxdes_dma, GFP_KERNEL);
  763. if (!priv->rxdes)
  764. return -ENOMEM;
  765. priv->txdes = dma_alloc_coherent(priv->dev,
  766. MAX_TX_QUEUE_ENTRIES * sizeof(struct ftgmac100_txdes),
  767. &priv->txdes_dma, GFP_KERNEL);
  768. if (!priv->txdes)
  769. return -ENOMEM;
  770. /* Allocate scratch packet buffer */
  771. priv->rx_scratch = dma_alloc_coherent(priv->dev,
  772. RX_BUF_SIZE,
  773. &priv->rx_scratch_dma,
  774. GFP_KERNEL);
  775. if (!priv->rx_scratch)
  776. return -ENOMEM;
  777. return 0;
  778. }
  779. static void ftgmac100_init_rings(struct ftgmac100 *priv)
  780. {
  781. struct ftgmac100_rxdes *rxdes = NULL;
  782. struct ftgmac100_txdes *txdes = NULL;
  783. int i;
  784. /* Update entries counts */
  785. priv->rx_q_entries = priv->new_rx_q_entries;
  786. priv->tx_q_entries = priv->new_tx_q_entries;
  787. if (WARN_ON(priv->rx_q_entries < MIN_RX_QUEUE_ENTRIES))
  788. return;
  789. /* Initialize RX ring */
  790. for (i = 0; i < priv->rx_q_entries; i++) {
  791. rxdes = &priv->rxdes[i];
  792. rxdes->rxdes0 = 0;
  793. rxdes->rxdes3 = cpu_to_le32(priv->rx_scratch_dma);
  794. }
  795. /* Mark the end of the ring */
  796. rxdes->rxdes0 |= cpu_to_le32(priv->rxdes0_edorr_mask);
  797. if (WARN_ON(priv->tx_q_entries < MIN_RX_QUEUE_ENTRIES))
  798. return;
  799. /* Initialize TX ring */
  800. for (i = 0; i < priv->tx_q_entries; i++) {
  801. txdes = &priv->txdes[i];
  802. txdes->txdes0 = 0;
  803. }
  804. txdes->txdes0 |= cpu_to_le32(priv->txdes0_edotr_mask);
  805. }
  806. static int ftgmac100_alloc_rx_buffers(struct ftgmac100 *priv)
  807. {
  808. int i;
  809. for (i = 0; i < priv->rx_q_entries; i++) {
  810. struct ftgmac100_rxdes *rxdes = &priv->rxdes[i];
  811. if (ftgmac100_alloc_rx_buf(priv, i, rxdes, GFP_KERNEL))
  812. return -ENOMEM;
  813. }
  814. return 0;
  815. }
  816. static int ftgmac100_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
  817. {
  818. struct net_device *netdev = bus->priv;
  819. struct ftgmac100 *priv = netdev_priv(netdev);
  820. unsigned int phycr;
  821. int i;
  822. phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
  823. /* preserve MDC cycle threshold */
  824. phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
  825. phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
  826. FTGMAC100_PHYCR_REGAD(regnum) |
  827. FTGMAC100_PHYCR_MIIRD;
  828. iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
  829. for (i = 0; i < 10; i++) {
  830. phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
  831. if ((phycr & FTGMAC100_PHYCR_MIIRD) == 0) {
  832. int data;
  833. data = ioread32(priv->base + FTGMAC100_OFFSET_PHYDATA);
  834. return FTGMAC100_PHYDATA_MIIRDATA(data);
  835. }
  836. udelay(100);
  837. }
  838. netdev_err(netdev, "mdio read timed out\n");
  839. return -EIO;
  840. }
  841. static int ftgmac100_mdiobus_write(struct mii_bus *bus, int phy_addr,
  842. int regnum, u16 value)
  843. {
  844. struct net_device *netdev = bus->priv;
  845. struct ftgmac100 *priv = netdev_priv(netdev);
  846. unsigned int phycr;
  847. int data;
  848. int i;
  849. phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
  850. /* preserve MDC cycle threshold */
  851. phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
  852. phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
  853. FTGMAC100_PHYCR_REGAD(regnum) |
  854. FTGMAC100_PHYCR_MIIWR;
  855. data = FTGMAC100_PHYDATA_MIIWDATA(value);
  856. iowrite32(data, priv->base + FTGMAC100_OFFSET_PHYDATA);
  857. iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
  858. for (i = 0; i < 10; i++) {
  859. phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
  860. if ((phycr & FTGMAC100_PHYCR_MIIWR) == 0)
  861. return 0;
  862. udelay(100);
  863. }
  864. netdev_err(netdev, "mdio write timed out\n");
  865. return -EIO;
  866. }
  867. static void ftgmac100_get_drvinfo(struct net_device *netdev,
  868. struct ethtool_drvinfo *info)
  869. {
  870. strscpy(info->driver, DRV_NAME, sizeof(info->driver));
  871. strscpy(info->bus_info, dev_name(&netdev->dev), sizeof(info->bus_info));
  872. }
  873. static void
  874. ftgmac100_get_ringparam(struct net_device *netdev,
  875. struct ethtool_ringparam *ering,
  876. struct kernel_ethtool_ringparam *kernel_ering,
  877. struct netlink_ext_ack *extack)
  878. {
  879. struct ftgmac100 *priv = netdev_priv(netdev);
  880. memset(ering, 0, sizeof(*ering));
  881. ering->rx_max_pending = MAX_RX_QUEUE_ENTRIES;
  882. ering->tx_max_pending = MAX_TX_QUEUE_ENTRIES;
  883. ering->rx_pending = priv->rx_q_entries;
  884. ering->tx_pending = priv->tx_q_entries;
  885. }
  886. static int
  887. ftgmac100_set_ringparam(struct net_device *netdev,
  888. struct ethtool_ringparam *ering,
  889. struct kernel_ethtool_ringparam *kernel_ering,
  890. struct netlink_ext_ack *extack)
  891. {
  892. struct ftgmac100 *priv = netdev_priv(netdev);
  893. if (ering->rx_pending > MAX_RX_QUEUE_ENTRIES ||
  894. ering->tx_pending > MAX_TX_QUEUE_ENTRIES ||
  895. ering->rx_pending < MIN_RX_QUEUE_ENTRIES ||
  896. ering->tx_pending < MIN_TX_QUEUE_ENTRIES ||
  897. !is_power_of_2(ering->rx_pending) ||
  898. !is_power_of_2(ering->tx_pending))
  899. return -EINVAL;
  900. priv->new_rx_q_entries = ering->rx_pending;
  901. priv->new_tx_q_entries = ering->tx_pending;
  902. if (netif_running(netdev))
  903. schedule_work(&priv->reset_task);
  904. return 0;
  905. }
  906. static void ftgmac100_get_pauseparam(struct net_device *netdev,
  907. struct ethtool_pauseparam *pause)
  908. {
  909. struct ftgmac100 *priv = netdev_priv(netdev);
  910. pause->autoneg = priv->aneg_pause;
  911. pause->tx_pause = priv->tx_pause;
  912. pause->rx_pause = priv->rx_pause;
  913. }
  914. static int ftgmac100_set_pauseparam(struct net_device *netdev,
  915. struct ethtool_pauseparam *pause)
  916. {
  917. struct ftgmac100 *priv = netdev_priv(netdev);
  918. struct phy_device *phydev = netdev->phydev;
  919. priv->aneg_pause = pause->autoneg;
  920. priv->tx_pause = pause->tx_pause;
  921. priv->rx_pause = pause->rx_pause;
  922. if (phydev)
  923. phy_set_asym_pause(phydev, pause->rx_pause, pause->tx_pause);
  924. if (netif_running(netdev)) {
  925. if (!(phydev && priv->aneg_pause))
  926. ftgmac100_config_pause(priv);
  927. }
  928. return 0;
  929. }
  930. static const struct ethtool_ops ftgmac100_ethtool_ops = {
  931. .get_drvinfo = ftgmac100_get_drvinfo,
  932. .get_link = ethtool_op_get_link,
  933. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  934. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  935. .nway_reset = phy_ethtool_nway_reset,
  936. .get_ringparam = ftgmac100_get_ringparam,
  937. .set_ringparam = ftgmac100_set_ringparam,
  938. .get_pauseparam = ftgmac100_get_pauseparam,
  939. .set_pauseparam = ftgmac100_set_pauseparam,
  940. };
  941. static irqreturn_t ftgmac100_interrupt(int irq, void *dev_id)
  942. {
  943. struct net_device *netdev = dev_id;
  944. struct ftgmac100 *priv = netdev_priv(netdev);
  945. unsigned int status, new_mask = FTGMAC100_INT_BAD;
  946. /* Fetch and clear interrupt bits, process abnormal ones */
  947. status = ioread32(priv->base + FTGMAC100_OFFSET_ISR);
  948. iowrite32(status, priv->base + FTGMAC100_OFFSET_ISR);
  949. if (unlikely(status & FTGMAC100_INT_BAD)) {
  950. /* RX buffer unavailable */
  951. if (status & FTGMAC100_INT_NO_RXBUF)
  952. netdev->stats.rx_over_errors++;
  953. /* received packet lost due to RX FIFO full */
  954. if (status & FTGMAC100_INT_RPKT_LOST)
  955. netdev->stats.rx_fifo_errors++;
  956. /* sent packet lost due to excessive TX collision */
  957. if (status & FTGMAC100_INT_XPKT_LOST)
  958. netdev->stats.tx_fifo_errors++;
  959. /* AHB error -> Reset the chip */
  960. if (status & FTGMAC100_INT_AHB_ERR) {
  961. if (net_ratelimit())
  962. netdev_warn(netdev,
  963. "AHB bus error ! Resetting chip.\n");
  964. iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
  965. schedule_work(&priv->reset_task);
  966. return IRQ_HANDLED;
  967. }
  968. /* We may need to restart the MAC after such errors, delay
  969. * this until after we have freed some Rx buffers though
  970. */
  971. priv->need_mac_restart = true;
  972. /* Disable those errors until we restart */
  973. new_mask &= ~status;
  974. }
  975. /* Only enable "bad" interrupts while NAPI is on */
  976. iowrite32(new_mask, priv->base + FTGMAC100_OFFSET_IER);
  977. /* Schedule NAPI bh */
  978. napi_schedule_irqoff(&priv->napi);
  979. return IRQ_HANDLED;
  980. }
  981. static bool ftgmac100_check_rx(struct ftgmac100 *priv)
  982. {
  983. struct ftgmac100_rxdes *rxdes = &priv->rxdes[priv->rx_pointer];
  984. /* Do we have a packet ? */
  985. return !!(rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RXPKT_RDY));
  986. }
  987. static int ftgmac100_poll(struct napi_struct *napi, int budget)
  988. {
  989. struct ftgmac100 *priv = container_of(napi, struct ftgmac100, napi);
  990. int work_done = 0;
  991. bool more;
  992. /* Handle TX completions */
  993. if (ftgmac100_tx_buf_cleanable(priv))
  994. ftgmac100_tx_complete(priv);
  995. /* Handle RX packets */
  996. do {
  997. more = ftgmac100_rx_packet(priv, &work_done);
  998. } while (more && work_done < budget);
  999. /* The interrupt is telling us to kick the MAC back to life
  1000. * after an RX overflow
  1001. */
  1002. if (unlikely(priv->need_mac_restart)) {
  1003. ftgmac100_start_hw(priv);
  1004. priv->need_mac_restart = false;
  1005. /* Re-enable "bad" interrupts */
  1006. iowrite32(FTGMAC100_INT_BAD,
  1007. priv->base + FTGMAC100_OFFSET_IER);
  1008. }
  1009. /* As long as we are waiting for transmit packets to be
  1010. * completed we keep NAPI going
  1011. */
  1012. if (ftgmac100_tx_buf_cleanable(priv))
  1013. work_done = budget;
  1014. if (work_done < budget) {
  1015. /* We are about to re-enable all interrupts. However
  1016. * the HW has been latching RX/TX packet interrupts while
  1017. * they were masked. So we clear them first, then we need
  1018. * to re-check if there's something to process
  1019. */
  1020. iowrite32(FTGMAC100_INT_RXTX,
  1021. priv->base + FTGMAC100_OFFSET_ISR);
  1022. /* Push the above (and provides a barrier vs. subsequent
  1023. * reads of the descriptor).
  1024. */
  1025. ioread32(priv->base + FTGMAC100_OFFSET_ISR);
  1026. /* Check RX and TX descriptors for more work to do */
  1027. if (ftgmac100_check_rx(priv) ||
  1028. ftgmac100_tx_buf_cleanable(priv))
  1029. return budget;
  1030. /* deschedule NAPI */
  1031. napi_complete(napi);
  1032. /* enable all interrupts */
  1033. iowrite32(FTGMAC100_INT_ALL,
  1034. priv->base + FTGMAC100_OFFSET_IER);
  1035. }
  1036. return work_done;
  1037. }
  1038. static int ftgmac100_init_all(struct ftgmac100 *priv, bool ignore_alloc_err)
  1039. {
  1040. int err = 0;
  1041. /* Re-init descriptors (adjust queue sizes) */
  1042. ftgmac100_init_rings(priv);
  1043. /* Realloc rx descriptors */
  1044. err = ftgmac100_alloc_rx_buffers(priv);
  1045. if (err && !ignore_alloc_err)
  1046. return err;
  1047. /* Reinit and restart HW */
  1048. ftgmac100_init_hw(priv);
  1049. ftgmac100_config_pause(priv);
  1050. ftgmac100_start_hw(priv);
  1051. /* Re-enable the device */
  1052. napi_enable(&priv->napi);
  1053. netif_start_queue(priv->netdev);
  1054. /* Enable all interrupts */
  1055. iowrite32(FTGMAC100_INT_ALL, priv->base + FTGMAC100_OFFSET_IER);
  1056. return err;
  1057. }
  1058. static void ftgmac100_reset(struct ftgmac100 *priv)
  1059. {
  1060. struct net_device *netdev = priv->netdev;
  1061. int err;
  1062. netdev_dbg(netdev, "Resetting NIC...\n");
  1063. /* Lock the world */
  1064. rtnl_lock();
  1065. if (netdev->phydev)
  1066. mutex_lock(&netdev->phydev->lock);
  1067. if (priv->mii_bus)
  1068. mutex_lock(&priv->mii_bus->mdio_lock);
  1069. /* Check if the interface is still up */
  1070. if (!netif_running(netdev))
  1071. goto bail;
  1072. /* Stop the network stack */
  1073. netif_trans_update(netdev);
  1074. napi_disable(&priv->napi);
  1075. netif_tx_disable(netdev);
  1076. /* Stop and reset the MAC */
  1077. ftgmac100_stop_hw(priv);
  1078. err = ftgmac100_reset_and_config_mac(priv);
  1079. if (err) {
  1080. /* Not much we can do ... it might come back... */
  1081. netdev_err(netdev, "attempting to continue...\n");
  1082. }
  1083. /* Free all rx and tx buffers */
  1084. ftgmac100_free_buffers(priv);
  1085. /* Setup everything again and restart chip */
  1086. ftgmac100_init_all(priv, true);
  1087. netdev_dbg(netdev, "Reset done !\n");
  1088. bail:
  1089. if (priv->mii_bus)
  1090. mutex_unlock(&priv->mii_bus->mdio_lock);
  1091. if (netdev->phydev)
  1092. mutex_unlock(&netdev->phydev->lock);
  1093. rtnl_unlock();
  1094. }
  1095. static void ftgmac100_reset_task(struct work_struct *work)
  1096. {
  1097. struct ftgmac100 *priv = container_of(work, struct ftgmac100,
  1098. reset_task);
  1099. ftgmac100_reset(priv);
  1100. }
  1101. static void ftgmac100_adjust_link(struct net_device *netdev)
  1102. {
  1103. struct ftgmac100 *priv = netdev_priv(netdev);
  1104. struct phy_device *phydev = netdev->phydev;
  1105. bool tx_pause, rx_pause;
  1106. int new_speed;
  1107. /* We store "no link" as speed 0 */
  1108. if (!phydev->link)
  1109. new_speed = 0;
  1110. else
  1111. new_speed = phydev->speed;
  1112. /* Grab pause settings from PHY if configured to do so */
  1113. if (priv->aneg_pause) {
  1114. rx_pause = tx_pause = phydev->pause;
  1115. if (phydev->asym_pause)
  1116. tx_pause = !rx_pause;
  1117. } else {
  1118. rx_pause = priv->rx_pause;
  1119. tx_pause = priv->tx_pause;
  1120. }
  1121. /* Link hasn't changed, do nothing */
  1122. if (phydev->speed == priv->cur_speed &&
  1123. phydev->duplex == priv->cur_duplex &&
  1124. rx_pause == priv->rx_pause &&
  1125. tx_pause == priv->tx_pause)
  1126. return;
  1127. /* Print status if we have a link or we had one and just lost it,
  1128. * don't print otherwise.
  1129. */
  1130. if (new_speed || priv->cur_speed)
  1131. phy_print_status(phydev);
  1132. priv->cur_speed = new_speed;
  1133. priv->cur_duplex = phydev->duplex;
  1134. priv->rx_pause = rx_pause;
  1135. priv->tx_pause = tx_pause;
  1136. /* Link is down, do nothing else */
  1137. if (!new_speed)
  1138. return;
  1139. /* Disable all interrupts */
  1140. iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
  1141. /* Release phy lock to allow ftgmac100_reset to aquire it, keeping lock
  1142. * order consistent to prevent dead lock.
  1143. */
  1144. if (netdev->phydev)
  1145. mutex_unlock(&netdev->phydev->lock);
  1146. ftgmac100_reset(priv);
  1147. if (netdev->phydev)
  1148. mutex_lock(&netdev->phydev->lock);
  1149. }
  1150. static int ftgmac100_mii_probe(struct net_device *netdev)
  1151. {
  1152. struct ftgmac100 *priv = netdev_priv(netdev);
  1153. struct platform_device *pdev = to_platform_device(priv->dev);
  1154. struct device_node *np = pdev->dev.of_node;
  1155. struct phy_device *phydev;
  1156. phy_interface_t phy_intf;
  1157. int err;
  1158. /* Default to RGMII. It's a gigabit part after all */
  1159. err = of_get_phy_mode(np, &phy_intf);
  1160. if (err)
  1161. phy_intf = PHY_INTERFACE_MODE_RGMII;
  1162. /* Aspeed only supports these. I don't know about other IP
  1163. * block vendors so I'm going to just let them through for
  1164. * now. Note that this is only a warning if for some obscure
  1165. * reason the DT really means to lie about it or it's a newer
  1166. * part we don't know about.
  1167. *
  1168. * On the Aspeed SoC there are additionally straps and SCU
  1169. * control bits that could tell us what the interface is
  1170. * (or allow us to configure it while the IP block is held
  1171. * in reset). For now I chose to keep this driver away from
  1172. * those SoC specific bits and assume the device-tree is
  1173. * right and the SCU has been configured properly by pinmux
  1174. * or the firmware.
  1175. */
  1176. if (priv->is_aspeed && !(phy_interface_mode_is_rgmii(phy_intf))) {
  1177. netdev_warn(netdev,
  1178. "Unsupported PHY mode %s !\n",
  1179. phy_modes(phy_intf));
  1180. }
  1181. phydev = phy_find_first(priv->mii_bus);
  1182. if (!phydev) {
  1183. netdev_info(netdev, "%s: no PHY found\n", netdev->name);
  1184. return -ENODEV;
  1185. }
  1186. phydev = phy_connect(netdev, phydev_name(phydev),
  1187. &ftgmac100_adjust_link, phy_intf);
  1188. if (IS_ERR(phydev)) {
  1189. netdev_err(netdev, "%s: Could not attach to PHY\n", netdev->name);
  1190. return PTR_ERR(phydev);
  1191. }
  1192. /* Indicate that we support PAUSE frames (see comment in
  1193. * Documentation/networking/phy.rst)
  1194. */
  1195. phy_support_asym_pause(phydev);
  1196. /* Display what we found */
  1197. phy_attached_info(phydev);
  1198. return 0;
  1199. }
  1200. static int ftgmac100_open(struct net_device *netdev)
  1201. {
  1202. struct ftgmac100 *priv = netdev_priv(netdev);
  1203. int err;
  1204. /* Allocate ring buffers */
  1205. err = ftgmac100_alloc_rings(priv);
  1206. if (err) {
  1207. netdev_err(netdev, "Failed to allocate descriptors\n");
  1208. return err;
  1209. }
  1210. /* When using NC-SI we force the speed to 100Mbit/s full duplex,
  1211. *
  1212. * Otherwise we leave it set to 0 (no link), the link
  1213. * message from the PHY layer will handle setting it up to
  1214. * something else if needed.
  1215. */
  1216. if (priv->use_ncsi) {
  1217. priv->cur_duplex = DUPLEX_FULL;
  1218. priv->cur_speed = SPEED_100;
  1219. } else {
  1220. priv->cur_duplex = 0;
  1221. priv->cur_speed = 0;
  1222. }
  1223. /* Reset the hardware */
  1224. err = ftgmac100_reset_and_config_mac(priv);
  1225. if (err)
  1226. goto err_hw;
  1227. /* Initialize NAPI */
  1228. netif_napi_add(netdev, &priv->napi, ftgmac100_poll);
  1229. /* Grab our interrupt */
  1230. err = request_irq(netdev->irq, ftgmac100_interrupt, 0, netdev->name, netdev);
  1231. if (err) {
  1232. netdev_err(netdev, "failed to request irq %d\n", netdev->irq);
  1233. goto err_irq;
  1234. }
  1235. /* Start things up */
  1236. err = ftgmac100_init_all(priv, false);
  1237. if (err) {
  1238. netdev_err(netdev, "Failed to allocate packet buffers\n");
  1239. goto err_alloc;
  1240. }
  1241. if (netdev->phydev) {
  1242. /* If we have a PHY, start polling */
  1243. phy_start(netdev->phydev);
  1244. } else if (priv->use_ncsi) {
  1245. /* If using NC-SI, set our carrier on and start the stack */
  1246. netif_carrier_on(netdev);
  1247. /* Start the NCSI device */
  1248. err = ncsi_start_dev(priv->ndev);
  1249. if (err)
  1250. goto err_ncsi;
  1251. }
  1252. return 0;
  1253. err_ncsi:
  1254. napi_disable(&priv->napi);
  1255. netif_stop_queue(netdev);
  1256. err_alloc:
  1257. ftgmac100_free_buffers(priv);
  1258. free_irq(netdev->irq, netdev);
  1259. err_irq:
  1260. netif_napi_del(&priv->napi);
  1261. err_hw:
  1262. iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
  1263. ftgmac100_free_rings(priv);
  1264. return err;
  1265. }
  1266. static int ftgmac100_stop(struct net_device *netdev)
  1267. {
  1268. struct ftgmac100 *priv = netdev_priv(netdev);
  1269. /* Note about the reset task: We are called with the rtnl lock
  1270. * held, so we are synchronized against the core of the reset
  1271. * task. We must not try to synchronously cancel it otherwise
  1272. * we can deadlock. But since it will test for netif_running()
  1273. * which has already been cleared by the net core, we don't
  1274. * anything special to do.
  1275. */
  1276. /* disable all interrupts */
  1277. iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
  1278. netif_stop_queue(netdev);
  1279. napi_disable(&priv->napi);
  1280. netif_napi_del(&priv->napi);
  1281. if (netdev->phydev)
  1282. phy_stop(netdev->phydev);
  1283. else if (priv->use_ncsi)
  1284. ncsi_stop_dev(priv->ndev);
  1285. ftgmac100_stop_hw(priv);
  1286. free_irq(netdev->irq, netdev);
  1287. ftgmac100_free_buffers(priv);
  1288. ftgmac100_free_rings(priv);
  1289. return 0;
  1290. }
  1291. static void ftgmac100_tx_timeout(struct net_device *netdev, unsigned int txqueue)
  1292. {
  1293. struct ftgmac100 *priv = netdev_priv(netdev);
  1294. /* Disable all interrupts */
  1295. iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
  1296. /* Do the reset outside of interrupt context */
  1297. schedule_work(&priv->reset_task);
  1298. }
  1299. static int ftgmac100_set_features(struct net_device *netdev,
  1300. netdev_features_t features)
  1301. {
  1302. struct ftgmac100 *priv = netdev_priv(netdev);
  1303. netdev_features_t changed = netdev->features ^ features;
  1304. if (!netif_running(netdev))
  1305. return 0;
  1306. /* Update the vlan filtering bit */
  1307. if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
  1308. u32 maccr;
  1309. maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
  1310. if (priv->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
  1311. maccr |= FTGMAC100_MACCR_RM_VLAN;
  1312. else
  1313. maccr &= ~FTGMAC100_MACCR_RM_VLAN;
  1314. iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
  1315. }
  1316. return 0;
  1317. }
  1318. #ifdef CONFIG_NET_POLL_CONTROLLER
  1319. static void ftgmac100_poll_controller(struct net_device *netdev)
  1320. {
  1321. unsigned long flags;
  1322. local_irq_save(flags);
  1323. ftgmac100_interrupt(netdev->irq, netdev);
  1324. local_irq_restore(flags);
  1325. }
  1326. #endif
  1327. static const struct net_device_ops ftgmac100_netdev_ops = {
  1328. .ndo_open = ftgmac100_open,
  1329. .ndo_stop = ftgmac100_stop,
  1330. .ndo_start_xmit = ftgmac100_hard_start_xmit,
  1331. .ndo_set_mac_address = ftgmac100_set_mac_addr,
  1332. .ndo_validate_addr = eth_validate_addr,
  1333. .ndo_eth_ioctl = phy_do_ioctl,
  1334. .ndo_tx_timeout = ftgmac100_tx_timeout,
  1335. .ndo_set_rx_mode = ftgmac100_set_rx_mode,
  1336. .ndo_set_features = ftgmac100_set_features,
  1337. #ifdef CONFIG_NET_POLL_CONTROLLER
  1338. .ndo_poll_controller = ftgmac100_poll_controller,
  1339. #endif
  1340. .ndo_vlan_rx_add_vid = ncsi_vlan_rx_add_vid,
  1341. .ndo_vlan_rx_kill_vid = ncsi_vlan_rx_kill_vid,
  1342. };
  1343. static int ftgmac100_setup_mdio(struct net_device *netdev)
  1344. {
  1345. struct ftgmac100 *priv = netdev_priv(netdev);
  1346. struct platform_device *pdev = to_platform_device(priv->dev);
  1347. struct device_node *np = pdev->dev.of_node;
  1348. struct device_node *mdio_np;
  1349. int i, err = 0;
  1350. u32 reg;
  1351. /* initialize mdio bus */
  1352. priv->mii_bus = mdiobus_alloc();
  1353. if (!priv->mii_bus)
  1354. return -EIO;
  1355. if (of_device_is_compatible(np, "aspeed,ast2400-mac") ||
  1356. of_device_is_compatible(np, "aspeed,ast2500-mac")) {
  1357. /* The AST2600 has a separate MDIO controller */
  1358. /* For the AST2400 and AST2500 this driver only supports the
  1359. * old MDIO interface
  1360. */
  1361. reg = ioread32(priv->base + FTGMAC100_OFFSET_REVR);
  1362. reg &= ~FTGMAC100_REVR_NEW_MDIO_INTERFACE;
  1363. iowrite32(reg, priv->base + FTGMAC100_OFFSET_REVR);
  1364. }
  1365. priv->mii_bus->name = "ftgmac100_mdio";
  1366. snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%d",
  1367. pdev->name, pdev->id);
  1368. priv->mii_bus->parent = priv->dev;
  1369. priv->mii_bus->priv = priv->netdev;
  1370. priv->mii_bus->read = ftgmac100_mdiobus_read;
  1371. priv->mii_bus->write = ftgmac100_mdiobus_write;
  1372. for (i = 0; i < PHY_MAX_ADDR; i++)
  1373. priv->mii_bus->irq[i] = PHY_POLL;
  1374. mdio_np = of_get_child_by_name(np, "mdio");
  1375. err = of_mdiobus_register(priv->mii_bus, mdio_np);
  1376. if (err) {
  1377. dev_err(priv->dev, "Cannot register MDIO bus!\n");
  1378. goto err_register_mdiobus;
  1379. }
  1380. of_node_put(mdio_np);
  1381. return 0;
  1382. err_register_mdiobus:
  1383. mdiobus_free(priv->mii_bus);
  1384. return err;
  1385. }
  1386. static void ftgmac100_phy_disconnect(struct net_device *netdev)
  1387. {
  1388. struct ftgmac100 *priv = netdev_priv(netdev);
  1389. if (!netdev->phydev)
  1390. return;
  1391. phy_disconnect(netdev->phydev);
  1392. if (of_phy_is_fixed_link(priv->dev->of_node))
  1393. of_phy_deregister_fixed_link(priv->dev->of_node);
  1394. }
  1395. static void ftgmac100_destroy_mdio(struct net_device *netdev)
  1396. {
  1397. struct ftgmac100 *priv = netdev_priv(netdev);
  1398. if (!priv->mii_bus)
  1399. return;
  1400. mdiobus_unregister(priv->mii_bus);
  1401. mdiobus_free(priv->mii_bus);
  1402. }
  1403. static void ftgmac100_ncsi_handler(struct ncsi_dev *nd)
  1404. {
  1405. if (unlikely(nd->state != ncsi_dev_state_functional))
  1406. return;
  1407. netdev_dbg(nd->dev, "NCSI interface %s\n",
  1408. nd->link_up ? "up" : "down");
  1409. }
  1410. static int ftgmac100_setup_clk(struct ftgmac100 *priv)
  1411. {
  1412. struct clk *clk;
  1413. int rc;
  1414. clk = devm_clk_get(priv->dev, NULL /* MACCLK */);
  1415. if (IS_ERR(clk))
  1416. return PTR_ERR(clk);
  1417. priv->clk = clk;
  1418. rc = clk_prepare_enable(priv->clk);
  1419. if (rc)
  1420. return rc;
  1421. /* Aspeed specifies a 100MHz clock is required for up to
  1422. * 1000Mbit link speeds. As NCSI is limited to 100Mbit, 25MHz
  1423. * is sufficient
  1424. */
  1425. rc = clk_set_rate(priv->clk, priv->use_ncsi ? FTGMAC_25MHZ :
  1426. FTGMAC_100MHZ);
  1427. if (rc)
  1428. goto cleanup_clk;
  1429. /* RCLK is for RMII, typically used for NCSI. Optional because it's not
  1430. * necessary if it's the AST2400 MAC, or the MAC is configured for
  1431. * RGMII, or the controller is not an ASPEED-based controller.
  1432. */
  1433. priv->rclk = devm_clk_get_optional(priv->dev, "RCLK");
  1434. rc = clk_prepare_enable(priv->rclk);
  1435. if (!rc)
  1436. return 0;
  1437. cleanup_clk:
  1438. clk_disable_unprepare(priv->clk);
  1439. return rc;
  1440. }
  1441. static bool ftgmac100_has_child_node(struct device_node *np, const char *name)
  1442. {
  1443. struct device_node *child_np = of_get_child_by_name(np, name);
  1444. bool ret = false;
  1445. if (child_np) {
  1446. ret = true;
  1447. of_node_put(child_np);
  1448. }
  1449. return ret;
  1450. }
  1451. static int ftgmac100_probe(struct platform_device *pdev)
  1452. {
  1453. struct resource *res;
  1454. int irq;
  1455. struct net_device *netdev;
  1456. struct ftgmac100 *priv;
  1457. struct device_node *np;
  1458. int err = 0;
  1459. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1460. if (!res)
  1461. return -ENXIO;
  1462. irq = platform_get_irq(pdev, 0);
  1463. if (irq < 0)
  1464. return irq;
  1465. /* setup net_device */
  1466. netdev = alloc_etherdev(sizeof(*priv));
  1467. if (!netdev) {
  1468. err = -ENOMEM;
  1469. goto err_alloc_etherdev;
  1470. }
  1471. SET_NETDEV_DEV(netdev, &pdev->dev);
  1472. netdev->ethtool_ops = &ftgmac100_ethtool_ops;
  1473. netdev->netdev_ops = &ftgmac100_netdev_ops;
  1474. netdev->watchdog_timeo = 5 * HZ;
  1475. platform_set_drvdata(pdev, netdev);
  1476. /* setup private data */
  1477. priv = netdev_priv(netdev);
  1478. priv->netdev = netdev;
  1479. priv->dev = &pdev->dev;
  1480. INIT_WORK(&priv->reset_task, ftgmac100_reset_task);
  1481. /* map io memory */
  1482. priv->res = request_mem_region(res->start, resource_size(res),
  1483. dev_name(&pdev->dev));
  1484. if (!priv->res) {
  1485. dev_err(&pdev->dev, "Could not reserve memory region\n");
  1486. err = -ENOMEM;
  1487. goto err_req_mem;
  1488. }
  1489. priv->base = ioremap(res->start, resource_size(res));
  1490. if (!priv->base) {
  1491. dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n");
  1492. err = -EIO;
  1493. goto err_ioremap;
  1494. }
  1495. netdev->irq = irq;
  1496. /* Enable pause */
  1497. priv->tx_pause = true;
  1498. priv->rx_pause = true;
  1499. priv->aneg_pause = true;
  1500. /* MAC address from chip or random one */
  1501. ftgmac100_initial_mac(priv);
  1502. np = pdev->dev.of_node;
  1503. if (np && (of_device_is_compatible(np, "aspeed,ast2400-mac") ||
  1504. of_device_is_compatible(np, "aspeed,ast2500-mac") ||
  1505. of_device_is_compatible(np, "aspeed,ast2600-mac"))) {
  1506. priv->rxdes0_edorr_mask = BIT(30);
  1507. priv->txdes0_edotr_mask = BIT(30);
  1508. priv->is_aspeed = true;
  1509. } else {
  1510. priv->rxdes0_edorr_mask = BIT(15);
  1511. priv->txdes0_edotr_mask = BIT(15);
  1512. }
  1513. if (np && of_get_property(np, "use-ncsi", NULL)) {
  1514. if (!IS_ENABLED(CONFIG_NET_NCSI)) {
  1515. dev_err(&pdev->dev, "NCSI stack not enabled\n");
  1516. err = -EINVAL;
  1517. goto err_phy_connect;
  1518. }
  1519. dev_info(&pdev->dev, "Using NCSI interface\n");
  1520. priv->use_ncsi = true;
  1521. priv->ndev = ncsi_register_dev(netdev, ftgmac100_ncsi_handler);
  1522. if (!priv->ndev) {
  1523. err = -EINVAL;
  1524. goto err_phy_connect;
  1525. }
  1526. } else if (np && of_phy_is_fixed_link(np)) {
  1527. struct phy_device *phy;
  1528. err = of_phy_register_fixed_link(np);
  1529. if (err) {
  1530. dev_err(&pdev->dev, "Failed to register fixed PHY\n");
  1531. goto err_phy_connect;
  1532. }
  1533. phy = of_phy_get_and_connect(priv->netdev, np,
  1534. &ftgmac100_adjust_link);
  1535. if (!phy) {
  1536. dev_err(&pdev->dev, "Failed to connect to fixed PHY\n");
  1537. of_phy_deregister_fixed_link(np);
  1538. err = -EINVAL;
  1539. goto err_phy_connect;
  1540. }
  1541. /* Display what we found */
  1542. phy_attached_info(phy);
  1543. } else if (np && of_get_property(np, "phy-handle", NULL)) {
  1544. struct phy_device *phy;
  1545. /* Support "mdio"/"phy" child nodes for ast2400/2500 with
  1546. * an embedded MDIO controller. Automatically scan the DTS for
  1547. * available PHYs and register them.
  1548. */
  1549. if (of_device_is_compatible(np, "aspeed,ast2400-mac") ||
  1550. of_device_is_compatible(np, "aspeed,ast2500-mac")) {
  1551. err = ftgmac100_setup_mdio(netdev);
  1552. if (err)
  1553. goto err_setup_mdio;
  1554. }
  1555. phy = of_phy_get_and_connect(priv->netdev, np,
  1556. &ftgmac100_adjust_link);
  1557. if (!phy) {
  1558. dev_err(&pdev->dev, "Failed to connect to phy\n");
  1559. err = -EINVAL;
  1560. goto err_phy_connect;
  1561. }
  1562. /* Indicate that we support PAUSE frames (see comment in
  1563. * Documentation/networking/phy.rst)
  1564. */
  1565. phy_support_asym_pause(phy);
  1566. /* Display what we found */
  1567. phy_attached_info(phy);
  1568. } else if (np && !ftgmac100_has_child_node(np, "mdio")) {
  1569. /* Support legacy ASPEED devicetree descriptions that decribe a
  1570. * MAC with an embedded MDIO controller but have no "mdio"
  1571. * child node. Automatically scan the MDIO bus for available
  1572. * PHYs.
  1573. */
  1574. priv->use_ncsi = false;
  1575. err = ftgmac100_setup_mdio(netdev);
  1576. if (err)
  1577. goto err_setup_mdio;
  1578. err = ftgmac100_mii_probe(netdev);
  1579. if (err) {
  1580. dev_err(priv->dev, "MII probe failed!\n");
  1581. goto err_ncsi_dev;
  1582. }
  1583. }
  1584. if (priv->is_aspeed) {
  1585. err = ftgmac100_setup_clk(priv);
  1586. if (err)
  1587. goto err_phy_connect;
  1588. /* Disable ast2600 problematic HW arbitration */
  1589. if (of_device_is_compatible(np, "aspeed,ast2600-mac"))
  1590. iowrite32(FTGMAC100_TM_DEFAULT,
  1591. priv->base + FTGMAC100_OFFSET_TM);
  1592. }
  1593. /* Default ring sizes */
  1594. priv->rx_q_entries = priv->new_rx_q_entries = DEF_RX_QUEUE_ENTRIES;
  1595. priv->tx_q_entries = priv->new_tx_q_entries = DEF_TX_QUEUE_ENTRIES;
  1596. /* Base feature set */
  1597. netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_HW_CSUM |
  1598. NETIF_F_GRO | NETIF_F_SG | NETIF_F_HW_VLAN_CTAG_RX |
  1599. NETIF_F_HW_VLAN_CTAG_TX;
  1600. if (priv->use_ncsi)
  1601. netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  1602. /* AST2400 doesn't have working HW checksum generation */
  1603. if (np && (of_device_is_compatible(np, "aspeed,ast2400-mac")))
  1604. netdev->hw_features &= ~NETIF_F_HW_CSUM;
  1605. /* AST2600 tx checksum with NCSI is broken */
  1606. if (priv->use_ncsi && of_device_is_compatible(np, "aspeed,ast2600-mac"))
  1607. netdev->hw_features &= ~NETIF_F_HW_CSUM;
  1608. if (np && of_get_property(np, "no-hw-checksum", NULL))
  1609. netdev->hw_features &= ~(NETIF_F_HW_CSUM | NETIF_F_RXCSUM);
  1610. netdev->features |= netdev->hw_features;
  1611. /* register network device */
  1612. err = register_netdev(netdev);
  1613. if (err) {
  1614. dev_err(&pdev->dev, "Failed to register netdev\n");
  1615. goto err_register_netdev;
  1616. }
  1617. netdev_info(netdev, "irq %d, mapped at %p\n", netdev->irq, priv->base);
  1618. return 0;
  1619. err_register_netdev:
  1620. clk_disable_unprepare(priv->rclk);
  1621. clk_disable_unprepare(priv->clk);
  1622. err_phy_connect:
  1623. ftgmac100_phy_disconnect(netdev);
  1624. err_ncsi_dev:
  1625. if (priv->ndev)
  1626. ncsi_unregister_dev(priv->ndev);
  1627. ftgmac100_destroy_mdio(netdev);
  1628. err_setup_mdio:
  1629. iounmap(priv->base);
  1630. err_ioremap:
  1631. release_resource(priv->res);
  1632. err_req_mem:
  1633. free_netdev(netdev);
  1634. err_alloc_etherdev:
  1635. return err;
  1636. }
  1637. static int ftgmac100_remove(struct platform_device *pdev)
  1638. {
  1639. struct net_device *netdev;
  1640. struct ftgmac100 *priv;
  1641. netdev = platform_get_drvdata(pdev);
  1642. priv = netdev_priv(netdev);
  1643. if (priv->ndev)
  1644. ncsi_unregister_dev(priv->ndev);
  1645. unregister_netdev(netdev);
  1646. clk_disable_unprepare(priv->rclk);
  1647. clk_disable_unprepare(priv->clk);
  1648. /* There's a small chance the reset task will have been re-queued,
  1649. * during stop, make sure it's gone before we free the structure.
  1650. */
  1651. cancel_work_sync(&priv->reset_task);
  1652. ftgmac100_phy_disconnect(netdev);
  1653. ftgmac100_destroy_mdio(netdev);
  1654. iounmap(priv->base);
  1655. release_resource(priv->res);
  1656. netif_napi_del(&priv->napi);
  1657. free_netdev(netdev);
  1658. return 0;
  1659. }
  1660. static const struct of_device_id ftgmac100_of_match[] = {
  1661. { .compatible = "faraday,ftgmac100" },
  1662. { }
  1663. };
  1664. MODULE_DEVICE_TABLE(of, ftgmac100_of_match);
  1665. static struct platform_driver ftgmac100_driver = {
  1666. .probe = ftgmac100_probe,
  1667. .remove = ftgmac100_remove,
  1668. .driver = {
  1669. .name = DRV_NAME,
  1670. .of_match_table = ftgmac100_of_match,
  1671. },
  1672. };
  1673. module_platform_driver(ftgmac100_driver);
  1674. MODULE_AUTHOR("Po-Yu Chuang <[email protected]>");
  1675. MODULE_DESCRIPTION("FTGMAC100 driver");
  1676. MODULE_LICENSE("GPL");