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 27 #ifndef __MTKSWITCHVAR_H__ 28 #define __MTKSWITCHVAR_H__ 29 30 typedef enum { 31 MTK_SWITCH_NONE, 32 MTK_SWITCH_RT3050, 33 MTK_SWITCH_RT3352, 34 MTK_SWITCH_RT5350, 35 MTK_SWITCH_MT7620, 36 MTK_SWITCH_MT7621, 37 MTK_SWITCH_MT7628, 38 } mtk_switch_type; 39 40 #define MTK_IS_SWITCH(_sc, _type) \ 41 (!!((_sc)->sc_switchtype == MTK_SWITCH_ ## _type)) 42 43 #define MTKSWITCH_MAX_PORTS 7 44 #define MTKSWITCH_MAX_PHYS 7 45 #define MTKSWITCH_CPU_PORT 6 46 47 #define MTKSWITCH_LINK_UP (1<<0) 48 #define MTKSWITCH_SPEED_MASK (3<<1) 49 #define MTKSWITCH_SPEED_10 (0<<1) 50 #define MTKSWITCH_SPEED_100 (1<<1) 51 #define MTKSWITCH_SPEED_1000 (2<<1) 52 #define MTKSWITCH_DUPLEX (1<<3) 53 #define MTKSWITCH_TXFLOW (1<<4) 54 #define MTKSWITCH_RXFLOW (1<<5) 55 56 struct mtkswitch_softc { 57 struct mtx sc_mtx; 58 device_t sc_dev; 59 struct resource *sc_res; 60 int numphys; 61 uint32_t phymap; 62 int numports; 63 uint32_t portmap; 64 int cpuport; 65 uint32_t valid_vlans; 66 mtk_switch_type sc_switchtype; 67 char *ifname[MTKSWITCH_MAX_PHYS]; 68 device_t miibus[MTKSWITCH_MAX_PHYS]; 69 if_t ifp[MTKSWITCH_MAX_PHYS]; 70 struct callout callout_tick; 71 etherswitch_info_t info; 72 73 uint32_t vlan_mode; 74 75 struct { 76 /* Global setup */ 77 int (* mtkswitch_reset) (struct mtkswitch_softc *); 78 int (* mtkswitch_hw_setup) (struct mtkswitch_softc *); 79 int (* mtkswitch_hw_global_setup) (struct mtkswitch_softc *); 80 81 /* Port functions */ 82 void (* mtkswitch_port_init) (struct mtkswitch_softc *, int); 83 uint32_t (* mtkswitch_get_port_status) 84 (struct mtkswitch_softc *, int); 85 86 /* ATU functions */ 87 int (* mtkswitch_atu_flush) (struct mtkswitch_softc *); 88 89 /* VLAN functions */ 90 int (* mtkswitch_port_vlan_setup) (struct mtkswitch_softc *, 91 etherswitch_port_t *); 92 int (* mtkswitch_port_vlan_get) (struct mtkswitch_softc *, 93 etherswitch_port_t *); 94 void (* mtkswitch_vlan_init_hw) (struct mtkswitch_softc *); 95 int (* mtkswitch_vlan_getvgroup) (struct mtkswitch_softc *, 96 etherswitch_vlangroup_t *); 97 int (* mtkswitch_vlan_setvgroup) (struct mtkswitch_softc *, 98 etherswitch_vlangroup_t *); 99 int (* mtkswitch_vlan_get_pvid) (struct mtkswitch_softc *, 100 int, int *); 101 int (* mtkswitch_vlan_set_pvid) (struct mtkswitch_softc *, 102 int, int); 103 104 /* PHY functions */ 105 int (* mtkswitch_phy_read) (device_t, int, int); 106 int (* mtkswitch_phy_write) (device_t, int, int, int); 107 108 /* Register functions */ 109 int (* mtkswitch_reg_read) (device_t, int); 110 int (* mtkswitch_reg_write) (device_t, int, int); 111 112 /* Internal register access functions */ 113 uint32_t (* mtkswitch_read) (struct mtkswitch_softc *, int); 114 uint32_t (* mtkswitch_write) (struct mtkswitch_softc *, int, 115 uint32_t); 116 } hal; 117 }; 118 119 #define MTKSWITCH_LOCK(_sc) \ 120 mtx_lock(&(_sc)->sc_mtx) 121 #define MTKSWITCH_UNLOCK(_sc) \ 122 mtx_unlock(&(_sc)->sc_mtx) 123 #define MTKSWITCH_LOCK_ASSERT(_sc, _what) \ 124 mtx_assert(&(_sc)->sc_mtx, (_what)) 125 #define MTKSWITCH_TRYLOCK(_sc) \ 126 mtx_trylock(&(_sc)->sc_mtx) 127 128 #define MTKSWITCH_READ(_sc, _reg) \ 129 bus_read_4((_sc)->sc_res, (_reg)) 130 #define MTKSWITCH_WRITE(_sc, _reg, _val) \ 131 bus_write_4((_sc)->sc_res, (_reg), (_val)) 132 #define MTKSWITCH_MOD(_sc, _reg, _clr, _set) \ 133 MTKSWITCH_WRITE((_sc), (_reg), \ 134 ((MTKSWITCH_READ((_sc), (_reg)) & ~(_clr)) | (_set)) 135 136 #define MTKSWITCH_REG32(addr) ((addr) & ~(0x3)) 137 #define MTKSWITCH_IS_HI16(addr) (((addr) & 0x3) > 0x1) 138 #define MTKSWITCH_HI16(x) (((x) >> 16) & 0xffff) 139 #define MTKSWITCH_LO16(x) ((x) & 0xffff) 140 #define MTKSWITCH_TO_HI16(x) (((x) & 0xffff) << 16) 141 #define MTKSWITCH_TO_LO16(x) ((x) & 0xffff) 142 #define MTKSWITCH_HI16_MSK 0xffff0000 143 #define MTKSWITCH_LO16_MSK 0x0000ffff 144 145 #if defined(DEBUG) 146 #define DPRINTF(dev, args...) device_printf(dev, args) 147 #define DEVERR(dev, err, fmt, args...) do { \ 148 if (err != 0) device_printf(dev, fmt, err, args); \ 149 } while (0) 150 #define DEBUG_INCRVAR(var) do { \ 151 var++; \ 152 } while (0) 153 #else 154 #define DPRINTF(dev, args...) 155 #define DEVERR(dev, err, fmt, args...) 156 #define DEBUG_INCRVAR(var) 157 #endif 158 159 extern void mtk_attach_switch_rt3050(struct mtkswitch_softc *); 160 extern void mtk_attach_switch_mt7620(struct mtkswitch_softc *); 161 162 #endif /* __MTKSWITCHVAR_H__ */ 163