nps_enet.c 19 KB

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