nps_enet.c 17 KB

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