2 * Copyright (c) 2016 Stanislav Galabov.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #ifndef __MTKSWITCHVAR_H__
30 #define __MTKSWITCHVAR_H__
42 #define MTK_IS_SWITCH(_sc, _type) \
43 (!!((_sc)->sc_switchtype == MTK_SWITCH_ ## _type))
45 #define MTKSWITCH_MAX_PORTS 7
46 #define MTKSWITCH_MAX_PHYS 7
47 #define MTKSWITCH_CPU_PORT 6
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)
58 struct mtkswitch_softc {
61 struct resource *sc_res;
68 mtk_switch_type sc_switchtype;
69 char *ifname[MTKSWITCH_MAX_PHYS];
70 device_t miibus[MTKSWITCH_MAX_PHYS];
71 struct ifnet *ifp[MTKSWITCH_MAX_PHYS];
72 struct callout callout_tick;
73 etherswitch_info_t info;
79 int (* mtkswitch_reset) (struct mtkswitch_softc *);
80 int (* mtkswitch_hw_setup) (struct mtkswitch_softc *);
81 int (* mtkswitch_hw_global_setup) (struct mtkswitch_softc *);
84 void (* mtkswitch_port_init) (struct mtkswitch_softc *, int);
85 uint32_t (* mtkswitch_get_port_status)
86 (struct mtkswitch_softc *, int);
89 int (* mtkswitch_atu_flush) (struct mtkswitch_softc *);
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 *,
103 int (* mtkswitch_vlan_set_pvid) (struct mtkswitch_softc *,
107 int (* mtkswitch_phy_read) (device_t, int, int);
108 int (* mtkswitch_phy_write) (device_t, int, int, int);
110 /* Register functions */
111 int (* mtkswitch_reg_read) (device_t, int);
112 int (* mtkswitch_reg_write) (device_t, int, int);
114 /* Internal register access functions */
115 uint32_t (* mtkswitch_read) (struct mtkswitch_softc *, int);
116 uint32_t (* mtkswitch_write) (struct mtkswitch_softc *, int,
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)
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))
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
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); \
152 #define DEBUG_INCRVAR(var) do { \
156 #define DPRINTF(dev, args...)
157 #define DEVERR(dev, err, fmt, args...)
158 #define DEBUG_INCRVAR(var)
161 extern void mtk_attach_switch_rt3050(struct mtkswitch_softc *);
162 extern void mtk_attach_switch_mt7620(struct mtkswitch_softc *);
164 #endif /* __MTKSWITCHVAR_H__ */