ftgmac100.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  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/dma-mapping.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/ethtool.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/io.h>
  27. #include <linux/module.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/of.h>
  30. #include <linux/phy.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/property.h>
  33. #include <net/ip.h>
  34. #include <net/ncsi.h>
  35. #include "ftgmac100.h"
  36. #define DRV_NAME "ftgmac100"
  37. #define DRV_VERSION "0.7"
  38. #define RX_QUEUE_ENTRIES 256 /* must be power of 2 */
  39. #define TX_QUEUE_ENTRIES 512 /* must be power of 2 */
  40. #define MAX_PKT_SIZE 1536
  41. #define RX_BUF_SIZE MAX_PKT_SIZE /* must be smaller than 0x3fff */
  42. struct ftgmac100_descs {
  43. struct ftgmac100_rxdes rxdes[RX_QUEUE_ENTRIES];
  44. struct ftgmac100_txdes txdes[TX_QUEUE_ENTRIES];
  45. };
  46. struct ftgmac100 {
  47. /* Registers */
  48. struct resource *res;
  49. void __iomem *base;
  50. struct ftgmac100_descs *descs;
  51. dma_addr_t descs_dma_addr;
  52. /* Rx ring */
  53. struct sk_buff *rx_skbs[RX_QUEUE_ENTRIES];
  54. unsigned int rx_pointer;
  55. u32 rxdes0_edorr_mask;
  56. /* Tx ring */
  57. unsigned int tx_clean_pointer;
  58. unsigned int tx_pointer;
  59. unsigned int tx_pending;
  60. u32 txdes0_edotr_mask;
  61. spinlock_t tx_lock;
  62. /* Scratch page to use when rx skb alloc fails */
  63. void *rx_scratch;
  64. dma_addr_t rx_scratch_dma;
  65. /* Component structures */
  66. struct net_device *netdev;
  67. struct device *dev;
  68. struct ncsi_dev *ndev;
  69. struct napi_struct napi;
  70. struct work_struct reset_task;
  71. struct mii_bus *mii_bus;
  72. /* Link management */
  73. int cur_speed;
  74. int cur_duplex;
  75. bool use_ncsi;
  76. /* Misc */
  77. bool need_mac_restart;
  78. };
  79. static void ftgmac100_set_rx_ring_base(struct ftgmac100 *priv, dma_addr_t addr)
  80. {
  81. iowrite32(addr, priv->base + FTGMAC100_OFFSET_RXR_BADR);
  82. }
  83. static void ftgmac100_set_rx_buffer_size(struct ftgmac100 *priv,
  84. unsigned int size)
  85. {
  86. size = FTGMAC100_RBSR_SIZE(size);
  87. iowrite32(size, priv->base + FTGMAC100_OFFSET_RBSR);
  88. }
  89. static void ftgmac100_set_normal_prio_tx_ring_base(struct ftgmac100 *priv,
  90. dma_addr_t addr)
  91. {
  92. iowrite32(addr, priv->base + FTGMAC100_OFFSET_NPTXR_BADR);
  93. }
  94. static void ftgmac100_txdma_normal_prio_start_polling(struct ftgmac100 *priv)
  95. {
  96. iowrite32(1, priv->base + FTGMAC100_OFFSET_NPTXPD);
  97. }
  98. static int ftgmac100_reset_mac(struct ftgmac100 *priv, u32 maccr)
  99. {
  100. struct net_device *netdev = priv->netdev;
  101. int i;
  102. /* NOTE: reset clears all registers */
  103. iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
  104. iowrite32(maccr | FTGMAC100_MACCR_SW_RST,
  105. priv->base + FTGMAC100_OFFSET_MACCR);
  106. for (i = 0; i < 50; i++) {
  107. unsigned int maccr;
  108. maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
  109. if (!(maccr & FTGMAC100_MACCR_SW_RST))
  110. return 0;
  111. udelay(1);
  112. }
  113. netdev_err(netdev, "Hardware reset failed\n");
  114. return -EIO;
  115. }
  116. static int ftgmac100_reset_and_config_mac(struct ftgmac100 *priv)
  117. {
  118. u32 maccr = 0;
  119. switch (priv->cur_speed) {
  120. case SPEED_10:
  121. case 0: /* no link */
  122. break;
  123. case SPEED_100:
  124. maccr |= FTGMAC100_MACCR_FAST_MODE;
  125. break;
  126. case SPEED_1000:
  127. maccr |= FTGMAC100_MACCR_GIGA_MODE;
  128. break;
  129. default:
  130. netdev_err(priv->netdev, "Unknown speed %d !\n",
  131. priv->cur_speed);
  132. break;
  133. }
  134. /* (Re)initialize the queue pointers */
  135. priv->rx_pointer = 0;
  136. priv->tx_clean_pointer = 0;
  137. priv->tx_pointer = 0;
  138. priv->tx_pending = 0;
  139. /* The doc says reset twice with 10us interval */
  140. if (ftgmac100_reset_mac(priv, maccr))
  141. return -EIO;
  142. usleep_range(10, 1000);
  143. return ftgmac100_reset_mac(priv, maccr);
  144. }
  145. static void ftgmac100_set_mac(struct ftgmac100 *priv, const unsigned char *mac)
  146. {
  147. unsigned int maddr = mac[0] << 8 | mac[1];
  148. unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
  149. iowrite32(maddr, priv->base + FTGMAC100_OFFSET_MAC_MADR);
  150. iowrite32(laddr, priv->base + FTGMAC100_OFFSET_MAC_LADR);
  151. }
  152. static void ftgmac100_setup_mac(struct ftgmac100 *priv)
  153. {
  154. u8 mac[ETH_ALEN];
  155. unsigned int m;
  156. unsigned int l;
  157. void *addr;
  158. addr = device_get_mac_address(priv->dev, mac, ETH_ALEN);
  159. if (addr) {
  160. ether_addr_copy(priv->netdev->dev_addr, mac);
  161. dev_info(priv->dev, "Read MAC address %pM from device tree\n",
  162. mac);
  163. return;
  164. }
  165. m = ioread32(priv->base + FTGMAC100_OFFSET_MAC_MADR);
  166. l = ioread32(priv->base + FTGMAC100_OFFSET_MAC_LADR);
  167. mac[0] = (m >> 8) & 0xff;
  168. mac[1] = m & 0xff;
  169. mac[2] = (l >> 24) & 0xff;
  170. mac[3] = (l >> 16) & 0xff;
  171. mac[4] = (l >> 8) & 0xff;
  172. mac[5] = l & 0xff;
  173. if (is_valid_ether_addr(mac)) {
  174. ether_addr_copy(priv->netdev->dev_addr, mac);
  175. dev_info(priv->dev, "Read MAC address %pM from chip\n", mac);
  176. } else {
  177. eth_hw_addr_random(priv->netdev);
  178. dev_info(priv->dev, "Generated random MAC address %pM\n",
  179. priv->netdev->dev_addr);
  180. }
  181. }
  182. static int ftgmac100_set_mac_addr(struct net_device *dev, void *p)
  183. {
  184. int ret;
  185. ret = eth_prepare_mac_addr_change(dev, p);
  186. if (ret < 0)
  187. return ret;
  188. eth_commit_mac_addr_change(dev, p);
  189. ftgmac100_set_mac(netdev_priv(dev), dev->dev_addr);
  190. return 0;
  191. }
  192. static void ftgmac100_init_hw(struct ftgmac100 *priv)
  193. {
  194. /* setup ring buffer base registers */
  195. ftgmac100_set_rx_ring_base(priv,
  196. priv->descs_dma_addr +
  197. offsetof(struct ftgmac100_descs, rxdes));
  198. ftgmac100_set_normal_prio_tx_ring_base(priv,
  199. priv->descs_dma_addr +
  200. offsetof(struct ftgmac100_descs, txdes));
  201. ftgmac100_set_rx_buffer_size(priv, RX_BUF_SIZE);
  202. iowrite32(FTGMAC100_APTC_RXPOLL_CNT(1), priv->base + FTGMAC100_OFFSET_APTC);
  203. ftgmac100_set_mac(priv, priv->netdev->dev_addr);
  204. }
  205. static void ftgmac100_start_hw(struct ftgmac100 *priv)
  206. {
  207. u32 maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
  208. /* Keep the original GMAC and FAST bits */
  209. maccr &= (FTGMAC100_MACCR_FAST_MODE | FTGMAC100_MACCR_GIGA_MODE);
  210. /* Add all the main enable bits */
  211. maccr |= FTGMAC100_MACCR_TXDMA_EN |
  212. FTGMAC100_MACCR_RXDMA_EN |
  213. FTGMAC100_MACCR_TXMAC_EN |
  214. FTGMAC100_MACCR_RXMAC_EN |
  215. FTGMAC100_MACCR_CRC_APD |
  216. FTGMAC100_MACCR_PHY_LINK_LEVEL |
  217. FTGMAC100_MACCR_RX_RUNT |
  218. FTGMAC100_MACCR_RX_BROADPKT;
  219. /* Add other bits as needed */
  220. if (priv->cur_duplex == DUPLEX_FULL)
  221. maccr |= FTGMAC100_MACCR_FULLDUP;
  222. /* Hit the HW */
  223. iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
  224. }
  225. static void ftgmac100_stop_hw(struct ftgmac100 *priv)
  226. {
  227. iowrite32(0, priv->base + FTGMAC100_OFFSET_MACCR);
  228. }
  229. static int ftgmac100_alloc_rx_buf(struct ftgmac100 *priv, unsigned int entry,
  230. struct ftgmac100_rxdes *rxdes, gfp_t gfp)
  231. {
  232. struct net_device *netdev = priv->netdev;
  233. struct sk_buff *skb;
  234. dma_addr_t map;
  235. int err;
  236. skb = netdev_alloc_skb_ip_align(netdev, RX_BUF_SIZE);
  237. if (unlikely(!skb)) {
  238. if (net_ratelimit())
  239. netdev_warn(netdev, "failed to allocate rx skb\n");
  240. err = -ENOMEM;
  241. map = priv->rx_scratch_dma;
  242. } else {
  243. map = dma_map_single(priv->dev, skb->data, RX_BUF_SIZE,
  244. DMA_FROM_DEVICE);
  245. if (unlikely(dma_mapping_error(priv->dev, map))) {
  246. if (net_ratelimit())
  247. netdev_err(netdev, "failed to map rx page\n");
  248. dev_kfree_skb_any(skb);
  249. map = priv->rx_scratch_dma;
  250. skb = NULL;
  251. err = -ENOMEM;
  252. }
  253. }
  254. /* Store skb */
  255. priv->rx_skbs[entry] = skb;
  256. /* Store DMA address into RX desc */
  257. rxdes->rxdes3 = cpu_to_le32(map);
  258. /* Ensure the above is ordered vs clearing the OWN bit */
  259. dma_wmb();
  260. /* Clean status (which resets own bit) */
  261. if (entry == (RX_QUEUE_ENTRIES - 1))
  262. rxdes->rxdes0 = cpu_to_le32(priv->rxdes0_edorr_mask);
  263. else
  264. rxdes->rxdes0 = 0;
  265. return 0;
  266. }
  267. static int ftgmac100_next_rx_pointer(int pointer)
  268. {
  269. return (pointer + 1) & (RX_QUEUE_ENTRIES - 1);
  270. }
  271. static void ftgmac100_rx_packet_error(struct ftgmac100 *priv, u32 status)
  272. {
  273. struct net_device *netdev = priv->netdev;
  274. if (status & FTGMAC100_RXDES0_RX_ERR)
  275. netdev->stats.rx_errors++;
  276. if (status & FTGMAC100_RXDES0_CRC_ERR)
  277. netdev->stats.rx_crc_errors++;
  278. if (status & (FTGMAC100_RXDES0_FTL |
  279. FTGMAC100_RXDES0_RUNT |
  280. FTGMAC100_RXDES0_RX_ODD_NB))
  281. netdev->stats.rx_length_errors++;
  282. }
  283. static bool ftgmac100_rx_packet(struct ftgmac100 *priv, int *processed)
  284. {
  285. struct net_device *netdev = priv->netdev;
  286. struct ftgmac100_rxdes *rxdes;
  287. struct sk_buff *skb;
  288. unsigned int pointer, size;
  289. u32 status, csum_vlan;
  290. dma_addr_t map;
  291. /* Grab next RX descriptor */
  292. pointer = priv->rx_pointer;
  293. rxdes = &priv->descs->rxdes[pointer];
  294. /* Grab descriptor status */
  295. status = le32_to_cpu(rxdes->rxdes0);
  296. /* Do we have a packet ? */
  297. if (!(status & FTGMAC100_RXDES0_RXPKT_RDY))
  298. return false;
  299. /* Order subsequent reads with the test for the ready bit */
  300. dma_rmb();
  301. /* We don't cope with fragmented RX packets */
  302. if (unlikely(!(status & FTGMAC100_RXDES0_FRS) ||
  303. !(status & FTGMAC100_RXDES0_LRS)))
  304. goto drop;
  305. /* Grab received size and csum vlan field in the descriptor */
  306. size = status & FTGMAC100_RXDES0_VDBC;
  307. csum_vlan = le32_to_cpu(rxdes->rxdes1);
  308. /* Any error (other than csum offload) flagged ? */
  309. if (unlikely(status & RXDES0_ANY_ERROR)) {
  310. /* Correct for incorrect flagging of runt packets
  311. * with vlan tags... Just accept a runt packet that
  312. * has been flagged as vlan and whose size is at
  313. * least 60 bytes.
  314. */
  315. if ((status & FTGMAC100_RXDES0_RUNT) &&
  316. (csum_vlan & FTGMAC100_RXDES1_VLANTAG_AVAIL) &&
  317. (size >= 60))
  318. status &= ~FTGMAC100_RXDES0_RUNT;
  319. /* Any error still in there ? */
  320. if (status & RXDES0_ANY_ERROR) {
  321. ftgmac100_rx_packet_error(priv, status);
  322. goto drop;
  323. }
  324. }
  325. /* If the packet had no skb (failed to allocate earlier)
  326. * then try to allocate one and skip
  327. */
  328. skb = priv->rx_skbs[pointer];
  329. if (!unlikely(skb)) {
  330. ftgmac100_alloc_rx_buf(priv, pointer, rxdes, GFP_ATOMIC);
  331. goto drop;
  332. }
  333. if (unlikely(status & FTGMAC100_RXDES0_MULTICAST))
  334. netdev->stats.multicast++;
  335. /* If the HW found checksum errors, bounce it to software.
  336. *
  337. * If we didn't, we need to see if the packet was recognized
  338. * by HW as one of the supported checksummed protocols before
  339. * we accept the HW test results.
  340. */
  341. if (netdev->features & NETIF_F_RXCSUM) {
  342. u32 err_bits = FTGMAC100_RXDES1_TCP_CHKSUM_ERR |
  343. FTGMAC100_RXDES1_UDP_CHKSUM_ERR |
  344. FTGMAC100_RXDES1_IP_CHKSUM_ERR;
  345. if ((csum_vlan & err_bits) ||
  346. !(csum_vlan & FTGMAC100_RXDES1_PROT_MASK))
  347. skb->ip_summed = CHECKSUM_NONE;
  348. else
  349. skb->ip_summed = CHECKSUM_UNNECESSARY;
  350. }
  351. /* Transfer received size to skb */
  352. skb_put(skb, size);
  353. /* Tear down DMA mapping, do necessary cache management */
  354. map = le32_to_cpu(rxdes->rxdes3);
  355. #if defined(CONFIG_ARM) && !defined(CONFIG_ARM_DMA_USE_IOMMU)
  356. /* When we don't have an iommu, we can save cycles by not
  357. * invalidating the cache for the part of the packet that
  358. * wasn't received.
  359. */
  360. dma_unmap_single(priv->dev, map, size, DMA_FROM_DEVICE);
  361. #else
  362. dma_unmap_single(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
  363. #endif
  364. /* Resplenish rx ring */
  365. ftgmac100_alloc_rx_buf(priv, pointer, rxdes, GFP_ATOMIC);
  366. priv->rx_pointer = ftgmac100_next_rx_pointer(pointer);
  367. skb->protocol = eth_type_trans(skb, netdev);
  368. netdev->stats.rx_packets++;
  369. netdev->stats.rx_bytes += size;
  370. /* push packet to protocol stack */
  371. if (skb->ip_summed == CHECKSUM_NONE)
  372. netif_receive_skb(skb);
  373. else
  374. napi_gro_receive(&priv->napi, skb);
  375. (*processed)++;
  376. return true;
  377. drop:
  378. /* Clean rxdes0 (which resets own bit) */
  379. rxdes->rxdes0 = cpu_to_le32(status & priv->rxdes0_edorr_mask);
  380. priv->rx_pointer = ftgmac100_next_rx_pointer(pointer);
  381. netdev->stats.rx_dropped++;
  382. return true;
  383. }
  384. static void ftgmac100_txdes_reset(const struct ftgmac100 *priv,
  385. struct ftgmac100_txdes *txdes)
  386. {
  387. /* clear all except end of ring bit */
  388. txdes->txdes0 &= cpu_to_le32(priv->txdes0_edotr_mask);
  389. txdes->txdes1 = 0;
  390. txdes->txdes2 = 0;
  391. txdes->txdes3 = 0;
  392. }
  393. static bool ftgmac100_txdes_owned_by_dma(struct ftgmac100_txdes *txdes)
  394. {
  395. return txdes->txdes0 & cpu_to_le32(FTGMAC100_TXDES0_TXDMA_OWN);
  396. }
  397. static void ftgmac100_txdes_set_dma_own(struct ftgmac100_txdes *txdes)
  398. {
  399. /*
  400. * Make sure dma own bit will not be set before any other
  401. * descriptor fields.
  402. */
  403. wmb();
  404. txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_TXDMA_OWN);
  405. }
  406. static void ftgmac100_txdes_set_end_of_ring(const struct ftgmac100 *priv,
  407. struct ftgmac100_txdes *txdes)
  408. {
  409. txdes->txdes0 |= cpu_to_le32(priv->txdes0_edotr_mask);
  410. }
  411. static void ftgmac100_txdes_set_first_segment(struct ftgmac100_txdes *txdes)
  412. {
  413. txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_FTS);
  414. }
  415. static void ftgmac100_txdes_set_last_segment(struct ftgmac100_txdes *txdes)
  416. {
  417. txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_LTS);
  418. }
  419. static void ftgmac100_txdes_set_buffer_size(struct ftgmac100_txdes *txdes,
  420. unsigned int len)
  421. {
  422. txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_TXBUF_SIZE(len));
  423. }
  424. static void ftgmac100_txdes_set_txint(struct ftgmac100_txdes *txdes)
  425. {
  426. txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_TXIC);
  427. }
  428. static void ftgmac100_txdes_set_tcpcs(struct ftgmac100_txdes *txdes)
  429. {
  430. txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_TCP_CHKSUM);
  431. }
  432. static void ftgmac100_txdes_set_udpcs(struct ftgmac100_txdes *txdes)
  433. {
  434. txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_UDP_CHKSUM);
  435. }
  436. static void ftgmac100_txdes_set_ipcs(struct ftgmac100_txdes *txdes)
  437. {
  438. txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_IP_CHKSUM);
  439. }
  440. static void ftgmac100_txdes_set_dma_addr(struct ftgmac100_txdes *txdes,
  441. dma_addr_t addr)
  442. {
  443. txdes->txdes3 = cpu_to_le32(addr);
  444. }
  445. static dma_addr_t ftgmac100_txdes_get_dma_addr(struct ftgmac100_txdes *txdes)
  446. {
  447. return le32_to_cpu(txdes->txdes3);
  448. }
  449. /*
  450. * txdes2 is not used by hardware. We use it to keep track of socket buffer.
  451. * Since hardware does not touch it, we can skip cpu_to_le32()/le32_to_cpu().
  452. */
  453. static void ftgmac100_txdes_set_skb(struct ftgmac100_txdes *txdes,
  454. struct sk_buff *skb)
  455. {
  456. txdes->txdes2 = (unsigned int)skb;
  457. }
  458. static struct sk_buff *ftgmac100_txdes_get_skb(struct ftgmac100_txdes *txdes)
  459. {
  460. return (struct sk_buff *)txdes->txdes2;
  461. }
  462. static int ftgmac100_next_tx_pointer(int pointer)
  463. {
  464. return (pointer + 1) & (TX_QUEUE_ENTRIES - 1);
  465. }
  466. static void ftgmac100_tx_pointer_advance(struct ftgmac100 *priv)
  467. {
  468. priv->tx_pointer = ftgmac100_next_tx_pointer(priv->tx_pointer);
  469. }
  470. static void ftgmac100_tx_clean_pointer_advance(struct ftgmac100 *priv)
  471. {
  472. priv->tx_clean_pointer = ftgmac100_next_tx_pointer(priv->tx_clean_pointer);
  473. }
  474. static struct ftgmac100_txdes *ftgmac100_current_txdes(struct ftgmac100 *priv)
  475. {
  476. return &priv->descs->txdes[priv->tx_pointer];
  477. }
  478. static struct ftgmac100_txdes *
  479. ftgmac100_current_clean_txdes(struct ftgmac100 *priv)
  480. {
  481. return &priv->descs->txdes[priv->tx_clean_pointer];
  482. }
  483. static bool ftgmac100_tx_complete_packet(struct ftgmac100 *priv)
  484. {
  485. struct net_device *netdev = priv->netdev;
  486. struct ftgmac100_txdes *txdes;
  487. struct sk_buff *skb;
  488. dma_addr_t map;
  489. if (priv->tx_pending == 0)
  490. return false;
  491. txdes = ftgmac100_current_clean_txdes(priv);
  492. if (ftgmac100_txdes_owned_by_dma(txdes))
  493. return false;
  494. skb = ftgmac100_txdes_get_skb(txdes);
  495. map = ftgmac100_txdes_get_dma_addr(txdes);
  496. netdev->stats.tx_packets++;
  497. netdev->stats.tx_bytes += skb->len;
  498. dma_unmap_single(priv->dev, map, skb_headlen(skb), DMA_TO_DEVICE);
  499. dev_kfree_skb(skb);
  500. ftgmac100_txdes_reset(priv, txdes);
  501. ftgmac100_tx_clean_pointer_advance(priv);
  502. spin_lock(&priv->tx_lock);
  503. priv->tx_pending--;
  504. spin_unlock(&priv->tx_lock);
  505. netif_wake_queue(netdev);
  506. return true;
  507. }
  508. static void ftgmac100_tx_complete(struct ftgmac100 *priv)
  509. {
  510. while (ftgmac100_tx_complete_packet(priv))
  511. ;
  512. }
  513. static int ftgmac100_xmit(struct ftgmac100 *priv, struct sk_buff *skb,
  514. dma_addr_t map)
  515. {
  516. struct net_device *netdev = priv->netdev;
  517. struct ftgmac100_txdes *txdes;
  518. unsigned int len = (skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len;
  519. txdes = ftgmac100_current_txdes(priv);
  520. ftgmac100_tx_pointer_advance(priv);
  521. /* setup TX descriptor */
  522. ftgmac100_txdes_set_skb(txdes, skb);
  523. ftgmac100_txdes_set_dma_addr(txdes, map);
  524. ftgmac100_txdes_set_buffer_size(txdes, len);
  525. ftgmac100_txdes_set_first_segment(txdes);
  526. ftgmac100_txdes_set_last_segment(txdes);
  527. ftgmac100_txdes_set_txint(txdes);
  528. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  529. __be16 protocol = skb->protocol;
  530. if (protocol == cpu_to_be16(ETH_P_IP)) {
  531. u8 ip_proto = ip_hdr(skb)->protocol;
  532. ftgmac100_txdes_set_ipcs(txdes);
  533. if (ip_proto == IPPROTO_TCP)
  534. ftgmac100_txdes_set_tcpcs(txdes);
  535. else if (ip_proto == IPPROTO_UDP)
  536. ftgmac100_txdes_set_udpcs(txdes);
  537. }
  538. }
  539. spin_lock(&priv->tx_lock);
  540. priv->tx_pending++;
  541. if (priv->tx_pending == TX_QUEUE_ENTRIES)
  542. netif_stop_queue(netdev);
  543. /* start transmit */
  544. ftgmac100_txdes_set_dma_own(txdes);
  545. spin_unlock(&priv->tx_lock);
  546. ftgmac100_txdma_normal_prio_start_polling(priv);
  547. return NETDEV_TX_OK;
  548. }
  549. static void ftgmac100_free_buffers(struct ftgmac100 *priv)
  550. {
  551. int i;
  552. /* Free all RX buffers */
  553. for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
  554. struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[i];
  555. struct sk_buff *skb = priv->rx_skbs[i];
  556. dma_addr_t map = le32_to_cpu(rxdes->rxdes3);
  557. if (!skb)
  558. continue;
  559. priv->rx_skbs[i] = NULL;
  560. dma_unmap_single(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
  561. dev_kfree_skb_any(skb);
  562. }
  563. /* Free all TX buffers */
  564. for (i = 0; i < TX_QUEUE_ENTRIES; i++) {
  565. struct ftgmac100_txdes *txdes = &priv->descs->txdes[i];
  566. struct sk_buff *skb = ftgmac100_txdes_get_skb(txdes);
  567. dma_addr_t map = ftgmac100_txdes_get_dma_addr(txdes);
  568. if (!skb)
  569. continue;
  570. dma_unmap_single(priv->dev, map, skb_headlen(skb), DMA_TO_DEVICE);
  571. kfree_skb(skb);
  572. }
  573. }
  574. static void ftgmac100_free_rings(struct ftgmac100 *priv)
  575. {
  576. /* Free descriptors */
  577. if (priv->descs)
  578. dma_free_coherent(priv->dev, sizeof(struct ftgmac100_descs),
  579. priv->descs, priv->descs_dma_addr);
  580. /* Free scratch packet buffer */
  581. if (priv->rx_scratch)
  582. dma_free_coherent(priv->dev, RX_BUF_SIZE,
  583. priv->rx_scratch, priv->rx_scratch_dma);
  584. }
  585. static int ftgmac100_alloc_rings(struct ftgmac100 *priv)
  586. {
  587. /* Allocate descriptors */
  588. priv->descs = dma_zalloc_coherent(priv->dev,
  589. sizeof(struct ftgmac100_descs),
  590. &priv->descs_dma_addr, GFP_KERNEL);
  591. if (!priv->descs)
  592. return -ENOMEM;
  593. /* Allocate scratch packet buffer */
  594. priv->rx_scratch = dma_alloc_coherent(priv->dev,
  595. RX_BUF_SIZE,
  596. &priv->rx_scratch_dma,
  597. GFP_KERNEL);
  598. if (!priv->rx_scratch)
  599. return -ENOMEM;
  600. return 0;
  601. }
  602. static void ftgmac100_init_rings(struct ftgmac100 *priv)
  603. {
  604. struct ftgmac100_rxdes *rxdes;
  605. int i;
  606. /* Initialize RX ring */
  607. for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
  608. rxdes = &priv->descs->rxdes[i];
  609. rxdes->rxdes0 = 0;
  610. rxdes->rxdes3 = cpu_to_le32(priv->rx_scratch_dma);
  611. }
  612. /* Mark the end of the ring */
  613. rxdes->rxdes0 |= cpu_to_le32(priv->rxdes0_edorr_mask);
  614. /* Initialize TX ring */
  615. for (i = 0; i < TX_QUEUE_ENTRIES; i++)
  616. priv->descs->txdes[i].txdes0 = 0;
  617. ftgmac100_txdes_set_end_of_ring(priv, &priv->descs->txdes[i -1]);
  618. }
  619. static int ftgmac100_alloc_rx_buffers(struct ftgmac100 *priv)
  620. {
  621. int i;
  622. for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
  623. struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[i];
  624. if (ftgmac100_alloc_rx_buf(priv, i, rxdes, GFP_KERNEL))
  625. return -ENOMEM;
  626. }
  627. return 0;
  628. }
  629. static void ftgmac100_adjust_link(struct net_device *netdev)
  630. {
  631. struct ftgmac100 *priv = netdev_priv(netdev);
  632. struct phy_device *phydev = netdev->phydev;
  633. int new_speed;
  634. /* We store "no link" as speed 0 */
  635. if (!phydev->link)
  636. new_speed = 0;
  637. else
  638. new_speed = phydev->speed;
  639. if (phydev->speed == priv->cur_speed &&
  640. phydev->duplex == priv->cur_duplex)
  641. return;
  642. /* Print status if we have a link or we had one and just lost it,
  643. * don't print otherwise.
  644. */
  645. if (new_speed || priv->cur_speed)
  646. phy_print_status(phydev);
  647. priv->cur_speed = new_speed;
  648. priv->cur_duplex = phydev->duplex;
  649. /* Link is down, do nothing else */
  650. if (!new_speed)
  651. return;
  652. /* Disable all interrupts */
  653. iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
  654. /* Reset the adapter asynchronously */
  655. schedule_work(&priv->reset_task);
  656. }
  657. static int ftgmac100_mii_probe(struct ftgmac100 *priv)
  658. {
  659. struct net_device *netdev = priv->netdev;
  660. struct phy_device *phydev;
  661. phydev = phy_find_first(priv->mii_bus);
  662. if (!phydev) {
  663. netdev_info(netdev, "%s: no PHY found\n", netdev->name);
  664. return -ENODEV;
  665. }
  666. phydev = phy_connect(netdev, phydev_name(phydev),
  667. &ftgmac100_adjust_link, PHY_INTERFACE_MODE_GMII);
  668. if (IS_ERR(phydev)) {
  669. netdev_err(netdev, "%s: Could not attach to PHY\n", netdev->name);
  670. return PTR_ERR(phydev);
  671. }
  672. return 0;
  673. }
  674. static int ftgmac100_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
  675. {
  676. struct net_device *netdev = bus->priv;
  677. struct ftgmac100 *priv = netdev_priv(netdev);
  678. unsigned int phycr;
  679. int i;
  680. phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
  681. /* preserve MDC cycle threshold */
  682. phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
  683. phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
  684. FTGMAC100_PHYCR_REGAD(regnum) |
  685. FTGMAC100_PHYCR_MIIRD;
  686. iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
  687. for (i = 0; i < 10; i++) {
  688. phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
  689. if ((phycr & FTGMAC100_PHYCR_MIIRD) == 0) {
  690. int data;
  691. data = ioread32(priv->base + FTGMAC100_OFFSET_PHYDATA);
  692. return FTGMAC100_PHYDATA_MIIRDATA(data);
  693. }
  694. udelay(100);
  695. }
  696. netdev_err(netdev, "mdio read timed out\n");
  697. return -EIO;
  698. }
  699. static int ftgmac100_mdiobus_write(struct mii_bus *bus, int phy_addr,
  700. int regnum, u16 value)
  701. {
  702. struct net_device *netdev = bus->priv;
  703. struct ftgmac100 *priv = netdev_priv(netdev);
  704. unsigned int phycr;
  705. int data;
  706. int i;
  707. phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
  708. /* preserve MDC cycle threshold */
  709. phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
  710. phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
  711. FTGMAC100_PHYCR_REGAD(regnum) |
  712. FTGMAC100_PHYCR_MIIWR;
  713. data = FTGMAC100_PHYDATA_MIIWDATA(value);
  714. iowrite32(data, priv->base + FTGMAC100_OFFSET_PHYDATA);
  715. iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
  716. for (i = 0; i < 10; i++) {
  717. phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
  718. if ((phycr & FTGMAC100_PHYCR_MIIWR) == 0)
  719. return 0;
  720. udelay(100);
  721. }
  722. netdev_err(netdev, "mdio write timed out\n");
  723. return -EIO;
  724. }
  725. static void ftgmac100_get_drvinfo(struct net_device *netdev,
  726. struct ethtool_drvinfo *info)
  727. {
  728. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  729. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  730. strlcpy(info->bus_info, dev_name(&netdev->dev), sizeof(info->bus_info));
  731. }
  732. static const struct ethtool_ops ftgmac100_ethtool_ops = {
  733. .get_drvinfo = ftgmac100_get_drvinfo,
  734. .get_link = ethtool_op_get_link,
  735. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  736. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  737. };
  738. static irqreturn_t ftgmac100_interrupt(int irq, void *dev_id)
  739. {
  740. struct net_device *netdev = dev_id;
  741. struct ftgmac100 *priv = netdev_priv(netdev);
  742. unsigned int status, new_mask = FTGMAC100_INT_BAD;
  743. /* Fetch and clear interrupt bits, process abnormal ones */
  744. status = ioread32(priv->base + FTGMAC100_OFFSET_ISR);
  745. iowrite32(status, priv->base + FTGMAC100_OFFSET_ISR);
  746. if (unlikely(status & FTGMAC100_INT_BAD)) {
  747. /* RX buffer unavailable */
  748. if (status & FTGMAC100_INT_NO_RXBUF)
  749. netdev->stats.rx_over_errors++;
  750. /* received packet lost due to RX FIFO full */
  751. if (status & FTGMAC100_INT_RPKT_LOST)
  752. netdev->stats.rx_fifo_errors++;
  753. /* sent packet lost due to excessive TX collision */
  754. if (status & FTGMAC100_INT_XPKT_LOST)
  755. netdev->stats.tx_fifo_errors++;
  756. /* AHB error -> Reset the chip */
  757. if (status & FTGMAC100_INT_AHB_ERR) {
  758. if (net_ratelimit())
  759. netdev_warn(netdev,
  760. "AHB bus error ! Resetting chip.\n");
  761. iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
  762. schedule_work(&priv->reset_task);
  763. return IRQ_HANDLED;
  764. }
  765. /* We may need to restart the MAC after such errors, delay
  766. * this until after we have freed some Rx buffers though
  767. */
  768. priv->need_mac_restart = true;
  769. /* Disable those errors until we restart */
  770. new_mask &= ~status;
  771. }
  772. /* Only enable "bad" interrupts while NAPI is on */
  773. iowrite32(new_mask, priv->base + FTGMAC100_OFFSET_IER);
  774. /* Schedule NAPI bh */
  775. napi_schedule_irqoff(&priv->napi);
  776. return IRQ_HANDLED;
  777. }
  778. static bool ftgmac100_check_rx(struct ftgmac100 *priv)
  779. {
  780. struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[priv->rx_pointer];
  781. /* Do we have a packet ? */
  782. return !!(rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RXPKT_RDY));
  783. }
  784. static int ftgmac100_poll(struct napi_struct *napi, int budget)
  785. {
  786. struct ftgmac100 *priv = container_of(napi, struct ftgmac100, napi);
  787. bool more, completed = true;
  788. int rx = 0;
  789. ftgmac100_tx_complete(priv);
  790. do {
  791. more = ftgmac100_rx_packet(priv, &rx);
  792. } while (more && rx < budget);
  793. if (more && rx == budget)
  794. completed = false;
  795. /* The interrupt is telling us to kick the MAC back to life
  796. * after an RX overflow
  797. */
  798. if (unlikely(priv->need_mac_restart)) {
  799. ftgmac100_start_hw(priv);
  800. /* Re-enable "bad" interrupts */
  801. iowrite32(FTGMAC100_INT_BAD,
  802. priv->base + FTGMAC100_OFFSET_IER);
  803. }
  804. /* Keep NAPI going if we have still packets to reclaim */
  805. if (priv->tx_pending)
  806. return budget;
  807. if (completed) {
  808. /* We are about to re-enable all interrupts. However
  809. * the HW has been latching RX/TX packet interrupts while
  810. * they were masked. So we clear them first, then we need
  811. * to re-check if there's something to process
  812. */
  813. iowrite32(FTGMAC100_INT_RXTX,
  814. priv->base + FTGMAC100_OFFSET_ISR);
  815. if (ftgmac100_check_rx(priv) || priv->tx_pending)
  816. return budget;
  817. /* deschedule NAPI */
  818. napi_complete(napi);
  819. /* enable all interrupts */
  820. iowrite32(FTGMAC100_INT_ALL,
  821. priv->base + FTGMAC100_OFFSET_IER);
  822. }
  823. return rx;
  824. }
  825. static int ftgmac100_init_all(struct ftgmac100 *priv, bool ignore_alloc_err)
  826. {
  827. int err = 0;
  828. /* Re-init descriptors (adjust queue sizes) */
  829. ftgmac100_init_rings(priv);
  830. /* Realloc rx descriptors */
  831. err = ftgmac100_alloc_rx_buffers(priv);
  832. if (err && !ignore_alloc_err)
  833. return err;
  834. /* Reinit and restart HW */
  835. ftgmac100_init_hw(priv);
  836. ftgmac100_start_hw(priv);
  837. /* Re-enable the device */
  838. napi_enable(&priv->napi);
  839. netif_start_queue(priv->netdev);
  840. /* Enable all interrupts */
  841. iowrite32(FTGMAC100_INT_ALL, priv->base + FTGMAC100_OFFSET_IER);
  842. return err;
  843. }
  844. static void ftgmac100_reset_task(struct work_struct *work)
  845. {
  846. struct ftgmac100 *priv = container_of(work, struct ftgmac100,
  847. reset_task);
  848. struct net_device *netdev = priv->netdev;
  849. int err;
  850. netdev_dbg(netdev, "Resetting NIC...\n");
  851. /* Lock the world */
  852. rtnl_lock();
  853. if (netdev->phydev)
  854. mutex_lock(&netdev->phydev->lock);
  855. if (priv->mii_bus)
  856. mutex_lock(&priv->mii_bus->mdio_lock);
  857. /* Check if the interface is still up */
  858. if (!netif_running(netdev))
  859. goto bail;
  860. /* Stop the network stack */
  861. netif_trans_update(netdev);
  862. napi_disable(&priv->napi);
  863. netif_tx_disable(netdev);
  864. /* Stop and reset the MAC */
  865. ftgmac100_stop_hw(priv);
  866. err = ftgmac100_reset_and_config_mac(priv);
  867. if (err) {
  868. /* Not much we can do ... it might come back... */
  869. netdev_err(netdev, "attempting to continue...\n");
  870. }
  871. /* Free all rx and tx buffers */
  872. ftgmac100_free_buffers(priv);
  873. /* Setup everything again and restart chip */
  874. ftgmac100_init_all(priv, true);
  875. netdev_dbg(netdev, "Reset done !\n");
  876. bail:
  877. if (priv->mii_bus)
  878. mutex_unlock(&priv->mii_bus->mdio_lock);
  879. if (netdev->phydev)
  880. mutex_unlock(&netdev->phydev->lock);
  881. rtnl_unlock();
  882. }
  883. static int ftgmac100_open(struct net_device *netdev)
  884. {
  885. struct ftgmac100 *priv = netdev_priv(netdev);
  886. int err;
  887. /* Allocate ring buffers */
  888. err = ftgmac100_alloc_rings(priv);
  889. if (err) {
  890. netdev_err(netdev, "Failed to allocate descriptors\n");
  891. return err;
  892. }
  893. /* When using NC-SI we force the speed to 100Mbit/s full duplex,
  894. *
  895. * Otherwise we leave it set to 0 (no link), the link
  896. * message from the PHY layer will handle setting it up to
  897. * something else if needed.
  898. */
  899. if (priv->use_ncsi) {
  900. priv->cur_duplex = DUPLEX_FULL;
  901. priv->cur_speed = SPEED_100;
  902. } else {
  903. priv->cur_duplex = 0;
  904. priv->cur_speed = 0;
  905. }
  906. /* Reset the hardware */
  907. err = ftgmac100_reset_and_config_mac(priv);
  908. if (err)
  909. goto err_hw;
  910. /* Initialize NAPI */
  911. netif_napi_add(netdev, &priv->napi, ftgmac100_poll, 64);
  912. /* Grab our interrupt */
  913. err = request_irq(netdev->irq, ftgmac100_interrupt, 0, netdev->name, netdev);
  914. if (err) {
  915. netdev_err(netdev, "failed to request irq %d\n", netdev->irq);
  916. goto err_irq;
  917. }
  918. /* Start things up */
  919. err = ftgmac100_init_all(priv, false);
  920. if (err) {
  921. netdev_err(netdev, "Failed to allocate packet buffers\n");
  922. goto err_alloc;
  923. }
  924. if (netdev->phydev) {
  925. /* If we have a PHY, start polling */
  926. phy_start(netdev->phydev);
  927. } else if (priv->use_ncsi) {
  928. /* If using NC-SI, set our carrier on and start the stack */
  929. netif_carrier_on(netdev);
  930. /* Start the NCSI device */
  931. err = ncsi_start_dev(priv->ndev);
  932. if (err)
  933. goto err_ncsi;
  934. }
  935. return 0;
  936. err_ncsi:
  937. napi_disable(&priv->napi);
  938. netif_stop_queue(netdev);
  939. err_alloc:
  940. ftgmac100_free_buffers(priv);
  941. free_irq(netdev->irq, netdev);
  942. err_irq:
  943. netif_napi_del(&priv->napi);
  944. err_hw:
  945. iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
  946. ftgmac100_free_rings(priv);
  947. return err;
  948. }
  949. static int ftgmac100_stop(struct net_device *netdev)
  950. {
  951. struct ftgmac100 *priv = netdev_priv(netdev);
  952. /* Note about the reset task: We are called with the rtnl lock
  953. * held, so we are synchronized against the core of the reset
  954. * task. We must not try to synchronously cancel it otherwise
  955. * we can deadlock. But since it will test for netif_running()
  956. * which has already been cleared by the net core, we don't
  957. * anything special to do.
  958. */
  959. /* disable all interrupts */
  960. iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
  961. netif_stop_queue(netdev);
  962. napi_disable(&priv->napi);
  963. netif_napi_del(&priv->napi);
  964. if (netdev->phydev)
  965. phy_stop(netdev->phydev);
  966. else if (priv->use_ncsi)
  967. ncsi_stop_dev(priv->ndev);
  968. ftgmac100_stop_hw(priv);
  969. free_irq(netdev->irq, netdev);
  970. ftgmac100_free_buffers(priv);
  971. ftgmac100_free_rings(priv);
  972. return 0;
  973. }
  974. static int ftgmac100_hard_start_xmit(struct sk_buff *skb,
  975. struct net_device *netdev)
  976. {
  977. struct ftgmac100 *priv = netdev_priv(netdev);
  978. dma_addr_t map;
  979. if (unlikely(skb->len > MAX_PKT_SIZE)) {
  980. if (net_ratelimit())
  981. netdev_dbg(netdev, "tx packet too big\n");
  982. netdev->stats.tx_dropped++;
  983. kfree_skb(skb);
  984. return NETDEV_TX_OK;
  985. }
  986. map = dma_map_single(priv->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
  987. if (unlikely(dma_mapping_error(priv->dev, map))) {
  988. /* drop packet */
  989. if (net_ratelimit())
  990. netdev_err(netdev, "map socket buffer failed\n");
  991. netdev->stats.tx_dropped++;
  992. kfree_skb(skb);
  993. return NETDEV_TX_OK;
  994. }
  995. return ftgmac100_xmit(priv, skb, map);
  996. }
  997. /* optional */
  998. static int ftgmac100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  999. {
  1000. if (!netdev->phydev)
  1001. return -ENXIO;
  1002. return phy_mii_ioctl(netdev->phydev, ifr, cmd);
  1003. }
  1004. static const struct net_device_ops ftgmac100_netdev_ops = {
  1005. .ndo_open = ftgmac100_open,
  1006. .ndo_stop = ftgmac100_stop,
  1007. .ndo_start_xmit = ftgmac100_hard_start_xmit,
  1008. .ndo_set_mac_address = ftgmac100_set_mac_addr,
  1009. .ndo_validate_addr = eth_validate_addr,
  1010. .ndo_do_ioctl = ftgmac100_do_ioctl,
  1011. };
  1012. static int ftgmac100_setup_mdio(struct net_device *netdev)
  1013. {
  1014. struct ftgmac100 *priv = netdev_priv(netdev);
  1015. struct platform_device *pdev = to_platform_device(priv->dev);
  1016. int i, err = 0;
  1017. u32 reg;
  1018. /* initialize mdio bus */
  1019. priv->mii_bus = mdiobus_alloc();
  1020. if (!priv->mii_bus)
  1021. return -EIO;
  1022. if (of_machine_is_compatible("aspeed,ast2400") ||
  1023. of_machine_is_compatible("aspeed,ast2500")) {
  1024. /* This driver supports the old MDIO interface */
  1025. reg = ioread32(priv->base + FTGMAC100_OFFSET_REVR);
  1026. reg &= ~FTGMAC100_REVR_NEW_MDIO_INTERFACE;
  1027. iowrite32(reg, priv->base + FTGMAC100_OFFSET_REVR);
  1028. };
  1029. priv->mii_bus->name = "ftgmac100_mdio";
  1030. snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%d",
  1031. pdev->name, pdev->id);
  1032. priv->mii_bus->priv = priv->netdev;
  1033. priv->mii_bus->read = ftgmac100_mdiobus_read;
  1034. priv->mii_bus->write = ftgmac100_mdiobus_write;
  1035. for (i = 0; i < PHY_MAX_ADDR; i++)
  1036. priv->mii_bus->irq[i] = PHY_POLL;
  1037. err = mdiobus_register(priv->mii_bus);
  1038. if (err) {
  1039. dev_err(priv->dev, "Cannot register MDIO bus!\n");
  1040. goto err_register_mdiobus;
  1041. }
  1042. err = ftgmac100_mii_probe(priv);
  1043. if (err) {
  1044. dev_err(priv->dev, "MII Probe failed!\n");
  1045. goto err_mii_probe;
  1046. }
  1047. return 0;
  1048. err_mii_probe:
  1049. mdiobus_unregister(priv->mii_bus);
  1050. err_register_mdiobus:
  1051. mdiobus_free(priv->mii_bus);
  1052. return err;
  1053. }
  1054. static void ftgmac100_destroy_mdio(struct net_device *netdev)
  1055. {
  1056. struct ftgmac100 *priv = netdev_priv(netdev);
  1057. if (!netdev->phydev)
  1058. return;
  1059. phy_disconnect(netdev->phydev);
  1060. mdiobus_unregister(priv->mii_bus);
  1061. mdiobus_free(priv->mii_bus);
  1062. }
  1063. static void ftgmac100_ncsi_handler(struct ncsi_dev *nd)
  1064. {
  1065. if (unlikely(nd->state != ncsi_dev_state_functional))
  1066. return;
  1067. netdev_info(nd->dev, "NCSI interface %s\n",
  1068. nd->link_up ? "up" : "down");
  1069. }
  1070. static int ftgmac100_probe(struct platform_device *pdev)
  1071. {
  1072. struct resource *res;
  1073. int irq;
  1074. struct net_device *netdev;
  1075. struct ftgmac100 *priv;
  1076. int err = 0;
  1077. if (!pdev)
  1078. return -ENODEV;
  1079. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1080. if (!res)
  1081. return -ENXIO;
  1082. irq = platform_get_irq(pdev, 0);
  1083. if (irq < 0)
  1084. return irq;
  1085. /* setup net_device */
  1086. netdev = alloc_etherdev(sizeof(*priv));
  1087. if (!netdev) {
  1088. err = -ENOMEM;
  1089. goto err_alloc_etherdev;
  1090. }
  1091. SET_NETDEV_DEV(netdev, &pdev->dev);
  1092. netdev->ethtool_ops = &ftgmac100_ethtool_ops;
  1093. netdev->netdev_ops = &ftgmac100_netdev_ops;
  1094. platform_set_drvdata(pdev, netdev);
  1095. /* setup private data */
  1096. priv = netdev_priv(netdev);
  1097. priv->netdev = netdev;
  1098. priv->dev = &pdev->dev;
  1099. INIT_WORK(&priv->reset_task, ftgmac100_reset_task);
  1100. spin_lock_init(&priv->tx_lock);
  1101. /* map io memory */
  1102. priv->res = request_mem_region(res->start, resource_size(res),
  1103. dev_name(&pdev->dev));
  1104. if (!priv->res) {
  1105. dev_err(&pdev->dev, "Could not reserve memory region\n");
  1106. err = -ENOMEM;
  1107. goto err_req_mem;
  1108. }
  1109. priv->base = ioremap(res->start, resource_size(res));
  1110. if (!priv->base) {
  1111. dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n");
  1112. err = -EIO;
  1113. goto err_ioremap;
  1114. }
  1115. netdev->irq = irq;
  1116. /* MAC address from chip or random one */
  1117. ftgmac100_setup_mac(priv);
  1118. if (of_machine_is_compatible("aspeed,ast2400") ||
  1119. of_machine_is_compatible("aspeed,ast2500")) {
  1120. priv->rxdes0_edorr_mask = BIT(30);
  1121. priv->txdes0_edotr_mask = BIT(30);
  1122. } else {
  1123. priv->rxdes0_edorr_mask = BIT(15);
  1124. priv->txdes0_edotr_mask = BIT(15);
  1125. }
  1126. if (pdev->dev.of_node &&
  1127. of_get_property(pdev->dev.of_node, "use-ncsi", NULL)) {
  1128. if (!IS_ENABLED(CONFIG_NET_NCSI)) {
  1129. dev_err(&pdev->dev, "NCSI stack not enabled\n");
  1130. goto err_ncsi_dev;
  1131. }
  1132. dev_info(&pdev->dev, "Using NCSI interface\n");
  1133. priv->use_ncsi = true;
  1134. priv->ndev = ncsi_register_dev(netdev, ftgmac100_ncsi_handler);
  1135. if (!priv->ndev)
  1136. goto err_ncsi_dev;
  1137. } else {
  1138. priv->use_ncsi = false;
  1139. err = ftgmac100_setup_mdio(netdev);
  1140. if (err)
  1141. goto err_setup_mdio;
  1142. }
  1143. /* We have to disable on-chip IP checksum functionality
  1144. * when NCSI is enabled on the interface. It doesn't work
  1145. * in that case.
  1146. */
  1147. netdev->features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_GRO;
  1148. if (priv->use_ncsi &&
  1149. of_get_property(pdev->dev.of_node, "no-hw-checksum", NULL))
  1150. netdev->features &= ~NETIF_F_IP_CSUM;
  1151. /* register network device */
  1152. err = register_netdev(netdev);
  1153. if (err) {
  1154. dev_err(&pdev->dev, "Failed to register netdev\n");
  1155. goto err_register_netdev;
  1156. }
  1157. netdev_info(netdev, "irq %d, mapped at %p\n", netdev->irq, priv->base);
  1158. return 0;
  1159. err_ncsi_dev:
  1160. err_register_netdev:
  1161. ftgmac100_destroy_mdio(netdev);
  1162. err_setup_mdio:
  1163. iounmap(priv->base);
  1164. err_ioremap:
  1165. release_resource(priv->res);
  1166. err_req_mem:
  1167. netif_napi_del(&priv->napi);
  1168. free_netdev(netdev);
  1169. err_alloc_etherdev:
  1170. return err;
  1171. }
  1172. static int ftgmac100_remove(struct platform_device *pdev)
  1173. {
  1174. struct net_device *netdev;
  1175. struct ftgmac100 *priv;
  1176. netdev = platform_get_drvdata(pdev);
  1177. priv = netdev_priv(netdev);
  1178. unregister_netdev(netdev);
  1179. /* There's a small chance the reset task will have been re-queued,
  1180. * during stop, make sure it's gone before we free the structure.
  1181. */
  1182. cancel_work_sync(&priv->reset_task);
  1183. ftgmac100_destroy_mdio(netdev);
  1184. iounmap(priv->base);
  1185. release_resource(priv->res);
  1186. netif_napi_del(&priv->napi);
  1187. free_netdev(netdev);
  1188. return 0;
  1189. }
  1190. static const struct of_device_id ftgmac100_of_match[] = {
  1191. { .compatible = "faraday,ftgmac100" },
  1192. { }
  1193. };
  1194. MODULE_DEVICE_TABLE(of, ftgmac100_of_match);
  1195. static struct platform_driver ftgmac100_driver = {
  1196. .probe = ftgmac100_probe,
  1197. .remove = ftgmac100_remove,
  1198. .driver = {
  1199. .name = DRV_NAME,
  1200. .of_match_table = ftgmac100_of_match,
  1201. },
  1202. };
  1203. module_platform_driver(ftgmac100_driver);
  1204. MODULE_AUTHOR("Po-Yu Chuang <ratbert@faraday-tech.com>");
  1205. MODULE_DESCRIPTION("FTGMAC100 driver");
  1206. MODULE_LICENSE("GPL");