Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754014AbbHEN4O (ORCPT ); Wed, 5 Aug 2015 09:56:14 -0400 Received: from mail-bl2on0125.outbound.protection.outlook.com ([65.55.169.125]:24768 "EHLO na01-bl2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752567AbbHENoN (ORCPT ); Wed, 5 Aug 2015 09:44:13 -0400 Authentication-Results: spf=fail (sender IP is 192.88.158.2) smtp.mailfrom=freescale.com; freescale.mail.onmicrosoft.com; dkim=none (message not signed) header.d=none; From: To: CC: , , , , , , , , Igal Liberman Subject: [v4, 9/9] fsl/fman: Add FMan MAC driver Date: Wed, 5 Aug 2015 12:25:25 +0300 Message-ID: <1438766725-8053-10-git-send-email-igal.liberman@freescale.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1438766725-8053-1-git-send-email-igal.liberman@freescale.com> References: <1438766725-8053-1-git-send-email-igal.liberman@freescale.com> Reply-To: X-EOPAttributedMessage: 0 X-Microsoft-Exchange-Diagnostics: 1;BN1AFFO11FD020;1:tP1lLyrw9fXmAFASXzp/9nwP0unsp1+D4KDcuSXPSfhVEhMIHI/O+nxeRHNe4BrKOueWXQXWe2QwVsRuZtPurT03rx+1/efXzWIiaDFLWGZsUfbRN9gjyqLwdLpPImMRVmuXCbz1LuBpfStavfyjxnVesbjBH+xW7V+5rzolejBkg3YOqYKUIFt1/cjuG8KZyv55sBXwR9+V8+dqcepSAGVTXUeBDFkJUhsq1YPkvqhWz+ccoZB8P7DgyWTwj5ctFzkn2ofiV7yGwsXHx26vvMdl2Tp3Kf7FUaTj9kkNOt8pBH9vLXvDwy2mDQWEoSBXMkBmlcKrwT/XhIgw4rv8vA== X-Forefront-Antispam-Report: CIP:192.88.158.2;CTRY:US;IPV:NLI;EFV:NLI;SFV:NSPM;SFS:(10019020)(6009001)(2980300002)(339900001)(199003)(189002)(50466002)(86362001)(48376002)(85426001)(50226001)(86152002)(62966003)(46102003)(68736005)(2950100001)(575784001)(2351001)(189998001)(36756003)(77096005)(69596002)(19580405001)(77156002)(47776003)(87936001)(229853001)(50986999)(53806999)(81156007)(107886002)(97736004)(33646002)(5001960100002)(105606002)(104016003)(6806004)(5001830100001)(4001540100001)(5001860100001)(106466001)(19580395003)(76176999)(5003940100001)(43066003)(5890100001)(64706001)(110136002)(2004002)(4001430100001)(579004);DIR:OUT;SFP:1102;SCL:1;SRVR:DM2PR03MB560;H:az84smr01.freescale.net;FPR:;SPF:Fail;PTR:InfoDomainNonexistent;A:1;MX:1;LANG:en; MIME-Version: 1.0 Content-Type: text/plain X-Microsoft-Exchange-Diagnostics: 1;DM2PR03MB560;2:nqVlQehqu/mgzODRwzn3WKaEEFrLKfbgwIhjM2gWURbngbkDRoPTjuu67fzIazHNf/pIPWeZOQY7kyrY2bS/QMF7jlZw1NY2NsmQTGqLCxjxziBwRzv4QjYS1I5EDHS2Mc17xbzf8u2ifGt7d5EgRoCRWi40Ea/KTyUmwNSIUIA=;3:2ikzT84+JK602eW4aqm73emw0DWH5MayTazIopIPspyOHapfVYhILPIoMbnDMSUe5AZ0TmJXowYDGP+YNE3Ul4jXMPNEgcq+/BqcEnzeymkqgRFmf57PDEntKQULaouOWytwbKZ+aELQtQ1ZHUs5xlqVd9fFLvPGB0vZybjh53gwx/tbY8tHqn7qfc470Z7pKNcsGmDgcqVwlN3W7zI/k5D7wAJYSTeA5OHtBByx5II=;25:WbUGnbYRjhd/K20gU0ZVbV2mA0ksYH6mT/FjJx99UHk/ldl9XuKj2OhyB5JhYpJdgzwtXusL3U/b+CWdiZ/UwAJaKXMZz8eKTeeGfF2Y1TG1exRo6r3xesxe3liI02r3wzIDJ85qu8780jblHB4pS2OMWpFafvH+CyqBm6mcTMXZoVAYgqLKvT6IQ5p5/Ea7H+sgRWBnKx2TRChPcO6fZaWYhYsa6HZd3k4HXPuQwm9qfy315vvmW3w/UJv0LJA1Yq4hhlvgayxEGCMJXmKOtw== X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:DM2PR03MB560; X-Microsoft-Exchange-Diagnostics: 1;DM2PR03MB560;20:CLnZNkBsBlyOmjQihKafd4KqZauK5fJ2b51mQTweZTe6LB13fn+TQVJDCWMa+IlKxFPFNOmW2nDrKefIOZ1BW+oFn/+OZ+1e0LEH4INNYJpCSNn1+Q5laE6iIwyipjqlKEc8IbeBgDod2Iy5CzK92v1Drt9gwhsSZmMzGxmz3qieinkt7Qv1d/yQBFn8KIhPHY8t41VuQKvdTygTcHqKPfa4sdHrbRaiTdvULcS8NvRc5bSp0hT3h04dJu3NW5odz3qJALzKSS77HXiIrWbWoR0oIZlnKNqESmvR2GLvzrXMtBZGkcCGIqWlbzfQpALm5OALbl2+ZJtzZLZ7N/5oBtIpZV8RWDJ3V3LxI3siB5o=;4:rZCg2MJdvPvD0X0WXbzz1Bzu2WpSSgrZbnivPRUy15Bk5b7Ji5VlcI7Y/qRHlNAyPyByHU7lfKjOgjeacM6oQPlVInDbn2ExkjGfFg4g279yMEE3BRPan6xKjBqwPMLwADySLfQwvzn4KRTLLSkksPX4yIpAKTVmZb0rqoZXQKijzb+i0UhEQocB3bEtiO6Qa8NoubQUdDU0zBfuifn0+SUvEKSo5VyuRA5jtPQGCJt3thvWxaLqjh0NAH8JITJn9yYGaGWoECwO9cBCCb3MxSpScjO/94ldd5k9VzSALeY= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(601004)(5005006)(3002001);SRVR:DM2PR03MB560;BCL:0;PCL:0;RULEID:;SRVR:DM2PR03MB560; X-Forefront-PRVS: 06592CCE58 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;DM2PR03MB560;23:m1Xb5X3meCBx5TF8mm+zSEd42GmBUUBWYpHgHpQJFW?= =?us-ascii?Q?oBBYPFDOLaRji5M82AvP8ws7QPp8CVDL+0cHAIwNb1o+X79Uv3LoYM+cVNiX?= =?us-ascii?Q?rPkItGgbYhnL4P/gtEg1gLVMQBTbaoZg6p7NR22a9NWBgdFE8FyMlLV+U2du?= =?us-ascii?Q?3TjVUVZP2b+WBN04Vkk+yv7PxCrqF/14j7DauBgSRIrH0g2umqemKDCDHMJs?= =?us-ascii?Q?95ArwXZ5rGeLT8F5VhAP03qj0HwX0zx8eK1QHv53Db5IWud7QLiTBZTQP1ys?= =?us-ascii?Q?keLrZq18oZ9Djdp2IICdSR8bQX1w0gY3CBWoOno/CpO9nu/+DngA73wcTWwN?= =?us-ascii?Q?pw0k8RrpqJ3YY99eTqeLWNKNYxKj9wNBHMT0AusyqZqaLL0c5ECqiD1kRkVD?= =?us-ascii?Q?H4SxOOQyT5rFv4eM6HTYyG1FhvxR9NCMCI5sS3V6RsV8NKzo50Docg/ixtcZ?= =?us-ascii?Q?ToTvS2epx7OgOwFXYOHV0HGZTLQ89lfK7fQCq1f6xRTBqrjB1OUk54wpTyfk?= =?us-ascii?Q?arAMyuX+FN2XX1tdnHCpkVgKjR7kAAEv7BfxI5VHkbi9+H7N3IcytUrT5Zh0?= =?us-ascii?Q?NgmC6Vh3gdLnJPyIgAkHvju+iT44SQXqW4vXZK1AbLlVFK5chgBsAoif2TjH?= =?us-ascii?Q?vAYFH23eajIrTSeDy3pwYOIXDGl8cB9gj+tfXxdBbuSjCfEro1hyuZ0zjjgP?= =?us-ascii?Q?SlCgyvm26oLaMjf+RpOX9wyQhFypy8tjiBfxFD+xe50F+oCGZjZdyoZI5Gjs?= =?us-ascii?Q?JO85f0J1sUGn912RGv02CH2FCm/LuQuKa/Qj/0EMWoGKcKvFhZoVRj6hofvH?= =?us-ascii?Q?mjKd9YOUbcNjf08bzFEwtyP5d3C5NTcexJ0lV/PaSqLRPWbI55ALKXf0ANX+?= =?us-ascii?Q?1hJvXRi0mtYZeop1YeUzUT+EtVNqeQW8sODEb/F6LwPUvkSguTjW/MtYwXs7?= =?us-ascii?Q?yU1uigaouAiuRUTgOLjejAw5RQGG0JN+KsKXEcDyjXRb5woz9S2ul6h2fqc8?= =?us-ascii?Q?gZOmK3Tvy8A95hwppVDK58XV6w7UdNJrrT3QhIZs1s3Bv5+APnCXYZkRBkke?= =?us-ascii?Q?NT9rG4wLDZjbzkkqBHpRf52pN2PXFZ7PToohWmQJU1ZvCVm8xBQJ7gm9slDk?= =?us-ascii?Q?qnQY8Gng0iGCoP8MfdcIpNHKD7rjwg7UWSAQLGzsNFA2GiXivxD43nPZ6LdO?= =?us-ascii?Q?9CAG5fE6v61aip8J5Eh/v99jFbbayjNMd9cwfwdyUsR+EsMv4qvfu2/p8deF?= =?us-ascii?Q?57gbqf8wW4kAgH8qzKDLKtb/Q35R4JQSVLBV5VDF3nVcjI0P6n4SWATTReaQ?= =?us-ascii?Q?=3D=3D?= X-Microsoft-Exchange-Diagnostics: 1;DM2PR03MB560;5:qlUZ6UUTQQ5H7iRJILgO/irn/DJBGv0ipHSdYB+l1tUnr3e5oKYknkfZ/iTN20cCd9r1vbNcq0DaMk+p2v8Tpfyld7BLolAacy9UCycn/io6d2GFYM2GRkH56JXQmtIE0y2VxZFZUcNMvsia6BY2RQ==;24:yOOt5kD1MV62CyCdFV4tCP5NVD1EEfyvAydyYE5TBppGV7pUqfcTnM6GNZwXVnwQbUJpropUxpjZbr7BTsmXWWxdWATOAKfiunx3rtEq0Y0=;20:LG9mzNcXK8QgAQ6jTwR4/JTTd8pFvB7IcytF7DiInltbP01wV+Tou3drrM4j/Kg6wMb0e4hJFwsJhaFVBwUEnQ== X-OriginatorOrg: freescale.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Aug 2015 13:29:57.1574 (UTC) X-MS-Exchange-CrossTenant-Id: 710a03f5-10f6-4d38-9ff4-a80b81da590d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=710a03f5-10f6-4d38-9ff4-a80b81da590d;Ip=[192.88.158.2];Helo=[az84smr01.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM2PR03MB560 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 39642 Lines: 1324 From: Igal Liberman This patch adds the Ethernet MAC driver support. Signed-off-by: Igal Liberman Signed-off-by: Madalin Bucur --- drivers/net/ethernet/freescale/fman/inc/mac.h | 135 ++++ drivers/net/ethernet/freescale/fman/mac/Makefile | 3 +- drivers/net/ethernet/freescale/fman/mac/mac-api.c | 688 +++++++++++++++++++++ drivers/net/ethernet/freescale/fman/mac/mac.c | 445 +++++++++++++ 4 files changed, 1270 insertions(+), 1 deletion(-) create mode 100644 drivers/net/ethernet/freescale/fman/inc/mac.h create mode 100644 drivers/net/ethernet/freescale/fman/mac/mac-api.c create mode 100644 drivers/net/ethernet/freescale/fman/mac/mac.c diff --git a/drivers/net/ethernet/freescale/fman/inc/mac.h b/drivers/net/ethernet/freescale/fman/inc/mac.h new file mode 100644 index 0000000..f86d0bc --- /dev/null +++ b/drivers/net/ethernet/freescale/fman/inc/mac.h @@ -0,0 +1,135 @@ +/* Copyright 2008-2015 Freescale Semiconductor, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Freescale Semiconductor nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * + * ALTERNATIVELY, this software may be distributed under the terms of the + * GNU General Public License ("GPL") as published by the Free Software + * Foundation, either version 2 of that License or (at your option) any + * later version. + * + * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __MAC_H +#define __MAC_H + +#include /* struct device, BUS_ID_SIZE */ +#include /* ETH_ALEN */ +#include /* phy_interface_t, struct phy_device */ +#include + +#include "enet_ext.h" + +#include "fsl_fman_drv.h" /* struct port_device */ +#include "fm_port_ext.h" + +struct fm_mac_dev; +enum fm_mac_exceptions; + +enum {DTSEC, XGMAC, MEMAC}; + +struct mac_device { + struct device *dev; + void *priv; + u8 cell_index; + struct resource *res; + void __iomem *vaddr; + u8 addr[ETH_ALEN]; + bool promisc; + + struct fm *fm_dev; + struct fm_port_drv_t *port_dev[2]; + + phy_interface_t phy_if; + u32 if_support; + bool link; + bool fixed_link; + u16 speed; + u16 max_speed; + struct device_node *phy_node; + struct device_node *tbi_node; + struct phy_device *phy_dev; + void *fm; + /* List of multicast addresses */ + struct list_head mc_addr_list; + struct platform_device *eth_dev; + + bool autoneg_pause; + bool rx_pause_req; + bool tx_pause_req; + bool rx_pause_active; + bool tx_pause_active; + + int (*init_phy)(struct net_device *net_dev, struct mac_device *mac_dev); + int (*init)(struct mac_device *mac_dev); + int (*start)(struct mac_device *mac_dev); + int (*stop)(struct mac_device *mac_dev); + int (*set_promisc)(struct fm_mac_dev *fm_mac_dev, bool enable); + int (*change_addr)(struct fm_mac_dev *fm_mac_dev, + enet_addr_t *p_enet_addr); + int (*set_multi)(struct net_device *net_dev, + struct mac_device *mac_dev); + int (*set_rx_pause)(struct fm_mac_dev *fm_mac_dev, bool en); + int (*set_tx_pause)(struct fm_mac_dev *fm_mac_dev, u8 priority, + u16 pause_time, u16 thresh_time); + int (*set_exception)(struct fm_mac_dev *fm_mac_dev, + enum fm_mac_exceptions exception, + bool enable); + int (*add_hash_mac_addr)(struct fm_mac_dev *fm_mac_dev, + enet_addr_t *p_eth_addr); + int (*remove_hash_mac_addr)(struct fm_mac_dev *fm_mac_dev, + enet_addr_t *p_eth_addr); +}; + +struct mac_address { + u8 addr[ETH_ALEN]; + struct list_head list; +}; + +struct dpaa_eth_data { + struct device_node *mac_node; + struct mac_device *mac_dev; + int mac_hw_id; + int fman_hw_id; +}; + +#define get_fm_handle(net_dev) \ + (((struct dpa_priv_s *)netdev_priv(net_dev))->mac_dev->fm_dev) + +#define for_each_port_device(i, port_dev) \ + for (i = 0; i < ARRAY_SIZE(port_dev); i++) + +static inline __attribute((nonnull)) void *macdev_priv( + const struct mac_device *mac_dev) +{ + return (void *)mac_dev + sizeof(*mac_dev); +} + +extern const char *mac_driver_description; +extern const size_t mac_sizeof_priv[]; +extern void (*const mac_setup[])(struct mac_device *mac_dev); + +int set_mac_active_pause(struct mac_device *mac_dev, bool rx, bool tx); +void get_pause_cfg(struct mac_device *mac_dev, bool *rx_pause, bool *tx_pause); +struct fm_mac_dev *get_mac_handle(struct mac_device *mac_dev); + +#endif /* __MAC_H */ diff --git a/drivers/net/ethernet/freescale/fman/mac/Makefile b/drivers/net/ethernet/freescale/fman/mac/Makefile index 26e35e1..909abec 100644 --- a/drivers/net/ethernet/freescale/fman/mac/Makefile +++ b/drivers/net/ethernet/freescale/fman/mac/Makefile @@ -1,7 +1,8 @@ -obj-y += fsl_fman_mac.o +obj-y += fsl_fman_mac.o fsl_mac.o fsl_fman_mac-objs := fman_dtsec.o fman_dtsec_mii_acc.o \ fm_dtsec.o \ fman_memac.o fman_memac_mii_acc.o \ fm_memac.o \ fman_tgec.o fm_tgec.o +fsl_mac-objs += mac.o mac-api.o diff --git a/drivers/net/ethernet/freescale/fman/mac/mac-api.c b/drivers/net/ethernet/freescale/fman/mac/mac-api.c new file mode 100644 index 0000000..e7cac9f --- /dev/null +++ b/drivers/net/ethernet/freescale/fman/mac/mac-api.c @@ -0,0 +1,688 @@ +/* Copyright 2008-2015 Freescale Semiconductor, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Freescale Semiconductor nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * + * ALTERNATIVELY, this software may be distributed under the terms of the + * GNU General Public License ("GPL") as published by the Free Software + * Foundation, either version 2 of that License or (at your option) any + * later version. + * + * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include + +#include "mac.h" +#include "fsl_fman_drv.h" +#include "fm_mac.h" +#include "fm_dtsec.h" +#include "fm_tgec.h" +#include "fm_memac.h" + +#define MAC_DESCRIPTION "FSL FMan MAC API based driver" + +MODULE_LICENSE("Dual BSD/GPL"); + +MODULE_AUTHOR("Emil Medve "); + +MODULE_DESCRIPTION(MAC_DESCRIPTION); + +struct mac_priv_s { + struct fm_mac_dev *fm_mac; +}; + +const char *mac_driver_description __initconst = MAC_DESCRIPTION; +const size_t mac_sizeof_priv[] = { + [DTSEC] = sizeof(struct mac_priv_s), + [XGMAC] = sizeof(struct mac_priv_s), + [MEMAC] = sizeof(struct mac_priv_s) +}; + +static const enum e_enet_mode _100[] = { + [PHY_INTERFACE_MODE_MII] = ENET_MODE_MII_100, + [PHY_INTERFACE_MODE_RMII] = ENET_MODE_RMII_100 +}; + +static const enum e_enet_mode _1000[] = { + [PHY_INTERFACE_MODE_GMII] = ENET_MODE_GMII_1000, + [PHY_INTERFACE_MODE_SGMII] = ENET_MODE_SGMII_1000, + [PHY_INTERFACE_MODE_TBI] = ENET_MODE_TBI_1000, + [PHY_INTERFACE_MODE_RGMII] = ENET_MODE_RGMII_1000, + [PHY_INTERFACE_MODE_RGMII_ID] = ENET_MODE_RGMII_1000, + [PHY_INTERFACE_MODE_RGMII_RXID] = ENET_MODE_RGMII_1000, + [PHY_INTERFACE_MODE_RGMII_TXID] = ENET_MODE_RGMII_1000, + [PHY_INTERFACE_MODE_RTBI] = ENET_MODE_RTBI_1000 +}; + +static enum e_enet_mode __attribute__((nonnull)) +macdev2enetinterface(const struct mac_device *mac_dev) +{ + switch (mac_dev->max_speed) { + case SPEED_100: + return _100[mac_dev->phy_if]; + case SPEED_1000: + return _1000[mac_dev->phy_if]; + case SPEED_10000: + return ENET_MODE_XGMII_10000; + default: + return ENET_MODE_MII_100; + } +} + +struct fm_mac_dev *get_mac_handle(struct mac_device *mac_dev) +{ + const struct mac_priv_s *priv; + + priv = macdev_priv(mac_dev); + + return priv->fm_mac; +} +EXPORT_SYMBOL(get_mac_handle); + +static void mac_exception(void *_mac_dev, enum fm_mac_exceptions ex) +{ + struct mac_device *mac_dev; + + mac_dev = (struct mac_device *)_mac_dev; + + if (FM_MAC_EX_10G_RX_FIFO_OVFL == ex) { + /* don't flag RX FIFO after the first */ + mac_dev->set_exception(get_mac_handle(mac_dev), + FM_MAC_EX_10G_RX_FIFO_OVFL, false); + dev_err(mac_dev->dev, "10G MAC got RX FIFO Error = %x\n", + ex); + } + + dev_dbg(mac_dev->dev, "%s:%s() -> %d\n", KBUILD_BASENAME ".c", + __func__, ex); +} + +static int tgec_initialization(struct mac_device *mac_dev) +{ + int err; + struct mac_priv_s *priv; + struct fm_mac_params_t param; + u32 version; + + priv = macdev_priv(mac_dev); + + param.base_addr = (typeof(param.base_addr)) + devm_ioremap(mac_dev->dev, mac_dev->res->start, 0x2000); + param.enet_mode = macdev2enetinterface(mac_dev); + memcpy(¶m.addr, mac_dev->addr, + min(sizeof(param.addr), sizeof(mac_dev->addr))); + param.mac_id = mac_dev->cell_index; + param.fm = (void *)mac_dev->fm; + param.exception_cb = mac_exception; + param.event_cb = mac_exception; + param.dev_id = mac_dev; + + priv->fm_mac = tgec_config(¶m); + if (!priv->fm_mac) { + err = -EINVAL; + goto _return; + } + + err = tgec_cfg_max_frame_len(priv->fm_mac, fm_get_max_frm()); + if (err < 0) + goto _return_fm_mac_free; + + err = tgec_init(priv->fm_mac); + if (err < 0) + goto _return_fm_mac_free; + + /* For 1G MAC, disable by default the MIB counters overflow interrupt */ + if (macdev2enetinterface(mac_dev) != ENET_MODE_XGMII_10000) { + err = mac_dev->set_exception(get_mac_handle(mac_dev), + FM_MAC_EX_1G_RX_MIB_CNT_OVFL, + false); + if (err < 0) + goto _return_fm_mac_free; + } + + /* For 10G MAC, disable Tx ECC exception */ + if (macdev2enetinterface(mac_dev) == ENET_MODE_XGMII_10000) { + err = mac_dev->set_exception(get_mac_handle(mac_dev), + FM_MAC_EX_10G_TX_ECC_ER, false); + if (err < 0) + goto _return_fm_mac_free; + } + + err = tgec_get_version(priv->fm_mac, &version); + if (err < 0) + goto _return_fm_mac_free; + + dev_info(mac_dev->dev, "FMan %s version: 0x%08x\n", + ((macdev2enetinterface(mac_dev) != ENET_MODE_XGMII_10000) ? + "dTSEC" : "XGEC"), version); + + goto _return; + +_return_fm_mac_free: + tgec_free(get_mac_handle(mac_dev)); + +_return: + return err; +} + +static int dtsec_initialization(struct mac_device *mac_dev) +{ + int err; + struct mac_priv_s *priv; + struct fm_mac_params_t param; + u32 version; + + priv = macdev_priv(mac_dev); + + param.base_addr = (typeof(param.base_addr)) + devm_ioremap(mac_dev->dev, mac_dev->res->start, 0x2000); + param.enet_mode = macdev2enetinterface(mac_dev); + memcpy(¶m.addr, mac_dev->addr, + min(sizeof(param.addr), sizeof(mac_dev->addr))); + param.mac_id = mac_dev->cell_index; + param.fm = (void *)mac_dev->fm; + param.exception_cb = mac_exception; + param.event_cb = mac_exception; + param.dev_id = mac_dev; + + priv->fm_mac = dtsec_config(¶m); + if (!priv->fm_mac) { + err = -EINVAL; + goto _return; + } + + err = dtsec_cfg_max_frame_len(priv->fm_mac, fm_get_max_frm()); + if (err < 0) + goto _return_fm_mac_free; + + err = dtsec_cfg_pad_and_crc(priv->fm_mac, true); + if (err < 0) + goto _return_fm_mac_free; + + err = dtsec_init(priv->fm_mac); + if (err < 0) + goto _return_fm_mac_free; + + /* For 1G MAC, disable by default the MIB counters overflow interrupt */ + if (macdev2enetinterface(mac_dev) != ENET_MODE_XGMII_10000) { + err = mac_dev->set_exception(get_mac_handle(mac_dev), + FM_MAC_EX_1G_RX_MIB_CNT_OVFL, + false); + if (err < 0) + goto _return_fm_mac_free; + } + + /* For 10G MAC, disable Tx ECC exception */ + if (macdev2enetinterface(mac_dev) == ENET_MODE_XGMII_10000) { + err = mac_dev->set_exception(get_mac_handle(mac_dev), + FM_MAC_EX_10G_TX_ECC_ER, false); + if (err < 0) + goto _return_fm_mac_free; + } + + err = dtsec_get_version(priv->fm_mac, &version); + if (err < 0) + goto _return_fm_mac_free; + + dev_info(mac_dev->dev, "FMan %s version: 0x%08x\n", + ((macdev2enetinterface(mac_dev) != ENET_MODE_XGMII_10000) ? + "dTSEC" : "XGEC"), version); + + goto _return; + +_return_fm_mac_free: + dtsec_free(get_mac_handle(mac_dev)); + +_return: + return err; +} + +static int memac_initialization(struct mac_device *mac_dev) +{ + int err; + struct mac_priv_s *priv; + struct fm_mac_params_t param; + + priv = macdev_priv(mac_dev); + + param.base_addr = (typeof(param.base_addr)) + devm_ioremap(mac_dev->dev, mac_dev->res->start, 0x2000); + param.enet_mode = macdev2enetinterface(mac_dev); + memcpy(¶m.addr, mac_dev->addr, sizeof(mac_dev->addr)); + param.mac_id = mac_dev->cell_index; + param.fm = (void *)mac_dev->fm; + param.exception_cb = mac_exception; + param.event_cb = mac_exception; + param.dev_id = mac_dev; + + priv->fm_mac = memac_config(¶m); + if (!priv->fm_mac) { + err = -EINVAL; + goto _return; + } + + err = memac_cfg_max_frame_len(priv->fm_mac, fm_get_max_frm()); + if (err < 0) + goto _return_fm_mac_free; + + err = memac_cfg_reset_on_init(priv->fm_mac, true); + if (err < 0) + goto _return_fm_mac_free; + + err = memac_cfg_fixed_link(priv->fm_mac, mac_dev->fixed_link); + if (err < 0) + goto _return_fm_mac_free; + + err = memac_init(priv->fm_mac); + if (err < 0) + goto _return_fm_mac_free; + + dev_info(mac_dev->dev, "FMan MEMAC\n"); + + goto _return; + +_return_fm_mac_free: + memac_free(priv->fm_mac); + +_return: + return err; +} + +static int dtsec_start(struct mac_device *mac_dev) +{ + int err; + struct phy_device *phy_dev = mac_dev->phy_dev; + + err = dtsec_enable(get_mac_handle(mac_dev), + COMM_MODE_RX_AND_TX); + + if (!err && phy_dev) + phy_start(phy_dev); + + return err; +} + +static int tgec_start(struct mac_device *mac_dev) +{ + int err; + struct phy_device *phy_dev = mac_dev->phy_dev; + + err = tgec_enable(get_mac_handle(mac_dev), + COMM_MODE_RX_AND_TX); + + if (!err && phy_dev) + phy_start(phy_dev); + + return err; +} + +static int memac_start(struct mac_device *mac_dev) +{ + int err; + struct phy_device *phy_dev = mac_dev->phy_dev; + + err = memac_enable(get_mac_handle(mac_dev), + COMM_MODE_RX_AND_TX); + + if (!err && phy_dev) + phy_start(phy_dev); + + return err; +} + +static int dtsec_stop(struct mac_device *mac_dev) +{ + if (mac_dev->phy_dev) + phy_stop(mac_dev->phy_dev); + + return dtsec_disable(get_mac_handle(mac_dev), + COMM_MODE_RX_AND_TX); +} + +static int tgec_stop(struct mac_device *mac_dev) +{ + return tgec_disable(get_mac_handle(mac_dev), + COMM_MODE_RX_AND_TX); +} + +static int memac_stop(struct mac_device *mac_dev) +{ + if (mac_dev->phy_dev && (macdev2enetinterface(mac_dev) != + ENET_MODE_XGMII_10000)) + phy_stop(mac_dev->phy_dev); + + return memac_disable(get_mac_handle(mac_dev), + COMM_MODE_RX_AND_TX); +} + +static int set_multi(struct net_device *net_dev, struct mac_device *mac_dev) +{ + struct mac_priv_s *mac_priv; + struct mac_address *old_addr, *tmp; + struct netdev_hw_addr *ha; + int err; + enet_addr_t *addr; + + mac_priv = macdev_priv(mac_dev); + + /* Clear previous address list */ + list_for_each_entry_safe(old_addr, tmp, &mac_dev->mc_addr_list, list) { + addr = (enet_addr_t *)old_addr->addr; + err = mac_dev->remove_hash_mac_addr(mac_priv->fm_mac, addr); + if (err < 0) + return err; + + list_del(&old_addr->list); + kfree(old_addr); + } + + /* Add all the addresses from the new list */ + netdev_for_each_mc_addr(ha, net_dev) { + addr = (enet_addr_t *)ha->addr; + err = mac_dev->add_hash_mac_addr(mac_priv->fm_mac, addr); + if (err < 0) + return err; + + tmp = kmalloc(sizeof(*tmp), GFP_ATOMIC); + if (!tmp) + return -ENOMEM; + + ether_addr_copy(tmp->addr, ha->addr); + list_add(&tmp->list, &mac_dev->mc_addr_list); + } + return 0; +} + +/* Avoid redundant calls to FMD, if the MAC driver already contains the desired + * active PAUSE settings. Otherwise, the new active settings should be reflected + * in FMan. + */ +int set_mac_active_pause(struct mac_device *mac_dev, bool rx, bool tx) +{ + struct fm_mac_dev *fm_mac_dev = get_mac_handle(mac_dev); + int err = 0; + + if (rx != mac_dev->rx_pause_active) { + err = mac_dev->set_rx_pause(fm_mac_dev, rx); + if (likely(err == 0)) + mac_dev->rx_pause_active = rx; + } + + if (tx != mac_dev->tx_pause_active) { + u16 pause_time = (tx ? FSL_FM_PAUSE_TIME_ENABLE : + FSL_FM_PAUSE_TIME_DISABLE); + + err = mac_dev->set_tx_pause(fm_mac_dev, 0, + pause_time, 0); + + if (likely(err == 0)) + mac_dev->tx_pause_active = tx; + } + + return err; +} +EXPORT_SYMBOL(set_mac_active_pause); + +/* Determine the MAC RX/TX PAUSE frames settings based on PHY + * autonegotiation or values set by eththool. + */ +void get_pause_cfg(struct mac_device *mac_dev, bool *rx_pause, bool *tx_pause) +{ + struct phy_device *phy_dev = mac_dev->phy_dev; + u16 lcl_adv, rmt_adv; + u8 flowctrl; + + *rx_pause = *tx_pause = false; + + if (!phy_dev->duplex) + return; + + /* If PAUSE autonegotiation is disabled, the TX/RX PAUSE settings + * are those set by ethtool. + */ + if (!mac_dev->autoneg_pause) { + *rx_pause = mac_dev->rx_pause_req; + *tx_pause = mac_dev->tx_pause_req; + return; + } + + /* Else if PAUSE autonegotiation is enabled, the TX/RX PAUSE + * settings depend on the result of the link negotiation. + */ + + /* get local capabilities */ + lcl_adv = 0; + if (phy_dev->advertising & ADVERTISED_Pause) + lcl_adv |= ADVERTISE_PAUSE_CAP; + if (phy_dev->advertising & ADVERTISED_Asym_Pause) + lcl_adv |= ADVERTISE_PAUSE_ASYM; + + /* get link partner capabilities */ + rmt_adv = 0; + if (phy_dev->pause) + rmt_adv |= LPA_PAUSE_CAP; + if (phy_dev->asym_pause) + rmt_adv |= LPA_PAUSE_ASYM; + + /* Calculate TX/RX settings based on local and peer advertised + * symmetric/asymmetric PAUSE capabilities. + */ + flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv); + if (flowctrl & FLOW_CTRL_RX) + *rx_pause = true; + if (flowctrl & FLOW_CTRL_TX) + *tx_pause = true; +} +EXPORT_SYMBOL(get_pause_cfg); + +static void adjust_link_void(struct net_device *net_dev) +{ +} + +static void adjust_link_dtsec(struct net_device *net_dev) +{ + struct device *dev = net_dev->dev.parent; + struct dpaa_eth_data *eth_data = dev->platform_data; + struct mac_device *mac_dev = eth_data->mac_dev; + struct phy_device *phy_dev = mac_dev->phy_dev; + struct fm_mac_dev *fm_mac_dev; + bool rx_pause, tx_pause; + int err; + + fm_mac_dev = get_mac_handle(mac_dev); + if (!phy_dev->link) { + /* TODO: move in dtsec_an_errata() */ + dtsec_restart_autoneg(fm_mac_dev); + + return; + } + + dtsec_adjust_link(fm_mac_dev, phy_dev->speed); + get_pause_cfg(mac_dev, &rx_pause, &tx_pause); + err = set_mac_active_pause(mac_dev, rx_pause, tx_pause); + if (err < 0) + netdev_err(net_dev, "set_mac_active_pause() = %d\n", err); +} + +static void adjust_link_memac(struct net_device *net_dev) +{ + struct device *dev = net_dev->dev.parent; + struct dpaa_eth_data *eth_data = dev->platform_data; + struct mac_device *mac_dev = eth_data->mac_dev; + struct phy_device *phy_dev = mac_dev->phy_dev; + struct fm_mac_dev *fm_mac_dev; + bool rx_pause, tx_pause; + int err; + + fm_mac_dev = get_mac_handle(mac_dev); + memac_adjust_link(fm_mac_dev, phy_dev->speed); + + get_pause_cfg(mac_dev, &rx_pause, &tx_pause); + err = set_mac_active_pause(mac_dev, rx_pause, tx_pause); + if (err < 0) + netdev_err(net_dev, "set_mac_active_pause() = %d\n", err); +} + +/* Initializes driver's PHY state, and attaches to the PHY. + * Returns 0 on success. + */ +static int dtsec_init_phy(struct net_device *net_dev, + struct mac_device *mac_dev) +{ + struct phy_device *phy_dev; + + phy_dev = of_phy_connect(net_dev, mac_dev->phy_node, + &adjust_link_dtsec, 0, + mac_dev->phy_if); + if (!phy_dev) { + netdev_err(net_dev, "Could not connect to PHY %s\n", + mac_dev->phy_node->full_name); + return -ENODEV; + } + + /* Remove any features not supported by the controller */ + phy_dev->supported &= mac_dev->if_support; + /* Enable the symmetric and asymmetric PAUSE frame advertisements, + * as most of the PHY drivers do not enable them by default. + */ + phy_dev->supported |= (SUPPORTED_Pause | SUPPORTED_Asym_Pause); + phy_dev->advertising = phy_dev->supported; + + mac_dev->phy_dev = phy_dev; + + return 0; +} + +static int xgmac_init_phy(struct net_device *net_dev, + struct mac_device *mac_dev) +{ + struct phy_device *phy_dev; + + phy_dev = of_phy_connect(net_dev, mac_dev->phy_node, &adjust_link_void, + 0, mac_dev->phy_if); + if (!phy_dev) { + netdev_err(net_dev, "Could not attach to PHY %s\n", + mac_dev->phy_node->full_name); + return -ENODEV; + } + + phy_dev->supported &= mac_dev->if_support; + /* Enable the symmetric and asymmetric PAUSE frame advertisements, + * as most of the PHY drivers do not enable them by default. + */ + phy_dev->supported |= (SUPPORTED_Pause | SUPPORTED_Asym_Pause); + phy_dev->advertising = phy_dev->supported; + + mac_dev->phy_dev = phy_dev; + + return 0; +} + +static int memac_init_phy(struct net_device *net_dev, + struct mac_device *mac_dev) +{ + struct phy_device *phy_dev; + + phy_dev = of_phy_connect(net_dev, mac_dev->phy_node, + &adjust_link_memac, 0, + mac_dev->phy_if); + + if (!phy_dev) { + netdev_err(net_dev, "Could not connect to PHY %s\n", + mac_dev->phy_node->full_name); + return -ENODEV; + } + + /* Remove any features not supported by the controller */ + phy_dev->supported &= mac_dev->if_support; + /* Enable the symmetric and asymmetric PAUSE frame advertisements, + * as most of the PHY drivers do not enable them by default. + */ + phy_dev->supported |= (SUPPORTED_Pause | SUPPORTED_Asym_Pause); + phy_dev->advertising = phy_dev->supported; + + mac_dev->phy_dev = phy_dev; + + return 0; +} + +static void setup_dtsec(struct mac_device *mac_dev) +{ + mac_dev->init_phy = dtsec_init_phy; + mac_dev->init = dtsec_initialization; + mac_dev->start = dtsec_start; + mac_dev->stop = dtsec_stop; + mac_dev->set_promisc = dtsec_set_promiscuous; + mac_dev->change_addr = dtsec_modify_mac_address; + mac_dev->add_hash_mac_addr = dtsec_add_hash_mac_address; + mac_dev->remove_hash_mac_addr = dtsec_del_hash_mac_address; + mac_dev->set_multi = set_multi; + mac_dev->set_tx_pause = dtsec_set_tx_pause_frames; + mac_dev->set_rx_pause = dtsec_accept_rx_pause_frames; + mac_dev->set_exception = dtsec_set_exception; +} + +static void setup_xgmac(struct mac_device *mac_dev) +{ + mac_dev->init_phy = xgmac_init_phy; + mac_dev->init = tgec_initialization; + mac_dev->start = tgec_start; + mac_dev->stop = tgec_stop; + mac_dev->set_promisc = tgec_set_promiscuous; + mac_dev->change_addr = tgec_modify_mac_address; + mac_dev->add_hash_mac_addr = tgec_add_hash_mac_address; + mac_dev->remove_hash_mac_addr = tgec_del_hash_mac_address; + mac_dev->set_multi = set_multi; + mac_dev->set_tx_pause = tgec_set_tx_pause_frames; + mac_dev->set_rx_pause = tgec_accept_rx_pause_frames; + mac_dev->set_exception = tgec_set_exception; +} + +static void setup_memac(struct mac_device *mac_dev) +{ + mac_dev->init_phy = memac_init_phy; + mac_dev->init = memac_initialization; + mac_dev->start = memac_start; + mac_dev->stop = memac_stop; + mac_dev->set_promisc = memac_set_promiscuous; + mac_dev->change_addr = memac_modify_mac_address; + mac_dev->add_hash_mac_addr = memac_add_hash_mac_address; + mac_dev->remove_hash_mac_addr = memac_del_hash_mac_address; + mac_dev->set_multi = set_multi; + mac_dev->set_tx_pause = memac_set_tx_pause_frames; + mac_dev->set_rx_pause = memac_accept_rx_pause_frames; + mac_dev->set_exception = memac_set_exception; +} + +void (*const mac_setup[])(struct mac_device *mac_dev) = { + [DTSEC] = setup_dtsec, + [XGMAC] = setup_xgmac, + [MEMAC] = setup_memac +}; diff --git a/drivers/net/ethernet/freescale/fman/mac/mac.c b/drivers/net/ethernet/freescale/fman/mac/mac.c new file mode 100644 index 0000000..c44544b --- /dev/null +++ b/drivers/net/ethernet/freescale/fman/mac/mac.c @@ -0,0 +1,445 @@ +/* Copyright 2008-2015 Freescale Semiconductor, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Freescale Semiconductor nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * + * ALTERNATIVELY, this software may be distributed under the terms of the + * GNU General Public License ("GPL") as published by the Free Software + * Foundation, either version 2 of that License or (at your option) any + * later version. + * + * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mac.h" + +#define DTSEC_SUPPORTED \ + (SUPPORTED_10baseT_Half \ + | SUPPORTED_10baseT_Full \ + | SUPPORTED_100baseT_Half \ + | SUPPORTED_100baseT_Full \ + | SUPPORTED_Autoneg \ + | SUPPORTED_Pause \ + | SUPPORTED_Asym_Pause \ + | SUPPORTED_MII) + +static DEFINE_MUTEX(eth_lock); + +static const char phy_str[][11] = { + [PHY_INTERFACE_MODE_MII] = "mii", + [PHY_INTERFACE_MODE_GMII] = "gmii", + [PHY_INTERFACE_MODE_SGMII] = "sgmii", + [PHY_INTERFACE_MODE_TBI] = "tbi", + [PHY_INTERFACE_MODE_RMII] = "rmii", + [PHY_INTERFACE_MODE_RGMII] = "rgmii", + [PHY_INTERFACE_MODE_RGMII_ID] = "rgmii-id", + [PHY_INTERFACE_MODE_RGMII_RXID] = "rgmii-rxid", + [PHY_INTERFACE_MODE_RGMII_TXID] = "rgmii-txid", + [PHY_INTERFACE_MODE_RTBI] = "rtbi", + [PHY_INTERFACE_MODE_XGMII] = "xgmii" +}; + +static phy_interface_t __pure __attribute__((nonnull)) str2phy(const char *str) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(phy_str); i++) + if (strcmp(str, phy_str[i]) == 0) + return (phy_interface_t)i; + + return PHY_INTERFACE_MODE_MII; +} + +static const u16 phy2speed[] = { + [PHY_INTERFACE_MODE_MII] = SPEED_100, + [PHY_INTERFACE_MODE_GMII] = SPEED_1000, + [PHY_INTERFACE_MODE_SGMII] = SPEED_1000, + [PHY_INTERFACE_MODE_TBI] = SPEED_1000, + [PHY_INTERFACE_MODE_RMII] = SPEED_100, + [PHY_INTERFACE_MODE_RGMII] = SPEED_1000, + [PHY_INTERFACE_MODE_RGMII_ID] = SPEED_1000, + [PHY_INTERFACE_MODE_RGMII_RXID] = SPEED_1000, + [PHY_INTERFACE_MODE_RGMII_TXID] = SPEED_1000, + [PHY_INTERFACE_MODE_RTBI] = SPEED_1000, + [PHY_INTERFACE_MODE_XGMII] = SPEED_10000 +}; + +static struct mac_device * +alloc_macdev(struct device *dev, size_t sizeof_priv, + void (*setup)(struct mac_device *mac_dev)) +{ + struct mac_device *mac_dev; + + mac_dev = devm_kzalloc(dev, sizeof(*mac_dev) + sizeof_priv, GFP_KERNEL); + if (!mac_dev) { + mac_dev = ERR_PTR(-ENOMEM); + } else { + mac_dev->dev = dev; + dev_set_drvdata(dev, mac_dev); + setup(mac_dev); + } + + return mac_dev; +} + +static struct platform_device *dpaa_eth_add_device(int fman_id, + struct mac_device *mac_dev, + struct device_node *node) +{ + struct platform_device *pdev; + struct dpaa_eth_data data; + static int dpaa_eth_dev_cnt; + int ret; + + data.mac_dev = mac_dev; + data.mac_hw_id = mac_dev->cell_index; + data.fman_hw_id = fman_id; + data.mac_node = node; + + mutex_lock(ð_lock); + + pdev = platform_device_alloc("dpaa-ethernet", dpaa_eth_dev_cnt); + if (!pdev) { + ret = -ENOMEM; + goto no_mem; + } + + ret = platform_device_add_data(pdev, &data, sizeof(data)); + if (ret) + goto err; + + ret = platform_device_add(pdev); + if (ret) + goto err; + + dpaa_eth_dev_cnt++; + mutex_unlock(ð_lock); + + return pdev; + +err: + platform_device_put(pdev); +no_mem: + mutex_unlock(ð_lock); + + return ERR_PTR(ret); +} + +static const struct of_device_id mac_match[] = { + [DTSEC] = { + .compatible = "fsl,fman-dtsec" + }, + [XGMAC] = { + .compatible = "fsl,fman-xgec" + }, + [MEMAC] = { + .compatible = "fsl,fman-memac" + }, + {} +}; +MODULE_DEVICE_TABLE(of, mac_match); + +static int mac_probe(struct platform_device *_of_dev) +{ + int err, i, lenp; + struct device *dev; + struct device_node *mac_node, *dev_node; + struct mac_device *mac_dev; + struct platform_device *of_dev; + struct resource res; + const u8 *mac_addr; + const char *char_prop; + const u32 *u32_prop; + const struct of_device_id *match; + u8 fman_id; + + const phandle *phandle_prop; + + dev = &_of_dev->dev; + mac_node = dev->of_node; + + match = of_match_device(mac_match, dev); + if (!match) + return -EINVAL; + + for (i = 0; i < ARRAY_SIZE(mac_match) - 1 && match != mac_match + i; + i++) + ; + BUG_ON(i >= ARRAY_SIZE(mac_match) - 1); + + mac_dev = alloc_macdev(dev, mac_sizeof_priv[i], mac_setup[i]); + if (IS_ERR(mac_dev)) { + err = PTR_ERR(mac_dev); + dev_err(dev, "alloc_macdev() = %d\n", err); + goto _return; + } + + INIT_LIST_HEAD(&mac_dev->mc_addr_list); + + /* Get the FM node */ + dev_node = of_get_parent(mac_node); + if (!dev_node) { + dev_err(dev, "of_get_parent(%s) failed\n", + mac_node->full_name); + err = -EINVAL; + goto _return_dev_set_drvdata; + } + + of_dev = of_find_device_by_node(dev_node); + if (!of_dev) { + dev_err(dev, "of_find_device_by_node(%s) failed\n", + dev_node->full_name); + err = -EINVAL; + goto _return_of_node_put; + } + + /* Get the FMan cell-index */ + u32_prop = of_get_property(dev_node, "cell-index", &lenp); + if (!u32_prop) { + dev_err(dev, "of_get_property(%s, cell-index) failed\n", + dev_node->full_name); + err = -EINVAL; + goto _return_of_node_put; + } + BUG_ON(lenp != sizeof(u32)); + fman_id = (u8)*u32_prop + 1; /* cell-index 0 => FMan id 1 */ + + mac_dev->fm_dev = fm_bind(&of_dev->dev); + if (!mac_dev->fm_dev) { + dev_err(dev, "fm_bind(%s) failed\n", dev_node->full_name); + err = -ENODEV; + goto _return_of_node_put; + } + + mac_dev->fm = (void *)fm_get_handle(mac_dev->fm_dev); + of_node_put(dev_node); + + /* Get the address of the memory mapped registers */ + err = of_address_to_resource(mac_node, 0, &res); + if (err < 0) { + dev_err(dev, "of_address_to_resource(%s) = %d\n", + mac_node->full_name, err); + goto _return_dev_set_drvdata; + } + + mac_dev->res = + __devm_request_region(dev, fm_get_mem_region(mac_dev->fm_dev), + res.start, res.end + 1 - res.start, + "mac"); + if (!mac_dev->res) { + dev_err(dev, "__devm_request_mem_region(mac) failed\n"); + err = -EBUSY; + goto _return_dev_set_drvdata; + } + + mac_dev->vaddr = devm_ioremap(dev, mac_dev->res->start, + mac_dev->res->end + 1 + - mac_dev->res->start); + if (!mac_dev->vaddr) { + dev_err(dev, "devm_ioremap() failed\n"); + err = -EIO; + goto _return_dev_set_drvdata; + } + +#define TBIPA_OFFSET 0x1c +#define TBIPA_DEFAULT_ADDR 5 /* override if used as external PHY addr. */ + mac_dev->tbi_node = of_parse_phandle(mac_node, "tbi-handle", 0); + if (mac_dev->tbi_node) { + u32 tbiaddr = TBIPA_DEFAULT_ADDR; + + u32_prop = of_get_property(mac_dev->tbi_node, "reg", NULL); + if (u32_prop) + tbiaddr = *u32_prop; + out_be32(mac_dev->vaddr + TBIPA_OFFSET, tbiaddr); + } + + if (!of_device_is_available(mac_node)) { + devm_iounmap(dev, mac_dev->vaddr); + __devm_release_region(dev, fm_get_mem_region(mac_dev->fm_dev), + res.start, res.end + 1 - res.start); + fm_unbind(mac_dev->fm_dev); + devm_kfree(dev, mac_dev); + dev_set_drvdata(dev, NULL); + return -ENODEV; + } + + /* Get the cell-index */ + u32_prop = of_get_property(mac_node, "cell-index", &lenp); + if (!u32_prop) { + dev_err(dev, "of_get_property(%s, cell-index) failed\n", + mac_node->full_name); + err = -EINVAL; + goto _return_dev_set_drvdata; + } + BUG_ON(lenp != sizeof(u32)); + mac_dev->cell_index = (u8)*u32_prop; + + /* Get the MAC address */ + mac_addr = of_get_mac_address(mac_node); + if (!mac_addr) { + dev_err(dev, "of_get_mac_address(%s) failed\n", + mac_node->full_name); + err = -EINVAL; + goto _return_dev_set_drvdata; + } + memcpy(mac_dev->addr, mac_addr, sizeof(mac_dev->addr)); + + /* Get the port handles */ + phandle_prop = of_get_property(mac_node, "fsl,fman-ports", &lenp); + if (!phandle_prop) { + dev_err(dev, "of_get_property(%s, fsl,fman-ports) failed\n", + mac_node->full_name); + err = -EINVAL; + goto _return_dev_set_drvdata; + } + BUG_ON(lenp != sizeof(phandle) * ARRAY_SIZE(mac_dev->port_dev)); + + for_each_port_device(i, mac_dev->port_dev) { + /* Find the port node */ + dev_node = of_find_node_by_phandle(phandle_prop[i]); + if (!dev_node) { + dev_err(dev, "of_find_node_by_phandle() failed\n"); + err = -EINVAL; + goto _return_of_node_put; + } + + of_dev = of_find_device_by_node(dev_node); + if (!of_dev) { + dev_err(dev, "of_find_device_by_node(%s) failed\n", + dev_node->full_name); + err = -EINVAL; + goto _return_of_node_put; + } + + mac_dev->port_dev[i] = fm_port_bind(&of_dev->dev); + if (!mac_dev->port_dev[i]) { + dev_err(dev, "dev_get_drvdata(%s) failed\n", + dev_node->full_name); + err = -EINVAL; + goto _return_of_node_put; + } + of_node_put(dev_node); + } + + /* Get the PHY connection type */ + char_prop = (const char *)of_get_property(mac_node, + "phy-connection-type", NULL); + if (!char_prop) { + dev_warn(dev, + "of_get_property(%s, phy-connection-type) failed. Defaulting to MII\n", + mac_node->full_name); + mac_dev->phy_if = PHY_INTERFACE_MODE_MII; + } else { + mac_dev->phy_if = str2phy(char_prop); + } + + mac_dev->link = false; + mac_dev->fixed_link = false; + mac_dev->speed = phy2speed[mac_dev->phy_if]; + mac_dev->max_speed = mac_dev->speed; + mac_dev->if_support = DTSEC_SUPPORTED; + /* We don't support half-duplex in SGMII mode */ + if (strstr(char_prop, "sgmii")) + mac_dev->if_support &= ~(SUPPORTED_10baseT_Half | + SUPPORTED_100baseT_Half); + + /* Gigabit support (no half-duplex) */ + if (mac_dev->max_speed == 1000) + mac_dev->if_support |= SUPPORTED_1000baseT_Full; + + /* The 10G interface only supports one mode */ + if (strstr(char_prop, "xgmii")) + mac_dev->if_support = SUPPORTED_10000baseT_Full; + + /* Get the rest of the PHY information */ + mac_dev->phy_node = of_parse_phandle(mac_node, "phy-handle", 0); + if (!mac_dev->phy_node && of_phy_is_fixed_link(mac_node)) { + err = of_phy_register_fixed_link(mac_node); + if (err) + goto _return_dev_set_drvdata; + mac_dev->fixed_link = true; + mac_dev->phy_node = of_node_get(mac_node); + } + + err = mac_dev->init(mac_dev); + if (err < 0) { + dev_err(dev, "mac_dev->init() = %d\n", err); + of_node_put(mac_dev->phy_node); + goto _return_dev_set_drvdata; + } + + /* pause frame autonegotiation enabled */ + mac_dev->autoneg_pause = true; + + /* by intializing the values to false, force FMD to enable PAUSE frames + * on RX and TX + */ + mac_dev->rx_pause_req = true; + mac_dev->tx_pause_req = true; + mac_dev->rx_pause_active = false; + mac_dev->tx_pause_active = false; + err = set_mac_active_pause(mac_dev, true, true); + if (err < 0) + dev_err(dev, "set_mac_active_pause() = %d\n", err); + + dev_info(dev, + "FMan MAC address: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n", + mac_dev->addr[0], mac_dev->addr[1], mac_dev->addr[2], + mac_dev->addr[3], mac_dev->addr[4], mac_dev->addr[5]); + + mac_dev->eth_dev = dpaa_eth_add_device(fman_id, mac_dev, + mac_node); + if (IS_ERR(mac_dev->eth_dev)) { + dev_err(dev, "failed to add Ethernet platform device for MAC %d\n", + mac_dev->cell_index); + mac_dev->eth_dev = NULL; + } + + goto _return; + +_return_of_node_put: + of_node_put(dev_node); +_return_dev_set_drvdata: + dev_set_drvdata(dev, NULL); +_return: + return err; +} + +static struct platform_driver mac_driver = { + .driver = { + .name = KBUILD_MODNAME, + .of_match_table = mac_match, + }, + .probe = mac_probe, +}; + +builtin_platform_driver(mac_driver); -- 1.7.9.5 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/