ftgmac100.c 55 KB

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