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