emac_main.c 21 KB

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