1 /*-
2  * Copyright (c) 2016 Stanislav Galabov.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #ifndef	__MTKSWITCHVAR_H__
30 #define	__MTKSWITCHVAR_H__
31 
32 typedef enum {
33 	MTK_SWITCH_NONE,
34 	MTK_SWITCH_RT3050,
35 	MTK_SWITCH_RT3352,
36 	MTK_SWITCH_RT5350,
37 	MTK_SWITCH_MT7620,
38 	MTK_SWITCH_MT7621,
39 	MTK_SWITCH_MT7628,
40 } mtk_switch_type;
41 
42 #define	MTK_IS_SWITCH(_sc, _type)		\
43 	    (!!((_sc)->sc_switchtype == MTK_SWITCH_ ## _type))
44 
45 #define	MTKSWITCH_MAX_PORTS	7
46 #define MTKSWITCH_MAX_PHYS	7
47 #define	MTKSWITCH_CPU_PORT	6
48 
49 #define	MTKSWITCH_LINK_UP	(1<<0)
50 #define	MTKSWITCH_SPEED_MASK	(3<<1)
51 #define	MTKSWITCH_SPEED_10	(0<<1)
52 #define MTKSWITCH_SPEED_100	(1<<1)
53 #define	MTKSWITCH_SPEED_1000	(2<<1)
54 #define	MTKSWITCH_DUPLEX	(1<<3)
55 #define MTKSWITCH_TXFLOW	(1<<4)
56 #define MTKSWITCH_RXFLOW	(1<<5)
57 
58 struct mtkswitch_softc {
59 	struct mtx	sc_mtx;
60 	device_t	sc_dev;
61 	struct resource *sc_res;
62 	int		numphys;
63 	uint32_t	phymap;
64 	int		numports;
65 	uint32_t	portmap;
66 	int		cpuport;
67 	uint32_t	valid_vlans;
68 	mtk_switch_type	sc_switchtype;
69 	char		*ifname[MTKSWITCH_MAX_PHYS];
70 	device_t	miibus[MTKSWITCH_MAX_PHYS];
71 	if_t ifp[MTKSWITCH_MAX_PHYS];
72 	struct callout	callout_tick;
73 	etherswitch_info_t info;
74 
75 	uint32_t	vlan_mode;
76 
77 	struct {
78 		/* Global setup */
79 		int (* mtkswitch_reset) (struct mtkswitch_softc *);
80 		int (* mtkswitch_hw_setup) (struct mtkswitch_softc *);
81 		int (* mtkswitch_hw_global_setup) (struct mtkswitch_softc *);
82 
83 		/* Port functions */
84 		void (* mtkswitch_port_init) (struct mtkswitch_softc *, int);
85 		uint32_t (* mtkswitch_get_port_status)
86 		    (struct mtkswitch_softc *, int);
87 
88 		/* ATU functions */
89 		int (* mtkswitch_atu_flush) (struct mtkswitch_softc *);
90 
91 		/* VLAN functions */
92 		int (* mtkswitch_port_vlan_setup) (struct mtkswitch_softc *,
93 		    etherswitch_port_t *);
94 		int (* mtkswitch_port_vlan_get) (struct mtkswitch_softc *,
95 		    etherswitch_port_t *);
96 		void (* mtkswitch_vlan_init_hw) (struct mtkswitch_softc *);
97 		int (* mtkswitch_vlan_getvgroup) (struct mtkswitch_softc *,
98 		    etherswitch_vlangroup_t *);
99 		int (* mtkswitch_vlan_setvgroup) (struct mtkswitch_softc *,
100 		    etherswitch_vlangroup_t *);
101 		int (* mtkswitch_vlan_get_pvid) (struct mtkswitch_softc *,
102 		    int, int *);
103 		int (* mtkswitch_vlan_set_pvid) (struct mtkswitch_softc *,
104 		    int, int);
105 
106 		/* PHY functions */
107 		int (* mtkswitch_phy_read) (device_t, int, int);
108 		int (* mtkswitch_phy_write) (device_t, int, int, int);
109 
110 		/* Register functions */
111 		int (* mtkswitch_reg_read) (device_t, int);
112 		int (* mtkswitch_reg_write) (device_t, int, int);
113 
114 		/* Internal register access functions */
115 		uint32_t (* mtkswitch_read) (struct mtkswitch_softc *, int);
116 		uint32_t (* mtkswitch_write) (struct mtkswitch_softc *, int,
117 		    uint32_t);
118 	} hal;
119 };
120 
121 #define	MTKSWITCH_LOCK(_sc)			\
122 	    mtx_lock(&(_sc)->sc_mtx)
123 #define	MTKSWITCH_UNLOCK(_sc)			\
124 	    mtx_unlock(&(_sc)->sc_mtx)
125 #define	MTKSWITCH_LOCK_ASSERT(_sc, _what)	\
126 	    mtx_assert(&(_sc)->sc_mtx, (_what))
127 #define	MTKSWITCH_TRYLOCK(_sc)			\
128 	    mtx_trylock(&(_sc)->sc_mtx)
129 
130 #define	MTKSWITCH_READ(_sc, _reg)		\
131 	    bus_read_4((_sc)->sc_res, (_reg))
132 #define MTKSWITCH_WRITE(_sc, _reg, _val)	\
133 	    bus_write_4((_sc)->sc_res, (_reg), (_val))
134 #define	MTKSWITCH_MOD(_sc, _reg, _clr, _set)	\
135 	    MTKSWITCH_WRITE((_sc), (_reg),	\
136 	        ((MTKSWITCH_READ((_sc), (_reg)) & ~(_clr)) | (_set))
137 
138 #define	MTKSWITCH_REG32(addr)	((addr) & ~(0x3))
139 #define	MTKSWITCH_IS_HI16(addr)	(((addr) & 0x3) > 0x1)
140 #define	MTKSWITCH_HI16(x)	(((x) >> 16) & 0xffff)
141 #define	MTKSWITCH_LO16(x)	((x) & 0xffff)
142 #define	MTKSWITCH_TO_HI16(x)	(((x) & 0xffff) << 16)
143 #define	MTKSWITCH_TO_LO16(x)	((x) & 0xffff)
144 #define	MTKSWITCH_HI16_MSK	0xffff0000
145 #define MTKSWITCH_LO16_MSK	0x0000ffff
146 
147 #if defined(DEBUG)
148 #define	DPRINTF(dev, args...)	device_printf(dev, args)
149 #define	DEVERR(dev, err, fmt, args...)	do {	\
150 	    if (err != 0) device_printf(dev, fmt, err, args);	\
151 	} while (0)
152 #define	DEBUG_INCRVAR(var)		do {	\
153 	    var++;				\
154 	} while (0)
155 #else
156 #define	DPRINTF(dev, args...)
157 #define	DEVERR(dev, err, fmt, args...)
158 #define	DEBUG_INCRVAR(var)
159 #endif
160 
161 extern void mtk_attach_switch_rt3050(struct mtkswitch_softc *);
162 extern void mtk_attach_switch_mt7620(struct mtkswitch_softc *);
163 
164 #endif	/* __MTKSWITCHVAR_H__ */
165