emac.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /* Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. /* Qualcomm Technologies, Inc. EMAC Gigabit Ethernet Driver */
  13. #include <linux/if_ether.h>
  14. #include <linux/if_vlan.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_net.h>
  20. #include <linux/of_device.h>
  21. #include <linux/phy.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/acpi.h>
  24. #include "emac.h"
  25. #include "emac-mac.h"
  26. #include "emac-phy.h"
  27. #include "emac-sgmii.h"
  28. #define EMAC_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
  29. NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP)
  30. #define EMAC_RRD_SIZE 4
  31. /* The RRD size if timestamping is enabled: */
  32. #define EMAC_TS_RRD_SIZE 6
  33. #define EMAC_TPD_SIZE 4
  34. #define EMAC_RFD_SIZE 2
  35. #define REG_MAC_RX_STATUS_BIN EMAC_RXMAC_STATC_REG0
  36. #define REG_MAC_RX_STATUS_END EMAC_RXMAC_STATC_REG22
  37. #define REG_MAC_TX_STATUS_BIN EMAC_TXMAC_STATC_REG0
  38. #define REG_MAC_TX_STATUS_END EMAC_TXMAC_STATC_REG24
  39. #define RXQ0_NUM_RFD_PREF_DEF 8
  40. #define TXQ0_NUM_TPD_PREF_DEF 5
  41. #define EMAC_PREAMBLE_DEF 7
  42. #define DMAR_DLY_CNT_DEF 15
  43. #define DMAW_DLY_CNT_DEF 4
  44. #define IMR_NORMAL_MASK (\
  45. ISR_ERROR |\
  46. ISR_GPHY_LINK |\
  47. ISR_TX_PKT |\
  48. GPHY_WAKEUP_INT)
  49. #define IMR_EXTENDED_MASK (\
  50. SW_MAN_INT |\
  51. ISR_OVER |\
  52. ISR_ERROR |\
  53. ISR_GPHY_LINK |\
  54. ISR_TX_PKT |\
  55. GPHY_WAKEUP_INT)
  56. #define ISR_TX_PKT (\
  57. TX_PKT_INT |\
  58. TX_PKT_INT1 |\
  59. TX_PKT_INT2 |\
  60. TX_PKT_INT3)
  61. #define ISR_GPHY_LINK (\
  62. GPHY_LINK_UP_INT |\
  63. GPHY_LINK_DOWN_INT)
  64. #define ISR_OVER (\
  65. RFD0_UR_INT |\
  66. RFD1_UR_INT |\
  67. RFD2_UR_INT |\
  68. RFD3_UR_INT |\
  69. RFD4_UR_INT |\
  70. RXF_OF_INT |\
  71. TXF_UR_INT)
  72. #define ISR_ERROR (\
  73. DMAR_TO_INT |\
  74. DMAW_TO_INT |\
  75. TXQ_TO_INT)
  76. /* in sync with enum emac_clk_id */
  77. static const char * const emac_clk_name[] = {
  78. "axi_clk", "cfg_ahb_clk", "high_speed_clk", "mdio_clk", "tx_clk",
  79. "rx_clk", "sys_clk"
  80. };
  81. void emac_reg_update32(void __iomem *addr, u32 mask, u32 val)
  82. {
  83. u32 data = readl(addr);
  84. writel(((data & ~mask) | val), addr);
  85. }
  86. /* reinitialize */
  87. int emac_reinit_locked(struct emac_adapter *adpt)
  88. {
  89. int ret;
  90. mutex_lock(&adpt->reset_lock);
  91. emac_mac_down(adpt);
  92. emac_sgmii_reset(adpt);
  93. ret = emac_mac_up(adpt);
  94. mutex_unlock(&adpt->reset_lock);
  95. return ret;
  96. }
  97. /* NAPI */
  98. static int emac_napi_rtx(struct napi_struct *napi, int budget)
  99. {
  100. struct emac_rx_queue *rx_q =
  101. container_of(napi, struct emac_rx_queue, napi);
  102. struct emac_adapter *adpt = netdev_priv(rx_q->netdev);
  103. struct emac_irq *irq = rx_q->irq;
  104. int work_done = 0;
  105. emac_mac_rx_process(adpt, rx_q, &work_done, budget);
  106. if (work_done < budget) {
  107. napi_complete_done(napi, work_done);
  108. irq->mask |= rx_q->intr;
  109. writel(irq->mask, adpt->base + EMAC_INT_MASK);
  110. }
  111. return work_done;
  112. }
  113. /* Transmit the packet */
  114. static int emac_start_xmit(struct sk_buff *skb, struct net_device *netdev)
  115. {
  116. struct emac_adapter *adpt = netdev_priv(netdev);
  117. return emac_mac_tx_buf_send(adpt, &adpt->tx_q, skb);
  118. }
  119. irqreturn_t emac_isr(int _irq, void *data)
  120. {
  121. struct emac_irq *irq = data;
  122. struct emac_adapter *adpt =
  123. container_of(irq, struct emac_adapter, irq);
  124. struct emac_rx_queue *rx_q = &adpt->rx_q;
  125. u32 isr, status;
  126. /* disable the interrupt */
  127. writel(0, adpt->base + EMAC_INT_MASK);
  128. isr = readl_relaxed(adpt->base + EMAC_INT_STATUS);
  129. status = isr & irq->mask;
  130. if (status == 0)
  131. goto exit;
  132. if (status & ISR_ERROR) {
  133. netif_warn(adpt, intr, adpt->netdev,
  134. "warning: error irq status 0x%lx\n",
  135. status & ISR_ERROR);
  136. /* reset MAC */
  137. schedule_work(&adpt->work_thread);
  138. }
  139. /* Schedule the napi for receive queue with interrupt
  140. * status bit set
  141. */
  142. if (status & rx_q->intr) {
  143. if (napi_schedule_prep(&rx_q->napi)) {
  144. irq->mask &= ~rx_q->intr;
  145. __napi_schedule(&rx_q->napi);
  146. }
  147. }
  148. if (status & TX_PKT_INT)
  149. emac_mac_tx_process(adpt, &adpt->tx_q);
  150. if (status & ISR_OVER)
  151. net_warn_ratelimited("warning: TX/RX overflow\n");
  152. /* link event */
  153. if (status & ISR_GPHY_LINK)
  154. phy_mac_interrupt(adpt->phydev, !!(status & GPHY_LINK_UP_INT));
  155. exit:
  156. /* enable the interrupt */
  157. writel(irq->mask, adpt->base + EMAC_INT_MASK);
  158. return IRQ_HANDLED;
  159. }
  160. /* Configure VLAN tag strip/insert feature */
  161. static int emac_set_features(struct net_device *netdev,
  162. netdev_features_t features)
  163. {
  164. netdev_features_t changed = features ^ netdev->features;
  165. struct emac_adapter *adpt = netdev_priv(netdev);
  166. /* We only need to reprogram the hardware if the VLAN tag features
  167. * have changed, and if it's already running.
  168. */
  169. if (!(changed & (NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX)))
  170. return 0;
  171. if (!netif_running(netdev))
  172. return 0;
  173. /* emac_mac_mode_config() uses netdev->features to configure the EMAC,
  174. * so make sure it's set first.
  175. */
  176. netdev->features = features;
  177. return emac_reinit_locked(adpt);
  178. }
  179. /* Configure Multicast and Promiscuous modes */
  180. static void emac_rx_mode_set(struct net_device *netdev)
  181. {
  182. struct emac_adapter *adpt = netdev_priv(netdev);
  183. struct netdev_hw_addr *ha;
  184. emac_mac_mode_config(adpt);
  185. /* update multicast address filtering */
  186. emac_mac_multicast_addr_clear(adpt);
  187. netdev_for_each_mc_addr(ha, netdev)
  188. emac_mac_multicast_addr_set(adpt, ha->addr);
  189. }
  190. /* Change the Maximum Transfer Unit (MTU) */
  191. static int emac_change_mtu(struct net_device *netdev, int new_mtu)
  192. {
  193. struct emac_adapter *adpt = netdev_priv(netdev);
  194. netif_info(adpt, hw, adpt->netdev,
  195. "changing MTU from %d to %d\n", netdev->mtu,
  196. new_mtu);
  197. netdev->mtu = new_mtu;
  198. if (netif_running(netdev))
  199. return emac_reinit_locked(adpt);
  200. return 0;
  201. }
  202. /* Called when the network interface is made active */
  203. static int emac_open(struct net_device *netdev)
  204. {
  205. struct emac_adapter *adpt = netdev_priv(netdev);
  206. struct emac_irq *irq = &adpt->irq;
  207. int ret;
  208. ret = request_irq(irq->irq, emac_isr, 0, "emac-core0", irq);
  209. if (ret) {
  210. netdev_err(adpt->netdev, "could not request emac-core0 irq\n");
  211. return ret;
  212. }
  213. /* allocate rx/tx dma buffer & descriptors */
  214. ret = emac_mac_rx_tx_rings_alloc_all(adpt);
  215. if (ret) {
  216. netdev_err(adpt->netdev, "error allocating rx/tx rings\n");
  217. free_irq(irq->irq, irq);
  218. return ret;
  219. }
  220. ret = emac_mac_up(adpt);
  221. if (ret) {
  222. emac_mac_rx_tx_rings_free_all(adpt);
  223. free_irq(irq->irq, irq);
  224. return ret;
  225. }
  226. ret = adpt->phy.open(adpt);
  227. if (ret) {
  228. emac_mac_down(adpt);
  229. emac_mac_rx_tx_rings_free_all(adpt);
  230. free_irq(irq->irq, irq);
  231. return ret;
  232. }
  233. return 0;
  234. }
  235. /* Called when the network interface is disabled */
  236. static int emac_close(struct net_device *netdev)
  237. {
  238. struct emac_adapter *adpt = netdev_priv(netdev);
  239. mutex_lock(&adpt->reset_lock);
  240. adpt->phy.close(adpt);
  241. emac_mac_down(adpt);
  242. emac_mac_rx_tx_rings_free_all(adpt);
  243. free_irq(adpt->irq.irq, &adpt->irq);
  244. mutex_unlock(&adpt->reset_lock);
  245. return 0;
  246. }
  247. /* Respond to a TX hang */
  248. static void emac_tx_timeout(struct net_device *netdev)
  249. {
  250. struct emac_adapter *adpt = netdev_priv(netdev);
  251. schedule_work(&adpt->work_thread);
  252. }
  253. /* IOCTL support for the interface */
  254. static int emac_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  255. {
  256. if (!netif_running(netdev))
  257. return -EINVAL;
  258. if (!netdev->phydev)
  259. return -ENODEV;
  260. return phy_mii_ioctl(netdev->phydev, ifr, cmd);
  261. }
  262. /**
  263. * emac_update_hw_stats - read the EMAC stat registers
  264. *
  265. * Reads the stats registers and write the values to adpt->stats.
  266. *
  267. * adpt->stats.lock must be held while calling this function,
  268. * and while reading from adpt->stats.
  269. */
  270. void emac_update_hw_stats(struct emac_adapter *adpt)
  271. {
  272. struct emac_stats *stats = &adpt->stats;
  273. u64 *stats_itr = &adpt->stats.rx_ok;
  274. void __iomem *base = adpt->base;
  275. unsigned int addr;
  276. addr = REG_MAC_RX_STATUS_BIN;
  277. while (addr <= REG_MAC_RX_STATUS_END) {
  278. *stats_itr += readl_relaxed(base + addr);
  279. stats_itr++;
  280. addr += sizeof(u32);
  281. }
  282. /* additional rx status */
  283. stats->rx_crc_align += readl_relaxed(base + EMAC_RXMAC_STATC_REG23);
  284. stats->rx_jabbers += readl_relaxed(base + EMAC_RXMAC_STATC_REG24);
  285. /* update tx status */
  286. addr = REG_MAC_TX_STATUS_BIN;
  287. stats_itr = &stats->tx_ok;
  288. while (addr <= REG_MAC_TX_STATUS_END) {
  289. *stats_itr += readl_relaxed(base + addr);
  290. stats_itr++;
  291. addr += sizeof(u32);
  292. }
  293. /* additional tx status */
  294. stats->tx_col += readl_relaxed(base + EMAC_TXMAC_STATC_REG25);
  295. }
  296. /* Provide network statistics info for the interface */
  297. static void emac_get_stats64(struct net_device *netdev,
  298. struct rtnl_link_stats64 *net_stats)
  299. {
  300. struct emac_adapter *adpt = netdev_priv(netdev);
  301. struct emac_stats *stats = &adpt->stats;
  302. spin_lock(&stats->lock);
  303. emac_update_hw_stats(adpt);
  304. /* return parsed statistics */
  305. net_stats->rx_packets = stats->rx_ok;
  306. net_stats->tx_packets = stats->tx_ok;
  307. net_stats->rx_bytes = stats->rx_byte_cnt;
  308. net_stats->tx_bytes = stats->tx_byte_cnt;
  309. net_stats->multicast = stats->rx_mcast;
  310. net_stats->collisions = stats->tx_1_col + stats->tx_2_col * 2 +
  311. stats->tx_late_col + stats->tx_abort_col;
  312. net_stats->rx_errors = stats->rx_frag + stats->rx_fcs_err +
  313. stats->rx_len_err + stats->rx_sz_ov +
  314. stats->rx_align_err;
  315. net_stats->rx_fifo_errors = stats->rx_rxf_ov;
  316. net_stats->rx_length_errors = stats->rx_len_err;
  317. net_stats->rx_crc_errors = stats->rx_fcs_err;
  318. net_stats->rx_frame_errors = stats->rx_align_err;
  319. net_stats->rx_over_errors = stats->rx_rxf_ov;
  320. net_stats->rx_missed_errors = stats->rx_rxf_ov;
  321. net_stats->tx_errors = stats->tx_late_col + stats->tx_abort_col +
  322. stats->tx_underrun + stats->tx_trunc;
  323. net_stats->tx_fifo_errors = stats->tx_underrun;
  324. net_stats->tx_aborted_errors = stats->tx_abort_col;
  325. net_stats->tx_window_errors = stats->tx_late_col;
  326. spin_unlock(&stats->lock);
  327. }
  328. static const struct net_device_ops emac_netdev_ops = {
  329. .ndo_open = emac_open,
  330. .ndo_stop = emac_close,
  331. .ndo_validate_addr = eth_validate_addr,
  332. .ndo_start_xmit = emac_start_xmit,
  333. .ndo_set_mac_address = eth_mac_addr,
  334. .ndo_change_mtu = emac_change_mtu,
  335. .ndo_do_ioctl = emac_ioctl,
  336. .ndo_tx_timeout = emac_tx_timeout,
  337. .ndo_get_stats64 = emac_get_stats64,
  338. .ndo_set_features = emac_set_features,
  339. .ndo_set_rx_mode = emac_rx_mode_set,
  340. };
  341. /* Watchdog task routine, called to reinitialize the EMAC */
  342. static void emac_work_thread(struct work_struct *work)
  343. {
  344. struct emac_adapter *adpt =
  345. container_of(work, struct emac_adapter, work_thread);
  346. emac_reinit_locked(adpt);
  347. }
  348. /* Initialize various data structures */
  349. static void emac_init_adapter(struct emac_adapter *adpt)
  350. {
  351. u32 reg;
  352. adpt->rrd_size = EMAC_RRD_SIZE;
  353. adpt->tpd_size = EMAC_TPD_SIZE;
  354. adpt->rfd_size = EMAC_RFD_SIZE;
  355. /* descriptors */
  356. adpt->tx_desc_cnt = EMAC_DEF_TX_DESCS;
  357. adpt->rx_desc_cnt = EMAC_DEF_RX_DESCS;
  358. /* dma */
  359. adpt->dma_order = emac_dma_ord_out;
  360. adpt->dmar_block = emac_dma_req_4096;
  361. adpt->dmaw_block = emac_dma_req_128;
  362. adpt->dmar_dly_cnt = DMAR_DLY_CNT_DEF;
  363. adpt->dmaw_dly_cnt = DMAW_DLY_CNT_DEF;
  364. adpt->tpd_burst = TXQ0_NUM_TPD_PREF_DEF;
  365. adpt->rfd_burst = RXQ0_NUM_RFD_PREF_DEF;
  366. /* irq moderator */
  367. reg = ((EMAC_DEF_RX_IRQ_MOD >> 1) << IRQ_MODERATOR2_INIT_SHFT) |
  368. ((EMAC_DEF_TX_IRQ_MOD >> 1) << IRQ_MODERATOR_INIT_SHFT);
  369. adpt->irq_mod = reg;
  370. /* others */
  371. adpt->preamble = EMAC_PREAMBLE_DEF;
  372. /* default to automatic flow control */
  373. adpt->automatic = true;
  374. }
  375. /* Get the clock */
  376. static int emac_clks_get(struct platform_device *pdev,
  377. struct emac_adapter *adpt)
  378. {
  379. unsigned int i;
  380. for (i = 0; i < EMAC_CLK_CNT; i++) {
  381. struct clk *clk = devm_clk_get(&pdev->dev, emac_clk_name[i]);
  382. if (IS_ERR(clk)) {
  383. dev_err(&pdev->dev,
  384. "could not claim clock %s (error=%li)\n",
  385. emac_clk_name[i], PTR_ERR(clk));
  386. return PTR_ERR(clk);
  387. }
  388. adpt->clk[i] = clk;
  389. }
  390. return 0;
  391. }
  392. /* Initialize clocks */
  393. static int emac_clks_phase1_init(struct platform_device *pdev,
  394. struct emac_adapter *adpt)
  395. {
  396. int ret;
  397. /* On ACPI platforms, clocks are controlled by firmware and/or
  398. * ACPI, not by drivers.
  399. */
  400. if (has_acpi_companion(&pdev->dev))
  401. return 0;
  402. ret = emac_clks_get(pdev, adpt);
  403. if (ret)
  404. return ret;
  405. ret = clk_prepare_enable(adpt->clk[EMAC_CLK_AXI]);
  406. if (ret)
  407. return ret;
  408. ret = clk_prepare_enable(adpt->clk[EMAC_CLK_CFG_AHB]);
  409. if (ret)
  410. return ret;
  411. ret = clk_set_rate(adpt->clk[EMAC_CLK_HIGH_SPEED], 19200000);
  412. if (ret)
  413. return ret;
  414. return clk_prepare_enable(adpt->clk[EMAC_CLK_HIGH_SPEED]);
  415. }
  416. /* Enable clocks; needs emac_clks_phase1_init to be called before */
  417. static int emac_clks_phase2_init(struct platform_device *pdev,
  418. struct emac_adapter *adpt)
  419. {
  420. int ret;
  421. if (has_acpi_companion(&pdev->dev))
  422. return 0;
  423. ret = clk_set_rate(adpt->clk[EMAC_CLK_TX], 125000000);
  424. if (ret)
  425. return ret;
  426. ret = clk_prepare_enable(adpt->clk[EMAC_CLK_TX]);
  427. if (ret)
  428. return ret;
  429. ret = clk_set_rate(adpt->clk[EMAC_CLK_HIGH_SPEED], 125000000);
  430. if (ret)
  431. return ret;
  432. ret = clk_set_rate(adpt->clk[EMAC_CLK_MDIO], 25000000);
  433. if (ret)
  434. return ret;
  435. ret = clk_prepare_enable(adpt->clk[EMAC_CLK_MDIO]);
  436. if (ret)
  437. return ret;
  438. ret = clk_prepare_enable(adpt->clk[EMAC_CLK_RX]);
  439. if (ret)
  440. return ret;
  441. return clk_prepare_enable(adpt->clk[EMAC_CLK_SYS]);
  442. }
  443. static void emac_clks_teardown(struct emac_adapter *adpt)
  444. {
  445. unsigned int i;
  446. for (i = 0; i < EMAC_CLK_CNT; i++)
  447. clk_disable_unprepare(adpt->clk[i]);
  448. }
  449. /* Get the resources */
  450. static int emac_probe_resources(struct platform_device *pdev,
  451. struct emac_adapter *adpt)
  452. {
  453. struct net_device *netdev = adpt->netdev;
  454. struct resource *res;
  455. char maddr[ETH_ALEN];
  456. int ret = 0;
  457. /* get mac address */
  458. if (device_get_mac_address(&pdev->dev, maddr, ETH_ALEN))
  459. ether_addr_copy(netdev->dev_addr, maddr);
  460. else
  461. eth_hw_addr_random(netdev);
  462. /* Core 0 interrupt */
  463. ret = platform_get_irq(pdev, 0);
  464. if (ret < 0) {
  465. dev_err(&pdev->dev,
  466. "error: missing core0 irq resource (error=%i)\n", ret);
  467. return ret;
  468. }
  469. adpt->irq.irq = ret;
  470. /* base register address */
  471. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  472. adpt->base = devm_ioremap_resource(&pdev->dev, res);
  473. if (IS_ERR(adpt->base))
  474. return PTR_ERR(adpt->base);
  475. /* CSR register address */
  476. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  477. adpt->csr = devm_ioremap_resource(&pdev->dev, res);
  478. if (IS_ERR(adpt->csr))
  479. return PTR_ERR(adpt->csr);
  480. netdev->base_addr = (unsigned long)adpt->base;
  481. return 0;
  482. }
  483. static const struct of_device_id emac_dt_match[] = {
  484. {
  485. .compatible = "qcom,fsm9900-emac",
  486. },
  487. {}
  488. };
  489. MODULE_DEVICE_TABLE(of, emac_dt_match);
  490. #if IS_ENABLED(CONFIG_ACPI)
  491. static const struct acpi_device_id emac_acpi_match[] = {
  492. {
  493. .id = "QCOM8070",
  494. },
  495. {}
  496. };
  497. MODULE_DEVICE_TABLE(acpi, emac_acpi_match);
  498. #endif
  499. static int emac_probe(struct platform_device *pdev)
  500. {
  501. struct net_device *netdev;
  502. struct emac_adapter *adpt;
  503. struct emac_sgmii *phy;
  504. u16 devid, revid;
  505. u32 reg;
  506. int ret;
  507. /* The EMAC itself is capable of 64-bit DMA, so try that first. */
  508. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  509. if (ret) {
  510. /* Some platforms may restrict the EMAC's address bus to less
  511. * then the size of DDR. In this case, we need to try a
  512. * smaller mask. We could try every possible smaller mask,
  513. * but that's overkill. Instead, just fall to 32-bit, which
  514. * should always work.
  515. */
  516. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  517. if (ret) {
  518. dev_err(&pdev->dev, "could not set DMA mask\n");
  519. return ret;
  520. }
  521. }
  522. netdev = alloc_etherdev(sizeof(struct emac_adapter));
  523. if (!netdev)
  524. return -ENOMEM;
  525. dev_set_drvdata(&pdev->dev, netdev);
  526. SET_NETDEV_DEV(netdev, &pdev->dev);
  527. emac_set_ethtool_ops(netdev);
  528. adpt = netdev_priv(netdev);
  529. adpt->netdev = netdev;
  530. adpt->msg_enable = EMAC_MSG_DEFAULT;
  531. phy = &adpt->phy;
  532. atomic_set(&phy->decode_error_count, 0);
  533. mutex_init(&adpt->reset_lock);
  534. spin_lock_init(&adpt->stats.lock);
  535. adpt->irq.mask = RX_PKT_INT0 | IMR_NORMAL_MASK;
  536. ret = emac_probe_resources(pdev, adpt);
  537. if (ret)
  538. goto err_undo_netdev;
  539. /* initialize clocks */
  540. ret = emac_clks_phase1_init(pdev, adpt);
  541. if (ret) {
  542. dev_err(&pdev->dev, "could not initialize clocks\n");
  543. goto err_undo_netdev;
  544. }
  545. netdev->watchdog_timeo = EMAC_WATCHDOG_TIME;
  546. netdev->irq = adpt->irq.irq;
  547. netdev->netdev_ops = &emac_netdev_ops;
  548. emac_init_adapter(adpt);
  549. /* init external phy */
  550. ret = emac_phy_config(pdev, adpt);
  551. if (ret)
  552. goto err_undo_clocks;
  553. /* init internal sgmii phy */
  554. ret = emac_sgmii_config(pdev, adpt);
  555. if (ret)
  556. goto err_undo_mdiobus;
  557. /* enable clocks */
  558. ret = emac_clks_phase2_init(pdev, adpt);
  559. if (ret) {
  560. dev_err(&pdev->dev, "could not initialize clocks\n");
  561. goto err_undo_mdiobus;
  562. }
  563. emac_mac_reset(adpt);
  564. /* set hw features */
  565. netdev->features = NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
  566. NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
  567. NETIF_F_HW_VLAN_CTAG_TX;
  568. netdev->hw_features = netdev->features;
  569. netdev->vlan_features |= NETIF_F_SG | NETIF_F_HW_CSUM |
  570. NETIF_F_TSO | NETIF_F_TSO6;
  571. /* MTU range: 46 - 9194 */
  572. netdev->min_mtu = EMAC_MIN_ETH_FRAME_SIZE -
  573. (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  574. netdev->max_mtu = EMAC_MAX_ETH_FRAME_SIZE -
  575. (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  576. INIT_WORK(&adpt->work_thread, emac_work_thread);
  577. /* Initialize queues */
  578. emac_mac_rx_tx_ring_init_all(pdev, adpt);
  579. netif_napi_add(netdev, &adpt->rx_q.napi, emac_napi_rtx,
  580. NAPI_POLL_WEIGHT);
  581. ret = register_netdev(netdev);
  582. if (ret) {
  583. dev_err(&pdev->dev, "could not register net device\n");
  584. goto err_undo_napi;
  585. }
  586. reg = readl_relaxed(adpt->base + EMAC_DMA_MAS_CTRL);
  587. devid = (reg & DEV_ID_NUM_BMSK) >> DEV_ID_NUM_SHFT;
  588. revid = (reg & DEV_REV_NUM_BMSK) >> DEV_REV_NUM_SHFT;
  589. reg = readl_relaxed(adpt->base + EMAC_CORE_HW_VERSION);
  590. netif_info(adpt, probe, netdev,
  591. "hardware id %d.%d, hardware version %d.%d.%d\n",
  592. devid, revid,
  593. (reg & MAJOR_BMSK) >> MAJOR_SHFT,
  594. (reg & MINOR_BMSK) >> MINOR_SHFT,
  595. (reg & STEP_BMSK) >> STEP_SHFT);
  596. return 0;
  597. err_undo_napi:
  598. netif_napi_del(&adpt->rx_q.napi);
  599. err_undo_mdiobus:
  600. put_device(&adpt->phydev->mdio.dev);
  601. mdiobus_unregister(adpt->mii_bus);
  602. err_undo_clocks:
  603. emac_clks_teardown(adpt);
  604. err_undo_netdev:
  605. free_netdev(netdev);
  606. return ret;
  607. }
  608. static int emac_remove(struct platform_device *pdev)
  609. {
  610. struct net_device *netdev = dev_get_drvdata(&pdev->dev);
  611. struct emac_adapter *adpt = netdev_priv(netdev);
  612. unregister_netdev(netdev);
  613. netif_napi_del(&adpt->rx_q.napi);
  614. emac_clks_teardown(adpt);
  615. put_device(&adpt->phydev->mdio.dev);
  616. mdiobus_unregister(adpt->mii_bus);
  617. free_netdev(netdev);
  618. if (adpt->phy.digital)
  619. iounmap(adpt->phy.digital);
  620. iounmap(adpt->phy.base);
  621. return 0;
  622. }
  623. static struct platform_driver emac_platform_driver = {
  624. .probe = emac_probe,
  625. .remove = emac_remove,
  626. .driver = {
  627. .name = "qcom-emac",
  628. .of_match_table = emac_dt_match,
  629. .acpi_match_table = ACPI_PTR(emac_acpi_match),
  630. },
  631. };
  632. module_platform_driver(emac_platform_driver);
  633. MODULE_LICENSE("GPL v2");
  634. MODULE_ALIAS("platform:qcom-emac");