From patchwork Sat Aug 2 03:37:16 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steve Rae X-Patchwork-Id: 34742 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qg0-f71.google.com (mail-qg0-f71.google.com [209.85.192.71]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 885A9202A1 for ; Sat, 2 Aug 2014 03:37:21 +0000 (UTC) Received: by mail-qg0-f71.google.com with SMTP id f51sf9976058qge.6 for ; Fri, 01 Aug 2014 20:37:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:in-reply-to :references:mime-version:cc:subject:precedence:list-id :list-unsubscribe:list-archive:list-post:list-help:list-subscribe :sender:errors-to:x-original-sender :x-original-authentication-results:mailing-list:content-type :content-transfer-encoding; bh=Pu3tfxBeAau0pOTVO2uTWWgGUpNFZHWvS/3aov/yU8c=; b=clmv0OqPkacWHjMcUPVrPcuyocnloBDQ2CpzFci9ccyO2VghKqWC3vQshjD8+AIux8 HJzVv1nYo+XeRsivW6B31cRGsQmfTXZVtyyk2InkUWF/xRn8XzOFo9i/1v/RPVHEZBL4 prJflHXZiYdpKWnXsEWqsxp2dNbYHiTT78osVMJLAVPNz5b8u449gec68xm3rzbRiHaw Qvv9VEDr4XkUZ1cwXqcEXMJdu1VjKYwxOc/Kh1iGoUxY/A/I9XbaSIBX7/lb5uUNW2vD mur9EYJYvMtTQ19aTibVnGCfil2GV4gRijAfJPIgft6t6JiuKT7WmW5VzrNjnXhTjyd1 EofA== X-Gm-Message-State: ALoCoQne6eJlVWlEG79HoaqLTf5UOxlBxzXGTMuHy8niUIxpvwnZI5+Y3BKWNPC6vpj8mjysxKPQ X-Received: by 10.236.122.236 with SMTP id t72mr3538058yhh.15.1406950641159; Fri, 01 Aug 2014 20:37:21 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.49.1 with SMTP id p1ls1514287qga.97.gmail; Fri, 01 Aug 2014 20:37:21 -0700 (PDT) X-Received: by 10.220.164.198 with SMTP id f6mr11342071vcy.51.1406950641040; Fri, 01 Aug 2014 20:37:21 -0700 (PDT) Received: from mail-vc0-f170.google.com (mail-vc0-f170.google.com [209.85.220.170]) by mx.google.com with ESMTPS id vy2si9026358vcb.73.2014.08.01.20.37.21 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 01 Aug 2014 20:37:21 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.170 as permitted sender) client-ip=209.85.220.170; Received: by mail-vc0-f170.google.com with SMTP id lf12so7988569vcb.15 for ; Fri, 01 Aug 2014 20:37:21 -0700 (PDT) X-Received: by 10.53.13.200 with SMTP id fa8mr9543338vdd.57.1406950640910; Fri, 01 Aug 2014 20:37:20 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.221.37.5 with SMTP id tc5csp154718vcb; Fri, 1 Aug 2014 20:37:19 -0700 (PDT) X-Received: by 10.194.202.231 with SMTP id kl7mr13650010wjc.134.1406950638580; Fri, 01 Aug 2014 20:37:18 -0700 (PDT) Received: from theia.denx.de (theia.denx.de. [85.214.87.163]) by mx.google.com with ESMTP id s5si10011781wiv.46.2014.08.01.20.37.18 for ; Fri, 01 Aug 2014 20:37:18 -0700 (PDT) Received-SPF: none (google.com: u-boot-bounces@lists.denx.de does not designate permitted sender hosts) client-ip=85.214.87.163; Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id CDC6C4A049; Sat, 2 Aug 2014 05:37:17 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id g1mcmK79ALKd; Sat, 2 Aug 2014 05:37:17 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 5D0714A050; Sat, 2 Aug 2014 05:37:11 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 095354A051 for ; Sat, 2 Aug 2014 05:37:07 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id HVSEg9exNRST for ; Sat, 2 Aug 2014 05:37:02 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail-gw1-out.broadcom.com (mail-gw1-out.broadcom.com [216.31.210.62]) by theia.denx.de (Postfix) with ESMTP id 1B7494A039 for ; Sat, 2 Aug 2014 05:36:57 +0200 (CEST) X-IronPort-AV: E=Sophos;i="5.01,784,1400050800"; d="scan'208";a="41570041" Received: from irvexchcas08.broadcom.com (HELO IRVEXCHCAS08.corp.ad.broadcom.com) ([10.9.208.57]) by mail-gw1-out.broadcom.com with ESMTP; 01 Aug 2014 21:17:41 -0700 Received: from IRVEXCHSMTP2.corp.ad.broadcom.com (10.9.207.52) by IRVEXCHCAS08.corp.ad.broadcom.com (10.9.208.57) with Microsoft SMTP Server (TLS) id 14.3.174.1; Fri, 1 Aug 2014 20:36:55 -0700 Received: from mail-irva-13.broadcom.com (10.10.10.20) by IRVEXCHSMTP2.corp.ad.broadcom.com (10.9.207.52) with Microsoft SMTP Server id 14.3.174.1; Fri, 1 Aug 2014 20:36:55 -0700 Received: from mail.broadcom.com (lbrmn-vmlnx03.ric.broadcom.com [10.136.4.105]) by mail-irva-13.broadcom.com (Postfix) with ESMTP id 6753F9F9F7; Fri, 1 Aug 2014 20:36:55 -0700 (PDT) From: Steve Rae To: Date: Fri, 1 Aug 2014 20:37:16 -0700 Message-ID: <1406950637-19820-2-git-send-email-srae@broadcom.com> X-Mailer: git-send-email 1.8.5 In-Reply-To: <1406950637-19820-1-git-send-email-srae@broadcom.com> References: <1406950637-19820-1-git-send-email-srae@broadcom.com> MIME-Version: 1.0 Cc: Steve Rae , Jiandong Zheng Subject: [U-Boot] [PATCH v2 2/3] arm: bcm281xx: net: Add Ethernet Driver X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: srae@broadcom.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.170 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 From: Jiandong Zheng The Broadcom StarFighter2 Ethernet driver is used in multiple Broadcom SoC(s) and: - supports multiple MAC blocks, - provides support for the Broadcom GMAC. This driver requires MII and PHYLIB. Signed-off-by: Jiandong Zheng Signed-off-by: Steve Rae --- Changes in v2: - added Ethernet Driver drivers/net/Makefile | 2 + drivers/net/bcm-sf2-eth-gmac.c | 971 +++++++++++++++++++++++++++++++++++++++++ drivers/net/bcm-sf2-eth-gmac.h | 224 ++++++++++ drivers/net/bcm-sf2-eth.c | 268 ++++++++++++ drivers/net/bcm-sf2-eth.h | 70 +++ include/netdev.h | 1 + 6 files changed, 1536 insertions(+) create mode 100644 drivers/net/bcm-sf2-eth-gmac.c create mode 100644 drivers/net/bcm-sf2-eth-gmac.h create mode 100644 drivers/net/bcm-sf2-eth.c create mode 100644 drivers/net/bcm-sf2-eth.h diff --git a/drivers/net/Makefile b/drivers/net/Makefile index 7cc6b6f..14b5eb2 100644 --- a/drivers/net/Makefile +++ b/drivers/net/Makefile @@ -10,6 +10,8 @@ obj-$(CONFIG_ALTERA_TSE) += altera_tse.o obj-$(CONFIG_ARMADA100_FEC) += armada100_fec.o obj-$(CONFIG_DRIVER_AT91EMAC) += at91_emac.o obj-$(CONFIG_DRIVER_AX88180) += ax88180.o +obj-$(CONFIG_BCM_SF2_ETH) += bcm-sf2-eth.o +obj-$(CONFIG_BCM_SF2_ETH_GMAC) += bcm-sf2-eth-gmac.o obj-$(CONFIG_BFIN_MAC) += bfin_mac.o obj-$(CONFIG_CALXEDA_XGMAC) += calxedaxgmac.o obj-$(CONFIG_CS8900) += cs8900.o diff --git a/drivers/net/bcm-sf2-eth-gmac.c b/drivers/net/bcm-sf2-eth-gmac.c new file mode 100644 index 0000000..977feec --- /dev/null +++ b/drivers/net/bcm-sf2-eth-gmac.c @@ -0,0 +1,971 @@ +/* + * Copyright 2014 Broadcom Corporation. + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#ifdef BCM_GMAC_DEBUG +#ifndef DEBUG +#define DEBUG +#endif +#endif + +#include +#include +#include +#include +#include +#include + +#include "bcm-sf2-eth.h" +#include "bcm-sf2-eth-gmac.h" + +#define SPINWAIT(exp, us) { \ + uint countdown = (us) + 9; \ + while ((exp) && (countdown >= 10)) {\ + udelay(10); \ + countdown -= 10; \ + } \ +} + +static int gmac_disable_dma(struct eth_dma *dma, int dir); +static int gmac_enable_dma(struct eth_dma *dma, int dir); + +/* DMA Descriptor */ +typedef struct { + /* misc control bits */ + uint32_t ctrl1; + /* buffer count and address extension */ + uint32_t ctrl2; + /* memory address of the date buffer, bits 31:0 */ + uint32_t addrlow; + /* memory address of the date buffer, bits 63:32 */ + uint32_t addrhigh; +} dma64dd_t; + +uint32_t g_dmactrlflags; + +static uint32_t dma_ctrlflags(uint32_t mask, uint32_t flags) +{ + debug("%s enter\n", __func__); + + g_dmactrlflags &= ~mask; + g_dmactrlflags |= flags; + + /* If trying to enable parity, check if parity is actually supported */ + if (g_dmactrlflags & DMA_CTRL_PEN) { + uint32_t control; + + control = readl(GMAC0_DMA_TX_CTRL_ADDR); + writel(control | D64_XC_PD, GMAC0_DMA_TX_CTRL_ADDR); + if (readl(GMAC0_DMA_TX_CTRL_ADDR) & D64_XC_PD) { + /* + * We *can* disable it, therefore it is supported; + * restore control register + */ + writel(control, GMAC0_DMA_TX_CTRL_ADDR); + } else { + /* Not supported, don't allow it to be enabled */ + g_dmactrlflags &= ~DMA_CTRL_PEN; + } + } + + return g_dmactrlflags; +} + +static inline void reg32_clear_bits(uint32_t reg, uint32_t value) +{ + uint32_t v = readl(reg); + v &= ~(value); + writel(v, reg); +} + +static inline void reg32_set_bits(uint32_t reg, uint32_t value) +{ + uint32_t v = readl(reg); + v |= value; + writel(v, reg); +} + +#ifdef BCM_GMAC_DEBUG +static void dma_tx_dump(struct eth_dma *dma) +{ + dma64dd_t *descp = NULL; + uint8_t *bufp; + int i; + + printf("TX DMA Register:\n"); + printf("control:0x%x; ptr:0x%x; addrl:0x%x; addrh:0x%x; stat0:0x%x, stat1:0x%x\n", + readl(GMAC0_DMA_TX_CTRL_ADDR), + readl(GMAC0_DMA_TX_PTR_ADDR), + readl(GMAC0_DMA_TX_ADDR_LOW_ADDR), + readl(GMAC0_DMA_TX_ADDR_HIGH_ADDR), + readl(GMAC0_DMA_TX_STATUS0_ADDR), + readl(GMAC0_DMA_TX_STATUS1_ADDR)); + + printf("TX Descriptors:\n"); + for (i = 0; i < TX_BUF_NUM; i++) { + descp = (dma64dd_t *)(dma->tx_desc_aligned) + i; + printf("ctrl1:0x%08x; ctrl2:0x%08x; addr:0x%x 0x%08x\n", + descp->ctrl1, descp->ctrl2, + descp->addrhigh, descp->addrlow); + } + + printf("TX Buffers:\n"); + /* Initialize TX DMA descriptor table */ + for (i = 0; i < TX_BUF_NUM; i++) { + bufp = (uint8_t *)(dma->tx_buf + i * TX_BUF_SIZE); + printf("buf%d:0x%x; ", i, (uint32_t)bufp); + } + printf("\n"); +} + +static void dma_rx_dump(struct eth_dma *dma) +{ + dma64dd_t *descp = NULL; + uint8_t *bufp; + int i; + + printf("RX DMA Register:\n"); + printf("control:0x%x; ptr:0x%x; addrl:0x%x; addrh:0x%x; stat0:0x%x, stat1:0x%x\n", + readl(GMAC0_DMA_RX_CTRL_ADDR), + readl(GMAC0_DMA_RX_PTR_ADDR), + readl(GMAC0_DMA_RX_ADDR_LOW_ADDR), + readl(GMAC0_DMA_RX_ADDR_HIGH_ADDR), + readl(GMAC0_DMA_RX_STATUS0_ADDR), + readl(GMAC0_DMA_RX_STATUS1_ADDR)); + + printf("RX Descriptors:\n"); + for (i = 0; i < RX_BUF_NUM; i++) { + descp = (dma64dd_t *)(dma->rx_desc_aligned) + i; + printf("ctrl1:0x%08x; ctrl2:0x%08x; addr:0x%x 0x%08x\n", + descp->ctrl1, descp->ctrl2, + descp->addrhigh, descp->addrlow); + } + + printf("RX Buffers:\n"); + for (i = 0; i < RX_BUF_NUM; i++) { + bufp = dma->rx_buf + i * RX_BUF_SIZE; + printf("buf%d:0x%x; ", i, (uint32_t)bufp); + } + printf("\n"); +} +#endif + +static int dma_tx_init(struct eth_dma *dma) +{ + dma64dd_t *descp = NULL; + uint8_t *bufp; + int i; + uint32_t ctrl; + + debug("%s enter\n", __func__); + + /* clear descriptor memory */ + memset((void *)(dma->tx_desc_aligned), 0, + TX_BUF_NUM * sizeof(dma64dd_t)); + memset(dma->tx_buf, 0, TX_BUF_NUM * TX_BUF_SIZE); + + /* Initialize TX DMA descriptor table */ + for (i = 0; i < TX_BUF_NUM; i++) { + descp = (dma64dd_t *)(dma->tx_desc_aligned) + i; + bufp = dma->tx_buf + i * TX_BUF_SIZE; + /* clear buffer memory */ + memset((void *)bufp, 0, TX_BUF_SIZE); + + ctrl = 0; + /* if last descr set endOfTable */ + if (i == (TX_BUF_NUM-1)) + ctrl = D64_CTRL1_EOT; + descp->ctrl1 = ctrl; + descp->ctrl2 = 0; + descp->addrlow = (uint32_t)bufp; + descp->addrhigh = 0; + } + + /* flush descriptor and buffer */ + descp = dma->tx_desc_aligned; + bufp = dma->tx_buf; + flush_dcache_range((unsigned long)descp, + (unsigned long)(descp + + sizeof(dma64dd_t) * TX_BUF_NUM)); + flush_dcache_range((unsigned long)(bufp), + (unsigned long)(bufp + TX_BUF_SIZE * TX_BUF_NUM)); + + /* initialize the DMA channel */ + writel((uint32_t)(dma->tx_desc_aligned), GMAC0_DMA_TX_ADDR_LOW_ADDR); + writel(0, GMAC0_DMA_TX_ADDR_HIGH_ADDR); + + /* now update the dma last descriptor */ + writel(((uint32_t)(dma->tx_desc_aligned)) & D64_XP_LD_MASK, + GMAC0_DMA_TX_PTR_ADDR); + + return 0; +} + +static int dma_rx_init(struct eth_dma *dma) +{ + uint32_t last_desc; + dma64dd_t *descp = NULL; + uint8_t *bufp; + uint32_t ctrl; + int i; + + debug("%s enter\n", __func__); + + /* clear descriptor memory */ + memset((void *)(dma->rx_desc_aligned), 0, + RX_BUF_NUM * sizeof(dma64dd_t)); + /* clear buffer memory */ + memset(dma->rx_buf, 0, RX_BUF_NUM * RX_BUF_SIZE); + + /* Initialize RX DMA descriptor table */ + for (i = 0; i < RX_BUF_NUM; i++) { + descp = (dma64dd_t *)(dma->rx_desc_aligned) + i; + bufp = dma->rx_buf + i * RX_BUF_SIZE; + ctrl = 0; + /* if last descr set endOfTable */ + if (i == (RX_BUF_NUM - 1)) + ctrl = D64_CTRL1_EOT; + descp->ctrl1 = ctrl; + descp->ctrl2 = RX_BUF_SIZE; + descp->addrlow = (uint32_t)bufp; + descp->addrhigh = 0; + + last_desc = ((uint32_t)(descp) & D64_XP_LD_MASK) + + sizeof(dma64dd_t); + } + + descp = dma->rx_desc_aligned; + bufp = dma->rx_buf; + /* flush descriptor and buffer */ + flush_dcache_range((unsigned long)descp, + (unsigned long)(descp + + sizeof(dma64dd_t) * RX_BUF_NUM)); + flush_dcache_range((unsigned long)(bufp), + (unsigned long)(bufp + RX_BUF_SIZE * RX_BUF_NUM)); + + /* initailize the DMA channel */ + writel((uint32_t)descp, GMAC0_DMA_RX_ADDR_LOW_ADDR); + writel(0, GMAC0_DMA_RX_ADDR_HIGH_ADDR); + + /* now update the dma last descriptor */ + writel(last_desc, GMAC0_DMA_RX_PTR_ADDR); + + return 0; +} + +static int dma_init(struct eth_dma *dma) +{ + debug(" %s enter\n", __func__); + + /* + * Default flags: For backwards compatibility both + * Rx Overflow Continue and Parity are DISABLED. + */ + dma_ctrlflags(DMA_CTRL_ROC | DMA_CTRL_PEN, 0); + + debug("rx burst len 0x%x\n", + (readl(GMAC0_DMA_RX_CTRL_ADDR) & D64_RC_BL_MASK) + >> D64_RC_BL_SHIFT); + debug("tx burst len 0x%x\n", + (readl(GMAC0_DMA_TX_CTRL_ADDR) & D64_XC_BL_MASK) + >> D64_XC_BL_SHIFT); + + dma_tx_init(dma); + dma_rx_init(dma); + + /* From end of chip_init() */ + /* enable the overflow continue feature and disable parity */ + dma_ctrlflags(DMA_CTRL_ROC | DMA_CTRL_PEN /* mask */, + DMA_CTRL_ROC /* value */); + + return 0; +} + +static int dma_deinit(struct eth_dma *dma) +{ + debug(" %s enter\n", __func__); + + gmac_disable_dma(dma, MAC_DMA_RX); + gmac_disable_dma(dma, MAC_DMA_TX); + + free(dma->tx_buf); + dma->tx_buf = NULL; + free(dma->tx_desc); + dma->tx_desc = NULL; + dma->tx_desc_aligned = NULL; + + free(dma->rx_buf); + dma->rx_buf = NULL; + free(dma->rx_desc); + dma->rx_desc = NULL; + dma->rx_desc_aligned = NULL; + + return 0; +} + +int gmac_tx_packet(struct eth_dma *dma, void *packet, int length) +{ + uint8_t *bufp = dma->tx_buf + dma->cur_tx_index * TX_BUF_SIZE; + + /* kick off the dma */ + size_t len = length; + int txout = dma->cur_tx_index; + uint32_t flags; + dma64dd_t *descp = NULL; + uint32_t ctrl; + uint32_t last_desc = (((uint32_t)dma->tx_desc_aligned) + + sizeof(dma64dd_t)) & D64_XP_LD_MASK; + size_t buflen; + + debug("%s enter\n", __func__); + + /* load the buffer */ + memcpy(bufp, packet, len); + + /* Add 4 bytes for Ethernet FCS/CRC */ + buflen = len + 4; + + ctrl = (buflen & D64_CTRL2_BC_MASK); + + /* the transmit will only be one frame or set SOF, EOF */ + /* also set int on completion */ + flags = D64_CTRL1_SOF | D64_CTRL1_IOC | D64_CTRL1_EOF; + + /* txout points to the descriptor to uset */ + /* if last descriptor then set EOT */ + if (txout == (TX_BUF_NUM - 1)) { + flags |= D64_CTRL1_EOT; + last_desc = ((uint32_t)(dma->tx_desc_aligned)) & D64_XP_LD_MASK; + } + + /* write the descriptor */ + descp = ((dma64dd_t *)(dma->tx_desc_aligned)) + txout; + descp->addrlow = (uint32_t)bufp; + descp->addrhigh = 0; + descp->ctrl1 = flags; + descp->ctrl2 = ctrl; + + /* flush descriptor and buffer */ + flush_dcache_range((unsigned long)descp, + (unsigned long)(descp + sizeof(dma64dd_t))); + flush_dcache_range((unsigned long)bufp, + (unsigned long)(bufp + TX_BUF_SIZE)); + + /* now update the dma last descriptor */ + writel(last_desc, GMAC0_DMA_TX_PTR_ADDR); + + /* tx dma should be enabled so packet should go out */ + + /* update txout */ + dma->cur_tx_index = (txout + 1) & (TX_BUF_NUM - 1); + + return 0; +} + +bool gmac_check_tx_done(struct eth_dma *dma) +{ + /* wait for tx to complete */ + uint32_t intstatus; + bool xfrdone = false; + + debug("%s enter\n", __func__); + + intstatus = readl(GMAC0_INT_STATUS_ADDR); + + debug("int(0x%x)\n", intstatus); + if (intstatus & (I_XI0 | I_XI1 | I_XI2 | I_XI3)) { + xfrdone = true; + /* clear the int bits */ + intstatus &= ~(I_XI0 | I_XI1 | I_XI2 | I_XI3); + writel(intstatus, GMAC0_INT_STATUS_ADDR); + } else { + debug("Tx int(0x%x)\n", intstatus); + } + + return xfrdone; +} + +int gmac_check_rx_done(struct eth_dma *dma, uint8_t *buf) +{ + void *bufp, *datap; + size_t rcvlen = 0, buflen = 0; + uint32_t stat0 = 0, stat1 = 0; + uint32_t control, offset; + uint8_t statbuf[HWRXOFF*2]; + + int index, curr, active; + dma64dd_t *descp = NULL; + + /* udelay(50); */ + + /* + * this api will check if a packet has been received. + * If so it will return the address of the buffer and current + * descriptor index will be incremented to the + * next descriptor. Once done with the frame the buffer should be + * added back onto the descriptor and the lastdscr should be updated + * to this descriptor. + */ + index = dma->cur_rx_index; + offset = (uint32_t)(dma->rx_desc_aligned); + stat0 = readl(GMAC0_DMA_RX_STATUS0_ADDR) & D64_RS0_CD_MASK; + stat1 = readl(GMAC0_DMA_RX_STATUS1_ADDR) & D64_RS0_CD_MASK; + curr = ((stat0 - offset) & D64_RS0_CD_MASK) / sizeof(dma64dd_t); + active = ((stat1 - offset) & D64_RS0_CD_MASK) / sizeof(dma64dd_t); + + /* check if any frame */ + if (index == curr) + return -1; + + debug("received packet\n"); + debug("expect(0x%x) curr(0x%x) active(0x%x)\n", index, curr, active); + /* remove warning */ + if (index == active) + ; + + /* get the packet pointer that corresponds to the rx descriptor */ + bufp = dma->rx_buf + index * RX_BUF_SIZE; + + descp = (dma64dd_t *)(dma->rx_desc_aligned) + index; + /* flush descriptor and buffer */ + flush_dcache_range((unsigned long)descp, + (unsigned long)(descp + sizeof(dma64dd_t))); + flush_dcache_range((unsigned long)bufp, + (unsigned long)(bufp + RX_BUF_SIZE)); + + buflen = (descp->ctrl2 & D64_CTRL2_BC_MASK); + + stat0 = readl(GMAC0_DMA_RX_STATUS0_ADDR); + stat1 = readl(GMAC0_DMA_RX_STATUS1_ADDR); + + debug("bufp(0x%x) index(0x%x) buflen(0x%x) stat0(0x%x) stat1(0x%x)\n", + (uint32_t)bufp, index, buflen, stat0, stat1); + + dma->cur_rx_index = (index + 1) & (RX_BUF_NUM - 1); + + /* get buffer offset */ + control = readl(GMAC0_DMA_RX_CTRL_ADDR); + offset = (control & D64_RC_RO_MASK) >> D64_RC_RO_SHIFT; + rcvlen = *(uint16_t *)bufp; + + debug("Received %d bytes\n", rcvlen); + /* copy status into temp buf then copy data from rx buffer */ + memcpy(statbuf, bufp, offset); + datap = (void *)((uint32_t)bufp + offset); + memcpy(buf, datap, rcvlen); + + /* update descriptor that is being added back on ring */ + descp->ctrl2 = RX_BUF_SIZE; + descp->addrlow = (uint32_t)bufp; + descp->addrhigh = 0; + /* flush descriptor */ + flush_dcache_range((unsigned long)descp, + (unsigned long)(descp + sizeof(dma64dd_t))); + + /* set the lastdscr for the rx ring */ + writel(((uint32_t)descp) & D64_XP_LD_MASK, GMAC0_DMA_RX_PTR_ADDR); + + return (int)rcvlen; +} + +static int gmac_disable_dma(struct eth_dma *dma, int dir) +{ + int status; + + debug("%s enter\n", __func__); + + if (dir == MAC_DMA_TX) { + /* address PR8249/PR7577 issue */ + /* suspend tx DMA first */ + writel(D64_XC_SE, GMAC0_DMA_TX_CTRL_ADDR); + SPINWAIT(((status = (readl(GMAC0_DMA_TX_STATUS0_ADDR) & + D64_XS0_XS_MASK)) != + D64_XS0_XS_DISABLED) && + (status != D64_XS0_XS_IDLE) && + (status != D64_XS0_XS_STOPPED), 10000); + + /* + * PR2414 WAR: DMA engines are not disabled until + * transfer finishes + */ + writel(0, GMAC0_DMA_TX_CTRL_ADDR); + SPINWAIT(((status = (readl(GMAC0_DMA_TX_STATUS0_ADDR) & + D64_XS0_XS_MASK)) != + D64_XS0_XS_DISABLED), 10000); + + /* wait for the last transaction to complete */ + udelay(2); + + status = (status == D64_XS0_XS_DISABLED); + } else { + /* + * PR2414 WAR: DMA engines are not disabled until + * transfer finishes + */ + writel(0, GMAC0_DMA_RX_CTRL_ADDR); + SPINWAIT(((status = (readl(GMAC0_DMA_RX_STATUS0_ADDR) & + D64_RS0_RS_MASK)) != + D64_RS0_RS_DISABLED), 10000); + + status = (status == D64_RS0_RS_DISABLED); + } + + return status; +} + +static int gmac_enable_dma(struct eth_dma *dma, int dir) +{ + uint32_t control; + + debug("%s enter\n", __func__); + + if (dir == MAC_DMA_TX) { + dma->cur_tx_index = 0; + + /* + * These bits 20:18 (burstLen) of control register can be + * written but will take effect only if these bits are + * valid. So this will not affect previous versions + * of the DMA. They will continue to have those bits set to 0. + */ + control = readl(GMAC0_DMA_TX_CTRL_ADDR); + + control |= D64_XC_XE; + if ((g_dmactrlflags & DMA_CTRL_PEN) == 0) + control |= D64_XC_PD; + + writel(control, GMAC0_DMA_TX_CTRL_ADDR); + + /* initailize the DMA channel */ + writel((uint32_t)(dma->tx_desc_aligned), + GMAC0_DMA_TX_ADDR_LOW_ADDR); + writel(0, GMAC0_DMA_TX_ADDR_HIGH_ADDR); + } else { + dma->cur_rx_index = 0; + + control = (readl(GMAC0_DMA_RX_CTRL_ADDR) & + D64_RC_AE) | D64_RC_RE; + + if ((g_dmactrlflags & DMA_CTRL_PEN) == 0) + control |= D64_RC_PD; + + if (g_dmactrlflags & DMA_CTRL_ROC) + control |= D64_RC_OC; + + /* + * These bits 20:18 (burstLen) of control register can be + * written but will take effect only if these bits are + * valid. So this will not affect previous versions + * of the DMA. They will continue to have those bits set to 0. + */ + control &= ~D64_RC_BL_MASK; + /* Keep default Rx burstlen */ + control |= readl(GMAC0_DMA_RX_CTRL_ADDR) & D64_RC_BL_MASK; + control |= HWRXOFF << D64_RC_RO_SHIFT; + + writel(control, GMAC0_DMA_RX_CTRL_ADDR); + + /* + * the rx descriptor ring should have + * the addresses set properly; + * set the lastdscr for the rx ring + */ + writel(((uint32_t)(dma->rx_desc_aligned) + + (RX_BUF_NUM - 1) * RX_BUF_SIZE) & + D64_XP_LD_MASK, GMAC0_DMA_RX_PTR_ADDR); + } + + return 0; +} + +bool gmac_mii_busywait(unsigned int timeout) +{ + uint32_t tmp = 0; + + while (timeout > 10) { + tmp = readl(GMAC_MII_CTRL_ADDR); + if (tmp & (1 << GMAC_MII_BUSY_SHIFT)) { + udelay(10); + timeout -= 10; + } else { + break; + } + } + return tmp & (1 << GMAC_MII_BUSY_SHIFT); +} + +int gmac_miiphy_read(const char *devname, unsigned char phyaddr, + unsigned char reg, unsigned short *value) +{ + uint32_t tmp = 0; + + (void)devname; + + /* Busy wait timeout is 1ms */ + if (gmac_mii_busywait(1000)) { + error("%s: Prepare MII read: MII/MDIO busy\n", __func__); + return -1; + } + + /* Read operation */ + tmp = GMAC_MII_DATA_READ_CMD; + tmp |= (phyaddr << GMAC_MII_PHY_ADDR_SHIFT) | + (reg << GMAC_MII_PHY_REG_SHIFT); + debug("MII read cmd 0x%x, phy 0x%x, reg 0x%x\n", tmp, phyaddr, reg); + writel(tmp, GMAC_MII_DATA_ADDR); + + if (gmac_mii_busywait(1000)) { + error("%s: MII read failure: MII/MDIO busy\n", __func__); + return -1; + } + + *value = readl(GMAC_MII_DATA_ADDR) & 0xffff; + debug("MII read data 0x%x\n", *value); + return 0; +} + +int gmac_miiphy_write(const char *devname, unsigned char phyaddr, + unsigned char reg, unsigned short value) +{ + uint32_t tmp = 0; + + (void)devname; + + /* Busy wait timeout is 1ms */ + if (gmac_mii_busywait(1000)) { + error("%s: Prepare MII write: MII/MDIO busy\n", __func__); + return -1; + } + + /* Write operation */ + tmp = GMAC_MII_DATA_WRITE_CMD | (value & 0xffff); + tmp |= ((phyaddr << GMAC_MII_PHY_ADDR_SHIFT) | + (reg << GMAC_MII_PHY_REG_SHIFT)); + debug("MII write cmd 0x%x, phy 0x%x, reg 0x%x, data 0x%x\n", + tmp, phyaddr, reg, value); + writel(tmp, GMAC_MII_DATA_ADDR); + + if (gmac_mii_busywait(1000)) { + error("%s: MII write failure: MII/MDIO busy\n", __func__); + return -1; + } + + return 0; +} + +void gmac_init_reset(void) +{ + debug("%s enter\n", __func__); + + /* set command config reg CC_SR */ + reg32_set_bits(UNIMAC0_CMD_CFG_ADDR, CC_SR); + udelay(GMAC_RESET_DELAY); +} + +void gmac_clear_reset(void) +{ + debug("%s enter\n", __func__); + + /* clear command config reg CC_SR */ + reg32_clear_bits(UNIMAC0_CMD_CFG_ADDR, CC_SR); + udelay(GMAC_RESET_DELAY); +} + +static void gmac_enable_local(bool en) +{ + uint32_t cmdcfg; + + debug("%s enter\n", __func__); + + /* read command config reg */ + cmdcfg = readl(UNIMAC0_CMD_CFG_ADDR); + + /* put mac in reset */ + gmac_init_reset(); + + cmdcfg |= CC_SR; + + /* first deassert rx_ena and tx_ena while in reset */ + cmdcfg &= ~(CC_RE | CC_TE); + /* write command config reg */ + writel(cmdcfg, UNIMAC0_CMD_CFG_ADDR); + + /* bring mac out of reset */ + gmac_clear_reset(); + + /* if not enable exit now */ + if (!en) + return; + + /* enable the mac transmit and receive paths now */ + udelay(2); + cmdcfg &= ~CC_SR; + cmdcfg |= (CC_RE | CC_TE); + + /* assert rx_ena and tx_ena when out of reset to enable the mac */ + writel(cmdcfg, UNIMAC0_CMD_CFG_ADDR); + + return; +} + +int gmac_enable(void) +{ + gmac_enable_local(1); + + /* clear interrupts */ + writel(I_INTMASK, GMAC0_INT_STATUS_ADDR); + return 0; +} + +int gmac_disable(void) +{ + gmac_enable_local(0); + return 0; +} + +int gmac_set_speed(int speed, int duplex) +{ + uint32_t cmdcfg; + uint32_t hd_ena; + uint32_t speed_cfg; + + hd_ena = duplex ? 0 : CC_HD; + if (speed == 1000) { + speed_cfg = 2; + } else if (speed == 100) { + speed_cfg = 1; + } else if (speed == 10) { + speed_cfg = 0; + } else { + error("%s: Invalid GMAC speed(%d)!\n", __func__, speed); + return -1; + } + + cmdcfg = readl(UNIMAC0_CMD_CFG_ADDR); + cmdcfg &= ~(CC_ES_MASK | CC_HD); + cmdcfg |= ((speed_cfg << CC_ES_SHIFT) | hd_ena); + + printf("Change GMAC speed to %dMB\n", speed); + debug("GMAC speed cfg 0x%x\n", cmdcfg); + writel(cmdcfg, UNIMAC0_CMD_CFG_ADDR); + + return 0; +} + +int gmac_set_mac_addr(unsigned char *mac) +{ + /* set our local address */ + debug("GMAC: %02x:%02x:%02x:%02x:%02x:%02x\n", + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + writel(htonl(*(uint32_t *)mac), UNIMAC0_MAC_MSB_ADDR); + writew(htons(*(uint32_t *)&mac[4]), UNIMAC0_MAC_LSB_ADDR); + + return 0; +} + +int gmac_mac_init(struct eth_device *dev) +{ + struct eth_info *eth = (struct eth_info *)(dev->priv); + struct eth_dma *dma = &(eth->dma); + + uint32_t tmp; + uint32_t cmdcfg; + int chipid; + + debug("%s enter\n", __func__); + + /* Always use GMAC0 */ + printf("Using GMAC%d\n", 0); + + /* Reset AMAC0 core */ + writel(0, AMAC0_IDM_RESET_ADDR); + tmp = readl(AMAC0_IO_CTRL_DIRECT_ADDR); + /* Set clock */ + tmp &= ~(1 << AMAC0_IO_CTRL_CLK_250_SEL_SHIFT); + tmp |= (1 << AMAC0_IO_CTRL_GMII_MODE_SHIFT); + /* Set Tx clock */ + tmp &= ~(1 << AMAC0_IO_CTRL_DEST_SYNC_MODE_EN_SHIFT); + writel(tmp, AMAC0_IO_CTRL_DIRECT_ADDR); + + /* reset gmac */ + /* + * As AMAC is just reset, NO need? + * set eth_data into loopback mode to ensure no rx traffic + * gmac_loopback(eth_data, TRUE); + * ET_TRACE(("%s gmac loopback\n", __func__)); + * udelay(1); + */ + + cmdcfg = readl(UNIMAC0_CMD_CFG_ADDR); + cmdcfg &= ~(CC_TE | CC_RE | CC_RPI | CC_TAI | CC_HD | CC_ML | + CC_CFE | CC_RL | CC_RED | CC_PE | CC_TPI | + CC_PAD_EN | CC_PF); + cmdcfg |= (CC_PROM | CC_NLC | CC_CFE); + /* put mac in reset */ + gmac_init_reset(); + writel(cmdcfg, UNIMAC0_CMD_CFG_ADDR); + gmac_clear_reset(); + + /* enable clear MIB on read */ + reg32_set_bits(GMAC0_DEV_CTRL_ADDR, DC_MROR); + /* PHY: set smi_master to drive mdc_clk */ + reg32_set_bits(GMAC0_PHY_CTRL_ADDR, PC_MTE); + + /* clear persistent sw intstatus */ + writel(0, GMAC0_INT_STATUS_ADDR); + + if (dma_init(dma) < 0) { + error("%s: GMAC dma_init failed\n", __func__); + goto err_exit; + } + + chipid = CHIPID; + printf("%s: Chip ID: 0x%x\n", __func__, chipid); + + /* set switch bypass mode */ + tmp = readl(SWITCH_GLOBAL_CONFIG_ADDR); + tmp |= (1 << CDRU_SWITCH_BYPASS_SWITCH_SHIFT); + + /* Switch mode */ + /* tmp &= ~(1 << CDRU_SWITCH_BYPASS_SWITCH_SHIFT); */ + + writel(tmp, SWITCH_GLOBAL_CONFIG_ADDR); + + tmp = readl(CRMU_CHIP_IO_PAD_CONTROL_ADDR); + tmp &= ~(1 << CDRU_IOMUX_FORCE_PAD_IN_SHIFT); + writel(tmp, CRMU_CHIP_IO_PAD_CONTROL_ADDR); + + /* Set MDIO to internal GPHY */ + tmp = readl(GMAC_MII_CTRL_ADDR); + /* Select internal MDC/MDIO bus*/ + tmp &= ~(1 << GMAC_MII_CTRL_BYP_SHIFT); + /* select MDC/MDIO connecting to on-chip internal PHYs */ + tmp &= ~(1 << GMAC_MII_CTRL_EXT_SHIFT); + /* + * give bit[6:0](MDCDIV) with required divisor to set + * the MDC clock frequency, 66MHZ/0x1A=2.5MHZ + */ + tmp |= 0x1A; + + writel(tmp, GMAC_MII_CTRL_ADDR); + + if (gmac_mii_busywait(1000)) { + error("%s: Configure MDIO: MII/MDIO busy\n", __func__); + goto err_exit; + } + + /* Configure GMAC0 */ + /* enable one rx interrupt per received frame */ + writel(1 << GMAC0_IRL_FRAMECOUNT_SHIFT, GMAC0_INTR_RECV_LAZY_ADDR); + + /* read command config reg */ + cmdcfg = readl(UNIMAC0_CMD_CFG_ADDR); + /* enable 802.3x tx flow control (honor received PAUSE frames) */ + cmdcfg &= ~CC_RPI; + /* enable promiscuous mode */ + cmdcfg |= CC_PROM; + /* Disable loopback mode */ + cmdcfg &= ~CC_ML; + /* set the speed */ + cmdcfg &= ~(CC_ES_MASK | CC_HD); + /* Set to 1Gbps and full duplex by default */ + cmdcfg |= (2 << CC_ES_SHIFT); + + /* put mac in reset */ + gmac_init_reset(); + /* write register */ + writel(cmdcfg, UNIMAC0_CMD_CFG_ADDR); + /* bring mac out of reset */ + gmac_clear_reset(); + + /* set max frame lengths; account for possible vlan tag */ + writel(PKTSIZE + 32, UNIMAC0_FRM_LENGTH_ADDR); + + return 0; + +err_exit: + dma_deinit(dma); + return -1; +} + +int gmac_add(struct eth_device *dev) +{ + struct eth_info *eth = (struct eth_info *)(dev->priv); + struct eth_dma *dma = &(eth->dma); + void *tmp; + + /* + * Desc has to be 16-byte aligned ? + * If it is 8-byte aligned by malloc, fail Tx + */ + tmp = malloc(sizeof(dma64dd_t) * TX_BUF_NUM + 8); + if (tmp == NULL) { + printf("%s: Failed to allocate TX desc Buffer\n", __func__); + return -1; + } + + dma->tx_desc = (void *)tmp; + dma->tx_desc_aligned = (void *)(((uint32_t)tmp) & (~0xf)); + debug("TX Descriptor Buffer: %p; length: 0x%x\n", + dma->tx_desc_aligned, sizeof(dma64dd_t) * TX_BUF_NUM); + + tmp = malloc(TX_BUF_SIZE * TX_BUF_NUM); + if (tmp == NULL) { + printf("%s: Failed to allocate TX Data Buffer\n", __func__); + free(dma->tx_desc); + return -1; + } + dma->tx_buf = (uint8_t *)tmp; + debug("TX Data Buffer: %p; length: 0x%x\n", + dma->tx_buf, TX_BUF_SIZE * TX_BUF_NUM); + + /* Desc has to be 16-byte aligned ? */ + tmp = malloc(sizeof(dma64dd_t) * RX_BUF_NUM + 8); + if (tmp == NULL) { + printf("%s: Failed to allocate RX Descriptor\n", __func__); + free(dma->tx_desc); + free(dma->tx_buf); + return -1; + } + dma->rx_desc = tmp; + dma->rx_desc_aligned = (void *)(((uint32_t)tmp) & (~0xf)); + debug("RX Descriptor Buffer: %p, length: 0x%x\n", + dma->rx_desc_aligned, sizeof(dma64dd_t) * RX_BUF_NUM); + + tmp = malloc(RX_BUF_SIZE * RX_BUF_NUM); + if (tmp == NULL) { + printf("%s: Failed to allocate RX Data Buffer\n", __func__); + free(dma->tx_desc); + free(dma->tx_buf); + free(dma->rx_desc); + return -1; + } + dma->rx_buf = tmp; + debug("RX Data Buffer: %p; length: 0x%x\n", + dma->rx_buf, RX_BUF_SIZE * RX_BUF_NUM); + + g_dmactrlflags = 0; + + eth->phy_interface = PHY_INTERFACE_MODE_GMII; + + dma->tx_packet = gmac_tx_packet; + dma->check_tx_done = gmac_check_tx_done; + + dma->check_rx_done = gmac_check_rx_done; + + dma->enable_dma = gmac_enable_dma; + dma->disable_dma = gmac_disable_dma; + + eth->miiphy_read = gmac_miiphy_read; + eth->miiphy_write = gmac_miiphy_write; + + eth->mac_init = gmac_mac_init; + eth->disable_mac = gmac_disable; + eth->enable_mac = gmac_enable; + eth->set_mac_addr = gmac_set_mac_addr; + eth->set_mac_speed = gmac_set_speed; + + return 0; +} diff --git a/drivers/net/bcm-sf2-eth-gmac.h b/drivers/net/bcm-sf2-eth-gmac.h new file mode 100644 index 0000000..810a61726 --- /dev/null +++ b/drivers/net/bcm-sf2-eth-gmac.h @@ -0,0 +1,224 @@ +/* + * Copyright 2014 Broadcom Corporation. + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#ifndef _BCM_SF2_ETH_GMAC_H_ +#define _BCM_SF2_ETH_GMAC_H_ + +#define BCM_SF2_ETH_MAC_NAME "gmac" + +#ifndef ETHHW_PORT_INT +#define ETHHW_PORT_INT 8 +#endif + +#define GMAC0_REG_BASE 0x18042000 +#define GMAC0_DEV_CTRL_ADDR GMAC0_REG_BASE +#define GMAC0_INT_STATUS_ADDR (GMAC0_REG_BASE + 0x020) +#define GMAC0_INTR_RECV_LAZY_ADDR (GMAC0_REG_BASE + 0x100) +#define GMAC0_PHY_CTRL_ADDR (GMAC0_REG_BASE + 0x188) + + +#define GMAC_DMA_PTR_OFFSET 0x04 +#define GMAC_DMA_ADDR_LOW_OFFSET 0x08 +#define GMAC_DMA_ADDR_HIGH_OFFSET 0x0c +#define GMAC_DMA_STATUS0_OFFSET 0x10 +#define GMAC_DMA_STATUS1_OFFSET 0x14 + +#define GMAC0_DMA_TX_CTRL_ADDR (GMAC0_REG_BASE + 0x200) +#define GMAC0_DMA_TX_PTR_ADDR \ + (GMAC0_DMA_TX_CTRL_ADDR + GMAC_DMA_PTR_OFFSET) +#define GMAC0_DMA_TX_ADDR_LOW_ADDR \ + (GMAC0_DMA_TX_CTRL_ADDR + GMAC_DMA_ADDR_LOW_OFFSET) +#define GMAC0_DMA_TX_ADDR_HIGH_ADDR \ + (GMAC0_DMA_TX_CTRL_ADDR + GMAC_DMA_ADDR_HIGH_OFFSET) +#define GMAC0_DMA_TX_STATUS0_ADDR \ + (GMAC0_DMA_TX_CTRL_ADDR + GMAC_DMA_STATUS0_OFFSET) +#define GMAC0_DMA_TX_STATUS1_ADDR \ + (GMAC0_DMA_TX_CTRL_ADDR + GMAC_DMA_STATUS1_OFFSET) + +#define GMAC0_DMA_RX_CTRL_ADDR (GMAC0_REG_BASE + 0x220) +#define GMAC0_DMA_RX_PTR_ADDR \ + (GMAC0_DMA_RX_CTRL_ADDR + GMAC_DMA_PTR_OFFSET) +#define GMAC0_DMA_RX_ADDR_LOW_ADDR \ + (GMAC0_DMA_RX_CTRL_ADDR + GMAC_DMA_ADDR_LOW_OFFSET) +#define GMAC0_DMA_RX_ADDR_HIGH_ADDR \ + (GMAC0_DMA_RX_CTRL_ADDR + GMAC_DMA_ADDR_HIGH_OFFSET) +#define GMAC0_DMA_RX_STATUS0_ADDR \ + (GMAC0_DMA_RX_CTRL_ADDR + GMAC_DMA_STATUS0_OFFSET) +#define GMAC0_DMA_RX_STATUS1_ADDR \ + (GMAC0_DMA_RX_CTRL_ADDR + GMAC_DMA_STATUS1_OFFSET) + +#define UNIMAC0_CMD_CFG_ADDR (GMAC0_REG_BASE + 0x808) +#define UNIMAC0_MAC_MSB_ADDR (GMAC0_REG_BASE + 0x80c) +#define UNIMAC0_MAC_LSB_ADDR (GMAC0_REG_BASE + 0x810) +#define UNIMAC0_FRM_LENGTH_ADDR (GMAC0_REG_BASE + 0x814) + +#define GMAC0_IRL_FRAMECOUNT_SHIFT 24 + +/* transmit channel control */ +/* transmit enable */ +#define D64_XC_XE 0x00000001 +/* transmit suspend request */ +#define D64_XC_SE 0x00000002 +/* parity check disable */ +#define D64_XC_PD 0x00000800 +/* BurstLen bits */ +#define D64_XC_BL_MASK 0x001C0000 +#define D64_XC_BL_SHIFT 18 + +/* transmit descriptor table pointer */ +/* last valid descriptor */ +#define D64_XP_LD_MASK 0x00001fff + +/* transmit channel status */ +/* transmit state */ +#define D64_XS0_XS_MASK 0xf0000000 +#define D64_XS0_XS_SHIFT 28 +#define D64_XS0_XS_DISABLED 0x00000000 +#define D64_XS0_XS_ACTIVE 0x10000000 +#define D64_XS0_XS_IDLE 0x20000000 +#define D64_XS0_XS_STOPPED 0x30000000 +#define D64_XS0_XS_SUSP 0x40000000 + +/* receive channel control */ +/* receive enable */ +#define D64_RC_RE 0x00000001 +/* address extension bits */ +#define D64_RC_AE 0x00030000 +/* overflow continue */ +#define D64_RC_OC 0x00000400 +/* parity check disable */ +#define D64_RC_PD 0x00000800 +/* receive frame offset */ +#define D64_RC_RO_MASK 0x000000fe +#define D64_RC_RO_SHIFT 1 +/* BurstLen bits */ +#define D64_RC_BL_MASK 0x001C0000 +#define D64_RC_BL_SHIFT 18 + +/* flags for dma controller */ +/* partity enable */ +#define DMA_CTRL_PEN (1 << 0) +/* rx overflow continue */ +#define DMA_CTRL_ROC (1 << 1) + +/* receive descriptor table pointer */ +/* last valid descriptor */ +#define D64_RP_LD_MASK 0x00001fff + +/* receive channel status */ +/* current descriptor pointer */ +#define D64_RS0_CD_MASK 0x00001fff +/* receive state */ +#define D64_RS0_RS_MASK 0xf0000000 +#define D64_RS0_RS_SHIFT 28 +#define D64_RS0_RS_DISABLED 0x00000000 +#define D64_RS0_RS_ACTIVE 0x10000000 +#define D64_RS0_RS_IDLE 0x20000000 +#define D64_RS0_RS_STOPPED 0x30000000 +#define D64_RS0_RS_SUSP 0x40000000 + +/* descriptor control flags 1 */ +/* core specific flags */ +#define D64_CTRL_COREFLAGS 0x0ff00000 +/* end of descriptor table */ +#define D64_CTRL1_EOT ((uint32_t)1 << 28) +/* interrupt on completion */ +#define D64_CTRL1_IOC ((uint32_t)1 << 29) +/* end of frame */ +#define D64_CTRL1_EOF ((uint32_t)1 << 30) +/* start of frame */ +#define D64_CTRL1_SOF ((uint32_t)1 << 31) + +/* descriptor control flags 2 */ +/* buffer byte count. real data len must <= 16KB */ +#define D64_CTRL2_BC_MASK 0x00007fff +/* address extension bits */ +#define D64_CTRL2_AE 0x00030000 +#define D64_CTRL2_AE_SHIFT 16 +/* parity bit */ +#define D64_CTRL2_PARITY 0x00040000 +/* control flags in the range [27:20] are core-specific and not defined here */ +#define D64_CTRL_CORE_MASK 0x0ff00000 + +#define DC_MROR 0x00000010 +#define PC_MTE 0x00800000 + +/* command config */ +#define CC_TE 0x00000001 +#define CC_RE 0x00000002 +#define CC_ES_MASK 0x0000000c +#define CC_ES_SHIFT 2 +#define CC_PROM 0x00000010 +#define CC_PAD_EN 0x00000020 +#define CC_CF 0x00000040 +#define CC_PF 0x00000080 +#define CC_RPI 0x00000100 +#define CC_TAI 0x00000200 +#define CC_HD 0x00000400 +#define CC_HD_SHIFT 10 +#define CC_SR 0x00002000 +#define CC_ML 0x00008000 +#define CC_AE 0x00400000 +#define CC_CFE 0x00800000 +#define CC_NLC 0x01000000 +#define CC_RL 0x02000000 +#define CC_RED 0x04000000 +#define CC_PE 0x08000000 +#define CC_TPI 0x10000000 +#define CC_AT 0x20000000 + +#define I_PDEE 0x00000400 +#define I_PDE 0x00000800 +#define I_DE 0x00001000 +#define I_RDU 0x00002000 +#define I_RFO 0x00004000 +#define I_XFU 0x00008000 +#define I_RI 0x00010000 +#define I_XI0 0x01000000 +#define I_XI1 0x02000000 +#define I_XI2 0x04000000 +#define I_XI3 0x08000000 +#define I_ERRORS (I_PDEE | I_PDE | I_DE | I_RDU | I_RFO | I_XFU) +#define DEF_INTMASK (I_XI0 | I_XI1 | I_XI2 | I_XI3 | I_RI | I_ERRORS) + +#define I_INTMASK 0x0f01fcff + +#define CHIP_DRU_BASE 0x0301d000 +#define CRMU_CHIP_IO_PAD_CONTROL_ADDR (CHIP_DRU_BASE + 0x0bc) +#define SWITCH_GLOBAL_CONFIG_ADDR (CHIP_DRU_BASE + 0x194) + +#define CDRU_IOMUX_FORCE_PAD_IN_SHIFT 0 +#define CDRU_SWITCH_BYPASS_SWITCH_SHIFT 13 + +#define AMAC0_IDM_RESET_ADDR 0x18110800 +#define AMAC0_IO_CTRL_DIRECT_ADDR 0x18110408 +#define AMAC0_IO_CTRL_CLK_250_SEL_SHIFT 6 +#define AMAC0_IO_CTRL_GMII_MODE_SHIFT 5 +#define AMAC0_IO_CTRL_DEST_SYNC_MODE_EN_SHIFT 3 + +#define CHIPA_CHIP_ID_ADDR 0x18000000 +#define CHIPID (readl(CHIPA_CHIP_ID_ADDR) & 0xFFFF) +#define CHIPREV (((readl(CHIPA_CHIP_ID_ADDR) >> 16) & 0xF) +#define CHIPSKU (((readl(CHIPA_CHIP_ID_ADDR) >> 20) & 0xF) + +#define GMAC_MII_CTRL_ADDR 0x18002000 +#define GMAC_MII_CTRL_BYP_SHIFT 10 +#define GMAC_MII_CTRL_EXT_SHIFT 9 +#define GMAC_MII_DATA_ADDR 0x18002004 +#define GMAC_MII_DATA_READ_CMD 0x60020000 +#define GMAC_MII_DATA_WRITE_CMD 0x50020000 +#define GMAC_MII_BUSY_SHIFT 8 +#define GMAC_MII_PHY_ADDR_SHIFT 23 +#define GMAC_MII_PHY_REG_SHIFT 18 + +#define GMAC_RESET_DELAY 2 +#define HWRXOFF 30 +#define MAXNAMEL 8 +#define NUMTXQ 4 + +int gmac_add(struct eth_device *dev); + +#endif /* _BCM_SF2_ETH_GMAC_H_ */ diff --git a/drivers/net/bcm-sf2-eth.c b/drivers/net/bcm-sf2-eth.c new file mode 100644 index 0000000..5252d49 --- /dev/null +++ b/drivers/net/bcm-sf2-eth.c @@ -0,0 +1,268 @@ +/* + * Copyright 2014 Broadcom Corporation. + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include +#include +#include +#include + +#include +#include + +#include + +#include +#include "bcm-sf2-eth.h" + +#if defined(CONFIG_BCM_SF2_ETH_GMAC) +#include "bcm-sf2-eth-gmac.h" +#else +#error "bcm_sf2_eth: NEED to define a MAC!" +#endif + +#define BCM_NET_MODULE_DESCRIPTION "Broadcom Starfighter2 Ethernet driver" +#define BCM_NET_MODULE_VERSION "0.1" +#define BCM_SF2_ETH_DEV_NAME "bcm_sf2" + +static const char banner[] = + BCM_NET_MODULE_DESCRIPTION " " BCM_NET_MODULE_VERSION "\n"; + +static int bcm_sf2_eth_init(struct eth_device *dev) +{ + struct eth_info *eth = (struct eth_info *)(dev->priv); + struct eth_dma *dma = &(eth->dma); + struct phy_device *phydev; + int rc = 0; + int i; + + rc = eth->mac_init(dev); + if (rc) { + error("%s: Couldn't cofigure MAC!\n", __func__); + return rc; + } + + /* disable DMA */ + dma->disable_dma(dma, MAC_DMA_RX); + dma->disable_dma(dma, MAC_DMA_TX); + + eth->port_num = 0; + debug("Connecting PHY 0...\n"); + phydev = phy_connect(miiphy_get_dev_by_name(dev->name), + 0, dev, eth->phy_interface); + if (phydev != NULL) { + eth->port[0] = phydev; + eth->port_num += 1; + } else { + debug("No PHY found for port 0\n"); + } + + for (i = 0; i < eth->port_num; i++) + phy_config(eth->port[i]); + + return rc; +} + +/* + * u-boot net functions + */ + +static int bcm_sf2_eth_send(struct eth_device *dev, void *packet, int length) +{ + struct eth_dma *dma = &(((struct eth_info *)(dev->priv))->dma); + uint8_t *buf = (uint8_t *)packet; + int rc = 0; + int i = 0; + + debug("%s enter\n", __func__); + + /* load buf and start transmit */ + rc = dma->tx_packet(dma, buf, length); + if (rc) { + debug("ERROR - Tx failed\n"); + return rc; + } + + while (!(dma->check_tx_done(dma))) { + udelay(100); + debug("."); + i++; + if (i > 20) { + error("%s: Tx timeout: retried 20 times\n", __func__); + rc = -1; + break; + } + } + + debug("%s exit rc(0x%x)\n", __func__, rc); + return rc; +} + +static int bcm_sf2_eth_receive(struct eth_device *dev) +{ + struct eth_dma *dma = &(((struct eth_info *)(dev->priv))->dma); + uint8_t *buf = (uint8_t *)NetRxPackets[0]; + int rcvlen; + int rc = 0; + int i = 0; + + while (1) { + /* Poll Rx queue to get a packet */ + rcvlen = dma->check_rx_done(dma, buf); + if (rcvlen < 0) { + /* No packet received */ + rc = -1; + debug("\nNO More Rx\n"); + break; + } else if ((rcvlen == 0) || (rcvlen > RX_BUF_SIZE)) { + error("%s: Wrong Ethernet packet size (%d B), skip!\n", + __func__, rcvlen); + break; + } else { + debug("recieved\n"); + + /* Forward received packet to uboot network handler */ + NetReceive(buf, rcvlen); + + if (++i >= PKTBUFSRX) + i = 0; + buf = NetRxPackets[i]; + } + } + + return rc; +} + +static int bcm_sf2_eth_write_hwaddr(struct eth_device *dev) +{ + struct eth_info *eth = (struct eth_info *)(dev->priv); + + printf(" ETH MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", + dev->enetaddr[0], dev->enetaddr[1], dev->enetaddr[2], + dev->enetaddr[3], dev->enetaddr[4], dev->enetaddr[5]); + + return eth->set_mac_addr(dev->enetaddr); +} + +static int bcm_sf2_eth_open(struct eth_device *dev, bd_t *bt) +{ + struct eth_info *eth = (struct eth_info *)(dev->priv); + struct eth_dma *dma = &(eth->dma); + int i; + + debug("Enabling BCM SF2 Ethernet.\n"); + + /* Set MAC address from env */ + if (bcm_sf2_eth_write_hwaddr(dev) != 0) { + error("%s: MAC set error when opening !\n", __func__); + return -1; + } + + eth->enable_mac(); + + /* enable tx and rx DMA */ + dma->enable_dma(dma, MAC_DMA_RX); + dma->enable_dma(dma, MAC_DMA_TX); + + /* + * Need to start PHY here because link speed can change + * before each ethernet operation + */ + for (i = 0; i < eth->port_num; i++) { + if (phy_startup(eth->port[i])) { + error("%s: PHY %d startup failed!\n", __func__, i); + if (i == CONFIG_BCM_SF2_ETH_DEFAULT_PORT) { + error("%s: No default port %d!\n", __func__, i); + return -1; + } + } + } + + /* Set MAC speed using default port */ + i = CONFIG_BCM_SF2_ETH_DEFAULT_PORT; + debug("PHY %d: speed:%d, duplex:%d, link:%d\n", i, + eth->port[i]->speed, eth->port[i]->duplex, eth->port[i]->link); + eth->set_mac_speed(eth->port[i]->speed, eth->port[i]->duplex); + + debug("Enable Ethernet Done.\n"); + + return 0; +} + +static void bcm_sf2_eth_close(struct eth_device *dev) +{ + struct eth_info *eth = (struct eth_info *)(dev->priv); + struct eth_dma *dma = &(eth->dma); + + /* disable DMA */ + dma->disable_dma(dma, MAC_DMA_RX); + dma->disable_dma(dma, MAC_DMA_TX); + + eth->disable_mac(); +} + +int bcm_sf2_eth_register(bd_t *bis, u8 dev_num) +{ + struct eth_device *dev; + struct eth_info *eth; + int rc; + + dev = (struct eth_device *)malloc(sizeof(struct eth_device)); + if (dev == NULL) { + error("%s: Not enough memory!\n", __func__); + return -1; + } + + eth = (struct eth_info *)malloc(sizeof(struct eth_info)); + if (eth == NULL) { + error("%s: Not enough memory!\n", __func__); + return -1; + } + + printf(banner); + + memset(dev, 0, sizeof(*dev)); + sprintf(dev->name, "%s_%s-%hu", BCM_SF2_ETH_DEV_NAME, + BCM_SF2_ETH_MAC_NAME, dev_num); + + dev->priv = (void *)eth; + dev->iobase = 0; + + dev->init = bcm_sf2_eth_open; + dev->halt = bcm_sf2_eth_close; + dev->send = bcm_sf2_eth_send; + dev->recv = bcm_sf2_eth_receive; + dev->write_hwaddr = bcm_sf2_eth_write_hwaddr; + +#ifdef CONFIG_BCM_SF2_ETH_GMAC + if (gmac_add(dev)) { + free(eth); + free(dev); + error("%s: Adding GMAC failed!\n", __func__); + return -1; + } +#else +#error "bcm_sf2_eth: NEED to register a MAC!" +#endif + + eth_register(dev); + +#ifdef CONFIG_CMD_MII + miiphy_register(dev->name, eth->miiphy_read, eth->miiphy_write); +#endif + + /* Initialization */ + debug("Ethernet initialization ..."); + + rc = bcm_sf2_eth_init(dev); + if (rc != 0) { + error("%s: configuration failed!\n", __func__); + return -1; + } + + printf("Basic ethernet functionality initialized\n"); + + return 0; +} diff --git a/drivers/net/bcm-sf2-eth.h b/drivers/net/bcm-sf2-eth.h new file mode 100644 index 0000000..49a5836 --- /dev/null +++ b/drivers/net/bcm-sf2-eth.h @@ -0,0 +1,70 @@ +/* + * Copyright 2014 Broadcom Corporation. + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#ifndef _BCM_SF2_ETH_H_ +#define _BCM_SF2_ETH_H_ + +#include + +#define RX_BUF_SIZE 2048 +/* RX_BUF_NUM must be power of 2 */ +#define RX_BUF_NUM 32 + +#define TX_BUF_SIZE 2048 +/* TX_BUF_NUM must be power of 2 */ +#define TX_BUF_NUM 2 + +/* Support 2 Ethernet ports now */ +#define BCM_ETH_MAX_PORT_NUM 2 + +#define CONFIG_BCM_SF2_ETH_DEFAULT_PORT 0 + +enum { + MAC_DMA_TX = 1, + MAC_DMA_RX = 2 +}; + +struct eth_dma { + void *tx_desc_aligned; + void *rx_desc_aligned; + void *tx_desc; + void *rx_desc; + + uint8_t *tx_buf; + uint8_t *rx_buf; + + int cur_tx_index; + int cur_rx_index; + + int (*tx_packet)(struct eth_dma *dma, void *packet, int length); + bool (*check_tx_done)(struct eth_dma *dma); + + int (*check_rx_done)(struct eth_dma *dma, uint8_t *buf); + + int (*enable_dma)(struct eth_dma *dma, int dir); + int (*disable_dma)(struct eth_dma *dma, int dir); +}; + +struct eth_info { + struct eth_dma dma; + phy_interface_t phy_interface; + struct phy_device *port[BCM_ETH_MAX_PORT_NUM]; + int port_num; + + int (*miiphy_read)(const char *devname, unsigned char phyaddr, + unsigned char reg, unsigned short *value); + int (*miiphy_write)(const char *devname, unsigned char phyaddr, + unsigned char reg, unsigned short value); + + int (*mac_init)(struct eth_device *dev); + int (*enable_mac)(void); + int (*disable_mac)(void); + int (*set_mac_addr)(unsigned char *mac); + int (*set_mac_speed)(int speed, int duplex); + +}; + +#endif /* _BCM_SF2_ETH_H_ */ diff --git a/include/netdev.h b/include/netdev.h index e45dd7a..b3b5930 100644 --- a/include/netdev.h +++ b/include/netdev.h @@ -31,6 +31,7 @@ int altera_tse_initialize(u8 dev_num, int mac_base, int at91emac_register(bd_t *bis, unsigned long iobase); int au1x00_enet_initialize(bd_t*); int ax88180_initialize(bd_t *bis); +int bcm_sf2_eth_register(bd_t *bis, u8 dev_num); int bfin_EMAC_initialize(bd_t *bis); int calxedaxgmac_initialize(u32 id, ulong base_addr); int cs8900_initialize(u8 dev_num, int base_addr);