emac.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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(napi);
  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. int ret;
  207. /* allocate rx/tx dma buffer & descriptors */
  208. ret = emac_mac_rx_tx_rings_alloc_all(adpt);
  209. if (ret) {
  210. netdev_err(adpt->netdev, "error allocating rx/tx rings\n");
  211. return ret;
  212. }
  213. ret = emac_mac_up(adpt);
  214. if (ret) {
  215. emac_mac_rx_tx_rings_free_all(adpt);
  216. return ret;
  217. }
  218. emac_mac_start(adpt);
  219. return 0;
  220. }
  221. /* Called when the network interface is disabled */
  222. static int emac_close(struct net_device *netdev)
  223. {
  224. struct emac_adapter *adpt = netdev_priv(netdev);
  225. mutex_lock(&adpt->reset_lock);
  226. emac_mac_down(adpt);
  227. emac_mac_rx_tx_rings_free_all(adpt);
  228. mutex_unlock(&adpt->reset_lock);
  229. return 0;
  230. }
  231. /* Respond to a TX hang */
  232. static void emac_tx_timeout(struct net_device *netdev)
  233. {
  234. struct emac_adapter *adpt = netdev_priv(netdev);
  235. schedule_work(&adpt->work_thread);
  236. }
  237. /* IOCTL support for the interface */
  238. static int emac_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  239. {
  240. if (!netif_running(netdev))
  241. return -EINVAL;
  242. if (!netdev->phydev)
  243. return -ENODEV;
  244. return phy_mii_ioctl(netdev->phydev, ifr, cmd);
  245. }
  246. /* Provide network statistics info for the interface */
  247. static struct rtnl_link_stats64 *emac_get_stats64(struct net_device *netdev,
  248. struct rtnl_link_stats64 *net_stats)
  249. {
  250. struct emac_adapter *adpt = netdev_priv(netdev);
  251. unsigned int addr = REG_MAC_RX_STATUS_BIN;
  252. struct emac_stats *stats = &adpt->stats;
  253. u64 *stats_itr = &adpt->stats.rx_ok;
  254. u32 val;
  255. spin_lock(&stats->lock);
  256. while (addr <= REG_MAC_RX_STATUS_END) {
  257. val = readl_relaxed(adpt->base + addr);
  258. *stats_itr += val;
  259. stats_itr++;
  260. addr += sizeof(u32);
  261. }
  262. /* additional rx status */
  263. val = readl_relaxed(adpt->base + EMAC_RXMAC_STATC_REG23);
  264. adpt->stats.rx_crc_align += val;
  265. val = readl_relaxed(adpt->base + EMAC_RXMAC_STATC_REG24);
  266. adpt->stats.rx_jabbers += val;
  267. /* update tx status */
  268. addr = REG_MAC_TX_STATUS_BIN;
  269. stats_itr = &adpt->stats.tx_ok;
  270. while (addr <= REG_MAC_TX_STATUS_END) {
  271. val = readl_relaxed(adpt->base + addr);
  272. *stats_itr += val;
  273. ++stats_itr;
  274. addr += sizeof(u32);
  275. }
  276. /* additional tx status */
  277. val = readl_relaxed(adpt->base + EMAC_TXMAC_STATC_REG25);
  278. adpt->stats.tx_col += val;
  279. /* return parsed statistics */
  280. net_stats->rx_packets = stats->rx_ok;
  281. net_stats->tx_packets = stats->tx_ok;
  282. net_stats->rx_bytes = stats->rx_byte_cnt;
  283. net_stats->tx_bytes = stats->tx_byte_cnt;
  284. net_stats->multicast = stats->rx_mcast;
  285. net_stats->collisions = stats->tx_1_col + stats->tx_2_col * 2 +
  286. stats->tx_late_col + stats->tx_abort_col;
  287. net_stats->rx_errors = stats->rx_frag + stats->rx_fcs_err +
  288. stats->rx_len_err + stats->rx_sz_ov +
  289. stats->rx_align_err;
  290. net_stats->rx_fifo_errors = stats->rx_rxf_ov;
  291. net_stats->rx_length_errors = stats->rx_len_err;
  292. net_stats->rx_crc_errors = stats->rx_fcs_err;
  293. net_stats->rx_frame_errors = stats->rx_align_err;
  294. net_stats->rx_over_errors = stats->rx_rxf_ov;
  295. net_stats->rx_missed_errors = stats->rx_rxf_ov;
  296. net_stats->tx_errors = stats->tx_late_col + stats->tx_abort_col +
  297. stats->tx_underrun + stats->tx_trunc;
  298. net_stats->tx_fifo_errors = stats->tx_underrun;
  299. net_stats->tx_aborted_errors = stats->tx_abort_col;
  300. net_stats->tx_window_errors = stats->tx_late_col;
  301. spin_unlock(&stats->lock);
  302. return net_stats;
  303. }
  304. static const struct net_device_ops emac_netdev_ops = {
  305. .ndo_open = emac_open,
  306. .ndo_stop = emac_close,
  307. .ndo_validate_addr = eth_validate_addr,
  308. .ndo_start_xmit = emac_start_xmit,
  309. .ndo_set_mac_address = eth_mac_addr,
  310. .ndo_change_mtu = emac_change_mtu,
  311. .ndo_do_ioctl = emac_ioctl,
  312. .ndo_tx_timeout = emac_tx_timeout,
  313. .ndo_get_stats64 = emac_get_stats64,
  314. .ndo_set_features = emac_set_features,
  315. .ndo_set_rx_mode = emac_rx_mode_set,
  316. };
  317. /* Watchdog task routine, called to reinitialize the EMAC */
  318. static void emac_work_thread(struct work_struct *work)
  319. {
  320. struct emac_adapter *adpt =
  321. container_of(work, struct emac_adapter, work_thread);
  322. emac_reinit_locked(adpt);
  323. }
  324. /* Initialize various data structures */
  325. static void emac_init_adapter(struct emac_adapter *adpt)
  326. {
  327. u32 reg;
  328. /* descriptors */
  329. adpt->tx_desc_cnt = EMAC_DEF_TX_DESCS;
  330. adpt->rx_desc_cnt = EMAC_DEF_RX_DESCS;
  331. /* dma */
  332. adpt->dma_order = emac_dma_ord_out;
  333. adpt->dmar_block = emac_dma_req_4096;
  334. adpt->dmaw_block = emac_dma_req_128;
  335. adpt->dmar_dly_cnt = DMAR_DLY_CNT_DEF;
  336. adpt->dmaw_dly_cnt = DMAW_DLY_CNT_DEF;
  337. adpt->tpd_burst = TXQ0_NUM_TPD_PREF_DEF;
  338. adpt->rfd_burst = RXQ0_NUM_RFD_PREF_DEF;
  339. /* irq moderator */
  340. reg = ((EMAC_DEF_RX_IRQ_MOD >> 1) << IRQ_MODERATOR2_INIT_SHFT) |
  341. ((EMAC_DEF_TX_IRQ_MOD >> 1) << IRQ_MODERATOR_INIT_SHFT);
  342. adpt->irq_mod = reg;
  343. /* others */
  344. adpt->preamble = EMAC_PREAMBLE_DEF;
  345. }
  346. /* Get the clock */
  347. static int emac_clks_get(struct platform_device *pdev,
  348. struct emac_adapter *adpt)
  349. {
  350. unsigned int i;
  351. for (i = 0; i < EMAC_CLK_CNT; i++) {
  352. struct clk *clk = devm_clk_get(&pdev->dev, emac_clk_name[i]);
  353. if (IS_ERR(clk)) {
  354. dev_err(&pdev->dev,
  355. "could not claim clock %s (error=%li)\n",
  356. emac_clk_name[i], PTR_ERR(clk));
  357. return PTR_ERR(clk);
  358. }
  359. adpt->clk[i] = clk;
  360. }
  361. return 0;
  362. }
  363. /* Initialize clocks */
  364. static int emac_clks_phase1_init(struct platform_device *pdev,
  365. struct emac_adapter *adpt)
  366. {
  367. int ret;
  368. /* On ACPI platforms, clocks are controlled by firmware and/or
  369. * ACPI, not by drivers.
  370. */
  371. if (has_acpi_companion(&pdev->dev))
  372. return 0;
  373. ret = emac_clks_get(pdev, adpt);
  374. if (ret)
  375. return ret;
  376. ret = clk_prepare_enable(adpt->clk[EMAC_CLK_AXI]);
  377. if (ret)
  378. return ret;
  379. ret = clk_prepare_enable(adpt->clk[EMAC_CLK_CFG_AHB]);
  380. if (ret)
  381. return ret;
  382. ret = clk_set_rate(adpt->clk[EMAC_CLK_HIGH_SPEED], 19200000);
  383. if (ret)
  384. return ret;
  385. return clk_prepare_enable(adpt->clk[EMAC_CLK_HIGH_SPEED]);
  386. }
  387. /* Enable clocks; needs emac_clks_phase1_init to be called before */
  388. static int emac_clks_phase2_init(struct platform_device *pdev,
  389. struct emac_adapter *adpt)
  390. {
  391. int ret;
  392. if (has_acpi_companion(&pdev->dev))
  393. return 0;
  394. ret = clk_set_rate(adpt->clk[EMAC_CLK_TX], 125000000);
  395. if (ret)
  396. return ret;
  397. ret = clk_prepare_enable(adpt->clk[EMAC_CLK_TX]);
  398. if (ret)
  399. return ret;
  400. ret = clk_set_rate(adpt->clk[EMAC_CLK_HIGH_SPEED], 125000000);
  401. if (ret)
  402. return ret;
  403. ret = clk_set_rate(adpt->clk[EMAC_CLK_MDIO], 25000000);
  404. if (ret)
  405. return ret;
  406. ret = clk_prepare_enable(adpt->clk[EMAC_CLK_MDIO]);
  407. if (ret)
  408. return ret;
  409. ret = clk_prepare_enable(adpt->clk[EMAC_CLK_RX]);
  410. if (ret)
  411. return ret;
  412. return clk_prepare_enable(adpt->clk[EMAC_CLK_SYS]);
  413. }
  414. static void emac_clks_teardown(struct emac_adapter *adpt)
  415. {
  416. unsigned int i;
  417. for (i = 0; i < EMAC_CLK_CNT; i++)
  418. clk_disable_unprepare(adpt->clk[i]);
  419. }
  420. /* Get the resources */
  421. static int emac_probe_resources(struct platform_device *pdev,
  422. struct emac_adapter *adpt)
  423. {
  424. struct net_device *netdev = adpt->netdev;
  425. struct resource *res;
  426. char maddr[ETH_ALEN];
  427. int ret = 0;
  428. /* get mac address */
  429. if (device_get_mac_address(&pdev->dev, maddr, ETH_ALEN))
  430. ether_addr_copy(netdev->dev_addr, maddr);
  431. else
  432. eth_hw_addr_random(netdev);
  433. /* Core 0 interrupt */
  434. ret = platform_get_irq(pdev, 0);
  435. if (ret < 0) {
  436. dev_err(&pdev->dev,
  437. "error: missing core0 irq resource (error=%i)\n", ret);
  438. return ret;
  439. }
  440. adpt->irq.irq = ret;
  441. /* base register address */
  442. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  443. adpt->base = devm_ioremap_resource(&pdev->dev, res);
  444. if (IS_ERR(adpt->base))
  445. return PTR_ERR(adpt->base);
  446. /* CSR register address */
  447. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  448. adpt->csr = devm_ioremap_resource(&pdev->dev, res);
  449. if (IS_ERR(adpt->csr))
  450. return PTR_ERR(adpt->csr);
  451. netdev->base_addr = (unsigned long)adpt->base;
  452. return 0;
  453. }
  454. static const struct of_device_id emac_dt_match[] = {
  455. {
  456. .compatible = "qcom,fsm9900-emac",
  457. },
  458. {}
  459. };
  460. MODULE_DEVICE_TABLE(of, emac_dt_match);
  461. #if IS_ENABLED(CONFIG_ACPI)
  462. static const struct acpi_device_id emac_acpi_match[] = {
  463. {
  464. .id = "QCOM8070",
  465. },
  466. {}
  467. };
  468. MODULE_DEVICE_TABLE(acpi, emac_acpi_match);
  469. #endif
  470. static int emac_probe(struct platform_device *pdev)
  471. {
  472. struct net_device *netdev;
  473. struct emac_adapter *adpt;
  474. struct emac_phy *phy;
  475. u16 devid, revid;
  476. u32 reg;
  477. int ret;
  478. /* The EMAC itself is capable of 64-bit DMA, so try that first. */
  479. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  480. if (ret) {
  481. /* Some platforms may restrict the EMAC's address bus to less
  482. * then the size of DDR. In this case, we need to try a
  483. * smaller mask. We could try every possible smaller mask,
  484. * but that's overkill. Instead, just fall to 32-bit, which
  485. * should always work.
  486. */
  487. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  488. if (ret) {
  489. dev_err(&pdev->dev, "could not set DMA mask\n");
  490. return ret;
  491. }
  492. }
  493. netdev = alloc_etherdev(sizeof(struct emac_adapter));
  494. if (!netdev)
  495. return -ENOMEM;
  496. dev_set_drvdata(&pdev->dev, netdev);
  497. SET_NETDEV_DEV(netdev, &pdev->dev);
  498. adpt = netdev_priv(netdev);
  499. adpt->netdev = netdev;
  500. adpt->msg_enable = EMAC_MSG_DEFAULT;
  501. phy = &adpt->phy;
  502. mutex_init(&adpt->reset_lock);
  503. spin_lock_init(&adpt->stats.lock);
  504. adpt->irq.mask = RX_PKT_INT0 | IMR_NORMAL_MASK;
  505. ret = emac_probe_resources(pdev, adpt);
  506. if (ret)
  507. goto err_undo_netdev;
  508. /* initialize clocks */
  509. ret = emac_clks_phase1_init(pdev, adpt);
  510. if (ret) {
  511. dev_err(&pdev->dev, "could not initialize clocks\n");
  512. goto err_undo_netdev;
  513. }
  514. netdev->watchdog_timeo = EMAC_WATCHDOG_TIME;
  515. netdev->irq = adpt->irq.irq;
  516. adpt->rrd_size = EMAC_RRD_SIZE;
  517. adpt->tpd_size = EMAC_TPD_SIZE;
  518. adpt->rfd_size = EMAC_RFD_SIZE;
  519. netdev->netdev_ops = &emac_netdev_ops;
  520. emac_init_adapter(adpt);
  521. /* init external phy */
  522. ret = emac_phy_config(pdev, adpt);
  523. if (ret)
  524. goto err_undo_clocks;
  525. /* init internal sgmii phy */
  526. ret = emac_sgmii_config(pdev, adpt);
  527. if (ret)
  528. goto err_undo_mdiobus;
  529. /* enable clocks */
  530. ret = emac_clks_phase2_init(pdev, adpt);
  531. if (ret) {
  532. dev_err(&pdev->dev, "could not initialize clocks\n");
  533. goto err_undo_mdiobus;
  534. }
  535. emac_mac_reset(adpt);
  536. /* set hw features */
  537. netdev->features = NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
  538. NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
  539. NETIF_F_HW_VLAN_CTAG_TX;
  540. netdev->hw_features = netdev->features;
  541. netdev->vlan_features |= NETIF_F_SG | NETIF_F_HW_CSUM |
  542. NETIF_F_TSO | NETIF_F_TSO6;
  543. /* MTU range: 46 - 9194 */
  544. netdev->min_mtu = EMAC_MIN_ETH_FRAME_SIZE -
  545. (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  546. netdev->max_mtu = EMAC_MAX_ETH_FRAME_SIZE -
  547. (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  548. INIT_WORK(&adpt->work_thread, emac_work_thread);
  549. /* Initialize queues */
  550. emac_mac_rx_tx_ring_init_all(pdev, adpt);
  551. netif_napi_add(netdev, &adpt->rx_q.napi, emac_napi_rtx,
  552. NAPI_POLL_WEIGHT);
  553. ret = register_netdev(netdev);
  554. if (ret) {
  555. dev_err(&pdev->dev, "could not register net device\n");
  556. goto err_undo_napi;
  557. }
  558. reg = readl_relaxed(adpt->base + EMAC_DMA_MAS_CTRL);
  559. devid = (reg & DEV_ID_NUM_BMSK) >> DEV_ID_NUM_SHFT;
  560. revid = (reg & DEV_REV_NUM_BMSK) >> DEV_REV_NUM_SHFT;
  561. reg = readl_relaxed(adpt->base + EMAC_CORE_HW_VERSION);
  562. netif_info(adpt, probe, netdev,
  563. "hardware id %d.%d, hardware version %d.%d.%d\n",
  564. devid, revid,
  565. (reg & MAJOR_BMSK) >> MAJOR_SHFT,
  566. (reg & MINOR_BMSK) >> MINOR_SHFT,
  567. (reg & STEP_BMSK) >> STEP_SHFT);
  568. return 0;
  569. err_undo_napi:
  570. netif_napi_del(&adpt->rx_q.napi);
  571. err_undo_mdiobus:
  572. put_device(&adpt->phydev->mdio.dev);
  573. mdiobus_unregister(adpt->mii_bus);
  574. err_undo_clocks:
  575. emac_clks_teardown(adpt);
  576. err_undo_netdev:
  577. free_netdev(netdev);
  578. return ret;
  579. }
  580. static int emac_remove(struct platform_device *pdev)
  581. {
  582. struct net_device *netdev = dev_get_drvdata(&pdev->dev);
  583. struct emac_adapter *adpt = netdev_priv(netdev);
  584. unregister_netdev(netdev);
  585. netif_napi_del(&adpt->rx_q.napi);
  586. emac_clks_teardown(adpt);
  587. put_device(&adpt->phydev->mdio.dev);
  588. mdiobus_unregister(adpt->mii_bus);
  589. free_netdev(netdev);
  590. if (adpt->phy.digital)
  591. iounmap(adpt->phy.digital);
  592. iounmap(adpt->phy.base);
  593. return 0;
  594. }
  595. static struct platform_driver emac_platform_driver = {
  596. .probe = emac_probe,
  597. .remove = emac_remove,
  598. .driver = {
  599. .name = "qcom-emac",
  600. .of_match_table = emac_dt_match,
  601. .acpi_match_table = ACPI_PTR(emac_acpi_match),
  602. },
  603. };
  604. module_platform_driver(emac_platform_driver);
  605. MODULE_LICENSE("GPL v2");
  606. MODULE_ALIAS("platform:qcom-emac");