10dd07709SNoam Camus /* 20dd07709SNoam Camus * Copyright(c) 2015 EZchip Technologies. 30dd07709SNoam Camus * 40dd07709SNoam Camus * This program is free software; you can redistribute it and/or modify it 50dd07709SNoam Camus * under the terms and conditions of the GNU General Public License, 60dd07709SNoam Camus * version 2, as published by the Free Software Foundation. 70dd07709SNoam Camus * 80dd07709SNoam Camus * This program is distributed in the hope it will be useful, but WITHOUT 90dd07709SNoam Camus * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 100dd07709SNoam Camus * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 110dd07709SNoam Camus * more details. 120dd07709SNoam Camus * 130dd07709SNoam Camus * The full GNU General Public License is included in this distribution in 140dd07709SNoam Camus * the file called "COPYING". 150dd07709SNoam Camus */ 160dd07709SNoam Camus 170dd07709SNoam Camus #include <linux/module.h> 180dd07709SNoam Camus #include <linux/etherdevice.h> 190dd07709SNoam Camus #include <linux/of_address.h> 200dd07709SNoam Camus #include <linux/of_irq.h> 210dd07709SNoam Camus #include <linux/of_net.h> 220dd07709SNoam Camus #include <linux/of_platform.h> 230dd07709SNoam Camus #include "nps_enet.h" 240dd07709SNoam Camus 250dd07709SNoam Camus #define DRV_NAME "nps_mgt_enet" 260dd07709SNoam Camus 270dd07709SNoam Camus static void nps_enet_clean_rx_fifo(struct net_device *ndev, u32 frame_len) 280dd07709SNoam Camus { 290dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 300dd07709SNoam Camus u32 i, len = DIV_ROUND_UP(frame_len, sizeof(u32)); 310dd07709SNoam Camus 320dd07709SNoam Camus /* Empty Rx FIFO buffer by reading all words */ 330dd07709SNoam Camus for (i = 0; i < len; i++) 340dd07709SNoam Camus nps_enet_reg_get(priv, NPS_ENET_REG_RX_BUF); 350dd07709SNoam Camus } 360dd07709SNoam Camus 370dd07709SNoam Camus static void nps_enet_read_rx_fifo(struct net_device *ndev, 380dd07709SNoam Camus unsigned char *dst, u32 length) 390dd07709SNoam Camus { 400dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 410dd07709SNoam Camus s32 i, last = length & (sizeof(u32) - 1); 420dd07709SNoam Camus u32 *reg = (u32 *)dst, len = length / sizeof(u32); 430dd07709SNoam Camus bool dst_is_aligned = IS_ALIGNED((unsigned long)dst, sizeof(u32)); 440dd07709SNoam Camus 450dd07709SNoam Camus /* In case dst is not aligned we need an intermediate buffer */ 46b54b8c2dSLada Trimasova if (dst_is_aligned) { 47b54b8c2dSLada Trimasova ioread32_rep(priv->regs_base + NPS_ENET_REG_RX_BUF, reg, len); 48b54b8c2dSLada Trimasova reg += len; 49*ddbff3e8SElad Kanfi } else { /* !dst_is_aligned */ 500dd07709SNoam Camus for (i = 0; i < len; i++, reg++) { 51b0a8d1a0SArnd Bergmann u32 buf = nps_enet_reg_get(priv, NPS_ENET_REG_RX_BUF); 52*ddbff3e8SElad Kanfi 53b54b8c2dSLada Trimasova put_unaligned_be32(buf, reg); 540dd07709SNoam Camus } 550dd07709SNoam Camus } 560dd07709SNoam Camus /* copy last bytes (if any) */ 570dd07709SNoam Camus if (last) { 58b54b8c2dSLada Trimasova u32 buf; 59*ddbff3e8SElad Kanfi 60b54b8c2dSLada Trimasova ioread32_rep(priv->regs_base + NPS_ENET_REG_RX_BUF, &buf, 1); 61b0a8d1a0SArnd Bergmann memcpy((u8 *)reg, &buf, last); 620dd07709SNoam Camus } 630dd07709SNoam Camus } 640dd07709SNoam Camus 650dd07709SNoam Camus static u32 nps_enet_rx_handler(struct net_device *ndev) 660dd07709SNoam Camus { 670dd07709SNoam Camus u32 frame_len, err = 0; 680dd07709SNoam Camus u32 work_done = 0; 690dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 700dd07709SNoam Camus struct sk_buff *skb; 71b54b8c2dSLada Trimasova u32 rx_ctrl_value = nps_enet_reg_get(priv, NPS_ENET_REG_RX_CTL); 72b54b8c2dSLada Trimasova u32 rx_ctrl_cr = (rx_ctrl_value & RX_CTL_CR_MASK) >> RX_CTL_CR_SHIFT; 73b54b8c2dSLada Trimasova u32 rx_ctrl_er = (rx_ctrl_value & RX_CTL_ER_MASK) >> RX_CTL_ER_SHIFT; 74b54b8c2dSLada Trimasova u32 rx_ctrl_crc = (rx_ctrl_value & RX_CTL_CRC_MASK) >> RX_CTL_CRC_SHIFT; 750dd07709SNoam Camus 76b54b8c2dSLada Trimasova frame_len = (rx_ctrl_value & RX_CTL_NR_MASK) >> RX_CTL_NR_SHIFT; 770dd07709SNoam Camus 780dd07709SNoam Camus /* Check if we got RX */ 79b54b8c2dSLada Trimasova if (!rx_ctrl_cr) 800dd07709SNoam Camus return work_done; 810dd07709SNoam Camus 820dd07709SNoam Camus /* If we got here there is a work for us */ 830dd07709SNoam Camus work_done++; 840dd07709SNoam Camus 850dd07709SNoam Camus /* Check Rx error */ 86b54b8c2dSLada Trimasova if (rx_ctrl_er) { 870dd07709SNoam Camus ndev->stats.rx_errors++; 880dd07709SNoam Camus err = 1; 890dd07709SNoam Camus } 900dd07709SNoam Camus 910dd07709SNoam Camus /* Check Rx CRC error */ 92b54b8c2dSLada Trimasova if (rx_ctrl_crc) { 930dd07709SNoam Camus ndev->stats.rx_crc_errors++; 940dd07709SNoam Camus ndev->stats.rx_dropped++; 950dd07709SNoam Camus err = 1; 960dd07709SNoam Camus } 970dd07709SNoam Camus 980dd07709SNoam Camus /* Check Frame length Min 64b */ 990dd07709SNoam Camus if (unlikely(frame_len < ETH_ZLEN)) { 1000dd07709SNoam Camus ndev->stats.rx_length_errors++; 1010dd07709SNoam Camus ndev->stats.rx_dropped++; 1020dd07709SNoam Camus err = 1; 1030dd07709SNoam Camus } 1040dd07709SNoam Camus 1050dd07709SNoam Camus if (err) 1060dd07709SNoam Camus goto rx_irq_clean; 1070dd07709SNoam Camus 1080dd07709SNoam Camus /* Skb allocation */ 1090dd07709SNoam Camus skb = netdev_alloc_skb_ip_align(ndev, frame_len); 1100dd07709SNoam Camus if (unlikely(!skb)) { 1110dd07709SNoam Camus ndev->stats.rx_errors++; 1120dd07709SNoam Camus ndev->stats.rx_dropped++; 1130dd07709SNoam Camus goto rx_irq_clean; 1140dd07709SNoam Camus } 1150dd07709SNoam Camus 1160dd07709SNoam Camus /* Copy frame from Rx fifo into the skb */ 1170dd07709SNoam Camus nps_enet_read_rx_fifo(ndev, skb->data, frame_len); 1180dd07709SNoam Camus 1190dd07709SNoam Camus skb_put(skb, frame_len); 1200dd07709SNoam Camus skb->protocol = eth_type_trans(skb, ndev); 1210dd07709SNoam Camus skb->ip_summed = CHECKSUM_UNNECESSARY; 1220dd07709SNoam Camus 1230dd07709SNoam Camus ndev->stats.rx_packets++; 1240dd07709SNoam Camus ndev->stats.rx_bytes += frame_len; 1250dd07709SNoam Camus netif_receive_skb(skb); 1260dd07709SNoam Camus 1270dd07709SNoam Camus goto rx_irq_frame_done; 1280dd07709SNoam Camus 1290dd07709SNoam Camus rx_irq_clean: 1300dd07709SNoam Camus /* Clean Rx fifo */ 1310dd07709SNoam Camus nps_enet_clean_rx_fifo(ndev, frame_len); 1320dd07709SNoam Camus 1330dd07709SNoam Camus rx_irq_frame_done: 1340dd07709SNoam Camus /* Ack Rx ctrl register */ 1350dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_RX_CTL, 0); 1360dd07709SNoam Camus 1370dd07709SNoam Camus return work_done; 1380dd07709SNoam Camus } 1390dd07709SNoam Camus 1400dd07709SNoam Camus static void nps_enet_tx_handler(struct net_device *ndev) 1410dd07709SNoam Camus { 1420dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 143b54b8c2dSLada Trimasova u32 tx_ctrl_value = nps_enet_reg_get(priv, NPS_ENET_REG_TX_CTL); 144b54b8c2dSLada Trimasova u32 tx_ctrl_ct = (tx_ctrl_value & TX_CTL_CT_MASK) >> TX_CTL_CT_SHIFT; 145b54b8c2dSLada Trimasova u32 tx_ctrl_et = (tx_ctrl_value & TX_CTL_ET_MASK) >> TX_CTL_ET_SHIFT; 146b54b8c2dSLada Trimasova u32 tx_ctrl_nt = (tx_ctrl_value & TX_CTL_NT_MASK) >> TX_CTL_NT_SHIFT; 1470dd07709SNoam Camus 1480dd07709SNoam Camus /* Check if we got TX */ 149e5df49d5SElad Kanfi if (!priv->tx_skb || tx_ctrl_ct) 1500dd07709SNoam Camus return; 1510dd07709SNoam Camus 1523d99b74aSNoam Camus /* Ack Tx ctrl register */ 1533d99b74aSNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_TX_CTL, 0); 1543d99b74aSNoam Camus 1550dd07709SNoam Camus /* Check Tx transmit error */ 156b54b8c2dSLada Trimasova if (unlikely(tx_ctrl_et)) { 1570dd07709SNoam Camus ndev->stats.tx_errors++; 1580dd07709SNoam Camus } else { 1590dd07709SNoam Camus ndev->stats.tx_packets++; 160b54b8c2dSLada Trimasova ndev->stats.tx_bytes += tx_ctrl_nt; 1610dd07709SNoam Camus } 1620dd07709SNoam Camus 1630dd07709SNoam Camus dev_kfree_skb(priv->tx_skb); 164e5df49d5SElad Kanfi priv->tx_skb = NULL; 1650dd07709SNoam Camus 1660dd07709SNoam Camus if (netif_queue_stopped(ndev)) 1670dd07709SNoam Camus netif_wake_queue(ndev); 1680dd07709SNoam Camus } 1690dd07709SNoam Camus 1700dd07709SNoam Camus /** 1710dd07709SNoam Camus * nps_enet_poll - NAPI poll handler. 1720dd07709SNoam Camus * @napi: Pointer to napi_struct structure. 1730dd07709SNoam Camus * @budget: How many frames to process on one call. 1740dd07709SNoam Camus * 1750dd07709SNoam Camus * returns: Number of processed frames 1760dd07709SNoam Camus */ 1770dd07709SNoam Camus static int nps_enet_poll(struct napi_struct *napi, int budget) 1780dd07709SNoam Camus { 1790dd07709SNoam Camus struct net_device *ndev = napi->dev; 1800dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 1810dd07709SNoam Camus u32 work_done; 1820dd07709SNoam Camus 1830dd07709SNoam Camus nps_enet_tx_handler(ndev); 1840dd07709SNoam Camus work_done = nps_enet_rx_handler(ndev); 1850dd07709SNoam Camus if (work_done < budget) { 186b54b8c2dSLada Trimasova u32 buf_int_enable_value = 0; 18705c00d82SElad Kanfi u32 tx_ctrl_value = nps_enet_reg_get(priv, NPS_ENET_REG_TX_CTL); 18805c00d82SElad Kanfi u32 tx_ctrl_ct = 18905c00d82SElad Kanfi (tx_ctrl_value & TX_CTL_CT_MASK) >> TX_CTL_CT_SHIFT; 19041493795SNoam Camus 1910dd07709SNoam Camus napi_complete(napi); 192b54b8c2dSLada Trimasova 193b54b8c2dSLada Trimasova /* set tx_done and rx_rdy bits */ 194b54b8c2dSLada Trimasova buf_int_enable_value |= NPS_ENET_ENABLE << RX_RDY_SHIFT; 195b54b8c2dSLada Trimasova buf_int_enable_value |= NPS_ENET_ENABLE << TX_DONE_SHIFT; 196b54b8c2dSLada Trimasova 1970dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_BUF_INT_ENABLE, 198b54b8c2dSLada Trimasova buf_int_enable_value); 19905c00d82SElad Kanfi 20005c00d82SElad Kanfi /* in case we will get a tx interrupt while interrupts 20105c00d82SElad Kanfi * are masked, we will lose it since the tx is edge interrupt. 20205c00d82SElad Kanfi * specifically, while executing the code section above, 20305c00d82SElad Kanfi * between nps_enet_tx_handler and the interrupts enable, all 20405c00d82SElad Kanfi * tx requests will be stuck until we will get an rx interrupt. 20505c00d82SElad Kanfi * the two code lines below will solve this situation by 20605c00d82SElad Kanfi * re-adding ourselves to the poll list. 20705c00d82SElad Kanfi */ 20805c00d82SElad Kanfi 20986651650SElad Kanfi if (priv->tx_skb && !tx_ctrl_ct) { 21086651650SElad Kanfi nps_enet_reg_set(priv, NPS_ENET_REG_BUF_INT_ENABLE, 0); 21105c00d82SElad Kanfi napi_reschedule(napi); 2120dd07709SNoam Camus } 21386651650SElad Kanfi } 2140dd07709SNoam Camus 2150dd07709SNoam Camus return work_done; 2160dd07709SNoam Camus } 2170dd07709SNoam Camus 2180dd07709SNoam Camus /** 2190dd07709SNoam Camus * nps_enet_irq_handler - Global interrupt handler for ENET. 2200dd07709SNoam Camus * @irq: irq number. 2210dd07709SNoam Camus * @dev_instance: device instance. 2220dd07709SNoam Camus * 2230dd07709SNoam Camus * returns: IRQ_HANDLED for all cases. 2240dd07709SNoam Camus * 2250dd07709SNoam Camus * EZchip ENET has 2 interrupt causes, and depending on bits raised in 2260dd07709SNoam Camus * CTRL registers we may tell what is a reason for interrupt to fire up. 2270dd07709SNoam Camus * We got one for RX and the other for TX (completion). 2280dd07709SNoam Camus */ 2290dd07709SNoam Camus static irqreturn_t nps_enet_irq_handler(s32 irq, void *dev_instance) 2300dd07709SNoam Camus { 2310dd07709SNoam Camus struct net_device *ndev = dev_instance; 2320dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 233b54b8c2dSLada Trimasova u32 rx_ctrl_value = nps_enet_reg_get(priv, NPS_ENET_REG_RX_CTL); 234b54b8c2dSLada Trimasova u32 tx_ctrl_value = nps_enet_reg_get(priv, NPS_ENET_REG_TX_CTL); 235b54b8c2dSLada Trimasova u32 tx_ctrl_ct = (tx_ctrl_value & TX_CTL_CT_MASK) >> TX_CTL_CT_SHIFT; 236b54b8c2dSLada Trimasova u32 rx_ctrl_cr = (rx_ctrl_value & RX_CTL_CR_MASK) >> RX_CTL_CR_SHIFT; 2370dd07709SNoam Camus 238e5df49d5SElad Kanfi if ((!tx_ctrl_ct && priv->tx_skb) || rx_ctrl_cr) 2390dd07709SNoam Camus if (likely(napi_schedule_prep(&priv->napi))) { 2400dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_BUF_INT_ENABLE, 0); 2410dd07709SNoam Camus __napi_schedule(&priv->napi); 2420dd07709SNoam Camus } 2430dd07709SNoam Camus 2440dd07709SNoam Camus return IRQ_HANDLED; 2450dd07709SNoam Camus } 2460dd07709SNoam Camus 2470dd07709SNoam Camus static void nps_enet_set_hw_mac_address(struct net_device *ndev) 2480dd07709SNoam Camus { 2490dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 250b54b8c2dSLada Trimasova u32 ge_mac_cfg_1_value = 0; 251b54b8c2dSLada Trimasova u32 *ge_mac_cfg_2_value = &priv->ge_mac_cfg_2_value; 2520dd07709SNoam Camus 2530dd07709SNoam Camus /* set MAC address in HW */ 254b54b8c2dSLada Trimasova ge_mac_cfg_1_value |= ndev->dev_addr[0] << CFG_1_OCTET_0_SHIFT; 255b54b8c2dSLada Trimasova ge_mac_cfg_1_value |= ndev->dev_addr[1] << CFG_1_OCTET_1_SHIFT; 256b54b8c2dSLada Trimasova ge_mac_cfg_1_value |= ndev->dev_addr[2] << CFG_1_OCTET_2_SHIFT; 257b54b8c2dSLada Trimasova ge_mac_cfg_1_value |= ndev->dev_addr[3] << CFG_1_OCTET_3_SHIFT; 258b54b8c2dSLada Trimasova *ge_mac_cfg_2_value = (*ge_mac_cfg_2_value & ~CFG_2_OCTET_4_MASK) 259b54b8c2dSLada Trimasova | ndev->dev_addr[4] << CFG_2_OCTET_4_SHIFT; 260b54b8c2dSLada Trimasova *ge_mac_cfg_2_value = (*ge_mac_cfg_2_value & ~CFG_2_OCTET_5_MASK) 261b54b8c2dSLada Trimasova | ndev->dev_addr[5] << CFG_2_OCTET_5_SHIFT; 2620dd07709SNoam Camus 2630dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_1, 264b54b8c2dSLada Trimasova ge_mac_cfg_1_value); 2650dd07709SNoam Camus 2660dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_2, 267b54b8c2dSLada Trimasova *ge_mac_cfg_2_value); 2680dd07709SNoam Camus } 2690dd07709SNoam Camus 2700dd07709SNoam Camus /** 2710dd07709SNoam Camus * nps_enet_hw_reset - Reset the network device. 2720dd07709SNoam Camus * @ndev: Pointer to the network device. 2730dd07709SNoam Camus * 2740dd07709SNoam Camus * This function reset the PCS and TX fifo. 2750dd07709SNoam Camus * The programming model is to set the relevant reset bits 2760dd07709SNoam Camus * wait for some time for this to propagate and then unset 2770dd07709SNoam Camus * the reset bits. This way we ensure that reset procedure 2780dd07709SNoam Camus * is done successfully by device. 2790dd07709SNoam Camus */ 2800dd07709SNoam Camus static void nps_enet_hw_reset(struct net_device *ndev) 2810dd07709SNoam Camus { 2820dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 283b54b8c2dSLada Trimasova u32 ge_rst_value = 0, phase_fifo_ctl_value = 0; 2840dd07709SNoam Camus 2850dd07709SNoam Camus /* Pcs reset sequence*/ 286b54b8c2dSLada Trimasova ge_rst_value |= NPS_ENET_ENABLE << RST_GMAC_0_SHIFT; 287b54b8c2dSLada Trimasova nps_enet_reg_set(priv, NPS_ENET_REG_GE_RST, ge_rst_value); 2880dd07709SNoam Camus usleep_range(10, 20); 289b54b8c2dSLada Trimasova nps_enet_reg_set(priv, NPS_ENET_REG_GE_RST, ge_rst_value); 2900dd07709SNoam Camus 2910dd07709SNoam Camus /* Tx fifo reset sequence */ 292b54b8c2dSLada Trimasova phase_fifo_ctl_value |= NPS_ENET_ENABLE << PHASE_FIFO_CTL_RST_SHIFT; 293b54b8c2dSLada Trimasova phase_fifo_ctl_value |= NPS_ENET_ENABLE << PHASE_FIFO_CTL_INIT_SHIFT; 2940dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_PHASE_FIFO_CTL, 295b54b8c2dSLada Trimasova phase_fifo_ctl_value); 2960dd07709SNoam Camus usleep_range(10, 20); 297b54b8c2dSLada Trimasova phase_fifo_ctl_value = 0; 2980dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_PHASE_FIFO_CTL, 299b54b8c2dSLada Trimasova phase_fifo_ctl_value); 3000dd07709SNoam Camus } 3010dd07709SNoam Camus 3020dd07709SNoam Camus static void nps_enet_hw_enable_control(struct net_device *ndev) 3030dd07709SNoam Camus { 3040dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 305b54b8c2dSLada Trimasova u32 ge_mac_cfg_0_value = 0, buf_int_enable_value = 0; 306b54b8c2dSLada Trimasova u32 *ge_mac_cfg_2_value = &priv->ge_mac_cfg_2_value; 307b54b8c2dSLada Trimasova u32 *ge_mac_cfg_3_value = &priv->ge_mac_cfg_3_value; 3080dd07709SNoam Camus s32 max_frame_length; 3090dd07709SNoam Camus 3100dd07709SNoam Camus /* Enable Rx and Tx statistics */ 311b54b8c2dSLada Trimasova *ge_mac_cfg_2_value = (*ge_mac_cfg_2_value & ~CFG_2_STAT_EN_MASK) 312b54b8c2dSLada Trimasova | NPS_ENET_GE_MAC_CFG_2_STAT_EN << CFG_2_STAT_EN_SHIFT; 3130dd07709SNoam Camus 3140dd07709SNoam Camus /* Discard packets with different MAC address */ 315b54b8c2dSLada Trimasova *ge_mac_cfg_2_value = (*ge_mac_cfg_2_value & ~CFG_2_DISK_DA_MASK) 316b54b8c2dSLada Trimasova | NPS_ENET_ENABLE << CFG_2_DISK_DA_SHIFT; 3170dd07709SNoam Camus 3180dd07709SNoam Camus /* Discard multicast packets */ 319b54b8c2dSLada Trimasova *ge_mac_cfg_2_value = (*ge_mac_cfg_2_value & ~CFG_2_DISK_MC_MASK) 320b54b8c2dSLada Trimasova | NPS_ENET_ENABLE << CFG_2_DISK_MC_SHIFT; 3210dd07709SNoam Camus 3220dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_2, 323b54b8c2dSLada Trimasova *ge_mac_cfg_2_value); 3240dd07709SNoam Camus 3250dd07709SNoam Camus /* Discard Packets bigger than max frame length */ 3260dd07709SNoam Camus max_frame_length = ETH_HLEN + ndev->mtu + ETH_FCS_LEN; 327b54b8c2dSLada Trimasova if (max_frame_length <= NPS_ENET_MAX_FRAME_LENGTH) { 328b54b8c2dSLada Trimasova *ge_mac_cfg_3_value = 329b54b8c2dSLada Trimasova (*ge_mac_cfg_3_value & ~CFG_3_MAX_LEN_MASK) 330b54b8c2dSLada Trimasova | max_frame_length << CFG_3_MAX_LEN_SHIFT; 331b54b8c2dSLada Trimasova } 3320dd07709SNoam Camus 3330dd07709SNoam Camus /* Enable interrupts */ 334b54b8c2dSLada Trimasova buf_int_enable_value |= NPS_ENET_ENABLE << RX_RDY_SHIFT; 335b54b8c2dSLada Trimasova buf_int_enable_value |= NPS_ENET_ENABLE << TX_DONE_SHIFT; 3360dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_BUF_INT_ENABLE, 337b54b8c2dSLada Trimasova buf_int_enable_value); 3380dd07709SNoam Camus 3390dd07709SNoam Camus /* Write device MAC address to HW */ 3400dd07709SNoam Camus nps_enet_set_hw_mac_address(ndev); 3410dd07709SNoam Camus 3420dd07709SNoam Camus /* Rx and Tx HW features */ 343b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_TX_PAD_EN_SHIFT; 344b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_TX_CRC_EN_SHIFT; 345b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_RX_CRC_STRIP_SHIFT; 3460dd07709SNoam Camus 3470dd07709SNoam Camus /* IFG configuration */ 348b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= 349b54b8c2dSLada Trimasova NPS_ENET_GE_MAC_CFG_0_RX_IFG << CFG_0_RX_IFG_SHIFT; 350b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= 351b54b8c2dSLada Trimasova NPS_ENET_GE_MAC_CFG_0_TX_IFG << CFG_0_TX_IFG_SHIFT; 3520dd07709SNoam Camus 3530dd07709SNoam Camus /* preamble configuration */ 354b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_RX_PR_CHECK_EN_SHIFT; 355b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= 356b54b8c2dSLada Trimasova NPS_ENET_GE_MAC_CFG_0_TX_PR_LEN << CFG_0_TX_PR_LEN_SHIFT; 3570dd07709SNoam Camus 3580dd07709SNoam Camus /* enable flow control frames */ 359b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_TX_FC_EN_SHIFT; 360b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_RX_FC_EN_SHIFT; 361b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= 362b54b8c2dSLada Trimasova NPS_ENET_GE_MAC_CFG_0_TX_FC_RETR << CFG_0_TX_FC_RETR_SHIFT; 363b54b8c2dSLada Trimasova *ge_mac_cfg_3_value = (*ge_mac_cfg_3_value & ~CFG_3_CF_DROP_MASK) 364b54b8c2dSLada Trimasova | NPS_ENET_ENABLE << CFG_3_CF_DROP_SHIFT; 3650dd07709SNoam Camus 3660dd07709SNoam Camus /* Enable Rx and Tx */ 367b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_RX_EN_SHIFT; 368b54b8c2dSLada Trimasova ge_mac_cfg_0_value |= NPS_ENET_ENABLE << CFG_0_TX_EN_SHIFT; 3690dd07709SNoam Camus 370de671567SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_3, 371b54b8c2dSLada Trimasova *ge_mac_cfg_3_value); 3720dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_0, 373b54b8c2dSLada Trimasova ge_mac_cfg_0_value); 3740dd07709SNoam Camus } 3750dd07709SNoam Camus 3760dd07709SNoam Camus static void nps_enet_hw_disable_control(struct net_device *ndev) 3770dd07709SNoam Camus { 3780dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 3790dd07709SNoam Camus 3800dd07709SNoam Camus /* Disable interrupts */ 3810dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_BUF_INT_ENABLE, 0); 3820dd07709SNoam Camus 3830dd07709SNoam Camus /* Disable Rx and Tx */ 3840dd07709SNoam Camus nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_0, 0); 3850dd07709SNoam Camus } 3860dd07709SNoam Camus 3870dd07709SNoam Camus static void nps_enet_send_frame(struct net_device *ndev, 3880dd07709SNoam Camus struct sk_buff *skb) 3890dd07709SNoam Camus { 3900dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 391b54b8c2dSLada Trimasova u32 tx_ctrl_value = 0; 3920dd07709SNoam Camus short length = skb->len; 3930dd07709SNoam Camus u32 i, len = DIV_ROUND_UP(length, sizeof(u32)); 394b0a8d1a0SArnd Bergmann u32 *src = (void *)skb->data; 3950dd07709SNoam Camus bool src_is_aligned = IS_ALIGNED((unsigned long)src, sizeof(u32)); 3960dd07709SNoam Camus 3970dd07709SNoam Camus /* In case src is not aligned we need an intermediate buffer */ 3980dd07709SNoam Camus if (src_is_aligned) 399b54b8c2dSLada Trimasova iowrite32_rep(priv->regs_base + NPS_ENET_REG_TX_BUF, src, len); 400b0a8d1a0SArnd Bergmann else /* !src_is_aligned */ 401b0a8d1a0SArnd Bergmann for (i = 0; i < len; i++, src++) 402b0a8d1a0SArnd Bergmann nps_enet_reg_set(priv, NPS_ENET_REG_TX_BUF, 403b54b8c2dSLada Trimasova get_unaligned_be32(src)); 4040dd07709SNoam Camus 4050dd07709SNoam Camus /* Write the length of the Frame */ 406b54b8c2dSLada Trimasova tx_ctrl_value |= length << TX_CTL_NT_SHIFT; 4070dd07709SNoam Camus 408b54b8c2dSLada Trimasova tx_ctrl_value |= NPS_ENET_ENABLE << TX_CTL_CT_SHIFT; 4090dd07709SNoam Camus /* Send Frame */ 410b54b8c2dSLada Trimasova nps_enet_reg_set(priv, NPS_ENET_REG_TX_CTL, tx_ctrl_value); 4110dd07709SNoam Camus } 4120dd07709SNoam Camus 4130dd07709SNoam Camus /** 4140dd07709SNoam Camus * nps_enet_set_mac_address - Set the MAC address for this device. 4150dd07709SNoam Camus * @ndev: Pointer to net_device structure. 4160dd07709SNoam Camus * @p: 6 byte Address to be written as MAC address. 4170dd07709SNoam Camus * 4180dd07709SNoam Camus * This function copies the HW address from the sockaddr structure to the 4190dd07709SNoam Camus * net_device structure and updates the address in HW. 4200dd07709SNoam Camus * 4210dd07709SNoam Camus * returns: -EBUSY if the net device is busy or 0 if the address is set 4220dd07709SNoam Camus * successfully. 4230dd07709SNoam Camus */ 4240dd07709SNoam Camus static s32 nps_enet_set_mac_address(struct net_device *ndev, void *p) 4250dd07709SNoam Camus { 4260dd07709SNoam Camus struct sockaddr *addr = p; 4270dd07709SNoam Camus s32 res; 4280dd07709SNoam Camus 4290dd07709SNoam Camus if (netif_running(ndev)) 4300dd07709SNoam Camus return -EBUSY; 4310dd07709SNoam Camus 4320dd07709SNoam Camus res = eth_mac_addr(ndev, p); 4330dd07709SNoam Camus if (!res) { 4340dd07709SNoam Camus ether_addr_copy(ndev->dev_addr, addr->sa_data); 4350dd07709SNoam Camus nps_enet_set_hw_mac_address(ndev); 4360dd07709SNoam Camus } 4370dd07709SNoam Camus 4380dd07709SNoam Camus return res; 4390dd07709SNoam Camus } 4400dd07709SNoam Camus 4410dd07709SNoam Camus /** 4420dd07709SNoam Camus * nps_enet_set_rx_mode - Change the receive filtering mode. 4430dd07709SNoam Camus * @ndev: Pointer to the network device. 4440dd07709SNoam Camus * 4450dd07709SNoam Camus * This function enables/disables promiscuous mode 4460dd07709SNoam Camus */ 4470dd07709SNoam Camus static void nps_enet_set_rx_mode(struct net_device *ndev) 4480dd07709SNoam Camus { 4490dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 450b54b8c2dSLada Trimasova u32 ge_mac_cfg_2_value = priv->ge_mac_cfg_2_value; 4510dd07709SNoam Camus 4520dd07709SNoam Camus if (ndev->flags & IFF_PROMISC) { 453b54b8c2dSLada Trimasova ge_mac_cfg_2_value = (ge_mac_cfg_2_value & ~CFG_2_DISK_DA_MASK) 454b54b8c2dSLada Trimasova | NPS_ENET_DISABLE << CFG_2_DISK_DA_SHIFT; 455b54b8c2dSLada Trimasova ge_mac_cfg_2_value = (ge_mac_cfg_2_value & ~CFG_2_DISK_MC_MASK) 456b54b8c2dSLada Trimasova | NPS_ENET_DISABLE << CFG_2_DISK_MC_SHIFT; 457b54b8c2dSLada Trimasova 4580dd07709SNoam Camus } else { 459b54b8c2dSLada Trimasova ge_mac_cfg_2_value = (ge_mac_cfg_2_value & ~CFG_2_DISK_DA_MASK) 460b54b8c2dSLada Trimasova | NPS_ENET_ENABLE << CFG_2_DISK_DA_SHIFT; 461b54b8c2dSLada Trimasova ge_mac_cfg_2_value = (ge_mac_cfg_2_value & ~CFG_2_DISK_MC_MASK) 462b54b8c2dSLada Trimasova | NPS_ENET_ENABLE << CFG_2_DISK_MC_SHIFT; 4630dd07709SNoam Camus } 4640dd07709SNoam Camus 465b54b8c2dSLada Trimasova nps_enet_reg_set(priv, NPS_ENET_REG_GE_MAC_CFG_2, ge_mac_cfg_2_value); 4660dd07709SNoam Camus } 4670dd07709SNoam Camus 4680dd07709SNoam Camus /** 4690dd07709SNoam Camus * nps_enet_open - Open the network device. 4700dd07709SNoam Camus * @ndev: Pointer to the network device. 4710dd07709SNoam Camus * 4720dd07709SNoam Camus * returns: 0, on success or non-zero error value on failure. 4730dd07709SNoam Camus * 4740dd07709SNoam Camus * This function sets the MAC address, requests and enables an IRQ 4750dd07709SNoam Camus * for the ENET device and starts the Tx queue. 4760dd07709SNoam Camus */ 4770dd07709SNoam Camus static s32 nps_enet_open(struct net_device *ndev) 4780dd07709SNoam Camus { 4790dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 4800dd07709SNoam Camus s32 err; 4810dd07709SNoam Camus 4820dd07709SNoam Camus /* Reset private variables */ 483e5df49d5SElad Kanfi priv->tx_skb = NULL; 484b54b8c2dSLada Trimasova priv->ge_mac_cfg_2_value = 0; 485b54b8c2dSLada Trimasova priv->ge_mac_cfg_3_value = 0; 4860dd07709SNoam Camus 4870dd07709SNoam Camus /* ge_mac_cfg_3 default values */ 488b54b8c2dSLada Trimasova priv->ge_mac_cfg_3_value |= 489b54b8c2dSLada Trimasova NPS_ENET_GE_MAC_CFG_3_RX_IFG_TH << CFG_3_RX_IFG_TH_SHIFT; 490b54b8c2dSLada Trimasova 491b54b8c2dSLada Trimasova priv->ge_mac_cfg_3_value |= 492b54b8c2dSLada Trimasova NPS_ENET_GE_MAC_CFG_3_MAX_LEN << CFG_3_MAX_LEN_SHIFT; 4930dd07709SNoam Camus 4940dd07709SNoam Camus /* Disable HW device */ 4950dd07709SNoam Camus nps_enet_hw_disable_control(ndev); 4960dd07709SNoam Camus 4970dd07709SNoam Camus /* irq Rx allocation */ 4980dd07709SNoam Camus err = request_irq(priv->irq, nps_enet_irq_handler, 4990dd07709SNoam Camus 0, "enet-rx-tx", ndev); 5000dd07709SNoam Camus if (err) 5010dd07709SNoam Camus return err; 5020dd07709SNoam Camus 5030dd07709SNoam Camus napi_enable(&priv->napi); 5040dd07709SNoam Camus 5050dd07709SNoam Camus /* Enable HW device */ 5060dd07709SNoam Camus nps_enet_hw_reset(ndev); 5070dd07709SNoam Camus nps_enet_hw_enable_control(ndev); 5080dd07709SNoam Camus 5090dd07709SNoam Camus netif_start_queue(ndev); 5100dd07709SNoam Camus 5110dd07709SNoam Camus return 0; 5120dd07709SNoam Camus } 5130dd07709SNoam Camus 5140dd07709SNoam Camus /** 5150dd07709SNoam Camus * nps_enet_stop - Close the network device. 5160dd07709SNoam Camus * @ndev: Pointer to the network device. 5170dd07709SNoam Camus * 5180dd07709SNoam Camus * This function stops the Tx queue, disables interrupts for the ENET device. 5190dd07709SNoam Camus */ 5200dd07709SNoam Camus static s32 nps_enet_stop(struct net_device *ndev) 5210dd07709SNoam Camus { 5220dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 5230dd07709SNoam Camus 5240dd07709SNoam Camus napi_disable(&priv->napi); 5250dd07709SNoam Camus netif_stop_queue(ndev); 5260dd07709SNoam Camus nps_enet_hw_disable_control(ndev); 5270dd07709SNoam Camus free_irq(priv->irq, ndev); 5280dd07709SNoam Camus 5290dd07709SNoam Camus return 0; 5300dd07709SNoam Camus } 5310dd07709SNoam Camus 5320dd07709SNoam Camus /** 5330dd07709SNoam Camus * nps_enet_start_xmit - Starts the data transmission. 5340dd07709SNoam Camus * @skb: sk_buff pointer that contains data to be Transmitted. 5350dd07709SNoam Camus * @ndev: Pointer to net_device structure. 5360dd07709SNoam Camus * 5370dd07709SNoam Camus * returns: NETDEV_TX_OK, on success 5380dd07709SNoam Camus * NETDEV_TX_BUSY, if any of the descriptors are not free. 5390dd07709SNoam Camus * 5400dd07709SNoam Camus * This function is invoked from upper layers to initiate transmission. 5410dd07709SNoam Camus */ 5420dd07709SNoam Camus static netdev_tx_t nps_enet_start_xmit(struct sk_buff *skb, 5430dd07709SNoam Camus struct net_device *ndev) 5440dd07709SNoam Camus { 5450dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 5460dd07709SNoam Camus 5470dd07709SNoam Camus /* This driver handles one frame at a time */ 5480dd07709SNoam Camus netif_stop_queue(ndev); 5490dd07709SNoam Camus 5500dd07709SNoam Camus priv->tx_skb = skb; 5510dd07709SNoam Camus 552e5df49d5SElad Kanfi /* make sure tx_skb is actually written to the memory 553e5df49d5SElad Kanfi * before the HW is informed and the IRQ is fired. 554e5df49d5SElad Kanfi */ 555e5df49d5SElad Kanfi wmb(); 556e5df49d5SElad Kanfi 55793fcf83eSNoam Camus nps_enet_send_frame(ndev, skb); 55893fcf83eSNoam Camus 5590dd07709SNoam Camus return NETDEV_TX_OK; 5600dd07709SNoam Camus } 5610dd07709SNoam Camus 5620dd07709SNoam Camus #ifdef CONFIG_NET_POLL_CONTROLLER 5630dd07709SNoam Camus static void nps_enet_poll_controller(struct net_device *ndev) 5640dd07709SNoam Camus { 5650dd07709SNoam Camus disable_irq(ndev->irq); 5660dd07709SNoam Camus nps_enet_irq_handler(ndev->irq, ndev); 5670dd07709SNoam Camus enable_irq(ndev->irq); 5680dd07709SNoam Camus } 5690dd07709SNoam Camus #endif 5700dd07709SNoam Camus 5710dd07709SNoam Camus static const struct net_device_ops nps_netdev_ops = { 5720dd07709SNoam Camus .ndo_open = nps_enet_open, 5730dd07709SNoam Camus .ndo_stop = nps_enet_stop, 5740dd07709SNoam Camus .ndo_start_xmit = nps_enet_start_xmit, 5750dd07709SNoam Camus .ndo_set_mac_address = nps_enet_set_mac_address, 5760dd07709SNoam Camus .ndo_set_rx_mode = nps_enet_set_rx_mode, 5770dd07709SNoam Camus #ifdef CONFIG_NET_POLL_CONTROLLER 5780dd07709SNoam Camus .ndo_poll_controller = nps_enet_poll_controller, 5790dd07709SNoam Camus #endif 5800dd07709SNoam Camus }; 5810dd07709SNoam Camus 5820dd07709SNoam Camus static s32 nps_enet_probe(struct platform_device *pdev) 5830dd07709SNoam Camus { 5840dd07709SNoam Camus struct device *dev = &pdev->dev; 5850dd07709SNoam Camus struct net_device *ndev; 5860dd07709SNoam Camus struct nps_enet_priv *priv; 5870dd07709SNoam Camus s32 err = 0; 5880dd07709SNoam Camus const char *mac_addr; 5890dd07709SNoam Camus struct resource *res_regs; 5900dd07709SNoam Camus 5910dd07709SNoam Camus if (!dev->of_node) 5920dd07709SNoam Camus return -ENODEV; 5930dd07709SNoam Camus 5940dd07709SNoam Camus ndev = alloc_etherdev(sizeof(struct nps_enet_priv)); 5950dd07709SNoam Camus if (!ndev) 5960dd07709SNoam Camus return -ENOMEM; 5970dd07709SNoam Camus 5980dd07709SNoam Camus platform_set_drvdata(pdev, ndev); 5990dd07709SNoam Camus SET_NETDEV_DEV(ndev, dev); 6000dd07709SNoam Camus priv = netdev_priv(ndev); 6010dd07709SNoam Camus 6020dd07709SNoam Camus /* The EZ NET specific entries in the device structure. */ 6030dd07709SNoam Camus ndev->netdev_ops = &nps_netdev_ops; 6040dd07709SNoam Camus ndev->watchdog_timeo = (400 * HZ / 1000); 6050dd07709SNoam Camus /* FIXME :: no multicast support yet */ 6060dd07709SNoam Camus ndev->flags &= ~IFF_MULTICAST; 6070dd07709SNoam Camus 6080dd07709SNoam Camus res_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); 6090dd07709SNoam Camus priv->regs_base = devm_ioremap_resource(dev, res_regs); 6100dd07709SNoam Camus if (IS_ERR(priv->regs_base)) { 6110dd07709SNoam Camus err = PTR_ERR(priv->regs_base); 6120dd07709SNoam Camus goto out_netdev; 6130dd07709SNoam Camus } 6140dd07709SNoam Camus dev_dbg(dev, "Registers base address is 0x%p\n", priv->regs_base); 6150dd07709SNoam Camus 6160dd07709SNoam Camus /* set kernel MAC address to dev */ 6170dd07709SNoam Camus mac_addr = of_get_mac_address(dev->of_node); 6180dd07709SNoam Camus if (mac_addr) 6190dd07709SNoam Camus ether_addr_copy(ndev->dev_addr, mac_addr); 6200dd07709SNoam Camus else 6210dd07709SNoam Camus eth_hw_addr_random(ndev); 6220dd07709SNoam Camus 6230dd07709SNoam Camus /* Get IRQ number */ 6240dd07709SNoam Camus priv->irq = platform_get_irq(pdev, 0); 6250dd07709SNoam Camus if (!priv->irq) { 6260dd07709SNoam Camus dev_err(dev, "failed to retrieve <irq Rx-Tx> value from device tree\n"); 6270dd07709SNoam Camus err = -ENODEV; 6280dd07709SNoam Camus goto out_netdev; 6290dd07709SNoam Camus } 6300dd07709SNoam Camus 6310dd07709SNoam Camus netif_napi_add(ndev, &priv->napi, nps_enet_poll, 6320dd07709SNoam Camus NPS_ENET_NAPI_POLL_WEIGHT); 6330dd07709SNoam Camus 6340dd07709SNoam Camus /* Register the driver. Should be the last thing in probe */ 6350dd07709SNoam Camus err = register_netdev(ndev); 6360dd07709SNoam Camus if (err) { 6370dd07709SNoam Camus dev_err(dev, "Failed to register ndev for %s, err = 0x%08x\n", 6380dd07709SNoam Camus ndev->name, (s32)err); 6390dd07709SNoam Camus goto out_netif_api; 6400dd07709SNoam Camus } 6410dd07709SNoam Camus 6420dd07709SNoam Camus dev_info(dev, "(rx/tx=%d)\n", priv->irq); 6430dd07709SNoam Camus return 0; 6440dd07709SNoam Camus 6450dd07709SNoam Camus out_netif_api: 6460dd07709SNoam Camus netif_napi_del(&priv->napi); 6470dd07709SNoam Camus out_netdev: 6480dd07709SNoam Camus if (err) 6490dd07709SNoam Camus free_netdev(ndev); 6500dd07709SNoam Camus 6510dd07709SNoam Camus return err; 6520dd07709SNoam Camus } 6530dd07709SNoam Camus 6540dd07709SNoam Camus static s32 nps_enet_remove(struct platform_device *pdev) 6550dd07709SNoam Camus { 6560dd07709SNoam Camus struct net_device *ndev = platform_get_drvdata(pdev); 6570dd07709SNoam Camus struct nps_enet_priv *priv = netdev_priv(ndev); 6580dd07709SNoam Camus 6590dd07709SNoam Camus unregister_netdev(ndev); 6600dd07709SNoam Camus free_netdev(ndev); 6610dd07709SNoam Camus netif_napi_del(&priv->napi); 6620dd07709SNoam Camus 6630dd07709SNoam Camus return 0; 6640dd07709SNoam Camus } 6650dd07709SNoam Camus 6660dd07709SNoam Camus static const struct of_device_id nps_enet_dt_ids[] = { 6670dd07709SNoam Camus { .compatible = "ezchip,nps-mgt-enet" }, 6680dd07709SNoam Camus { /* Sentinel */ } 6690dd07709SNoam Camus }; 6700dd07709SNoam Camus 6710dd07709SNoam Camus static struct platform_driver nps_enet_driver = { 6720dd07709SNoam Camus .probe = nps_enet_probe, 6730dd07709SNoam Camus .remove = nps_enet_remove, 6740dd07709SNoam Camus .driver = { 6750dd07709SNoam Camus .name = DRV_NAME, 6760dd07709SNoam Camus .of_match_table = nps_enet_dt_ids, 6770dd07709SNoam Camus }, 6780dd07709SNoam Camus }; 6790dd07709SNoam Camus 6800dd07709SNoam Camus module_platform_driver(nps_enet_driver); 6810dd07709SNoam Camus 6820dd07709SNoam Camus MODULE_AUTHOR("EZchip Semiconductor"); 6830dd07709SNoam Camus MODULE_LICENSE("GPL v2"); 684