nps_enet.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*
  2. * Copyright(c) 2015 EZchip Technologies.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * The full GNU General Public License is included in this distribution in
  14. * the file called "COPYING".
  15. */
  16. #include <linux/module.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/of_net.h>
  21. #include <linux/of_platform.h>
  22. #include "nps_enet.h"
  23. #define DRV_NAME "nps_mgt_enet"
  24. static void nps_enet_clean_rx_fifo(struct net_device *ndev, u32 frame_len)
  25. {
  26. struct nps_enet_priv *priv = netdev_priv(ndev);
  27. u32 i, len = DIV_ROUND_UP(frame_len, sizeof(u32));
  28. /* Empty Rx FIFO buffer by reading all words */
  29. for (i = 0; i < len; i++)
  30. nps_enet_reg_get(priv, NPS_ENET_REG_RX_BUF);
  31. }
  32. static void nps_enet_read_rx_fifo(struct net_device *ndev,
  33. unsigned char *dst, u32 length)
  34. {
  35. struct nps_enet_priv *priv = netdev_priv(ndev);
  36. s32 i, last = length & (sizeof(u32) - 1);
  37. u32 *reg = (u32 *)dst, len = length / sizeof(u32);
  38. bool dst_is_aligned = IS_ALIGNED((unsigned long)dst, sizeof(u32));
  39. /* In case dst is not aligned we need an intermediate buffer */
  40. if (dst_is_aligned)
  41. for (i = 0; i < len; i++, reg++)
  42. *reg = nps_enet_reg_get(priv, NPS_ENET_REG_RX_BUF);
  43. else { /* !dst_is_aligned */
  44. for (i = 0; i < len; i++, reg++) {
  45. u32 buf =
  46. nps_enet_reg_get(priv, NPS_ENET_REG_RX_BUF);
  47. /* to accommodate word-unaligned address of "reg"
  48. * we have to do memcpy_toio() instead of simple "=".
  49. */
  50. memcpy_toio((void __iomem *)reg, &buf, sizeof(buf));
  51. }
  52. }
  53. /* copy last bytes (if any) */
  54. if (last) {
  55. u32 buf = nps_enet_reg_get(priv, NPS_ENET_REG_RX_BUF);
  56. memcpy_toio((void __iomem *)reg, &buf, last);
  57. }
  58. }
  59. static u32 nps_enet_rx_handler(struct net_device *ndev)
  60. {
  61. u32 frame_len, err = 0;
  62. u32 work_done = 0;
  63. struct nps_enet_priv *priv = netdev_priv(ndev);
  64. struct sk_buff *skb;
  65. struct nps_enet_rx_ctl rx_ctrl;
  66. rx_ctrl.value = nps_enet_reg_get(priv, NPS_ENET_REG_RX_CTL);
  67. frame_len = rx_ctrl.nr;
  68. /* Check if we got RX */
  69. if (!rx_ctrl.cr)
  70. return work_done;
  71. /* If we got here there is a work for us */
  72. work_done++;
  73. /* Check Rx error */
  74. if (rx_ctrl.er) {
  75. ndev->stats.rx_errors++;
  76. err = 1;
  77. }
  78. /* Check Rx CRC error */
  79. if (rx_ctrl.crc) {
  80. ndev->stats.rx_crc_errors++;
  81. ndev->stats.rx_dropped++;
  82. err = 1;
  83. }
  84. /* Check Frame length Min 64b */
  85. if (unlikely(frame_len < ETH_ZLEN)) {
  86. ndev->stats.rx_length_errors++;
  87. ndev->stats.rx_dropped++;
  88. err = 1;
  89. }
  90. if (err)
  91. goto rx_irq_clean;
  92. /* Skb allocation */
  93. skb = netdev_alloc_skb_ip_align(ndev, frame_len);
  94. if (unlikely(!skb)) {
  95. ndev->stats.rx_errors++;
  96. ndev->stats.rx_dropped++;
  97. goto rx_irq_clean;
  98. }
  99. /* Copy frame from Rx fifo into the skb */
  100. nps_enet_read_rx_fifo(ndev, skb->data, frame_len);
  101. skb_put(skb, frame_len);
  102. skb->protocol = eth_type_trans(skb, ndev);
  103. skb->ip_summed = CHECKSUM_UNNECESSARY;
  104. ndev->stats.rx_packets++;
  105. ndev->stats.rx_bytes += frame_len;
  106. netif_receive_skb(skb);
  107. goto rx_irq_frame_done;
  108. rx_irq_clean:
  109. /* Clean Rx fifo */
  110. nps_enet_clean_rx_fifo(ndev, frame_len);
  111. rx_irq_frame_done:
  112. /* Ack Rx ctrl register */
  113. nps_enet_reg_set(priv, NPS_ENET_REG_RX_CTL, 0);
  114. return work_done;
  115. }
  116. static void nps_enet_tx_handler(struct net_device *ndev)
  117. {
  118. struct nps_enet_priv *priv = netdev_priv(ndev);
  119. struct nps_enet_tx_ctl tx_ctrl;
  120. tx_ctrl.value = nps_enet_reg_get(priv, NPS_ENET_REG_TX_CTL);
  121. /* Check if we got TX */
  122. if (!priv->tx_packet_sent || tx_ctrl.ct)
  123. return;
  124. /* Ack Tx ctrl register */
  125. nps_enet_reg_set(priv, NPS_ENET_REG_TX_CTL, 0);
  126. /* Check Tx transmit error */
  127. if (unlikely(tx_ctrl.et)) {
  128. ndev->stats.tx_errors++;
  129. } else {
  130. ndev->stats.tx_packets++;
  131. ndev->stats.tx_bytes += tx_ctrl.nt;
  132. }
  133. dev_kfree_skb(priv->tx_skb);
  134. priv->tx_packet_sent = false;
  135. if (netif_queue_stopped(ndev))
  136. netif_wake_queue(ndev);
  137. }
  138. /**
  139. * nps_enet_poll - NAPI poll handler.
  140. * @napi: Pointer to napi_struct structure.
  141. * @budget: How many frames to process on one call.
  142. *
  143. * returns: Number of processed frames
  144. */
  145. static int nps_enet_poll(struct napi_struct *napi, int budget)
  146. {
  147. struct net_device *ndev = napi->dev;
  148. struct nps_enet_priv *priv = netdev_priv(ndev);
  149. u32 work_done;
  150. nps_enet_tx_handler(ndev);
  151. work_done = nps_enet_rx_handler(ndev);
  152. if (work_done < budget) {
  153. struct nps_enet_buf_int_enable buf_int_enable;
  154. napi_complete(napi);
  155. buf_int_enable.rx_rdy = NPS_ENET_ENABLE;
  156. buf_int_enable.tx_done = NPS_ENET_ENABLE;
  157. nps_enet_reg_set(priv, NPS_ENET_REG_BUF_INT_ENABLE,
  158. buf_int_enable.value);
  159. }
  160. return work_done;
  161. }
  162. /**
  163. * nps_enet_irq_handler - Global interrupt handler for ENET.
  164. * @irq: irq number.
  165. * @dev_instance: device instance.
  166. *
  167. * returns: IRQ_HANDLED for all cases.
  168. *
  169. * EZchip ENET has 2 interrupt causes, and depending on bits raised in
  170. * CTRL registers we may tell what is a reason for interrupt to fire up.
  171. * We got one for RX and the other for TX (completion).
  172. */
  173. static irqreturn_t nps_enet_irq_handler(s32 irq, void *dev_instance)
  174. {
  175. struct net_device *ndev = dev_instance;
  176. struct nps_enet_priv *priv = netdev_priv(ndev);
  177. struct nps_enet_rx_ctl rx_ctrl;
  178. struct nps_enet_tx_ctl tx_ctrl;
  179. rx_ctrl.value = nps_enet_reg_get(priv, NPS_ENET_REG_RX_CTL);
  180. tx_ctrl.value = nps_enet_reg_get(priv, NPS_ENET_REG_TX_CTL);
  181. if ((!tx_ctrl.ct && priv->tx_packet_sent) || rx_ctrl.cr)
  182. if (likely(napi_schedule_prep(&priv->napi))) {
  183. nps_enet_reg_set(priv, NPS_ENET_REG_BUF_INT_ENABLE, 0);
  184. __napi_schedule(&priv->napi);
  185. }
  186. return IRQ_HANDLED;
  187. }
  188. static void nps_enet_set_hw_mac_address(struct net_device *ndev)
  189. {
  190. struct nps_enet_priv *priv = netdev_priv(ndev);
  191. struct nps_enet_ge_mac_cfg_1 ge_mac_cfg_1;
  192. struct nps_enet_ge_mac_cfg_2 *ge_mac_cfg_2 = &priv->ge_mac_cfg_2;
  193. /* set MAC address in HW */
  194. ge_mac_cfg_1.octet_0 = ndev->dev_addr[0];
  195. ge_mac_cfg_1.octet_1 = ndev->dev_addr[1];
  196. ge_mac_cfg_1.octet_2 = ndev->dev_addr[2];
  197. ge_mac_cfg_1.octet_3 = ndev->dev_addr[3];
  198. ge_mac_cfg_2->octet_4 = ndev->dev_addr[4];
  199. ge_mac_cfg_2->octet_5 = ndev->dev_addr[5];
  200. nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_1,
  201. ge_mac_cfg_1.value);
  202. nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_2,
  203. ge_mac_cfg_2->value);
  204. }
  205. /**
  206. * nps_enet_hw_reset - Reset the network device.
  207. * @ndev: Pointer to the network device.
  208. *
  209. * This function reset the PCS and TX fifo.
  210. * The programming model is to set the relevant reset bits
  211. * wait for some time for this to propagate and then unset
  212. * the reset bits. This way we ensure that reset procedure
  213. * is done successfully by device.
  214. */
  215. static void nps_enet_hw_reset(struct net_device *ndev)
  216. {
  217. struct nps_enet_priv *priv = netdev_priv(ndev);
  218. struct nps_enet_ge_rst ge_rst;
  219. struct nps_enet_phase_fifo_ctl phase_fifo_ctl;
  220. ge_rst.value = 0;
  221. phase_fifo_ctl.value = 0;
  222. /* Pcs reset sequence*/
  223. ge_rst.gmac_0 = NPS_ENET_ENABLE;
  224. nps_enet_reg_set(priv, NPS_ENET_REG_GE_RST, ge_rst.value);
  225. usleep_range(10, 20);
  226. ge_rst.value = 0;
  227. nps_enet_reg_set(priv, NPS_ENET_REG_GE_RST, ge_rst.value);
  228. /* Tx fifo reset sequence */
  229. phase_fifo_ctl.rst = NPS_ENET_ENABLE;
  230. phase_fifo_ctl.init = NPS_ENET_ENABLE;
  231. nps_enet_reg_set(priv, NPS_ENET_REG_PHASE_FIFO_CTL,
  232. phase_fifo_ctl.value);
  233. usleep_range(10, 20);
  234. phase_fifo_ctl.value = 0;
  235. nps_enet_reg_set(priv, NPS_ENET_REG_PHASE_FIFO_CTL,
  236. phase_fifo_ctl.value);
  237. }
  238. static void nps_enet_hw_enable_control(struct net_device *ndev)
  239. {
  240. struct nps_enet_priv *priv = netdev_priv(ndev);
  241. struct nps_enet_ge_mac_cfg_0 ge_mac_cfg_0;
  242. struct nps_enet_buf_int_enable buf_int_enable;
  243. struct nps_enet_ge_mac_cfg_2 *ge_mac_cfg_2 = &priv->ge_mac_cfg_2;
  244. struct nps_enet_ge_mac_cfg_3 *ge_mac_cfg_3 = &priv->ge_mac_cfg_3;
  245. s32 max_frame_length;
  246. ge_mac_cfg_0.value = 0;
  247. buf_int_enable.value = 0;
  248. /* Enable Rx and Tx statistics */
  249. ge_mac_cfg_2->stat_en = NPS_ENET_GE_MAC_CFG_2_STAT_EN;
  250. /* Discard packets with different MAC address */
  251. ge_mac_cfg_2->disc_da = NPS_ENET_ENABLE;
  252. /* Discard multicast packets */
  253. ge_mac_cfg_2->disc_mc = NPS_ENET_ENABLE;
  254. nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_2,
  255. ge_mac_cfg_2->value);
  256. /* Discard Packets bigger than max frame length */
  257. max_frame_length = ETH_HLEN + ndev->mtu + ETH_FCS_LEN;
  258. if (max_frame_length <= NPS_ENET_MAX_FRAME_LENGTH)
  259. ge_mac_cfg_3->max_len = max_frame_length;
  260. /* Enable interrupts */
  261. buf_int_enable.rx_rdy = NPS_ENET_ENABLE;
  262. buf_int_enable.tx_done = NPS_ENET_ENABLE;
  263. nps_enet_reg_set(priv, NPS_ENET_REG_BUF_INT_ENABLE,
  264. buf_int_enable.value);
  265. /* Write device MAC address to HW */
  266. nps_enet_set_hw_mac_address(ndev);
  267. /* Rx and Tx HW features */
  268. ge_mac_cfg_0.tx_pad_en = NPS_ENET_ENABLE;
  269. ge_mac_cfg_0.tx_crc_en = NPS_ENET_ENABLE;
  270. ge_mac_cfg_0.rx_crc_strip = NPS_ENET_ENABLE;
  271. /* IFG configuration */
  272. ge_mac_cfg_0.rx_ifg = NPS_ENET_GE_MAC_CFG_0_RX_IFG;
  273. ge_mac_cfg_0.tx_ifg = NPS_ENET_GE_MAC_CFG_0_TX_IFG;
  274. /* preamble configuration */
  275. ge_mac_cfg_0.rx_pr_check_en = NPS_ENET_ENABLE;
  276. ge_mac_cfg_0.tx_pr_len = NPS_ENET_GE_MAC_CFG_0_TX_PR_LEN;
  277. /* enable flow control frames */
  278. ge_mac_cfg_0.tx_fc_en = NPS_ENET_ENABLE;
  279. ge_mac_cfg_0.rx_fc_en = NPS_ENET_ENABLE;
  280. ge_mac_cfg_0.tx_fc_retr = NPS_ENET_GE_MAC_CFG_0_TX_FC_RETR;
  281. ge_mac_cfg_3->cf_drop = NPS_ENET_ENABLE;
  282. /* Enable Rx and Tx */
  283. ge_mac_cfg_0.rx_en = NPS_ENET_ENABLE;
  284. ge_mac_cfg_0.tx_en = NPS_ENET_ENABLE;
  285. nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_3,
  286. ge_mac_cfg_3->value);
  287. nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_0,
  288. ge_mac_cfg_0.value);
  289. }
  290. static void nps_enet_hw_disable_control(struct net_device *ndev)
  291. {
  292. struct nps_enet_priv *priv = netdev_priv(ndev);
  293. /* Disable interrupts */
  294. nps_enet_reg_set(priv, NPS_ENET_REG_BUF_INT_ENABLE, 0);
  295. /* Disable Rx and Tx */
  296. nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_0, 0);
  297. }
  298. static void nps_enet_send_frame(struct net_device *ndev,
  299. struct sk_buff *skb)
  300. {
  301. struct nps_enet_priv *priv = netdev_priv(ndev);
  302. struct nps_enet_tx_ctl tx_ctrl;
  303. short length = skb->len;
  304. u32 i, len = DIV_ROUND_UP(length, sizeof(u32));
  305. u32 *src = (u32 *)virt_to_phys(skb->data);
  306. bool src_is_aligned = IS_ALIGNED((unsigned long)src, sizeof(u32));
  307. tx_ctrl.value = 0;
  308. /* In case src is not aligned we need an intermediate buffer */
  309. if (src_is_aligned)
  310. for (i = 0; i < len; i++, src++)
  311. nps_enet_reg_set(priv, NPS_ENET_REG_TX_BUF, *src);
  312. else { /* !src_is_aligned */
  313. for (i = 0; i < len; i++, src++) {
  314. u32 buf;
  315. /* to accommodate word-unaligned address of "src"
  316. * we have to do memcpy_fromio() instead of simple "="
  317. */
  318. memcpy_fromio(&buf, (void __iomem *)src, sizeof(buf));
  319. nps_enet_reg_set(priv, NPS_ENET_REG_TX_BUF, buf);
  320. }
  321. }
  322. /* Write the length of the Frame */
  323. tx_ctrl.nt = length;
  324. /* Indicate SW is done */
  325. priv->tx_packet_sent = true;
  326. tx_ctrl.ct = NPS_ENET_ENABLE;
  327. /* Send Frame */
  328. nps_enet_reg_set(priv, NPS_ENET_REG_TX_CTL, tx_ctrl.value);
  329. }
  330. /**
  331. * nps_enet_set_mac_address - Set the MAC address for this device.
  332. * @ndev: Pointer to net_device structure.
  333. * @p: 6 byte Address to be written as MAC address.
  334. *
  335. * This function copies the HW address from the sockaddr structure to the
  336. * net_device structure and updates the address in HW.
  337. *
  338. * returns: -EBUSY if the net device is busy or 0 if the address is set
  339. * successfully.
  340. */
  341. static s32 nps_enet_set_mac_address(struct net_device *ndev, void *p)
  342. {
  343. struct sockaddr *addr = p;
  344. s32 res;
  345. if (netif_running(ndev))
  346. return -EBUSY;
  347. res = eth_mac_addr(ndev, p);
  348. if (!res) {
  349. ether_addr_copy(ndev->dev_addr, addr->sa_data);
  350. nps_enet_set_hw_mac_address(ndev);
  351. }
  352. return res;
  353. }
  354. /**
  355. * nps_enet_set_rx_mode - Change the receive filtering mode.
  356. * @ndev: Pointer to the network device.
  357. *
  358. * This function enables/disables promiscuous mode
  359. */
  360. static void nps_enet_set_rx_mode(struct net_device *ndev)
  361. {
  362. struct nps_enet_priv *priv = netdev_priv(ndev);
  363. struct nps_enet_ge_mac_cfg_2 ge_mac_cfg_2;
  364. ge_mac_cfg_2.value = priv->ge_mac_cfg_2.value;
  365. if (ndev->flags & IFF_PROMISC) {
  366. ge_mac_cfg_2.disc_da = NPS_ENET_DISABLE;
  367. ge_mac_cfg_2.disc_mc = NPS_ENET_DISABLE;
  368. } else {
  369. ge_mac_cfg_2.disc_da = NPS_ENET_ENABLE;
  370. ge_mac_cfg_2.disc_mc = NPS_ENET_ENABLE;
  371. }
  372. nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_2, ge_mac_cfg_2.value);
  373. }
  374. /**
  375. * nps_enet_open - Open the network device.
  376. * @ndev: Pointer to the network device.
  377. *
  378. * returns: 0, on success or non-zero error value on failure.
  379. *
  380. * This function sets the MAC address, requests and enables an IRQ
  381. * for the ENET device and starts the Tx queue.
  382. */
  383. static s32 nps_enet_open(struct net_device *ndev)
  384. {
  385. struct nps_enet_priv *priv = netdev_priv(ndev);
  386. s32 err;
  387. /* Reset private variables */
  388. priv->tx_packet_sent = false;
  389. priv->ge_mac_cfg_2.value = 0;
  390. priv->ge_mac_cfg_3.value = 0;
  391. /* ge_mac_cfg_3 default values */
  392. priv->ge_mac_cfg_3.rx_ifg_th = NPS_ENET_GE_MAC_CFG_3_RX_IFG_TH;
  393. priv->ge_mac_cfg_3.max_len = NPS_ENET_GE_MAC_CFG_3_MAX_LEN;
  394. /* Disable HW device */
  395. nps_enet_hw_disable_control(ndev);
  396. /* irq Rx allocation */
  397. err = request_irq(priv->irq, nps_enet_irq_handler,
  398. 0, "enet-rx-tx", ndev);
  399. if (err)
  400. return err;
  401. napi_enable(&priv->napi);
  402. /* Enable HW device */
  403. nps_enet_hw_reset(ndev);
  404. nps_enet_hw_enable_control(ndev);
  405. netif_start_queue(ndev);
  406. return 0;
  407. }
  408. /**
  409. * nps_enet_stop - Close the network device.
  410. * @ndev: Pointer to the network device.
  411. *
  412. * This function stops the Tx queue, disables interrupts for the ENET device.
  413. */
  414. static s32 nps_enet_stop(struct net_device *ndev)
  415. {
  416. struct nps_enet_priv *priv = netdev_priv(ndev);
  417. napi_disable(&priv->napi);
  418. netif_stop_queue(ndev);
  419. nps_enet_hw_disable_control(ndev);
  420. free_irq(priv->irq, ndev);
  421. return 0;
  422. }
  423. /**
  424. * nps_enet_start_xmit - Starts the data transmission.
  425. * @skb: sk_buff pointer that contains data to be Transmitted.
  426. * @ndev: Pointer to net_device structure.
  427. *
  428. * returns: NETDEV_TX_OK, on success
  429. * NETDEV_TX_BUSY, if any of the descriptors are not free.
  430. *
  431. * This function is invoked from upper layers to initiate transmission.
  432. */
  433. static netdev_tx_t nps_enet_start_xmit(struct sk_buff *skb,
  434. struct net_device *ndev)
  435. {
  436. struct nps_enet_priv *priv = netdev_priv(ndev);
  437. /* This driver handles one frame at a time */
  438. netif_stop_queue(ndev);
  439. priv->tx_skb = skb;
  440. nps_enet_send_frame(ndev, skb);
  441. return NETDEV_TX_OK;
  442. }
  443. #ifdef CONFIG_NET_POLL_CONTROLLER
  444. static void nps_enet_poll_controller(struct net_device *ndev)
  445. {
  446. disable_irq(ndev->irq);
  447. nps_enet_irq_handler(ndev->irq, ndev);
  448. enable_irq(ndev->irq);
  449. }
  450. #endif
  451. static const struct net_device_ops nps_netdev_ops = {
  452. .ndo_open = nps_enet_open,
  453. .ndo_stop = nps_enet_stop,
  454. .ndo_start_xmit = nps_enet_start_xmit,
  455. .ndo_set_mac_address = nps_enet_set_mac_address,
  456. .ndo_set_rx_mode = nps_enet_set_rx_mode,
  457. #ifdef CONFIG_NET_POLL_CONTROLLER
  458. .ndo_poll_controller = nps_enet_poll_controller,
  459. #endif
  460. };
  461. static s32 nps_enet_probe(struct platform_device *pdev)
  462. {
  463. struct device *dev = &pdev->dev;
  464. struct net_device *ndev;
  465. struct nps_enet_priv *priv;
  466. s32 err = 0;
  467. const char *mac_addr;
  468. struct resource *res_regs;
  469. if (!dev->of_node)
  470. return -ENODEV;
  471. ndev = alloc_etherdev(sizeof(struct nps_enet_priv));
  472. if (!ndev)
  473. return -ENOMEM;
  474. platform_set_drvdata(pdev, ndev);
  475. SET_NETDEV_DEV(ndev, dev);
  476. priv = netdev_priv(ndev);
  477. /* The EZ NET specific entries in the device structure. */
  478. ndev->netdev_ops = &nps_netdev_ops;
  479. ndev->watchdog_timeo = (400 * HZ / 1000);
  480. /* FIXME :: no multicast support yet */
  481. ndev->flags &= ~IFF_MULTICAST;
  482. res_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  483. priv->regs_base = devm_ioremap_resource(dev, res_regs);
  484. if (IS_ERR(priv->regs_base)) {
  485. err = PTR_ERR(priv->regs_base);
  486. goto out_netdev;
  487. }
  488. dev_dbg(dev, "Registers base address is 0x%p\n", priv->regs_base);
  489. /* set kernel MAC address to dev */
  490. mac_addr = of_get_mac_address(dev->of_node);
  491. if (mac_addr)
  492. ether_addr_copy(ndev->dev_addr, mac_addr);
  493. else
  494. eth_hw_addr_random(ndev);
  495. /* Get IRQ number */
  496. priv->irq = platform_get_irq(pdev, 0);
  497. if (!priv->irq) {
  498. dev_err(dev, "failed to retrieve <irq Rx-Tx> value from device tree\n");
  499. err = -ENODEV;
  500. goto out_netdev;
  501. }
  502. netif_napi_add(ndev, &priv->napi, nps_enet_poll,
  503. NPS_ENET_NAPI_POLL_WEIGHT);
  504. /* Register the driver. Should be the last thing in probe */
  505. err = register_netdev(ndev);
  506. if (err) {
  507. dev_err(dev, "Failed to register ndev for %s, err = 0x%08x\n",
  508. ndev->name, (s32)err);
  509. goto out_netif_api;
  510. }
  511. dev_info(dev, "(rx/tx=%d)\n", priv->irq);
  512. return 0;
  513. out_netif_api:
  514. netif_napi_del(&priv->napi);
  515. out_netdev:
  516. if (err)
  517. free_netdev(ndev);
  518. return err;
  519. }
  520. static s32 nps_enet_remove(struct platform_device *pdev)
  521. {
  522. struct net_device *ndev = platform_get_drvdata(pdev);
  523. struct nps_enet_priv *priv = netdev_priv(ndev);
  524. unregister_netdev(ndev);
  525. free_netdev(ndev);
  526. netif_napi_del(&priv->napi);
  527. return 0;
  528. }
  529. static const struct of_device_id nps_enet_dt_ids[] = {
  530. { .compatible = "ezchip,nps-mgt-enet" },
  531. { /* Sentinel */ }
  532. };
  533. static struct platform_driver nps_enet_driver = {
  534. .probe = nps_enet_probe,
  535. .remove = nps_enet_remove,
  536. .driver = {
  537. .name = DRV_NAME,
  538. .of_match_table = nps_enet_dt_ids,
  539. },
  540. };
  541. module_platform_driver(nps_enet_driver);
  542. MODULE_AUTHOR("EZchip Semiconductor");
  543. MODULE_LICENSE("GPL v2");