1248dd603SAdrian Chadd /*-
2*4d846d26SWarner 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 * All rights reserved.
8248dd603SAdrian Chadd *
9248dd603SAdrian Chadd * Redistribution and use in source and binary forms, with or without
10248dd603SAdrian Chadd * modification, are permitted provided that the following conditions
11248dd603SAdrian Chadd * are met:
12248dd603SAdrian Chadd * 1. Redistributions of source code must retain the above copyright
13248dd603SAdrian Chadd * notice, this list of conditions and the following disclaimer.
14248dd603SAdrian Chadd * 2. Redistributions in binary form must reproduce the above copyright
15248dd603SAdrian Chadd * notice, this list of conditions and the following disclaimer in the
16248dd603SAdrian Chadd * documentation and/or other materials provided with the distribution.
17248dd603SAdrian Chadd *
18248dd603SAdrian Chadd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19248dd603SAdrian Chadd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20248dd603SAdrian Chadd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21248dd603SAdrian Chadd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22248dd603SAdrian Chadd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23248dd603SAdrian Chadd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24248dd603SAdrian Chadd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25248dd603SAdrian Chadd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26248dd603SAdrian Chadd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27248dd603SAdrian Chadd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28248dd603SAdrian Chadd * SUCH DAMAGE.
29248dd603SAdrian Chadd */
30248dd603SAdrian Chadd
31248dd603SAdrian Chadd #include <sys/param.h>
32248dd603SAdrian Chadd #include <sys/bus.h>
33248dd603SAdrian Chadd #include <sys/kernel.h>
34248dd603SAdrian Chadd #include <sys/lock.h>
35248dd603SAdrian Chadd #include <sys/mutex.h>
36248dd603SAdrian Chadd #include <sys/systm.h>
37248dd603SAdrian Chadd #include <sys/socket.h>
38248dd603SAdrian Chadd
39248dd603SAdrian Chadd #include <net/if.h>
40248dd603SAdrian Chadd
41248dd603SAdrian Chadd #include <dev/mii/mii.h>
42248dd603SAdrian Chadd
43248dd603SAdrian Chadd #include <dev/etherswitch/etherswitch.h>
44248dd603SAdrian Chadd #include <dev/etherswitch/ip17x/ip17x_phy.h>
45248dd603SAdrian Chadd #include <dev/etherswitch/ip17x/ip17x_reg.h>
46248dd603SAdrian Chadd #include <dev/etherswitch/ip17x/ip17x_var.h>
47248dd603SAdrian Chadd #include <dev/etherswitch/ip17x/ip17x_vlans.h>
48248dd603SAdrian Chadd #include <dev/etherswitch/ip17x/ip175c.h>
49248dd603SAdrian Chadd
50248dd603SAdrian Chadd /*
51248dd603SAdrian Chadd * IP175C specific functions.
52248dd603SAdrian Chadd */
53248dd603SAdrian Chadd
54248dd603SAdrian Chadd /*
55248dd603SAdrian Chadd * Reset the switch.
56248dd603SAdrian Chadd */
57248dd603SAdrian Chadd static int
ip175c_reset(struct ip17x_softc * sc)58248dd603SAdrian Chadd ip175c_reset(struct ip17x_softc *sc)
59248dd603SAdrian Chadd {
60248dd603SAdrian Chadd uint32_t data;
61248dd603SAdrian Chadd
62248dd603SAdrian Chadd /* Reset all the switch settings. */
63248dd603SAdrian Chadd if (ip17x_writephy(sc->sc_dev, IP175C_RESET_PHY, IP175C_RESET_REG,
64248dd603SAdrian Chadd 0x175c))
65248dd603SAdrian Chadd return (-1);
666a7a25afSLuiz Otavio O Souza DELAY(2000);
67248dd603SAdrian Chadd
68248dd603SAdrian Chadd /* Force IP175C mode. */
69248dd603SAdrian Chadd data = ip17x_readphy(sc->sc_dev, IP175C_MODE_PHY, IP175C_MODE_REG);
70248dd603SAdrian Chadd if (data == 0x175a) {
71248dd603SAdrian Chadd if (ip17x_writephy(sc->sc_dev, IP175C_MODE_PHY, IP175C_MODE_REG,
72248dd603SAdrian Chadd 0x175c))
73248dd603SAdrian Chadd return (-1);
74248dd603SAdrian Chadd }
75248dd603SAdrian Chadd
76248dd603SAdrian Chadd return (0);
77248dd603SAdrian Chadd }
78248dd603SAdrian Chadd
79248dd603SAdrian Chadd static int
ip175c_port_vlan_setup(struct ip17x_softc * sc)80248dd603SAdrian Chadd ip175c_port_vlan_setup(struct ip17x_softc *sc)
81248dd603SAdrian Chadd {
82248dd603SAdrian Chadd struct ip17x_vlan *v;
83248dd603SAdrian Chadd uint32_t ports[IP175X_NUM_PORTS], reg[IP175X_NUM_PORTS/2];
84248dd603SAdrian Chadd int i, err, phy;
85248dd603SAdrian Chadd
86248dd603SAdrian Chadd KASSERT(sc->cpuport == 5, ("cpuport != 5 not supported for IP175C"));
87248dd603SAdrian Chadd KASSERT(sc->numports == 6, ("numports != 6 not supported for IP175C"));
88248dd603SAdrian Chadd
89248dd603SAdrian Chadd /* Build the port access masks. */
90248dd603SAdrian Chadd memset(ports, 0, sizeof(ports));
91248dd603SAdrian Chadd for (i = 0; i < sc->info.es_nports; i++) {
92248dd603SAdrian Chadd phy = sc->portphy[i];
93248dd603SAdrian Chadd v = &sc->vlan[i];
94248dd603SAdrian Chadd ports[phy] = v->ports;
95248dd603SAdrian Chadd }
96248dd603SAdrian Chadd
97248dd603SAdrian Chadd /* Move the cpuport bit to its correct place. */
98248dd603SAdrian Chadd for (i = 0; i < sc->numports; i++) {
99248dd603SAdrian Chadd if (ports[i] & (1 << sc->cpuport)) {
100248dd603SAdrian Chadd ports[i] |= (1 << 7);
101248dd603SAdrian Chadd ports[i] &= ~(1 << sc->cpuport);
102248dd603SAdrian Chadd }
103248dd603SAdrian Chadd }
104248dd603SAdrian Chadd
105248dd603SAdrian Chadd /* And now build the switch register data. */
106248dd603SAdrian Chadd memset(reg, 0, sizeof(reg));
107248dd603SAdrian Chadd for (i = 0; i < (sc->numports / 2); i++)
108248dd603SAdrian Chadd reg[i] = ports[i * 2] << 8 | ports[i * 2 + 1];
109248dd603SAdrian Chadd
110248dd603SAdrian Chadd /* Update the switch resgisters. */
111248dd603SAdrian Chadd err = ip17x_writephy(sc->sc_dev, 29, 19, reg[0]);
112248dd603SAdrian Chadd if (err == 0)
113248dd603SAdrian Chadd err = ip17x_writephy(sc->sc_dev, 29, 20, reg[1]);
114248dd603SAdrian Chadd if (err == 0)
115248dd603SAdrian Chadd err = ip17x_updatephy(sc->sc_dev, 29, 21, 0xff00, reg[2]);
116248dd603SAdrian Chadd if (err == 0)
117248dd603SAdrian Chadd err = ip17x_updatephy(sc->sc_dev, 30, 18, 0x00ff, reg[2]);
118248dd603SAdrian Chadd return (err);
119248dd603SAdrian Chadd }
120248dd603SAdrian Chadd
121248dd603SAdrian Chadd static int
ip175c_dot1q_vlan_setup(struct ip17x_softc * sc)122248dd603SAdrian Chadd ip175c_dot1q_vlan_setup(struct ip17x_softc *sc)
123248dd603SAdrian Chadd {
124248dd603SAdrian Chadd struct ip17x_vlan *v;
125248dd603SAdrian Chadd uint32_t data;
126248dd603SAdrian Chadd uint32_t vlans[IP17X_MAX_VLANS];
127248dd603SAdrian Chadd int i, j;
128248dd603SAdrian Chadd
129248dd603SAdrian Chadd KASSERT(sc->cpuport == 5, ("cpuport != 5 not supported for IP175C"));
130248dd603SAdrian Chadd KASSERT(sc->numports == 6, ("numports != 6 not supported for IP175C"));
131248dd603SAdrian Chadd
132248dd603SAdrian Chadd /* Add and strip VLAN tags. */
133248dd603SAdrian Chadd data = (sc->addtag & ~(1 << IP175X_CPU_PORT)) << 11;
134248dd603SAdrian Chadd data |= (sc->striptag & ~(1 << IP175X_CPU_PORT)) << 6;
135248dd603SAdrian Chadd if (sc->addtag & (1 << IP175X_CPU_PORT))
136248dd603SAdrian Chadd data |= (1 << 1);
137248dd603SAdrian Chadd if (sc->striptag & (1 << IP175X_CPU_PORT))
138248dd603SAdrian Chadd data |= (1 << 0);
139248dd603SAdrian Chadd if (ip17x_writephy(sc->sc_dev, 29, 23, data))
140248dd603SAdrian Chadd return (-1);
141248dd603SAdrian Chadd
142248dd603SAdrian Chadd /* Set the VID_IDX_SEL to 0. */
143248dd603SAdrian Chadd if (ip17x_updatephy(sc->sc_dev, 30, 9, 0x70, 0))
144248dd603SAdrian Chadd return (-1);
145248dd603SAdrian Chadd
146248dd603SAdrian Chadd /* Calculate the port masks. */
147248dd603SAdrian Chadd memset(vlans, 0, sizeof(vlans));
148248dd603SAdrian Chadd for (i = 0; i < IP17X_MAX_VLANS; i++) {
149248dd603SAdrian Chadd v = &sc->vlan[i];
150cc320e37SLuiz Otavio O Souza if ((v->vlanid & ETHERSWITCH_VID_VALID) == 0)
151248dd603SAdrian Chadd continue;
152cc320e37SLuiz Otavio O Souza vlans[v->vlanid & ETHERSWITCH_VID_MASK] = v->ports;
153248dd603SAdrian Chadd }
154248dd603SAdrian Chadd
155248dd603SAdrian Chadd for (j = 0, i = 1; i <= IP17X_MAX_VLANS / 2; i++) {
156248dd603SAdrian Chadd data = vlans[j++] & 0x3f;
157248dd603SAdrian Chadd data |= (vlans[j++] & 0x3f) << 8;
158248dd603SAdrian Chadd if (ip17x_writephy(sc->sc_dev, 30, i, data))
159248dd603SAdrian Chadd return (-1);
160248dd603SAdrian Chadd }
161248dd603SAdrian Chadd
162248dd603SAdrian Chadd /* Port default VLAN ID. */
163248dd603SAdrian Chadd for (i = 0; i < sc->numports; i++) {
164248dd603SAdrian Chadd if (i == IP175X_CPU_PORT) {
165248dd603SAdrian Chadd if (ip17x_writephy(sc->sc_dev, 29, 30, sc->pvid[i]))
166248dd603SAdrian Chadd return (-1);
167248dd603SAdrian Chadd } else {
168248dd603SAdrian Chadd if (ip17x_writephy(sc->sc_dev, 29, 24 + i, sc->pvid[i]))
169248dd603SAdrian Chadd return (-1);
170248dd603SAdrian Chadd }
171248dd603SAdrian Chadd }
172248dd603SAdrian Chadd
173248dd603SAdrian Chadd return (0);
174248dd603SAdrian Chadd }
175248dd603SAdrian Chadd
176248dd603SAdrian Chadd /*
177248dd603SAdrian Chadd * Set the Switch configuration.
178248dd603SAdrian Chadd */
179248dd603SAdrian Chadd static int
ip175c_hw_setup(struct ip17x_softc * sc)180248dd603SAdrian Chadd ip175c_hw_setup(struct ip17x_softc *sc)
181248dd603SAdrian Chadd {
182248dd603SAdrian Chadd
183248dd603SAdrian Chadd switch (sc->vlan_mode) {
184248dd603SAdrian Chadd case ETHERSWITCH_VLAN_PORT:
185248dd603SAdrian Chadd return (ip175c_port_vlan_setup(sc));
186248dd603SAdrian Chadd break;
187248dd603SAdrian Chadd case ETHERSWITCH_VLAN_DOT1Q:
188248dd603SAdrian Chadd return (ip175c_dot1q_vlan_setup(sc));
189248dd603SAdrian Chadd break;
190248dd603SAdrian Chadd }
191248dd603SAdrian Chadd return (-1);
192248dd603SAdrian Chadd }
193248dd603SAdrian Chadd
194248dd603SAdrian Chadd /*
195248dd603SAdrian Chadd * Set the switch VLAN mode.
196248dd603SAdrian Chadd */
197248dd603SAdrian Chadd static int
ip175c_set_vlan_mode(struct ip17x_softc * sc,uint32_t mode)198248dd603SAdrian Chadd ip175c_set_vlan_mode(struct ip17x_softc *sc, uint32_t mode)
199248dd603SAdrian Chadd {
200248dd603SAdrian Chadd
201248dd603SAdrian Chadd switch (mode) {
202248dd603SAdrian Chadd case ETHERSWITCH_VLAN_DOT1Q:
203248dd603SAdrian Chadd /* Enable VLAN tag processing. */
204248dd603SAdrian Chadd ip17x_updatephy(sc->sc_dev, 30, 9, 0x80, 0x80);
205248dd603SAdrian Chadd sc->vlan_mode = mode;
206248dd603SAdrian Chadd break;
207248dd603SAdrian Chadd case ETHERSWITCH_VLAN_PORT:
208248dd603SAdrian Chadd default:
209248dd603SAdrian Chadd /* Disable VLAN tag processing. */
210248dd603SAdrian Chadd ip17x_updatephy(sc->sc_dev, 30, 9, 0x80, 0);
211248dd603SAdrian Chadd sc->vlan_mode = ETHERSWITCH_VLAN_PORT;
212248dd603SAdrian Chadd break;
21374b8d63dSPedro F. Giffuni }
214248dd603SAdrian Chadd
215248dd603SAdrian Chadd /* Reset vlans. */
216248dd603SAdrian Chadd ip17x_reset_vlans(sc, sc->vlan_mode);
217248dd603SAdrian Chadd
218248dd603SAdrian Chadd /* Update switch configuration. */
219248dd603SAdrian Chadd ip175c_hw_setup(sc);
220248dd603SAdrian Chadd
221248dd603SAdrian Chadd return (0);
222248dd603SAdrian Chadd }
223248dd603SAdrian Chadd
224248dd603SAdrian Chadd /*
225248dd603SAdrian Chadd * Get the switch VLAN mode.
226248dd603SAdrian Chadd */
227248dd603SAdrian Chadd static int
ip175c_get_vlan_mode(struct ip17x_softc * sc)228248dd603SAdrian Chadd ip175c_get_vlan_mode(struct ip17x_softc *sc)
229248dd603SAdrian Chadd {
230248dd603SAdrian Chadd
231248dd603SAdrian Chadd return (sc->vlan_mode);
232248dd603SAdrian Chadd }
233248dd603SAdrian Chadd
234248dd603SAdrian Chadd void
ip175c_attach(struct ip17x_softc * sc)235248dd603SAdrian Chadd ip175c_attach(struct ip17x_softc *sc)
236248dd603SAdrian Chadd {
2372567d713SMichael Zhilin uint32_t data;
2382567d713SMichael Zhilin
2392567d713SMichael Zhilin data = ip17x_readphy(sc->sc_dev, IP175C_MII_PHY, IP175C_MII_CTL_REG);
2402567d713SMichael Zhilin device_printf(sc->sc_dev, "MII: %x\n", data);
2412567d713SMichael Zhilin /* check mii1 interface if disabled then phy4 and mac4 hold on switch */
2422567d713SMichael Zhilin if((data & (1 << IP175C_MII_MII1_RMII_EN)) == 0)
2432567d713SMichael Zhilin sc->phymask |= 0x10;
244248dd603SAdrian Chadd
245248dd603SAdrian Chadd sc->hal.ip17x_reset = ip175c_reset;
246248dd603SAdrian Chadd sc->hal.ip17x_hw_setup = ip175c_hw_setup;
247248dd603SAdrian Chadd sc->hal.ip17x_get_vlan_mode = ip175c_get_vlan_mode;
248248dd603SAdrian Chadd sc->hal.ip17x_set_vlan_mode = ip175c_set_vlan_mode;
249248dd603SAdrian Chadd
250248dd603SAdrian Chadd /* Defaults for IP175C. */
251248dd603SAdrian Chadd sc->cpuport = IP175X_CPU_PORT;
252248dd603SAdrian Chadd sc->numports = IP175X_NUM_PORTS;
253248dd603SAdrian Chadd sc->info.es_vlan_caps = ETHERSWITCH_VLAN_PORT | ETHERSWITCH_VLAN_DOT1Q;
254248dd603SAdrian Chadd
255248dd603SAdrian Chadd device_printf(sc->sc_dev, "type: IP175C\n");
256248dd603SAdrian Chadd }
257