2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 Luiz Otavio O Souza.
5 * Copyright (c) 2011-2012 Stefan Bethke.
6 * Copyright (c) 2012 Adrian Chadd.
7 * Copyright (C) 2008 Patrick Horn.
8 * Copyright (C) 2008, 2010 Martin Mares.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/param.h>
37 #include <sys/kernel.h>
39 #include <sys/mutex.h>
40 #include <sys/systm.h>
41 #include <sys/socket.h>
45 #include <dev/mii/mii.h>
47 #include <dev/etherswitch/etherswitch.h>
48 #include <dev/etherswitch/ip17x/ip17x_phy.h>
49 #include <dev/etherswitch/ip17x/ip17x_reg.h>
50 #include <dev/etherswitch/ip17x/ip17x_var.h>
51 #include <dev/etherswitch/ip17x/ip17x_vlans.h>
52 #include <dev/etherswitch/ip17x/ip175d.h>
55 * IP175D specific functions.
59 * Reset the switch to default state.
62 ip175d_reset(struct ip17x_softc *sc)
65 /* Reset all the switch settings. */
66 ip17x_writephy(sc->sc_dev, IP175D_RESET_PHY, IP175D_RESET_REG, 0x175d);
69 /* Disable the special tagging mode. */
70 ip17x_updatephy(sc->sc_dev, 21, 22, 0x3, 0x0);
72 /* Set 802.1q protocol type. */
73 ip17x_writephy(sc->sc_dev, 22, 3, 0x8100);
79 * Set the Switch configuration.
82 ip175d_hw_setup(struct ip17x_softc *sc)
85 uint32_t ports[IP17X_MAX_VLANS];
86 uint32_t addtag[IP17X_MAX_VLANS];
87 uint32_t striptag[IP17X_MAX_VLANS];
92 for (i = 0; i < IP17X_MAX_VLANS; i++) {
99 if ((v->vlanid & ETHERSWITCH_VID_VALID) == 0 ||
100 sc->vlan_mode == 0) {
101 /* Vlangroup disabled. Reset the filter. */
102 ip17x_writephy(sc->sc_dev, 22, 14 + i, i + 1);
107 vlan_mask |= (1 << i);
110 /* Setup the filter, write the VLAN id. */
111 ip17x_writephy(sc->sc_dev, 22, 14 + i,
112 v->vlanid & ETHERSWITCH_VID_MASK);
114 for (j = 0; j < MII_NPHY; j++) {
115 if ((ports[i] & (1 << j)) == 0)
117 if (sc->addtag & (1 << j))
118 addtag[i] |= (1 << j);
119 if (sc->striptag & (1 << j))
120 striptag[i] |= (1 << j);
124 /* Write the port masks, tag adds and removals. */
125 for (i = 0; i < IP17X_MAX_VLANS / 2; i++) {
126 ip17x_writephy(sc->sc_dev, 23, i,
127 ports[2 * i] | (ports[2 * i + 1] << 8));
128 ip17x_writephy(sc->sc_dev, 23, i + 8,
129 addtag[2 * i] | (addtag[2 * i + 1] << 8));
130 ip17x_writephy(sc->sc_dev, 23, i + 16,
131 striptag[2 * i] | (striptag[2 * i + 1] << 8));
134 /* Write the in use vlan mask. */
135 ip17x_writephy(sc->sc_dev, 22, 10, vlan_mask);
137 /* Write the PVID of each port. */
138 for (i = 0; i < sc->numports; i++)
139 ip17x_writephy(sc->sc_dev, 22, 4 + i, sc->pvid[i]);
145 * Set the switch VLAN mode.
148 ip175d_set_vlan_mode(struct ip17x_softc *sc, uint32_t mode)
152 case ETHERSWITCH_VLAN_DOT1Q:
154 * VLAN classification rules: tag-based VLANs,
155 * use VID to classify, drop packets that cannot
158 ip17x_updatephy(sc->sc_dev, 22, 0, 0x3fff, 0x003f);
159 sc->vlan_mode = mode;
161 case ETHERSWITCH_VLAN_PORT:
162 sc->vlan_mode = mode;
166 * VLAN classification rules: everything off &
169 ip17x_updatephy(sc->sc_dev, 22, 0, 0xbfff, 0x8000);
174 if (sc->vlan_mode != 0) {
176 * Ingress rules: CFI=1 dropped, null VID is untagged, VID=1 passed,
177 * VID=0xfff discarded, admin both tagged and untagged, ingress
180 ip17x_updatephy(sc->sc_dev, 22, 1, 0x0fff, 0x0c3f);
182 /* Egress rules: IGMP processing off, keep VLAN header off. */
183 ip17x_updatephy(sc->sc_dev, 22, 2, 0x0fff, 0x0000);
185 ip17x_updatephy(sc->sc_dev, 22, 1, 0x0fff, 0x043f);
186 ip17x_updatephy(sc->sc_dev, 22, 2, 0x0fff, 0x0020);
190 ip17x_reset_vlans(sc, sc->vlan_mode);
192 /* Update switch configuration. */
199 * Get the switch VLAN mode.
202 ip175d_get_vlan_mode(struct ip17x_softc *sc)
205 return (sc->vlan_mode);
209 ip175d_attach(struct ip17x_softc *sc)
212 sc->hal.ip17x_reset = ip175d_reset;
213 sc->hal.ip17x_hw_setup = ip175d_hw_setup;
214 sc->hal.ip17x_get_vlan_mode = ip175d_get_vlan_mode;
215 sc->hal.ip17x_set_vlan_mode = ip175d_set_vlan_mode;
217 /* Defaults for IP175C. */
218 sc->cpuport = IP175X_CPU_PORT;
219 sc->numports = IP175X_NUM_PORTS;
220 sc->info.es_vlan_caps = ETHERSWITCH_VLAN_DOT1Q;
222 device_printf(sc->sc_dev, "type: IP175D\n");