195711cd5SPawel Dembicki // SPDX-License-Identifier: GPL-2.0 295711cd5SPawel Dembicki /* DSA driver for: 395711cd5SPawel Dembicki * Vitesse VSC7385 SparX-G5 5+1-port Integrated Gigabit Ethernet Switch 495711cd5SPawel Dembicki * Vitesse VSC7388 SparX-G8 8-port Integrated Gigabit Ethernet Switch 595711cd5SPawel Dembicki * Vitesse VSC7395 SparX-G5e 5+1-port Integrated Gigabit Ethernet Switch 695711cd5SPawel Dembicki * Vitesse VSC7398 SparX-G8e 8-port Integrated Gigabit Ethernet Switch 795711cd5SPawel Dembicki * 895711cd5SPawel Dembicki * These switches have a built-in 8051 CPU and can download and execute a 995711cd5SPawel Dembicki * firmware in this CPU. They can also be configured to use an external CPU 1095711cd5SPawel Dembicki * handling the switch in a memory-mapped manner by connecting to that external 1195711cd5SPawel Dembicki * CPU's memory bus. 1295711cd5SPawel Dembicki * 1395711cd5SPawel Dembicki * Copyright (C) 2018 Linus Wallej <linus.walleij@linaro.org> 1495711cd5SPawel Dembicki * Includes portions of code from the firmware uploader by: 1595711cd5SPawel Dembicki * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org> 1695711cd5SPawel Dembicki */ 1795711cd5SPawel Dembicki #include <linux/kernel.h> 1895711cd5SPawel Dembicki #include <linux/module.h> 1995711cd5SPawel Dembicki #include <linux/device.h> 2095711cd5SPawel Dembicki #include <linux/of.h> 2195711cd5SPawel Dembicki #include <linux/of_device.h> 2295711cd5SPawel Dembicki #include <linux/of_mdio.h> 2395711cd5SPawel Dembicki #include <linux/bitops.h> 2495711cd5SPawel Dembicki #include <linux/if_bridge.h> 2595711cd5SPawel Dembicki #include <linux/etherdevice.h> 2695711cd5SPawel Dembicki #include <linux/gpio/consumer.h> 2795711cd5SPawel Dembicki #include <linux/gpio/driver.h> 2895711cd5SPawel Dembicki #include <linux/random.h> 2995711cd5SPawel Dembicki #include <net/dsa.h> 3095711cd5SPawel Dembicki 3195711cd5SPawel Dembicki #include "vitesse-vsc73xx.h" 3295711cd5SPawel Dembicki 3395711cd5SPawel Dembicki #define VSC73XX_BLOCK_MAC 0x1 /* Subblocks 0-4, 6 (CPU port) */ 3495711cd5SPawel Dembicki #define VSC73XX_BLOCK_ANALYZER 0x2 /* Only subblock 0 */ 3595711cd5SPawel Dembicki #define VSC73XX_BLOCK_MII 0x3 /* Subblocks 0 and 1 */ 3695711cd5SPawel Dembicki #define VSC73XX_BLOCK_MEMINIT 0x3 /* Only subblock 2 */ 3795711cd5SPawel Dembicki #define VSC73XX_BLOCK_CAPTURE 0x4 /* Only subblock 2 */ 3895711cd5SPawel Dembicki #define VSC73XX_BLOCK_ARBITER 0x5 /* Only subblock 0 */ 3995711cd5SPawel Dembicki #define VSC73XX_BLOCK_SYSTEM 0x7 /* Only subblock 0 */ 4095711cd5SPawel Dembicki 4195711cd5SPawel Dembicki #define CPU_PORT 6 /* CPU port */ 4295711cd5SPawel Dembicki 4395711cd5SPawel Dembicki /* MAC Block registers */ 4495711cd5SPawel Dembicki #define VSC73XX_MAC_CFG 0x00 4595711cd5SPawel Dembicki #define VSC73XX_MACHDXGAP 0x02 4695711cd5SPawel Dembicki #define VSC73XX_FCCONF 0x04 4795711cd5SPawel Dembicki #define VSC73XX_FCMACHI 0x08 4895711cd5SPawel Dembicki #define VSC73XX_FCMACLO 0x0c 4995711cd5SPawel Dembicki #define VSC73XX_MAXLEN 0x10 5095711cd5SPawel Dembicki #define VSC73XX_ADVPORTM 0x19 5195711cd5SPawel Dembicki #define VSC73XX_TXUPDCFG 0x24 5295711cd5SPawel Dembicki #define VSC73XX_TXQ_SELECT_CFG 0x28 5395711cd5SPawel Dembicki #define VSC73XX_RXOCT 0x50 5495711cd5SPawel Dembicki #define VSC73XX_TXOCT 0x51 5595711cd5SPawel Dembicki #define VSC73XX_C_RX0 0x52 5695711cd5SPawel Dembicki #define VSC73XX_C_RX1 0x53 5795711cd5SPawel Dembicki #define VSC73XX_C_RX2 0x54 5895711cd5SPawel Dembicki #define VSC73XX_C_TX0 0x55 5995711cd5SPawel Dembicki #define VSC73XX_C_TX1 0x56 6095711cd5SPawel Dembicki #define VSC73XX_C_TX2 0x57 6195711cd5SPawel Dembicki #define VSC73XX_C_CFG 0x58 6295711cd5SPawel Dembicki #define VSC73XX_CAT_DROP 0x6e 6395711cd5SPawel Dembicki #define VSC73XX_CAT_PR_MISC_L2 0x6f 6495711cd5SPawel Dembicki #define VSC73XX_CAT_PR_USR_PRIO 0x75 6595711cd5SPawel Dembicki #define VSC73XX_Q_MISC_CONF 0xdf 6695711cd5SPawel Dembicki 6795711cd5SPawel Dembicki /* MAC_CFG register bits */ 6895711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_WEXC_DIS BIT(31) 6995711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_PORT_RST BIT(29) 7095711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_TX_EN BIT(28) 7195711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_SEED_LOAD BIT(27) 7295711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_SEED_MASK GENMASK(26, 19) 7395711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_SEED_OFFSET 19 7495711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_FDX BIT(18) 7595711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_GIGA_MODE BIT(17) 7695711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_RX_EN BIT(16) 7795711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_VLAN_DBLAWR BIT(15) 7895711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_VLAN_AWR BIT(14) 7995711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_100_BASE_T BIT(13) /* Not in manual */ 8095711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_TX_IPG_MASK GENMASK(10, 6) 8195711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_TX_IPG_OFFSET 6 8295711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_TX_IPG_1000M (6 << VSC73XX_MAC_CFG_TX_IPG_OFFSET) 8395711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_TX_IPG_100_10M (17 << VSC73XX_MAC_CFG_TX_IPG_OFFSET) 8495711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_MAC_RX_RST BIT(5) 8595711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_MAC_TX_RST BIT(4) 8695711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_CLK_SEL_MASK GENMASK(2, 0) 8795711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_CLK_SEL_OFFSET 0 8895711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_CLK_SEL_1000M 1 8995711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_CLK_SEL_100M 2 9095711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_CLK_SEL_10M 3 9195711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_CLK_SEL_EXT 4 9295711cd5SPawel Dembicki 9395711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_1000M_F_PHY (VSC73XX_MAC_CFG_FDX | \ 9495711cd5SPawel Dembicki VSC73XX_MAC_CFG_GIGA_MODE | \ 9595711cd5SPawel Dembicki VSC73XX_MAC_CFG_TX_IPG_1000M | \ 9695711cd5SPawel Dembicki VSC73XX_MAC_CFG_CLK_SEL_EXT) 9795711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_100_10M_F_PHY (VSC73XX_MAC_CFG_FDX | \ 9895711cd5SPawel Dembicki VSC73XX_MAC_CFG_TX_IPG_100_10M | \ 9995711cd5SPawel Dembicki VSC73XX_MAC_CFG_CLK_SEL_EXT) 10095711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_100_10M_H_PHY (VSC73XX_MAC_CFG_TX_IPG_100_10M | \ 10195711cd5SPawel Dembicki VSC73XX_MAC_CFG_CLK_SEL_EXT) 10295711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_1000M_F_RGMII (VSC73XX_MAC_CFG_FDX | \ 10395711cd5SPawel Dembicki VSC73XX_MAC_CFG_GIGA_MODE | \ 10495711cd5SPawel Dembicki VSC73XX_MAC_CFG_TX_IPG_1000M | \ 10595711cd5SPawel Dembicki VSC73XX_MAC_CFG_CLK_SEL_1000M) 10695711cd5SPawel Dembicki #define VSC73XX_MAC_CFG_RESET (VSC73XX_MAC_CFG_PORT_RST | \ 10795711cd5SPawel Dembicki VSC73XX_MAC_CFG_MAC_RX_RST | \ 10895711cd5SPawel Dembicki VSC73XX_MAC_CFG_MAC_TX_RST) 10995711cd5SPawel Dembicki 11095711cd5SPawel Dembicki /* Flow control register bits */ 11195711cd5SPawel Dembicki #define VSC73XX_FCCONF_ZERO_PAUSE_EN BIT(17) 11295711cd5SPawel Dembicki #define VSC73XX_FCCONF_FLOW_CTRL_OBEY BIT(16) 11395711cd5SPawel Dembicki #define VSC73XX_FCCONF_PAUSE_VAL_MASK GENMASK(15, 0) 11495711cd5SPawel Dembicki 11595711cd5SPawel Dembicki /* ADVPORTM advanced port setup register bits */ 11695711cd5SPawel Dembicki #define VSC73XX_ADVPORTM_IFG_PPM BIT(7) 11795711cd5SPawel Dembicki #define VSC73XX_ADVPORTM_EXC_COL_CONT BIT(6) 11895711cd5SPawel Dembicki #define VSC73XX_ADVPORTM_EXT_PORT BIT(5) 11995711cd5SPawel Dembicki #define VSC73XX_ADVPORTM_INV_GTX BIT(4) 12095711cd5SPawel Dembicki #define VSC73XX_ADVPORTM_ENA_GTX BIT(3) 12195711cd5SPawel Dembicki #define VSC73XX_ADVPORTM_DDR_MODE BIT(2) 12295711cd5SPawel Dembicki #define VSC73XX_ADVPORTM_IO_LOOPBACK BIT(1) 12395711cd5SPawel Dembicki #define VSC73XX_ADVPORTM_HOST_LOOPBACK BIT(0) 12495711cd5SPawel Dembicki 12595711cd5SPawel Dembicki /* CAT_DROP categorizer frame dropping register bits */ 12695711cd5SPawel Dembicki #define VSC73XX_CAT_DROP_DROP_MC_SMAC_ENA BIT(6) 12795711cd5SPawel Dembicki #define VSC73XX_CAT_DROP_FWD_CTRL_ENA BIT(4) 12895711cd5SPawel Dembicki #define VSC73XX_CAT_DROP_FWD_PAUSE_ENA BIT(3) 12995711cd5SPawel Dembicki #define VSC73XX_CAT_DROP_UNTAGGED_ENA BIT(2) 13095711cd5SPawel Dembicki #define VSC73XX_CAT_DROP_TAGGED_ENA BIT(1) 13195711cd5SPawel Dembicki #define VSC73XX_CAT_DROP_NULL_MAC_ENA BIT(0) 13295711cd5SPawel Dembicki 13395711cd5SPawel Dembicki #define VSC73XX_Q_MISC_CONF_EXTENT_MEM BIT(31) 13495711cd5SPawel Dembicki #define VSC73XX_Q_MISC_CONF_EARLY_TX_MASK GENMASK(4, 1) 13595711cd5SPawel Dembicki #define VSC73XX_Q_MISC_CONF_EARLY_TX_512 (1 << 1) 13695711cd5SPawel Dembicki #define VSC73XX_Q_MISC_CONF_MAC_PAUSE_MODE BIT(0) 13795711cd5SPawel Dembicki 13895711cd5SPawel Dembicki /* Frame analyzer block 2 registers */ 13995711cd5SPawel Dembicki #define VSC73XX_STORMLIMIT 0x02 14095711cd5SPawel Dembicki #define VSC73XX_ADVLEARN 0x03 14195711cd5SPawel Dembicki #define VSC73XX_IFLODMSK 0x04 14295711cd5SPawel Dembicki #define VSC73XX_VLANMASK 0x05 14395711cd5SPawel Dembicki #define VSC73XX_MACHDATA 0x06 14495711cd5SPawel Dembicki #define VSC73XX_MACLDATA 0x07 14595711cd5SPawel Dembicki #define VSC73XX_ANMOVED 0x08 14695711cd5SPawel Dembicki #define VSC73XX_ANAGEFIL 0x09 14795711cd5SPawel Dembicki #define VSC73XX_ANEVENTS 0x0a 14895711cd5SPawel Dembicki #define VSC73XX_ANCNTMASK 0x0b 14995711cd5SPawel Dembicki #define VSC73XX_ANCNTVAL 0x0c 15095711cd5SPawel Dembicki #define VSC73XX_LEARNMASK 0x0d 15195711cd5SPawel Dembicki #define VSC73XX_UFLODMASK 0x0e 15295711cd5SPawel Dembicki #define VSC73XX_MFLODMASK 0x0f 15395711cd5SPawel Dembicki #define VSC73XX_RECVMASK 0x10 15495711cd5SPawel Dembicki #define VSC73XX_AGGRCTRL 0x20 15595711cd5SPawel Dembicki #define VSC73XX_AGGRMSKS 0x30 /* Until 0x3f */ 15695711cd5SPawel Dembicki #define VSC73XX_DSTMASKS 0x40 /* Until 0x7f */ 15795711cd5SPawel Dembicki #define VSC73XX_SRCMASKS 0x80 /* Until 0x87 */ 15895711cd5SPawel Dembicki #define VSC73XX_CAPENAB 0xa0 15995711cd5SPawel Dembicki #define VSC73XX_MACACCESS 0xb0 16095711cd5SPawel Dembicki #define VSC73XX_IPMCACCESS 0xb1 16195711cd5SPawel Dembicki #define VSC73XX_MACTINDX 0xc0 16295711cd5SPawel Dembicki #define VSC73XX_VLANACCESS 0xd0 16395711cd5SPawel Dembicki #define VSC73XX_VLANTIDX 0xe0 16495711cd5SPawel Dembicki #define VSC73XX_AGENCTRL 0xf0 16595711cd5SPawel Dembicki #define VSC73XX_CAPRST 0xff 16695711cd5SPawel Dembicki 16795711cd5SPawel Dembicki #define VSC73XX_MACACCESS_CPU_COPY BIT(14) 16895711cd5SPawel Dembicki #define VSC73XX_MACACCESS_FWD_KILL BIT(13) 16995711cd5SPawel Dembicki #define VSC73XX_MACACCESS_IGNORE_VLAN BIT(12) 17095711cd5SPawel Dembicki #define VSC73XX_MACACCESS_AGED_FLAG BIT(11) 17195711cd5SPawel Dembicki #define VSC73XX_MACACCESS_VALID BIT(10) 17295711cd5SPawel Dembicki #define VSC73XX_MACACCESS_LOCKED BIT(9) 17395711cd5SPawel Dembicki #define VSC73XX_MACACCESS_DEST_IDX_MASK GENMASK(8, 3) 17495711cd5SPawel Dembicki #define VSC73XX_MACACCESS_CMD_MASK GENMASK(2, 0) 17595711cd5SPawel Dembicki #define VSC73XX_MACACCESS_CMD_IDLE 0 17695711cd5SPawel Dembicki #define VSC73XX_MACACCESS_CMD_LEARN 1 17795711cd5SPawel Dembicki #define VSC73XX_MACACCESS_CMD_FORGET 2 17895711cd5SPawel Dembicki #define VSC73XX_MACACCESS_CMD_AGE_TABLE 3 17995711cd5SPawel Dembicki #define VSC73XX_MACACCESS_CMD_FLUSH_TABLE 4 18095711cd5SPawel Dembicki #define VSC73XX_MACACCESS_CMD_CLEAR_TABLE 5 18195711cd5SPawel Dembicki #define VSC73XX_MACACCESS_CMD_READ_ENTRY 6 18295711cd5SPawel Dembicki #define VSC73XX_MACACCESS_CMD_WRITE_ENTRY 7 18395711cd5SPawel Dembicki 18495711cd5SPawel Dembicki #define VSC73XX_VLANACCESS_LEARN_DISABLED BIT(30) 18595711cd5SPawel Dembicki #define VSC73XX_VLANACCESS_VLAN_MIRROR BIT(29) 18695711cd5SPawel Dembicki #define VSC73XX_VLANACCESS_VLAN_SRC_CHECK BIT(28) 18795711cd5SPawel Dembicki #define VSC73XX_VLANACCESS_VLAN_PORT_MASK GENMASK(9, 2) 18895711cd5SPawel Dembicki #define VSC73XX_VLANACCESS_VLAN_TBL_CMD_MASK GENMASK(2, 0) 18995711cd5SPawel Dembicki #define VSC73XX_VLANACCESS_VLAN_TBL_CMD_IDLE 0 19095711cd5SPawel Dembicki #define VSC73XX_VLANACCESS_VLAN_TBL_CMD_READ_ENTRY 1 19195711cd5SPawel Dembicki #define VSC73XX_VLANACCESS_VLAN_TBL_CMD_WRITE_ENTRY 2 19295711cd5SPawel Dembicki #define VSC73XX_VLANACCESS_VLAN_TBL_CMD_CLEAR_TABLE 3 19395711cd5SPawel Dembicki 19495711cd5SPawel Dembicki /* MII block 3 registers */ 19595711cd5SPawel Dembicki #define VSC73XX_MII_STAT 0x0 19695711cd5SPawel Dembicki #define VSC73XX_MII_CMD 0x1 19795711cd5SPawel Dembicki #define VSC73XX_MII_DATA 0x2 19895711cd5SPawel Dembicki 19995711cd5SPawel Dembicki /* Arbiter block 5 registers */ 20095711cd5SPawel Dembicki #define VSC73XX_ARBEMPTY 0x0c 20195711cd5SPawel Dembicki #define VSC73XX_ARBDISC 0x0e 20295711cd5SPawel Dembicki #define VSC73XX_SBACKWDROP 0x12 20395711cd5SPawel Dembicki #define VSC73XX_DBACKWDROP 0x13 20495711cd5SPawel Dembicki #define VSC73XX_ARBBURSTPROB 0x15 20595711cd5SPawel Dembicki 20695711cd5SPawel Dembicki /* System block 7 registers */ 20795711cd5SPawel Dembicki #define VSC73XX_ICPU_SIPAD 0x01 20895711cd5SPawel Dembicki #define VSC73XX_GMIIDELAY 0x05 20995711cd5SPawel Dembicki #define VSC73XX_ICPU_CTRL 0x10 21095711cd5SPawel Dembicki #define VSC73XX_ICPU_ADDR 0x11 21195711cd5SPawel Dembicki #define VSC73XX_ICPU_SRAM 0x12 21295711cd5SPawel Dembicki #define VSC73XX_HWSEM 0x13 21395711cd5SPawel Dembicki #define VSC73XX_GLORESET 0x14 21495711cd5SPawel Dembicki #define VSC73XX_ICPU_MBOX_VAL 0x15 21595711cd5SPawel Dembicki #define VSC73XX_ICPU_MBOX_SET 0x16 21695711cd5SPawel Dembicki #define VSC73XX_ICPU_MBOX_CLR 0x17 21795711cd5SPawel Dembicki #define VSC73XX_CHIPID 0x18 21895711cd5SPawel Dembicki #define VSC73XX_GPIO 0x34 21995711cd5SPawel Dembicki 22095711cd5SPawel Dembicki #define VSC73XX_GMIIDELAY_GMII0_GTXDELAY_NONE 0 22195711cd5SPawel Dembicki #define VSC73XX_GMIIDELAY_GMII0_GTXDELAY_1_4_NS 1 22295711cd5SPawel Dembicki #define VSC73XX_GMIIDELAY_GMII0_GTXDELAY_1_7_NS 2 22395711cd5SPawel Dembicki #define VSC73XX_GMIIDELAY_GMII0_GTXDELAY_2_0_NS 3 22495711cd5SPawel Dembicki 22595711cd5SPawel Dembicki #define VSC73XX_GMIIDELAY_GMII0_RXDELAY_NONE (0 << 4) 22695711cd5SPawel Dembicki #define VSC73XX_GMIIDELAY_GMII0_RXDELAY_1_4_NS (1 << 4) 22795711cd5SPawel Dembicki #define VSC73XX_GMIIDELAY_GMII0_RXDELAY_1_7_NS (2 << 4) 22895711cd5SPawel Dembicki #define VSC73XX_GMIIDELAY_GMII0_RXDELAY_2_0_NS (3 << 4) 22995711cd5SPawel Dembicki 23095711cd5SPawel Dembicki #define VSC73XX_ICPU_CTRL_WATCHDOG_RST BIT(31) 23195711cd5SPawel Dembicki #define VSC73XX_ICPU_CTRL_CLK_DIV_MASK GENMASK(12, 8) 23295711cd5SPawel Dembicki #define VSC73XX_ICPU_CTRL_SRST_HOLD BIT(7) 23395711cd5SPawel Dembicki #define VSC73XX_ICPU_CTRL_ICPU_PI_EN BIT(6) 23495711cd5SPawel Dembicki #define VSC73XX_ICPU_CTRL_BOOT_EN BIT(3) 23595711cd5SPawel Dembicki #define VSC73XX_ICPU_CTRL_EXT_ACC_EN BIT(2) 23695711cd5SPawel Dembicki #define VSC73XX_ICPU_CTRL_CLK_EN BIT(1) 23795711cd5SPawel Dembicki #define VSC73XX_ICPU_CTRL_SRST BIT(0) 23895711cd5SPawel Dembicki 23995711cd5SPawel Dembicki #define VSC73XX_CHIPID_ID_SHIFT 12 24095711cd5SPawel Dembicki #define VSC73XX_CHIPID_ID_MASK 0xffff 24195711cd5SPawel Dembicki #define VSC73XX_CHIPID_REV_SHIFT 28 24295711cd5SPawel Dembicki #define VSC73XX_CHIPID_REV_MASK 0xf 24395711cd5SPawel Dembicki #define VSC73XX_CHIPID_ID_7385 0x7385 24495711cd5SPawel Dembicki #define VSC73XX_CHIPID_ID_7388 0x7388 24595711cd5SPawel Dembicki #define VSC73XX_CHIPID_ID_7395 0x7395 24695711cd5SPawel Dembicki #define VSC73XX_CHIPID_ID_7398 0x7398 24795711cd5SPawel Dembicki 24895711cd5SPawel Dembicki #define VSC73XX_GLORESET_STROBE BIT(4) 24995711cd5SPawel Dembicki #define VSC73XX_GLORESET_ICPU_LOCK BIT(3) 25095711cd5SPawel Dembicki #define VSC73XX_GLORESET_MEM_LOCK BIT(2) 25195711cd5SPawel Dembicki #define VSC73XX_GLORESET_PHY_RESET BIT(1) 25295711cd5SPawel Dembicki #define VSC73XX_GLORESET_MASTER_RESET BIT(0) 25395711cd5SPawel Dembicki 25495711cd5SPawel Dembicki #define VSC7385_CLOCK_DELAY ((3 << 4) | 3) 25595711cd5SPawel Dembicki #define VSC7385_CLOCK_DELAY_MASK ((3 << 4) | 3) 25695711cd5SPawel Dembicki 25795711cd5SPawel Dembicki #define VSC73XX_ICPU_CTRL_STOP (VSC73XX_ICPU_CTRL_SRST_HOLD | \ 25895711cd5SPawel Dembicki VSC73XX_ICPU_CTRL_BOOT_EN | \ 25995711cd5SPawel Dembicki VSC73XX_ICPU_CTRL_EXT_ACC_EN) 26095711cd5SPawel Dembicki 26195711cd5SPawel Dembicki #define VSC73XX_ICPU_CTRL_START (VSC73XX_ICPU_CTRL_CLK_DIV | \ 26295711cd5SPawel Dembicki VSC73XX_ICPU_CTRL_BOOT_EN | \ 26395711cd5SPawel Dembicki VSC73XX_ICPU_CTRL_CLK_EN | \ 26495711cd5SPawel Dembicki VSC73XX_ICPU_CTRL_SRST) 26595711cd5SPawel Dembicki 26695711cd5SPawel Dembicki #define IS_7385(a) ((a)->chipid == VSC73XX_CHIPID_ID_7385) 26795711cd5SPawel Dembicki #define IS_7388(a) ((a)->chipid == VSC73XX_CHIPID_ID_7388) 26895711cd5SPawel Dembicki #define IS_7395(a) ((a)->chipid == VSC73XX_CHIPID_ID_7395) 26995711cd5SPawel Dembicki #define IS_7398(a) ((a)->chipid == VSC73XX_CHIPID_ID_7398) 27095711cd5SPawel Dembicki #define IS_739X(a) (IS_7395(a) || IS_7398(a)) 27195711cd5SPawel Dembicki 27295711cd5SPawel Dembicki struct vsc73xx_counter { 27395711cd5SPawel Dembicki u8 counter; 27495711cd5SPawel Dembicki const char *name; 27595711cd5SPawel Dembicki }; 27695711cd5SPawel Dembicki 27795711cd5SPawel Dembicki /* Counters are named according to the MIB standards where applicable. 27895711cd5SPawel Dembicki * Some counters are custom, non-standard. The standard counters are 27995711cd5SPawel Dembicki * named in accordance with RFC2819, RFC2021 and IEEE Std 802.3-2002 Annex 28095711cd5SPawel Dembicki * 30A Counters. 28195711cd5SPawel Dembicki */ 28295711cd5SPawel Dembicki static const struct vsc73xx_counter vsc73xx_rx_counters[] = { 28395711cd5SPawel Dembicki { 0, "RxEtherStatsPkts" }, 28495711cd5SPawel Dembicki { 1, "RxBroadcast+MulticastPkts" }, /* non-standard counter */ 28595711cd5SPawel Dembicki { 2, "RxTotalErrorPackets" }, /* non-standard counter */ 28695711cd5SPawel Dembicki { 3, "RxEtherStatsBroadcastPkts" }, 28795711cd5SPawel Dembicki { 4, "RxEtherStatsMulticastPkts" }, 28895711cd5SPawel Dembicki { 5, "RxEtherStatsPkts64Octets" }, 28995711cd5SPawel Dembicki { 6, "RxEtherStatsPkts65to127Octets" }, 29095711cd5SPawel Dembicki { 7, "RxEtherStatsPkts128to255Octets" }, 29195711cd5SPawel Dembicki { 8, "RxEtherStatsPkts256to511Octets" }, 29295711cd5SPawel Dembicki { 9, "RxEtherStatsPkts512to1023Octets" }, 29395711cd5SPawel Dembicki { 10, "RxEtherStatsPkts1024to1518Octets" }, 29495711cd5SPawel Dembicki { 11, "RxJumboFrames" }, /* non-standard counter */ 29595711cd5SPawel Dembicki { 12, "RxaPauseMACControlFramesTransmitted" }, 29695711cd5SPawel Dembicki { 13, "RxFIFODrops" }, /* non-standard counter */ 29795711cd5SPawel Dembicki { 14, "RxBackwardDrops" }, /* non-standard counter */ 29895711cd5SPawel Dembicki { 15, "RxClassifierDrops" }, /* non-standard counter */ 29995711cd5SPawel Dembicki { 16, "RxEtherStatsCRCAlignErrors" }, 30095711cd5SPawel Dembicki { 17, "RxEtherStatsUndersizePkts" }, 30195711cd5SPawel Dembicki { 18, "RxEtherStatsOversizePkts" }, 30295711cd5SPawel Dembicki { 19, "RxEtherStatsFragments" }, 30395711cd5SPawel Dembicki { 20, "RxEtherStatsJabbers" }, 30495711cd5SPawel Dembicki { 21, "RxaMACControlFramesReceived" }, 30595711cd5SPawel Dembicki /* 22-24 are undefined */ 30695711cd5SPawel Dembicki { 25, "RxaFramesReceivedOK" }, 30795711cd5SPawel Dembicki { 26, "RxQoSClass0" }, /* non-standard counter */ 30895711cd5SPawel Dembicki { 27, "RxQoSClass1" }, /* non-standard counter */ 30995711cd5SPawel Dembicki { 28, "RxQoSClass2" }, /* non-standard counter */ 31095711cd5SPawel Dembicki { 29, "RxQoSClass3" }, /* non-standard counter */ 31195711cd5SPawel Dembicki }; 31295711cd5SPawel Dembicki 31395711cd5SPawel Dembicki static const struct vsc73xx_counter vsc73xx_tx_counters[] = { 31495711cd5SPawel Dembicki { 0, "TxEtherStatsPkts" }, 31595711cd5SPawel Dembicki { 1, "TxBroadcast+MulticastPkts" }, /* non-standard counter */ 31695711cd5SPawel Dembicki { 2, "TxTotalErrorPackets" }, /* non-standard counter */ 31795711cd5SPawel Dembicki { 3, "TxEtherStatsBroadcastPkts" }, 31895711cd5SPawel Dembicki { 4, "TxEtherStatsMulticastPkts" }, 31995711cd5SPawel Dembicki { 5, "TxEtherStatsPkts64Octets" }, 32095711cd5SPawel Dembicki { 6, "TxEtherStatsPkts65to127Octets" }, 32195711cd5SPawel Dembicki { 7, "TxEtherStatsPkts128to255Octets" }, 32295711cd5SPawel Dembicki { 8, "TxEtherStatsPkts256to511Octets" }, 32395711cd5SPawel Dembicki { 9, "TxEtherStatsPkts512to1023Octets" }, 32495711cd5SPawel Dembicki { 10, "TxEtherStatsPkts1024to1518Octets" }, 32595711cd5SPawel Dembicki { 11, "TxJumboFrames" }, /* non-standard counter */ 32695711cd5SPawel Dembicki { 12, "TxaPauseMACControlFramesTransmitted" }, 32795711cd5SPawel Dembicki { 13, "TxFIFODrops" }, /* non-standard counter */ 32895711cd5SPawel Dembicki { 14, "TxDrops" }, /* non-standard counter */ 32995711cd5SPawel Dembicki { 15, "TxEtherStatsCollisions" }, 33095711cd5SPawel Dembicki { 16, "TxEtherStatsCRCAlignErrors" }, 33195711cd5SPawel Dembicki { 17, "TxEtherStatsUndersizePkts" }, 33295711cd5SPawel Dembicki { 18, "TxEtherStatsOversizePkts" }, 33395711cd5SPawel Dembicki { 19, "TxEtherStatsFragments" }, 33495711cd5SPawel Dembicki { 20, "TxEtherStatsJabbers" }, 33595711cd5SPawel Dembicki /* 21-24 are undefined */ 33695711cd5SPawel Dembicki { 25, "TxaFramesReceivedOK" }, 33795711cd5SPawel Dembicki { 26, "TxQoSClass0" }, /* non-standard counter */ 33895711cd5SPawel Dembicki { 27, "TxQoSClass1" }, /* non-standard counter */ 33995711cd5SPawel Dembicki { 28, "TxQoSClass2" }, /* non-standard counter */ 34095711cd5SPawel Dembicki { 29, "TxQoSClass3" }, /* non-standard counter */ 34195711cd5SPawel Dembicki }; 34295711cd5SPawel Dembicki 34395711cd5SPawel Dembicki int vsc73xx_is_addr_valid(u8 block, u8 subblock) 34495711cd5SPawel Dembicki { 34595711cd5SPawel Dembicki switch (block) { 34695711cd5SPawel Dembicki case VSC73XX_BLOCK_MAC: 34795711cd5SPawel Dembicki switch (subblock) { 34895711cd5SPawel Dembicki case 0 ... 4: 34995711cd5SPawel Dembicki case 6: 35095711cd5SPawel Dembicki return 1; 35195711cd5SPawel Dembicki } 35295711cd5SPawel Dembicki break; 35395711cd5SPawel Dembicki 35495711cd5SPawel Dembicki case VSC73XX_BLOCK_ANALYZER: 35595711cd5SPawel Dembicki case VSC73XX_BLOCK_SYSTEM: 35695711cd5SPawel Dembicki switch (subblock) { 35795711cd5SPawel Dembicki case 0: 35895711cd5SPawel Dembicki return 1; 35995711cd5SPawel Dembicki } 36095711cd5SPawel Dembicki break; 36195711cd5SPawel Dembicki 36295711cd5SPawel Dembicki case VSC73XX_BLOCK_MII: 36395711cd5SPawel Dembicki case VSC73XX_BLOCK_CAPTURE: 36495711cd5SPawel Dembicki case VSC73XX_BLOCK_ARBITER: 36595711cd5SPawel Dembicki switch (subblock) { 36695711cd5SPawel Dembicki case 0 ... 1: 36795711cd5SPawel Dembicki return 1; 36895711cd5SPawel Dembicki } 36995711cd5SPawel Dembicki break; 37095711cd5SPawel Dembicki } 37195711cd5SPawel Dembicki 37295711cd5SPawel Dembicki return 0; 37395711cd5SPawel Dembicki } 37495711cd5SPawel Dembicki EXPORT_SYMBOL(vsc73xx_is_addr_valid); 37595711cd5SPawel Dembicki 37695711cd5SPawel Dembicki static int vsc73xx_read(struct vsc73xx *vsc, u8 block, u8 subblock, u8 reg, 37795711cd5SPawel Dembicki u32 *val) 37895711cd5SPawel Dembicki { 37995711cd5SPawel Dembicki return vsc->ops->read(vsc, block, subblock, reg, val); 38095711cd5SPawel Dembicki } 38195711cd5SPawel Dembicki 38295711cd5SPawel Dembicki static int vsc73xx_write(struct vsc73xx *vsc, u8 block, u8 subblock, u8 reg, 38395711cd5SPawel Dembicki u32 val) 38495711cd5SPawel Dembicki { 38595711cd5SPawel Dembicki return vsc->ops->write(vsc, block, subblock, reg, val); 38695711cd5SPawel Dembicki } 38795711cd5SPawel Dembicki 38895711cd5SPawel Dembicki static int vsc73xx_update_bits(struct vsc73xx *vsc, u8 block, u8 subblock, 38995711cd5SPawel Dembicki u8 reg, u32 mask, u32 val) 39095711cd5SPawel Dembicki { 39195711cd5SPawel Dembicki u32 tmp, orig; 39295711cd5SPawel Dembicki int ret; 39395711cd5SPawel Dembicki 39495711cd5SPawel Dembicki /* Same read-modify-write algorithm as e.g. regmap */ 39595711cd5SPawel Dembicki ret = vsc73xx_read(vsc, block, subblock, reg, &orig); 39695711cd5SPawel Dembicki if (ret) 39795711cd5SPawel Dembicki return ret; 39895711cd5SPawel Dembicki tmp = orig & ~mask; 39995711cd5SPawel Dembicki tmp |= val & mask; 40095711cd5SPawel Dembicki return vsc73xx_write(vsc, block, subblock, reg, tmp); 40195711cd5SPawel Dembicki } 40295711cd5SPawel Dembicki 40395711cd5SPawel Dembicki static int vsc73xx_detect(struct vsc73xx *vsc) 40495711cd5SPawel Dembicki { 40595711cd5SPawel Dembicki bool icpu_si_boot_en; 40695711cd5SPawel Dembicki bool icpu_pi_en; 40795711cd5SPawel Dembicki u32 val; 40895711cd5SPawel Dembicki u32 rev; 40995711cd5SPawel Dembicki int ret; 41095711cd5SPawel Dembicki u32 id; 41195711cd5SPawel Dembicki 41295711cd5SPawel Dembicki ret = vsc73xx_read(vsc, VSC73XX_BLOCK_SYSTEM, 0, 41395711cd5SPawel Dembicki VSC73XX_ICPU_MBOX_VAL, &val); 41495711cd5SPawel Dembicki if (ret) { 41595711cd5SPawel Dembicki dev_err(vsc->dev, "unable to read mailbox (%d)\n", ret); 41695711cd5SPawel Dembicki return ret; 41795711cd5SPawel Dembicki } 41895711cd5SPawel Dembicki 41995711cd5SPawel Dembicki if (val == 0xffffffff) { 4201da39ff0SPawel Dembicki dev_info(vsc->dev, "chip seems dead.\n"); 4211da39ff0SPawel Dembicki return -EAGAIN; 42295711cd5SPawel Dembicki } 42395711cd5SPawel Dembicki 42495711cd5SPawel Dembicki ret = vsc73xx_read(vsc, VSC73XX_BLOCK_SYSTEM, 0, 42595711cd5SPawel Dembicki VSC73XX_CHIPID, &val); 42695711cd5SPawel Dembicki if (ret) { 42795711cd5SPawel Dembicki dev_err(vsc->dev, "unable to read chip id (%d)\n", ret); 42895711cd5SPawel Dembicki return ret; 42995711cd5SPawel Dembicki } 43095711cd5SPawel Dembicki 43195711cd5SPawel Dembicki id = (val >> VSC73XX_CHIPID_ID_SHIFT) & 43295711cd5SPawel Dembicki VSC73XX_CHIPID_ID_MASK; 43395711cd5SPawel Dembicki switch (id) { 43495711cd5SPawel Dembicki case VSC73XX_CHIPID_ID_7385: 43595711cd5SPawel Dembicki case VSC73XX_CHIPID_ID_7388: 43695711cd5SPawel Dembicki case VSC73XX_CHIPID_ID_7395: 43795711cd5SPawel Dembicki case VSC73XX_CHIPID_ID_7398: 43895711cd5SPawel Dembicki break; 43995711cd5SPawel Dembicki default: 44095711cd5SPawel Dembicki dev_err(vsc->dev, "unsupported chip, id=%04x\n", id); 44195711cd5SPawel Dembicki return -ENODEV; 44295711cd5SPawel Dembicki } 44395711cd5SPawel Dembicki 44495711cd5SPawel Dembicki vsc->chipid = id; 44595711cd5SPawel Dembicki rev = (val >> VSC73XX_CHIPID_REV_SHIFT) & 44695711cd5SPawel Dembicki VSC73XX_CHIPID_REV_MASK; 44795711cd5SPawel Dembicki dev_info(vsc->dev, "VSC%04X (rev: %d) switch found\n", id, rev); 44895711cd5SPawel Dembicki 44995711cd5SPawel Dembicki ret = vsc73xx_read(vsc, VSC73XX_BLOCK_SYSTEM, 0, 45095711cd5SPawel Dembicki VSC73XX_ICPU_CTRL, &val); 45195711cd5SPawel Dembicki if (ret) { 45295711cd5SPawel Dembicki dev_err(vsc->dev, "unable to read iCPU control\n"); 45395711cd5SPawel Dembicki return ret; 45495711cd5SPawel Dembicki } 45595711cd5SPawel Dembicki 45695711cd5SPawel Dembicki /* The iCPU can always be used but can boot in different ways. 45795711cd5SPawel Dembicki * If it is initially disabled and has no external memory, 45895711cd5SPawel Dembicki * we are in control and can do whatever we like, else we 45995711cd5SPawel Dembicki * are probably in trouble (we need some way to communicate 46095711cd5SPawel Dembicki * with the running firmware) so we bail out for now. 46195711cd5SPawel Dembicki */ 46295711cd5SPawel Dembicki icpu_pi_en = !!(val & VSC73XX_ICPU_CTRL_ICPU_PI_EN); 46395711cd5SPawel Dembicki icpu_si_boot_en = !!(val & VSC73XX_ICPU_CTRL_BOOT_EN); 46495711cd5SPawel Dembicki if (icpu_si_boot_en && icpu_pi_en) { 46595711cd5SPawel Dembicki dev_err(vsc->dev, 46695711cd5SPawel Dembicki "iCPU enabled boots from SI, has external memory\n"); 46795711cd5SPawel Dembicki dev_err(vsc->dev, "no idea how to deal with this\n"); 46895711cd5SPawel Dembicki return -ENODEV; 46995711cd5SPawel Dembicki } 47095711cd5SPawel Dembicki if (icpu_si_boot_en && !icpu_pi_en) { 47195711cd5SPawel Dembicki dev_err(vsc->dev, 4721da39ff0SPawel Dembicki "iCPU enabled boots from PI/SI, no external memory\n"); 4731da39ff0SPawel Dembicki return -EAGAIN; 47495711cd5SPawel Dembicki } 47595711cd5SPawel Dembicki if (!icpu_si_boot_en && icpu_pi_en) { 47695711cd5SPawel Dembicki dev_err(vsc->dev, 47795711cd5SPawel Dembicki "iCPU enabled, boots from PI external memory\n"); 47895711cd5SPawel Dembicki dev_err(vsc->dev, "no idea how to deal with this\n"); 47995711cd5SPawel Dembicki return -ENODEV; 48095711cd5SPawel Dembicki } 48195711cd5SPawel Dembicki /* !icpu_si_boot_en && !cpu_pi_en */ 48295711cd5SPawel Dembicki dev_info(vsc->dev, "iCPU disabled, no external memory\n"); 48395711cd5SPawel Dembicki 48495711cd5SPawel Dembicki return 0; 48595711cd5SPawel Dembicki } 48695711cd5SPawel Dembicki 48795711cd5SPawel Dembicki static int vsc73xx_phy_read(struct dsa_switch *ds, int phy, int regnum) 48895711cd5SPawel Dembicki { 48995711cd5SPawel Dembicki struct vsc73xx *vsc = ds->priv; 49095711cd5SPawel Dembicki u32 cmd; 49195711cd5SPawel Dembicki u32 val; 49295711cd5SPawel Dembicki int ret; 49395711cd5SPawel Dembicki 49495711cd5SPawel Dembicki /* Setting bit 26 means "read" */ 49595711cd5SPawel Dembicki cmd = BIT(26) | (phy << 21) | (regnum << 16); 49695711cd5SPawel Dembicki ret = vsc73xx_write(vsc, VSC73XX_BLOCK_MII, 0, 1, cmd); 49795711cd5SPawel Dembicki if (ret) 49895711cd5SPawel Dembicki return ret; 49995711cd5SPawel Dembicki msleep(2); 50095711cd5SPawel Dembicki ret = vsc73xx_read(vsc, VSC73XX_BLOCK_MII, 0, 2, &val); 50195711cd5SPawel Dembicki if (ret) 50295711cd5SPawel Dembicki return ret; 50395711cd5SPawel Dembicki if (val & BIT(16)) { 50495711cd5SPawel Dembicki dev_err(vsc->dev, "reading reg %02x from phy%d failed\n", 50595711cd5SPawel Dembicki regnum, phy); 50695711cd5SPawel Dembicki return -EIO; 50795711cd5SPawel Dembicki } 50895711cd5SPawel Dembicki val &= 0xFFFFU; 50995711cd5SPawel Dembicki 51095711cd5SPawel Dembicki dev_dbg(vsc->dev, "read reg %02x from phy%d = %04x\n", 51195711cd5SPawel Dembicki regnum, phy, val); 51295711cd5SPawel Dembicki 51395711cd5SPawel Dembicki return val; 51495711cd5SPawel Dembicki } 51595711cd5SPawel Dembicki 51695711cd5SPawel Dembicki static int vsc73xx_phy_write(struct dsa_switch *ds, int phy, int regnum, 51795711cd5SPawel Dembicki u16 val) 51895711cd5SPawel Dembicki { 51995711cd5SPawel Dembicki struct vsc73xx *vsc = ds->priv; 52095711cd5SPawel Dembicki u32 cmd; 52195711cd5SPawel Dembicki int ret; 52295711cd5SPawel Dembicki 52395711cd5SPawel Dembicki /* It was found through tedious experiments that this router 52495711cd5SPawel Dembicki * chip really hates to have it's PHYs reset. They 52595711cd5SPawel Dembicki * never recover if that happens: autonegotiation stops 52695711cd5SPawel Dembicki * working after a reset. Just filter out this command. 52795711cd5SPawel Dembicki * (Resetting the whole chip is OK.) 52895711cd5SPawel Dembicki */ 52995711cd5SPawel Dembicki if (regnum == 0 && (val & BIT(15))) { 53095711cd5SPawel Dembicki dev_info(vsc->dev, "reset PHY - disallowed\n"); 53195711cd5SPawel Dembicki return 0; 53295711cd5SPawel Dembicki } 53395711cd5SPawel Dembicki 53495711cd5SPawel Dembicki cmd = (phy << 21) | (regnum << 16); 53595711cd5SPawel Dembicki ret = vsc73xx_write(vsc, VSC73XX_BLOCK_MII, 0, 1, cmd); 53695711cd5SPawel Dembicki if (ret) 53795711cd5SPawel Dembicki return ret; 53895711cd5SPawel Dembicki 53995711cd5SPawel Dembicki dev_dbg(vsc->dev, "write %04x to reg %02x in phy%d\n", 54095711cd5SPawel Dembicki val, regnum, phy); 54195711cd5SPawel Dembicki return 0; 54295711cd5SPawel Dembicki } 54395711cd5SPawel Dembicki 54495711cd5SPawel Dembicki static enum dsa_tag_protocol vsc73xx_get_tag_protocol(struct dsa_switch *ds, 5454d776482SFlorian Fainelli int port, 5464d776482SFlorian Fainelli enum dsa_tag_protocol mp) 54795711cd5SPawel Dembicki { 54895711cd5SPawel Dembicki /* The switch internally uses a 8 byte header with length, 54995711cd5SPawel Dembicki * source port, tag, LPA and priority. This is supposedly 55095711cd5SPawel Dembicki * only accessible when operating the switch using the internal 55195711cd5SPawel Dembicki * CPU or with an external CPU mapping the device in, but not 55295711cd5SPawel Dembicki * when operating the switch over SPI and putting frames in/out 55395711cd5SPawel Dembicki * on port 6 (the CPU port). So far we must assume that we 55495711cd5SPawel Dembicki * cannot access the tag. (See "Internal frame header" section 55595711cd5SPawel Dembicki * 3.9.1 in the manual.) 55695711cd5SPawel Dembicki */ 55795711cd5SPawel Dembicki return DSA_TAG_PROTO_NONE; 55895711cd5SPawel Dembicki } 55995711cd5SPawel Dembicki 56095711cd5SPawel Dembicki static int vsc73xx_setup(struct dsa_switch *ds) 56195711cd5SPawel Dembicki { 56295711cd5SPawel Dembicki struct vsc73xx *vsc = ds->priv; 56395711cd5SPawel Dembicki int i; 56495711cd5SPawel Dembicki 56595711cd5SPawel Dembicki dev_info(vsc->dev, "set up the switch\n"); 56695711cd5SPawel Dembicki 56795711cd5SPawel Dembicki /* Issue RESET */ 56895711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_SYSTEM, 0, VSC73XX_GLORESET, 56995711cd5SPawel Dembicki VSC73XX_GLORESET_MASTER_RESET); 57095711cd5SPawel Dembicki usleep_range(125, 200); 57195711cd5SPawel Dembicki 57295711cd5SPawel Dembicki /* Initialize memory, initialize RAM bank 0..15 except 6 and 7 57395711cd5SPawel Dembicki * This sequence appears in the 57495711cd5SPawel Dembicki * VSC7385 SparX-G5 datasheet section 6.6.1 57595711cd5SPawel Dembicki * VSC7395 SparX-G5e datasheet section 6.6.1 57695711cd5SPawel Dembicki * "initialization sequence". 57795711cd5SPawel Dembicki * No explanation is given to the 0x1010400 magic number. 57895711cd5SPawel Dembicki */ 57995711cd5SPawel Dembicki for (i = 0; i <= 15; i++) { 58095711cd5SPawel Dembicki if (i != 6 && i != 7) { 58195711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_MEMINIT, 58295711cd5SPawel Dembicki 2, 58395711cd5SPawel Dembicki 0, 0x1010400 + i); 58495711cd5SPawel Dembicki mdelay(1); 58595711cd5SPawel Dembicki } 58695711cd5SPawel Dembicki } 58795711cd5SPawel Dembicki mdelay(30); 58895711cd5SPawel Dembicki 58995711cd5SPawel Dembicki /* Clear MAC table */ 59095711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_ANALYZER, 0, 59195711cd5SPawel Dembicki VSC73XX_MACACCESS, 59295711cd5SPawel Dembicki VSC73XX_MACACCESS_CMD_CLEAR_TABLE); 59395711cd5SPawel Dembicki 59495711cd5SPawel Dembicki /* Clear VLAN table */ 59595711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_ANALYZER, 0, 59695711cd5SPawel Dembicki VSC73XX_VLANACCESS, 59795711cd5SPawel Dembicki VSC73XX_VLANACCESS_VLAN_TBL_CMD_CLEAR_TABLE); 59895711cd5SPawel Dembicki 59995711cd5SPawel Dembicki msleep(40); 60095711cd5SPawel Dembicki 60195711cd5SPawel Dembicki /* Use 20KiB buffers on all ports on VSC7395 60295711cd5SPawel Dembicki * The VSC7385 has 16KiB buffers and that is the 60395711cd5SPawel Dembicki * default if we don't set this up explicitly. 60495711cd5SPawel Dembicki * Port "31" is "all ports". 60595711cd5SPawel Dembicki */ 60695711cd5SPawel Dembicki if (IS_739X(vsc)) 60795711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 0x1f, 60895711cd5SPawel Dembicki VSC73XX_Q_MISC_CONF, 60995711cd5SPawel Dembicki VSC73XX_Q_MISC_CONF_EXTENT_MEM); 61095711cd5SPawel Dembicki 61195711cd5SPawel Dembicki /* Put all ports into reset until enabled */ 61295711cd5SPawel Dembicki for (i = 0; i < 7; i++) { 61395711cd5SPawel Dembicki if (i == 5) 61495711cd5SPawel Dembicki continue; 61595711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 4, 61695711cd5SPawel Dembicki VSC73XX_MAC_CFG, VSC73XX_MAC_CFG_RESET); 61795711cd5SPawel Dembicki } 61895711cd5SPawel Dembicki 61995711cd5SPawel Dembicki /* MII delay, set both GTX and RX delay to 2 ns */ 62095711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_SYSTEM, 0, VSC73XX_GMIIDELAY, 62195711cd5SPawel Dembicki VSC73XX_GMIIDELAY_GMII0_GTXDELAY_2_0_NS | 62295711cd5SPawel Dembicki VSC73XX_GMIIDELAY_GMII0_RXDELAY_2_0_NS); 62395711cd5SPawel Dembicki /* Enable reception of frames on all ports */ 62495711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_ANALYZER, 0, VSC73XX_RECVMASK, 62595711cd5SPawel Dembicki 0x5f); 62695711cd5SPawel Dembicki /* IP multicast flood mask (table 144) */ 62795711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_ANALYZER, 0, VSC73XX_IFLODMSK, 62895711cd5SPawel Dembicki 0xff); 62995711cd5SPawel Dembicki 63095711cd5SPawel Dembicki mdelay(50); 63195711cd5SPawel Dembicki 63295711cd5SPawel Dembicki /* Release reset from the internal PHYs */ 63395711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_SYSTEM, 0, VSC73XX_GLORESET, 63495711cd5SPawel Dembicki VSC73XX_GLORESET_PHY_RESET); 63595711cd5SPawel Dembicki 63695711cd5SPawel Dembicki udelay(4); 63795711cd5SPawel Dembicki 63895711cd5SPawel Dembicki return 0; 63995711cd5SPawel Dembicki } 64095711cd5SPawel Dembicki 64195711cd5SPawel Dembicki static void vsc73xx_init_port(struct vsc73xx *vsc, int port) 64295711cd5SPawel Dembicki { 64395711cd5SPawel Dembicki u32 val; 64495711cd5SPawel Dembicki 64595711cd5SPawel Dembicki /* MAC configure, first reset the port and then write defaults */ 64695711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 64795711cd5SPawel Dembicki port, 64895711cd5SPawel Dembicki VSC73XX_MAC_CFG, 64995711cd5SPawel Dembicki VSC73XX_MAC_CFG_RESET); 65095711cd5SPawel Dembicki 65195711cd5SPawel Dembicki /* Take up the port in 1Gbit mode by default, this will be 65295711cd5SPawel Dembicki * augmented after auto-negotiation on the PHY-facing 65395711cd5SPawel Dembicki * ports. 65495711cd5SPawel Dembicki */ 65595711cd5SPawel Dembicki if (port == CPU_PORT) 65695711cd5SPawel Dembicki val = VSC73XX_MAC_CFG_1000M_F_RGMII; 65795711cd5SPawel Dembicki else 65895711cd5SPawel Dembicki val = VSC73XX_MAC_CFG_1000M_F_PHY; 65995711cd5SPawel Dembicki 66095711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 66195711cd5SPawel Dembicki port, 66295711cd5SPawel Dembicki VSC73XX_MAC_CFG, 66395711cd5SPawel Dembicki val | 66495711cd5SPawel Dembicki VSC73XX_MAC_CFG_TX_EN | 66595711cd5SPawel Dembicki VSC73XX_MAC_CFG_RX_EN); 66695711cd5SPawel Dembicki 66795711cd5SPawel Dembicki /* Flow control for the CPU port: 66895711cd5SPawel Dembicki * Use a zero delay pause frame when pause condition is left 66995711cd5SPawel Dembicki * Obey pause control frames 67095711cd5SPawel Dembicki */ 67195711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 67295711cd5SPawel Dembicki port, 67395711cd5SPawel Dembicki VSC73XX_FCCONF, 67495711cd5SPawel Dembicki VSC73XX_FCCONF_ZERO_PAUSE_EN | 67595711cd5SPawel Dembicki VSC73XX_FCCONF_FLOW_CTRL_OBEY); 67695711cd5SPawel Dembicki 67795711cd5SPawel Dembicki /* Issue pause control frames on PHY facing ports. 67895711cd5SPawel Dembicki * Allow early initiation of MAC transmission if the amount 67995711cd5SPawel Dembicki * of egress data is below 512 bytes on CPU port. 68095711cd5SPawel Dembicki * FIXME: enable 20KiB buffers? 68195711cd5SPawel Dembicki */ 68295711cd5SPawel Dembicki if (port == CPU_PORT) 68395711cd5SPawel Dembicki val = VSC73XX_Q_MISC_CONF_EARLY_TX_512; 68495711cd5SPawel Dembicki else 68595711cd5SPawel Dembicki val = VSC73XX_Q_MISC_CONF_MAC_PAUSE_MODE; 68695711cd5SPawel Dembicki val |= VSC73XX_Q_MISC_CONF_EXTENT_MEM; 68795711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 68895711cd5SPawel Dembicki port, 68995711cd5SPawel Dembicki VSC73XX_Q_MISC_CONF, 69095711cd5SPawel Dembicki val); 69195711cd5SPawel Dembicki 69295711cd5SPawel Dembicki /* Flow control MAC: a MAC address used in flow control frames */ 69395711cd5SPawel Dembicki val = (vsc->addr[5] << 16) | (vsc->addr[4] << 8) | (vsc->addr[3]); 69495711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 69595711cd5SPawel Dembicki port, 69695711cd5SPawel Dembicki VSC73XX_FCMACHI, 69795711cd5SPawel Dembicki val); 69895711cd5SPawel Dembicki val = (vsc->addr[2] << 16) | (vsc->addr[1] << 8) | (vsc->addr[0]); 69995711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 70095711cd5SPawel Dembicki port, 70195711cd5SPawel Dembicki VSC73XX_FCMACLO, 70295711cd5SPawel Dembicki val); 70395711cd5SPawel Dembicki 70495711cd5SPawel Dembicki /* Tell the categorizer to forward pause frames, not control 70595711cd5SPawel Dembicki * frame. Do not drop anything. 70695711cd5SPawel Dembicki */ 70795711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 70895711cd5SPawel Dembicki port, 70995711cd5SPawel Dembicki VSC73XX_CAT_DROP, 71095711cd5SPawel Dembicki VSC73XX_CAT_DROP_FWD_PAUSE_ENA); 71195711cd5SPawel Dembicki 71295711cd5SPawel Dembicki /* Clear all counters */ 71395711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 71495711cd5SPawel Dembicki port, VSC73XX_C_RX0, 0); 71595711cd5SPawel Dembicki } 71695711cd5SPawel Dembicki 71795711cd5SPawel Dembicki static void vsc73xx_adjust_enable_port(struct vsc73xx *vsc, 71895711cd5SPawel Dembicki int port, struct phy_device *phydev, 71995711cd5SPawel Dembicki u32 initval) 72095711cd5SPawel Dembicki { 72195711cd5SPawel Dembicki u32 val = initval; 72295711cd5SPawel Dembicki u8 seed; 72395711cd5SPawel Dembicki 72495711cd5SPawel Dembicki /* Reset this port FIXME: break out subroutine */ 72595711cd5SPawel Dembicki val |= VSC73XX_MAC_CFG_RESET; 72695711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port, VSC73XX_MAC_CFG, val); 72795711cd5SPawel Dembicki 72895711cd5SPawel Dembicki /* Seed the port randomness with randomness */ 72995711cd5SPawel Dembicki get_random_bytes(&seed, 1); 73095711cd5SPawel Dembicki val |= seed << VSC73XX_MAC_CFG_SEED_OFFSET; 73195711cd5SPawel Dembicki val |= VSC73XX_MAC_CFG_SEED_LOAD; 73295711cd5SPawel Dembicki val |= VSC73XX_MAC_CFG_WEXC_DIS; 73395711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port, VSC73XX_MAC_CFG, val); 73495711cd5SPawel Dembicki 73595711cd5SPawel Dembicki /* Flow control for the PHY facing ports: 73695711cd5SPawel Dembicki * Use a zero delay pause frame when pause condition is left 73795711cd5SPawel Dembicki * Obey pause control frames 73895711cd5SPawel Dembicki * When generating pause frames, use 0xff as pause value 73995711cd5SPawel Dembicki */ 74095711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port, VSC73XX_FCCONF, 74195711cd5SPawel Dembicki VSC73XX_FCCONF_ZERO_PAUSE_EN | 74295711cd5SPawel Dembicki VSC73XX_FCCONF_FLOW_CTRL_OBEY | 74395711cd5SPawel Dembicki 0xff); 74495711cd5SPawel Dembicki 74595711cd5SPawel Dembicki /* Disallow backward dropping of frames from this port */ 74695711cd5SPawel Dembicki vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ARBITER, 0, 74795711cd5SPawel Dembicki VSC73XX_SBACKWDROP, BIT(port), 0); 74895711cd5SPawel Dembicki 74995711cd5SPawel Dembicki /* Enable TX, RX, deassert reset, stop loading seed */ 75095711cd5SPawel Dembicki vsc73xx_update_bits(vsc, VSC73XX_BLOCK_MAC, port, 75195711cd5SPawel Dembicki VSC73XX_MAC_CFG, 75295711cd5SPawel Dembicki VSC73XX_MAC_CFG_RESET | VSC73XX_MAC_CFG_SEED_LOAD | 75395711cd5SPawel Dembicki VSC73XX_MAC_CFG_TX_EN | VSC73XX_MAC_CFG_RX_EN, 75495711cd5SPawel Dembicki VSC73XX_MAC_CFG_TX_EN | VSC73XX_MAC_CFG_RX_EN); 75595711cd5SPawel Dembicki } 75695711cd5SPawel Dembicki 75795711cd5SPawel Dembicki static void vsc73xx_adjust_link(struct dsa_switch *ds, int port, 75895711cd5SPawel Dembicki struct phy_device *phydev) 75995711cd5SPawel Dembicki { 76095711cd5SPawel Dembicki struct vsc73xx *vsc = ds->priv; 76195711cd5SPawel Dembicki u32 val; 76295711cd5SPawel Dembicki 76395711cd5SPawel Dembicki /* Special handling of the CPU-facing port */ 76495711cd5SPawel Dembicki if (port == CPU_PORT) { 76595711cd5SPawel Dembicki /* Other ports are already initialized but not this one */ 76695711cd5SPawel Dembicki vsc73xx_init_port(vsc, CPU_PORT); 76795711cd5SPawel Dembicki /* Select the external port for this interface (EXT_PORT) 76895711cd5SPawel Dembicki * Enable the GMII GTX external clock 76995711cd5SPawel Dembicki * Use double data rate (DDR mode) 77095711cd5SPawel Dembicki */ 77195711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 77295711cd5SPawel Dembicki CPU_PORT, 77395711cd5SPawel Dembicki VSC73XX_ADVPORTM, 77495711cd5SPawel Dembicki VSC73XX_ADVPORTM_EXT_PORT | 77595711cd5SPawel Dembicki VSC73XX_ADVPORTM_ENA_GTX | 77695711cd5SPawel Dembicki VSC73XX_ADVPORTM_DDR_MODE); 77795711cd5SPawel Dembicki } 77895711cd5SPawel Dembicki 77995711cd5SPawel Dembicki /* This is the MAC confiuration that always need to happen 78095711cd5SPawel Dembicki * after a PHY or the CPU port comes up or down. 78195711cd5SPawel Dembicki */ 78295711cd5SPawel Dembicki if (!phydev->link) { 78395711cd5SPawel Dembicki int maxloop = 10; 78495711cd5SPawel Dembicki 78595711cd5SPawel Dembicki dev_dbg(vsc->dev, "port %d: went down\n", 78695711cd5SPawel Dembicki port); 78795711cd5SPawel Dembicki 78895711cd5SPawel Dembicki /* Disable RX on this port */ 78995711cd5SPawel Dembicki vsc73xx_update_bits(vsc, VSC73XX_BLOCK_MAC, port, 79095711cd5SPawel Dembicki VSC73XX_MAC_CFG, 79195711cd5SPawel Dembicki VSC73XX_MAC_CFG_RX_EN, 0); 79295711cd5SPawel Dembicki 79395711cd5SPawel Dembicki /* Discard packets */ 79495711cd5SPawel Dembicki vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ARBITER, 0, 79595711cd5SPawel Dembicki VSC73XX_ARBDISC, BIT(port), BIT(port)); 79695711cd5SPawel Dembicki 79795711cd5SPawel Dembicki /* Wait until queue is empty */ 79895711cd5SPawel Dembicki vsc73xx_read(vsc, VSC73XX_BLOCK_ARBITER, 0, 79995711cd5SPawel Dembicki VSC73XX_ARBEMPTY, &val); 80095711cd5SPawel Dembicki while (!(val & BIT(port))) { 80195711cd5SPawel Dembicki msleep(1); 80295711cd5SPawel Dembicki vsc73xx_read(vsc, VSC73XX_BLOCK_ARBITER, 0, 80395711cd5SPawel Dembicki VSC73XX_ARBEMPTY, &val); 80495711cd5SPawel Dembicki if (--maxloop == 0) { 80595711cd5SPawel Dembicki dev_err(vsc->dev, 80695711cd5SPawel Dembicki "timeout waiting for block arbiter\n"); 80795711cd5SPawel Dembicki /* Continue anyway */ 80895711cd5SPawel Dembicki break; 80995711cd5SPawel Dembicki } 81095711cd5SPawel Dembicki } 81195711cd5SPawel Dembicki 81295711cd5SPawel Dembicki /* Put this port into reset */ 81395711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port, VSC73XX_MAC_CFG, 81495711cd5SPawel Dembicki VSC73XX_MAC_CFG_RESET); 81595711cd5SPawel Dembicki 81695711cd5SPawel Dembicki /* Accept packets again */ 81795711cd5SPawel Dembicki vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ARBITER, 0, 81895711cd5SPawel Dembicki VSC73XX_ARBDISC, BIT(port), 0); 81995711cd5SPawel Dembicki 82095711cd5SPawel Dembicki /* Allow backward dropping of frames from this port */ 82195711cd5SPawel Dembicki vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ARBITER, 0, 82295711cd5SPawel Dembicki VSC73XX_SBACKWDROP, BIT(port), BIT(port)); 82395711cd5SPawel Dembicki 82495711cd5SPawel Dembicki /* Receive mask (disable forwarding) */ 82595711cd5SPawel Dembicki vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ANALYZER, 0, 82695711cd5SPawel Dembicki VSC73XX_RECVMASK, BIT(port), 0); 82795711cd5SPawel Dembicki 82895711cd5SPawel Dembicki return; 82995711cd5SPawel Dembicki } 83095711cd5SPawel Dembicki 83195711cd5SPawel Dembicki /* Figure out what speed was negotiated */ 83295711cd5SPawel Dembicki if (phydev->speed == SPEED_1000) { 83395711cd5SPawel Dembicki dev_dbg(vsc->dev, "port %d: 1000 Mbit mode full duplex\n", 83495711cd5SPawel Dembicki port); 83595711cd5SPawel Dembicki 83695711cd5SPawel Dembicki /* Set up default for internal port or external RGMII */ 83795711cd5SPawel Dembicki if (phydev->interface == PHY_INTERFACE_MODE_RGMII) 83895711cd5SPawel Dembicki val = VSC73XX_MAC_CFG_1000M_F_RGMII; 83995711cd5SPawel Dembicki else 84095711cd5SPawel Dembicki val = VSC73XX_MAC_CFG_1000M_F_PHY; 84195711cd5SPawel Dembicki vsc73xx_adjust_enable_port(vsc, port, phydev, val); 84295711cd5SPawel Dembicki } else if (phydev->speed == SPEED_100) { 84395711cd5SPawel Dembicki if (phydev->duplex == DUPLEX_FULL) { 84495711cd5SPawel Dembicki val = VSC73XX_MAC_CFG_100_10M_F_PHY; 84595711cd5SPawel Dembicki dev_dbg(vsc->dev, 84695711cd5SPawel Dembicki "port %d: 100 Mbit full duplex mode\n", 84795711cd5SPawel Dembicki port); 84895711cd5SPawel Dembicki } else { 84995711cd5SPawel Dembicki val = VSC73XX_MAC_CFG_100_10M_H_PHY; 85095711cd5SPawel Dembicki dev_dbg(vsc->dev, 85195711cd5SPawel Dembicki "port %d: 100 Mbit half duplex mode\n", 85295711cd5SPawel Dembicki port); 85395711cd5SPawel Dembicki } 85495711cd5SPawel Dembicki vsc73xx_adjust_enable_port(vsc, port, phydev, val); 85595711cd5SPawel Dembicki } else if (phydev->speed == SPEED_10) { 85695711cd5SPawel Dembicki if (phydev->duplex == DUPLEX_FULL) { 85795711cd5SPawel Dembicki val = VSC73XX_MAC_CFG_100_10M_F_PHY; 85895711cd5SPawel Dembicki dev_dbg(vsc->dev, 85995711cd5SPawel Dembicki "port %d: 10 Mbit full duplex mode\n", 86095711cd5SPawel Dembicki port); 86195711cd5SPawel Dembicki } else { 86295711cd5SPawel Dembicki val = VSC73XX_MAC_CFG_100_10M_H_PHY; 86395711cd5SPawel Dembicki dev_dbg(vsc->dev, 86495711cd5SPawel Dembicki "port %d: 10 Mbit half duplex mode\n", 86595711cd5SPawel Dembicki port); 86695711cd5SPawel Dembicki } 86795711cd5SPawel Dembicki vsc73xx_adjust_enable_port(vsc, port, phydev, val); 86895711cd5SPawel Dembicki } else { 86995711cd5SPawel Dembicki dev_err(vsc->dev, 87095711cd5SPawel Dembicki "could not adjust link: unknown speed\n"); 87195711cd5SPawel Dembicki } 87295711cd5SPawel Dembicki 87395711cd5SPawel Dembicki /* Enable port (forwarding) in the receieve mask */ 87495711cd5SPawel Dembicki vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ANALYZER, 0, 87595711cd5SPawel Dembicki VSC73XX_RECVMASK, BIT(port), BIT(port)); 87695711cd5SPawel Dembicki } 87795711cd5SPawel Dembicki 87895711cd5SPawel Dembicki static int vsc73xx_port_enable(struct dsa_switch *ds, int port, 87995711cd5SPawel Dembicki struct phy_device *phy) 88095711cd5SPawel Dembicki { 88195711cd5SPawel Dembicki struct vsc73xx *vsc = ds->priv; 88295711cd5SPawel Dembicki 88395711cd5SPawel Dembicki dev_info(vsc->dev, "enable port %d\n", port); 88495711cd5SPawel Dembicki vsc73xx_init_port(vsc, port); 88595711cd5SPawel Dembicki 88695711cd5SPawel Dembicki return 0; 88795711cd5SPawel Dembicki } 88895711cd5SPawel Dembicki 88995711cd5SPawel Dembicki static void vsc73xx_port_disable(struct dsa_switch *ds, int port) 89095711cd5SPawel Dembicki { 89195711cd5SPawel Dembicki struct vsc73xx *vsc = ds->priv; 89295711cd5SPawel Dembicki 89395711cd5SPawel Dembicki /* Just put the port into reset */ 89495711cd5SPawel Dembicki vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port, 89595711cd5SPawel Dembicki VSC73XX_MAC_CFG, VSC73XX_MAC_CFG_RESET); 89695711cd5SPawel Dembicki } 89795711cd5SPawel Dembicki 89895711cd5SPawel Dembicki static const struct vsc73xx_counter * 89995711cd5SPawel Dembicki vsc73xx_find_counter(struct vsc73xx *vsc, 90095711cd5SPawel Dembicki u8 counter, 90195711cd5SPawel Dembicki bool tx) 90295711cd5SPawel Dembicki { 90395711cd5SPawel Dembicki const struct vsc73xx_counter *cnts; 90495711cd5SPawel Dembicki int num_cnts; 90595711cd5SPawel Dembicki int i; 90695711cd5SPawel Dembicki 90795711cd5SPawel Dembicki if (tx) { 90895711cd5SPawel Dembicki cnts = vsc73xx_tx_counters; 90995711cd5SPawel Dembicki num_cnts = ARRAY_SIZE(vsc73xx_tx_counters); 91095711cd5SPawel Dembicki } else { 91195711cd5SPawel Dembicki cnts = vsc73xx_rx_counters; 91295711cd5SPawel Dembicki num_cnts = ARRAY_SIZE(vsc73xx_rx_counters); 91395711cd5SPawel Dembicki } 91495711cd5SPawel Dembicki 91595711cd5SPawel Dembicki for (i = 0; i < num_cnts; i++) { 91695711cd5SPawel Dembicki const struct vsc73xx_counter *cnt; 91795711cd5SPawel Dembicki 91895711cd5SPawel Dembicki cnt = &cnts[i]; 91995711cd5SPawel Dembicki if (cnt->counter == counter) 92095711cd5SPawel Dembicki return cnt; 92195711cd5SPawel Dembicki } 92295711cd5SPawel Dembicki 92395711cd5SPawel Dembicki return NULL; 92495711cd5SPawel Dembicki } 92595711cd5SPawel Dembicki 92695711cd5SPawel Dembicki static void vsc73xx_get_strings(struct dsa_switch *ds, int port, u32 stringset, 92795711cd5SPawel Dembicki uint8_t *data) 92895711cd5SPawel Dembicki { 92995711cd5SPawel Dembicki const struct vsc73xx_counter *cnt; 93095711cd5SPawel Dembicki struct vsc73xx *vsc = ds->priv; 93195711cd5SPawel Dembicki u8 indices[6]; 93295711cd5SPawel Dembicki int i, j; 93395711cd5SPawel Dembicki u32 val; 93495711cd5SPawel Dembicki int ret; 93595711cd5SPawel Dembicki 93695711cd5SPawel Dembicki if (stringset != ETH_SS_STATS) 93795711cd5SPawel Dembicki return; 93895711cd5SPawel Dembicki 93995711cd5SPawel Dembicki ret = vsc73xx_read(vsc, VSC73XX_BLOCK_MAC, port, 94095711cd5SPawel Dembicki VSC73XX_C_CFG, &val); 94195711cd5SPawel Dembicki if (ret) 94295711cd5SPawel Dembicki return; 94395711cd5SPawel Dembicki 94495711cd5SPawel Dembicki indices[0] = (val & 0x1f); /* RX counter 0 */ 94595711cd5SPawel Dembicki indices[1] = ((val >> 5) & 0x1f); /* RX counter 1 */ 94695711cd5SPawel Dembicki indices[2] = ((val >> 10) & 0x1f); /* RX counter 2 */ 94795711cd5SPawel Dembicki indices[3] = ((val >> 16) & 0x1f); /* TX counter 0 */ 94895711cd5SPawel Dembicki indices[4] = ((val >> 21) & 0x1f); /* TX counter 1 */ 94995711cd5SPawel Dembicki indices[5] = ((val >> 26) & 0x1f); /* TX counter 2 */ 95095711cd5SPawel Dembicki 95195711cd5SPawel Dembicki /* The first counters is the RX octets */ 95295711cd5SPawel Dembicki j = 0; 95395711cd5SPawel Dembicki strncpy(data + j * ETH_GSTRING_LEN, 95495711cd5SPawel Dembicki "RxEtherStatsOctets", ETH_GSTRING_LEN); 95595711cd5SPawel Dembicki j++; 95695711cd5SPawel Dembicki 95795711cd5SPawel Dembicki /* Each port supports recording 3 RX counters and 3 TX counters, 95895711cd5SPawel Dembicki * figure out what counters we use in this set-up and return the 95995711cd5SPawel Dembicki * names of them. The hardware default counters will be number of 96095711cd5SPawel Dembicki * packets on RX/TX, combined broadcast+multicast packets RX/TX and 96195711cd5SPawel Dembicki * total error packets RX/TX. 96295711cd5SPawel Dembicki */ 96395711cd5SPawel Dembicki for (i = 0; i < 3; i++) { 96495711cd5SPawel Dembicki cnt = vsc73xx_find_counter(vsc, indices[i], false); 96595711cd5SPawel Dembicki if (cnt) 96695711cd5SPawel Dembicki strncpy(data + j * ETH_GSTRING_LEN, 96795711cd5SPawel Dembicki cnt->name, ETH_GSTRING_LEN); 96895711cd5SPawel Dembicki j++; 96995711cd5SPawel Dembicki } 97095711cd5SPawel Dembicki 97195711cd5SPawel Dembicki /* TX stats begins with the number of TX octets */ 97295711cd5SPawel Dembicki strncpy(data + j * ETH_GSTRING_LEN, 97395711cd5SPawel Dembicki "TxEtherStatsOctets", ETH_GSTRING_LEN); 97495711cd5SPawel Dembicki j++; 97595711cd5SPawel Dembicki 97695711cd5SPawel Dembicki for (i = 3; i < 6; i++) { 97795711cd5SPawel Dembicki cnt = vsc73xx_find_counter(vsc, indices[i], true); 97895711cd5SPawel Dembicki if (cnt) 97995711cd5SPawel Dembicki strncpy(data + j * ETH_GSTRING_LEN, 98095711cd5SPawel Dembicki cnt->name, ETH_GSTRING_LEN); 98195711cd5SPawel Dembicki j++; 98295711cd5SPawel Dembicki } 98395711cd5SPawel Dembicki } 98495711cd5SPawel Dembicki 98595711cd5SPawel Dembicki static int vsc73xx_get_sset_count(struct dsa_switch *ds, int port, int sset) 98695711cd5SPawel Dembicki { 98795711cd5SPawel Dembicki /* We only support SS_STATS */ 98895711cd5SPawel Dembicki if (sset != ETH_SS_STATS) 98995711cd5SPawel Dembicki return 0; 99095711cd5SPawel Dembicki /* RX and TX packets, then 3 RX counters, 3 TX counters */ 99195711cd5SPawel Dembicki return 8; 99295711cd5SPawel Dembicki } 99395711cd5SPawel Dembicki 99495711cd5SPawel Dembicki static void vsc73xx_get_ethtool_stats(struct dsa_switch *ds, int port, 99595711cd5SPawel Dembicki uint64_t *data) 99695711cd5SPawel Dembicki { 99795711cd5SPawel Dembicki struct vsc73xx *vsc = ds->priv; 99895711cd5SPawel Dembicki u8 regs[] = { 99995711cd5SPawel Dembicki VSC73XX_RXOCT, 100095711cd5SPawel Dembicki VSC73XX_C_RX0, 100195711cd5SPawel Dembicki VSC73XX_C_RX1, 100295711cd5SPawel Dembicki VSC73XX_C_RX2, 100395711cd5SPawel Dembicki VSC73XX_TXOCT, 100495711cd5SPawel Dembicki VSC73XX_C_TX0, 100595711cd5SPawel Dembicki VSC73XX_C_TX1, 100695711cd5SPawel Dembicki VSC73XX_C_TX2, 100795711cd5SPawel Dembicki }; 100895711cd5SPawel Dembicki u32 val; 100995711cd5SPawel Dembicki int ret; 101095711cd5SPawel Dembicki int i; 101195711cd5SPawel Dembicki 101295711cd5SPawel Dembicki for (i = 0; i < ARRAY_SIZE(regs); i++) { 101395711cd5SPawel Dembicki ret = vsc73xx_read(vsc, VSC73XX_BLOCK_MAC, port, 101495711cd5SPawel Dembicki regs[i], &val); 101595711cd5SPawel Dembicki if (ret) { 101695711cd5SPawel Dembicki dev_err(vsc->dev, "error reading counter %d\n", i); 101795711cd5SPawel Dembicki return; 101895711cd5SPawel Dembicki } 101995711cd5SPawel Dembicki data[i] = val; 102095711cd5SPawel Dembicki } 102195711cd5SPawel Dembicki } 102295711cd5SPawel Dembicki 1023fb77ffc6SVladimir Oltean static int vsc73xx_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 1024fb77ffc6SVladimir Oltean { 1025fb77ffc6SVladimir Oltean struct vsc73xx *vsc = ds->priv; 1026fb77ffc6SVladimir Oltean 1027fb77ffc6SVladimir Oltean return vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port, 1028fb77ffc6SVladimir Oltean VSC73XX_MAXLEN, new_mtu); 1029fb77ffc6SVladimir Oltean } 1030fb77ffc6SVladimir Oltean 1031fb77ffc6SVladimir Oltean /* According to application not "VSC7398 Jumbo Frames" setting 1032fb77ffc6SVladimir Oltean * up the MTU to 9.6 KB does not affect the performance on standard 1033fb77ffc6SVladimir Oltean * frames. It is clear from the application note that 1034fb77ffc6SVladimir Oltean * "9.6 kilobytes" == 9600 bytes. 1035fb77ffc6SVladimir Oltean */ 1036fb77ffc6SVladimir Oltean static int vsc73xx_get_max_mtu(struct dsa_switch *ds, int port) 1037fb77ffc6SVladimir Oltean { 1038fb77ffc6SVladimir Oltean return 9600; 1039fb77ffc6SVladimir Oltean } 1040fb77ffc6SVladimir Oltean 104195711cd5SPawel Dembicki static const struct dsa_switch_ops vsc73xx_ds_ops = { 104295711cd5SPawel Dembicki .get_tag_protocol = vsc73xx_get_tag_protocol, 104395711cd5SPawel Dembicki .setup = vsc73xx_setup, 104495711cd5SPawel Dembicki .phy_read = vsc73xx_phy_read, 104595711cd5SPawel Dembicki .phy_write = vsc73xx_phy_write, 104695711cd5SPawel Dembicki .adjust_link = vsc73xx_adjust_link, 104795711cd5SPawel Dembicki .get_strings = vsc73xx_get_strings, 104895711cd5SPawel Dembicki .get_ethtool_stats = vsc73xx_get_ethtool_stats, 104995711cd5SPawel Dembicki .get_sset_count = vsc73xx_get_sset_count, 105095711cd5SPawel Dembicki .port_enable = vsc73xx_port_enable, 105195711cd5SPawel Dembicki .port_disable = vsc73xx_port_disable, 1052fb77ffc6SVladimir Oltean .port_change_mtu = vsc73xx_change_mtu, 1053fb77ffc6SVladimir Oltean .port_max_mtu = vsc73xx_get_max_mtu, 105495711cd5SPawel Dembicki }; 105595711cd5SPawel Dembicki 105695711cd5SPawel Dembicki static int vsc73xx_gpio_get(struct gpio_chip *chip, unsigned int offset) 105795711cd5SPawel Dembicki { 105895711cd5SPawel Dembicki struct vsc73xx *vsc = gpiochip_get_data(chip); 105995711cd5SPawel Dembicki u32 val; 106095711cd5SPawel Dembicki int ret; 106195711cd5SPawel Dembicki 106295711cd5SPawel Dembicki ret = vsc73xx_read(vsc, VSC73XX_BLOCK_SYSTEM, 0, 106395711cd5SPawel Dembicki VSC73XX_GPIO, &val); 106495711cd5SPawel Dembicki if (ret) 106595711cd5SPawel Dembicki return ret; 106695711cd5SPawel Dembicki 106795711cd5SPawel Dembicki return !!(val & BIT(offset)); 106895711cd5SPawel Dembicki } 106995711cd5SPawel Dembicki 107095711cd5SPawel Dembicki static void vsc73xx_gpio_set(struct gpio_chip *chip, unsigned int offset, 107195711cd5SPawel Dembicki int val) 107295711cd5SPawel Dembicki { 107395711cd5SPawel Dembicki struct vsc73xx *vsc = gpiochip_get_data(chip); 107495711cd5SPawel Dembicki u32 tmp = val ? BIT(offset) : 0; 107595711cd5SPawel Dembicki 107695711cd5SPawel Dembicki vsc73xx_update_bits(vsc, VSC73XX_BLOCK_SYSTEM, 0, 107795711cd5SPawel Dembicki VSC73XX_GPIO, BIT(offset), tmp); 107895711cd5SPawel Dembicki } 107995711cd5SPawel Dembicki 108095711cd5SPawel Dembicki static int vsc73xx_gpio_direction_output(struct gpio_chip *chip, 108195711cd5SPawel Dembicki unsigned int offset, int val) 108295711cd5SPawel Dembicki { 108395711cd5SPawel Dembicki struct vsc73xx *vsc = gpiochip_get_data(chip); 108495711cd5SPawel Dembicki u32 tmp = val ? BIT(offset) : 0; 108595711cd5SPawel Dembicki 108695711cd5SPawel Dembicki return vsc73xx_update_bits(vsc, VSC73XX_BLOCK_SYSTEM, 0, 108795711cd5SPawel Dembicki VSC73XX_GPIO, BIT(offset + 4) | BIT(offset), 108895711cd5SPawel Dembicki BIT(offset + 4) | tmp); 108995711cd5SPawel Dembicki } 109095711cd5SPawel Dembicki 109195711cd5SPawel Dembicki static int vsc73xx_gpio_direction_input(struct gpio_chip *chip, 109295711cd5SPawel Dembicki unsigned int offset) 109395711cd5SPawel Dembicki { 109495711cd5SPawel Dembicki struct vsc73xx *vsc = gpiochip_get_data(chip); 109595711cd5SPawel Dembicki 109695711cd5SPawel Dembicki return vsc73xx_update_bits(vsc, VSC73XX_BLOCK_SYSTEM, 0, 109795711cd5SPawel Dembicki VSC73XX_GPIO, BIT(offset + 4), 109895711cd5SPawel Dembicki 0); 109995711cd5SPawel Dembicki } 110095711cd5SPawel Dembicki 110195711cd5SPawel Dembicki static int vsc73xx_gpio_get_direction(struct gpio_chip *chip, 110295711cd5SPawel Dembicki unsigned int offset) 110395711cd5SPawel Dembicki { 110495711cd5SPawel Dembicki struct vsc73xx *vsc = gpiochip_get_data(chip); 110595711cd5SPawel Dembicki u32 val; 110695711cd5SPawel Dembicki int ret; 110795711cd5SPawel Dembicki 110895711cd5SPawel Dembicki ret = vsc73xx_read(vsc, VSC73XX_BLOCK_SYSTEM, 0, 110995711cd5SPawel Dembicki VSC73XX_GPIO, &val); 111095711cd5SPawel Dembicki if (ret) 111195711cd5SPawel Dembicki return ret; 111295711cd5SPawel Dembicki 111395711cd5SPawel Dembicki return !(val & BIT(offset + 4)); 111495711cd5SPawel Dembicki } 111595711cd5SPawel Dembicki 111695711cd5SPawel Dembicki static int vsc73xx_gpio_probe(struct vsc73xx *vsc) 111795711cd5SPawel Dembicki { 111895711cd5SPawel Dembicki int ret; 111995711cd5SPawel Dembicki 112095711cd5SPawel Dembicki vsc->gc.label = devm_kasprintf(vsc->dev, GFP_KERNEL, "VSC%04x", 112195711cd5SPawel Dembicki vsc->chipid); 112295711cd5SPawel Dembicki vsc->gc.ngpio = 4; 112395711cd5SPawel Dembicki vsc->gc.owner = THIS_MODULE; 112495711cd5SPawel Dembicki vsc->gc.parent = vsc->dev; 1125aa1d54c6SFlorian Fainelli #if IS_ENABLED(CONFIG_OF_GPIO) 112695711cd5SPawel Dembicki vsc->gc.of_node = vsc->dev->of_node; 1127aa1d54c6SFlorian Fainelli #endif 112895711cd5SPawel Dembicki vsc->gc.base = -1; 112995711cd5SPawel Dembicki vsc->gc.get = vsc73xx_gpio_get; 113095711cd5SPawel Dembicki vsc->gc.set = vsc73xx_gpio_set; 113195711cd5SPawel Dembicki vsc->gc.direction_input = vsc73xx_gpio_direction_input; 113295711cd5SPawel Dembicki vsc->gc.direction_output = vsc73xx_gpio_direction_output; 113395711cd5SPawel Dembicki vsc->gc.get_direction = vsc73xx_gpio_get_direction; 113495711cd5SPawel Dembicki vsc->gc.can_sleep = true; 113595711cd5SPawel Dembicki ret = devm_gpiochip_add_data(vsc->dev, &vsc->gc, vsc); 113695711cd5SPawel Dembicki if (ret) { 113795711cd5SPawel Dembicki dev_err(vsc->dev, "unable to register GPIO chip\n"); 113895711cd5SPawel Dembicki return ret; 113995711cd5SPawel Dembicki } 114095711cd5SPawel Dembicki return 0; 114195711cd5SPawel Dembicki } 114295711cd5SPawel Dembicki 114395711cd5SPawel Dembicki int vsc73xx_probe(struct vsc73xx *vsc) 114495711cd5SPawel Dembicki { 114595711cd5SPawel Dembicki struct device *dev = vsc->dev; 114695711cd5SPawel Dembicki int ret; 114795711cd5SPawel Dembicki 114895711cd5SPawel Dembicki /* Release reset, if any */ 114995711cd5SPawel Dembicki vsc->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); 115095711cd5SPawel Dembicki if (IS_ERR(vsc->reset)) { 115195711cd5SPawel Dembicki dev_err(dev, "failed to get RESET GPIO\n"); 115295711cd5SPawel Dembicki return PTR_ERR(vsc->reset); 115395711cd5SPawel Dembicki } 115495711cd5SPawel Dembicki if (vsc->reset) 115595711cd5SPawel Dembicki /* Wait 20ms according to datasheet table 245 */ 115695711cd5SPawel Dembicki msleep(20); 115795711cd5SPawel Dembicki 115895711cd5SPawel Dembicki ret = vsc73xx_detect(vsc); 11591da39ff0SPawel Dembicki if (ret == -EAGAIN) { 11601da39ff0SPawel Dembicki dev_err(vsc->dev, 11611da39ff0SPawel Dembicki "Chip seems to be out of control. Assert reset and try again.\n"); 11621da39ff0SPawel Dembicki gpiod_set_value_cansleep(vsc->reset, 1); 11631da39ff0SPawel Dembicki /* Reset pulse should be 20ns minimum, according to datasheet 11641da39ff0SPawel Dembicki * table 245, so 10us should be fine 11651da39ff0SPawel Dembicki */ 11661da39ff0SPawel Dembicki usleep_range(10, 100); 11671da39ff0SPawel Dembicki gpiod_set_value_cansleep(vsc->reset, 0); 11681da39ff0SPawel Dembicki /* Wait 20ms according to datasheet table 245 */ 11691da39ff0SPawel Dembicki msleep(20); 11701da39ff0SPawel Dembicki ret = vsc73xx_detect(vsc); 11711da39ff0SPawel Dembicki } 117295711cd5SPawel Dembicki if (ret) { 117395711cd5SPawel Dembicki dev_err(dev, "no chip found (%d)\n", ret); 117495711cd5SPawel Dembicki return -ENODEV; 117595711cd5SPawel Dembicki } 117695711cd5SPawel Dembicki 117795711cd5SPawel Dembicki eth_random_addr(vsc->addr); 117895711cd5SPawel Dembicki dev_info(vsc->dev, 117995711cd5SPawel Dembicki "MAC for control frames: %02X:%02X:%02X:%02X:%02X:%02X\n", 118095711cd5SPawel Dembicki vsc->addr[0], vsc->addr[1], vsc->addr[2], 118195711cd5SPawel Dembicki vsc->addr[3], vsc->addr[4], vsc->addr[5]); 118295711cd5SPawel Dembicki 118395711cd5SPawel Dembicki /* The VSC7395 switch chips have 5+1 ports which means 5 118495711cd5SPawel Dembicki * ordinary ports and a sixth CPU port facing the processor 118595711cd5SPawel Dembicki * with an RGMII interface. These ports are numbered 0..4 118695711cd5SPawel Dembicki * and 6, so they leave a "hole" in the port map for port 5, 118795711cd5SPawel Dembicki * which is invalid. 118895711cd5SPawel Dembicki * 118995711cd5SPawel Dembicki * The VSC7398 has 8 ports, port 7 is again the CPU port. 119095711cd5SPawel Dembicki * 119195711cd5SPawel Dembicki * We allocate 8 ports and avoid access to the nonexistant 119295711cd5SPawel Dembicki * ports. 119395711cd5SPawel Dembicki */ 11947e99e347SVivien Didelot vsc->ds = devm_kzalloc(dev, sizeof(*vsc->ds), GFP_KERNEL); 119595711cd5SPawel Dembicki if (!vsc->ds) 119695711cd5SPawel Dembicki return -ENOMEM; 11977e99e347SVivien Didelot 11987e99e347SVivien Didelot vsc->ds->dev = dev; 11997e99e347SVivien Didelot vsc->ds->num_ports = 8; 120095711cd5SPawel Dembicki vsc->ds->priv = vsc; 120195711cd5SPawel Dembicki 120295711cd5SPawel Dembicki vsc->ds->ops = &vsc73xx_ds_ops; 120395711cd5SPawel Dembicki ret = dsa_register_switch(vsc->ds); 120495711cd5SPawel Dembicki if (ret) { 120595711cd5SPawel Dembicki dev_err(dev, "unable to register switch (%d)\n", ret); 120695711cd5SPawel Dembicki return ret; 120795711cd5SPawel Dembicki } 120895711cd5SPawel Dembicki 120995711cd5SPawel Dembicki ret = vsc73xx_gpio_probe(vsc); 121095711cd5SPawel Dembicki if (ret) { 121195711cd5SPawel Dembicki dsa_unregister_switch(vsc->ds); 121295711cd5SPawel Dembicki return ret; 121395711cd5SPawel Dembicki } 121495711cd5SPawel Dembicki 121595711cd5SPawel Dembicki return 0; 121695711cd5SPawel Dembicki } 121795711cd5SPawel Dembicki EXPORT_SYMBOL(vsc73xx_probe); 121895711cd5SPawel Dembicki 1219*e99fa423SUwe Kleine-König void vsc73xx_remove(struct vsc73xx *vsc) 122095711cd5SPawel Dembicki { 122195711cd5SPawel Dembicki dsa_unregister_switch(vsc->ds); 122295711cd5SPawel Dembicki gpiod_set_value(vsc->reset, 1); 122395711cd5SPawel Dembicki } 122495711cd5SPawel Dembicki EXPORT_SYMBOL(vsc73xx_remove); 122595711cd5SPawel Dembicki 12260650bf52SVladimir Oltean void vsc73xx_shutdown(struct vsc73xx *vsc) 12270650bf52SVladimir Oltean { 12280650bf52SVladimir Oltean dsa_switch_shutdown(vsc->ds); 12290650bf52SVladimir Oltean } 12300650bf52SVladimir Oltean EXPORT_SYMBOL(vsc73xx_shutdown); 12310650bf52SVladimir Oltean 123295711cd5SPawel Dembicki MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>"); 123395711cd5SPawel Dembicki MODULE_DESCRIPTION("Vitesse VSC7385/7388/7395/7398 driver"); 123495711cd5SPawel Dembicki MODULE_LICENSE("GPL v2"); 1235