emac.c 18 KB

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