xref: /linux/drivers/net/dsa/vitesse-vsc73xx-core.c (revision 6cc5280a08899e704336446c300af2ca80da9e5f)
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>
20eb7e33d0SPawel Dembicki #include <linux/iopoll.h>
2195711cd5SPawel Dembicki #include <linux/of.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 
272eb7e33d0SPawel Dembicki #define VSC73XX_POLL_SLEEP_US		1000
273eb7e33d0SPawel Dembicki #define VSC73XX_POLL_TIMEOUT_US		10000
274eb7e33d0SPawel Dembicki 
27595711cd5SPawel Dembicki struct vsc73xx_counter {
27695711cd5SPawel Dembicki 	u8 counter;
27795711cd5SPawel Dembicki 	const char *name;
27895711cd5SPawel Dembicki };
27995711cd5SPawel Dembicki 
28095711cd5SPawel Dembicki /* Counters are named according to the MIB standards where applicable.
28195711cd5SPawel Dembicki  * Some counters are custom, non-standard. The standard counters are
28295711cd5SPawel Dembicki  * named in accordance with RFC2819, RFC2021 and IEEE Std 802.3-2002 Annex
28395711cd5SPawel Dembicki  * 30A Counters.
28495711cd5SPawel Dembicki  */
28595711cd5SPawel Dembicki static const struct vsc73xx_counter vsc73xx_rx_counters[] = {
28695711cd5SPawel Dembicki 	{ 0, "RxEtherStatsPkts" },
28795711cd5SPawel Dembicki 	{ 1, "RxBroadcast+MulticastPkts" }, /* non-standard counter */
28895711cd5SPawel Dembicki 	{ 2, "RxTotalErrorPackets" }, /* non-standard counter */
28995711cd5SPawel Dembicki 	{ 3, "RxEtherStatsBroadcastPkts" },
29095711cd5SPawel Dembicki 	{ 4, "RxEtherStatsMulticastPkts" },
29195711cd5SPawel Dembicki 	{ 5, "RxEtherStatsPkts64Octets" },
29295711cd5SPawel Dembicki 	{ 6, "RxEtherStatsPkts65to127Octets" },
29395711cd5SPawel Dembicki 	{ 7, "RxEtherStatsPkts128to255Octets" },
29495711cd5SPawel Dembicki 	{ 8, "RxEtherStatsPkts256to511Octets" },
29595711cd5SPawel Dembicki 	{ 9, "RxEtherStatsPkts512to1023Octets" },
29695711cd5SPawel Dembicki 	{ 10, "RxEtherStatsPkts1024to1518Octets" },
29795711cd5SPawel Dembicki 	{ 11, "RxJumboFrames" }, /* non-standard counter */
29895711cd5SPawel Dembicki 	{ 12, "RxaPauseMACControlFramesTransmitted" },
29995711cd5SPawel Dembicki 	{ 13, "RxFIFODrops" }, /* non-standard counter */
30095711cd5SPawel Dembicki 	{ 14, "RxBackwardDrops" }, /* non-standard counter */
30195711cd5SPawel Dembicki 	{ 15, "RxClassifierDrops" }, /* non-standard counter */
30295711cd5SPawel Dembicki 	{ 16, "RxEtherStatsCRCAlignErrors" },
30395711cd5SPawel Dembicki 	{ 17, "RxEtherStatsUndersizePkts" },
30495711cd5SPawel Dembicki 	{ 18, "RxEtherStatsOversizePkts" },
30595711cd5SPawel Dembicki 	{ 19, "RxEtherStatsFragments" },
30695711cd5SPawel Dembicki 	{ 20, "RxEtherStatsJabbers" },
30795711cd5SPawel Dembicki 	{ 21, "RxaMACControlFramesReceived" },
30895711cd5SPawel Dembicki 	/* 22-24 are undefined */
30995711cd5SPawel Dembicki 	{ 25, "RxaFramesReceivedOK" },
31095711cd5SPawel Dembicki 	{ 26, "RxQoSClass0" }, /* non-standard counter */
31195711cd5SPawel Dembicki 	{ 27, "RxQoSClass1" }, /* non-standard counter */
31295711cd5SPawel Dembicki 	{ 28, "RxQoSClass2" }, /* non-standard counter */
31395711cd5SPawel Dembicki 	{ 29, "RxQoSClass3" }, /* non-standard counter */
31495711cd5SPawel Dembicki };
31595711cd5SPawel Dembicki 
31695711cd5SPawel Dembicki static const struct vsc73xx_counter vsc73xx_tx_counters[] = {
31795711cd5SPawel Dembicki 	{ 0, "TxEtherStatsPkts" },
31895711cd5SPawel Dembicki 	{ 1, "TxBroadcast+MulticastPkts" }, /* non-standard counter */
31995711cd5SPawel Dembicki 	{ 2, "TxTotalErrorPackets" }, /* non-standard counter */
32095711cd5SPawel Dembicki 	{ 3, "TxEtherStatsBroadcastPkts" },
32195711cd5SPawel Dembicki 	{ 4, "TxEtherStatsMulticastPkts" },
32295711cd5SPawel Dembicki 	{ 5, "TxEtherStatsPkts64Octets" },
32395711cd5SPawel Dembicki 	{ 6, "TxEtherStatsPkts65to127Octets" },
32495711cd5SPawel Dembicki 	{ 7, "TxEtherStatsPkts128to255Octets" },
32595711cd5SPawel Dembicki 	{ 8, "TxEtherStatsPkts256to511Octets" },
32695711cd5SPawel Dembicki 	{ 9, "TxEtherStatsPkts512to1023Octets" },
32795711cd5SPawel Dembicki 	{ 10, "TxEtherStatsPkts1024to1518Octets" },
32895711cd5SPawel Dembicki 	{ 11, "TxJumboFrames" }, /* non-standard counter */
32995711cd5SPawel Dembicki 	{ 12, "TxaPauseMACControlFramesTransmitted" },
33095711cd5SPawel Dembicki 	{ 13, "TxFIFODrops" }, /* non-standard counter */
33195711cd5SPawel Dembicki 	{ 14, "TxDrops" }, /* non-standard counter */
33295711cd5SPawel Dembicki 	{ 15, "TxEtherStatsCollisions" },
33395711cd5SPawel Dembicki 	{ 16, "TxEtherStatsCRCAlignErrors" },
33495711cd5SPawel Dembicki 	{ 17, "TxEtherStatsUndersizePkts" },
33595711cd5SPawel Dembicki 	{ 18, "TxEtherStatsOversizePkts" },
33695711cd5SPawel Dembicki 	{ 19, "TxEtherStatsFragments" },
33795711cd5SPawel Dembicki 	{ 20, "TxEtherStatsJabbers" },
33895711cd5SPawel Dembicki 	/* 21-24 are undefined */
33995711cd5SPawel Dembicki 	{ 25, "TxaFramesReceivedOK" },
34095711cd5SPawel Dembicki 	{ 26, "TxQoSClass0" }, /* non-standard counter */
34195711cd5SPawel Dembicki 	{ 27, "TxQoSClass1" }, /* non-standard counter */
34295711cd5SPawel Dembicki 	{ 28, "TxQoSClass2" }, /* non-standard counter */
34395711cd5SPawel Dembicki 	{ 29, "TxQoSClass3" }, /* non-standard counter */
34495711cd5SPawel Dembicki };
34595711cd5SPawel Dembicki 
34695711cd5SPawel Dembicki int vsc73xx_is_addr_valid(u8 block, u8 subblock)
34795711cd5SPawel Dembicki {
34895711cd5SPawel Dembicki 	switch (block) {
34995711cd5SPawel Dembicki 	case VSC73XX_BLOCK_MAC:
35095711cd5SPawel Dembicki 		switch (subblock) {
35195711cd5SPawel Dembicki 		case 0 ... 4:
35295711cd5SPawel Dembicki 		case 6:
35395711cd5SPawel Dembicki 			return 1;
35495711cd5SPawel Dembicki 		}
35595711cd5SPawel Dembicki 		break;
35695711cd5SPawel Dembicki 
35795711cd5SPawel Dembicki 	case VSC73XX_BLOCK_ANALYZER:
35895711cd5SPawel Dembicki 	case VSC73XX_BLOCK_SYSTEM:
35995711cd5SPawel Dembicki 		switch (subblock) {
36095711cd5SPawel Dembicki 		case 0:
36195711cd5SPawel Dembicki 			return 1;
36295711cd5SPawel Dembicki 		}
36395711cd5SPawel Dembicki 		break;
36495711cd5SPawel Dembicki 
36595711cd5SPawel Dembicki 	case VSC73XX_BLOCK_MII:
36695711cd5SPawel Dembicki 	case VSC73XX_BLOCK_CAPTURE:
36795711cd5SPawel Dembicki 	case VSC73XX_BLOCK_ARBITER:
36895711cd5SPawel Dembicki 		switch (subblock) {
36995711cd5SPawel Dembicki 		case 0 ... 1:
37095711cd5SPawel Dembicki 			return 1;
37195711cd5SPawel Dembicki 		}
37295711cd5SPawel Dembicki 		break;
37395711cd5SPawel Dembicki 	}
37495711cd5SPawel Dembicki 
37595711cd5SPawel Dembicki 	return 0;
37695711cd5SPawel Dembicki }
37795711cd5SPawel Dembicki EXPORT_SYMBOL(vsc73xx_is_addr_valid);
37895711cd5SPawel Dembicki 
37995711cd5SPawel Dembicki static int vsc73xx_read(struct vsc73xx *vsc, u8 block, u8 subblock, u8 reg,
38095711cd5SPawel Dembicki 			u32 *val)
38195711cd5SPawel Dembicki {
38295711cd5SPawel Dembicki 	return vsc->ops->read(vsc, block, subblock, reg, val);
38395711cd5SPawel Dembicki }
38495711cd5SPawel Dembicki 
38595711cd5SPawel Dembicki static int vsc73xx_write(struct vsc73xx *vsc, u8 block, u8 subblock, u8 reg,
38695711cd5SPawel Dembicki 			 u32 val)
38795711cd5SPawel Dembicki {
38895711cd5SPawel Dembicki 	return vsc->ops->write(vsc, block, subblock, reg, val);
38995711cd5SPawel Dembicki }
39095711cd5SPawel Dembicki 
39195711cd5SPawel Dembicki static int vsc73xx_update_bits(struct vsc73xx *vsc, u8 block, u8 subblock,
39295711cd5SPawel Dembicki 			       u8 reg, u32 mask, u32 val)
39395711cd5SPawel Dembicki {
39495711cd5SPawel Dembicki 	u32 tmp, orig;
39595711cd5SPawel Dembicki 	int ret;
39695711cd5SPawel Dembicki 
39795711cd5SPawel Dembicki 	/* Same read-modify-write algorithm as e.g. regmap */
39895711cd5SPawel Dembicki 	ret = vsc73xx_read(vsc, block, subblock, reg, &orig);
39995711cd5SPawel Dembicki 	if (ret)
40095711cd5SPawel Dembicki 		return ret;
40195711cd5SPawel Dembicki 	tmp = orig & ~mask;
40295711cd5SPawel Dembicki 	tmp |= val & mask;
40395711cd5SPawel Dembicki 	return vsc73xx_write(vsc, block, subblock, reg, tmp);
40495711cd5SPawel Dembicki }
40595711cd5SPawel Dembicki 
40695711cd5SPawel Dembicki static int vsc73xx_detect(struct vsc73xx *vsc)
40795711cd5SPawel Dembicki {
40895711cd5SPawel Dembicki 	bool icpu_si_boot_en;
40995711cd5SPawel Dembicki 	bool icpu_pi_en;
41095711cd5SPawel Dembicki 	u32 val;
41195711cd5SPawel Dembicki 	u32 rev;
41295711cd5SPawel Dembicki 	int ret;
41395711cd5SPawel Dembicki 	u32 id;
41495711cd5SPawel Dembicki 
41595711cd5SPawel Dembicki 	ret = vsc73xx_read(vsc, VSC73XX_BLOCK_SYSTEM, 0,
41695711cd5SPawel Dembicki 			   VSC73XX_ICPU_MBOX_VAL, &val);
41795711cd5SPawel Dembicki 	if (ret) {
41895711cd5SPawel Dembicki 		dev_err(vsc->dev, "unable to read mailbox (%d)\n", ret);
41995711cd5SPawel Dembicki 		return ret;
42095711cd5SPawel Dembicki 	}
42195711cd5SPawel Dembicki 
42295711cd5SPawel Dembicki 	if (val == 0xffffffff) {
4231da39ff0SPawel Dembicki 		dev_info(vsc->dev, "chip seems dead.\n");
4241da39ff0SPawel Dembicki 		return -EAGAIN;
42595711cd5SPawel Dembicki 	}
42695711cd5SPawel Dembicki 
42795711cd5SPawel Dembicki 	ret = vsc73xx_read(vsc, VSC73XX_BLOCK_SYSTEM, 0,
42895711cd5SPawel Dembicki 			   VSC73XX_CHIPID, &val);
42995711cd5SPawel Dembicki 	if (ret) {
43095711cd5SPawel Dembicki 		dev_err(vsc->dev, "unable to read chip id (%d)\n", ret);
43195711cd5SPawel Dembicki 		return ret;
43295711cd5SPawel Dembicki 	}
43395711cd5SPawel Dembicki 
43495711cd5SPawel Dembicki 	id = (val >> VSC73XX_CHIPID_ID_SHIFT) &
43595711cd5SPawel Dembicki 		VSC73XX_CHIPID_ID_MASK;
43695711cd5SPawel Dembicki 	switch (id) {
43795711cd5SPawel Dembicki 	case VSC73XX_CHIPID_ID_7385:
43895711cd5SPawel Dembicki 	case VSC73XX_CHIPID_ID_7388:
43995711cd5SPawel Dembicki 	case VSC73XX_CHIPID_ID_7395:
44095711cd5SPawel Dembicki 	case VSC73XX_CHIPID_ID_7398:
44195711cd5SPawel Dembicki 		break;
44295711cd5SPawel Dembicki 	default:
44395711cd5SPawel Dembicki 		dev_err(vsc->dev, "unsupported chip, id=%04x\n", id);
44495711cd5SPawel Dembicki 		return -ENODEV;
44595711cd5SPawel Dembicki 	}
44695711cd5SPawel Dembicki 
44795711cd5SPawel Dembicki 	vsc->chipid = id;
44895711cd5SPawel Dembicki 	rev = (val >> VSC73XX_CHIPID_REV_SHIFT) &
44995711cd5SPawel Dembicki 		VSC73XX_CHIPID_REV_MASK;
45095711cd5SPawel Dembicki 	dev_info(vsc->dev, "VSC%04X (rev: %d) switch found\n", id, rev);
45195711cd5SPawel Dembicki 
45295711cd5SPawel Dembicki 	ret = vsc73xx_read(vsc, VSC73XX_BLOCK_SYSTEM, 0,
45395711cd5SPawel Dembicki 			   VSC73XX_ICPU_CTRL, &val);
45495711cd5SPawel Dembicki 	if (ret) {
45595711cd5SPawel Dembicki 		dev_err(vsc->dev, "unable to read iCPU control\n");
45695711cd5SPawel Dembicki 		return ret;
45795711cd5SPawel Dembicki 	}
45895711cd5SPawel Dembicki 
45995711cd5SPawel Dembicki 	/* The iCPU can always be used but can boot in different ways.
46095711cd5SPawel Dembicki 	 * If it is initially disabled and has no external memory,
46195711cd5SPawel Dembicki 	 * we are in control and can do whatever we like, else we
46295711cd5SPawel Dembicki 	 * are probably in trouble (we need some way to communicate
46395711cd5SPawel Dembicki 	 * with the running firmware) so we bail out for now.
46495711cd5SPawel Dembicki 	 */
46595711cd5SPawel Dembicki 	icpu_pi_en = !!(val & VSC73XX_ICPU_CTRL_ICPU_PI_EN);
46695711cd5SPawel Dembicki 	icpu_si_boot_en = !!(val & VSC73XX_ICPU_CTRL_BOOT_EN);
46795711cd5SPawel Dembicki 	if (icpu_si_boot_en && icpu_pi_en) {
46895711cd5SPawel Dembicki 		dev_err(vsc->dev,
46995711cd5SPawel Dembicki 			"iCPU enabled boots from SI, has external memory\n");
47095711cd5SPawel Dembicki 		dev_err(vsc->dev, "no idea how to deal with this\n");
47195711cd5SPawel Dembicki 		return -ENODEV;
47295711cd5SPawel Dembicki 	}
47395711cd5SPawel Dembicki 	if (icpu_si_boot_en && !icpu_pi_en) {
47495711cd5SPawel Dembicki 		dev_err(vsc->dev,
4751da39ff0SPawel Dembicki 			"iCPU enabled boots from PI/SI, no external memory\n");
4761da39ff0SPawel Dembicki 		return -EAGAIN;
47795711cd5SPawel Dembicki 	}
47895711cd5SPawel Dembicki 	if (!icpu_si_boot_en && icpu_pi_en) {
47995711cd5SPawel Dembicki 		dev_err(vsc->dev,
48095711cd5SPawel Dembicki 			"iCPU enabled, boots from PI external memory\n");
48195711cd5SPawel Dembicki 		dev_err(vsc->dev, "no idea how to deal with this\n");
48295711cd5SPawel Dembicki 		return -ENODEV;
48395711cd5SPawel Dembicki 	}
48495711cd5SPawel Dembicki 	/* !icpu_si_boot_en && !cpu_pi_en */
48595711cd5SPawel Dembicki 	dev_info(vsc->dev, "iCPU disabled, no external memory\n");
48695711cd5SPawel Dembicki 
48795711cd5SPawel Dembicki 	return 0;
48895711cd5SPawel Dembicki }
48995711cd5SPawel Dembicki 
49095711cd5SPawel Dembicki static int vsc73xx_phy_read(struct dsa_switch *ds, int phy, int regnum)
49195711cd5SPawel Dembicki {
49295711cd5SPawel Dembicki 	struct vsc73xx *vsc = ds->priv;
49395711cd5SPawel Dembicki 	u32 cmd;
49495711cd5SPawel Dembicki 	u32 val;
49595711cd5SPawel Dembicki 	int ret;
49695711cd5SPawel Dembicki 
49795711cd5SPawel Dembicki 	/* Setting bit 26 means "read" */
49895711cd5SPawel Dembicki 	cmd = BIT(26) | (phy << 21) | (regnum << 16);
49995711cd5SPawel Dembicki 	ret = vsc73xx_write(vsc, VSC73XX_BLOCK_MII, 0, 1, cmd);
50095711cd5SPawel Dembicki 	if (ret)
50195711cd5SPawel Dembicki 		return ret;
50295711cd5SPawel Dembicki 	msleep(2);
50395711cd5SPawel Dembicki 	ret = vsc73xx_read(vsc, VSC73XX_BLOCK_MII, 0, 2, &val);
50495711cd5SPawel Dembicki 	if (ret)
50595711cd5SPawel Dembicki 		return ret;
50695711cd5SPawel Dembicki 	if (val & BIT(16)) {
50795711cd5SPawel Dembicki 		dev_err(vsc->dev, "reading reg %02x from phy%d failed\n",
50895711cd5SPawel Dembicki 			regnum, phy);
50995711cd5SPawel Dembicki 		return -EIO;
51095711cd5SPawel Dembicki 	}
51195711cd5SPawel Dembicki 	val &= 0xFFFFU;
51295711cd5SPawel Dembicki 
51395711cd5SPawel Dembicki 	dev_dbg(vsc->dev, "read reg %02x from phy%d = %04x\n",
51495711cd5SPawel Dembicki 		regnum, phy, val);
51595711cd5SPawel Dembicki 
51695711cd5SPawel Dembicki 	return val;
51795711cd5SPawel Dembicki }
51895711cd5SPawel Dembicki 
51995711cd5SPawel Dembicki static int vsc73xx_phy_write(struct dsa_switch *ds, int phy, int regnum,
52095711cd5SPawel Dembicki 			     u16 val)
52195711cd5SPawel Dembicki {
52295711cd5SPawel Dembicki 	struct vsc73xx *vsc = ds->priv;
52395711cd5SPawel Dembicki 	u32 cmd;
52495711cd5SPawel Dembicki 	int ret;
52595711cd5SPawel Dembicki 
52695711cd5SPawel Dembicki 	/* It was found through tedious experiments that this router
52795711cd5SPawel Dembicki 	 * chip really hates to have it's PHYs reset. They
52895711cd5SPawel Dembicki 	 * never recover if that happens: autonegotiation stops
52995711cd5SPawel Dembicki 	 * working after a reset. Just filter out this command.
53095711cd5SPawel Dembicki 	 * (Resetting the whole chip is OK.)
53195711cd5SPawel Dembicki 	 */
53295711cd5SPawel Dembicki 	if (regnum == 0 && (val & BIT(15))) {
53395711cd5SPawel Dembicki 		dev_info(vsc->dev, "reset PHY - disallowed\n");
53495711cd5SPawel Dembicki 		return 0;
53595711cd5SPawel Dembicki 	}
53695711cd5SPawel Dembicki 
53795711cd5SPawel Dembicki 	cmd = (phy << 21) | (regnum << 16);
53895711cd5SPawel Dembicki 	ret = vsc73xx_write(vsc, VSC73XX_BLOCK_MII, 0, 1, cmd);
53995711cd5SPawel Dembicki 	if (ret)
54095711cd5SPawel Dembicki 		return ret;
54195711cd5SPawel Dembicki 
54295711cd5SPawel Dembicki 	dev_dbg(vsc->dev, "write %04x to reg %02x in phy%d\n",
54395711cd5SPawel Dembicki 		val, regnum, phy);
54495711cd5SPawel Dembicki 	return 0;
54595711cd5SPawel Dembicki }
54695711cd5SPawel Dembicki 
54795711cd5SPawel Dembicki static enum dsa_tag_protocol vsc73xx_get_tag_protocol(struct dsa_switch *ds,
5484d776482SFlorian Fainelli 						      int port,
5494d776482SFlorian Fainelli 						      enum dsa_tag_protocol mp)
55095711cd5SPawel Dembicki {
55195711cd5SPawel Dembicki 	/* The switch internally uses a 8 byte header with length,
55295711cd5SPawel Dembicki 	 * source port, tag, LPA and priority. This is supposedly
55395711cd5SPawel Dembicki 	 * only accessible when operating the switch using the internal
55495711cd5SPawel Dembicki 	 * CPU or with an external CPU mapping the device in, but not
55595711cd5SPawel Dembicki 	 * when operating the switch over SPI and putting frames in/out
55695711cd5SPawel Dembicki 	 * on port 6 (the CPU port). So far we must assume that we
55795711cd5SPawel Dembicki 	 * cannot access the tag. (See "Internal frame header" section
55895711cd5SPawel Dembicki 	 * 3.9.1 in the manual.)
55995711cd5SPawel Dembicki 	 */
56095711cd5SPawel Dembicki 	return DSA_TAG_PROTO_NONE;
56195711cd5SPawel Dembicki }
56295711cd5SPawel Dembicki 
56395711cd5SPawel Dembicki static int vsc73xx_setup(struct dsa_switch *ds)
56495711cd5SPawel Dembicki {
56595711cd5SPawel Dembicki 	struct vsc73xx *vsc = ds->priv;
56695711cd5SPawel Dembicki 	int i;
56795711cd5SPawel Dembicki 
56895711cd5SPawel Dembicki 	dev_info(vsc->dev, "set up the switch\n");
56995711cd5SPawel Dembicki 
57095711cd5SPawel Dembicki 	/* Issue RESET */
57195711cd5SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_SYSTEM, 0, VSC73XX_GLORESET,
57295711cd5SPawel Dembicki 		      VSC73XX_GLORESET_MASTER_RESET);
57395711cd5SPawel Dembicki 	usleep_range(125, 200);
57495711cd5SPawel Dembicki 
57595711cd5SPawel Dembicki 	/* Initialize memory, initialize RAM bank 0..15 except 6 and 7
57695711cd5SPawel Dembicki 	 * This sequence appears in the
57795711cd5SPawel Dembicki 	 * VSC7385 SparX-G5 datasheet section 6.6.1
57895711cd5SPawel Dembicki 	 * VSC7395 SparX-G5e datasheet section 6.6.1
57995711cd5SPawel Dembicki 	 * "initialization sequence".
58095711cd5SPawel Dembicki 	 * No explanation is given to the 0x1010400 magic number.
58195711cd5SPawel Dembicki 	 */
58295711cd5SPawel Dembicki 	for (i = 0; i <= 15; i++) {
58395711cd5SPawel Dembicki 		if (i != 6 && i != 7) {
58495711cd5SPawel Dembicki 			vsc73xx_write(vsc, VSC73XX_BLOCK_MEMINIT,
58595711cd5SPawel Dembicki 				      2,
58695711cd5SPawel Dembicki 				      0, 0x1010400 + i);
58795711cd5SPawel Dembicki 			mdelay(1);
58895711cd5SPawel Dembicki 		}
58995711cd5SPawel Dembicki 	}
59095711cd5SPawel Dembicki 	mdelay(30);
59195711cd5SPawel Dembicki 
59295711cd5SPawel Dembicki 	/* Clear MAC table */
59395711cd5SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_ANALYZER, 0,
59495711cd5SPawel Dembicki 		      VSC73XX_MACACCESS,
59595711cd5SPawel Dembicki 		      VSC73XX_MACACCESS_CMD_CLEAR_TABLE);
59695711cd5SPawel Dembicki 
59795711cd5SPawel Dembicki 	/* Clear VLAN table */
59895711cd5SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_ANALYZER, 0,
59995711cd5SPawel Dembicki 		      VSC73XX_VLANACCESS,
60095711cd5SPawel Dembicki 		      VSC73XX_VLANACCESS_VLAN_TBL_CMD_CLEAR_TABLE);
60195711cd5SPawel Dembicki 
60295711cd5SPawel Dembicki 	msleep(40);
60395711cd5SPawel Dembicki 
60495711cd5SPawel Dembicki 	/* Use 20KiB buffers on all ports on VSC7395
60595711cd5SPawel Dembicki 	 * The VSC7385 has 16KiB buffers and that is the
60695711cd5SPawel Dembicki 	 * default if we don't set this up explicitly.
60795711cd5SPawel Dembicki 	 * Port "31" is "all ports".
60895711cd5SPawel Dembicki 	 */
60995711cd5SPawel Dembicki 	if (IS_739X(vsc))
61095711cd5SPawel Dembicki 		vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 0x1f,
61195711cd5SPawel Dembicki 			      VSC73XX_Q_MISC_CONF,
61295711cd5SPawel Dembicki 			      VSC73XX_Q_MISC_CONF_EXTENT_MEM);
61395711cd5SPawel Dembicki 
61495711cd5SPawel Dembicki 	/* Put all ports into reset until enabled */
61595711cd5SPawel Dembicki 	for (i = 0; i < 7; i++) {
61695711cd5SPawel Dembicki 		if (i == 5)
61795711cd5SPawel Dembicki 			continue;
61895711cd5SPawel Dembicki 		vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, 4,
61995711cd5SPawel Dembicki 			      VSC73XX_MAC_CFG, VSC73XX_MAC_CFG_RESET);
62095711cd5SPawel Dembicki 	}
62195711cd5SPawel Dembicki 
62295711cd5SPawel Dembicki 	/* MII delay, set both GTX and RX delay to 2 ns */
62395711cd5SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_SYSTEM, 0, VSC73XX_GMIIDELAY,
62495711cd5SPawel Dembicki 		      VSC73XX_GMIIDELAY_GMII0_GTXDELAY_2_0_NS |
62595711cd5SPawel Dembicki 		      VSC73XX_GMIIDELAY_GMII0_RXDELAY_2_0_NS);
62695711cd5SPawel Dembicki 	/* Enable reception of frames on all ports */
62795711cd5SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_ANALYZER, 0, VSC73XX_RECVMASK,
62895711cd5SPawel Dembicki 		      0x5f);
62995711cd5SPawel Dembicki 	/* IP multicast flood mask (table 144) */
63095711cd5SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_ANALYZER, 0, VSC73XX_IFLODMSK,
63195711cd5SPawel Dembicki 		      0xff);
63295711cd5SPawel Dembicki 
63395711cd5SPawel Dembicki 	mdelay(50);
63495711cd5SPawel Dembicki 
63595711cd5SPawel Dembicki 	/* Release reset from the internal PHYs */
63695711cd5SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_SYSTEM, 0, VSC73XX_GLORESET,
63795711cd5SPawel Dembicki 		      VSC73XX_GLORESET_PHY_RESET);
63895711cd5SPawel Dembicki 
63995711cd5SPawel Dembicki 	udelay(4);
64095711cd5SPawel Dembicki 
64195711cd5SPawel Dembicki 	return 0;
64295711cd5SPawel Dembicki }
64395711cd5SPawel Dembicki 
64495711cd5SPawel Dembicki static void vsc73xx_init_port(struct vsc73xx *vsc, int port)
64595711cd5SPawel Dembicki {
64695711cd5SPawel Dembicki 	u32 val;
64795711cd5SPawel Dembicki 
64895711cd5SPawel Dembicki 	/* MAC configure, first reset the port and then write defaults */
64995711cd5SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_MAC,
65095711cd5SPawel Dembicki 		      port,
65195711cd5SPawel Dembicki 		      VSC73XX_MAC_CFG,
65295711cd5SPawel Dembicki 		      VSC73XX_MAC_CFG_RESET);
65395711cd5SPawel Dembicki 
65495711cd5SPawel Dembicki 	/* Take up the port in 1Gbit mode by default, this will be
65595711cd5SPawel Dembicki 	 * augmented after auto-negotiation on the PHY-facing
65695711cd5SPawel Dembicki 	 * ports.
65795711cd5SPawel Dembicki 	 */
65895711cd5SPawel Dembicki 	if (port == CPU_PORT)
65995711cd5SPawel Dembicki 		val = VSC73XX_MAC_CFG_1000M_F_RGMII;
66095711cd5SPawel Dembicki 	else
66195711cd5SPawel Dembicki 		val = VSC73XX_MAC_CFG_1000M_F_PHY;
66295711cd5SPawel Dembicki 
66395711cd5SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_MAC,
66495711cd5SPawel Dembicki 		      port,
66595711cd5SPawel Dembicki 		      VSC73XX_MAC_CFG,
66695711cd5SPawel Dembicki 		      val |
66795711cd5SPawel Dembicki 		      VSC73XX_MAC_CFG_TX_EN |
66895711cd5SPawel Dembicki 		      VSC73XX_MAC_CFG_RX_EN);
66995711cd5SPawel Dembicki 
67095711cd5SPawel Dembicki 	/* Flow control for the CPU port:
67195711cd5SPawel Dembicki 	 * Use a zero delay pause frame when pause condition is left
67295711cd5SPawel Dembicki 	 * Obey pause control frames
67395711cd5SPawel Dembicki 	 */
67495711cd5SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_MAC,
67595711cd5SPawel Dembicki 		      port,
67695711cd5SPawel Dembicki 		      VSC73XX_FCCONF,
67795711cd5SPawel Dembicki 		      VSC73XX_FCCONF_ZERO_PAUSE_EN |
67895711cd5SPawel Dembicki 		      VSC73XX_FCCONF_FLOW_CTRL_OBEY);
67995711cd5SPawel Dembicki 
68095711cd5SPawel Dembicki 	/* Issue pause control frames on PHY facing ports.
68195711cd5SPawel Dembicki 	 * Allow early initiation of MAC transmission if the amount
68295711cd5SPawel Dembicki 	 * of egress data is below 512 bytes on CPU port.
68395711cd5SPawel Dembicki 	 * FIXME: enable 20KiB buffers?
68495711cd5SPawel Dembicki 	 */
68595711cd5SPawel Dembicki 	if (port == CPU_PORT)
68695711cd5SPawel Dembicki 		val = VSC73XX_Q_MISC_CONF_EARLY_TX_512;
68795711cd5SPawel Dembicki 	else
68895711cd5SPawel Dembicki 		val = VSC73XX_Q_MISC_CONF_MAC_PAUSE_MODE;
68995711cd5SPawel Dembicki 	val |= VSC73XX_Q_MISC_CONF_EXTENT_MEM;
69095711cd5SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_MAC,
69195711cd5SPawel Dembicki 		      port,
69295711cd5SPawel Dembicki 		      VSC73XX_Q_MISC_CONF,
69395711cd5SPawel Dembicki 		      val);
69495711cd5SPawel Dembicki 
69595711cd5SPawel Dembicki 	/* Flow control MAC: a MAC address used in flow control frames */
69695711cd5SPawel Dembicki 	val = (vsc->addr[5] << 16) | (vsc->addr[4] << 8) | (vsc->addr[3]);
69795711cd5SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_MAC,
69895711cd5SPawel Dembicki 		      port,
69995711cd5SPawel Dembicki 		      VSC73XX_FCMACHI,
70095711cd5SPawel Dembicki 		      val);
70195711cd5SPawel Dembicki 	val = (vsc->addr[2] << 16) | (vsc->addr[1] << 8) | (vsc->addr[0]);
70295711cd5SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_MAC,
70395711cd5SPawel Dembicki 		      port,
70495711cd5SPawel Dembicki 		      VSC73XX_FCMACLO,
70595711cd5SPawel Dembicki 		      val);
70695711cd5SPawel Dembicki 
70795711cd5SPawel Dembicki 	/* Tell the categorizer to forward pause frames, not control
70895711cd5SPawel Dembicki 	 * frame. Do not drop anything.
70995711cd5SPawel Dembicki 	 */
71095711cd5SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_MAC,
71195711cd5SPawel Dembicki 		      port,
71295711cd5SPawel Dembicki 		      VSC73XX_CAT_DROP,
71395711cd5SPawel Dembicki 		      VSC73XX_CAT_DROP_FWD_PAUSE_ENA);
71495711cd5SPawel Dembicki 
71595711cd5SPawel Dembicki 	/* Clear all counters */
71695711cd5SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_MAC,
71795711cd5SPawel Dembicki 		      port, VSC73XX_C_RX0, 0);
71895711cd5SPawel Dembicki }
71995711cd5SPawel Dembicki 
72021fc3416SPawel Dembicki static void vsc73xx_reset_port(struct vsc73xx *vsc, int port, u32 initval)
72195711cd5SPawel Dembicki {
722eb7e33d0SPawel Dembicki 	int ret, err;
72321fc3416SPawel Dembicki 	u32 val;
72495711cd5SPawel Dembicki 
72595711cd5SPawel Dembicki 	/* Disable RX on this port */
72695711cd5SPawel Dembicki 	vsc73xx_update_bits(vsc, VSC73XX_BLOCK_MAC, port,
72795711cd5SPawel Dembicki 			    VSC73XX_MAC_CFG,
72895711cd5SPawel Dembicki 			    VSC73XX_MAC_CFG_RX_EN, 0);
72995711cd5SPawel Dembicki 
73095711cd5SPawel Dembicki 	/* Discard packets */
73195711cd5SPawel Dembicki 	vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ARBITER, 0,
73295711cd5SPawel Dembicki 			    VSC73XX_ARBDISC, BIT(port), BIT(port));
73395711cd5SPawel Dembicki 
73495711cd5SPawel Dembicki 	/* Wait until queue is empty */
735eb7e33d0SPawel Dembicki 	ret = read_poll_timeout(vsc73xx_read, err,
736eb7e33d0SPawel Dembicki 				err < 0 || (val & BIT(port)),
737eb7e33d0SPawel Dembicki 				VSC73XX_POLL_SLEEP_US,
738eb7e33d0SPawel Dembicki 				VSC73XX_POLL_TIMEOUT_US, false,
739eb7e33d0SPawel Dembicki 				vsc, VSC73XX_BLOCK_ARBITER, 0,
74095711cd5SPawel Dembicki 				VSC73XX_ARBEMPTY, &val);
741eb7e33d0SPawel Dembicki 	if (ret)
74295711cd5SPawel Dembicki 		dev_err(vsc->dev,
74395711cd5SPawel Dembicki 			"timeout waiting for block arbiter\n");
744eb7e33d0SPawel Dembicki 	else if (err < 0)
745eb7e33d0SPawel Dembicki 		dev_err(vsc->dev, "error reading arbiter\n");
74695711cd5SPawel Dembicki 
74795711cd5SPawel Dembicki 	/* Put this port into reset */
74895711cd5SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port, VSC73XX_MAC_CFG,
74921fc3416SPawel Dembicki 		      VSC73XX_MAC_CFG_RESET | initval);
75021fc3416SPawel Dembicki }
75195711cd5SPawel Dembicki 
75221fc3416SPawel Dembicki static void vsc73xx_mac_config(struct phylink_config *config, unsigned int mode,
75321fc3416SPawel Dembicki 			       const struct phylink_link_state *state)
75421fc3416SPawel Dembicki {
75521fc3416SPawel Dembicki 	struct dsa_port *dp = dsa_phylink_to_port(config);
75621fc3416SPawel Dembicki 	struct vsc73xx *vsc = dp->ds->priv;
75721fc3416SPawel Dembicki 	int port = dp->index;
75821fc3416SPawel Dembicki 
75921fc3416SPawel Dembicki 	/* Special handling of the CPU-facing port */
76021fc3416SPawel Dembicki 	if (port == CPU_PORT) {
76121fc3416SPawel Dembicki 		/* Other ports are already initialized but not this one */
76221fc3416SPawel Dembicki 		vsc73xx_init_port(vsc, CPU_PORT);
76321fc3416SPawel Dembicki 		/* Select the external port for this interface (EXT_PORT)
76421fc3416SPawel Dembicki 		 * Enable the GMII GTX external clock
76521fc3416SPawel Dembicki 		 * Use double data rate (DDR mode)
76621fc3416SPawel Dembicki 		 */
76721fc3416SPawel Dembicki 		vsc73xx_write(vsc, VSC73XX_BLOCK_MAC,
76821fc3416SPawel Dembicki 			      CPU_PORT,
76921fc3416SPawel Dembicki 			      VSC73XX_ADVPORTM,
77021fc3416SPawel Dembicki 			      VSC73XX_ADVPORTM_EXT_PORT |
77121fc3416SPawel Dembicki 			      VSC73XX_ADVPORTM_ENA_GTX |
77221fc3416SPawel Dembicki 			      VSC73XX_ADVPORTM_DDR_MODE);
77321fc3416SPawel Dembicki 	}
77421fc3416SPawel Dembicki }
77521fc3416SPawel Dembicki 
77621fc3416SPawel Dembicki static void vsc73xx_mac_link_down(struct phylink_config *config,
77721fc3416SPawel Dembicki 				  unsigned int mode, phy_interface_t interface)
77821fc3416SPawel Dembicki {
77921fc3416SPawel Dembicki 	struct dsa_port *dp = dsa_phylink_to_port(config);
78021fc3416SPawel Dembicki 	struct vsc73xx *vsc = dp->ds->priv;
78121fc3416SPawel Dembicki 	int port = dp->index;
78221fc3416SPawel Dembicki 
78321fc3416SPawel Dembicki 	/* This routine is described in the datasheet (below ARBDISC register
78421fc3416SPawel Dembicki 	 * description)
78521fc3416SPawel Dembicki 	 */
78621fc3416SPawel Dembicki 	vsc73xx_reset_port(vsc, port, 0);
78795711cd5SPawel Dembicki 
78895711cd5SPawel Dembicki 	/* Allow backward dropping of frames from this port */
78995711cd5SPawel Dembicki 	vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ARBITER, 0,
79095711cd5SPawel Dembicki 			    VSC73XX_SBACKWDROP, BIT(port), BIT(port));
79195711cd5SPawel Dembicki 
79295711cd5SPawel Dembicki 	/* Receive mask (disable forwarding) */
79395711cd5SPawel Dembicki 	vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ANALYZER, 0,
79495711cd5SPawel Dembicki 			    VSC73XX_RECVMASK, BIT(port), 0);
79595711cd5SPawel Dembicki }
79695711cd5SPawel Dembicki 
79721fc3416SPawel Dembicki static void vsc73xx_mac_link_up(struct phylink_config *config,
79821fc3416SPawel Dembicki 				struct phy_device *phy, unsigned int mode,
79921fc3416SPawel Dembicki 				phy_interface_t interface, int speed,
80021fc3416SPawel Dembicki 				int duplex, bool tx_pause, bool rx_pause)
80121fc3416SPawel Dembicki {
80221fc3416SPawel Dembicki 	struct dsa_port *dp = dsa_phylink_to_port(config);
80321fc3416SPawel Dembicki 	struct vsc73xx *vsc = dp->ds->priv;
80421fc3416SPawel Dembicki 	int port = dp->index;
80521fc3416SPawel Dembicki 	u32 val;
80621fc3416SPawel Dembicki 	u8 seed;
80795711cd5SPawel Dembicki 
80821fc3416SPawel Dembicki 	if (speed == SPEED_1000)
80921fc3416SPawel Dembicki 		val = VSC73XX_MAC_CFG_GIGA_MODE | VSC73XX_MAC_CFG_TX_IPG_1000M;
81095711cd5SPawel Dembicki 	else
81121fc3416SPawel Dembicki 		val = VSC73XX_MAC_CFG_TX_IPG_100_10M;
81295711cd5SPawel Dembicki 
81312af94b2SPawel Dembicki 	if (phy_interface_mode_is_rgmii(interface))
81421fc3416SPawel Dembicki 		val |= VSC73XX_MAC_CFG_CLK_SEL_1000M;
81521fc3416SPawel Dembicki 	else
81621fc3416SPawel Dembicki 		val |= VSC73XX_MAC_CFG_CLK_SEL_EXT;
81721fc3416SPawel Dembicki 
81821fc3416SPawel Dembicki 	if (duplex == DUPLEX_FULL)
81921fc3416SPawel Dembicki 		val |= VSC73XX_MAC_CFG_FDX;
82021fc3416SPawel Dembicki 
82121fc3416SPawel Dembicki 	/* This routine is described in the datasheet (below ARBDISC register
82221fc3416SPawel Dembicki 	 * description)
82321fc3416SPawel Dembicki 	 */
82421fc3416SPawel Dembicki 	vsc73xx_reset_port(vsc, port, val);
82521fc3416SPawel Dembicki 
82621fc3416SPawel Dembicki 	/* Seed the port randomness with randomness */
82721fc3416SPawel Dembicki 	get_random_bytes(&seed, 1);
82821fc3416SPawel Dembicki 	val |= seed << VSC73XX_MAC_CFG_SEED_OFFSET;
82921fc3416SPawel Dembicki 	val |= VSC73XX_MAC_CFG_SEED_LOAD;
83021fc3416SPawel Dembicki 	val |= VSC73XX_MAC_CFG_WEXC_DIS;
83121fc3416SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port, VSC73XX_MAC_CFG, val);
83221fc3416SPawel Dembicki 
83321fc3416SPawel Dembicki 	/* Flow control for the PHY facing ports:
83421fc3416SPawel Dembicki 	 * Use a zero delay pause frame when pause condition is left
83521fc3416SPawel Dembicki 	 * Obey pause control frames
83621fc3416SPawel Dembicki 	 * When generating pause frames, use 0xff as pause value
83721fc3416SPawel Dembicki 	 */
83821fc3416SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port, VSC73XX_FCCONF,
83921fc3416SPawel Dembicki 		      VSC73XX_FCCONF_ZERO_PAUSE_EN |
84021fc3416SPawel Dembicki 		      VSC73XX_FCCONF_FLOW_CTRL_OBEY |
84121fc3416SPawel Dembicki 		      0xff);
84221fc3416SPawel Dembicki 
84321fc3416SPawel Dembicki 	/* Accept packets again */
84421fc3416SPawel Dembicki 	vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ARBITER, 0,
84521fc3416SPawel Dembicki 			    VSC73XX_ARBDISC, BIT(port), 0);
84621fc3416SPawel Dembicki 
84721fc3416SPawel Dembicki 	/* Enable port (forwarding) in the receive mask */
84895711cd5SPawel Dembicki 	vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ANALYZER, 0,
84995711cd5SPawel Dembicki 			    VSC73XX_RECVMASK, BIT(port), BIT(port));
85021fc3416SPawel Dembicki 
85121fc3416SPawel Dembicki 	/* Disallow backward dropping of frames from this port */
85221fc3416SPawel Dembicki 	vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ARBITER, 0,
85321fc3416SPawel Dembicki 			    VSC73XX_SBACKWDROP, BIT(port), 0);
85421fc3416SPawel Dembicki 
85521fc3416SPawel Dembicki 	/* Enable TX, RX, deassert reset, stop loading seed */
85621fc3416SPawel Dembicki 	vsc73xx_update_bits(vsc, VSC73XX_BLOCK_MAC, port,
85721fc3416SPawel Dembicki 			    VSC73XX_MAC_CFG,
85821fc3416SPawel Dembicki 			    VSC73XX_MAC_CFG_RESET | VSC73XX_MAC_CFG_SEED_LOAD |
85921fc3416SPawel Dembicki 			    VSC73XX_MAC_CFG_TX_EN | VSC73XX_MAC_CFG_RX_EN,
86021fc3416SPawel Dembicki 			    VSC73XX_MAC_CFG_TX_EN | VSC73XX_MAC_CFG_RX_EN);
86195711cd5SPawel Dembicki }
86295711cd5SPawel Dembicki 
86395711cd5SPawel Dembicki static int vsc73xx_port_enable(struct dsa_switch *ds, int port,
86495711cd5SPawel Dembicki 			       struct phy_device *phy)
86595711cd5SPawel Dembicki {
86695711cd5SPawel Dembicki 	struct vsc73xx *vsc = ds->priv;
86795711cd5SPawel Dembicki 
86895711cd5SPawel Dembicki 	dev_info(vsc->dev, "enable port %d\n", port);
86995711cd5SPawel Dembicki 	vsc73xx_init_port(vsc, port);
87095711cd5SPawel Dembicki 
87195711cd5SPawel Dembicki 	return 0;
87295711cd5SPawel Dembicki }
87395711cd5SPawel Dembicki 
87495711cd5SPawel Dembicki static void vsc73xx_port_disable(struct dsa_switch *ds, int port)
87595711cd5SPawel Dembicki {
87695711cd5SPawel Dembicki 	struct vsc73xx *vsc = ds->priv;
87795711cd5SPawel Dembicki 
87895711cd5SPawel Dembicki 	/* Just put the port into reset */
87995711cd5SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port,
88095711cd5SPawel Dembicki 		      VSC73XX_MAC_CFG, VSC73XX_MAC_CFG_RESET);
88195711cd5SPawel Dembicki }
88295711cd5SPawel Dembicki 
88395711cd5SPawel Dembicki static const struct vsc73xx_counter *
88495711cd5SPawel Dembicki vsc73xx_find_counter(struct vsc73xx *vsc,
88595711cd5SPawel Dembicki 		     u8 counter,
88695711cd5SPawel Dembicki 		     bool tx)
88795711cd5SPawel Dembicki {
88895711cd5SPawel Dembicki 	const struct vsc73xx_counter *cnts;
88995711cd5SPawel Dembicki 	int num_cnts;
89095711cd5SPawel Dembicki 	int i;
89195711cd5SPawel Dembicki 
89295711cd5SPawel Dembicki 	if (tx) {
89395711cd5SPawel Dembicki 		cnts = vsc73xx_tx_counters;
89495711cd5SPawel Dembicki 		num_cnts = ARRAY_SIZE(vsc73xx_tx_counters);
89595711cd5SPawel Dembicki 	} else {
89695711cd5SPawel Dembicki 		cnts = vsc73xx_rx_counters;
89795711cd5SPawel Dembicki 		num_cnts = ARRAY_SIZE(vsc73xx_rx_counters);
89895711cd5SPawel Dembicki 	}
89995711cd5SPawel Dembicki 
90095711cd5SPawel Dembicki 	for (i = 0; i < num_cnts; i++) {
90195711cd5SPawel Dembicki 		const struct vsc73xx_counter *cnt;
90295711cd5SPawel Dembicki 
90395711cd5SPawel Dembicki 		cnt = &cnts[i];
90495711cd5SPawel Dembicki 		if (cnt->counter == counter)
90595711cd5SPawel Dembicki 			return cnt;
90695711cd5SPawel Dembicki 	}
90795711cd5SPawel Dembicki 
90895711cd5SPawel Dembicki 	return NULL;
90995711cd5SPawel Dembicki }
91095711cd5SPawel Dembicki 
91195711cd5SPawel Dembicki static void vsc73xx_get_strings(struct dsa_switch *ds, int port, u32 stringset,
91295711cd5SPawel Dembicki 				uint8_t *data)
91395711cd5SPawel Dembicki {
91495711cd5SPawel Dembicki 	const struct vsc73xx_counter *cnt;
91595711cd5SPawel Dembicki 	struct vsc73xx *vsc = ds->priv;
91695711cd5SPawel Dembicki 	u8 indices[6];
917e3bbab47SJustin Stitt 	u8 *buf = data;
918e3bbab47SJustin Stitt 	int i;
91995711cd5SPawel Dembicki 	u32 val;
92095711cd5SPawel Dembicki 	int ret;
92195711cd5SPawel Dembicki 
92295711cd5SPawel Dembicki 	if (stringset != ETH_SS_STATS)
92395711cd5SPawel Dembicki 		return;
92495711cd5SPawel Dembicki 
92595711cd5SPawel Dembicki 	ret = vsc73xx_read(vsc, VSC73XX_BLOCK_MAC, port,
92695711cd5SPawel Dembicki 			   VSC73XX_C_CFG, &val);
92795711cd5SPawel Dembicki 	if (ret)
92895711cd5SPawel Dembicki 		return;
92995711cd5SPawel Dembicki 
93095711cd5SPawel Dembicki 	indices[0] = (val & 0x1f); /* RX counter 0 */
93195711cd5SPawel Dembicki 	indices[1] = ((val >> 5) & 0x1f); /* RX counter 1 */
93295711cd5SPawel Dembicki 	indices[2] = ((val >> 10) & 0x1f); /* RX counter 2 */
93395711cd5SPawel Dembicki 	indices[3] = ((val >> 16) & 0x1f); /* TX counter 0 */
93495711cd5SPawel Dembicki 	indices[4] = ((val >> 21) & 0x1f); /* TX counter 1 */
93595711cd5SPawel Dembicki 	indices[5] = ((val >> 26) & 0x1f); /* TX counter 2 */
93695711cd5SPawel Dembicki 
93795711cd5SPawel Dembicki 	/* The first counters is the RX octets */
938e403cfffSjustinstitt@google.com 	ethtool_puts(&buf, "RxEtherStatsOctets");
93995711cd5SPawel Dembicki 
94095711cd5SPawel Dembicki 	/* Each port supports recording 3 RX counters and 3 TX counters,
94195711cd5SPawel Dembicki 	 * figure out what counters we use in this set-up and return the
94295711cd5SPawel Dembicki 	 * names of them. The hardware default counters will be number of
94395711cd5SPawel Dembicki 	 * packets on RX/TX, combined broadcast+multicast packets RX/TX and
94495711cd5SPawel Dembicki 	 * total error packets RX/TX.
94595711cd5SPawel Dembicki 	 */
94695711cd5SPawel Dembicki 	for (i = 0; i < 3; i++) {
94795711cd5SPawel Dembicki 		cnt = vsc73xx_find_counter(vsc, indices[i], false);
948e403cfffSjustinstitt@google.com 		ethtool_puts(&buf, cnt ? cnt->name : "");
94995711cd5SPawel Dembicki 	}
95095711cd5SPawel Dembicki 
95195711cd5SPawel Dembicki 	/* TX stats begins with the number of TX octets */
952e403cfffSjustinstitt@google.com 	ethtool_puts(&buf, "TxEtherStatsOctets");
95395711cd5SPawel Dembicki 
95495711cd5SPawel Dembicki 	for (i = 3; i < 6; i++) {
95595711cd5SPawel Dembicki 		cnt = vsc73xx_find_counter(vsc, indices[i], true);
956e403cfffSjustinstitt@google.com 		ethtool_puts(&buf, cnt ? cnt->name : "");
957e3bbab47SJustin Stitt 
95895711cd5SPawel Dembicki 	}
95995711cd5SPawel Dembicki }
96095711cd5SPawel Dembicki 
96195711cd5SPawel Dembicki static int vsc73xx_get_sset_count(struct dsa_switch *ds, int port, int sset)
96295711cd5SPawel Dembicki {
96395711cd5SPawel Dembicki 	/* We only support SS_STATS */
96495711cd5SPawel Dembicki 	if (sset != ETH_SS_STATS)
96595711cd5SPawel Dembicki 		return 0;
96695711cd5SPawel Dembicki 	/* RX and TX packets, then 3 RX counters, 3 TX counters */
96795711cd5SPawel Dembicki 	return 8;
96895711cd5SPawel Dembicki }
96995711cd5SPawel Dembicki 
97095711cd5SPawel Dembicki static void vsc73xx_get_ethtool_stats(struct dsa_switch *ds, int port,
97195711cd5SPawel Dembicki 				      uint64_t *data)
97295711cd5SPawel Dembicki {
97395711cd5SPawel Dembicki 	struct vsc73xx *vsc = ds->priv;
97495711cd5SPawel Dembicki 	u8 regs[] = {
97595711cd5SPawel Dembicki 		VSC73XX_RXOCT,
97695711cd5SPawel Dembicki 		VSC73XX_C_RX0,
97795711cd5SPawel Dembicki 		VSC73XX_C_RX1,
97895711cd5SPawel Dembicki 		VSC73XX_C_RX2,
97995711cd5SPawel Dembicki 		VSC73XX_TXOCT,
98095711cd5SPawel Dembicki 		VSC73XX_C_TX0,
98195711cd5SPawel Dembicki 		VSC73XX_C_TX1,
98295711cd5SPawel Dembicki 		VSC73XX_C_TX2,
98395711cd5SPawel Dembicki 	};
98495711cd5SPawel Dembicki 	u32 val;
98595711cd5SPawel Dembicki 	int ret;
98695711cd5SPawel Dembicki 	int i;
98795711cd5SPawel Dembicki 
98895711cd5SPawel Dembicki 	for (i = 0; i < ARRAY_SIZE(regs); i++) {
98995711cd5SPawel Dembicki 		ret = vsc73xx_read(vsc, VSC73XX_BLOCK_MAC, port,
99095711cd5SPawel Dembicki 				   regs[i], &val);
99195711cd5SPawel Dembicki 		if (ret) {
99295711cd5SPawel Dembicki 			dev_err(vsc->dev, "error reading counter %d\n", i);
99395711cd5SPawel Dembicki 			return;
99495711cd5SPawel Dembicki 		}
99595711cd5SPawel Dembicki 		data[i] = val;
99695711cd5SPawel Dembicki 	}
99795711cd5SPawel Dembicki }
99895711cd5SPawel Dembicki 
999fb77ffc6SVladimir Oltean static int vsc73xx_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
1000fb77ffc6SVladimir Oltean {
1001fb77ffc6SVladimir Oltean 	struct vsc73xx *vsc = ds->priv;
1002fb77ffc6SVladimir Oltean 
1003fb77ffc6SVladimir Oltean 	return vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port,
10043cf62c81SPawel Dembicki 			     VSC73XX_MAXLEN, new_mtu + ETH_HLEN + ETH_FCS_LEN);
1005fb77ffc6SVladimir Oltean }
1006fb77ffc6SVladimir Oltean 
1007fb77ffc6SVladimir Oltean /* According to application not "VSC7398 Jumbo Frames" setting
10083cf62c81SPawel Dembicki  * up the frame size to 9.6 KB does not affect the performance on standard
1009fb77ffc6SVladimir Oltean  * frames. It is clear from the application note that
1010fb77ffc6SVladimir Oltean  * "9.6 kilobytes" == 9600 bytes.
1011fb77ffc6SVladimir Oltean  */
1012fb77ffc6SVladimir Oltean static int vsc73xx_get_max_mtu(struct dsa_switch *ds, int port)
1013fb77ffc6SVladimir Oltean {
10143cf62c81SPawel Dembicki 	return 9600 - ETH_HLEN - ETH_FCS_LEN;
1015fb77ffc6SVladimir Oltean }
1016fb77ffc6SVladimir Oltean 
1017a026809cSRussell King (Oracle) static void vsc73xx_phylink_get_caps(struct dsa_switch *dsa, int port,
1018a026809cSRussell King (Oracle) 				     struct phylink_config *config)
1019a026809cSRussell King (Oracle) {
1020a026809cSRussell King (Oracle) 	unsigned long *interfaces = config->supported_interfaces;
1021a026809cSRussell King (Oracle) 
1022a026809cSRussell King (Oracle) 	if (port == 5)
1023a026809cSRussell King (Oracle) 		return;
1024a026809cSRussell King (Oracle) 
1025a026809cSRussell King (Oracle) 	if (port == CPU_PORT) {
1026a026809cSRussell King (Oracle) 		__set_bit(PHY_INTERFACE_MODE_MII, interfaces);
1027a026809cSRussell King (Oracle) 		__set_bit(PHY_INTERFACE_MODE_REVMII, interfaces);
1028a026809cSRussell King (Oracle) 		__set_bit(PHY_INTERFACE_MODE_GMII, interfaces);
1029a026809cSRussell King (Oracle) 		__set_bit(PHY_INTERFACE_MODE_RGMII, interfaces);
1030a026809cSRussell King (Oracle) 	}
1031a026809cSRussell King (Oracle) 
1032a026809cSRussell King (Oracle) 	if (port <= 4) {
1033a026809cSRussell King (Oracle) 		/* Internal PHYs */
1034a026809cSRussell King (Oracle) 		__set_bit(PHY_INTERFACE_MODE_INTERNAL, interfaces);
1035a026809cSRussell King (Oracle) 		/* phylib default */
1036a026809cSRussell King (Oracle) 		__set_bit(PHY_INTERFACE_MODE_GMII, interfaces);
1037a026809cSRussell King (Oracle) 	}
1038a026809cSRussell King (Oracle) 
1039a026809cSRussell King (Oracle) 	config->mac_capabilities = MAC_SYM_PAUSE | MAC_10 | MAC_100 | MAC_1000;
1040a026809cSRussell King (Oracle) }
1041a026809cSRussell King (Oracle) 
104221fc3416SPawel Dembicki static const struct phylink_mac_ops vsc73xx_phylink_mac_ops = {
104321fc3416SPawel Dembicki 	.mac_config = vsc73xx_mac_config,
104421fc3416SPawel Dembicki 	.mac_link_down = vsc73xx_mac_link_down,
104521fc3416SPawel Dembicki 	.mac_link_up = vsc73xx_mac_link_up,
104621fc3416SPawel Dembicki };
104721fc3416SPawel Dembicki 
104895711cd5SPawel Dembicki static const struct dsa_switch_ops vsc73xx_ds_ops = {
104995711cd5SPawel Dembicki 	.get_tag_protocol = vsc73xx_get_tag_protocol,
105095711cd5SPawel Dembicki 	.setup = vsc73xx_setup,
105195711cd5SPawel Dembicki 	.phy_read = vsc73xx_phy_read,
105295711cd5SPawel Dembicki 	.phy_write = vsc73xx_phy_write,
105395711cd5SPawel Dembicki 	.get_strings = vsc73xx_get_strings,
105495711cd5SPawel Dembicki 	.get_ethtool_stats = vsc73xx_get_ethtool_stats,
105595711cd5SPawel Dembicki 	.get_sset_count = vsc73xx_get_sset_count,
105695711cd5SPawel Dembicki 	.port_enable = vsc73xx_port_enable,
105795711cd5SPawel Dembicki 	.port_disable = vsc73xx_port_disable,
1058fb77ffc6SVladimir Oltean 	.port_change_mtu = vsc73xx_change_mtu,
1059fb77ffc6SVladimir Oltean 	.port_max_mtu = vsc73xx_get_max_mtu,
1060a026809cSRussell King (Oracle) 	.phylink_get_caps = vsc73xx_phylink_get_caps,
106195711cd5SPawel Dembicki };
106295711cd5SPawel Dembicki 
106395711cd5SPawel Dembicki static int vsc73xx_gpio_get(struct gpio_chip *chip, unsigned int offset)
106495711cd5SPawel Dembicki {
106595711cd5SPawel Dembicki 	struct vsc73xx *vsc = gpiochip_get_data(chip);
106695711cd5SPawel Dembicki 	u32 val;
106795711cd5SPawel Dembicki 	int ret;
106895711cd5SPawel Dembicki 
106995711cd5SPawel Dembicki 	ret = vsc73xx_read(vsc, VSC73XX_BLOCK_SYSTEM, 0,
107095711cd5SPawel Dembicki 			   VSC73XX_GPIO, &val);
107195711cd5SPawel Dembicki 	if (ret)
107295711cd5SPawel Dembicki 		return ret;
107395711cd5SPawel Dembicki 
107495711cd5SPawel Dembicki 	return !!(val & BIT(offset));
107595711cd5SPawel Dembicki }
107695711cd5SPawel Dembicki 
107795711cd5SPawel Dembicki static void vsc73xx_gpio_set(struct gpio_chip *chip, unsigned int offset,
107895711cd5SPawel Dembicki 			     int val)
107995711cd5SPawel Dembicki {
108095711cd5SPawel Dembicki 	struct vsc73xx *vsc = gpiochip_get_data(chip);
108195711cd5SPawel Dembicki 	u32 tmp = val ? BIT(offset) : 0;
108295711cd5SPawel Dembicki 
108395711cd5SPawel Dembicki 	vsc73xx_update_bits(vsc, VSC73XX_BLOCK_SYSTEM, 0,
108495711cd5SPawel Dembicki 			    VSC73XX_GPIO, BIT(offset), tmp);
108595711cd5SPawel Dembicki }
108695711cd5SPawel Dembicki 
108795711cd5SPawel Dembicki static int vsc73xx_gpio_direction_output(struct gpio_chip *chip,
108895711cd5SPawel Dembicki 					 unsigned int offset, int val)
108995711cd5SPawel Dembicki {
109095711cd5SPawel Dembicki 	struct vsc73xx *vsc = gpiochip_get_data(chip);
109195711cd5SPawel Dembicki 	u32 tmp = val ? BIT(offset) : 0;
109295711cd5SPawel Dembicki 
109395711cd5SPawel Dembicki 	return vsc73xx_update_bits(vsc, VSC73XX_BLOCK_SYSTEM, 0,
109495711cd5SPawel Dembicki 				   VSC73XX_GPIO, BIT(offset + 4) | BIT(offset),
109595711cd5SPawel Dembicki 				   BIT(offset + 4) | tmp);
109695711cd5SPawel Dembicki }
109795711cd5SPawel Dembicki 
109895711cd5SPawel Dembicki static int vsc73xx_gpio_direction_input(struct gpio_chip *chip,
109995711cd5SPawel Dembicki 					unsigned int offset)
110095711cd5SPawel Dembicki {
110195711cd5SPawel Dembicki 	struct vsc73xx *vsc = gpiochip_get_data(chip);
110295711cd5SPawel Dembicki 
110395711cd5SPawel Dembicki 	return  vsc73xx_update_bits(vsc, VSC73XX_BLOCK_SYSTEM, 0,
110495711cd5SPawel Dembicki 				    VSC73XX_GPIO, BIT(offset + 4),
110595711cd5SPawel Dembicki 				    0);
110695711cd5SPawel Dembicki }
110795711cd5SPawel Dembicki 
110895711cd5SPawel Dembicki static int vsc73xx_gpio_get_direction(struct gpio_chip *chip,
110995711cd5SPawel Dembicki 				      unsigned int offset)
111095711cd5SPawel Dembicki {
111195711cd5SPawel Dembicki 	struct vsc73xx *vsc = gpiochip_get_data(chip);
111295711cd5SPawel Dembicki 	u32 val;
111395711cd5SPawel Dembicki 	int ret;
111495711cd5SPawel Dembicki 
111595711cd5SPawel Dembicki 	ret = vsc73xx_read(vsc, VSC73XX_BLOCK_SYSTEM, 0,
111695711cd5SPawel Dembicki 			   VSC73XX_GPIO, &val);
111795711cd5SPawel Dembicki 	if (ret)
111895711cd5SPawel Dembicki 		return ret;
111995711cd5SPawel Dembicki 
112095711cd5SPawel Dembicki 	return !(val & BIT(offset + 4));
112195711cd5SPawel Dembicki }
112295711cd5SPawel Dembicki 
112395711cd5SPawel Dembicki static int vsc73xx_gpio_probe(struct vsc73xx *vsc)
112495711cd5SPawel Dembicki {
112595711cd5SPawel Dembicki 	int ret;
112695711cd5SPawel Dembicki 
112795711cd5SPawel Dembicki 	vsc->gc.label = devm_kasprintf(vsc->dev, GFP_KERNEL, "VSC%04x",
112895711cd5SPawel Dembicki 				       vsc->chipid);
1129776dac5aSKunwu Chan 	if (!vsc->gc.label)
1130776dac5aSKunwu Chan 		return -ENOMEM;
113195711cd5SPawel Dembicki 	vsc->gc.ngpio = 4;
113295711cd5SPawel Dembicki 	vsc->gc.owner = THIS_MODULE;
113395711cd5SPawel Dembicki 	vsc->gc.parent = vsc->dev;
113495711cd5SPawel Dembicki 	vsc->gc.base = -1;
113595711cd5SPawel Dembicki 	vsc->gc.get = vsc73xx_gpio_get;
113695711cd5SPawel Dembicki 	vsc->gc.set = vsc73xx_gpio_set;
113795711cd5SPawel Dembicki 	vsc->gc.direction_input = vsc73xx_gpio_direction_input;
113895711cd5SPawel Dembicki 	vsc->gc.direction_output = vsc73xx_gpio_direction_output;
113995711cd5SPawel Dembicki 	vsc->gc.get_direction = vsc73xx_gpio_get_direction;
114095711cd5SPawel Dembicki 	vsc->gc.can_sleep = true;
114195711cd5SPawel Dembicki 	ret = devm_gpiochip_add_data(vsc->dev, &vsc->gc, vsc);
114295711cd5SPawel Dembicki 	if (ret) {
114395711cd5SPawel Dembicki 		dev_err(vsc->dev, "unable to register GPIO chip\n");
114495711cd5SPawel Dembicki 		return ret;
114595711cd5SPawel Dembicki 	}
114695711cd5SPawel Dembicki 	return 0;
114795711cd5SPawel Dembicki }
114895711cd5SPawel Dembicki 
114995711cd5SPawel Dembicki int vsc73xx_probe(struct vsc73xx *vsc)
115095711cd5SPawel Dembicki {
115195711cd5SPawel Dembicki 	struct device *dev = vsc->dev;
115295711cd5SPawel Dembicki 	int ret;
115395711cd5SPawel Dembicki 
115495711cd5SPawel Dembicki 	/* Release reset, if any */
115595711cd5SPawel Dembicki 	vsc->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
115695711cd5SPawel Dembicki 	if (IS_ERR(vsc->reset)) {
115795711cd5SPawel Dembicki 		dev_err(dev, "failed to get RESET GPIO\n");
115895711cd5SPawel Dembicki 		return PTR_ERR(vsc->reset);
115995711cd5SPawel Dembicki 	}
116095711cd5SPawel Dembicki 	if (vsc->reset)
116195711cd5SPawel Dembicki 		/* Wait 20ms according to datasheet table 245 */
116295711cd5SPawel Dembicki 		msleep(20);
116395711cd5SPawel Dembicki 
116495711cd5SPawel Dembicki 	ret = vsc73xx_detect(vsc);
11651da39ff0SPawel Dembicki 	if (ret == -EAGAIN) {
11661da39ff0SPawel Dembicki 		dev_err(vsc->dev,
11671da39ff0SPawel Dembicki 			"Chip seems to be out of control. Assert reset and try again.\n");
11681da39ff0SPawel Dembicki 		gpiod_set_value_cansleep(vsc->reset, 1);
11691da39ff0SPawel Dembicki 		/* Reset pulse should be 20ns minimum, according to datasheet
11701da39ff0SPawel Dembicki 		 * table 245, so 10us should be fine
11711da39ff0SPawel Dembicki 		 */
11721da39ff0SPawel Dembicki 		usleep_range(10, 100);
11731da39ff0SPawel Dembicki 		gpiod_set_value_cansleep(vsc->reset, 0);
11741da39ff0SPawel Dembicki 		/* Wait 20ms according to datasheet table 245 */
11751da39ff0SPawel Dembicki 		msleep(20);
11761da39ff0SPawel Dembicki 		ret = vsc73xx_detect(vsc);
11771da39ff0SPawel Dembicki 	}
117895711cd5SPawel Dembicki 	if (ret) {
117995711cd5SPawel Dembicki 		dev_err(dev, "no chip found (%d)\n", ret);
118095711cd5SPawel Dembicki 		return -ENODEV;
118195711cd5SPawel Dembicki 	}
118295711cd5SPawel Dembicki 
118395711cd5SPawel Dembicki 	eth_random_addr(vsc->addr);
118495711cd5SPawel Dembicki 	dev_info(vsc->dev,
118595711cd5SPawel Dembicki 		 "MAC for control frames: %02X:%02X:%02X:%02X:%02X:%02X\n",
118695711cd5SPawel Dembicki 		 vsc->addr[0], vsc->addr[1], vsc->addr[2],
118795711cd5SPawel Dembicki 		 vsc->addr[3], vsc->addr[4], vsc->addr[5]);
118895711cd5SPawel Dembicki 
11897e99e347SVivien Didelot 	vsc->ds = devm_kzalloc(dev, sizeof(*vsc->ds), GFP_KERNEL);
119095711cd5SPawel Dembicki 	if (!vsc->ds)
119195711cd5SPawel Dembicki 		return -ENOMEM;
11927e99e347SVivien Didelot 
11937e99e347SVivien Didelot 	vsc->ds->dev = dev;
1194*6cc5280aSPawel Dembicki 	vsc->ds->num_ports = VSC73XX_MAX_NUM_PORTS;
119595711cd5SPawel Dembicki 	vsc->ds->priv = vsc;
119695711cd5SPawel Dembicki 
119795711cd5SPawel Dembicki 	vsc->ds->ops = &vsc73xx_ds_ops;
119821fc3416SPawel Dembicki 	vsc->ds->phylink_mac_ops = &vsc73xx_phylink_mac_ops;
119995711cd5SPawel Dembicki 	ret = dsa_register_switch(vsc->ds);
120095711cd5SPawel Dembicki 	if (ret) {
120195711cd5SPawel Dembicki 		dev_err(dev, "unable to register switch (%d)\n", ret);
120295711cd5SPawel Dembicki 		return ret;
120395711cd5SPawel Dembicki 	}
120495711cd5SPawel Dembicki 
120595711cd5SPawel Dembicki 	ret = vsc73xx_gpio_probe(vsc);
120695711cd5SPawel Dembicki 	if (ret) {
120795711cd5SPawel Dembicki 		dsa_unregister_switch(vsc->ds);
120895711cd5SPawel Dembicki 		return ret;
120995711cd5SPawel Dembicki 	}
121095711cd5SPawel Dembicki 
121195711cd5SPawel Dembicki 	return 0;
121295711cd5SPawel Dembicki }
121395711cd5SPawel Dembicki EXPORT_SYMBOL(vsc73xx_probe);
121495711cd5SPawel Dembicki 
1215e99fa423SUwe Kleine-König void vsc73xx_remove(struct vsc73xx *vsc)
121695711cd5SPawel Dembicki {
121795711cd5SPawel Dembicki 	dsa_unregister_switch(vsc->ds);
121895711cd5SPawel Dembicki 	gpiod_set_value(vsc->reset, 1);
121995711cd5SPawel Dembicki }
122095711cd5SPawel Dembicki EXPORT_SYMBOL(vsc73xx_remove);
122195711cd5SPawel Dembicki 
12220650bf52SVladimir Oltean void vsc73xx_shutdown(struct vsc73xx *vsc)
12230650bf52SVladimir Oltean {
12240650bf52SVladimir Oltean 	dsa_switch_shutdown(vsc->ds);
12250650bf52SVladimir Oltean }
12260650bf52SVladimir Oltean EXPORT_SYMBOL(vsc73xx_shutdown);
12270650bf52SVladimir Oltean 
122895711cd5SPawel Dembicki MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
122995711cd5SPawel Dembicki MODULE_DESCRIPTION("Vitesse VSC7385/7388/7395/7398 driver");
123095711cd5SPawel Dembicki MODULE_LICENSE("GPL v2");
1231