emac_main.c 23 KB

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