ftgmac100.c 38 KB

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