emac_main.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. /*
  2. * Copyright (C) 2004-2013 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Driver for the ARC EMAC 10100 (hardware revision 5)
  9. *
  10. * Contributors:
  11. * Amit Bhor
  12. * Sameer Dhavale
  13. * Vineet Gupta
  14. */
  15. #include <linux/crc32.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/module.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/of_mdio.h>
  23. #include <linux/of_net.h>
  24. #include <linux/of_platform.h>
  25. #include "emac.h"
  26. /**
  27. * arc_emac_tx_avail - Return the number of available slots in the tx ring.
  28. * @priv: Pointer to ARC EMAC private data structure.
  29. *
  30. * returns: the number of slots available for transmission in tx the ring.
  31. */
  32. static inline int arc_emac_tx_avail(struct arc_emac_priv *priv)
  33. {
  34. return (priv->txbd_dirty + TX_BD_NUM - priv->txbd_curr - 1) % TX_BD_NUM;
  35. }
  36. /**
  37. * arc_emac_adjust_link - Adjust the PHY link duplex.
  38. * @ndev: Pointer to the net_device structure.
  39. *
  40. * This function is called to change the duplex setting after auto negotiation
  41. * is done by the PHY.
  42. */
  43. static void arc_emac_adjust_link(struct net_device *ndev)
  44. {
  45. struct arc_emac_priv *priv = netdev_priv(ndev);
  46. struct phy_device *phy_dev = priv->phy_dev;
  47. unsigned int reg, state_changed = 0;
  48. if (priv->link != phy_dev->link) {
  49. priv->link = phy_dev->link;
  50. state_changed = 1;
  51. }
  52. if (priv->speed != phy_dev->speed) {
  53. priv->speed = phy_dev->speed;
  54. state_changed = 1;
  55. if (priv->set_mac_speed)
  56. priv->set_mac_speed(priv, priv->speed);
  57. }
  58. if (priv->duplex != phy_dev->duplex) {
  59. reg = arc_reg_get(priv, R_CTRL);
  60. if (phy_dev->duplex == DUPLEX_FULL)
  61. reg |= ENFL_MASK;
  62. else
  63. reg &= ~ENFL_MASK;
  64. arc_reg_set(priv, R_CTRL, reg);
  65. priv->duplex = phy_dev->duplex;
  66. state_changed = 1;
  67. }
  68. if (state_changed)
  69. phy_print_status(phy_dev);
  70. }
  71. /**
  72. * arc_emac_get_settings - Get PHY settings.
  73. * @ndev: Pointer to net_device structure.
  74. * @cmd: Pointer to ethtool_cmd structure.
  75. *
  76. * This implements ethtool command for getting PHY settings. If PHY could
  77. * not be found, the function returns -ENODEV. This function calls the
  78. * relevant PHY ethtool API to get the PHY settings.
  79. * Issue "ethtool ethX" under linux prompt to execute this function.
  80. */
  81. static int arc_emac_get_settings(struct net_device *ndev,
  82. struct ethtool_cmd *cmd)
  83. {
  84. struct arc_emac_priv *priv = netdev_priv(ndev);
  85. return phy_ethtool_gset(priv->phy_dev, cmd);
  86. }
  87. /**
  88. * arc_emac_set_settings - Set PHY settings as passed in the argument.
  89. * @ndev: Pointer to net_device structure.
  90. * @cmd: Pointer to ethtool_cmd structure.
  91. *
  92. * This implements ethtool command for setting various PHY settings. If PHY
  93. * could not be found, the function returns -ENODEV. This function calls the
  94. * relevant PHY ethtool API to set the PHY.
  95. * Issue e.g. "ethtool -s ethX speed 1000" under linux prompt to execute this
  96. * function.
  97. */
  98. static int arc_emac_set_settings(struct net_device *ndev,
  99. struct ethtool_cmd *cmd)
  100. {
  101. struct arc_emac_priv *priv = netdev_priv(ndev);
  102. if (!capable(CAP_NET_ADMIN))
  103. return -EPERM;
  104. return phy_ethtool_sset(priv->phy_dev, cmd);
  105. }
  106. /**
  107. * arc_emac_get_drvinfo - Get EMAC driver information.
  108. * @ndev: Pointer to net_device structure.
  109. * @info: Pointer to ethtool_drvinfo structure.
  110. *
  111. * This implements ethtool command for getting the driver information.
  112. * Issue "ethtool -i ethX" under linux prompt to execute this function.
  113. */
  114. static void arc_emac_get_drvinfo(struct net_device *ndev,
  115. struct ethtool_drvinfo *info)
  116. {
  117. struct arc_emac_priv *priv = netdev_priv(ndev);
  118. strlcpy(info->driver, priv->drv_name, sizeof(info->driver));
  119. strlcpy(info->version, priv->drv_version, sizeof(info->version));
  120. }
  121. static const struct ethtool_ops arc_emac_ethtool_ops = {
  122. .get_settings = arc_emac_get_settings,
  123. .set_settings = arc_emac_set_settings,
  124. .get_drvinfo = arc_emac_get_drvinfo,
  125. .get_link = ethtool_op_get_link,
  126. };
  127. #define FIRST_OR_LAST_MASK (FIRST_MASK | LAST_MASK)
  128. /**
  129. * arc_emac_tx_clean - clears processed by EMAC Tx BDs.
  130. * @ndev: Pointer to the network device.
  131. */
  132. static void arc_emac_tx_clean(struct net_device *ndev)
  133. {
  134. struct arc_emac_priv *priv = netdev_priv(ndev);
  135. struct net_device_stats *stats = &ndev->stats;
  136. unsigned int i;
  137. for (i = 0; i < TX_BD_NUM; i++) {
  138. unsigned int *txbd_dirty = &priv->txbd_dirty;
  139. struct arc_emac_bd *txbd = &priv->txbd[*txbd_dirty];
  140. struct buffer_state *tx_buff = &priv->tx_buff[*txbd_dirty];
  141. struct sk_buff *skb = tx_buff->skb;
  142. unsigned int info = le32_to_cpu(txbd->info);
  143. if ((info & FOR_EMAC) || !txbd->data || !skb)
  144. break;
  145. if (unlikely(info & (DROP | DEFR | LTCL | UFLO))) {
  146. stats->tx_errors++;
  147. stats->tx_dropped++;
  148. if (info & DEFR)
  149. stats->tx_carrier_errors++;
  150. if (info & LTCL)
  151. stats->collisions++;
  152. if (info & UFLO)
  153. stats->tx_fifo_errors++;
  154. } else if (likely(info & FIRST_OR_LAST_MASK)) {
  155. stats->tx_packets++;
  156. stats->tx_bytes += skb->len;
  157. }
  158. dma_unmap_single(&ndev->dev, dma_unmap_addr(tx_buff, addr),
  159. dma_unmap_len(tx_buff, len), DMA_TO_DEVICE);
  160. /* return the sk_buff to system */
  161. dev_kfree_skb_irq(skb);
  162. txbd->data = 0;
  163. txbd->info = 0;
  164. tx_buff->skb = NULL;
  165. *txbd_dirty = (*txbd_dirty + 1) % TX_BD_NUM;
  166. }
  167. /* Ensure that txbd_dirty is visible to tx() before checking
  168. * for queue stopped.
  169. */
  170. smp_mb();
  171. if (netif_queue_stopped(ndev) && arc_emac_tx_avail(priv))
  172. netif_wake_queue(ndev);
  173. }
  174. /**
  175. * arc_emac_rx - processing of Rx packets.
  176. * @ndev: Pointer to the network device.
  177. * @budget: How many BDs to process on 1 call.
  178. *
  179. * returns: Number of processed BDs
  180. *
  181. * Iterate through Rx BDs and deliver received packages to upper layer.
  182. */
  183. static int arc_emac_rx(struct net_device *ndev, int budget)
  184. {
  185. struct arc_emac_priv *priv = netdev_priv(ndev);
  186. unsigned int work_done;
  187. for (work_done = 0; work_done < budget; work_done++) {
  188. unsigned int *last_rx_bd = &priv->last_rx_bd;
  189. struct net_device_stats *stats = &ndev->stats;
  190. struct buffer_state *rx_buff = &priv->rx_buff[*last_rx_bd];
  191. struct arc_emac_bd *rxbd = &priv->rxbd[*last_rx_bd];
  192. unsigned int pktlen, info = le32_to_cpu(rxbd->info);
  193. struct sk_buff *skb;
  194. dma_addr_t addr;
  195. if (unlikely((info & OWN_MASK) == FOR_EMAC))
  196. break;
  197. /* Make a note that we saw a packet at this BD.
  198. * So next time, driver starts from this + 1
  199. */
  200. *last_rx_bd = (*last_rx_bd + 1) % RX_BD_NUM;
  201. if (unlikely((info & FIRST_OR_LAST_MASK) !=
  202. FIRST_OR_LAST_MASK)) {
  203. /* We pre-allocate buffers of MTU size so incoming
  204. * packets won't be split/chained.
  205. */
  206. if (net_ratelimit())
  207. netdev_err(ndev, "incomplete packet received\n");
  208. /* Return ownership to EMAC */
  209. rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
  210. stats->rx_errors++;
  211. stats->rx_length_errors++;
  212. continue;
  213. }
  214. pktlen = info & LEN_MASK;
  215. stats->rx_packets++;
  216. stats->rx_bytes += pktlen;
  217. skb = rx_buff->skb;
  218. skb_put(skb, pktlen);
  219. skb->dev = ndev;
  220. skb->protocol = eth_type_trans(skb, ndev);
  221. dma_unmap_single(&ndev->dev, dma_unmap_addr(rx_buff, addr),
  222. dma_unmap_len(rx_buff, len), DMA_FROM_DEVICE);
  223. /* Prepare the BD for next cycle */
  224. rx_buff->skb = netdev_alloc_skb_ip_align(ndev,
  225. EMAC_BUFFER_SIZE);
  226. if (unlikely(!rx_buff->skb)) {
  227. stats->rx_errors++;
  228. /* Because receive_skb is below, increment rx_dropped */
  229. stats->rx_dropped++;
  230. continue;
  231. }
  232. /* receive_skb only if new skb was allocated to avoid holes */
  233. netif_receive_skb(skb);
  234. addr = dma_map_single(&ndev->dev, (void *)rx_buff->skb->data,
  235. EMAC_BUFFER_SIZE, DMA_FROM_DEVICE);
  236. if (dma_mapping_error(&ndev->dev, addr)) {
  237. if (net_ratelimit())
  238. netdev_err(ndev, "cannot dma map\n");
  239. dev_kfree_skb(rx_buff->skb);
  240. stats->rx_errors++;
  241. continue;
  242. }
  243. dma_unmap_addr_set(rx_buff, addr, addr);
  244. dma_unmap_len_set(rx_buff, len, EMAC_BUFFER_SIZE);
  245. rxbd->data = cpu_to_le32(addr);
  246. /* Make sure pointer to data buffer is set */
  247. wmb();
  248. /* Return ownership to EMAC */
  249. rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
  250. }
  251. return work_done;
  252. }
  253. /**
  254. * arc_emac_poll - NAPI poll handler.
  255. * @napi: Pointer to napi_struct structure.
  256. * @budget: How many BDs to process on 1 call.
  257. *
  258. * returns: Number of processed BDs
  259. */
  260. static int arc_emac_poll(struct napi_struct *napi, int budget)
  261. {
  262. struct net_device *ndev = napi->dev;
  263. struct arc_emac_priv *priv = netdev_priv(ndev);
  264. unsigned int work_done;
  265. arc_emac_tx_clean(ndev);
  266. work_done = arc_emac_rx(ndev, budget);
  267. if (work_done < budget) {
  268. napi_complete(napi);
  269. arc_reg_or(priv, R_ENABLE, RXINT_MASK | TXINT_MASK);
  270. }
  271. return work_done;
  272. }
  273. /**
  274. * arc_emac_intr - Global interrupt handler for EMAC.
  275. * @irq: irq number.
  276. * @dev_instance: device instance.
  277. *
  278. * returns: IRQ_HANDLED for all cases.
  279. *
  280. * ARC EMAC has only 1 interrupt line, and depending on bits raised in
  281. * STATUS register we may tell what is a reason for interrupt to fire.
  282. */
  283. static irqreturn_t arc_emac_intr(int irq, void *dev_instance)
  284. {
  285. struct net_device *ndev = dev_instance;
  286. struct arc_emac_priv *priv = netdev_priv(ndev);
  287. struct net_device_stats *stats = &ndev->stats;
  288. unsigned int status;
  289. status = arc_reg_get(priv, R_STATUS);
  290. status &= ~MDIO_MASK;
  291. /* Reset all flags except "MDIO complete" */
  292. arc_reg_set(priv, R_STATUS, status);
  293. if (status & (RXINT_MASK | TXINT_MASK)) {
  294. if (likely(napi_schedule_prep(&priv->napi))) {
  295. arc_reg_clr(priv, R_ENABLE, RXINT_MASK | TXINT_MASK);
  296. __napi_schedule(&priv->napi);
  297. }
  298. }
  299. if (status & ERR_MASK) {
  300. /* MSER/RXCR/RXFR/RXFL interrupt fires on corresponding
  301. * 8-bit error counter overrun.
  302. */
  303. if (status & MSER_MASK) {
  304. stats->rx_missed_errors += 0x100;
  305. stats->rx_errors += 0x100;
  306. }
  307. if (status & RXCR_MASK) {
  308. stats->rx_crc_errors += 0x100;
  309. stats->rx_errors += 0x100;
  310. }
  311. if (status & RXFR_MASK) {
  312. stats->rx_frame_errors += 0x100;
  313. stats->rx_errors += 0x100;
  314. }
  315. if (status & RXFL_MASK) {
  316. stats->rx_over_errors += 0x100;
  317. stats->rx_errors += 0x100;
  318. }
  319. }
  320. return IRQ_HANDLED;
  321. }
  322. #ifdef CONFIG_NET_POLL_CONTROLLER
  323. static void arc_emac_poll_controller(struct net_device *dev)
  324. {
  325. disable_irq(dev->irq);
  326. arc_emac_intr(dev->irq, dev);
  327. enable_irq(dev->irq);
  328. }
  329. #endif
  330. /**
  331. * arc_emac_open - Open the network device.
  332. * @ndev: Pointer to the network device.
  333. *
  334. * returns: 0, on success or non-zero error value on failure.
  335. *
  336. * This function sets the MAC address, requests and enables an IRQ
  337. * for the EMAC device and starts the Tx queue.
  338. * It also connects to the phy device.
  339. */
  340. static int arc_emac_open(struct net_device *ndev)
  341. {
  342. struct arc_emac_priv *priv = netdev_priv(ndev);
  343. struct phy_device *phy_dev = priv->phy_dev;
  344. int i;
  345. phy_dev->autoneg = AUTONEG_ENABLE;
  346. phy_dev->speed = 0;
  347. phy_dev->duplex = 0;
  348. phy_dev->advertising &= phy_dev->supported;
  349. priv->last_rx_bd = 0;
  350. /* Allocate and set buffers for Rx BD's */
  351. for (i = 0; i < RX_BD_NUM; i++) {
  352. dma_addr_t addr;
  353. unsigned int *last_rx_bd = &priv->last_rx_bd;
  354. struct arc_emac_bd *rxbd = &priv->rxbd[*last_rx_bd];
  355. struct buffer_state *rx_buff = &priv->rx_buff[*last_rx_bd];
  356. rx_buff->skb = netdev_alloc_skb_ip_align(ndev,
  357. EMAC_BUFFER_SIZE);
  358. if (unlikely(!rx_buff->skb))
  359. return -ENOMEM;
  360. addr = dma_map_single(&ndev->dev, (void *)rx_buff->skb->data,
  361. EMAC_BUFFER_SIZE, DMA_FROM_DEVICE);
  362. if (dma_mapping_error(&ndev->dev, addr)) {
  363. netdev_err(ndev, "cannot dma map\n");
  364. dev_kfree_skb(rx_buff->skb);
  365. return -ENOMEM;
  366. }
  367. dma_unmap_addr_set(rx_buff, addr, addr);
  368. dma_unmap_len_set(rx_buff, len, EMAC_BUFFER_SIZE);
  369. rxbd->data = cpu_to_le32(addr);
  370. /* Make sure pointer to data buffer is set */
  371. wmb();
  372. /* Return ownership to EMAC */
  373. rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
  374. *last_rx_bd = (*last_rx_bd + 1) % RX_BD_NUM;
  375. }
  376. priv->txbd_curr = 0;
  377. priv->txbd_dirty = 0;
  378. /* Clean Tx BD's */
  379. memset(priv->txbd, 0, TX_RING_SZ);
  380. /* Initialize logical address filter */
  381. arc_reg_set(priv, R_LAFL, 0);
  382. arc_reg_set(priv, R_LAFH, 0);
  383. /* Set BD ring pointers for device side */
  384. arc_reg_set(priv, R_RX_RING, (unsigned int)priv->rxbd_dma);
  385. arc_reg_set(priv, R_TX_RING, (unsigned int)priv->txbd_dma);
  386. /* Enable interrupts */
  387. arc_reg_set(priv, R_ENABLE, RXINT_MASK | TXINT_MASK | ERR_MASK);
  388. /* Set CONTROL */
  389. arc_reg_set(priv, R_CTRL,
  390. (RX_BD_NUM << 24) | /* RX BD table length */
  391. (TX_BD_NUM << 16) | /* TX BD table length */
  392. TXRN_MASK | RXRN_MASK);
  393. napi_enable(&priv->napi);
  394. /* Enable EMAC */
  395. arc_reg_or(priv, R_CTRL, EN_MASK);
  396. phy_start_aneg(priv->phy_dev);
  397. netif_start_queue(ndev);
  398. return 0;
  399. }
  400. /**
  401. * arc_emac_set_rx_mode - Change the receive filtering mode.
  402. * @ndev: Pointer to the network device.
  403. *
  404. * This function enables/disables promiscuous or all-multicast mode
  405. * and updates the multicast filtering list of the network device.
  406. */
  407. static void arc_emac_set_rx_mode(struct net_device *ndev)
  408. {
  409. struct arc_emac_priv *priv = netdev_priv(ndev);
  410. if (ndev->flags & IFF_PROMISC) {
  411. arc_reg_or(priv, R_CTRL, PROM_MASK);
  412. } else {
  413. arc_reg_clr(priv, R_CTRL, PROM_MASK);
  414. if (ndev->flags & IFF_ALLMULTI) {
  415. arc_reg_set(priv, R_LAFL, ~0);
  416. arc_reg_set(priv, R_LAFH, ~0);
  417. } else {
  418. struct netdev_hw_addr *ha;
  419. unsigned int filter[2] = { 0, 0 };
  420. int bit;
  421. netdev_for_each_mc_addr(ha, ndev) {
  422. bit = ether_crc_le(ETH_ALEN, ha->addr) >> 26;
  423. filter[bit >> 5] |= 1 << (bit & 31);
  424. }
  425. arc_reg_set(priv, R_LAFL, filter[0]);
  426. arc_reg_set(priv, R_LAFH, filter[1]);
  427. }
  428. }
  429. }
  430. /**
  431. * arc_free_tx_queue - free skb from tx queue
  432. * @ndev: Pointer to the network device.
  433. *
  434. * This function must be called while EMAC disable
  435. */
  436. static void arc_free_tx_queue(struct net_device *ndev)
  437. {
  438. struct arc_emac_priv *priv = netdev_priv(ndev);
  439. unsigned int i;
  440. for (i = 0; i < TX_BD_NUM; i++) {
  441. struct arc_emac_bd *txbd = &priv->txbd[i];
  442. struct buffer_state *tx_buff = &priv->tx_buff[i];
  443. if (tx_buff->skb) {
  444. dma_unmap_single(&ndev->dev,
  445. dma_unmap_addr(tx_buff, addr),
  446. dma_unmap_len(tx_buff, len),
  447. DMA_TO_DEVICE);
  448. /* return the sk_buff to system */
  449. dev_kfree_skb_irq(tx_buff->skb);
  450. }
  451. txbd->info = 0;
  452. txbd->data = 0;
  453. tx_buff->skb = NULL;
  454. }
  455. }
  456. /**
  457. * arc_free_rx_queue - free skb from rx queue
  458. * @ndev: Pointer to the network device.
  459. *
  460. * This function must be called while EMAC disable
  461. */
  462. static void arc_free_rx_queue(struct net_device *ndev)
  463. {
  464. struct arc_emac_priv *priv = netdev_priv(ndev);
  465. unsigned int i;
  466. for (i = 0; i < RX_BD_NUM; i++) {
  467. struct arc_emac_bd *rxbd = &priv->rxbd[i];
  468. struct buffer_state *rx_buff = &priv->rx_buff[i];
  469. if (rx_buff->skb) {
  470. dma_unmap_single(&ndev->dev,
  471. dma_unmap_addr(rx_buff, addr),
  472. dma_unmap_len(rx_buff, len),
  473. DMA_FROM_DEVICE);
  474. /* return the sk_buff to system */
  475. dev_kfree_skb_irq(rx_buff->skb);
  476. }
  477. rxbd->info = 0;
  478. rxbd->data = 0;
  479. rx_buff->skb = NULL;
  480. }
  481. }
  482. /**
  483. * arc_emac_stop - Close the network device.
  484. * @ndev: Pointer to the network device.
  485. *
  486. * This function stops the Tx queue, disables interrupts and frees the IRQ for
  487. * the EMAC device.
  488. * It also disconnects the PHY device associated with the EMAC device.
  489. */
  490. static int arc_emac_stop(struct net_device *ndev)
  491. {
  492. struct arc_emac_priv *priv = netdev_priv(ndev);
  493. napi_disable(&priv->napi);
  494. netif_stop_queue(ndev);
  495. /* Disable interrupts */
  496. arc_reg_clr(priv, R_ENABLE, RXINT_MASK | TXINT_MASK | ERR_MASK);
  497. /* Disable EMAC */
  498. arc_reg_clr(priv, R_CTRL, EN_MASK);
  499. /* Return the sk_buff to system */
  500. arc_free_tx_queue(ndev);
  501. arc_free_rx_queue(ndev);
  502. return 0;
  503. }
  504. /**
  505. * arc_emac_stats - Get system network statistics.
  506. * @ndev: Pointer to net_device structure.
  507. *
  508. * Returns the address of the device statistics structure.
  509. * Statistics are updated in interrupt handler.
  510. */
  511. static struct net_device_stats *arc_emac_stats(struct net_device *ndev)
  512. {
  513. struct arc_emac_priv *priv = netdev_priv(ndev);
  514. struct net_device_stats *stats = &ndev->stats;
  515. unsigned long miss, rxerr;
  516. u8 rxcrc, rxfram, rxoflow;
  517. rxerr = arc_reg_get(priv, R_RXERR);
  518. miss = arc_reg_get(priv, R_MISS);
  519. rxcrc = rxerr;
  520. rxfram = rxerr >> 8;
  521. rxoflow = rxerr >> 16;
  522. stats->rx_errors += miss;
  523. stats->rx_errors += rxcrc + rxfram + rxoflow;
  524. stats->rx_over_errors += rxoflow;
  525. stats->rx_frame_errors += rxfram;
  526. stats->rx_crc_errors += rxcrc;
  527. stats->rx_missed_errors += miss;
  528. return stats;
  529. }
  530. /**
  531. * arc_emac_tx - Starts the data transmission.
  532. * @skb: sk_buff pointer that contains data to be Transmitted.
  533. * @ndev: Pointer to net_device structure.
  534. *
  535. * returns: NETDEV_TX_OK, on success
  536. * NETDEV_TX_BUSY, if any of the descriptors are not free.
  537. *
  538. * This function is invoked from upper layers to initiate transmission.
  539. */
  540. static int arc_emac_tx(struct sk_buff *skb, struct net_device *ndev)
  541. {
  542. struct arc_emac_priv *priv = netdev_priv(ndev);
  543. unsigned int len, *txbd_curr = &priv->txbd_curr;
  544. struct net_device_stats *stats = &ndev->stats;
  545. __le32 *info = &priv->txbd[*txbd_curr].info;
  546. dma_addr_t addr;
  547. if (skb_padto(skb, ETH_ZLEN))
  548. return NETDEV_TX_OK;
  549. len = max_t(unsigned int, ETH_ZLEN, skb->len);
  550. if (unlikely(!arc_emac_tx_avail(priv))) {
  551. netif_stop_queue(ndev);
  552. netdev_err(ndev, "BUG! Tx Ring full when queue awake!\n");
  553. return NETDEV_TX_BUSY;
  554. }
  555. addr = dma_map_single(&ndev->dev, (void *)skb->data, len,
  556. DMA_TO_DEVICE);
  557. if (unlikely(dma_mapping_error(&ndev->dev, addr))) {
  558. stats->tx_dropped++;
  559. stats->tx_errors++;
  560. dev_kfree_skb(skb);
  561. return NETDEV_TX_OK;
  562. }
  563. dma_unmap_addr_set(&priv->tx_buff[*txbd_curr], addr, addr);
  564. dma_unmap_len_set(&priv->tx_buff[*txbd_curr], len, len);
  565. priv->txbd[*txbd_curr].data = cpu_to_le32(addr);
  566. /* Make sure pointer to data buffer is set */
  567. wmb();
  568. skb_tx_timestamp(skb);
  569. *info = cpu_to_le32(FOR_EMAC | FIRST_OR_LAST_MASK | len);
  570. /* Make sure info word is set */
  571. wmb();
  572. priv->tx_buff[*txbd_curr].skb = skb;
  573. /* Increment index to point to the next BD */
  574. *txbd_curr = (*txbd_curr + 1) % TX_BD_NUM;
  575. /* Ensure that tx_clean() sees the new txbd_curr before
  576. * checking the queue status. This prevents an unneeded wake
  577. * of the queue in tx_clean().
  578. */
  579. smp_mb();
  580. if (!arc_emac_tx_avail(priv)) {
  581. netif_stop_queue(ndev);
  582. /* Refresh tx_dirty */
  583. smp_mb();
  584. if (arc_emac_tx_avail(priv))
  585. netif_start_queue(ndev);
  586. }
  587. arc_reg_set(priv, R_STATUS, TXPL_MASK);
  588. return NETDEV_TX_OK;
  589. }
  590. static void arc_emac_set_address_internal(struct net_device *ndev)
  591. {
  592. struct arc_emac_priv *priv = netdev_priv(ndev);
  593. unsigned int addr_low, addr_hi;
  594. addr_low = le32_to_cpu(*(__le32 *)&ndev->dev_addr[0]);
  595. addr_hi = le16_to_cpu(*(__le16 *)&ndev->dev_addr[4]);
  596. arc_reg_set(priv, R_ADDRL, addr_low);
  597. arc_reg_set(priv, R_ADDRH, addr_hi);
  598. }
  599. /**
  600. * arc_emac_set_address - Set the MAC address for this device.
  601. * @ndev: Pointer to net_device structure.
  602. * @p: 6 byte Address to be written as MAC address.
  603. *
  604. * This function copies the HW address from the sockaddr structure to the
  605. * net_device structure and updates the address in HW.
  606. *
  607. * returns: -EBUSY if the net device is busy or 0 if the address is set
  608. * successfully.
  609. */
  610. static int arc_emac_set_address(struct net_device *ndev, void *p)
  611. {
  612. struct sockaddr *addr = p;
  613. if (netif_running(ndev))
  614. return -EBUSY;
  615. if (!is_valid_ether_addr(addr->sa_data))
  616. return -EADDRNOTAVAIL;
  617. memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
  618. arc_emac_set_address_internal(ndev);
  619. return 0;
  620. }
  621. static const struct net_device_ops arc_emac_netdev_ops = {
  622. .ndo_open = arc_emac_open,
  623. .ndo_stop = arc_emac_stop,
  624. .ndo_start_xmit = arc_emac_tx,
  625. .ndo_set_mac_address = arc_emac_set_address,
  626. .ndo_get_stats = arc_emac_stats,
  627. .ndo_set_rx_mode = arc_emac_set_rx_mode,
  628. #ifdef CONFIG_NET_POLL_CONTROLLER
  629. .ndo_poll_controller = arc_emac_poll_controller,
  630. #endif
  631. };
  632. int arc_emac_probe(struct net_device *ndev, int interface)
  633. {
  634. struct device *dev = ndev->dev.parent;
  635. struct resource res_regs;
  636. struct device_node *phy_node;
  637. struct arc_emac_priv *priv;
  638. const char *mac_addr;
  639. unsigned int id, clock_frequency, irq;
  640. int err;
  641. /* Get PHY from device tree */
  642. phy_node = of_parse_phandle(dev->of_node, "phy", 0);
  643. if (!phy_node) {
  644. dev_err(dev, "failed to retrieve phy description from device tree\n");
  645. return -ENODEV;
  646. }
  647. /* Get EMAC registers base address from device tree */
  648. err = of_address_to_resource(dev->of_node, 0, &res_regs);
  649. if (err) {
  650. dev_err(dev, "failed to retrieve registers base from device tree\n");
  651. return -ENODEV;
  652. }
  653. /* Get IRQ from device tree */
  654. irq = irq_of_parse_and_map(dev->of_node, 0);
  655. if (!irq) {
  656. dev_err(dev, "failed to retrieve <irq> value from device tree\n");
  657. return -ENODEV;
  658. }
  659. ndev->netdev_ops = &arc_emac_netdev_ops;
  660. ndev->ethtool_ops = &arc_emac_ethtool_ops;
  661. ndev->watchdog_timeo = TX_TIMEOUT;
  662. /* FIXME :: no multicast support yet */
  663. ndev->flags &= ~IFF_MULTICAST;
  664. priv = netdev_priv(ndev);
  665. priv->dev = dev;
  666. priv->regs = devm_ioremap_resource(dev, &res_regs);
  667. if (IS_ERR(priv->regs))
  668. return PTR_ERR(priv->regs);
  669. dev_dbg(dev, "Registers base address is 0x%p\n", priv->regs);
  670. if (priv->clk) {
  671. err = clk_prepare_enable(priv->clk);
  672. if (err) {
  673. dev_err(dev, "failed to enable clock\n");
  674. return err;
  675. }
  676. clock_frequency = clk_get_rate(priv->clk);
  677. } else {
  678. /* Get CPU clock frequency from device tree */
  679. if (of_property_read_u32(dev->of_node, "clock-frequency",
  680. &clock_frequency)) {
  681. dev_err(dev, "failed to retrieve <clock-frequency> from device tree\n");
  682. return -EINVAL;
  683. }
  684. }
  685. id = arc_reg_get(priv, R_ID);
  686. /* Check for EMAC revision 5 or 7, magic number */
  687. if (!(id == 0x0005fd02 || id == 0x0007fd02)) {
  688. dev_err(dev, "ARC EMAC not detected, id=0x%x\n", id);
  689. err = -ENODEV;
  690. goto out_clken;
  691. }
  692. dev_info(dev, "ARC EMAC detected with id: 0x%x\n", id);
  693. /* Set poll rate so that it polls every 1 ms */
  694. arc_reg_set(priv, R_POLLRATE, clock_frequency / 1000000);
  695. ndev->irq = irq;
  696. dev_info(dev, "IRQ is %d\n", ndev->irq);
  697. /* Register interrupt handler for device */
  698. err = devm_request_irq(dev, ndev->irq, arc_emac_intr, 0,
  699. ndev->name, ndev);
  700. if (err) {
  701. dev_err(dev, "could not allocate IRQ\n");
  702. goto out_clken;
  703. }
  704. /* Get MAC address from device tree */
  705. mac_addr = of_get_mac_address(dev->of_node);
  706. if (mac_addr)
  707. memcpy(ndev->dev_addr, mac_addr, ETH_ALEN);
  708. else
  709. eth_hw_addr_random(ndev);
  710. arc_emac_set_address_internal(ndev);
  711. dev_info(dev, "MAC address is now %pM\n", ndev->dev_addr);
  712. /* Do 1 allocation instead of 2 separate ones for Rx and Tx BD rings */
  713. priv->rxbd = dmam_alloc_coherent(dev, RX_RING_SZ + TX_RING_SZ,
  714. &priv->rxbd_dma, GFP_KERNEL);
  715. if (!priv->rxbd) {
  716. dev_err(dev, "failed to allocate data buffers\n");
  717. err = -ENOMEM;
  718. goto out_clken;
  719. }
  720. priv->txbd = priv->rxbd + RX_BD_NUM;
  721. priv->txbd_dma = priv->rxbd_dma + RX_RING_SZ;
  722. dev_dbg(dev, "EMAC Device addr: Rx Ring [0x%x], Tx Ring[%x]\n",
  723. (unsigned int)priv->rxbd_dma, (unsigned int)priv->txbd_dma);
  724. err = arc_mdio_probe(priv);
  725. if (err) {
  726. dev_err(dev, "failed to probe MII bus\n");
  727. goto out_clken;
  728. }
  729. priv->phy_dev = of_phy_connect(ndev, phy_node, arc_emac_adjust_link, 0,
  730. interface);
  731. if (!priv->phy_dev) {
  732. dev_err(dev, "of_phy_connect() failed\n");
  733. err = -ENODEV;
  734. goto out_mdio;
  735. }
  736. dev_info(dev, "connected to %s phy with id 0x%x\n",
  737. priv->phy_dev->drv->name, priv->phy_dev->phy_id);
  738. netif_napi_add(ndev, &priv->napi, arc_emac_poll, ARC_EMAC_NAPI_WEIGHT);
  739. err = register_netdev(ndev);
  740. if (err) {
  741. dev_err(dev, "failed to register network device\n");
  742. goto out_netif_api;
  743. }
  744. return 0;
  745. out_netif_api:
  746. netif_napi_del(&priv->napi);
  747. phy_disconnect(priv->phy_dev);
  748. priv->phy_dev = NULL;
  749. out_mdio:
  750. arc_mdio_remove(priv);
  751. out_clken:
  752. if (priv->clk)
  753. clk_disable_unprepare(priv->clk);
  754. return err;
  755. }
  756. EXPORT_SYMBOL_GPL(arc_emac_probe);
  757. int arc_emac_remove(struct net_device *ndev)
  758. {
  759. struct arc_emac_priv *priv = netdev_priv(ndev);
  760. phy_disconnect(priv->phy_dev);
  761. priv->phy_dev = NULL;
  762. arc_mdio_remove(priv);
  763. unregister_netdev(ndev);
  764. netif_napi_del(&priv->napi);
  765. if (!IS_ERR(priv->clk))
  766. clk_disable_unprepare(priv->clk);
  767. return 0;
  768. }
  769. EXPORT_SYMBOL_GPL(arc_emac_remove);
  770. MODULE_AUTHOR("Alexey Brodkin <abrodkin@synopsys.com>");
  771. MODULE_DESCRIPTION("ARC EMAC driver");
  772. MODULE_LICENSE("GPL");