ftgmac100.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945
  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. /* Re-enable "bad" interrupts */
  1093. iowrite32(FTGMAC100_INT_BAD,
  1094. priv->base + FTGMAC100_OFFSET_IER);
  1095. }
  1096. /* As long as we are waiting for transmit packets to be
  1097. * completed we keep NAPI going
  1098. */
  1099. if (ftgmac100_tx_buf_cleanable(priv))
  1100. work_done = budget;
  1101. if (work_done < budget) {
  1102. /* We are about to re-enable all interrupts. However
  1103. * the HW has been latching RX/TX packet interrupts while
  1104. * they were masked. So we clear them first, then we need
  1105. * to re-check if there's something to process
  1106. */
  1107. iowrite32(FTGMAC100_INT_RXTX,
  1108. priv->base + FTGMAC100_OFFSET_ISR);
  1109. /* Push the above (and provides a barrier vs. subsequent
  1110. * reads of the descriptor).
  1111. */
  1112. ioread32(priv->base + FTGMAC100_OFFSET_ISR);
  1113. /* Check RX and TX descriptors for more work to do */
  1114. if (ftgmac100_check_rx(priv) ||
  1115. ftgmac100_tx_buf_cleanable(priv))
  1116. return budget;
  1117. /* deschedule NAPI */
  1118. napi_complete(napi);
  1119. /* enable all interrupts */
  1120. iowrite32(FTGMAC100_INT_ALL,
  1121. priv->base + FTGMAC100_OFFSET_IER);
  1122. }
  1123. return work_done;
  1124. }
  1125. static int ftgmac100_init_all(struct ftgmac100 *priv, bool ignore_alloc_err)
  1126. {
  1127. int err = 0;
  1128. /* Re-init descriptors (adjust queue sizes) */
  1129. ftgmac100_init_rings(priv);
  1130. /* Realloc rx descriptors */
  1131. err = ftgmac100_alloc_rx_buffers(priv);
  1132. if (err && !ignore_alloc_err)
  1133. return err;
  1134. /* Reinit and restart HW */
  1135. ftgmac100_init_hw(priv);
  1136. ftgmac100_config_pause(priv);
  1137. ftgmac100_start_hw(priv);
  1138. /* Re-enable the device */
  1139. napi_enable(&priv->napi);
  1140. netif_start_queue(priv->netdev);
  1141. /* Enable all interrupts */
  1142. iowrite32(FTGMAC100_INT_ALL, priv->base + FTGMAC100_OFFSET_IER);
  1143. return err;
  1144. }
  1145. static void ftgmac100_reset_task(struct work_struct *work)
  1146. {
  1147. struct ftgmac100 *priv = container_of(work, struct ftgmac100,
  1148. reset_task);
  1149. struct net_device *netdev = priv->netdev;
  1150. int err;
  1151. netdev_dbg(netdev, "Resetting NIC...\n");
  1152. /* Lock the world */
  1153. rtnl_lock();
  1154. if (netdev->phydev)
  1155. mutex_lock(&netdev->phydev->lock);
  1156. if (priv->mii_bus)
  1157. mutex_lock(&priv->mii_bus->mdio_lock);
  1158. /* Check if the interface is still up */
  1159. if (!netif_running(netdev))
  1160. goto bail;
  1161. /* Stop the network stack */
  1162. netif_trans_update(netdev);
  1163. napi_disable(&priv->napi);
  1164. netif_tx_disable(netdev);
  1165. /* Stop and reset the MAC */
  1166. ftgmac100_stop_hw(priv);
  1167. err = ftgmac100_reset_and_config_mac(priv);
  1168. if (err) {
  1169. /* Not much we can do ... it might come back... */
  1170. netdev_err(netdev, "attempting to continue...\n");
  1171. }
  1172. /* Free all rx and tx buffers */
  1173. ftgmac100_free_buffers(priv);
  1174. /* Setup everything again and restart chip */
  1175. ftgmac100_init_all(priv, true);
  1176. netdev_dbg(netdev, "Reset done !\n");
  1177. bail:
  1178. if (priv->mii_bus)
  1179. mutex_unlock(&priv->mii_bus->mdio_lock);
  1180. if (netdev->phydev)
  1181. mutex_unlock(&netdev->phydev->lock);
  1182. rtnl_unlock();
  1183. }
  1184. static int ftgmac100_open(struct net_device *netdev)
  1185. {
  1186. struct ftgmac100 *priv = netdev_priv(netdev);
  1187. int err;
  1188. /* Allocate ring buffers */
  1189. err = ftgmac100_alloc_rings(priv);
  1190. if (err) {
  1191. netdev_err(netdev, "Failed to allocate descriptors\n");
  1192. return err;
  1193. }
  1194. /* When using NC-SI we force the speed to 100Mbit/s full duplex,
  1195. *
  1196. * Otherwise we leave it set to 0 (no link), the link
  1197. * message from the PHY layer will handle setting it up to
  1198. * something else if needed.
  1199. */
  1200. if (priv->use_ncsi) {
  1201. priv->cur_duplex = DUPLEX_FULL;
  1202. priv->cur_speed = SPEED_100;
  1203. } else {
  1204. priv->cur_duplex = 0;
  1205. priv->cur_speed = 0;
  1206. }
  1207. /* Reset the hardware */
  1208. err = ftgmac100_reset_and_config_mac(priv);
  1209. if (err)
  1210. goto err_hw;
  1211. /* Initialize NAPI */
  1212. netif_napi_add(netdev, &priv->napi, ftgmac100_poll, 64);
  1213. /* Grab our interrupt */
  1214. err = request_irq(netdev->irq, ftgmac100_interrupt, 0, netdev->name, netdev);
  1215. if (err) {
  1216. netdev_err(netdev, "failed to request irq %d\n", netdev->irq);
  1217. goto err_irq;
  1218. }
  1219. /* Start things up */
  1220. err = ftgmac100_init_all(priv, false);
  1221. if (err) {
  1222. netdev_err(netdev, "Failed to allocate packet buffers\n");
  1223. goto err_alloc;
  1224. }
  1225. if (netdev->phydev) {
  1226. /* If we have a PHY, start polling */
  1227. phy_start(netdev->phydev);
  1228. } else if (priv->use_ncsi) {
  1229. /* If using NC-SI, set our carrier on and start the stack */
  1230. netif_carrier_on(netdev);
  1231. /* Start the NCSI device */
  1232. err = ncsi_start_dev(priv->ndev);
  1233. if (err)
  1234. goto err_ncsi;
  1235. }
  1236. return 0;
  1237. err_ncsi:
  1238. napi_disable(&priv->napi);
  1239. netif_stop_queue(netdev);
  1240. err_alloc:
  1241. ftgmac100_free_buffers(priv);
  1242. free_irq(netdev->irq, netdev);
  1243. err_irq:
  1244. netif_napi_del(&priv->napi);
  1245. err_hw:
  1246. iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
  1247. ftgmac100_free_rings(priv);
  1248. return err;
  1249. }
  1250. static int ftgmac100_stop(struct net_device *netdev)
  1251. {
  1252. struct ftgmac100 *priv = netdev_priv(netdev);
  1253. /* Note about the reset task: We are called with the rtnl lock
  1254. * held, so we are synchronized against the core of the reset
  1255. * task. We must not try to synchronously cancel it otherwise
  1256. * we can deadlock. But since it will test for netif_running()
  1257. * which has already been cleared by the net core, we don't
  1258. * anything special to do.
  1259. */
  1260. /* disable all interrupts */
  1261. iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
  1262. netif_stop_queue(netdev);
  1263. napi_disable(&priv->napi);
  1264. netif_napi_del(&priv->napi);
  1265. if (netdev->phydev)
  1266. phy_stop(netdev->phydev);
  1267. else if (priv->use_ncsi)
  1268. ncsi_stop_dev(priv->ndev);
  1269. ftgmac100_stop_hw(priv);
  1270. free_irq(netdev->irq, netdev);
  1271. ftgmac100_free_buffers(priv);
  1272. ftgmac100_free_rings(priv);
  1273. return 0;
  1274. }
  1275. /* optional */
  1276. static int ftgmac100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  1277. {
  1278. if (!netdev->phydev)
  1279. return -ENXIO;
  1280. return phy_mii_ioctl(netdev->phydev, ifr, cmd);
  1281. }
  1282. static void ftgmac100_tx_timeout(struct net_device *netdev)
  1283. {
  1284. struct ftgmac100 *priv = netdev_priv(netdev);
  1285. /* Disable all interrupts */
  1286. iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
  1287. /* Do the reset outside of interrupt context */
  1288. schedule_work(&priv->reset_task);
  1289. }
  1290. static int ftgmac100_set_features(struct net_device *netdev,
  1291. netdev_features_t features)
  1292. {
  1293. struct ftgmac100 *priv = netdev_priv(netdev);
  1294. netdev_features_t changed = netdev->features ^ features;
  1295. if (!netif_running(netdev))
  1296. return 0;
  1297. /* Update the vlan filtering bit */
  1298. if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
  1299. u32 maccr;
  1300. maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
  1301. if (priv->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
  1302. maccr |= FTGMAC100_MACCR_RM_VLAN;
  1303. else
  1304. maccr &= ~FTGMAC100_MACCR_RM_VLAN;
  1305. iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
  1306. }
  1307. return 0;
  1308. }
  1309. #ifdef CONFIG_NET_POLL_CONTROLLER
  1310. static void ftgmac100_poll_controller(struct net_device *netdev)
  1311. {
  1312. unsigned long flags;
  1313. local_irq_save(flags);
  1314. ftgmac100_interrupt(netdev->irq, netdev);
  1315. local_irq_restore(flags);
  1316. }
  1317. #endif
  1318. static const struct net_device_ops ftgmac100_netdev_ops = {
  1319. .ndo_open = ftgmac100_open,
  1320. .ndo_stop = ftgmac100_stop,
  1321. .ndo_start_xmit = ftgmac100_hard_start_xmit,
  1322. .ndo_set_mac_address = ftgmac100_set_mac_addr,
  1323. .ndo_validate_addr = eth_validate_addr,
  1324. .ndo_do_ioctl = ftgmac100_do_ioctl,
  1325. .ndo_tx_timeout = ftgmac100_tx_timeout,
  1326. .ndo_set_rx_mode = ftgmac100_set_rx_mode,
  1327. .ndo_set_features = ftgmac100_set_features,
  1328. #ifdef CONFIG_NET_POLL_CONTROLLER
  1329. .ndo_poll_controller = ftgmac100_poll_controller,
  1330. #endif
  1331. .ndo_vlan_rx_add_vid = ncsi_vlan_rx_add_vid,
  1332. .ndo_vlan_rx_kill_vid = ncsi_vlan_rx_kill_vid,
  1333. };
  1334. static int ftgmac100_setup_mdio(struct net_device *netdev)
  1335. {
  1336. struct ftgmac100 *priv = netdev_priv(netdev);
  1337. struct platform_device *pdev = to_platform_device(priv->dev);
  1338. int phy_intf = PHY_INTERFACE_MODE_RGMII;
  1339. struct device_node *np = pdev->dev.of_node;
  1340. int i, err = 0;
  1341. u32 reg;
  1342. /* initialize mdio bus */
  1343. priv->mii_bus = mdiobus_alloc();
  1344. if (!priv->mii_bus)
  1345. return -EIO;
  1346. if (priv->is_aspeed) {
  1347. /* This driver supports the old MDIO interface */
  1348. reg = ioread32(priv->base + FTGMAC100_OFFSET_REVR);
  1349. reg &= ~FTGMAC100_REVR_NEW_MDIO_INTERFACE;
  1350. iowrite32(reg, priv->base + FTGMAC100_OFFSET_REVR);
  1351. };
  1352. /* Get PHY mode from device-tree */
  1353. if (np) {
  1354. /* Default to RGMII. It's a gigabit part after all */
  1355. phy_intf = of_get_phy_mode(np);
  1356. if (phy_intf < 0)
  1357. phy_intf = PHY_INTERFACE_MODE_RGMII;
  1358. /* Aspeed only supports these. I don't know about other IP
  1359. * block vendors so I'm going to just let them through for
  1360. * now. Note that this is only a warning if for some obscure
  1361. * reason the DT really means to lie about it or it's a newer
  1362. * part we don't know about.
  1363. *
  1364. * On the Aspeed SoC there are additionally straps and SCU
  1365. * control bits that could tell us what the interface is
  1366. * (or allow us to configure it while the IP block is held
  1367. * in reset). For now I chose to keep this driver away from
  1368. * those SoC specific bits and assume the device-tree is
  1369. * right and the SCU has been configured properly by pinmux
  1370. * or the firmware.
  1371. */
  1372. if (priv->is_aspeed &&
  1373. phy_intf != PHY_INTERFACE_MODE_RMII &&
  1374. phy_intf != PHY_INTERFACE_MODE_RGMII &&
  1375. phy_intf != PHY_INTERFACE_MODE_RGMII_ID &&
  1376. phy_intf != PHY_INTERFACE_MODE_RGMII_RXID &&
  1377. phy_intf != PHY_INTERFACE_MODE_RGMII_TXID) {
  1378. netdev_warn(netdev,
  1379. "Unsupported PHY mode %s !\n",
  1380. phy_modes(phy_intf));
  1381. }
  1382. }
  1383. priv->mii_bus->name = "ftgmac100_mdio";
  1384. snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%d",
  1385. pdev->name, pdev->id);
  1386. priv->mii_bus->parent = priv->dev;
  1387. priv->mii_bus->priv = priv->netdev;
  1388. priv->mii_bus->read = ftgmac100_mdiobus_read;
  1389. priv->mii_bus->write = ftgmac100_mdiobus_write;
  1390. for (i = 0; i < PHY_MAX_ADDR; i++)
  1391. priv->mii_bus->irq[i] = PHY_POLL;
  1392. err = mdiobus_register(priv->mii_bus);
  1393. if (err) {
  1394. dev_err(priv->dev, "Cannot register MDIO bus!\n");
  1395. goto err_register_mdiobus;
  1396. }
  1397. err = ftgmac100_mii_probe(priv, phy_intf);
  1398. if (err) {
  1399. dev_err(priv->dev, "MII Probe failed!\n");
  1400. goto err_mii_probe;
  1401. }
  1402. return 0;
  1403. err_mii_probe:
  1404. mdiobus_unregister(priv->mii_bus);
  1405. err_register_mdiobus:
  1406. mdiobus_free(priv->mii_bus);
  1407. return err;
  1408. }
  1409. static void ftgmac100_destroy_mdio(struct net_device *netdev)
  1410. {
  1411. struct ftgmac100 *priv = netdev_priv(netdev);
  1412. if (!netdev->phydev)
  1413. return;
  1414. phy_disconnect(netdev->phydev);
  1415. mdiobus_unregister(priv->mii_bus);
  1416. mdiobus_free(priv->mii_bus);
  1417. }
  1418. static void ftgmac100_ncsi_handler(struct ncsi_dev *nd)
  1419. {
  1420. if (unlikely(nd->state != ncsi_dev_state_functional))
  1421. return;
  1422. netdev_dbg(nd->dev, "NCSI interface %s\n",
  1423. nd->link_up ? "up" : "down");
  1424. }
  1425. static void ftgmac100_setup_clk(struct ftgmac100 *priv)
  1426. {
  1427. priv->clk = devm_clk_get(priv->dev, NULL);
  1428. if (IS_ERR(priv->clk))
  1429. return;
  1430. clk_prepare_enable(priv->clk);
  1431. /* Aspeed specifies a 100MHz clock is required for up to
  1432. * 1000Mbit link speeds. As NCSI is limited to 100Mbit, 25MHz
  1433. * is sufficient
  1434. */
  1435. clk_set_rate(priv->clk, priv->use_ncsi ? FTGMAC_25MHZ :
  1436. FTGMAC_100MHZ);
  1437. }
  1438. static int ftgmac100_probe(struct platform_device *pdev)
  1439. {
  1440. struct resource *res;
  1441. int irq;
  1442. struct net_device *netdev;
  1443. struct ftgmac100 *priv;
  1444. struct device_node *np;
  1445. int err = 0;
  1446. if (!pdev)
  1447. return -ENODEV;
  1448. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1449. if (!res)
  1450. return -ENXIO;
  1451. irq = platform_get_irq(pdev, 0);
  1452. if (irq < 0)
  1453. return irq;
  1454. /* setup net_device */
  1455. netdev = alloc_etherdev(sizeof(*priv));
  1456. if (!netdev) {
  1457. err = -ENOMEM;
  1458. goto err_alloc_etherdev;
  1459. }
  1460. SET_NETDEV_DEV(netdev, &pdev->dev);
  1461. netdev->ethtool_ops = &ftgmac100_ethtool_ops;
  1462. netdev->netdev_ops = &ftgmac100_netdev_ops;
  1463. netdev->watchdog_timeo = 5 * HZ;
  1464. platform_set_drvdata(pdev, netdev);
  1465. /* setup private data */
  1466. priv = netdev_priv(netdev);
  1467. priv->netdev = netdev;
  1468. priv->dev = &pdev->dev;
  1469. INIT_WORK(&priv->reset_task, ftgmac100_reset_task);
  1470. /* map io memory */
  1471. priv->res = request_mem_region(res->start, resource_size(res),
  1472. dev_name(&pdev->dev));
  1473. if (!priv->res) {
  1474. dev_err(&pdev->dev, "Could not reserve memory region\n");
  1475. err = -ENOMEM;
  1476. goto err_req_mem;
  1477. }
  1478. priv->base = ioremap(res->start, resource_size(res));
  1479. if (!priv->base) {
  1480. dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n");
  1481. err = -EIO;
  1482. goto err_ioremap;
  1483. }
  1484. netdev->irq = irq;
  1485. /* Enable pause */
  1486. priv->tx_pause = true;
  1487. priv->rx_pause = true;
  1488. priv->aneg_pause = true;
  1489. /* MAC address from chip or random one */
  1490. ftgmac100_initial_mac(priv);
  1491. np = pdev->dev.of_node;
  1492. if (np && (of_device_is_compatible(np, "aspeed,ast2400-mac") ||
  1493. of_device_is_compatible(np, "aspeed,ast2500-mac"))) {
  1494. priv->rxdes0_edorr_mask = BIT(30);
  1495. priv->txdes0_edotr_mask = BIT(30);
  1496. priv->is_aspeed = true;
  1497. } else {
  1498. priv->rxdes0_edorr_mask = BIT(15);
  1499. priv->txdes0_edotr_mask = BIT(15);
  1500. }
  1501. if (np && of_get_property(np, "use-ncsi", NULL)) {
  1502. if (!IS_ENABLED(CONFIG_NET_NCSI)) {
  1503. dev_err(&pdev->dev, "NCSI stack not enabled\n");
  1504. goto err_ncsi_dev;
  1505. }
  1506. dev_info(&pdev->dev, "Using NCSI interface\n");
  1507. priv->use_ncsi = true;
  1508. priv->ndev = ncsi_register_dev(netdev, ftgmac100_ncsi_handler);
  1509. if (!priv->ndev)
  1510. goto err_ncsi_dev;
  1511. } else {
  1512. priv->use_ncsi = false;
  1513. err = ftgmac100_setup_mdio(netdev);
  1514. if (err)
  1515. goto err_setup_mdio;
  1516. }
  1517. if (priv->is_aspeed)
  1518. ftgmac100_setup_clk(priv);
  1519. /* Default ring sizes */
  1520. priv->rx_q_entries = priv->new_rx_q_entries = DEF_RX_QUEUE_ENTRIES;
  1521. priv->tx_q_entries = priv->new_tx_q_entries = DEF_TX_QUEUE_ENTRIES;
  1522. /* Base feature set */
  1523. netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_HW_CSUM |
  1524. NETIF_F_GRO | NETIF_F_SG | NETIF_F_HW_VLAN_CTAG_RX |
  1525. NETIF_F_HW_VLAN_CTAG_TX;
  1526. if (priv->use_ncsi)
  1527. netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  1528. /* AST2400 doesn't have working HW checksum generation */
  1529. if (np && (of_device_is_compatible(np, "aspeed,ast2400-mac")))
  1530. netdev->hw_features &= ~NETIF_F_HW_CSUM;
  1531. if (np && of_get_property(np, "no-hw-checksum", NULL))
  1532. netdev->hw_features &= ~(NETIF_F_HW_CSUM | NETIF_F_RXCSUM);
  1533. netdev->features |= netdev->hw_features;
  1534. /* register network device */
  1535. err = register_netdev(netdev);
  1536. if (err) {
  1537. dev_err(&pdev->dev, "Failed to register netdev\n");
  1538. goto err_register_netdev;
  1539. }
  1540. netdev_info(netdev, "irq %d, mapped at %p\n", netdev->irq, priv->base);
  1541. return 0;
  1542. err_ncsi_dev:
  1543. err_register_netdev:
  1544. ftgmac100_destroy_mdio(netdev);
  1545. err_setup_mdio:
  1546. iounmap(priv->base);
  1547. err_ioremap:
  1548. release_resource(priv->res);
  1549. err_req_mem:
  1550. free_netdev(netdev);
  1551. err_alloc_etherdev:
  1552. return err;
  1553. }
  1554. static int ftgmac100_remove(struct platform_device *pdev)
  1555. {
  1556. struct net_device *netdev;
  1557. struct ftgmac100 *priv;
  1558. netdev = platform_get_drvdata(pdev);
  1559. priv = netdev_priv(netdev);
  1560. unregister_netdev(netdev);
  1561. clk_disable_unprepare(priv->clk);
  1562. /* There's a small chance the reset task will have been re-queued,
  1563. * during stop, make sure it's gone before we free the structure.
  1564. */
  1565. cancel_work_sync(&priv->reset_task);
  1566. ftgmac100_destroy_mdio(netdev);
  1567. iounmap(priv->base);
  1568. release_resource(priv->res);
  1569. netif_napi_del(&priv->napi);
  1570. free_netdev(netdev);
  1571. return 0;
  1572. }
  1573. static const struct of_device_id ftgmac100_of_match[] = {
  1574. { .compatible = "faraday,ftgmac100" },
  1575. { }
  1576. };
  1577. MODULE_DEVICE_TABLE(of, ftgmac100_of_match);
  1578. static struct platform_driver ftgmac100_driver = {
  1579. .probe = ftgmac100_probe,
  1580. .remove = ftgmac100_remove,
  1581. .driver = {
  1582. .name = DRV_NAME,
  1583. .of_match_table = ftgmac100_of_match,
  1584. },
  1585. };
  1586. module_platform_driver(ftgmac100_driver);
  1587. MODULE_AUTHOR("Po-Yu Chuang <ratbert@faraday-tech.com>");
  1588. MODULE_DESCRIPTION("FTGMAC100 driver");
  1589. MODULE_LICENSE("GPL");