xref: /freebsd/sys/dev/etherswitch/ip17x/ip175d.c (revision 95ee2897)
1248dd603SAdrian Chadd /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4248dd603SAdrian Chadd  * Copyright (c) 2013 Luiz Otavio O Souza.
5248dd603SAdrian Chadd  * Copyright (c) 2011-2012 Stefan Bethke.
6248dd603SAdrian Chadd  * Copyright (c) 2012 Adrian Chadd.
7248dd603SAdrian Chadd  * Copyright (C) 2008 Patrick Horn.
8248dd603SAdrian Chadd  * Copyright (C) 2008, 2010 Martin Mares.
9248dd603SAdrian Chadd  * All rights reserved.
10248dd603SAdrian Chadd  *
11248dd603SAdrian Chadd  * Redistribution and use in source and binary forms, with or without
12248dd603SAdrian Chadd  * modification, are permitted provided that the following conditions
13248dd603SAdrian Chadd  * are met:
14248dd603SAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
15248dd603SAdrian Chadd  *    notice, this list of conditions and the following disclaimer.
16248dd603SAdrian Chadd  * 2. Redistributions in binary form must reproduce the above copyright
17248dd603SAdrian Chadd  *    notice, this list of conditions and the following disclaimer in the
18248dd603SAdrian Chadd  *    documentation and/or other materials provided with the distribution.
19248dd603SAdrian Chadd  *
20248dd603SAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21248dd603SAdrian Chadd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22248dd603SAdrian Chadd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23248dd603SAdrian Chadd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24248dd603SAdrian Chadd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25248dd603SAdrian Chadd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26248dd603SAdrian Chadd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27248dd603SAdrian Chadd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28248dd603SAdrian Chadd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29248dd603SAdrian Chadd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30248dd603SAdrian Chadd  * SUCH DAMAGE.
31248dd603SAdrian Chadd  */
32248dd603SAdrian Chadd 
33248dd603SAdrian Chadd #include <sys/param.h>
34248dd603SAdrian Chadd #include <sys/bus.h>
35248dd603SAdrian Chadd #include <sys/kernel.h>
36248dd603SAdrian Chadd #include <sys/lock.h>
37248dd603SAdrian Chadd #include <sys/mutex.h>
38248dd603SAdrian Chadd #include <sys/systm.h>
39248dd603SAdrian Chadd #include <sys/socket.h>
40248dd603SAdrian Chadd 
41248dd603SAdrian Chadd #include <net/if.h>
42248dd603SAdrian Chadd 
43248dd603SAdrian Chadd #include <dev/mii/mii.h>
44248dd603SAdrian Chadd 
45248dd603SAdrian Chadd #include <dev/etherswitch/etherswitch.h>
46248dd603SAdrian Chadd #include <dev/etherswitch/ip17x/ip17x_phy.h>
47248dd603SAdrian Chadd #include <dev/etherswitch/ip17x/ip17x_reg.h>
48248dd603SAdrian Chadd #include <dev/etherswitch/ip17x/ip17x_var.h>
49248dd603SAdrian Chadd #include <dev/etherswitch/ip17x/ip17x_vlans.h>
50248dd603SAdrian Chadd #include <dev/etherswitch/ip17x/ip175d.h>
51248dd603SAdrian Chadd 
52248dd603SAdrian Chadd /*
53248dd603SAdrian Chadd  * IP175D specific functions.
54248dd603SAdrian Chadd  */
55248dd603SAdrian Chadd 
56248dd603SAdrian Chadd /*
57248dd603SAdrian Chadd  * Reset the switch to default state.
58248dd603SAdrian Chadd  */
59248dd603SAdrian Chadd static int
ip175d_reset(struct ip17x_softc * sc)60248dd603SAdrian Chadd ip175d_reset(struct ip17x_softc *sc)
61248dd603SAdrian Chadd {
62248dd603SAdrian Chadd 
63248dd603SAdrian Chadd 	/* Reset all the switch settings. */
64248dd603SAdrian Chadd 	ip17x_writephy(sc->sc_dev, IP175D_RESET_PHY, IP175D_RESET_REG, 0x175d);
656a7a25afSLuiz Otavio O Souza 	DELAY(2000);
66248dd603SAdrian Chadd 
67248dd603SAdrian Chadd 	/* Disable the special tagging mode. */
68248dd603SAdrian Chadd 	ip17x_updatephy(sc->sc_dev, 21, 22, 0x3, 0x0);
69248dd603SAdrian Chadd 
70248dd603SAdrian Chadd 	/* Set 802.1q protocol type. */
71248dd603SAdrian Chadd 	ip17x_writephy(sc->sc_dev, 22, 3, 0x8100);
72248dd603SAdrian Chadd 
73248dd603SAdrian Chadd 	return (0);
74248dd603SAdrian Chadd }
75248dd603SAdrian Chadd 
76248dd603SAdrian Chadd /*
77248dd603SAdrian Chadd  * Set the Switch configuration.
78248dd603SAdrian Chadd  */
79248dd603SAdrian Chadd static int
ip175d_hw_setup(struct ip17x_softc * sc)80248dd603SAdrian Chadd ip175d_hw_setup(struct ip17x_softc *sc)
81248dd603SAdrian Chadd {
82248dd603SAdrian Chadd 	struct ip17x_vlan *v;
83248dd603SAdrian Chadd 	uint32_t ports[IP17X_MAX_VLANS];
84248dd603SAdrian Chadd 	uint32_t addtag[IP17X_MAX_VLANS];
85248dd603SAdrian Chadd 	uint32_t striptag[IP17X_MAX_VLANS];
86248dd603SAdrian Chadd 	uint32_t vlan_mask;
87248dd603SAdrian Chadd 	int i, j;
88248dd603SAdrian Chadd 
89248dd603SAdrian Chadd 	vlan_mask = 0;
90248dd603SAdrian Chadd 	for (i = 0; i < IP17X_MAX_VLANS; i++) {
91248dd603SAdrian Chadd 
92248dd603SAdrian Chadd 		ports[i] = 0;
93248dd603SAdrian Chadd 		addtag[i] = 0;
94248dd603SAdrian Chadd 		striptag[i] = 0;
95248dd603SAdrian Chadd 
96248dd603SAdrian Chadd 		v = &sc->vlan[i];
97cc320e37SLuiz Otavio O Souza 		if ((v->vlanid & ETHERSWITCH_VID_VALID) == 0 ||
98cc320e37SLuiz Otavio O Souza 		    sc->vlan_mode == 0) {
99248dd603SAdrian Chadd 			/* Vlangroup disabled.  Reset the filter. */
100248dd603SAdrian Chadd 			ip17x_writephy(sc->sc_dev, 22, 14 + i, i + 1);
101248dd603SAdrian Chadd 			ports[i] = 0x3f;
102248dd603SAdrian Chadd 			continue;
103248dd603SAdrian Chadd 		}
104248dd603SAdrian Chadd 
105248dd603SAdrian Chadd 		vlan_mask |= (1 << i);
106248dd603SAdrian Chadd 		ports[i] = v->ports;
107248dd603SAdrian Chadd 
108248dd603SAdrian Chadd 		/* Setup the filter, write the VLAN id. */
109cc320e37SLuiz Otavio O Souza 		ip17x_writephy(sc->sc_dev, 22, 14 + i,
110cc320e37SLuiz Otavio O Souza 		    v->vlanid & ETHERSWITCH_VID_MASK);
111248dd603SAdrian Chadd 
112248dd603SAdrian Chadd 		for (j = 0; j < MII_NPHY; j++) {
113248dd603SAdrian Chadd 			if ((ports[i] & (1 << j)) == 0)
114248dd603SAdrian Chadd 				continue;
115248dd603SAdrian Chadd 			if (sc->addtag & (1 << j))
116248dd603SAdrian Chadd 				addtag[i] |= (1 << j);
117248dd603SAdrian Chadd 			if (sc->striptag & (1 << j))
118248dd603SAdrian Chadd 				striptag[i] |= (1 << j);
119248dd603SAdrian Chadd 		}
120248dd603SAdrian Chadd 	}
121248dd603SAdrian Chadd 
122248dd603SAdrian Chadd 	/* Write the port masks, tag adds and removals. */
123248dd603SAdrian Chadd 	for (i = 0; i < IP17X_MAX_VLANS / 2; i++) {
124248dd603SAdrian Chadd 		ip17x_writephy(sc->sc_dev, 23, i,
125248dd603SAdrian Chadd 		    ports[2 * i] | (ports[2 * i + 1] << 8));
126248dd603SAdrian Chadd 		ip17x_writephy(sc->sc_dev, 23, i + 8,
127248dd603SAdrian Chadd 		    addtag[2 * i] | (addtag[2 * i + 1] << 8));
128248dd603SAdrian Chadd 		ip17x_writephy(sc->sc_dev, 23, i + 16,
129248dd603SAdrian Chadd 		    striptag[2 * i] | (striptag[2 * i + 1] << 8));
130248dd603SAdrian Chadd 	}
131248dd603SAdrian Chadd 
132248dd603SAdrian Chadd 	/* Write the in use vlan mask. */
133248dd603SAdrian Chadd 	ip17x_writephy(sc->sc_dev, 22, 10, vlan_mask);
134248dd603SAdrian Chadd 
135248dd603SAdrian Chadd 	/* Write the PVID of each port. */
136248dd603SAdrian Chadd 	for (i = 0; i < sc->numports; i++)
137248dd603SAdrian Chadd 		ip17x_writephy(sc->sc_dev, 22, 4 + i, sc->pvid[i]);
138248dd603SAdrian Chadd 
139248dd603SAdrian Chadd 	return (0);
140248dd603SAdrian Chadd }
141248dd603SAdrian Chadd 
142248dd603SAdrian Chadd /*
143248dd603SAdrian Chadd  * Set the switch VLAN mode.
144248dd603SAdrian Chadd  */
145248dd603SAdrian Chadd static int
ip175d_set_vlan_mode(struct ip17x_softc * sc,uint32_t mode)146248dd603SAdrian Chadd ip175d_set_vlan_mode(struct ip17x_softc *sc, uint32_t mode)
147248dd603SAdrian Chadd {
148248dd603SAdrian Chadd 
149248dd603SAdrian Chadd 	switch (mode) {
150248dd603SAdrian Chadd 	case ETHERSWITCH_VLAN_DOT1Q:
151248dd603SAdrian Chadd 		/*
152248dd603SAdrian Chadd 		 * VLAN classification rules: tag-based VLANs,
153248dd603SAdrian Chadd 		 * use VID to classify, drop packets that cannot
154248dd603SAdrian Chadd 		 * be classified.
155248dd603SAdrian Chadd 		 */
156248dd603SAdrian Chadd 		ip17x_updatephy(sc->sc_dev, 22, 0, 0x3fff, 0x003f);
157248dd603SAdrian Chadd 		sc->vlan_mode = mode;
158248dd603SAdrian Chadd 		break;
159248dd603SAdrian Chadd 	case ETHERSWITCH_VLAN_PORT:
160248dd603SAdrian Chadd 		sc->vlan_mode = mode;
161248dd603SAdrian Chadd 		/* fallthrough */
162248dd603SAdrian Chadd 	default:
163248dd603SAdrian Chadd 		/*
164248dd603SAdrian Chadd 		 * VLAN classification rules: everything off &
165248dd603SAdrian Chadd 		 * clear table.
166248dd603SAdrian Chadd 		 */
167248dd603SAdrian Chadd 		ip17x_updatephy(sc->sc_dev, 22, 0, 0xbfff, 0x8000);
168248dd603SAdrian Chadd 		sc->vlan_mode = 0;
169248dd603SAdrian Chadd 		break;
17074b8d63dSPedro F. Giffuni 	}
171248dd603SAdrian Chadd 
172248dd603SAdrian Chadd 	if (sc->vlan_mode != 0) {
173248dd603SAdrian Chadd 		/*
174248dd603SAdrian Chadd 		 * Ingress rules: CFI=1 dropped, null VID is untagged, VID=1 passed,
175248dd603SAdrian Chadd 		 * VID=0xfff discarded, admin both tagged and untagged, ingress
176248dd603SAdrian Chadd 		 * filters enabled.
177248dd603SAdrian Chadd 		 */
178248dd603SAdrian Chadd 		ip17x_updatephy(sc->sc_dev, 22, 1, 0x0fff, 0x0c3f);
179248dd603SAdrian Chadd 
180248dd603SAdrian Chadd 		/* Egress rules: IGMP processing off, keep VLAN header off. */
181248dd603SAdrian Chadd 		ip17x_updatephy(sc->sc_dev, 22, 2, 0x0fff, 0x0000);
182248dd603SAdrian Chadd 	} else {
183248dd603SAdrian Chadd 		ip17x_updatephy(sc->sc_dev, 22, 1, 0x0fff, 0x043f);
184248dd603SAdrian Chadd 		ip17x_updatephy(sc->sc_dev, 22, 2, 0x0fff, 0x0020);
185248dd603SAdrian Chadd 	}
186248dd603SAdrian Chadd 
187248dd603SAdrian Chadd 	/* Reset vlans. */
188248dd603SAdrian Chadd 	ip17x_reset_vlans(sc, sc->vlan_mode);
189248dd603SAdrian Chadd 
190248dd603SAdrian Chadd 	/* Update switch configuration. */
191248dd603SAdrian Chadd 	ip175d_hw_setup(sc);
192248dd603SAdrian Chadd 
193248dd603SAdrian Chadd 	return (0);
194248dd603SAdrian Chadd }
195248dd603SAdrian Chadd 
196248dd603SAdrian Chadd /*
197248dd603SAdrian Chadd  * Get the switch VLAN mode.
198248dd603SAdrian Chadd  */
199248dd603SAdrian Chadd static int
ip175d_get_vlan_mode(struct ip17x_softc * sc)200248dd603SAdrian Chadd ip175d_get_vlan_mode(struct ip17x_softc *sc)
201248dd603SAdrian Chadd {
202248dd603SAdrian Chadd 
203248dd603SAdrian Chadd 	return (sc->vlan_mode);
204248dd603SAdrian Chadd }
205248dd603SAdrian Chadd 
206248dd603SAdrian Chadd void
ip175d_attach(struct ip17x_softc * sc)207248dd603SAdrian Chadd ip175d_attach(struct ip17x_softc *sc)
208248dd603SAdrian Chadd {
209248dd603SAdrian Chadd 
210248dd603SAdrian Chadd 	sc->hal.ip17x_reset = ip175d_reset;
211248dd603SAdrian Chadd 	sc->hal.ip17x_hw_setup = ip175d_hw_setup;
212248dd603SAdrian Chadd 	sc->hal.ip17x_get_vlan_mode = ip175d_get_vlan_mode;
213248dd603SAdrian Chadd 	sc->hal.ip17x_set_vlan_mode = ip175d_set_vlan_mode;
214248dd603SAdrian Chadd 
215248dd603SAdrian Chadd 	/* Defaults for IP175C. */
216248dd603SAdrian Chadd 	sc->cpuport = IP175X_CPU_PORT;
217248dd603SAdrian Chadd 	sc->numports = IP175X_NUM_PORTS;
218248dd603SAdrian Chadd 	sc->info.es_vlan_caps = ETHERSWITCH_VLAN_DOT1Q;
219248dd603SAdrian Chadd 
220248dd603SAdrian Chadd 	device_printf(sc->sc_dev, "type: IP175D\n");
221248dd603SAdrian Chadd }
222