ftgmac100.c 54 KB

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