ftgmac100.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  1. /*
  2. * Faraday FTGMAC100 Gigabit Ethernet
  3. *
  4. * (C) Copyright 2009-2011 Faraday Technology
  5. * Po-Yu Chuang <ratbert@faraday-tech.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/clk.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/ethtool.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/io.h>
  28. #include <linux/module.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/of.h>
  31. #include <linux/phy.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/property.h>
  34. #include <linux/crc32.h>
  35. #include <linux/if_vlan.h>
  36. #include <linux/of_net.h>
  37. #include <net/ip.h>
  38. #include <net/ncsi.h>
  39. #include "ftgmac100.h"
  40. #define DRV_NAME "ftgmac100"
  41. #define DRV_VERSION "0.7"
  42. /* Arbitrary values, I am not sure the HW has limits */
  43. #define MAX_RX_QUEUE_ENTRIES 1024
  44. #define MAX_TX_QUEUE_ENTRIES 1024
  45. #define MIN_RX_QUEUE_ENTRIES 32
  46. #define MIN_TX_QUEUE_ENTRIES 32
  47. /* Defaults */
  48. #define DEF_RX_QUEUE_ENTRIES 128
  49. #define DEF_TX_QUEUE_ENTRIES 128
  50. #define MAX_PKT_SIZE 1536
  51. #define RX_BUF_SIZE MAX_PKT_SIZE /* must be smaller than 0x3fff */
  52. /* Min number of tx ring entries before stopping queue */
  53. #define TX_THRESHOLD (MAX_SKB_FRAGS + 1)
  54. #define FTGMAC_100MHZ 100000000
  55. #define FTGMAC_25MHZ 25000000
  56. struct ftgmac100 {
  57. /* Registers */
  58. struct resource *res;
  59. void __iomem *base;
  60. /* Rx ring */
  61. unsigned int rx_q_entries;
  62. struct ftgmac100_rxdes *rxdes;
  63. dma_addr_t rxdes_dma;
  64. struct sk_buff **rx_skbs;
  65. unsigned int rx_pointer;
  66. u32 rxdes0_edorr_mask;
  67. /* Tx ring */
  68. unsigned int tx_q_entries;
  69. struct ftgmac100_txdes *txdes;
  70. dma_addr_t txdes_dma;
  71. struct sk_buff **tx_skbs;
  72. unsigned int tx_clean_pointer;
  73. unsigned int tx_pointer;
  74. u32 txdes0_edotr_mask;
  75. /* Used to signal the reset task of ring change request */
  76. unsigned int new_rx_q_entries;
  77. unsigned int new_tx_q_entries;
  78. /* Scratch page to use when rx skb alloc fails */
  79. void *rx_scratch;
  80. dma_addr_t rx_scratch_dma;
  81. /* Component structures */
  82. struct net_device *netdev;
  83. struct device *dev;
  84. struct ncsi_dev *ndev;
  85. struct napi_struct napi;
  86. struct work_struct reset_task;
  87. struct mii_bus *mii_bus;
  88. struct clk *clk;
  89. /* Link management */
  90. int cur_speed;
  91. int cur_duplex;
  92. bool use_ncsi;
  93. /* Multicast filter settings */
  94. u32 maht0;
  95. u32 maht1;
  96. /* Flow control settings */
  97. bool tx_pause;
  98. bool rx_pause;
  99. bool aneg_pause;
  100. /* Misc */
  101. bool need_mac_restart;
  102. bool is_aspeed;
  103. };
  104. static int ftgmac100_reset_mac(struct ftgmac100 *priv, u32 maccr)
  105. {
  106. struct net_device *netdev = priv->netdev;
  107. int i;
  108. /* NOTE: reset clears all registers */
  109. iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
  110. iowrite32(maccr | FTGMAC100_MACCR_SW_RST,
  111. priv->base + FTGMAC100_OFFSET_MACCR);
  112. for (i = 0; i < 200; i++) {
  113. unsigned int maccr;
  114. maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
  115. if (!(maccr & FTGMAC100_MACCR_SW_RST))
  116. return 0;
  117. udelay(1);
  118. }
  119. netdev_err(netdev, "Hardware reset failed\n");
  120. return -EIO;
  121. }
  122. static int ftgmac100_reset_and_config_mac(struct ftgmac100 *priv)
  123. {
  124. u32 maccr = 0;
  125. switch (priv->cur_speed) {
  126. case SPEED_10:
  127. case 0: /* no link */
  128. break;
  129. case SPEED_100:
  130. maccr |= FTGMAC100_MACCR_FAST_MODE;
  131. break;
  132. case SPEED_1000:
  133. maccr |= FTGMAC100_MACCR_GIGA_MODE;
  134. break;
  135. default:
  136. netdev_err(priv->netdev, "Unknown speed %d !\n",
  137. priv->cur_speed);
  138. break;
  139. }
  140. /* (Re)initialize the queue pointers */
  141. priv->rx_pointer = 0;
  142. priv->tx_clean_pointer = 0;
  143. priv->tx_pointer = 0;
  144. /* The doc says reset twice with 10us interval */
  145. if (ftgmac100_reset_mac(priv, maccr))
  146. return -EIO;
  147. usleep_range(10, 1000);
  148. return ftgmac100_reset_mac(priv, maccr);
  149. }
  150. static void ftgmac100_write_mac_addr(struct ftgmac100 *priv, const u8 *mac)
  151. {
  152. unsigned int maddr = mac[0] << 8 | mac[1];
  153. unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
  154. iowrite32(maddr, priv->base + FTGMAC100_OFFSET_MAC_MADR);
  155. iowrite32(laddr, priv->base + FTGMAC100_OFFSET_MAC_LADR);
  156. }
  157. static void ftgmac100_initial_mac(struct ftgmac100 *priv)
  158. {
  159. u8 mac[ETH_ALEN];
  160. unsigned int m;
  161. unsigned int l;
  162. void *addr;
  163. addr = device_get_mac_address(priv->dev, mac, ETH_ALEN);
  164. if (addr) {
  165. ether_addr_copy(priv->netdev->dev_addr, mac);
  166. dev_info(priv->dev, "Read MAC address %pM from device tree\n",
  167. mac);
  168. return;
  169. }
  170. m = ioread32(priv->base + FTGMAC100_OFFSET_MAC_MADR);
  171. l = ioread32(priv->base + FTGMAC100_OFFSET_MAC_LADR);
  172. mac[0] = (m >> 8) & 0xff;
  173. mac[1] = m & 0xff;
  174. mac[2] = (l >> 24) & 0xff;
  175. mac[3] = (l >> 16) & 0xff;
  176. mac[4] = (l >> 8) & 0xff;
  177. mac[5] = l & 0xff;
  178. if (is_valid_ether_addr(mac)) {
  179. ether_addr_copy(priv->netdev->dev_addr, mac);
  180. dev_info(priv->dev, "Read MAC address %pM from chip\n", mac);
  181. } else {
  182. eth_hw_addr_random(priv->netdev);
  183. dev_info(priv->dev, "Generated random MAC address %pM\n",
  184. priv->netdev->dev_addr);
  185. }
  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. priv->tx_clean_pointer = ftgmac100_next_tx_pointer(priv, pointer);
  549. return true;
  550. }
  551. static void ftgmac100_tx_complete(struct ftgmac100 *priv)
  552. {
  553. struct net_device *netdev = priv->netdev;
  554. /* Process all completed packets */
  555. while (ftgmac100_tx_buf_cleanable(priv) &&
  556. ftgmac100_tx_complete_packet(priv))
  557. ;
  558. /* Restart queue if needed */
  559. smp_mb();
  560. if (unlikely(netif_queue_stopped(netdev) &&
  561. ftgmac100_tx_buf_avail(priv) >= TX_THRESHOLD)) {
  562. struct netdev_queue *txq;
  563. txq = netdev_get_tx_queue(netdev, 0);
  564. __netif_tx_lock(txq, smp_processor_id());
  565. if (netif_queue_stopped(netdev) &&
  566. ftgmac100_tx_buf_avail(priv) >= TX_THRESHOLD)
  567. netif_wake_queue(netdev);
  568. __netif_tx_unlock(txq);
  569. }
  570. }
  571. static bool ftgmac100_prep_tx_csum(struct sk_buff *skb, u32 *csum_vlan)
  572. {
  573. if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
  574. u8 ip_proto = ip_hdr(skb)->protocol;
  575. *csum_vlan |= FTGMAC100_TXDES1_IP_CHKSUM;
  576. switch(ip_proto) {
  577. case IPPROTO_TCP:
  578. *csum_vlan |= FTGMAC100_TXDES1_TCP_CHKSUM;
  579. return true;
  580. case IPPROTO_UDP:
  581. *csum_vlan |= FTGMAC100_TXDES1_UDP_CHKSUM;
  582. return true;
  583. case IPPROTO_IP:
  584. return true;
  585. }
  586. }
  587. return skb_checksum_help(skb) == 0;
  588. }
  589. static netdev_tx_t ftgmac100_hard_start_xmit(struct sk_buff *skb,
  590. struct net_device *netdev)
  591. {
  592. struct ftgmac100 *priv = netdev_priv(netdev);
  593. struct ftgmac100_txdes *txdes, *first;
  594. unsigned int pointer, nfrags, len, i, j;
  595. u32 f_ctl_stat, ctl_stat, csum_vlan;
  596. dma_addr_t map;
  597. /* The HW doesn't pad small frames */
  598. if (eth_skb_pad(skb)) {
  599. netdev->stats.tx_dropped++;
  600. return NETDEV_TX_OK;
  601. }
  602. /* Reject oversize packets */
  603. if (unlikely(skb->len > MAX_PKT_SIZE)) {
  604. if (net_ratelimit())
  605. netdev_dbg(netdev, "tx packet too big\n");
  606. goto drop;
  607. }
  608. /* Do we have a limit on #fragments ? I yet have to get a reply
  609. * from Aspeed. If there's one I haven't hit it.
  610. */
  611. nfrags = skb_shinfo(skb)->nr_frags;
  612. /* Setup HW checksumming */
  613. csum_vlan = 0;
  614. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  615. !ftgmac100_prep_tx_csum(skb, &csum_vlan))
  616. goto drop;
  617. /* Add VLAN tag */
  618. if (skb_vlan_tag_present(skb)) {
  619. csum_vlan |= FTGMAC100_TXDES1_INS_VLANTAG;
  620. csum_vlan |= skb_vlan_tag_get(skb) & 0xffff;
  621. }
  622. /* Get header len */
  623. len = skb_headlen(skb);
  624. /* Map the packet head */
  625. map = dma_map_single(priv->dev, skb->data, len, DMA_TO_DEVICE);
  626. if (dma_mapping_error(priv->dev, map)) {
  627. if (net_ratelimit())
  628. netdev_err(netdev, "map tx packet head failed\n");
  629. goto drop;
  630. }
  631. /* Grab the next free tx descriptor */
  632. pointer = priv->tx_pointer;
  633. txdes = first = &priv->txdes[pointer];
  634. /* Setup it up with the packet head. Don't write the head to the
  635. * ring just yet
  636. */
  637. priv->tx_skbs[pointer] = skb;
  638. f_ctl_stat = ftgmac100_base_tx_ctlstat(priv, pointer);
  639. f_ctl_stat |= FTGMAC100_TXDES0_TXDMA_OWN;
  640. f_ctl_stat |= FTGMAC100_TXDES0_TXBUF_SIZE(len);
  641. f_ctl_stat |= FTGMAC100_TXDES0_FTS;
  642. if (nfrags == 0)
  643. f_ctl_stat |= FTGMAC100_TXDES0_LTS;
  644. txdes->txdes3 = cpu_to_le32(map);
  645. txdes->txdes1 = cpu_to_le32(csum_vlan);
  646. /* Next descriptor */
  647. pointer = ftgmac100_next_tx_pointer(priv, pointer);
  648. /* Add the fragments */
  649. for (i = 0; i < nfrags; i++) {
  650. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  651. len = frag->size;
  652. /* Map it */
  653. map = skb_frag_dma_map(priv->dev, frag, 0, len,
  654. DMA_TO_DEVICE);
  655. if (dma_mapping_error(priv->dev, map))
  656. goto dma_err;
  657. /* Setup descriptor */
  658. priv->tx_skbs[pointer] = skb;
  659. txdes = &priv->txdes[pointer];
  660. ctl_stat = ftgmac100_base_tx_ctlstat(priv, pointer);
  661. ctl_stat |= FTGMAC100_TXDES0_TXDMA_OWN;
  662. ctl_stat |= FTGMAC100_TXDES0_TXBUF_SIZE(len);
  663. if (i == (nfrags - 1))
  664. ctl_stat |= FTGMAC100_TXDES0_LTS;
  665. txdes->txdes0 = cpu_to_le32(ctl_stat);
  666. txdes->txdes1 = 0;
  667. txdes->txdes3 = cpu_to_le32(map);
  668. /* Next one */
  669. pointer = ftgmac100_next_tx_pointer(priv, pointer);
  670. }
  671. /* Order the previous packet and descriptor udpates
  672. * before setting the OWN bit on the first descriptor.
  673. */
  674. dma_wmb();
  675. first->txdes0 = cpu_to_le32(f_ctl_stat);
  676. /* Update next TX pointer */
  677. priv->tx_pointer = pointer;
  678. /* If there isn't enough room for all the fragments of a new packet
  679. * in the TX ring, stop the queue. The sequence below is race free
  680. * vs. a concurrent restart in ftgmac100_poll()
  681. */
  682. if (unlikely(ftgmac100_tx_buf_avail(priv) < TX_THRESHOLD)) {
  683. netif_stop_queue(netdev);
  684. /* Order the queue stop with the test below */
  685. smp_mb();
  686. if (ftgmac100_tx_buf_avail(priv) >= TX_THRESHOLD)
  687. netif_wake_queue(netdev);
  688. }
  689. /* Poke transmitter to read the updated TX descriptors */
  690. iowrite32(1, priv->base + FTGMAC100_OFFSET_NPTXPD);
  691. return NETDEV_TX_OK;
  692. dma_err:
  693. if (net_ratelimit())
  694. netdev_err(netdev, "map tx fragment failed\n");
  695. /* Free head */
  696. pointer = priv->tx_pointer;
  697. ftgmac100_free_tx_packet(priv, pointer, skb, first, f_ctl_stat);
  698. first->txdes0 = cpu_to_le32(f_ctl_stat & priv->txdes0_edotr_mask);
  699. /* Then all fragments */
  700. for (j = 0; j < i; j++) {
  701. pointer = ftgmac100_next_tx_pointer(priv, pointer);
  702. txdes = &priv->txdes[pointer];
  703. ctl_stat = le32_to_cpu(txdes->txdes0);
  704. ftgmac100_free_tx_packet(priv, pointer, skb, txdes, ctl_stat);
  705. txdes->txdes0 = cpu_to_le32(ctl_stat & priv->txdes0_edotr_mask);
  706. }
  707. /* This cannot be reached if we successfully mapped the
  708. * last fragment, so we know ftgmac100_free_tx_packet()
  709. * hasn't freed the skb yet.
  710. */
  711. drop:
  712. /* Drop the packet */
  713. dev_kfree_skb_any(skb);
  714. netdev->stats.tx_dropped++;
  715. return NETDEV_TX_OK;
  716. }
  717. static void ftgmac100_free_buffers(struct ftgmac100 *priv)
  718. {
  719. int i;
  720. /* Free all RX buffers */
  721. for (i = 0; i < priv->rx_q_entries; i++) {
  722. struct ftgmac100_rxdes *rxdes = &priv->rxdes[i];
  723. struct sk_buff *skb = priv->rx_skbs[i];
  724. dma_addr_t map = le32_to_cpu(rxdes->rxdes3);
  725. if (!skb)
  726. continue;
  727. priv->rx_skbs[i] = NULL;
  728. dma_unmap_single(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
  729. dev_kfree_skb_any(skb);
  730. }
  731. /* Free all TX buffers */
  732. for (i = 0; i < priv->tx_q_entries; i++) {
  733. struct ftgmac100_txdes *txdes = &priv->txdes[i];
  734. struct sk_buff *skb = priv->tx_skbs[i];
  735. if (!skb)
  736. continue;
  737. ftgmac100_free_tx_packet(priv, i, skb, txdes,
  738. le32_to_cpu(txdes->txdes0));
  739. }
  740. }
  741. static void ftgmac100_free_rings(struct ftgmac100 *priv)
  742. {
  743. /* Free skb arrays */
  744. kfree(priv->rx_skbs);
  745. kfree(priv->tx_skbs);
  746. /* Free descriptors */
  747. if (priv->rxdes)
  748. dma_free_coherent(priv->dev, MAX_RX_QUEUE_ENTRIES *
  749. sizeof(struct ftgmac100_rxdes),
  750. priv->rxdes, priv->rxdes_dma);
  751. priv->rxdes = NULL;
  752. if (priv->txdes)
  753. dma_free_coherent(priv->dev, MAX_TX_QUEUE_ENTRIES *
  754. sizeof(struct ftgmac100_txdes),
  755. priv->txdes, priv->txdes_dma);
  756. priv->txdes = NULL;
  757. /* Free scratch packet buffer */
  758. if (priv->rx_scratch)
  759. dma_free_coherent(priv->dev, RX_BUF_SIZE,
  760. priv->rx_scratch, priv->rx_scratch_dma);
  761. }
  762. static int ftgmac100_alloc_rings(struct ftgmac100 *priv)
  763. {
  764. /* Allocate skb arrays */
  765. priv->rx_skbs = kcalloc(MAX_RX_QUEUE_ENTRIES, sizeof(void *),
  766. GFP_KERNEL);
  767. if (!priv->rx_skbs)
  768. return -ENOMEM;
  769. priv->tx_skbs = kcalloc(MAX_TX_QUEUE_ENTRIES, sizeof(void *),
  770. GFP_KERNEL);
  771. if (!priv->tx_skbs)
  772. return -ENOMEM;
  773. /* Allocate descriptors */
  774. priv->rxdes = dma_zalloc_coherent(priv->dev,
  775. MAX_RX_QUEUE_ENTRIES *
  776. sizeof(struct ftgmac100_rxdes),
  777. &priv->rxdes_dma, GFP_KERNEL);
  778. if (!priv->rxdes)
  779. return -ENOMEM;
  780. priv->txdes = dma_zalloc_coherent(priv->dev,
  781. MAX_TX_QUEUE_ENTRIES *
  782. sizeof(struct ftgmac100_txdes),
  783. &priv->txdes_dma, GFP_KERNEL);
  784. if (!priv->txdes)
  785. return -ENOMEM;
  786. /* Allocate scratch packet buffer */
  787. priv->rx_scratch = dma_alloc_coherent(priv->dev,
  788. RX_BUF_SIZE,
  789. &priv->rx_scratch_dma,
  790. GFP_KERNEL);
  791. if (!priv->rx_scratch)
  792. return -ENOMEM;
  793. return 0;
  794. }
  795. static void ftgmac100_init_rings(struct ftgmac100 *priv)
  796. {
  797. struct ftgmac100_rxdes *rxdes = NULL;
  798. struct ftgmac100_txdes *txdes = NULL;
  799. int i;
  800. /* Update entries counts */
  801. priv->rx_q_entries = priv->new_rx_q_entries;
  802. priv->tx_q_entries = priv->new_tx_q_entries;
  803. if (WARN_ON(priv->rx_q_entries < MIN_RX_QUEUE_ENTRIES))
  804. return;
  805. /* Initialize RX ring */
  806. for (i = 0; i < priv->rx_q_entries; i++) {
  807. rxdes = &priv->rxdes[i];
  808. rxdes->rxdes0 = 0;
  809. rxdes->rxdes3 = cpu_to_le32(priv->rx_scratch_dma);
  810. }
  811. /* Mark the end of the ring */
  812. rxdes->rxdes0 |= cpu_to_le32(priv->rxdes0_edorr_mask);
  813. if (WARN_ON(priv->tx_q_entries < MIN_RX_QUEUE_ENTRIES))
  814. return;
  815. /* Initialize TX ring */
  816. for (i = 0; i < priv->tx_q_entries; i++) {
  817. txdes = &priv->txdes[i];
  818. txdes->txdes0 = 0;
  819. }
  820. txdes->txdes0 |= cpu_to_le32(priv->txdes0_edotr_mask);
  821. }
  822. static int ftgmac100_alloc_rx_buffers(struct ftgmac100 *priv)
  823. {
  824. int i;
  825. for (i = 0; i < priv->rx_q_entries; i++) {
  826. struct ftgmac100_rxdes *rxdes = &priv->rxdes[i];
  827. if (ftgmac100_alloc_rx_buf(priv, i, rxdes, GFP_KERNEL))
  828. return -ENOMEM;
  829. }
  830. return 0;
  831. }
  832. static void ftgmac100_adjust_link(struct net_device *netdev)
  833. {
  834. struct ftgmac100 *priv = netdev_priv(netdev);
  835. struct phy_device *phydev = netdev->phydev;
  836. bool tx_pause, rx_pause;
  837. int new_speed;
  838. /* We store "no link" as speed 0 */
  839. if (!phydev->link)
  840. new_speed = 0;
  841. else
  842. new_speed = phydev->speed;
  843. /* Grab pause settings from PHY if configured to do so */
  844. if (priv->aneg_pause) {
  845. rx_pause = tx_pause = phydev->pause;
  846. if (phydev->asym_pause)
  847. tx_pause = !rx_pause;
  848. } else {
  849. rx_pause = priv->rx_pause;
  850. tx_pause = priv->tx_pause;
  851. }
  852. /* Link hasn't changed, do nothing */
  853. if (phydev->speed == priv->cur_speed &&
  854. phydev->duplex == priv->cur_duplex &&
  855. rx_pause == priv->rx_pause &&
  856. tx_pause == priv->tx_pause)
  857. return;
  858. /* Print status if we have a link or we had one and just lost it,
  859. * don't print otherwise.
  860. */
  861. if (new_speed || priv->cur_speed)
  862. phy_print_status(phydev);
  863. priv->cur_speed = new_speed;
  864. priv->cur_duplex = phydev->duplex;
  865. priv->rx_pause = rx_pause;
  866. priv->tx_pause = tx_pause;
  867. /* Link is down, do nothing else */
  868. if (!new_speed)
  869. return;
  870. /* Disable all interrupts */
  871. iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
  872. /* Reset the adapter asynchronously */
  873. schedule_work(&priv->reset_task);
  874. }
  875. static int ftgmac100_mii_probe(struct ftgmac100 *priv, phy_interface_t intf)
  876. {
  877. struct net_device *netdev = priv->netdev;
  878. struct phy_device *phydev;
  879. phydev = phy_find_first(priv->mii_bus);
  880. if (!phydev) {
  881. netdev_info(netdev, "%s: no PHY found\n", netdev->name);
  882. return -ENODEV;
  883. }
  884. phydev = phy_connect(netdev, phydev_name(phydev),
  885. &ftgmac100_adjust_link, intf);
  886. if (IS_ERR(phydev)) {
  887. netdev_err(netdev, "%s: Could not attach to PHY\n", netdev->name);
  888. return PTR_ERR(phydev);
  889. }
  890. /* Indicate that we support PAUSE frames (see comment in
  891. * Documentation/networking/phy.txt)
  892. */
  893. phydev->supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
  894. phydev->advertising = phydev->supported;
  895. /* Display what we found */
  896. phy_attached_info(phydev);
  897. return 0;
  898. }
  899. static int ftgmac100_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
  900. {
  901. struct net_device *netdev = bus->priv;
  902. struct ftgmac100 *priv = netdev_priv(netdev);
  903. unsigned int phycr;
  904. int i;
  905. phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
  906. /* preserve MDC cycle threshold */
  907. phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
  908. phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
  909. FTGMAC100_PHYCR_REGAD(regnum) |
  910. FTGMAC100_PHYCR_MIIRD;
  911. iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
  912. for (i = 0; i < 10; i++) {
  913. phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
  914. if ((phycr & FTGMAC100_PHYCR_MIIRD) == 0) {
  915. int data;
  916. data = ioread32(priv->base + FTGMAC100_OFFSET_PHYDATA);
  917. return FTGMAC100_PHYDATA_MIIRDATA(data);
  918. }
  919. udelay(100);
  920. }
  921. netdev_err(netdev, "mdio read timed out\n");
  922. return -EIO;
  923. }
  924. static int ftgmac100_mdiobus_write(struct mii_bus *bus, int phy_addr,
  925. int regnum, u16 value)
  926. {
  927. struct net_device *netdev = bus->priv;
  928. struct ftgmac100 *priv = netdev_priv(netdev);
  929. unsigned int phycr;
  930. int data;
  931. int i;
  932. phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
  933. /* preserve MDC cycle threshold */
  934. phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
  935. phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
  936. FTGMAC100_PHYCR_REGAD(regnum) |
  937. FTGMAC100_PHYCR_MIIWR;
  938. data = FTGMAC100_PHYDATA_MIIWDATA(value);
  939. iowrite32(data, priv->base + FTGMAC100_OFFSET_PHYDATA);
  940. iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
  941. for (i = 0; i < 10; i++) {
  942. phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
  943. if ((phycr & FTGMAC100_PHYCR_MIIWR) == 0)
  944. return 0;
  945. udelay(100);
  946. }
  947. netdev_err(netdev, "mdio write timed out\n");
  948. return -EIO;
  949. }
  950. static void ftgmac100_get_drvinfo(struct net_device *netdev,
  951. struct ethtool_drvinfo *info)
  952. {
  953. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  954. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  955. strlcpy(info->bus_info, dev_name(&netdev->dev), sizeof(info->bus_info));
  956. }
  957. static void ftgmac100_get_ringparam(struct net_device *netdev,
  958. struct ethtool_ringparam *ering)
  959. {
  960. struct ftgmac100 *priv = netdev_priv(netdev);
  961. memset(ering, 0, sizeof(*ering));
  962. ering->rx_max_pending = MAX_RX_QUEUE_ENTRIES;
  963. ering->tx_max_pending = MAX_TX_QUEUE_ENTRIES;
  964. ering->rx_pending = priv->rx_q_entries;
  965. ering->tx_pending = priv->tx_q_entries;
  966. }
  967. static int ftgmac100_set_ringparam(struct net_device *netdev,
  968. struct ethtool_ringparam *ering)
  969. {
  970. struct ftgmac100 *priv = netdev_priv(netdev);
  971. if (ering->rx_pending > MAX_RX_QUEUE_ENTRIES ||
  972. ering->tx_pending > MAX_TX_QUEUE_ENTRIES ||
  973. ering->rx_pending < MIN_RX_QUEUE_ENTRIES ||
  974. ering->tx_pending < MIN_TX_QUEUE_ENTRIES ||
  975. !is_power_of_2(ering->rx_pending) ||
  976. !is_power_of_2(ering->tx_pending))
  977. return -EINVAL;
  978. priv->new_rx_q_entries = ering->rx_pending;
  979. priv->new_tx_q_entries = ering->tx_pending;
  980. if (netif_running(netdev))
  981. schedule_work(&priv->reset_task);
  982. return 0;
  983. }
  984. static void ftgmac100_get_pauseparam(struct net_device *netdev,
  985. struct ethtool_pauseparam *pause)
  986. {
  987. struct ftgmac100 *priv = netdev_priv(netdev);
  988. pause->autoneg = priv->aneg_pause;
  989. pause->tx_pause = priv->tx_pause;
  990. pause->rx_pause = priv->rx_pause;
  991. }
  992. static int ftgmac100_set_pauseparam(struct net_device *netdev,
  993. struct ethtool_pauseparam *pause)
  994. {
  995. struct ftgmac100 *priv = netdev_priv(netdev);
  996. struct phy_device *phydev = netdev->phydev;
  997. priv->aneg_pause = pause->autoneg;
  998. priv->tx_pause = pause->tx_pause;
  999. priv->rx_pause = pause->rx_pause;
  1000. if (phydev) {
  1001. phydev->advertising &= ~ADVERTISED_Pause;
  1002. phydev->advertising &= ~ADVERTISED_Asym_Pause;
  1003. if (pause->rx_pause) {
  1004. phydev->advertising |= ADVERTISED_Pause;
  1005. phydev->advertising |= ADVERTISED_Asym_Pause;
  1006. }
  1007. if (pause->tx_pause)
  1008. phydev->advertising ^= ADVERTISED_Asym_Pause;
  1009. }
  1010. if (netif_running(netdev)) {
  1011. if (phydev && priv->aneg_pause)
  1012. phy_start_aneg(phydev);
  1013. else
  1014. ftgmac100_config_pause(priv);
  1015. }
  1016. return 0;
  1017. }
  1018. static const struct ethtool_ops ftgmac100_ethtool_ops = {
  1019. .get_drvinfo = ftgmac100_get_drvinfo,
  1020. .get_link = ethtool_op_get_link,
  1021. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  1022. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  1023. .nway_reset = phy_ethtool_nway_reset,
  1024. .get_ringparam = ftgmac100_get_ringparam,
  1025. .set_ringparam = ftgmac100_set_ringparam,
  1026. .get_pauseparam = ftgmac100_get_pauseparam,
  1027. .set_pauseparam = ftgmac100_set_pauseparam,
  1028. };
  1029. static irqreturn_t ftgmac100_interrupt(int irq, void *dev_id)
  1030. {
  1031. struct net_device *netdev = dev_id;
  1032. struct ftgmac100 *priv = netdev_priv(netdev);
  1033. unsigned int status, new_mask = FTGMAC100_INT_BAD;
  1034. /* Fetch and clear interrupt bits, process abnormal ones */
  1035. status = ioread32(priv->base + FTGMAC100_OFFSET_ISR);
  1036. iowrite32(status, priv->base + FTGMAC100_OFFSET_ISR);
  1037. if (unlikely(status & FTGMAC100_INT_BAD)) {
  1038. /* RX buffer unavailable */
  1039. if (status & FTGMAC100_INT_NO_RXBUF)
  1040. netdev->stats.rx_over_errors++;
  1041. /* received packet lost due to RX FIFO full */
  1042. if (status & FTGMAC100_INT_RPKT_LOST)
  1043. netdev->stats.rx_fifo_errors++;
  1044. /* sent packet lost due to excessive TX collision */
  1045. if (status & FTGMAC100_INT_XPKT_LOST)
  1046. netdev->stats.tx_fifo_errors++;
  1047. /* AHB error -> Reset the chip */
  1048. if (status & FTGMAC100_INT_AHB_ERR) {
  1049. if (net_ratelimit())
  1050. netdev_warn(netdev,
  1051. "AHB bus error ! Resetting chip.\n");
  1052. iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
  1053. schedule_work(&priv->reset_task);
  1054. return IRQ_HANDLED;
  1055. }
  1056. /* We may need to restart the MAC after such errors, delay
  1057. * this until after we have freed some Rx buffers though
  1058. */
  1059. priv->need_mac_restart = true;
  1060. /* Disable those errors until we restart */
  1061. new_mask &= ~status;
  1062. }
  1063. /* Only enable "bad" interrupts while NAPI is on */
  1064. iowrite32(new_mask, priv->base + FTGMAC100_OFFSET_IER);
  1065. /* Schedule NAPI bh */
  1066. napi_schedule_irqoff(&priv->napi);
  1067. return IRQ_HANDLED;
  1068. }
  1069. static bool ftgmac100_check_rx(struct ftgmac100 *priv)
  1070. {
  1071. struct ftgmac100_rxdes *rxdes = &priv->rxdes[priv->rx_pointer];
  1072. /* Do we have a packet ? */
  1073. return !!(rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RXPKT_RDY));
  1074. }
  1075. static int ftgmac100_poll(struct napi_struct *napi, int budget)
  1076. {
  1077. struct ftgmac100 *priv = container_of(napi, struct ftgmac100, napi);
  1078. int work_done = 0;
  1079. bool more;
  1080. /* Handle TX completions */
  1081. if (ftgmac100_tx_buf_cleanable(priv))
  1082. ftgmac100_tx_complete(priv);
  1083. /* Handle RX packets */
  1084. do {
  1085. more = ftgmac100_rx_packet(priv, &work_done);
  1086. } while (more && work_done < budget);
  1087. /* The interrupt is telling us to kick the MAC back to life
  1088. * after an RX overflow
  1089. */
  1090. if (unlikely(priv->need_mac_restart)) {
  1091. ftgmac100_start_hw(priv);
  1092. priv->need_mac_restart = false;
  1093. /* Re-enable "bad" interrupts */
  1094. iowrite32(FTGMAC100_INT_BAD,
  1095. priv->base + FTGMAC100_OFFSET_IER);
  1096. }
  1097. /* As long as we are waiting for transmit packets to be
  1098. * completed we keep NAPI going
  1099. */
  1100. if (ftgmac100_tx_buf_cleanable(priv))
  1101. work_done = budget;
  1102. if (work_done < budget) {
  1103. /* We are about to re-enable all interrupts. However
  1104. * the HW has been latching RX/TX packet interrupts while
  1105. * they were masked. So we clear them first, then we need
  1106. * to re-check if there's something to process
  1107. */
  1108. iowrite32(FTGMAC100_INT_RXTX,
  1109. priv->base + FTGMAC100_OFFSET_ISR);
  1110. /* Push the above (and provides a barrier vs. subsequent
  1111. * reads of the descriptor).
  1112. */
  1113. ioread32(priv->base + FTGMAC100_OFFSET_ISR);
  1114. /* Check RX and TX descriptors for more work to do */
  1115. if (ftgmac100_check_rx(priv) ||
  1116. ftgmac100_tx_buf_cleanable(priv))
  1117. return budget;
  1118. /* deschedule NAPI */
  1119. napi_complete(napi);
  1120. /* enable all interrupts */
  1121. iowrite32(FTGMAC100_INT_ALL,
  1122. priv->base + FTGMAC100_OFFSET_IER);
  1123. }
  1124. return work_done;
  1125. }
  1126. static int ftgmac100_init_all(struct ftgmac100 *priv, bool ignore_alloc_err)
  1127. {
  1128. int err = 0;
  1129. /* Re-init descriptors (adjust queue sizes) */
  1130. ftgmac100_init_rings(priv);
  1131. /* Realloc rx descriptors */
  1132. err = ftgmac100_alloc_rx_buffers(priv);
  1133. if (err && !ignore_alloc_err)
  1134. return err;
  1135. /* Reinit and restart HW */
  1136. ftgmac100_init_hw(priv);
  1137. ftgmac100_config_pause(priv);
  1138. ftgmac100_start_hw(priv);
  1139. /* Re-enable the device */
  1140. napi_enable(&priv->napi);
  1141. netif_start_queue(priv->netdev);
  1142. /* Enable all interrupts */
  1143. iowrite32(FTGMAC100_INT_ALL, priv->base + FTGMAC100_OFFSET_IER);
  1144. return err;
  1145. }
  1146. static void ftgmac100_reset_task(struct work_struct *work)
  1147. {
  1148. struct ftgmac100 *priv = container_of(work, struct ftgmac100,
  1149. reset_task);
  1150. struct net_device *netdev = priv->netdev;
  1151. int err;
  1152. netdev_dbg(netdev, "Resetting NIC...\n");
  1153. /* Lock the world */
  1154. rtnl_lock();
  1155. if (netdev->phydev)
  1156. mutex_lock(&netdev->phydev->lock);
  1157. if (priv->mii_bus)
  1158. mutex_lock(&priv->mii_bus->mdio_lock);
  1159. /* Check if the interface is still up */
  1160. if (!netif_running(netdev))
  1161. goto bail;
  1162. /* Stop the network stack */
  1163. netif_trans_update(netdev);
  1164. napi_disable(&priv->napi);
  1165. netif_tx_disable(netdev);
  1166. /* Stop and reset the MAC */
  1167. ftgmac100_stop_hw(priv);
  1168. err = ftgmac100_reset_and_config_mac(priv);
  1169. if (err) {
  1170. /* Not much we can do ... it might come back... */
  1171. netdev_err(netdev, "attempting to continue...\n");
  1172. }
  1173. /* Free all rx and tx buffers */
  1174. ftgmac100_free_buffers(priv);
  1175. /* Setup everything again and restart chip */
  1176. ftgmac100_init_all(priv, true);
  1177. netdev_dbg(netdev, "Reset done !\n");
  1178. bail:
  1179. if (priv->mii_bus)
  1180. mutex_unlock(&priv->mii_bus->mdio_lock);
  1181. if (netdev->phydev)
  1182. mutex_unlock(&netdev->phydev->lock);
  1183. rtnl_unlock();
  1184. }
  1185. static int ftgmac100_open(struct net_device *netdev)
  1186. {
  1187. struct ftgmac100 *priv = netdev_priv(netdev);
  1188. int err;
  1189. /* Allocate ring buffers */
  1190. err = ftgmac100_alloc_rings(priv);
  1191. if (err) {
  1192. netdev_err(netdev, "Failed to allocate descriptors\n");
  1193. return err;
  1194. }
  1195. /* When using NC-SI we force the speed to 100Mbit/s full duplex,
  1196. *
  1197. * Otherwise we leave it set to 0 (no link), the link
  1198. * message from the PHY layer will handle setting it up to
  1199. * something else if needed.
  1200. */
  1201. if (priv->use_ncsi) {
  1202. priv->cur_duplex = DUPLEX_FULL;
  1203. priv->cur_speed = SPEED_100;
  1204. } else {
  1205. priv->cur_duplex = 0;
  1206. priv->cur_speed = 0;
  1207. }
  1208. /* Reset the hardware */
  1209. err = ftgmac100_reset_and_config_mac(priv);
  1210. if (err)
  1211. goto err_hw;
  1212. /* Initialize NAPI */
  1213. netif_napi_add(netdev, &priv->napi, ftgmac100_poll, 64);
  1214. /* Grab our interrupt */
  1215. err = request_irq(netdev->irq, ftgmac100_interrupt, 0, netdev->name, netdev);
  1216. if (err) {
  1217. netdev_err(netdev, "failed to request irq %d\n", netdev->irq);
  1218. goto err_irq;
  1219. }
  1220. /* Start things up */
  1221. err = ftgmac100_init_all(priv, false);
  1222. if (err) {
  1223. netdev_err(netdev, "Failed to allocate packet buffers\n");
  1224. goto err_alloc;
  1225. }
  1226. if (netdev->phydev) {
  1227. /* If we have a PHY, start polling */
  1228. phy_start(netdev->phydev);
  1229. } else if (priv->use_ncsi) {
  1230. /* If using NC-SI, set our carrier on and start the stack */
  1231. netif_carrier_on(netdev);
  1232. /* Start the NCSI device */
  1233. err = ncsi_start_dev(priv->ndev);
  1234. if (err)
  1235. goto err_ncsi;
  1236. }
  1237. return 0;
  1238. err_ncsi:
  1239. napi_disable(&priv->napi);
  1240. netif_stop_queue(netdev);
  1241. err_alloc:
  1242. ftgmac100_free_buffers(priv);
  1243. free_irq(netdev->irq, netdev);
  1244. err_irq:
  1245. netif_napi_del(&priv->napi);
  1246. err_hw:
  1247. iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
  1248. ftgmac100_free_rings(priv);
  1249. return err;
  1250. }
  1251. static int ftgmac100_stop(struct net_device *netdev)
  1252. {
  1253. struct ftgmac100 *priv = netdev_priv(netdev);
  1254. /* Note about the reset task: We are called with the rtnl lock
  1255. * held, so we are synchronized against the core of the reset
  1256. * task. We must not try to synchronously cancel it otherwise
  1257. * we can deadlock. But since it will test for netif_running()
  1258. * which has already been cleared by the net core, we don't
  1259. * anything special to do.
  1260. */
  1261. /* disable all interrupts */
  1262. iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
  1263. netif_stop_queue(netdev);
  1264. napi_disable(&priv->napi);
  1265. netif_napi_del(&priv->napi);
  1266. if (netdev->phydev)
  1267. phy_stop(netdev->phydev);
  1268. else if (priv->use_ncsi)
  1269. ncsi_stop_dev(priv->ndev);
  1270. ftgmac100_stop_hw(priv);
  1271. free_irq(netdev->irq, netdev);
  1272. ftgmac100_free_buffers(priv);
  1273. ftgmac100_free_rings(priv);
  1274. return 0;
  1275. }
  1276. /* optional */
  1277. static int ftgmac100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  1278. {
  1279. if (!netdev->phydev)
  1280. return -ENXIO;
  1281. return phy_mii_ioctl(netdev->phydev, ifr, cmd);
  1282. }
  1283. static void ftgmac100_tx_timeout(struct net_device *netdev)
  1284. {
  1285. struct ftgmac100 *priv = netdev_priv(netdev);
  1286. /* Disable all interrupts */
  1287. iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
  1288. /* Do the reset outside of interrupt context */
  1289. schedule_work(&priv->reset_task);
  1290. }
  1291. static int ftgmac100_set_features(struct net_device *netdev,
  1292. netdev_features_t features)
  1293. {
  1294. struct ftgmac100 *priv = netdev_priv(netdev);
  1295. netdev_features_t changed = netdev->features ^ features;
  1296. if (!netif_running(netdev))
  1297. return 0;
  1298. /* Update the vlan filtering bit */
  1299. if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
  1300. u32 maccr;
  1301. maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
  1302. if (priv->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
  1303. maccr |= FTGMAC100_MACCR_RM_VLAN;
  1304. else
  1305. maccr &= ~FTGMAC100_MACCR_RM_VLAN;
  1306. iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
  1307. }
  1308. return 0;
  1309. }
  1310. #ifdef CONFIG_NET_POLL_CONTROLLER
  1311. static void ftgmac100_poll_controller(struct net_device *netdev)
  1312. {
  1313. unsigned long flags;
  1314. local_irq_save(flags);
  1315. ftgmac100_interrupt(netdev->irq, netdev);
  1316. local_irq_restore(flags);
  1317. }
  1318. #endif
  1319. static const struct net_device_ops ftgmac100_netdev_ops = {
  1320. .ndo_open = ftgmac100_open,
  1321. .ndo_stop = ftgmac100_stop,
  1322. .ndo_start_xmit = ftgmac100_hard_start_xmit,
  1323. .ndo_set_mac_address = ftgmac100_set_mac_addr,
  1324. .ndo_validate_addr = eth_validate_addr,
  1325. .ndo_do_ioctl = ftgmac100_do_ioctl,
  1326. .ndo_tx_timeout = ftgmac100_tx_timeout,
  1327. .ndo_set_rx_mode = ftgmac100_set_rx_mode,
  1328. .ndo_set_features = ftgmac100_set_features,
  1329. #ifdef CONFIG_NET_POLL_CONTROLLER
  1330. .ndo_poll_controller = ftgmac100_poll_controller,
  1331. #endif
  1332. .ndo_vlan_rx_add_vid = ncsi_vlan_rx_add_vid,
  1333. .ndo_vlan_rx_kill_vid = ncsi_vlan_rx_kill_vid,
  1334. };
  1335. static int ftgmac100_setup_mdio(struct net_device *netdev)
  1336. {
  1337. struct ftgmac100 *priv = netdev_priv(netdev);
  1338. struct platform_device *pdev = to_platform_device(priv->dev);
  1339. int phy_intf = PHY_INTERFACE_MODE_RGMII;
  1340. struct device_node *np = pdev->dev.of_node;
  1341. int i, err = 0;
  1342. u32 reg;
  1343. /* initialize mdio bus */
  1344. priv->mii_bus = mdiobus_alloc();
  1345. if (!priv->mii_bus)
  1346. return -EIO;
  1347. if (priv->is_aspeed) {
  1348. /* This driver supports the old MDIO interface */
  1349. reg = ioread32(priv->base + FTGMAC100_OFFSET_REVR);
  1350. reg &= ~FTGMAC100_REVR_NEW_MDIO_INTERFACE;
  1351. iowrite32(reg, priv->base + FTGMAC100_OFFSET_REVR);
  1352. };
  1353. /* Get PHY mode from device-tree */
  1354. if (np) {
  1355. /* Default to RGMII. It's a gigabit part after all */
  1356. phy_intf = of_get_phy_mode(np);
  1357. if (phy_intf < 0)
  1358. phy_intf = PHY_INTERFACE_MODE_RGMII;
  1359. /* Aspeed only supports these. I don't know about other IP
  1360. * block vendors so I'm going to just let them through for
  1361. * now. Note that this is only a warning if for some obscure
  1362. * reason the DT really means to lie about it or it's a newer
  1363. * part we don't know about.
  1364. *
  1365. * On the Aspeed SoC there are additionally straps and SCU
  1366. * control bits that could tell us what the interface is
  1367. * (or allow us to configure it while the IP block is held
  1368. * in reset). For now I chose to keep this driver away from
  1369. * those SoC specific bits and assume the device-tree is
  1370. * right and the SCU has been configured properly by pinmux
  1371. * or the firmware.
  1372. */
  1373. if (priv->is_aspeed &&
  1374. phy_intf != PHY_INTERFACE_MODE_RMII &&
  1375. phy_intf != PHY_INTERFACE_MODE_RGMII &&
  1376. phy_intf != PHY_INTERFACE_MODE_RGMII_ID &&
  1377. phy_intf != PHY_INTERFACE_MODE_RGMII_RXID &&
  1378. phy_intf != PHY_INTERFACE_MODE_RGMII_TXID) {
  1379. netdev_warn(netdev,
  1380. "Unsupported PHY mode %s !\n",
  1381. phy_modes(phy_intf));
  1382. }
  1383. }
  1384. priv->mii_bus->name = "ftgmac100_mdio";
  1385. snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%d",
  1386. pdev->name, pdev->id);
  1387. priv->mii_bus->parent = priv->dev;
  1388. priv->mii_bus->priv = priv->netdev;
  1389. priv->mii_bus->read = ftgmac100_mdiobus_read;
  1390. priv->mii_bus->write = ftgmac100_mdiobus_write;
  1391. for (i = 0; i < PHY_MAX_ADDR; i++)
  1392. priv->mii_bus->irq[i] = PHY_POLL;
  1393. err = mdiobus_register(priv->mii_bus);
  1394. if (err) {
  1395. dev_err(priv->dev, "Cannot register MDIO bus!\n");
  1396. goto err_register_mdiobus;
  1397. }
  1398. err = ftgmac100_mii_probe(priv, phy_intf);
  1399. if (err) {
  1400. dev_err(priv->dev, "MII Probe failed!\n");
  1401. goto err_mii_probe;
  1402. }
  1403. return 0;
  1404. err_mii_probe:
  1405. mdiobus_unregister(priv->mii_bus);
  1406. err_register_mdiobus:
  1407. mdiobus_free(priv->mii_bus);
  1408. return err;
  1409. }
  1410. static void ftgmac100_destroy_mdio(struct net_device *netdev)
  1411. {
  1412. struct ftgmac100 *priv = netdev_priv(netdev);
  1413. if (!netdev->phydev)
  1414. return;
  1415. phy_disconnect(netdev->phydev);
  1416. mdiobus_unregister(priv->mii_bus);
  1417. mdiobus_free(priv->mii_bus);
  1418. }
  1419. static void ftgmac100_ncsi_handler(struct ncsi_dev *nd)
  1420. {
  1421. if (unlikely(nd->state != ncsi_dev_state_functional))
  1422. return;
  1423. netdev_dbg(nd->dev, "NCSI interface %s\n",
  1424. nd->link_up ? "up" : "down");
  1425. }
  1426. static void ftgmac100_setup_clk(struct ftgmac100 *priv)
  1427. {
  1428. priv->clk = devm_clk_get(priv->dev, NULL);
  1429. if (IS_ERR(priv->clk))
  1430. return;
  1431. clk_prepare_enable(priv->clk);
  1432. /* Aspeed specifies a 100MHz clock is required for up to
  1433. * 1000Mbit link speeds. As NCSI is limited to 100Mbit, 25MHz
  1434. * is sufficient
  1435. */
  1436. clk_set_rate(priv->clk, priv->use_ncsi ? FTGMAC_25MHZ :
  1437. FTGMAC_100MHZ);
  1438. }
  1439. static int ftgmac100_probe(struct platform_device *pdev)
  1440. {
  1441. struct resource *res;
  1442. int irq;
  1443. struct net_device *netdev;
  1444. struct ftgmac100 *priv;
  1445. struct device_node *np;
  1446. int err = 0;
  1447. if (!pdev)
  1448. return -ENODEV;
  1449. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1450. if (!res)
  1451. return -ENXIO;
  1452. irq = platform_get_irq(pdev, 0);
  1453. if (irq < 0)
  1454. return irq;
  1455. /* setup net_device */
  1456. netdev = alloc_etherdev(sizeof(*priv));
  1457. if (!netdev) {
  1458. err = -ENOMEM;
  1459. goto err_alloc_etherdev;
  1460. }
  1461. SET_NETDEV_DEV(netdev, &pdev->dev);
  1462. netdev->ethtool_ops = &ftgmac100_ethtool_ops;
  1463. netdev->netdev_ops = &ftgmac100_netdev_ops;
  1464. netdev->watchdog_timeo = 5 * HZ;
  1465. platform_set_drvdata(pdev, netdev);
  1466. /* setup private data */
  1467. priv = netdev_priv(netdev);
  1468. priv->netdev = netdev;
  1469. priv->dev = &pdev->dev;
  1470. INIT_WORK(&priv->reset_task, ftgmac100_reset_task);
  1471. /* map io memory */
  1472. priv->res = request_mem_region(res->start, resource_size(res),
  1473. dev_name(&pdev->dev));
  1474. if (!priv->res) {
  1475. dev_err(&pdev->dev, "Could not reserve memory region\n");
  1476. err = -ENOMEM;
  1477. goto err_req_mem;
  1478. }
  1479. priv->base = ioremap(res->start, resource_size(res));
  1480. if (!priv->base) {
  1481. dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n");
  1482. err = -EIO;
  1483. goto err_ioremap;
  1484. }
  1485. netdev->irq = irq;
  1486. /* Enable pause */
  1487. priv->tx_pause = true;
  1488. priv->rx_pause = true;
  1489. priv->aneg_pause = true;
  1490. /* MAC address from chip or random one */
  1491. ftgmac100_initial_mac(priv);
  1492. np = pdev->dev.of_node;
  1493. if (np && (of_device_is_compatible(np, "aspeed,ast2400-mac") ||
  1494. of_device_is_compatible(np, "aspeed,ast2500-mac"))) {
  1495. priv->rxdes0_edorr_mask = BIT(30);
  1496. priv->txdes0_edotr_mask = BIT(30);
  1497. priv->is_aspeed = true;
  1498. } else {
  1499. priv->rxdes0_edorr_mask = BIT(15);
  1500. priv->txdes0_edotr_mask = BIT(15);
  1501. }
  1502. if (np && of_get_property(np, "use-ncsi", NULL)) {
  1503. if (!IS_ENABLED(CONFIG_NET_NCSI)) {
  1504. dev_err(&pdev->dev, "NCSI stack not enabled\n");
  1505. goto err_ncsi_dev;
  1506. }
  1507. dev_info(&pdev->dev, "Using NCSI interface\n");
  1508. priv->use_ncsi = true;
  1509. priv->ndev = ncsi_register_dev(netdev, ftgmac100_ncsi_handler);
  1510. if (!priv->ndev)
  1511. goto err_ncsi_dev;
  1512. } else {
  1513. priv->use_ncsi = false;
  1514. err = ftgmac100_setup_mdio(netdev);
  1515. if (err)
  1516. goto err_setup_mdio;
  1517. }
  1518. if (priv->is_aspeed)
  1519. ftgmac100_setup_clk(priv);
  1520. /* Default ring sizes */
  1521. priv->rx_q_entries = priv->new_rx_q_entries = DEF_RX_QUEUE_ENTRIES;
  1522. priv->tx_q_entries = priv->new_tx_q_entries = DEF_TX_QUEUE_ENTRIES;
  1523. /* Base feature set */
  1524. netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_HW_CSUM |
  1525. NETIF_F_GRO | NETIF_F_SG | NETIF_F_HW_VLAN_CTAG_RX |
  1526. NETIF_F_HW_VLAN_CTAG_TX;
  1527. if (priv->use_ncsi)
  1528. netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  1529. /* AST2400 doesn't have working HW checksum generation */
  1530. if (np && (of_device_is_compatible(np, "aspeed,ast2400-mac")))
  1531. netdev->hw_features &= ~NETIF_F_HW_CSUM;
  1532. if (np && of_get_property(np, "no-hw-checksum", NULL))
  1533. netdev->hw_features &= ~(NETIF_F_HW_CSUM | NETIF_F_RXCSUM);
  1534. netdev->features |= netdev->hw_features;
  1535. /* register network device */
  1536. err = register_netdev(netdev);
  1537. if (err) {
  1538. dev_err(&pdev->dev, "Failed to register netdev\n");
  1539. goto err_register_netdev;
  1540. }
  1541. netdev_info(netdev, "irq %d, mapped at %p\n", netdev->irq, priv->base);
  1542. return 0;
  1543. err_ncsi_dev:
  1544. if (priv->ndev)
  1545. ncsi_unregister_dev(priv->ndev);
  1546. err_register_netdev:
  1547. ftgmac100_destroy_mdio(netdev);
  1548. err_setup_mdio:
  1549. iounmap(priv->base);
  1550. err_ioremap:
  1551. release_resource(priv->res);
  1552. err_req_mem:
  1553. free_netdev(netdev);
  1554. err_alloc_etherdev:
  1555. return err;
  1556. }
  1557. static int ftgmac100_remove(struct platform_device *pdev)
  1558. {
  1559. struct net_device *netdev;
  1560. struct ftgmac100 *priv;
  1561. netdev = platform_get_drvdata(pdev);
  1562. priv = netdev_priv(netdev);
  1563. if (priv->ndev)
  1564. ncsi_unregister_dev(priv->ndev);
  1565. unregister_netdev(netdev);
  1566. clk_disable_unprepare(priv->clk);
  1567. /* There's a small chance the reset task will have been re-queued,
  1568. * during stop, make sure it's gone before we free the structure.
  1569. */
  1570. cancel_work_sync(&priv->reset_task);
  1571. ftgmac100_destroy_mdio(netdev);
  1572. iounmap(priv->base);
  1573. release_resource(priv->res);
  1574. netif_napi_del(&priv->napi);
  1575. free_netdev(netdev);
  1576. return 0;
  1577. }
  1578. static const struct of_device_id ftgmac100_of_match[] = {
  1579. { .compatible = "faraday,ftgmac100" },
  1580. { }
  1581. };
  1582. MODULE_DEVICE_TABLE(of, ftgmac100_of_match);
  1583. static struct platform_driver ftgmac100_driver = {
  1584. .probe = ftgmac100_probe,
  1585. .remove = ftgmac100_remove,
  1586. .driver = {
  1587. .name = DRV_NAME,
  1588. .of_match_table = ftgmac100_of_match,
  1589. },
  1590. };
  1591. module_platform_driver(ftgmac100_driver);
  1592. MODULE_AUTHOR("Po-Yu Chuang <ratbert@faraday-tech.com>");
  1593. MODULE_DESCRIPTION("FTGMAC100 driver");
  1594. MODULE_LICENSE("GPL");