emac_main.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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 (DUPLEX_FULL == phy_dev->duplex)
  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)
  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. *txbd_dirty = (*txbd_dirty + 1) % TX_BD_NUM;
  165. }
  166. /* Ensure that txbd_dirty is visible to tx() before checking
  167. * for queue stopped.
  168. */
  169. smp_mb();
  170. if (netif_queue_stopped(ndev) && arc_emac_tx_avail(priv))
  171. netif_wake_queue(ndev);
  172. }
  173. /**
  174. * arc_emac_rx - processing of Rx packets.
  175. * @ndev: Pointer to the network device.
  176. * @budget: How many BDs to process on 1 call.
  177. *
  178. * returns: Number of processed BDs
  179. *
  180. * Iterate through Rx BDs and deliver received packages to upper layer.
  181. */
  182. static int arc_emac_rx(struct net_device *ndev, int budget)
  183. {
  184. struct arc_emac_priv *priv = netdev_priv(ndev);
  185. unsigned int work_done;
  186. for (work_done = 0; work_done < budget; work_done++) {
  187. unsigned int *last_rx_bd = &priv->last_rx_bd;
  188. struct net_device_stats *stats = &ndev->stats;
  189. struct buffer_state *rx_buff = &priv->rx_buff[*last_rx_bd];
  190. struct arc_emac_bd *rxbd = &priv->rxbd[*last_rx_bd];
  191. unsigned int pktlen, info = le32_to_cpu(rxbd->info);
  192. struct sk_buff *skb;
  193. dma_addr_t addr;
  194. if (unlikely((info & OWN_MASK) == FOR_EMAC))
  195. break;
  196. /* Make a note that we saw a packet at this BD.
  197. * So next time, driver starts from this + 1
  198. */
  199. *last_rx_bd = (*last_rx_bd + 1) % RX_BD_NUM;
  200. if (unlikely((info & FIRST_OR_LAST_MASK) !=
  201. FIRST_OR_LAST_MASK)) {
  202. /* We pre-allocate buffers of MTU size so incoming
  203. * packets won't be split/chained.
  204. */
  205. if (net_ratelimit())
  206. netdev_err(ndev, "incomplete packet received\n");
  207. /* Return ownership to EMAC */
  208. rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
  209. stats->rx_errors++;
  210. stats->rx_length_errors++;
  211. continue;
  212. }
  213. /* Prepare the BD for next cycle. netif_receive_skb()
  214. * only if new skb was allocated and mapped to avoid holes
  215. * in the RX fifo.
  216. */
  217. skb = netdev_alloc_skb_ip_align(ndev, EMAC_BUFFER_SIZE);
  218. if (unlikely(!skb)) {
  219. if (net_ratelimit())
  220. netdev_err(ndev, "cannot allocate skb\n");
  221. /* Return ownership to EMAC */
  222. rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
  223. stats->rx_errors++;
  224. stats->rx_dropped++;
  225. continue;
  226. }
  227. addr = dma_map_single(&ndev->dev, (void *)skb->data,
  228. EMAC_BUFFER_SIZE, DMA_FROM_DEVICE);
  229. if (dma_mapping_error(&ndev->dev, addr)) {
  230. if (net_ratelimit())
  231. netdev_err(ndev, "cannot map dma buffer\n");
  232. dev_kfree_skb(skb);
  233. /* Return ownership to EMAC */
  234. rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
  235. stats->rx_errors++;
  236. stats->rx_dropped++;
  237. continue;
  238. }
  239. /* unmap previosly mapped skb */
  240. dma_unmap_single(&ndev->dev, dma_unmap_addr(rx_buff, addr),
  241. dma_unmap_len(rx_buff, len), DMA_FROM_DEVICE);
  242. pktlen = info & LEN_MASK;
  243. stats->rx_packets++;
  244. stats->rx_bytes += pktlen;
  245. skb_put(rx_buff->skb, pktlen);
  246. rx_buff->skb->dev = ndev;
  247. rx_buff->skb->protocol = eth_type_trans(rx_buff->skb, ndev);
  248. netif_receive_skb(rx_buff->skb);
  249. rx_buff->skb = skb;
  250. dma_unmap_addr_set(rx_buff, addr, addr);
  251. dma_unmap_len_set(rx_buff, len, EMAC_BUFFER_SIZE);
  252. rxbd->data = cpu_to_le32(addr);
  253. /* Make sure pointer to data buffer is set */
  254. wmb();
  255. /* Return ownership to EMAC */
  256. rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
  257. }
  258. return work_done;
  259. }
  260. /**
  261. * arc_emac_poll - NAPI poll handler.
  262. * @napi: Pointer to napi_struct structure.
  263. * @budget: How many BDs to process on 1 call.
  264. *
  265. * returns: Number of processed BDs
  266. */
  267. static int arc_emac_poll(struct napi_struct *napi, int budget)
  268. {
  269. struct net_device *ndev = napi->dev;
  270. struct arc_emac_priv *priv = netdev_priv(ndev);
  271. unsigned int work_done;
  272. arc_emac_tx_clean(ndev);
  273. work_done = arc_emac_rx(ndev, budget);
  274. if (work_done < budget) {
  275. napi_complete(napi);
  276. arc_reg_or(priv, R_ENABLE, RXINT_MASK | TXINT_MASK);
  277. }
  278. return work_done;
  279. }
  280. /**
  281. * arc_emac_intr - Global interrupt handler for EMAC.
  282. * @irq: irq number.
  283. * @dev_instance: device instance.
  284. *
  285. * returns: IRQ_HANDLED for all cases.
  286. *
  287. * ARC EMAC has only 1 interrupt line, and depending on bits raised in
  288. * STATUS register we may tell what is a reason for interrupt to fire.
  289. */
  290. static irqreturn_t arc_emac_intr(int irq, void *dev_instance)
  291. {
  292. struct net_device *ndev = dev_instance;
  293. struct arc_emac_priv *priv = netdev_priv(ndev);
  294. struct net_device_stats *stats = &ndev->stats;
  295. unsigned int status;
  296. status = arc_reg_get(priv, R_STATUS);
  297. status &= ~MDIO_MASK;
  298. /* Reset all flags except "MDIO complete" */
  299. arc_reg_set(priv, R_STATUS, status);
  300. if (status & (RXINT_MASK | TXINT_MASK)) {
  301. if (likely(napi_schedule_prep(&priv->napi))) {
  302. arc_reg_clr(priv, R_ENABLE, RXINT_MASK | TXINT_MASK);
  303. __napi_schedule(&priv->napi);
  304. }
  305. }
  306. if (status & ERR_MASK) {
  307. /* MSER/RXCR/RXFR/RXFL interrupt fires on corresponding
  308. * 8-bit error counter overrun.
  309. */
  310. if (status & MSER_MASK) {
  311. stats->rx_missed_errors += 0x100;
  312. stats->rx_errors += 0x100;
  313. }
  314. if (status & RXCR_MASK) {
  315. stats->rx_crc_errors += 0x100;
  316. stats->rx_errors += 0x100;
  317. }
  318. if (status & RXFR_MASK) {
  319. stats->rx_frame_errors += 0x100;
  320. stats->rx_errors += 0x100;
  321. }
  322. if (status & RXFL_MASK) {
  323. stats->rx_over_errors += 0x100;
  324. stats->rx_errors += 0x100;
  325. }
  326. }
  327. return IRQ_HANDLED;
  328. }
  329. #ifdef CONFIG_NET_POLL_CONTROLLER
  330. static void arc_emac_poll_controller(struct net_device *dev)
  331. {
  332. disable_irq(dev->irq);
  333. arc_emac_intr(dev->irq, dev);
  334. enable_irq(dev->irq);
  335. }
  336. #endif
  337. /**
  338. * arc_emac_open - Open the network device.
  339. * @ndev: Pointer to the network device.
  340. *
  341. * returns: 0, on success or non-zero error value on failure.
  342. *
  343. * This function sets the MAC address, requests and enables an IRQ
  344. * for the EMAC device and starts the Tx queue.
  345. * It also connects to the phy device.
  346. */
  347. static int arc_emac_open(struct net_device *ndev)
  348. {
  349. struct arc_emac_priv *priv = netdev_priv(ndev);
  350. struct phy_device *phy_dev = priv->phy_dev;
  351. int i;
  352. phy_dev->autoneg = AUTONEG_ENABLE;
  353. phy_dev->speed = 0;
  354. phy_dev->duplex = 0;
  355. phy_dev->advertising &= phy_dev->supported;
  356. priv->last_rx_bd = 0;
  357. /* Allocate and set buffers for Rx BD's */
  358. for (i = 0; i < RX_BD_NUM; i++) {
  359. dma_addr_t addr;
  360. unsigned int *last_rx_bd = &priv->last_rx_bd;
  361. struct arc_emac_bd *rxbd = &priv->rxbd[*last_rx_bd];
  362. struct buffer_state *rx_buff = &priv->rx_buff[*last_rx_bd];
  363. rx_buff->skb = netdev_alloc_skb_ip_align(ndev,
  364. EMAC_BUFFER_SIZE);
  365. if (unlikely(!rx_buff->skb))
  366. return -ENOMEM;
  367. addr = dma_map_single(&ndev->dev, (void *)rx_buff->skb->data,
  368. EMAC_BUFFER_SIZE, DMA_FROM_DEVICE);
  369. if (dma_mapping_error(&ndev->dev, addr)) {
  370. netdev_err(ndev, "cannot dma map\n");
  371. dev_kfree_skb(rx_buff->skb);
  372. return -ENOMEM;
  373. }
  374. dma_unmap_addr_set(rx_buff, addr, addr);
  375. dma_unmap_len_set(rx_buff, len, EMAC_BUFFER_SIZE);
  376. rxbd->data = cpu_to_le32(addr);
  377. /* Make sure pointer to data buffer is set */
  378. wmb();
  379. /* Return ownership to EMAC */
  380. rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
  381. *last_rx_bd = (*last_rx_bd + 1) % RX_BD_NUM;
  382. }
  383. /* Clean Tx BD's */
  384. memset(priv->txbd, 0, TX_RING_SZ);
  385. /* Initialize logical address filter */
  386. arc_reg_set(priv, R_LAFL, 0);
  387. arc_reg_set(priv, R_LAFH, 0);
  388. /* Set BD ring pointers for device side */
  389. arc_reg_set(priv, R_RX_RING, (unsigned int)priv->rxbd_dma);
  390. arc_reg_set(priv, R_TX_RING, (unsigned int)priv->txbd_dma);
  391. /* Enable interrupts */
  392. arc_reg_set(priv, R_ENABLE, RXINT_MASK | TXINT_MASK | ERR_MASK);
  393. /* Set CONTROL */
  394. arc_reg_set(priv, R_CTRL,
  395. (RX_BD_NUM << 24) | /* RX BD table length */
  396. (TX_BD_NUM << 16) | /* TX BD table length */
  397. TXRN_MASK | RXRN_MASK);
  398. napi_enable(&priv->napi);
  399. /* Enable EMAC */
  400. arc_reg_or(priv, R_CTRL, EN_MASK);
  401. phy_start_aneg(priv->phy_dev);
  402. netif_start_queue(ndev);
  403. return 0;
  404. }
  405. /**
  406. * arc_emac_set_rx_mode - Change the receive filtering mode.
  407. * @ndev: Pointer to the network device.
  408. *
  409. * This function enables/disables promiscuous or all-multicast mode
  410. * and updates the multicast filtering list of the network device.
  411. */
  412. static void arc_emac_set_rx_mode(struct net_device *ndev)
  413. {
  414. struct arc_emac_priv *priv = netdev_priv(ndev);
  415. if (ndev->flags & IFF_PROMISC) {
  416. arc_reg_or(priv, R_CTRL, PROM_MASK);
  417. } else {
  418. arc_reg_clr(priv, R_CTRL, PROM_MASK);
  419. if (ndev->flags & IFF_ALLMULTI) {
  420. arc_reg_set(priv, R_LAFL, ~0);
  421. arc_reg_set(priv, R_LAFH, ~0);
  422. } else {
  423. struct netdev_hw_addr *ha;
  424. unsigned int filter[2] = { 0, 0 };
  425. int bit;
  426. netdev_for_each_mc_addr(ha, ndev) {
  427. bit = ether_crc_le(ETH_ALEN, ha->addr) >> 26;
  428. filter[bit >> 5] |= 1 << (bit & 31);
  429. }
  430. arc_reg_set(priv, R_LAFL, filter[0]);
  431. arc_reg_set(priv, R_LAFH, filter[1]);
  432. }
  433. }
  434. }
  435. /**
  436. * arc_emac_stop - Close the network device.
  437. * @ndev: Pointer to the network device.
  438. *
  439. * This function stops the Tx queue, disables interrupts and frees the IRQ for
  440. * the EMAC device.
  441. * It also disconnects the PHY device associated with the EMAC device.
  442. */
  443. static int arc_emac_stop(struct net_device *ndev)
  444. {
  445. struct arc_emac_priv *priv = netdev_priv(ndev);
  446. napi_disable(&priv->napi);
  447. netif_stop_queue(ndev);
  448. /* Disable interrupts */
  449. arc_reg_clr(priv, R_ENABLE, RXINT_MASK | TXINT_MASK | ERR_MASK);
  450. /* Disable EMAC */
  451. arc_reg_clr(priv, R_CTRL, EN_MASK);
  452. return 0;
  453. }
  454. /**
  455. * arc_emac_stats - Get system network statistics.
  456. * @ndev: Pointer to net_device structure.
  457. *
  458. * Returns the address of the device statistics structure.
  459. * Statistics are updated in interrupt handler.
  460. */
  461. static struct net_device_stats *arc_emac_stats(struct net_device *ndev)
  462. {
  463. struct arc_emac_priv *priv = netdev_priv(ndev);
  464. struct net_device_stats *stats = &ndev->stats;
  465. unsigned long miss, rxerr;
  466. u8 rxcrc, rxfram, rxoflow;
  467. rxerr = arc_reg_get(priv, R_RXERR);
  468. miss = arc_reg_get(priv, R_MISS);
  469. rxcrc = rxerr;
  470. rxfram = rxerr >> 8;
  471. rxoflow = rxerr >> 16;
  472. stats->rx_errors += miss;
  473. stats->rx_errors += rxcrc + rxfram + rxoflow;
  474. stats->rx_over_errors += rxoflow;
  475. stats->rx_frame_errors += rxfram;
  476. stats->rx_crc_errors += rxcrc;
  477. stats->rx_missed_errors += miss;
  478. return stats;
  479. }
  480. /**
  481. * arc_emac_tx - Starts the data transmission.
  482. * @skb: sk_buff pointer that contains data to be Transmitted.
  483. * @ndev: Pointer to net_device structure.
  484. *
  485. * returns: NETDEV_TX_OK, on success
  486. * NETDEV_TX_BUSY, if any of the descriptors are not free.
  487. *
  488. * This function is invoked from upper layers to initiate transmission.
  489. */
  490. static int arc_emac_tx(struct sk_buff *skb, struct net_device *ndev)
  491. {
  492. struct arc_emac_priv *priv = netdev_priv(ndev);
  493. unsigned int len, *txbd_curr = &priv->txbd_curr;
  494. struct net_device_stats *stats = &ndev->stats;
  495. __le32 *info = &priv->txbd[*txbd_curr].info;
  496. dma_addr_t addr;
  497. if (skb_padto(skb, ETH_ZLEN))
  498. return NETDEV_TX_OK;
  499. len = max_t(unsigned int, ETH_ZLEN, skb->len);
  500. if (unlikely(!arc_emac_tx_avail(priv))) {
  501. netif_stop_queue(ndev);
  502. netdev_err(ndev, "BUG! Tx Ring full when queue awake!\n");
  503. return NETDEV_TX_BUSY;
  504. }
  505. addr = dma_map_single(&ndev->dev, (void *)skb->data, len,
  506. DMA_TO_DEVICE);
  507. if (unlikely(dma_mapping_error(&ndev->dev, addr))) {
  508. stats->tx_dropped++;
  509. stats->tx_errors++;
  510. dev_kfree_skb(skb);
  511. return NETDEV_TX_OK;
  512. }
  513. dma_unmap_addr_set(&priv->tx_buff[*txbd_curr], addr, addr);
  514. dma_unmap_len_set(&priv->tx_buff[*txbd_curr], len, len);
  515. priv->tx_buff[*txbd_curr].skb = skb;
  516. priv->txbd[*txbd_curr].data = cpu_to_le32(addr);
  517. /* Make sure pointer to data buffer is set */
  518. wmb();
  519. skb_tx_timestamp(skb);
  520. *info = cpu_to_le32(FOR_EMAC | FIRST_OR_LAST_MASK | len);
  521. /* Increment index to point to the next BD */
  522. *txbd_curr = (*txbd_curr + 1) % TX_BD_NUM;
  523. /* Ensure that tx_clean() sees the new txbd_curr before
  524. * checking the queue status. This prevents an unneeded wake
  525. * of the queue in tx_clean().
  526. */
  527. smp_mb();
  528. if (!arc_emac_tx_avail(priv)) {
  529. netif_stop_queue(ndev);
  530. /* Refresh tx_dirty */
  531. smp_mb();
  532. if (arc_emac_tx_avail(priv))
  533. netif_start_queue(ndev);
  534. }
  535. arc_reg_set(priv, R_STATUS, TXPL_MASK);
  536. return NETDEV_TX_OK;
  537. }
  538. static void arc_emac_set_address_internal(struct net_device *ndev)
  539. {
  540. struct arc_emac_priv *priv = netdev_priv(ndev);
  541. unsigned int addr_low, addr_hi;
  542. addr_low = le32_to_cpu(*(__le32 *) &ndev->dev_addr[0]);
  543. addr_hi = le16_to_cpu(*(__le16 *) &ndev->dev_addr[4]);
  544. arc_reg_set(priv, R_ADDRL, addr_low);
  545. arc_reg_set(priv, R_ADDRH, addr_hi);
  546. }
  547. /**
  548. * arc_emac_set_address - Set the MAC address for this device.
  549. * @ndev: Pointer to net_device structure.
  550. * @p: 6 byte Address to be written as MAC address.
  551. *
  552. * This function copies the HW address from the sockaddr structure to the
  553. * net_device structure and updates the address in HW.
  554. *
  555. * returns: -EBUSY if the net device is busy or 0 if the address is set
  556. * successfully.
  557. */
  558. static int arc_emac_set_address(struct net_device *ndev, void *p)
  559. {
  560. struct sockaddr *addr = p;
  561. if (netif_running(ndev))
  562. return -EBUSY;
  563. if (!is_valid_ether_addr(addr->sa_data))
  564. return -EADDRNOTAVAIL;
  565. memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
  566. arc_emac_set_address_internal(ndev);
  567. return 0;
  568. }
  569. static const struct net_device_ops arc_emac_netdev_ops = {
  570. .ndo_open = arc_emac_open,
  571. .ndo_stop = arc_emac_stop,
  572. .ndo_start_xmit = arc_emac_tx,
  573. .ndo_set_mac_address = arc_emac_set_address,
  574. .ndo_get_stats = arc_emac_stats,
  575. .ndo_set_rx_mode = arc_emac_set_rx_mode,
  576. #ifdef CONFIG_NET_POLL_CONTROLLER
  577. .ndo_poll_controller = arc_emac_poll_controller,
  578. #endif
  579. };
  580. int arc_emac_probe(struct net_device *ndev, int interface)
  581. {
  582. struct device *dev = ndev->dev.parent;
  583. struct resource res_regs;
  584. struct device_node *phy_node;
  585. struct arc_emac_priv *priv;
  586. const char *mac_addr;
  587. unsigned int id, clock_frequency, irq;
  588. int err;
  589. /* Get PHY from device tree */
  590. phy_node = of_parse_phandle(dev->of_node, "phy", 0);
  591. if (!phy_node) {
  592. dev_err(dev, "failed to retrieve phy description from device tree\n");
  593. return -ENODEV;
  594. }
  595. /* Get EMAC registers base address from device tree */
  596. err = of_address_to_resource(dev->of_node, 0, &res_regs);
  597. if (err) {
  598. dev_err(dev, "failed to retrieve registers base from device tree\n");
  599. return -ENODEV;
  600. }
  601. /* Get IRQ from device tree */
  602. irq = irq_of_parse_and_map(dev->of_node, 0);
  603. if (!irq) {
  604. dev_err(dev, "failed to retrieve <irq> value from device tree\n");
  605. return -ENODEV;
  606. }
  607. ndev->netdev_ops = &arc_emac_netdev_ops;
  608. ndev->ethtool_ops = &arc_emac_ethtool_ops;
  609. ndev->watchdog_timeo = TX_TIMEOUT;
  610. /* FIXME :: no multicast support yet */
  611. ndev->flags &= ~IFF_MULTICAST;
  612. priv = netdev_priv(ndev);
  613. priv->dev = dev;
  614. priv->regs = devm_ioremap_resource(dev, &res_regs);
  615. if (IS_ERR(priv->regs)) {
  616. return PTR_ERR(priv->regs);
  617. }
  618. dev_dbg(dev, "Registers base address is 0x%p\n", priv->regs);
  619. if (priv->clk) {
  620. err = clk_prepare_enable(priv->clk);
  621. if (err) {
  622. dev_err(dev, "failed to enable clock\n");
  623. return err;
  624. }
  625. clock_frequency = clk_get_rate(priv->clk);
  626. } else {
  627. /* Get CPU clock frequency from device tree */
  628. if (of_property_read_u32(dev->of_node, "clock-frequency",
  629. &clock_frequency)) {
  630. dev_err(dev, "failed to retrieve <clock-frequency> from device tree\n");
  631. return -EINVAL;
  632. }
  633. }
  634. id = arc_reg_get(priv, R_ID);
  635. /* Check for EMAC revision 5 or 7, magic number */
  636. if (!(id == 0x0005fd02 || id == 0x0007fd02)) {
  637. dev_err(dev, "ARC EMAC not detected, id=0x%x\n", id);
  638. err = -ENODEV;
  639. goto out_clken;
  640. }
  641. dev_info(dev, "ARC EMAC detected with id: 0x%x\n", id);
  642. /* Set poll rate so that it polls every 1 ms */
  643. arc_reg_set(priv, R_POLLRATE, clock_frequency / 1000000);
  644. ndev->irq = irq;
  645. dev_info(dev, "IRQ is %d\n", ndev->irq);
  646. /* Register interrupt handler for device */
  647. err = devm_request_irq(dev, ndev->irq, arc_emac_intr, 0,
  648. ndev->name, ndev);
  649. if (err) {
  650. dev_err(dev, "could not allocate IRQ\n");
  651. goto out_clken;
  652. }
  653. /* Get MAC address from device tree */
  654. mac_addr = of_get_mac_address(dev->of_node);
  655. if (mac_addr)
  656. memcpy(ndev->dev_addr, mac_addr, ETH_ALEN);
  657. else
  658. eth_hw_addr_random(ndev);
  659. arc_emac_set_address_internal(ndev);
  660. dev_info(dev, "MAC address is now %pM\n", ndev->dev_addr);
  661. /* Do 1 allocation instead of 2 separate ones for Rx and Tx BD rings */
  662. priv->rxbd = dmam_alloc_coherent(dev, RX_RING_SZ + TX_RING_SZ,
  663. &priv->rxbd_dma, GFP_KERNEL);
  664. if (!priv->rxbd) {
  665. dev_err(dev, "failed to allocate data buffers\n");
  666. err = -ENOMEM;
  667. goto out_clken;
  668. }
  669. priv->txbd = priv->rxbd + RX_BD_NUM;
  670. priv->txbd_dma = priv->rxbd_dma + RX_RING_SZ;
  671. dev_dbg(dev, "EMAC Device addr: Rx Ring [0x%x], Tx Ring[%x]\n",
  672. (unsigned int)priv->rxbd_dma, (unsigned int)priv->txbd_dma);
  673. err = arc_mdio_probe(priv);
  674. if (err) {
  675. dev_err(dev, "failed to probe MII bus\n");
  676. goto out_clken;
  677. }
  678. priv->phy_dev = of_phy_connect(ndev, phy_node, arc_emac_adjust_link, 0,
  679. interface);
  680. if (!priv->phy_dev) {
  681. dev_err(dev, "of_phy_connect() failed\n");
  682. err = -ENODEV;
  683. goto out_mdio;
  684. }
  685. dev_info(dev, "connected to %s phy with id 0x%x\n",
  686. priv->phy_dev->drv->name, priv->phy_dev->phy_id);
  687. netif_napi_add(ndev, &priv->napi, arc_emac_poll, ARC_EMAC_NAPI_WEIGHT);
  688. err = register_netdev(ndev);
  689. if (err) {
  690. dev_err(dev, "failed to register network device\n");
  691. goto out_netif_api;
  692. }
  693. return 0;
  694. out_netif_api:
  695. netif_napi_del(&priv->napi);
  696. phy_disconnect(priv->phy_dev);
  697. priv->phy_dev = NULL;
  698. out_mdio:
  699. arc_mdio_remove(priv);
  700. out_clken:
  701. if (priv->clk)
  702. clk_disable_unprepare(priv->clk);
  703. return err;
  704. }
  705. EXPORT_SYMBOL_GPL(arc_emac_probe);
  706. int arc_emac_remove(struct net_device *ndev)
  707. {
  708. struct arc_emac_priv *priv = netdev_priv(ndev);
  709. phy_disconnect(priv->phy_dev);
  710. priv->phy_dev = NULL;
  711. arc_mdio_remove(priv);
  712. unregister_netdev(ndev);
  713. netif_napi_del(&priv->napi);
  714. if (!IS_ERR(priv->clk)) {
  715. clk_disable_unprepare(priv->clk);
  716. }
  717. return 0;
  718. }
  719. EXPORT_SYMBOL_GPL(arc_emac_remove);
  720. MODULE_AUTHOR("Alexey Brodkin <abrodkin@synopsys.com>");
  721. MODULE_DESCRIPTION("ARC EMAC driver");
  722. MODULE_LICENSE("GPL");