xref: /linux/drivers/net/dsa/vitesse-vsc73xx-core.c (revision eb7e33d01db3aec128590391b2397384bab406b6)
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>
20*eb7e33d0SPawel 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 
272*eb7e33d0SPawel Dembicki #define VSC73XX_POLL_SLEEP_US		1000
273*eb7e33d0SPawel Dembicki #define VSC73XX_POLL_TIMEOUT_US		10000
274*eb7e33d0SPawel 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 
72095711cd5SPawel Dembicki static void vsc73xx_adjust_enable_port(struct vsc73xx *vsc,
72195711cd5SPawel Dembicki 				       int port, struct phy_device *phydev,
72295711cd5SPawel Dembicki 				       u32 initval)
72395711cd5SPawel Dembicki {
72495711cd5SPawel Dembicki 	u32 val = initval;
72595711cd5SPawel Dembicki 	u8 seed;
72695711cd5SPawel Dembicki 
72795711cd5SPawel Dembicki 	/* Reset this port FIXME: break out subroutine */
72895711cd5SPawel Dembicki 	val |= VSC73XX_MAC_CFG_RESET;
72995711cd5SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port, VSC73XX_MAC_CFG, val);
73095711cd5SPawel Dembicki 
73195711cd5SPawel Dembicki 	/* Seed the port randomness with randomness */
73295711cd5SPawel Dembicki 	get_random_bytes(&seed, 1);
73395711cd5SPawel Dembicki 	val |= seed << VSC73XX_MAC_CFG_SEED_OFFSET;
73495711cd5SPawel Dembicki 	val |= VSC73XX_MAC_CFG_SEED_LOAD;
73595711cd5SPawel Dembicki 	val |= VSC73XX_MAC_CFG_WEXC_DIS;
73695711cd5SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port, VSC73XX_MAC_CFG, val);
73795711cd5SPawel Dembicki 
73895711cd5SPawel Dembicki 	/* Flow control for the PHY facing ports:
73995711cd5SPawel Dembicki 	 * Use a zero delay pause frame when pause condition is left
74095711cd5SPawel Dembicki 	 * Obey pause control frames
74195711cd5SPawel Dembicki 	 * When generating pause frames, use 0xff as pause value
74295711cd5SPawel Dembicki 	 */
74395711cd5SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port, VSC73XX_FCCONF,
74495711cd5SPawel Dembicki 		      VSC73XX_FCCONF_ZERO_PAUSE_EN |
74595711cd5SPawel Dembicki 		      VSC73XX_FCCONF_FLOW_CTRL_OBEY |
74695711cd5SPawel Dembicki 		      0xff);
74795711cd5SPawel Dembicki 
74895711cd5SPawel Dembicki 	/* Disallow backward dropping of frames from this port */
74995711cd5SPawel Dembicki 	vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ARBITER, 0,
75095711cd5SPawel Dembicki 			    VSC73XX_SBACKWDROP, BIT(port), 0);
75195711cd5SPawel Dembicki 
75295711cd5SPawel Dembicki 	/* Enable TX, RX, deassert reset, stop loading seed */
75395711cd5SPawel Dembicki 	vsc73xx_update_bits(vsc, VSC73XX_BLOCK_MAC, port,
75495711cd5SPawel Dembicki 			    VSC73XX_MAC_CFG,
75595711cd5SPawel Dembicki 			    VSC73XX_MAC_CFG_RESET | VSC73XX_MAC_CFG_SEED_LOAD |
75695711cd5SPawel Dembicki 			    VSC73XX_MAC_CFG_TX_EN | VSC73XX_MAC_CFG_RX_EN,
75795711cd5SPawel Dembicki 			    VSC73XX_MAC_CFG_TX_EN | VSC73XX_MAC_CFG_RX_EN);
75895711cd5SPawel Dembicki }
75995711cd5SPawel Dembicki 
76095711cd5SPawel Dembicki static void vsc73xx_adjust_link(struct dsa_switch *ds, int port,
76195711cd5SPawel Dembicki 				struct phy_device *phydev)
76295711cd5SPawel Dembicki {
76395711cd5SPawel Dembicki 	struct vsc73xx *vsc = ds->priv;
76495711cd5SPawel Dembicki 	u32 val;
76595711cd5SPawel Dembicki 
76695711cd5SPawel Dembicki 	/* Special handling of the CPU-facing port */
76795711cd5SPawel Dembicki 	if (port == CPU_PORT) {
76895711cd5SPawel Dembicki 		/* Other ports are already initialized but not this one */
76995711cd5SPawel Dembicki 		vsc73xx_init_port(vsc, CPU_PORT);
77095711cd5SPawel Dembicki 		/* Select the external port for this interface (EXT_PORT)
77195711cd5SPawel Dembicki 		 * Enable the GMII GTX external clock
77295711cd5SPawel Dembicki 		 * Use double data rate (DDR mode)
77395711cd5SPawel Dembicki 		 */
77495711cd5SPawel Dembicki 		vsc73xx_write(vsc, VSC73XX_BLOCK_MAC,
77595711cd5SPawel Dembicki 			      CPU_PORT,
77695711cd5SPawel Dembicki 			      VSC73XX_ADVPORTM,
77795711cd5SPawel Dembicki 			      VSC73XX_ADVPORTM_EXT_PORT |
77895711cd5SPawel Dembicki 			      VSC73XX_ADVPORTM_ENA_GTX |
77995711cd5SPawel Dembicki 			      VSC73XX_ADVPORTM_DDR_MODE);
78095711cd5SPawel Dembicki 	}
78195711cd5SPawel Dembicki 
78295711cd5SPawel Dembicki 	/* This is the MAC confiuration that always need to happen
78395711cd5SPawel Dembicki 	 * after a PHY or the CPU port comes up or down.
78495711cd5SPawel Dembicki 	 */
78595711cd5SPawel Dembicki 	if (!phydev->link) {
786*eb7e33d0SPawel Dembicki 		int ret, err;
78795711cd5SPawel Dembicki 
78895711cd5SPawel Dembicki 		dev_dbg(vsc->dev, "port %d: went down\n",
78995711cd5SPawel Dembicki 			port);
79095711cd5SPawel Dembicki 
79195711cd5SPawel Dembicki 		/* Disable RX on this port */
79295711cd5SPawel Dembicki 		vsc73xx_update_bits(vsc, VSC73XX_BLOCK_MAC, port,
79395711cd5SPawel Dembicki 				    VSC73XX_MAC_CFG,
79495711cd5SPawel Dembicki 				    VSC73XX_MAC_CFG_RX_EN, 0);
79595711cd5SPawel Dembicki 
79695711cd5SPawel Dembicki 		/* Discard packets */
79795711cd5SPawel Dembicki 		vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ARBITER, 0,
79895711cd5SPawel Dembicki 				    VSC73XX_ARBDISC, BIT(port), BIT(port));
79995711cd5SPawel Dembicki 
80095711cd5SPawel Dembicki 		/* Wait until queue is empty */
801*eb7e33d0SPawel Dembicki 		ret = read_poll_timeout(vsc73xx_read, err,
802*eb7e33d0SPawel Dembicki 					err < 0 || (val & BIT(port)),
803*eb7e33d0SPawel Dembicki 					VSC73XX_POLL_SLEEP_US,
804*eb7e33d0SPawel Dembicki 					VSC73XX_POLL_TIMEOUT_US, false,
805*eb7e33d0SPawel Dembicki 					vsc, VSC73XX_BLOCK_ARBITER, 0,
80695711cd5SPawel Dembicki 					VSC73XX_ARBEMPTY, &val);
807*eb7e33d0SPawel Dembicki 		if (ret)
80895711cd5SPawel Dembicki 			dev_err(vsc->dev,
80995711cd5SPawel Dembicki 				"timeout waiting for block arbiter\n");
810*eb7e33d0SPawel Dembicki 		else if (err < 0)
811*eb7e33d0SPawel Dembicki 			dev_err(vsc->dev, "error reading arbiter\n");
81295711cd5SPawel Dembicki 
81395711cd5SPawel Dembicki 		/* Put this port into reset */
81495711cd5SPawel Dembicki 		vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port, VSC73XX_MAC_CFG,
81595711cd5SPawel Dembicki 			      VSC73XX_MAC_CFG_RESET);
81695711cd5SPawel Dembicki 
81795711cd5SPawel Dembicki 		/* Accept packets again */
81895711cd5SPawel Dembicki 		vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ARBITER, 0,
81995711cd5SPawel Dembicki 				    VSC73XX_ARBDISC, BIT(port), 0);
82095711cd5SPawel Dembicki 
82195711cd5SPawel Dembicki 		/* Allow backward dropping of frames from this port */
82295711cd5SPawel Dembicki 		vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ARBITER, 0,
82395711cd5SPawel Dembicki 				    VSC73XX_SBACKWDROP, BIT(port), BIT(port));
82495711cd5SPawel Dembicki 
82595711cd5SPawel Dembicki 		/* Receive mask (disable forwarding) */
82695711cd5SPawel Dembicki 		vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ANALYZER, 0,
82795711cd5SPawel Dembicki 				    VSC73XX_RECVMASK, BIT(port), 0);
82895711cd5SPawel Dembicki 
82995711cd5SPawel Dembicki 		return;
83095711cd5SPawel Dembicki 	}
83195711cd5SPawel Dembicki 
83295711cd5SPawel Dembicki 	/* Figure out what speed was negotiated */
83395711cd5SPawel Dembicki 	if (phydev->speed == SPEED_1000) {
83495711cd5SPawel Dembicki 		dev_dbg(vsc->dev, "port %d: 1000 Mbit mode full duplex\n",
83595711cd5SPawel Dembicki 			port);
83695711cd5SPawel Dembicki 
83795711cd5SPawel Dembicki 		/* Set up default for internal port or external RGMII */
83895711cd5SPawel Dembicki 		if (phydev->interface == PHY_INTERFACE_MODE_RGMII)
83995711cd5SPawel Dembicki 			val = VSC73XX_MAC_CFG_1000M_F_RGMII;
84095711cd5SPawel Dembicki 		else
84195711cd5SPawel Dembicki 			val = VSC73XX_MAC_CFG_1000M_F_PHY;
84295711cd5SPawel Dembicki 		vsc73xx_adjust_enable_port(vsc, port, phydev, val);
84395711cd5SPawel Dembicki 	} else if (phydev->speed == SPEED_100) {
84495711cd5SPawel Dembicki 		if (phydev->duplex == DUPLEX_FULL) {
84595711cd5SPawel Dembicki 			val = VSC73XX_MAC_CFG_100_10M_F_PHY;
84695711cd5SPawel Dembicki 			dev_dbg(vsc->dev,
84795711cd5SPawel Dembicki 				"port %d: 100 Mbit full duplex mode\n",
84895711cd5SPawel Dembicki 				port);
84995711cd5SPawel Dembicki 		} else {
85095711cd5SPawel Dembicki 			val = VSC73XX_MAC_CFG_100_10M_H_PHY;
85195711cd5SPawel Dembicki 			dev_dbg(vsc->dev,
85295711cd5SPawel Dembicki 				"port %d: 100 Mbit half duplex mode\n",
85395711cd5SPawel Dembicki 				port);
85495711cd5SPawel Dembicki 		}
85595711cd5SPawel Dembicki 		vsc73xx_adjust_enable_port(vsc, port, phydev, val);
85695711cd5SPawel Dembicki 	} else if (phydev->speed == SPEED_10) {
85795711cd5SPawel Dembicki 		if (phydev->duplex == DUPLEX_FULL) {
85895711cd5SPawel Dembicki 			val = VSC73XX_MAC_CFG_100_10M_F_PHY;
85995711cd5SPawel Dembicki 			dev_dbg(vsc->dev,
86095711cd5SPawel Dembicki 				"port %d: 10 Mbit full duplex mode\n",
86195711cd5SPawel Dembicki 				port);
86295711cd5SPawel Dembicki 		} else {
86395711cd5SPawel Dembicki 			val = VSC73XX_MAC_CFG_100_10M_H_PHY;
86495711cd5SPawel Dembicki 			dev_dbg(vsc->dev,
86595711cd5SPawel Dembicki 				"port %d: 10 Mbit half duplex mode\n",
86695711cd5SPawel Dembicki 				port);
86795711cd5SPawel Dembicki 		}
86895711cd5SPawel Dembicki 		vsc73xx_adjust_enable_port(vsc, port, phydev, val);
86995711cd5SPawel Dembicki 	} else {
87095711cd5SPawel Dembicki 		dev_err(vsc->dev,
87195711cd5SPawel Dembicki 			"could not adjust link: unknown speed\n");
87295711cd5SPawel Dembicki 	}
87395711cd5SPawel Dembicki 
87495711cd5SPawel Dembicki 	/* Enable port (forwarding) in the receieve mask */
87595711cd5SPawel Dembicki 	vsc73xx_update_bits(vsc, VSC73XX_BLOCK_ANALYZER, 0,
87695711cd5SPawel Dembicki 			    VSC73XX_RECVMASK, BIT(port), BIT(port));
87795711cd5SPawel Dembicki }
87895711cd5SPawel Dembicki 
87995711cd5SPawel Dembicki static int vsc73xx_port_enable(struct dsa_switch *ds, int port,
88095711cd5SPawel Dembicki 			       struct phy_device *phy)
88195711cd5SPawel Dembicki {
88295711cd5SPawel Dembicki 	struct vsc73xx *vsc = ds->priv;
88395711cd5SPawel Dembicki 
88495711cd5SPawel Dembicki 	dev_info(vsc->dev, "enable port %d\n", port);
88595711cd5SPawel Dembicki 	vsc73xx_init_port(vsc, port);
88695711cd5SPawel Dembicki 
88795711cd5SPawel Dembicki 	return 0;
88895711cd5SPawel Dembicki }
88995711cd5SPawel Dembicki 
89095711cd5SPawel Dembicki static void vsc73xx_port_disable(struct dsa_switch *ds, int port)
89195711cd5SPawel Dembicki {
89295711cd5SPawel Dembicki 	struct vsc73xx *vsc = ds->priv;
89395711cd5SPawel Dembicki 
89495711cd5SPawel Dembicki 	/* Just put the port into reset */
89595711cd5SPawel Dembicki 	vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port,
89695711cd5SPawel Dembicki 		      VSC73XX_MAC_CFG, VSC73XX_MAC_CFG_RESET);
89795711cd5SPawel Dembicki }
89895711cd5SPawel Dembicki 
89995711cd5SPawel Dembicki static const struct vsc73xx_counter *
90095711cd5SPawel Dembicki vsc73xx_find_counter(struct vsc73xx *vsc,
90195711cd5SPawel Dembicki 		     u8 counter,
90295711cd5SPawel Dembicki 		     bool tx)
90395711cd5SPawel Dembicki {
90495711cd5SPawel Dembicki 	const struct vsc73xx_counter *cnts;
90595711cd5SPawel Dembicki 	int num_cnts;
90695711cd5SPawel Dembicki 	int i;
90795711cd5SPawel Dembicki 
90895711cd5SPawel Dembicki 	if (tx) {
90995711cd5SPawel Dembicki 		cnts = vsc73xx_tx_counters;
91095711cd5SPawel Dembicki 		num_cnts = ARRAY_SIZE(vsc73xx_tx_counters);
91195711cd5SPawel Dembicki 	} else {
91295711cd5SPawel Dembicki 		cnts = vsc73xx_rx_counters;
91395711cd5SPawel Dembicki 		num_cnts = ARRAY_SIZE(vsc73xx_rx_counters);
91495711cd5SPawel Dembicki 	}
91595711cd5SPawel Dembicki 
91695711cd5SPawel Dembicki 	for (i = 0; i < num_cnts; i++) {
91795711cd5SPawel Dembicki 		const struct vsc73xx_counter *cnt;
91895711cd5SPawel Dembicki 
91995711cd5SPawel Dembicki 		cnt = &cnts[i];
92095711cd5SPawel Dembicki 		if (cnt->counter == counter)
92195711cd5SPawel Dembicki 			return cnt;
92295711cd5SPawel Dembicki 	}
92395711cd5SPawel Dembicki 
92495711cd5SPawel Dembicki 	return NULL;
92595711cd5SPawel Dembicki }
92695711cd5SPawel Dembicki 
92795711cd5SPawel Dembicki static void vsc73xx_get_strings(struct dsa_switch *ds, int port, u32 stringset,
92895711cd5SPawel Dembicki 				uint8_t *data)
92995711cd5SPawel Dembicki {
93095711cd5SPawel Dembicki 	const struct vsc73xx_counter *cnt;
93195711cd5SPawel Dembicki 	struct vsc73xx *vsc = ds->priv;
93295711cd5SPawel Dembicki 	u8 indices[6];
933e3bbab47SJustin Stitt 	u8 *buf = data;
934e3bbab47SJustin Stitt 	int i;
93595711cd5SPawel Dembicki 	u32 val;
93695711cd5SPawel Dembicki 	int ret;
93795711cd5SPawel Dembicki 
93895711cd5SPawel Dembicki 	if (stringset != ETH_SS_STATS)
93995711cd5SPawel Dembicki 		return;
94095711cd5SPawel Dembicki 
94195711cd5SPawel Dembicki 	ret = vsc73xx_read(vsc, VSC73XX_BLOCK_MAC, port,
94295711cd5SPawel Dembicki 			   VSC73XX_C_CFG, &val);
94395711cd5SPawel Dembicki 	if (ret)
94495711cd5SPawel Dembicki 		return;
94595711cd5SPawel Dembicki 
94695711cd5SPawel Dembicki 	indices[0] = (val & 0x1f); /* RX counter 0 */
94795711cd5SPawel Dembicki 	indices[1] = ((val >> 5) & 0x1f); /* RX counter 1 */
94895711cd5SPawel Dembicki 	indices[2] = ((val >> 10) & 0x1f); /* RX counter 2 */
94995711cd5SPawel Dembicki 	indices[3] = ((val >> 16) & 0x1f); /* TX counter 0 */
95095711cd5SPawel Dembicki 	indices[4] = ((val >> 21) & 0x1f); /* TX counter 1 */
95195711cd5SPawel Dembicki 	indices[5] = ((val >> 26) & 0x1f); /* TX counter 2 */
95295711cd5SPawel Dembicki 
95395711cd5SPawel Dembicki 	/* The first counters is the RX octets */
954e403cfffSjustinstitt@google.com 	ethtool_puts(&buf, "RxEtherStatsOctets");
95595711cd5SPawel Dembicki 
95695711cd5SPawel Dembicki 	/* Each port supports recording 3 RX counters and 3 TX counters,
95795711cd5SPawel Dembicki 	 * figure out what counters we use in this set-up and return the
95895711cd5SPawel Dembicki 	 * names of them. The hardware default counters will be number of
95995711cd5SPawel Dembicki 	 * packets on RX/TX, combined broadcast+multicast packets RX/TX and
96095711cd5SPawel Dembicki 	 * total error packets RX/TX.
96195711cd5SPawel Dembicki 	 */
96295711cd5SPawel Dembicki 	for (i = 0; i < 3; i++) {
96395711cd5SPawel Dembicki 		cnt = vsc73xx_find_counter(vsc, indices[i], false);
964e403cfffSjustinstitt@google.com 		ethtool_puts(&buf, cnt ? cnt->name : "");
96595711cd5SPawel Dembicki 	}
96695711cd5SPawel Dembicki 
96795711cd5SPawel Dembicki 	/* TX stats begins with the number of TX octets */
968e403cfffSjustinstitt@google.com 	ethtool_puts(&buf, "TxEtherStatsOctets");
96995711cd5SPawel Dembicki 
97095711cd5SPawel Dembicki 	for (i = 3; i < 6; i++) {
97195711cd5SPawel Dembicki 		cnt = vsc73xx_find_counter(vsc, indices[i], true);
972e403cfffSjustinstitt@google.com 		ethtool_puts(&buf, cnt ? cnt->name : "");
973e3bbab47SJustin Stitt 
97495711cd5SPawel Dembicki 	}
97595711cd5SPawel Dembicki }
97695711cd5SPawel Dembicki 
97795711cd5SPawel Dembicki static int vsc73xx_get_sset_count(struct dsa_switch *ds, int port, int sset)
97895711cd5SPawel Dembicki {
97995711cd5SPawel Dembicki 	/* We only support SS_STATS */
98095711cd5SPawel Dembicki 	if (sset != ETH_SS_STATS)
98195711cd5SPawel Dembicki 		return 0;
98295711cd5SPawel Dembicki 	/* RX and TX packets, then 3 RX counters, 3 TX counters */
98395711cd5SPawel Dembicki 	return 8;
98495711cd5SPawel Dembicki }
98595711cd5SPawel Dembicki 
98695711cd5SPawel Dembicki static void vsc73xx_get_ethtool_stats(struct dsa_switch *ds, int port,
98795711cd5SPawel Dembicki 				      uint64_t *data)
98895711cd5SPawel Dembicki {
98995711cd5SPawel Dembicki 	struct vsc73xx *vsc = ds->priv;
99095711cd5SPawel Dembicki 	u8 regs[] = {
99195711cd5SPawel Dembicki 		VSC73XX_RXOCT,
99295711cd5SPawel Dembicki 		VSC73XX_C_RX0,
99395711cd5SPawel Dembicki 		VSC73XX_C_RX1,
99495711cd5SPawel Dembicki 		VSC73XX_C_RX2,
99595711cd5SPawel Dembicki 		VSC73XX_TXOCT,
99695711cd5SPawel Dembicki 		VSC73XX_C_TX0,
99795711cd5SPawel Dembicki 		VSC73XX_C_TX1,
99895711cd5SPawel Dembicki 		VSC73XX_C_TX2,
99995711cd5SPawel Dembicki 	};
100095711cd5SPawel Dembicki 	u32 val;
100195711cd5SPawel Dembicki 	int ret;
100295711cd5SPawel Dembicki 	int i;
100395711cd5SPawel Dembicki 
100495711cd5SPawel Dembicki 	for (i = 0; i < ARRAY_SIZE(regs); i++) {
100595711cd5SPawel Dembicki 		ret = vsc73xx_read(vsc, VSC73XX_BLOCK_MAC, port,
100695711cd5SPawel Dembicki 				   regs[i], &val);
100795711cd5SPawel Dembicki 		if (ret) {
100895711cd5SPawel Dembicki 			dev_err(vsc->dev, "error reading counter %d\n", i);
100995711cd5SPawel Dembicki 			return;
101095711cd5SPawel Dembicki 		}
101195711cd5SPawel Dembicki 		data[i] = val;
101295711cd5SPawel Dembicki 	}
101395711cd5SPawel Dembicki }
101495711cd5SPawel Dembicki 
1015fb77ffc6SVladimir Oltean static int vsc73xx_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
1016fb77ffc6SVladimir Oltean {
1017fb77ffc6SVladimir Oltean 	struct vsc73xx *vsc = ds->priv;
1018fb77ffc6SVladimir Oltean 
1019fb77ffc6SVladimir Oltean 	return vsc73xx_write(vsc, VSC73XX_BLOCK_MAC, port,
10203cf62c81SPawel Dembicki 			     VSC73XX_MAXLEN, new_mtu + ETH_HLEN + ETH_FCS_LEN);
1021fb77ffc6SVladimir Oltean }
1022fb77ffc6SVladimir Oltean 
1023fb77ffc6SVladimir Oltean /* According to application not "VSC7398 Jumbo Frames" setting
10243cf62c81SPawel Dembicki  * up the frame size to 9.6 KB does not affect the performance on standard
1025fb77ffc6SVladimir Oltean  * frames. It is clear from the application note that
1026fb77ffc6SVladimir Oltean  * "9.6 kilobytes" == 9600 bytes.
1027fb77ffc6SVladimir Oltean  */
1028fb77ffc6SVladimir Oltean static int vsc73xx_get_max_mtu(struct dsa_switch *ds, int port)
1029fb77ffc6SVladimir Oltean {
10303cf62c81SPawel Dembicki 	return 9600 - ETH_HLEN - ETH_FCS_LEN;
1031fb77ffc6SVladimir Oltean }
1032fb77ffc6SVladimir Oltean 
1033a026809cSRussell King (Oracle) static void vsc73xx_phylink_get_caps(struct dsa_switch *dsa, int port,
1034a026809cSRussell King (Oracle) 				     struct phylink_config *config)
1035a026809cSRussell King (Oracle) {
1036a026809cSRussell King (Oracle) 	unsigned long *interfaces = config->supported_interfaces;
1037a026809cSRussell King (Oracle) 
1038a026809cSRussell King (Oracle) 	if (port == 5)
1039a026809cSRussell King (Oracle) 		return;
1040a026809cSRussell King (Oracle) 
1041a026809cSRussell King (Oracle) 	if (port == CPU_PORT) {
1042a026809cSRussell King (Oracle) 		__set_bit(PHY_INTERFACE_MODE_MII, interfaces);
1043a026809cSRussell King (Oracle) 		__set_bit(PHY_INTERFACE_MODE_REVMII, interfaces);
1044a026809cSRussell King (Oracle) 		__set_bit(PHY_INTERFACE_MODE_GMII, interfaces);
1045a026809cSRussell King (Oracle) 		__set_bit(PHY_INTERFACE_MODE_RGMII, interfaces);
1046a026809cSRussell King (Oracle) 	}
1047a026809cSRussell King (Oracle) 
1048a026809cSRussell King (Oracle) 	if (port <= 4) {
1049a026809cSRussell King (Oracle) 		/* Internal PHYs */
1050a026809cSRussell King (Oracle) 		__set_bit(PHY_INTERFACE_MODE_INTERNAL, interfaces);
1051a026809cSRussell King (Oracle) 		/* phylib default */
1052a026809cSRussell King (Oracle) 		__set_bit(PHY_INTERFACE_MODE_GMII, interfaces);
1053a026809cSRussell King (Oracle) 	}
1054a026809cSRussell King (Oracle) 
1055a026809cSRussell King (Oracle) 	config->mac_capabilities = MAC_SYM_PAUSE | MAC_10 | MAC_100 | MAC_1000;
1056a026809cSRussell King (Oracle) }
1057a026809cSRussell King (Oracle) 
105895711cd5SPawel Dembicki static const struct dsa_switch_ops vsc73xx_ds_ops = {
105995711cd5SPawel Dembicki 	.get_tag_protocol = vsc73xx_get_tag_protocol,
106095711cd5SPawel Dembicki 	.setup = vsc73xx_setup,
106195711cd5SPawel Dembicki 	.phy_read = vsc73xx_phy_read,
106295711cd5SPawel Dembicki 	.phy_write = vsc73xx_phy_write,
106395711cd5SPawel Dembicki 	.adjust_link = vsc73xx_adjust_link,
106495711cd5SPawel Dembicki 	.get_strings = vsc73xx_get_strings,
106595711cd5SPawel Dembicki 	.get_ethtool_stats = vsc73xx_get_ethtool_stats,
106695711cd5SPawel Dembicki 	.get_sset_count = vsc73xx_get_sset_count,
106795711cd5SPawel Dembicki 	.port_enable = vsc73xx_port_enable,
106895711cd5SPawel Dembicki 	.port_disable = vsc73xx_port_disable,
1069fb77ffc6SVladimir Oltean 	.port_change_mtu = vsc73xx_change_mtu,
1070fb77ffc6SVladimir Oltean 	.port_max_mtu = vsc73xx_get_max_mtu,
1071a026809cSRussell King (Oracle) 	.phylink_get_caps = vsc73xx_phylink_get_caps,
107295711cd5SPawel Dembicki };
107395711cd5SPawel Dembicki 
107495711cd5SPawel Dembicki static int vsc73xx_gpio_get(struct gpio_chip *chip, unsigned int offset)
107595711cd5SPawel Dembicki {
107695711cd5SPawel Dembicki 	struct vsc73xx *vsc = gpiochip_get_data(chip);
107795711cd5SPawel Dembicki 	u32 val;
107895711cd5SPawel Dembicki 	int ret;
107995711cd5SPawel Dembicki 
108095711cd5SPawel Dembicki 	ret = vsc73xx_read(vsc, VSC73XX_BLOCK_SYSTEM, 0,
108195711cd5SPawel Dembicki 			   VSC73XX_GPIO, &val);
108295711cd5SPawel Dembicki 	if (ret)
108395711cd5SPawel Dembicki 		return ret;
108495711cd5SPawel Dembicki 
108595711cd5SPawel Dembicki 	return !!(val & BIT(offset));
108695711cd5SPawel Dembicki }
108795711cd5SPawel Dembicki 
108895711cd5SPawel Dembicki static void vsc73xx_gpio_set(struct gpio_chip *chip, unsigned int offset,
108995711cd5SPawel Dembicki 			     int val)
109095711cd5SPawel Dembicki {
109195711cd5SPawel Dembicki 	struct vsc73xx *vsc = gpiochip_get_data(chip);
109295711cd5SPawel Dembicki 	u32 tmp = val ? BIT(offset) : 0;
109395711cd5SPawel Dembicki 
109495711cd5SPawel Dembicki 	vsc73xx_update_bits(vsc, VSC73XX_BLOCK_SYSTEM, 0,
109595711cd5SPawel Dembicki 			    VSC73XX_GPIO, BIT(offset), tmp);
109695711cd5SPawel Dembicki }
109795711cd5SPawel Dembicki 
109895711cd5SPawel Dembicki static int vsc73xx_gpio_direction_output(struct gpio_chip *chip,
109995711cd5SPawel Dembicki 					 unsigned int offset, int val)
110095711cd5SPawel Dembicki {
110195711cd5SPawel Dembicki 	struct vsc73xx *vsc = gpiochip_get_data(chip);
110295711cd5SPawel Dembicki 	u32 tmp = val ? BIT(offset) : 0;
110395711cd5SPawel Dembicki 
110495711cd5SPawel Dembicki 	return vsc73xx_update_bits(vsc, VSC73XX_BLOCK_SYSTEM, 0,
110595711cd5SPawel Dembicki 				   VSC73XX_GPIO, BIT(offset + 4) | BIT(offset),
110695711cd5SPawel Dembicki 				   BIT(offset + 4) | tmp);
110795711cd5SPawel Dembicki }
110895711cd5SPawel Dembicki 
110995711cd5SPawel Dembicki static int vsc73xx_gpio_direction_input(struct gpio_chip *chip,
111095711cd5SPawel Dembicki 					unsigned int offset)
111195711cd5SPawel Dembicki {
111295711cd5SPawel Dembicki 	struct vsc73xx *vsc = gpiochip_get_data(chip);
111395711cd5SPawel Dembicki 
111495711cd5SPawel Dembicki 	return  vsc73xx_update_bits(vsc, VSC73XX_BLOCK_SYSTEM, 0,
111595711cd5SPawel Dembicki 				    VSC73XX_GPIO, BIT(offset + 4),
111695711cd5SPawel Dembicki 				    0);
111795711cd5SPawel Dembicki }
111895711cd5SPawel Dembicki 
111995711cd5SPawel Dembicki static int vsc73xx_gpio_get_direction(struct gpio_chip *chip,
112095711cd5SPawel Dembicki 				      unsigned int offset)
112195711cd5SPawel Dembicki {
112295711cd5SPawel Dembicki 	struct vsc73xx *vsc = gpiochip_get_data(chip);
112395711cd5SPawel Dembicki 	u32 val;
112495711cd5SPawel Dembicki 	int ret;
112595711cd5SPawel Dembicki 
112695711cd5SPawel Dembicki 	ret = vsc73xx_read(vsc, VSC73XX_BLOCK_SYSTEM, 0,
112795711cd5SPawel Dembicki 			   VSC73XX_GPIO, &val);
112895711cd5SPawel Dembicki 	if (ret)
112995711cd5SPawel Dembicki 		return ret;
113095711cd5SPawel Dembicki 
113195711cd5SPawel Dembicki 	return !(val & BIT(offset + 4));
113295711cd5SPawel Dembicki }
113395711cd5SPawel Dembicki 
113495711cd5SPawel Dembicki static int vsc73xx_gpio_probe(struct vsc73xx *vsc)
113595711cd5SPawel Dembicki {
113695711cd5SPawel Dembicki 	int ret;
113795711cd5SPawel Dembicki 
113895711cd5SPawel Dembicki 	vsc->gc.label = devm_kasprintf(vsc->dev, GFP_KERNEL, "VSC%04x",
113995711cd5SPawel Dembicki 				       vsc->chipid);
1140776dac5aSKunwu Chan 	if (!vsc->gc.label)
1141776dac5aSKunwu Chan 		return -ENOMEM;
114295711cd5SPawel Dembicki 	vsc->gc.ngpio = 4;
114395711cd5SPawel Dembicki 	vsc->gc.owner = THIS_MODULE;
114495711cd5SPawel Dembicki 	vsc->gc.parent = vsc->dev;
114595711cd5SPawel Dembicki 	vsc->gc.base = -1;
114695711cd5SPawel Dembicki 	vsc->gc.get = vsc73xx_gpio_get;
114795711cd5SPawel Dembicki 	vsc->gc.set = vsc73xx_gpio_set;
114895711cd5SPawel Dembicki 	vsc->gc.direction_input = vsc73xx_gpio_direction_input;
114995711cd5SPawel Dembicki 	vsc->gc.direction_output = vsc73xx_gpio_direction_output;
115095711cd5SPawel Dembicki 	vsc->gc.get_direction = vsc73xx_gpio_get_direction;
115195711cd5SPawel Dembicki 	vsc->gc.can_sleep = true;
115295711cd5SPawel Dembicki 	ret = devm_gpiochip_add_data(vsc->dev, &vsc->gc, vsc);
115395711cd5SPawel Dembicki 	if (ret) {
115495711cd5SPawel Dembicki 		dev_err(vsc->dev, "unable to register GPIO chip\n");
115595711cd5SPawel Dembicki 		return ret;
115695711cd5SPawel Dembicki 	}
115795711cd5SPawel Dembicki 	return 0;
115895711cd5SPawel Dembicki }
115995711cd5SPawel Dembicki 
116095711cd5SPawel Dembicki int vsc73xx_probe(struct vsc73xx *vsc)
116195711cd5SPawel Dembicki {
116295711cd5SPawel Dembicki 	struct device *dev = vsc->dev;
116395711cd5SPawel Dembicki 	int ret;
116495711cd5SPawel Dembicki 
116595711cd5SPawel Dembicki 	/* Release reset, if any */
116695711cd5SPawel Dembicki 	vsc->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
116795711cd5SPawel Dembicki 	if (IS_ERR(vsc->reset)) {
116895711cd5SPawel Dembicki 		dev_err(dev, "failed to get RESET GPIO\n");
116995711cd5SPawel Dembicki 		return PTR_ERR(vsc->reset);
117095711cd5SPawel Dembicki 	}
117195711cd5SPawel Dembicki 	if (vsc->reset)
117295711cd5SPawel Dembicki 		/* Wait 20ms according to datasheet table 245 */
117395711cd5SPawel Dembicki 		msleep(20);
117495711cd5SPawel Dembicki 
117595711cd5SPawel Dembicki 	ret = vsc73xx_detect(vsc);
11761da39ff0SPawel Dembicki 	if (ret == -EAGAIN) {
11771da39ff0SPawel Dembicki 		dev_err(vsc->dev,
11781da39ff0SPawel Dembicki 			"Chip seems to be out of control. Assert reset and try again.\n");
11791da39ff0SPawel Dembicki 		gpiod_set_value_cansleep(vsc->reset, 1);
11801da39ff0SPawel Dembicki 		/* Reset pulse should be 20ns minimum, according to datasheet
11811da39ff0SPawel Dembicki 		 * table 245, so 10us should be fine
11821da39ff0SPawel Dembicki 		 */
11831da39ff0SPawel Dembicki 		usleep_range(10, 100);
11841da39ff0SPawel Dembicki 		gpiod_set_value_cansleep(vsc->reset, 0);
11851da39ff0SPawel Dembicki 		/* Wait 20ms according to datasheet table 245 */
11861da39ff0SPawel Dembicki 		msleep(20);
11871da39ff0SPawel Dembicki 		ret = vsc73xx_detect(vsc);
11881da39ff0SPawel Dembicki 	}
118995711cd5SPawel Dembicki 	if (ret) {
119095711cd5SPawel Dembicki 		dev_err(dev, "no chip found (%d)\n", ret);
119195711cd5SPawel Dembicki 		return -ENODEV;
119295711cd5SPawel Dembicki 	}
119395711cd5SPawel Dembicki 
119495711cd5SPawel Dembicki 	eth_random_addr(vsc->addr);
119595711cd5SPawel Dembicki 	dev_info(vsc->dev,
119695711cd5SPawel Dembicki 		 "MAC for control frames: %02X:%02X:%02X:%02X:%02X:%02X\n",
119795711cd5SPawel Dembicki 		 vsc->addr[0], vsc->addr[1], vsc->addr[2],
119895711cd5SPawel Dembicki 		 vsc->addr[3], vsc->addr[4], vsc->addr[5]);
119995711cd5SPawel Dembicki 
120095711cd5SPawel Dembicki 	/* The VSC7395 switch chips have 5+1 ports which means 5
120195711cd5SPawel Dembicki 	 * ordinary ports and a sixth CPU port facing the processor
120295711cd5SPawel Dembicki 	 * with an RGMII interface. These ports are numbered 0..4
120395711cd5SPawel Dembicki 	 * and 6, so they leave a "hole" in the port map for port 5,
120495711cd5SPawel Dembicki 	 * which is invalid.
120595711cd5SPawel Dembicki 	 *
120695711cd5SPawel Dembicki 	 * The VSC7398 has 8 ports, port 7 is again the CPU port.
120795711cd5SPawel Dembicki 	 *
120895711cd5SPawel Dembicki 	 * We allocate 8 ports and avoid access to the nonexistant
120995711cd5SPawel Dembicki 	 * ports.
121095711cd5SPawel Dembicki 	 */
12117e99e347SVivien Didelot 	vsc->ds = devm_kzalloc(dev, sizeof(*vsc->ds), GFP_KERNEL);
121295711cd5SPawel Dembicki 	if (!vsc->ds)
121395711cd5SPawel Dembicki 		return -ENOMEM;
12147e99e347SVivien Didelot 
12157e99e347SVivien Didelot 	vsc->ds->dev = dev;
12167e99e347SVivien Didelot 	vsc->ds->num_ports = 8;
121795711cd5SPawel Dembicki 	vsc->ds->priv = vsc;
121895711cd5SPawel Dembicki 
121995711cd5SPawel Dembicki 	vsc->ds->ops = &vsc73xx_ds_ops;
122095711cd5SPawel Dembicki 	ret = dsa_register_switch(vsc->ds);
122195711cd5SPawel Dembicki 	if (ret) {
122295711cd5SPawel Dembicki 		dev_err(dev, "unable to register switch (%d)\n", ret);
122395711cd5SPawel Dembicki 		return ret;
122495711cd5SPawel Dembicki 	}
122595711cd5SPawel Dembicki 
122695711cd5SPawel Dembicki 	ret = vsc73xx_gpio_probe(vsc);
122795711cd5SPawel Dembicki 	if (ret) {
122895711cd5SPawel Dembicki 		dsa_unregister_switch(vsc->ds);
122995711cd5SPawel Dembicki 		return ret;
123095711cd5SPawel Dembicki 	}
123195711cd5SPawel Dembicki 
123295711cd5SPawel Dembicki 	return 0;
123395711cd5SPawel Dembicki }
123495711cd5SPawel Dembicki EXPORT_SYMBOL(vsc73xx_probe);
123595711cd5SPawel Dembicki 
1236e99fa423SUwe Kleine-König void vsc73xx_remove(struct vsc73xx *vsc)
123795711cd5SPawel Dembicki {
123895711cd5SPawel Dembicki 	dsa_unregister_switch(vsc->ds);
123995711cd5SPawel Dembicki 	gpiod_set_value(vsc->reset, 1);
124095711cd5SPawel Dembicki }
124195711cd5SPawel Dembicki EXPORT_SYMBOL(vsc73xx_remove);
124295711cd5SPawel Dembicki 
12430650bf52SVladimir Oltean void vsc73xx_shutdown(struct vsc73xx *vsc)
12440650bf52SVladimir Oltean {
12450650bf52SVladimir Oltean 	dsa_switch_shutdown(vsc->ds);
12460650bf52SVladimir Oltean }
12470650bf52SVladimir Oltean EXPORT_SYMBOL(vsc73xx_shutdown);
12480650bf52SVladimir Oltean 
124995711cd5SPawel Dembicki MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
125095711cd5SPawel Dembicki MODULE_DESCRIPTION("Vitesse VSC7385/7388/7395/7398 driver");
125195711cd5SPawel Dembicki MODULE_LICENSE("GPL v2");
1252