nps_enet.c 19 KB

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