nps_enet.c 19 KB

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