emac.c 18 KB

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