nps_enet.c 19 KB

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