14fa9c49fSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 20dd07709SNoam Camus /* 30dd07709SNoam Camus * Copyright(c) 2015 EZchip Technologies. 40dd07709SNoam Camus */ 50dd07709SNoam Camus 60dd07709SNoam Camus #include <linux/module.h> 70dd07709SNoam Camus #include <linux/etherdevice.h> 8282ccf6eSFlorian Westphal #include <linux/interrupt.h> 90dd07709SNoam Camus #include <linux/of_address.h> 100dd07709SNoam Camus #include <linux/of_irq.h> 110dd07709SNoam Camus #include <linux/of_net.h> 120dd07709SNoam Camus #include <linux/of_platform.h> 130dd07709SNoam Camus #include "nps_enet.h" 140dd07709SNoam Camus 150dd07709SNoam Camus #define DRV_NAME "nps_mgt_enet" 160dd07709SNoam Camus 17094f57aaSElad Kanfi static inline bool nps_enet_is_tx_pending(struct nps_enet_priv *priv) 18094f57aaSElad Kanfi { 19094f57aaSElad Kanfi u32 tx_ctrl_value = nps_enet_reg_get(priv, NPS_ENET_REG_TX_CTL); 20094f57aaSElad Kanfi u32 tx_ctrl_ct = (tx_ctrl_value & TX_CTL_CT_MASK) >> TX_CTL_CT_SHIFT; 21094f57aaSElad Kanfi 22094f57aaSElad Kanfi return (!tx_ctrl_ct && priv->tx_skb); 23094f57aaSElad Kanfi } 24094f57aaSElad Kanfi 250dd07709SNoam Camus static void nps_enet_clean_rx_fifo(struct net_device *ndev, u32 frame_len) 260dd07709SNoam Camus { 270dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 280dd07709SNoam Camus u32 i, len = DIV_ROUND_UP(frame_len, sizeof(u32)); 290dd07709SNoam Camus 300dd07709SNoam Camus /* Empty Rx FIFO buffer by reading all words */ 310dd07709SNoam Camus for (i = 0; i < len; i++) 320dd07709SNoam Camus nps_enet_reg_get(priv, NPS_ENET_REG_RX_BUF); 330dd07709SNoam Camus } 340dd07709SNoam Camus 350dd07709SNoam Camus static void nps_enet_read_rx_fifo(struct net_device *ndev, 360dd07709SNoam Camus unsigned char *dst, u32 length) 370dd07709SNoam Camus { 380dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 390dd07709SNoam Camus s32 i, last = length & (sizeof(u32) - 1); 400dd07709SNoam Camus u32 *reg = (u32 *)dst, len = length / sizeof(u32); 410dd07709SNoam Camus bool dst_is_aligned = IS_ALIGNED((unsigned long)dst, sizeof(u32)); 420dd07709SNoam Camus 430dd07709SNoam Camus /* In case dst is not aligned we need an intermediate buffer */ 44b54b8c2dSLada Trimasova if (dst_is_aligned) { 45b54b8c2dSLada Trimasova ioread32_rep(priv->regs_base + NPS_ENET_REG_RX_BUF, reg, len); 46b54b8c2dSLada Trimasova reg += len; 47ddbff3e8SElad Kanfi } else { /* !dst_is_aligned */ 480dd07709SNoam Camus for (i = 0; i < len; i++, reg++) { 49b0a8d1a0SArnd Bergmann u32 buf = nps_enet_reg_get(priv, NPS_ENET_REG_RX_BUF); 50ddbff3e8SElad Kanfi 51b54b8c2dSLada Trimasova put_unaligned_be32(buf, reg); 520dd07709SNoam Camus } 530dd07709SNoam Camus } 540dd07709SNoam Camus /* copy last bytes (if any) */ 550dd07709SNoam Camus if (last) { 56b54b8c2dSLada Trimasova u32 buf; 57ddbff3e8SElad Kanfi 58b54b8c2dSLada Trimasova ioread32_rep(priv->regs_base + NPS_ENET_REG_RX_BUF, &buf, 1); 59b0a8d1a0SArnd Bergmann memcpy((u8 *)reg, &buf, last); 600dd07709SNoam Camus } 610dd07709SNoam Camus } 620dd07709SNoam Camus 630dd07709SNoam Camus static u32 nps_enet_rx_handler(struct net_device *ndev) 640dd07709SNoam Camus { 650dd07709SNoam Camus u32 frame_len, err = 0; 660dd07709SNoam Camus u32 work_done = 0; 670dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 680dd07709SNoam Camus struct sk_buff *skb; 69b54b8c2dSLada Trimasova u32 rx_ctrl_value = nps_enet_reg_get(priv, NPS_ENET_REG_RX_CTL); 70b54b8c2dSLada Trimasova u32 rx_ctrl_cr = (rx_ctrl_value & RX_CTL_CR_MASK) >> RX_CTL_CR_SHIFT; 71b54b8c2dSLada Trimasova u32 rx_ctrl_er = (rx_ctrl_value & RX_CTL_ER_MASK) >> RX_CTL_ER_SHIFT; 72b54b8c2dSLada Trimasova u32 rx_ctrl_crc = (rx_ctrl_value & RX_CTL_CRC_MASK) >> RX_CTL_CRC_SHIFT; 730dd07709SNoam Camus 74b54b8c2dSLada Trimasova frame_len = (rx_ctrl_value & RX_CTL_NR_MASK) >> RX_CTL_NR_SHIFT; 750dd07709SNoam Camus 760dd07709SNoam Camus /* Check if we got RX */ 77b54b8c2dSLada Trimasova if (!rx_ctrl_cr) 780dd07709SNoam Camus return work_done; 790dd07709SNoam Camus 800dd07709SNoam Camus /* If we got here there is a work for us */ 810dd07709SNoam Camus work_done++; 820dd07709SNoam Camus 830dd07709SNoam Camus /* Check Rx error */ 84b54b8c2dSLada Trimasova if (rx_ctrl_er) { 850dd07709SNoam Camus ndev->stats.rx_errors++; 860dd07709SNoam Camus err = 1; 870dd07709SNoam Camus } 880dd07709SNoam Camus 890dd07709SNoam Camus /* Check Rx CRC error */ 90b54b8c2dSLada Trimasova if (rx_ctrl_crc) { 910dd07709SNoam Camus ndev->stats.rx_crc_errors++; 920dd07709SNoam Camus ndev->stats.rx_dropped++; 930dd07709SNoam Camus err = 1; 940dd07709SNoam Camus } 950dd07709SNoam Camus 960dd07709SNoam Camus /* Check Frame length Min 64b */ 970dd07709SNoam Camus if (unlikely(frame_len < ETH_ZLEN)) { 980dd07709SNoam Camus ndev->stats.rx_length_errors++; 990dd07709SNoam Camus ndev->stats.rx_dropped++; 1000dd07709SNoam Camus err = 1; 1010dd07709SNoam Camus } 1020dd07709SNoam Camus 1030dd07709SNoam Camus if (err) 1040dd07709SNoam Camus goto rx_irq_clean; 1050dd07709SNoam Camus 1060dd07709SNoam Camus /* Skb allocation */ 1070dd07709SNoam Camus skb = netdev_alloc_skb_ip_align(ndev, frame_len); 1080dd07709SNoam Camus if (unlikely(!skb)) { 1090dd07709SNoam Camus ndev->stats.rx_errors++; 1100dd07709SNoam Camus ndev->stats.rx_dropped++; 1110dd07709SNoam Camus goto rx_irq_clean; 1120dd07709SNoam Camus } 1130dd07709SNoam Camus 1140dd07709SNoam Camus /* Copy frame from Rx fifo into the skb */ 1150dd07709SNoam Camus nps_enet_read_rx_fifo(ndev, skb->data, frame_len); 1160dd07709SNoam Camus 1170dd07709SNoam Camus skb_put(skb, frame_len); 1180dd07709SNoam Camus skb->protocol = eth_type_trans(skb, ndev); 1190dd07709SNoam Camus skb->ip_summed = CHECKSUM_UNNECESSARY; 1200dd07709SNoam Camus 1210dd07709SNoam Camus ndev->stats.rx_packets++; 1220dd07709SNoam Camus ndev->stats.rx_bytes += frame_len; 1230dd07709SNoam Camus netif_receive_skb(skb); 1240dd07709SNoam Camus 1250dd07709SNoam Camus goto rx_irq_frame_done; 1260dd07709SNoam Camus 1270dd07709SNoam Camus rx_irq_clean: 1280dd07709SNoam Camus /* Clean Rx fifo */ 1290dd07709SNoam Camus nps_enet_clean_rx_fifo(ndev, frame_len); 1300dd07709SNoam Camus 1310dd07709SNoam Camus rx_irq_frame_done: 1320dd07709SNoam Camus /* Ack Rx ctrl register */ 1330dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_RX_CTL, 0); 1340dd07709SNoam Camus 1350dd07709SNoam Camus return work_done; 1360dd07709SNoam Camus } 1370dd07709SNoam Camus 1380dd07709SNoam Camus static void nps_enet_tx_handler(struct net_device *ndev) 1390dd07709SNoam Camus { 1400dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 141b54b8c2dSLada Trimasova u32 tx_ctrl_value = nps_enet_reg_get(priv, NPS_ENET_REG_TX_CTL); 142b54b8c2dSLada Trimasova u32 tx_ctrl_et = (tx_ctrl_value & TX_CTL_ET_MASK) >> TX_CTL_ET_SHIFT; 143b54b8c2dSLada Trimasova u32 tx_ctrl_nt = (tx_ctrl_value & TX_CTL_NT_MASK) >> TX_CTL_NT_SHIFT; 1440dd07709SNoam Camus 1450dd07709SNoam Camus /* Check if we got TX */ 146094f57aaSElad Kanfi if (!nps_enet_is_tx_pending(priv)) 1470dd07709SNoam Camus return; 1480dd07709SNoam Camus 1493d99b74aSNoam Camus /* Ack Tx ctrl register */ 1503d99b74aSNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_TX_CTL, 0); 1513d99b74aSNoam Camus 1520dd07709SNoam Camus /* Check Tx transmit error */ 153b54b8c2dSLada Trimasova if (unlikely(tx_ctrl_et)) { 1540dd07709SNoam Camus ndev->stats.tx_errors++; 1550dd07709SNoam Camus } else { 1560dd07709SNoam Camus ndev->stats.tx_packets++; 157b54b8c2dSLada Trimasova ndev->stats.tx_bytes += tx_ctrl_nt; 1580dd07709SNoam Camus } 1590dd07709SNoam Camus 1600dd07709SNoam Camus dev_kfree_skb(priv->tx_skb); 161e5df49d5SElad Kanfi priv->tx_skb = NULL; 1620dd07709SNoam Camus 1630dd07709SNoam Camus if (netif_queue_stopped(ndev)) 1640dd07709SNoam Camus netif_wake_queue(ndev); 1650dd07709SNoam Camus } 1660dd07709SNoam Camus 1670dd07709SNoam Camus /** 1680dd07709SNoam Camus * nps_enet_poll - NAPI poll handler. 1690dd07709SNoam Camus * @napi: Pointer to napi_struct structure. 1700dd07709SNoam Camus * @budget: How many frames to process on one call. 1710dd07709SNoam Camus * 1720dd07709SNoam Camus * returns: Number of processed frames 1730dd07709SNoam Camus */ 1740dd07709SNoam Camus static int nps_enet_poll(struct napi_struct *napi, int budget) 1750dd07709SNoam Camus { 1760dd07709SNoam Camus struct net_device *ndev = napi->dev; 1770dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 1780dd07709SNoam Camus u32 work_done; 1790dd07709SNoam Camus 1800dd07709SNoam Camus nps_enet_tx_handler(ndev); 1810dd07709SNoam Camus work_done = nps_enet_rx_handler(ndev); 182358e78b5SZakharov Vlad if ((work_done < budget) && napi_complete_done(napi, work_done)) { 183b54b8c2dSLada Trimasova u32 buf_int_enable_value = 0; 18441493795SNoam Camus 185b54b8c2dSLada Trimasova /* set tx_done and rx_rdy bits */ 186b54b8c2dSLada Trimasova buf_int_enable_value |= NPS_ENET_ENABLE << RX_RDY_SHIFT; 187b54b8c2dSLada Trimasova buf_int_enable_value |= NPS_ENET_ENABLE << TX_DONE_SHIFT; 188b54b8c2dSLada Trimasova 1890dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_BUF_INT_ENABLE, 190b54b8c2dSLada Trimasova buf_int_enable_value); 19105c00d82SElad Kanfi 19205c00d82SElad Kanfi /* in case we will get a tx interrupt while interrupts 19305c00d82SElad Kanfi * are masked, we will lose it since the tx is edge interrupt. 19405c00d82SElad Kanfi * specifically, while executing the code section above, 19505c00d82SElad Kanfi * between nps_enet_tx_handler and the interrupts enable, all 19605c00d82SElad Kanfi * tx requests will be stuck until we will get an rx interrupt. 19705c00d82SElad Kanfi * the two code lines below will solve this situation by 19805c00d82SElad Kanfi * re-adding ourselves to the poll list. 19905c00d82SElad Kanfi */ 200094f57aaSElad Kanfi if (nps_enet_is_tx_pending(priv)) { 20186651650SElad Kanfi nps_enet_reg_set(priv, NPS_ENET_REG_BUF_INT_ENABLE, 0); 20205c00d82SElad Kanfi napi_reschedule(napi); 2030dd07709SNoam Camus } 20486651650SElad Kanfi } 2050dd07709SNoam Camus 2060dd07709SNoam Camus return work_done; 2070dd07709SNoam Camus } 2080dd07709SNoam Camus 2090dd07709SNoam Camus /** 2100dd07709SNoam Camus * nps_enet_irq_handler - Global interrupt handler for ENET. 2110dd07709SNoam Camus * @irq: irq number. 2120dd07709SNoam Camus * @dev_instance: device instance. 2130dd07709SNoam Camus * 2140dd07709SNoam Camus * returns: IRQ_HANDLED for all cases. 2150dd07709SNoam Camus * 2160dd07709SNoam Camus * EZchip ENET has 2 interrupt causes, and depending on bits raised in 2170dd07709SNoam Camus * CTRL registers we may tell what is a reason for interrupt to fire up. 2180dd07709SNoam Camus * We got one for RX and the other for TX (completion). 2190dd07709SNoam Camus */ 2200dd07709SNoam Camus static irqreturn_t nps_enet_irq_handler(s32 irq, void *dev_instance) 2210dd07709SNoam Camus { 2220dd07709SNoam Camus struct net_device *ndev = dev_instance; 2230dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 224b54b8c2dSLada Trimasova u32 rx_ctrl_value = nps_enet_reg_get(priv, NPS_ENET_REG_RX_CTL); 225b54b8c2dSLada Trimasova u32 rx_ctrl_cr = (rx_ctrl_value & RX_CTL_CR_MASK) >> RX_CTL_CR_SHIFT; 2260dd07709SNoam Camus 227094f57aaSElad Kanfi if (nps_enet_is_tx_pending(priv) || rx_ctrl_cr) 2280dd07709SNoam Camus if (likely(napi_schedule_prep(&priv->napi))) { 2290dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_BUF_INT_ENABLE, 0); 2300dd07709SNoam Camus __napi_schedule(&priv->napi); 2310dd07709SNoam Camus } 2320dd07709SNoam Camus 2330dd07709SNoam Camus return IRQ_HANDLED; 2340dd07709SNoam Camus } 2350dd07709SNoam Camus 2360dd07709SNoam Camus static void nps_enet_set_hw_mac_address(struct net_device *ndev) 2370dd07709SNoam Camus { 2380dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 239b54b8c2dSLada Trimasova u32 ge_mac_cfg_1_value = 0; 240b54b8c2dSLada Trimasova u32 *ge_mac_cfg_2_value = &priv->ge_mac_cfg_2_value; 2410dd07709SNoam Camus 2420dd07709SNoam Camus /* set MAC address in HW */ 243b54b8c2dSLada Trimasova ge_mac_cfg_1_value |= ndev->dev_addr[0] << CFG_1_OCTET_0_SHIFT; 244b54b8c2dSLada Trimasova ge_mac_cfg_1_value |= ndev->dev_addr[1] << CFG_1_OCTET_1_SHIFT; 245b54b8c2dSLada Trimasova ge_mac_cfg_1_value |= ndev->dev_addr[2] << CFG_1_OCTET_2_SHIFT; 246b54b8c2dSLada Trimasova ge_mac_cfg_1_value |= ndev->dev_addr[3] << CFG_1_OCTET_3_SHIFT; 247b54b8c2dSLada Trimasova *ge_mac_cfg_2_value = (*ge_mac_cfg_2_value & ~CFG_2_OCTET_4_MASK) 248b54b8c2dSLada Trimasova | ndev->dev_addr[4] << CFG_2_OCTET_4_SHIFT; 249b54b8c2dSLada Trimasova *ge_mac_cfg_2_value = (*ge_mac_cfg_2_value & ~CFG_2_OCTET_5_MASK) 250b54b8c2dSLada Trimasova | ndev->dev_addr[5] << CFG_2_OCTET_5_SHIFT; 2510dd07709SNoam Camus 2520dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_1, 253b54b8c2dSLada Trimasova ge_mac_cfg_1_value); 2540dd07709SNoam Camus 2550dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_2, 256b54b8c2dSLada Trimasova *ge_mac_cfg_2_value); 2570dd07709SNoam Camus } 2580dd07709SNoam Camus 2590dd07709SNoam Camus /** 2600dd07709SNoam Camus * nps_enet_hw_reset - Reset the network device. 2610dd07709SNoam Camus * @ndev: Pointer to the network device. 2620dd07709SNoam Camus * 2630dd07709SNoam Camus * This function reset the PCS and TX fifo. 2640dd07709SNoam Camus * The programming model is to set the relevant reset bits 2650dd07709SNoam Camus * wait for some time for this to propagate and then unset 2660dd07709SNoam Camus * the reset bits. This way we ensure that reset procedure 2670dd07709SNoam Camus * is done successfully by device. 2680dd07709SNoam Camus */ 2690dd07709SNoam Camus static void nps_enet_hw_reset(struct net_device *ndev) 2700dd07709SNoam Camus { 2710dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 272b54b8c2dSLada Trimasova u32 ge_rst_value = 0, phase_fifo_ctl_value = 0; 2730dd07709SNoam Camus 2740dd07709SNoam Camus /* Pcs reset sequence*/ 275b54b8c2dSLada Trimasova ge_rst_value |= NPS_ENET_ENABLE << RST_GMAC_0_SHIFT; 276b54b8c2dSLada Trimasova nps_enet_reg_set(priv, NPS_ENET_REG_GE_RST, ge_rst_value); 2770dd07709SNoam Camus usleep_range(10, 20); 278136ab0d0SNoam Camus ge_rst_value = 0; 279b54b8c2dSLada Trimasova nps_enet_reg_set(priv, NPS_ENET_REG_GE_RST, ge_rst_value); 2800dd07709SNoam Camus 2810dd07709SNoam Camus /* Tx fifo reset sequence */ 282b54b8c2dSLada Trimasova phase_fifo_ctl_value |= NPS_ENET_ENABLE << PHASE_FIFO_CTL_RST_SHIFT; 283b54b8c2dSLada Trimasova phase_fifo_ctl_value |= NPS_ENET_ENABLE << PHASE_FIFO_CTL_INIT_SHIFT; 2840dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_PHASE_FIFO_CTL, 285b54b8c2dSLada Trimasova phase_fifo_ctl_value); 2860dd07709SNoam Camus usleep_range(10, 20); 287b54b8c2dSLada Trimasova phase_fifo_ctl_value = 0; 2880dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_PHASE_FIFO_CTL, 289b54b8c2dSLada Trimasova phase_fifo_ctl_value); 2900dd07709SNoam Camus } 2910dd07709SNoam Camus 2920dd07709SNoam Camus static void nps_enet_hw_enable_control(struct net_device *ndev) 2930dd07709SNoam Camus { 2940dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 295b54b8c2dSLada Trimasova u32 ge_mac_cfg_0_value = 0, buf_int_enable_value = 0; 296b54b8c2dSLada Trimasova u32 *ge_mac_cfg_2_value = &priv->ge_mac_cfg_2_value; 297b54b8c2dSLada Trimasova u32 *ge_mac_cfg_3_value = &priv->ge_mac_cfg_3_value; 2980dd07709SNoam Camus s32 max_frame_length; 2990dd07709SNoam Camus 3000dd07709SNoam Camus /* Enable Rx and Tx statistics */ 301b54b8c2dSLada Trimasova *ge_mac_cfg_2_value = (*ge_mac_cfg_2_value & ~CFG_2_STAT_EN_MASK) 302b54b8c2dSLada Trimasova | NPS_ENET_GE_MAC_CFG_2_STAT_EN << CFG_2_STAT_EN_SHIFT; 3030dd07709SNoam Camus 3040dd07709SNoam Camus /* Discard packets with different MAC address */ 305b54b8c2dSLada Trimasova *ge_mac_cfg_2_value = (*ge_mac_cfg_2_value & ~CFG_2_DISK_DA_MASK) 306b54b8c2dSLada Trimasova | NPS_ENET_ENABLE << CFG_2_DISK_DA_SHIFT; 3070dd07709SNoam Camus 3080dd07709SNoam Camus /* Discard multicast packets */ 309b54b8c2dSLada Trimasova *ge_mac_cfg_2_value = (*ge_mac_cfg_2_value & ~CFG_2_DISK_MC_MASK) 310b54b8c2dSLada Trimasova | NPS_ENET_ENABLE << CFG_2_DISK_MC_SHIFT; 3110dd07709SNoam Camus 3120dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_2, 313b54b8c2dSLada Trimasova *ge_mac_cfg_2_value); 3140dd07709SNoam Camus 3150dd07709SNoam Camus /* Discard Packets bigger than max frame length */ 3160dd07709SNoam Camus max_frame_length = ETH_HLEN + ndev->mtu + ETH_FCS_LEN; 317b54b8c2dSLada Trimasova if (max_frame_length <= NPS_ENET_MAX_FRAME_LENGTH) { 318b54b8c2dSLada Trimasova *ge_mac_cfg_3_value = 319b54b8c2dSLada Trimasova (*ge_mac_cfg_3_value & ~CFG_3_MAX_LEN_MASK) 320b54b8c2dSLada Trimasova | max_frame_length << CFG_3_MAX_LEN_SHIFT; 321b54b8c2dSLada Trimasova } 3220dd07709SNoam Camus 3230dd07709SNoam Camus /* Enable interrupts */ 324b54b8c2dSLada Trimasova buf_int_enable_value |= NPS_ENET_ENABLE << RX_RDY_SHIFT; 325b54b8c2dSLada Trimasova buf_int_enable_value |= NPS_ENET_ENABLE << TX_DONE_SHIFT; 3260dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_BUF_INT_ENABLE, 327b54b8c2dSLada Trimasova buf_int_enable_value); 3280dd07709SNoam Camus 3290dd07709SNoam Camus /* Write device MAC address to HW */ 3300dd07709SNoam Camus nps_enet_set_hw_mac_address(ndev); 3310dd07709SNoam Camus 3320dd07709SNoam Camus /* Rx and Tx HW features */ 333b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_TX_PAD_EN_SHIFT; 334b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_TX_CRC_EN_SHIFT; 335b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_RX_CRC_STRIP_SHIFT; 3360dd07709SNoam Camus 3370dd07709SNoam Camus /* IFG configuration */ 338b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= 339b54b8c2dSLada Trimasova NPS_ENET_GE_MAC_CFG_0_RX_IFG << CFG_0_RX_IFG_SHIFT; 340b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= 341b54b8c2dSLada Trimasova NPS_ENET_GE_MAC_CFG_0_TX_IFG << CFG_0_TX_IFG_SHIFT; 3420dd07709SNoam Camus 3430dd07709SNoam Camus /* preamble configuration */ 344b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_RX_PR_CHECK_EN_SHIFT; 345b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= 346b54b8c2dSLada Trimasova NPS_ENET_GE_MAC_CFG_0_TX_PR_LEN << CFG_0_TX_PR_LEN_SHIFT; 3470dd07709SNoam Camus 3480dd07709SNoam Camus /* enable flow control frames */ 349b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_TX_FC_EN_SHIFT; 350b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_RX_FC_EN_SHIFT; 351b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= 352b54b8c2dSLada Trimasova NPS_ENET_GE_MAC_CFG_0_TX_FC_RETR << CFG_0_TX_FC_RETR_SHIFT; 353b54b8c2dSLada Trimasova *ge_mac_cfg_3_value = (*ge_mac_cfg_3_value & ~CFG_3_CF_DROP_MASK) 354b54b8c2dSLada Trimasova | NPS_ENET_ENABLE << CFG_3_CF_DROP_SHIFT; 3550dd07709SNoam Camus 3560dd07709SNoam Camus /* Enable Rx and Tx */ 357b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_RX_EN_SHIFT; 358b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_TX_EN_SHIFT; 3590dd07709SNoam Camus 360de671567SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_3, 361b54b8c2dSLada Trimasova *ge_mac_cfg_3_value); 3620dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_0, 363b54b8c2dSLada Trimasova ge_mac_cfg_0_value); 3640dd07709SNoam Camus } 3650dd07709SNoam Camus 3660dd07709SNoam Camus static void nps_enet_hw_disable_control(struct net_device *ndev) 3670dd07709SNoam Camus { 3680dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 3690dd07709SNoam Camus 3700dd07709SNoam Camus /* Disable interrupts */ 3710dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_BUF_INT_ENABLE, 0); 3720dd07709SNoam Camus 3730dd07709SNoam Camus /* Disable Rx and Tx */ 3740dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_0, 0); 3750dd07709SNoam Camus } 3760dd07709SNoam Camus 3770dd07709SNoam Camus static void nps_enet_send_frame(struct net_device *ndev, 3780dd07709SNoam Camus struct sk_buff *skb) 3790dd07709SNoam Camus { 3800dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 381b54b8c2dSLada Trimasova u32 tx_ctrl_value = 0; 3820dd07709SNoam Camus short length = skb->len; 3830dd07709SNoam Camus u32 i, len = DIV_ROUND_UP(length, sizeof(u32)); 384b0a8d1a0SArnd Bergmann u32 *src = (void *)skb->data; 3850dd07709SNoam Camus bool src_is_aligned = IS_ALIGNED((unsigned long)src, sizeof(u32)); 3860dd07709SNoam Camus 3870dd07709SNoam Camus /* In case src is not aligned we need an intermediate buffer */ 3880dd07709SNoam Camus if (src_is_aligned) 389b54b8c2dSLada Trimasova iowrite32_rep(priv->regs_base + NPS_ENET_REG_TX_BUF, src, len); 390b0a8d1a0SArnd Bergmann else /* !src_is_aligned */ 391b0a8d1a0SArnd Bergmann for (i = 0; i < len; i++, src++) 392b0a8d1a0SArnd Bergmann nps_enet_reg_set(priv, NPS_ENET_REG_TX_BUF, 393b54b8c2dSLada Trimasova get_unaligned_be32(src)); 3940dd07709SNoam Camus 3950dd07709SNoam Camus /* Write the length of the Frame */ 396b54b8c2dSLada Trimasova tx_ctrl_value |= length << TX_CTL_NT_SHIFT; 3970dd07709SNoam Camus 398b54b8c2dSLada Trimasova tx_ctrl_value |= NPS_ENET_ENABLE << TX_CTL_CT_SHIFT; 3990dd07709SNoam Camus /* Send Frame */ 400b54b8c2dSLada Trimasova nps_enet_reg_set(priv, NPS_ENET_REG_TX_CTL, tx_ctrl_value); 4010dd07709SNoam Camus } 4020dd07709SNoam Camus 4030dd07709SNoam Camus /** 4040dd07709SNoam Camus * nps_enet_set_mac_address - Set the MAC address for this device. 4050dd07709SNoam Camus * @ndev: Pointer to net_device structure. 4060dd07709SNoam Camus * @p: 6 byte Address to be written as MAC address. 4070dd07709SNoam Camus * 4080dd07709SNoam Camus * This function copies the HW address from the sockaddr structure to the 4090dd07709SNoam Camus * net_device structure and updates the address in HW. 4100dd07709SNoam Camus * 4110dd07709SNoam Camus * returns: -EBUSY if the net device is busy or 0 if the address is set 4120dd07709SNoam Camus * successfully. 4130dd07709SNoam Camus */ 4140dd07709SNoam Camus static s32 nps_enet_set_mac_address(struct net_device *ndev, void *p) 4150dd07709SNoam Camus { 4160dd07709SNoam Camus struct sockaddr *addr = p; 4170dd07709SNoam Camus s32 res; 4180dd07709SNoam Camus 4190dd07709SNoam Camus if (netif_running(ndev)) 4200dd07709SNoam Camus return -EBUSY; 4210dd07709SNoam Camus 4220dd07709SNoam Camus res = eth_mac_addr(ndev, p); 4230dd07709SNoam Camus if (!res) { 4240dd07709SNoam Camus ether_addr_copy(ndev->dev_addr, addr->sa_data); 4250dd07709SNoam Camus nps_enet_set_hw_mac_address(ndev); 4260dd07709SNoam Camus } 4270dd07709SNoam Camus 4280dd07709SNoam Camus return res; 4290dd07709SNoam Camus } 4300dd07709SNoam Camus 4310dd07709SNoam Camus /** 4320dd07709SNoam Camus * nps_enet_set_rx_mode - Change the receive filtering mode. 4330dd07709SNoam Camus * @ndev: Pointer to the network device. 4340dd07709SNoam Camus * 4350dd07709SNoam Camus * This function enables/disables promiscuous mode 4360dd07709SNoam Camus */ 4370dd07709SNoam Camus static void nps_enet_set_rx_mode(struct net_device *ndev) 4380dd07709SNoam Camus { 4390dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 440b54b8c2dSLada Trimasova u32 ge_mac_cfg_2_value = priv->ge_mac_cfg_2_value; 4410dd07709SNoam Camus 4420dd07709SNoam Camus if (ndev->flags & IFF_PROMISC) { 443b54b8c2dSLada Trimasova ge_mac_cfg_2_value = (ge_mac_cfg_2_value & ~CFG_2_DISK_DA_MASK) 444b54b8c2dSLada Trimasova | NPS_ENET_DISABLE << CFG_2_DISK_DA_SHIFT; 445b54b8c2dSLada Trimasova ge_mac_cfg_2_value = (ge_mac_cfg_2_value & ~CFG_2_DISK_MC_MASK) 446b54b8c2dSLada Trimasova | NPS_ENET_DISABLE << CFG_2_DISK_MC_SHIFT; 447b54b8c2dSLada Trimasova 4480dd07709SNoam Camus } else { 449b54b8c2dSLada Trimasova ge_mac_cfg_2_value = (ge_mac_cfg_2_value & ~CFG_2_DISK_DA_MASK) 450b54b8c2dSLada Trimasova | NPS_ENET_ENABLE << CFG_2_DISK_DA_SHIFT; 451b54b8c2dSLada Trimasova ge_mac_cfg_2_value = (ge_mac_cfg_2_value & ~CFG_2_DISK_MC_MASK) 452b54b8c2dSLada Trimasova | NPS_ENET_ENABLE << CFG_2_DISK_MC_SHIFT; 4530dd07709SNoam Camus } 4540dd07709SNoam Camus 455b54b8c2dSLada Trimasova nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_2, ge_mac_cfg_2_value); 4560dd07709SNoam Camus } 4570dd07709SNoam Camus 4580dd07709SNoam Camus /** 4590dd07709SNoam Camus * nps_enet_open - Open the network device. 4600dd07709SNoam Camus * @ndev: Pointer to the network device. 4610dd07709SNoam Camus * 4620dd07709SNoam Camus * returns: 0, on success or non-zero error value on failure. 4630dd07709SNoam Camus * 4640dd07709SNoam Camus * This function sets the MAC address, requests and enables an IRQ 4650dd07709SNoam Camus * for the ENET device and starts the Tx queue. 4660dd07709SNoam Camus */ 4670dd07709SNoam Camus static s32 nps_enet_open(struct net_device *ndev) 4680dd07709SNoam Camus { 4690dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 4700dd07709SNoam Camus s32 err; 4710dd07709SNoam Camus 4720dd07709SNoam Camus /* Reset private variables */ 473e5df49d5SElad Kanfi priv->tx_skb = NULL; 474b54b8c2dSLada Trimasova priv->ge_mac_cfg_2_value = 0; 475b54b8c2dSLada Trimasova priv->ge_mac_cfg_3_value = 0; 4760dd07709SNoam Camus 4770dd07709SNoam Camus /* ge_mac_cfg_3 default values */ 478b54b8c2dSLada Trimasova priv->ge_mac_cfg_3_value |= 479b54b8c2dSLada Trimasova NPS_ENET_GE_MAC_CFG_3_RX_IFG_TH << CFG_3_RX_IFG_TH_SHIFT; 480b54b8c2dSLada Trimasova 481b54b8c2dSLada Trimasova priv->ge_mac_cfg_3_value |= 482b54b8c2dSLada Trimasova NPS_ENET_GE_MAC_CFG_3_MAX_LEN << CFG_3_MAX_LEN_SHIFT; 4830dd07709SNoam Camus 4840dd07709SNoam Camus /* Disable HW device */ 4850dd07709SNoam Camus nps_enet_hw_disable_control(ndev); 4860dd07709SNoam Camus 4870dd07709SNoam Camus /* irq Rx allocation */ 4880dd07709SNoam Camus err = request_irq(priv->irq, nps_enet_irq_handler, 4890dd07709SNoam Camus 0, "enet-rx-tx", ndev); 4900dd07709SNoam Camus if (err) 4910dd07709SNoam Camus return err; 4920dd07709SNoam Camus 4930dd07709SNoam Camus napi_enable(&priv->napi); 4940dd07709SNoam Camus 4950dd07709SNoam Camus /* Enable HW device */ 4960dd07709SNoam Camus nps_enet_hw_reset(ndev); 4970dd07709SNoam Camus nps_enet_hw_enable_control(ndev); 4980dd07709SNoam Camus 4990dd07709SNoam Camus netif_start_queue(ndev); 5000dd07709SNoam Camus 5010dd07709SNoam Camus return 0; 5020dd07709SNoam Camus } 5030dd07709SNoam Camus 5040dd07709SNoam Camus /** 5050dd07709SNoam Camus * nps_enet_stop - Close the network device. 5060dd07709SNoam Camus * @ndev: Pointer to the network device. 5070dd07709SNoam Camus * 5080dd07709SNoam Camus * This function stops the Tx queue, disables interrupts for the ENET device. 5090dd07709SNoam Camus */ 5100dd07709SNoam Camus static s32 nps_enet_stop(struct net_device *ndev) 5110dd07709SNoam Camus { 5120dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 5130dd07709SNoam Camus 5140dd07709SNoam Camus napi_disable(&priv->napi); 5150dd07709SNoam Camus netif_stop_queue(ndev); 5160dd07709SNoam Camus nps_enet_hw_disable_control(ndev); 5170dd07709SNoam Camus free_irq(priv->irq, ndev); 5180dd07709SNoam Camus 5190dd07709SNoam Camus return 0; 5200dd07709SNoam Camus } 5210dd07709SNoam Camus 5220dd07709SNoam Camus /** 5230dd07709SNoam Camus * nps_enet_start_xmit - Starts the data transmission. 5240dd07709SNoam Camus * @skb: sk_buff pointer that contains data to be Transmitted. 5250dd07709SNoam Camus * @ndev: Pointer to net_device structure. 5260dd07709SNoam Camus * 5270dd07709SNoam Camus * returns: NETDEV_TX_OK, on success 5280dd07709SNoam Camus * NETDEV_TX_BUSY, if any of the descriptors are not free. 5290dd07709SNoam Camus * 5300dd07709SNoam Camus * This function is invoked from upper layers to initiate transmission. 5310dd07709SNoam Camus */ 5320dd07709SNoam Camus static netdev_tx_t nps_enet_start_xmit(struct sk_buff *skb, 5330dd07709SNoam Camus struct net_device *ndev) 5340dd07709SNoam Camus { 5350dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 5360dd07709SNoam Camus 5370dd07709SNoam Camus /* This driver handles one frame at a time */ 5380dd07709SNoam Camus netif_stop_queue(ndev); 5390dd07709SNoam Camus 5400dd07709SNoam Camus priv->tx_skb = skb; 5410dd07709SNoam Camus 542e5df49d5SElad Kanfi /* make sure tx_skb is actually written to the memory 543e5df49d5SElad Kanfi * before the HW is informed and the IRQ is fired. 544e5df49d5SElad Kanfi */ 545e5df49d5SElad Kanfi wmb(); 546e5df49d5SElad Kanfi 54793fcf83eSNoam Camus nps_enet_send_frame(ndev, skb); 54893fcf83eSNoam Camus 5490dd07709SNoam Camus return NETDEV_TX_OK; 5500dd07709SNoam Camus } 5510dd07709SNoam Camus 5520dd07709SNoam Camus #ifdef CONFIG_NET_POLL_CONTROLLER 5530dd07709SNoam Camus static void nps_enet_poll_controller(struct net_device *ndev) 5540dd07709SNoam Camus { 5550dd07709SNoam Camus disable_irq(ndev->irq); 5560dd07709SNoam Camus nps_enet_irq_handler(ndev->irq, ndev); 5570dd07709SNoam Camus enable_irq(ndev->irq); 5580dd07709SNoam Camus } 5590dd07709SNoam Camus #endif 5600dd07709SNoam Camus 5610dd07709SNoam Camus static const struct net_device_ops nps_netdev_ops = { 5620dd07709SNoam Camus .ndo_open = nps_enet_open, 5630dd07709SNoam Camus .ndo_stop = nps_enet_stop, 5640dd07709SNoam Camus .ndo_start_xmit = nps_enet_start_xmit, 5650dd07709SNoam Camus .ndo_set_mac_address = nps_enet_set_mac_address, 5660dd07709SNoam Camus .ndo_set_rx_mode = nps_enet_set_rx_mode, 5670dd07709SNoam Camus #ifdef CONFIG_NET_POLL_CONTROLLER 5680dd07709SNoam Camus .ndo_poll_controller = nps_enet_poll_controller, 5690dd07709SNoam Camus #endif 5700dd07709SNoam Camus }; 5710dd07709SNoam Camus 5720dd07709SNoam Camus static s32 nps_enet_probe(struct platform_device *pdev) 5730dd07709SNoam Camus { 5740dd07709SNoam Camus struct device *dev = &pdev->dev; 5750dd07709SNoam Camus struct net_device *ndev; 5760dd07709SNoam Camus struct nps_enet_priv *priv; 5770dd07709SNoam Camus s32 err = 0; 5780dd07709SNoam Camus const char *mac_addr; 5790dd07709SNoam Camus 5800dd07709SNoam Camus if (!dev->of_node) 5810dd07709SNoam Camus return -ENODEV; 5820dd07709SNoam Camus 5830dd07709SNoam Camus ndev = alloc_etherdev(sizeof(struct nps_enet_priv)); 5840dd07709SNoam Camus if (!ndev) 5850dd07709SNoam Camus return -ENOMEM; 5860dd07709SNoam Camus 5870dd07709SNoam Camus platform_set_drvdata(pdev, ndev); 5880dd07709SNoam Camus SET_NETDEV_DEV(ndev, dev); 5890dd07709SNoam Camus priv = netdev_priv(ndev); 5900dd07709SNoam Camus 5910dd07709SNoam Camus /* The EZ NET specific entries in the device structure. */ 5920dd07709SNoam Camus ndev->netdev_ops = &nps_netdev_ops; 5930dd07709SNoam Camus ndev->watchdog_timeo = (400 * HZ / 1000); 5940dd07709SNoam Camus /* FIXME :: no multicast support yet */ 5950dd07709SNoam Camus ndev->flags &= ~IFF_MULTICAST; 5960dd07709SNoam Camus 597*b6df9830SYueHaibing priv->regs_base = devm_platform_ioremap_resource(pdev, 0); 5980dd07709SNoam Camus if (IS_ERR(priv->regs_base)) { 5990dd07709SNoam Camus err = PTR_ERR(priv->regs_base); 6000dd07709SNoam Camus goto out_netdev; 6010dd07709SNoam Camus } 6020dd07709SNoam Camus dev_dbg(dev, "Registers base address is 0x%p\n", priv->regs_base); 6030dd07709SNoam Camus 6040dd07709SNoam Camus /* set kernel MAC address to dev */ 6050dd07709SNoam Camus mac_addr = of_get_mac_address(dev->of_node); 606a51645f7SPetr Štetiar if (!IS_ERR(mac_addr)) 6070dd07709SNoam Camus ether_addr_copy(ndev->dev_addr, mac_addr); 6080dd07709SNoam Camus else 6090dd07709SNoam Camus eth_hw_addr_random(ndev); 6100dd07709SNoam Camus 6110dd07709SNoam Camus /* Get IRQ number */ 6120dd07709SNoam Camus priv->irq = platform_get_irq(pdev, 0); 6130dd07709SNoam Camus if (!priv->irq) { 6140dd07709SNoam Camus dev_err(dev, "failed to retrieve <irq Rx-Tx> value from device tree\n"); 6150dd07709SNoam Camus err = -ENODEV; 6160dd07709SNoam Camus goto out_netdev; 6170dd07709SNoam Camus } 6180dd07709SNoam Camus 6190dd07709SNoam Camus netif_napi_add(ndev, &priv->napi, nps_enet_poll, 6200dd07709SNoam Camus NPS_ENET_NAPI_POLL_WEIGHT); 6210dd07709SNoam Camus 6220dd07709SNoam Camus /* Register the driver. Should be the last thing in probe */ 6230dd07709SNoam Camus err = register_netdev(ndev); 6240dd07709SNoam Camus if (err) { 6250dd07709SNoam Camus dev_err(dev, "Failed to register ndev for %s, err = 0x%08x\n", 6260dd07709SNoam Camus ndev->name, (s32)err); 6270dd07709SNoam Camus goto out_netif_api; 6280dd07709SNoam Camus } 6290dd07709SNoam Camus 6300dd07709SNoam Camus dev_info(dev, "(rx/tx=%d)\n", priv->irq); 6310dd07709SNoam Camus return 0; 6320dd07709SNoam Camus 6330dd07709SNoam Camus out_netif_api: 6340dd07709SNoam Camus netif_napi_del(&priv->napi); 6350dd07709SNoam Camus out_netdev: 6360dd07709SNoam Camus if (err) 6370dd07709SNoam Camus free_netdev(ndev); 6380dd07709SNoam Camus 6390dd07709SNoam Camus return err; 6400dd07709SNoam Camus } 6410dd07709SNoam Camus 6420dd07709SNoam Camus static s32 nps_enet_remove(struct platform_device *pdev) 6430dd07709SNoam Camus { 6440dd07709SNoam Camus struct net_device *ndev = platform_get_drvdata(pdev); 6450dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 6460dd07709SNoam Camus 6470dd07709SNoam Camus unregister_netdev(ndev); 6480dd07709SNoam Camus free_netdev(ndev); 6490dd07709SNoam Camus netif_napi_del(&priv->napi); 6500dd07709SNoam Camus 6510dd07709SNoam Camus return 0; 6520dd07709SNoam Camus } 6530dd07709SNoam Camus 6540dd07709SNoam Camus static const struct of_device_id nps_enet_dt_ids[] = { 6550dd07709SNoam Camus { .compatible = "ezchip,nps-mgt-enet" }, 6560dd07709SNoam Camus { /* Sentinel */ } 6570dd07709SNoam Camus }; 658fc971a2fSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, nps_enet_dt_ids); 6590dd07709SNoam Camus 6600dd07709SNoam Camus static struct platform_driver nps_enet_driver = { 6610dd07709SNoam Camus .probe = nps_enet_probe, 6620dd07709SNoam Camus .remove = nps_enet_remove, 6630dd07709SNoam Camus .driver = { 6640dd07709SNoam Camus .name = DRV_NAME, 6650dd07709SNoam Camus .of_match_table = nps_enet_dt_ids, 6660dd07709SNoam Camus }, 6670dd07709SNoam Camus }; 6680dd07709SNoam Camus 6690dd07709SNoam Camus module_platform_driver(nps_enet_driver); 6700dd07709SNoam Camus 6710dd07709SNoam Camus MODULE_AUTHOR("EZchip Semiconductor"); 6720dd07709SNoam Camus MODULE_LICENSE("GPL v2"); 673