emac_main.c 22 KB

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